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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * ZFS control directory (a.k.a. ".zfs")
31 * This directory provides a common location for all ZFS meta-objects.
32 * Currently, this is only the 'snapshot' directory, but this may expand in the
33 * future. The elements are built using the GFS primitives, as the hierarchy
34 * does not actually exist on disk.
36 * For 'snapshot', we don't want to have all snapshots always mounted, because
37 * this would take up a huge amount of space in /etc/mnttab. We have three
40 * ctldir ------> snapshotdir -------> snapshot
46 * The 'snapshot' node contains just enough information to lookup '..' and act
47 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
48 * perform an automount of the underlying filesystem and return the
49 * corresponding vnode.
51 * All mounts are handled automatically by the kernel, but unmounts are
52 * (currently) handled from user land. The main reason is that there is no
53 * reliable way to auto-unmount the filesystem when it's "no longer in use".
54 * When the user unmounts a filesystem, we call zfsctl_unmount(), which
55 * unmounts any snapshots within the snapshot directory.
58 #include <sys/zfs_context.h>
59 #include <sys/zfs_ctldir.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/zfs_vfsops.h>
62 #include <sys/namei.h>
66 #include <sys/mount.h>
75 snapentry_compare(const void *a, const void *b)
77 const zfs_snapentry_t *sa = a;
78 const zfs_snapentry_t *sb = b;
79 int ret = strcmp(sa->se_name, sb->se_name);
89 static struct vop_vector zfsctl_ops_root;
90 static struct vop_vector zfsctl_ops_snapdir;
91 static struct vop_vector zfsctl_ops_snapshot;
93 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
94 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
96 typedef struct zfsctl_node {
97 gfs_dir_t zc_gfs_private;
99 timestruc_t zc_cmtime; /* ctime and mtime, always the same */
102 typedef struct zfsctl_snapdir {
103 zfsctl_node_t sd_node;
109 * Root directory elements. We have only a single static entry, 'snapshot'.
111 static gfs_dirent_t zfsctl_root_entries[] = {
112 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
116 /* include . and .. in the calculation */
117 #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \
118 sizeof (gfs_dirent_t)) + 1)
122 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
123 * directories. This is called from the ZFS init routine, and initializes the
124 * vnode ops vectors that we'll be using.
137 * Return the inode number associated with the 'snapshot' directory.
141 zfsctl_root_inode_cb(vnode_t *vp, int index)
144 return (ZFSCTL_INO_SNAPDIR);
148 * Create the '.zfs' directory. This directory is cached as part of the VFS
149 * structure. This results in a hold on the vfs_t. The code in zfs_umount()
150 * therefore checks against a vfs_count of 2 instead of 1. This reference
151 * is removed when the ctldir is destroyed in the unmount.
154 zfsctl_create(zfsvfs_t *zfsvfs)
159 ASSERT(zfsvfs->z_ctldir == NULL);
161 vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
162 &zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
163 zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
165 zcp->zc_id = ZFSCTL_INO_ROOT;
167 VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp, curthread) == 0);
168 ZFS_TIME_DECODE(&zcp->zc_cmtime, VTOZ(rvp)->z_phys->zp_crtime);
172 * We're only faking the fact that we have a root of a filesystem for
173 * the sake of the GFS interfaces. Undo the flag manipulation it did
176 vp->v_vflag &= ~VV_ROOT;
178 zfsvfs->z_ctldir = vp;
180 VOP_UNLOCK(vp, 0, curthread);
184 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
185 * There might still be more references if we were force unmounted, but only
186 * new zfs_inactive() calls can occur and they don't reference .zfs
189 zfsctl_destroy(zfsvfs_t *zfsvfs)
191 VN_RELE(zfsvfs->z_ctldir);
192 zfsvfs->z_ctldir = NULL;
196 * Given a root znode, retrieve the associated .zfs directory.
197 * Add a hold to the vnode and return it.
200 zfsctl_root(znode_t *zp)
202 ASSERT(zfs_has_ctldir(zp));
203 VN_HOLD(zp->z_zfsvfs->z_ctldir);
204 return (zp->z_zfsvfs->z_ctldir);
208 * Common open routine. Disallow any write access.
212 zfsctl_common_open(struct vop_open_args *ap)
214 int flags = ap->a_mode;
223 * Common close routine. Nothing to do here.
227 zfsctl_common_close(struct vop_close_args *ap)
233 * Common access routine. Disallow writes.
237 zfsctl_common_access(ap)
238 struct vop_access_args /* {
241 struct ucred *a_cred;
245 int mode = ap->a_mode;
254 * Common getattr function. Fill in basic information.
257 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
259 zfsctl_node_t *zcp = vp->v_data;
266 * We are a purly virtual object, so we have no
267 * blocksize or allocated blocks.
272 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
273 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
277 * We live in the now (for atime).
281 vap->va_mtime = vap->va_ctime = vap->va_birthtime = zcp->zc_cmtime;
282 /* FreeBSD: Reset chflags(2) flags. */
287 zfsctl_common_fid(ap)
288 struct vop_fid_args /* {
293 vnode_t *vp = ap->a_vp;
294 fid_t *fidp = (void *)ap->a_fid;
295 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
296 zfsctl_node_t *zcp = vp->v_data;
297 uint64_t object = zcp->zc_id;
303 fidp->fid_len = SHORT_FID_LEN;
305 zfid = (zfid_short_t *)fidp;
307 zfid->zf_len = SHORT_FID_LEN;
309 for (i = 0; i < sizeof (zfid->zf_object); i++)
310 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
312 /* .zfs znodes always have a generation number of 0 */
313 for (i = 0; i < sizeof (zfid->zf_gen); i++)
321 zfsctl_common_reclaim(ap)
322 struct vop_reclaim_args /* {
327 vnode_t *vp = ap->a_vp;
330 * Destroy the vm object and flush associated pages.
332 vnode_destroy_vobject(vp);
340 * .zfs inode namespace
342 * We need to generate unique inode numbers for all files and directories
343 * within the .zfs pseudo-filesystem. We use the following scheme:
348 * .zfs/snapshot/<snap> objectid(snap)
351 #define ZFSCTL_INO_SNAP(id) (id)
354 * Get root directory attributes.
358 zfsctl_root_getattr(ap)
359 struct vop_getattr_args /* {
362 struct ucred *a_cred;
366 struct vnode *vp = ap->a_vp;
367 struct vattr *vap = ap->a_vap;
368 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
371 vap->va_nodeid = ZFSCTL_INO_ROOT;
372 vap->va_nlink = vap->va_size = NROOT_ENTRIES;
374 zfsctl_common_getattr(vp, vap);
381 * Special case the handling of "..".
385 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
386 int flags, vnode_t *rdir, cred_t *cr)
388 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
393 if (strcmp(nm, "..") == 0) {
394 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp, curthread);
396 VOP_UNLOCK(*vpp, 0, curthread);
398 err = gfs_dir_lookup(dvp, nm, vpp);
407 * Special case the handling of "..".
411 zfsctl_root_lookup_vop(ap)
412 struct vop_lookup_args /* {
414 struct vnode **a_vpp;
415 struct componentname *a_cnp;
418 vnode_t *dvp = ap->a_dvp;
419 vnode_t **vpp = ap->a_vpp;
420 cred_t *cr = ap->a_cnp->cn_cred;
421 int flags = ap->a_cnp->cn_flags;
422 int nameiop = ap->a_cnp->cn_nameiop;
423 char nm[NAME_MAX + 1];
426 if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
429 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
430 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
432 err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr);
433 if (err == 0 && (nm[0] != '.' || nm[1] != '\0'))
434 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, ap->a_cnp->cn_thread);
439 static struct vop_vector zfsctl_ops_root = {
440 .vop_default = &default_vnodeops,
441 .vop_open = zfsctl_common_open,
442 .vop_close = zfsctl_common_close,
443 .vop_ioctl = VOP_EINVAL,
444 .vop_getattr = zfsctl_root_getattr,
445 .vop_access = zfsctl_common_access,
446 .vop_readdir = gfs_vop_readdir,
447 .vop_lookup = zfsctl_root_lookup_vop,
448 .vop_inactive = gfs_vop_inactive,
449 .vop_reclaim = zfsctl_common_reclaim,
450 .vop_fid = zfsctl_common_fid,
454 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
456 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
458 dmu_objset_name(os, zname);
459 if (strlen(zname) + 1 + strlen(name) >= len)
460 return (ENAMETOOLONG);
461 (void) strcat(zname, "@");
462 (void) strcat(zname, name);
467 zfsctl_unmount_snap(vnode_t *dvp, const char *name, int force, cred_t *cr)
469 zfsctl_snapdir_t *sdp = dvp->v_data;
470 zfs_snapentry_t search, *sep;
471 struct vop_inactive_args ap;
475 ASSERT(MUTEX_HELD(&sdp->sd_lock));
477 search.se_name = (char *)name;
478 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL)
481 ASSERT(vn_ismntpt(sep->se_root));
483 /* this will be dropped by dounmount() */
484 if ((err = vn_vfswlock(sep->se_root)) != 0)
487 err = dounmount(vn_mountedvfs(sep->se_root), force, curthread);
490 ASSERT(sep->se_root->v_count == 1);
491 ap.a_vp = sep->se_root;
492 gfs_vop_inactive(&ap);
494 avl_remove(&sdp->sd_snaps, sep);
495 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
496 kmem_free(sep, sizeof (zfs_snapentry_t));
503 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
508 char newpath[MAXNAMELEN];
511 ASSERT(MUTEX_HELD(&sdp->sd_lock));
514 vfsp = vn_mountedvfs(sep->se_root);
515 ASSERT(vfsp != NULL);
520 * Change the name in the AVL tree.
522 avl_remove(&sdp->sd_snaps, sep);
523 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
524 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
525 (void) strcpy(sep->se_name, nm);
526 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
527 avl_insert(&sdp->sd_snaps, sep, where);
530 * Change the current mountpoint info:
531 * - update the tail of the mntpoint path
532 * - update the tail of the resource path
534 pathref = vfs_getmntpoint(vfsp);
535 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
536 VERIFY((tail = strrchr(newpath, '/')) != NULL);
538 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
539 (void) strcat(newpath, nm);
540 refstr_rele(pathref);
541 vfs_setmntpoint(vfsp, newpath);
543 pathref = vfs_getresource(vfsp);
544 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
545 VERIFY((tail = strrchr(newpath, '@')) != NULL);
547 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
548 (void) strcat(newpath, nm);
549 refstr_rele(pathref);
550 vfs_setresource(vfsp, newpath);
558 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
561 zfsctl_snapdir_t *sdp = sdvp->v_data;
562 zfs_snapentry_t search, *sep;
564 char from[MAXNAMELEN], to[MAXNAMELEN];
567 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
570 err = zfs_secpolicy_write(from, cr);
575 * Cannot move snapshots out of the snapdir.
580 if (strcmp(snm, tnm) == 0)
583 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
587 mutex_enter(&sdp->sd_lock);
589 search.se_name = (char *)snm;
590 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
591 mutex_exit(&sdp->sd_lock);
595 err = dmu_objset_rename(from, to, B_FALSE);
597 zfsctl_rename_snap(sdp, sep, tnm);
599 mutex_exit(&sdp->sd_lock);
608 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
610 zfsctl_snapdir_t *sdp = dvp->v_data;
611 char snapname[MAXNAMELEN];
614 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
617 err = zfs_secpolicy_write(snapname, cr);
621 mutex_enter(&sdp->sd_lock);
623 err = zfsctl_unmount_snap(dvp, name, 0, cr);
625 mutex_exit(&sdp->sd_lock);
629 err = dmu_objset_destroy(snapname);
631 mutex_exit(&sdp->sd_lock);
638 * Lookup entry point for the 'snapshot' directory. Try to open the
639 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
640 * Perform a mount of the associated dataset on top of the vnode.
644 zfsctl_snapdir_lookup(ap)
645 struct vop_lookup_args /* {
647 struct vnode **a_vpp;
648 struct componentname *a_cnp;
651 vnode_t *dvp = ap->a_dvp;
652 vnode_t **vpp = ap->a_vpp;
653 char nm[NAME_MAX + 1];
654 zfsctl_snapdir_t *sdp = dvp->v_data;
656 char snapname[MAXNAMELEN];
658 zfs_snapentry_t *sep, search;
659 size_t mountpoint_len;
661 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
664 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
665 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
667 ASSERT(dvp->v_type == VDIR);
669 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0)
675 * If we get a recursive call, that means we got called
676 * from the domount() code while it was trying to look up the
677 * spec (which looks like a local path for zfs). We need to
678 * add some flag to domount() to tell it not to do this lookup.
680 if (MUTEX_HELD(&sdp->sd_lock))
685 mutex_enter(&sdp->sd_lock);
686 search.se_name = (char *)nm;
687 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
690 if ((*vpp)->v_mountedhere == NULL) {
692 * The snapshot was unmounted behind our backs,
697 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, ap->a_cnp->cn_thread);
698 mutex_exit(&sdp->sd_lock);
704 * The requested snapshot is not currently mounted, look it up.
706 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
708 mutex_exit(&sdp->sd_lock);
712 if (dmu_objset_open(snapname, DMU_OST_ZFS,
713 DS_MODE_STANDARD | DS_MODE_READONLY, &snap) != 0) {
714 mutex_exit(&sdp->sd_lock);
719 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
720 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
721 (void) strcpy(sep->se_name, nm);
722 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
724 avl_insert(&sdp->sd_snaps, sep, where);
726 dmu_objset_close(snap);
728 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
729 strlen("/.zfs/snapshot/") + strlen(nm) + 1;
730 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
731 (void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
732 dvp->v_vfsp->mnt_stat.f_mntonname, nm);
733 err = domount(curthread, *vpp, "zfs", mountpoint, snapname, 0);
734 kmem_free(mountpoint, mountpoint_len);
735 /* FreeBSD: This line was moved from below to avoid a lock recursion. */
737 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, curthread);
738 mutex_exit(&sdp->sd_lock);
741 * If we had an error, drop our hold on the vnode and
742 * zfsctl_snapshot_inactive() will clean up.
753 zfsctl_snapdir_readdir_cb(vnode_t *vp, struct dirent64 *dp, int *eofp,
754 offset_t *offp, offset_t *nextp, void *data)
756 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
757 char snapname[MAXNAMELEN];
763 if (dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
764 &cookie) == ENOENT) {
770 (void) strcpy(dp->d_name, snapname);
771 dp->d_ino = ZFSCTL_INO_SNAP(id);
780 zfsctl_mknode_snapdir(vnode_t *pvp)
783 zfsctl_snapdir_t *sdp;
785 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
786 &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
787 zfsctl_snapdir_readdir_cb, NULL);
789 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
790 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
791 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
792 avl_create(&sdp->sd_snaps, snapentry_compare,
793 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
794 VOP_UNLOCK(vp, 0, curthread);
800 zfsctl_snapdir_getattr(ap)
801 struct vop_getattr_args /* {
804 struct ucred *a_cred;
808 struct vnode *vp = ap->a_vp;
809 struct vattr *vap = ap->a_vap;
810 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
811 zfsctl_snapdir_t *sdp = vp->v_data;
814 zfsctl_common_getattr(vp, vap);
815 vap->va_nodeid = gfs_file_inode(vp);
816 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
824 zfsctl_snapdir_inactive(ap)
825 struct vop_inactive_args /* {
830 vnode_t *vp = ap->a_vp;
831 zfsctl_snapdir_t *sdp = vp->v_data;
834 private = gfs_dir_inactive(vp);
835 if (private != NULL) {
836 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
837 mutex_destroy(&sdp->sd_lock);
838 avl_destroy(&sdp->sd_snaps);
839 kmem_free(private, sizeof (zfsctl_snapdir_t));
844 static struct vop_vector zfsctl_ops_snapdir = {
845 .vop_default = &default_vnodeops,
846 .vop_open = zfsctl_common_open,
847 .vop_close = zfsctl_common_close,
848 .vop_ioctl = VOP_EINVAL,
849 .vop_getattr = zfsctl_snapdir_getattr,
850 .vop_access = zfsctl_common_access,
851 .vop_readdir = gfs_vop_readdir,
852 .vop_lookup = zfsctl_snapdir_lookup,
853 .vop_inactive = zfsctl_snapdir_inactive,
854 .vop_reclaim = zfsctl_common_reclaim,
855 .vop_fid = zfsctl_common_fid,
859 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
864 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
865 &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
868 VOP_UNLOCK(vp, 0, curthread);
874 zfsctl_snapshot_inactive(ap)
875 struct vop_inactive_args /* {
880 vnode_t *vp = ap->a_vp;
881 struct vop_inactive_args iap;
882 zfsctl_snapdir_t *sdp;
883 zfs_snapentry_t *sep, *next;
887 VERIFY(gfs_dir_lookup(vp, "..", &dvp) == 0);
889 VOP_UNLOCK(dvp, 0, ap->a_td);
891 if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
892 mutex_enter(&sdp->sd_lock);
894 if (vp->v_count > 1) {
896 mutex_exit(&sdp->sd_lock);
899 ASSERT(!vn_ismntpt(vp));
901 sep = avl_first(&sdp->sd_snaps);
902 while (sep != NULL) {
903 next = AVL_NEXT(&sdp->sd_snaps, sep);
905 if (sep->se_root == vp) {
906 avl_remove(&sdp->sd_snaps, sep);
907 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
908 kmem_free(sep, sizeof (zfs_snapentry_t));
916 mutex_exit(&sdp->sd_lock);
920 * Dispose of the vnode for the snapshot mount point.
921 * This is safe to do because once this entry has been removed
922 * from the AVL tree, it can't be found again, so cannot become
923 * "active". If we lookup the same name again we will end up
924 * creating a new vnode.
927 return (gfs_vop_inactive(&iap));
931 zfsctl_traverse_begin(vnode_t **vpp, int lktype, kthread_t *td)
935 /* Snapshot should be already mounted, but just in case. */
936 if (vn_mountedvfs(*vpp) == NULL)
938 return (traverse(vpp, lktype));
942 zfsctl_traverse_end(vnode_t *vp, int err)
952 zfsctl_snapshot_getattr(ap)
953 struct vop_getattr_args /* {
956 struct ucred *a_cred;
960 vnode_t *vp = ap->a_vp;
963 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY, ap->a_td);
965 err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred, ap->a_td);
966 zfsctl_traverse_end(vp, err);
971 zfsctl_snapshot_fid(ap)
972 struct vop_fid_args /* {
977 vnode_t *vp = ap->a_vp;
980 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY, curthread);
982 err = VOP_VPTOFH(vp, (void *)ap->a_fid);
983 zfsctl_traverse_end(vp, err);
988 * These VP's should never see the light of day. They should always
991 static struct vop_vector zfsctl_ops_snapshot = {
992 .vop_default = &default_vnodeops,
993 .vop_inactive = zfsctl_snapshot_inactive,
994 .vop_reclaim = zfsctl_common_reclaim,
995 .vop_getattr = zfsctl_snapshot_getattr,
996 .vop_fid = zfsctl_snapshot_fid,
1000 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1002 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1004 zfsctl_snapdir_t *sdp;
1006 zfs_snapentry_t *sep;
1009 ASSERT(zfsvfs->z_ctldir != NULL);
1010 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1011 NULL, 0, NULL, kcred);
1016 mutex_enter(&sdp->sd_lock);
1017 sep = avl_first(&sdp->sd_snaps);
1018 while (sep != NULL) {
1021 if (zcp->zc_id == objsetid)
1024 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1029 error = traverse(&vp, LK_SHARED | LK_RETRY);
1031 if (vp == sep->se_root)
1034 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1036 mutex_exit(&sdp->sd_lock);
1043 mutex_exit(&sdp->sd_lock);
1052 * Unmount any snapshots for the given filesystem. This is called from
1053 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1057 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1059 struct vop_inactive_args ap;
1060 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1062 zfsctl_snapdir_t *sdp;
1063 zfs_snapentry_t *sep, *next;
1066 ASSERT(zfsvfs->z_ctldir != NULL);
1067 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1073 mutex_enter(&sdp->sd_lock);
1075 sep = avl_first(&sdp->sd_snaps);
1076 while (sep != NULL) {
1078 next = AVL_NEXT(&sdp->sd_snaps, sep);
1081 * If this snapshot is not mounted, then it must
1082 * have just been unmounted by somebody else, and
1083 * will be cleaned up by zfsctl_snapdir_inactive().
1085 if (vn_ismntpt(svp)) {
1086 if ((error = vn_vfswlock(svp)) != 0)
1090 * Increase usecount, so dounmount() won't vrele() it
1091 * to 0 and call zfsctl_snapdir_inactive().
1094 vfsp = vn_mountedvfs(svp);
1096 error = dounmount(vfsp, fflags, curthread);
1103 avl_remove(&sdp->sd_snaps, sep);
1104 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1105 kmem_free(sep, sizeof (zfs_snapentry_t));
1108 * We can't use VN_RELE(), as that will try to
1109 * invoke zfsctl_snapdir_inactive(), and that
1110 * would lead to an attempt to re-grab the sd_lock.
1112 ASSERT3U(svp->v_count, ==, 1);
1114 gfs_vop_inactive(&ap);
1119 mutex_exit(&sdp->sd_lock);