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