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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
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>
33 #include <sys/fcntl.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>
42 #include <sys/byteorder.h>
45 #include <sys/atomic.h>
47 #include <sys/namei.h>
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.
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)
60 vap->va_mask = (uint_t)mask;
62 vap->va_type = IFTOVT(mode);
64 vap->va_mode = mode & MODEMASK;
66 vap->va_uid = (uid_t)(IS_EPHEMERAL(uid)) ? -1 : uid;
68 vap->va_gid = (gid_t)(IS_EPHEMERAL(gid)) ? -1 : gid;
69 vap->va_rdev = zfs_cmpldev(rdev);
70 vap->va_nodeid = nodeid;
75 zfs_replay_error(void *arg1, void *arg2, boolean_t byteswap)
77 return (SET_ERROR(ENOTSUP));
81 zfs_replay_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
83 xoptattr_t *xoap = NULL;
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 */
96 ASSERT(lrattr->lr_attr_masksize == xvap->xva_mapsize);
98 bitmap = &lrattr->lr_attr_bitmap;
99 for (i = 0; i != lrattr->lr_attr_masksize; i++, bitmap++)
100 xvap->xva_reqattrmap[i] = *bitmap;
102 attrs = (uint64_t *)(lrattr + lrattr->lr_attr_masksize - 1);
104 scanstamp = (caddr_t)(crtime + 2);
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 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
136 xoap->xoa_offline = ((*attrs & XAT0_OFFLINE) != 0);
137 if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
138 xoap->xoa_sparse = ((*attrs & XAT0_SPARSE) != 0);
142 zfs_replay_domain_cnt(uint64_t uid, uint64_t gid)
148 uid_idx = FUID_INDEX(uid);
149 gid_idx = FUID_INDEX(gid);
152 if (gid_idx > 0 && gid_idx != uid_idx)
159 zfs_replay_fuid_domain_common(zfs_fuid_info_t *fuid_infop, void *start,
164 for (i = 0; i != domcnt; i++) {
165 fuid_infop->z_domain_table[i] = start;
166 start = (caddr_t)start + strlen(start) + 1;
173 * Set the uid/gid in the fuid_info structure.
176 zfs_replay_fuid_ugid(zfs_fuid_info_t *fuid_infop, uint64_t uid, uint64_t gid)
179 * If owner or group are log specific FUIDs then slurp up
180 * domain information and build zfs_fuid_info_t
182 if (IS_EPHEMERAL(uid))
183 fuid_infop->z_fuid_owner = uid;
185 if (IS_EPHEMERAL(gid))
186 fuid_infop->z_fuid_group = gid;
190 * Load fuid domains into fuid_info_t
192 static zfs_fuid_info_t *
193 zfs_replay_fuid_domain(void *buf, void **end, uint64_t uid, uint64_t gid)
197 zfs_fuid_info_t *fuid_infop;
199 fuid_infop = zfs_fuid_info_alloc();
201 domcnt = zfs_replay_domain_cnt(uid, gid);
206 fuid_infop->z_domain_table =
207 kmem_zalloc(domcnt * sizeof (char **), KM_SLEEP);
209 zfs_replay_fuid_ugid(fuid_infop, uid, gid);
211 fuid_infop->z_domain_cnt = domcnt;
212 *end = zfs_replay_fuid_domain_common(fuid_infop, buf, domcnt);
217 * load zfs_fuid_t's and fuid_domains into fuid_info_t
219 static zfs_fuid_info_t *
220 zfs_replay_fuids(void *start, void **end, int idcnt, int domcnt, uint64_t uid,
223 uint64_t *log_fuid = (uint64_t *)start;
224 zfs_fuid_info_t *fuid_infop;
227 fuid_infop = zfs_fuid_info_alloc();
228 fuid_infop->z_domain_cnt = domcnt;
230 fuid_infop->z_domain_table =
231 kmem_zalloc(domcnt * sizeof (char **), KM_SLEEP);
233 for (i = 0; i != idcnt; i++) {
236 zfuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP);
237 zfuid->z_logfuid = *log_fuid;
240 list_insert_tail(&fuid_infop->z_fuids, zfuid);
244 zfs_replay_fuid_ugid(fuid_infop, uid, gid);
246 *end = zfs_replay_fuid_domain_common(fuid_infop, log_fuid, domcnt);
251 zfs_replay_swap_attrs(lr_attr_t *lrattr)
253 /* swap the lr_attr structure */
254 byteswap_uint32_array(lrattr, sizeof (*lrattr));
255 /* swap the bitmap */
256 byteswap_uint32_array(lrattr + 1, (lrattr->lr_attr_masksize - 1) *
258 /* swap the attributes, create time + 64 bit word for attributes */
259 byteswap_uint64_array((caddr_t)(lrattr + 1) + (sizeof (uint32_t) *
260 (lrattr->lr_attr_masksize - 1)), 3 * sizeof (uint64_t));
264 * Replay file create with optional ACL, xvattr information as well
265 * as option FUID information.
268 zfs_replay_create_acl(void *arg1, void *arg2, boolean_t byteswap)
270 zfsvfs_t *zfsvfs = arg1;
271 lr_acl_create_t *lracl = arg2;
272 char *name = NULL; /* location determined later */
273 lr_create_t *lr = (lr_create_t *)lracl;
278 vsecattr_t vsec = { 0 };
286 txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
288 byteswap_uint64_array(lracl, sizeof (*lracl));
289 if (txtype == TX_CREATE_ACL_ATTR ||
290 txtype == TX_MKDIR_ACL_ATTR) {
291 lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
292 zfs_replay_swap_attrs(lrattr);
293 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
296 aclstart = (caddr_t)(lracl + 1) + xvatlen;
297 zfs_ace_byteswap(aclstart, lracl->lr_acl_bytes, B_FALSE);
299 if (lracl->lr_fuidcnt) {
300 byteswap_uint64_array((caddr_t)aclstart +
301 ZIL_ACE_LENGTH(lracl->lr_acl_bytes),
302 lracl->lr_fuidcnt * sizeof (uint64_t));
306 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
310 zfs_init_vattr(&xva.xva_vattr, AT_TYPE | AT_MODE | AT_UID | AT_GID,
311 lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
314 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
315 * eventually end up in zfs_mknode(), which assigns the object's
316 * creation time and generation number. The generic VOP_CREATE()
317 * doesn't have either concept, so we smuggle the values inside
318 * the vattr's otherwise unused va_ctime and va_nblocks fields.
320 ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
321 xva.xva_vattr.va_nblocks = lr->lr_gen;
323 error = dmu_object_info(zfsvfs->z_os, lr->lr_foid, NULL);
327 if (lr->lr_common.lrc_txtype & TX_CI)
331 aclstart = (caddr_t)(lracl + 1);
332 fuidstart = (caddr_t)aclstart +
333 ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
334 zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
335 (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
336 lr->lr_uid, lr->lr_gid);
338 case TX_CREATE_ACL_ATTR:
340 lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
341 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
342 xva.xva_vattr.va_mask |= AT_XVATTR;
343 zfs_replay_xvattr(lrattr, &xva);
345 vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
346 vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
347 vsec.vsa_aclcnt = lracl->lr_aclcnt;
348 vsec.vsa_aclentsz = lracl->lr_acl_bytes;
349 vsec.vsa_aclflags = lracl->lr_acl_flags;
350 if (zfsvfs->z_fuid_replay == NULL) {
351 fuidstart = (caddr_t)(lracl + 1) + xvatlen +
352 ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
353 zfsvfs->z_fuid_replay =
354 zfs_replay_fuids(fuidstart,
355 (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
356 lr->lr_uid, lr->lr_gid);
360 error = VOP_CREATE(ZTOV(dzp), name, &xva.xva_vattr,
361 0, 0, &vp, kcred, vflg, NULL, &vsec);
363 panic("%s:%u: unsupported condition", __func__, __LINE__);
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);
374 case TX_MKDIR_ACL_ATTR:
376 lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
377 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
378 zfs_replay_xvattr(lrattr, &xva);
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);
394 error = VOP_MKDIR(ZTOV(dzp), name, &xva.xva_vattr,
395 &vp, kcred, NULL, vflg, &vsec);
397 panic("%s:%u: unsupported condition", __func__, __LINE__);
401 error = SET_ERROR(ENOTSUP);
405 if (error == 0 && vp != NULL)
410 if (zfsvfs->z_fuid_replay)
411 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
412 zfsvfs->z_fuid_replay = NULL;
418 zfs_replay_create(void *arg1, void *arg2, boolean_t byteswap)
420 zfsvfs_t *zfsvfs = arg1;
421 lr_create_t *lr = arg2;
422 char *name = NULL; /* location determined later */
423 char *link; /* symlink content follows name */
428 size_t lrsize = sizeof (lr_create_t);
433 struct componentname cn;
436 txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
438 byteswap_uint64_array(lr, sizeof (*lr));
439 if (txtype == TX_CREATE_ATTR || txtype == TX_MKDIR_ATTR)
440 zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
444 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
448 zfs_init_vattr(&xva.xva_vattr, AT_TYPE | AT_MODE | AT_UID | AT_GID,
449 lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, lr->lr_foid);
452 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
453 * eventually end up in zfs_mknode(), which assigns the object's
454 * creation time and generation number. The generic VOP_CREATE()
455 * doesn't have either concept, so we smuggle the values inside
456 * the vattr's otherwise unused va_ctime and va_nblocks fields.
458 ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
459 xva.xva_vattr.va_nblocks = lr->lr_gen;
461 error = dmu_object_info(zfsvfs->z_os, lr->lr_foid, NULL);
465 if (lr->lr_common.lrc_txtype & TX_CI)
469 * Symlinks don't have fuid info, and CIFS never creates
472 * The _ATTR versions will grab the fuid info in their subcases.
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) {
478 zfsvfs->z_fuid_replay =
479 zfs_replay_fuid_domain(start, &start,
480 lr->lr_uid, lr->lr_gid);
484 cn.cn_thread = curthread;
485 cn.cn_flags = SAVENAME;
487 vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
490 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
491 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
492 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
493 start = (caddr_t)(lr + 1) + xvatlen;
494 zfsvfs->z_fuid_replay =
495 zfs_replay_fuid_domain(start, &start,
496 lr->lr_uid, lr->lr_gid);
497 name = (char *)start;
502 name = (char *)start;
504 cn.cn_nameptr = name;
505 error = VOP_CREATE(ZTOV(dzp), &vp, &cn, &xva.xva_vattr /*,vflg*/);
508 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
509 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
510 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
511 start = (caddr_t)(lr + 1) + xvatlen;
512 zfsvfs->z_fuid_replay =
513 zfs_replay_fuid_domain(start, &start,
514 lr->lr_uid, lr->lr_gid);
515 name = (char *)start;
520 name = (char *)(lr + 1);
522 cn.cn_nameptr = name;
523 error = VOP_MKDIR(ZTOV(dzp), &vp, &cn, &xva.xva_vattr /*,vflg*/);
526 error = zfs_make_xattrdir(dzp, &xva.xva_vattr, &vp, kcred);
529 name = (char *)(lr + 1);
530 link = name + strlen(name) + 1;
531 cn.cn_nameptr = name;
532 error = VOP_SYMLINK(ZTOV(dzp), &vp, &cn, &xva.xva_vattr, link /*,vflg*/);
535 error = SET_ERROR(ENOTSUP);
537 VOP_UNLOCK(ZTOV(dzp), 0);
540 if (error == 0 && vp != NULL)
545 if (zfsvfs->z_fuid_replay)
546 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
547 zfsvfs->z_fuid_replay = NULL;
552 zfs_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
554 zfsvfs_t *zfsvfs = arg1;
555 lr_remove_t *lr = arg2;
556 char *name = (char *)(lr + 1); /* name follows lr_remove_t */
558 struct componentname cn;
564 byteswap_uint64_array(lr, sizeof (*lr));
566 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
569 if (lr->lr_common.lrc_txtype & TX_CI)
571 cn.cn_nameptr = name;
572 cn.cn_namelen = strlen(name);
573 cn.cn_nameiop = DELETE;
574 cn.cn_flags = ISLASTCN | SAVENAME;
575 cn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
577 cn.cn_thread = curthread;
578 vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
579 error = VOP_LOOKUP(ZTOV(dzp), &vp, &cn);
581 VOP_UNLOCK(ZTOV(dzp), 0);
585 switch ((int)lr->lr_common.lrc_txtype) {
587 error = VOP_REMOVE(ZTOV(dzp), vp, &cn /*,vflg*/);
590 error = VOP_RMDIR(ZTOV(dzp), vp, &cn /*,vflg*/);
593 error = SET_ERROR(ENOTSUP);
596 VOP_UNLOCK(ZTOV(dzp), 0);
605 zfs_replay_link(void *arg1, void *arg2, boolean_t byteswap)
607 zfsvfs_t *zfsvfs = arg1;
608 lr_link_t *lr = arg2;
609 char *name = (char *)(lr + 1); /* name follows lr_link_t */
611 struct componentname cn;
616 byteswap_uint64_array(lr, sizeof (*lr));
618 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
621 if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
626 if (lr->lr_common.lrc_txtype & TX_CI)
629 cn.cn_nameptr = name;
631 cn.cn_thread = curthread;
632 cn.cn_flags = SAVENAME;
634 vn_lock(ZTOV(dzp), LK_EXCLUSIVE | LK_RETRY);
635 vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY);
636 error = VOP_LINK(ZTOV(dzp), ZTOV(zp), &cn /*,vflg*/);
637 VOP_UNLOCK(ZTOV(zp), 0);
638 VOP_UNLOCK(ZTOV(dzp), 0);
647 zfs_replay_rename(void *arg1, void *arg2, boolean_t byteswap)
649 zfsvfs_t *zfsvfs = arg1;
650 lr_rename_t *lr = arg2;
651 char *sname = (char *)(lr + 1); /* sname and tname follow lr_rename_t */
652 char *tname = sname + strlen(sname) + 1;
653 znode_t *sdzp, *tdzp;
654 struct componentname scn, tcn;
656 kthread_t *td = curthread;
661 byteswap_uint64_array(lr, sizeof (*lr));
663 if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
666 if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
671 if (lr->lr_common.lrc_txtype & TX_CI)
675 scn.cn_nameptr = sname;
676 scn.cn_namelen = strlen(sname);
677 scn.cn_nameiop = DELETE;
678 scn.cn_flags = ISLASTCN | SAVENAME;
679 scn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
682 vn_lock(ZTOV(sdzp), LK_EXCLUSIVE | LK_RETRY);
683 error = VOP_LOOKUP(ZTOV(sdzp), &svp, &scn);
684 VOP_UNLOCK(ZTOV(sdzp), 0);
689 tcn.cn_nameptr = tname;
690 tcn.cn_namelen = strlen(tname);
691 tcn.cn_nameiop = RENAME;
692 tcn.cn_flags = ISLASTCN | SAVENAME;
693 tcn.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
696 vn_lock(ZTOV(tdzp), LK_EXCLUSIVE | LK_RETRY);
697 error = VOP_LOOKUP(ZTOV(tdzp), &tvp, &tcn);
698 if (error == EJUSTRETURN)
700 else if (error != 0) {
701 VOP_UNLOCK(ZTOV(tdzp), 0);
705 error = VOP_RENAME(ZTOV(sdzp), svp, &scn, ZTOV(tdzp), tvp, &tcn /*,vflg*/);
719 zfs_replay_write(void *arg1, void *arg2, boolean_t byteswap)
721 zfsvfs_t *zfsvfs = arg1;
722 lr_write_t *lr = arg2;
723 char *data = (char *)(lr + 1); /* data follows lr_write_t */
727 uint64_t eod, offset, length;
730 byteswap_uint64_array(lr, sizeof (*lr));
732 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
734 * As we can log writes out of order, it's possible the
735 * file has been removed. In this case just drop the write
736 * and return success.
743 offset = lr->lr_offset;
744 length = lr->lr_length;
745 eod = offset + length; /* end of data for this write */
748 * This may be a write from a dmu_sync() for a whole block,
749 * and may extend beyond the current end of the file.
750 * We can't just replay what was written for this TX_WRITE as
751 * a future TX_WRITE2 may extend the eof and the data for that
752 * write needs to be there. So we write the whole block and
753 * reduce the eof. This needs to be done within the single dmu
754 * transaction created within vn_rdwr -> zfs_write. So a possible
755 * new end of file is passed through in zfsvfs->z_replay_eof
758 zfsvfs->z_replay_eof = 0; /* 0 means don't change end of file */
760 /* If it's a dmu_sync() block, write the whole block */
761 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
762 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
763 if (length < blocksize) {
764 offset -= offset % blocksize;
767 if (zp->z_size < eod)
768 zfsvfs->z_replay_eof = eod;
771 error = vn_rdwr(UIO_WRITE, ZTOV(zp), data, length, offset,
772 UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
775 zfsvfs->z_replay_eof = 0; /* safety */
781 * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
782 * meaning the pool block is already being synced. So now that we always write
783 * out full blocks, all we have to do is expand the eof if
787 zfs_replay_write2(void *arg1, void *arg2, boolean_t byteswap)
789 zfsvfs_t *zfsvfs = arg1;
790 lr_write_t *lr = arg2;
796 byteswap_uint64_array(lr, sizeof (*lr));
798 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
802 end = lr->lr_offset + lr->lr_length;
803 if (end > zp->z_size) {
804 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
807 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
808 error = dmu_tx_assign(tx, TXG_WAIT);
811 if (error == ERESTART) {
819 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
820 (void *)&zp->z_size, sizeof (uint64_t), tx);
822 /* Ensure the replayed seq is updated */
823 (void) zil_replaying(zfsvfs->z_log, tx);
834 zfs_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
837 zfsvfs_t *zfsvfs = arg1;
838 lr_truncate_t *lr = arg2;
844 byteswap_uint64_array(lr, sizeof (*lr));
846 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
849 bzero(&fl, sizeof (fl));
852 fl.l_start = lr->lr_offset;
853 fl.l_len = lr->lr_length;
855 error = VOP_SPACE(ZTOV(zp), F_FREESP, &fl, FWRITE | FOFFMAX,
856 lr->lr_offset, kcred, NULL);
862 ZFS_LOG(0, "Unexpected code path, report to pjd@FreeBSD.org");
868 zfs_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
870 zfsvfs_t *zfsvfs = arg1;
871 lr_setattr_t *lr = arg2;
874 vattr_t *vap = &xva.xva_vattr;
881 byteswap_uint64_array(lr, sizeof (*lr));
883 if ((lr->lr_mask & AT_XVATTR) &&
884 zfsvfs->z_version >= ZPL_VERSION_INITIAL)
885 zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
888 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
891 zfs_init_vattr(vap, lr->lr_mask, lr->lr_mode,
892 lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
894 vap->va_size = lr->lr_size;
895 ZFS_TIME_DECODE(&vap->va_atime, lr->lr_atime);
896 ZFS_TIME_DECODE(&vap->va_mtime, lr->lr_mtime);
899 * Fill in xvattr_t portions if necessary.
902 start = (lr_setattr_t *)(lr + 1);
903 if (vap->va_mask & AT_XVATTR) {
904 zfs_replay_xvattr((lr_attr_t *)start, &xva);
905 start = (caddr_t)start +
906 ZIL_XVAT_SIZE(((lr_attr_t *)start)->lr_attr_masksize);
908 xva.xva_vattr.va_mask &= ~AT_XVATTR;
910 zfsvfs->z_fuid_replay = zfs_replay_fuid_domain(start, &start,
911 lr->lr_uid, lr->lr_gid);
914 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
915 error = VOP_SETATTR(vp, vap, kcred);
918 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
919 zfsvfs->z_fuid_replay = NULL;
925 extern int zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
926 caller_context_t *ct);
929 zfs_replay_acl_v0(void *arg1, void *arg2, boolean_t byteswap)
931 zfsvfs_t *zfsvfs = arg1;
932 lr_acl_v0_t *lr = arg2;
933 ace_t *ace = (ace_t *)(lr + 1); /* ace array follows lr_acl_t */
940 byteswap_uint64_array(lr, sizeof (*lr));
941 zfs_oldace_byteswap(ace, lr->lr_aclcnt);
944 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
947 bzero(&vsa, sizeof (vsa));
948 vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
949 vsa.vsa_aclcnt = lr->lr_aclcnt;
950 vsa.vsa_aclentsz = sizeof (ace_t) * vsa.vsa_aclcnt;
951 vsa.vsa_aclflags = 0;
952 vsa.vsa_aclentp = ace;
955 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
956 error = zfs_setsecattr(vp, &vsa, 0, kcred, NULL);
965 * Replaying ACLs is complicated by FUID support.
966 * The log record may contain some optional data
967 * to be used for replaying FUID's. These pieces
968 * are the actual FUIDs that were created initially.
969 * The FUID table index may no longer be valid and
970 * during zfs_create() a new index may be assigned.
971 * Because of this the log will contain the original
972 * doman+rid in order to create a new FUID.
974 * The individual ACEs may contain an ephemeral uid/gid which is no
975 * longer valid and will need to be replaced with an actual FUID.
979 zfs_replay_acl(void *arg1, void *arg2, boolean_t byteswap)
981 zfsvfs_t *zfsvfs = arg1;
983 ace_t *ace = (ace_t *)(lr + 1);
990 byteswap_uint64_array(lr, sizeof (*lr));
991 zfs_ace_byteswap(ace, lr->lr_acl_bytes, B_FALSE);
992 if (lr->lr_fuidcnt) {
993 byteswap_uint64_array((caddr_t)ace +
994 ZIL_ACE_LENGTH(lr->lr_acl_bytes),
995 lr->lr_fuidcnt * sizeof (uint64_t));
999 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1002 bzero(&vsa, sizeof (vsa));
1003 vsa.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1004 vsa.vsa_aclcnt = lr->lr_aclcnt;
1005 vsa.vsa_aclentp = ace;
1006 vsa.vsa_aclentsz = lr->lr_acl_bytes;
1007 vsa.vsa_aclflags = lr->lr_acl_flags;
1009 if (lr->lr_fuidcnt) {
1010 void *fuidstart = (caddr_t)ace +
1011 ZIL_ACE_LENGTH(lr->lr_acl_bytes);
1013 zfsvfs->z_fuid_replay =
1014 zfs_replay_fuids(fuidstart, &fuidstart,
1015 lr->lr_fuidcnt, lr->lr_domcnt, 0, 0);
1019 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1020 error = zfs_setsecattr(vp, &vsa, 0, kcred, NULL);
1023 if (zfsvfs->z_fuid_replay)
1024 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
1026 zfsvfs->z_fuid_replay = NULL;
1033 * Callback vectors for replaying records
1035 zil_replay_func_t *zfs_replay_vector[TX_MAX_TYPE] = {
1036 zfs_replay_error, /* 0 no such transaction type */
1037 zfs_replay_create, /* TX_CREATE */
1038 zfs_replay_create, /* TX_MKDIR */
1039 zfs_replay_create, /* TX_MKXATTR */
1040 zfs_replay_create, /* TX_SYMLINK */
1041 zfs_replay_remove, /* TX_REMOVE */
1042 zfs_replay_remove, /* TX_RMDIR */
1043 zfs_replay_link, /* TX_LINK */
1044 zfs_replay_rename, /* TX_RENAME */
1045 zfs_replay_write, /* TX_WRITE */
1046 zfs_replay_truncate, /* TX_TRUNCATE */
1047 zfs_replay_setattr, /* TX_SETATTR */
1048 zfs_replay_acl_v0, /* TX_ACL_V0 */
1049 zfs_replay_acl, /* TX_ACL */
1050 zfs_replay_create_acl, /* TX_CREATE_ACL */
1051 zfs_replay_create, /* TX_CREATE_ATTR */
1052 zfs_replay_create_acl, /* TX_CREATE_ACL_ATTR */
1053 zfs_replay_create_acl, /* TX_MKDIR_ACL */
1054 zfs_replay_create, /* TX_MKDIR_ATTR */
1055 zfs_replay_create_acl, /* TX_MKDIR_ACL_ATTR */
1056 zfs_replay_write2, /* TX_WRITE2 */