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