]> granicus.if.org Git - zfs/blob - module/zfs/zfs_log.c
Fix 0 byte memory leak in zfs receive
[zfs] / module / zfs / zfs_log.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2015 by Delphix. All rights reserved.
24  */
25
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/sysmacros.h>
30 #include <sys/cmn_err.h>
31 #include <sys/kmem.h>
32 #include <sys/thread.h>
33 #include <sys/file.h>
34 #include <sys/vfs.h>
35 #include <sys/zfs_znode.h>
36 #include <sys/zfs_dir.h>
37 #include <sys/zil.h>
38 #include <sys/zil_impl.h>
39 #include <sys/byteorder.h>
40 #include <sys/policy.h>
41 #include <sys/stat.h>
42 #include <sys/mode.h>
43 #include <sys/acl.h>
44 #include <sys/dmu.h>
45 #include <sys/spa.h>
46 #include <sys/zfs_fuid.h>
47 #include <sys/dsl_dataset.h>
48
49 /*
50  * These zfs_log_* functions must be called within a dmu tx, in one
51  * of 2 contexts depending on zilog->z_replay:
52  *
53  * Non replay mode
54  * ---------------
55  * We need to record the transaction so that if it is committed to
56  * the Intent Log then it can be replayed.  An intent log transaction
57  * structure (itx_t) is allocated and all the information necessary to
58  * possibly replay the transaction is saved in it. The itx is then assigned
59  * a sequence number and inserted in the in-memory list anchored in the zilog.
60  *
61  * Replay mode
62  * -----------
63  * We need to mark the intent log record as replayed in the log header.
64  * This is done in the same transaction as the replay so that they
65  * commit atomically.
66  */
67
68 int
69 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
70 {
71         int isxvattr = (vap->va_mask & ATTR_XVATTR);
72         switch (type) {
73         case Z_FILE:
74                 if (vsecp == NULL && !isxvattr)
75                         return (TX_CREATE);
76                 if (vsecp && isxvattr)
77                         return (TX_CREATE_ACL_ATTR);
78                 if (vsecp)
79                         return (TX_CREATE_ACL);
80                 else
81                         return (TX_CREATE_ATTR);
82                 /*NOTREACHED*/
83         case Z_DIR:
84                 if (vsecp == NULL && !isxvattr)
85                         return (TX_MKDIR);
86                 if (vsecp && isxvattr)
87                         return (TX_MKDIR_ACL_ATTR);
88                 if (vsecp)
89                         return (TX_MKDIR_ACL);
90                 else
91                         return (TX_MKDIR_ATTR);
92         case Z_XATTRDIR:
93                 return (TX_MKXATTR);
94         }
95         ASSERT(0);
96         return (TX_MAX_TYPE);
97 }
98
99 /*
100  * build up the log data necessary for logging xvattr_t
101  * First lr_attr_t is initialized.  following the lr_attr_t
102  * is the mapsize and attribute bitmap copied from the xvattr_t.
103  * Following the bitmap and bitmapsize two 64 bit words are reserved
104  * for the create time which may be set.  Following the create time
105  * records a single 64 bit integer which has the bits to set on
106  * replay for the xvattr.
107  */
108 static void
109 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
110 {
111         uint32_t        *bitmap;
112         uint64_t        *attrs;
113         uint64_t        *crtime;
114         xoptattr_t      *xoap;
115         void            *scanstamp;
116         int             i;
117
118         xoap = xva_getxoptattr(xvap);
119         ASSERT(xoap);
120
121         lrattr->lr_attr_masksize = xvap->xva_mapsize;
122         bitmap = &lrattr->lr_attr_bitmap;
123         for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
124                 *bitmap = xvap->xva_reqattrmap[i];
125         }
126
127         /* Now pack the attributes up in a single uint64_t */
128         attrs = (uint64_t *)bitmap;
129         crtime = attrs + 1;
130         scanstamp = (caddr_t)(crtime + 2);
131         *attrs = 0;
132         if (XVA_ISSET_REQ(xvap, XAT_READONLY))
133                 *attrs |= (xoap->xoa_readonly == 0) ? 0 :
134                     XAT0_READONLY;
135         if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
136                 *attrs |= (xoap->xoa_hidden == 0) ? 0 :
137                     XAT0_HIDDEN;
138         if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
139                 *attrs |= (xoap->xoa_system == 0) ? 0 :
140                     XAT0_SYSTEM;
141         if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
142                 *attrs |= (xoap->xoa_archive == 0) ? 0 :
143                     XAT0_ARCHIVE;
144         if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
145                 *attrs |= (xoap->xoa_immutable == 0) ? 0 :
146                     XAT0_IMMUTABLE;
147         if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
148                 *attrs |= (xoap->xoa_nounlink == 0) ? 0 :
149                     XAT0_NOUNLINK;
150         if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
151                 *attrs |= (xoap->xoa_appendonly == 0) ? 0 :
152                     XAT0_APPENDONLY;
153         if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
154                 *attrs |= (xoap->xoa_opaque == 0) ? 0 :
155                     XAT0_APPENDONLY;
156         if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
157                 *attrs |= (xoap->xoa_nodump == 0) ? 0 :
158                     XAT0_NODUMP;
159         if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
160                 *attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
161                     XAT0_AV_QUARANTINED;
162         if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
163                 *attrs |= (xoap->xoa_av_modified == 0) ? 0 :
164                     XAT0_AV_MODIFIED;
165         if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
166                 ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
167         if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
168                 ASSERT(!XVA_ISSET_REQ(xvap, XAT_PROJID));
169
170                 bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
171         } else if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
172                 /*
173                  * XAT_PROJID and XAT_AV_SCANSTAMP will never be valid
174                  * at the same time, so we can share the same space.
175                  */
176                 bcopy(&xoap->xoa_projid, scanstamp, sizeof (uint64_t));
177         }
178         if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
179                 *attrs |= (xoap->xoa_reparse == 0) ? 0 :
180                     XAT0_REPARSE;
181         if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
182                 *attrs |= (xoap->xoa_offline == 0) ? 0 :
183                     XAT0_OFFLINE;
184         if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
185                 *attrs |= (xoap->xoa_sparse == 0) ? 0 :
186                     XAT0_SPARSE;
187         if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT))
188                 *attrs |= (xoap->xoa_projinherit == 0) ? 0 :
189                     XAT0_PROJINHERIT;
190 }
191
192 static void *
193 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
194 {
195         zfs_fuid_t *zfuid;
196         uint64_t *fuidloc = start;
197
198         /* First copy in the ACE FUIDs */
199         for (zfuid = list_head(&fuidp->z_fuids); zfuid;
200             zfuid = list_next(&fuidp->z_fuids, zfuid)) {
201                 *fuidloc++ = zfuid->z_logfuid;
202         }
203         return (fuidloc);
204 }
205
206
207 static void *
208 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
209 {
210         zfs_fuid_domain_t *zdomain;
211
212         /* now copy in the domain info, if any */
213         if (fuidp->z_domain_str_sz != 0) {
214                 for (zdomain = list_head(&fuidp->z_domains); zdomain;
215                     zdomain = list_next(&fuidp->z_domains, zdomain)) {
216                         bcopy((void *)zdomain->z_domain, start,
217                             strlen(zdomain->z_domain) + 1);
218                         start = (caddr_t)start +
219                             strlen(zdomain->z_domain) + 1;
220                 }
221         }
222         return (start);
223 }
224
225 /*
226  * If zp is an xattr node, check whether the xattr owner is unlinked.
227  * We don't want to log anything if the owner is unlinked.
228  */
229 static int
230 zfs_xattr_owner_unlinked(znode_t *zp)
231 {
232         int unlinked = 0;
233         znode_t *dzp;
234         igrab(ZTOI(zp));
235         /*
236          * if zp is XATTR node, keep walking up via z_xattr_parent until we
237          * get the owner
238          */
239         while (zp->z_pflags & ZFS_XATTR) {
240                 ASSERT3U(zp->z_xattr_parent, !=, 0);
241                 if (zfs_zget(ZTOZSB(zp), zp->z_xattr_parent, &dzp) != 0) {
242                         unlinked = 1;
243                         break;
244                 }
245                 iput(ZTOI(zp));
246                 zp = dzp;
247                 unlinked = zp->z_unlinked;
248         }
249         iput(ZTOI(zp));
250         return (unlinked);
251 }
252
253 /*
254  * Handles TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, TX_MKDIR_ATTR and
255  * TK_MKXATTR transactions.
256  *
257  * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
258  * domain information appended prior to the name.  In this case the
259  * uid/gid in the log record will be a log centric FUID.
260  *
261  * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
262  * may contain attributes, ACL and optional fuid information.
263  *
264  * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
265  * and ACL and normal users/groups in the ACEs.
266  *
267  * There may be an optional xvattr attribute information similar
268  * to zfs_log_setattr.
269  *
270  * Also, after the file name "domain" strings may be appended.
271  */
272 void
273 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
274     znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
275     zfs_fuid_info_t *fuidp, vattr_t *vap)
276 {
277         itx_t *itx;
278         lr_create_t *lr;
279         lr_acl_create_t *lracl;
280         size_t aclsize = 0;
281         size_t xvatsize = 0;
282         size_t txsize;
283         xvattr_t *xvap = (xvattr_t *)vap;
284         void *end;
285         size_t lrsize;
286         size_t namesize = strlen(name) + 1;
287         size_t fuidsz = 0;
288
289         if (zil_replaying(zilog, tx) || zfs_xattr_owner_unlinked(dzp))
290                 return;
291
292         /*
293          * If we have FUIDs present then add in space for
294          * domains and ACE fuid's if any.
295          */
296         if (fuidp) {
297                 fuidsz += fuidp->z_domain_str_sz;
298                 fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
299         }
300
301         if (vap->va_mask & ATTR_XVATTR)
302                 xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
303
304         if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
305             (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
306             (int)txtype == TX_MKXATTR) {
307                 txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
308                 lrsize = sizeof (*lr);
309         } else {
310                 txsize =
311                     sizeof (lr_acl_create_t) + namesize + fuidsz +
312                     ZIL_ACE_LENGTH(aclsize) + xvatsize;
313                 lrsize = sizeof (lr_acl_create_t);
314         }
315
316         itx = zil_itx_create(txtype, txsize);
317
318         lr = (lr_create_t *)&itx->itx_lr;
319         lr->lr_doid = dzp->z_id;
320         lr->lr_foid = zp->z_id;
321         /* Store dnode slot count in 8 bits above object id. */
322         LR_FOID_SET_SLOTS(lr->lr_foid, zp->z_dnodesize >> DNODE_SHIFT);
323         lr->lr_mode = zp->z_mode;
324         if (!IS_EPHEMERAL(KUID_TO_SUID(ZTOI(zp)->i_uid))) {
325                 lr->lr_uid = (uint64_t)KUID_TO_SUID(ZTOI(zp)->i_uid);
326         } else {
327                 lr->lr_uid = fuidp->z_fuid_owner;
328         }
329         if (!IS_EPHEMERAL(KGID_TO_SGID(ZTOI(zp)->i_gid))) {
330                 lr->lr_gid = (uint64_t)KGID_TO_SGID(ZTOI(zp)->i_gid);
331         } else {
332                 lr->lr_gid = fuidp->z_fuid_group;
333         }
334         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
335             sizeof (uint64_t));
336         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
337             lr->lr_crtime, sizeof (uint64_t) * 2);
338
339         if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(ZTOZSB(zp)), &lr->lr_rdev,
340             sizeof (lr->lr_rdev)) != 0)
341                 lr->lr_rdev = 0;
342
343         /*
344          * Fill in xvattr info if any
345          */
346         if (vap->va_mask & ATTR_XVATTR) {
347                 zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
348                 end = (caddr_t)lr + lrsize + xvatsize;
349         } else {
350                 end = (caddr_t)lr + lrsize;
351         }
352
353         /* Now fill in any ACL info */
354
355         if (vsecp) {
356                 lracl = (lr_acl_create_t *)&itx->itx_lr;
357                 lracl->lr_aclcnt = vsecp->vsa_aclcnt;
358                 lracl->lr_acl_bytes = aclsize;
359                 lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
360                 lracl->lr_fuidcnt  = fuidp ? fuidp->z_fuid_cnt : 0;
361                 if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
362                         lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
363                 else
364                         lracl->lr_acl_flags = 0;
365
366                 bcopy(vsecp->vsa_aclentp, end, aclsize);
367                 end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
368         }
369
370         /* drop in FUID info */
371         if (fuidp) {
372                 end = zfs_log_fuid_ids(fuidp, end);
373                 end = zfs_log_fuid_domains(fuidp, end);
374         }
375         /*
376          * Now place file name in log record
377          */
378         bcopy(name, end, namesize);
379
380         zil_itx_assign(zilog, itx, tx);
381 }
382
383 /*
384  * Handles both TX_REMOVE and TX_RMDIR transactions.
385  */
386 void
387 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
388     znode_t *dzp, char *name, uint64_t foid)
389 {
390         itx_t *itx;
391         lr_remove_t *lr;
392         size_t namesize = strlen(name) + 1;
393
394         if (zil_replaying(zilog, tx) || zfs_xattr_owner_unlinked(dzp))
395                 return;
396
397         itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
398         lr = (lr_remove_t *)&itx->itx_lr;
399         lr->lr_doid = dzp->z_id;
400         bcopy(name, (char *)(lr + 1), namesize);
401
402         itx->itx_oid = foid;
403
404         zil_itx_assign(zilog, itx, tx);
405 }
406
407 /*
408  * Handles TX_LINK transactions.
409  */
410 void
411 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
412     znode_t *dzp, znode_t *zp, char *name)
413 {
414         itx_t *itx;
415         lr_link_t *lr;
416         size_t namesize = strlen(name) + 1;
417
418         if (zil_replaying(zilog, tx))
419                 return;
420
421         itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
422         lr = (lr_link_t *)&itx->itx_lr;
423         lr->lr_doid = dzp->z_id;
424         lr->lr_link_obj = zp->z_id;
425         bcopy(name, (char *)(lr + 1), namesize);
426
427         zil_itx_assign(zilog, itx, tx);
428 }
429
430 /*
431  * Handles TX_SYMLINK transactions.
432  */
433 void
434 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
435     znode_t *dzp, znode_t *zp, char *name, char *link)
436 {
437         itx_t *itx;
438         lr_create_t *lr;
439         size_t namesize = strlen(name) + 1;
440         size_t linksize = strlen(link) + 1;
441
442         if (zil_replaying(zilog, tx))
443                 return;
444
445         itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
446         lr = (lr_create_t *)&itx->itx_lr;
447         lr->lr_doid = dzp->z_id;
448         lr->lr_foid = zp->z_id;
449         lr->lr_uid = KUID_TO_SUID(ZTOI(zp)->i_uid);
450         lr->lr_gid = KGID_TO_SGID(ZTOI(zp)->i_gid);
451         lr->lr_mode = zp->z_mode;
452         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
453             sizeof (uint64_t));
454         (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
455             lr->lr_crtime, sizeof (uint64_t) * 2);
456         bcopy(name, (char *)(lr + 1), namesize);
457         bcopy(link, (char *)(lr + 1) + namesize, linksize);
458
459         zil_itx_assign(zilog, itx, tx);
460 }
461
462 /*
463  * Handles TX_RENAME transactions.
464  */
465 void
466 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
467     znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
468 {
469         itx_t *itx;
470         lr_rename_t *lr;
471         size_t snamesize = strlen(sname) + 1;
472         size_t dnamesize = strlen(dname) + 1;
473
474         if (zil_replaying(zilog, tx))
475                 return;
476
477         itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
478         lr = (lr_rename_t *)&itx->itx_lr;
479         lr->lr_sdoid = sdzp->z_id;
480         lr->lr_tdoid = tdzp->z_id;
481         bcopy(sname, (char *)(lr + 1), snamesize);
482         bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
483         itx->itx_oid = szp->z_id;
484
485         zil_itx_assign(zilog, itx, tx);
486 }
487
488 /*
489  * zfs_log_write() handles TX_WRITE transactions. The specified callback is
490  * called as soon as the write is on stable storage (be it via a DMU sync or a
491  * ZIL commit).
492  */
493 long zfs_immediate_write_sz = 32768;
494
495 void
496 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
497     znode_t *zp, offset_t off, ssize_t resid, int ioflag,
498     zil_callback_t callback, void *callback_data)
499 {
500         uint32_t blocksize = zp->z_blksz;
501         itx_wr_state_t write_state;
502         uintptr_t fsync_cnt;
503
504         if (zil_replaying(zilog, tx) || zp->z_unlinked ||
505             zfs_xattr_owner_unlinked(zp)) {
506                 if (callback != NULL)
507                         callback(callback_data);
508                 return;
509         }
510
511         if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
512                 write_state = WR_INDIRECT;
513         else if (!spa_has_slogs(zilog->zl_spa) &&
514             resid >= zfs_immediate_write_sz)
515                 write_state = WR_INDIRECT;
516         else if (ioflag & (FSYNC | FDSYNC))
517                 write_state = WR_COPIED;
518         else
519                 write_state = WR_NEED_COPY;
520
521         if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
522                 (void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
523         }
524
525         while (resid) {
526                 itx_t *itx;
527                 lr_write_t *lr;
528                 itx_wr_state_t wr_state = write_state;
529                 ssize_t len = resid;
530
531                 if (wr_state == WR_COPIED && resid > ZIL_MAX_COPIED_DATA)
532                         wr_state = WR_NEED_COPY;
533                 else if (wr_state == WR_INDIRECT)
534                         len = MIN(blocksize - P2PHASE(off, blocksize), resid);
535
536                 itx = zil_itx_create(txtype, sizeof (*lr) +
537                     (wr_state == WR_COPIED ? len : 0));
538                 lr = (lr_write_t *)&itx->itx_lr;
539                 if (wr_state == WR_COPIED && dmu_read(ZTOZSB(zp)->z_os,
540                     zp->z_id, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
541                         zil_itx_destroy(itx);
542                         itx = zil_itx_create(txtype, sizeof (*lr));
543                         lr = (lr_write_t *)&itx->itx_lr;
544                         wr_state = WR_NEED_COPY;
545                 }
546
547                 itx->itx_wr_state = wr_state;
548                 lr->lr_foid = zp->z_id;
549                 lr->lr_offset = off;
550                 lr->lr_length = len;
551                 lr->lr_blkoff = 0;
552                 BP_ZERO(&lr->lr_blkptr);
553
554                 itx->itx_private = ZTOZSB(zp);
555
556                 if (!(ioflag & (FSYNC | FDSYNC)) && (zp->z_sync_cnt == 0) &&
557                     (fsync_cnt == 0))
558                         itx->itx_sync = B_FALSE;
559
560                 itx->itx_callback = callback;
561                 itx->itx_callback_data = callback_data;
562                 zil_itx_assign(zilog, itx, tx);
563
564                 off += len;
565                 resid -= len;
566         }
567 }
568
569 /*
570  * Handles TX_TRUNCATE transactions.
571  */
572 void
573 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
574     znode_t *zp, uint64_t off, uint64_t len)
575 {
576         itx_t *itx;
577         lr_truncate_t *lr;
578
579         if (zil_replaying(zilog, tx) || zp->z_unlinked ||
580             zfs_xattr_owner_unlinked(zp))
581                 return;
582
583         itx = zil_itx_create(txtype, sizeof (*lr));
584         lr = (lr_truncate_t *)&itx->itx_lr;
585         lr->lr_foid = zp->z_id;
586         lr->lr_offset = off;
587         lr->lr_length = len;
588
589         itx->itx_sync = (zp->z_sync_cnt != 0);
590         zil_itx_assign(zilog, itx, tx);
591 }
592
593 /*
594  * Handles TX_SETATTR transactions.
595  */
596 void
597 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
598     znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
599 {
600         itx_t           *itx;
601         lr_setattr_t    *lr;
602         xvattr_t        *xvap = (xvattr_t *)vap;
603         size_t          recsize = sizeof (lr_setattr_t);
604         void            *start;
605
606         if (zil_replaying(zilog, tx) || zp->z_unlinked)
607                 return;
608
609         /*
610          * If XVATTR set, then log record size needs to allow
611          * for lr_attr_t + xvattr mask, mapsize and create time
612          * plus actual attribute values
613          */
614         if (vap->va_mask & ATTR_XVATTR)
615                 recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
616
617         if (fuidp)
618                 recsize += fuidp->z_domain_str_sz;
619
620         itx = zil_itx_create(txtype, recsize);
621         lr = (lr_setattr_t *)&itx->itx_lr;
622         lr->lr_foid = zp->z_id;
623         lr->lr_mask = (uint64_t)mask_applied;
624         lr->lr_mode = (uint64_t)vap->va_mode;
625         if ((mask_applied & ATTR_UID) && IS_EPHEMERAL(vap->va_uid))
626                 lr->lr_uid = fuidp->z_fuid_owner;
627         else
628                 lr->lr_uid = (uint64_t)vap->va_uid;
629
630         if ((mask_applied & ATTR_GID) && IS_EPHEMERAL(vap->va_gid))
631                 lr->lr_gid = fuidp->z_fuid_group;
632         else
633                 lr->lr_gid = (uint64_t)vap->va_gid;
634
635         lr->lr_size = (uint64_t)vap->va_size;
636         ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
637         ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
638         start = (lr_setattr_t *)(lr + 1);
639         if (vap->va_mask & ATTR_XVATTR) {
640                 zfs_log_xvattr((lr_attr_t *)start, xvap);
641                 start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
642         }
643
644         /*
645          * Now stick on domain information if any on end
646          */
647
648         if (fuidp)
649                 (void) zfs_log_fuid_domains(fuidp, start);
650
651         itx->itx_sync = (zp->z_sync_cnt != 0);
652         zil_itx_assign(zilog, itx, tx);
653 }
654
655 /*
656  * Handles TX_ACL transactions.
657  */
658 void
659 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
660     vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
661 {
662         itx_t *itx;
663         lr_acl_v0_t *lrv0;
664         lr_acl_t *lr;
665         int txtype;
666         int lrsize;
667         size_t txsize;
668         size_t aclbytes = vsecp->vsa_aclentsz;
669
670         if (zil_replaying(zilog, tx) || zp->z_unlinked)
671                 return;
672
673         txtype = (ZTOZSB(zp)->z_version < ZPL_VERSION_FUID) ?
674             TX_ACL_V0 : TX_ACL;
675
676         if (txtype == TX_ACL)
677                 lrsize = sizeof (*lr);
678         else
679                 lrsize = sizeof (*lrv0);
680
681         txsize = lrsize +
682             ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
683             (fuidp ? fuidp->z_domain_str_sz : 0) +
684             sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
685
686         itx = zil_itx_create(txtype, txsize);
687
688         lr = (lr_acl_t *)&itx->itx_lr;
689         lr->lr_foid = zp->z_id;
690         if (txtype == TX_ACL) {
691                 lr->lr_acl_bytes = aclbytes;
692                 lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
693                 lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
694                 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
695                         lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
696                 else
697                         lr->lr_acl_flags = 0;
698         }
699         lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
700
701         if (txtype == TX_ACL_V0) {
702                 lrv0 = (lr_acl_v0_t *)lr;
703                 bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
704         } else {
705                 void *start = (ace_t *)(lr + 1);
706
707                 bcopy(vsecp->vsa_aclentp, start, aclbytes);
708
709                 start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
710
711                 if (fuidp) {
712                         start = zfs_log_fuid_ids(fuidp, start);
713                         (void) zfs_log_fuid_domains(fuidp, start);
714                 }
715         }
716
717         itx->itx_sync = (zp->z_sync_cnt != 0);
718         zil_itx_assign(zilog, itx, tx);
719 }
720
721 #if defined(_KERNEL)
722 module_param(zfs_immediate_write_sz, long, 0644);
723 MODULE_PARM_DESC(zfs_immediate_write_sz, "Largest data block to write to zil");
724 #endif