4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
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_destroy.h>
76 #include <sys/dsl_deleg.h>
77 #include <sys/mount.h>
78 #include <sys/sunddi.h>
80 #include "zfs_namecheck.h"
82 typedef struct zfsctl_node {
83 gfs_dir_t zc_gfs_private;
85 timestruc_t zc_cmtime; /* ctime and mtime, always the same */
88 typedef struct zfsctl_snapdir {
89 zfsctl_node_t sd_node;
101 snapentry_compare(const void *a, const void *b)
103 const zfs_snapentry_t *sa = a;
104 const zfs_snapentry_t *sb = b;
105 int ret = strcmp(sa->se_name, sb->se_name);
116 vnodeops_t *zfsctl_ops_root;
117 vnodeops_t *zfsctl_ops_snapdir;
118 vnodeops_t *zfsctl_ops_snapshot;
119 vnodeops_t *zfsctl_ops_shares;
120 vnodeops_t *zfsctl_ops_shares_dir;
122 static const fs_operation_def_t zfsctl_tops_root[];
123 static const fs_operation_def_t zfsctl_tops_snapdir[];
124 static const fs_operation_def_t zfsctl_tops_snapshot[];
125 static const fs_operation_def_t zfsctl_tops_shares[];
127 static struct vop_vector zfsctl_ops_root;
128 static struct vop_vector zfsctl_ops_snapdir;
129 static struct vop_vector zfsctl_ops_snapshot;
130 static struct vop_vector zfsctl_ops_shares;
131 static struct vop_vector zfsctl_ops_shares_dir;
134 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
135 static vnode_t *zfsctl_mknode_shares(vnode_t *);
136 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
137 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
140 static gfs_opsvec_t zfsctl_opsvec[] = {
141 { ".zfs", zfsctl_tops_root, &zfsctl_ops_root },
142 { ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir },
143 { ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot },
144 { ".zfs/shares", zfsctl_tops_shares, &zfsctl_ops_shares_dir },
145 { ".zfs/shares/vnode", zfsctl_tops_shares, &zfsctl_ops_shares },
151 * Root directory elements. We only have two entries
152 * snapshot and shares.
154 static gfs_dirent_t zfsctl_root_entries[] = {
155 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
156 { "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
160 /* include . and .. in the calculation */
161 #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \
162 sizeof (gfs_dirent_t)) + 1)
166 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
167 * directories. This is called from the ZFS init routine, and initializes the
168 * vnode ops vectors that we'll be using.
174 VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0);
183 * Remove vfsctl vnode ops
186 vn_freevnodeops(zfsctl_ops_root);
187 if (zfsctl_ops_snapdir)
188 vn_freevnodeops(zfsctl_ops_snapdir);
189 if (zfsctl_ops_snapshot)
190 vn_freevnodeops(zfsctl_ops_snapshot);
191 if (zfsctl_ops_shares)
192 vn_freevnodeops(zfsctl_ops_shares);
193 if (zfsctl_ops_shares_dir)
194 vn_freevnodeops(zfsctl_ops_shares_dir);
196 zfsctl_ops_root = NULL;
197 zfsctl_ops_snapdir = NULL;
198 zfsctl_ops_snapshot = NULL;
199 zfsctl_ops_shares = NULL;
200 zfsctl_ops_shares_dir = NULL;
205 zfsctl_is_node(vnode_t *vp)
207 return (vn_matchops(vp, zfsctl_ops_root) ||
208 vn_matchops(vp, zfsctl_ops_snapdir) ||
209 vn_matchops(vp, zfsctl_ops_snapshot) ||
210 vn_matchops(vp, zfsctl_ops_shares) ||
211 vn_matchops(vp, zfsctl_ops_shares_dir));
216 * Return the inode number associated with the 'snapshot' or
217 * 'shares' directory.
221 zfsctl_root_inode_cb(vnode_t *vp, int index)
223 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
228 return (ZFSCTL_INO_SNAPDIR);
230 return (zfsvfs->z_shares_dir);
234 * Create the '.zfs' directory. This directory is cached as part of the VFS
235 * structure. This results in a hold on the vfs_t. The code in zfs_umount()
236 * therefore checks against a vfs_count of 2 instead of 1. This reference
237 * is removed when the ctldir is destroyed in the unmount.
240 zfsctl_create(zfsvfs_t *zfsvfs)
246 ASSERT(zfsvfs->z_ctldir == NULL);
248 vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
249 &zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
250 zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
252 zcp->zc_id = ZFSCTL_INO_ROOT;
254 VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp) == 0);
255 VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
256 &crtime, sizeof (crtime)));
257 ZFS_TIME_DECODE(&zcp->zc_cmtime, crtime);
261 * We're only faking the fact that we have a root of a filesystem for
262 * the sake of the GFS interfaces. Undo the flag manipulation it did
265 vp->v_vflag &= ~VV_ROOT;
267 zfsvfs->z_ctldir = vp;
273 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
274 * There might still be more references if we were force unmounted, but only
275 * new zfs_inactive() calls can occur and they don't reference .zfs
278 zfsctl_destroy(zfsvfs_t *zfsvfs)
280 VN_RELE(zfsvfs->z_ctldir);
281 zfsvfs->z_ctldir = NULL;
285 * Given a root znode, retrieve the associated .zfs directory.
286 * Add a hold to the vnode and return it.
289 zfsctl_root(znode_t *zp)
291 ASSERT(zfs_has_ctldir(zp));
292 VN_HOLD(zp->z_zfsvfs->z_ctldir);
293 return (zp->z_zfsvfs->z_ctldir);
297 * Common open routine. Disallow any write access.
301 zfsctl_common_open(struct vop_open_args *ap)
303 int flags = ap->a_mode;
306 return (SET_ERROR(EACCES));
312 * Common close routine. Nothing to do here.
316 zfsctl_common_close(struct vop_close_args *ap)
322 * Common access routine. Disallow writes.
326 zfsctl_common_access(ap)
327 struct vop_access_args /* {
330 struct ucred *a_cred;
334 accmode_t accmode = ap->a_accmode;
337 if (flags & V_ACE_MASK) {
338 if (accmode & ACE_ALL_WRITE_PERMS)
339 return (SET_ERROR(EACCES));
342 if (accmode & VWRITE)
343 return (SET_ERROR(EACCES));
352 * Common getattr function. Fill in basic information.
355 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
363 * We are a purely virtual object, so we have no
364 * blocksize or allocated blocks.
369 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
370 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
374 * We live in the now (for atime).
378 /* FreeBSD: Reset chflags(2) flags. */
384 zfsctl_common_fid(ap)
385 struct vop_fid_args /* {
390 vnode_t *vp = ap->a_vp;
391 fid_t *fidp = (void *)ap->a_fid;
392 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
393 zfsctl_node_t *zcp = vp->v_data;
394 uint64_t object = zcp->zc_id;
401 if (fidp->fid_len < SHORT_FID_LEN) {
402 fidp->fid_len = SHORT_FID_LEN;
404 return (SET_ERROR(ENOSPC));
408 zfid = (zfid_short_t *)fidp;
410 zfid->zf_len = SHORT_FID_LEN;
412 for (i = 0; i < sizeof (zfid->zf_object); i++)
413 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
415 /* .zfs znodes always have a generation number of 0 */
416 for (i = 0; i < sizeof (zfid->zf_gen); i++)
426 zfsctl_shares_fid(ap)
427 struct vop_fid_args /* {
432 vnode_t *vp = ap->a_vp;
433 fid_t *fidp = (void *)ap->a_fid;
434 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
440 if (zfsvfs->z_shares_dir == 0) {
442 return (SET_ERROR(ENOTSUP));
445 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
446 error = VOP_FID(ZTOV(dzp), fidp);
455 * .zfs inode namespace
457 * We need to generate unique inode numbers for all files and directories
458 * within the .zfs pseudo-filesystem. We use the following scheme:
463 * .zfs/snapshot/<snap> objectid(snap)
466 #define ZFSCTL_INO_SNAP(id) (id)
469 * Get root directory attributes.
473 zfsctl_root_getattr(ap)
474 struct vop_getattr_args /* {
477 struct ucred *a_cred;
480 struct vnode *vp = ap->a_vp;
481 struct vattr *vap = ap->a_vap;
482 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
483 zfsctl_node_t *zcp = vp->v_data;
486 vap->va_nodeid = ZFSCTL_INO_ROOT;
487 vap->va_nlink = vap->va_size = NROOT_ENTRIES;
488 vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
489 vap->va_birthtime = vap->va_ctime;
491 zfsctl_common_getattr(vp, vap);
498 * Special case the handling of "..".
502 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
503 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
504 int *direntflags, pathname_t *realpnp)
506 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
510 * No extended attributes allowed under .zfs
512 if (flags & LOOKUP_XATTR)
513 return (SET_ERROR(EINVAL));
517 if (strcmp(nm, "..") == 0) {
519 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp);
522 * NB: can not use VFS_ROOT here as it would acquire
523 * the vnode lock of the parent (root) vnode while
524 * holding the child's (.zfs) lock.
528 err = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
533 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
534 cr, ct, direntflags, realpnp);
544 zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
545 caller_context_t *ct)
548 * We only care about ACL_ENABLED so that libsec can
549 * display ACL correctly and not default to POSIX draft.
551 if (cmd == _PC_ACL_ENABLED) {
552 *valp = _ACL_ACE_ENABLED;
556 return (fs_pathconf(vp, cmd, valp, cr, ct));
561 static const fs_operation_def_t zfsctl_tops_root[] = {
562 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
563 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
564 { VOPNAME_IOCTL, { .error = fs_inval } },
565 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } },
566 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
567 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
568 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } },
569 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
570 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
571 { VOPNAME_PATHCONF, { .vop_pathconf = zfsctl_pathconf } },
572 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } },
578 * Special case the handling of "..".
582 zfsctl_freebsd_root_lookup(ap)
583 struct vop_lookup_args /* {
585 struct vnode **a_vpp;
586 struct componentname *a_cnp;
589 vnode_t *dvp = ap->a_dvp;
590 vnode_t **vpp = ap->a_vpp;
591 cred_t *cr = ap->a_cnp->cn_cred;
592 int flags = ap->a_cnp->cn_flags;
593 int lkflags = ap->a_cnp->cn_lkflags;
594 int nameiop = ap->a_cnp->cn_nameiop;
595 char nm[NAME_MAX + 1];
598 if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
601 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
602 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
604 err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL);
605 if (err == 0 && (nm[0] != '.' || nm[1] != '\0')) {
606 if (flags & ISDOTDOT)
608 err = vn_lock(*vpp, lkflags);
613 if (flags & ISDOTDOT)
614 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
620 static struct vop_vector zfsctl_ops_root = {
621 .vop_default = &default_vnodeops,
622 .vop_open = zfsctl_common_open,
623 .vop_close = zfsctl_common_close,
624 .vop_ioctl = VOP_EINVAL,
625 .vop_getattr = zfsctl_root_getattr,
626 .vop_access = zfsctl_common_access,
627 .vop_readdir = gfs_vop_readdir,
628 .vop_lookup = zfsctl_freebsd_root_lookup,
629 .vop_inactive = VOP_NULL,
630 .vop_reclaim = gfs_vop_reclaim,
632 .vop_pathconf = zfsctl_pathconf,
634 .vop_fid = zfsctl_common_fid,
638 * Gets the full dataset name that corresponds to the given snapshot name
640 * zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1"
643 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
645 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
647 if (zfs_component_namecheck(name, NULL, NULL) != 0)
648 return (SET_ERROR(EILSEQ));
649 dmu_objset_name(os, zname);
650 if (strlen(zname) + 1 + strlen(name) >= len)
651 return (SET_ERROR(ENAMETOOLONG));
652 (void) strcat(zname, "@");
653 (void) strcat(zname, name);
658 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
660 vnode_t *svp = sep->se_root;
663 ASSERT(vn_ismntpt(svp));
665 /* this will be dropped by dounmount() */
666 if ((error = vn_vfswlock(svp)) != 0)
671 error = dounmount(vn_mountedvfs(svp), fflags, cr);
678 * We can't use VN_RELE(), as that will try to invoke
679 * zfsctl_snapdir_inactive(), which would cause us to destroy
680 * the sd_lock mutex held by our caller.
682 ASSERT(svp->v_count == 1);
683 gfs_vop_reclaim(svp, cr, NULL);
685 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
686 kmem_free(sep, sizeof (zfs_snapentry_t));
690 return (dounmount(vn_mountedvfs(svp), fflags, curthread));
696 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
701 char newpath[MAXNAMELEN];
704 ASSERT(MUTEX_HELD(&sdp->sd_lock));
707 vfsp = vn_mountedvfs(sep->se_root);
708 ASSERT(vfsp != NULL);
713 * Change the name in the AVL tree.
715 avl_remove(&sdp->sd_snaps, sep);
716 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
717 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
718 (void) strcpy(sep->se_name, nm);
719 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
720 avl_insert(&sdp->sd_snaps, sep, where);
723 * Change the current mountpoint info:
724 * - update the tail of the mntpoint path
725 * - update the tail of the resource path
727 pathref = vfs_getmntpoint(vfsp);
728 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
729 VERIFY((tail = strrchr(newpath, '/')) != NULL);
731 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
732 (void) strcat(newpath, nm);
733 refstr_rele(pathref);
734 vfs_setmntpoint(vfsp, newpath, 0);
736 pathref = vfs_getresource(vfsp);
737 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
738 VERIFY((tail = strrchr(newpath, '@')) != NULL);
740 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
741 (void) strcat(newpath, nm);
742 refstr_rele(pathref);
743 vfs_setresource(vfsp, newpath, 0);
752 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
753 cred_t *cr, caller_context_t *ct, int flags)
755 zfsctl_snapdir_t *sdp = sdvp->v_data;
756 zfs_snapentry_t search, *sep;
759 char from[MAXNAMELEN], to[MAXNAMELEN];
760 char real[MAXNAMELEN], fsname[MAXNAMELEN];
763 zfsvfs = sdvp->v_vfsp->vfs_data;
766 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
767 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
771 } else if (err != ENOTSUP) {
779 dmu_objset_name(zfsvfs->z_os, fsname);
781 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
783 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
785 err = zfs_secpolicy_rename_perms(from, to, cr);
790 * Cannot move snapshots out of the snapdir.
793 return (SET_ERROR(EINVAL));
795 if (strcmp(snm, tnm) == 0)
798 mutex_enter(&sdp->sd_lock);
800 search.se_name = (char *)snm;
801 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
802 mutex_exit(&sdp->sd_lock);
803 return (SET_ERROR(ENOENT));
806 err = dsl_dataset_rename_snapshot(fsname, snm, tnm, 0);
808 zfsctl_rename_snap(sdp, sep, tnm);
810 mutex_exit(&sdp->sd_lock);
819 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
820 caller_context_t *ct, int flags)
822 zfsctl_snapdir_t *sdp = dvp->v_data;
823 zfs_snapentry_t *sep;
824 zfs_snapentry_t search;
826 char snapname[MAXNAMELEN];
827 char real[MAXNAMELEN];
830 zfsvfs = dvp->v_vfsp->vfs_data;
833 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
835 err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
839 } else if (err != ENOTSUP) {
847 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
849 err = zfs_secpolicy_destroy_perms(snapname, cr);
853 mutex_enter(&sdp->sd_lock);
855 search.se_name = name;
856 sep = avl_find(&sdp->sd_snaps, &search, NULL);
858 avl_remove(&sdp->sd_snaps, sep);
859 err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
861 avl_add(&sdp->sd_snaps, sep);
863 err = dsl_destroy_snapshot(snapname, B_FALSE);
865 err = SET_ERROR(ENOENT);
868 mutex_exit(&sdp->sd_lock);
875 * This creates a snapshot under '.zfs/snapshot'.
879 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp,
880 cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
882 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
883 char name[MAXNAMELEN];
885 static enum symfollow follow = NO_FOLLOW;
886 static enum uio_seg seg = UIO_SYSSPACE;
888 if (zfs_component_namecheck(dirname, NULL, NULL) != 0)
889 return (SET_ERROR(EILSEQ));
891 dmu_objset_name(zfsvfs->z_os, name);
895 err = zfs_secpolicy_snapshot_perms(name, cr);
900 err = dmu_objset_snapshot_one(name, dirname);
903 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
910 zfsctl_freebsd_snapdir_mkdir(ap)
911 struct vop_mkdir_args /* {
913 struct vnode **a_vpp;
914 struct componentname *a_cnp;
919 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
921 return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL,
922 ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL));
926 * Lookup entry point for the 'snapshot' directory. Try to open the
927 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
928 * Perform a mount of the associated dataset on top of the vnode.
932 zfsctl_snapdir_lookup(ap)
933 struct vop_lookup_args /* {
935 struct vnode **a_vpp;
936 struct componentname *a_cnp;
939 vnode_t *dvp = ap->a_dvp;
940 vnode_t **vpp = ap->a_vpp;
941 struct componentname *cnp = ap->a_cnp;
942 char nm[NAME_MAX + 1];
943 zfsctl_snapdir_t *sdp = dvp->v_data;
945 char snapname[MAXNAMELEN];
946 char real[MAXNAMELEN];
948 zfs_snapentry_t *sep, search;
949 size_t mountpoint_len;
951 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
956 * No extended attributes allowed under .zfs
958 if (flags & LOOKUP_XATTR)
959 return (SET_ERROR(EINVAL));
960 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
961 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
963 ASSERT(dvp->v_type == VDIR);
968 * If we get a recursive call, that means we got called
969 * from the domount() code while it was trying to look up the
970 * spec (which looks like a local path for zfs). We need to
971 * add some flag to domount() to tell it not to do this lookup.
973 if (MUTEX_HELD(&sdp->sd_lock))
974 return (SET_ERROR(ENOENT));
978 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
979 if (nm[0] == '.' && nm[1] == '.' && nm[2] =='\0') {
981 VERIFY0(vn_lock(*vpp, LK_EXCLUSIVE));
982 VERIFY0(vn_lock(dvp, LK_EXCLUSIVE));
988 if (flags & FIGNORECASE) {
989 boolean_t conflict = B_FALSE;
991 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
992 MAXNAMELEN, &conflict);
994 strlcpy(nm, real, sizeof(nm));
995 } else if (err != ENOTSUP) {
1001 (void) strlcpy(realpnp->pn_buf, nm,
1002 realpnp->pn_bufsize);
1003 if (conflict && direntflags)
1004 *direntflags = ED_CASE_CONFLICT;
1009 mutex_enter(&sdp->sd_lock);
1010 search.se_name = (char *)nm;
1011 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
1012 *vpp = sep->se_root;
1014 err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY);
1017 } else if (*vpp == sep->se_root) {
1019 * The snapshot was unmounted behind our backs,
1020 * try to remount it.
1022 VERIFY(zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname) == 0);
1025 mutex_exit(&sdp->sd_lock);
1031 * The requested snapshot is not currently mounted, look it up.
1033 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
1035 mutex_exit(&sdp->sd_lock);
1038 * handle "ls *" or "?" in a graceful manner,
1039 * forcing EILSEQ to ENOENT.
1040 * Since shell ultimately passes "*" or "?" as name to lookup
1042 return (err == EILSEQ ? ENOENT : err);
1044 if (dmu_objset_hold(snapname, FTAG, &snap) != 0) {
1045 mutex_exit(&sdp->sd_lock);
1048 return (SET_ERROR(ENOENT));
1049 #else /* !illumos */
1050 /* Translate errors and add SAVENAME when needed. */
1051 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
1053 cnp->cn_flags |= SAVENAME;
1055 err = SET_ERROR(ENOENT);
1059 #endif /* !illumos */
1062 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
1063 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
1064 (void) strcpy(sep->se_name, nm);
1065 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
1066 avl_insert(&sdp->sd_snaps, sep, where);
1068 dmu_objset_rele(snap, FTAG);
1070 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
1071 strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(nm) + 1;
1072 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
1073 (void) snprintf(mountpoint, mountpoint_len,
1074 "%s/" ZFS_CTLDIR_NAME "/snapshot/%s",
1075 dvp->v_vfsp->mnt_stat.f_mntonname, nm);
1076 mutex_exit(&sdp->sd_lock);
1079 * The vnode may get reclaimed between dropping sd_lock and
1080 * getting the vnode lock.
1082 err = vn_lock(*vpp, LK_EXCLUSIVE);
1086 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0);
1087 kmem_free(mountpoint, mountpoint_len);
1090 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
1092 * This is where we lie about our v_vfsp in order to
1093 * make .zfs/snapshot/<snapname> accessible over NFS
1094 * without requiring manual mounts of <snapname>.
1096 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
1097 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
1103 * If we had an error, drop our hold on the vnode and
1104 * zfsctl_snapshot_inactive() will clean up.
1119 zfsctl_shares_lookup(ap)
1120 struct vop_lookup_args /* {
1121 struct vnode *a_dvp;
1122 struct vnode **a_vpp;
1123 struct componentname *a_cnp;
1126 vnode_t *dvp = ap->a_dvp;
1127 vnode_t **vpp = ap->a_vpp;
1128 struct componentname *cnp = ap->a_cnp;
1129 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1130 char nm[NAME_MAX + 1];
1136 ASSERT(cnp->cn_namelen < sizeof(nm));
1137 strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1);
1139 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
1140 if (nm[0] == '.' && nm[1] == '.' && nm[2] =='\0') {
1142 VERIFY0(vn_lock(*vpp, LK_EXCLUSIVE));
1143 VERIFY0(vn_lock(dvp, LK_EXCLUSIVE));
1149 if (zfsvfs->z_shares_dir == 0) {
1151 return (SET_ERROR(ENOTSUP));
1153 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
1154 error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp);
1164 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
1165 offset_t *offp, offset_t *nextp, void *data, int flags)
1167 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1168 char snapname[MAXNAMELEN];
1169 uint64_t id, cookie;
1170 boolean_t case_conflict;
1176 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
1177 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
1178 &cookie, &case_conflict);
1179 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
1182 if (error == ENOENT) {
1189 if (flags & V_RDDIR_ENTFLAGS) {
1190 edirent_t *eodp = dp;
1192 (void) strcpy(eodp->ed_name, snapname);
1193 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1194 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1196 struct dirent64 *odp = dp;
1198 (void) strcpy(odp->d_name, snapname);
1199 odp->d_ino = ZFSCTL_INO_SNAP(id);
1210 zfsctl_shares_readdir(ap)
1211 struct vop_readdir_args /* {
1214 struct ucred *a_cred;
1220 vnode_t *vp = ap->a_vp;
1221 uio_t *uiop = ap->a_uio;
1222 cred_t *cr = ap->a_cred;
1223 int *eofp = ap->a_eofflag;
1224 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1230 if (zfsvfs->z_shares_dir == 0) {
1232 return (SET_ERROR(ENOTSUP));
1234 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1235 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1236 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies);
1237 VN_URELE(ZTOV(dzp));
1240 error = SET_ERROR(ENOENT);
1248 * pvp is the '.zfs' directory (zfsctl_node_t).
1250 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1252 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1253 * when a lookup is performed on .zfs for "snapshot".
1256 zfsctl_mknode_snapdir(vnode_t *pvp)
1259 zfsctl_snapdir_t *sdp;
1261 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
1262 &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1263 zfsctl_snapdir_readdir_cb, NULL);
1265 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1266 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1267 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1268 avl_create(&sdp->sd_snaps, snapentry_compare,
1269 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1275 zfsctl_mknode_shares(vnode_t *pvp)
1280 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1281 &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1284 sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1292 zfsctl_shares_getattr(ap)
1293 struct vop_getattr_args /* {
1295 struct vattr *a_vap;
1296 struct ucred *a_cred;
1297 struct thread *a_td;
1300 vnode_t *vp = ap->a_vp;
1301 vattr_t *vap = ap->a_vap;
1302 cred_t *cr = ap->a_cred;
1303 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1308 if (zfsvfs->z_shares_dir == 0) {
1310 return (SET_ERROR(ENOTSUP));
1312 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1313 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY);
1314 error = VOP_GETATTR(ZTOV(dzp), vap, cr);
1315 VN_URELE(ZTOV(dzp));
1325 zfsctl_snapdir_getattr(ap)
1326 struct vop_getattr_args /* {
1328 struct vattr *a_vap;
1329 struct ucred *a_cred;
1332 vnode_t *vp = ap->a_vp;
1333 vattr_t *vap = ap->a_vap;
1334 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1335 zfsctl_snapdir_t *sdp = vp->v_data;
1338 zfsctl_common_getattr(vp, vap);
1339 vap->va_nodeid = gfs_file_inode(vp);
1340 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1341 vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1342 vap->va_birthtime = vap->va_ctime;
1350 zfsctl_snapdir_reclaim(ap)
1351 struct vop_reclaim_args /* {
1353 struct thread *a_td;
1356 vnode_t *vp = ap->a_vp;
1357 zfsctl_snapdir_t *sdp = vp->v_data;
1358 zfs_snapentry_t *sep;
1360 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1361 mutex_destroy(&sdp->sd_lock);
1362 avl_destroy(&sdp->sd_snaps);
1363 gfs_vop_reclaim(ap);
1369 static const fs_operation_def_t zfsctl_tops_snapdir[] = {
1370 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
1371 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
1372 { VOPNAME_IOCTL, { .error = fs_inval } },
1373 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_snapdir_getattr } },
1374 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
1375 { VOPNAME_RENAME, { .vop_rename = zfsctl_snapdir_rename } },
1376 { VOPNAME_RMDIR, { .vop_rmdir = zfsctl_snapdir_remove } },
1377 { VOPNAME_MKDIR, { .vop_mkdir = zfsctl_snapdir_mkdir } },
1378 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
1379 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_snapdir_lookup } },
1380 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
1381 { VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapdir_inactive } },
1382 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } },
1386 static const fs_operation_def_t zfsctl_tops_shares[] = {
1387 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
1388 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
1389 { VOPNAME_IOCTL, { .error = fs_inval } },
1390 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_shares_getattr } },
1391 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
1392 { VOPNAME_READDIR, { .vop_readdir = zfsctl_shares_readdir } },
1393 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_shares_lookup } },
1394 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
1395 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
1396 { VOPNAME_FID, { .vop_fid = zfsctl_shares_fid } },
1400 static struct vop_vector zfsctl_ops_snapdir = {
1401 .vop_default = &default_vnodeops,
1402 .vop_open = zfsctl_common_open,
1403 .vop_close = zfsctl_common_close,
1404 .vop_ioctl = VOP_EINVAL,
1405 .vop_getattr = zfsctl_snapdir_getattr,
1406 .vop_access = zfsctl_common_access,
1407 .vop_mkdir = zfsctl_freebsd_snapdir_mkdir,
1408 .vop_readdir = gfs_vop_readdir,
1409 .vop_lookup = zfsctl_snapdir_lookup,
1410 .vop_inactive = VOP_NULL,
1411 .vop_reclaim = zfsctl_snapdir_reclaim,
1412 .vop_fid = zfsctl_common_fid,
1415 static struct vop_vector zfsctl_ops_shares = {
1416 .vop_default = &default_vnodeops,
1417 .vop_open = zfsctl_common_open,
1418 .vop_close = zfsctl_common_close,
1419 .vop_ioctl = VOP_EINVAL,
1420 .vop_getattr = zfsctl_shares_getattr,
1421 .vop_access = zfsctl_common_access,
1422 .vop_readdir = zfsctl_shares_readdir,
1423 .vop_lookup = zfsctl_shares_lookup,
1424 .vop_inactive = VOP_NULL,
1425 .vop_reclaim = gfs_vop_reclaim,
1426 .vop_fid = zfsctl_shares_fid,
1431 * pvp is the GFS vnode '.zfs/snapshot'.
1433 * This creates a GFS node under '.zfs/snapshot' representing each
1434 * snapshot. This newly created GFS node is what we mount snapshot
1438 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1443 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1444 &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1446 zcp->zc_id = objset;
1453 zfsctl_snapshot_inactive(ap)
1454 struct vop_inactive_args /* {
1456 struct thread *a_td;
1459 vnode_t *vp = ap->a_vp;
1461 vrecycle(vp, curthread);
1466 zfsctl_snapshot_reclaim(ap)
1467 struct vop_reclaim_args /* {
1469 struct thread *a_td;
1472 vnode_t *vp = ap->a_vp;
1473 cred_t *cr = ap->a_td->td_ucred;
1474 zfsctl_snapdir_t *sdp;
1475 zfs_snapentry_t *sep, *next;
1479 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1481 /* this may already have been unmounted */
1486 if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
1487 mutex_enter(&sdp->sd_lock);
1489 ASSERT(!vn_ismntpt(vp));
1491 sep = avl_first(&sdp->sd_snaps);
1492 while (sep != NULL) {
1493 next = AVL_NEXT(&sdp->sd_snaps, sep);
1495 if (sep->se_root == vp) {
1496 avl_remove(&sdp->sd_snaps, sep);
1497 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1498 kmem_free(sep, sizeof (zfs_snapentry_t));
1503 ASSERT(sep != NULL);
1506 mutex_exit(&sdp->sd_lock);
1510 * Dispose of the vnode for the snapshot mount point.
1511 * This is safe to do because once this entry has been removed
1512 * from the AVL tree, it can't be found again, so cannot become
1513 * "active". If we lookup the same name again we will end up
1514 * creating a new vnode.
1516 gfs_vop_reclaim(ap);
1522 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1524 zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data;
1526 zfsctl_snapdir_t *sdp;
1527 zfs_snapentry_t *sep;
1530 ASSERT(zfsvfs->z_ctldir != NULL);
1531 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1532 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1537 mutex_enter(&sdp->sd_lock);
1538 sep = avl_first(&sdp->sd_snaps);
1539 while (sep != NULL) {
1543 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1546 mutex_exit(&sdp->sd_lock);
1551 len = strlen(sep->se_name);
1552 *ap->a_buflen -= len;
1553 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len);
1554 mutex_exit(&sdp->sd_lock);
1564 * These VP's should never see the light of day. They should always
1567 static struct vop_vector zfsctl_ops_snapshot = {
1568 .vop_default = &default_vnodeops,
1569 .vop_inactive = zfsctl_snapshot_inactive,
1570 .vop_reclaim = zfsctl_snapshot_reclaim,
1571 .vop_vptocnp = zfsctl_snapshot_vptocnp,
1575 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1577 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1579 zfsctl_snapdir_t *sdp;
1581 zfs_snapentry_t *sep;
1584 ASSERT(zfsvfs->z_ctldir != NULL);
1585 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1586 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1591 mutex_enter(&sdp->sd_lock);
1592 sep = avl_first(&sdp->sd_snaps);
1593 while (sep != NULL) {
1596 if (zcp->zc_id == objsetid)
1599 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1605 * Return the mounted root rather than the covered mount point.
1606 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1607 * and returns the ZFS vnode mounted on top of the GFS node.
1608 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1610 error = traverse(&vp, LK_SHARED | LK_RETRY);
1612 if (vp == sep->se_root) {
1613 VN_RELE(vp); /* release covered vp */
1614 error = SET_ERROR(EINVAL);
1616 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1617 VN_URELE(vp); /* put snapshot's root vp */
1620 mutex_exit(&sdp->sd_lock);
1622 error = SET_ERROR(EINVAL);
1623 mutex_exit(&sdp->sd_lock);
1632 * Unmount any snapshots for the given filesystem. This is called from
1633 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1637 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1639 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1641 zfsctl_snapdir_t *sdp;
1642 zfs_snapentry_t *sep, *next;
1645 ASSERT(zfsvfs->z_ctldir != NULL);
1646 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1647 NULL, 0, NULL, cr, NULL, NULL, NULL);
1652 mutex_enter(&sdp->sd_lock);
1654 sep = avl_first(&sdp->sd_snaps);
1655 while (sep != NULL) {
1656 next = AVL_NEXT(&sdp->sd_snaps, sep);
1659 * If this snapshot is not mounted, then it must
1660 * have just been unmounted by somebody else, and
1661 * will be cleaned up by zfsctl_snapdir_inactive().
1663 if (vn_ismntpt(sep->se_root)) {
1664 error = zfsctl_unmount_snap(sep, fflags, cr);
1669 * Before reinserting snapshot to the tree,
1670 * check if it was actually removed. For example
1671 * when snapshot mount point is busy, we will
1672 * have an error here, but there will be no need
1673 * to reinsert snapshot.
1675 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
1676 avl_insert(&sdp->sd_snaps, sep, where);
1683 mutex_exit(&sdp->sd_lock);