xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_log.c (revision 9401:afae664f76f6)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51669Sperrin  * Common Development and Distribution License (the "License").
61669Sperrin  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*9401SNeil.Perrin@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/types.h>
27789Sahrens #include <sys/param.h>
28789Sahrens #include <sys/systm.h>
29789Sahrens #include <sys/sysmacros.h>
30789Sahrens #include <sys/cmn_err.h>
31789Sahrens #include <sys/kmem.h>
32789Sahrens #include <sys/thread.h>
33789Sahrens #include <sys/file.h>
34789Sahrens #include <sys/vfs.h>
35789Sahrens #include <sys/zfs_znode.h>
36789Sahrens #include <sys/zfs_dir.h>
37789Sahrens #include <sys/zil.h>
384620Sperrin #include <sys/zil_impl.h>
39789Sahrens #include <sys/byteorder.h>
40789Sahrens #include <sys/policy.h>
41789Sahrens #include <sys/stat.h>
42789Sahrens #include <sys/mode.h>
43789Sahrens #include <sys/acl.h>
44789Sahrens #include <sys/dmu.h>
45789Sahrens #include <sys/spa.h>
465331Samw #include <sys/zfs_fuid.h>
47789Sahrens #include <sys/ddi.h>
488227SNeil.Perrin@Sun.COM #include <sys/dsl_dataset.h>
498227SNeil.Perrin@Sun.COM 
508227SNeil.Perrin@Sun.COM #define	ZFS_HANDLE_REPLAY(zilog, tx) \
518227SNeil.Perrin@Sun.COM 	if (zilog->zl_replay) { \
528227SNeil.Perrin@Sun.COM 		dsl_dataset_dirty(dmu_objset_ds(zilog->zl_os), tx); \
538227SNeil.Perrin@Sun.COM 		zilog->zl_replayed_seq[dmu_tx_get_txg(tx) & TXG_MASK] = \
548227SNeil.Perrin@Sun.COM 		    zilog->zl_replaying_seq; \
558227SNeil.Perrin@Sun.COM 		return; \
568227SNeil.Perrin@Sun.COM 	}
57789Sahrens 
58789Sahrens /*
598227SNeil.Perrin@Sun.COM  * These zfs_log_* functions must be called within a dmu tx, in one
608227SNeil.Perrin@Sun.COM  * of 2 contexts depending on zilog->z_replay:
618227SNeil.Perrin@Sun.COM  *
628227SNeil.Perrin@Sun.COM  * Non replay mode
638227SNeil.Perrin@Sun.COM  * ---------------
648227SNeil.Perrin@Sun.COM  * We need to record the transaction so that if it is committed to
658227SNeil.Perrin@Sun.COM  * the Intent Log then it can be replayed.  An intent log transaction
668227SNeil.Perrin@Sun.COM  * structure (itx_t) is allocated and all the information necessary to
678227SNeil.Perrin@Sun.COM  * possibly replay the transaction is saved in it. The itx is then assigned
688227SNeil.Perrin@Sun.COM  * a sequence number and inserted in the in-memory list anchored in the zilog.
698227SNeil.Perrin@Sun.COM  *
708227SNeil.Perrin@Sun.COM  * Replay mode
718227SNeil.Perrin@Sun.COM  * -----------
728227SNeil.Perrin@Sun.COM  * We need to mark the intent log record as replayed in the log header.
738227SNeil.Perrin@Sun.COM  * This is done in the same transaction as the replay so that they
748227SNeil.Perrin@Sun.COM  * commit atomically.
75789Sahrens  */
76789Sahrens 
775331Samw int
785331Samw zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
795331Samw {
805331Samw 	int isxvattr = (vap->va_mask & AT_XVATTR);
815331Samw 	switch (type) {
825331Samw 	case Z_FILE:
835331Samw 		if (vsecp == NULL && !isxvattr)
845331Samw 			return (TX_CREATE);
855331Samw 		if (vsecp && isxvattr)
865331Samw 			return (TX_CREATE_ACL_ATTR);
875331Samw 		if (vsecp)
885331Samw 			return (TX_CREATE_ACL);
895331Samw 		else
905331Samw 			return (TX_CREATE_ATTR);
915331Samw 		/*NOTREACHED*/
925331Samw 	case Z_DIR:
935331Samw 		if (vsecp == NULL && !isxvattr)
945331Samw 			return (TX_MKDIR);
955331Samw 		if (vsecp && isxvattr)
965331Samw 			return (TX_MKDIR_ACL_ATTR);
975331Samw 		if (vsecp)
985331Samw 			return (TX_MKDIR_ACL);
995331Samw 		else
1005331Samw 			return (TX_MKDIR_ATTR);
1015331Samw 	case Z_XATTRDIR:
1025331Samw 		return (TX_MKXATTR);
1035331Samw 	}
1045331Samw 	ASSERT(0);
1055331Samw 	return (TX_MAX_TYPE);
1065331Samw }
1075331Samw 
108789Sahrens /*
1095331Samw  * build up the log data necessary for logging xvattr_t
1105331Samw  * First lr_attr_t is initialized.  following the lr_attr_t
1115331Samw  * is the mapsize and attribute bitmap copied from the xvattr_t.
1125331Samw  * Following the bitmap and bitmapsize two 64 bit words are reserved
1135331Samw  * for the create time which may be set.  Following the create time
1145331Samw  * records a single 64 bit integer which has the bits to set on
1155331Samw  * replay for the xvattr.
1165331Samw  */
1175331Samw static void
1185331Samw zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
1195331Samw {
1205331Samw 	uint32_t	*bitmap;
1215331Samw 	uint64_t	*attrs;
1225331Samw 	uint64_t	*crtime;
1235331Samw 	xoptattr_t	*xoap;
1245331Samw 	void		*scanstamp;
1255331Samw 	int		i;
1265331Samw 
1275331Samw 	xoap = xva_getxoptattr(xvap);
1285331Samw 	ASSERT(xoap);
1295331Samw 
1305331Samw 	lrattr->lr_attr_masksize = xvap->xva_mapsize;
1315331Samw 	bitmap = &lrattr->lr_attr_bitmap;
1325331Samw 	for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
1335331Samw 		*bitmap = xvap->xva_reqattrmap[i];
1345331Samw 	}
1355331Samw 
1365331Samw 	/* Now pack the attributes up in a single uint64_t */
1375331Samw 	attrs = (uint64_t *)bitmap;
1385331Samw 	crtime = attrs + 1;
1395331Samw 	scanstamp = (caddr_t)(crtime + 2);
1405331Samw 	*attrs = 0;
1415331Samw 	if (XVA_ISSET_REQ(xvap, XAT_READONLY))
1425331Samw 		*attrs |= (xoap->xoa_readonly == 0) ? 0 :
1435331Samw 		    XAT0_READONLY;
1445331Samw 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
1455331Samw 		*attrs |= (xoap->xoa_hidden == 0) ? 0 :
1465331Samw 		    XAT0_HIDDEN;
1475331Samw 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
1485331Samw 		*attrs |= (xoap->xoa_system == 0) ? 0 :
1495331Samw 		    XAT0_SYSTEM;
1505331Samw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
1515331Samw 		*attrs |= (xoap->xoa_archive == 0) ? 0 :
1525331Samw 		    XAT0_ARCHIVE;
1535331Samw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
1545331Samw 		*attrs |= (xoap->xoa_immutable == 0) ? 0 :
1555331Samw 		    XAT0_IMMUTABLE;
1565331Samw 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
1575331Samw 		*attrs |= (xoap->xoa_nounlink == 0) ? 0 :
1585331Samw 		    XAT0_NOUNLINK;
1595331Samw 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
1605331Samw 		*attrs |= (xoap->xoa_appendonly == 0) ? 0 :
1615331Samw 		    XAT0_APPENDONLY;
1625331Samw 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
1635331Samw 		*attrs |= (xoap->xoa_opaque == 0) ? 0 :
1645331Samw 		    XAT0_APPENDONLY;
1655331Samw 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
1665331Samw 		*attrs |= (xoap->xoa_nodump == 0) ? 0 :
1675331Samw 		    XAT0_NODUMP;
1685331Samw 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
1695331Samw 		*attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
1705331Samw 		    XAT0_AV_QUARANTINED;
1715331Samw 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
1725331Samw 		*attrs |= (xoap->xoa_av_modified == 0) ? 0 :
1735331Samw 		    XAT0_AV_MODIFIED;
1745331Samw 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
1755331Samw 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
1765331Samw 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
1775331Samw 		bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
1785331Samw }
1795331Samw 
1805331Samw static void *
1815331Samw zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
1825331Samw {
1835331Samw 	zfs_fuid_t *zfuid;
1845331Samw 	uint64_t *fuidloc = start;
1855331Samw 
1865331Samw 	/* First copy in the ACE FUIDs */
1875331Samw 	for (zfuid = list_head(&fuidp->z_fuids); zfuid;
1885331Samw 	    zfuid = list_next(&fuidp->z_fuids, zfuid)) {
1895331Samw 		*fuidloc++ = zfuid->z_logfuid;
1905331Samw 	}
1915331Samw 	return (fuidloc);
1925331Samw }
1935331Samw 
1945331Samw 
1955331Samw static void *
1965331Samw zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
1975331Samw {
1985331Samw 	zfs_fuid_domain_t *zdomain;
1995331Samw 
2005331Samw 	/* now copy in the domain info, if any */
2015331Samw 	if (fuidp->z_domain_str_sz != 0) {
2025331Samw 		for (zdomain = list_head(&fuidp->z_domains); zdomain;
2035331Samw 		    zdomain = list_next(&fuidp->z_domains, zdomain)) {
2045331Samw 			bcopy((void *)zdomain->z_domain, start,
2055331Samw 			    strlen(zdomain->z_domain) + 1);
2065331Samw 			start = (caddr_t)start +
2075331Samw 			    strlen(zdomain->z_domain) + 1;
2085331Samw 		}
2095331Samw 	}
2105331Samw 	return (start);
2115331Samw }
2125331Samw 
2135331Samw /*
2145331Samw  * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR,
2155331Samw  * TX_MKDIR_ATTR and TX_MKXATTR
216789Sahrens  * transactions.
2175331Samw  *
2185331Samw  * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
2195331Samw  * domain information appended prior to the name.  In this case the
2205331Samw  * uid/gid in the log record will be a log centric FUID.
2215331Samw  *
2225331Samw  * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
2235331Samw  * may contain attributes, ACL and optional fuid information.
2245331Samw  *
2255331Samw  * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
2265331Samw  * and ACL and normal users/groups in the ACEs.
2275331Samw  *
2285331Samw  * There may be an optional xvattr attribute information similar
2295331Samw  * to zfs_log_setattr.
2305331Samw  *
2315331Samw  * Also, after the file name "domain" strings may be appended.
232789Sahrens  */
2332638Sperrin void
2345331Samw zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
2355331Samw     znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
2365331Samw     zfs_fuid_info_t *fuidp, vattr_t *vap)
237789Sahrens {
238789Sahrens 	itx_t *itx;
239789Sahrens 	uint64_t seq;
240789Sahrens 	lr_create_t *lr;
2415331Samw 	lr_acl_create_t *lracl;
2425331Samw 	size_t aclsize;
2435331Samw 	size_t xvatsize = 0;
2445331Samw 	size_t txsize;
2455331Samw 	xvattr_t *xvap = (xvattr_t *)vap;
2465331Samw 	void *end;
2475331Samw 	size_t lrsize;
248789Sahrens 	size_t namesize = strlen(name) + 1;
2495331Samw 	size_t fuidsz = 0;
250789Sahrens 
251789Sahrens 	if (zilog == NULL)
2522638Sperrin 		return;
253789Sahrens 
2548227SNeil.Perrin@Sun.COM 	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
2558227SNeil.Perrin@Sun.COM 
2565331Samw 	/*
2575331Samw 	 * If we have FUIDs present then add in space for
2585331Samw 	 * domains and ACE fuid's if any.
2595331Samw 	 */
2605331Samw 	if (fuidp) {
2615331Samw 		fuidsz += fuidp->z_domain_str_sz;
2625331Samw 		fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
2635331Samw 	}
2645331Samw 
2655331Samw 	if (vap->va_mask & AT_XVATTR)
2665331Samw 		xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
2675331Samw 
2685331Samw 	if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
2695331Samw 	    (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
2705331Samw 	    (int)txtype == TX_MKXATTR) {
2715331Samw 		txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
2725331Samw 		lrsize = sizeof (*lr);
2735331Samw 	} else {
2745331Samw 		aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0;
2755331Samw 		txsize =
2765331Samw 		    sizeof (lr_acl_create_t) + namesize + fuidsz +
2775435Smarks 		    ZIL_ACE_LENGTH(aclsize) + xvatsize;
2785331Samw 		lrsize = sizeof (lr_acl_create_t);
2795331Samw 	}
2805331Samw 
2815331Samw 	itx = zil_itx_create(txtype, txsize);
2825331Samw 
283789Sahrens 	lr = (lr_create_t *)&itx->itx_lr;
284789Sahrens 	lr->lr_doid = dzp->z_id;
285789Sahrens 	lr->lr_foid = zp->z_id;
286789Sahrens 	lr->lr_mode = zp->z_phys->zp_mode;
2875331Samw 	if (!IS_EPHEMERAL(zp->z_phys->zp_uid)) {
2885331Samw 		lr->lr_uid = (uint64_t)zp->z_phys->zp_uid;
2895331Samw 	} else {
2905331Samw 		lr->lr_uid = fuidp->z_fuid_owner;
2915331Samw 	}
2925331Samw 	if (!IS_EPHEMERAL(zp->z_phys->zp_gid)) {
2935331Samw 		lr->lr_gid = (uint64_t)zp->z_phys->zp_gid;
2945331Samw 	} else {
2955331Samw 		lr->lr_gid = fuidp->z_fuid_group;
2965331Samw 	}
297789Sahrens 	lr->lr_gen = zp->z_phys->zp_gen;
298789Sahrens 	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
299789Sahrens 	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
300789Sahrens 	lr->lr_rdev = zp->z_phys->zp_rdev;
3015331Samw 
3025331Samw 	/*
3035331Samw 	 * Fill in xvattr info if any
3045331Samw 	 */
3055331Samw 	if (vap->va_mask & AT_XVATTR) {
3065331Samw 		zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
3075331Samw 		end = (caddr_t)lr + lrsize + xvatsize;
3085331Samw 	} else {
3095331Samw 		end = (caddr_t)lr + lrsize;
3105331Samw 	}
3115331Samw 
3125331Samw 	/* Now fill in any ACL info */
3135331Samw 
3145331Samw 	if (vsecp) {
3155331Samw 		lracl = (lr_acl_create_t *)&itx->itx_lr;
3165331Samw 		lracl->lr_aclcnt = vsecp->vsa_aclcnt;
3175331Samw 		lracl->lr_acl_bytes = aclsize;
3185331Samw 		lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
3195331Samw 		lracl->lr_fuidcnt  = fuidp ? fuidp->z_fuid_cnt : 0;
3205331Samw 		if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
3215331Samw 			lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
3225331Samw 		else
3235331Samw 			lracl->lr_acl_flags = 0;
3245331Samw 
3255331Samw 		bcopy(vsecp->vsa_aclentp, end, aclsize);
3265435Smarks 		end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
3275331Samw 	}
3285331Samw 
3295331Samw 	/* drop in FUID info */
3305331Samw 	if (fuidp) {
3315331Samw 		end = zfs_log_fuid_ids(fuidp, end);
3325331Samw 		end = zfs_log_fuid_domains(fuidp, end);
3335331Samw 	}
3345331Samw 	/*
3355331Samw 	 * Now place file name in log record
3365331Samw 	 */
3375331Samw 	bcopy(name, end, namesize);
338789Sahrens 
339789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
340789Sahrens 	dzp->z_last_itx = seq;
341789Sahrens 	zp->z_last_itx = seq;
342789Sahrens }
343789Sahrens 
344789Sahrens /*
345789Sahrens  * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
346789Sahrens  */
3472638Sperrin void
3485331Samw zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
349789Sahrens 	znode_t *dzp, char *name)
350789Sahrens {
351789Sahrens 	itx_t *itx;
352789Sahrens 	uint64_t seq;
353789Sahrens 	lr_remove_t *lr;
354789Sahrens 	size_t namesize = strlen(name) + 1;
355789Sahrens 
356789Sahrens 	if (zilog == NULL)
3572638Sperrin 		return;
358789Sahrens 
3598227SNeil.Perrin@Sun.COM 	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
3608227SNeil.Perrin@Sun.COM 
361789Sahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
362789Sahrens 	lr = (lr_remove_t *)&itx->itx_lr;
363789Sahrens 	lr->lr_doid = dzp->z_id;
364789Sahrens 	bcopy(name, (char *)(lr + 1), namesize);
365789Sahrens 
366789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
367789Sahrens 	dzp->z_last_itx = seq;
368789Sahrens }
369789Sahrens 
370789Sahrens /*
371789Sahrens  * zfs_log_link() handles TX_LINK transactions.
372789Sahrens  */
3732638Sperrin void
3745331Samw zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
375789Sahrens 	znode_t *dzp, znode_t *zp, char *name)
376789Sahrens {
377789Sahrens 	itx_t *itx;
378789Sahrens 	uint64_t seq;
379789Sahrens 	lr_link_t *lr;
380789Sahrens 	size_t namesize = strlen(name) + 1;
381789Sahrens 
382789Sahrens 	if (zilog == NULL)
3832638Sperrin 		return;
384789Sahrens 
3858227SNeil.Perrin@Sun.COM 	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
3868227SNeil.Perrin@Sun.COM 
387789Sahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
388789Sahrens 	lr = (lr_link_t *)&itx->itx_lr;
389789Sahrens 	lr->lr_doid = dzp->z_id;
390789Sahrens 	lr->lr_link_obj = zp->z_id;
391789Sahrens 	bcopy(name, (char *)(lr + 1), namesize);
392789Sahrens 
393789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
394789Sahrens 	dzp->z_last_itx = seq;
395789Sahrens 	zp->z_last_itx = seq;
396789Sahrens }
397789Sahrens 
398789Sahrens /*
399789Sahrens  * zfs_log_symlink() handles TX_SYMLINK transactions.
400789Sahrens  */
4012638Sperrin void
4025331Samw zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
4035331Samw     znode_t *dzp, znode_t *zp, char *name, char *link)
404789Sahrens {
405789Sahrens 	itx_t *itx;
406789Sahrens 	uint64_t seq;
407789Sahrens 	lr_create_t *lr;
408789Sahrens 	size_t namesize = strlen(name) + 1;
409789Sahrens 	size_t linksize = strlen(link) + 1;
410789Sahrens 
411789Sahrens 	if (zilog == NULL)
4122638Sperrin 		return;
413789Sahrens 
4148227SNeil.Perrin@Sun.COM 	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
4158227SNeil.Perrin@Sun.COM 
416789Sahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
417789Sahrens 	lr = (lr_create_t *)&itx->itx_lr;
418789Sahrens 	lr->lr_doid = dzp->z_id;
419789Sahrens 	lr->lr_foid = zp->z_id;
420789Sahrens 	lr->lr_mode = zp->z_phys->zp_mode;
421789Sahrens 	lr->lr_uid = zp->z_phys->zp_uid;
422789Sahrens 	lr->lr_gid = zp->z_phys->zp_gid;
423789Sahrens 	lr->lr_gen = zp->z_phys->zp_gen;
424789Sahrens 	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
425789Sahrens 	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
426789Sahrens 	bcopy(name, (char *)(lr + 1), namesize);
427789Sahrens 	bcopy(link, (char *)(lr + 1) + namesize, linksize);
428789Sahrens 
429789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
430789Sahrens 	dzp->z_last_itx = seq;
431789Sahrens 	zp->z_last_itx = seq;
432789Sahrens }
433789Sahrens 
434789Sahrens /*
435789Sahrens  * zfs_log_rename() handles TX_RENAME transactions.
436789Sahrens  */
4372638Sperrin void
4385331Samw zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
439789Sahrens 	znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
440789Sahrens {
441789Sahrens 	itx_t *itx;
442789Sahrens 	uint64_t seq;
443789Sahrens 	lr_rename_t *lr;
444789Sahrens 	size_t snamesize = strlen(sname) + 1;
445789Sahrens 	size_t dnamesize = strlen(dname) + 1;
446789Sahrens 
447789Sahrens 	if (zilog == NULL)
4482638Sperrin 		return;
449789Sahrens 
4508227SNeil.Perrin@Sun.COM 	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
4518227SNeil.Perrin@Sun.COM 
452789Sahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
453789Sahrens 	lr = (lr_rename_t *)&itx->itx_lr;
454789Sahrens 	lr->lr_sdoid = sdzp->z_id;
455789Sahrens 	lr->lr_tdoid = tdzp->z_id;
456789Sahrens 	bcopy(sname, (char *)(lr + 1), snamesize);
457789Sahrens 	bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
458789Sahrens 
459789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
460789Sahrens 	sdzp->z_last_itx = seq;
461789Sahrens 	tdzp->z_last_itx = seq;
462789Sahrens 	szp->z_last_itx = seq;
463789Sahrens }
464789Sahrens 
465789Sahrens /*
466789Sahrens  * zfs_log_write() handles TX_WRITE transactions.
467789Sahrens  */
4682237Smaybee ssize_t zfs_immediate_write_sz = 32768;
469789Sahrens 
4702638Sperrin void
471789Sahrens zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
4724620Sperrin 	znode_t *zp, offset_t off, ssize_t resid, int ioflag)
473789Sahrens {
4741669Sperrin 	itx_wr_state_t write_state;
4754620Sperrin 	boolean_t slogging;
4764720Sfr157268 	uintptr_t fsync_cnt;
477789Sahrens 
4783461Sahrens 	if (zilog == NULL || zp->z_unlinked)
4792638Sperrin 		return;
480789Sahrens 
4818227SNeil.Perrin@Sun.COM 	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
4828227SNeil.Perrin@Sun.COM 
4834620Sperrin 	slogging = spa_has_slogs(zilog->zl_spa);
4847360SNeil.Perrin@Sun.COM 	if (resid > zfs_immediate_write_sz && !slogging && resid <= zp->z_blksz)
4851669Sperrin 		write_state = WR_INDIRECT;
4866396Sperrin 	else if (ioflag & (FSYNC | FDSYNC))
4871669Sperrin 		write_state = WR_COPIED;
4883638Sbillm 	else
4891669Sperrin 		write_state = WR_NEED_COPY;
4903638Sbillm 
4914720Sfr157268 	if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
4924720Sfr157268 		(void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
4934720Sfr157268 	}
4944720Sfr157268 
4954620Sperrin 	while (resid) {
4964620Sperrin 		itx_t *itx;
4974620Sperrin 		lr_write_t *lr;
4984620Sperrin 		ssize_t len;
4994620Sperrin 
5004620Sperrin 		/*
5017360SNeil.Perrin@Sun.COM 		 * If the write would overflow the largest block then split it.
5024620Sperrin 		 */
5037360SNeil.Perrin@Sun.COM 		if (write_state != WR_INDIRECT && resid > ZIL_MAX_LOG_DATA)
5044620Sperrin 			len = SPA_MAXBLOCKSIZE >> 1;
5054620Sperrin 		else
5064620Sperrin 			len = resid;
5074620Sperrin 
5084620Sperrin 		itx = zil_itx_create(txtype, sizeof (*lr) +
5094620Sperrin 		    (write_state == WR_COPIED ? len : 0));
5104620Sperrin 		lr = (lr_write_t *)&itx->itx_lr;
5114620Sperrin 		if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
5124620Sperrin 		    zp->z_id, off, len, lr + 1) != 0) {
5133638Sbillm 			kmem_free(itx, offsetof(itx_t, itx_lr) +
5143638Sbillm 			    itx->itx_lr.lrc_reclen);
5151669Sperrin 			itx = zil_itx_create(txtype, sizeof (*lr));
5163638Sbillm 			lr = (lr_write_t *)&itx->itx_lr;
5171669Sperrin 			write_state = WR_NEED_COPY;
5181669Sperrin 		}
5194620Sperrin 
5204620Sperrin 		itx->itx_wr_state = write_state;
5216101Sperrin 		if (write_state == WR_NEED_COPY)
5226101Sperrin 			itx->itx_sod += len;
5234620Sperrin 		lr->lr_foid = zp->z_id;
5244620Sperrin 		lr->lr_offset = off;
5254620Sperrin 		lr->lr_length = len;
5264620Sperrin 		lr->lr_blkoff = 0;
5274620Sperrin 		BP_ZERO(&lr->lr_blkptr);
5283638Sbillm 
5294620Sperrin 		itx->itx_private = zp->z_zfsvfs;
530789Sahrens 
5316396Sperrin 		if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0) ||
5326396Sperrin 		    (ioflag & (FSYNC | FDSYNC)))
5334720Sfr157268 			itx->itx_sync = B_TRUE;
5344720Sfr157268 		else
5354720Sfr157268 			itx->itx_sync = B_FALSE;
5364720Sfr157268 
5374620Sperrin 		zp->z_last_itx = zil_itx_assign(zilog, itx, tx);
538789Sahrens 
5394620Sperrin 		off += len;
5404620Sperrin 		resid -= len;
5414620Sperrin 	}
542789Sahrens }
543789Sahrens 
544789Sahrens /*
545789Sahrens  * zfs_log_truncate() handles TX_TRUNCATE transactions.
546789Sahrens  */
5472638Sperrin void
548789Sahrens zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
549789Sahrens 	znode_t *zp, uint64_t off, uint64_t len)
550789Sahrens {
551789Sahrens 	itx_t *itx;
552789Sahrens 	uint64_t seq;
553789Sahrens 	lr_truncate_t *lr;
554789Sahrens 
5553461Sahrens 	if (zilog == NULL || zp->z_unlinked)
5562638Sperrin 		return;
557789Sahrens 
5588227SNeil.Perrin@Sun.COM 	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
5598227SNeil.Perrin@Sun.COM 
560789Sahrens 	itx = zil_itx_create(txtype, sizeof (*lr));
561789Sahrens 	lr = (lr_truncate_t *)&itx->itx_lr;
562789Sahrens 	lr->lr_foid = zp->z_id;
563789Sahrens 	lr->lr_offset = off;
564789Sahrens 	lr->lr_length = len;
565789Sahrens 
5663063Sperrin 	itx->itx_sync = (zp->z_sync_cnt != 0);
567789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
568789Sahrens 	zp->z_last_itx = seq;
569789Sahrens }
570789Sahrens 
571789Sahrens /*
572789Sahrens  * zfs_log_setattr() handles TX_SETATTR transactions.
573789Sahrens  */
5742638Sperrin void
575789Sahrens zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
5765331Samw 	znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
577789Sahrens {
5785331Samw 	itx_t		*itx;
5795331Samw 	uint64_t	seq;
5805331Samw 	lr_setattr_t	*lr;
5815331Samw 	xvattr_t	*xvap = (xvattr_t *)vap;
5825331Samw 	size_t		recsize = sizeof (lr_setattr_t);
5835331Samw 	void		*start;
5845331Samw 
585789Sahrens 
5863461Sahrens 	if (zilog == NULL || zp->z_unlinked)
5872638Sperrin 		return;
588789Sahrens 
5898227SNeil.Perrin@Sun.COM 	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
5908227SNeil.Perrin@Sun.COM 
5915331Samw 	/*
5925331Samw 	 * If XVATTR set, then log record size needs to allow
5935331Samw 	 * for lr_attr_t + xvattr mask, mapsize and create time
5945331Samw 	 * plus actual attribute values
5955331Samw 	 */
5965331Samw 	if (vap->va_mask & AT_XVATTR)
5975331Samw 		recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
5985331Samw 
5995331Samw 	if (fuidp)
6005331Samw 		recsize += fuidp->z_domain_str_sz;
6015331Samw 
6025331Samw 	itx = zil_itx_create(txtype, recsize);
603789Sahrens 	lr = (lr_setattr_t *)&itx->itx_lr;
604789Sahrens 	lr->lr_foid = zp->z_id;
605789Sahrens 	lr->lr_mask = (uint64_t)mask_applied;
606789Sahrens 	lr->lr_mode = (uint64_t)vap->va_mode;
6075331Samw 	if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
6085331Samw 		lr->lr_uid = fuidp->z_fuid_owner;
6095331Samw 	else
6105331Samw 		lr->lr_uid = (uint64_t)vap->va_uid;
6115331Samw 
6125331Samw 	if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
6135331Samw 		lr->lr_gid = fuidp->z_fuid_group;
6145331Samw 	else
6155331Samw 		lr->lr_gid = (uint64_t)vap->va_gid;
6165331Samw 
617789Sahrens 	lr->lr_size = (uint64_t)vap->va_size;
618789Sahrens 	ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
619789Sahrens 	ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
6205331Samw 	start = (lr_setattr_t *)(lr + 1);
6215331Samw 	if (vap->va_mask & AT_XVATTR) {
6225331Samw 		zfs_log_xvattr((lr_attr_t *)start, xvap);
6235331Samw 		start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
6245331Samw 	}
6255331Samw 
6265331Samw 	/*
6275331Samw 	 * Now stick on domain information if any on end
6285331Samw 	 */
6295331Samw 
6305331Samw 	if (fuidp)
6315331Samw 		(void) zfs_log_fuid_domains(fuidp, start);
632789Sahrens 
6333063Sperrin 	itx->itx_sync = (zp->z_sync_cnt != 0);
634789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
635789Sahrens 	zp->z_last_itx = seq;
636789Sahrens }
637789Sahrens 
638789Sahrens /*
639789Sahrens  * zfs_log_acl() handles TX_ACL transactions.
640789Sahrens  */
6412638Sperrin void
6425331Samw zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
6435331Samw     vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
644789Sahrens {
645789Sahrens 	itx_t *itx;
646789Sahrens 	uint64_t seq;
6475331Samw 	lr_acl_v0_t *lrv0;
648789Sahrens 	lr_acl_t *lr;
6495331Samw 	int txtype;
6505331Samw 	int lrsize;
6515331Samw 	size_t txsize;
6525331Samw 	size_t aclbytes = vsecp->vsa_aclentsz;
6535331Samw 
6546514Smarks 	if (zilog == NULL || zp->z_unlinked)
6556514Smarks 		return;
6566514Smarks 
6578227SNeil.Perrin@Sun.COM 	ZFS_HANDLE_REPLAY(zilog, tx); /* exits if replay */
6588227SNeil.Perrin@Sun.COM 
6596514Smarks 	txtype = (zp->z_zfsvfs->z_version < ZPL_VERSION_FUID) ?
6605331Samw 	    TX_ACL_V0 : TX_ACL;
6615331Samw 
6625331Samw 	if (txtype == TX_ACL)
6635331Samw 		lrsize = sizeof (*lr);
6645331Samw 	else
6655331Samw 		lrsize = sizeof (*lrv0);
666789Sahrens 
6675435Smarks 	txsize = lrsize +
6685435Smarks 	    ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
6695435Smarks 	    (fuidp ? fuidp->z_domain_str_sz : 0) +
6706514Smarks 	    sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
6715331Samw 
6725331Samw 	itx = zil_itx_create(txtype, txsize);
6735331Samw 
674789Sahrens 	lr = (lr_acl_t *)&itx->itx_lr;
675789Sahrens 	lr->lr_foid = zp->z_id;
6765331Samw 	if (txtype == TX_ACL) {
6775331Samw 		lr->lr_acl_bytes = aclbytes;
6785331Samw 		lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
6795331Samw 		lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
6805331Samw 		if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
6815331Samw 			lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
6825331Samw 		else
6835331Samw 			lr->lr_acl_flags = 0;
6845331Samw 	}
6855331Samw 	lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
6865331Samw 
6875331Samw 	if (txtype == TX_ACL_V0) {
6885331Samw 		lrv0 = (lr_acl_v0_t *)lr;
6895331Samw 		bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
6905331Samw 	} else {
6915331Samw 		void *start = (ace_t *)(lr + 1);
6925331Samw 
6935331Samw 		bcopy(vsecp->vsa_aclentp, start, aclbytes);
6945331Samw 
6955435Smarks 		start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
6965331Samw 
6975331Samw 		if (fuidp) {
6985331Samw 			start = zfs_log_fuid_ids(fuidp, start);
6995331Samw 			(void) zfs_log_fuid_domains(fuidp, start);
7005331Samw 		}
7015331Samw 	}
702789Sahrens 
7033063Sperrin 	itx->itx_sync = (zp->z_sync_cnt != 0);
704789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
705789Sahrens 	zp->z_last_itx = seq;
706789Sahrens }
707