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