xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_log.c (revision 44bac77bf8165ebe38afb85dda247b928d88edf8)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/cmn_err.h>
33 #include <sys/kmem.h>
34 #include <sys/thread.h>
35 #include <sys/file.h>
36 #include <sys/vfs.h>
37 #include <sys/zfs_znode.h>
38 #include <sys/zfs_dir.h>
39 #include <sys/zil.h>
40 #include <sys/zil_impl.h>
41 #include <sys/byteorder.h>
42 #include <sys/policy.h>
43 #include <sys/stat.h>
44 #include <sys/mode.h>
45 #include <sys/acl.h>
46 #include <sys/dmu.h>
47 #include <sys/spa.h>
48 #include <sys/zfs_fuid.h>
49 #include <sys/ddi.h>
50 
51 /*
52  * All the functions in this file are used to construct the log entries
53  * to record transactions. They allocate * an intent log transaction
54  * structure (itx_t) and save within it all the information necessary to
55  * possibly replay the transaction. The itx is then assigned a sequence
56  * number and inserted in the in-memory list anchored in the zilog.
57  */
58 
59 int
60 zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
61 {
62 	int isxvattr = (vap->va_mask & AT_XVATTR);
63 	switch (type) {
64 	case Z_FILE:
65 		if (vsecp == NULL && !isxvattr)
66 			return (TX_CREATE);
67 		if (vsecp && isxvattr)
68 			return (TX_CREATE_ACL_ATTR);
69 		if (vsecp)
70 			return (TX_CREATE_ACL);
71 		else
72 			return (TX_CREATE_ATTR);
73 		/*NOTREACHED*/
74 	case Z_DIR:
75 		if (vsecp == NULL && !isxvattr)
76 			return (TX_MKDIR);
77 		if (vsecp && isxvattr)
78 			return (TX_MKDIR_ACL_ATTR);
79 		if (vsecp)
80 			return (TX_MKDIR_ACL);
81 		else
82 			return (TX_MKDIR_ATTR);
83 	case Z_XATTRDIR:
84 		return (TX_MKXATTR);
85 	}
86 	ASSERT(0);
87 	return (TX_MAX_TYPE);
88 }
89 
90 /*
91  * build up the log data necessary for logging xvattr_t
92  * First lr_attr_t is initialized.  following the lr_attr_t
93  * is the mapsize and attribute bitmap copied from the xvattr_t.
94  * Following the bitmap and bitmapsize two 64 bit words are reserved
95  * for the create time which may be set.  Following the create time
96  * records a single 64 bit integer which has the bits to set on
97  * replay for the xvattr.
98  */
99 static void
100 zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
101 {
102 	uint32_t	*bitmap;
103 	uint64_t	*attrs;
104 	uint64_t	*crtime;
105 	xoptattr_t	*xoap;
106 	void		*scanstamp;
107 	int		i;
108 
109 	xoap = xva_getxoptattr(xvap);
110 	ASSERT(xoap);
111 
112 	lrattr->lr_attr_masksize = xvap->xva_mapsize;
113 	bitmap = &lrattr->lr_attr_bitmap;
114 	for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
115 		*bitmap = xvap->xva_reqattrmap[i];
116 	}
117 
118 	/* Now pack the attributes up in a single uint64_t */
119 	attrs = (uint64_t *)bitmap;
120 	crtime = attrs + 1;
121 	scanstamp = (caddr_t)(crtime + 2);
122 	*attrs = 0;
123 	if (XVA_ISSET_REQ(xvap, XAT_READONLY))
124 		*attrs |= (xoap->xoa_readonly == 0) ? 0 :
125 		    XAT0_READONLY;
126 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
127 		*attrs |= (xoap->xoa_hidden == 0) ? 0 :
128 		    XAT0_HIDDEN;
129 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
130 		*attrs |= (xoap->xoa_system == 0) ? 0 :
131 		    XAT0_SYSTEM;
132 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
133 		*attrs |= (xoap->xoa_archive == 0) ? 0 :
134 		    XAT0_ARCHIVE;
135 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
136 		*attrs |= (xoap->xoa_immutable == 0) ? 0 :
137 		    XAT0_IMMUTABLE;
138 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
139 		*attrs |= (xoap->xoa_nounlink == 0) ? 0 :
140 		    XAT0_NOUNLINK;
141 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
142 		*attrs |= (xoap->xoa_appendonly == 0) ? 0 :
143 		    XAT0_APPENDONLY;
144 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
145 		*attrs |= (xoap->xoa_opaque == 0) ? 0 :
146 		    XAT0_APPENDONLY;
147 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
148 		*attrs |= (xoap->xoa_nodump == 0) ? 0 :
149 		    XAT0_NODUMP;
150 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
151 		*attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
152 		    XAT0_AV_QUARANTINED;
153 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
154 		*attrs |= (xoap->xoa_av_modified == 0) ? 0 :
155 		    XAT0_AV_MODIFIED;
156 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
157 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
158 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
159 		bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
160 }
161 
162 static void *
163 zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
164 {
165 	zfs_fuid_t *zfuid;
166 	uint64_t *fuidloc = start;
167 
168 	/* First copy in the ACE FUIDs */
169 	for (zfuid = list_head(&fuidp->z_fuids); zfuid;
170 	    zfuid = list_next(&fuidp->z_fuids, zfuid)) {
171 		*fuidloc++ = zfuid->z_logfuid;
172 	}
173 	return (fuidloc);
174 }
175 
176 
177 static void *
178 zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
179 {
180 	zfs_fuid_domain_t *zdomain;
181 
182 	/* now copy in the domain info, if any */
183 	if (fuidp->z_domain_str_sz != 0) {
184 		for (zdomain = list_head(&fuidp->z_domains); zdomain;
185 		    zdomain = list_next(&fuidp->z_domains, zdomain)) {
186 			bcopy((void *)zdomain->z_domain, start,
187 			    strlen(zdomain->z_domain) + 1);
188 			start = (caddr_t)start +
189 			    strlen(zdomain->z_domain) + 1;
190 		}
191 	}
192 	return (start);
193 }
194 
195 /*
196  * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR,
197  * TX_MKDIR_ATTR and TX_MKXATTR
198  * transactions.
199  *
200  * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
201  * domain information appended prior to the name.  In this case the
202  * uid/gid in the log record will be a log centric FUID.
203  *
204  * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
205  * may contain attributes, ACL and optional fuid information.
206  *
207  * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
208  * and ACL and normal users/groups in the ACEs.
209  *
210  * There may be an optional xvattr attribute information similar
211  * to zfs_log_setattr.
212  *
213  * Also, after the file name "domain" strings may be appended.
214  */
215 void
216 zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
217     znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
218     zfs_fuid_info_t *fuidp, vattr_t *vap)
219 {
220 	itx_t *itx;
221 	uint64_t seq;
222 	lr_create_t *lr;
223 	lr_acl_create_t *lracl;
224 	size_t aclsize;
225 	size_t xvatsize = 0;
226 	size_t txsize;
227 	xvattr_t *xvap = (xvattr_t *)vap;
228 	void *end;
229 	size_t lrsize;
230 	size_t namesize = strlen(name) + 1;
231 	size_t fuidsz = 0;
232 
233 	if (zilog == NULL)
234 		return;
235 
236 	/*
237 	 * If we have FUIDs present then add in space for
238 	 * domains and ACE fuid's if any.
239 	 */
240 	if (fuidp) {
241 		fuidsz += fuidp->z_domain_str_sz;
242 		fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
243 	}
244 
245 	if (vap->va_mask & AT_XVATTR)
246 		xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
247 
248 	if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
249 	    (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
250 	    (int)txtype == TX_MKXATTR) {
251 		txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
252 		lrsize = sizeof (*lr);
253 	} else {
254 		aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0;
255 		txsize =
256 		    sizeof (lr_acl_create_t) + namesize + fuidsz +
257 		    ZIL_ACE_LENGTH(aclsize) + xvatsize;
258 		lrsize = sizeof (lr_acl_create_t);
259 	}
260 
261 	itx = zil_itx_create(txtype, txsize);
262 
263 	lr = (lr_create_t *)&itx->itx_lr;
264 	lr->lr_doid = dzp->z_id;
265 	lr->lr_foid = zp->z_id;
266 	lr->lr_mode = zp->z_phys->zp_mode;
267 	if (!IS_EPHEMERAL(zp->z_phys->zp_uid)) {
268 		lr->lr_uid = (uint64_t)zp->z_phys->zp_uid;
269 	} else {
270 		lr->lr_uid = fuidp->z_fuid_owner;
271 	}
272 	if (!IS_EPHEMERAL(zp->z_phys->zp_gid)) {
273 		lr->lr_gid = (uint64_t)zp->z_phys->zp_gid;
274 	} else {
275 		lr->lr_gid = fuidp->z_fuid_group;
276 	}
277 	lr->lr_gen = zp->z_phys->zp_gen;
278 	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
279 	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
280 	lr->lr_rdev = zp->z_phys->zp_rdev;
281 
282 	/*
283 	 * Fill in xvattr info if any
284 	 */
285 	if (vap->va_mask & AT_XVATTR) {
286 		zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
287 		end = (caddr_t)lr + lrsize + xvatsize;
288 	} else {
289 		end = (caddr_t)lr + lrsize;
290 	}
291 
292 	/* Now fill in any ACL info */
293 
294 	if (vsecp) {
295 		lracl = (lr_acl_create_t *)&itx->itx_lr;
296 		lracl->lr_aclcnt = vsecp->vsa_aclcnt;
297 		lracl->lr_acl_bytes = aclsize;
298 		lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
299 		lracl->lr_fuidcnt  = fuidp ? fuidp->z_fuid_cnt : 0;
300 		if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
301 			lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
302 		else
303 			lracl->lr_acl_flags = 0;
304 
305 		bcopy(vsecp->vsa_aclentp, end, aclsize);
306 		end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
307 	}
308 
309 	/* drop in FUID info */
310 	if (fuidp) {
311 		end = zfs_log_fuid_ids(fuidp, end);
312 		end = zfs_log_fuid_domains(fuidp, end);
313 	}
314 	/*
315 	 * Now place file name in log record
316 	 */
317 	bcopy(name, end, namesize);
318 
319 	seq = zil_itx_assign(zilog, itx, tx);
320 	dzp->z_last_itx = seq;
321 	zp->z_last_itx = seq;
322 }
323 
324 /*
325  * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
326  */
327 void
328 zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
329 	znode_t *dzp, char *name)
330 {
331 	itx_t *itx;
332 	uint64_t seq;
333 	lr_remove_t *lr;
334 	size_t namesize = strlen(name) + 1;
335 
336 	if (zilog == NULL)
337 		return;
338 
339 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
340 	lr = (lr_remove_t *)&itx->itx_lr;
341 	lr->lr_doid = dzp->z_id;
342 	bcopy(name, (char *)(lr + 1), namesize);
343 
344 	seq = zil_itx_assign(zilog, itx, tx);
345 	dzp->z_last_itx = seq;
346 }
347 
348 /*
349  * zfs_log_link() handles TX_LINK transactions.
350  */
351 void
352 zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
353 	znode_t *dzp, znode_t *zp, char *name)
354 {
355 	itx_t *itx;
356 	uint64_t seq;
357 	lr_link_t *lr;
358 	size_t namesize = strlen(name) + 1;
359 
360 	if (zilog == NULL)
361 		return;
362 
363 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
364 	lr = (lr_link_t *)&itx->itx_lr;
365 	lr->lr_doid = dzp->z_id;
366 	lr->lr_link_obj = zp->z_id;
367 	bcopy(name, (char *)(lr + 1), namesize);
368 
369 	seq = zil_itx_assign(zilog, itx, tx);
370 	dzp->z_last_itx = seq;
371 	zp->z_last_itx = seq;
372 }
373 
374 /*
375  * zfs_log_symlink() handles TX_SYMLINK transactions.
376  */
377 void
378 zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
379     znode_t *dzp, znode_t *zp, char *name, char *link)
380 {
381 	itx_t *itx;
382 	uint64_t seq;
383 	lr_create_t *lr;
384 	size_t namesize = strlen(name) + 1;
385 	size_t linksize = strlen(link) + 1;
386 
387 	if (zilog == NULL)
388 		return;
389 
390 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
391 	lr = (lr_create_t *)&itx->itx_lr;
392 	lr->lr_doid = dzp->z_id;
393 	lr->lr_foid = zp->z_id;
394 	lr->lr_mode = zp->z_phys->zp_mode;
395 	lr->lr_uid = zp->z_phys->zp_uid;
396 	lr->lr_gid = zp->z_phys->zp_gid;
397 	lr->lr_gen = zp->z_phys->zp_gen;
398 	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
399 	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
400 	bcopy(name, (char *)(lr + 1), namesize);
401 	bcopy(link, (char *)(lr + 1) + namesize, linksize);
402 
403 	seq = zil_itx_assign(zilog, itx, tx);
404 	dzp->z_last_itx = seq;
405 	zp->z_last_itx = seq;
406 }
407 
408 /*
409  * zfs_log_rename() handles TX_RENAME transactions.
410  */
411 void
412 zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
413 	znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
414 {
415 	itx_t *itx;
416 	uint64_t seq;
417 	lr_rename_t *lr;
418 	size_t snamesize = strlen(sname) + 1;
419 	size_t dnamesize = strlen(dname) + 1;
420 
421 	if (zilog == NULL)
422 		return;
423 
424 	itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
425 	lr = (lr_rename_t *)&itx->itx_lr;
426 	lr->lr_sdoid = sdzp->z_id;
427 	lr->lr_tdoid = tdzp->z_id;
428 	bcopy(sname, (char *)(lr + 1), snamesize);
429 	bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
430 
431 	seq = zil_itx_assign(zilog, itx, tx);
432 	sdzp->z_last_itx = seq;
433 	tdzp->z_last_itx = seq;
434 	szp->z_last_itx = seq;
435 }
436 
437 /*
438  * zfs_log_write() handles TX_WRITE transactions.
439  */
440 ssize_t zfs_immediate_write_sz = 32768;
441 
442 #define	ZIL_MAX_LOG_DATA (SPA_MAXBLOCKSIZE - sizeof (zil_trailer_t) - \
443     sizeof (lr_write_t))
444 
445 void
446 zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
447 	znode_t *zp, offset_t off, ssize_t resid, int ioflag)
448 {
449 	itx_wr_state_t write_state;
450 	boolean_t slogging;
451 	uintptr_t fsync_cnt;
452 
453 	if (zilog == NULL || zp->z_unlinked)
454 		return;
455 
456 	/*
457 	 * Writes are handled in three different ways:
458 	 *
459 	 * WR_INDIRECT:
460 	 *    If the write is greater than zfs_immediate_write_sz and there are
461 	 *    no separate logs in this pool then later *if* we need to log the
462 	 *    write then dmu_sync() is used to immediately write the block and
463 	 *    its block pointer is put in the log record.
464 	 * WR_COPIED:
465 	 *    If we know we'll immediately be committing the
466 	 *    transaction (FSYNC or FDSYNC), the we allocate a larger
467 	 *    log record here for the data and copy the data in.
468 	 * WR_NEED_COPY:
469 	 *    Otherwise we don't allocate a buffer, and *if* we need to
470 	 *    flush the write later then a buffer is allocated and
471 	 *    we retrieve the data using the dmu.
472 	 */
473 	slogging = spa_has_slogs(zilog->zl_spa);
474 	if (resid > zfs_immediate_write_sz && !slogging)
475 		write_state = WR_INDIRECT;
476 	else if (ioflag & (FSYNC | FDSYNC))
477 		write_state = WR_COPIED;
478 	else
479 		write_state = WR_NEED_COPY;
480 
481 	if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
482 		(void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
483 	}
484 
485 	while (resid) {
486 		itx_t *itx;
487 		lr_write_t *lr;
488 		ssize_t len;
489 
490 		/*
491 		 * If there are slogs and the write would overflow the largest
492 		 * block, then because we don't want to use the main pool
493 		 * to dmu_sync, we have to split the write.
494 		 */
495 		if (slogging && resid > ZIL_MAX_LOG_DATA)
496 			len = SPA_MAXBLOCKSIZE >> 1;
497 		else
498 			len = resid;
499 
500 		itx = zil_itx_create(txtype, sizeof (*lr) +
501 		    (write_state == WR_COPIED ? len : 0));
502 		lr = (lr_write_t *)&itx->itx_lr;
503 		if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
504 		    zp->z_id, off, len, lr + 1) != 0) {
505 			kmem_free(itx, offsetof(itx_t, itx_lr) +
506 			    itx->itx_lr.lrc_reclen);
507 			itx = zil_itx_create(txtype, sizeof (*lr));
508 			lr = (lr_write_t *)&itx->itx_lr;
509 			write_state = WR_NEED_COPY;
510 		}
511 
512 		itx->itx_wr_state = write_state;
513 		if (write_state == WR_NEED_COPY)
514 			itx->itx_sod += len;
515 		lr->lr_foid = zp->z_id;
516 		lr->lr_offset = off;
517 		lr->lr_length = len;
518 		lr->lr_blkoff = 0;
519 		BP_ZERO(&lr->lr_blkptr);
520 
521 		itx->itx_private = zp->z_zfsvfs;
522 
523 		if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0) ||
524 		    (ioflag & (FSYNC | FDSYNC)))
525 			itx->itx_sync = B_TRUE;
526 		else
527 			itx->itx_sync = B_FALSE;
528 
529 		zp->z_last_itx = zil_itx_assign(zilog, itx, tx);
530 
531 		off += len;
532 		resid -= len;
533 	}
534 }
535 
536 /*
537  * zfs_log_truncate() handles TX_TRUNCATE transactions.
538  */
539 void
540 zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
541 	znode_t *zp, uint64_t off, uint64_t len)
542 {
543 	itx_t *itx;
544 	uint64_t seq;
545 	lr_truncate_t *lr;
546 
547 	if (zilog == NULL || zp->z_unlinked)
548 		return;
549 
550 	itx = zil_itx_create(txtype, sizeof (*lr));
551 	lr = (lr_truncate_t *)&itx->itx_lr;
552 	lr->lr_foid = zp->z_id;
553 	lr->lr_offset = off;
554 	lr->lr_length = len;
555 
556 	itx->itx_sync = (zp->z_sync_cnt != 0);
557 	seq = zil_itx_assign(zilog, itx, tx);
558 	zp->z_last_itx = seq;
559 }
560 
561 /*
562  * zfs_log_setattr() handles TX_SETATTR transactions.
563  */
564 void
565 zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
566 	znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
567 {
568 	itx_t		*itx;
569 	uint64_t	seq;
570 	lr_setattr_t	*lr;
571 	xvattr_t	*xvap = (xvattr_t *)vap;
572 	size_t		recsize = sizeof (lr_setattr_t);
573 	void		*start;
574 
575 
576 	if (zilog == NULL || zp->z_unlinked)
577 		return;
578 
579 	/*
580 	 * If XVATTR set, then log record size needs to allow
581 	 * for lr_attr_t + xvattr mask, mapsize and create time
582 	 * plus actual attribute values
583 	 */
584 	if (vap->va_mask & AT_XVATTR)
585 		recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
586 
587 	if (fuidp)
588 		recsize += fuidp->z_domain_str_sz;
589 
590 	itx = zil_itx_create(txtype, recsize);
591 	lr = (lr_setattr_t *)&itx->itx_lr;
592 	lr->lr_foid = zp->z_id;
593 	lr->lr_mask = (uint64_t)mask_applied;
594 	lr->lr_mode = (uint64_t)vap->va_mode;
595 	if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
596 		lr->lr_uid = fuidp->z_fuid_owner;
597 	else
598 		lr->lr_uid = (uint64_t)vap->va_uid;
599 
600 	if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
601 		lr->lr_gid = fuidp->z_fuid_group;
602 	else
603 		lr->lr_gid = (uint64_t)vap->va_gid;
604 
605 	lr->lr_size = (uint64_t)vap->va_size;
606 	ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
607 	ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
608 	start = (lr_setattr_t *)(lr + 1);
609 	if (vap->va_mask & AT_XVATTR) {
610 		zfs_log_xvattr((lr_attr_t *)start, xvap);
611 		start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
612 	}
613 
614 	/*
615 	 * Now stick on domain information if any on end
616 	 */
617 
618 	if (fuidp)
619 		(void) zfs_log_fuid_domains(fuidp, start);
620 
621 	itx->itx_sync = (zp->z_sync_cnt != 0);
622 	seq = zil_itx_assign(zilog, itx, tx);
623 	zp->z_last_itx = seq;
624 }
625 
626 /*
627  * zfs_log_acl() handles TX_ACL transactions.
628  */
629 void
630 zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
631     vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
632 {
633 	itx_t *itx;
634 	uint64_t seq;
635 	lr_acl_v0_t *lrv0;
636 	lr_acl_t *lr;
637 	int txtype;
638 	int lrsize;
639 	size_t txsize;
640 	size_t aclbytes = vsecp->vsa_aclentsz;
641 
642 	if (zilog == NULL || zp->z_unlinked)
643 		return;
644 
645 	txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
646 	    TX_ACL_V0 : TX_ACL;
647 
648 	if (txtype == TX_ACL)
649 		lrsize = sizeof (*lr);
650 	else
651 		lrsize = sizeof (*lrv0);
652 
653 	txsize = lrsize +
654 	    ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
655 	    (fuidp ? fuidp->z_domain_str_sz : 0) +
656 	    sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
657 
658 	itx = zil_itx_create(txtype, txsize);
659 
660 	lr = (lr_acl_t *)&itx->itx_lr;
661 	lr->lr_foid = zp->z_id;
662 	if (txtype == TX_ACL) {
663 		lr->lr_acl_bytes = aclbytes;
664 		lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
665 		lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
666 		if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
667 			lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
668 		else
669 			lr->lr_acl_flags = 0;
670 	}
671 	lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
672 
673 	if (txtype == TX_ACL_V0) {
674 		lrv0 = (lr_acl_v0_t *)lr;
675 		bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
676 	} else {
677 		void *start = (ace_t *)(lr + 1);
678 
679 		bcopy(vsecp->vsa_aclentp, start, aclbytes);
680 
681 		start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
682 
683 		if (fuidp) {
684 			start = zfs_log_fuid_ids(fuidp, start);
685 			(void) zfs_log_fuid_domains(fuidp, start);
686 		}
687 	}
688 
689 	itx->itx_sync = (zp->z_sync_cnt != 0);
690 	seq = zil_itx_assign(zilog, itx, tx);
691 	zp->z_last_itx = seq;
692 }
693