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.
26 * ZFS control directory (a.k.a. ".zfs")
28 * This directory provides a common location for all ZFS meta-objects.
29 * Currently, this is only the 'snapshot' directory, but this may expand in the
30 * future. The elements are built using the GFS primitives, as the hierarchy
31 * does not actually exist on disk.
33 * For 'snapshot', we don't want to have all snapshots always mounted, because
34 * this would take up a huge amount of space in /etc/mnttab. We have three
37 * ctldir ------> snapshotdir -------> snapshot
43 * The 'snapshot' node contains just enough information to lookup '..' and act
44 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
45 * perform an automount of the underlying filesystem and return the
46 * corresponding vnode.
48 * All mounts are handled automatically by the kernel, but unmounts are
49 * (currently) handled from user land. The main reason is that there is no
50 * reliable way to auto-unmount the filesystem when it's "no longer in use".
51 * When the user unmounts a filesystem, we call zfsctl_unmount(), which
52 * unmounts any snapshots within the snapshot directory.
54 * The '.zfs', '.zfs/snapshot', and all directories created under
55 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
56 * share the same vfs_t as the head filesystem (what '.zfs' lives under).
58 * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
59 * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
60 * However, vnodes within these mounted on file systems have their v_vfsp
61 * fields set to the head filesystem to make NFS happy (see
62 * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
63 * so that it cannot be freed until all snapshots have been unmounted.
66 #include <sys/zfs_context.h>
67 #include <sys/zfs_ctldir.h>
68 #include <sys/zfs_ioctl.h>
69 #include <sys/zfs_vfsops.h>
70 #include <sys/namei.h>
74 #include <sys/dsl_deleg.h>
75 #include <sys/mount.h>
76 #include <sys/sunddi.h>
78 #include "zfs_namecheck.h"
80 typedef struct zfsctl_node {
81 gfs_dir_t zc_gfs_private;
83 timestruc_t zc_cmtime; /* ctime and mtime, always the same */
86 typedef struct zfsctl_snapdir {
87 zfsctl_node_t sd_node;
99 snapentry_compare(const void *a, const void *b)
101 const zfs_snapentry_t *sa = a;
102 const zfs_snapentry_t *sb = b;
103 int ret = strcmp(sa->se_name, sb->se_name);
113 static struct vop_vector zfsctl_ops_root;
114 static struct vop_vector zfsctl_ops_snapdir;
115 static struct vop_vector zfsctl_ops_snapshot;
116 static struct vop_vector zfsctl_ops_shares;
117 static struct vop_vector zfsctl_ops_shares_dir;
119 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
120 static vnode_t *zfsctl_mknode_shares(vnode_t *);
121 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
122 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
125 * Root directory elements. We only have two entries
126 * snapshot and shares.
128 static gfs_dirent_t zfsctl_root_entries[] = {
129 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
130 { "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
134 /* include . and .. in the calculation */
135 #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \
136 sizeof (gfs_dirent_t)) + 1)
140 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
141 * directories. This is called from the ZFS init routine, and initializes the
142 * vnode ops vectors that we'll be using.
155 zfsctl_is_node(vnode_t *vp)
157 return (vn_matchops(vp, zfsctl_ops_root) ||
158 vn_matchops(vp, zfsctl_ops_snapdir) ||
159 vn_matchops(vp, zfsctl_ops_snapshot) ||
160 vn_matchops(vp, zfsctl_ops_shares) ||
161 vn_matchops(vp, zfsctl_ops_shares_dir));
166 * Return the inode number associated with the 'snapshot' or
167 * 'shares' directory.
171 zfsctl_root_inode_cb(vnode_t *vp, int index)
173 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
178 return (ZFSCTL_INO_SNAPDIR);
180 return (zfsvfs->z_shares_dir);
184 * Create the '.zfs' directory. This directory is cached as part of the VFS
185 * structure. This results in a hold on the vfs_t. The code in zfs_umount()
186 * therefore checks against a vfs_count of 2 instead of 1. This reference
187 * is removed when the ctldir is destroyed in the unmount.
190 zfsctl_create(zfsvfs_t *zfsvfs)
195 ASSERT(zfsvfs->z_ctldir == NULL);
197 vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
198 &zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
199 zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
201 zcp->zc_id = ZFSCTL_INO_ROOT;
203 VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp) == 0);
204 ZFS_TIME_DECODE(&zcp->zc_cmtime, VTOZ(rvp)->z_phys->zp_crtime);
208 * We're only faking the fact that we have a root of a filesystem for
209 * the sake of the GFS interfaces. Undo the flag manipulation it did
212 vp->v_vflag &= ~VV_ROOT;
214 zfsvfs->z_ctldir = vp;
220 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
221 * There might still be more references if we were force unmounted, but only
222 * new zfs_inactive() calls can occur and they don't reference .zfs
225 zfsctl_destroy(zfsvfs_t *zfsvfs)
227 VN_RELE(zfsvfs->z_ctldir);
228 zfsvfs->z_ctldir = NULL;
232 * Given a root znode, retrieve the associated .zfs directory.
233 * Add a hold to the vnode and return it.
236 zfsctl_root(znode_t *zp)
238 ASSERT(zfs_has_ctldir(zp));
239 VN_HOLD(zp->z_zfsvfs->z_ctldir);
240 return (zp->z_zfsvfs->z_ctldir);
244 * Common open routine. Disallow any write access.
248 zfsctl_common_open(struct vop_open_args *ap)
250 int flags = ap->a_mode;
259 * Common close routine. Nothing to do here.
263 zfsctl_common_close(struct vop_close_args *ap)
269 * Common access routine. Disallow writes.
273 zfsctl_common_access(ap)
274 struct vop_access_args /* {
277 struct ucred *a_cred;
281 int mode = ap->a_accmode;
284 if (flags & V_ACE_MASK) {
285 if (accmode & ACE_ALL_WRITE_PERMS)
299 * Common getattr function. Fill in basic information.
302 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
304 zfsctl_node_t *zcp = vp->v_data;
311 * We are a purly virtual object, so we have no
312 * blocksize or allocated blocks.
317 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
318 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
322 * We live in the now (for atime).
326 vap->va_mtime = vap->va_ctime = vap->va_birthtime = zcp->zc_cmtime;
327 /* FreeBSD: Reset chflags(2) flags. */
333 zfsctl_common_fid(ap)
334 struct vop_fid_args /* {
339 vnode_t *vp = ap->a_vp;
340 fid_t *fidp = (void *)ap->a_fid;
341 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
342 zfsctl_node_t *zcp = vp->v_data;
343 uint64_t object = zcp->zc_id;
349 fidp->fid_len = SHORT_FID_LEN;
351 zfid = (zfid_short_t *)fidp;
353 zfid->zf_len = SHORT_FID_LEN;
355 for (i = 0; i < sizeof (zfid->zf_object); i++)
356 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
358 /* .zfs znodes always have a generation number of 0 */
359 for (i = 0; i < sizeof (zfid->zf_gen); i++)
368 zfsctl_shares_fid(ap)
369 struct vop_fid_args /* {
374 vnode_t *vp = ap->a_vp;
375 fid_t *fidp = (void *)ap->a_fid;
376 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
382 if (zfsvfs->z_shares_dir == 0) {
387 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
388 error = VOP_FID(ZTOV(dzp), fidp);
397 zfsctl_common_reclaim(ap)
398 struct vop_reclaim_args /* {
403 vnode_t *vp = ap->a_vp;
406 * Destroy the vm object and flush associated pages.
408 vnode_destroy_vobject(vp);
416 * .zfs inode namespace
418 * We need to generate unique inode numbers for all files and directories
419 * within the .zfs pseudo-filesystem. We use the following scheme:
424 * .zfs/snapshot/<snap> objectid(snap)
427 #define ZFSCTL_INO_SNAP(id) (id)
430 * Get root directory attributes.
434 zfsctl_root_getattr(ap)
435 struct vop_getattr_args /* {
438 struct ucred *a_cred;
442 struct vnode *vp = ap->a_vp;
443 struct vattr *vap = ap->a_vap;
444 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
447 vap->va_nodeid = ZFSCTL_INO_ROOT;
448 vap->va_nlink = vap->va_size = NROOT_ENTRIES;
450 zfsctl_common_getattr(vp, vap);
458 zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
459 caller_context_t *ct)
462 * We only care about ACL_ENABLED so that libsec can
463 * display ACL correctly and not default to POSIX draft.
465 if (cmd == _PC_ACL_ENABLED) {
466 *valp = _ACL_ACE_ENABLED;
470 return (fs_pathconf(vp, cmd, valp, cr, ct));
475 static const fs_operation_def_t zfsctl_tops_root[] = {
476 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
477 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
478 { VOPNAME_IOCTL, { .error = fs_inval } },
479 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } },
480 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
481 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
482 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } },
483 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
484 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
485 { VOPNAME_PATHCONF, { .vop_pathconf = zfsctl_pathconf } },
486 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } },
492 * Special case the handling of "..".
496 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
497 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
498 int *direntflags, pathname_t *realpnp)
500 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
504 * No extended attributes allowed under .zfs
506 if (flags & LOOKUP_XATTR)
511 if (strcmp(nm, "..") == 0) {
512 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp);
516 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
517 cr, ct, direntflags, realpnp);
526 * Special case the handling of "..".
530 zfsctl_freebsd_root_lookup(ap)
531 struct vop_lookup_args /* {
533 struct vnode **a_vpp;
534 struct componentname *a_cnp;
537 vnode_t *dvp = ap->a_dvp;
538 vnode_t **vpp = ap->a_vpp;
539 cred_t *cr = ap->a_cnp->cn_cred;
540 int flags = ap->a_cnp->cn_flags;
541 int nameiop = ap->a_cnp->cn_nameiop;
542 char nm[NAME_MAX + 1];
545 if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
548 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
549 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
551 err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL);
552 if (err == 0 && (nm[0] != '.' || nm[1] != '\0'))
553 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
558 static struct vop_vector zfsctl_ops_root = {
559 .vop_default = &default_vnodeops,
560 .vop_open = zfsctl_common_open,
561 .vop_close = zfsctl_common_close,
562 .vop_ioctl = VOP_EINVAL,
563 .vop_getattr = zfsctl_root_getattr,
564 .vop_access = zfsctl_common_access,
565 .vop_readdir = gfs_vop_readdir,
566 .vop_lookup = zfsctl_freebsd_root_lookup,
567 .vop_inactive = gfs_vop_inactive,
568 .vop_reclaim = zfsctl_common_reclaim,
569 .vop_fid = zfsctl_common_fid,
573 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
575 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
577 if (snapshot_namecheck(name, NULL, NULL) != 0)
579 dmu_objset_name(os, zname);
580 if (strlen(zname) + 1 + strlen(name) >= len)
581 return (ENAMETOOLONG);
582 (void) strcat(zname, "@");
583 (void) strcat(zname, name);
588 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
590 vnode_t *svp = sep->se_root;
593 ASSERT(vn_ismntpt(svp));
595 /* this will be dropped by dounmount() */
596 if ((error = vn_vfswlock(svp)) != 0)
599 return (dounmount(vn_mountedvfs(svp), fflags, curthread));
604 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
609 char newpath[MAXNAMELEN];
612 ASSERT(MUTEX_HELD(&sdp->sd_lock));
615 vfsp = vn_mountedvfs(sep->se_root);
616 ASSERT(vfsp != NULL);
621 * Change the name in the AVL tree.
623 avl_remove(&sdp->sd_snaps, sep);
624 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
625 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
626 (void) strcpy(sep->se_name, nm);
627 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
628 avl_insert(&sdp->sd_snaps, sep, where);
631 * Change the current mountpoint info:
632 * - update the tail of the mntpoint path
633 * - update the tail of the resource path
635 pathref = vfs_getmntpoint(vfsp);
636 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
637 VERIFY((tail = strrchr(newpath, '/')) != NULL);
639 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
640 (void) strcat(newpath, nm);
641 refstr_rele(pathref);
642 vfs_setmntpoint(vfsp, newpath);
644 pathref = vfs_getresource(vfsp);
645 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
646 VERIFY((tail = strrchr(newpath, '@')) != NULL);
648 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
649 (void) strcat(newpath, nm);
650 refstr_rele(pathref);
651 vfs_setresource(vfsp, newpath);
660 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
661 cred_t *cr, caller_context_t *ct, int flags)
663 zfsctl_snapdir_t *sdp = sdvp->v_data;
664 zfs_snapentry_t search, *sep;
667 char from[MAXNAMELEN], to[MAXNAMELEN];
668 char real[MAXNAMELEN];
671 zfsvfs = sdvp->v_vfsp->vfs_data;
674 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
675 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
679 } else if (err != ENOTSUP) {
687 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
689 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
691 err = zfs_secpolicy_rename_perms(from, to, cr);
696 * Cannot move snapshots out of the snapdir.
701 if (strcmp(snm, tnm) == 0)
704 mutex_enter(&sdp->sd_lock);
706 search.se_name = (char *)snm;
707 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
708 mutex_exit(&sdp->sd_lock);
712 err = dmu_objset_rename(from, to, B_FALSE);
714 zfsctl_rename_snap(sdp, sep, tnm);
716 mutex_exit(&sdp->sd_lock);
725 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
726 caller_context_t *ct, int flags)
728 zfsctl_snapdir_t *sdp = dvp->v_data;
729 zfs_snapentry_t *sep;
730 zfs_snapentry_t search;
732 char snapname[MAXNAMELEN];
733 char real[MAXNAMELEN];
736 zfsvfs = dvp->v_vfsp->vfs_data;
739 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
741 err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
745 } else if (err != ENOTSUP) {
753 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
755 err = zfs_secpolicy_destroy_perms(snapname, cr);
759 mutex_enter(&sdp->sd_lock);
761 search.se_name = name;
762 sep = avl_find(&sdp->sd_snaps, &search, NULL);
764 avl_remove(&sdp->sd_snaps, sep);
765 err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
769 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
770 avl_insert(&sdp->sd_snaps, sep, where);
772 err = dmu_objset_destroy(snapname);
777 mutex_exit(&sdp->sd_lock);
784 * This creates a snapshot under '.zfs/snapshot'.
788 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp,
789 cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
791 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
792 char name[MAXNAMELEN];
794 static enum symfollow follow = NO_FOLLOW;
795 static enum uio_seg seg = UIO_SYSSPACE;
797 if (snapshot_namecheck(dirname, NULL, NULL) != 0)
800 dmu_objset_name(zfsvfs->z_os, name);
804 err = zfs_secpolicy_snapshot_perms(name, cr);
809 err = dmu_objset_snapshot(name, dirname, NULL, B_FALSE);
812 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
819 zfsctl_freebsd_snapdir_mkdir(ap)
820 struct vop_mkdir_args /* {
822 struct vnode **a_vpp;
823 struct componentname *a_cnp;
828 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
830 return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL,
831 ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL));
835 * Lookup entry point for the 'snapshot' directory. Try to open the
836 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
837 * Perform a mount of the associated dataset on top of the vnode.
841 zfsctl_snapdir_lookup(ap)
842 struct vop_lookup_args /* {
844 struct vnode **a_vpp;
845 struct componentname *a_cnp;
848 vnode_t *dvp = ap->a_dvp;
849 vnode_t **vpp = ap->a_vpp;
850 struct componentname *cnp = ap->a_cnp;
851 char nm[NAME_MAX + 1];
852 zfsctl_snapdir_t *sdp = dvp->v_data;
854 char snapname[MAXNAMELEN];
855 char real[MAXNAMELEN];
857 zfs_snapentry_t *sep, search;
858 size_t mountpoint_len;
860 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
865 * No extended attributes allowed under .zfs
867 if (flags & LOOKUP_XATTR)
869 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
870 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
872 ASSERT(dvp->v_type == VDIR);
877 * If we get a recursive call, that means we got called
878 * from the domount() code while it was trying to look up the
879 * spec (which looks like a local path for zfs). We need to
880 * add some flag to domount() to tell it not to do this lookup.
882 if (MUTEX_HELD(&sdp->sd_lock))
887 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
892 if (flags & FIGNORECASE) {
893 boolean_t conflict = B_FALSE;
895 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
896 MAXNAMELEN, &conflict);
898 strlcpy(nm, real, sizeof(nm));
899 } else if (err != ENOTSUP) {
905 (void) strlcpy(realpnp->pn_buf, nm,
906 realpnp->pn_bufsize);
907 if (conflict && direntflags)
908 *direntflags = ED_CASE_CONFLICT;
912 mutex_enter(&sdp->sd_lock);
913 search.se_name = (char *)nm;
914 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
917 err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY);
921 } else if (*vpp == sep->se_root) {
923 * The snapshot was unmounted behind our backs,
929 * VROOT was set during the traverse call. We need
930 * to clear it since we're pretending to be part
931 * of our parent's vfs.
933 (*vpp)->v_flag &= ~VROOT;
935 mutex_exit(&sdp->sd_lock);
941 * The requested snapshot is not currently mounted, look it up.
943 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
945 mutex_exit(&sdp->sd_lock);
948 * handle "ls *" or "?" in a graceful manner,
949 * forcing EILSEQ to ENOENT.
950 * Since shell ultimately passes "*" or "?" as name to lookup
952 return (err == EILSEQ ? ENOENT : err);
954 if (dmu_objset_open(snapname, DMU_OST_ZFS,
955 DS_MODE_USER | DS_MODE_READONLY, &snap) != 0) {
956 mutex_exit(&sdp->sd_lock);
957 /* Translate errors and add SAVENAME when needed. */
958 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
960 cnp->cn_flags |= SAVENAME;
968 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
969 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
970 (void) strcpy(sep->se_name, nm);
971 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
973 avl_insert(&sdp->sd_snaps, sep, where);
975 dmu_objset_close(snap);
977 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
978 strlen("/.zfs/snapshot/") + strlen(nm) + 1;
979 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
980 (void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
981 dvp->v_vfsp->mnt_stat.f_mntonname, nm);
982 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0);
983 kmem_free(mountpoint, mountpoint_len);
986 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
988 * This is where we lie about our v_vfsp in order to
989 * make .zfs/snapshot/<snapname> accessible over NFS
990 * without requiring manual mounts of <snapname>.
992 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
993 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
995 mutex_exit(&sdp->sd_lock);
1004 zfsctl_shares_lookup(ap)
1005 struct vop_lookup_args /* {
1006 struct vnode *a_dvp;
1007 struct vnode **a_vpp;
1008 struct componentname *a_cnp;
1011 vnode_t *dvp = ap->a_dvp;
1012 vnode_t **vpp = ap->a_vpp;
1013 struct componentname *cnp = ap->a_cnp;
1014 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1015 char nm[NAME_MAX + 1];
1021 ASSERT(cnp->cn_namelen < sizeof(nm));
1022 strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1);
1024 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1029 if (zfsvfs->z_shares_dir == 0) {
1033 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
1034 error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp);
1044 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1045 offset_t *offp, offset_t *nextp, void *data, int flags)
1047 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1048 char snapname[MAXNAMELEN];
1049 uint64_t id, cookie;
1050 boolean_t case_conflict;
1056 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
1057 &cookie, &case_conflict);
1060 if (error == ENOENT) {
1067 if (flags & V_RDDIR_ENTFLAGS) {
1068 edirent_t *eodp = dp;
1070 (void) strcpy(eodp->ed_name, snapname);
1071 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1072 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1074 struct dirent64 *odp = dp;
1076 (void) strcpy(odp->d_name, snapname);
1077 odp->d_ino = ZFSCTL_INO_SNAP(id);
1088 zfsctl_shares_readdir(ap)
1089 struct vop_readdir_args /* {
1092 struct ucred *a_cred;
1098 vnode_t *vp = ap->a_vp;
1099 uio_t *uiop = ap->a_uio;
1100 cred_t *cr = ap->a_cred;
1101 int *eofp = ap->a_eofflag;
1102 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1108 if (zfsvfs->z_shares_dir == 0) {
1112 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1113 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1114 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies);
1115 VN_URELE(ZTOV(dzp));
1126 * pvp is the '.zfs' directory (zfsctl_node_t).
1127 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1129 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1130 * when a lookup is performed on .zfs for "snapshot".
1133 zfsctl_mknode_snapdir(vnode_t *pvp)
1136 zfsctl_snapdir_t *sdp;
1138 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
1139 &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1140 zfsctl_snapdir_readdir_cb, NULL);
1142 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1143 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1144 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1145 avl_create(&sdp->sd_snaps, snapentry_compare,
1146 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1152 zfsctl_mknode_shares(vnode_t *pvp)
1157 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1158 &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1161 sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1169 zfsctl_shares_getattr(ap)
1170 struct vop_getattr_args /* {
1172 struct vattr *a_vap;
1173 struct ucred *a_cred;
1174 struct thread *a_td;
1177 vnode_t *vp = ap->a_vp;
1178 vattr_t *vap = ap->a_vap;
1179 cred_t *cr = ap->a_cred;
1180 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1185 if (zfsvfs->z_shares_dir == 0) {
1189 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1190 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1191 error = VOP_GETATTR(ZTOV(dzp), vap, cr);
1192 VN_URELE(ZTOV(dzp));
1200 zfsctl_snapdir_getattr(ap)
1201 struct vop_getattr_args /* {
1203 struct vattr *a_vap;
1204 struct ucred *a_cred;
1205 struct thread *a_td;
1208 struct vnode *vp = ap->a_vp;
1209 struct vattr *vap = ap->a_vap;
1210 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1211 zfsctl_snapdir_t *sdp = vp->v_data;
1214 zfsctl_common_getattr(vp, vap);
1215 vap->va_nodeid = gfs_file_inode(vp);
1216 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1224 zfsctl_snapdir_inactive(ap)
1225 struct vop_inactive_args /* {
1227 struct thread *a_td;
1230 vnode_t *vp = ap->a_vp;
1231 zfsctl_snapdir_t *sdp = vp->v_data;
1232 zfs_snapentry_t *sep;
1235 * On forced unmount we have to free snapshots from here.
1237 mutex_enter(&sdp->sd_lock);
1238 while ((sep = avl_first(&sdp->sd_snaps)) != NULL) {
1239 avl_remove(&sdp->sd_snaps, sep);
1240 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1241 kmem_free(sep, sizeof (zfs_snapentry_t));
1243 mutex_exit(&sdp->sd_lock);
1244 gfs_dir_inactive(vp);
1245 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1246 mutex_destroy(&sdp->sd_lock);
1247 avl_destroy(&sdp->sd_snaps);
1248 kmem_free(sdp, sizeof (zfsctl_snapdir_t));
1253 static struct vop_vector zfsctl_ops_snapdir = {
1254 .vop_default = &default_vnodeops,
1255 .vop_open = zfsctl_common_open,
1256 .vop_close = zfsctl_common_close,
1257 .vop_ioctl = VOP_EINVAL,
1258 .vop_getattr = zfsctl_snapdir_getattr,
1259 .vop_access = zfsctl_common_access,
1260 .vop_mkdir = zfsctl_freebsd_snapdir_mkdir,
1261 .vop_readdir = gfs_vop_readdir,
1262 .vop_lookup = zfsctl_snapdir_lookup,
1263 .vop_inactive = zfsctl_snapdir_inactive,
1264 .vop_reclaim = zfsctl_common_reclaim,
1265 .vop_fid = zfsctl_common_fid,
1268 static struct vop_vector zfsctl_ops_shares = {
1269 .vop_default = &default_vnodeops,
1270 .vop_open = zfsctl_common_open,
1271 .vop_close = zfsctl_common_close,
1272 .vop_ioctl = VOP_EINVAL,
1273 .vop_getattr = zfsctl_shares_getattr,
1274 .vop_access = zfsctl_common_access,
1275 .vop_readdir = zfsctl_shares_readdir,
1276 .vop_lookup = zfsctl_shares_lookup,
1277 .vop_inactive = gfs_vop_inactive,
1278 .vop_reclaim = zfsctl_common_reclaim,
1279 .vop_fid = zfsctl_shares_fid,
1283 * pvp is the GFS vnode '.zfs/snapshot'.
1285 * This creates a GFS node under '.zfs/snapshot' representing each
1286 * snapshot. This newly created GFS node is what we mount snapshot
1290 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1295 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1296 &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1299 zcp->zc_id = objset;
1306 zfsctl_snapshot_inactive(ap)
1307 struct vop_inactive_args /* {
1309 struct thread *a_td;
1312 vnode_t *vp = ap->a_vp;
1313 cred_t *cr = ap->a_td->td_ucred;
1314 struct vop_inactive_args iap;
1315 zfsctl_snapdir_t *sdp;
1316 zfs_snapentry_t *sep, *next;
1320 if (vp->v_count > 0)
1323 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1327 if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
1328 mutex_enter(&sdp->sd_lock);
1330 ASSERT(!vn_ismntpt(vp));
1332 sep = avl_first(&sdp->sd_snaps);
1333 while (sep != NULL) {
1334 next = AVL_NEXT(&sdp->sd_snaps, sep);
1336 if (sep->se_root == vp) {
1337 avl_remove(&sdp->sd_snaps, sep);
1338 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1339 kmem_free(sep, sizeof (zfs_snapentry_t));
1344 ASSERT(sep != NULL);
1347 mutex_exit(&sdp->sd_lock);
1352 * Dispose of the vnode for the snapshot mount point.
1353 * This is safe to do because once this entry has been removed
1354 * from the AVL tree, it can't be found again, so cannot become
1355 * "active". If we lookup the same name again we will end up
1356 * creating a new vnode.
1359 return (gfs_vop_inactive(&iap));
1363 zfsctl_traverse_begin(vnode_t **vpp, int lktype)
1367 /* Snapshot should be already mounted, but just in case. */
1368 if (vn_mountedvfs(*vpp) == NULL)
1370 return (traverse(vpp, lktype));
1374 zfsctl_traverse_end(vnode_t *vp, int err)
1384 zfsctl_snapshot_getattr(ap)
1385 struct vop_getattr_args /* {
1387 struct vattr *a_vap;
1388 struct ucred *a_cred;
1391 vnode_t *vp = ap->a_vp;
1394 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1396 err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred);
1397 zfsctl_traverse_end(vp, err);
1402 zfsctl_snapshot_fid(ap)
1403 struct vop_fid_args /* {
1408 vnode_t *vp = ap->a_vp;
1411 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1413 err = VOP_VPTOFH(vp, (void *)ap->a_fid);
1414 zfsctl_traverse_end(vp, err);
1419 zfsctl_snapshot_lookup(ap)
1420 struct vop_lookup_args /* {
1421 struct vnode *a_dvp;
1422 struct vnode **a_vpp;
1423 struct componentname *a_cnp;
1426 vnode_t *dvp = ap->a_dvp;
1427 vnode_t **vpp = ap->a_vpp;
1428 struct componentname *cnp = ap->a_cnp;
1429 cred_t *cr = ap->a_cnp->cn_cred;
1430 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1433 if (cnp->cn_namelen != 2 || cnp->cn_nameptr[0] != '.' ||
1434 cnp->cn_nameptr[1] != '.') {
1438 ASSERT(dvp->v_type == VDIR);
1439 ASSERT(zfsvfs->z_ctldir != NULL);
1441 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", vpp,
1442 NULL, 0, NULL, cr, NULL, NULL, NULL);
1444 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1449 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1451 zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data;
1453 zfsctl_snapdir_t *sdp;
1454 zfs_snapentry_t *sep;
1457 ASSERT(zfsvfs->z_ctldir != NULL);
1458 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1459 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1464 mutex_enter(&sdp->sd_lock);
1465 sep = avl_first(&sdp->sd_snaps);
1466 while (sep != NULL) {
1470 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1473 mutex_exit(&sdp->sd_lock);
1478 len = strlen(sep->se_name);
1479 *ap->a_buflen -= len;
1480 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len);
1481 mutex_exit(&sdp->sd_lock);
1491 * These VP's should never see the light of day. They should always
1494 static struct vop_vector zfsctl_ops_snapshot = {
1495 .vop_default = &default_vnodeops,
1496 .vop_inactive = zfsctl_snapshot_inactive,
1497 .vop_lookup = zfsctl_snapshot_lookup,
1498 .vop_reclaim = zfsctl_common_reclaim,
1499 .vop_getattr = zfsctl_snapshot_getattr,
1500 .vop_fid = zfsctl_snapshot_fid,
1501 .vop_vptocnp = zfsctl_snapshot_vptocnp,
1505 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1507 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1509 zfsctl_snapdir_t *sdp;
1511 zfs_snapentry_t *sep;
1514 ASSERT(zfsvfs->z_ctldir != NULL);
1515 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1516 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1521 mutex_enter(&sdp->sd_lock);
1522 sep = avl_first(&sdp->sd_snaps);
1523 while (sep != NULL) {
1526 if (zcp->zc_id == objsetid)
1529 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1535 * Return the mounted root rather than the covered mount point.
1536 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1537 * and returns the ZFS vnode mounted on top of the GFS node.
1538 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1540 error = traverse(&vp, LK_SHARED | LK_RETRY);
1542 if (vp == sep->se_root)
1545 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1547 mutex_exit(&sdp->sd_lock);
1554 mutex_exit(&sdp->sd_lock);
1563 * Unmount any snapshots for the given filesystem. This is called from
1564 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1568 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1570 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1572 zfsctl_snapdir_t *sdp;
1573 zfs_snapentry_t *sep, *next;
1576 ASSERT(zfsvfs->z_ctldir != NULL);
1577 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1578 NULL, 0, NULL, cr, NULL, NULL, NULL);
1583 mutex_enter(&sdp->sd_lock);
1585 sep = avl_first(&sdp->sd_snaps);
1586 while (sep != NULL) {
1587 next = AVL_NEXT(&sdp->sd_snaps, sep);
1590 * If this snapshot is not mounted, then it must
1591 * have just been unmounted by somebody else, and
1592 * will be cleaned up by zfsctl_snapdir_inactive().
1594 if (vn_ismntpt(sep->se_root)) {
1595 error = zfsctl_unmount_snap(sep, fflags, cr);
1600 * Before reinserting snapshot to the tree,
1601 * check if it was actually removed. For example
1602 * when snapshot mount point is busy, we will
1603 * have an error here, but there will be no need
1604 * to reinsert snapshot.
1606 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
1607 avl_insert(&sdp->sd_snaps, sep, where);
1614 mutex_exit(&sdp->sd_lock);