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