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 https://opensource.org/licenses/CDDL-1.0.
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) 2012 Cyril Plisko. All rights reserved.
24 * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/sysmacros.h>
30 #include <sys/cmn_err.h>
32 #include <sys/thread.h>
34 #include <sys/fcntl.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/zfs_vnops.h>
44 #include <sys/byteorder.h>
47 #include <sys/atomic.h>
50 #include <sys/dmu_objset.h>
51 #include <sys/zfeature.h>
54 * NB: FreeBSD expects to be able to do vnode locking in lookup and
55 * hold the locks across all subsequent VOPs until vput is called.
56 * This means that its zfs vnops routines can't do any internal locking.
57 * In order to have the same contract as the Linux vnops there would
58 * needed to be duplicate locked vnops. If the vnops were used more widely
59 * in common code this would likely be preferable. However, currently
60 * this is the only file where this is the case.
64 * Functions to replay ZFS intent log (ZIL) records
65 * The functions are called through a function vector (zfs_replay_vector)
66 * which is indexed by the transaction type.
70 zfs_init_vattr(vattr_t *vap, uint64_t mask, uint64_t mode,
71 uint64_t uid, uint64_t gid, uint64_t rdev, uint64_t nodeid)
73 memset(vap, 0, sizeof (*vap));
74 vap->va_mask = (uint_t)mask;
76 #if defined(__FreeBSD__) || defined(__APPLE__)
77 vap->va_type = IFTOVT(mode);
79 vap->va_uid = (uid_t)(IS_EPHEMERAL(uid)) ? -1 : uid;
80 vap->va_gid = (gid_t)(IS_EPHEMERAL(gid)) ? -1 : gid;
81 vap->va_rdev = zfs_cmpldev(rdev);
82 vap->va_nodeid = nodeid;
86 zfs_replay_error(void *arg1, void *arg2, boolean_t byteswap)
88 (void) arg1, (void) arg2, (void) byteswap;
89 return (SET_ERROR(ENOTSUP));
93 zfs_replay_xvattr(lr_attr_t *lrattr, xvattr_t *xvap)
95 xoptattr_t *xoap = NULL;
102 xvap->xva_vattr.va_mask |= ATTR_XVATTR;
103 if ((xoap = xva_getxoptattr(xvap)) == NULL) {
104 xvap->xva_vattr.va_mask &= ~ATTR_XVATTR; /* shouldn't happen */
108 ASSERT(lrattr->lr_attr_masksize == xvap->xva_mapsize);
110 bitmap = &lrattr->lr_attr_bitmap;
111 for (i = 0; i != lrattr->lr_attr_masksize; i++, bitmap++)
112 xvap->xva_reqattrmap[i] = *bitmap;
114 attrs = (uint64_t *)(lrattr + lrattr->lr_attr_masksize - 1);
116 scanstamp = (caddr_t)(crtime + 2);
118 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN))
119 xoap->xoa_hidden = ((*attrs & XAT0_HIDDEN) != 0);
120 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM))
121 xoap->xoa_system = ((*attrs & XAT0_SYSTEM) != 0);
122 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE))
123 xoap->xoa_archive = ((*attrs & XAT0_ARCHIVE) != 0);
124 if (XVA_ISSET_REQ(xvap, XAT_READONLY))
125 xoap->xoa_readonly = ((*attrs & XAT0_READONLY) != 0);
126 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
127 xoap->xoa_immutable = ((*attrs & XAT0_IMMUTABLE) != 0);
128 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
129 xoap->xoa_nounlink = ((*attrs & XAT0_NOUNLINK) != 0);
130 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
131 xoap->xoa_appendonly = ((*attrs & XAT0_APPENDONLY) != 0);
132 if (XVA_ISSET_REQ(xvap, XAT_NODUMP))
133 xoap->xoa_nodump = ((*attrs & XAT0_NODUMP) != 0);
134 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE))
135 xoap->xoa_opaque = ((*attrs & XAT0_OPAQUE) != 0);
136 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
137 xoap->xoa_av_modified = ((*attrs & XAT0_AV_MODIFIED) != 0);
138 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED))
139 xoap->xoa_av_quarantined =
140 ((*attrs & XAT0_AV_QUARANTINED) != 0);
141 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
142 ZFS_TIME_DECODE(&xoap->xoa_createtime, crtime);
143 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
144 ASSERT(!XVA_ISSET_REQ(xvap, XAT_PROJID));
146 memcpy(xoap->xoa_av_scanstamp, scanstamp, AV_SCANSTAMP_SZ);
147 } else if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
149 * XAT_PROJID and XAT_AV_SCANSTAMP will never be valid
150 * at the same time, so we can share the same space.
152 memcpy(&xoap->xoa_projid, scanstamp, sizeof (uint64_t));
154 if (XVA_ISSET_REQ(xvap, XAT_REPARSE))
155 xoap->xoa_reparse = ((*attrs & XAT0_REPARSE) != 0);
156 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE))
157 xoap->xoa_offline = ((*attrs & XAT0_OFFLINE) != 0);
158 if (XVA_ISSET_REQ(xvap, XAT_SPARSE))
159 xoap->xoa_sparse = ((*attrs & XAT0_SPARSE) != 0);
160 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT))
161 xoap->xoa_projinherit = ((*attrs & XAT0_PROJINHERIT) != 0);
165 zfs_replay_domain_cnt(uint64_t uid, uint64_t gid)
171 uid_idx = FUID_INDEX(uid);
172 gid_idx = FUID_INDEX(gid);
175 if (gid_idx > 0 && gid_idx != uid_idx)
182 zfs_replay_fuid_domain_common(zfs_fuid_info_t *fuid_infop, void *start,
187 for (i = 0; i != domcnt; i++) {
188 fuid_infop->z_domain_table[i] = start;
189 start = (caddr_t)start + strlen(start) + 1;
196 * Set the uid/gid in the fuid_info structure.
199 zfs_replay_fuid_ugid(zfs_fuid_info_t *fuid_infop, uint64_t uid, uint64_t gid)
202 * If owner or group are log specific FUIDs then slurp up
203 * domain information and build zfs_fuid_info_t
205 if (IS_EPHEMERAL(uid))
206 fuid_infop->z_fuid_owner = uid;
208 if (IS_EPHEMERAL(gid))
209 fuid_infop->z_fuid_group = gid;
213 * Load fuid domains into fuid_info_t
215 static zfs_fuid_info_t *
216 zfs_replay_fuid_domain(void *buf, void **end, uint64_t uid, uint64_t gid)
220 zfs_fuid_info_t *fuid_infop;
222 fuid_infop = zfs_fuid_info_alloc();
224 domcnt = zfs_replay_domain_cnt(uid, gid);
229 fuid_infop->z_domain_table =
230 kmem_zalloc(domcnt * sizeof (char *), KM_SLEEP);
232 zfs_replay_fuid_ugid(fuid_infop, uid, gid);
234 fuid_infop->z_domain_cnt = domcnt;
235 *end = zfs_replay_fuid_domain_common(fuid_infop, buf, domcnt);
240 * load zfs_fuid_t's and fuid_domains into fuid_info_t
242 static zfs_fuid_info_t *
243 zfs_replay_fuids(void *start, void **end, int idcnt, int domcnt, uint64_t uid,
246 uint64_t *log_fuid = (uint64_t *)start;
247 zfs_fuid_info_t *fuid_infop;
250 fuid_infop = zfs_fuid_info_alloc();
251 fuid_infop->z_domain_cnt = domcnt;
253 fuid_infop->z_domain_table =
254 kmem_zalloc(domcnt * sizeof (char *), KM_SLEEP);
256 for (i = 0; i != idcnt; i++) {
259 zfuid = kmem_alloc(sizeof (zfs_fuid_t), KM_SLEEP);
260 zfuid->z_logfuid = *log_fuid;
263 list_insert_tail(&fuid_infop->z_fuids, zfuid);
267 zfs_replay_fuid_ugid(fuid_infop, uid, gid);
269 *end = zfs_replay_fuid_domain_common(fuid_infop, log_fuid, domcnt);
274 zfs_replay_swap_attrs(lr_attr_t *lrattr)
276 /* swap the lr_attr structure */
277 byteswap_uint32_array(lrattr, sizeof (*lrattr));
278 /* swap the bitmap */
279 byteswap_uint32_array(lrattr + 1, (lrattr->lr_attr_masksize - 1) *
281 /* swap the attributes, create time + 64 bit word for attributes */
282 byteswap_uint64_array((caddr_t)(lrattr + 1) + (sizeof (uint32_t) *
283 (lrattr->lr_attr_masksize - 1)), 3 * sizeof (uint64_t));
287 * Replay file create with optional ACL, xvattr information as well
288 * as option FUID information.
291 zfs_replay_create_acl(void *arg1, void *arg2, boolean_t byteswap)
293 zfsvfs_t *zfsvfs = arg1;
294 lr_acl_create_t *lracl = arg2;
295 char *name = NULL; /* location determined later */
296 lr_create_t *lr = (lr_create_t *)lracl;
301 vsecattr_t vsec = { 0 };
311 txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
313 byteswap_uint64_array(lracl, sizeof (*lracl));
314 if (txtype == TX_CREATE_ACL_ATTR ||
315 txtype == TX_MKDIR_ACL_ATTR) {
316 lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
317 zfs_replay_swap_attrs(lrattr);
318 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
321 aclstart = (caddr_t)(lracl + 1) + xvatlen;
322 zfs_ace_byteswap(aclstart, lracl->lr_acl_bytes, B_FALSE);
324 if (lracl->lr_fuidcnt) {
325 byteswap_uint64_array((caddr_t)aclstart +
326 ZIL_ACE_LENGTH(lracl->lr_acl_bytes),
327 lracl->lr_fuidcnt * sizeof (uint64_t));
331 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
334 objid = LR_FOID_GET_OBJ(lr->lr_foid);
335 dnodesize = LR_FOID_GET_SLOTS(lr->lr_foid) << DNODE_SHIFT;
338 zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
339 lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, objid);
342 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
343 * eventually end up in zfs_mknode(), which assigns the object's
344 * creation time, generation number, and dnode size. The generic
345 * zfs_create() has no concept of these attributes, so we smuggle
346 * the values inside the vattr's otherwise unused va_ctime,
347 * va_nblocks, and va_fsid fields.
349 ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
350 xva.xva_vattr.va_nblocks = lr->lr_gen;
351 xva.xva_vattr.va_fsid = dnodesize;
353 error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT);
357 if (lr->lr_common.lrc_txtype & TX_CI)
361 aclstart = (caddr_t)(lracl + 1);
362 fuidstart = (caddr_t)aclstart +
363 ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
364 zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
365 (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
366 lr->lr_uid, lr->lr_gid);
368 case TX_CREATE_ACL_ATTR:
370 lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
371 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
372 xva.xva_vattr.va_mask |= ATTR_XVATTR;
373 zfs_replay_xvattr(lrattr, &xva);
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);
389 #if defined(__linux__)
390 error = zfs_create(dzp, name, &xva.xva_vattr,
391 0, 0, &zp, kcred, vflg, &vsec, kcred->user_ns);
393 error = zfs_create(dzp, name, &xva.xva_vattr,
394 0, 0, &zp, kcred, vflg, &vsec, NULL);
398 aclstart = (caddr_t)(lracl + 1);
399 fuidstart = (caddr_t)aclstart +
400 ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
401 zfsvfs->z_fuid_replay = zfs_replay_fuids(fuidstart,
402 (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
403 lr->lr_uid, lr->lr_gid);
405 case TX_MKDIR_ACL_ATTR:
407 lrattr = (lr_attr_t *)(caddr_t)(lracl + 1);
408 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
409 zfs_replay_xvattr(lrattr, &xva);
411 vsec.vsa_mask = VSA_ACE | VSA_ACE_ACLFLAGS;
412 vsec.vsa_aclentp = (caddr_t)(lracl + 1) + xvatlen;
413 vsec.vsa_aclcnt = lracl->lr_aclcnt;
414 vsec.vsa_aclentsz = lracl->lr_acl_bytes;
415 vsec.vsa_aclflags = lracl->lr_acl_flags;
416 if (zfsvfs->z_fuid_replay == NULL) {
417 fuidstart = (caddr_t)(lracl + 1) + xvatlen +
418 ZIL_ACE_LENGTH(lracl->lr_acl_bytes);
419 zfsvfs->z_fuid_replay =
420 zfs_replay_fuids(fuidstart,
421 (void *)&name, lracl->lr_fuidcnt, lracl->lr_domcnt,
422 lr->lr_uid, lr->lr_gid);
424 #if defined(__linux__)
425 error = zfs_mkdir(dzp, name, &xva.xva_vattr,
426 &zp, kcred, vflg, &vsec, kcred->user_ns);
428 error = zfs_mkdir(dzp, name, &xva.xva_vattr,
429 &zp, kcred, vflg, &vsec, NULL);
433 error = SET_ERROR(ENOTSUP);
437 if (error == 0 && zp != NULL) {
439 VOP_UNLOCK1(ZTOV(zp));
445 if (zfsvfs->z_fuid_replay)
446 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
447 zfsvfs->z_fuid_replay = NULL;
453 zfs_replay_create(void *arg1, void *arg2, boolean_t byteswap)
455 zfsvfs_t *zfsvfs = arg1;
456 lr_create_t *lr = arg2;
457 char *name = NULL; /* location determined later */
458 char *link; /* symlink content follows name */
463 size_t lrsize = sizeof (lr_create_t);
472 txtype = (lr->lr_common.lrc_txtype & ~TX_CI);
474 byteswap_uint64_array(lr, sizeof (*lr));
475 if (txtype == TX_CREATE_ATTR || txtype == TX_MKDIR_ATTR)
476 zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
480 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
483 objid = LR_FOID_GET_OBJ(lr->lr_foid);
484 dnodesize = LR_FOID_GET_SLOTS(lr->lr_foid) << DNODE_SHIFT;
487 zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
488 lr->lr_mode, lr->lr_uid, lr->lr_gid, lr->lr_rdev, objid);
491 * All forms of zfs create (create, mkdir, mkxattrdir, symlink)
492 * eventually end up in zfs_mknode(), which assigns the object's
493 * creation time, generation number, and dnode slot count. The
494 * generic zfs_create() has no concept of these attributes, so
495 * we smuggle the values inside the vattr's otherwise unused
496 * va_ctime, va_nblocks, and va_fsid fields.
498 ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_crtime);
499 xva.xva_vattr.va_nblocks = lr->lr_gen;
500 xva.xva_vattr.va_fsid = dnodesize;
502 error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT);
506 if (lr->lr_common.lrc_txtype & TX_CI)
510 * Symlinks don't have fuid info, and CIFS never creates
513 * The _ATTR versions will grab the fuid info in their subcases.
515 if ((int)lr->lr_common.lrc_txtype != TX_SYMLINK &&
516 (int)lr->lr_common.lrc_txtype != TX_MKDIR_ATTR &&
517 (int)lr->lr_common.lrc_txtype != TX_CREATE_ATTR) {
519 zfsvfs->z_fuid_replay =
520 zfs_replay_fuid_domain(start, &start,
521 lr->lr_uid, lr->lr_gid);
526 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
527 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
528 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
529 start = (caddr_t)(lr + 1) + xvatlen;
530 zfsvfs->z_fuid_replay =
531 zfs_replay_fuid_domain(start, &start,
532 lr->lr_uid, lr->lr_gid);
533 name = (char *)start;
538 name = (char *)start;
540 #if defined(__linux__)
541 error = zfs_create(dzp, name, &xva.xva_vattr,
542 0, 0, &zp, kcred, vflg, NULL, kcred->user_ns);
544 error = zfs_create(dzp, name, &xva.xva_vattr,
545 0, 0, &zp, kcred, vflg, NULL, NULL);
549 lrattr = (lr_attr_t *)(caddr_t)(lr + 1);
550 xvatlen = ZIL_XVAT_SIZE(lrattr->lr_attr_masksize);
551 zfs_replay_xvattr((lr_attr_t *)((caddr_t)lr + lrsize), &xva);
552 start = (caddr_t)(lr + 1) + xvatlen;
553 zfsvfs->z_fuid_replay =
554 zfs_replay_fuid_domain(start, &start,
555 lr->lr_uid, lr->lr_gid);
556 name = (char *)start;
561 name = (char *)(lr + 1);
563 #if defined(__linux__)
564 error = zfs_mkdir(dzp, name, &xva.xva_vattr,
565 &zp, kcred, vflg, NULL, kcred->user_ns);
567 error = zfs_mkdir(dzp, name, &xva.xva_vattr,
568 &zp, kcred, vflg, NULL, NULL);
573 error = zfs_make_xattrdir(dzp, &xva.xva_vattr, &zp, kcred);
576 name = (char *)(lr + 1);
577 link = name + strlen(name) + 1;
578 #if defined(__linux__)
579 error = zfs_symlink(dzp, name, &xva.xva_vattr,
580 link, &zp, kcred, vflg, kcred->user_ns);
582 error = zfs_symlink(dzp, name, &xva.xva_vattr,
583 link, &zp, kcred, vflg, NULL);
587 error = SET_ERROR(ENOTSUP);
591 if (error == 0 && zp != NULL) {
593 VOP_UNLOCK1(ZTOV(zp));
599 if (zfsvfs->z_fuid_replay)
600 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
601 zfsvfs->z_fuid_replay = NULL;
606 zfs_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
608 zfsvfs_t *zfsvfs = arg1;
609 lr_remove_t *lr = arg2;
610 char *name = (char *)(lr + 1); /* name follows lr_remove_t */
616 byteswap_uint64_array(lr, sizeof (*lr));
618 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
621 if (lr->lr_common.lrc_txtype & TX_CI)
624 switch ((int)lr->lr_common.lrc_txtype) {
626 error = zfs_remove(dzp, name, kcred, vflg);
629 error = zfs_rmdir(dzp, name, NULL, kcred, vflg);
632 error = SET_ERROR(ENOTSUP);
641 zfs_replay_link(void *arg1, void *arg2, boolean_t byteswap)
643 zfsvfs_t *zfsvfs = arg1;
644 lr_link_t *lr = arg2;
645 char *name = (char *)(lr + 1); /* name follows lr_link_t */
651 byteswap_uint64_array(lr, sizeof (*lr));
653 if ((error = zfs_zget(zfsvfs, lr->lr_doid, &dzp)) != 0)
656 if ((error = zfs_zget(zfsvfs, lr->lr_link_obj, &zp)) != 0) {
661 if (lr->lr_common.lrc_txtype & TX_CI)
664 error = zfs_link(dzp, zp, name, kcred, vflg);
672 do_zfs_replay_rename(zfsvfs_t *zfsvfs, lr_rename_t *lr, char *sname,
673 char *tname, uint64_t rflags, vattr_t *wo_vap)
675 znode_t *sdzp, *tdzp;
678 /* Only Linux currently supports RENAME_* flags. */
680 VERIFY0(rflags & ~(RENAME_EXCHANGE | RENAME_WHITEOUT));
682 /* wo_vap must be non-NULL iff. we're doing RENAME_WHITEOUT */
683 VERIFY_EQUIV(rflags & RENAME_WHITEOUT, wo_vap != NULL);
688 if ((error = zfs_zget(zfsvfs, lr->lr_sdoid, &sdzp)) != 0)
691 if ((error = zfs_zget(zfsvfs, lr->lr_tdoid, &tdzp)) != 0) {
696 if (lr->lr_common.lrc_txtype & TX_CI)
699 #if defined(__linux__)
700 error = zfs_rename(sdzp, sname, tdzp, tname, kcred, vflg, rflags,
701 wo_vap, kcred->user_ns);
703 error = zfs_rename(sdzp, sname, tdzp, tname, kcred, vflg, rflags,
713 zfs_replay_rename(void *arg1, void *arg2, boolean_t byteswap)
715 zfsvfs_t *zfsvfs = arg1;
716 lr_rename_t *lr = arg2;
717 char *sname = (char *)(lr + 1); /* sname and tname follow lr_rename_t */
718 char *tname = sname + strlen(sname) + 1;
721 byteswap_uint64_array(lr, sizeof (*lr));
723 return (do_zfs_replay_rename(zfsvfs, lr, sname, tname, 0, NULL));
727 zfs_replay_rename_exchange(void *arg1, void *arg2, boolean_t byteswap)
730 zfsvfs_t *zfsvfs = arg1;
731 lr_rename_t *lr = arg2;
732 char *sname = (char *)(lr + 1); /* sname and tname follow lr_rename_t */
733 char *tname = sname + strlen(sname) + 1;
736 byteswap_uint64_array(lr, sizeof (*lr));
738 return (do_zfs_replay_rename(zfsvfs, lr, sname, tname, RENAME_EXCHANGE,
741 return (SET_ERROR(ENOTSUP));
746 zfs_replay_rename_whiteout(void *arg1, void *arg2, boolean_t byteswap)
749 zfsvfs_t *zfsvfs = arg1;
750 lr_rename_whiteout_t *lr = arg2;
752 /* sname and tname follow lr_rename_whiteout_t */
753 char *sname = (char *)(lr + 1);
754 char *tname = sname + strlen(sname) + 1;
755 /* For the whiteout file. */
761 byteswap_uint64_array(lr, sizeof (*lr));
763 objid = LR_FOID_GET_OBJ(lr->lr_wfoid);
764 dnodesize = LR_FOID_GET_SLOTS(lr->lr_wfoid) << DNODE_SHIFT;
767 zfs_init_vattr(&xva.xva_vattr, ATTR_MODE | ATTR_UID | ATTR_GID,
768 lr->lr_wmode, lr->lr_wuid, lr->lr_wgid, lr->lr_wrdev, objid);
771 * As with TX_CREATE, RENAME_WHITEOUT ends up in zfs_mknode(), which
772 * assigns the object's creation time, generation number, and dnode
773 * slot count. The generic zfs_rename() has no concept of these
774 * attributes, so we smuggle the values inside the vattr's otherwise
775 * unused va_ctime, va_nblocks, and va_fsid fields.
777 ZFS_TIME_DECODE(&xva.xva_vattr.va_ctime, lr->lr_wcrtime);
778 xva.xva_vattr.va_nblocks = lr->lr_wgen;
779 xva.xva_vattr.va_fsid = dnodesize;
781 error = dnode_try_claim(zfsvfs->z_os, objid, dnodesize >> DNODE_SHIFT);
785 return (do_zfs_replay_rename(zfsvfs, &lr->lr_rename, sname, tname,
786 RENAME_WHITEOUT, &xva.xva_vattr));
788 return (SET_ERROR(ENOTSUP));
793 zfs_replay_write(void *arg1, void *arg2, boolean_t byteswap)
795 zfsvfs_t *zfsvfs = arg1;
796 lr_write_t *lr = arg2;
797 char *data = (char *)(lr + 1); /* data follows lr_write_t */
800 uint64_t eod, offset, length;
803 byteswap_uint64_array(lr, sizeof (*lr));
805 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0) {
807 * As we can log writes out of order, it's possible the
808 * file has been removed. In this case just drop the write
809 * and return success.
816 offset = lr->lr_offset;
817 length = lr->lr_length;
818 eod = offset + length; /* end of data for this write */
821 * This may be a write from a dmu_sync() for a whole block,
822 * and may extend beyond the current end of the file.
823 * We can't just replay what was written for this TX_WRITE as
824 * a future TX_WRITE2 may extend the eof and the data for that
825 * write needs to be there. So we write the whole block and
826 * reduce the eof. This needs to be done within the single dmu
827 * transaction created within vn_rdwr -> zfs_write. So a possible
828 * new end of file is passed through in zfsvfs->z_replay_eof
831 zfsvfs->z_replay_eof = 0; /* 0 means don't change end of file */
833 /* If it's a dmu_sync() block, write the whole block */
834 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
835 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
836 if (length < blocksize) {
837 offset -= offset % blocksize;
840 if (zp->z_size < eod)
841 zfsvfs->z_replay_eof = eod;
843 error = zfs_write_simple(zp, data, length, offset, NULL);
845 zfsvfs->z_replay_eof = 0; /* safety */
851 * TX_WRITE2 are only generated when dmu_sync() returns EALREADY
852 * meaning the pool block is already being synced. So now that we always write
853 * out full blocks, all we have to do is expand the eof if
857 zfs_replay_write2(void *arg1, void *arg2, boolean_t byteswap)
859 zfsvfs_t *zfsvfs = arg1;
860 lr_write_t *lr = arg2;
866 byteswap_uint64_array(lr, sizeof (*lr));
868 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
872 end = lr->lr_offset + lr->lr_length;
873 if (end > zp->z_size) {
874 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
877 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
878 error = dmu_tx_assign(tx, TXG_WAIT);
881 if (error == ERESTART) {
889 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
890 (void *)&zp->z_size, sizeof (uint64_t), tx);
892 /* Ensure the replayed seq is updated */
893 (void) zil_replaying(zfsvfs->z_log, tx);
904 zfs_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
906 zfsvfs_t *zfsvfs = arg1;
907 lr_truncate_t *lr = arg2;
913 byteswap_uint64_array(lr, sizeof (*lr));
915 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
919 fl.l_whence = SEEK_SET;
920 fl.l_start = lr->lr_offset;
921 fl.l_len = lr->lr_length;
923 error = zfs_space(zp, F_FREESP, &fl, O_RDWR | O_LARGEFILE,
924 lr->lr_offset, kcred);
932 zfs_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
934 zfsvfs_t *zfsvfs = arg1;
935 lr_setattr_t *lr = arg2;
938 vattr_t *vap = &xva.xva_vattr;
944 byteswap_uint64_array(lr, sizeof (*lr));
946 if ((lr->lr_mask & ATTR_XVATTR) &&
947 zfsvfs->z_version >= ZPL_VERSION_INITIAL)
948 zfs_replay_swap_attrs((lr_attr_t *)(lr + 1));
951 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
954 zfs_init_vattr(vap, lr->lr_mask, lr->lr_mode,
955 lr->lr_uid, lr->lr_gid, 0, lr->lr_foid);
957 vap->va_size = lr->lr_size;
958 ZFS_TIME_DECODE(&vap->va_atime, lr->lr_atime);
959 ZFS_TIME_DECODE(&vap->va_mtime, lr->lr_mtime);
960 gethrestime(&vap->va_ctime);
961 vap->va_mask |= ATTR_CTIME;
964 * Fill in xvattr_t portions if necessary.
967 start = (lr_setattr_t *)(lr + 1);
968 if (vap->va_mask & ATTR_XVATTR) {
969 zfs_replay_xvattr((lr_attr_t *)start, &xva);
970 start = (caddr_t)start +
971 ZIL_XVAT_SIZE(((lr_attr_t *)start)->lr_attr_masksize);
973 xva.xva_vattr.va_mask &= ~ATTR_XVATTR;
975 zfsvfs->z_fuid_replay = zfs_replay_fuid_domain(start, &start,
976 lr->lr_uid, lr->lr_gid);
978 #if defined(__linux__)
979 error = zfs_setattr(zp, vap, 0, kcred, kcred->user_ns);
981 error = zfs_setattr(zp, vap, 0, kcred, NULL);
984 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
985 zfsvfs->z_fuid_replay = NULL;
992 zfs_replay_setsaxattr(void *arg1, void *arg2, boolean_t byteswap)
994 zfsvfs_t *zfsvfs = arg1;
995 lr_setsaxattr_t *lr = arg2;
1004 ASSERT(spa_feature_is_active(zfsvfs->z_os->os_spa,
1005 SPA_FEATURE_ZILSAXATTR));
1007 byteswap_uint64_array(lr, sizeof (*lr));
1009 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1012 rw_enter(&zp->z_xattr_lock, RW_WRITER);
1013 mutex_enter(&zp->z_lock);
1014 if (zp->z_xattr_cached == NULL)
1015 error = zfs_sa_get_xattr(zp);
1016 mutex_exit(&zp->z_lock);
1021 ASSERT(zp->z_xattr_cached);
1022 nvl = zp->z_xattr_cached;
1024 /* Get xattr name, value and size from log record */
1026 name = (char *)(lr + 1);
1029 error = nvlist_remove(nvl, name, DATA_TYPE_BYTE_ARRAY);
1031 value = name + strlen(name) + 1;
1032 /* Limited to 32k to keep nvpair memory allocations small */
1033 if (size > DXATTR_MAX_ENTRY_SIZE) {
1034 error = SET_ERROR(EFBIG);
1038 /* Prevent the DXATTR SA from consuming the entire SA region */
1039 error = nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
1043 if (sa_size > DXATTR_MAX_SA_SIZE) {
1044 error = SET_ERROR(EFBIG);
1048 error = nvlist_add_byte_array(nvl, name, (uchar_t *)value,
1053 * Update the SA for additions, modifications, and removals. On
1054 * error drop the inconsistent cached version of the nvlist, it
1055 * will be reconstructed from the ARC when next accessed.
1058 error = zfs_sa_set_xattr(zp, name, value, size);
1062 zp->z_xattr_cached = NULL;
1066 rw_exit(&zp->z_xattr_lock);
1072 zfs_replay_acl_v0(void *arg1, void *arg2, boolean_t byteswap)
1074 zfsvfs_t *zfsvfs = arg1;
1075 lr_acl_v0_t *lr = arg2;
1076 ace_t *ace = (ace_t *)(lr + 1); /* ace array follows lr_acl_t */
1077 vsecattr_t vsa = {0};
1082 byteswap_uint64_array(lr, sizeof (*lr));
1083 zfs_oldace_byteswap(ace, lr->lr_aclcnt);
1086 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1089 vsa.vsa_mask = VSA_ACE | VSA_ACECNT;
1090 vsa.vsa_aclcnt = lr->lr_aclcnt;
1091 vsa.vsa_aclentsz = sizeof (ace_t) * vsa.vsa_aclcnt;
1092 vsa.vsa_aclflags = 0;
1093 vsa.vsa_aclentp = ace;
1095 error = zfs_setsecattr(zp, &vsa, 0, kcred);
1103 * Replaying ACLs is complicated by FUID support.
1104 * The log record may contain some optional data
1105 * to be used for replaying FUID's. These pieces
1106 * are the actual FUIDs that were created initially.
1107 * The FUID table index may no longer be valid and
1108 * during zfs_create() a new index may be assigned.
1109 * Because of this the log will contain the original
1110 * domain+rid in order to create a new FUID.
1112 * The individual ACEs may contain an ephemeral uid/gid which is no
1113 * longer valid and will need to be replaced with an actual FUID.
1117 zfs_replay_acl(void *arg1, void *arg2, boolean_t byteswap)
1119 zfsvfs_t *zfsvfs = arg1;
1120 lr_acl_t *lr = arg2;
1121 ace_t *ace = (ace_t *)(lr + 1);
1122 vsecattr_t vsa = {0};
1127 byteswap_uint64_array(lr, sizeof (*lr));
1128 zfs_ace_byteswap(ace, lr->lr_acl_bytes, B_FALSE);
1129 if (lr->lr_fuidcnt) {
1130 byteswap_uint64_array((caddr_t)ace +
1131 ZIL_ACE_LENGTH(lr->lr_acl_bytes),
1132 lr->lr_fuidcnt * sizeof (uint64_t));
1136 if ((error = zfs_zget(zfsvfs, lr->lr_foid, &zp)) != 0)
1139 vsa.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1140 vsa.vsa_aclcnt = lr->lr_aclcnt;
1141 vsa.vsa_aclentp = ace;
1142 vsa.vsa_aclentsz = lr->lr_acl_bytes;
1143 vsa.vsa_aclflags = lr->lr_acl_flags;
1145 if (lr->lr_fuidcnt) {
1146 void *fuidstart = (caddr_t)ace +
1147 ZIL_ACE_LENGTH(lr->lr_acl_bytes);
1149 zfsvfs->z_fuid_replay =
1150 zfs_replay_fuids(fuidstart, &fuidstart,
1151 lr->lr_fuidcnt, lr->lr_domcnt, 0, 0);
1154 error = zfs_setsecattr(zp, &vsa, 0, kcred);
1156 if (zfsvfs->z_fuid_replay)
1157 zfs_fuid_info_free(zfsvfs->z_fuid_replay);
1159 zfsvfs->z_fuid_replay = NULL;
1166 * Callback vectors for replaying records
1168 zil_replay_func_t *const zfs_replay_vector[TX_MAX_TYPE] = {
1169 zfs_replay_error, /* no such type */
1170 zfs_replay_create, /* TX_CREATE */
1171 zfs_replay_create, /* TX_MKDIR */
1172 zfs_replay_create, /* TX_MKXATTR */
1173 zfs_replay_create, /* TX_SYMLINK */
1174 zfs_replay_remove, /* TX_REMOVE */
1175 zfs_replay_remove, /* TX_RMDIR */
1176 zfs_replay_link, /* TX_LINK */
1177 zfs_replay_rename, /* TX_RENAME */
1178 zfs_replay_write, /* TX_WRITE */
1179 zfs_replay_truncate, /* TX_TRUNCATE */
1180 zfs_replay_setattr, /* TX_SETATTR */
1181 zfs_replay_acl_v0, /* TX_ACL_V0 */
1182 zfs_replay_acl, /* TX_ACL */
1183 zfs_replay_create_acl, /* TX_CREATE_ACL */
1184 zfs_replay_create, /* TX_CREATE_ATTR */
1185 zfs_replay_create_acl, /* TX_CREATE_ACL_ATTR */
1186 zfs_replay_create_acl, /* TX_MKDIR_ACL */
1187 zfs_replay_create, /* TX_MKDIR_ATTR */
1188 zfs_replay_create_acl, /* TX_MKDIR_ACL_ATTR */
1189 zfs_replay_write2, /* TX_WRITE2 */
1190 zfs_replay_setsaxattr, /* TX_SETSAXATTR */
1191 zfs_replay_rename_exchange, /* TX_RENAME_EXCHANGE */
1192 zfs_replay_rename_whiteout, /* TX_RENAME_WHITEOUT */