xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_replay.c (revision 2a9459bdd821c1cf59590a7a9069ac9c591e8a6b)
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 http://www.opensolaris.org/os/licensing.
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.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/spa.h>
44 #include <sys/zil.h>
45 #include <sys/byteorder.h>
46 #include <sys/stat.h>
47 #include <sys/mode.h>
48 #include <sys/acl.h>
49 #include <sys/atomic.h>
50 #include <sys/cred.h>
51 
52 /*
53  * Functions to replay ZFS intent log (ZIL) records
54  * The functions are called through a function vector (zfs_replay_vector)
55  * which is indexed by the transaction type.
56  */
57 
58 static void
59 zfs_init_vattr(vattr_t *vap, uint64_t mask, uint64_t mode,
60 	uint64_t uid, uint64_t gid, uint64_t rdev, uint64_t nodeid)
61 {
62 	bzero(vap, sizeof (*vap));
63 	vap->va_mask = (uint_t)mask;
64 	vap->va_type = IFTOVT(mode);
65 	vap->va_mode = mode & MODEMASK;
66 	vap->va_uid = (uid_t)(IS_EPHEMERAL(uid)) ? -1 : uid;
67 	vap->va_gid = (gid_t)(IS_EPHEMERAL(gid)) ? -1 : gid;
68 	vap->va_rdev = zfs_cmpldev(rdev);
69 	vap->va_nodeid = nodeid;
70 }
71 
72 /* ARGSUSED */
73 static int
74 zfs_replay_error(zfsvfs_t *zfsvfs, lr_t *lr, boolean_t byteswap)
75 {
76 	return (ENOTSUP);
77 }
78 
79 static void
80 zfs_replay_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
81 {
82 	xoptattr_t *xoap = NULL;
83 	uint64_t *attrs;
84 	uint64_t *crtime;
85 	uint32_t *bitmap;
86 	void *scanstamp;
87 	int i;
88 
89 	xvap->xva_vattr.va_mask |= AT_XVATTR;
90 	if ((xoap = xva_getxoptattr(xvap)) == NULL) {
91 		xvap->xva_vattr.va_mask &= ~AT_XVATTR; /* shouldn't happen */
92 		return;
93 	}
94 
95 	ASSERT(lrattr->lr_attr_masksize == xvap->xva_mapsize);
96 
97 	bitmap = &lrattr->lr_attr_bitmap;
98 	for (i = 0; i != lrattr->lr_attr_masksize; i++, bitmap++)
99 		xvap->xva_reqattrmap[i] = *bitmap;
100 
101 	attrs = (uint64_t *)(lrattr + lrattr->lr_attr_masksize - 1);
102 	crtime = attrs + 1;
103 	scanstamp = (caddr_t)(crtime + 2);
104 
105 	if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
106 		xoap->xoa_hidden = ((*attrs & XAT0_HIDDEN) != 0);
107 	if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
108 		xoap->xoa_system = ((*attrs & XAT0_SYSTEM) != 0);
109 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
110 		xoap->xoa_archive = ((*attrs & XAT0_ARCHIVE) != 0);
111 	if (XVA_ISSET_REQ(xvap, XAT_READONLY))
112 		xoap->xoa_readonly = ((*attrs & XAT0_READONLY) != 0);
113 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
114 		xoap->xoa_immutable = ((*attrs & XAT0_IMMUTABLE) != 0);
115 	if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
116 		xoap->xoa_nounlink = ((*attrs & XAT0_NOUNLINK) != 0);
117 	if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
118 		xoap->xoa_appendonly = ((*attrs & XAT0_APPENDONLY) != 0);
119 	if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
120 		xoap->xoa_nodump = ((*attrs & XAT0_NODUMP) != 0);
121 	if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
122 		xoap->xoa_opaque = ((*attrs & XAT0_OPAQUE) != 0);
123 	if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
124 		xoap->xoa_av_modified = ((*attrs & XAT0_AV_MODIFIED) != 0);
125 	if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
126 		xoap->xoa_av_quarantined =
127 		    ((*attrs & XAT0_AV_QUARANTINED) != 0);
128 	if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
129 		ZFS_TIME_DECODE(&xoap->xoa_createtime, crtime);
130 	if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
131 		bcopy(scanstamp, xoap->xoa_av_scanstamp, AV_SCANSTAMP_SZ);
132 }
133 
134 static int
135 zfs_replay_domain_cnt(uint64_t uid, uint64_t gid)
136 {
137 	uint64_t uid_idx;
138 	uint64_t gid_idx;
139 	int domcnt = 0;
140 
141 	uid_idx = FUID_INDEX(uid);
142 	gid_idx = FUID_INDEX(gid);
143 	if (uid_idx)
144 		domcnt++;
145 	if (gid_idx > 0 && gid_idx != uid_idx)
146 		domcnt++;
147 
148 	return (domcnt);
149 }
150 
151 static void *
152 zfs_replay_fuid_domain_common(zfs_fuid_info_t *fuid_infop, void *start,
153     int domcnt)
154 {
155 	int i;
156 
157 	for (i = 0; i != domcnt; i++) {
158 		fuid_infop->z_domain_table[i] = start;
159 		start = (caddr_t)start + strlen(start) + 1;
160 	}
161 
162 	return (start);
163 }
164 
165 /*
166  * Set the uid/gid in the fuid_info structure.
167  */
168 static void
169 zfs_replay_fuid_ugid(zfs_fuid_info_t *fuid_infop, uint64_t uid, uint64_t gid)
170 {
171 	/*
172 	 * If owner or group are log specific FUIDs then slurp up
173 	 * domain information and build zfs_fuid_info_t
174 	 */
175 	if (IS_EPHEMERAL(uid))
176 		fuid_infop->z_fuid_owner = uid;
177 
178 	if (IS_EPHEMERAL(gid))
179 		fuid_infop->z_fuid_group = gid;
180 }
181 
182 /*
183  * Load fuid domains into fuid_info_t
184  */
185 static zfs_fuid_info_t *
186 zfs_replay_fuid_domain(void *buf, void **end, uint64_t uid, uint64_t gid)
187 {
188 	int domcnt;
189 
190 	zfs_fuid_info_t *fuid_infop;
191 
192 	fuid_infop = zfs_fuid_info_alloc();
193 
194 	domcnt = zfs_replay_domain_cnt(uid, gid);
195 
196 	if (domcnt == 0)
197 		return (fuid_infop);
198 
199 	fuid_infop->z_domain_table =
200 	    kmem_zalloc(domcnt * sizeof (char **), KM_SLEEP);
201 
202 	zfs_replay_fuid_ugid(fuid_infop, uid, gid);
203 
204 	fuid_infop->z_domain_cnt = domcnt;
205 	*end = zfs_replay_fuid_domain_common(fuid_infop, buf, domcnt);
206 	return (fuid_infop);
207 }
208 
209 /*
210  * load zfs_fuid_t's and fuid_domains into fuid_info_t
211  */
212 static zfs_fuid_info_t *
213 zfs_replay_fuids(void *start, void **end, int idcnt, int domcnt, uint64_t uid,
214     uint64_t gid)
215 {
216 	uint64_t *log_fuid = (uint64_t *)start;
217 	zfs_fuid_info_t *fuid_infop;
218 	int i;
219 
220 	fuid_infop = zfs_fuid_info_alloc();
221 	fuid_infop->z_domain_cnt = domcnt;
222 
223 	fuid_infop->z_domain_table =
224 	    kmem_zalloc(domcnt * sizeof (char **), KM_SLEEP);
225 
226 	for (i = 0; i != idcnt; i++) {
227 		zfs_fuid_t *zfuid;
228 
229 		zfuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP);
230 		zfuid->z_logfuid = *log_fuid;
231 		zfuid->z_id = -1;
232 		zfuid->z_domidx = 0;
233 		list_insert_tail(&fuid_infop->z_fuids, zfuid);
234 		log_fuid++;
235 	}
236 
237 	zfs_replay_fuid_ugid(fuid_infop, uid, gid);
238 
239 	*end = zfs_replay_fuid_domain_common(fuid_infop, log_fuid, domcnt);
240 	return (fuid_infop);
241 }
242 
243 static void
244 zfs_replay_swap_attrs(lr_attr_t *lrattr)
245 {
246 	/* swap the lr_attr structure */
247 	byteswap_uint32_array(lrattr, sizeof (*lrattr));
248 	/* swap the bitmap */
249 	byteswap_uint32_array(lrattr + 1, (lrattr->lr_attr_masksize - 1) *
250 	    sizeof (uint32_t));
251 	/* swap the attributes, create time + 64 bit word for attributes */
252 	byteswap_uint64_array((caddr_t)(lrattr + 1) + (sizeof (uint32_t) *
253 	    (lrattr->lr_attr_masksize - 1)), 3 * sizeof (uint64_t));
254 }
255 
256 /*
257  * Replay file create with optional ACL, xvattr information as well
258  * as option FUID information.
259  */
260 static int
261 zfs_replay_create_acl(zfsvfs_t *zfsvfs,
262     lr_acl_create_t *lracl, boolean_t byteswap)
263 {
264 	char *name = NULL;		/* location determined later */
265 	lr_create_t *lr = (lr_create_t *)lracl;
266 	znode_t *dzp;
267 	vnode_t *vp = NULL;
268 	xvattr_t xva;
269 	int vflg = 0;
270 	vsecattr_t vsec = { 0 };
271 	lr_attr_t *lrattr;
272 	void *aclstart;
273 	void *fuidstart;
274 	size_t xvatlen = 0;
275 	uint64_t txtype;
276 	int error;
277 
278 	if (byteswap) {
279 		byteswap_uint64_array(lracl, sizeof (*lracl));
280 		txtype = (int)lr->lr_common.lrc_txtype;
281 		if (txtype == TX_CREATE_ACL_ATTR ||
282 		    txtype == TX_MKDIR_ACL_ATTR) {
283 			lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
284 			zfs_replay_swap_attrs(lrattr);
285 			xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
286 		}
287 
288 		aclstart = (caddr_t)(lracl + 1) + xvatlen;
289 		zfs_ace_byteswap(aclstart, lracl->lr_acl_bytes, B_FALSE);
290 		/* swap fuids */
291 		if (lracl->lr_fuidcnt) {
292 			byteswap_uint64_array((caddr_t)aclstart +
293 			    ZIL_ACE_LENGTH(lracl->lr_acl_bytes),
294 			    lracl->lr_fuidcnt * sizeof (uint64_t));
295 		}
296 	}
297 
298 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
299 		return (error);
300 
301 	xva_init(&xva);
302 	zfs_init_vattr(&xva.xva_vattr, AT_TYPE | AT_MODE | AT_UID | AT_GID,
303 	    lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
304 
305 	/*
306 	 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
307 	 * eventually end up in zfs_mknode(), which assigns the object's
308 	 * creation time and generation number.  The generic VOP_CREATE()
309 	 * doesn't have either concept, so we smuggle the values inside
310 	 * the vattr's otherwise unused va_ctime and va_nblocks fields.
311 	 */
312 	ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
313 	xva.xva_vattr.va_nblocks = lr->lr_gen;
314 
315 	error = dmu_object_info(zfsvfs->z_os, lr->lr_foid, NULL);
316 	if (error != ENOENT)
317 		goto bail;
318 
319 	if (lr->lr_common.lrc_txtype & TX_CI)
320 		vflg |= FIGNORECASE;
321 	switch ((int)lr->lr_common.lrc_txtype) {
322 	case TX_CREATE_ACL:
323 		aclstart = (caddr_t)(lracl + 1);
324 		fuidstart = (caddr_t)aclstart +
325 		    ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
326 		zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
327 		    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
328 		    lr->lr_uid, lr->lr_gid);
329 		/*FALLTHROUGH*/
330 	case TX_CREATE_ACL_ATTR:
331 		if (name == NULL) {
332 			lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
333 			xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
334 			xva.xva_vattr.va_mask |= AT_XVATTR;
335 			zfs_replay_xvattr(lrattr, &xva);
336 		}
337 		vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
338 		vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
339 		vsec.vsa_aclcnt = lracl->lr_aclcnt;
340 		vsec.vsa_aclentsz = lracl->lr_acl_bytes;
341 		vsec.vsa_aclflags = lracl->lr_acl_flags;
342 		if (zfsvfs->z_fuid_replay == NULL)
343 			fuidstart = (caddr_t)(lracl + 1) + xvatlen +
344 			    ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
345 			zfsvfs->z_fuid_replay =
346 			    zfs_replay_fuids(fuidstart,
347 			    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
348 			    lr->lr_uid, lr->lr_gid);
349 
350 		error = VOP_CREATE(ZTOV(dzp), name, &xva.xva_vattr,
351 		    0, 0, &vp, kcred, vflg, NULL, &vsec);
352 		break;
353 	case TX_MKDIR_ACL:
354 		aclstart = (caddr_t)(lracl + 1);
355 		fuidstart = (caddr_t)aclstart +
356 		    ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
357 		zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
358 		    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
359 		    lr->lr_uid, lr->lr_gid);
360 		/*FALLTHROUGH*/
361 	case TX_MKDIR_ACL_ATTR:
362 		if (name == NULL) {
363 			lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
364 			xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
365 			zfs_replay_xvattr(lrattr, &xva);
366 		}
367 		vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
368 		vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
369 		vsec.vsa_aclcnt = lracl->lr_aclcnt;
370 		vsec.vsa_aclentsz = lracl->lr_acl_bytes;
371 		vsec.vsa_aclflags = lracl->lr_acl_flags;
372 		if (zfsvfs->z_fuid_replay == NULL)
373 			fuidstart = (caddr_t)(lracl + 1) + xvatlen +
374 			    ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
375 			zfsvfs->z_fuid_replay =
376 			    zfs_replay_fuids(fuidstart,
377 			    (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
378 			    lr->lr_uid, lr->lr_gid);
379 		error = VOP_MKDIR(ZTOV(dzp), name, &xva.xva_vattr,
380 		    &vp, kcred, NULL, vflg, &vsec);
381 		break;
382 	default:
383 		error = ENOTSUP;
384 	}
385 
386 bail:
387 	if (error == 0 && vp != NULL)
388 		VN_RELE(vp);
389 
390 	VN_RELE(ZTOV(dzp));
391 
392 	zfs_fuid_info_free(zfsvfs->z_fuid_replay);
393 	zfsvfs->z_fuid_replay = NULL;
394 
395 	return (error);
396 }
397 
398 static int
399 zfs_replay_create(zfsvfs_t *zfsvfs, lr_create_t *lr, boolean_t byteswap)
400 {
401 	char *name = NULL;		/* location determined later */
402 	char *link;			/* symlink content follows name */
403 	znode_t *dzp;
404 	vnode_t *vp = NULL;
405 	xvattr_t xva;
406 	int vflg = 0;
407 	size_t lrsize = sizeof (lr_create_t);
408 	lr_attr_t *lrattr;
409 	void *start;
410 	size_t xvatlen;
411 	uint64_t txtype;
412 	int error;
413 
414 	if (byteswap) {
415 		byteswap_uint64_array(lr, sizeof (*lr));
416 		txtype = (int)lr->lr_common.lrc_txtype;
417 		if (txtype == TX_CREATE_ATTR || txtype == TX_MKDIR_ATTR)
418 			zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
419 	}
420 
421 
422 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
423 		return (error);
424 
425 	xva_init(&xva);
426 	zfs_init_vattr(&xva.xva_vattr, AT_TYPE | AT_MODE | AT_UID | AT_GID,
427 	    lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
428 
429 	/*
430 	 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
431 	 * eventually end up in zfs_mknode(), which assigns the object's
432 	 * creation time and generation number.  The generic VOP_CREATE()
433 	 * doesn't have either concept, so we smuggle the values inside
434 	 * the vattr's otherwise unused va_ctime and va_nblocks fields.
435 	 */
436 	ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
437 	xva.xva_vattr.va_nblocks = lr->lr_gen;
438 
439 	error = dmu_object_info(zfsvfs->z_os, lr->lr_foid, NULL);
440 	if (error != ENOENT)
441 		goto out;
442 
443 	if (lr->lr_common.lrc_txtype & TX_CI)
444 		vflg |= FIGNORECASE;
445 
446 	/*
447 	 * Symlinks don't have fuid info, and CIFS never creates
448 	 * symlinks.
449 	 *
450 	 * The _ATTR versions will grab the fuid info in their subcases.
451 	 */
452 	if ((int)lr->lr_common.lrc_txtype != TX_SYMLINK &&
453 	    (int)lr->lr_common.lrc_txtype != TX_MKDIR_ATTR &&
454 	    (int)lr->lr_common.lrc_txtype != TX_CREATE_ATTR) {
455 		start = (lr + 1);
456 		zfsvfs->z_fuid_replay =
457 		    zfs_replay_fuid_domain(start, &start,
458 		    lr->lr_uid, lr->lr_gid);
459 	}
460 
461 	switch ((int)lr->lr_common.lrc_txtype) {
462 	case TX_CREATE_ATTR:
463 		lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
464 		xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
465 		zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
466 		start = (caddr_t)(lr + 1) + xvatlen;
467 		zfsvfs->z_fuid_replay =
468 		    zfs_replay_fuid_domain(start, &start,
469 		    lr->lr_uid, lr->lr_gid);
470 		name = (char *)start;
471 
472 		/*FALLTHROUGH*/
473 	case TX_CREATE:
474 		if (name == NULL)
475 			name = (char *)start;
476 
477 		error = VOP_CREATE(ZTOV(dzp), name, &xva.xva_vattr,
478 		    0, 0, &vp, kcred, vflg, NULL, NULL);
479 		break;
480 	case TX_MKDIR_ATTR:
481 		lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
482 		xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
483 		zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
484 		start = (caddr_t)(lr + 1) + xvatlen;
485 		zfsvfs->z_fuid_replay =
486 		    zfs_replay_fuid_domain(start, &start,
487 		    lr->lr_uid, lr->lr_gid);
488 		name = (char *)start;
489 
490 		/*FALLTHROUGH*/
491 	case TX_MKDIR:
492 		if (name == NULL)
493 			name = (char *)(lr + 1);
494 
495 		error = VOP_MKDIR(ZTOV(dzp), name, &xva.xva_vattr,
496 		    &vp, kcred, NULL, vflg, NULL);
497 		break;
498 	case TX_MKXATTR:
499 		name = (char *)(lr + 1);
500 		error = zfs_make_xattrdir(dzp, &xva.xva_vattr, &vp, kcred);
501 		break;
502 	case TX_SYMLINK:
503 		name = (char *)(lr + 1);
504 		link = name + strlen(name) + 1;
505 		error = VOP_SYMLINK(ZTOV(dzp), name, &xva.xva_vattr,
506 		    link, kcred, NULL, vflg);
507 		break;
508 	default:
509 		error = ENOTSUP;
510 	}
511 
512 out:
513 	if (error == 0 && vp != NULL)
514 		VN_RELE(vp);
515 
516 	VN_RELE(ZTOV(dzp));
517 
518 	if (zfsvfs->z_fuid_replay)
519 		zfs_fuid_info_free(zfsvfs->z_fuid_replay);
520 	zfsvfs->z_fuid_replay = NULL;
521 	return (error);
522 }
523 
524 static int
525 zfs_replay_remove(zfsvfs_t *zfsvfs, lr_remove_t *lr, boolean_t byteswap)
526 {
527 	char *name = (char *)(lr + 1);	/* name follows lr_remove_t */
528 	znode_t *dzp;
529 	int error;
530 	int vflg = 0;
531 
532 	if (byteswap)
533 		byteswap_uint64_array(lr, sizeof (*lr));
534 
535 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
536 		return (error);
537 
538 	if (lr->lr_common.lrc_txtype & TX_CI)
539 		vflg |= FIGNORECASE;
540 
541 	switch ((int)lr->lr_common.lrc_txtype) {
542 	case TX_REMOVE:
543 		error = VOP_REMOVE(ZTOV(dzp), name, kcred, NULL, vflg);
544 		break;
545 	case TX_RMDIR:
546 		error = VOP_RMDIR(ZTOV(dzp), name, NULL, kcred, NULL, vflg);
547 		break;
548 	default:
549 		error = ENOTSUP;
550 	}
551 
552 	VN_RELE(ZTOV(dzp));
553 
554 	return (error);
555 }
556 
557 static int
558 zfs_replay_link(zfsvfs_t *zfsvfs, lr_link_t *lr, boolean_t byteswap)
559 {
560 	char *name = (char *)(lr + 1);	/* name follows lr_link_t */
561 	znode_t *dzp, *zp;
562 	int error;
563 	int vflg = 0;
564 
565 	if (byteswap)
566 		byteswap_uint64_array(lr, sizeof (*lr));
567 
568 	if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
569 		return (error);
570 
571 	if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
572 		VN_RELE(ZTOV(dzp));
573 		return (error);
574 	}
575 
576 	if (lr->lr_common.lrc_txtype & TX_CI)
577 		vflg |= FIGNORECASE;
578 
579 	error = VOP_LINK(ZTOV(dzp), ZTOV(zp), name, kcred, NULL, vflg);
580 
581 	VN_RELE(ZTOV(zp));
582 	VN_RELE(ZTOV(dzp));
583 
584 	return (error);
585 }
586 
587 static int
588 zfs_replay_rename(zfsvfs_t *zfsvfs, lr_rename_t *lr, boolean_t byteswap)
589 {
590 	char *sname = (char *)(lr + 1);	/* sname and tname follow lr_rename_t */
591 	char *tname = sname + strlen(sname) + 1;
592 	znode_t *sdzp, *tdzp;
593 	int error;
594 	int vflg = 0;
595 
596 	if (byteswap)
597 		byteswap_uint64_array(lr, sizeof (*lr));
598 
599 	if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
600 		return (error);
601 
602 	if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
603 		VN_RELE(ZTOV(sdzp));
604 		return (error);
605 	}
606 
607 	if (lr->lr_common.lrc_txtype & TX_CI)
608 		vflg |= FIGNORECASE;
609 
610 	error = VOP_RENAME(ZTOV(sdzp), sname, ZTOV(tdzp), tname, kcred,
611 	    NULL, vflg);
612 
613 	VN_RELE(ZTOV(tdzp));
614 	VN_RELE(ZTOV(sdzp));
615 
616 	return (error);
617 }
618 
619 static int
620 zfs_replay_write(zfsvfs_t *zfsvfs, lr_write_t *lr, boolean_t byteswap)
621 {
622 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
623 	znode_t	*zp;
624 	int error;
625 	ssize_t resid;
626 
627 	if (byteswap)
628 		byteswap_uint64_array(lr, sizeof (*lr));
629 
630 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
631 		/*
632 		 * As we can log writes out of order, it's possible the
633 		 * file has been removed. In this case just drop the write
634 		 * and return success.
635 		 */
636 		if (error == ENOENT)
637 			error = 0;
638 		return (error);
639 	}
640 
641 	error = vn_rdwr(UIO_WRITE, ZTOV(zp), data, lr->lr_length,
642 	    lr->lr_offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
643 
644 	VN_RELE(ZTOV(zp));
645 
646 	return (error);
647 }
648 
649 static int
650 zfs_replay_truncate(zfsvfs_t *zfsvfs, lr_truncate_t *lr, boolean_t byteswap)
651 {
652 	znode_t *zp;
653 	flock64_t fl;
654 	int error;
655 
656 	if (byteswap)
657 		byteswap_uint64_array(lr, sizeof (*lr));
658 
659 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
660 		/*
661 		 * As we can log truncates out of order, it's possible the
662 		 * file has been removed. In this case just drop the truncate
663 		 * and return success.
664 		 */
665 		if (error == ENOENT)
666 			error = 0;
667 		return (error);
668 	}
669 
670 	bzero(&fl, sizeof (fl));
671 	fl.l_type = F_WRLCK;
672 	fl.l_whence = 0;
673 	fl.l_start = lr->lr_offset;
674 	fl.l_len = lr->lr_length;
675 
676 	error = VOP_SPACE(ZTOV(zp), F_FREESP, &fl, FWRITE | FOFFMAX,
677 	    lr->lr_offset, kcred, NULL);
678 
679 	VN_RELE(ZTOV(zp));
680 
681 	return (error);
682 }
683 
684 static int
685 zfs_replay_setattr(zfsvfs_t *zfsvfs, lr_setattr_t *lr, boolean_t byteswap)
686 {
687 	znode_t *zp;
688 	xvattr_t xva;
689 	vattr_t *vap = &xva.xva_vattr;
690 	int error;
691 	void *start;
692 
693 	xva_init(&xva);
694 	if (byteswap) {
695 		byteswap_uint64_array(lr, sizeof (*lr));
696 
697 		if ((lr->lr_mask & AT_XVATTR) &&
698 		    zfsvfs->z_version >= ZPL_VERSION_INITIAL)
699 			zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
700 	}
701 
702 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
703 		/*
704 		 * As we can log setattrs out of order, it's possible the
705 		 * file has been removed. In this case just drop the setattr
706 		 * and return success.
707 		 */
708 		if (error == ENOENT)
709 			error = 0;
710 		return (error);
711 	}
712 
713 	zfs_init_vattr(vap, lr->lr_mask, lr->lr_mode,
714 	    lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
715 
716 	vap->va_size = lr->lr_size;
717 	ZFS_TIME_DECODE(&vap->va_atime, lr->lr_atime);
718 	ZFS_TIME_DECODE(&vap->va_mtime, lr->lr_mtime);
719 
720 	/*
721 	 * Fill in xvattr_t portions if necessary.
722 	 */
723 
724 	start = (lr_setattr_t *)(lr + 1);
725 	if (vap->va_mask & AT_XVATTR) {
726 		zfs_replay_xvattr((lr_attr_t *)start, &xva);
727 		start = (caddr_t)start +
728 		    ZIL_XVAT_SIZE(((lr_attr_t *)start)->lr_attr_masksize);
729 	} else
730 		xva.xva_vattr.va_mask &= ~AT_XVATTR;
731 
732 	zfsvfs->z_fuid_replay = zfs_replay_fuid_domain(start, &start,
733 	    lr->lr_uid, lr->lr_gid);
734 
735 	error = VOP_SETATTR(ZTOV(zp), vap, 0, kcred, NULL);
736 
737 	zfs_fuid_info_free(zfsvfs->z_fuid_replay);
738 	zfsvfs->z_fuid_replay = NULL;
739 	VN_RELE(ZTOV(zp));
740 
741 	return (error);
742 }
743 
744 static int
745 zfs_replay_acl_v0(zfsvfs_t *zfsvfs, lr_acl_v0_t *lr, boolean_t byteswap)
746 {
747 	ace_t *ace = (ace_t *)(lr + 1);	/* ace array follows lr_acl_t */
748 	vsecattr_t vsa;
749 	znode_t *zp;
750 	int error;
751 
752 	if (byteswap) {
753 		byteswap_uint64_array(lr, sizeof (*lr));
754 		zfs_oldace_byteswap(ace, lr->lr_aclcnt);
755 	}
756 
757 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
758 		/*
759 		 * As we can log acls out of order, it's possible the
760 		 * file has been removed. In this case just drop the acl
761 		 * and return success.
762 		 */
763 		if (error == ENOENT)
764 			error = 0;
765 		return (error);
766 	}
767 
768 	bzero(&vsa, sizeof (vsa));
769 	vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
770 	vsa.vsa_aclcnt = lr->lr_aclcnt;
771 	vsa.vsa_aclentp = ace;
772 
773 	error = VOP_SETSECATTR(ZTOV(zp), &vsa, 0, kcred, NULL);
774 
775 	VN_RELE(ZTOV(zp));
776 
777 	return (error);
778 }
779 
780 /*
781  * Replaying ACLs is complicated by FUID support.
782  * The log record may contain some optional data
783  * to be used for replaying FUID's.  These pieces
784  * are the actual FUIDs that were created initially.
785  * The FUID table index may no longer be valid and
786  * during zfs_create() a new index may be assigned.
787  * Because of this the log will contain the original
788  * doman+rid in order to create a new FUID.
789  *
790  * The individual ACEs may contain an ephemeral uid/gid which is no
791  * longer valid and will need to be replaced with an actual FUID.
792  *
793  */
794 static int
795 zfs_replay_acl(zfsvfs_t *zfsvfs, lr_acl_t *lr, boolean_t byteswap)
796 {
797 	ace_t *ace = (ace_t *)(lr + 1);
798 	vsecattr_t vsa;
799 	znode_t *zp;
800 	int error;
801 
802 	if (byteswap) {
803 		byteswap_uint64_array(lr, sizeof (*lr));
804 		zfs_ace_byteswap(ace, lr->lr_acl_bytes, B_FALSE);
805 		if (lr->lr_fuidcnt) {
806 			byteswap_uint64_array((caddr_t)ace +
807 			    ZIL_ACE_LENGTH(lr->lr_acl_bytes),
808 			    lr->lr_fuidcnt * sizeof (uint64_t));
809 		}
810 	}
811 
812 	if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
813 		/*
814 		 * As we can log acls out of order, it's possible the
815 		 * file has been removed. In this case just drop the acl
816 		 * and return success.
817 		 */
818 		if (error == ENOENT)
819 			error = 0;
820 		return (error);
821 	}
822 
823 	bzero(&vsa, sizeof (vsa));
824 	vsa.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
825 	vsa.vsa_aclcnt = lr->lr_aclcnt;
826 	vsa.vsa_aclentp = ace;
827 	vsa.vsa_aclentsz = lr->lr_acl_bytes;
828 	vsa.vsa_aclflags = lr->lr_acl_flags;
829 
830 	if (lr->lr_fuidcnt) {
831 		void *fuidstart = (caddr_t)ace +
832 		    ZIL_ACE_LENGTH(lr->lr_acl_bytes);
833 
834 		zfsvfs->z_fuid_replay =
835 		    zfs_replay_fuids(fuidstart, &fuidstart,
836 		    lr->lr_fuidcnt, lr->lr_domcnt, 0, 0);
837 	}
838 
839 	error = VOP_SETSECATTR(ZTOV(zp), &vsa, 0, kcred, NULL);
840 
841 	if (zfsvfs->z_fuid_replay)
842 		zfs_fuid_info_free(zfsvfs->z_fuid_replay);
843 
844 	zfsvfs->z_fuid_replay = NULL;
845 	VN_RELE(ZTOV(zp));
846 
847 	return (error);
848 }
849 
850 /*
851  * Callback vectors for replaying records
852  */
853 zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = {
854 	zfs_replay_error,	/* 0 no such transaction type */
855 	zfs_replay_create,	/* TX_CREATE */
856 	zfs_replay_create,	/* TX_MKDIR */
857 	zfs_replay_create,	/* TX_MKXATTR */
858 	zfs_replay_create,	/* TX_SYMLINK */
859 	zfs_replay_remove,	/* TX_REMOVE */
860 	zfs_replay_remove,	/* TX_RMDIR */
861 	zfs_replay_link,	/* TX_LINK */
862 	zfs_replay_rename,	/* TX_RENAME */
863 	zfs_replay_write,	/* TX_WRITE */
864 	zfs_replay_truncate,	/* TX_TRUNCATE */
865 	zfs_replay_setattr,	/* TX_SETATTR */
866 	zfs_replay_acl_v0,	/* TX_ACL_V0 */
867 	zfs_replay_acl,		/* TX_ACL */
868 	zfs_replay_create_acl,	/* TX_CREATE_ACL */
869 	zfs_replay_create,	/* TX_CREATE_ATTR */
870 	zfs_replay_create_acl,	/* TX_CREATE_ACL_ATTR */
871 	zfs_replay_create_acl,	/* TX_MKDIR_ACL */
872 	zfs_replay_create,	/* TX_MKDIR_ATTR */
873 	zfs_replay_create_acl,	/* TX_MKDIR_ACL_ATTR */
874 };
875