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) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
28 * ZFS control directory (a.k.a. ".zfs")
30 * This directory provides a common location for all ZFS meta-objects.
31 * Currently, this is only the 'snapshot' directory, but this may expand in the
32 * future. The elements are built using the GFS primitives, as the hierarchy
33 * does not actually exist on disk.
35 * For 'snapshot', we don't want to have all snapshots always mounted, because
36 * this would take up a huge amount of space in /etc/mnttab. We have three
39 * ctldir ------> snapshotdir -------> snapshot
45 * The 'snapshot' node contains just enough information to lookup '..' and act
46 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
47 * perform an automount of the underlying filesystem and return the
48 * corresponding vnode.
50 * All mounts are handled automatically by the kernel, but unmounts are
51 * (currently) handled from user land. The main reason is that there is no
52 * reliable way to auto-unmount the filesystem when it's "no longer in use".
53 * When the user unmounts a filesystem, we call zfsctl_unmount(), which
54 * unmounts any snapshots within the snapshot directory.
56 * The '.zfs', '.zfs/snapshot', and all directories created under
57 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
58 * share the same vfs_t as the head filesystem (what '.zfs' lives under).
60 * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
61 * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
62 * However, vnodes within these mounted on file systems have their v_vfsp
63 * fields set to the head filesystem to make NFS happy (see
64 * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
65 * so that it cannot be freed until all snapshots have been unmounted.
68 #include <sys/zfs_context.h>
69 #include <sys/zfs_ctldir.h>
70 #include <sys/zfs_ioctl.h>
71 #include <sys/zfs_vfsops.h>
72 #include <sys/namei.h>
75 #include <sys/dsl_dataset.h>
76 #include <sys/dsl_destroy.h>
77 #include <sys/dsl_deleg.h>
78 #include <sys/mount.h>
81 #include "zfs_namecheck.h"
84 * "Synthetic" filesystem implementation.
88 * Assert that A implies B.
90 #define KASSERT_IMPLY(A, B, msg) KASSERT(!(A) || (B), (msg));
92 static MALLOC_DEFINE(M_SFSNODES, "sfs_nodes", "synthetic-fs nodes");
94 typedef struct sfs_node {
95 char sn_name[ZFS_MAX_DATASET_NAME_LEN];
96 uint64_t sn_parent_id;
101 * Check the parent's ID as well as the node's to account for a chance
102 * that IDs originating from different domains (snapshot IDs, artifical
103 * IDs, znode IDs) may clash.
106 sfs_compare_ids(struct vnode *vp, void *arg)
108 sfs_node_t *n1 = vp->v_data;
109 sfs_node_t *n2 = arg;
112 equal = n1->sn_id == n2->sn_id &&
113 n1->sn_parent_id == n2->sn_parent_id;
115 /* Zero means equality. */
120 sfs_vnode_get(const struct mount *mp, int flags, uint64_t parent_id,
121 uint64_t id, struct vnode **vpp)
127 search.sn_parent_id = parent_id;
128 err = vfs_hash_get(mp, (u_int)id, flags, curthread, vpp,
129 sfs_compare_ids, &search);
134 sfs_vnode_insert(struct vnode *vp, int flags, uint64_t parent_id,
135 uint64_t id, struct vnode **vpp)
139 KASSERT(vp->v_data != NULL, ("sfs_vnode_insert with NULL v_data"));
140 err = vfs_hash_insert(vp, (u_int)id, flags, curthread, vpp,
141 sfs_compare_ids, vp->v_data);
146 sfs_vnode_remove(struct vnode *vp)
151 typedef void sfs_vnode_setup_fn(vnode_t *vp, void *arg);
154 sfs_vgetx(struct mount *mp, int flags, uint64_t parent_id, uint64_t id,
155 const char *tag, struct vop_vector *vops,
156 sfs_vnode_setup_fn setup, void *arg,
162 error = sfs_vnode_get(mp, flags, parent_id, id, vpp);
163 if (error != 0 || *vpp != NULL) {
164 KASSERT_IMPLY(error == 0, (*vpp)->v_data != NULL,
165 "sfs vnode with no data");
169 /* Allocate a new vnode/inode. */
170 error = getnewvnode(tag, mp, vops, &vp);
177 * Exclusively lock the vnode vnode while it's being constructed.
179 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
180 error = insmntque(vp, mp);
188 error = sfs_vnode_insert(vp, flags, parent_id, id, vpp);
189 if (error != 0 || *vpp != NULL) {
190 KASSERT_IMPLY(error == 0, (*vpp)->v_data != NULL,
191 "sfs vnode with no data");
200 sfs_print_node(sfs_node_t *node)
202 printf("\tname = %s\n", node->sn_name);
203 printf("\tparent_id = %ju\n", (uintmax_t)node->sn_parent_id);
204 printf("\tid = %ju\n", (uintmax_t)node->sn_id);
208 sfs_alloc_node(size_t size, const char *name, uint64_t parent_id, uint64_t id)
210 struct sfs_node *node;
212 KASSERT(strlen(name) < sizeof(node->sn_name),
213 ("sfs node name is too long"));
214 KASSERT(size >= sizeof(*node), ("sfs node size is too small"));
215 node = malloc(size, M_SFSNODES, M_WAITOK | M_ZERO);
216 strlcpy(node->sn_name, name, sizeof(node->sn_name));
217 node->sn_parent_id = parent_id;
224 sfs_destroy_node(sfs_node_t *node)
226 free(node, M_SFSNODES);
230 sfs_reclaim_vnode(vnode_t *vp)
235 sfs_vnode_remove(vp);
242 sfs_readdir_common(uint64_t parent_id, uint64_t id, struct vop_readdir_args *ap,
243 uio_t *uio, off_t *offp)
248 /* Reset ncookies for subsequent use of vfs_read_dirent. */
249 if (ap->a_ncookies != NULL)
252 if (uio->uio_resid < sizeof(entry))
253 return (SET_ERROR(EINVAL));
255 if (uio->uio_offset < 0)
256 return (SET_ERROR(EINVAL));
257 if (uio->uio_offset == 0) {
259 entry.d_type = DT_DIR;
260 entry.d_name[0] = '.';
261 entry.d_name[1] = '\0';
263 entry.d_reclen = sizeof(entry);
264 error = vfs_read_dirent(ap, &entry, uio->uio_offset);
266 return (SET_ERROR(error));
269 if (uio->uio_offset < sizeof(entry))
270 return (SET_ERROR(EINVAL));
271 if (uio->uio_offset == sizeof(entry)) {
272 entry.d_fileno = parent_id;
273 entry.d_type = DT_DIR;
274 entry.d_name[0] = '.';
275 entry.d_name[1] = '.';
276 entry.d_name[2] = '\0';
278 entry.d_reclen = sizeof(entry);
279 error = vfs_read_dirent(ap, &entry, uio->uio_offset);
281 return (SET_ERROR(error));
285 *offp = 2 * sizeof(entry);
291 * .zfs inode namespace
293 * We need to generate unique inode numbers for all files and directories
294 * within the .zfs pseudo-filesystem. We use the following scheme:
299 * .zfs/snapshot/<snap> objectid(snap)
301 #define ZFSCTL_INO_SNAP(id) (id)
303 static struct vop_vector zfsctl_ops_root;
304 static struct vop_vector zfsctl_ops_snapdir;
305 static struct vop_vector zfsctl_ops_snapshot;
306 static struct vop_vector zfsctl_ops_shares_dir;
319 zfsctl_is_node(vnode_t *vp)
321 return (vn_matchops(vp, zfsctl_ops_root) ||
322 vn_matchops(vp, zfsctl_ops_snapdir) ||
323 vn_matchops(vp, zfsctl_ops_snapshot) ||
324 vn_matchops(vp, zfsctl_ops_shares_dir));
328 typedef struct zfsctl_root {
336 * Create the '.zfs' directory.
339 zfsctl_create(zfsvfs_t *zfsvfs)
341 zfsctl_root_t *dot_zfs;
346 ASSERT(zfsvfs->z_ctldir == NULL);
348 snapdir = sfs_alloc_node(sizeof(*snapdir), "snapshot", ZFSCTL_INO_ROOT,
350 dot_zfs = (zfsctl_root_t *)sfs_alloc_node(sizeof(*dot_zfs), ".zfs", 0,
352 dot_zfs->snapdir = snapdir;
354 VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp) == 0);
355 VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
356 &crtime, sizeof(crtime)));
357 ZFS_TIME_DECODE(&dot_zfs->cmtime, crtime);
360 zfsvfs->z_ctldir = dot_zfs;
364 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
365 * The nodes must not have any associated vnodes by now as they should be
369 zfsctl_destroy(zfsvfs_t *zfsvfs)
371 sfs_destroy_node(zfsvfs->z_ctldir->snapdir);
372 sfs_destroy_node((sfs_node_t *)zfsvfs->z_ctldir);
373 zfsvfs->z_ctldir = NULL;
377 zfsctl_fs_root_vnode(struct mount *mp, void *arg __unused, int flags,
380 return (VFS_ROOT(mp, flags, vpp));
384 zfsctl_common_vnode_setup(vnode_t *vp, void *arg)
386 ASSERT_VOP_ELOCKED(vp, __func__);
388 /* We support shared locking. */
395 zfsctl_root_vnode(struct mount *mp, void *arg __unused, int flags,
401 node = ((zfsvfs_t*)mp->mnt_data)->z_ctldir;
402 err = sfs_vgetx(mp, flags, 0, ZFSCTL_INO_ROOT, "zfs", &zfsctl_ops_root,
403 zfsctl_common_vnode_setup, node, vpp);
408 zfsctl_snapdir_vnode(struct mount *mp, void *arg __unused, int flags,
414 node = ((zfsvfs_t*)mp->mnt_data)->z_ctldir->snapdir;
415 err = sfs_vgetx(mp, flags, ZFSCTL_INO_ROOT, ZFSCTL_INO_SNAPDIR, "zfs",
416 &zfsctl_ops_snapdir, zfsctl_common_vnode_setup, node, vpp);
421 * Given a root znode, retrieve the associated .zfs directory.
422 * Add a hold to the vnode and return it.
425 zfsctl_root(zfsvfs_t *zfsvfs, int flags, vnode_t **vpp)
430 error = zfsctl_root_vnode(zfsvfs->z_vfs, NULL, flags, vpp);
435 * Common open routine. Disallow any write access.
439 zfsctl_common_open(struct vop_open_args *ap)
441 int flags = ap->a_mode;
444 return (SET_ERROR(EACCES));
450 * Common close routine. Nothing to do here.
454 zfsctl_common_close(struct vop_close_args *ap)
460 * Common access routine. Disallow writes.
464 zfsctl_common_access(ap)
465 struct vop_access_args /* {
468 struct ucred *a_cred;
472 accmode_t accmode = ap->a_accmode;
474 if (accmode & VWRITE)
475 return (SET_ERROR(EACCES));
480 * Common getattr function. Fill in basic information.
483 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
494 * We are a purely virtual object, so we have no
495 * blocksize or allocated blocks.
500 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
501 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
505 * We live in the now (for atime).
509 /* FreeBSD: Reset chflags(2) flags. */
512 vap->va_nodeid = node->sn_id;
514 /* At least '.' and '..'. */
520 zfsctl_common_fid(ap)
521 struct vop_fid_args /* {
526 vnode_t *vp = ap->a_vp;
527 fid_t *fidp = (void *)ap->a_fid;
528 sfs_node_t *node = vp->v_data;
529 uint64_t object = node->sn_id;
533 zfid = (zfid_short_t *)fidp;
534 zfid->zf_len = SHORT_FID_LEN;
536 for (i = 0; i < sizeof(zfid->zf_object); i++)
537 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
539 /* .zfs nodes always have a generation number of 0 */
540 for (i = 0; i < sizeof(zfid->zf_gen); i++)
547 zfsctl_common_reclaim(ap)
548 struct vop_reclaim_args /* {
553 vnode_t *vp = ap->a_vp;
555 (void) sfs_reclaim_vnode(vp);
560 zfsctl_common_print(ap)
561 struct vop_print_args /* {
565 sfs_print_node(ap->a_vp->v_data);
570 * Get root directory attributes.
574 zfsctl_root_getattr(ap)
575 struct vop_getattr_args /* {
578 struct ucred *a_cred;
581 struct vnode *vp = ap->a_vp;
582 struct vattr *vap = ap->a_vap;
583 zfsctl_root_t *node = vp->v_data;
585 zfsctl_common_getattr(vp, vap);
586 vap->va_ctime = node->cmtime;
587 vap->va_mtime = vap->va_ctime;
588 vap->va_birthtime = vap->va_ctime;
589 vap->va_nlink += 1; /* snapdir */
590 vap->va_size = vap->va_nlink;
595 * When we lookup "." we still can be asked to lock it
596 * differently, can't we?
599 zfsctl_relock_dot(vnode_t *dvp, int ltype)
602 if (ltype != VOP_ISLOCKED(dvp)) {
603 if (ltype == LK_EXCLUSIVE)
604 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
605 else /* if (ltype == LK_SHARED) */
606 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
608 /* Relock for the "." case may left us with reclaimed vnode. */
609 if ((dvp->v_iflag & VI_DOOMED) != 0) {
611 return (SET_ERROR(ENOENT));
618 * Special case the handling of "..".
621 zfsctl_root_lookup(ap)
622 struct vop_lookup_args /* {
624 struct vnode **a_vpp;
625 struct componentname *a_cnp;
628 struct componentname *cnp = ap->a_cnp;
629 vnode_t *dvp = ap->a_dvp;
630 vnode_t **vpp = ap->a_vpp;
631 cred_t *cr = ap->a_cnp->cn_cred;
632 int flags = ap->a_cnp->cn_flags;
633 int lkflags = ap->a_cnp->cn_lkflags;
634 int nameiop = ap->a_cnp->cn_nameiop;
638 ASSERT(dvp->v_type == VDIR);
640 if ((flags & ISLASTCN) != 0 && nameiop != LOOKUP)
641 return (SET_ERROR(ENOTSUP));
643 if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') {
644 err = zfsctl_relock_dot(dvp, lkflags & LK_TYPE_MASK);
647 } else if ((flags & ISDOTDOT) != 0) {
648 err = vn_vget_ino_gen(dvp, zfsctl_fs_root_vnode, NULL,
650 } else if (strncmp(cnp->cn_nameptr, "snapshot", cnp->cn_namelen) == 0) {
651 err = zfsctl_snapdir_vnode(dvp->v_mount, NULL, lkflags, vpp);
653 err = SET_ERROR(ENOENT);
661 zfsctl_root_readdir(ap)
662 struct vop_readdir_args /* {
665 struct ucred *a_cred;
672 vnode_t *vp = ap->a_vp;
673 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
674 zfsctl_root_t *node = vp->v_data;
675 uio_t *uio = ap->a_uio;
676 int *eofp = ap->a_eofflag;
680 ASSERT(vp->v_type == VDIR);
682 error = sfs_readdir_common(zfsvfs->z_root, ZFSCTL_INO_ROOT, ap, uio,
685 if (error == ENAMETOOLONG) /* ran out of destination space */
689 if (uio->uio_offset != dots_offset)
690 return (SET_ERROR(EINVAL));
692 CTASSERT(sizeof(node->snapdir->sn_name) <= sizeof(entry.d_name));
693 entry.d_fileno = node->snapdir->sn_id;
694 entry.d_type = DT_DIR;
695 strcpy(entry.d_name, node->snapdir->sn_name);
696 entry.d_namlen = strlen(entry.d_name);
697 entry.d_reclen = sizeof(entry);
698 error = vfs_read_dirent(ap, &entry, uio->uio_offset);
700 if (error == ENAMETOOLONG)
702 return (SET_ERROR(error));
709 static struct vop_vector zfsctl_ops_root = {
710 .vop_default = &default_vnodeops,
711 .vop_open = zfsctl_common_open,
712 .vop_close = zfsctl_common_close,
713 .vop_ioctl = VOP_EINVAL,
714 .vop_getattr = zfsctl_root_getattr,
715 .vop_access = zfsctl_common_access,
716 .vop_readdir = zfsctl_root_readdir,
717 .vop_lookup = zfsctl_root_lookup,
718 .vop_inactive = VOP_NULL,
719 .vop_reclaim = zfsctl_common_reclaim,
720 .vop_fid = zfsctl_common_fid,
721 .vop_print = zfsctl_common_print,
725 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
727 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
729 dmu_objset_name(os, zname);
730 if (strlen(zname) + 1 + strlen(name) >= len)
731 return (SET_ERROR(ENAMETOOLONG));
732 (void) strcat(zname, "@");
733 (void) strcat(zname, name);
738 zfsctl_snapshot_lookup(vnode_t *vp, const char *name, uint64_t *id)
740 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
743 err = dsl_dataset_snap_lookup(dmu_objset_ds(os), name, id);
748 * Given a vnode get a root vnode of a filesystem mounted on top of
749 * the vnode, if any. The root vnode is referenced and locked.
750 * If no filesystem is mounted then the orinal vnode remains referenced
751 * and locked. If any error happens the orinal vnode is unlocked and
755 zfsctl_mounted_here(vnode_t **vpp, int flags)
760 ASSERT_VOP_LOCKED(*vpp, __func__);
761 ASSERT3S((*vpp)->v_type, ==, VDIR);
763 if ((mp = (*vpp)->v_mountedhere) != NULL) {
764 err = vfs_busy(mp, 0);
765 KASSERT(err == 0, ("vfs_busy(mp, 0) failed with %d", err));
766 KASSERT(vrefcnt(*vpp) > 1, ("unreferenced mountpoint"));
768 err = VFS_ROOT(mp, flags, vpp);
772 return (EJUSTRETURN);
776 const char *snap_name;
778 } snapshot_setup_arg_t;
781 zfsctl_snapshot_vnode_setup(vnode_t *vp, void *arg)
783 snapshot_setup_arg_t *ssa = arg;
786 ASSERT_VOP_ELOCKED(vp, __func__);
788 node = sfs_alloc_node(sizeof(sfs_node_t),
789 ssa->snap_name, ZFSCTL_INO_SNAPDIR, ssa->snap_id);
790 zfsctl_common_vnode_setup(vp, node);
792 /* We have to support recursive locking. */
797 * Lookup entry point for the 'snapshot' directory. Try to open the
798 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
799 * Perform a mount of the associated dataset on top of the vnode.
803 zfsctl_snapdir_lookup(ap)
804 struct vop_lookup_args /* {
806 struct vnode **a_vpp;
807 struct componentname *a_cnp;
810 vnode_t *dvp = ap->a_dvp;
811 vnode_t **vpp = ap->a_vpp;
812 struct componentname *cnp = ap->a_cnp;
813 char name[NAME_MAX + 1];
814 char fullname[ZFS_MAX_DATASET_NAME_LEN];
816 size_t mountpoint_len;
817 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
819 int nameiop = cnp->cn_nameiop;
820 int lkflags = cnp->cn_lkflags;
821 int flags = cnp->cn_flags;
824 ASSERT(dvp->v_type == VDIR);
826 if ((flags & ISLASTCN) != 0 && nameiop != LOOKUP)
827 return (SET_ERROR(ENOTSUP));
829 if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') {
830 err = zfsctl_relock_dot(dvp, lkflags & LK_TYPE_MASK);
835 if (flags & ISDOTDOT) {
836 err = vn_vget_ino_gen(dvp, zfsctl_root_vnode, NULL, lkflags,
841 if (cnp->cn_namelen >= sizeof(name))
842 return (SET_ERROR(ENAMETOOLONG));
844 strlcpy(name, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
845 err = zfsctl_snapshot_lookup(dvp, name, &snap_id);
847 return (SET_ERROR(ENOENT));
850 snapshot_setup_arg_t ssa;
852 ssa.snap_name = name;
853 ssa.snap_id = snap_id;
854 err = sfs_vgetx(dvp->v_mount, LK_SHARED, ZFSCTL_INO_SNAPDIR,
855 snap_id, "zfs", &zfsctl_ops_snapshot,
856 zfsctl_snapshot_vnode_setup, &ssa, vpp);
860 /* Check if a new vnode has just been created. */
861 if (VOP_ISLOCKED(*vpp) == LK_EXCLUSIVE)
865 * The vnode must be referenced at least by this thread and
866 * the mounted snapshot or the thread doing the mounting.
867 * There can be more references from concurrent lookups.
869 KASSERT(vrefcnt(*vpp) > 1, ("found unreferenced mountpoint"));
872 * Check if a snapshot is already mounted on top of the vnode.
874 err = zfsctl_mounted_here(vpp, lkflags);
875 if (err != EJUSTRETURN)
880 * If the vnode not covered yet, then the mount operation
881 * must be in progress.
884 KASSERT(((*vpp)->v_iflag & VI_MOUNT) != 0,
885 ("snapshot vnode not covered"));
891 * In this situation we can loop on uncontested locks and starve
892 * the thread doing the lengthy, non-trivial mount operation.
894 kern_yield(PRI_USER);
897 VERIFY0(zfsctl_snapshot_zname(dvp, name, sizeof(fullname), fullname));
899 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
900 strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(name) + 1;
901 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
902 (void) snprintf(mountpoint, mountpoint_len,
903 "%s/" ZFS_CTLDIR_NAME "/snapshot/%s",
904 dvp->v_vfsp->mnt_stat.f_mntonname, name);
906 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, fullname, 0);
907 kmem_free(mountpoint, mountpoint_len);
910 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
912 * This is where we lie about our v_vfsp in order to
913 * make .zfs/snapshot/<snapname> accessible over NFS
914 * without requiring manual mounts of <snapname>.
916 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
917 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
919 /* Clear the root flag (set via VFS_ROOT) as well. */
920 (*vpp)->v_vflag &= ~VV_ROOT;
929 zfsctl_snapdir_readdir(ap)
930 struct vop_readdir_args /* {
933 struct ucred *a_cred;
939 char snapname[ZFS_MAX_DATASET_NAME_LEN];
941 vnode_t *vp = ap->a_vp;
942 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
943 uio_t *uio = ap->a_uio;
944 int *eofp = ap->a_eofflag;
948 ASSERT(vp->v_type == VDIR);
950 error = sfs_readdir_common(ZFSCTL_INO_ROOT, ZFSCTL_INO_SNAPDIR, ap, uio,
953 if (error == ENAMETOOLONG) /* ran out of destination space */
962 cookie = uio->uio_offset - dots_offset;
964 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
965 error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof(snapname),
966 snapname, &id, &cookie, NULL);
967 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
969 if (error == ENOENT) {
978 entry.d_type = DT_DIR;
979 strcpy(entry.d_name, snapname);
980 entry.d_namlen = strlen(entry.d_name);
981 entry.d_reclen = sizeof(entry);
982 error = vfs_read_dirent(ap, &entry, uio->uio_offset);
984 if (error == ENAMETOOLONG)
986 return (SET_ERROR(error));
988 uio->uio_offset = cookie + dots_offset;
995 zfsctl_snapdir_getattr(ap)
996 struct vop_getattr_args /* {
999 struct ucred *a_cred;
1002 vnode_t *vp = ap->a_vp;
1003 vattr_t *vap = ap->a_vap;
1004 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1005 dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os);
1006 sfs_node_t *node = vp->v_data;
1007 uint64_t snap_count;
1010 zfsctl_common_getattr(vp, vap);
1011 vap->va_ctime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1012 vap->va_mtime = vap->va_ctime;
1013 vap->va_birthtime = vap->va_ctime;
1014 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) {
1015 err = zap_count(dmu_objset_pool(ds->ds_objset)->dp_meta_objset,
1016 dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count);
1019 vap->va_nlink += snap_count;
1021 vap->va_size = vap->va_nlink;
1026 static struct vop_vector zfsctl_ops_snapdir = {
1027 .vop_default = &default_vnodeops,
1028 .vop_open = zfsctl_common_open,
1029 .vop_close = zfsctl_common_close,
1030 .vop_getattr = zfsctl_snapdir_getattr,
1031 .vop_access = zfsctl_common_access,
1032 .vop_readdir = zfsctl_snapdir_readdir,
1033 .vop_lookup = zfsctl_snapdir_lookup,
1034 .vop_reclaim = zfsctl_common_reclaim,
1035 .vop_fid = zfsctl_common_fid,
1036 .vop_print = zfsctl_common_print,
1040 zfsctl_snapshot_inactive(ap)
1041 struct vop_inactive_args /* {
1043 struct thread *a_td;
1046 vnode_t *vp = ap->a_vp;
1048 VERIFY(vrecycle(vp) == 1);
1053 zfsctl_snapshot_reclaim(ap)
1054 struct vop_reclaim_args /* {
1056 struct thread *a_td;
1059 vnode_t *vp = ap->a_vp;
1060 void *data = vp->v_data;
1062 sfs_reclaim_vnode(vp);
1063 sfs_destroy_node(data);
1068 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1080 len = strlen(node->sn_name);
1081 if (*ap->a_buflen < len)
1082 return (SET_ERROR(ENOMEM));
1085 * Prevent unmounting of the snapshot while the vnode lock
1086 * is not held. That is not strictly required, but allows
1087 * us to assert that an uncovered snapshot vnode is never
1090 mp = vp->v_mountedhere;
1092 return (SET_ERROR(ENOENT));
1093 error = vfs_busy(mp, 0);
1094 KASSERT(error == 0, ("vfs_busy(mp, 0) failed with %d", error));
1097 * We can vput the vnode as we can now depend on the reference owned
1098 * by the busied mp. But we also need to hold the vnode, because
1099 * the reference may go after vfs_unbusy() which has to be called
1100 * before we can lock the vnode again.
1102 locked = VOP_ISLOCKED(vp);
1106 /* Look up .zfs/snapshot, our parent. */
1107 error = zfsctl_snapdir_vnode(vp->v_mount, NULL, LK_SHARED, &dvp);
1111 *ap->a_buflen -= len;
1112 bcopy(node->sn_name, ap->a_buf + *ap->a_buflen, len);
1115 vget(vp, locked | LK_VNHELD | LK_RETRY, curthread);
1120 * These VP's should never see the light of day. They should always
1123 static struct vop_vector zfsctl_ops_snapshot = {
1124 .vop_default = NULL, /* ensure very restricted access */
1125 .vop_inactive = zfsctl_snapshot_inactive,
1126 .vop_reclaim = zfsctl_snapshot_reclaim,
1127 .vop_vptocnp = zfsctl_snapshot_vptocnp,
1128 .vop_lock1 = vop_stdlock,
1129 .vop_unlock = vop_stdunlock,
1130 .vop_islocked = vop_stdislocked,
1131 .vop_advlockpurge = vop_stdadvlockpurge, /* called by vgone */
1132 .vop_print = zfsctl_common_print,
1136 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1139 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1143 ASSERT(zfsvfs->z_ctldir != NULL);
1145 error = sfs_vnode_get(vfsp, LK_EXCLUSIVE,
1146 ZFSCTL_INO_SNAPDIR, objsetid, &vp);
1147 if (error == 0 && vp != NULL) {
1149 * XXX Probably need to at least reference, if not busy, the mp.
1151 if (vp->v_mountedhere != NULL)
1152 *zfsvfsp = vp->v_mountedhere->mnt_data;
1155 if (*zfsvfsp == NULL)
1156 return (SET_ERROR(EINVAL));
1161 * Unmount any snapshots for the given filesystem. This is called from
1162 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1166 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1168 char snapname[ZFS_MAX_DATASET_NAME_LEN];
1169 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1178 ASSERT(zfsvfs->z_ctldir != NULL);
1184 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
1185 error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof(snapname),
1186 snapname, &id, &cookie, NULL);
1187 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
1189 if (error == ENOENT)
1195 error = sfs_vnode_get(vfsp, LK_EXCLUSIVE,
1196 ZFSCTL_INO_SNAPDIR, id, &vp);
1197 if (error != 0 || vp == NULL)
1200 mp = vp->v_mountedhere;
1203 * v_mountedhere being NULL means that the
1204 * (uncovered) vnode is in a transient state
1205 * (mounting or unmounting), so loop until it
1215 continue; /* no mountpoint, nothing to do */
1218 * The mount-point vnode is kept locked to avoid spurious EBUSY
1219 * from a concurrent umount.
1220 * The vnode lock must have recursive locking enabled.
1223 error = dounmount(mp, fflags, curthread);
1224 KASSERT_IMPLY(error == 0, vrefcnt(vp) == 1,
1225 ("extra references after unmount"));
1230 KASSERT_IMPLY((fflags & MS_FORCE) != 0, error == 0,
1231 ("force unmounting failed"));