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