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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * ZFS control directory (a.k.a. ".zfs")
29 * This directory provides a common location for all ZFS meta-objects.
30 * Currently, this is only the 'snapshot' directory, but this may expand in the
31 * future. The elements are built using the GFS primitives, as the hierarchy
32 * does not actually exist on disk.
34 * For 'snapshot', we don't want to have all snapshots always mounted, because
35 * this would take up a huge amount of space in /etc/mnttab. We have three
38 * ctldir ------> snapshotdir -------> snapshot
44 * The 'snapshot' node contains just enough information to lookup '..' and act
45 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
46 * perform an automount of the underlying filesystem and return the
47 * corresponding vnode.
49 * All mounts are handled automatically by the kernel, but unmounts are
50 * (currently) handled from user land. The main reason is that there is no
51 * reliable way to auto-unmount the filesystem when it's "no longer in use".
52 * When the user unmounts a filesystem, we call zfsctl_unmount(), which
53 * unmounts any snapshots within the snapshot directory.
55 * The '.zfs', '.zfs/snapshot', and all directories created under
56 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
57 * share the same vfs_t as the head filesystem (what '.zfs' lives under).
59 * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
60 * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
61 * However, vnodes within these mounted on file systems have their v_vfsp
62 * fields set to the head filesystem to make NFS happy (see
63 * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
64 * so that it cannot be freed until all snapshots have been unmounted.
67 #include <sys/zfs_context.h>
68 #include <sys/zfs_ctldir.h>
69 #include <sys/zfs_ioctl.h>
70 #include <sys/zfs_vfsops.h>
71 #include <sys/namei.h>
75 #include <sys/dsl_deleg.h>
76 #include <sys/mount.h>
77 #include <sys/sunddi.h>
79 #include "zfs_namecheck.h"
81 typedef struct zfsctl_node {
82 gfs_dir_t zc_gfs_private;
84 timestruc_t zc_cmtime; /* ctime and mtime, always the same */
87 typedef struct zfsctl_snapdir {
88 zfsctl_node_t sd_node;
100 snapentry_compare(const void *a, const void *b)
102 const zfs_snapentry_t *sa = a;
103 const zfs_snapentry_t *sb = b;
104 int ret = strcmp(sa->se_name, sb->se_name);
114 static struct vop_vector zfsctl_ops_root;
115 static struct vop_vector zfsctl_ops_snapdir;
116 static struct vop_vector zfsctl_ops_snapshot;
117 static struct vop_vector zfsctl_ops_shares;
118 static struct vop_vector zfsctl_ops_shares_dir;
120 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
121 static vnode_t *zfsctl_mknode_shares(vnode_t *);
122 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
123 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
126 * Root directory elements. We only have two entries
127 * snapshot and shares.
129 static gfs_dirent_t zfsctl_root_entries[] = {
130 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
131 { "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
135 /* include . and .. in the calculation */
136 #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \
137 sizeof (gfs_dirent_t)) + 1)
141 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
142 * directories. This is called from the ZFS init routine, and initializes the
143 * vnode ops vectors that we'll be using.
156 * Return the inode number associated with the 'snapshot' or
157 * 'shares' directory.
161 zfsctl_root_inode_cb(vnode_t *vp, int index)
163 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
168 return (ZFSCTL_INO_SNAPDIR);
170 return (zfsvfs->z_shares_dir);
174 * Create the '.zfs' directory. This directory is cached as part of the VFS
175 * structure. This results in a hold on the vfs_t. The code in zfs_umount()
176 * therefore checks against a vfs_count of 2 instead of 1. This reference
177 * is removed when the ctldir is destroyed in the unmount.
180 zfsctl_create(zfsvfs_t *zfsvfs)
185 ASSERT(zfsvfs->z_ctldir == NULL);
187 vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
188 &zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
189 zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
191 zcp->zc_id = ZFSCTL_INO_ROOT;
193 VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp) == 0);
194 ZFS_TIME_DECODE(&zcp->zc_cmtime, VTOZ(rvp)->z_phys->zp_crtime);
198 * We're only faking the fact that we have a root of a filesystem for
199 * the sake of the GFS interfaces. Undo the flag manipulation it did
202 vp->v_vflag &= ~VV_ROOT;
204 zfsvfs->z_ctldir = vp;
210 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
211 * There might still be more references if we were force unmounted, but only
212 * new zfs_inactive() calls can occur and they don't reference .zfs
215 zfsctl_destroy(zfsvfs_t *zfsvfs)
217 VN_RELE(zfsvfs->z_ctldir);
218 zfsvfs->z_ctldir = NULL;
222 * Given a root znode, retrieve the associated .zfs directory.
223 * Add a hold to the vnode and return it.
226 zfsctl_root(znode_t *zp)
228 ASSERT(zfs_has_ctldir(zp));
229 VN_HOLD(zp->z_zfsvfs->z_ctldir);
230 return (zp->z_zfsvfs->z_ctldir);
234 * Common open routine. Disallow any write access.
238 zfsctl_common_open(struct vop_open_args *ap)
240 int flags = ap->a_mode;
249 * Common close routine. Nothing to do here.
253 zfsctl_common_close(struct vop_close_args *ap)
259 * Common access routine. Disallow writes.
263 zfsctl_common_access(ap)
264 struct vop_access_args /* {
267 struct ucred *a_cred;
271 int mode = ap->a_accmode;
274 if (flags & V_ACE_MASK) {
275 if (accmode & ACE_ALL_WRITE_PERMS)
289 * Common getattr function. Fill in basic information.
292 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
294 zfsctl_node_t *zcp = vp->v_data;
301 * We are a purly virtual object, so we have no
302 * blocksize or allocated blocks.
307 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
308 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
312 * We live in the now (for atime).
316 vap->va_mtime = vap->va_ctime = vap->va_birthtime = zcp->zc_cmtime;
317 /* FreeBSD: Reset chflags(2) flags. */
323 zfsctl_common_fid(ap)
324 struct vop_fid_args /* {
329 vnode_t *vp = ap->a_vp;
330 fid_t *fidp = (void *)ap->a_fid;
331 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
332 zfsctl_node_t *zcp = vp->v_data;
333 uint64_t object = zcp->zc_id;
339 fidp->fid_len = SHORT_FID_LEN;
341 zfid = (zfid_short_t *)fidp;
343 zfid->zf_len = SHORT_FID_LEN;
345 for (i = 0; i < sizeof (zfid->zf_object); i++)
346 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
348 /* .zfs znodes always have a generation number of 0 */
349 for (i = 0; i < sizeof (zfid->zf_gen); i++)
358 zfsctl_shares_fid(ap)
359 struct vop_fid_args /* {
364 vnode_t *vp = ap->a_vp;
365 fid_t *fidp = (void *)ap->a_fid;
366 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
372 if (zfsvfs->z_shares_dir == 0) {
377 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
378 error = VOP_FID(ZTOV(dzp), fidp);
387 zfsctl_common_reclaim(ap)
388 struct vop_reclaim_args /* {
393 vnode_t *vp = ap->a_vp;
396 * Destroy the vm object and flush associated pages.
398 vnode_destroy_vobject(vp);
406 * .zfs inode namespace
408 * We need to generate unique inode numbers for all files and directories
409 * within the .zfs pseudo-filesystem. We use the following scheme:
414 * .zfs/snapshot/<snap> objectid(snap)
417 #define ZFSCTL_INO_SNAP(id) (id)
420 * Get root directory attributes.
424 zfsctl_root_getattr(ap)
425 struct vop_getattr_args /* {
428 struct ucred *a_cred;
432 struct vnode *vp = ap->a_vp;
433 struct vattr *vap = ap->a_vap;
434 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
437 vap->va_nodeid = ZFSCTL_INO_ROOT;
438 vap->va_nlink = vap->va_size = NROOT_ENTRIES;
440 zfsctl_common_getattr(vp, vap);
448 zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
449 caller_context_t *ct)
452 * We only care about ACL_ENABLED so that libsec can
453 * display ACL correctly and not default to POSIX draft.
455 if (cmd == _PC_ACL_ENABLED) {
456 *valp = _ACL_ACE_ENABLED;
460 return (fs_pathconf(vp, cmd, valp, cr, ct));
465 static const fs_operation_def_t zfsctl_tops_root[] = {
466 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
467 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
468 { VOPNAME_IOCTL, { .error = fs_inval } },
469 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } },
470 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
471 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
472 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } },
473 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
474 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
475 { VOPNAME_PATHCONF, { .vop_pathconf = zfsctl_pathconf } },
476 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } },
482 * Special case the handling of "..".
486 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
487 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
488 int *direntflags, pathname_t *realpnp)
490 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
494 * No extended attributes allowed under .zfs
496 if (flags & LOOKUP_XATTR)
501 if (strcmp(nm, "..") == 0) {
502 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp);
506 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
507 cr, ct, direntflags, realpnp);
516 * Special case the handling of "..".
520 zfsctl_freebsd_root_lookup(ap)
521 struct vop_lookup_args /* {
523 struct vnode **a_vpp;
524 struct componentname *a_cnp;
527 vnode_t *dvp = ap->a_dvp;
528 vnode_t **vpp = ap->a_vpp;
529 cred_t *cr = ap->a_cnp->cn_cred;
530 int flags = ap->a_cnp->cn_flags;
531 int nameiop = ap->a_cnp->cn_nameiop;
532 char nm[NAME_MAX + 1];
535 if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
538 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
539 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
541 err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL);
542 if (err == 0 && (nm[0] != '.' || nm[1] != '\0'))
543 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
548 static struct vop_vector zfsctl_ops_root = {
549 .vop_default = &default_vnodeops,
550 .vop_open = zfsctl_common_open,
551 .vop_close = zfsctl_common_close,
552 .vop_ioctl = VOP_EINVAL,
553 .vop_getattr = zfsctl_root_getattr,
554 .vop_access = zfsctl_common_access,
555 .vop_readdir = gfs_vop_readdir,
556 .vop_lookup = zfsctl_freebsd_root_lookup,
557 .vop_inactive = gfs_vop_inactive,
558 .vop_reclaim = zfsctl_common_reclaim,
559 .vop_fid = zfsctl_common_fid,
563 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
565 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
567 if (snapshot_namecheck(name, NULL, NULL) != 0)
569 dmu_objset_name(os, zname);
570 if (strlen(zname) + 1 + strlen(name) >= len)
571 return (ENAMETOOLONG);
572 (void) strcat(zname, "@");
573 (void) strcat(zname, name);
578 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
580 vnode_t *svp = sep->se_root;
583 ASSERT(vn_ismntpt(svp));
585 /* this will be dropped by dounmount() */
586 if ((error = vn_vfswlock(svp)) != 0)
589 return (dounmount(vn_mountedvfs(svp), fflags, curthread));
594 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
599 char newpath[MAXNAMELEN];
602 ASSERT(MUTEX_HELD(&sdp->sd_lock));
605 vfsp = vn_mountedvfs(sep->se_root);
606 ASSERT(vfsp != NULL);
611 * Change the name in the AVL tree.
613 avl_remove(&sdp->sd_snaps, sep);
614 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
615 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
616 (void) strcpy(sep->se_name, nm);
617 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
618 avl_insert(&sdp->sd_snaps, sep, where);
621 * Change the current mountpoint info:
622 * - update the tail of the mntpoint path
623 * - update the tail of the resource path
625 pathref = vfs_getmntpoint(vfsp);
626 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
627 VERIFY((tail = strrchr(newpath, '/')) != NULL);
629 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
630 (void) strcat(newpath, nm);
631 refstr_rele(pathref);
632 vfs_setmntpoint(vfsp, newpath);
634 pathref = vfs_getresource(vfsp);
635 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
636 VERIFY((tail = strrchr(newpath, '@')) != NULL);
638 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
639 (void) strcat(newpath, nm);
640 refstr_rele(pathref);
641 vfs_setresource(vfsp, newpath);
650 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
651 cred_t *cr, caller_context_t *ct, int flags)
653 zfsctl_snapdir_t *sdp = sdvp->v_data;
654 zfs_snapentry_t search, *sep;
657 char from[MAXNAMELEN], to[MAXNAMELEN];
658 char real[MAXNAMELEN];
661 zfsvfs = sdvp->v_vfsp->vfs_data;
664 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
665 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
669 } else if (err != ENOTSUP) {
677 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
679 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
681 err = zfs_secpolicy_rename_perms(from, to, cr);
686 * Cannot move snapshots out of the snapdir.
691 if (strcmp(snm, tnm) == 0)
694 mutex_enter(&sdp->sd_lock);
696 search.se_name = (char *)snm;
697 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
698 mutex_exit(&sdp->sd_lock);
702 err = dmu_objset_rename(from, to, B_FALSE);
704 zfsctl_rename_snap(sdp, sep, tnm);
706 mutex_exit(&sdp->sd_lock);
715 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
716 caller_context_t *ct, int flags)
718 zfsctl_snapdir_t *sdp = dvp->v_data;
719 zfs_snapentry_t *sep;
720 zfs_snapentry_t search;
722 char snapname[MAXNAMELEN];
723 char real[MAXNAMELEN];
726 zfsvfs = dvp->v_vfsp->vfs_data;
729 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
731 err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
735 } else if (err != ENOTSUP) {
743 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
745 err = zfs_secpolicy_destroy_perms(snapname, cr);
749 mutex_enter(&sdp->sd_lock);
751 search.se_name = name;
752 sep = avl_find(&sdp->sd_snaps, &search, NULL);
754 avl_remove(&sdp->sd_snaps, sep);
755 err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
759 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
760 avl_insert(&sdp->sd_snaps, sep, where);
762 err = dmu_objset_destroy(snapname);
767 mutex_exit(&sdp->sd_lock);
774 * This creates a snapshot under '.zfs/snapshot'.
778 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp,
779 cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
781 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
782 char name[MAXNAMELEN];
784 static enum symfollow follow = NO_FOLLOW;
785 static enum uio_seg seg = UIO_SYSSPACE;
787 if (snapshot_namecheck(dirname, NULL, NULL) != 0)
790 dmu_objset_name(zfsvfs->z_os, name);
794 err = zfs_secpolicy_snapshot_perms(name, cr);
799 err = dmu_objset_snapshot(name, dirname, NULL, B_FALSE);
802 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
809 zfsctl_freebsd_snapdir_mkdir(ap)
810 struct vop_mkdir_args /* {
812 struct vnode **a_vpp;
813 struct componentname *a_cnp;
818 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
820 return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL,
821 ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL));
825 * Lookup entry point for the 'snapshot' directory. Try to open the
826 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
827 * Perform a mount of the associated dataset on top of the vnode.
831 zfsctl_snapdir_lookup(ap)
832 struct vop_lookup_args /* {
834 struct vnode **a_vpp;
835 struct componentname *a_cnp;
838 vnode_t *dvp = ap->a_dvp;
839 vnode_t **vpp = ap->a_vpp;
840 struct componentname *cnp = ap->a_cnp;
841 char nm[NAME_MAX + 1];
842 zfsctl_snapdir_t *sdp = dvp->v_data;
844 char snapname[MAXNAMELEN];
845 char real[MAXNAMELEN];
847 zfs_snapentry_t *sep, search;
848 size_t mountpoint_len;
850 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
855 * No extended attributes allowed under .zfs
857 if (flags & LOOKUP_XATTR)
859 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
860 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
862 ASSERT(dvp->v_type == VDIR);
867 * If we get a recursive call, that means we got called
868 * from the domount() code while it was trying to look up the
869 * spec (which looks like a local path for zfs). We need to
870 * add some flag to domount() to tell it not to do this lookup.
872 if (MUTEX_HELD(&sdp->sd_lock))
877 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
882 if (flags & FIGNORECASE) {
883 boolean_t conflict = B_FALSE;
885 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
886 MAXNAMELEN, &conflict);
888 strlcpy(nm, real, sizeof(nm));
889 } else if (err != ENOTSUP) {
895 (void) strlcpy(realpnp->pn_buf, nm,
896 realpnp->pn_bufsize);
897 if (conflict && direntflags)
898 *direntflags = ED_CASE_CONFLICT;
902 mutex_enter(&sdp->sd_lock);
903 search.se_name = (char *)nm;
904 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
907 err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY);
911 } else if (*vpp == sep->se_root) {
913 * The snapshot was unmounted behind our backs,
919 * VROOT was set during the traverse call. We need
920 * to clear it since we're pretending to be part
921 * of our parent's vfs.
923 (*vpp)->v_flag &= ~VROOT;
925 mutex_exit(&sdp->sd_lock);
931 * The requested snapshot is not currently mounted, look it up.
933 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
935 mutex_exit(&sdp->sd_lock);
938 * handle "ls *" or "?" in a graceful manner,
939 * forcing EILSEQ to ENOENT.
940 * Since shell ultimately passes "*" or "?" as name to lookup
942 return (err == EILSEQ ? ENOENT : err);
944 if (dmu_objset_open(snapname, DMU_OST_ZFS,
945 DS_MODE_USER | DS_MODE_READONLY, &snap) != 0) {
946 mutex_exit(&sdp->sd_lock);
947 /* Translate errors and add SAVENAME when needed. */
948 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
950 cnp->cn_flags |= SAVENAME;
958 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
959 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
960 (void) strcpy(sep->se_name, nm);
961 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
963 avl_insert(&sdp->sd_snaps, sep, where);
965 dmu_objset_close(snap);
967 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
968 strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(nm) + 1;
969 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
970 (void) snprintf(mountpoint, mountpoint_len,
971 "%s/" ZFS_CTLDIR_NAME "/snapshot/%s",
972 dvp->v_vfsp->mnt_stat.f_mntonname, nm);
973 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0);
974 kmem_free(mountpoint, mountpoint_len);
977 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
979 * This is where we lie about our v_vfsp in order to
980 * make .zfs/snapshot/<snapname> accessible over NFS
981 * without requiring manual mounts of <snapname>.
983 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
984 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
986 mutex_exit(&sdp->sd_lock);
995 zfsctl_shares_lookup(ap)
996 struct vop_lookup_args /* {
998 struct vnode **a_vpp;
999 struct componentname *a_cnp;
1002 vnode_t *dvp = ap->a_dvp;
1003 vnode_t **vpp = ap->a_vpp;
1004 struct componentname *cnp = ap->a_cnp;
1005 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1006 char nm[NAME_MAX + 1];
1012 ASSERT(cnp->cn_namelen < sizeof(nm));
1013 strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1);
1015 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1020 if (zfsvfs->z_shares_dir == 0) {
1024 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
1025 error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp);
1035 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1036 offset_t *offp, offset_t *nextp, void *data, int flags)
1038 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1039 char snapname[MAXNAMELEN];
1040 uint64_t id, cookie;
1041 boolean_t case_conflict;
1047 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
1048 &cookie, &case_conflict);
1051 if (error == ENOENT) {
1058 if (flags & V_RDDIR_ENTFLAGS) {
1059 edirent_t *eodp = dp;
1061 (void) strcpy(eodp->ed_name, snapname);
1062 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1063 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1065 struct dirent64 *odp = dp;
1067 (void) strcpy(odp->d_name, snapname);
1068 odp->d_ino = ZFSCTL_INO_SNAP(id);
1079 zfsctl_shares_readdir(ap)
1080 struct vop_readdir_args /* {
1083 struct ucred *a_cred;
1089 vnode_t *vp = ap->a_vp;
1090 uio_t *uiop = ap->a_uio;
1091 cred_t *cr = ap->a_cred;
1092 int *eofp = ap->a_eofflag;
1093 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1099 if (zfsvfs->z_shares_dir == 0) {
1103 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1104 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1105 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies);
1106 VN_URELE(ZTOV(dzp));
1117 * pvp is the '.zfs' directory (zfsctl_node_t).
1118 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1120 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1121 * when a lookup is performed on .zfs for "snapshot".
1124 zfsctl_mknode_snapdir(vnode_t *pvp)
1127 zfsctl_snapdir_t *sdp;
1129 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
1130 &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1131 zfsctl_snapdir_readdir_cb, NULL);
1133 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1134 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1135 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1136 avl_create(&sdp->sd_snaps, snapentry_compare,
1137 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1143 zfsctl_mknode_shares(vnode_t *pvp)
1148 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1149 &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1152 sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1160 zfsctl_shares_getattr(ap)
1161 struct vop_getattr_args /* {
1163 struct vattr *a_vap;
1164 struct ucred *a_cred;
1165 struct thread *a_td;
1168 vnode_t *vp = ap->a_vp;
1169 vattr_t *vap = ap->a_vap;
1170 cred_t *cr = ap->a_cred;
1171 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1176 if (zfsvfs->z_shares_dir == 0) {
1180 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1181 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1182 error = VOP_GETATTR(ZTOV(dzp), vap, cr);
1183 VN_URELE(ZTOV(dzp));
1191 zfsctl_snapdir_getattr(ap)
1192 struct vop_getattr_args /* {
1194 struct vattr *a_vap;
1195 struct ucred *a_cred;
1196 struct thread *a_td;
1199 struct vnode *vp = ap->a_vp;
1200 struct vattr *vap = ap->a_vap;
1201 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1202 zfsctl_snapdir_t *sdp = vp->v_data;
1205 zfsctl_common_getattr(vp, vap);
1206 vap->va_nodeid = gfs_file_inode(vp);
1207 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1215 zfsctl_snapdir_inactive(ap)
1216 struct vop_inactive_args /* {
1218 struct thread *a_td;
1221 vnode_t *vp = ap->a_vp;
1222 zfsctl_snapdir_t *sdp = vp->v_data;
1223 zfs_snapentry_t *sep;
1226 * On forced unmount we have to free snapshots from here.
1228 mutex_enter(&sdp->sd_lock);
1229 while ((sep = avl_first(&sdp->sd_snaps)) != NULL) {
1230 avl_remove(&sdp->sd_snaps, sep);
1231 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1232 kmem_free(sep, sizeof (zfs_snapentry_t));
1234 mutex_exit(&sdp->sd_lock);
1235 gfs_dir_inactive(vp);
1236 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1237 mutex_destroy(&sdp->sd_lock);
1238 avl_destroy(&sdp->sd_snaps);
1239 kmem_free(sdp, sizeof (zfsctl_snapdir_t));
1244 static struct vop_vector zfsctl_ops_snapdir = {
1245 .vop_default = &default_vnodeops,
1246 .vop_open = zfsctl_common_open,
1247 .vop_close = zfsctl_common_close,
1248 .vop_ioctl = VOP_EINVAL,
1249 .vop_getattr = zfsctl_snapdir_getattr,
1250 .vop_access = zfsctl_common_access,
1251 .vop_mkdir = zfsctl_freebsd_snapdir_mkdir,
1252 .vop_readdir = gfs_vop_readdir,
1253 .vop_lookup = zfsctl_snapdir_lookup,
1254 .vop_inactive = zfsctl_snapdir_inactive,
1255 .vop_reclaim = zfsctl_common_reclaim,
1256 .vop_fid = zfsctl_common_fid,
1259 static struct vop_vector zfsctl_ops_shares = {
1260 .vop_default = &default_vnodeops,
1261 .vop_open = zfsctl_common_open,
1262 .vop_close = zfsctl_common_close,
1263 .vop_ioctl = VOP_EINVAL,
1264 .vop_getattr = zfsctl_shares_getattr,
1265 .vop_access = zfsctl_common_access,
1266 .vop_readdir = zfsctl_shares_readdir,
1267 .vop_lookup = zfsctl_shares_lookup,
1268 .vop_inactive = gfs_vop_inactive,
1269 .vop_reclaim = zfsctl_common_reclaim,
1270 .vop_fid = zfsctl_shares_fid,
1274 * pvp is the GFS vnode '.zfs/snapshot'.
1276 * This creates a GFS node under '.zfs/snapshot' representing each
1277 * snapshot. This newly created GFS node is what we mount snapshot
1281 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1286 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1287 &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1290 zcp->zc_id = objset;
1297 zfsctl_snapshot_inactive(ap)
1298 struct vop_inactive_args /* {
1300 struct thread *a_td;
1303 vnode_t *vp = ap->a_vp;
1304 cred_t *cr = ap->a_td->td_ucred;
1305 struct vop_inactive_args iap;
1306 zfsctl_snapdir_t *sdp;
1307 zfs_snapentry_t *sep, *next;
1311 if (vp->v_count > 0)
1314 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1318 if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
1319 mutex_enter(&sdp->sd_lock);
1321 ASSERT(!vn_ismntpt(vp));
1323 sep = avl_first(&sdp->sd_snaps);
1324 while (sep != NULL) {
1325 next = AVL_NEXT(&sdp->sd_snaps, sep);
1327 if (sep->se_root == vp) {
1328 avl_remove(&sdp->sd_snaps, sep);
1329 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1330 kmem_free(sep, sizeof (zfs_snapentry_t));
1335 ASSERT(sep != NULL);
1338 mutex_exit(&sdp->sd_lock);
1343 * Dispose of the vnode for the snapshot mount point.
1344 * This is safe to do because once this entry has been removed
1345 * from the AVL tree, it can't be found again, so cannot become
1346 * "active". If we lookup the same name again we will end up
1347 * creating a new vnode.
1350 return (gfs_vop_inactive(&iap));
1354 zfsctl_traverse_begin(vnode_t **vpp, int lktype)
1358 /* Snapshot should be already mounted, but just in case. */
1359 if (vn_mountedvfs(*vpp) == NULL)
1361 return (traverse(vpp, lktype));
1365 zfsctl_traverse_end(vnode_t *vp, int err)
1375 zfsctl_snapshot_getattr(ap)
1376 struct vop_getattr_args /* {
1378 struct vattr *a_vap;
1379 struct ucred *a_cred;
1382 vnode_t *vp = ap->a_vp;
1385 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1387 err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred);
1388 zfsctl_traverse_end(vp, err);
1393 zfsctl_snapshot_fid(ap)
1394 struct vop_fid_args /* {
1399 vnode_t *vp = ap->a_vp;
1402 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1404 err = VOP_VPTOFH(vp, (void *)ap->a_fid);
1405 zfsctl_traverse_end(vp, err);
1410 zfsctl_snapshot_lookup(ap)
1411 struct vop_lookup_args /* {
1412 struct vnode *a_dvp;
1413 struct vnode **a_vpp;
1414 struct componentname *a_cnp;
1417 vnode_t *dvp = ap->a_dvp;
1418 vnode_t **vpp = ap->a_vpp;
1419 struct componentname *cnp = ap->a_cnp;
1420 cred_t *cr = ap->a_cnp->cn_cred;
1421 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1424 if (cnp->cn_namelen != 2 || cnp->cn_nameptr[0] != '.' ||
1425 cnp->cn_nameptr[1] != '.') {
1429 ASSERT(dvp->v_type == VDIR);
1430 ASSERT(zfsvfs->z_ctldir != NULL);
1432 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", vpp,
1433 NULL, 0, NULL, cr, NULL, NULL, NULL);
1435 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1440 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1442 zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data;
1444 zfsctl_snapdir_t *sdp;
1445 zfs_snapentry_t *sep;
1448 ASSERT(zfsvfs->z_ctldir != NULL);
1449 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1450 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1455 mutex_enter(&sdp->sd_lock);
1456 sep = avl_first(&sdp->sd_snaps);
1457 while (sep != NULL) {
1461 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1464 mutex_exit(&sdp->sd_lock);
1469 len = strlen(sep->se_name);
1470 *ap->a_buflen -= len;
1471 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len);
1472 mutex_exit(&sdp->sd_lock);
1482 * These VP's should never see the light of day. They should always
1485 static struct vop_vector zfsctl_ops_snapshot = {
1486 .vop_default = &default_vnodeops,
1487 .vop_inactive = zfsctl_snapshot_inactive,
1488 .vop_lookup = zfsctl_snapshot_lookup,
1489 .vop_reclaim = zfsctl_common_reclaim,
1490 .vop_getattr = zfsctl_snapshot_getattr,
1491 .vop_fid = zfsctl_snapshot_fid,
1492 .vop_vptocnp = zfsctl_snapshot_vptocnp,
1496 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1498 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1500 zfsctl_snapdir_t *sdp;
1502 zfs_snapentry_t *sep;
1505 ASSERT(zfsvfs->z_ctldir != NULL);
1506 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1507 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1512 mutex_enter(&sdp->sd_lock);
1513 sep = avl_first(&sdp->sd_snaps);
1514 while (sep != NULL) {
1517 if (zcp->zc_id == objsetid)
1520 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1526 * Return the mounted root rather than the covered mount point.
1527 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1528 * and returns the ZFS vnode mounted on top of the GFS node.
1529 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1531 error = traverse(&vp, LK_SHARED | LK_RETRY);
1533 if (vp == sep->se_root)
1536 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1538 mutex_exit(&sdp->sd_lock);
1545 mutex_exit(&sdp->sd_lock);
1554 * Unmount any snapshots for the given filesystem. This is called from
1555 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1559 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1561 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1563 zfsctl_snapdir_t *sdp;
1564 zfs_snapentry_t *sep, *next;
1567 ASSERT(zfsvfs->z_ctldir != NULL);
1568 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1569 NULL, 0, NULL, cr, NULL, NULL, NULL);
1574 mutex_enter(&sdp->sd_lock);
1576 sep = avl_first(&sdp->sd_snaps);
1577 while (sep != NULL) {
1578 next = AVL_NEXT(&sdp->sd_snaps, sep);
1581 * If this snapshot is not mounted, then it must
1582 * have just been unmounted by somebody else, and
1583 * will be cleaned up by zfsctl_snapdir_inactive().
1585 if (vn_ismntpt(sep->se_root)) {
1586 error = zfsctl_unmount_snap(sep, fflags, cr);
1591 * Before reinserting snapshot to the tree,
1592 * check if it was actually removed. For example
1593 * when snapshot mount point is busy, we will
1594 * have an error here, but there will be no need
1595 * to reinsert snapshot.
1597 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
1598 avl_insert(&sdp->sd_snaps, sep, where);
1605 mutex_exit(&sdp->sd_lock);