1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51512Sek110237 * Common Development and Distribution License (the "License"). 61512Sek110237 * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 22*8547SMark.Shellenbaum@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens /* 27789Sahrens * ZFS control directory (a.k.a. ".zfs") 28789Sahrens * 29789Sahrens * This directory provides a common location for all ZFS meta-objects. 30789Sahrens * Currently, this is only the 'snapshot' directory, but this may expand in the 31789Sahrens * future. The elements are built using the GFS primitives, as the hierarchy 32789Sahrens * does not actually exist on disk. 33789Sahrens * 34789Sahrens * For 'snapshot', we don't want to have all snapshots always mounted, because 35789Sahrens * this would take up a huge amount of space in /etc/mnttab. We have three 36789Sahrens * types of objects: 37789Sahrens * 38789Sahrens * ctldir ------> snapshotdir -------> snapshot 39789Sahrens * | 40789Sahrens * | 41789Sahrens * V 42789Sahrens * mounted fs 43789Sahrens * 44789Sahrens * The 'snapshot' node contains just enough information to lookup '..' and act 45789Sahrens * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we 46789Sahrens * perform an automount of the underlying filesystem and return the 47789Sahrens * corresponding vnode. 48789Sahrens * 49789Sahrens * All mounts are handled automatically by the kernel, but unmounts are 50789Sahrens * (currently) handled from user land. The main reason is that there is no 51789Sahrens * reliable way to auto-unmount the filesystem when it's "no longer in use". 52789Sahrens * When the user unmounts a filesystem, we call zfsctl_unmount(), which 53789Sahrens * unmounts any snapshots within the snapshot directory. 545326Sek110237 * 555326Sek110237 * The '.zfs', '.zfs/snapshot', and all directories created under 565326Sek110237 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and 575326Sek110237 * share the same vfs_t as the head filesystem (what '.zfs' lives under). 585326Sek110237 * 595326Sek110237 * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>' 605326Sek110237 * (ie: snapshots) are ZFS nodes and have their own unique vfs_t. 615326Sek110237 * However, vnodes within these mounted on file systems have their v_vfsp 625326Sek110237 * fields set to the head filesystem to make NFS happy (see 636068Sck153898 * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t 646068Sck153898 * so that it cannot be freed until all snapshots have been unmounted. 65789Sahrens */ 66789Sahrens 67789Sahrens #include <fs/fs_subr.h> 68789Sahrens #include <sys/zfs_ctldir.h> 69789Sahrens #include <sys/zfs_ioctl.h> 70789Sahrens #include <sys/zfs_vfsops.h> 713898Srsb #include <sys/vfs_opreg.h> 72789Sahrens #include <sys/gfs.h> 73789Sahrens #include <sys/stat.h> 74789Sahrens #include <sys/dmu.h> 754543Smarks #include <sys/dsl_deleg.h> 76789Sahrens #include <sys/mount.h> 776492Stimh #include <sys/sunddi.h> 78789Sahrens 796658Smarks #include "zfs_namecheck.h" 806658Smarks 816068Sck153898 typedef struct zfsctl_node { 826068Sck153898 gfs_dir_t zc_gfs_private; 836068Sck153898 uint64_t zc_id; 846068Sck153898 timestruc_t zc_cmtime; /* ctime and mtime, always the same */ 856068Sck153898 } zfsctl_node_t; 866068Sck153898 876068Sck153898 typedef struct zfsctl_snapdir { 886068Sck153898 zfsctl_node_t sd_node; 896068Sck153898 kmutex_t sd_lock; 906068Sck153898 avl_tree_t sd_snaps; 916068Sck153898 } zfsctl_snapdir_t; 926068Sck153898 93789Sahrens typedef struct { 94789Sahrens char *se_name; 95789Sahrens vnode_t *se_root; 96789Sahrens avl_node_t se_node; 97789Sahrens } zfs_snapentry_t; 98789Sahrens 99789Sahrens static int 100789Sahrens snapentry_compare(const void *a, const void *b) 101789Sahrens { 102789Sahrens const zfs_snapentry_t *sa = a; 103789Sahrens const zfs_snapentry_t *sb = b; 104789Sahrens int ret = strcmp(sa->se_name, sb->se_name); 105789Sahrens 106789Sahrens if (ret < 0) 107789Sahrens return (-1); 108789Sahrens else if (ret > 0) 109789Sahrens return (1); 110789Sahrens else 111789Sahrens return (0); 112789Sahrens } 113789Sahrens 114789Sahrens vnodeops_t *zfsctl_ops_root; 115789Sahrens vnodeops_t *zfsctl_ops_snapdir; 116789Sahrens vnodeops_t *zfsctl_ops_snapshot; 117789Sahrens 118789Sahrens static const fs_operation_def_t zfsctl_tops_root[]; 119789Sahrens static const fs_operation_def_t zfsctl_tops_snapdir[]; 120789Sahrens static const fs_operation_def_t zfsctl_tops_snapshot[]; 121789Sahrens 122789Sahrens static vnode_t *zfsctl_mknode_snapdir(vnode_t *); 123789Sahrens static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset); 1246068Sck153898 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *); 125789Sahrens 126789Sahrens static gfs_opsvec_t zfsctl_opsvec[] = { 127789Sahrens { ".zfs", zfsctl_tops_root, &zfsctl_ops_root }, 128789Sahrens { ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir }, 129789Sahrens { ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot }, 130789Sahrens { NULL } 131789Sahrens }; 132789Sahrens 133789Sahrens /* 134789Sahrens * Root directory elements. We have only a single static entry, 'snapshot'. 135789Sahrens */ 136789Sahrens static gfs_dirent_t zfsctl_root_entries[] = { 137789Sahrens { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE }, 138789Sahrens { NULL } 139789Sahrens }; 140789Sahrens 141789Sahrens /* include . and .. in the calculation */ 142789Sahrens #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \ 143789Sahrens sizeof (gfs_dirent_t)) + 1) 144789Sahrens 145789Sahrens 146789Sahrens /* 147789Sahrens * Initialize the various GFS pieces we'll need to create and manipulate .zfs 148789Sahrens * directories. This is called from the ZFS init routine, and initializes the 149789Sahrens * vnode ops vectors that we'll be using. 150789Sahrens */ 151789Sahrens void 152789Sahrens zfsctl_init(void) 153789Sahrens { 154789Sahrens VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0); 155789Sahrens } 156789Sahrens 157789Sahrens void 158789Sahrens zfsctl_fini(void) 159789Sahrens { 160789Sahrens /* 161789Sahrens * Remove vfsctl vnode ops 162789Sahrens */ 163789Sahrens if (zfsctl_ops_root) 164789Sahrens vn_freevnodeops(zfsctl_ops_root); 165789Sahrens if (zfsctl_ops_snapdir) 166789Sahrens vn_freevnodeops(zfsctl_ops_snapdir); 167789Sahrens if (zfsctl_ops_snapshot) 168789Sahrens vn_freevnodeops(zfsctl_ops_snapshot); 169789Sahrens 170789Sahrens zfsctl_ops_root = NULL; 171789Sahrens zfsctl_ops_snapdir = NULL; 172789Sahrens zfsctl_ops_snapshot = NULL; 173789Sahrens } 174789Sahrens 175789Sahrens /* 176789Sahrens * Return the inode number associated with the 'snapshot' directory. 177789Sahrens */ 178789Sahrens /* ARGSUSED */ 179789Sahrens static ino64_t 180789Sahrens zfsctl_root_inode_cb(vnode_t *vp, int index) 181789Sahrens { 182789Sahrens ASSERT(index == 0); 183789Sahrens return (ZFSCTL_INO_SNAPDIR); 184789Sahrens } 185789Sahrens 186789Sahrens /* 187789Sahrens * Create the '.zfs' directory. This directory is cached as part of the VFS 188789Sahrens * structure. This results in a hold on the vfs_t. The code in zfs_umount() 189789Sahrens * therefore checks against a vfs_count of 2 instead of 1. This reference 190789Sahrens * is removed when the ctldir is destroyed in the unmount. 191789Sahrens */ 192789Sahrens void 193789Sahrens zfsctl_create(zfsvfs_t *zfsvfs) 194789Sahrens { 1951571Sek110237 vnode_t *vp, *rvp; 196789Sahrens zfsctl_node_t *zcp; 197789Sahrens 198789Sahrens ASSERT(zfsvfs->z_ctldir == NULL); 199789Sahrens 200789Sahrens vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs, 201789Sahrens zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries, 202789Sahrens zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL); 203789Sahrens zcp = vp->v_data; 204789Sahrens zcp->zc_id = ZFSCTL_INO_ROOT; 205789Sahrens 2061571Sek110237 VERIFY(VFS_ROOT(zfsvfs->z_vfs, &rvp) == 0); 2071571Sek110237 ZFS_TIME_DECODE(&zcp->zc_cmtime, VTOZ(rvp)->z_phys->zp_crtime); 2081571Sek110237 VN_RELE(rvp); 2091571Sek110237 210789Sahrens /* 211789Sahrens * We're only faking the fact that we have a root of a filesystem for 212789Sahrens * the sake of the GFS interfaces. Undo the flag manipulation it did 213789Sahrens * for us. 214789Sahrens */ 215789Sahrens vp->v_flag &= ~(VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT); 216789Sahrens 217789Sahrens zfsvfs->z_ctldir = vp; 218789Sahrens } 219789Sahrens 220789Sahrens /* 2211298Sperrin * Destroy the '.zfs' directory. Only called when the filesystem is unmounted. 2221298Sperrin * There might still be more references if we were force unmounted, but only 2231298Sperrin * new zfs_inactive() calls can occur and they don't reference .zfs 224789Sahrens */ 225789Sahrens void 226789Sahrens zfsctl_destroy(zfsvfs_t *zfsvfs) 227789Sahrens { 228789Sahrens VN_RELE(zfsvfs->z_ctldir); 229789Sahrens zfsvfs->z_ctldir = NULL; 230789Sahrens } 231789Sahrens 232789Sahrens /* 233789Sahrens * Given a root znode, retrieve the associated .zfs directory. 234789Sahrens * Add a hold to the vnode and return it. 235789Sahrens */ 236789Sahrens vnode_t * 237789Sahrens zfsctl_root(znode_t *zp) 238789Sahrens { 239789Sahrens ASSERT(zfs_has_ctldir(zp)); 240789Sahrens VN_HOLD(zp->z_zfsvfs->z_ctldir); 241789Sahrens return (zp->z_zfsvfs->z_ctldir); 242789Sahrens } 243789Sahrens 244789Sahrens /* 245789Sahrens * Common open routine. Disallow any write access. 246789Sahrens */ 247789Sahrens /* ARGSUSED */ 248789Sahrens static int 2495331Samw zfsctl_common_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct) 250789Sahrens { 251789Sahrens if (flags & FWRITE) 252789Sahrens return (EACCES); 253789Sahrens 254789Sahrens return (0); 255789Sahrens } 256789Sahrens 257789Sahrens /* 258789Sahrens * Common close routine. Nothing to do here. 259789Sahrens */ 260789Sahrens /* ARGSUSED */ 261789Sahrens static int 262789Sahrens zfsctl_common_close(vnode_t *vpp, int flags, int count, offset_t off, 2635331Samw cred_t *cr, caller_context_t *ct) 264789Sahrens { 265789Sahrens return (0); 266789Sahrens } 267789Sahrens 268789Sahrens /* 269789Sahrens * Common access routine. Disallow writes. 270789Sahrens */ 271789Sahrens /* ARGSUSED */ 272789Sahrens static int 2735331Samw zfsctl_common_access(vnode_t *vp, int mode, int flags, cred_t *cr, 2745331Samw caller_context_t *ct) 275789Sahrens { 276*8547SMark.Shellenbaum@Sun.COM if (flags & V_ACE_MASK) { 277*8547SMark.Shellenbaum@Sun.COM if (mode & ACE_ALL_WRITE_PERMS) 278*8547SMark.Shellenbaum@Sun.COM return (EACCES); 279*8547SMark.Shellenbaum@Sun.COM } else { 280*8547SMark.Shellenbaum@Sun.COM if (mode & VWRITE) 281*8547SMark.Shellenbaum@Sun.COM return (EACCES); 282*8547SMark.Shellenbaum@Sun.COM } 283789Sahrens 284789Sahrens return (0); 285789Sahrens } 286789Sahrens 287789Sahrens /* 288789Sahrens * Common getattr function. Fill in basic information. 289789Sahrens */ 290789Sahrens static void 291789Sahrens zfsctl_common_getattr(vnode_t *vp, vattr_t *vap) 292789Sahrens { 2931571Sek110237 zfsctl_node_t *zcp = vp->v_data; 2941571Sek110237 timestruc_t now; 295789Sahrens 296789Sahrens vap->va_uid = 0; 297789Sahrens vap->va_gid = 0; 298789Sahrens vap->va_rdev = 0; 299789Sahrens /* 300789Sahrens * We are a purly virtual object, so we have no 301789Sahrens * blocksize or allocated blocks. 302789Sahrens */ 303789Sahrens vap->va_blksize = 0; 304789Sahrens vap->va_nblocks = 0; 305789Sahrens vap->va_seq = 0; 306789Sahrens vap->va_fsid = vp->v_vfsp->vfs_dev; 307789Sahrens vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | 308789Sahrens S_IROTH | S_IXOTH; 309789Sahrens vap->va_type = VDIR; 310789Sahrens /* 3111571Sek110237 * We live in the now (for atime). 312789Sahrens */ 313789Sahrens gethrestime(&now); 3141571Sek110237 vap->va_atime = now; 3151571Sek110237 vap->va_mtime = vap->va_ctime = zcp->zc_cmtime; 316789Sahrens } 317789Sahrens 3185331Samw /*ARGSUSED*/ 319789Sahrens static int 3205331Samw zfsctl_common_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 321789Sahrens { 322789Sahrens zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 323789Sahrens zfsctl_node_t *zcp = vp->v_data; 324789Sahrens uint64_t object = zcp->zc_id; 325789Sahrens zfid_short_t *zfid; 326789Sahrens int i; 327789Sahrens 328789Sahrens ZFS_ENTER(zfsvfs); 329789Sahrens 330789Sahrens if (fidp->fid_len < SHORT_FID_LEN) { 331789Sahrens fidp->fid_len = SHORT_FID_LEN; 3321512Sek110237 ZFS_EXIT(zfsvfs); 333789Sahrens return (ENOSPC); 334789Sahrens } 335789Sahrens 336789Sahrens zfid = (zfid_short_t *)fidp; 337789Sahrens 338789Sahrens zfid->zf_len = SHORT_FID_LEN; 339789Sahrens 340789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 341789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 342789Sahrens 343789Sahrens /* .zfs znodes always have a generation number of 0 */ 344789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 345789Sahrens zfid->zf_gen[i] = 0; 346789Sahrens 347789Sahrens ZFS_EXIT(zfsvfs); 348789Sahrens return (0); 349789Sahrens } 350789Sahrens 351789Sahrens /* 352789Sahrens * .zfs inode namespace 353789Sahrens * 354789Sahrens * We need to generate unique inode numbers for all files and directories 355789Sahrens * within the .zfs pseudo-filesystem. We use the following scheme: 356789Sahrens * 357789Sahrens * ENTRY ZFSCTL_INODE 358789Sahrens * .zfs 1 359789Sahrens * .zfs/snapshot 2 360789Sahrens * .zfs/snapshot/<snap> objectid(snap) 361789Sahrens */ 362789Sahrens 363789Sahrens #define ZFSCTL_INO_SNAP(id) (id) 364789Sahrens 365789Sahrens /* 366789Sahrens * Get root directory attributes. 367789Sahrens */ 368789Sahrens /* ARGSUSED */ 369789Sahrens static int 3705331Samw zfsctl_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 3715331Samw caller_context_t *ct) 372789Sahrens { 373789Sahrens zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 374789Sahrens 375789Sahrens ZFS_ENTER(zfsvfs); 376789Sahrens vap->va_nodeid = ZFSCTL_INO_ROOT; 377789Sahrens vap->va_nlink = vap->va_size = NROOT_ENTRIES; 378789Sahrens 379789Sahrens zfsctl_common_getattr(vp, vap); 380789Sahrens ZFS_EXIT(zfsvfs); 381789Sahrens 382789Sahrens return (0); 383789Sahrens } 384789Sahrens 385789Sahrens /* 386789Sahrens * Special case the handling of "..". 387789Sahrens */ 388789Sahrens /* ARGSUSED */ 389789Sahrens int 390789Sahrens zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp, 3915331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 3925331Samw int *direntflags, pathname_t *realpnp) 393789Sahrens { 394789Sahrens zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 395789Sahrens int err; 396789Sahrens 3975331Samw /* 3985331Samw * No extended attributes allowed under .zfs 3995331Samw */ 4005331Samw if (flags & LOOKUP_XATTR) 4015331Samw return (EINVAL); 4025331Samw 403789Sahrens ZFS_ENTER(zfsvfs); 404789Sahrens 405789Sahrens if (strcmp(nm, "..") == 0) { 406789Sahrens err = VFS_ROOT(dvp->v_vfsp, vpp); 407789Sahrens } else { 4086492Stimh err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir, 4096492Stimh cr, ct, direntflags, realpnp); 410789Sahrens } 411789Sahrens 412789Sahrens ZFS_EXIT(zfsvfs); 413789Sahrens 414789Sahrens return (err); 415789Sahrens } 416789Sahrens 417*8547SMark.Shellenbaum@Sun.COM static int 418*8547SMark.Shellenbaum@Sun.COM zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 419*8547SMark.Shellenbaum@Sun.COM caller_context_t *ct) 420*8547SMark.Shellenbaum@Sun.COM { 421*8547SMark.Shellenbaum@Sun.COM /* 422*8547SMark.Shellenbaum@Sun.COM * We only care about ACL_ENABLED so that libsec can 423*8547SMark.Shellenbaum@Sun.COM * display ACL correctly and not default to POSIX draft. 424*8547SMark.Shellenbaum@Sun.COM */ 425*8547SMark.Shellenbaum@Sun.COM if (cmd == _PC_ACL_ENABLED) { 426*8547SMark.Shellenbaum@Sun.COM *valp = _ACL_ACE_ENABLED; 427*8547SMark.Shellenbaum@Sun.COM return (0); 428*8547SMark.Shellenbaum@Sun.COM } 429*8547SMark.Shellenbaum@Sun.COM 430*8547SMark.Shellenbaum@Sun.COM return (fs_pathconf(vp, cmd, valp, cr, ct)); 431*8547SMark.Shellenbaum@Sun.COM } 432*8547SMark.Shellenbaum@Sun.COM 433789Sahrens static const fs_operation_def_t zfsctl_tops_root[] = { 4343898Srsb { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 4353898Srsb { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 4363898Srsb { VOPNAME_IOCTL, { .error = fs_inval } }, 4373898Srsb { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } }, 4383898Srsb { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 4393898Srsb { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } }, 4403898Srsb { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } }, 4413898Srsb { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 4423898Srsb { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 443*8547SMark.Shellenbaum@Sun.COM { VOPNAME_PATHCONF, { .vop_pathconf = zfsctl_pathconf } }, 4443898Srsb { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } }, 445789Sahrens { NULL } 446789Sahrens }; 447789Sahrens 448789Sahrens static int 449789Sahrens zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname) 450789Sahrens { 451789Sahrens objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 452789Sahrens 4536658Smarks if (snapshot_namecheck(name, NULL, NULL) != 0) 4546658Smarks return (EILSEQ); 455789Sahrens dmu_objset_name(os, zname); 4561154Smaybee if (strlen(zname) + 1 + strlen(name) >= len) 4571154Smaybee return (ENAMETOOLONG); 458789Sahrens (void) strcat(zname, "@"); 459789Sahrens (void) strcat(zname, name); 460789Sahrens return (0); 461789Sahrens } 462789Sahrens 4636068Sck153898 static int 4646068Sck153898 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr) 465789Sahrens { 4666068Sck153898 vnode_t *svp = sep->se_root; 4676068Sck153898 int error; 468789Sahrens 4696068Sck153898 ASSERT(vn_ismntpt(svp)); 470789Sahrens 471789Sahrens /* this will be dropped by dounmount() */ 4726068Sck153898 if ((error = vn_vfswlock(svp)) != 0) 4736068Sck153898 return (error); 474789Sahrens 4756068Sck153898 VN_HOLD(svp); 4766068Sck153898 error = dounmount(vn_mountedvfs(svp), fflags, cr); 4776068Sck153898 if (error) { 4786068Sck153898 VN_RELE(svp); 4796068Sck153898 return (error); 4801589Smaybee } 4816068Sck153898 VFS_RELE(svp->v_vfsp); 4826068Sck153898 /* 4836068Sck153898 * We can't use VN_RELE(), as that will try to invoke 4846068Sck153898 * zfsctl_snapdir_inactive(), which would cause us to destroy 4856068Sck153898 * the sd_lock mutex held by our caller. 4866068Sck153898 */ 4876068Sck153898 ASSERT(svp->v_count == 1); 4886068Sck153898 gfs_vop_inactive(svp, cr, NULL); 489789Sahrens 490789Sahrens kmem_free(sep->se_name, strlen(sep->se_name) + 1); 491789Sahrens kmem_free(sep, sizeof (zfs_snapentry_t)); 492789Sahrens 493789Sahrens return (0); 494789Sahrens } 495789Sahrens 4961154Smaybee static void 497789Sahrens zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm) 498789Sahrens { 499789Sahrens avl_index_t where; 500789Sahrens vfs_t *vfsp; 501789Sahrens refstr_t *pathref; 502789Sahrens char newpath[MAXNAMELEN]; 503789Sahrens char *tail; 504789Sahrens 505789Sahrens ASSERT(MUTEX_HELD(&sdp->sd_lock)); 506789Sahrens ASSERT(sep != NULL); 507789Sahrens 508789Sahrens vfsp = vn_mountedvfs(sep->se_root); 509789Sahrens ASSERT(vfsp != NULL); 510789Sahrens 5111154Smaybee vfs_lock_wait(vfsp); 512789Sahrens 513789Sahrens /* 514789Sahrens * Change the name in the AVL tree. 515789Sahrens */ 516789Sahrens avl_remove(&sdp->sd_snaps, sep); 517789Sahrens kmem_free(sep->se_name, strlen(sep->se_name) + 1); 518789Sahrens sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP); 519789Sahrens (void) strcpy(sep->se_name, nm); 520789Sahrens VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL); 521789Sahrens avl_insert(&sdp->sd_snaps, sep, where); 522789Sahrens 523789Sahrens /* 524789Sahrens * Change the current mountpoint info: 525789Sahrens * - update the tail of the mntpoint path 526789Sahrens * - update the tail of the resource path 527789Sahrens */ 528789Sahrens pathref = vfs_getmntpoint(vfsp); 5292417Sahrens (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath)); 5302417Sahrens VERIFY((tail = strrchr(newpath, '/')) != NULL); 5312417Sahrens *(tail+1) = '\0'; 5322417Sahrens ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath)); 533789Sahrens (void) strcat(newpath, nm); 534789Sahrens refstr_rele(pathref); 535789Sahrens vfs_setmntpoint(vfsp, newpath); 536789Sahrens 537789Sahrens pathref = vfs_getresource(vfsp); 5382417Sahrens (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath)); 5392417Sahrens VERIFY((tail = strrchr(newpath, '@')) != NULL); 5402417Sahrens *(tail+1) = '\0'; 5412417Sahrens ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath)); 542789Sahrens (void) strcat(newpath, nm); 543789Sahrens refstr_rele(pathref); 544789Sahrens vfs_setresource(vfsp, newpath); 545789Sahrens 546789Sahrens vfs_unlock(vfsp); 547789Sahrens } 548789Sahrens 5495331Samw /*ARGSUSED*/ 550789Sahrens static int 551789Sahrens zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, 5525331Samw cred_t *cr, caller_context_t *ct, int flags) 553789Sahrens { 554789Sahrens zfsctl_snapdir_t *sdp = sdvp->v_data; 555789Sahrens zfs_snapentry_t search, *sep; 5566492Stimh zfsvfs_t *zfsvfs; 557789Sahrens avl_index_t where; 558789Sahrens char from[MAXNAMELEN], to[MAXNAMELEN]; 5596492Stimh char real[MAXNAMELEN]; 560789Sahrens int err; 561789Sahrens 5626492Stimh zfsvfs = sdvp->v_vfsp->vfs_data; 5636492Stimh ZFS_ENTER(zfsvfs); 5646492Stimh 5656492Stimh if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 5666492Stimh err = dmu_snapshot_realname(zfsvfs->z_os, snm, real, 5676492Stimh MAXNAMELEN, NULL); 5686492Stimh if (err == 0) { 5696492Stimh snm = real; 5706492Stimh } else if (err != ENOTSUP) { 5716492Stimh ZFS_EXIT(zfsvfs); 5726492Stimh return (err); 5736492Stimh } 5746492Stimh } 5756492Stimh 5766492Stimh ZFS_EXIT(zfsvfs); 5776492Stimh 5781154Smaybee err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from); 5796492Stimh if (!err) 5806492Stimh err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to); 5816492Stimh if (!err) 5826492Stimh err = zfs_secpolicy_rename_perms(from, to, cr); 5831154Smaybee if (err) 5841154Smaybee return (err); 5854543Smarks 586789Sahrens /* 587789Sahrens * Cannot move snapshots out of the snapdir. 588789Sahrens */ 589789Sahrens if (sdvp != tdvp) 590789Sahrens return (EINVAL); 591789Sahrens 592789Sahrens if (strcmp(snm, tnm) == 0) 593789Sahrens return (0); 594789Sahrens 595789Sahrens mutex_enter(&sdp->sd_lock); 596789Sahrens 597789Sahrens search.se_name = (char *)snm; 5981154Smaybee if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) { 5991154Smaybee mutex_exit(&sdp->sd_lock); 6001154Smaybee return (ENOENT); 601789Sahrens } 602789Sahrens 6034007Smmusante err = dmu_objset_rename(from, to, B_FALSE); 6041154Smaybee if (err == 0) 6051154Smaybee zfsctl_rename_snap(sdp, sep, tnm); 606789Sahrens 607789Sahrens mutex_exit(&sdp->sd_lock); 608789Sahrens 609789Sahrens return (err); 610789Sahrens } 611789Sahrens 612789Sahrens /* ARGSUSED */ 613789Sahrens static int 6145331Samw zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 6155331Samw caller_context_t *ct, int flags) 616789Sahrens { 617789Sahrens zfsctl_snapdir_t *sdp = dvp->v_data; 6186068Sck153898 zfs_snapentry_t *sep; 6196068Sck153898 zfs_snapentry_t search; 6206492Stimh zfsvfs_t *zfsvfs; 621789Sahrens char snapname[MAXNAMELEN]; 6226492Stimh char real[MAXNAMELEN]; 623789Sahrens int err; 624789Sahrens 6256492Stimh zfsvfs = dvp->v_vfsp->vfs_data; 6266492Stimh ZFS_ENTER(zfsvfs); 6276492Stimh 6286492Stimh if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 6296492Stimh 6306492Stimh err = dmu_snapshot_realname(zfsvfs->z_os, name, real, 6316492Stimh MAXNAMELEN, NULL); 6326492Stimh if (err == 0) { 6336492Stimh name = real; 6346492Stimh } else if (err != ENOTSUP) { 6356492Stimh ZFS_EXIT(zfsvfs); 6366492Stimh return (err); 6376492Stimh } 6386492Stimh } 6396492Stimh 6406492Stimh ZFS_EXIT(zfsvfs); 6416492Stimh 6421154Smaybee err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname); 6436492Stimh if (!err) 6446492Stimh err = zfs_secpolicy_destroy_perms(snapname, cr); 6451154Smaybee if (err) 6461154Smaybee return (err); 6474543Smarks 648789Sahrens mutex_enter(&sdp->sd_lock); 649789Sahrens 6506068Sck153898 search.se_name = name; 6516068Sck153898 sep = avl_find(&sdp->sd_snaps, &search, NULL); 6526068Sck153898 if (sep) { 6536068Sck153898 avl_remove(&sdp->sd_snaps, sep); 6546068Sck153898 err = zfsctl_unmount_snap(sep, MS_FORCE, cr); 6556068Sck153898 if (err) 6566068Sck153898 avl_add(&sdp->sd_snaps, sep); 6576068Sck153898 else 6586068Sck153898 err = dmu_objset_destroy(snapname); 6596068Sck153898 } else { 6606068Sck153898 err = ENOENT; 661789Sahrens } 662789Sahrens 663789Sahrens mutex_exit(&sdp->sd_lock); 664789Sahrens 665789Sahrens return (err); 666789Sahrens } 667789Sahrens 6685326Sek110237 /* 6695326Sek110237 * This creates a snapshot under '.zfs/snapshot'. 6705326Sek110237 */ 6714543Smarks /* ARGSUSED */ 6724543Smarks static int 6734543Smarks zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, 6745331Samw cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp) 6754543Smarks { 6764543Smarks zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 6774543Smarks char name[MAXNAMELEN]; 6784543Smarks int err; 6794543Smarks static enum symfollow follow = NO_FOLLOW; 6804543Smarks static enum uio_seg seg = UIO_SYSSPACE; 6814543Smarks 6826658Smarks if (snapshot_namecheck(dirname, NULL, NULL) != 0) 6836658Smarks return (EILSEQ); 6846658Smarks 6854543Smarks dmu_objset_name(zfsvfs->z_os, name); 6864543Smarks 6874543Smarks *vpp = NULL; 6884543Smarks 6894543Smarks err = zfs_secpolicy_snapshot_perms(name, cr); 6904543Smarks if (err) 6914543Smarks return (err); 6924543Smarks 6934543Smarks if (err == 0) { 6944543Smarks err = dmu_objset_snapshot(name, dirname, B_FALSE); 6954543Smarks if (err) 6964543Smarks return (err); 6974543Smarks err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp); 6984543Smarks } 6994543Smarks 7004543Smarks return (err); 7014543Smarks } 7024543Smarks 703789Sahrens /* 704789Sahrens * Lookup entry point for the 'snapshot' directory. Try to open the 705789Sahrens * snapshot if it exist, creating the pseudo filesystem vnode as necessary. 706789Sahrens * Perform a mount of the associated dataset on top of the vnode. 707789Sahrens */ 708789Sahrens /* ARGSUSED */ 709789Sahrens static int 710789Sahrens zfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp, 7115331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 7125331Samw int *direntflags, pathname_t *realpnp) 713789Sahrens { 714789Sahrens zfsctl_snapdir_t *sdp = dvp->v_data; 715789Sahrens objset_t *snap; 716789Sahrens char snapname[MAXNAMELEN]; 7176492Stimh char real[MAXNAMELEN]; 718789Sahrens char *mountpoint; 719789Sahrens zfs_snapentry_t *sep, search; 720789Sahrens struct mounta margs; 721789Sahrens vfs_t *vfsp; 722789Sahrens size_t mountpoint_len; 723789Sahrens avl_index_t where; 724789Sahrens zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 725789Sahrens int err; 726789Sahrens 7275331Samw /* 7285331Samw * No extended attributes allowed under .zfs 7295331Samw */ 7305331Samw if (flags & LOOKUP_XATTR) 7315331Samw return (EINVAL); 7325331Samw 733789Sahrens ASSERT(dvp->v_type == VDIR); 734789Sahrens 735789Sahrens if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) 736789Sahrens return (0); 737789Sahrens 738789Sahrens /* 739789Sahrens * If we get a recursive call, that means we got called 740789Sahrens * from the domount() code while it was trying to look up the 741789Sahrens * spec (which looks like a local path for zfs). We need to 742789Sahrens * add some flag to domount() to tell it not to do this lookup. 743789Sahrens */ 744789Sahrens if (MUTEX_HELD(&sdp->sd_lock)) 745789Sahrens return (ENOENT); 746789Sahrens 747789Sahrens ZFS_ENTER(zfsvfs); 748789Sahrens 7496492Stimh if (flags & FIGNORECASE) { 7506492Stimh boolean_t conflict = B_FALSE; 7516492Stimh 7526492Stimh err = dmu_snapshot_realname(zfsvfs->z_os, nm, real, 7536492Stimh MAXNAMELEN, &conflict); 7546492Stimh if (err == 0) { 7556492Stimh nm = real; 7566492Stimh } else if (err != ENOTSUP) { 7576492Stimh ZFS_EXIT(zfsvfs); 7586492Stimh return (err); 7596492Stimh } 7606492Stimh if (realpnp) 7616492Stimh (void) strlcpy(realpnp->pn_buf, nm, 7626492Stimh realpnp->pn_bufsize); 7636492Stimh if (conflict && direntflags) 7646492Stimh *direntflags = ED_CASE_CONFLICT; 7656492Stimh } 7666492Stimh 767789Sahrens mutex_enter(&sdp->sd_lock); 768789Sahrens search.se_name = (char *)nm; 769789Sahrens if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) { 770789Sahrens *vpp = sep->se_root; 771789Sahrens VN_HOLD(*vpp); 7721589Smaybee err = traverse(vpp); 7731589Smaybee if (err) { 7741589Smaybee VN_RELE(*vpp); 7751589Smaybee *vpp = NULL; 7761589Smaybee } else if (*vpp == sep->se_root) { 7771589Smaybee /* 7781589Smaybee * The snapshot was unmounted behind our backs, 7791589Smaybee * try to remount it. 7801589Smaybee */ 781789Sahrens goto domount; 7826068Sck153898 } else { 7836068Sck153898 /* 7846068Sck153898 * VROOT was set during the traverse call. We need 7856068Sck153898 * to clear it since we're pretending to be part 7866068Sck153898 * of our parent's vfs. 7876068Sck153898 */ 7886068Sck153898 (*vpp)->v_flag &= ~VROOT; 7891566Smaybee } 790789Sahrens mutex_exit(&sdp->sd_lock); 791789Sahrens ZFS_EXIT(zfsvfs); 7921589Smaybee return (err); 793789Sahrens } 794789Sahrens 795789Sahrens /* 796789Sahrens * The requested snapshot is not currently mounted, look it up. 797789Sahrens */ 7981154Smaybee err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname); 7991154Smaybee if (err) { 8001154Smaybee mutex_exit(&sdp->sd_lock); 8011154Smaybee ZFS_EXIT(zfsvfs); 8027229Smarks /* 8037229Smarks * handle "ls *" or "?" in a graceful manner, 8047229Smarks * forcing EILSEQ to ENOENT. 8057229Smarks * Since shell ultimately passes "*" or "?" as name to lookup 8067229Smarks */ 8077229Smarks return (err == EILSEQ ? ENOENT : err); 8081154Smaybee } 809789Sahrens if (dmu_objset_open(snapname, DMU_OST_ZFS, 8106689Smaybee DS_MODE_USER | DS_MODE_READONLY, &snap) != 0) { 811789Sahrens mutex_exit(&sdp->sd_lock); 812789Sahrens ZFS_EXIT(zfsvfs); 813789Sahrens return (ENOENT); 814789Sahrens } 815789Sahrens 816789Sahrens sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP); 817789Sahrens sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP); 818789Sahrens (void) strcpy(sep->se_name, nm); 819789Sahrens *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap)); 820789Sahrens avl_insert(&sdp->sd_snaps, sep, where); 821789Sahrens 822789Sahrens dmu_objset_close(snap); 823789Sahrens domount: 824789Sahrens mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) + 825789Sahrens strlen("/.zfs/snapshot/") + strlen(nm) + 1; 826789Sahrens mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP); 827789Sahrens (void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s", 828789Sahrens refstr_value(dvp->v_vfsp->vfs_mntpt), nm); 829789Sahrens 830789Sahrens margs.spec = snapname; 831789Sahrens margs.dir = mountpoint; 832789Sahrens margs.flags = MS_SYSSPACE | MS_NOMNTTAB; 833789Sahrens margs.fstype = "zfs"; 834789Sahrens margs.dataptr = NULL; 835789Sahrens margs.datalen = 0; 836789Sahrens margs.optptr = NULL; 837789Sahrens margs.optlen = 0; 838789Sahrens 839789Sahrens err = domount("zfs", &margs, *vpp, kcred, &vfsp); 840789Sahrens kmem_free(mountpoint, mountpoint_len); 841789Sahrens 842816Smaybee if (err == 0) { 843816Smaybee /* 844816Smaybee * Return the mounted root rather than the covered mount point. 8455326Sek110237 * Takes the GFS vnode at .zfs/snapshot/<snapname> and returns 8465326Sek110237 * the ZFS vnode mounted on top of the GFS node. This ZFS 8475326Sek110237 * vnode is the root the newly created vfsp. 848816Smaybee */ 849816Smaybee VFS_RELE(vfsp); 850816Smaybee err = traverse(vpp); 851816Smaybee } 852789Sahrens 853816Smaybee if (err == 0) { 854816Smaybee /* 8555326Sek110237 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>. 8564736Sek110237 * 8574736Sek110237 * This is where we lie about our v_vfsp in order to 8585326Sek110237 * make .zfs/snapshot/<snapname> accessible over NFS 8595326Sek110237 * without requiring manual mounts of <snapname>. 860816Smaybee */ 861816Smaybee ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs); 862816Smaybee VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs; 863816Smaybee (*vpp)->v_vfsp = zfsvfs->z_vfs; 864816Smaybee (*vpp)->v_flag &= ~VROOT; 865816Smaybee } 866789Sahrens mutex_exit(&sdp->sd_lock); 867789Sahrens ZFS_EXIT(zfsvfs); 868789Sahrens 8691566Smaybee /* 8701566Smaybee * If we had an error, drop our hold on the vnode and 8711566Smaybee * zfsctl_snapshot_inactive() will clean up. 8721566Smaybee */ 8731566Smaybee if (err) { 874816Smaybee VN_RELE(*vpp); 8751566Smaybee *vpp = NULL; 8761566Smaybee } 877816Smaybee return (err); 878789Sahrens } 879789Sahrens 880789Sahrens /* ARGSUSED */ 881789Sahrens static int 8825663Sck153898 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp, 8835663Sck153898 offset_t *offp, offset_t *nextp, void *data, int flags) 884789Sahrens { 885789Sahrens zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 886789Sahrens char snapname[MAXNAMELEN]; 887789Sahrens uint64_t id, cookie; 8885663Sck153898 boolean_t case_conflict; 8895663Sck153898 int error; 890789Sahrens 891789Sahrens ZFS_ENTER(zfsvfs); 892789Sahrens 893789Sahrens cookie = *offp; 8945663Sck153898 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id, 8955663Sck153898 &cookie, &case_conflict); 8965663Sck153898 if (error) { 897789Sahrens ZFS_EXIT(zfsvfs); 8985663Sck153898 if (error == ENOENT) { 8995663Sck153898 *eofp = 1; 9005663Sck153898 return (0); 9015663Sck153898 } 9025663Sck153898 return (error); 903789Sahrens } 904789Sahrens 9055663Sck153898 if (flags & V_RDDIR_ENTFLAGS) { 9065663Sck153898 edirent_t *eodp = dp; 9075663Sck153898 9085663Sck153898 (void) strcpy(eodp->ed_name, snapname); 9095663Sck153898 eodp->ed_ino = ZFSCTL_INO_SNAP(id); 9105663Sck153898 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0; 9115663Sck153898 } else { 9125663Sck153898 struct dirent64 *odp = dp; 9135663Sck153898 9145663Sck153898 (void) strcpy(odp->d_name, snapname); 9155663Sck153898 odp->d_ino = ZFSCTL_INO_SNAP(id); 9165663Sck153898 } 917789Sahrens *nextp = cookie; 918789Sahrens 919789Sahrens ZFS_EXIT(zfsvfs); 920789Sahrens 921789Sahrens return (0); 922789Sahrens } 923789Sahrens 9245326Sek110237 /* 9255326Sek110237 * pvp is the '.zfs' directory (zfsctl_node_t). 9265326Sek110237 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t). 9275326Sek110237 * 9285326Sek110237 * This function is the callback to create a GFS vnode for '.zfs/snapshot' 9295326Sek110237 * when a lookup is performed on .zfs for "snapshot". 9305326Sek110237 */ 931789Sahrens vnode_t * 932789Sahrens zfsctl_mknode_snapdir(vnode_t *pvp) 933789Sahrens { 934789Sahrens vnode_t *vp; 935789Sahrens zfsctl_snapdir_t *sdp; 936789Sahrens 937789Sahrens vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, 938789Sahrens zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN, 939789Sahrens zfsctl_snapdir_readdir_cb, NULL); 940789Sahrens sdp = vp->v_data; 941789Sahrens sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR; 9421571Sek110237 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime; 943789Sahrens mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL); 944789Sahrens avl_create(&sdp->sd_snaps, snapentry_compare, 945789Sahrens sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node)); 946789Sahrens return (vp); 947789Sahrens } 948789Sahrens 949789Sahrens /* ARGSUSED */ 950789Sahrens static int 9515331Samw zfsctl_snapdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 9525331Samw caller_context_t *ct) 953789Sahrens { 954789Sahrens zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 955789Sahrens zfsctl_snapdir_t *sdp = vp->v_data; 956789Sahrens 957789Sahrens ZFS_ENTER(zfsvfs); 958789Sahrens zfsctl_common_getattr(vp, vap); 959789Sahrens vap->va_nodeid = gfs_file_inode(vp); 960789Sahrens vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2; 961789Sahrens ZFS_EXIT(zfsvfs); 962789Sahrens 963789Sahrens return (0); 964789Sahrens } 965789Sahrens 9661566Smaybee /* ARGSUSED */ 967789Sahrens static void 9685331Samw zfsctl_snapdir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 969789Sahrens { 970789Sahrens zfsctl_snapdir_t *sdp = vp->v_data; 9711566Smaybee void *private; 972789Sahrens 9731566Smaybee private = gfs_dir_inactive(vp); 9741566Smaybee if (private != NULL) { 9751566Smaybee ASSERT(avl_numnodes(&sdp->sd_snaps) == 0); 9761566Smaybee mutex_destroy(&sdp->sd_lock); 9771566Smaybee avl_destroy(&sdp->sd_snaps); 9781566Smaybee kmem_free(private, sizeof (zfsctl_snapdir_t)); 9791566Smaybee } 980789Sahrens } 981789Sahrens 982789Sahrens static const fs_operation_def_t zfsctl_tops_snapdir[] = { 9833898Srsb { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 9843898Srsb { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 9853898Srsb { VOPNAME_IOCTL, { .error = fs_inval } }, 9863898Srsb { VOPNAME_GETATTR, { .vop_getattr = zfsctl_snapdir_getattr } }, 9873898Srsb { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 9883898Srsb { VOPNAME_RENAME, { .vop_rename = zfsctl_snapdir_rename } }, 9893898Srsb { VOPNAME_RMDIR, { .vop_rmdir = zfsctl_snapdir_remove } }, 9904543Smarks { VOPNAME_MKDIR, { .vop_mkdir = zfsctl_snapdir_mkdir } }, 9913898Srsb { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } }, 9923898Srsb { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_snapdir_lookup } }, 9933898Srsb { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 9943898Srsb { VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapdir_inactive } }, 9953898Srsb { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } }, 996789Sahrens { NULL } 997789Sahrens }; 998789Sahrens 9995326Sek110237 /* 10005326Sek110237 * pvp is the GFS vnode '.zfs/snapshot'. 10015326Sek110237 * 10025326Sek110237 * This creates a GFS node under '.zfs/snapshot' representing each 10035326Sek110237 * snapshot. This newly created GFS node is what we mount snapshot 10045326Sek110237 * vfs_t's ontop of. 10055326Sek110237 */ 1006789Sahrens static vnode_t * 1007789Sahrens zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset) 1008789Sahrens { 1009789Sahrens vnode_t *vp; 1010789Sahrens zfsctl_node_t *zcp; 1011789Sahrens 1012789Sahrens vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, 1013789Sahrens zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL); 1014789Sahrens zcp = vp->v_data; 1015789Sahrens zcp->zc_id = objset; 10166068Sck153898 VFS_HOLD(vp->v_vfsp); 1017789Sahrens 1018789Sahrens return (vp); 1019789Sahrens } 1020789Sahrens 1021789Sahrens static void 10225331Samw zfsctl_snapshot_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 1023789Sahrens { 1024789Sahrens zfsctl_snapdir_t *sdp; 1025789Sahrens zfs_snapentry_t *sep, *next; 1026789Sahrens vnode_t *dvp; 1027789Sahrens 10286492Stimh VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0); 1029789Sahrens sdp = dvp->v_data; 1030789Sahrens 1031789Sahrens mutex_enter(&sdp->sd_lock); 1032789Sahrens 1033789Sahrens if (vp->v_count > 1) { 1034789Sahrens mutex_exit(&sdp->sd_lock); 1035789Sahrens return; 1036789Sahrens } 1037789Sahrens ASSERT(!vn_ismntpt(vp)); 1038789Sahrens 1039789Sahrens sep = avl_first(&sdp->sd_snaps); 1040789Sahrens while (sep != NULL) { 1041789Sahrens next = AVL_NEXT(&sdp->sd_snaps, sep); 1042789Sahrens 1043789Sahrens if (sep->se_root == vp) { 1044789Sahrens avl_remove(&sdp->sd_snaps, sep); 1045789Sahrens kmem_free(sep->se_name, strlen(sep->se_name) + 1); 1046789Sahrens kmem_free(sep, sizeof (zfs_snapentry_t)); 1047789Sahrens break; 1048789Sahrens } 1049789Sahrens sep = next; 1050789Sahrens } 1051789Sahrens ASSERT(sep != NULL); 1052789Sahrens 1053789Sahrens mutex_exit(&sdp->sd_lock); 1054789Sahrens VN_RELE(dvp); 10556068Sck153898 VFS_RELE(vp->v_vfsp); 1056789Sahrens 10571566Smaybee /* 10581566Smaybee * Dispose of the vnode for the snapshot mount point. 10591566Smaybee * This is safe to do because once this entry has been removed 10601566Smaybee * from the AVL tree, it can't be found again, so cannot become 10611566Smaybee * "active". If we lookup the same name again we will end up 10621566Smaybee * creating a new vnode. 10631566Smaybee */ 10645331Samw gfs_vop_inactive(vp, cr, ct); 1065789Sahrens } 1066789Sahrens 1067789Sahrens 1068789Sahrens /* 1069789Sahrens * These VP's should never see the light of day. They should always 1070789Sahrens * be covered. 1071789Sahrens */ 1072789Sahrens static const fs_operation_def_t zfsctl_tops_snapshot[] = { 10733898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapshot_inactive }, 1074789Sahrens NULL, NULL 1075789Sahrens }; 1076789Sahrens 1077789Sahrens int 1078789Sahrens zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp) 1079789Sahrens { 1080789Sahrens zfsvfs_t *zfsvfs = vfsp->vfs_data; 1081789Sahrens vnode_t *dvp, *vp; 1082789Sahrens zfsctl_snapdir_t *sdp; 1083789Sahrens zfsctl_node_t *zcp; 1084789Sahrens zfs_snapentry_t *sep; 1085789Sahrens int error; 1086789Sahrens 1087789Sahrens ASSERT(zfsvfs->z_ctldir != NULL); 1088789Sahrens error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 10895331Samw NULL, 0, NULL, kcred, NULL, NULL, NULL); 1090789Sahrens if (error != 0) 1091789Sahrens return (error); 1092789Sahrens sdp = dvp->v_data; 1093789Sahrens 1094789Sahrens mutex_enter(&sdp->sd_lock); 1095789Sahrens sep = avl_first(&sdp->sd_snaps); 1096789Sahrens while (sep != NULL) { 1097789Sahrens vp = sep->se_root; 1098789Sahrens zcp = vp->v_data; 1099789Sahrens if (zcp->zc_id == objsetid) 1100789Sahrens break; 1101789Sahrens 1102789Sahrens sep = AVL_NEXT(&sdp->sd_snaps, sep); 1103789Sahrens } 1104789Sahrens 1105789Sahrens if (sep != NULL) { 1106789Sahrens VN_HOLD(vp); 11075326Sek110237 /* 11085326Sek110237 * Return the mounted root rather than the covered mount point. 11095326Sek110237 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid> 11105326Sek110237 * and returns the ZFS vnode mounted on top of the GFS node. 11115326Sek110237 * This ZFS vnode is the root of the vfs for objset 'objsetid'. 11125326Sek110237 */ 1113789Sahrens error = traverse(&vp); 11141589Smaybee if (error == 0) { 11151589Smaybee if (vp == sep->se_root) 11161589Smaybee error = EINVAL; 11171589Smaybee else 11181589Smaybee *zfsvfsp = VTOZ(vp)->z_zfsvfs; 11191589Smaybee } 11201572Snd150628 mutex_exit(&sdp->sd_lock); 1121789Sahrens VN_RELE(vp); 1122789Sahrens } else { 1123789Sahrens error = EINVAL; 11241572Snd150628 mutex_exit(&sdp->sd_lock); 1125789Sahrens } 1126789Sahrens 1127789Sahrens VN_RELE(dvp); 1128789Sahrens 1129789Sahrens return (error); 1130789Sahrens } 1131789Sahrens 1132789Sahrens /* 1133789Sahrens * Unmount any snapshots for the given filesystem. This is called from 1134789Sahrens * zfs_umount() - if we have a ctldir, then go through and unmount all the 1135789Sahrens * snapshots. 1136789Sahrens */ 1137789Sahrens int 1138789Sahrens zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr) 1139789Sahrens { 1140789Sahrens zfsvfs_t *zfsvfs = vfsp->vfs_data; 11416068Sck153898 vnode_t *dvp; 1142789Sahrens zfsctl_snapdir_t *sdp; 1143789Sahrens zfs_snapentry_t *sep, *next; 1144789Sahrens int error; 1145789Sahrens 1146789Sahrens ASSERT(zfsvfs->z_ctldir != NULL); 1147789Sahrens error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 11485331Samw NULL, 0, NULL, cr, NULL, NULL, NULL); 1149789Sahrens if (error != 0) 1150789Sahrens return (error); 1151789Sahrens sdp = dvp->v_data; 1152789Sahrens 1153789Sahrens mutex_enter(&sdp->sd_lock); 1154789Sahrens 1155789Sahrens sep = avl_first(&sdp->sd_snaps); 1156789Sahrens while (sep != NULL) { 1157789Sahrens next = AVL_NEXT(&sdp->sd_snaps, sep); 1158789Sahrens 1159789Sahrens /* 1160789Sahrens * If this snapshot is not mounted, then it must 1161789Sahrens * have just been unmounted by somebody else, and 1162789Sahrens * will be cleaned up by zfsctl_snapdir_inactive(). 1163789Sahrens */ 11646068Sck153898 if (vn_ismntpt(sep->se_root)) { 11656068Sck153898 avl_remove(&sdp->sd_snaps, sep); 11666068Sck153898 error = zfsctl_unmount_snap(sep, fflags, cr); 1167789Sahrens if (error) { 11686068Sck153898 avl_add(&sdp->sd_snaps, sep); 11696068Sck153898 break; 1170789Sahrens } 1171789Sahrens } 1172789Sahrens sep = next; 1173789Sahrens } 11746068Sck153898 1175789Sahrens mutex_exit(&sdp->sd_lock); 1176789Sahrens VN_RELE(dvp); 1177789Sahrens 1178789Sahrens return (error); 1179789Sahrens } 1180