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