xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_log.c (revision a0b956f5ac5e0941f9e74e24c1c53e05ad061a38)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23eda14cbcSMatt Macy  * Copyright (c) 2015, 2018 by Delphix. All rights reserved.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy 
27eda14cbcSMatt Macy #include <sys/types.h>
28eda14cbcSMatt Macy #include <sys/param.h>
29eda14cbcSMatt Macy #include <sys/sysmacros.h>
30eda14cbcSMatt Macy #include <sys/cmn_err.h>
31eda14cbcSMatt Macy #include <sys/kmem.h>
32eda14cbcSMatt Macy #include <sys/thread.h>
33eda14cbcSMatt Macy #include <sys/file.h>
34eda14cbcSMatt Macy #include <sys/vfs.h>
35eda14cbcSMatt Macy #include <sys/zfs_znode.h>
36eda14cbcSMatt Macy #include <sys/zfs_dir.h>
37eda14cbcSMatt Macy #include <sys/zil.h>
38eda14cbcSMatt Macy #include <sys/zil_impl.h>
39eda14cbcSMatt Macy #include <sys/byteorder.h>
40eda14cbcSMatt Macy #include <sys/policy.h>
41eda14cbcSMatt Macy #include <sys/stat.h>
42eda14cbcSMatt Macy #include <sys/acl.h>
43eda14cbcSMatt Macy #include <sys/dmu.h>
44eda14cbcSMatt Macy #include <sys/dbuf.h>
45eda14cbcSMatt Macy #include <sys/spa.h>
46eda14cbcSMatt Macy #include <sys/zfs_fuid.h>
47eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
48eda14cbcSMatt Macy 
49eda14cbcSMatt Macy /*
50eda14cbcSMatt Macy  * These zfs_log_* functions must be called within a dmu tx, in one
51eda14cbcSMatt Macy  * of 2 contexts depending on zilog->z_replay:
52eda14cbcSMatt Macy  *
53eda14cbcSMatt Macy  * Non replay mode
54eda14cbcSMatt Macy  * ---------------
55eda14cbcSMatt Macy  * We need to record the transaction so that if it is committed to
56eda14cbcSMatt Macy  * the Intent Log then it can be replayed.  An intent log transaction
57eda14cbcSMatt Macy  * structure (itx_t) is allocated and all the information necessary to
58eda14cbcSMatt Macy  * possibly replay the transaction is saved in it. The itx is then assigned
59eda14cbcSMatt Macy  * a sequence number and inserted in the in-memory list anchored in the zilog.
60eda14cbcSMatt Macy  *
61eda14cbcSMatt Macy  * Replay mode
62eda14cbcSMatt Macy  * -----------
63eda14cbcSMatt Macy  * We need to mark the intent log record as replayed in the log header.
64eda14cbcSMatt Macy  * This is done in the same transaction as the replay so that they
65eda14cbcSMatt Macy  * commit atomically.
66eda14cbcSMatt Macy  */
67eda14cbcSMatt Macy 
68eda14cbcSMatt Macy int
69eda14cbcSMatt Macy zfs_log_create_txtype(zil_create_t type, vsecattr_t *vsecp, vattr_t *vap)
70eda14cbcSMatt Macy {
71eda14cbcSMatt Macy 	int isxvattr = (vap->va_mask & ATTR_XVATTR);
72eda14cbcSMatt Macy 	switch (type) {
73eda14cbcSMatt Macy 	case Z_FILE:
74eda14cbcSMatt Macy 		if (vsecp == NULL && !isxvattr)
75eda14cbcSMatt Macy 			return (TX_CREATE);
76eda14cbcSMatt Macy 		if (vsecp && isxvattr)
77eda14cbcSMatt Macy 			return (TX_CREATE_ACL_ATTR);
78eda14cbcSMatt Macy 		if (vsecp)
79eda14cbcSMatt Macy 			return (TX_CREATE_ACL);
80eda14cbcSMatt Macy 		else
81eda14cbcSMatt Macy 			return (TX_CREATE_ATTR);
82eda14cbcSMatt Macy 	case Z_DIR:
83eda14cbcSMatt Macy 		if (vsecp == NULL && !isxvattr)
84eda14cbcSMatt Macy 			return (TX_MKDIR);
85eda14cbcSMatt Macy 		if (vsecp && isxvattr)
86eda14cbcSMatt Macy 			return (TX_MKDIR_ACL_ATTR);
87eda14cbcSMatt Macy 		if (vsecp)
88eda14cbcSMatt Macy 			return (TX_MKDIR_ACL);
89eda14cbcSMatt Macy 		else
90eda14cbcSMatt Macy 			return (TX_MKDIR_ATTR);
91eda14cbcSMatt Macy 	case Z_XATTRDIR:
92eda14cbcSMatt Macy 		return (TX_MKXATTR);
93eda14cbcSMatt Macy 	}
94eda14cbcSMatt Macy 	ASSERT(0);
95eda14cbcSMatt Macy 	return (TX_MAX_TYPE);
96eda14cbcSMatt Macy }
97eda14cbcSMatt Macy 
98eda14cbcSMatt Macy /*
99eda14cbcSMatt Macy  * build up the log data necessary for logging xvattr_t
100eda14cbcSMatt Macy  * First lr_attr_t is initialized.  following the lr_attr_t
101eda14cbcSMatt Macy  * is the mapsize and attribute bitmap copied from the xvattr_t.
102eda14cbcSMatt Macy  * Following the bitmap and bitmapsize two 64 bit words are reserved
103eda14cbcSMatt Macy  * for the create time which may be set.  Following the create time
104eda14cbcSMatt Macy  * records a single 64 bit integer which has the bits to set on
105eda14cbcSMatt Macy  * replay for the xvattr.
106eda14cbcSMatt Macy  */
107eda14cbcSMatt Macy static void
108eda14cbcSMatt Macy zfs_log_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
109eda14cbcSMatt Macy {
110eda14cbcSMatt Macy 	xoptattr_t *xoap;
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy 	xoap = xva_getxoptattr(xvap);
113eda14cbcSMatt Macy 	ASSERT(xoap);
114eda14cbcSMatt Macy 
115eda14cbcSMatt Macy 	lrattr->lr_attr_masksize = xvap->xva_mapsize;
116*a0b956f5SMartin Matuska 	uint32_t *bitmap = &lrattr->lr_attr_bitmap;
117*a0b956f5SMartin Matuska 	for (int i = 0; i != xvap->xva_mapsize; i++, bitmap++)
118eda14cbcSMatt Macy 		*bitmap = xvap->xva_reqattrmap[i];
119eda14cbcSMatt Macy 
120*a0b956f5SMartin Matuska 	lr_attr_end_t *end = (lr_attr_end_t *)bitmap;
121*a0b956f5SMartin Matuska 	end->lr_attr_attrs = 0;
122*a0b956f5SMartin Matuska 	end->lr_attr_crtime[0] = 0;
123*a0b956f5SMartin Matuska 	end->lr_attr_crtime[1] = 0;
124*a0b956f5SMartin Matuska 	memset(end->lr_attr_scanstamp, 0, AV_SCANSTAMP_SZ);
125*a0b956f5SMartin Matuska 
126eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_READONLY))
127*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_readonly == 0) ? 0 :
128eda14cbcSMatt Macy 		    XAT0_READONLY;
129eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
130*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_hidden == 0) ? 0 :
131eda14cbcSMatt Macy 		    XAT0_HIDDEN;
132eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
133*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_system == 0) ? 0 :
134eda14cbcSMatt Macy 		    XAT0_SYSTEM;
135eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
136*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_archive == 0) ? 0 :
137eda14cbcSMatt Macy 		    XAT0_ARCHIVE;
138eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
139*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_immutable == 0) ? 0 :
140eda14cbcSMatt Macy 		    XAT0_IMMUTABLE;
141eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
142*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_nounlink == 0) ? 0 :
143eda14cbcSMatt Macy 		    XAT0_NOUNLINK;
144eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
145*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_appendonly == 0) ? 0 :
146eda14cbcSMatt Macy 		    XAT0_APPENDONLY;
147eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
148*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_opaque == 0) ? 0 :
149eda14cbcSMatt Macy 		    XAT0_APPENDONLY;
150eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
151*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_nodump == 0) ? 0 :
152eda14cbcSMatt Macy 		    XAT0_NODUMP;
153eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
154*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_av_quarantined == 0) ? 0 :
155eda14cbcSMatt Macy 		    XAT0_AV_QUARANTINED;
156eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
157*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_av_modified == 0) ? 0 :
158eda14cbcSMatt Macy 		    XAT0_AV_MODIFIED;
159eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
160*a0b956f5SMartin Matuska 		ZFS_TIME_ENCODE(&xoap->xoa_createtime, end->lr_attr_crtime);
161eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
162eda14cbcSMatt Macy 		ASSERT(!XVA_ISSET_REQ(xvap, XAT_PROJID));
163eda14cbcSMatt Macy 
164*a0b956f5SMartin Matuska 		memcpy(end->lr_attr_scanstamp, xoap->xoa_av_scanstamp,
165*a0b956f5SMartin Matuska 		    AV_SCANSTAMP_SZ);
166eda14cbcSMatt Macy 	} else if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
167eda14cbcSMatt Macy 		/*
168eda14cbcSMatt Macy 		 * XAT_PROJID and XAT_AV_SCANSTAMP will never be valid
169eda14cbcSMatt Macy 		 * at the same time, so we can share the same space.
170eda14cbcSMatt Macy 		 */
171*a0b956f5SMartin Matuska 		memcpy(end->lr_attr_scanstamp, &xoap->xoa_projid,
172*a0b956f5SMartin Matuska 		    sizeof (uint64_t));
173eda14cbcSMatt Macy 	}
174eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
175*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_reparse == 0) ? 0 :
176eda14cbcSMatt Macy 		    XAT0_REPARSE;
177eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
178*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_offline == 0) ? 0 :
179eda14cbcSMatt Macy 		    XAT0_OFFLINE;
180eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
181*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_sparse == 0) ? 0 :
182eda14cbcSMatt Macy 		    XAT0_SPARSE;
183eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT))
184*a0b956f5SMartin Matuska 		end->lr_attr_attrs |= (xoap->xoa_projinherit == 0) ? 0 :
185eda14cbcSMatt Macy 		    XAT0_PROJINHERIT;
186eda14cbcSMatt Macy }
187eda14cbcSMatt Macy 
188eda14cbcSMatt Macy static void *
189eda14cbcSMatt Macy zfs_log_fuid_ids(zfs_fuid_info_t *fuidp, void *start)
190eda14cbcSMatt Macy {
191eda14cbcSMatt Macy 	zfs_fuid_t *zfuid;
192eda14cbcSMatt Macy 	uint64_t *fuidloc = start;
193eda14cbcSMatt Macy 
194eda14cbcSMatt Macy 	/* First copy in the ACE FUIDs */
195eda14cbcSMatt Macy 	for (zfuid = list_head(&fuidp->z_fuids); zfuid;
196eda14cbcSMatt Macy 	    zfuid = list_next(&fuidp->z_fuids, zfuid)) {
197eda14cbcSMatt Macy 		*fuidloc++ = zfuid->z_logfuid;
198eda14cbcSMatt Macy 	}
199eda14cbcSMatt Macy 	return (fuidloc);
200eda14cbcSMatt Macy }
201eda14cbcSMatt Macy 
202eda14cbcSMatt Macy 
203eda14cbcSMatt Macy static void *
204eda14cbcSMatt Macy zfs_log_fuid_domains(zfs_fuid_info_t *fuidp, void *start)
205eda14cbcSMatt Macy {
206eda14cbcSMatt Macy 	zfs_fuid_domain_t *zdomain;
207eda14cbcSMatt Macy 
208eda14cbcSMatt Macy 	/* now copy in the domain info, if any */
209eda14cbcSMatt Macy 	if (fuidp->z_domain_str_sz != 0) {
210eda14cbcSMatt Macy 		for (zdomain = list_head(&fuidp->z_domains); zdomain;
211eda14cbcSMatt Macy 		    zdomain = list_next(&fuidp->z_domains, zdomain)) {
212da5137abSMartin Matuska 			memcpy(start, zdomain->z_domain,
213eda14cbcSMatt Macy 			    strlen(zdomain->z_domain) + 1);
214eda14cbcSMatt Macy 			start = (caddr_t)start +
215eda14cbcSMatt Macy 			    strlen(zdomain->z_domain) + 1;
216eda14cbcSMatt Macy 		}
217eda14cbcSMatt Macy 	}
218eda14cbcSMatt Macy 	return (start);
219eda14cbcSMatt Macy }
220eda14cbcSMatt Macy 
221eda14cbcSMatt Macy /*
222eda14cbcSMatt Macy  * If zp is an xattr node, check whether the xattr owner is unlinked.
223eda14cbcSMatt Macy  * We don't want to log anything if the owner is unlinked.
224eda14cbcSMatt Macy  */
225eda14cbcSMatt Macy static int
226eda14cbcSMatt Macy zfs_xattr_owner_unlinked(znode_t *zp)
227eda14cbcSMatt Macy {
228eda14cbcSMatt Macy 	int unlinked = 0;
229eda14cbcSMatt Macy 	znode_t *dzp;
230eda14cbcSMatt Macy #ifdef __FreeBSD__
231eda14cbcSMatt Macy 	znode_t *tzp = zp;
232eda14cbcSMatt Macy 
233eda14cbcSMatt Macy 	/*
234eda14cbcSMatt Macy 	 * zrele drops the vnode lock which violates the VOP locking contract
235eda14cbcSMatt Macy 	 * on FreeBSD. See comment at the top of zfs_replay.c for more detail.
236eda14cbcSMatt Macy 	 */
237eda14cbcSMatt Macy 	/*
238eda14cbcSMatt Macy 	 * if zp is XATTR node, keep walking up via z_xattr_parent until we
239eda14cbcSMatt Macy 	 * get the owner
240eda14cbcSMatt Macy 	 */
241eda14cbcSMatt Macy 	while (tzp->z_pflags & ZFS_XATTR) {
242eda14cbcSMatt Macy 		ASSERT3U(zp->z_xattr_parent, !=, 0);
243eda14cbcSMatt Macy 		if (zfs_zget(ZTOZSB(tzp), tzp->z_xattr_parent, &dzp) != 0) {
244eda14cbcSMatt Macy 			unlinked = 1;
245eda14cbcSMatt Macy 			break;
246eda14cbcSMatt Macy 		}
247eda14cbcSMatt Macy 
248eda14cbcSMatt Macy 		if (tzp != zp)
249eda14cbcSMatt Macy 			zrele(tzp);
250eda14cbcSMatt Macy 		tzp = dzp;
251eda14cbcSMatt Macy 		unlinked = tzp->z_unlinked;
252eda14cbcSMatt Macy 	}
253eda14cbcSMatt Macy 	if (tzp != zp)
254eda14cbcSMatt Macy 		zrele(tzp);
255eda14cbcSMatt Macy #else
256eda14cbcSMatt Macy 	zhold(zp);
257eda14cbcSMatt Macy 	/*
258eda14cbcSMatt Macy 	 * if zp is XATTR node, keep walking up via z_xattr_parent until we
259eda14cbcSMatt Macy 	 * get the owner
260eda14cbcSMatt Macy 	 */
261eda14cbcSMatt Macy 	while (zp->z_pflags & ZFS_XATTR) {
262eda14cbcSMatt Macy 		ASSERT3U(zp->z_xattr_parent, !=, 0);
263eda14cbcSMatt Macy 		if (zfs_zget(ZTOZSB(zp), zp->z_xattr_parent, &dzp) != 0) {
264eda14cbcSMatt Macy 			unlinked = 1;
265eda14cbcSMatt Macy 			break;
266eda14cbcSMatt Macy 		}
267eda14cbcSMatt Macy 
268eda14cbcSMatt Macy 		zrele(zp);
269eda14cbcSMatt Macy 		zp = dzp;
270eda14cbcSMatt Macy 		unlinked = zp->z_unlinked;
271eda14cbcSMatt Macy 	}
272eda14cbcSMatt Macy 	zrele(zp);
273eda14cbcSMatt Macy #endif
274eda14cbcSMatt Macy 	return (unlinked);
275eda14cbcSMatt Macy }
276eda14cbcSMatt Macy 
277eda14cbcSMatt Macy /*
278eda14cbcSMatt Macy  * Handles TX_CREATE, TX_CREATE_ATTR, TX_MKDIR, TX_MKDIR_ATTR and
279eda14cbcSMatt Macy  * TK_MKXATTR transactions.
280eda14cbcSMatt Macy  *
281eda14cbcSMatt Macy  * TX_CREATE and TX_MKDIR are standard creates, but they may have FUID
282eda14cbcSMatt Macy  * domain information appended prior to the name.  In this case the
283eda14cbcSMatt Macy  * uid/gid in the log record will be a log centric FUID.
284eda14cbcSMatt Macy  *
285eda14cbcSMatt Macy  * TX_CREATE_ACL_ATTR and TX_MKDIR_ACL_ATTR handle special creates that
286eda14cbcSMatt Macy  * may contain attributes, ACL and optional fuid information.
287eda14cbcSMatt Macy  *
288eda14cbcSMatt Macy  * TX_CREATE_ACL and TX_MKDIR_ACL handle special creates that specify
289eda14cbcSMatt Macy  * and ACL and normal users/groups in the ACEs.
290eda14cbcSMatt Macy  *
291eda14cbcSMatt Macy  * There may be an optional xvattr attribute information similar
292eda14cbcSMatt Macy  * to zfs_log_setattr.
293eda14cbcSMatt Macy  *
294eda14cbcSMatt Macy  * Also, after the file name "domain" strings may be appended.
295eda14cbcSMatt Macy  */
296eda14cbcSMatt Macy void
297eda14cbcSMatt Macy zfs_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
298180f8225SMatt Macy     znode_t *dzp, znode_t *zp, const char *name, vsecattr_t *vsecp,
299eda14cbcSMatt Macy     zfs_fuid_info_t *fuidp, vattr_t *vap)
300eda14cbcSMatt Macy {
301eda14cbcSMatt Macy 	itx_t *itx;
302eda14cbcSMatt Macy 	lr_create_t *lr;
303eda14cbcSMatt Macy 	lr_acl_create_t *lracl;
304eda14cbcSMatt Macy 	size_t aclsize = 0;
305eda14cbcSMatt Macy 	size_t xvatsize = 0;
306eda14cbcSMatt Macy 	size_t txsize;
307eda14cbcSMatt Macy 	xvattr_t *xvap = (xvattr_t *)vap;
308eda14cbcSMatt Macy 	void *end;
309eda14cbcSMatt Macy 	size_t lrsize;
310eda14cbcSMatt Macy 	size_t namesize = strlen(name) + 1;
311eda14cbcSMatt Macy 	size_t fuidsz = 0;
312eda14cbcSMatt Macy 
313eda14cbcSMatt Macy 	if (zil_replaying(zilog, tx) || zfs_xattr_owner_unlinked(dzp))
314eda14cbcSMatt Macy 		return;
315eda14cbcSMatt Macy 
316eda14cbcSMatt Macy 	/*
317eda14cbcSMatt Macy 	 * If we have FUIDs present then add in space for
318eda14cbcSMatt Macy 	 * domains and ACE fuid's if any.
319eda14cbcSMatt Macy 	 */
320eda14cbcSMatt Macy 	if (fuidp) {
321eda14cbcSMatt Macy 		fuidsz += fuidp->z_domain_str_sz;
322eda14cbcSMatt Macy 		fuidsz += fuidp->z_fuid_cnt * sizeof (uint64_t);
323eda14cbcSMatt Macy 	}
324eda14cbcSMatt Macy 
325eda14cbcSMatt Macy 	if (vap->va_mask & ATTR_XVATTR)
326eda14cbcSMatt Macy 		xvatsize = ZIL_XVAT_SIZE(xvap->xva_mapsize);
327eda14cbcSMatt Macy 
328eda14cbcSMatt Macy 	if ((int)txtype == TX_CREATE_ATTR || (int)txtype == TX_MKDIR_ATTR ||
329eda14cbcSMatt Macy 	    (int)txtype == TX_CREATE || (int)txtype == TX_MKDIR ||
330eda14cbcSMatt Macy 	    (int)txtype == TX_MKXATTR) {
331eda14cbcSMatt Macy 		txsize = sizeof (*lr) + namesize + fuidsz + xvatsize;
332eda14cbcSMatt Macy 		lrsize = sizeof (*lr);
333eda14cbcSMatt Macy 	} else {
334eda14cbcSMatt Macy 		txsize =
335eda14cbcSMatt Macy 		    sizeof (lr_acl_create_t) + namesize + fuidsz +
336eda14cbcSMatt Macy 		    ZIL_ACE_LENGTH(aclsize) + xvatsize;
337eda14cbcSMatt Macy 		lrsize = sizeof (lr_acl_create_t);
338eda14cbcSMatt Macy 	}
339eda14cbcSMatt Macy 
340eda14cbcSMatt Macy 	itx = zil_itx_create(txtype, txsize);
341eda14cbcSMatt Macy 
342eda14cbcSMatt Macy 	lr = (lr_create_t *)&itx->itx_lr;
343eda14cbcSMatt Macy 	lr->lr_doid = dzp->z_id;
344eda14cbcSMatt Macy 	lr->lr_foid = zp->z_id;
345eda14cbcSMatt Macy 	/* Store dnode slot count in 8 bits above object id. */
346eda14cbcSMatt Macy 	LR_FOID_SET_SLOTS(lr->lr_foid, zp->z_dnodesize >> DNODE_SHIFT);
347eda14cbcSMatt Macy 	lr->lr_mode = zp->z_mode;
348eda14cbcSMatt Macy 	if (!IS_EPHEMERAL(KUID_TO_SUID(ZTOUID(zp)))) {
349eda14cbcSMatt Macy 		lr->lr_uid = (uint64_t)KUID_TO_SUID(ZTOUID(zp));
350eda14cbcSMatt Macy 	} else {
351eda14cbcSMatt Macy 		lr->lr_uid = fuidp->z_fuid_owner;
352eda14cbcSMatt Macy 	}
353eda14cbcSMatt Macy 	if (!IS_EPHEMERAL(KGID_TO_SGID(ZTOGID(zp)))) {
354eda14cbcSMatt Macy 		lr->lr_gid = (uint64_t)KGID_TO_SGID(ZTOGID(zp));
355eda14cbcSMatt Macy 	} else {
356eda14cbcSMatt Macy 		lr->lr_gid = fuidp->z_fuid_group;
357eda14cbcSMatt Macy 	}
358eda14cbcSMatt Macy 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
359eda14cbcSMatt Macy 	    sizeof (uint64_t));
360eda14cbcSMatt Macy 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
361eda14cbcSMatt Macy 	    lr->lr_crtime, sizeof (uint64_t) * 2);
362eda14cbcSMatt Macy 
363eda14cbcSMatt Macy 	if (sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(ZTOZSB(zp)), &lr->lr_rdev,
364eda14cbcSMatt Macy 	    sizeof (lr->lr_rdev)) != 0)
365eda14cbcSMatt Macy 		lr->lr_rdev = 0;
366eda14cbcSMatt Macy 
367eda14cbcSMatt Macy 	/*
368eda14cbcSMatt Macy 	 * Fill in xvattr info if any
369eda14cbcSMatt Macy 	 */
370eda14cbcSMatt Macy 	if (vap->va_mask & ATTR_XVATTR) {
371eda14cbcSMatt Macy 		zfs_log_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), xvap);
372eda14cbcSMatt Macy 		end = (caddr_t)lr + lrsize + xvatsize;
373eda14cbcSMatt Macy 	} else {
374eda14cbcSMatt Macy 		end = (caddr_t)lr + lrsize;
375eda14cbcSMatt Macy 	}
376eda14cbcSMatt Macy 
377eda14cbcSMatt Macy 	/* Now fill in any ACL info */
378eda14cbcSMatt Macy 
379eda14cbcSMatt Macy 	if (vsecp) {
380eda14cbcSMatt Macy 		lracl = (lr_acl_create_t *)&itx->itx_lr;
381eda14cbcSMatt Macy 		lracl->lr_aclcnt = vsecp->vsa_aclcnt;
382eda14cbcSMatt Macy 		lracl->lr_acl_bytes = aclsize;
383eda14cbcSMatt Macy 		lracl->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
384eda14cbcSMatt Macy 		lracl->lr_fuidcnt  = fuidp ? fuidp->z_fuid_cnt : 0;
385eda14cbcSMatt Macy 		if (vsecp->vsa_aclflags & VSA_ACE_ACLFLAGS)
386eda14cbcSMatt Macy 			lracl->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
387eda14cbcSMatt Macy 		else
388eda14cbcSMatt Macy 			lracl->lr_acl_flags = 0;
389eda14cbcSMatt Macy 
390da5137abSMartin Matuska 		memcpy(end, vsecp->vsa_aclentp, aclsize);
391eda14cbcSMatt Macy 		end = (caddr_t)end + ZIL_ACE_LENGTH(aclsize);
392eda14cbcSMatt Macy 	}
393eda14cbcSMatt Macy 
394eda14cbcSMatt Macy 	/* drop in FUID info */
395eda14cbcSMatt Macy 	if (fuidp) {
396eda14cbcSMatt Macy 		end = zfs_log_fuid_ids(fuidp, end);
397eda14cbcSMatt Macy 		end = zfs_log_fuid_domains(fuidp, end);
398eda14cbcSMatt Macy 	}
399eda14cbcSMatt Macy 	/*
400eda14cbcSMatt Macy 	 * Now place file name in log record
401eda14cbcSMatt Macy 	 */
402da5137abSMartin Matuska 	memcpy(end, name, namesize);
403eda14cbcSMatt Macy 
404eda14cbcSMatt Macy 	zil_itx_assign(zilog, itx, tx);
405eda14cbcSMatt Macy }
406eda14cbcSMatt Macy 
407eda14cbcSMatt Macy /*
408eda14cbcSMatt Macy  * Handles both TX_REMOVE and TX_RMDIR transactions.
409eda14cbcSMatt Macy  */
410eda14cbcSMatt Macy void
411eda14cbcSMatt Macy zfs_log_remove(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
412180f8225SMatt Macy     znode_t *dzp, const char *name, uint64_t foid, boolean_t unlinked)
413eda14cbcSMatt Macy {
414eda14cbcSMatt Macy 	itx_t *itx;
415eda14cbcSMatt Macy 	lr_remove_t *lr;
416eda14cbcSMatt Macy 	size_t namesize = strlen(name) + 1;
417eda14cbcSMatt Macy 
418eda14cbcSMatt Macy 	if (zil_replaying(zilog, tx) || zfs_xattr_owner_unlinked(dzp))
419eda14cbcSMatt Macy 		return;
420eda14cbcSMatt Macy 
421eda14cbcSMatt Macy 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
422eda14cbcSMatt Macy 	lr = (lr_remove_t *)&itx->itx_lr;
423eda14cbcSMatt Macy 	lr->lr_doid = dzp->z_id;
424da5137abSMartin Matuska 	memcpy(lr + 1, name, namesize);
425eda14cbcSMatt Macy 
426eda14cbcSMatt Macy 	itx->itx_oid = foid;
427eda14cbcSMatt Macy 
428eda14cbcSMatt Macy 	/*
429eda14cbcSMatt Macy 	 * Object ids can be re-instantiated in the next txg so
430eda14cbcSMatt Macy 	 * remove any async transactions to avoid future leaks.
431eda14cbcSMatt Macy 	 * This can happen if a fsync occurs on the re-instantiated
432eda14cbcSMatt Macy 	 * object for a WR_INDIRECT or WR_NEED_COPY write, which gets
433eda14cbcSMatt Macy 	 * the new file data and flushes a write record for the old object.
434eda14cbcSMatt Macy 	 */
435eda14cbcSMatt Macy 	if (unlinked) {
436eda14cbcSMatt Macy 		ASSERT((txtype & ~TX_CI) == TX_REMOVE);
437eda14cbcSMatt Macy 		zil_remove_async(zilog, foid);
438eda14cbcSMatt Macy 	}
439eda14cbcSMatt Macy 	zil_itx_assign(zilog, itx, tx);
440eda14cbcSMatt Macy }
441eda14cbcSMatt Macy 
442eda14cbcSMatt Macy /*
443eda14cbcSMatt Macy  * Handles TX_LINK transactions.
444eda14cbcSMatt Macy  */
445eda14cbcSMatt Macy void
446eda14cbcSMatt Macy zfs_log_link(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
447180f8225SMatt Macy     znode_t *dzp, znode_t *zp, const char *name)
448eda14cbcSMatt Macy {
449eda14cbcSMatt Macy 	itx_t *itx;
450eda14cbcSMatt Macy 	lr_link_t *lr;
451eda14cbcSMatt Macy 	size_t namesize = strlen(name) + 1;
452eda14cbcSMatt Macy 
453eda14cbcSMatt Macy 	if (zil_replaying(zilog, tx))
454eda14cbcSMatt Macy 		return;
455eda14cbcSMatt Macy 
456eda14cbcSMatt Macy 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize);
457eda14cbcSMatt Macy 	lr = (lr_link_t *)&itx->itx_lr;
458eda14cbcSMatt Macy 	lr->lr_doid = dzp->z_id;
459eda14cbcSMatt Macy 	lr->lr_link_obj = zp->z_id;
460da5137abSMartin Matuska 	memcpy(lr + 1, name, namesize);
461eda14cbcSMatt Macy 
462eda14cbcSMatt Macy 	zil_itx_assign(zilog, itx, tx);
463eda14cbcSMatt Macy }
464eda14cbcSMatt Macy 
465eda14cbcSMatt Macy /*
466eda14cbcSMatt Macy  * Handles TX_SYMLINK transactions.
467eda14cbcSMatt Macy  */
468eda14cbcSMatt Macy void
469eda14cbcSMatt Macy zfs_log_symlink(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype,
470180f8225SMatt Macy     znode_t *dzp, znode_t *zp, const char *name, const char *link)
471eda14cbcSMatt Macy {
472eda14cbcSMatt Macy 	itx_t *itx;
473eda14cbcSMatt Macy 	lr_create_t *lr;
474eda14cbcSMatt Macy 	size_t namesize = strlen(name) + 1;
475eda14cbcSMatt Macy 	size_t linksize = strlen(link) + 1;
476eda14cbcSMatt Macy 
477eda14cbcSMatt Macy 	if (zil_replaying(zilog, tx))
478eda14cbcSMatt Macy 		return;
479eda14cbcSMatt Macy 
480eda14cbcSMatt Macy 	itx = zil_itx_create(txtype, sizeof (*lr) + namesize + linksize);
481eda14cbcSMatt Macy 	lr = (lr_create_t *)&itx->itx_lr;
482eda14cbcSMatt Macy 	lr->lr_doid = dzp->z_id;
483eda14cbcSMatt Macy 	lr->lr_foid = zp->z_id;
484eda14cbcSMatt Macy 	lr->lr_uid = KUID_TO_SUID(ZTOUID(zp));
485eda14cbcSMatt Macy 	lr->lr_gid = KGID_TO_SGID(ZTOGID(zp));
486eda14cbcSMatt Macy 	lr->lr_mode = zp->z_mode;
487eda14cbcSMatt Macy 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &lr->lr_gen,
488eda14cbcSMatt Macy 	    sizeof (uint64_t));
489eda14cbcSMatt Macy 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
490eda14cbcSMatt Macy 	    lr->lr_crtime, sizeof (uint64_t) * 2);
491da5137abSMartin Matuska 	memcpy((char *)(lr + 1), name, namesize);
492da5137abSMartin Matuska 	memcpy((char *)(lr + 1) + namesize, link, linksize);
493eda14cbcSMatt Macy 
494eda14cbcSMatt Macy 	zil_itx_assign(zilog, itx, tx);
495eda14cbcSMatt Macy }
496eda14cbcSMatt Macy 
497eda14cbcSMatt Macy /*
498eda14cbcSMatt Macy  * Handles TX_RENAME transactions.
499eda14cbcSMatt Macy  */
500eda14cbcSMatt Macy void
501180f8225SMatt Macy zfs_log_rename(zilog_t *zilog, dmu_tx_t *tx, uint64_t txtype, znode_t *sdzp,
502180f8225SMatt Macy     const char *sname, znode_t *tdzp, const char *dname, znode_t *szp)
503eda14cbcSMatt Macy {
504eda14cbcSMatt Macy 	itx_t *itx;
505eda14cbcSMatt Macy 	lr_rename_t *lr;
506eda14cbcSMatt Macy 	size_t snamesize = strlen(sname) + 1;
507eda14cbcSMatt Macy 	size_t dnamesize = strlen(dname) + 1;
508eda14cbcSMatt Macy 
509eda14cbcSMatt Macy 	if (zil_replaying(zilog, tx))
510eda14cbcSMatt Macy 		return;
511eda14cbcSMatt Macy 
512eda14cbcSMatt Macy 	itx = zil_itx_create(txtype, sizeof (*lr) + snamesize + dnamesize);
513eda14cbcSMatt Macy 	lr = (lr_rename_t *)&itx->itx_lr;
514eda14cbcSMatt Macy 	lr->lr_sdoid = sdzp->z_id;
515eda14cbcSMatt Macy 	lr->lr_tdoid = tdzp->z_id;
516da5137abSMartin Matuska 	memcpy((char *)(lr + 1), sname, snamesize);
517da5137abSMartin Matuska 	memcpy((char *)(lr + 1) + snamesize, dname, dnamesize);
518eda14cbcSMatt Macy 	itx->itx_oid = szp->z_id;
519eda14cbcSMatt Macy 
520eda14cbcSMatt Macy 	zil_itx_assign(zilog, itx, tx);
521eda14cbcSMatt Macy }
522eda14cbcSMatt Macy 
523eda14cbcSMatt Macy /*
524eda14cbcSMatt Macy  * zfs_log_write() handles TX_WRITE transactions. The specified callback is
525eda14cbcSMatt Macy  * called as soon as the write is on stable storage (be it via a DMU sync or a
526eda14cbcSMatt Macy  * ZIL commit).
527eda14cbcSMatt Macy  */
528e92ffd9bSMartin Matuska static long zfs_immediate_write_sz = 32768;
529eda14cbcSMatt Macy 
530eda14cbcSMatt Macy void
531eda14cbcSMatt Macy zfs_log_write(zilog_t *zilog, dmu_tx_t *tx, int txtype,
532eda14cbcSMatt Macy     znode_t *zp, offset_t off, ssize_t resid, int ioflag,
533eda14cbcSMatt Macy     zil_callback_t callback, void *callback_data)
534eda14cbcSMatt Macy {
535eda14cbcSMatt Macy 	dmu_buf_impl_t *db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl);
536eda14cbcSMatt Macy 	uint32_t blocksize = zp->z_blksz;
537eda14cbcSMatt Macy 	itx_wr_state_t write_state;
538eda14cbcSMatt Macy 	uintptr_t fsync_cnt;
539f9693befSMartin Matuska 	uint64_t gen = 0;
5403f9d360cSMartin Matuska 	ssize_t size = resid;
541eda14cbcSMatt Macy 
542eda14cbcSMatt Macy 	if (zil_replaying(zilog, tx) || zp->z_unlinked ||
543eda14cbcSMatt Macy 	    zfs_xattr_owner_unlinked(zp)) {
544eda14cbcSMatt Macy 		if (callback != NULL)
545eda14cbcSMatt Macy 			callback(callback_data);
546eda14cbcSMatt Macy 		return;
547eda14cbcSMatt Macy 	}
548eda14cbcSMatt Macy 
549eda14cbcSMatt Macy 	if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
550eda14cbcSMatt Macy 		write_state = WR_INDIRECT;
551eda14cbcSMatt Macy 	else if (!spa_has_slogs(zilog->zl_spa) &&
552eda14cbcSMatt Macy 	    resid >= zfs_immediate_write_sz)
553eda14cbcSMatt Macy 		write_state = WR_INDIRECT;
554eda14cbcSMatt Macy 	else if (ioflag & (O_SYNC | O_DSYNC))
555eda14cbcSMatt Macy 		write_state = WR_COPIED;
556eda14cbcSMatt Macy 	else
557eda14cbcSMatt Macy 		write_state = WR_NEED_COPY;
558eda14cbcSMatt Macy 
559eda14cbcSMatt Macy 	if ((fsync_cnt = (uintptr_t)tsd_get(zfs_fsyncer_key)) != 0) {
560eda14cbcSMatt Macy 		(void) tsd_set(zfs_fsyncer_key, (void *)(fsync_cnt - 1));
561eda14cbcSMatt Macy 	}
562eda14cbcSMatt Macy 
563f9693befSMartin Matuska 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(ZTOZSB(zp)), &gen,
564f9693befSMartin Matuska 	    sizeof (gen));
565f9693befSMartin Matuska 
566eda14cbcSMatt Macy 	while (resid) {
567eda14cbcSMatt Macy 		itx_t *itx;
568eda14cbcSMatt Macy 		lr_write_t *lr;
569eda14cbcSMatt Macy 		itx_wr_state_t wr_state = write_state;
570eda14cbcSMatt Macy 		ssize_t len = resid;
571eda14cbcSMatt Macy 
572eda14cbcSMatt Macy 		/*
573eda14cbcSMatt Macy 		 * A WR_COPIED record must fit entirely in one log block.
574eda14cbcSMatt Macy 		 * Large writes can use WR_NEED_COPY, which the ZIL will
575eda14cbcSMatt Macy 		 * split into multiple records across several log blocks
576eda14cbcSMatt Macy 		 * if necessary.
577eda14cbcSMatt Macy 		 */
578eda14cbcSMatt Macy 		if (wr_state == WR_COPIED &&
579eda14cbcSMatt Macy 		    resid > zil_max_copied_data(zilog))
580eda14cbcSMatt Macy 			wr_state = WR_NEED_COPY;
581eda14cbcSMatt Macy 		else if (wr_state == WR_INDIRECT)
582eda14cbcSMatt Macy 			len = MIN(blocksize - P2PHASE(off, blocksize), resid);
583eda14cbcSMatt Macy 
584eda14cbcSMatt Macy 		itx = zil_itx_create(txtype, sizeof (*lr) +
585eda14cbcSMatt Macy 		    (wr_state == WR_COPIED ? len : 0));
586eda14cbcSMatt Macy 		lr = (lr_write_t *)&itx->itx_lr;
587eda14cbcSMatt Macy 
588c40487d4SMatt Macy 		/*
589c40487d4SMatt Macy 		 * For WR_COPIED records, copy the data into the lr_write_t.
590c40487d4SMatt Macy 		 */
591c40487d4SMatt Macy 		if (wr_state == WR_COPIED) {
592c40487d4SMatt Macy 			int err;
593eda14cbcSMatt Macy 			DB_DNODE_ENTER(db);
594c40487d4SMatt Macy 			err = dmu_read_by_dnode(DB_DNODE(db), off, len, lr + 1,
595c40487d4SMatt Macy 			    DMU_READ_NO_PREFETCH);
596c40487d4SMatt Macy 			if (err != 0) {
597eda14cbcSMatt Macy 				zil_itx_destroy(itx);
598eda14cbcSMatt Macy 				itx = zil_itx_create(txtype, sizeof (*lr));
599eda14cbcSMatt Macy 				lr = (lr_write_t *)&itx->itx_lr;
600eda14cbcSMatt Macy 				wr_state = WR_NEED_COPY;
601eda14cbcSMatt Macy 			}
602eda14cbcSMatt Macy 			DB_DNODE_EXIT(db);
603c40487d4SMatt Macy 		}
604eda14cbcSMatt Macy 
605eda14cbcSMatt Macy 		itx->itx_wr_state = wr_state;
606eda14cbcSMatt Macy 		lr->lr_foid = zp->z_id;
607eda14cbcSMatt Macy 		lr->lr_offset = off;
608eda14cbcSMatt Macy 		lr->lr_length = len;
609eda14cbcSMatt Macy 		lr->lr_blkoff = 0;
610eda14cbcSMatt Macy 		BP_ZERO(&lr->lr_blkptr);
611eda14cbcSMatt Macy 
612eda14cbcSMatt Macy 		itx->itx_private = ZTOZSB(zp);
613f9693befSMartin Matuska 		itx->itx_gen = gen;
614eda14cbcSMatt Macy 
615eda14cbcSMatt Macy 		if (!(ioflag & (O_SYNC | O_DSYNC)) && (zp->z_sync_cnt == 0) &&
616eda14cbcSMatt Macy 		    (fsync_cnt == 0))
617eda14cbcSMatt Macy 			itx->itx_sync = B_FALSE;
618eda14cbcSMatt Macy 
619eda14cbcSMatt Macy 		itx->itx_callback = callback;
620eda14cbcSMatt Macy 		itx->itx_callback_data = callback_data;
621eda14cbcSMatt Macy 		zil_itx_assign(zilog, itx, tx);
622eda14cbcSMatt Macy 
623eda14cbcSMatt Macy 		off += len;
624eda14cbcSMatt Macy 		resid -= len;
625eda14cbcSMatt Macy 	}
6263f9d360cSMartin Matuska 
6273f9d360cSMartin Matuska 	if (write_state == WR_COPIED || write_state == WR_NEED_COPY) {
6283f9d360cSMartin Matuska 		dsl_pool_wrlog_count(zilog->zl_dmu_pool, size, tx->tx_txg);
6293f9d360cSMartin Matuska 	}
630eda14cbcSMatt Macy }
631eda14cbcSMatt Macy 
632eda14cbcSMatt Macy /*
633eda14cbcSMatt Macy  * Handles TX_TRUNCATE transactions.
634eda14cbcSMatt Macy  */
635eda14cbcSMatt Macy void
636eda14cbcSMatt Macy zfs_log_truncate(zilog_t *zilog, dmu_tx_t *tx, int txtype,
637eda14cbcSMatt Macy     znode_t *zp, uint64_t off, uint64_t len)
638eda14cbcSMatt Macy {
639eda14cbcSMatt Macy 	itx_t *itx;
640eda14cbcSMatt Macy 	lr_truncate_t *lr;
641eda14cbcSMatt Macy 
642eda14cbcSMatt Macy 	if (zil_replaying(zilog, tx) || zp->z_unlinked ||
643eda14cbcSMatt Macy 	    zfs_xattr_owner_unlinked(zp))
644eda14cbcSMatt Macy 		return;
645eda14cbcSMatt Macy 
646eda14cbcSMatt Macy 	itx = zil_itx_create(txtype, sizeof (*lr));
647eda14cbcSMatt Macy 	lr = (lr_truncate_t *)&itx->itx_lr;
648eda14cbcSMatt Macy 	lr->lr_foid = zp->z_id;
649eda14cbcSMatt Macy 	lr->lr_offset = off;
650eda14cbcSMatt Macy 	lr->lr_length = len;
651eda14cbcSMatt Macy 
652eda14cbcSMatt Macy 	itx->itx_sync = (zp->z_sync_cnt != 0);
653eda14cbcSMatt Macy 	zil_itx_assign(zilog, itx, tx);
654eda14cbcSMatt Macy }
655eda14cbcSMatt Macy 
656eda14cbcSMatt Macy /*
657eda14cbcSMatt Macy  * Handles TX_SETATTR transactions.
658eda14cbcSMatt Macy  */
659eda14cbcSMatt Macy void
660eda14cbcSMatt Macy zfs_log_setattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
661eda14cbcSMatt Macy     znode_t *zp, vattr_t *vap, uint_t mask_applied, zfs_fuid_info_t *fuidp)
662eda14cbcSMatt Macy {
663eda14cbcSMatt Macy 	itx_t		*itx;
664eda14cbcSMatt Macy 	lr_setattr_t	*lr;
665eda14cbcSMatt Macy 	xvattr_t	*xvap = (xvattr_t *)vap;
666eda14cbcSMatt Macy 	size_t		recsize = sizeof (lr_setattr_t);
667eda14cbcSMatt Macy 	void		*start;
668eda14cbcSMatt Macy 
669eda14cbcSMatt Macy 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
670eda14cbcSMatt Macy 		return;
671eda14cbcSMatt Macy 
672eda14cbcSMatt Macy 	/*
673eda14cbcSMatt Macy 	 * If XVATTR set, then log record size needs to allow
674eda14cbcSMatt Macy 	 * for lr_attr_t + xvattr mask, mapsize and create time
675eda14cbcSMatt Macy 	 * plus actual attribute values
676eda14cbcSMatt Macy 	 */
677eda14cbcSMatt Macy 	if (vap->va_mask & ATTR_XVATTR)
678eda14cbcSMatt Macy 		recsize = sizeof (*lr) + ZIL_XVAT_SIZE(xvap->xva_mapsize);
679eda14cbcSMatt Macy 
680eda14cbcSMatt Macy 	if (fuidp)
681eda14cbcSMatt Macy 		recsize += fuidp->z_domain_str_sz;
682eda14cbcSMatt Macy 
683eda14cbcSMatt Macy 	itx = zil_itx_create(txtype, recsize);
684eda14cbcSMatt Macy 	lr = (lr_setattr_t *)&itx->itx_lr;
685eda14cbcSMatt Macy 	lr->lr_foid = zp->z_id;
686eda14cbcSMatt Macy 	lr->lr_mask = (uint64_t)mask_applied;
687eda14cbcSMatt Macy 	lr->lr_mode = (uint64_t)vap->va_mode;
688eda14cbcSMatt Macy 	if ((mask_applied & ATTR_UID) && IS_EPHEMERAL(vap->va_uid))
689eda14cbcSMatt Macy 		lr->lr_uid = fuidp->z_fuid_owner;
690eda14cbcSMatt Macy 	else
691eda14cbcSMatt Macy 		lr->lr_uid = (uint64_t)vap->va_uid;
692eda14cbcSMatt Macy 
693eda14cbcSMatt Macy 	if ((mask_applied & ATTR_GID) && IS_EPHEMERAL(vap->va_gid))
694eda14cbcSMatt Macy 		lr->lr_gid = fuidp->z_fuid_group;
695eda14cbcSMatt Macy 	else
696eda14cbcSMatt Macy 		lr->lr_gid = (uint64_t)vap->va_gid;
697eda14cbcSMatt Macy 
698eda14cbcSMatt Macy 	lr->lr_size = (uint64_t)vap->va_size;
699eda14cbcSMatt Macy 	ZFS_TIME_ENCODE(&vap->va_atime, lr->lr_atime);
700eda14cbcSMatt Macy 	ZFS_TIME_ENCODE(&vap->va_mtime, lr->lr_mtime);
701eda14cbcSMatt Macy 	start = (lr_setattr_t *)(lr + 1);
702eda14cbcSMatt Macy 	if (vap->va_mask & ATTR_XVATTR) {
703eda14cbcSMatt Macy 		zfs_log_xvattr((lr_attr_t *)start, xvap);
704eda14cbcSMatt Macy 		start = (caddr_t)start + ZIL_XVAT_SIZE(xvap->xva_mapsize);
705eda14cbcSMatt Macy 	}
706eda14cbcSMatt Macy 
707eda14cbcSMatt Macy 	/*
708eda14cbcSMatt Macy 	 * Now stick on domain information if any on end
709eda14cbcSMatt Macy 	 */
710eda14cbcSMatt Macy 
711eda14cbcSMatt Macy 	if (fuidp)
712eda14cbcSMatt Macy 		(void) zfs_log_fuid_domains(fuidp, start);
713eda14cbcSMatt Macy 
714eda14cbcSMatt Macy 	itx->itx_sync = (zp->z_sync_cnt != 0);
715eda14cbcSMatt Macy 	zil_itx_assign(zilog, itx, tx);
716eda14cbcSMatt Macy }
717eda14cbcSMatt Macy 
718eda14cbcSMatt Macy /*
719c03c5b1cSMartin Matuska  * Handles TX_SETSAXATTR transactions.
720c03c5b1cSMartin Matuska  */
721c03c5b1cSMartin Matuska void
722c03c5b1cSMartin Matuska zfs_log_setsaxattr(zilog_t *zilog, dmu_tx_t *tx, int txtype,
723c03c5b1cSMartin Matuska     znode_t *zp, const char *name, const void *value, size_t size)
724c03c5b1cSMartin Matuska {
725c03c5b1cSMartin Matuska 	itx_t		*itx;
726c03c5b1cSMartin Matuska 	lr_setsaxattr_t	*lr;
727c03c5b1cSMartin Matuska 	size_t		recsize = sizeof (lr_setsaxattr_t);
728c03c5b1cSMartin Matuska 	void		*xattrstart;
729c03c5b1cSMartin Matuska 	int		namelen;
730c03c5b1cSMartin Matuska 
731c03c5b1cSMartin Matuska 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
732c03c5b1cSMartin Matuska 		return;
733c03c5b1cSMartin Matuska 
734c03c5b1cSMartin Matuska 	namelen = strlen(name) + 1;
735c03c5b1cSMartin Matuska 	recsize += (namelen + size);
736c03c5b1cSMartin Matuska 	itx = zil_itx_create(txtype, recsize);
737c03c5b1cSMartin Matuska 	lr = (lr_setsaxattr_t *)&itx->itx_lr;
738c03c5b1cSMartin Matuska 	lr->lr_foid = zp->z_id;
739c03c5b1cSMartin Matuska 	xattrstart = (char *)(lr + 1);
740da5137abSMartin Matuska 	memcpy(xattrstart, name, namelen);
741c03c5b1cSMartin Matuska 	if (value != NULL) {
742da5137abSMartin Matuska 		memcpy((char *)xattrstart + namelen, value, size);
743c03c5b1cSMartin Matuska 		lr->lr_size = size;
744c03c5b1cSMartin Matuska 	} else {
745c03c5b1cSMartin Matuska 		lr->lr_size = 0;
746c03c5b1cSMartin Matuska 	}
747c03c5b1cSMartin Matuska 
748c03c5b1cSMartin Matuska 	itx->itx_sync = (zp->z_sync_cnt != 0);
749c03c5b1cSMartin Matuska 	zil_itx_assign(zilog, itx, tx);
750c03c5b1cSMartin Matuska }
751c03c5b1cSMartin Matuska 
752c03c5b1cSMartin Matuska /*
753eda14cbcSMatt Macy  * Handles TX_ACL transactions.
754eda14cbcSMatt Macy  */
755eda14cbcSMatt Macy void
756eda14cbcSMatt Macy zfs_log_acl(zilog_t *zilog, dmu_tx_t *tx, znode_t *zp,
757eda14cbcSMatt Macy     vsecattr_t *vsecp, zfs_fuid_info_t *fuidp)
758eda14cbcSMatt Macy {
759eda14cbcSMatt Macy 	itx_t *itx;
760eda14cbcSMatt Macy 	lr_acl_v0_t *lrv0;
761eda14cbcSMatt Macy 	lr_acl_t *lr;
762eda14cbcSMatt Macy 	int txtype;
763eda14cbcSMatt Macy 	int lrsize;
764eda14cbcSMatt Macy 	size_t txsize;
765eda14cbcSMatt Macy 	size_t aclbytes = vsecp->vsa_aclentsz;
766eda14cbcSMatt Macy 
767eda14cbcSMatt Macy 	if (zil_replaying(zilog, tx) || zp->z_unlinked)
768eda14cbcSMatt Macy 		return;
769eda14cbcSMatt Macy 
770eda14cbcSMatt Macy 	txtype = (ZTOZSB(zp)->z_version < ZPL_VERSION_FUID) ?
771eda14cbcSMatt Macy 	    TX_ACL_V0 : TX_ACL;
772eda14cbcSMatt Macy 
773eda14cbcSMatt Macy 	if (txtype == TX_ACL)
774eda14cbcSMatt Macy 		lrsize = sizeof (*lr);
775eda14cbcSMatt Macy 	else
776eda14cbcSMatt Macy 		lrsize = sizeof (*lrv0);
777eda14cbcSMatt Macy 
778eda14cbcSMatt Macy 	txsize = lrsize +
779eda14cbcSMatt Macy 	    ((txtype == TX_ACL) ? ZIL_ACE_LENGTH(aclbytes) : aclbytes) +
780eda14cbcSMatt Macy 	    (fuidp ? fuidp->z_domain_str_sz : 0) +
781eda14cbcSMatt Macy 	    sizeof (uint64_t) * (fuidp ? fuidp->z_fuid_cnt : 0);
782eda14cbcSMatt Macy 
783eda14cbcSMatt Macy 	itx = zil_itx_create(txtype, txsize);
784eda14cbcSMatt Macy 
785eda14cbcSMatt Macy 	lr = (lr_acl_t *)&itx->itx_lr;
786eda14cbcSMatt Macy 	lr->lr_foid = zp->z_id;
787eda14cbcSMatt Macy 	if (txtype == TX_ACL) {
788eda14cbcSMatt Macy 		lr->lr_acl_bytes = aclbytes;
789eda14cbcSMatt Macy 		lr->lr_domcnt = fuidp ? fuidp->z_domain_cnt : 0;
790eda14cbcSMatt Macy 		lr->lr_fuidcnt = fuidp ? fuidp->z_fuid_cnt : 0;
791eda14cbcSMatt Macy 		if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS)
792eda14cbcSMatt Macy 			lr->lr_acl_flags = (uint64_t)vsecp->vsa_aclflags;
793eda14cbcSMatt Macy 		else
794eda14cbcSMatt Macy 			lr->lr_acl_flags = 0;
795eda14cbcSMatt Macy 	}
796eda14cbcSMatt Macy 	lr->lr_aclcnt = (uint64_t)vsecp->vsa_aclcnt;
797eda14cbcSMatt Macy 
798eda14cbcSMatt Macy 	if (txtype == TX_ACL_V0) {
799eda14cbcSMatt Macy 		lrv0 = (lr_acl_v0_t *)lr;
800da5137abSMartin Matuska 		memcpy(lrv0 + 1, vsecp->vsa_aclentp, aclbytes);
801eda14cbcSMatt Macy 	} else {
802eda14cbcSMatt Macy 		void *start = (ace_t *)(lr + 1);
803eda14cbcSMatt Macy 
804da5137abSMartin Matuska 		memcpy(start, vsecp->vsa_aclentp, aclbytes);
805eda14cbcSMatt Macy 
806eda14cbcSMatt Macy 		start = (caddr_t)start + ZIL_ACE_LENGTH(aclbytes);
807eda14cbcSMatt Macy 
808eda14cbcSMatt Macy 		if (fuidp) {
809eda14cbcSMatt Macy 			start = zfs_log_fuid_ids(fuidp, start);
810eda14cbcSMatt Macy 			(void) zfs_log_fuid_domains(fuidp, start);
811eda14cbcSMatt Macy 		}
812eda14cbcSMatt Macy 	}
813eda14cbcSMatt Macy 
814eda14cbcSMatt Macy 	itx->itx_sync = (zp->z_sync_cnt != 0);
815eda14cbcSMatt Macy 	zil_itx_assign(zilog, itx, tx);
816eda14cbcSMatt Macy }
817eda14cbcSMatt Macy 
818eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, immediate_write_sz, LONG, ZMOD_RW,
819eda14cbcSMatt Macy 	"Largest data block to write to zil");
820