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/snapshot/") + strlen(nm) + 1;
969 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
970 (void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
971 dvp->v_vfsp->mnt_stat.f_mntonname, nm);
972 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0);
973 kmem_free(mountpoint, mountpoint_len);
976 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
978 * This is where we lie about our v_vfsp in order to
979 * make .zfs/snapshot/<snapname> accessible over NFS
980 * without requiring manual mounts of <snapname>.
982 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
983 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
985 mutex_exit(&sdp->sd_lock);
994 zfsctl_shares_lookup(ap)
995 struct vop_lookup_args /* {
997 struct vnode **a_vpp;
998 struct componentname *a_cnp;
1001 vnode_t *dvp = ap->a_dvp;
1002 vnode_t **vpp = ap->a_vpp;
1003 struct componentname *cnp = ap->a_cnp;
1004 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1005 char nm[NAME_MAX + 1];
1011 ASSERT(cnp->cn_namelen < sizeof(nm));
1012 strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1);
1014 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1019 if (zfsvfs->z_shares_dir == 0) {
1023 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
1024 error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp);
1034 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1035 offset_t *offp, offset_t *nextp, void *data, int flags)
1037 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1038 char snapname[MAXNAMELEN];
1039 uint64_t id, cookie;
1040 boolean_t case_conflict;
1046 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
1047 &cookie, &case_conflict);
1050 if (error == ENOENT) {
1057 if (flags & V_RDDIR_ENTFLAGS) {
1058 edirent_t *eodp = dp;
1060 (void) strcpy(eodp->ed_name, snapname);
1061 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1062 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1064 struct dirent64 *odp = dp;
1066 (void) strcpy(odp->d_name, snapname);
1067 odp->d_ino = ZFSCTL_INO_SNAP(id);
1078 zfsctl_shares_readdir(ap)
1079 struct vop_readdir_args /* {
1082 struct ucred *a_cred;
1088 vnode_t *vp = ap->a_vp;
1089 uio_t *uiop = ap->a_uio;
1090 cred_t *cr = ap->a_cred;
1091 int *eofp = ap->a_eofflag;
1092 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1098 if (zfsvfs->z_shares_dir == 0) {
1102 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1103 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1104 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies);
1105 VN_URELE(ZTOV(dzp));
1116 * pvp is the '.zfs' directory (zfsctl_node_t).
1117 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1119 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1120 * when a lookup is performed on .zfs for "snapshot".
1123 zfsctl_mknode_snapdir(vnode_t *pvp)
1126 zfsctl_snapdir_t *sdp;
1128 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
1129 &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1130 zfsctl_snapdir_readdir_cb, NULL);
1132 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1133 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1134 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1135 avl_create(&sdp->sd_snaps, snapentry_compare,
1136 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1142 zfsctl_mknode_shares(vnode_t *pvp)
1147 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1148 &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1151 sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1159 zfsctl_shares_getattr(ap)
1160 struct vop_getattr_args /* {
1162 struct vattr *a_vap;
1163 struct ucred *a_cred;
1164 struct thread *a_td;
1167 vnode_t *vp = ap->a_vp;
1168 vattr_t *vap = ap->a_vap;
1169 cred_t *cr = ap->a_cred;
1170 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1175 if (zfsvfs->z_shares_dir == 0) {
1179 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1180 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1181 error = VOP_GETATTR(ZTOV(dzp), vap, cr);
1182 VN_URELE(ZTOV(dzp));
1190 zfsctl_snapdir_getattr(ap)
1191 struct vop_getattr_args /* {
1193 struct vattr *a_vap;
1194 struct ucred *a_cred;
1195 struct thread *a_td;
1198 struct vnode *vp = ap->a_vp;
1199 struct vattr *vap = ap->a_vap;
1200 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1201 zfsctl_snapdir_t *sdp = vp->v_data;
1204 zfsctl_common_getattr(vp, vap);
1205 vap->va_nodeid = gfs_file_inode(vp);
1206 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1214 zfsctl_snapdir_inactive(ap)
1215 struct vop_inactive_args /* {
1217 struct thread *a_td;
1220 vnode_t *vp = ap->a_vp;
1221 zfsctl_snapdir_t *sdp = vp->v_data;
1222 zfs_snapentry_t *sep;
1225 * On forced unmount we have to free snapshots from here.
1227 mutex_enter(&sdp->sd_lock);
1228 while ((sep = avl_first(&sdp->sd_snaps)) != NULL) {
1229 avl_remove(&sdp->sd_snaps, sep);
1230 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1231 kmem_free(sep, sizeof (zfs_snapentry_t));
1233 mutex_exit(&sdp->sd_lock);
1234 gfs_dir_inactive(vp);
1235 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1236 mutex_destroy(&sdp->sd_lock);
1237 avl_destroy(&sdp->sd_snaps);
1238 kmem_free(sdp, sizeof (zfsctl_snapdir_t));
1243 static struct vop_vector zfsctl_ops_snapdir = {
1244 .vop_default = &default_vnodeops,
1245 .vop_open = zfsctl_common_open,
1246 .vop_close = zfsctl_common_close,
1247 .vop_ioctl = VOP_EINVAL,
1248 .vop_getattr = zfsctl_snapdir_getattr,
1249 .vop_access = zfsctl_common_access,
1250 .vop_mkdir = zfsctl_freebsd_snapdir_mkdir,
1251 .vop_readdir = gfs_vop_readdir,
1252 .vop_lookup = zfsctl_snapdir_lookup,
1253 .vop_inactive = zfsctl_snapdir_inactive,
1254 .vop_reclaim = zfsctl_common_reclaim,
1255 .vop_fid = zfsctl_common_fid,
1258 static struct vop_vector zfsctl_ops_shares = {
1259 .vop_default = &default_vnodeops,
1260 .vop_open = zfsctl_common_open,
1261 .vop_close = zfsctl_common_close,
1262 .vop_ioctl = VOP_EINVAL,
1263 .vop_getattr = zfsctl_shares_getattr,
1264 .vop_access = zfsctl_common_access,
1265 .vop_readdir = zfsctl_shares_readdir,
1266 .vop_lookup = zfsctl_shares_lookup,
1267 .vop_inactive = gfs_vop_inactive,
1268 .vop_reclaim = zfsctl_common_reclaim,
1269 .vop_fid = zfsctl_shares_fid,
1273 * pvp is the GFS vnode '.zfs/snapshot'.
1275 * This creates a GFS node under '.zfs/snapshot' representing each
1276 * snapshot. This newly created GFS node is what we mount snapshot
1280 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1285 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1286 &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1289 zcp->zc_id = objset;
1296 zfsctl_snapshot_inactive(ap)
1297 struct vop_inactive_args /* {
1299 struct thread *a_td;
1302 vnode_t *vp = ap->a_vp;
1303 cred_t *cr = ap->a_td->td_ucred;
1304 struct vop_inactive_args iap;
1305 zfsctl_snapdir_t *sdp;
1306 zfs_snapentry_t *sep, *next;
1310 if (vp->v_count > 0)
1313 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1317 if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
1318 mutex_enter(&sdp->sd_lock);
1320 ASSERT(!vn_ismntpt(vp));
1322 sep = avl_first(&sdp->sd_snaps);
1323 while (sep != NULL) {
1324 next = AVL_NEXT(&sdp->sd_snaps, sep);
1326 if (sep->se_root == vp) {
1327 avl_remove(&sdp->sd_snaps, sep);
1328 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1329 kmem_free(sep, sizeof (zfs_snapentry_t));
1334 ASSERT(sep != NULL);
1337 mutex_exit(&sdp->sd_lock);
1342 * Dispose of the vnode for the snapshot mount point.
1343 * This is safe to do because once this entry has been removed
1344 * from the AVL tree, it can't be found again, so cannot become
1345 * "active". If we lookup the same name again we will end up
1346 * creating a new vnode.
1349 return (gfs_vop_inactive(&iap));
1353 zfsctl_traverse_begin(vnode_t **vpp, int lktype)
1357 /* Snapshot should be already mounted, but just in case. */
1358 if (vn_mountedvfs(*vpp) == NULL)
1360 return (traverse(vpp, lktype));
1364 zfsctl_traverse_end(vnode_t *vp, int err)
1374 zfsctl_snapshot_getattr(ap)
1375 struct vop_getattr_args /* {
1377 struct vattr *a_vap;
1378 struct ucred *a_cred;
1381 vnode_t *vp = ap->a_vp;
1384 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1386 err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred);
1387 zfsctl_traverse_end(vp, err);
1392 zfsctl_snapshot_fid(ap)
1393 struct vop_fid_args /* {
1398 vnode_t *vp = ap->a_vp;
1401 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1403 err = VOP_VPTOFH(vp, (void *)ap->a_fid);
1404 zfsctl_traverse_end(vp, err);
1409 zfsctl_snapshot_lookup(ap)
1410 struct vop_lookup_args /* {
1411 struct vnode *a_dvp;
1412 struct vnode **a_vpp;
1413 struct componentname *a_cnp;
1416 vnode_t *dvp = ap->a_dvp;
1417 vnode_t **vpp = ap->a_vpp;
1418 struct componentname *cnp = ap->a_cnp;
1419 cred_t *cr = ap->a_cnp->cn_cred;
1420 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1423 if (cnp->cn_namelen != 2 || cnp->cn_nameptr[0] != '.' ||
1424 cnp->cn_nameptr[1] != '.') {
1428 ASSERT(dvp->v_type == VDIR);
1429 ASSERT(zfsvfs->z_ctldir != NULL);
1431 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", vpp,
1432 NULL, 0, NULL, cr, NULL, NULL, NULL);
1434 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1439 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1441 zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data;
1443 zfsctl_snapdir_t *sdp;
1444 zfs_snapentry_t *sep;
1447 ASSERT(zfsvfs->z_ctldir != NULL);
1448 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1449 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1454 mutex_enter(&sdp->sd_lock);
1455 sep = avl_first(&sdp->sd_snaps);
1456 while (sep != NULL) {
1460 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1463 mutex_exit(&sdp->sd_lock);
1468 len = strlen(sep->se_name);
1469 *ap->a_buflen -= len;
1470 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len);
1471 mutex_exit(&sdp->sd_lock);
1481 * These VP's should never see the light of day. They should always
1484 static struct vop_vector zfsctl_ops_snapshot = {
1485 .vop_default = &default_vnodeops,
1486 .vop_inactive = zfsctl_snapshot_inactive,
1487 .vop_lookup = zfsctl_snapshot_lookup,
1488 .vop_reclaim = zfsctl_common_reclaim,
1489 .vop_getattr = zfsctl_snapshot_getattr,
1490 .vop_fid = zfsctl_snapshot_fid,
1491 .vop_vptocnp = zfsctl_snapshot_vptocnp,
1495 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1497 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1499 zfsctl_snapdir_t *sdp;
1501 zfs_snapentry_t *sep;
1504 ASSERT(zfsvfs->z_ctldir != NULL);
1505 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1506 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1511 mutex_enter(&sdp->sd_lock);
1512 sep = avl_first(&sdp->sd_snaps);
1513 while (sep != NULL) {
1516 if (zcp->zc_id == objsetid)
1519 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1525 * Return the mounted root rather than the covered mount point.
1526 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1527 * and returns the ZFS vnode mounted on top of the GFS node.
1528 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1530 error = traverse(&vp, LK_SHARED | LK_RETRY);
1532 if (vp == sep->se_root)
1535 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1537 mutex_exit(&sdp->sd_lock);
1544 mutex_exit(&sdp->sd_lock);
1553 * Unmount any snapshots for the given filesystem. This is called from
1554 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1558 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1560 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1562 zfsctl_snapdir_t *sdp;
1563 zfs_snapentry_t *sep, *next;
1566 ASSERT(zfsvfs->z_ctldir != NULL);
1567 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1568 NULL, 0, NULL, cr, NULL, NULL, NULL);
1573 mutex_enter(&sdp->sd_lock);
1575 sep = avl_first(&sdp->sd_snaps);
1576 while (sep != NULL) {
1577 next = AVL_NEXT(&sdp->sd_snaps, sep);
1580 * If this snapshot is not mounted, then it must
1581 * have just been unmounted by somebody else, and
1582 * will be cleaned up by zfsctl_snapdir_inactive().
1584 if (vn_ismntpt(sep->se_root)) {
1585 error = zfsctl_unmount_snap(sep, fflags, cr);
1590 * Before reinserting snapshot to the tree,
1591 * check if it was actually removed. For example
1592 * when snapshot mount point is busy, we will
1593 * have an error here, but there will be no need
1594 * to reinsert snapshot.
1596 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
1597 avl_insert(&sdp->sd_snaps, sep, where);
1604 mutex_exit(&sdp->sd_lock);