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