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