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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * ZFS control directory (a.k.a. ".zfs")
31 * This directory provides a common location for all ZFS meta-objects.
32 * Currently, this is only the 'snapshot' directory, but this may expand in the
33 * future. The elements are built using the GFS primitives, as the hierarchy
34 * does not actually exist on disk.
36 * For 'snapshot', we don't want to have all snapshots always mounted, because
37 * this would take up a huge amount of space in /etc/mnttab. We have three
40 * ctldir ------> snapshotdir -------> snapshot
46 * The 'snapshot' node contains just enough information to lookup '..' and act
47 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
48 * perform an automount of the underlying filesystem and return the
49 * corresponding vnode.
51 * All mounts are handled automatically by the kernel, but unmounts are
52 * (currently) handled from user land. The main reason is that there is no
53 * reliable way to auto-unmount the filesystem when it's "no longer in use".
54 * When the user unmounts a filesystem, we call zfsctl_unmount(), which
55 * unmounts any snapshots within the snapshot directory.
57 * The '.zfs', '.zfs/snapshot', and all directories created under
58 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
59 * share the same vfs_t as the head filesystem (what '.zfs' lives under).
61 * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
62 * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
63 * However, vnodes within these mounted on file systems have their v_vfsp
64 * fields set to the head filesystem to make NFS happy (see
65 * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
66 * so that it cannot be freed until all snapshots have been unmounted.
69 #include <sys/zfs_context.h>
70 #include <sys/zfs_ctldir.h>
71 #include <sys/zfs_ioctl.h>
72 #include <sys/zfs_vfsops.h>
73 #include <sys/namei.h>
77 #include <sys/dsl_deleg.h>
78 #include <sys/mount.h>
79 #include <sys/sunddi.h>
81 #include "zfs_namecheck.h"
83 typedef struct zfsctl_node {
84 gfs_dir_t zc_gfs_private;
86 timestruc_t zc_cmtime; /* ctime and mtime, always the same */
89 typedef struct zfsctl_snapdir {
90 zfsctl_node_t sd_node;
102 snapentry_compare(const void *a, const void *b)
104 const zfs_snapentry_t *sa = a;
105 const zfs_snapentry_t *sb = b;
106 int ret = strcmp(sa->se_name, sb->se_name);
116 static struct vop_vector zfsctl_ops_root;
117 static struct vop_vector zfsctl_ops_snapdir;
118 static struct vop_vector zfsctl_ops_snapshot;
120 static vnode_t *zfsctl_mknode_snapdir(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 have only a single static entry, 'snapshot'.
127 static gfs_dirent_t zfsctl_root_entries[] = {
128 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
132 /* include . and .. in the calculation */
133 #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \
134 sizeof (gfs_dirent_t)) + 1)
138 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
139 * directories. This is called from the ZFS init routine, and initializes the
140 * vnode ops vectors that we'll be using.
153 * Return the inode number associated with the 'snapshot' directory.
157 zfsctl_root_inode_cb(vnode_t *vp, int index)
160 return (ZFSCTL_INO_SNAPDIR);
164 * Create the '.zfs' directory. This directory is cached as part of the VFS
165 * structure. This results in a hold on the vfs_t. The code in zfs_umount()
166 * therefore checks against a vfs_count of 2 instead of 1. This reference
167 * is removed when the ctldir is destroyed in the unmount.
170 zfsctl_create(zfsvfs_t *zfsvfs)
175 ASSERT(zfsvfs->z_ctldir == NULL);
177 vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
178 &zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
179 zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
181 zcp->zc_id = ZFSCTL_INO_ROOT;
183 VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp) == 0);
184 ZFS_TIME_DECODE(&zcp->zc_cmtime, VTOZ(rvp)->z_phys->zp_crtime);
188 * We're only faking the fact that we have a root of a filesystem for
189 * the sake of the GFS interfaces. Undo the flag manipulation it did
192 vp->v_vflag &= ~VV_ROOT;
194 zfsvfs->z_ctldir = vp;
200 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
201 * There might still be more references if we were force unmounted, but only
202 * new zfs_inactive() calls can occur and they don't reference .zfs
205 zfsctl_destroy(zfsvfs_t *zfsvfs)
207 VN_RELE(zfsvfs->z_ctldir);
208 zfsvfs->z_ctldir = NULL;
212 * Given a root znode, retrieve the associated .zfs directory.
213 * Add a hold to the vnode and return it.
216 zfsctl_root(znode_t *zp)
218 ASSERT(zfs_has_ctldir(zp));
219 VN_HOLD(zp->z_zfsvfs->z_ctldir);
220 return (zp->z_zfsvfs->z_ctldir);
224 * Common open routine. Disallow any write access.
228 zfsctl_common_open(struct vop_open_args *ap)
230 int flags = ap->a_mode;
239 * Common close routine. Nothing to do here.
243 zfsctl_common_close(struct vop_close_args *ap)
249 * Common access routine. Disallow writes.
253 zfsctl_common_access(ap)
254 struct vop_access_args /* {
257 struct ucred *a_cred;
261 int mode = ap->a_accmode;
270 * Common getattr function. Fill in basic information.
273 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
275 zfsctl_node_t *zcp = vp->v_data;
282 * We are a purly virtual object, so we have no
283 * blocksize or allocated blocks.
288 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
289 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
293 * We live in the now (for atime).
297 vap->va_mtime = vap->va_ctime = vap->va_birthtime = zcp->zc_cmtime;
298 /* FreeBSD: Reset chflags(2) flags. */
304 zfsctl_common_fid(ap)
305 struct vop_fid_args /* {
310 vnode_t *vp = ap->a_vp;
311 fid_t *fidp = (void *)ap->a_fid;
312 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
313 zfsctl_node_t *zcp = vp->v_data;
314 uint64_t object = zcp->zc_id;
320 fidp->fid_len = SHORT_FID_LEN;
322 zfid = (zfid_short_t *)fidp;
324 zfid->zf_len = SHORT_FID_LEN;
326 for (i = 0; i < sizeof (zfid->zf_object); i++)
327 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
329 /* .zfs znodes always have a generation number of 0 */
330 for (i = 0; i < sizeof (zfid->zf_gen); i++)
338 zfsctl_common_reclaim(ap)
339 struct vop_reclaim_args /* {
344 vnode_t *vp = ap->a_vp;
347 * Destroy the vm object and flush associated pages.
349 vnode_destroy_vobject(vp);
357 * .zfs inode namespace
359 * We need to generate unique inode numbers for all files and directories
360 * within the .zfs pseudo-filesystem. We use the following scheme:
365 * .zfs/snapshot/<snap> objectid(snap)
368 #define ZFSCTL_INO_SNAP(id) (id)
371 * Get root directory attributes.
375 zfsctl_root_getattr(ap)
376 struct vop_getattr_args /* {
379 struct ucred *a_cred;
383 struct vnode *vp = ap->a_vp;
384 struct vattr *vap = ap->a_vap;
385 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
388 vap->va_nodeid = ZFSCTL_INO_ROOT;
389 vap->va_nlink = vap->va_size = NROOT_ENTRIES;
391 zfsctl_common_getattr(vp, vap);
398 * Special case the handling of "..".
402 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
403 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
404 int *direntflags, pathname_t *realpnp)
406 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
410 * No extended attributes allowed under .zfs
412 if (flags & LOOKUP_XATTR)
417 if (strcmp(nm, "..") == 0) {
418 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp);
422 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
423 cr, ct, direntflags, realpnp);
432 * Special case the handling of "..".
436 zfsctl_freebsd_root_lookup(ap)
437 struct vop_lookup_args /* {
439 struct vnode **a_vpp;
440 struct componentname *a_cnp;
443 vnode_t *dvp = ap->a_dvp;
444 vnode_t **vpp = ap->a_vpp;
445 cred_t *cr = ap->a_cnp->cn_cred;
446 int flags = ap->a_cnp->cn_flags;
447 int nameiop = ap->a_cnp->cn_nameiop;
448 char nm[NAME_MAX + 1];
451 if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE))
454 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
455 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
457 err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL);
458 if (err == 0 && (nm[0] != '.' || nm[1] != '\0'))
459 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
464 static struct vop_vector zfsctl_ops_root = {
465 .vop_default = &default_vnodeops,
466 .vop_open = zfsctl_common_open,
467 .vop_close = zfsctl_common_close,
468 .vop_ioctl = VOP_EINVAL,
469 .vop_getattr = zfsctl_root_getattr,
470 .vop_access = zfsctl_common_access,
471 .vop_readdir = gfs_vop_readdir,
472 .vop_lookup = zfsctl_freebsd_root_lookup,
473 .vop_inactive = gfs_vop_inactive,
474 .vop_reclaim = zfsctl_common_reclaim,
475 .vop_fid = zfsctl_common_fid,
479 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
481 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
483 if (snapshot_namecheck(name, NULL, NULL) != 0)
485 dmu_objset_name(os, zname);
486 if (strlen(zname) + 1 + strlen(name) >= len)
487 return (ENAMETOOLONG);
488 (void) strcat(zname, "@");
489 (void) strcat(zname, name);
494 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
496 vnode_t *svp = sep->se_root;
499 ASSERT(vn_ismntpt(svp));
501 /* this will be dropped by dounmount() */
502 if ((error = vn_vfswlock(svp)) != 0)
505 return (dounmount(vn_mountedvfs(svp), fflags, curthread));
510 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
515 char newpath[MAXNAMELEN];
518 ASSERT(MUTEX_HELD(&sdp->sd_lock));
521 vfsp = vn_mountedvfs(sep->se_root);
522 ASSERT(vfsp != NULL);
527 * Change the name in the AVL tree.
529 avl_remove(&sdp->sd_snaps, sep);
530 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
531 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
532 (void) strcpy(sep->se_name, nm);
533 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
534 avl_insert(&sdp->sd_snaps, sep, where);
537 * Change the current mountpoint info:
538 * - update the tail of the mntpoint path
539 * - update the tail of the resource path
541 pathref = vfs_getmntpoint(vfsp);
542 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
543 VERIFY((tail = strrchr(newpath, '/')) != NULL);
545 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
546 (void) strcat(newpath, nm);
547 refstr_rele(pathref);
548 vfs_setmntpoint(vfsp, newpath);
550 pathref = vfs_getresource(vfsp);
551 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
552 VERIFY((tail = strrchr(newpath, '@')) != NULL);
554 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
555 (void) strcat(newpath, nm);
556 refstr_rele(pathref);
557 vfs_setresource(vfsp, newpath);
566 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
567 cred_t *cr, caller_context_t *ct, int flags)
569 zfsctl_snapdir_t *sdp = sdvp->v_data;
570 zfs_snapentry_t search, *sep;
573 char from[MAXNAMELEN], to[MAXNAMELEN];
574 char real[MAXNAMELEN];
577 zfsvfs = sdvp->v_vfsp->vfs_data;
580 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
581 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
585 } else if (err != ENOTSUP) {
593 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
595 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
597 err = zfs_secpolicy_rename_perms(from, to, cr);
602 * Cannot move snapshots out of the snapdir.
607 if (strcmp(snm, tnm) == 0)
610 mutex_enter(&sdp->sd_lock);
612 search.se_name = (char *)snm;
613 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
614 mutex_exit(&sdp->sd_lock);
618 err = dmu_objset_rename(from, to, B_FALSE);
620 zfsctl_rename_snap(sdp, sep, tnm);
622 mutex_exit(&sdp->sd_lock);
631 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
632 caller_context_t *ct, int flags)
634 zfsctl_snapdir_t *sdp = dvp->v_data;
635 zfs_snapentry_t *sep;
636 zfs_snapentry_t search;
638 char snapname[MAXNAMELEN];
639 char real[MAXNAMELEN];
642 zfsvfs = dvp->v_vfsp->vfs_data;
645 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
647 err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
651 } else if (err != ENOTSUP) {
659 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
661 err = zfs_secpolicy_destroy_perms(snapname, cr);
665 mutex_enter(&sdp->sd_lock);
667 search.se_name = name;
668 sep = avl_find(&sdp->sd_snaps, &search, NULL);
670 avl_remove(&sdp->sd_snaps, sep);
671 err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
675 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
676 avl_insert(&sdp->sd_snaps, sep, where);
678 err = dmu_objset_destroy(snapname);
683 mutex_exit(&sdp->sd_lock);
690 * This creates a snapshot under '.zfs/snapshot'.
694 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp,
695 cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
697 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
698 char name[MAXNAMELEN];
700 static enum symfollow follow = NO_FOLLOW;
701 static enum uio_seg seg = UIO_SYSSPACE;
703 if (snapshot_namecheck(dirname, NULL, NULL) != 0)
706 dmu_objset_name(zfsvfs->z_os, name);
710 err = zfs_secpolicy_snapshot_perms(name, cr);
715 err = dmu_objset_snapshot(name, dirname, B_FALSE);
718 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
725 zfsctl_freebsd_snapdir_mkdir(ap)
726 struct vop_mkdir_args /* {
728 struct vnode **a_vpp;
729 struct componentname *a_cnp;
734 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
736 return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL,
737 ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL));
741 * Lookup entry point for the 'snapshot' directory. Try to open the
742 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
743 * Perform a mount of the associated dataset on top of the vnode.
747 zfsctl_snapdir_lookup(ap)
748 struct vop_lookup_args /* {
750 struct vnode **a_vpp;
751 struct componentname *a_cnp;
754 vnode_t *dvp = ap->a_dvp;
755 vnode_t **vpp = ap->a_vpp;
756 struct componentname *cnp = ap->a_cnp;
757 char nm[NAME_MAX + 1];
758 zfsctl_snapdir_t *sdp = dvp->v_data;
760 char snapname[MAXNAMELEN];
761 char real[MAXNAMELEN];
763 zfs_snapentry_t *sep, search;
764 size_t mountpoint_len;
766 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
771 * No extended attributes allowed under .zfs
773 if (flags & LOOKUP_XATTR)
775 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm));
776 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1);
778 ASSERT(dvp->v_type == VDIR);
780 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0)
786 * If we get a recursive call, that means we got called
787 * from the domount() code while it was trying to look up the
788 * spec (which looks like a local path for zfs). We need to
789 * add some flag to domount() to tell it not to do this lookup.
791 if (MUTEX_HELD(&sdp->sd_lock))
796 if (flags & FIGNORECASE) {
797 boolean_t conflict = B_FALSE;
799 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
800 MAXNAMELEN, &conflict);
802 strlcpy(nm, real, sizeof(nm));
803 } else if (err != ENOTSUP) {
809 (void) strlcpy(realpnp->pn_buf, nm,
810 realpnp->pn_bufsize);
811 if (conflict && direntflags)
812 *direntflags = ED_CASE_CONFLICT;
816 mutex_enter(&sdp->sd_lock);
817 search.se_name = (char *)nm;
818 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
821 err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY);
825 } else if (*vpp == sep->se_root) {
827 * The snapshot was unmounted behind our backs,
833 * VROOT was set during the traverse call. We need
834 * to clear it since we're pretending to be part
835 * of our parent's vfs.
837 (*vpp)->v_flag &= ~VROOT;
839 mutex_exit(&sdp->sd_lock);
845 * The requested snapshot is not currently mounted, look it up.
847 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
849 mutex_exit(&sdp->sd_lock);
852 * handle "ls *" or "?" in a graceful manner,
853 * forcing EILSEQ to ENOENT.
854 * Since shell ultimately passes "*" or "?" as name to lookup
856 return (err == EILSEQ ? ENOENT : err);
858 if (dmu_objset_open(snapname, DMU_OST_ZFS,
859 DS_MODE_USER | DS_MODE_READONLY, &snap) != 0) {
860 mutex_exit(&sdp->sd_lock);
861 /* Translate errors and add SAVENAME when needed. */
862 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
864 cnp->cn_flags |= SAVENAME;
872 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
873 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
874 (void) strcpy(sep->se_name, nm);
875 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
877 avl_insert(&sdp->sd_snaps, sep, where);
879 dmu_objset_close(snap);
881 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) +
882 strlen("/.zfs/snapshot/") + strlen(nm) + 1;
883 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
884 (void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
885 dvp->v_vfsp->mnt_stat.f_mntonname, nm);
886 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0);
887 kmem_free(mountpoint, mountpoint_len);
890 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
892 * This is where we lie about our v_vfsp in order to
893 * make .zfs/snapshot/<snapname> accessible over NFS
894 * without requiring manual mounts of <snapname>.
896 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
897 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
899 mutex_exit(&sdp->sd_lock);
908 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
909 offset_t *offp, offset_t *nextp, void *data, int flags)
911 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
912 char snapname[MAXNAMELEN];
914 boolean_t case_conflict;
920 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
921 &cookie, &case_conflict);
924 if (error == ENOENT) {
931 if (flags & V_RDDIR_ENTFLAGS) {
932 edirent_t *eodp = dp;
934 (void) strcpy(eodp->ed_name, snapname);
935 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
936 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
938 struct dirent64 *odp = dp;
940 (void) strcpy(odp->d_name, snapname);
941 odp->d_ino = ZFSCTL_INO_SNAP(id);
951 * pvp is the '.zfs' directory (zfsctl_node_t).
952 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
954 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
955 * when a lookup is performed on .zfs for "snapshot".
958 zfsctl_mknode_snapdir(vnode_t *pvp)
961 zfsctl_snapdir_t *sdp;
963 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp,
964 &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
965 zfsctl_snapdir_readdir_cb, NULL);
967 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
968 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
969 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
970 avl_create(&sdp->sd_snaps, snapentry_compare,
971 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
978 zfsctl_snapdir_getattr(ap)
979 struct vop_getattr_args /* {
982 struct ucred *a_cred;
986 struct vnode *vp = ap->a_vp;
987 struct vattr *vap = ap->a_vap;
988 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
989 zfsctl_snapdir_t *sdp = vp->v_data;
992 zfsctl_common_getattr(vp, vap);
993 vap->va_nodeid = gfs_file_inode(vp);
994 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1002 zfsctl_snapdir_inactive(ap)
1003 struct vop_inactive_args /* {
1005 struct thread *a_td;
1008 vnode_t *vp = ap->a_vp;
1009 zfsctl_snapdir_t *sdp = vp->v_data;
1010 zfs_snapentry_t *sep;
1013 * On forced unmount we have to free snapshots from here.
1015 mutex_enter(&sdp->sd_lock);
1016 while ((sep = avl_first(&sdp->sd_snaps)) != NULL) {
1017 avl_remove(&sdp->sd_snaps, sep);
1018 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1019 kmem_free(sep, sizeof (zfs_snapentry_t));
1021 mutex_exit(&sdp->sd_lock);
1022 gfs_dir_inactive(vp);
1023 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1024 mutex_destroy(&sdp->sd_lock);
1025 avl_destroy(&sdp->sd_snaps);
1026 kmem_free(sdp, sizeof (zfsctl_snapdir_t));
1031 static struct vop_vector zfsctl_ops_snapdir = {
1032 .vop_default = &default_vnodeops,
1033 .vop_open = zfsctl_common_open,
1034 .vop_close = zfsctl_common_close,
1035 .vop_ioctl = VOP_EINVAL,
1036 .vop_getattr = zfsctl_snapdir_getattr,
1037 .vop_access = zfsctl_common_access,
1038 .vop_mkdir = zfsctl_freebsd_snapdir_mkdir,
1039 .vop_readdir = gfs_vop_readdir,
1040 .vop_lookup = zfsctl_snapdir_lookup,
1041 .vop_inactive = zfsctl_snapdir_inactive,
1042 .vop_reclaim = zfsctl_common_reclaim,
1043 .vop_fid = zfsctl_common_fid,
1047 * pvp is the GFS vnode '.zfs/snapshot'.
1049 * This creates a GFS node under '.zfs/snapshot' representing each
1050 * snapshot. This newly created GFS node is what we mount snapshot
1054 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1059 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp,
1060 &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1063 zcp->zc_id = objset;
1064 VFS_HOLD(vp->v_vfsp);
1071 zfsctl_snapshot_inactive(ap)
1072 struct vop_inactive_args /* {
1074 struct thread *a_td;
1077 vnode_t *vp = ap->a_vp;
1078 cred_t *cr = ap->a_td->td_ucred;
1079 struct vop_inactive_args iap;
1080 zfsctl_snapdir_t *sdp;
1081 zfs_snapentry_t *sep, *next;
1085 if (vp->v_count > 0)
1088 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1092 if (!(locked = MUTEX_HELD(&sdp->sd_lock)))
1093 mutex_enter(&sdp->sd_lock);
1095 ASSERT(!vn_ismntpt(vp));
1097 sep = avl_first(&sdp->sd_snaps);
1098 while (sep != NULL) {
1099 next = AVL_NEXT(&sdp->sd_snaps, sep);
1101 if (sep->se_root == vp) {
1102 avl_remove(&sdp->sd_snaps, sep);
1103 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1104 kmem_free(sep, sizeof (zfs_snapentry_t));
1109 ASSERT(sep != NULL);
1112 mutex_exit(&sdp->sd_lock);
1115 VFS_RELE(vp->v_vfsp);
1118 * Dispose of the vnode for the snapshot mount point.
1119 * This is safe to do because once this entry has been removed
1120 * from the AVL tree, it can't be found again, so cannot become
1121 * "active". If we lookup the same name again we will end up
1122 * creating a new vnode.
1125 return (gfs_vop_inactive(&iap));
1129 zfsctl_traverse_begin(vnode_t **vpp, int lktype)
1133 /* Snapshot should be already mounted, but just in case. */
1134 if (vn_mountedvfs(*vpp) == NULL)
1136 return (traverse(vpp, lktype));
1140 zfsctl_traverse_end(vnode_t *vp, int err)
1150 zfsctl_snapshot_getattr(ap)
1151 struct vop_getattr_args /* {
1153 struct vattr *a_vap;
1154 struct ucred *a_cred;
1157 vnode_t *vp = ap->a_vp;
1160 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1162 err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred);
1163 zfsctl_traverse_end(vp, err);
1168 zfsctl_snapshot_fid(ap)
1169 struct vop_fid_args /* {
1174 vnode_t *vp = ap->a_vp;
1177 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY);
1179 err = VOP_VPTOFH(vp, (void *)ap->a_fid);
1180 zfsctl_traverse_end(vp, err);
1185 zfsctl_snapshot_lookup(ap)
1186 struct vop_lookup_args /* {
1187 struct vnode *a_dvp;
1188 struct vnode **a_vpp;
1189 struct componentname *a_cnp;
1192 vnode_t *dvp = ap->a_dvp;
1193 vnode_t **vpp = ap->a_vpp;
1194 struct componentname *cnp = ap->a_cnp;
1195 cred_t *cr = ap->a_cnp->cn_cred;
1196 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
1199 if (cnp->cn_namelen != 2 || cnp->cn_nameptr[0] != '.' ||
1200 cnp->cn_nameptr[1] != '.') {
1204 ASSERT(dvp->v_type == VDIR);
1205 ASSERT(zfsvfs->z_ctldir != NULL);
1207 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", vpp,
1208 NULL, 0, NULL, cr, NULL, NULL, NULL);
1210 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1215 zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap)
1217 zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data;
1219 zfsctl_snapdir_t *sdp;
1220 zfs_snapentry_t *sep;
1223 ASSERT(zfsvfs->z_ctldir != NULL);
1224 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1225 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1230 mutex_enter(&sdp->sd_lock);
1231 sep = avl_first(&sdp->sd_snaps);
1232 while (sep != NULL) {
1236 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1239 mutex_exit(&sdp->sd_lock);
1244 len = strlen(sep->se_name);
1245 *ap->a_buflen -= len;
1246 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len);
1247 mutex_exit(&sdp->sd_lock);
1257 * These VP's should never see the light of day. They should always
1260 static struct vop_vector zfsctl_ops_snapshot = {
1261 .vop_default = &default_vnodeops,
1262 .vop_inactive = zfsctl_snapshot_inactive,
1263 .vop_lookup = zfsctl_snapshot_lookup,
1264 .vop_reclaim = zfsctl_common_reclaim,
1265 .vop_getattr = zfsctl_snapshot_getattr,
1266 .vop_fid = zfsctl_snapshot_fid,
1267 .vop_vptocnp = zfsctl_snapshot_vptocnp,
1271 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1273 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1275 zfsctl_snapdir_t *sdp;
1277 zfs_snapentry_t *sep;
1280 ASSERT(zfsvfs->z_ctldir != NULL);
1281 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1282 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1287 mutex_enter(&sdp->sd_lock);
1288 sep = avl_first(&sdp->sd_snaps);
1289 while (sep != NULL) {
1292 if (zcp->zc_id == objsetid)
1295 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1301 * Return the mounted root rather than the covered mount point.
1302 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1303 * and returns the ZFS vnode mounted on top of the GFS node.
1304 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1306 error = traverse(&vp, LK_SHARED | LK_RETRY);
1308 if (vp == sep->se_root)
1311 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1313 mutex_exit(&sdp->sd_lock);
1320 mutex_exit(&sdp->sd_lock);
1329 * Unmount any snapshots for the given filesystem. This is called from
1330 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1334 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1336 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1338 zfsctl_snapdir_t *sdp;
1339 zfs_snapentry_t *sep, *next;
1342 ASSERT(zfsvfs->z_ctldir != NULL);
1343 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1344 NULL, 0, NULL, cr, NULL, NULL, NULL);
1349 mutex_enter(&sdp->sd_lock);
1351 sep = avl_first(&sdp->sd_snaps);
1352 while (sep != NULL) {
1353 next = AVL_NEXT(&sdp->sd_snaps, sep);
1356 * If this snapshot is not mounted, then it must
1357 * have just been unmounted by somebody else, and
1358 * will be cleaned up by zfsctl_snapdir_inactive().
1360 if (vn_ismntpt(sep->se_root)) {
1361 error = zfsctl_unmount_snap(sep, fflags, cr);
1366 * Before reinserting snapshot to the tree,
1367 * check if it was actually removed. For example
1368 * when snapshot mount point is busy, we will
1369 * have an error here, but there will be no need
1370 * to reinsert snapshot.
1372 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL)
1373 avl_insert(&sdp->sd_snaps, sep, where);
1380 mutex_exit(&sdp->sd_lock);