xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_replay.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lracl));
313 
314 	txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
315 	if (byteswap) {
316 		byteswap_uint64_array(lracl, sizeof (*lracl));
317 		if (txtype == TX_CREATE_ACL_ATTR ||
318 		    txtype == TX_MKDIR_ACL_ATTR) {
319 			lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
320 			zfs_replay_swap_attrs(lrattr);
321 			xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
322 		}
323 
324 		aclstart = (caddr_t)(lracl + 1) + xvatlen;
325 		zfs_ace_byteswap(aclstart, lracl->lr_acl_bytes, B_FALSE);
326 		/* swap fuids */
327 		if (lracl->lr_fuidcnt) {
328 			byteswap_uint64_array((caddr_t)aclstart +
329 			    ZIL_ACE_LENGTH(lracl->lr_acl_bytes),
330 			    lracl->lr_fuidcnt * sizeof (uint64_t));
331 		}
332 	}
333 
334 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
335 		return (error);
336 
337 	objid = LR_FOID_GET_OBJ(lr->lr_foid);
338 	dnodesize = LR_FOID_GET_SLOTS(lr->lr_foid) << DNODE_SHIFT;
339 
340 	xva_init(&xva);
341 	zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
342 	    lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, objid);
343 
344 	/*
345 	 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
346 	 * eventually end up in zfs_mknode(), which assigns the object's
347 	 * creation time, generation number, and dnode size. The generic
348 	 * zfs_create() has no concept of these attributes, so we smuggle
349 	 * the values inside the vattr's otherwise unused va_ctime,
350 	 * va_nblocks, and va_fsid fields.
351 	 */
352 	ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
353 	xva.xva_vattr.va_nblocks = lr->lr_gen;
354 	xva.xva_vattr.va_fsid = dnodesize;
355 
356 	error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT);
357 	if (error)
358 		goto bail;
359 
360 	if (lr->lr_common.lrc_txtype & TX_CI)
361 		vflg |= FIGNORECASE;
362 	switch (txtype) {
363 	case TX_CREATE_ACL:
364 		aclstart = (caddr_t)(lracl + 1);
365 		fuidstart = (caddr_t)aclstart +
366 		    ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
367 		zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
368 		    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
369 		    lr->lr_uid, lr->lr_gid);
370 		zfs_fallthrough;
371 	case TX_CREATE_ACL_ATTR:
372 		if (name == NULL) {
373 			lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
374 			xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
375 			xva.xva_vattr.va_mask |= ATTR_XVATTR;
376 			zfs_replay_xvattr(lrattr, &xva);
377 		}
378 		vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
379 		vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
380 		vsec.vsa_aclcnt = lracl->lr_aclcnt;
381 		vsec.vsa_aclentsz = lracl->lr_acl_bytes;
382 		vsec.vsa_aclflags = lracl->lr_acl_flags;
383 		if (zfsvfs->z_fuid_replay == NULL) {
384 			fuidstart = (caddr_t)(lracl + 1) + xvatlen +
385 			    ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
386 			zfsvfs->z_fuid_replay =
387 			    zfs_replay_fuids(fuidstart,
388 			    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
389 			    lr->lr_uid, lr->lr_gid);
390 		}
391 
392 #if defined(__linux__)
393 		error = zfs_create(dzp, name, &xva.xva_vattr,
394 		    0, 0, &zp, kcred, vflg, &vsec, zfs_init_idmap);
395 #else
396 		error = zfs_create(dzp, name, &xva.xva_vattr,
397 		    0, 0, &zp, kcred, vflg, &vsec, NULL);
398 #endif
399 		break;
400 	case TX_MKDIR_ACL:
401 		aclstart = (caddr_t)(lracl + 1);
402 		fuidstart = (caddr_t)aclstart +
403 		    ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
404 		zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
405 		    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
406 		    lr->lr_uid, lr->lr_gid);
407 		zfs_fallthrough;
408 	case TX_MKDIR_ACL_ATTR:
409 		if (name == NULL) {
410 			lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
411 			xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
412 			zfs_replay_xvattr(lrattr, &xva);
413 		}
414 		vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
415 		vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
416 		vsec.vsa_aclcnt = lracl->lr_aclcnt;
417 		vsec.vsa_aclentsz = lracl->lr_acl_bytes;
418 		vsec.vsa_aclflags = lracl->lr_acl_flags;
419 		if (zfsvfs->z_fuid_replay == NULL) {
420 			fuidstart = (caddr_t)(lracl + 1) + xvatlen +
421 			    ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
422 			zfsvfs->z_fuid_replay =
423 			    zfs_replay_fuids(fuidstart,
424 			    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
425 			    lr->lr_uid, lr->lr_gid);
426 		}
427 #if defined(__linux__)
428 		error = zfs_mkdir(dzp, name, &xva.xva_vattr,
429 		    &zp, kcred, vflg, &vsec, zfs_init_idmap);
430 #else
431 		error = zfs_mkdir(dzp, name, &xva.xva_vattr,
432 		    &zp, kcred, vflg, &vsec, NULL);
433 #endif
434 		break;
435 	default:
436 		error = SET_ERROR(ENOTSUP);
437 	}
438 
439 bail:
440 	if (error == 0 && zp != NULL) {
441 #ifdef __FreeBSD__
442 		VOP_UNLOCK1(ZTOV(zp));
443 #endif
444 		zrele(zp);
445 	}
446 	zrele(dzp);
447 
448 	if (zfsvfs->z_fuid_replay)
449 		zfs_fuid_info_free(zfsvfs->z_fuid_replay);
450 	zfsvfs->z_fuid_replay = NULL;
451 
452 	return (error);
453 }
454 
455 static int
456 zfs_replay_create(void *arg1, void *arg2, boolean_t byteswap)
457 {
458 	zfsvfs_t *zfsvfs = arg1;
459 	lr_create_t *lr = arg2;
460 	char *name = NULL;		/* location determined later */
461 	char *link;			/* symlink content follows name */
462 	znode_t *dzp;
463 	znode_t *zp = NULL;
464 	xvattr_t xva;
465 	int vflg = 0;
466 	size_t lrsize = sizeof (lr_create_t);
467 	lr_attr_t *lrattr;
468 	void *start;
469 	size_t xvatlen;
470 	uint64_t txtype;
471 	uint64_t objid;
472 	uint64_t dnodesize;
473 	int error;
474 
475 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
476 
477 	txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
478 	if (byteswap) {
479 		byteswap_uint64_array(lr, sizeof (*lr));
480 		if (txtype == TX_CREATE_ATTR || txtype == TX_MKDIR_ATTR)
481 			zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
482 	}
483 
484 
485 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
486 		return (error);
487 
488 	objid = LR_FOID_GET_OBJ(lr->lr_foid);
489 	dnodesize = LR_FOID_GET_SLOTS(lr->lr_foid) << DNODE_SHIFT;
490 
491 	xva_init(&xva);
492 	zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
493 	    lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, objid);
494 
495 	/*
496 	 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
497 	 * eventually end up in zfs_mknode(), which assigns the object's
498 	 * creation time, generation number, and dnode slot count. The
499 	 * generic zfs_create() has no concept of these attributes, so
500 	 * we smuggle the values inside the vattr's otherwise unused
501 	 * va_ctime, va_nblocks, and va_fsid fields.
502 	 */
503 	ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
504 	xva.xva_vattr.va_nblocks = lr->lr_gen;
505 	xva.xva_vattr.va_fsid = dnodesize;
506 
507 	error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT);
508 	if (error)
509 		goto out;
510 
511 	if (lr->lr_common.lrc_txtype & TX_CI)
512 		vflg |= FIGNORECASE;
513 
514 	/*
515 	 * Symlinks don't have fuid info, and CIFS never creates
516 	 * symlinks.
517 	 *
518 	 * The _ATTR versions will grab the fuid info in their subcases.
519 	 */
520 	if (txtype != TX_SYMLINK &&
521 	    txtype != TX_MKDIR_ATTR &&
522 	    txtype != TX_CREATE_ATTR) {
523 		start = (lr + 1);
524 		zfsvfs->z_fuid_replay =
525 		    zfs_replay_fuid_domain(start, &start,
526 		    lr->lr_uid, lr->lr_gid);
527 	}
528 
529 	switch (txtype) {
530 	case TX_CREATE_ATTR:
531 		lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
532 		xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
533 		zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
534 		start = (caddr_t)(lr + 1) + xvatlen;
535 		zfsvfs->z_fuid_replay =
536 		    zfs_replay_fuid_domain(start, &start,
537 		    lr->lr_uid, lr->lr_gid);
538 		name = (char *)start;
539 		zfs_fallthrough;
540 
541 	case TX_CREATE:
542 		if (name == NULL)
543 			name = (char *)start;
544 
545 #if defined(__linux__)
546 		error = zfs_create(dzp, name, &xva.xva_vattr,
547 		    0, 0, &zp, kcred, vflg, NULL, zfs_init_idmap);
548 #else
549 		error = zfs_create(dzp, name, &xva.xva_vattr,
550 		    0, 0, &zp, kcred, vflg, NULL, NULL);
551 #endif
552 		break;
553 	case TX_MKDIR_ATTR:
554 		lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
555 		xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
556 		zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
557 		start = (caddr_t)(lr + 1) + xvatlen;
558 		zfsvfs->z_fuid_replay =
559 		    zfs_replay_fuid_domain(start, &start,
560 		    lr->lr_uid, lr->lr_gid);
561 		name = (char *)start;
562 		zfs_fallthrough;
563 
564 	case TX_MKDIR:
565 		if (name == NULL)
566 			name = (char *)(lr + 1);
567 
568 #if defined(__linux__)
569 		error = zfs_mkdir(dzp, name, &xva.xva_vattr,
570 		    &zp, kcred, vflg, NULL, zfs_init_idmap);
571 #else
572 		error = zfs_mkdir(dzp, name, &xva.xva_vattr,
573 		    &zp, kcred, vflg, NULL, NULL);
574 #endif
575 
576 		break;
577 	case TX_MKXATTR:
578 		error = zfs_make_xattrdir(dzp, &xva.xva_vattr, &zp, kcred);
579 		break;
580 	case TX_SYMLINK:
581 		name = (char *)(lr + 1);
582 		link = name + strlen(name) + 1;
583 #if defined(__linux__)
584 		error = zfs_symlink(dzp, name, &xva.xva_vattr,
585 		    link, &zp, kcred, vflg, zfs_init_idmap);
586 #else
587 		error = zfs_symlink(dzp, name, &xva.xva_vattr,
588 		    link, &zp, kcred, vflg, NULL);
589 #endif
590 		break;
591 	default:
592 		error = SET_ERROR(ENOTSUP);
593 	}
594 
595 out:
596 	if (error == 0 && zp != NULL) {
597 #ifdef __FreeBSD__
598 		VOP_UNLOCK1(ZTOV(zp));
599 #endif
600 		zrele(zp);
601 	}
602 	zrele(dzp);
603 
604 	if (zfsvfs->z_fuid_replay)
605 		zfs_fuid_info_free(zfsvfs->z_fuid_replay);
606 	zfsvfs->z_fuid_replay = NULL;
607 	return (error);
608 }
609 
610 static int
611 zfs_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
612 {
613 	zfsvfs_t *zfsvfs = arg1;
614 	lr_remove_t *lr = arg2;
615 	char *name = (char *)(lr + 1);	/* name follows lr_remove_t */
616 	znode_t *dzp;
617 	int error;
618 	int vflg = 0;
619 
620 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
621 
622 	if (byteswap)
623 		byteswap_uint64_array(lr, sizeof (*lr));
624 
625 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
626 		return (error);
627 
628 	if (lr->lr_common.lrc_txtype & TX_CI)
629 		vflg |= FIGNORECASE;
630 
631 	switch ((int)lr->lr_common.lrc_txtype) {
632 	case TX_REMOVE:
633 		error = zfs_remove(dzp, name, kcred, vflg);
634 		break;
635 	case TX_RMDIR:
636 		error = zfs_rmdir(dzp, name, NULL, kcred, vflg);
637 		break;
638 	default:
639 		error = SET_ERROR(ENOTSUP);
640 	}
641 
642 	zrele(dzp);
643 
644 	return (error);
645 }
646 
647 static int
648 zfs_replay_link(void *arg1, void *arg2, boolean_t byteswap)
649 {
650 	zfsvfs_t *zfsvfs = arg1;
651 	lr_link_t *lr = arg2;
652 	char *name = (char *)(lr + 1);	/* name follows lr_link_t */
653 	znode_t *dzp, *zp;
654 	int error;
655 	int vflg = 0;
656 
657 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
658 
659 	if (byteswap)
660 		byteswap_uint64_array(lr, sizeof (*lr));
661 
662 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
663 		return (error);
664 
665 	if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
666 		zrele(dzp);
667 		return (error);
668 	}
669 
670 	if (lr->lr_common.lrc_txtype & TX_CI)
671 		vflg |= FIGNORECASE;
672 
673 	error = zfs_link(dzp, zp, name, kcred, vflg);
674 	zrele(zp);
675 	zrele(dzp);
676 
677 	return (error);
678 }
679 
680 static int
681 do_zfs_replay_rename(zfsvfs_t *zfsvfs, lr_rename_t *lr, char *sname,
682     char *tname, uint64_t rflags, vattr_t *wo_vap)
683 {
684 	znode_t *sdzp, *tdzp;
685 	int error, vflg = 0;
686 
687 	/* Only Linux currently supports RENAME_* flags. */
688 #ifdef __linux__
689 	VERIFY0(rflags & ~(RENAME_EXCHANGE | RENAME_WHITEOUT));
690 
691 	/* wo_vap must be non-NULL iff. we're doing RENAME_WHITEOUT */
692 	VERIFY_EQUIV(rflags & RENAME_WHITEOUT, wo_vap != NULL);
693 #else
694 	VERIFY0(rflags);
695 #endif
696 
697 	if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
698 		return (error);
699 
700 	if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
701 		zrele(sdzp);
702 		return (error);
703 	}
704 
705 	if (lr->lr_common.lrc_txtype & TX_CI)
706 		vflg |= FIGNORECASE;
707 
708 #if defined(__linux__)
709 	error = zfs_rename(sdzp, sname, tdzp, tname, kcred, vflg, rflags,
710 	    wo_vap, zfs_init_idmap);
711 #else
712 	error = zfs_rename(sdzp, sname, tdzp, tname, kcred, vflg, rflags,
713 	    wo_vap, NULL);
714 #endif
715 
716 	zrele(tdzp);
717 	zrele(sdzp);
718 	return (error);
719 }
720 
721 static int
722 zfs_replay_rename(void *arg1, void *arg2, boolean_t byteswap)
723 {
724 	zfsvfs_t *zfsvfs = arg1;
725 	lr_rename_t *lr = arg2;
726 
727 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
728 
729 	if (byteswap)
730 		byteswap_uint64_array(lr, sizeof (*lr));
731 
732 	char *sname = (char *)(lr + 1);	/* sname and tname follow lr_rename_t */
733 	char *tname = sname + strlen(sname) + 1;
734 	return (do_zfs_replay_rename(zfsvfs, lr, sname, tname, 0, NULL));
735 }
736 
737 static int
738 zfs_replay_rename_exchange(void *arg1, void *arg2, boolean_t byteswap)
739 {
740 #ifdef __linux__
741 	zfsvfs_t *zfsvfs = arg1;
742 	lr_rename_t *lr = arg2;
743 
744 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr));
745 
746 	if (byteswap)
747 		byteswap_uint64_array(lr, sizeof (*lr));
748 
749 	char *sname = (char *)(lr + 1);	/* sname and tname follow lr_rename_t */
750 	char *tname = sname + strlen(sname) + 1;
751 	return (do_zfs_replay_rename(zfsvfs, lr, sname, tname, RENAME_EXCHANGE,
752 	    NULL));
753 #else
754 	return (SET_ERROR(ENOTSUP));
755 #endif
756 }
757 
758 static int
759 zfs_replay_rename_whiteout(void *arg1, void *arg2, boolean_t byteswap)
760 {
761 #ifdef __linux__
762 	zfsvfs_t *zfsvfs = arg1;
763 	lr_rename_whiteout_t *lr = arg2;
764 	int error;
765 	/* For the whiteout file. */
766 	xvattr_t xva;
767 	uint64_t objid;
768 	uint64_t dnodesize;
769 
770 	ASSERT3U(lr->lr_rename.lr_common.lrc_reclen, >, sizeof (*lr));
771 
772 	if (byteswap)
773 		byteswap_uint64_array(lr, sizeof (*lr));
774 
775 	objid = LR_FOID_GET_OBJ(lr->lr_wfoid);
776 	dnodesize = LR_FOID_GET_SLOTS(lr->lr_wfoid) << DNODE_SHIFT;
777 
778 	xva_init(&xva);
779 	zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
780 	    lr->lr_wmode, lr->lr_wuid, lr->lr_wgid, lr->lr_wrdev, objid);
781 
782 	/*
783 	 * As with TX_CREATE, RENAME_WHITEOUT ends up in zfs_mknode(), which
784 	 * assigns the object's creation time, generation number, and dnode
785 	 * slot count. The generic zfs_rename() has no concept of these
786 	 * attributes, so we smuggle the values inside the vattr's otherwise
787 	 * unused va_ctime, va_nblocks, and va_fsid fields.
788 	 */
789 	ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_wcrtime);
790 	xva.xva_vattr.va_nblocks = lr->lr_wgen;
791 	xva.xva_vattr.va_fsid = dnodesize;
792 
793 	error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT);
794 	if (error)
795 		return (error);
796 
797 	/* sname and tname follow lr_rename_whiteout_t */
798 	char *sname = (char *)(lr + 1);
799 	char *tname = sname + strlen(sname) + 1;
800 	return (do_zfs_replay_rename(zfsvfs, &lr->lr_rename, sname, tname,
801 	    RENAME_WHITEOUT, &xva.xva_vattr));
802 #else
803 	return (SET_ERROR(ENOTSUP));
804 #endif
805 }
806 
807 static int
808 zfs_replay_write(void *arg1, void *arg2, boolean_t byteswap)
809 {
810 	zfsvfs_t *zfsvfs = arg1;
811 	lr_write_t *lr = arg2;
812 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
813 	znode_t	*zp;
814 	int error;
815 	uint64_t eod, offset, length;
816 
817 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
818 
819 	if (byteswap)
820 		byteswap_uint64_array(lr, sizeof (*lr));
821 
822 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
823 		/*
824 		 * As we can log writes out of order, it's possible the
825 		 * file has been removed. In this case just drop the write
826 		 * and return success.
827 		 */
828 		if (error == ENOENT)
829 			error = 0;
830 		return (error);
831 	}
832 
833 	offset = lr->lr_offset;
834 	length = lr->lr_length;
835 	eod = offset + length;	/* end of data for this write */
836 
837 	/*
838 	 * This may be a write from a dmu_sync() for a whole block,
839 	 * and may extend beyond the current end of the file.
840 	 * We can't just replay what was written for this TX_WRITE as
841 	 * a future TX_WRITE2 may extend the eof and the data for that
842 	 * write needs to be there. So we write the whole block and
843 	 * reduce the eof. This needs to be done within the single dmu
844 	 * transaction created within vn_rdwr -> zfs_write. So a possible
845 	 * new end of file is passed through in zfsvfs->z_replay_eof
846 	 */
847 
848 	zfsvfs->z_replay_eof = 0; /* 0 means don't change end of file */
849 
850 	/* If it's a dmu_sync() block, write the whole block */
851 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
852 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
853 		if (length < blocksize) {
854 			offset -= offset % blocksize;
855 			length = blocksize;
856 		}
857 		if (zp->z_size < eod)
858 			zfsvfs->z_replay_eof = eod;
859 	}
860 	error = zfs_write_simple(zp, data, length, offset, NULL);
861 	zrele(zp);
862 	zfsvfs->z_replay_eof = 0;	/* safety */
863 
864 	return (error);
865 }
866 
867 /*
868  * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
869  * meaning the pool block is already being synced. So now that we always write
870  * out full blocks, all we have to do is expand the eof if
871  * the file is grown.
872  */
873 static int
874 zfs_replay_write2(void *arg1, void *arg2, boolean_t byteswap)
875 {
876 	zfsvfs_t *zfsvfs = arg1;
877 	lr_write_t *lr = arg2;
878 	znode_t	*zp;
879 	int error;
880 	uint64_t end;
881 
882 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
883 
884 	if (byteswap)
885 		byteswap_uint64_array(lr, sizeof (*lr));
886 
887 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
888 		return (error);
889 
890 top:
891 	end = lr->lr_offset + lr->lr_length;
892 	if (end > zp->z_size) {
893 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
894 
895 		zp->z_size = end;
896 		dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
897 		error = dmu_tx_assign(tx, TXG_WAIT);
898 		if (error) {
899 			zrele(zp);
900 			if (error == ERESTART) {
901 				dmu_tx_wait(tx);
902 				dmu_tx_abort(tx);
903 				goto top;
904 			}
905 			dmu_tx_abort(tx);
906 			return (error);
907 		}
908 		(void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
909 		    (void *)&zp->z_size, sizeof (uint64_t), tx);
910 
911 		/* Ensure the replayed seq is updated */
912 		(void) zil_replaying(zfsvfs->z_log, tx);
913 
914 		dmu_tx_commit(tx);
915 	}
916 
917 	zrele(zp);
918 
919 	return (error);
920 }
921 
922 static int
923 zfs_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
924 {
925 	zfsvfs_t *zfsvfs = arg1;
926 	lr_truncate_t *lr = arg2;
927 	znode_t *zp;
928 	flock64_t fl = {0};
929 	int error;
930 
931 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
932 
933 	if (byteswap)
934 		byteswap_uint64_array(lr, sizeof (*lr));
935 
936 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
937 		return (error);
938 
939 	fl.l_type = F_WRLCK;
940 	fl.l_whence = SEEK_SET;
941 	fl.l_start = lr->lr_offset;
942 	fl.l_len = lr->lr_length;
943 
944 	error = zfs_space(zp, F_FREESP, &fl, O_RDWR | O_LARGEFILE,
945 	    lr->lr_offset, kcred);
946 
947 	zrele(zp);
948 
949 	return (error);
950 }
951 
952 static int
953 zfs_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
954 {
955 	zfsvfs_t *zfsvfs = arg1;
956 	lr_setattr_t *lr = arg2;
957 	znode_t *zp;
958 	xvattr_t xva;
959 	vattr_t *vap = &xva.xva_vattr;
960 	int error;
961 	void *start;
962 
963 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
964 
965 	xva_init(&xva);
966 	if (byteswap) {
967 		byteswap_uint64_array(lr, sizeof (*lr));
968 
969 		if ((lr->lr_mask & ATTR_XVATTR) &&
970 		    zfsvfs->z_version >= ZPL_VERSION_INITIAL)
971 			zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
972 	}
973 
974 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
975 		return (error);
976 
977 	zfs_init_vattr(vap, lr->lr_mask, lr->lr_mode,
978 	    lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
979 
980 	vap->va_size = lr->lr_size;
981 	ZFS_TIME_DECODE(&vap->va_atime, lr->lr_atime);
982 	ZFS_TIME_DECODE(&vap->va_mtime, lr->lr_mtime);
983 	gethrestime(&vap->va_ctime);
984 	vap->va_mask |= ATTR_CTIME;
985 
986 	/*
987 	 * Fill in xvattr_t portions if necessary.
988 	 */
989 
990 	start = (lr_setattr_t *)(lr + 1);
991 	if (vap->va_mask & ATTR_XVATTR) {
992 		zfs_replay_xvattr((lr_attr_t *)start, &xva);
993 		start = (caddr_t)start +
994 		    ZIL_XVAT_SIZE(((lr_attr_t *)start)->lr_attr_masksize);
995 	} else
996 		xva.xva_vattr.va_mask &= ~ATTR_XVATTR;
997 
998 	zfsvfs->z_fuid_replay = zfs_replay_fuid_domain(start, &start,
999 	    lr->lr_uid, lr->lr_gid);
1000 
1001 #if defined(__linux__)
1002 	error = zfs_setattr(zp, vap, 0, kcred, zfs_init_idmap);
1003 #else
1004 	error = zfs_setattr(zp, vap, 0, kcred, NULL);
1005 #endif
1006 
1007 	zfs_fuid_info_free(zfsvfs->z_fuid_replay);
1008 	zfsvfs->z_fuid_replay = NULL;
1009 	zrele(zp);
1010 
1011 	return (error);
1012 }
1013 
1014 static int
1015 zfs_replay_setsaxattr(void *arg1, void *arg2, boolean_t byteswap)
1016 {
1017 	zfsvfs_t *zfsvfs = arg1;
1018 	lr_setsaxattr_t *lr = arg2;
1019 	znode_t *zp;
1020 	nvlist_t *nvl;
1021 	size_t sa_size;
1022 	char *name;
1023 	char *value;
1024 	size_t size;
1025 	int error = 0;
1026 
1027 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
1028 	ASSERT3U(lr->lr_common.lrc_reclen, >, sizeof (*lr) + lr->lr_size);
1029 
1030 	ASSERT(spa_feature_is_active(zfsvfs->z_os->os_spa,
1031 	    SPA_FEATURE_ZILSAXATTR));
1032 	if (byteswap)
1033 		byteswap_uint64_array(lr, sizeof (*lr));
1034 
1035 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1036 		return (error);
1037 
1038 	rw_enter(&zp->z_xattr_lock, RW_WRITER);
1039 	mutex_enter(&zp->z_lock);
1040 	if (zp->z_xattr_cached == NULL)
1041 		error = zfs_sa_get_xattr(zp);
1042 	mutex_exit(&zp->z_lock);
1043 
1044 	if (error)
1045 		goto out;
1046 
1047 	ASSERT(zp->z_xattr_cached);
1048 	nvl = zp->z_xattr_cached;
1049 
1050 	/* Get xattr name, value and size from log record */
1051 	size = lr->lr_size;
1052 	name = (char *)(lr + 1);
1053 	if (size == 0) {
1054 		value = NULL;
1055 		error = nvlist_remove(nvl, name, DATA_TYPE_BYTE_ARRAY);
1056 	} else {
1057 		value = name + strlen(name) + 1;
1058 		/* Limited to 32k to keep nvpair memory allocations small */
1059 		if (size > DXATTR_MAX_ENTRY_SIZE) {
1060 			error = SET_ERROR(EFBIG);
1061 			goto out;
1062 		}
1063 
1064 		/* Prevent the DXATTR SA from consuming the entire SA region */
1065 		error = nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
1066 		if (error)
1067 			goto out;
1068 
1069 		if (sa_size > DXATTR_MAX_SA_SIZE) {
1070 			error = SET_ERROR(EFBIG);
1071 			goto out;
1072 		}
1073 
1074 		error = nvlist_add_byte_array(nvl, name, (uchar_t *)value,
1075 		    size);
1076 	}
1077 
1078 	/*
1079 	 * Update the SA for additions, modifications, and removals. On
1080 	 * error drop the inconsistent cached version of the nvlist, it
1081 	 * will be reconstructed from the ARC when next accessed.
1082 	 */
1083 	if (error == 0)
1084 		error = zfs_sa_set_xattr(zp, name, value, size);
1085 
1086 	if (error) {
1087 		nvlist_free(nvl);
1088 		zp->z_xattr_cached = NULL;
1089 	}
1090 
1091 out:
1092 	rw_exit(&zp->z_xattr_lock);
1093 	zrele(zp);
1094 	return (error);
1095 }
1096 
1097 static int
1098 zfs_replay_acl_v0(void *arg1, void *arg2, boolean_t byteswap)
1099 {
1100 	zfsvfs_t *zfsvfs = arg1;
1101 	lr_acl_v0_t *lr = arg2;
1102 	ace_t *ace = (ace_t *)(lr + 1);	/* ace array follows lr_acl_t */
1103 	vsecattr_t vsa = {0};
1104 	znode_t *zp;
1105 	int error;
1106 
1107 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
1108 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr) +
1109 	    sizeof (ace_t) * lr->lr_aclcnt);
1110 
1111 	if (byteswap) {
1112 		byteswap_uint64_array(lr, sizeof (*lr));
1113 		zfs_oldace_byteswap(ace, lr->lr_aclcnt);
1114 	}
1115 
1116 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1117 		return (error);
1118 
1119 	vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
1120 	vsa.vsa_aclcnt = lr->lr_aclcnt;
1121 	vsa.vsa_aclentsz = sizeof (ace_t) * vsa.vsa_aclcnt;
1122 	vsa.vsa_aclflags = 0;
1123 	vsa.vsa_aclentp = ace;
1124 
1125 	error = zfs_setsecattr(zp, &vsa, 0, kcred);
1126 
1127 	zrele(zp);
1128 
1129 	return (error);
1130 }
1131 
1132 /*
1133  * Replaying ACLs is complicated by FUID support.
1134  * The log record may contain some optional data
1135  * to be used for replaying FUID's.  These pieces
1136  * are the actual FUIDs that were created initially.
1137  * The FUID table index may no longer be valid and
1138  * during zfs_create() a new index may be assigned.
1139  * Because of this the log will contain the original
1140  * domain+rid in order to create a new FUID.
1141  *
1142  * The individual ACEs may contain an ephemeral uid/gid which is no
1143  * longer valid and will need to be replaced with an actual FUID.
1144  *
1145  */
1146 static int
1147 zfs_replay_acl(void *arg1, void *arg2, boolean_t byteswap)
1148 {
1149 	zfsvfs_t *zfsvfs = arg1;
1150 	lr_acl_t *lr = arg2;
1151 	ace_t *ace = (ace_t *)(lr + 1);
1152 	vsecattr_t vsa = {0};
1153 	znode_t *zp;
1154 	int error;
1155 
1156 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
1157 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr) + lr->lr_acl_bytes);
1158 
1159 	if (byteswap) {
1160 		byteswap_uint64_array(lr, sizeof (*lr));
1161 		zfs_ace_byteswap(ace, lr->lr_acl_bytes, B_FALSE);
1162 		if (lr->lr_fuidcnt) {
1163 			byteswap_uint64_array((caddr_t)ace +
1164 			    ZIL_ACE_LENGTH(lr->lr_acl_bytes),
1165 			    lr->lr_fuidcnt * sizeof (uint64_t));
1166 		}
1167 	}
1168 
1169 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1170 		return (error);
1171 
1172 	vsa.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1173 	vsa.vsa_aclcnt = lr->lr_aclcnt;
1174 	vsa.vsa_aclentp = ace;
1175 	vsa.vsa_aclentsz = lr->lr_acl_bytes;
1176 	vsa.vsa_aclflags = lr->lr_acl_flags;
1177 
1178 	if (lr->lr_fuidcnt) {
1179 		void *fuidstart = (caddr_t)ace +
1180 		    ZIL_ACE_LENGTH(lr->lr_acl_bytes);
1181 
1182 		zfsvfs->z_fuid_replay =
1183 		    zfs_replay_fuids(fuidstart, &fuidstart,
1184 		    lr->lr_fuidcnt, lr->lr_domcnt, 0, 0);
1185 	}
1186 
1187 	error = zfs_setsecattr(zp, &vsa, 0, kcred);
1188 
1189 	if (zfsvfs->z_fuid_replay)
1190 		zfs_fuid_info_free(zfsvfs->z_fuid_replay);
1191 
1192 	zfsvfs->z_fuid_replay = NULL;
1193 	zrele(zp);
1194 
1195 	return (error);
1196 }
1197 
1198 static int
1199 zfs_replay_clone_range(void *arg1, void *arg2, boolean_t byteswap)
1200 {
1201 	zfsvfs_t *zfsvfs = arg1;
1202 	lr_clone_range_t *lr = arg2;
1203 	znode_t *zp;
1204 	int error;
1205 
1206 	ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
1207 	ASSERT3U(lr->lr_common.lrc_reclen, >=, offsetof(lr_clone_range_t,
1208 	    lr_bps[lr->lr_nbps]));
1209 
1210 	if (byteswap)
1211 		byteswap_uint64_array(lr, sizeof (*lr));
1212 
1213 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
1214 		/*
1215 		 * Clones can be logged out of order, so don't be surprised if
1216 		 * the file is gone - just return success.
1217 		 */
1218 		if (error == ENOENT)
1219 			error = 0;
1220 		return (error);
1221 	}
1222 
1223 	error = zfs_clone_range_replay(zp, lr->lr_offset, lr->lr_length,
1224 	    lr->lr_blksz, lr->lr_bps, lr->lr_nbps);
1225 
1226 	zrele(zp);
1227 	return (error);
1228 }
1229 
1230 /*
1231  * Callback vectors for replaying records
1232  */
1233 zil_replay_func_t *const zfs_replay_vector[TX_MAX_TYPE] = {
1234 	zfs_replay_error,	/* no such type */
1235 	zfs_replay_create,	/* TX_CREATE */
1236 	zfs_replay_create,	/* TX_MKDIR */
1237 	zfs_replay_create,	/* TX_MKXATTR */
1238 	zfs_replay_create,	/* TX_SYMLINK */
1239 	zfs_replay_remove,	/* TX_REMOVE */
1240 	zfs_replay_remove,	/* TX_RMDIR */
1241 	zfs_replay_link,	/* TX_LINK */
1242 	zfs_replay_rename,	/* TX_RENAME */
1243 	zfs_replay_write,	/* TX_WRITE */
1244 	zfs_replay_truncate,	/* TX_TRUNCATE */
1245 	zfs_replay_setattr,	/* TX_SETATTR */
1246 	zfs_replay_acl_v0,	/* TX_ACL_V0 */
1247 	zfs_replay_acl,		/* TX_ACL */
1248 	zfs_replay_create_acl,	/* TX_CREATE_ACL */
1249 	zfs_replay_create,	/* TX_CREATE_ATTR */
1250 	zfs_replay_create_acl,	/* TX_CREATE_ACL_ATTR */
1251 	zfs_replay_create_acl,	/* TX_MKDIR_ACL */
1252 	zfs_replay_create,	/* TX_MKDIR_ATTR */
1253 	zfs_replay_create_acl,	/* TX_MKDIR_ACL_ATTR */
1254 	zfs_replay_write2,	/* TX_WRITE2 */
1255 	zfs_replay_setsaxattr,	/* TX_SETSAXATTR */
1256 	zfs_replay_rename_exchange,	/* TX_RENAME_EXCHANGE */
1257 	zfs_replay_rename_whiteout,	/* TX_RENAME_WHITEOUT */
1258 	zfs_replay_clone_range,	/* TX_CLONE_RANGE */
1259 };
1260