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