xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_replay.c (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
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
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
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) 2012 Cyril Plisko. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
252a58b312SMartin Matuska  * Copyright (c) 2021, 2022 by Pawel Jakub Dawidek
26eda14cbcSMatt Macy  */
27eda14cbcSMatt Macy 
28eda14cbcSMatt Macy #include <sys/types.h>
29eda14cbcSMatt Macy #include <sys/param.h>
30eda14cbcSMatt Macy #include <sys/sysmacros.h>
31eda14cbcSMatt Macy #include <sys/cmn_err.h>
32eda14cbcSMatt Macy #include <sys/kmem.h>
33eda14cbcSMatt Macy #include <sys/thread.h>
34eda14cbcSMatt Macy #include <sys/file.h>
35eda14cbcSMatt Macy #include <sys/fcntl.h>
36eda14cbcSMatt Macy #include <sys/vfs.h>
37eda14cbcSMatt Macy #include <sys/fs/zfs.h>
38eda14cbcSMatt Macy #include <sys/zfs_znode.h>
39eda14cbcSMatt Macy #include <sys/zfs_dir.h>
40eda14cbcSMatt Macy #include <sys/zfs_acl.h>
41eda14cbcSMatt Macy #include <sys/zfs_fuid.h>
42eda14cbcSMatt Macy #include <sys/zfs_vnops.h>
43eda14cbcSMatt Macy #include <sys/spa.h>
44eda14cbcSMatt Macy #include <sys/zil.h>
45eda14cbcSMatt Macy #include <sys/byteorder.h>
46eda14cbcSMatt Macy #include <sys/stat.h>
47eda14cbcSMatt Macy #include <sys/acl.h>
48eda14cbcSMatt Macy #include <sys/atomic.h>
49eda14cbcSMatt Macy #include <sys/cred.h>
50eda14cbcSMatt Macy #include <sys/zpl.h>
51c03c5b1cSMartin Matuska #include <sys/dmu_objset.h>
52c03c5b1cSMartin Matuska #include <sys/zfeature.h>
53eda14cbcSMatt Macy 
54eda14cbcSMatt Macy /*
55eda14cbcSMatt Macy  * NB: FreeBSD expects to be able to do vnode locking in lookup and
56eda14cbcSMatt Macy  * hold the locks across all subsequent VOPs until vput is called.
57eda14cbcSMatt Macy  * This means that its zfs vnops routines can't do any internal locking.
58eda14cbcSMatt Macy  * In order to have the same contract as the Linux vnops there would
59eda14cbcSMatt Macy  * needed to be duplicate locked vnops. If the vnops were used more widely
60eda14cbcSMatt Macy  * in common code this would likely be preferable. However, currently
61eda14cbcSMatt Macy  * this is the only file where this is the case.
62eda14cbcSMatt Macy  */
63eda14cbcSMatt Macy 
64eda14cbcSMatt Macy /*
65eda14cbcSMatt Macy  * Functions to replay ZFS intent log (ZIL) records
66eda14cbcSMatt Macy  * The functions are called through a function vector (zfs_replay_vector)
67eda14cbcSMatt Macy  * which is indexed by the transaction type.
68eda14cbcSMatt Macy  */
69eda14cbcSMatt Macy 
70eda14cbcSMatt Macy static void
zfs_init_vattr(vattr_t * vap,uint64_t mask,uint64_t mode,uint64_t uid,uint64_t gid,uint64_t rdev,uint64_t nodeid)71eda14cbcSMatt Macy zfs_init_vattr(vattr_t *vap, uint64_t mask, uint64_t mode,
72eda14cbcSMatt Macy     uint64_t uid, uint64_t gid, uint64_t rdev, uint64_t nodeid)
73eda14cbcSMatt Macy {
74da5137abSMartin Matuska 	memset(vap, 0, sizeof (*vap));
75eda14cbcSMatt Macy 	vap->va_mask = (uint_t)mask;
76eda14cbcSMatt Macy 	vap->va_mode = mode;
771f88aa09SMartin Matuska #if defined(__FreeBSD__) || defined(__APPLE__)
78eda14cbcSMatt Macy 	vap->va_type = IFTOVT(mode);
79eda14cbcSMatt Macy #endif
80eda14cbcSMatt Macy 	vap->va_uid = (uid_t)(IS_EPHEMERAL(uid)) ? -1 : uid;
81eda14cbcSMatt Macy 	vap->va_gid = (gid_t)(IS_EPHEMERAL(gid)) ? -1 : gid;
82eda14cbcSMatt Macy 	vap->va_rdev = zfs_cmpldev(rdev);
83eda14cbcSMatt Macy 	vap->va_nodeid = nodeid;
84eda14cbcSMatt Macy }
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy static int
zfs_replay_error(void * arg1,void * arg2,boolean_t byteswap)87eda14cbcSMatt Macy zfs_replay_error(void *arg1, void *arg2, boolean_t byteswap)
88eda14cbcSMatt Macy {
89c03c5b1cSMartin Matuska 	(void) arg1, (void) arg2, (void) byteswap;
90eda14cbcSMatt Macy 	return (SET_ERROR(ENOTSUP));
91eda14cbcSMatt Macy }
92eda14cbcSMatt Macy 
93eda14cbcSMatt Macy static void
zfs_replay_xvattr(lr_attr_t * lrattr,xvattr_t * xvap)94eda14cbcSMatt Macy zfs_replay_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
95eda14cbcSMatt Macy {
96eda14cbcSMatt Macy 	xoptattr_t *xoap = NULL;
97eda14cbcSMatt Macy 	uint64_t *attrs;
98eda14cbcSMatt Macy 	uint64_t *crtime;
99eda14cbcSMatt Macy 	uint32_t *bitmap;
100eda14cbcSMatt Macy 	void *scanstamp;
101eda14cbcSMatt Macy 	int i;
102eda14cbcSMatt Macy 
103eda14cbcSMatt Macy 	xvap->xva_vattr.va_mask |= ATTR_XVATTR;
104eda14cbcSMatt Macy 	if ((xoap = xva_getxoptattr(xvap)) == NULL) {
105eda14cbcSMatt Macy 		xvap->xva_vattr.va_mask &= ~ATTR_XVATTR; /* shouldn't happen */
106eda14cbcSMatt Macy 		return;
107eda14cbcSMatt Macy 	}
108eda14cbcSMatt Macy 
109eda14cbcSMatt Macy 	ASSERT(lrattr->lr_attr_masksize == xvap->xva_mapsize);
110eda14cbcSMatt Macy 
111eda14cbcSMatt Macy 	bitmap = &lrattr->lr_attr_bitmap;
112eda14cbcSMatt Macy 	for (i = 0; i != lrattr->lr_attr_masksize; i++, bitmap++)
113eda14cbcSMatt Macy 		xvap->xva_reqattrmap[i] = *bitmap;
114eda14cbcSMatt Macy 
115eda14cbcSMatt Macy 	attrs = (uint64_t *)(lrattr + lrattr->lr_attr_masksize - 1);
116eda14cbcSMatt Macy 	crtime = attrs + 1;
117eda14cbcSMatt Macy 	scanstamp = (caddr_t)(crtime + 2);
118eda14cbcSMatt Macy 
119eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
120eda14cbcSMatt Macy 		xoap->xoa_hidden = ((*attrs & XAT0_HIDDEN) != 0);
121eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
122eda14cbcSMatt Macy 		xoap->xoa_system = ((*attrs & XAT0_SYSTEM) != 0);
123eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
124eda14cbcSMatt Macy 		xoap->xoa_archive = ((*attrs & XAT0_ARCHIVE) != 0);
125eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_READONLY))
126eda14cbcSMatt Macy 		xoap->xoa_readonly = ((*attrs & XAT0_READONLY) != 0);
127eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
128eda14cbcSMatt Macy 		xoap->xoa_immutable = ((*attrs & XAT0_IMMUTABLE) != 0);
129eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
130eda14cbcSMatt Macy 		xoap->xoa_nounlink = ((*attrs & XAT0_NOUNLINK) != 0);
131eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
132eda14cbcSMatt Macy 		xoap->xoa_appendonly = ((*attrs & XAT0_APPENDONLY) != 0);
133eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
134eda14cbcSMatt Macy 		xoap->xoa_nodump = ((*attrs & XAT0_NODUMP) != 0);
135eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
136eda14cbcSMatt Macy 		xoap->xoa_opaque = ((*attrs & XAT0_OPAQUE) != 0);
137eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
138eda14cbcSMatt Macy 		xoap->xoa_av_modified = ((*attrs & XAT0_AV_MODIFIED) != 0);
139eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
140eda14cbcSMatt Macy 		xoap->xoa_av_quarantined =
141eda14cbcSMatt Macy 		    ((*attrs & XAT0_AV_QUARANTINED) != 0);
142eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
143eda14cbcSMatt Macy 		ZFS_TIME_DECODE(&xoap->xoa_createtime, crtime);
144eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
145eda14cbcSMatt Macy 		ASSERT(!XVA_ISSET_REQ(xvap, XAT_PROJID));
146eda14cbcSMatt Macy 
147da5137abSMartin Matuska 		memcpy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
148eda14cbcSMatt Macy 	} else if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
149eda14cbcSMatt Macy 		/*
150eda14cbcSMatt Macy 		 * XAT_PROJID and XAT_AV_SCANSTAMP will never be valid
151eda14cbcSMatt Macy 		 * at the same time, so we can share the same space.
152eda14cbcSMatt Macy 		 */
153da5137abSMartin Matuska 		memcpy(&xoap->xoa_projid, scanstamp, sizeof (uint64_t));
154eda14cbcSMatt Macy 	}
155eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
156eda14cbcSMatt Macy 		xoap->xoa_reparse = ((*attrs & XAT0_REPARSE) != 0);
157eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
158eda14cbcSMatt Macy 		xoap->xoa_offline = ((*attrs & XAT0_OFFLINE) != 0);
159eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
160eda14cbcSMatt Macy 		xoap->xoa_sparse = ((*attrs & XAT0_SPARSE) != 0);
161eda14cbcSMatt Macy 	if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT))
162eda14cbcSMatt Macy 		xoap->xoa_projinherit = ((*attrs & XAT0_PROJINHERIT) != 0);
163eda14cbcSMatt Macy }
164eda14cbcSMatt Macy 
165eda14cbcSMatt Macy static int
zfs_replay_domain_cnt(uint64_t uid,uint64_t gid)166eda14cbcSMatt Macy zfs_replay_domain_cnt(uint64_t uid, uint64_t gid)
167eda14cbcSMatt Macy {
168eda14cbcSMatt Macy 	uint64_t uid_idx;
169eda14cbcSMatt Macy 	uint64_t gid_idx;
170eda14cbcSMatt Macy 	int domcnt = 0;
171eda14cbcSMatt Macy 
172eda14cbcSMatt Macy 	uid_idx = FUID_INDEX(uid);
173eda14cbcSMatt Macy 	gid_idx = FUID_INDEX(gid);
174eda14cbcSMatt Macy 	if (uid_idx)
175eda14cbcSMatt Macy 		domcnt++;
176eda14cbcSMatt Macy 	if (gid_idx > 0 && gid_idx != uid_idx)
177eda14cbcSMatt Macy 		domcnt++;
178eda14cbcSMatt Macy 
179eda14cbcSMatt Macy 	return (domcnt);
180eda14cbcSMatt Macy }
181eda14cbcSMatt Macy 
182eda14cbcSMatt Macy static void *
zfs_replay_fuid_domain_common(zfs_fuid_info_t * fuid_infop,void * start,int domcnt)183eda14cbcSMatt Macy zfs_replay_fuid_domain_common(zfs_fuid_info_t *fuid_infop, void *start,
184eda14cbcSMatt Macy     int domcnt)
185eda14cbcSMatt Macy {
186eda14cbcSMatt Macy 	int i;
187eda14cbcSMatt Macy 
188eda14cbcSMatt Macy 	for (i = 0; i != domcnt; i++) {
189eda14cbcSMatt Macy 		fuid_infop->z_domain_table[i] = start;
190eda14cbcSMatt Macy 		start = (caddr_t)start + strlen(start) + 1;
191eda14cbcSMatt Macy 	}
192eda14cbcSMatt Macy 
193eda14cbcSMatt Macy 	return (start);
194eda14cbcSMatt Macy }
195eda14cbcSMatt Macy 
196eda14cbcSMatt Macy /*
197eda14cbcSMatt Macy  * Set the uid/gid in the fuid_info structure.
198eda14cbcSMatt Macy  */
199eda14cbcSMatt Macy static void
zfs_replay_fuid_ugid(zfs_fuid_info_t * fuid_infop,uint64_t uid,uint64_t gid)200eda14cbcSMatt Macy zfs_replay_fuid_ugid(zfs_fuid_info_t *fuid_infop, uint64_t uid, uint64_t gid)
201eda14cbcSMatt Macy {
202eda14cbcSMatt Macy 	/*
203eda14cbcSMatt Macy 	 * If owner or group are log specific FUIDs then slurp up
204eda14cbcSMatt Macy 	 * domain information and build zfs_fuid_info_t
205eda14cbcSMatt Macy 	 */
206eda14cbcSMatt Macy 	if (IS_EPHEMERAL(uid))
207eda14cbcSMatt Macy 		fuid_infop->z_fuid_owner = uid;
208eda14cbcSMatt Macy 
209eda14cbcSMatt Macy 	if (IS_EPHEMERAL(gid))
210eda14cbcSMatt Macy 		fuid_infop->z_fuid_group = gid;
211eda14cbcSMatt Macy }
212eda14cbcSMatt Macy 
213eda14cbcSMatt Macy /*
214eda14cbcSMatt Macy  * Load fuid domains into fuid_info_t
215eda14cbcSMatt Macy  */
216eda14cbcSMatt Macy static zfs_fuid_info_t *
zfs_replay_fuid_domain(void * buf,void ** end,uint64_t uid,uint64_t gid)217eda14cbcSMatt Macy zfs_replay_fuid_domain(void *buf, void **end, uint64_t uid, uint64_t gid)
218eda14cbcSMatt Macy {
219eda14cbcSMatt Macy 	int domcnt;
220eda14cbcSMatt Macy 
221eda14cbcSMatt Macy 	zfs_fuid_info_t *fuid_infop;
222eda14cbcSMatt Macy 
223eda14cbcSMatt Macy 	fuid_infop = zfs_fuid_info_alloc();
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy 	domcnt = zfs_replay_domain_cnt(uid, gid);
226eda14cbcSMatt Macy 
227eda14cbcSMatt Macy 	if (domcnt == 0)
228eda14cbcSMatt Macy 		return (fuid_infop);
229eda14cbcSMatt Macy 
230eda14cbcSMatt Macy 	fuid_infop->z_domain_table =
231eda14cbcSMatt Macy 	    kmem_zalloc(domcnt * sizeof (char *), KM_SLEEP);
232eda14cbcSMatt Macy 
233eda14cbcSMatt Macy 	zfs_replay_fuid_ugid(fuid_infop, uid, gid);
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy 	fuid_infop->z_domain_cnt = domcnt;
236eda14cbcSMatt Macy 	*end = zfs_replay_fuid_domain_common(fuid_infop, buf, domcnt);
237eda14cbcSMatt Macy 	return (fuid_infop);
238eda14cbcSMatt Macy }
239eda14cbcSMatt Macy 
240eda14cbcSMatt Macy /*
241eda14cbcSMatt Macy  * load zfs_fuid_t's and fuid_domains into fuid_info_t
242eda14cbcSMatt Macy  */
243eda14cbcSMatt Macy static zfs_fuid_info_t *
zfs_replay_fuids(void * start,void ** end,int idcnt,int domcnt,uint64_t uid,uint64_t gid)244eda14cbcSMatt Macy zfs_replay_fuids(void *start, void **end, int idcnt, int domcnt, uint64_t uid,
245eda14cbcSMatt Macy     uint64_t gid)
246eda14cbcSMatt Macy {
247eda14cbcSMatt Macy 	uint64_t *log_fuid = (uint64_t *)start;
248eda14cbcSMatt Macy 	zfs_fuid_info_t *fuid_infop;
249eda14cbcSMatt Macy 	int i;
250eda14cbcSMatt Macy 
251eda14cbcSMatt Macy 	fuid_infop = zfs_fuid_info_alloc();
252eda14cbcSMatt Macy 	fuid_infop->z_domain_cnt = domcnt;
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy 	fuid_infop->z_domain_table =
255eda14cbcSMatt Macy 	    kmem_zalloc(domcnt * sizeof (char *), KM_SLEEP);
256eda14cbcSMatt Macy 
257eda14cbcSMatt Macy 	for (i = 0; i != idcnt; i++) {
258eda14cbcSMatt Macy 		zfs_fuid_t *zfuid;
259eda14cbcSMatt Macy 
260eda14cbcSMatt Macy 		zfuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP);
261eda14cbcSMatt Macy 		zfuid->z_logfuid = *log_fuid;
262eda14cbcSMatt Macy 		zfuid->z_id = -1;
263eda14cbcSMatt Macy 		zfuid->z_domidx = 0;
264eda14cbcSMatt Macy 		list_insert_tail(&fuid_infop->z_fuids, zfuid);
265eda14cbcSMatt Macy 		log_fuid++;
266eda14cbcSMatt Macy 	}
267eda14cbcSMatt Macy 
268eda14cbcSMatt Macy 	zfs_replay_fuid_ugid(fuid_infop, uid, gid);
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy 	*end = zfs_replay_fuid_domain_common(fuid_infop, log_fuid, domcnt);
271eda14cbcSMatt Macy 	return (fuid_infop);
272eda14cbcSMatt Macy }
273eda14cbcSMatt Macy 
274eda14cbcSMatt Macy static void
zfs_replay_swap_attrs(lr_attr_t * lrattr)275eda14cbcSMatt Macy zfs_replay_swap_attrs(lr_attr_t *lrattr)
276eda14cbcSMatt Macy {
277eda14cbcSMatt Macy 	/* swap the lr_attr structure */
278eda14cbcSMatt Macy 	byteswap_uint32_array(lrattr, sizeof (*lrattr));
279eda14cbcSMatt Macy 	/* swap the bitmap */
280eda14cbcSMatt Macy 	byteswap_uint32_array(lrattr + 1, (lrattr->lr_attr_masksize - 1) *
281eda14cbcSMatt Macy 	    sizeof (uint32_t));
282eda14cbcSMatt Macy 	/* swap the attributes, create time + 64 bit word for attributes */
283eda14cbcSMatt Macy 	byteswap_uint64_array((caddr_t)(lrattr + 1) + (sizeof (uint32_t) *
284eda14cbcSMatt Macy 	    (lrattr->lr_attr_masksize - 1)), 3 * sizeof (uint64_t));
285eda14cbcSMatt Macy }
286eda14cbcSMatt Macy 
287eda14cbcSMatt Macy /*
288eda14cbcSMatt Macy  * Replay file create with optional ACL, xvattr information as well
289eda14cbcSMatt Macy  * as option FUID information.
290eda14cbcSMatt Macy  */
291eda14cbcSMatt Macy static int
zfs_replay_create_acl(void * arg1,void * arg2,boolean_t byteswap)292eda14cbcSMatt Macy zfs_replay_create_acl(void *arg1, void *arg2, boolean_t byteswap)
293eda14cbcSMatt Macy {
294eda14cbcSMatt Macy 	zfsvfs_t *zfsvfs = arg1;
295eda14cbcSMatt Macy 	lr_acl_create_t *lracl = arg2;
296*7a7741afSMartin Matuska 	_lr_create_t *lr = &lracl->lr_create;
297eda14cbcSMatt Macy 	char *name = NULL;		/* location determined later */
298eda14cbcSMatt Macy 	znode_t *dzp;
299eda14cbcSMatt Macy 	znode_t *zp;
300eda14cbcSMatt Macy 	xvattr_t xva;
301eda14cbcSMatt Macy 	int vflg = 0;
302eda14cbcSMatt Macy 	vsecattr_t vsec = { 0 };
303eda14cbcSMatt Macy 	lr_attr_t *lrattr;
304*7a7741afSMartin Matuska 	uint8_t *aclstart;
305*7a7741afSMartin Matuska 	uint8_t *fuidstart;
306eda14cbcSMatt Macy 	size_t xvatlen = 0;
307eda14cbcSMatt Macy 	uint64_t txtype;
308eda14cbcSMatt Macy 	uint64_t objid;
309eda14cbcSMatt Macy 	uint64_t dnodesize;
310eda14cbcSMatt Macy 	int error;
311eda14cbcSMatt Macy 
312525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lracl));
313525fe93dSMartin Matuska 
314eda14cbcSMatt Macy 	txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
315eda14cbcSMatt Macy 	if (byteswap) {
316eda14cbcSMatt Macy 		byteswap_uint64_array(lracl, sizeof (*lracl));
317eda14cbcSMatt Macy 		if (txtype == TX_CREATE_ACL_ATTR ||
318eda14cbcSMatt Macy 		    txtype == TX_MKDIR_ACL_ATTR) {
319*7a7741afSMartin Matuska 			lrattr = (lr_attr_t *)&lracl->lr_data[0];
320eda14cbcSMatt Macy 			zfs_replay_swap_attrs(lrattr);
321eda14cbcSMatt Macy 			xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
322eda14cbcSMatt Macy 		}
323eda14cbcSMatt Macy 
324*7a7741afSMartin Matuska 		aclstart = &lracl->lr_data[xvatlen];
325eda14cbcSMatt Macy 		zfs_ace_byteswap(aclstart, lracl->lr_acl_bytes, B_FALSE);
326*7a7741afSMartin Matuska 
327eda14cbcSMatt Macy 		/* swap fuids */
328eda14cbcSMatt Macy 		if (lracl->lr_fuidcnt) {
329*7a7741afSMartin Matuska 			byteswap_uint64_array(
330*7a7741afSMartin Matuska 			    &aclstart[ZIL_ACE_LENGTH(lracl->lr_acl_bytes)],
331eda14cbcSMatt Macy 			    lracl->lr_fuidcnt * sizeof (uint64_t));
332eda14cbcSMatt Macy 		}
333eda14cbcSMatt Macy 	}
334eda14cbcSMatt Macy 
335eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
336eda14cbcSMatt Macy 		return (error);
337eda14cbcSMatt Macy 
338eda14cbcSMatt Macy 	objid = LR_FOID_GET_OBJ(lr->lr_foid);
339eda14cbcSMatt Macy 	dnodesize = LR_FOID_GET_SLOTS(lr->lr_foid) << DNODE_SHIFT;
340eda14cbcSMatt Macy 
341eda14cbcSMatt Macy 	xva_init(&xva);
342eda14cbcSMatt Macy 	zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
343eda14cbcSMatt Macy 	    lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, objid);
344eda14cbcSMatt Macy 
345eda14cbcSMatt Macy 	/*
346eda14cbcSMatt Macy 	 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
347eda14cbcSMatt Macy 	 * eventually end up in zfs_mknode(), which assigns the object's
348eda14cbcSMatt Macy 	 * creation time, generation number, and dnode size. The generic
349eda14cbcSMatt Macy 	 * zfs_create() has no concept of these attributes, so we smuggle
350eda14cbcSMatt Macy 	 * the values inside the vattr's otherwise unused va_ctime,
351eda14cbcSMatt Macy 	 * va_nblocks, and va_fsid fields.
352eda14cbcSMatt Macy 	 */
353eda14cbcSMatt Macy 	ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
354eda14cbcSMatt Macy 	xva.xva_vattr.va_nblocks = lr->lr_gen;
355eda14cbcSMatt Macy 	xva.xva_vattr.va_fsid = dnodesize;
356eda14cbcSMatt Macy 
357eda14cbcSMatt Macy 	error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT);
358eda14cbcSMatt Macy 	if (error)
359eda14cbcSMatt Macy 		goto bail;
360eda14cbcSMatt Macy 
361eda14cbcSMatt Macy 	if (lr->lr_common.lrc_txtype & TX_CI)
362eda14cbcSMatt Macy 		vflg |= FIGNORECASE;
363eda14cbcSMatt Macy 	switch (txtype) {
364eda14cbcSMatt Macy 	case TX_CREATE_ACL:
365*7a7741afSMartin Matuska 		aclstart = &lracl->lr_data[0];
366*7a7741afSMartin Matuska 		fuidstart = &aclstart[ZIL_ACE_LENGTH(lracl->lr_acl_bytes)];
367eda14cbcSMatt Macy 		zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
368eda14cbcSMatt Macy 		    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
369eda14cbcSMatt Macy 		    lr->lr_uid, lr->lr_gid);
370c03c5b1cSMartin Matuska 		zfs_fallthrough;
371eda14cbcSMatt Macy 	case TX_CREATE_ACL_ATTR:
372eda14cbcSMatt Macy 		if (name == NULL) {
373*7a7741afSMartin Matuska 			lrattr = (lr_attr_t *)&lracl->lr_data[0];
374eda14cbcSMatt Macy 			xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
375eda14cbcSMatt Macy 			xva.xva_vattr.va_mask |= ATTR_XVATTR;
376eda14cbcSMatt Macy 			zfs_replay_xvattr(lrattr, &xva);
377eda14cbcSMatt Macy 		}
378eda14cbcSMatt Macy 		vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
379*7a7741afSMartin Matuska 		vsec.vsa_aclentp = &lracl->lr_data[xvatlen];
380eda14cbcSMatt Macy 		vsec.vsa_aclcnt = lracl->lr_aclcnt;
381eda14cbcSMatt Macy 		vsec.vsa_aclentsz = lracl->lr_acl_bytes;
382eda14cbcSMatt Macy 		vsec.vsa_aclflags = lracl->lr_acl_flags;
383eda14cbcSMatt Macy 		if (zfsvfs->z_fuid_replay == NULL) {
384*7a7741afSMartin Matuska 			fuidstart = &lracl->lr_data[xvatlen +
385*7a7741afSMartin Matuska 			    ZIL_ACE_LENGTH(lracl->lr_acl_bytes)];
386eda14cbcSMatt Macy 			zfsvfs->z_fuid_replay =
387eda14cbcSMatt Macy 			    zfs_replay_fuids(fuidstart,
388eda14cbcSMatt Macy 			    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
389eda14cbcSMatt Macy 			    lr->lr_uid, lr->lr_gid);
390eda14cbcSMatt Macy 		}
391eda14cbcSMatt Macy 
392dbd5678dSMartin Matuska #if defined(__linux__)
393eda14cbcSMatt Macy 		error = zfs_create(dzp, name, &xva.xva_vattr,
394d411c1d6SMartin Matuska 		    0, 0, &zp, kcred, vflg, &vsec, zfs_init_idmap);
395dbd5678dSMartin Matuska #else
396dbd5678dSMartin Matuska 		error = zfs_create(dzp, name, &xva.xva_vattr,
397dbd5678dSMartin Matuska 		    0, 0, &zp, kcred, vflg, &vsec, NULL);
398dbd5678dSMartin Matuska #endif
399eda14cbcSMatt Macy 		break;
400eda14cbcSMatt Macy 	case TX_MKDIR_ACL:
401*7a7741afSMartin Matuska 		aclstart = &lracl->lr_data[0];
402*7a7741afSMartin Matuska 		fuidstart = &aclstart[ZIL_ACE_LENGTH(lracl->lr_acl_bytes)];
403eda14cbcSMatt Macy 		zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
404eda14cbcSMatt Macy 		    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
405eda14cbcSMatt Macy 		    lr->lr_uid, lr->lr_gid);
406c03c5b1cSMartin Matuska 		zfs_fallthrough;
407eda14cbcSMatt Macy 	case TX_MKDIR_ACL_ATTR:
408eda14cbcSMatt Macy 		if (name == NULL) {
409eda14cbcSMatt Macy 			lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
410eda14cbcSMatt Macy 			xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
411eda14cbcSMatt Macy 			zfs_replay_xvattr(lrattr, &xva);
412eda14cbcSMatt Macy 		}
413eda14cbcSMatt Macy 		vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
414*7a7741afSMartin Matuska 		vsec.vsa_aclentp = &lracl->lr_data[xvatlen];
415eda14cbcSMatt Macy 		vsec.vsa_aclcnt = lracl->lr_aclcnt;
416eda14cbcSMatt Macy 		vsec.vsa_aclentsz = lracl->lr_acl_bytes;
417eda14cbcSMatt Macy 		vsec.vsa_aclflags = lracl->lr_acl_flags;
418eda14cbcSMatt Macy 		if (zfsvfs->z_fuid_replay == NULL) {
419*7a7741afSMartin Matuska 			fuidstart = &lracl->lr_data[xvatlen +
420*7a7741afSMartin Matuska 			    ZIL_ACE_LENGTH(lracl->lr_acl_bytes)];
421eda14cbcSMatt Macy 			zfsvfs->z_fuid_replay =
422eda14cbcSMatt Macy 			    zfs_replay_fuids(fuidstart,
423eda14cbcSMatt Macy 			    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
424eda14cbcSMatt Macy 			    lr->lr_uid, lr->lr_gid);
425eda14cbcSMatt Macy 		}
426dbd5678dSMartin Matuska #if defined(__linux__)
427eda14cbcSMatt Macy 		error = zfs_mkdir(dzp, name, &xva.xva_vattr,
428d411c1d6SMartin Matuska 		    &zp, kcred, vflg, &vsec, zfs_init_idmap);
429dbd5678dSMartin Matuska #else
430dbd5678dSMartin Matuska 		error = zfs_mkdir(dzp, name, &xva.xva_vattr,
431dbd5678dSMartin Matuska 		    &zp, kcred, vflg, &vsec, NULL);
432dbd5678dSMartin Matuska #endif
433eda14cbcSMatt Macy 		break;
434eda14cbcSMatt Macy 	default:
435eda14cbcSMatt Macy 		error = SET_ERROR(ENOTSUP);
436eda14cbcSMatt Macy 	}
437eda14cbcSMatt Macy 
438eda14cbcSMatt Macy bail:
439eda14cbcSMatt Macy 	if (error == 0 && zp != NULL) {
440eda14cbcSMatt Macy #ifdef __FreeBSD__
441ce4dcb97SMartin Matuska 		VOP_UNLOCK(ZTOV(zp));
442eda14cbcSMatt Macy #endif
443eda14cbcSMatt Macy 		zrele(zp);
444eda14cbcSMatt Macy 	}
445eda14cbcSMatt Macy 	zrele(dzp);
446eda14cbcSMatt Macy 
447eda14cbcSMatt Macy 	if (zfsvfs->z_fuid_replay)
448eda14cbcSMatt Macy 		zfs_fuid_info_free(zfsvfs->z_fuid_replay);
449eda14cbcSMatt Macy 	zfsvfs->z_fuid_replay = NULL;
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy 	return (error);
452eda14cbcSMatt Macy }
453eda14cbcSMatt Macy 
454eda14cbcSMatt Macy static int
zfs_replay_create(void * arg1,void * arg2,boolean_t byteswap)455eda14cbcSMatt Macy zfs_replay_create(void *arg1, void *arg2, boolean_t byteswap)
456eda14cbcSMatt Macy {
457eda14cbcSMatt Macy 	zfsvfs_t *zfsvfs = arg1;
458*7a7741afSMartin Matuska 	lr_create_t *lrc = arg2;
459*7a7741afSMartin Matuska 	_lr_create_t *lr = &lrc->lr_create;
460eda14cbcSMatt Macy 	char *name = NULL;		/* location determined later */
461eda14cbcSMatt Macy 	char *link;			/* symlink content follows name */
462eda14cbcSMatt Macy 	znode_t *dzp;
463eda14cbcSMatt Macy 	znode_t *zp = NULL;
464eda14cbcSMatt Macy 	xvattr_t xva;
465eda14cbcSMatt Macy 	int vflg = 0;
466eda14cbcSMatt Macy 	lr_attr_t *lrattr;
467eda14cbcSMatt Macy 	void *start;
468eda14cbcSMatt Macy 	size_t xvatlen;
469eda14cbcSMatt Macy 	uint64_t txtype;
470eda14cbcSMatt Macy 	uint64_t objid;
471eda14cbcSMatt Macy 	uint64_t dnodesize;
472eda14cbcSMatt Macy 	int error;
473eda14cbcSMatt Macy 
474525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
475525fe93dSMartin Matuska 
476eda14cbcSMatt Macy 	txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
477eda14cbcSMatt Macy 	if (byteswap) {
478*7a7741afSMartin Matuska 		byteswap_uint64_array(lrc, sizeof (*lrc));
479eda14cbcSMatt Macy 		if (txtype == TX_CREATE_ATTR || txtype == TX_MKDIR_ATTR)
480*7a7741afSMartin Matuska 			zfs_replay_swap_attrs((lr_attr_t *)&lrc->lr_data[0]);
481eda14cbcSMatt Macy 	}
482eda14cbcSMatt Macy 
483eda14cbcSMatt Macy 
484eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
485eda14cbcSMatt Macy 		return (error);
486eda14cbcSMatt Macy 
487eda14cbcSMatt Macy 	objid = LR_FOID_GET_OBJ(lr->lr_foid);
488eda14cbcSMatt Macy 	dnodesize = LR_FOID_GET_SLOTS(lr->lr_foid) << DNODE_SHIFT;
489eda14cbcSMatt Macy 
490eda14cbcSMatt Macy 	xva_init(&xva);
491eda14cbcSMatt Macy 	zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
492eda14cbcSMatt Macy 	    lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, objid);
493eda14cbcSMatt Macy 
494eda14cbcSMatt Macy 	/*
495eda14cbcSMatt Macy 	 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
496eda14cbcSMatt Macy 	 * eventually end up in zfs_mknode(), which assigns the object's
497eda14cbcSMatt Macy 	 * creation time, generation number, and dnode slot count. The
498eda14cbcSMatt Macy 	 * generic zfs_create() has no concept of these attributes, so
499eda14cbcSMatt Macy 	 * we smuggle the values inside the vattr's otherwise unused
500eda14cbcSMatt Macy 	 * va_ctime, va_nblocks, and va_fsid fields.
501eda14cbcSMatt Macy 	 */
502eda14cbcSMatt Macy 	ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
503eda14cbcSMatt Macy 	xva.xva_vattr.va_nblocks = lr->lr_gen;
504eda14cbcSMatt Macy 	xva.xva_vattr.va_fsid = dnodesize;
505eda14cbcSMatt Macy 
506eda14cbcSMatt Macy 	error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT);
507eda14cbcSMatt Macy 	if (error)
508eda14cbcSMatt Macy 		goto out;
509eda14cbcSMatt Macy 
510eda14cbcSMatt Macy 	if (lr->lr_common.lrc_txtype & TX_CI)
511eda14cbcSMatt Macy 		vflg |= FIGNORECASE;
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy 	/*
514eda14cbcSMatt Macy 	 * Symlinks don't have fuid info, and CIFS never creates
515eda14cbcSMatt Macy 	 * symlinks.
516eda14cbcSMatt Macy 	 *
517eda14cbcSMatt Macy 	 * The _ATTR versions will grab the fuid info in their subcases.
518eda14cbcSMatt Macy 	 */
519c9539b89SMartin Matuska 	if (txtype != TX_SYMLINK &&
520c9539b89SMartin Matuska 	    txtype != TX_MKDIR_ATTR &&
521c9539b89SMartin Matuska 	    txtype != TX_CREATE_ATTR) {
522*7a7741afSMartin Matuska 		start = (void *)&lrc->lr_data[0];
523eda14cbcSMatt Macy 		zfsvfs->z_fuid_replay =
524eda14cbcSMatt Macy 		    zfs_replay_fuid_domain(start, &start,
525eda14cbcSMatt Macy 		    lr->lr_uid, lr->lr_gid);
526eda14cbcSMatt Macy 	}
527eda14cbcSMatt Macy 
528eda14cbcSMatt Macy 	switch (txtype) {
529eda14cbcSMatt Macy 	case TX_CREATE_ATTR:
530*7a7741afSMartin Matuska 		lrattr = (lr_attr_t *)&lrc->lr_data[0];
531eda14cbcSMatt Macy 		xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
532*7a7741afSMartin Matuska 		zfs_replay_xvattr(lrattr, &xva);
533*7a7741afSMartin Matuska 		start = (void *)&lrc->lr_data[xvatlen];
534eda14cbcSMatt Macy 		zfsvfs->z_fuid_replay =
535eda14cbcSMatt Macy 		    zfs_replay_fuid_domain(start, &start,
536eda14cbcSMatt Macy 		    lr->lr_uid, lr->lr_gid);
537eda14cbcSMatt Macy 		name = (char *)start;
538c03c5b1cSMartin Matuska 		zfs_fallthrough;
5391f88aa09SMartin Matuska 
540eda14cbcSMatt Macy 	case TX_CREATE:
541eda14cbcSMatt Macy 		if (name == NULL)
542eda14cbcSMatt Macy 			name = (char *)start;
543eda14cbcSMatt Macy 
544dbd5678dSMartin Matuska #if defined(__linux__)
545eda14cbcSMatt Macy 		error = zfs_create(dzp, name, &xva.xva_vattr,
546d411c1d6SMartin Matuska 		    0, 0, &zp, kcred, vflg, NULL, zfs_init_idmap);
547dbd5678dSMartin Matuska #else
548dbd5678dSMartin Matuska 		error = zfs_create(dzp, name, &xva.xva_vattr,
549dbd5678dSMartin Matuska 		    0, 0, &zp, kcred, vflg, NULL, NULL);
550dbd5678dSMartin Matuska #endif
551eda14cbcSMatt Macy 		break;
552eda14cbcSMatt Macy 	case TX_MKDIR_ATTR:
553*7a7741afSMartin Matuska 		lrattr = (lr_attr_t *)&lrc->lr_data[0];
554eda14cbcSMatt Macy 		xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
555*7a7741afSMartin Matuska 		zfs_replay_xvattr(lrattr, &xva);
556*7a7741afSMartin Matuska 		start = &lrc->lr_data[xvatlen];
557eda14cbcSMatt Macy 		zfsvfs->z_fuid_replay =
558eda14cbcSMatt Macy 		    zfs_replay_fuid_domain(start, &start,
559eda14cbcSMatt Macy 		    lr->lr_uid, lr->lr_gid);
560eda14cbcSMatt Macy 		name = (char *)start;
561c03c5b1cSMartin Matuska 		zfs_fallthrough;
5621f88aa09SMartin Matuska 
563eda14cbcSMatt Macy 	case TX_MKDIR:
564eda14cbcSMatt Macy 		if (name == NULL)
565*7a7741afSMartin Matuska 			name = (char *)&lrc->lr_data[0];
566eda14cbcSMatt Macy 
567dbd5678dSMartin Matuska #if defined(__linux__)
568eda14cbcSMatt Macy 		error = zfs_mkdir(dzp, name, &xva.xva_vattr,
569d411c1d6SMartin Matuska 		    &zp, kcred, vflg, NULL, zfs_init_idmap);
570dbd5678dSMartin Matuska #else
571dbd5678dSMartin Matuska 		error = zfs_mkdir(dzp, name, &xva.xva_vattr,
572dbd5678dSMartin Matuska 		    &zp, kcred, vflg, NULL, NULL);
573dbd5678dSMartin Matuska #endif
574dbd5678dSMartin Matuska 
575eda14cbcSMatt Macy 		break;
576eda14cbcSMatt Macy 	case TX_MKXATTR:
577eda14cbcSMatt Macy 		error = zfs_make_xattrdir(dzp, &xva.xva_vattr, &zp, kcred);
578eda14cbcSMatt Macy 		break;
579eda14cbcSMatt Macy 	case TX_SYMLINK:
580*7a7741afSMartin Matuska 		name = &lrc->lr_data[0];
581*7a7741afSMartin Matuska 		link = &lrc->lr_data[strlen(name) + 1];
582dbd5678dSMartin Matuska #if defined(__linux__)
583eda14cbcSMatt Macy 		error = zfs_symlink(dzp, name, &xva.xva_vattr,
584d411c1d6SMartin Matuska 		    link, &zp, kcred, vflg, zfs_init_idmap);
585dbd5678dSMartin Matuska #else
586dbd5678dSMartin Matuska 		error = zfs_symlink(dzp, name, &xva.xva_vattr,
587dbd5678dSMartin Matuska 		    link, &zp, kcred, vflg, NULL);
588dbd5678dSMartin Matuska #endif
589eda14cbcSMatt Macy 		break;
590eda14cbcSMatt Macy 	default:
591eda14cbcSMatt Macy 		error = SET_ERROR(ENOTSUP);
592eda14cbcSMatt Macy 	}
593eda14cbcSMatt Macy 
594eda14cbcSMatt Macy out:
595eda14cbcSMatt Macy 	if (error == 0 && zp != NULL) {
596eda14cbcSMatt Macy #ifdef __FreeBSD__
597ce4dcb97SMartin Matuska 		VOP_UNLOCK(ZTOV(zp));
598eda14cbcSMatt Macy #endif
599eda14cbcSMatt Macy 		zrele(zp);
600eda14cbcSMatt Macy 	}
601eda14cbcSMatt Macy 	zrele(dzp);
602eda14cbcSMatt Macy 
603eda14cbcSMatt Macy 	if (zfsvfs->z_fuid_replay)
604eda14cbcSMatt Macy 		zfs_fuid_info_free(zfsvfs->z_fuid_replay);
605eda14cbcSMatt Macy 	zfsvfs->z_fuid_replay = NULL;
606eda14cbcSMatt Macy 	return (error);
607eda14cbcSMatt Macy }
608eda14cbcSMatt Macy 
609eda14cbcSMatt Macy static int
zfs_replay_remove(void * arg1,void * arg2,boolean_t byteswap)610eda14cbcSMatt Macy zfs_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
611eda14cbcSMatt Macy {
612eda14cbcSMatt Macy 	zfsvfs_t *zfsvfs = arg1;
613eda14cbcSMatt Macy 	lr_remove_t *lr = arg2;
614*7a7741afSMartin Matuska 	char *name = (char *)&lr->lr_data[0];	/* name follows lr_remove_t */
615eda14cbcSMatt Macy 	znode_t *dzp;
616eda14cbcSMatt Macy 	int error;
617eda14cbcSMatt Macy 	int vflg = 0;
618eda14cbcSMatt Macy 
619525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
620525fe93dSMartin Matuska 
621eda14cbcSMatt Macy 	if (byteswap)
622eda14cbcSMatt Macy 		byteswap_uint64_array(lr, sizeof (*lr));
623eda14cbcSMatt Macy 
624eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
625eda14cbcSMatt Macy 		return (error);
626eda14cbcSMatt Macy 
627eda14cbcSMatt Macy 	if (lr->lr_common.lrc_txtype & TX_CI)
628eda14cbcSMatt Macy 		vflg |= FIGNORECASE;
629eda14cbcSMatt Macy 
630eda14cbcSMatt Macy 	switch ((int)lr->lr_common.lrc_txtype) {
631eda14cbcSMatt Macy 	case TX_REMOVE:
632eda14cbcSMatt Macy 		error = zfs_remove(dzp, name, kcred, vflg);
633eda14cbcSMatt Macy 		break;
634eda14cbcSMatt Macy 	case TX_RMDIR:
635eda14cbcSMatt Macy 		error = zfs_rmdir(dzp, name, NULL, kcred, vflg);
636eda14cbcSMatt Macy 		break;
637eda14cbcSMatt Macy 	default:
638eda14cbcSMatt Macy 		error = SET_ERROR(ENOTSUP);
639eda14cbcSMatt Macy 	}
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy 	zrele(dzp);
642eda14cbcSMatt Macy 
643eda14cbcSMatt Macy 	return (error);
644eda14cbcSMatt Macy }
645eda14cbcSMatt Macy 
646eda14cbcSMatt Macy static int
zfs_replay_link(void * arg1,void * arg2,boolean_t byteswap)647eda14cbcSMatt Macy zfs_replay_link(void *arg1, void *arg2, boolean_t byteswap)
648eda14cbcSMatt Macy {
649eda14cbcSMatt Macy 	zfsvfs_t *zfsvfs = arg1;
650eda14cbcSMatt Macy 	lr_link_t *lr = arg2;
651*7a7741afSMartin Matuska 	char *name = &lr->lr_data[0];	/* name follows lr_link_t */
652eda14cbcSMatt Macy 	znode_t *dzp, *zp;
653eda14cbcSMatt Macy 	int error;
654eda14cbcSMatt Macy 	int vflg = 0;
655eda14cbcSMatt Macy 
656525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
657525fe93dSMartin Matuska 
658eda14cbcSMatt Macy 	if (byteswap)
659eda14cbcSMatt Macy 		byteswap_uint64_array(lr, sizeof (*lr));
660eda14cbcSMatt Macy 
661eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
662eda14cbcSMatt Macy 		return (error);
663eda14cbcSMatt Macy 
664eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
665eda14cbcSMatt Macy 		zrele(dzp);
666eda14cbcSMatt Macy 		return (error);
667eda14cbcSMatt Macy 	}
668eda14cbcSMatt Macy 
669eda14cbcSMatt Macy 	if (lr->lr_common.lrc_txtype & TX_CI)
670eda14cbcSMatt Macy 		vflg |= FIGNORECASE;
671eda14cbcSMatt Macy 
672eda14cbcSMatt Macy 	error = zfs_link(dzp, zp, name, kcred, vflg);
673eda14cbcSMatt Macy 	zrele(zp);
674eda14cbcSMatt Macy 	zrele(dzp);
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy 	return (error);
677eda14cbcSMatt Macy }
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy static int
do_zfs_replay_rename(zfsvfs_t * zfsvfs,_lr_rename_t * lr,char * sname,char * tname,uint64_t rflags,vattr_t * wo_vap)680*7a7741afSMartin Matuska do_zfs_replay_rename(zfsvfs_t *zfsvfs, _lr_rename_t *lr, char *sname,
681dbd5678dSMartin Matuska     char *tname, uint64_t rflags, vattr_t *wo_vap)
682eda14cbcSMatt Macy {
683eda14cbcSMatt Macy 	znode_t *sdzp, *tdzp;
684dbd5678dSMartin Matuska 	int error, vflg = 0;
685eda14cbcSMatt Macy 
686dbd5678dSMartin Matuska 	/* Only Linux currently supports RENAME_* flags. */
687dbd5678dSMartin Matuska #ifdef __linux__
688dbd5678dSMartin Matuska 	VERIFY0(rflags & ~(RENAME_EXCHANGE | RENAME_WHITEOUT));
689dbd5678dSMartin Matuska 
690dbd5678dSMartin Matuska 	/* wo_vap must be non-NULL iff. we're doing RENAME_WHITEOUT */
691dbd5678dSMartin Matuska 	VERIFY_EQUIV(rflags & RENAME_WHITEOUT, wo_vap != NULL);
692dbd5678dSMartin Matuska #else
693dbd5678dSMartin Matuska 	VERIFY0(rflags);
694dbd5678dSMartin Matuska #endif
695eda14cbcSMatt Macy 
696eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
697eda14cbcSMatt Macy 		return (error);
698eda14cbcSMatt Macy 
699eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
700eda14cbcSMatt Macy 		zrele(sdzp);
701eda14cbcSMatt Macy 		return (error);
702eda14cbcSMatt Macy 	}
703eda14cbcSMatt Macy 
704eda14cbcSMatt Macy 	if (lr->lr_common.lrc_txtype & TX_CI)
705eda14cbcSMatt Macy 		vflg |= FIGNORECASE;
706eda14cbcSMatt Macy 
707dbd5678dSMartin Matuska #if defined(__linux__)
708dbd5678dSMartin Matuska 	error = zfs_rename(sdzp, sname, tdzp, tname, kcred, vflg, rflags,
709d411c1d6SMartin Matuska 	    wo_vap, zfs_init_idmap);
710dbd5678dSMartin Matuska #else
711dbd5678dSMartin Matuska 	error = zfs_rename(sdzp, sname, tdzp, tname, kcred, vflg, rflags,
712dbd5678dSMartin Matuska 	    wo_vap, NULL);
713dbd5678dSMartin Matuska #endif
714eda14cbcSMatt Macy 
715eda14cbcSMatt Macy 	zrele(tdzp);
716eda14cbcSMatt Macy 	zrele(sdzp);
717eda14cbcSMatt Macy 	return (error);
718eda14cbcSMatt Macy }
719eda14cbcSMatt Macy 
720eda14cbcSMatt Macy static int
zfs_replay_rename(void * arg1,void * arg2,boolean_t byteswap)721dbd5678dSMartin Matuska zfs_replay_rename(void *arg1, void *arg2, boolean_t byteswap)
722dbd5678dSMartin Matuska {
723dbd5678dSMartin Matuska 	zfsvfs_t *zfsvfs = arg1;
724*7a7741afSMartin Matuska 	lr_rename_t *lrr = arg2;
725*7a7741afSMartin Matuska 	_lr_rename_t *lr = &lrr->lr_rename;
726525fe93dSMartin Matuska 
727525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
728dbd5678dSMartin Matuska 
729dbd5678dSMartin Matuska 	if (byteswap)
730*7a7741afSMartin Matuska 		byteswap_uint64_array(lrr, sizeof (*lrr));
731dbd5678dSMartin Matuska 
732*7a7741afSMartin Matuska 	/* sname and tname follow lr_rename_t */
733*7a7741afSMartin Matuska 	char *sname = (char *)&lrr->lr_data[0];
734*7a7741afSMartin Matuska 	char *tname = (char *)&lrr->lr_data[strlen(sname)+1];
735dbd5678dSMartin Matuska 	return (do_zfs_replay_rename(zfsvfs, lr, sname, tname, 0, NULL));
736dbd5678dSMartin Matuska }
737dbd5678dSMartin Matuska 
738dbd5678dSMartin Matuska static int
zfs_replay_rename_exchange(void * arg1,void * arg2,boolean_t byteswap)739dbd5678dSMartin Matuska zfs_replay_rename_exchange(void *arg1, void *arg2, boolean_t byteswap)
740dbd5678dSMartin Matuska {
741dbd5678dSMartin Matuska #ifdef __linux__
742dbd5678dSMartin Matuska 	zfsvfs_t *zfsvfs = arg1;
743*7a7741afSMartin Matuska 	lr_rename_t *lrr = arg2;
744*7a7741afSMartin Matuska 	_lr_rename_t *lr = &lrr->lr_rename;
745525fe93dSMartin Matuska 
746525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
747dbd5678dSMartin Matuska 
748dbd5678dSMartin Matuska 	if (byteswap)
749*7a7741afSMartin Matuska 		byteswap_uint64_array(lrr, sizeof (*lrr));
750dbd5678dSMartin Matuska 
751*7a7741afSMartin Matuska 	/* sname and tname follow lr_rename_t */
752*7a7741afSMartin Matuska 	char *sname = (char *)&lrr->lr_data[0];
753*7a7741afSMartin Matuska 	char *tname = (char *)&lrr->lr_data[strlen(sname)+1];
754dbd5678dSMartin Matuska 	return (do_zfs_replay_rename(zfsvfs, lr, sname, tname, RENAME_EXCHANGE,
755dbd5678dSMartin Matuska 	    NULL));
756dbd5678dSMartin Matuska #else
757dbd5678dSMartin Matuska 	return (SET_ERROR(ENOTSUP));
758dbd5678dSMartin Matuska #endif
759dbd5678dSMartin Matuska }
760dbd5678dSMartin Matuska 
761dbd5678dSMartin Matuska static int
zfs_replay_rename_whiteout(void * arg1,void * arg2,boolean_t byteswap)762dbd5678dSMartin Matuska zfs_replay_rename_whiteout(void *arg1, void *arg2, boolean_t byteswap)
763dbd5678dSMartin Matuska {
764dbd5678dSMartin Matuska #ifdef __linux__
765dbd5678dSMartin Matuska 	zfsvfs_t *zfsvfs = arg1;
766*7a7741afSMartin Matuska 	lr_rename_whiteout_t *lrrw = arg2;
767*7a7741afSMartin Matuska 	_lr_rename_t *lr = &lrrw->lr_rename;
768dbd5678dSMartin Matuska 	int error;
769dbd5678dSMartin Matuska 	/* For the whiteout file. */
770dbd5678dSMartin Matuska 	xvattr_t xva;
771dbd5678dSMartin Matuska 	uint64_t objid;
772dbd5678dSMartin Matuska 	uint64_t dnodesize;
773dbd5678dSMartin Matuska 
774*7a7741afSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
775525fe93dSMartin Matuska 
776dbd5678dSMartin Matuska 	if (byteswap)
777*7a7741afSMartin Matuska 		byteswap_uint64_array(lrrw, sizeof (*lrrw));
778dbd5678dSMartin Matuska 
779*7a7741afSMartin Matuska 	objid = LR_FOID_GET_OBJ(lrrw->lr_wfoid);
780*7a7741afSMartin Matuska 	dnodesize = LR_FOID_GET_SLOTS(lrrw->lr_wfoid) << DNODE_SHIFT;
781dbd5678dSMartin Matuska 
782dbd5678dSMartin Matuska 	xva_init(&xva);
783dbd5678dSMartin Matuska 	zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
784*7a7741afSMartin Matuska 	    lrrw->lr_wmode, lrrw->lr_wuid, lrrw->lr_wgid, lrrw->lr_wrdev,
785*7a7741afSMartin Matuska 	    objid);
786dbd5678dSMartin Matuska 
787dbd5678dSMartin Matuska 	/*
788dbd5678dSMartin Matuska 	 * As with TX_CREATE, RENAME_WHITEOUT ends up in zfs_mknode(), which
789dbd5678dSMartin Matuska 	 * assigns the object's creation time, generation number, and dnode
790dbd5678dSMartin Matuska 	 * slot count. The generic zfs_rename() has no concept of these
791dbd5678dSMartin Matuska 	 * attributes, so we smuggle the values inside the vattr's otherwise
792dbd5678dSMartin Matuska 	 * unused va_ctime, va_nblocks, and va_fsid fields.
793dbd5678dSMartin Matuska 	 */
794*7a7741afSMartin Matuska 	ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lrrw->lr_wcrtime);
795*7a7741afSMartin Matuska 	xva.xva_vattr.va_nblocks = lrrw->lr_wgen;
796dbd5678dSMartin Matuska 	xva.xva_vattr.va_fsid = dnodesize;
797dbd5678dSMartin Matuska 
798dbd5678dSMartin Matuska 	error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT);
799dbd5678dSMartin Matuska 	if (error)
800dbd5678dSMartin Matuska 		return (error);
801dbd5678dSMartin Matuska 
802525fe93dSMartin Matuska 	/* sname and tname follow lr_rename_whiteout_t */
803*7a7741afSMartin Matuska 	char *sname = (char *)&lrrw->lr_data[0];
804*7a7741afSMartin Matuska 	char *tname = (char *)&lrrw->lr_data[strlen(sname)+1];
805*7a7741afSMartin Matuska 	return (do_zfs_replay_rename(zfsvfs, lr, sname, tname,
806dbd5678dSMartin Matuska 	    RENAME_WHITEOUT, &xva.xva_vattr));
807dbd5678dSMartin Matuska #else
808dbd5678dSMartin Matuska 	return (SET_ERROR(ENOTSUP));
809dbd5678dSMartin Matuska #endif
810dbd5678dSMartin Matuska }
811dbd5678dSMartin Matuska 
812dbd5678dSMartin Matuska static int
zfs_replay_write(void * arg1,void * arg2,boolean_t byteswap)813eda14cbcSMatt Macy zfs_replay_write(void *arg1, void *arg2, boolean_t byteswap)
814eda14cbcSMatt Macy {
815eda14cbcSMatt Macy 	zfsvfs_t *zfsvfs = arg1;
816eda14cbcSMatt Macy 	lr_write_t *lr = arg2;
817*7a7741afSMartin Matuska 	char *data = &lr->lr_data[0];	/* data follows lr_write_t */
818eda14cbcSMatt Macy 	znode_t	*zp;
819eda14cbcSMatt Macy 	int error;
820eda14cbcSMatt Macy 	uint64_t eod, offset, length;
821eda14cbcSMatt Macy 
822525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
823525fe93dSMartin Matuska 
824eda14cbcSMatt Macy 	if (byteswap)
825eda14cbcSMatt Macy 		byteswap_uint64_array(lr, sizeof (*lr));
826eda14cbcSMatt Macy 
827eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
828eda14cbcSMatt Macy 		/*
829eda14cbcSMatt Macy 		 * As we can log writes out of order, it's possible the
830eda14cbcSMatt Macy 		 * file has been removed. In this case just drop the write
831eda14cbcSMatt Macy 		 * and return success.
832eda14cbcSMatt Macy 		 */
833eda14cbcSMatt Macy 		if (error == ENOENT)
834eda14cbcSMatt Macy 			error = 0;
835eda14cbcSMatt Macy 		return (error);
836eda14cbcSMatt Macy 	}
837eda14cbcSMatt Macy 
838eda14cbcSMatt Macy 	offset = lr->lr_offset;
839eda14cbcSMatt Macy 	length = lr->lr_length;
840eda14cbcSMatt Macy 	eod = offset + length;	/* end of data for this write */
841eda14cbcSMatt Macy 
842eda14cbcSMatt Macy 	/*
843eda14cbcSMatt Macy 	 * This may be a write from a dmu_sync() for a whole block,
844eda14cbcSMatt Macy 	 * and may extend beyond the current end of the file.
845eda14cbcSMatt Macy 	 * We can't just replay what was written for this TX_WRITE as
846eda14cbcSMatt Macy 	 * a future TX_WRITE2 may extend the eof and the data for that
847eda14cbcSMatt Macy 	 * write needs to be there. So we write the whole block and
848eda14cbcSMatt Macy 	 * reduce the eof. This needs to be done within the single dmu
849eda14cbcSMatt Macy 	 * transaction created within vn_rdwr -> zfs_write. So a possible
850eda14cbcSMatt Macy 	 * new end of file is passed through in zfsvfs->z_replay_eof
851eda14cbcSMatt Macy 	 */
852eda14cbcSMatt Macy 
853eda14cbcSMatt Macy 	zfsvfs->z_replay_eof = 0; /* 0 means don't change end of file */
854eda14cbcSMatt Macy 
855eda14cbcSMatt Macy 	/* If it's a dmu_sync() block, write the whole block */
856eda14cbcSMatt Macy 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
857eda14cbcSMatt Macy 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
858eda14cbcSMatt Macy 		if (length < blocksize) {
859eda14cbcSMatt Macy 			offset -= offset % blocksize;
860eda14cbcSMatt Macy 			length = blocksize;
861eda14cbcSMatt Macy 		}
862eda14cbcSMatt Macy 		if (zp->z_size < eod)
863eda14cbcSMatt Macy 			zfsvfs->z_replay_eof = eod;
864eda14cbcSMatt Macy 	}
865eda14cbcSMatt Macy 	error = zfs_write_simple(zp, data, length, offset, NULL);
866eda14cbcSMatt Macy 	zrele(zp);
867eda14cbcSMatt Macy 	zfsvfs->z_replay_eof = 0;	/* safety */
868eda14cbcSMatt Macy 
869eda14cbcSMatt Macy 	return (error);
870eda14cbcSMatt Macy }
871eda14cbcSMatt Macy 
872eda14cbcSMatt Macy /*
873eda14cbcSMatt Macy  * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
874eda14cbcSMatt Macy  * meaning the pool block is already being synced. So now that we always write
875eda14cbcSMatt Macy  * out full blocks, all we have to do is expand the eof if
876eda14cbcSMatt Macy  * the file is grown.
877eda14cbcSMatt Macy  */
878eda14cbcSMatt Macy static int
zfs_replay_write2(void * arg1,void * arg2,boolean_t byteswap)879eda14cbcSMatt Macy zfs_replay_write2(void *arg1, void *arg2, boolean_t byteswap)
880eda14cbcSMatt Macy {
881eda14cbcSMatt Macy 	zfsvfs_t *zfsvfs = arg1;
882eda14cbcSMatt Macy 	lr_write_t *lr = arg2;
883eda14cbcSMatt Macy 	znode_t	*zp;
884eda14cbcSMatt Macy 	int error;
885eda14cbcSMatt Macy 	uint64_t end;
886eda14cbcSMatt Macy 
887525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
888525fe93dSMartin Matuska 
889eda14cbcSMatt Macy 	if (byteswap)
890eda14cbcSMatt Macy 		byteswap_uint64_array(lr, sizeof (*lr));
891eda14cbcSMatt Macy 
892eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
893eda14cbcSMatt Macy 		return (error);
894eda14cbcSMatt Macy 
895eda14cbcSMatt Macy top:
896eda14cbcSMatt Macy 	end = lr->lr_offset + lr->lr_length;
897eda14cbcSMatt Macy 	if (end > zp->z_size) {
898eda14cbcSMatt Macy 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
899eda14cbcSMatt Macy 
900eda14cbcSMatt Macy 		zp->z_size = end;
901eda14cbcSMatt Macy 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
902eda14cbcSMatt Macy 		error = dmu_tx_assign(tx, TXG_WAIT);
903eda14cbcSMatt Macy 		if (error) {
904eda14cbcSMatt Macy 			zrele(zp);
905eda14cbcSMatt Macy 			if (error == ERESTART) {
906eda14cbcSMatt Macy 				dmu_tx_wait(tx);
907eda14cbcSMatt Macy 				dmu_tx_abort(tx);
908eda14cbcSMatt Macy 				goto top;
909eda14cbcSMatt Macy 			}
910eda14cbcSMatt Macy 			dmu_tx_abort(tx);
911eda14cbcSMatt Macy 			return (error);
912eda14cbcSMatt Macy 		}
913eda14cbcSMatt Macy 		(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
914eda14cbcSMatt Macy 		    (void *)&zp->z_size, sizeof (uint64_t), tx);
915eda14cbcSMatt Macy 
916eda14cbcSMatt Macy 		/* Ensure the replayed seq is updated */
917eda14cbcSMatt Macy 		(void) zil_replaying(zfsvfs->z_log, tx);
918eda14cbcSMatt Macy 
919eda14cbcSMatt Macy 		dmu_tx_commit(tx);
920eda14cbcSMatt Macy 	}
921eda14cbcSMatt Macy 
922eda14cbcSMatt Macy 	zrele(zp);
923eda14cbcSMatt Macy 
924eda14cbcSMatt Macy 	return (error);
925eda14cbcSMatt Macy }
926eda14cbcSMatt Macy 
927eda14cbcSMatt Macy static int
zfs_replay_truncate(void * arg1,void * arg2,boolean_t byteswap)928eda14cbcSMatt Macy zfs_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
929eda14cbcSMatt Macy {
930eda14cbcSMatt Macy 	zfsvfs_t *zfsvfs = arg1;
931eda14cbcSMatt Macy 	lr_truncate_t *lr = arg2;
932eda14cbcSMatt Macy 	znode_t *zp;
933da5137abSMartin Matuska 	flock64_t fl = {0};
934eda14cbcSMatt Macy 	int error;
935eda14cbcSMatt Macy 
936525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
937525fe93dSMartin Matuska 
938eda14cbcSMatt Macy 	if (byteswap)
939eda14cbcSMatt Macy 		byteswap_uint64_array(lr, sizeof (*lr));
940eda14cbcSMatt Macy 
941eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
942eda14cbcSMatt Macy 		return (error);
943eda14cbcSMatt Macy 
944eda14cbcSMatt Macy 	fl.l_type = F_WRLCK;
945eda14cbcSMatt Macy 	fl.l_whence = SEEK_SET;
946eda14cbcSMatt Macy 	fl.l_start = lr->lr_offset;
947eda14cbcSMatt Macy 	fl.l_len = lr->lr_length;
948eda14cbcSMatt Macy 
949eda14cbcSMatt Macy 	error = zfs_space(zp, F_FREESP, &fl, O_RDWR | O_LARGEFILE,
950eda14cbcSMatt Macy 	    lr->lr_offset, kcred);
951eda14cbcSMatt Macy 
952eda14cbcSMatt Macy 	zrele(zp);
953eda14cbcSMatt Macy 
954eda14cbcSMatt Macy 	return (error);
955eda14cbcSMatt Macy }
956eda14cbcSMatt Macy 
957eda14cbcSMatt Macy static int
zfs_replay_setattr(void * arg1,void * arg2,boolean_t byteswap)958eda14cbcSMatt Macy zfs_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
959eda14cbcSMatt Macy {
960eda14cbcSMatt Macy 	zfsvfs_t *zfsvfs = arg1;
961eda14cbcSMatt Macy 	lr_setattr_t *lr = arg2;
962eda14cbcSMatt Macy 	znode_t *zp;
963eda14cbcSMatt Macy 	xvattr_t xva;
964eda14cbcSMatt Macy 	vattr_t *vap = &xva.xva_vattr;
965eda14cbcSMatt Macy 	int error;
966eda14cbcSMatt Macy 	void *start;
967eda14cbcSMatt Macy 
968525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
969525fe93dSMartin Matuska 
970eda14cbcSMatt Macy 	xva_init(&xva);
971eda14cbcSMatt Macy 	if (byteswap) {
972eda14cbcSMatt Macy 		byteswap_uint64_array(lr, sizeof (*lr));
973eda14cbcSMatt Macy 
974eda14cbcSMatt Macy 		if ((lr->lr_mask & ATTR_XVATTR) &&
975eda14cbcSMatt Macy 		    zfsvfs->z_version >= ZPL_VERSION_INITIAL)
976*7a7741afSMartin Matuska 			zfs_replay_swap_attrs((lr_attr_t *)&lr->lr_data[0]);
977eda14cbcSMatt Macy 	}
978eda14cbcSMatt Macy 
979eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
980eda14cbcSMatt Macy 		return (error);
981eda14cbcSMatt Macy 
982eda14cbcSMatt Macy 	zfs_init_vattr(vap, lr->lr_mask, lr->lr_mode,
983eda14cbcSMatt Macy 	    lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
984eda14cbcSMatt Macy 
985eda14cbcSMatt Macy 	vap->va_size = lr->lr_size;
986eda14cbcSMatt Macy 	ZFS_TIME_DECODE(&vap->va_atime, lr->lr_atime);
987eda14cbcSMatt Macy 	ZFS_TIME_DECODE(&vap->va_mtime, lr->lr_mtime);
988eda14cbcSMatt Macy 	gethrestime(&vap->va_ctime);
989eda14cbcSMatt Macy 	vap->va_mask |= ATTR_CTIME;
990eda14cbcSMatt Macy 
991eda14cbcSMatt Macy 	/*
992eda14cbcSMatt Macy 	 * Fill in xvattr_t portions if necessary.
993eda14cbcSMatt Macy 	 */
994eda14cbcSMatt Macy 
995*7a7741afSMartin Matuska 	start = (void *)&lr->lr_data[0];
996eda14cbcSMatt Macy 	if (vap->va_mask & ATTR_XVATTR) {
997eda14cbcSMatt Macy 		zfs_replay_xvattr((lr_attr_t *)start, &xva);
998*7a7741afSMartin Matuska 		start = &lr->lr_data[
999*7a7741afSMartin Matuska 		    ZIL_XVAT_SIZE(((lr_attr_t *)start)->lr_attr_masksize)];
1000eda14cbcSMatt Macy 	} else
1001eda14cbcSMatt Macy 		xva.xva_vattr.va_mask &= ~ATTR_XVATTR;
1002eda14cbcSMatt Macy 
1003eda14cbcSMatt Macy 	zfsvfs->z_fuid_replay = zfs_replay_fuid_domain(start, &start,
1004eda14cbcSMatt Macy 	    lr->lr_uid, lr->lr_gid);
1005eda14cbcSMatt Macy 
1006dbd5678dSMartin Matuska #if defined(__linux__)
1007d411c1d6SMartin Matuska 	error = zfs_setattr(zp, vap, 0, kcred, zfs_init_idmap);
1008dbd5678dSMartin Matuska #else
1009dbd5678dSMartin Matuska 	error = zfs_setattr(zp, vap, 0, kcred, NULL);
1010dbd5678dSMartin Matuska #endif
1011eda14cbcSMatt Macy 
1012eda14cbcSMatt Macy 	zfs_fuid_info_free(zfsvfs->z_fuid_replay);
1013eda14cbcSMatt Macy 	zfsvfs->z_fuid_replay = NULL;
1014eda14cbcSMatt Macy 	zrele(zp);
1015eda14cbcSMatt Macy 
1016eda14cbcSMatt Macy 	return (error);
1017eda14cbcSMatt Macy }
1018eda14cbcSMatt Macy 
1019eda14cbcSMatt Macy static int
zfs_replay_setsaxattr(void * arg1,void * arg2,boolean_t byteswap)1020c03c5b1cSMartin Matuska zfs_replay_setsaxattr(void *arg1, void *arg2, boolean_t byteswap)
1021c03c5b1cSMartin Matuska {
1022c03c5b1cSMartin Matuska 	zfsvfs_t *zfsvfs = arg1;
1023c03c5b1cSMartin Matuska 	lr_setsaxattr_t *lr = arg2;
1024c03c5b1cSMartin Matuska 	znode_t *zp;
1025c03c5b1cSMartin Matuska 	nvlist_t *nvl;
1026c03c5b1cSMartin Matuska 	size_t sa_size;
1027c03c5b1cSMartin Matuska 	char *name;
1028c03c5b1cSMartin Matuska 	char *value;
1029c03c5b1cSMartin Matuska 	size_t size;
1030c03c5b1cSMartin Matuska 	int error = 0;
1031c03c5b1cSMartin Matuska 
1032525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
1033525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr) + lr->lr_size);
1034525fe93dSMartin Matuska 
1035c03c5b1cSMartin Matuska 	ASSERT(spa_feature_is_active(zfsvfs->z_os->os_spa,
1036c03c5b1cSMartin Matuska 	    SPA_FEATURE_ZILSAXATTR));
1037c03c5b1cSMartin Matuska 	if (byteswap)
1038c03c5b1cSMartin Matuska 		byteswap_uint64_array(lr, sizeof (*lr));
1039c03c5b1cSMartin Matuska 
1040c03c5b1cSMartin Matuska 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1041c03c5b1cSMartin Matuska 		return (error);
1042c03c5b1cSMartin Matuska 
1043c03c5b1cSMartin Matuska 	rw_enter(&zp->z_xattr_lock, RW_WRITER);
1044c03c5b1cSMartin Matuska 	mutex_enter(&zp->z_lock);
1045c03c5b1cSMartin Matuska 	if (zp->z_xattr_cached == NULL)
1046c03c5b1cSMartin Matuska 		error = zfs_sa_get_xattr(zp);
1047c03c5b1cSMartin Matuska 	mutex_exit(&zp->z_lock);
1048c03c5b1cSMartin Matuska 
1049c03c5b1cSMartin Matuska 	if (error)
1050c03c5b1cSMartin Matuska 		goto out;
1051c03c5b1cSMartin Matuska 
1052c03c5b1cSMartin Matuska 	ASSERT(zp->z_xattr_cached);
1053c03c5b1cSMartin Matuska 	nvl = zp->z_xattr_cached;
1054c03c5b1cSMartin Matuska 
1055c03c5b1cSMartin Matuska 	/* Get xattr name, value and size from log record */
1056c03c5b1cSMartin Matuska 	size = lr->lr_size;
1057*7a7741afSMartin Matuska 	name = (char *)&lr->lr_data[0];
1058c03c5b1cSMartin Matuska 	if (size == 0) {
1059c03c5b1cSMartin Matuska 		value = NULL;
1060c03c5b1cSMartin Matuska 		error = nvlist_remove(nvl, name, DATA_TYPE_BYTE_ARRAY);
1061c03c5b1cSMartin Matuska 	} else {
1062*7a7741afSMartin Matuska 		value = &lr->lr_data[strlen(name) + 1];
1063c03c5b1cSMartin Matuska 		/* Limited to 32k to keep nvpair memory allocations small */
1064c03c5b1cSMartin Matuska 		if (size > DXATTR_MAX_ENTRY_SIZE) {
1065c03c5b1cSMartin Matuska 			error = SET_ERROR(EFBIG);
1066c03c5b1cSMartin Matuska 			goto out;
1067c03c5b1cSMartin Matuska 		}
1068c03c5b1cSMartin Matuska 
1069c03c5b1cSMartin Matuska 		/* Prevent the DXATTR SA from consuming the entire SA region */
1070c03c5b1cSMartin Matuska 		error = nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
1071c03c5b1cSMartin Matuska 		if (error)
1072c03c5b1cSMartin Matuska 			goto out;
1073c03c5b1cSMartin Matuska 
1074c03c5b1cSMartin Matuska 		if (sa_size > DXATTR_MAX_SA_SIZE) {
1075c03c5b1cSMartin Matuska 			error = SET_ERROR(EFBIG);
1076c03c5b1cSMartin Matuska 			goto out;
1077c03c5b1cSMartin Matuska 		}
1078c03c5b1cSMartin Matuska 
1079c03c5b1cSMartin Matuska 		error = nvlist_add_byte_array(nvl, name, (uchar_t *)value,
1080c03c5b1cSMartin Matuska 		    size);
1081c03c5b1cSMartin Matuska 	}
1082c03c5b1cSMartin Matuska 
1083c03c5b1cSMartin Matuska 	/*
1084c03c5b1cSMartin Matuska 	 * Update the SA for additions, modifications, and removals. On
1085c03c5b1cSMartin Matuska 	 * error drop the inconsistent cached version of the nvlist, it
1086c03c5b1cSMartin Matuska 	 * will be reconstructed from the ARC when next accessed.
1087c03c5b1cSMartin Matuska 	 */
1088c03c5b1cSMartin Matuska 	if (error == 0)
1089c03c5b1cSMartin Matuska 		error = zfs_sa_set_xattr(zp, name, value, size);
1090c03c5b1cSMartin Matuska 
1091c03c5b1cSMartin Matuska 	if (error) {
1092c03c5b1cSMartin Matuska 		nvlist_free(nvl);
1093c03c5b1cSMartin Matuska 		zp->z_xattr_cached = NULL;
1094c03c5b1cSMartin Matuska 	}
1095c03c5b1cSMartin Matuska 
1096c03c5b1cSMartin Matuska out:
1097c03c5b1cSMartin Matuska 	rw_exit(&zp->z_xattr_lock);
1098c03c5b1cSMartin Matuska 	zrele(zp);
1099c03c5b1cSMartin Matuska 	return (error);
1100c03c5b1cSMartin Matuska }
1101c03c5b1cSMartin Matuska 
1102c03c5b1cSMartin Matuska static int
zfs_replay_acl_v0(void * arg1,void * arg2,boolean_t byteswap)1103eda14cbcSMatt Macy zfs_replay_acl_v0(void *arg1, void *arg2, boolean_t byteswap)
1104eda14cbcSMatt Macy {
1105eda14cbcSMatt Macy 	zfsvfs_t *zfsvfs = arg1;
1106eda14cbcSMatt Macy 	lr_acl_v0_t *lr = arg2;
1107*7a7741afSMartin Matuska 	ace_t *ace = (ace_t *)&lr->lr_data[0];
1108da5137abSMartin Matuska 	vsecattr_t vsa = {0};
1109eda14cbcSMatt Macy 	znode_t *zp;
1110eda14cbcSMatt Macy 	int error;
1111eda14cbcSMatt Macy 
1112525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
1113525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr) +
1114525fe93dSMartin Matuska 	    sizeof (ace_t) * lr->lr_aclcnt);
1115525fe93dSMartin Matuska 
1116eda14cbcSMatt Macy 	if (byteswap) {
1117eda14cbcSMatt Macy 		byteswap_uint64_array(lr, sizeof (*lr));
1118eda14cbcSMatt Macy 		zfs_oldace_byteswap(ace, lr->lr_aclcnt);
1119eda14cbcSMatt Macy 	}
1120eda14cbcSMatt Macy 
1121eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1122eda14cbcSMatt Macy 		return (error);
1123eda14cbcSMatt Macy 
1124eda14cbcSMatt Macy 	vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
1125eda14cbcSMatt Macy 	vsa.vsa_aclcnt = lr->lr_aclcnt;
1126eda14cbcSMatt Macy 	vsa.vsa_aclentsz = sizeof (ace_t) * vsa.vsa_aclcnt;
1127eda14cbcSMatt Macy 	vsa.vsa_aclflags = 0;
1128eda14cbcSMatt Macy 	vsa.vsa_aclentp = ace;
1129eda14cbcSMatt Macy 
1130eda14cbcSMatt Macy 	error = zfs_setsecattr(zp, &vsa, 0, kcred);
1131eda14cbcSMatt Macy 
1132eda14cbcSMatt Macy 	zrele(zp);
1133eda14cbcSMatt Macy 
1134eda14cbcSMatt Macy 	return (error);
1135eda14cbcSMatt Macy }
1136eda14cbcSMatt Macy 
1137eda14cbcSMatt Macy /*
1138eda14cbcSMatt Macy  * Replaying ACLs is complicated by FUID support.
1139eda14cbcSMatt Macy  * The log record may contain some optional data
1140eda14cbcSMatt Macy  * to be used for replaying FUID's.  These pieces
1141eda14cbcSMatt Macy  * are the actual FUIDs that were created initially.
1142eda14cbcSMatt Macy  * The FUID table index may no longer be valid and
1143eda14cbcSMatt Macy  * during zfs_create() a new index may be assigned.
1144eda14cbcSMatt Macy  * Because of this the log will contain the original
1145eda14cbcSMatt Macy  * domain+rid in order to create a new FUID.
1146eda14cbcSMatt Macy  *
1147eda14cbcSMatt Macy  * The individual ACEs may contain an ephemeral uid/gid which is no
1148eda14cbcSMatt Macy  * longer valid and will need to be replaced with an actual FUID.
1149eda14cbcSMatt Macy  *
1150eda14cbcSMatt Macy  */
1151eda14cbcSMatt Macy static int
zfs_replay_acl(void * arg1,void * arg2,boolean_t byteswap)1152eda14cbcSMatt Macy zfs_replay_acl(void *arg1, void *arg2, boolean_t byteswap)
1153eda14cbcSMatt Macy {
1154eda14cbcSMatt Macy 	zfsvfs_t *zfsvfs = arg1;
1155eda14cbcSMatt Macy 	lr_acl_t *lr = arg2;
1156*7a7741afSMartin Matuska 	ace_t *ace = (ace_t *)&lr->lr_data[0];
1157da5137abSMartin Matuska 	vsecattr_t vsa = {0};
1158eda14cbcSMatt Macy 	znode_t *zp;
1159eda14cbcSMatt Macy 	int error;
1160eda14cbcSMatt Macy 
1161525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
1162525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr) + lr->lr_acl_bytes);
1163525fe93dSMartin Matuska 
1164eda14cbcSMatt Macy 	if (byteswap) {
1165eda14cbcSMatt Macy 		byteswap_uint64_array(lr, sizeof (*lr));
1166eda14cbcSMatt Macy 		zfs_ace_byteswap(ace, lr->lr_acl_bytes, B_FALSE);
1167eda14cbcSMatt Macy 		if (lr->lr_fuidcnt) {
1168*7a7741afSMartin Matuska 			byteswap_uint64_array(&lr->lr_data[
1169*7a7741afSMartin Matuska 			    ZIL_ACE_LENGTH(lr->lr_acl_bytes)],
1170eda14cbcSMatt Macy 			    lr->lr_fuidcnt * sizeof (uint64_t));
1171eda14cbcSMatt Macy 		}
1172eda14cbcSMatt Macy 	}
1173eda14cbcSMatt Macy 
1174eda14cbcSMatt Macy 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1175eda14cbcSMatt Macy 		return (error);
1176eda14cbcSMatt Macy 
1177eda14cbcSMatt Macy 	vsa.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1178eda14cbcSMatt Macy 	vsa.vsa_aclcnt = lr->lr_aclcnt;
1179eda14cbcSMatt Macy 	vsa.vsa_aclentp = ace;
1180eda14cbcSMatt Macy 	vsa.vsa_aclentsz = lr->lr_acl_bytes;
1181eda14cbcSMatt Macy 	vsa.vsa_aclflags = lr->lr_acl_flags;
1182eda14cbcSMatt Macy 
1183eda14cbcSMatt Macy 	if (lr->lr_fuidcnt) {
1184*7a7741afSMartin Matuska 		void *fuidstart = &lr->lr_data[
1185*7a7741afSMartin Matuska 		    ZIL_ACE_LENGTH(lr->lr_acl_bytes)];
1186eda14cbcSMatt Macy 
1187eda14cbcSMatt Macy 		zfsvfs->z_fuid_replay =
1188eda14cbcSMatt Macy 		    zfs_replay_fuids(fuidstart, &fuidstart,
1189eda14cbcSMatt Macy 		    lr->lr_fuidcnt, lr->lr_domcnt, 0, 0);
1190eda14cbcSMatt Macy 	}
1191eda14cbcSMatt Macy 
1192eda14cbcSMatt Macy 	error = zfs_setsecattr(zp, &vsa, 0, kcred);
1193eda14cbcSMatt Macy 
1194eda14cbcSMatt Macy 	if (zfsvfs->z_fuid_replay)
1195eda14cbcSMatt Macy 		zfs_fuid_info_free(zfsvfs->z_fuid_replay);
1196eda14cbcSMatt Macy 
1197eda14cbcSMatt Macy 	zfsvfs->z_fuid_replay = NULL;
1198eda14cbcSMatt Macy 	zrele(zp);
1199eda14cbcSMatt Macy 
1200eda14cbcSMatt Macy 	return (error);
1201eda14cbcSMatt Macy }
1202eda14cbcSMatt Macy 
12032a58b312SMartin Matuska static int
zfs_replay_clone_range(void * arg1,void * arg2,boolean_t byteswap)12042a58b312SMartin Matuska zfs_replay_clone_range(void *arg1, void *arg2, boolean_t byteswap)
12052a58b312SMartin Matuska {
12062a58b312SMartin Matuska 	zfsvfs_t *zfsvfs = arg1;
12072a58b312SMartin Matuska 	lr_clone_range_t *lr = arg2;
12082a58b312SMartin Matuska 	znode_t *zp;
12092a58b312SMartin Matuska 	int error;
12102a58b312SMartin Matuska 
1211525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
1212525fe93dSMartin Matuska 	ASSERT3U(lr->lr_common.lrc_reclen, >=, offsetof(lr_clone_range_t,
1213525fe93dSMartin Matuska 	    lr_bps[lr->lr_nbps]));
1214525fe93dSMartin Matuska 
12152a58b312SMartin Matuska 	if (byteswap)
12162a58b312SMartin Matuska 		byteswap_uint64_array(lr, sizeof (*lr));
12172a58b312SMartin Matuska 
12182a58b312SMartin Matuska 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
12192a58b312SMartin Matuska 		/*
12202a58b312SMartin Matuska 		 * Clones can be logged out of order, so don't be surprised if
12212a58b312SMartin Matuska 		 * the file is gone - just return success.
12222a58b312SMartin Matuska 		 */
12232a58b312SMartin Matuska 		if (error == ENOENT)
12242a58b312SMartin Matuska 			error = 0;
12252a58b312SMartin Matuska 		return (error);
12262a58b312SMartin Matuska 	}
12272a58b312SMartin Matuska 
12282a58b312SMartin Matuska 	error = zfs_clone_range_replay(zp, lr->lr_offset, lr->lr_length,
12292a58b312SMartin Matuska 	    lr->lr_blksz, lr->lr_bps, lr->lr_nbps);
12302a58b312SMartin Matuska 
12312a58b312SMartin Matuska 	zrele(zp);
12322a58b312SMartin Matuska 	return (error);
12332a58b312SMartin Matuska }
12342a58b312SMartin Matuska 
1235eda14cbcSMatt Macy /*
1236eda14cbcSMatt Macy  * Callback vectors for replaying records
1237eda14cbcSMatt Macy  */
1238e92ffd9bSMartin Matuska zil_replay_func_t *const zfs_replay_vector[TX_MAX_TYPE] = {
1239eda14cbcSMatt Macy 	zfs_replay_error,	/* no such type */
1240eda14cbcSMatt Macy 	zfs_replay_create,	/* TX_CREATE */
1241eda14cbcSMatt Macy 	zfs_replay_create,	/* TX_MKDIR */
1242eda14cbcSMatt Macy 	zfs_replay_create,	/* TX_MKXATTR */
1243eda14cbcSMatt Macy 	zfs_replay_create,	/* TX_SYMLINK */
1244eda14cbcSMatt Macy 	zfs_replay_remove,	/* TX_REMOVE */
1245eda14cbcSMatt Macy 	zfs_replay_remove,	/* TX_RMDIR */
1246eda14cbcSMatt Macy 	zfs_replay_link,	/* TX_LINK */
1247eda14cbcSMatt Macy 	zfs_replay_rename,	/* TX_RENAME */
1248eda14cbcSMatt Macy 	zfs_replay_write,	/* TX_WRITE */
1249eda14cbcSMatt Macy 	zfs_replay_truncate,	/* TX_TRUNCATE */
1250eda14cbcSMatt Macy 	zfs_replay_setattr,	/* TX_SETATTR */
1251eda14cbcSMatt Macy 	zfs_replay_acl_v0,	/* TX_ACL_V0 */
1252eda14cbcSMatt Macy 	zfs_replay_acl,		/* TX_ACL */
1253eda14cbcSMatt Macy 	zfs_replay_create_acl,	/* TX_CREATE_ACL */
1254eda14cbcSMatt Macy 	zfs_replay_create,	/* TX_CREATE_ATTR */
1255eda14cbcSMatt Macy 	zfs_replay_create_acl,	/* TX_CREATE_ACL_ATTR */
1256eda14cbcSMatt Macy 	zfs_replay_create_acl,	/* TX_MKDIR_ACL */
1257eda14cbcSMatt Macy 	zfs_replay_create,	/* TX_MKDIR_ATTR */
1258eda14cbcSMatt Macy 	zfs_replay_create_acl,	/* TX_MKDIR_ACL_ATTR */
1259eda14cbcSMatt Macy 	zfs_replay_write2,	/* TX_WRITE2 */
1260c03c5b1cSMartin Matuska 	zfs_replay_setsaxattr,	/* TX_SETSAXATTR */
1261dbd5678dSMartin Matuska 	zfs_replay_rename_exchange,	/* TX_RENAME_EXCHANGE */
1262dbd5678dSMartin Matuska 	zfs_replay_rename_whiteout,	/* TX_RENAME_WHITEOUT */
12632a58b312SMartin Matuska 	zfs_replay_clone_range,	/* TX_CLONE_RANGE */
1264eda14cbcSMatt Macy };
1265