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