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