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