xref: /onnv-gate/usr/src/uts/common/fs/zfs/zfs_log.c (revision 5435:1be0be66916d)
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 /*
223461Sahrens  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/types.h>
29789Sahrens #include <sys/param.h>
30789Sahrens #include <sys/systm.h>
31789Sahrens #include <sys/sysmacros.h>
32789Sahrens #include <sys/cmn_err.h>
33789Sahrens #include <sys/kmem.h>
34789Sahrens #include <sys/thread.h>
35789Sahrens #include <sys/file.h>
36789Sahrens #include <sys/vfs.h>
37789Sahrens #include <sys/zfs_znode.h>
38789Sahrens #include <sys/zfs_dir.h>
39789Sahrens #include <sys/zil.h>
404620Sperrin #include <sys/zil_impl.h>
41789Sahrens #include <sys/byteorder.h>
42789Sahrens #include <sys/policy.h>
43789Sahrens #include <sys/stat.h>
44789Sahrens #include <sys/mode.h>
45789Sahrens #include <sys/acl.h>
46789Sahrens #include <sys/dmu.h>
47789Sahrens #include <sys/spa.h>
485331Samw #include <sys/zfs_fuid.h>
49789Sahrens #include <sys/ddi.h>
50789Sahrens 
51789Sahrens /*
52789Sahrens  * All the functions in this file are used to construct the log entries
535331Samw  * to record transactions. They allocate * an intent log transaction
54789Sahrens  * structure (itx_t) and save within it all the information necessary to
55789Sahrens  * possibly replay the transaction. The itx is then assigned a sequence
56789Sahrens  * number and inserted in the in-memory list anchored in the zilog.
57789Sahrens  */
58789Sahrens 
595331Samw int
605331Samw zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
615331Samw {
625331Samw 	int isxvattr = (vap->va_mask & AT_XVATTR);
635331Samw 	switch (type) {
645331Samw 	case Z_FILE:
655331Samw 		if (vsecp == NULL && !isxvattr)
665331Samw 			return (TX_CREATE);
675331Samw 		if (vsecp && isxvattr)
685331Samw 			return (TX_CREATE_ACL_ATTR);
695331Samw 		if (vsecp)
705331Samw 			return (TX_CREATE_ACL);
715331Samw 		else
725331Samw 			return (TX_CREATE_ATTR);
735331Samw 		/*NOTREACHED*/
745331Samw 	case Z_DIR:
755331Samw 		if (vsecp == NULL && !isxvattr)
765331Samw 			return (TX_MKDIR);
775331Samw 		if (vsecp && isxvattr)
785331Samw 			return (TX_MKDIR_ACL_ATTR);
795331Samw 		if (vsecp)
805331Samw 			return (TX_MKDIR_ACL);
815331Samw 		else
825331Samw 			return (TX_MKDIR_ATTR);
835331Samw 	case Z_XATTRDIR:
845331Samw 		return (TX_MKXATTR);
855331Samw 	}
865331Samw 	ASSERT(0);
875331Samw 	return (TX_MAX_TYPE);
885331Samw }
895331Samw 
90789Sahrens /*
915331Samw  * build up the log data necessary for logging xvattr_t
925331Samw  * First lr_attr_t is initialized.  following the lr_attr_t
935331Samw  * is the mapsize and attribute bitmap copied from the xvattr_t.
945331Samw  * Following the bitmap and bitmapsize two 64 bit words are reserved
955331Samw  * for the create time which may be set.  Following the create time
965331Samw  * records a single 64 bit integer which has the bits to set on
975331Samw  * replay for the xvattr.
985331Samw  */
995331Samw static void
1005331Samw zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
1015331Samw {
1025331Samw 	uint32_t	*bitmap;
1035331Samw 	uint64_t	*attrs;
1045331Samw 	uint64_t	*crtime;
1055331Samw 	xoptattr_t	*xoap;
1065331Samw 	void		*scanstamp;
1075331Samw 	int		i;
1085331Samw 
1095331Samw 	xoap = xva_getxoptattr(xvap);
1105331Samw 	ASSERT(xoap);
1115331Samw 
1125331Samw 	lrattr->lr_attr_masksize = xvap->xva_mapsize;
1135331Samw 	bitmap = &lrattr->lr_attr_bitmap;
1145331Samw 	for (i = 0; i != xvap->xva_mapsize; i++, bitmap++) {
1155331Samw 		*bitmap = xvap->xva_reqattrmap[i];
1165331Samw 	}
1175331Samw 
1185331Samw 	/* Now pack the attributes up in a single uint64_t */
1195331Samw 	attrs = (uint64_t *)bitmap;
1205331Samw 	crtime = attrs + 1;
1215331Samw 	scanstamp = (caddr_t)(crtime + 2);
1225331Samw 	*attrs = 0;
1235331Samw 	if (XVA_ISSET_REQ(xvap, XAT_READONLY))
1245331Samw 		*attrs |= (xoap->xoa_readonly == 0) ? 0 :
1255331Samw 		    XAT0_READONLY;
1265331Samw 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
1275331Samw 		*attrs |= (xoap->xoa_hidden == 0) ? 0 :
1285331Samw 		    XAT0_HIDDEN;
1295331Samw 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
1305331Samw 		*attrs |= (xoap->xoa_system == 0) ? 0 :
1315331Samw 		    XAT0_SYSTEM;
1325331Samw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
1335331Samw 		*attrs |= (xoap->xoa_archive == 0) ? 0 :
1345331Samw 		    XAT0_ARCHIVE;
1355331Samw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
1365331Samw 		*attrs |= (xoap->xoa_immutable == 0) ? 0 :
1375331Samw 		    XAT0_IMMUTABLE;
1385331Samw 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
1395331Samw 		*attrs |= (xoap->xoa_nounlink == 0) ? 0 :
1405331Samw 		    XAT0_NOUNLINK;
1415331Samw 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
1425331Samw 		*attrs |= (xoap->xoa_appendonly == 0) ? 0 :
1435331Samw 		    XAT0_APPENDONLY;
1445331Samw 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
1455331Samw 		*attrs |= (xoap->xoa_opaque == 0) ? 0 :
1465331Samw 		    XAT0_APPENDONLY;
1475331Samw 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
1485331Samw 		*attrs |= (xoap->xoa_nodump == 0) ? 0 :
1495331Samw 		    XAT0_NODUMP;
1505331Samw 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
1515331Samw 		*attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
1525331Samw 		    XAT0_AV_QUARANTINED;
1535331Samw 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
1545331Samw 		*attrs |= (xoap->xoa_av_modified == 0) ? 0 :
1555331Samw 		    XAT0_AV_MODIFIED;
1565331Samw 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
1575331Samw 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, crtime);
1585331Samw 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
1595331Samw 		bcopy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
1605331Samw }
1615331Samw 
1625331Samw static void *
1635331Samw zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
1645331Samw {
1655331Samw 	zfs_fuid_t *zfuid;
1665331Samw 	uint64_t *fuidloc = start;
1675331Samw 
1685331Samw 	/* First copy in the ACE FUIDs */
1695331Samw 	for (zfuid = list_head(&fuidp->z_fuids); zfuid;
1705331Samw 	    zfuid = list_next(&fuidp->z_fuids, zfuid)) {
1715331Samw 		*fuidloc++ = zfuid->z_logfuid;
1725331Samw 	}
1735331Samw 	return (fuidloc);
1745331Samw }
1755331Samw 
1765331Samw 
1775331Samw static void *
1785331Samw zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
1795331Samw {
1805331Samw 	zfs_fuid_domain_t *zdomain;
1815331Samw 
1825331Samw 	/* now copy in the domain info, if any */
1835331Samw 	if (fuidp->z_domain_str_sz != 0) {
1845331Samw 		for (zdomain = list_head(&fuidp->z_domains); zdomain;
1855331Samw 		    zdomain = list_next(&fuidp->z_domains, zdomain)) {
1865331Samw 			bcopy((void *)zdomain->z_domain, start,
1875331Samw 			    strlen(zdomain->z_domain) + 1);
1885331Samw 			start = (caddr_t)start +
1895331Samw 			    strlen(zdomain->z_domain) + 1;
1905331Samw 		}
1915331Samw 	}
1925331Samw 	return (start);
1935331Samw }
1945331Samw 
1955331Samw /*
1965331Samw  * zfs_log_create() is used to handle TX_CREATE, TX_CREATE_ATTR, TX_MKDIR,
1975331Samw  * TX_MKDIR_ATTR and TX_MKXATTR
198789Sahrens  * transactions.
1995331Samw  *
2005331Samw  * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
2015331Samw  * domain information appended prior to the name.  In this case the
2025331Samw  * uid/gid in the log record will be a log centric FUID.
2035331Samw  *
2045331Samw  * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
2055331Samw  * may contain attributes, ACL and optional fuid information.
2065331Samw  *
2075331Samw  * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
2085331Samw  * and ACL and normal users/groups in the ACEs.
2095331Samw  *
2105331Samw  * There may be an optional xvattr attribute information similar
2115331Samw  * to zfs_log_setattr.
2125331Samw  *
2135331Samw  * Also, after the file name "domain" strings may be appended.
214789Sahrens  */
2152638Sperrin void
2165331Samw zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
2175331Samw     znode_t *dzp, znode_t *zp, char *name, vsecattr_t *vsecp,
2185331Samw     zfs_fuid_info_t *fuidp, vattr_t *vap)
219789Sahrens {
220789Sahrens 	itx_t *itx;
221789Sahrens 	uint64_t seq;
222789Sahrens 	lr_create_t *lr;
2235331Samw 	lr_acl_create_t *lracl;
2245331Samw 	size_t aclsize;
2255331Samw 	size_t xvatsize = 0;
2265331Samw 	size_t txsize;
2275331Samw 	xvattr_t *xvap = (xvattr_t *)vap;
2285331Samw 	void *end;
2295331Samw 	size_t lrsize;
2305331Samw 
231789Sahrens 	size_t namesize = strlen(name) + 1;
2325331Samw 	size_t fuidsz = 0;
233789Sahrens 
234789Sahrens 	if (zilog == NULL)
2352638Sperrin 		return;
236789Sahrens 
2375331Samw 	/*
2385331Samw 	 * If we have FUIDs present then add in space for
2395331Samw 	 * domains and ACE fuid's if any.
2405331Samw 	 */
2415331Samw 	if (fuidp) {
2425331Samw 		fuidsz += fuidp->z_domain_str_sz;
2435331Samw 		fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
2445331Samw 	}
2455331Samw 
2465331Samw 	if (vap->va_mask & AT_XVATTR)
2475331Samw 		xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
2485331Samw 
2495331Samw 	if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
2505331Samw 	    (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
2515331Samw 	    (int)txtype == TX_MKXATTR) {
2525331Samw 		txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
2535331Samw 		lrsize = sizeof (*lr);
2545331Samw 	} else {
2555331Samw 		aclsize = (vsecp) ? vsecp->vsa_aclentsz : 0;
2565331Samw 		txsize =
2575331Samw 		    sizeof (lr_acl_create_t) + namesize + fuidsz +
258*5435Smarks 		    ZIL_ACE_LENGTH(aclsize) + xvatsize;
2595331Samw 		lrsize = sizeof (lr_acl_create_t);
2605331Samw 	}
2615331Samw 
2625331Samw 	itx = zil_itx_create(txtype, txsize);
2635331Samw 
264789Sahrens 	lr = (lr_create_t *)&itx->itx_lr;
265789Sahrens 	lr->lr_doid = dzp->z_id;
266789Sahrens 	lr->lr_foid = zp->z_id;
267789Sahrens 	lr->lr_mode = zp->z_phys->zp_mode;
2685331Samw 	if (!IS_EPHEMERAL(zp->z_phys->zp_uid)) {
2695331Samw 		lr->lr_uid = (uint64_t)zp->z_phys->zp_uid;
2705331Samw 	} else {
2715331Samw 		lr->lr_uid = fuidp->z_fuid_owner;
2725331Samw 	}
2735331Samw 	if (!IS_EPHEMERAL(zp->z_phys->zp_gid)) {
2745331Samw 		lr->lr_gid = (uint64_t)zp->z_phys->zp_gid;
2755331Samw 	} else {
2765331Samw 		lr->lr_gid = fuidp->z_fuid_group;
2775331Samw 	}
278789Sahrens 	lr->lr_gen = zp->z_phys->zp_gen;
279789Sahrens 	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
280789Sahrens 	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
281789Sahrens 	lr->lr_rdev = zp->z_phys->zp_rdev;
2825331Samw 
2835331Samw 	/*
2845331Samw 	 * Fill in xvattr info if any
2855331Samw 	 */
2865331Samw 	if (vap->va_mask & AT_XVATTR) {
2875331Samw 		zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
2885331Samw 		end = (caddr_t)lr + lrsize + xvatsize;
2895331Samw 	} else {
2905331Samw 		end = (caddr_t)lr + lrsize;
2915331Samw 	}
2925331Samw 
2935331Samw 	/* Now fill in any ACL info */
2945331Samw 
2955331Samw 	if (vsecp) {
2965331Samw 		lracl = (lr_acl_create_t *)&itx->itx_lr;
2975331Samw 		lracl->lr_aclcnt = vsecp->vsa_aclcnt;
2985331Samw 		lracl->lr_acl_bytes = aclsize;
2995331Samw 		lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
3005331Samw 		lracl->lr_fuidcnt  = fuidp ? fuidp->z_fuid_cnt : 0;
3015331Samw 		if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
3025331Samw 			lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
3035331Samw 		else
3045331Samw 			lracl->lr_acl_flags = 0;
3055331Samw 
3065331Samw 		bcopy(vsecp->vsa_aclentp, end, aclsize);
307*5435Smarks 		end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
3085331Samw 	}
3095331Samw 
3105331Samw 	/* drop in FUID info */
3115331Samw 	if (fuidp) {
3125331Samw 		end = zfs_log_fuid_ids(fuidp, end);
3135331Samw 		end = zfs_log_fuid_domains(fuidp, end);
3145331Samw 	}
3155331Samw 	/*
3165331Samw 	 * Now place file name in log record
3175331Samw 	 */
3185331Samw 	bcopy(name, end, namesize);
319789Sahrens 
320789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
321789Sahrens 	dzp->z_last_itx = seq;
322789Sahrens 	zp->z_last_itx = seq;
323789Sahrens }
324789Sahrens 
325789Sahrens /*
326789Sahrens  * zfs_log_remove() handles both TX_REMOVE and TX_RMDIR transactions.
327789Sahrens  */
3282638Sperrin void
3295331Samw zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
330789Sahrens 	znode_t *dzp, char *name)
331789Sahrens {
332789Sahrens 	itx_t *itx;
333789Sahrens 	uint64_t seq;
334789Sahrens 	lr_remove_t *lr;
335789Sahrens 	size_t namesize = strlen(name) + 1;
336789Sahrens 
337789Sahrens 	if (zilog == NULL)
3382638Sperrin 		return;
339789Sahrens 
340789Sahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
341789Sahrens 	lr = (lr_remove_t *)&itx->itx_lr;
342789Sahrens 	lr->lr_doid = dzp->z_id;
343789Sahrens 	bcopy(name, (char *)(lr + 1), namesize);
344789Sahrens 
345789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
346789Sahrens 	dzp->z_last_itx = seq;
347789Sahrens }
348789Sahrens 
349789Sahrens /*
350789Sahrens  * zfs_log_link() handles TX_LINK transactions.
351789Sahrens  */
3522638Sperrin void
3535331Samw zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
354789Sahrens 	znode_t *dzp, znode_t *zp, char *name)
355789Sahrens {
356789Sahrens 	itx_t *itx;
357789Sahrens 	uint64_t seq;
358789Sahrens 	lr_link_t *lr;
359789Sahrens 	size_t namesize = strlen(name) + 1;
360789Sahrens 
361789Sahrens 	if (zilog == NULL)
3622638Sperrin 		return;
363789Sahrens 
364789Sahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
365789Sahrens 	lr = (lr_link_t *)&itx->itx_lr;
366789Sahrens 	lr->lr_doid = dzp->z_id;
367789Sahrens 	lr->lr_link_obj = zp->z_id;
368789Sahrens 	bcopy(name, (char *)(lr + 1), namesize);
369789Sahrens 
370789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
371789Sahrens 	dzp->z_last_itx = seq;
372789Sahrens 	zp->z_last_itx = seq;
373789Sahrens }
374789Sahrens 
375789Sahrens /*
376789Sahrens  * zfs_log_symlink() handles TX_SYMLINK transactions.
377789Sahrens  */
3782638Sperrin void
3795331Samw zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
3805331Samw     znode_t *dzp, znode_t *zp, char *name, char *link)
381789Sahrens {
382789Sahrens 	itx_t *itx;
383789Sahrens 	uint64_t seq;
384789Sahrens 	lr_create_t *lr;
385789Sahrens 	size_t namesize = strlen(name) + 1;
386789Sahrens 	size_t linksize = strlen(link) + 1;
387789Sahrens 
388789Sahrens 	if (zilog == NULL)
3892638Sperrin 		return;
390789Sahrens 
391789Sahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
392789Sahrens 	lr = (lr_create_t *)&itx->itx_lr;
393789Sahrens 	lr->lr_doid = dzp->z_id;
394789Sahrens 	lr->lr_foid = zp->z_id;
395789Sahrens 	lr->lr_mode = zp->z_phys->zp_mode;
396789Sahrens 	lr->lr_uid = zp->z_phys->zp_uid;
397789Sahrens 	lr->lr_gid = zp->z_phys->zp_gid;
398789Sahrens 	lr->lr_gen = zp->z_phys->zp_gen;
399789Sahrens 	lr->lr_crtime[0] = zp->z_phys->zp_crtime[0];
400789Sahrens 	lr->lr_crtime[1] = zp->z_phys->zp_crtime[1];
401789Sahrens 	bcopy(name, (char *)(lr + 1), namesize);
402789Sahrens 	bcopy(link, (char *)(lr + 1) + namesize, linksize);
403789Sahrens 
404789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
405789Sahrens 	dzp->z_last_itx = seq;
406789Sahrens 	zp->z_last_itx = seq;
407789Sahrens }
408789Sahrens 
409789Sahrens /*
410789Sahrens  * zfs_log_rename() handles TX_RENAME transactions.
411789Sahrens  */
4122638Sperrin void
4135331Samw zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
414789Sahrens 	znode_t *sdzp, char *sname, znode_t *tdzp, char *dname, znode_t *szp)
415789Sahrens {
416789Sahrens 	itx_t *itx;
417789Sahrens 	uint64_t seq;
418789Sahrens 	lr_rename_t *lr;
419789Sahrens 	size_t snamesize = strlen(sname) + 1;
420789Sahrens 	size_t dnamesize = strlen(dname) + 1;
421789Sahrens 
422789Sahrens 	if (zilog == NULL)
4232638Sperrin 		return;
424789Sahrens 
425789Sahrens 	itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
426789Sahrens 	lr = (lr_rename_t *)&itx->itx_lr;
427789Sahrens 	lr->lr_sdoid = sdzp->z_id;
428789Sahrens 	lr->lr_tdoid = tdzp->z_id;
429789Sahrens 	bcopy(sname, (char *)(lr + 1), snamesize);
430789Sahrens 	bcopy(dname, (char *)(lr + 1) + snamesize, dnamesize);
431789Sahrens 
432789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
433789Sahrens 	sdzp->z_last_itx = seq;
434789Sahrens 	tdzp->z_last_itx = seq;
435789Sahrens 	szp->z_last_itx = seq;
436789Sahrens }
437789Sahrens 
438789Sahrens /*
439789Sahrens  * zfs_log_write() handles TX_WRITE transactions.
440789Sahrens  */
4412237Smaybee ssize_t zfs_immediate_write_sz = 32768;
442789Sahrens 
4434620Sperrin #define	ZIL_MAX_LOG_DATA (SPA_MAXBLOCKSIZE - sizeof (zil_trailer_t) - \
4444620Sperrin     sizeof (lr_write_t))
4454620Sperrin 
4462638Sperrin void
447789Sahrens zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
4484620Sperrin 	znode_t *zp, offset_t off, ssize_t resid, int ioflag)
449789Sahrens {
4501669Sperrin 	itx_wr_state_t write_state;
4514620Sperrin 	boolean_t slogging;
4524720Sfr157268 	uintptr_t fsync_cnt;
453789Sahrens 
4543461Sahrens 	if (zilog == NULL || zp->z_unlinked)
4552638Sperrin 		return;
456789Sahrens 
4571669Sperrin 	/*
4581669Sperrin 	 * Writes are handled in three different ways:
4591669Sperrin 	 *
4601669Sperrin 	 * WR_INDIRECT:
4614620Sperrin 	 *    If the write is greater than zfs_immediate_write_sz and there are
4624620Sperrin 	 *    no separate logs in this pool then later *if* we need to log the
4634620Sperrin 	 *    write then dmu_sync() is used to immediately write the block and
4644620Sperrin 	 *    its block pointer is put in the log record.
4651669Sperrin 	 * WR_COPIED:
4661669Sperrin 	 *    If we know we'll immediately be committing the
4671669Sperrin 	 *    transaction (FDSYNC (O_DSYNC)), the we allocate a larger
4681669Sperrin 	 *    log record here for the data and copy the data in.
4691669Sperrin 	 * WR_NEED_COPY:
4701669Sperrin 	 *    Otherwise we don't allocate a buffer, and *if* we need to
4711669Sperrin 	 *    flush the write later then a buffer is allocated and
4721669Sperrin 	 *    we retrieve the data using the dmu.
4731669Sperrin 	 */
4744620Sperrin 	slogging = spa_has_slogs(zilog->zl_spa);
4754620Sperrin 	if (resid > zfs_immediate_write_sz && !slogging)
4761669Sperrin 		write_state = WR_INDIRECT;
4773638Sbillm 	else if (ioflag & FDSYNC)
4781669Sperrin 		write_state = WR_COPIED;
4793638Sbillm 	else
4801669Sperrin 		write_state = WR_NEED_COPY;
4813638Sbillm 
4824720Sfr157268 	if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
4834720Sfr157268 		(void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
4844720Sfr157268 	}
4854720Sfr157268 
4864620Sperrin 	while (resid) {
4874620Sperrin 		itx_t *itx;
4884620Sperrin 		lr_write_t *lr;
4894620Sperrin 		ssize_t len;
4904620Sperrin 
4914620Sperrin 		/*
4924620Sperrin 		 * If there are slogs and the write would overflow the largest
4934620Sperrin 		 * block, then because we don't want to use the main pool
4944620Sperrin 		 * to dmu_sync, we have to split the write.
4954620Sperrin 		 */
4964620Sperrin 		if (slogging && resid > ZIL_MAX_LOG_DATA)
4974620Sperrin 			len = SPA_MAXBLOCKSIZE >> 1;
4984620Sperrin 		else
4994620Sperrin 			len = resid;
5004620Sperrin 
5014620Sperrin 		itx = zil_itx_create(txtype, sizeof (*lr) +
5024620Sperrin 		    (write_state == WR_COPIED ? len : 0));
5034620Sperrin 		lr = (lr_write_t *)&itx->itx_lr;
5044620Sperrin 		if (write_state == WR_COPIED && dmu_read(zp->z_zfsvfs->z_os,
5054620Sperrin 		    zp->z_id, off, len, lr + 1) != 0) {
5063638Sbillm 			kmem_free(itx, offsetof(itx_t, itx_lr) +
5073638Sbillm 			    itx->itx_lr.lrc_reclen);
5081669Sperrin 			itx = zil_itx_create(txtype, sizeof (*lr));
5093638Sbillm 			lr = (lr_write_t *)&itx->itx_lr;
5101669Sperrin 			write_state = WR_NEED_COPY;
5111669Sperrin 		}
5124620Sperrin 
5134620Sperrin 		itx->itx_wr_state = write_state;
5144620Sperrin 		lr->lr_foid = zp->z_id;
5154620Sperrin 		lr->lr_offset = off;
5164620Sperrin 		lr->lr_length = len;
5174620Sperrin 		lr->lr_blkoff = 0;
5184620Sperrin 		BP_ZERO(&lr->lr_blkptr);
5193638Sbillm 
5204620Sperrin 		itx->itx_private = zp->z_zfsvfs;
521789Sahrens 
5224720Sfr157268 		if ((zp->z_sync_cnt != 0) || (fsync_cnt != 0))
5234720Sfr157268 			itx->itx_sync = B_TRUE;
5244720Sfr157268 		else
5254720Sfr157268 			itx->itx_sync = B_FALSE;
5264720Sfr157268 
5274620Sperrin 		zp->z_last_itx = zil_itx_assign(zilog, itx, tx);
528789Sahrens 
5294620Sperrin 		off += len;
5304620Sperrin 		resid -= len;
5314620Sperrin 	}
532789Sahrens }
533789Sahrens 
534789Sahrens /*
535789Sahrens  * zfs_log_truncate() handles TX_TRUNCATE transactions.
536789Sahrens  */
5372638Sperrin void
538789Sahrens zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
539789Sahrens 	znode_t *zp, uint64_t off, uint64_t len)
540789Sahrens {
541789Sahrens 	itx_t *itx;
542789Sahrens 	uint64_t seq;
543789Sahrens 	lr_truncate_t *lr;
544789Sahrens 
5453461Sahrens 	if (zilog == NULL || zp->z_unlinked)
5462638Sperrin 		return;
547789Sahrens 
548789Sahrens 	itx = zil_itx_create(txtype, sizeof (*lr));
549789Sahrens 	lr = (lr_truncate_t *)&itx->itx_lr;
550789Sahrens 	lr->lr_foid = zp->z_id;
551789Sahrens 	lr->lr_offset = off;
552789Sahrens 	lr->lr_length = len;
553789Sahrens 
5543063Sperrin 	itx->itx_sync = (zp->z_sync_cnt != 0);
555789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
556789Sahrens 	zp->z_last_itx = seq;
557789Sahrens }
558789Sahrens 
559789Sahrens /*
560789Sahrens  * zfs_log_setattr() handles TX_SETATTR transactions.
561789Sahrens  */
5622638Sperrin void
563789Sahrens zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
5645331Samw 	znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
565789Sahrens {
5665331Samw 	itx_t		*itx;
5675331Samw 	uint64_t	seq;
5685331Samw 	lr_setattr_t	*lr;
5695331Samw 	xvattr_t	*xvap = (xvattr_t *)vap;
5705331Samw 	size_t		recsize = sizeof (lr_setattr_t);
5715331Samw 	void		*start;
5725331Samw 
573789Sahrens 
5743461Sahrens 	if (zilog == NULL || zp->z_unlinked)
5752638Sperrin 		return;
576789Sahrens 
5775331Samw 	/*
5785331Samw 	 * If XVATTR set, then log record size needs to allow
5795331Samw 	 * for lr_attr_t + xvattr mask, mapsize and create time
5805331Samw 	 * plus actual attribute values
5815331Samw 	 */
5825331Samw 	if (vap->va_mask & AT_XVATTR)
5835331Samw 		recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
5845331Samw 
5855331Samw 	if (fuidp)
5865331Samw 		recsize += fuidp->z_domain_str_sz;
5875331Samw 
5885331Samw 	itx = zil_itx_create(txtype, recsize);
589789Sahrens 	lr = (lr_setattr_t *)&itx->itx_lr;
590789Sahrens 	lr->lr_foid = zp->z_id;
591789Sahrens 	lr->lr_mask = (uint64_t)mask_applied;
592789Sahrens 	lr->lr_mode = (uint64_t)vap->va_mode;
5935331Samw 	if ((mask_applied & AT_UID) && IS_EPHEMERAL(vap->va_uid))
5945331Samw 		lr->lr_uid = fuidp->z_fuid_owner;
5955331Samw 	else
5965331Samw 		lr->lr_uid = (uint64_t)vap->va_uid;
5975331Samw 
5985331Samw 	if ((mask_applied & AT_GID) && IS_EPHEMERAL(vap->va_gid))
5995331Samw 		lr->lr_gid = fuidp->z_fuid_group;
6005331Samw 	else
6015331Samw 		lr->lr_gid = (uint64_t)vap->va_gid;
6025331Samw 
603789Sahrens 	lr->lr_size = (uint64_t)vap->va_size;
604789Sahrens 	ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
605789Sahrens 	ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
6065331Samw 	start = (lr_setattr_t *)(lr + 1);
6075331Samw 	if (vap->va_mask & AT_XVATTR) {
6085331Samw 		zfs_log_xvattr((lr_attr_t *)start, xvap);
6095331Samw 		start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
6105331Samw 	}
6115331Samw 
6125331Samw 	/*
6135331Samw 	 * Now stick on domain information if any on end
6145331Samw 	 */
6155331Samw 
6165331Samw 	if (fuidp)
6175331Samw 		(void) zfs_log_fuid_domains(fuidp, start);
618789Sahrens 
6193063Sperrin 	itx->itx_sync = (zp->z_sync_cnt != 0);
620789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
621789Sahrens 	zp->z_last_itx = seq;
622789Sahrens }
623789Sahrens 
624789Sahrens /*
625789Sahrens  * zfs_log_acl() handles TX_ACL transactions.
626789Sahrens  */
6272638Sperrin void
6285331Samw zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
6295331Samw     vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
630789Sahrens {
631789Sahrens 	itx_t *itx;
632789Sahrens 	uint64_t seq;
6335331Samw 	lr_acl_v0_t *lrv0;
634789Sahrens 	lr_acl_t *lr;
6355331Samw 	int txtype;
6365331Samw 	int lrsize;
6375331Samw 	size_t txsize;
6385331Samw 	size_t aclbytes = vsecp->vsa_aclentsz;
6395331Samw 
6405331Samw 	txtype = (zp->z_zfsvfs->z_version == ZPL_VERSION_INITIAL) ?
6415331Samw 	    TX_ACL_V0 : TX_ACL;
6425331Samw 
6435331Samw 	if (txtype == TX_ACL)
6445331Samw 		lrsize = sizeof (*lr);
6455331Samw 	else
6465331Samw 		lrsize = sizeof (*lrv0);
647789Sahrens 
6483461Sahrens 	if (zilog == NULL || zp->z_unlinked)
6492638Sperrin 		return;
650789Sahrens 
651*5435Smarks 	txsize = lrsize +
652*5435Smarks 	    ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
653*5435Smarks 	    (fuidp ? fuidp->z_domain_str_sz : 0) +
6545331Samw 	    sizeof (uint64) * (fuidp ? fuidp->z_fuid_cnt : 0);
6555331Samw 
6565331Samw 	itx = zil_itx_create(txtype, txsize);
6575331Samw 
658789Sahrens 	lr = (lr_acl_t *)&itx->itx_lr;
659789Sahrens 	lr->lr_foid = zp->z_id;
6605331Samw 	if (txtype == TX_ACL) {
6615331Samw 		lr->lr_acl_bytes = aclbytes;
6625331Samw 		lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
6635331Samw 		lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
6645331Samw 		if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
6655331Samw 			lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
6665331Samw 		else
6675331Samw 			lr->lr_acl_flags = 0;
6685331Samw 	}
6695331Samw 	lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
6705331Samw 
6715331Samw 	if (txtype == TX_ACL_V0) {
6725331Samw 		lrv0 = (lr_acl_v0_t *)lr;
6735331Samw 		bcopy(vsecp->vsa_aclentp, (ace_t *)(lrv0 + 1), aclbytes);
6745331Samw 	} else {
6755331Samw 		void *start = (ace_t *)(lr + 1);
6765331Samw 
6775331Samw 		bcopy(vsecp->vsa_aclentp, start, aclbytes);
6785331Samw 
679*5435Smarks 		start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
6805331Samw 
6815331Samw 		if (fuidp) {
6825331Samw 			start = zfs_log_fuid_ids(fuidp, start);
6835331Samw 			(void) zfs_log_fuid_domains(fuidp, start);
6845331Samw 		}
6855331Samw 	}
686789Sahrens 
6873063Sperrin 	itx->itx_sync = (zp->z_sync_cnt != 0);
688789Sahrens 	seq = zil_itx_assign(zilog, itx, tx);
689789Sahrens 	zp->z_last_itx = seq;
690789Sahrens }
691