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 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>
76 #include <sys/dsl_destroy.h>
77 #include <sys/dsl_deleg.h>
78 #include <sys/mount.h>
79 #include <sys/sunddi.h>
81 #include "zfs_namecheck.h"
83 typedef struct zfsctl_node {
84 gfs_dir_t zc_gfs_private;
86 timestruc_t zc_cmtime; /* ctime and mtime, always the same */
89 typedef struct zfsctl_snapdir {
90 zfsctl_node_t sd_node;
102 snapentry_compare(const void *a, const void *b)
104 const zfs_snapentry_t *sa = a;
105 const zfs_snapentry_t *sb = b;
106 int ret = strcmp(sa->se_name, sb->se_name);
117 vnodeops_t *zfsctl_ops_root;
118 vnodeops_t *zfsctl_ops_snapdir;
119 vnodeops_t *zfsctl_ops_snapshot;
120 vnodeops_t *zfsctl_ops_shares;
121 vnodeops_t *zfsctl_ops_shares_dir;
123 static const fs_operation_def_t zfsctl_tops_root[];
124 static const fs_operation_def_t zfsctl_tops_snapdir[];
125 static const fs_operation_def_t zfsctl_tops_snapshot[];
126 static const fs_operation_def_t zfsctl_tops_shares[];
128 static struct vop_vector zfsctl_ops_root;
129 static struct vop_vector zfsctl_ops_snapdir;
130 static struct vop_vector zfsctl_ops_snapshot;
131 static struct vop_vector zfsctl_ops_shares;
132 static struct vop_vector zfsctl_ops_shares_dir;
135 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
136 static vnode_t *zfsctl_mknode_shares(vnode_t *);
137 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
138 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
141 static gfs_opsvec_t zfsctl_opsvec[] = {
142 { ".zfs", zfsctl_tops_root, &zfsctl_ops_root },
143 { ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir },
144 { ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot },
145 { ".zfs/shares", zfsctl_tops_shares, &zfsctl_ops_shares_dir },
146 { ".zfs/shares/vnode", zfsctl_tops_shares, &zfsctl_ops_shares },
152 * Root directory elements. We only have two entries
153 * snapshot and shares.
155 static gfs_dirent_t zfsctl_root_entries[] = {
156 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
157 { "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
161 /* include . and .. in the calculation */
162 #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \
163 sizeof (gfs_dirent_t)) + 1)
167 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
168 * directories. This is called from the ZFS init routine, and initializes the
169 * vnode ops vectors that we'll be using.
175 VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0);
184 * Remove vfsctl vnode ops
187 vn_freevnodeops(zfsctl_ops_root);
188 if (zfsctl_ops_snapdir)
189 vn_freevnodeops(zfsctl_ops_snapdir);
190 if (zfsctl_ops_snapshot)
191 vn_freevnodeops(zfsctl_ops_snapshot);
192 if (zfsctl_ops_shares)
193 vn_freevnodeops(zfsctl_ops_shares);
194 if (zfsctl_ops_shares_dir)
195 vn_freevnodeops(zfsctl_ops_shares_dir);
197 zfsctl_ops_root = NULL;
198 zfsctl_ops_snapdir = NULL;
199 zfsctl_ops_snapshot = NULL;
200 zfsctl_ops_shares = NULL;
201 zfsctl_ops_shares_dir = NULL;
206 zfsctl_is_node(vnode_t *vp)
208 return (vn_matchops(vp, zfsctl_ops_root) ||
209 vn_matchops(vp, zfsctl_ops_snapdir) ||
210 vn_matchops(vp, zfsctl_ops_snapshot) ||
211 vn_matchops(vp, zfsctl_ops_shares) ||
212 vn_matchops(vp, zfsctl_ops_shares_dir));
217 * Return the inode number associated with the 'snapshot' or
218 * 'shares' directory.
222 zfsctl_root_inode_cb(vnode_t *vp, int index)
224 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
229 return (ZFSCTL_INO_SNAPDIR);
231 return (zfsvfs->z_shares_dir);
235 * Create the '.zfs' directory. This directory is cached as part of the VFS
236 * structure. This results in a hold on the vfs_t. The code in zfs_umount()
237 * therefore checks against a vfs_count of 2 instead of 1. This reference
238 * is removed when the ctldir is destroyed in the unmount.
241 zfsctl_create(zfsvfs_t *zfsvfs)
247 ASSERT(zfsvfs->z_ctldir == NULL);
249 vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
250 &zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
251 zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
253 zcp->zc_id = ZFSCTL_INO_ROOT;
255 VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp) == 0);
256 VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
257 &crtime, sizeof (crtime)));
258 ZFS_TIME_DECODE(&zcp->zc_cmtime, crtime);
262 * We're only faking the fact that we have a root of a filesystem for
263 * the sake of the GFS interfaces. Undo the flag manipulation it did
266 vp->v_vflag &= ~VV_ROOT;
268 zfsvfs->z_ctldir = vp;
274 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
275 * There might still be more references if we were force unmounted, but only
276 * new zfs_inactive() calls can occur and they don't reference .zfs
279 zfsctl_destroy(zfsvfs_t *zfsvfs)
281 VN_RELE(zfsvfs->z_ctldir);
282 zfsvfs->z_ctldir = NULL;
286 * Given a root znode, retrieve the associated .zfs directory.
287 * Add a hold to the vnode and return it.
290 zfsctl_root(znode_t *zp)
292 ASSERT(zfs_has_ctldir(zp));
293 VN_HOLD(zp->z_zfsvfs->z_ctldir);
294 return (zp->z_zfsvfs->z_ctldir);
298 zfsctl_common_print(ap)
299 struct vop_print_args /* {
303 vnode_t *vp = ap->a_vp;
304 gfs_file_t *fp = vp->v_data;
306 printf(" parent = %p\n", fp->gfs_parent);
307 printf(" type = %d\n", fp->gfs_type);
308 printf(" index = %d\n", fp->gfs_index);
309 printf(" ino = %ju\n", (uintmax_t)fp->gfs_ino);
314 * Common open routine. Disallow any write access.
318 zfsctl_common_open(struct vop_open_args *ap)
320 int flags = ap->a_mode;
323 return (SET_ERROR(EACCES));
329 * Common close routine. Nothing to do here.
333 zfsctl_common_close(struct vop_close_args *ap)
339 * Common access routine. Disallow writes.
343 zfsctl_common_access(ap)
344 struct vop_access_args /* {
347 struct ucred *a_cred;
351 accmode_t accmode = ap->a_accmode;
354 if (flags & V_ACE_MASK) {
355 if (accmode & ACE_ALL_WRITE_PERMS)
356 return (SET_ERROR(EACCES));
359 if (accmode & VWRITE)
360 return (SET_ERROR(EACCES));
369 * Common getattr function. Fill in basic information.
372 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
380 * We are a purely virtual object, so we have no
381 * blocksize or allocated blocks.
386 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
387 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
391 * We live in the now (for atime).
395 /* FreeBSD: Reset chflags(2) flags. */
401 zfsctl_common_fid(ap)
402 struct vop_fid_args /* {
407 vnode_t *vp = ap->a_vp;
408 fid_t *fidp = (void *)ap->a_fid;
409 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
410 zfsctl_node_t *zcp = vp->v_data;
411 uint64_t object = zcp->zc_id;
418 if (fidp->fid_len < SHORT_FID_LEN) {
419 fidp->fid_len = SHORT_FID_LEN;
421 return (SET_ERROR(ENOSPC));
425 zfid = (zfid_short_t *)fidp;
427 zfid->zf_len = SHORT_FID_LEN;
429 for (i = 0; i < sizeof (zfid->zf_object); i++)
430 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
432 /* .zfs znodes always have a generation number of 0 */
433 for (i = 0; i < sizeof (zfid->zf_gen); i++)
443 zfsctl_shares_fid(ap)
444 struct vop_fid_args /* {
449 vnode_t *vp = ap->a_vp;
450 fid_t *fidp = (void *)ap->a_fid;
451 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
457 if (zfsvfs->z_shares_dir == 0) {
459 return (SET_ERROR(ENOTSUP));
462 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
463 error = VOP_FID(ZTOV(dzp), fidp);
472 zfsctl_common_reclaim(ap)
473 struct vop_reclaim_args /* {
478 vnode_t *vp = ap->a_vp;
481 * Destroy the vm object and flush associated pages.
483 vnode_destroy_vobject(vp);
491 * .zfs inode namespace
493 * We need to generate unique inode numbers for all files and directories
494 * within the .zfs pseudo-filesystem. We use the following scheme:
499 * .zfs/snapshot/<snap> objectid(snap)
502 #define ZFSCTL_INO_SNAP(id) (id)
505 * Get root directory attributes.
509 zfsctl_root_getattr(ap)
510 struct vop_getattr_args /* {
513 struct ucred *a_cred;
516 struct vnode *vp = ap->a_vp;
517 struct vattr *vap = ap->a_vap;
518 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
519 zfsctl_node_t *zcp = vp->v_data;
522 vap->va_nodeid = ZFSCTL_INO_ROOT;
523 vap->va_nlink = vap->va_size = NROOT_ENTRIES;
524 vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
525 vap->va_birthtime = vap->va_ctime;
527 zfsctl_common_getattr(vp, vap);
534 * Special case the handling of "..".
538 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
539 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
540 int *direntflags, pathname_t *realpnp)
542 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
546 * No extended attributes allowed under .zfs
548 if (flags & LOOKUP_XATTR)
549 return (SET_ERROR(EINVAL));
553 if (strcmp(nm, "..") == 0) {
554 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp);
558 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
559 cr, ct, direntflags, realpnp);
568 zfsctl_root_print(ap)
569 struct vop_print_args /* {
573 printf(" .zfs node\n");
574 zfsctl_common_print(ap);
580 zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
581 caller_context_t *ct)
584 * We only care about ACL_ENABLED so that libsec can
585 * display ACL correctly and not default to POSIX draft.
587 if (cmd == _PC_ACL_ENABLED) {
588 *valp = _ACL_ACE_ENABLED;
592 return (fs_pathconf(vp, cmd, valp, cr, ct));
597 static const fs_operation_def_t zfsctl_tops_root[] = {
598 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
599 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
600 { VOPNAME_IOCTL, { .error = fs_inval } },
601 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } },
602 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
603 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
604 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } },
605 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
606 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
607 { VOPNAME_PATHCONF, { .vop_pathconf = zfsctl_pathconf } },
608 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } },
614 * Special case the handling of "..".
618 zfsctl_freebsd_root_lookup(ap)
619 struct vop_lookup_args /* {
621 struct vnode **a_vpp;
622 struct componentname *a_cnp;
625 vnode_t *dvp = ap->a_dvp;
626 vnode_t **vpp = ap->a_vpp;
627 cred_t *cr = ap->a_cnp->cn_cred;
628 int flags = ap->a_cnp->cn_flags;
629 int nameiop = ap->a_cnp->cn_nameiop;
630 char nm[NAME_MAX + 1];
634 if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
637 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
638 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
639 err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL);
640 if (err == 0 && (nm[0] != '.' || nm[1] != '\0')) {
641 ltype = VOP_ISLOCKED(dvp);
642 if (flags & ISDOTDOT) {
646 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
647 if (flags & ISDOTDOT) {
649 vn_lock(dvp, ltype| LK_RETRY);
656 static struct vop_vector zfsctl_ops_root = {
657 .vop_default = &default_vnodeops,
658 .vop_open = zfsctl_common_open,
659 .vop_close = zfsctl_common_close,
660 .vop_ioctl = VOP_EINVAL,
661 .vop_getattr = zfsctl_root_getattr,
662 .vop_access = zfsctl_common_access,
663 .vop_readdir = gfs_vop_readdir,
664 .vop_lookup = zfsctl_freebsd_root_lookup,
665 .vop_inactive = VOP_NULL,
666 .vop_reclaim = gfs_vop_reclaim,
668 .vop_pathconf = zfsctl_pathconf,
670 .vop_fid = zfsctl_common_fid,
671 .vop_print = zfsctl_root_print,
675 * Gets the full dataset name that corresponds to the given snapshot name
677 * zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1"
680 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
682 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
684 if (zfs_component_namecheck(name, NULL, NULL) != 0)
685 return (SET_ERROR(EILSEQ));
686 dmu_objset_name(os, zname);
687 if (strlen(zname) + 1 + strlen(name) >= len)
688 return (SET_ERROR(ENAMETOOLONG));
689 (void) strcat(zname, "@");
690 (void) strcat(zname, name);
695 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
697 vnode_t *svp = sep->se_root;
700 ASSERT(vn_ismntpt(svp));
702 /* this will be dropped by dounmount() */
703 if ((error = vn_vfswlock(svp)) != 0)
708 error = dounmount(vn_mountedvfs(svp), fflags, cr);
715 * We can't use VN_RELE(), as that will try to invoke
716 * zfsctl_snapdir_inactive(), which would cause us to destroy
717 * the sd_lock mutex held by our caller.
719 ASSERT(svp->v_count == 1);
720 gfs_vop_reclaim(svp, cr, NULL);
722 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
723 kmem_free(sep, sizeof (zfs_snapentry_t));
727 vfs_ref(vn_mountedvfs(svp));
728 return (dounmount(vn_mountedvfs(svp), fflags, curthread));
734 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
739 char newpath[MAXNAMELEN];
742 ASSERT(MUTEX_HELD(&sdp->sd_lock));
745 vfsp = vn_mountedvfs(sep->se_root);
746 ASSERT(vfsp != NULL);
751 * Change the name in the AVL tree.
753 avl_remove(&sdp->sd_snaps, sep);
754 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
755 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
756 (void) strcpy(sep->se_name, nm);
757 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
758 avl_insert(&sdp->sd_snaps, sep, where);
761 * Change the current mountpoint info:
762 * - update the tail of the mntpoint path
763 * - update the tail of the resource path
765 pathref = vfs_getmntpoint(vfsp);
766 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
767 VERIFY((tail = strrchr(newpath, '/')) != NULL);
769 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
770 (void) strcat(newpath, nm);
771 refstr_rele(pathref);
772 vfs_setmntpoint(vfsp, newpath, 0);
774 pathref = vfs_getresource(vfsp);
775 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
776 VERIFY((tail = strrchr(newpath, '@')) != NULL);
778 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
779 (void) strcat(newpath, nm);
780 refstr_rele(pathref);
781 vfs_setresource(vfsp, newpath, 0);
790 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
791 cred_t *cr, caller_context_t *ct, int flags)
793 zfsctl_snapdir_t *sdp = sdvp->v_data;
794 zfs_snapentry_t search, *sep;
797 char from[MAXNAMELEN], to[MAXNAMELEN];
798 char real[MAXNAMELEN], fsname[MAXNAMELEN];
801 zfsvfs = sdvp->v_vfsp->vfs_data;
804 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
805 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
809 } else if (err != ENOTSUP) {
817 dmu_objset_name(zfsvfs->z_os, fsname);
819 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
821 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
823 err = zfs_secpolicy_rename_perms(from, to, cr);
828 * Cannot move snapshots out of the snapdir.
831 return (SET_ERROR(EINVAL));
833 if (strcmp(snm, tnm) == 0)
836 mutex_enter(&sdp->sd_lock);
838 search.se_name = (char *)snm;
839 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
840 mutex_exit(&sdp->sd_lock);
841 return (SET_ERROR(ENOENT));
844 err = dsl_dataset_rename_snapshot(fsname, snm, tnm, 0);
846 zfsctl_rename_snap(sdp, sep, tnm);
848 mutex_exit(&sdp->sd_lock);
857 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
858 caller_context_t *ct, int flags)
860 zfsctl_snapdir_t *sdp = dvp->v_data;
861 zfs_snapentry_t *sep;
862 zfs_snapentry_t search;
864 char snapname[MAXNAMELEN];
865 char real[MAXNAMELEN];
868 zfsvfs = dvp->v_vfsp->vfs_data;
871 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
873 err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
877 } else if (err != ENOTSUP) {
885 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
887 err = zfs_secpolicy_destroy_perms(snapname, cr);
891 mutex_enter(&sdp->sd_lock);
893 search.se_name = name;
894 sep = avl_find(&sdp->sd_snaps, &search, NULL);
896 avl_remove(&sdp->sd_snaps, sep);
897 err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
899 avl_add(&sdp->sd_snaps, sep);
901 err = dsl_destroy_snapshot(snapname, B_FALSE);
903 err = SET_ERROR(ENOENT);
906 mutex_exit(&sdp->sd_lock);
913 * This creates a snapshot under '.zfs/snapshot'.
917 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp,
918 cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
920 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
921 char name[MAXNAMELEN];
923 static enum symfollow follow = NO_FOLLOW;
924 static enum uio_seg seg = UIO_SYSSPACE;
926 if (zfs_component_namecheck(dirname, NULL, NULL) != 0)
927 return (SET_ERROR(EILSEQ));
929 dmu_objset_name(zfsvfs->z_os, name);
933 err = zfs_secpolicy_snapshot_perms(name, cr);
938 err = dmu_objset_snapshot_one(name, dirname);
941 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
948 zfsctl_freebsd_snapdir_mkdir(ap)
949 struct vop_mkdir_args /* {
951 struct vnode **a_vpp;
952 struct componentname *a_cnp;
957 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
959 return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL,
960 ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL));
964 * Lookup entry point for the 'snapshot' directory. Try to open the
965 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
966 * Perform a mount of the associated dataset on top of the vnode.
970 zfsctl_snapdir_lookup(ap)
971 struct vop_lookup_args /* {
973 struct vnode **a_vpp;
974 struct componentname *a_cnp;
977 vnode_t *dvp = ap->a_dvp;
978 vnode_t **vpp = ap->a_vpp;
979 struct componentname *cnp = ap->a_cnp;
980 char nm[NAME_MAX + 1];
981 zfsctl_snapdir_t *sdp = dvp->v_data;
983 char snapname[MAXNAMELEN];
984 char real[MAXNAMELEN];
986 zfs_snapentry_t *sep, search;
987 size_t mountpoint_len;
989 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
991 int ltype, flags = 0;
994 * No extended attributes allowed under .zfs
996 if (flags & LOOKUP_XATTR)
997 return (SET_ERROR(EINVAL));
998 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
999 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
1001 ASSERT(dvp->v_type == VDIR);
1006 * If we get a recursive call, that means we got called
1007 * from the domount() code while it was trying to look up the
1008 * spec (which looks like a local path for zfs). We need to
1009 * add some flag to domount() to tell it not to do this lookup.
1011 if (MUTEX_HELD(&sdp->sd_lock))
1012 return (SET_ERROR(ENOENT));
1015 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1016 if (nm[0] == '.' && nm[1] == '.' && nm[2] =='\0') {
1018 VERIFY0(vn_lock(*vpp, LK_EXCLUSIVE));
1019 VERIFY0(vn_lock(dvp, LK_EXCLUSIVE));
1025 if (flags & FIGNORECASE) {
1026 boolean_t conflict = B_FALSE;
1028 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
1029 MAXNAMELEN, &conflict);
1031 strlcpy(nm, real, sizeof(nm));
1032 } else if (err != ENOTSUP) {
1038 (void) strlcpy(realpnp->pn_buf, nm,
1039 realpnp->pn_bufsize);
1040 if (conflict && direntflags)
1041 *direntflags = ED_CASE_CONFLICT;
1046 mutex_enter(&sdp->sd_lock);
1047 search.se_name = (char *)nm;
1048 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
1049 *vpp = sep->se_root;
1051 err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY);
1054 } else if (*vpp == sep->se_root) {
1056 * The snapshot was unmounted behind our backs,
1057 * try to remount it.
1059 VERIFY(zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname) == 0);
1062 mutex_exit(&sdp->sd_lock);
1068 * The requested snapshot is not currently mounted, look it up.
1070 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
1072 mutex_exit(&sdp->sd_lock);
1075 * handle "ls *" or "?" in a graceful manner,
1076 * forcing EILSEQ to ENOENT.
1077 * Since shell ultimately passes "*" or "?" as name to lookup
1079 return (err == EILSEQ ? ENOENT : err);
1081 if (dmu_objset_hold(snapname, FTAG, &snap) != 0) {
1082 mutex_exit(&sdp->sd_lock);
1085 return (SET_ERROR(ENOENT));
1086 #else /* !illumos */
1087 /* Translate errors and add SAVENAME when needed. */
1088 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
1090 cnp->cn_flags |= SAVENAME;
1092 err = SET_ERROR(ENOENT);
1096 #endif /* illumos */
1099 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
1100 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
1101 (void) strcpy(sep->se_name, nm);
1102 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
1103 avl_insert(&sdp->sd_snaps, sep, where);
1105 dmu_objset_rele(snap, FTAG);
1107 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
1108 strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(nm) + 1;
1109 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
1110 (void) snprintf(mountpoint, mountpoint_len,
1111 "%s/" ZFS_CTLDIR_NAME "/snapshot/%s",
1112 dvp->v_vfsp->mnt_stat.f_mntonname, nm);
1113 mutex_exit(&sdp->sd_lock);
1116 * The vnode may get reclaimed between dropping sd_lock and
1117 * getting the vnode lock.
1119 err = vn_lock(*vpp, LK_EXCLUSIVE);
1123 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0);
1124 kmem_free(mountpoint, mountpoint_len);
1127 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
1129 * This is where we lie about our v_vfsp in order to
1130 * make .zfs/snapshot/<snapname> accessible over NFS
1131 * without requiring manual mounts of <snapname>.
1133 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
1134 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
1140 * If we had an error, drop our hold on the vnode and
1141 * zfsctl_snapshot_inactive() will clean up.
1156 zfsctl_shares_lookup(ap)
1157 struct vop_lookup_args /* {
1158 struct vnode *a_dvp;
1159 struct vnode **a_vpp;
1160 struct componentname *a_cnp;
1163 vnode_t *dvp = ap->a_dvp;
1164 vnode_t **vpp = ap->a_vpp;
1165 struct componentname *cnp = ap->a_cnp;
1166 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1167 char nm[NAME_MAX + 1];
1173 ASSERT(cnp->cn_namelen < sizeof(nm));
1174 strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1);
1176 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1177 if (nm[0] == '.' && nm[1] == '.' && nm[2] =='\0') {
1179 VERIFY0(vn_lock(*vpp, LK_EXCLUSIVE));
1180 VERIFY0(vn_lock(dvp, LK_EXCLUSIVE));
1186 if (zfsvfs->z_shares_dir == 0) {
1188 return (SET_ERROR(ENOTSUP));
1190 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1191 error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp);
1202 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1203 offset_t *offp, offset_t *nextp, void *data, int flags)
1205 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1206 char snapname[MAXNAMELEN];
1207 uint64_t id, cookie;
1208 boolean_t case_conflict;
1214 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
1215 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
1216 &cookie, &case_conflict);
1217 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
1220 if (error == ENOENT) {
1227 if (flags & V_RDDIR_ENTFLAGS) {
1228 edirent_t *eodp = dp;
1230 (void) strcpy(eodp->ed_name, snapname);
1231 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1232 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1234 struct dirent64 *odp = dp;
1236 (void) strcpy(odp->d_name, snapname);
1237 odp->d_ino = ZFSCTL_INO_SNAP(id);
1248 zfsctl_shares_readdir(ap)
1249 struct vop_readdir_args /* {
1252 struct ucred *a_cred;
1258 vnode_t *vp = ap->a_vp;
1259 uio_t *uiop = ap->a_uio;
1260 cred_t *cr = ap->a_cred;
1261 int *eofp = ap->a_eofflag;
1262 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1268 if (zfsvfs->z_shares_dir == 0) {
1270 return (SET_ERROR(ENOTSUP));
1272 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1273 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1274 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies);
1275 VN_URELE(ZTOV(dzp));
1278 error = SET_ERROR(ENOENT);
1286 * pvp is the '.zfs' directory (zfsctl_node_t).
1288 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1290 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1291 * when a lookup is performed on .zfs for "snapshot".
1294 zfsctl_mknode_snapdir(vnode_t *pvp)
1297 zfsctl_snapdir_t *sdp;
1299 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
1300 &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1301 zfsctl_snapdir_readdir_cb, NULL);
1303 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1304 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1305 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1306 avl_create(&sdp->sd_snaps, snapentry_compare,
1307 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1313 zfsctl_mknode_shares(vnode_t *pvp)
1318 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1319 &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1322 sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1330 zfsctl_shares_getattr(ap)
1331 struct vop_getattr_args /* {
1333 struct vattr *a_vap;
1334 struct ucred *a_cred;
1335 struct thread *a_td;
1338 vnode_t *vp = ap->a_vp;
1339 vattr_t *vap = ap->a_vap;
1340 cred_t *cr = ap->a_cred;
1341 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1346 if (zfsvfs->z_shares_dir == 0) {
1348 return (SET_ERROR(ENOTSUP));
1350 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1351 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1352 error = VOP_GETATTR(ZTOV(dzp), vap, cr);
1353 VN_URELE(ZTOV(dzp));
1363 zfsctl_snapdir_getattr(ap)
1364 struct vop_getattr_args /* {
1366 struct vattr *a_vap;
1367 struct ucred *a_cred;
1370 vnode_t *vp = ap->a_vp;
1371 vattr_t *vap = ap->a_vap;
1372 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1373 zfsctl_snapdir_t *sdp = vp->v_data;
1376 zfsctl_common_getattr(vp, vap);
1377 vap->va_nodeid = gfs_file_inode(vp);
1378 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1379 vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1380 vap->va_birthtime = vap->va_ctime;
1388 zfsctl_snapdir_inactive(ap)
1389 struct vop_inactive_args /* {
1391 struct thread *a_td;
1394 vnode_t *vp = ap->a_vp;
1395 zfsctl_snapdir_t *sdp = vp->v_data;
1396 zfs_snapentry_t *sep;
1399 * On forced unmount we have to free snapshots from here.
1401 mutex_enter(&sdp->sd_lock);
1402 while ((sep = avl_first(&sdp->sd_snaps)) != NULL) {
1403 avl_remove(&sdp->sd_snaps, sep);
1404 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1405 kmem_free(sep, sizeof (zfs_snapentry_t));
1407 mutex_exit(&sdp->sd_lock);
1408 gfs_dir_inactive(vp);
1409 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1410 mutex_destroy(&sdp->sd_lock);
1411 avl_destroy(&sdp->sd_snaps);
1412 kmem_free(sdp, sizeof (zfsctl_snapdir_t));
1418 zfsctl_shares_print(ap)
1419 struct vop_print_args /* {
1423 printf(" .zfs/shares node\n");
1424 zfsctl_common_print(ap);
1429 zfsctl_snapdir_print(ap)
1430 struct vop_print_args /* {
1434 vnode_t *vp = ap->a_vp;
1435 zfsctl_snapdir_t *sdp = vp->v_data;
1437 printf(" .zfs/snapshot node\n");
1438 printf(" number of children = %lu\n", avl_numnodes(&sdp->sd_snaps));
1439 zfsctl_common_print(ap);
1444 static const fs_operation_def_t zfsctl_tops_snapdir[] = {
1445 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
1446 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
1447 { VOPNAME_IOCTL, { .error = fs_inval } },
1448 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_snapdir_getattr } },
1449 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
1450 { VOPNAME_RENAME, { .vop_rename = zfsctl_snapdir_rename } },
1451 { VOPNAME_RMDIR, { .vop_rmdir = zfsctl_snapdir_remove } },
1452 { VOPNAME_MKDIR, { .vop_mkdir = zfsctl_snapdir_mkdir } },
1453 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
1454 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_snapdir_lookup } },
1455 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
1456 { VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapdir_inactive } },
1457 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } },
1461 static const fs_operation_def_t zfsctl_tops_shares[] = {
1462 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
1463 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
1464 { VOPNAME_IOCTL, { .error = fs_inval } },
1465 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_shares_getattr } },
1466 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
1467 { VOPNAME_READDIR, { .vop_readdir = zfsctl_shares_readdir } },
1468 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_shares_lookup } },
1469 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
1470 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
1471 { VOPNAME_FID, { .vop_fid = zfsctl_shares_fid } },
1474 #else /* !illumos */
1475 static struct vop_vector zfsctl_ops_snapdir = {
1476 .vop_default = &default_vnodeops,
1477 .vop_open = zfsctl_common_open,
1478 .vop_close = zfsctl_common_close,
1479 .vop_ioctl = VOP_EINVAL,
1480 .vop_getattr = zfsctl_snapdir_getattr,
1481 .vop_access = zfsctl_common_access,
1482 .vop_mkdir = zfsctl_freebsd_snapdir_mkdir,
1483 .vop_readdir = gfs_vop_readdir,
1484 .vop_lookup = zfsctl_snapdir_lookup,
1485 .vop_inactive = zfsctl_snapdir_inactive,
1486 .vop_reclaim = zfsctl_common_reclaim,
1487 .vop_fid = zfsctl_common_fid,
1488 .vop_print = zfsctl_snapdir_print,
1491 static struct vop_vector zfsctl_ops_shares = {
1492 .vop_default = &default_vnodeops,
1493 .vop_open = zfsctl_common_open,
1494 .vop_close = zfsctl_common_close,
1495 .vop_ioctl = VOP_EINVAL,
1496 .vop_getattr = zfsctl_shares_getattr,
1497 .vop_access = zfsctl_common_access,
1498 .vop_readdir = zfsctl_shares_readdir,
1499 .vop_lookup = zfsctl_shares_lookup,
1500 .vop_inactive = VOP_NULL,
1501 .vop_reclaim = gfs_vop_reclaim,
1502 .vop_fid = zfsctl_shares_fid,
1503 .vop_print = zfsctl_shares_print,
1505 #endif /* illumos */
1508 * pvp is the GFS vnode '.zfs/snapshot'.
1510 * This creates a GFS node under '.zfs/snapshot' representing each
1511 * snapshot. This newly created GFS node is what we mount snapshot
1515 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1520 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1521 &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1523 zcp->zc_id = objset;
1531 zfsctl_snapshot_reclaim(ap)
1532 struct vop_inactive_args /* {
1534 struct thread *a_td;
1537 vnode_t *vp = ap->a_vp;
1538 cred_t *cr = ap->a_td->td_ucred;
1539 struct vop_reclaim_args iap;
1540 zfsctl_snapdir_t *sdp;
1541 zfs_snapentry_t *sep, *next;
1545 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1547 /* this may already have been unmounted */
1552 if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
1553 mutex_enter(&sdp->sd_lock);
1555 ASSERT(!vn_ismntpt(vp));
1557 sep = avl_first(&sdp->sd_snaps);
1558 while (sep != NULL) {
1559 next = AVL_NEXT(&sdp->sd_snaps, sep);
1561 if (sep->se_root == vp) {
1562 avl_remove(&sdp->sd_snaps, sep);
1563 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1564 kmem_free(sep, sizeof (zfs_snapentry_t));
1569 ASSERT(sep != NULL);
1572 mutex_exit(&sdp->sd_lock);
1576 * Dispose of the vnode for the snapshot mount point.
1577 * This is safe to do because once this entry has been removed
1578 * from the AVL tree, it can't be found again, so cannot become
1579 * "active". If we lookup the same name again we will end up
1580 * creating a new vnode.
1583 gfs_vop_reclaim(&iap);
1589 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1591 zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data;
1593 zfsctl_snapdir_t *sdp;
1594 zfs_snapentry_t *sep;
1597 ASSERT(zfsvfs->z_ctldir != NULL);
1598 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1599 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1604 mutex_enter(&sdp->sd_lock);
1605 sep = avl_first(&sdp->sd_snaps);
1606 while (sep != NULL) {
1610 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1613 mutex_exit(&sdp->sd_lock);
1618 len = strlen(sep->se_name);
1619 *ap->a_buflen -= len;
1620 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len);
1621 mutex_exit(&sdp->sd_lock);
1631 zfsctl_snaphot_print(ap)
1632 struct vop_print_args /* {
1636 vnode_t *vp = ap->a_vp;
1637 zfsctl_node_t *zcp = vp->v_data;
1639 printf(" .zfs/snapshot/<snap> node\n");
1640 printf(" id = %ju\n", (uintmax_t)zcp->zc_id);
1641 zfsctl_common_print(ap);
1646 * These VP's should never see the light of day. They should always
1649 static struct vop_vector zfsctl_ops_snapshot = {
1650 .vop_default = &default_vnodeops,
1651 .vop_inactive = VOP_NULL,
1652 .vop_reclaim = zfsctl_snapshot_reclaim,
1653 .vop_vptocnp = zfsctl_snapshot_vptocnp,
1654 .vop_print = zfsctl_snaphot_print,
1658 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1660 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1662 zfsctl_snapdir_t *sdp;
1664 zfs_snapentry_t *sep;
1667 ASSERT(zfsvfs->z_ctldir != NULL);
1668 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1669 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1674 mutex_enter(&sdp->sd_lock);
1675 sep = avl_first(&sdp->sd_snaps);
1676 while (sep != NULL) {
1679 if (zcp->zc_id == objsetid)
1682 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1688 * Return the mounted root rather than the covered mount point.
1689 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1690 * and returns the ZFS vnode mounted on top of the GFS node.
1691 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1693 error = traverse(&vp, LK_SHARED | LK_RETRY);
1695 if (vp == sep->se_root) {
1696 VN_RELE(vp); /* release covered vp */
1697 error = SET_ERROR(EINVAL);
1699 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1700 VN_URELE(vp); /* put snapshot's root vp */
1703 mutex_exit(&sdp->sd_lock);
1705 error = SET_ERROR(EINVAL);
1706 mutex_exit(&sdp->sd_lock);
1715 * Unmount any snapshots for the given filesystem. This is called from
1716 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1720 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1722 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1724 zfsctl_snapdir_t *sdp;
1725 zfs_snapentry_t *sep, *next;
1728 ASSERT(zfsvfs->z_ctldir != NULL);
1729 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1730 NULL, 0, NULL, cr, NULL, NULL, NULL);
1735 mutex_enter(&sdp->sd_lock);
1737 sep = avl_first(&sdp->sd_snaps);
1738 while (sep != NULL) {
1739 next = AVL_NEXT(&sdp->sd_snaps, sep);
1742 * If this snapshot is not mounted, then it must
1743 * have just been unmounted by somebody else, and
1744 * will be cleaned up by zfsctl_snapdir_inactive().
1746 if (vn_ismntpt(sep->se_root)) {
1747 error = zfsctl_unmount_snap(sep, fflags, cr);
1752 * Before reinserting snapshot to the tree,
1753 * check if it was actually removed. For example
1754 * when snapshot mount point is busy, we will
1755 * have an error here, but there will be no need
1756 * to reinsert snapshot.
1758 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
1759 avl_insert(&sdp->sd_snaps, sep, where);
1766 mutex_exit(&sdp->sd_lock);