1/* 2 * CDDL HEADER START 3 * 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. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21/* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2013 by Delphix. All rights reserved. 24 */ 25 26/* 27 * ZFS control directory (a.k.a. ".zfs") 28 * 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. 33 * 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 36 * types of objects: 37 * 38 * ctldir ------> snapshotdir -------> snapshot 39 * | 40 * | 41 * V 42 * mounted fs 43 * 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. 48 * 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. 54 * 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). 58 * 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. 65 */ 66 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> 72#include <sys/gfs.h> 73#include <sys/stat.h> 74#include <sys/dmu.h> 75#include <sys/dsl_destroy.h> 76#include <sys/dsl_deleg.h> 77#include <sys/mount.h> 78#include <sys/sunddi.h> 79 80#include "zfs_namecheck.h" 81 82typedef struct zfsctl_node { 83 gfs_dir_t zc_gfs_private; 84 uint64_t zc_id; 85 timestruc_t zc_cmtime; /* ctime and mtime, always the same */ 86} zfsctl_node_t; 87 88typedef struct zfsctl_snapdir { 89 zfsctl_node_t sd_node; 90 kmutex_t sd_lock; 91 avl_tree_t sd_snaps; 92} zfsctl_snapdir_t; 93 94typedef struct { 95 char *se_name; 96 vnode_t *se_root; 97 avl_node_t se_node; 98} zfs_snapentry_t; 99 100static int 101snapentry_compare(const void *a, const void *b) 102{ 103 const zfs_snapentry_t *sa = a; 104 const zfs_snapentry_t *sb = b; 105 int ret = strcmp(sa->se_name, sb->se_name); 106 107 if (ret < 0) 108 return (-1); 109 else if (ret > 0) 110 return (1); 111 else 112 return (0); 113} 114 115#ifdef sun 116vnodeops_t *zfsctl_ops_root; 117vnodeops_t *zfsctl_ops_snapdir; 118vnodeops_t *zfsctl_ops_snapshot; 119vnodeops_t *zfsctl_ops_shares; 120vnodeops_t *zfsctl_ops_shares_dir; 121 122static const fs_operation_def_t zfsctl_tops_root[]; 123static const fs_operation_def_t zfsctl_tops_snapdir[]; 124static const fs_operation_def_t zfsctl_tops_snapshot[]; 125static const fs_operation_def_t zfsctl_tops_shares[]; 126#else /* !sun */ 127static struct vop_vector zfsctl_ops_root; 128static struct vop_vector zfsctl_ops_snapdir; 129static struct vop_vector zfsctl_ops_snapshot; 130static struct vop_vector zfsctl_ops_shares; 131static struct vop_vector zfsctl_ops_shares_dir; 132#endif /* !sun */ 133 134static vnode_t *zfsctl_mknode_snapdir(vnode_t *); 135static vnode_t *zfsctl_mknode_shares(vnode_t *); 136static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset); 137static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *); 138 139#ifdef sun 140static 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 }, 146 { NULL } 147}; 148#endif /* sun */ 149 150/* 151 * Root directory elements. We only have two entries 152 * snapshot and shares. 153 */ 154static gfs_dirent_t zfsctl_root_entries[] = { 155 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE }, 156 { "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE }, 157 { NULL } 158}; 159 160/* include . and .. in the calculation */ 161#define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \ 162 sizeof (gfs_dirent_t)) + 1) 163 164 165/* 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. 169 */ 170void 171zfsctl_init(void) 172{ 173#ifdef sun 174 VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0); 175#endif 176} 177 178void 179zfsctl_fini(void) 180{ 181#ifdef sun 182 /* 183 * Remove vfsctl vnode ops 184 */ 185 if (zfsctl_ops_root) 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); 195 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; 201#endif /* sun */ 202} 203 204boolean_t 205zfsctl_is_node(vnode_t *vp) 206{ 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)); 212 213} 214 215/* 216 * Return the inode number associated with the 'snapshot' or 217 * 'shares' directory. 218 */ 219/* ARGSUSED */ 220static ino64_t 221zfsctl_root_inode_cb(vnode_t *vp, int index) 222{ 223 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 224 225 ASSERT(index <= 2); 226 227 if (index == 0) 228 return (ZFSCTL_INO_SNAPDIR); 229 230 return (zfsvfs->z_shares_dir); 231} 232 233/* 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. 238 */ 239void 240zfsctl_create(zfsvfs_t *zfsvfs) 241{ 242 vnode_t *vp, *rvp; 243 zfsctl_node_t *zcp; 244 uint64_t crtime[2]; 245 246 ASSERT(zfsvfs->z_ctldir == NULL); 247 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); 251 zcp = vp->v_data; 252 zcp->zc_id = ZFSCTL_INO_ROOT; 253 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); 258 VN_URELE(rvp); 259 260 /* 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 263 * for us. 264 */ 265 vp->v_vflag &= ~VV_ROOT; 266 267 zfsvfs->z_ctldir = vp; 268 269 VOP_UNLOCK(vp, 0); 270} 271 272/* 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 276 */ 277void 278zfsctl_destroy(zfsvfs_t *zfsvfs) 279{ 280 VN_RELE(zfsvfs->z_ctldir); 281 zfsvfs->z_ctldir = NULL; 282} 283 284/* 285 * Given a root znode, retrieve the associated .zfs directory. 286 * Add a hold to the vnode and return it. 287 */ 288vnode_t * 289zfsctl_root(znode_t *zp) 290{ 291 ASSERT(zfs_has_ctldir(zp)); 292 VN_HOLD(zp->z_zfsvfs->z_ctldir); 293 return (zp->z_zfsvfs->z_ctldir); 294} 295 296/* 297 * Common open routine. Disallow any write access. 298 */ 299/* ARGSUSED */ 300static int 301zfsctl_common_open(struct vop_open_args *ap) 302{ 303 int flags = ap->a_mode; 304 305 if (flags & FWRITE) 306 return (SET_ERROR(EACCES)); 307 308 return (0); 309} 310 311/* 312 * Common close routine. Nothing to do here. 313 */ 314/* ARGSUSED */ 315static int 316zfsctl_common_close(struct vop_close_args *ap) 317{ 318 return (0); 319} 320 321/* 322 * Common access routine. Disallow writes. 323 */ 324/* ARGSUSED */ 325static int 326zfsctl_common_access(ap) 327 struct vop_access_args /* { 328 struct vnode *a_vp; 329 accmode_t a_accmode; 330 struct ucred *a_cred; 331 struct thread *a_td; 332 } */ *ap; 333{ 334 accmode_t accmode = ap->a_accmode; 335 336#ifdef TODO 337 if (flags & V_ACE_MASK) { 338 if (accmode & ACE_ALL_WRITE_PERMS) 339 return (SET_ERROR(EACCES)); 340 } else { 341#endif 342 if (accmode & VWRITE) 343 return (SET_ERROR(EACCES)); 344#ifdef TODO 345 } 346#endif 347 348 return (0); 349} 350 351/* 352 * Common getattr function. Fill in basic information. 353 */ 354static void 355zfsctl_common_getattr(vnode_t *vp, vattr_t *vap) 356{ 357 timestruc_t now; 358 359 vap->va_uid = 0; 360 vap->va_gid = 0; 361 vap->va_rdev = 0; 362 /* 363 * We are a purely virtual object, so we have no 364 * blocksize or allocated blocks. 365 */ 366 vap->va_blksize = 0; 367 vap->va_nblocks = 0; 368 vap->va_seq = 0; 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 | 371 S_IROTH | S_IXOTH; 372 vap->va_type = VDIR; 373 /* 374 * We live in the now (for atime). 375 */ 376 gethrestime(&now); 377 vap->va_atime = now; 378 /* FreeBSD: Reset chflags(2) flags. */ 379 vap->va_flags = 0; 380} 381 382/*ARGSUSED*/ 383static int 384zfsctl_common_fid(ap) 385 struct vop_fid_args /* { 386 struct vnode *a_vp; 387 struct fid *a_fid; 388 } */ *ap; 389{ 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; 395 zfid_short_t *zfid; 396 int i; 397 398 ZFS_ENTER(zfsvfs); 399 400#ifdef illumos 401 if (fidp->fid_len < SHORT_FID_LEN) { 402 fidp->fid_len = SHORT_FID_LEN; 403 ZFS_EXIT(zfsvfs); 404 return (SET_ERROR(ENOSPC)); 405 } 406#else 407 fidp->fid_len = SHORT_FID_LEN; 408#endif 409 410 zfid = (zfid_short_t *)fidp; 411 412 zfid->zf_len = SHORT_FID_LEN; 413 414 for (i = 0; i < sizeof (zfid->zf_object); i++) 415 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 416 417 /* .zfs znodes always have a generation number of 0 */ 418 for (i = 0; i < sizeof (zfid->zf_gen); i++) 419 zfid->zf_gen[i] = 0; 420 421 ZFS_EXIT(zfsvfs); 422 return (0); 423} 424 425 426/*ARGSUSED*/ 427static int 428zfsctl_shares_fid(ap) 429 struct vop_fid_args /* { 430 struct vnode *a_vp; 431 struct fid *a_fid; 432 } */ *ap; 433{ 434 vnode_t *vp = ap->a_vp; 435 fid_t *fidp = (void *)ap->a_fid; 436 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 437 znode_t *dzp; 438 int error; 439 440 ZFS_ENTER(zfsvfs); 441 442 if (zfsvfs->z_shares_dir == 0) { 443 ZFS_EXIT(zfsvfs); 444 return (SET_ERROR(ENOTSUP)); 445 } 446 447 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 448 error = VOP_FID(ZTOV(dzp), fidp); 449 VN_RELE(ZTOV(dzp)); 450 } 451 452 ZFS_EXIT(zfsvfs); 453 return (error); 454} 455 456static int 457zfsctl_common_reclaim(ap) 458 struct vop_reclaim_args /* { 459 struct vnode *a_vp; 460 struct thread *a_td; 461 } */ *ap; 462{ 463 vnode_t *vp = ap->a_vp; 464 465 /* 466 * Destroy the vm object and flush associated pages. 467 */ 468 vnode_destroy_vobject(vp); 469 VI_LOCK(vp); 470 vp->v_data = NULL; 471 VI_UNLOCK(vp); 472 return (0); 473} 474 475/* 476 * .zfs inode namespace 477 * 478 * We need to generate unique inode numbers for all files and directories 479 * within the .zfs pseudo-filesystem. We use the following scheme: 480 * 481 * ENTRY ZFSCTL_INODE 482 * .zfs 1 483 * .zfs/snapshot 2 484 * .zfs/snapshot/<snap> objectid(snap) 485 */ 486 487#define ZFSCTL_INO_SNAP(id) (id) 488 489/* 490 * Get root directory attributes. 491 */ 492/* ARGSUSED */ 493static int 494zfsctl_root_getattr(ap) 495 struct vop_getattr_args /* { 496 struct vnode *a_vp; 497 struct vattr *a_vap; 498 struct ucred *a_cred; 499 } */ *ap; 500{ 501 struct vnode *vp = ap->a_vp; 502 struct vattr *vap = ap->a_vap; 503 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 504 zfsctl_node_t *zcp = vp->v_data; 505 506 ZFS_ENTER(zfsvfs); 507 vap->va_nodeid = ZFSCTL_INO_ROOT; 508 vap->va_nlink = vap->va_size = NROOT_ENTRIES; 509 vap->va_mtime = vap->va_ctime = zcp->zc_cmtime; 510 vap->va_birthtime = vap->va_ctime; 511 512 zfsctl_common_getattr(vp, vap); 513 ZFS_EXIT(zfsvfs); 514 515 return (0); 516} 517 518/* 519 * Special case the handling of "..". 520 */ 521/* ARGSUSED */ 522int 523zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp, 524 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 525 int *direntflags, pathname_t *realpnp) 526{ 527 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 528 int err; 529 530 /* 531 * No extended attributes allowed under .zfs 532 */ 533 if (flags & LOOKUP_XATTR) 534 return (SET_ERROR(EINVAL)); 535 536 ZFS_ENTER(zfsvfs); 537 538 if (strcmp(nm, "..") == 0) { 539 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp); 540 if (err == 0) 541 VOP_UNLOCK(*vpp, 0); 542 } else { 543 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir, 544 cr, ct, direntflags, realpnp); 545 } 546 547 ZFS_EXIT(zfsvfs); 548 549 return (err); 550} 551 552#ifdef sun 553static int 554zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 555 caller_context_t *ct) 556{ 557 /* 558 * We only care about ACL_ENABLED so that libsec can 559 * display ACL correctly and not default to POSIX draft. 560 */ 561 if (cmd == _PC_ACL_ENABLED) { 562 *valp = _ACL_ACE_ENABLED; 563 return (0); 564 } 565 566 return (fs_pathconf(vp, cmd, valp, cr, ct)); 567} 568#endif /* sun */ 569 570#ifdef sun 571static const fs_operation_def_t zfsctl_tops_root[] = { 572 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 573 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 574 { VOPNAME_IOCTL, { .error = fs_inval } }, 575 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } }, 576 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 577 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } }, 578 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } }, 579 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 580 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 581 { VOPNAME_PATHCONF, { .vop_pathconf = zfsctl_pathconf } }, 582 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } }, 583 { NULL } 584}; 585#endif /* sun */ 586 587/* 588 * Special case the handling of "..". 589 */ 590/* ARGSUSED */ 591int 592zfsctl_freebsd_root_lookup(ap) 593 struct vop_lookup_args /* { 594 struct vnode *a_dvp; 595 struct vnode **a_vpp; 596 struct componentname *a_cnp; 597 } */ *ap; 598{ 599 vnode_t *dvp = ap->a_dvp; 600 vnode_t **vpp = ap->a_vpp; 601 cred_t *cr = ap->a_cnp->cn_cred; 602 int flags = ap->a_cnp->cn_flags; 603 int nameiop = ap->a_cnp->cn_nameiop; 604 char nm[NAME_MAX + 1]; 605 int err; 606 int ltype; 607 608 if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE)) 609 return (EOPNOTSUPP); 610 611 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm)); 612 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1); 613 err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL); 614 if (err == 0 && (nm[0] != '.' || nm[1] != '\0')) { 615 ltype = VOP_ISLOCKED(dvp); 616 if (flags & ISDOTDOT) { 617 VN_HOLD(*vpp); 618 VOP_UNLOCK(dvp, 0); 619 } 620 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 621 if (flags & ISDOTDOT) { 622 VN_RELE(*vpp); 623 vn_lock(dvp, ltype| LK_RETRY); 624 } 625 } 626 627 return (err); 628} 629 630static struct vop_vector zfsctl_ops_root = { 631 .vop_default = &default_vnodeops, 632 .vop_open = zfsctl_common_open, 633 .vop_close = zfsctl_common_close, 634 .vop_ioctl = VOP_EINVAL, 635 .vop_getattr = zfsctl_root_getattr, 636 .vop_access = zfsctl_common_access, 637 .vop_readdir = gfs_vop_readdir, 638 .vop_lookup = zfsctl_freebsd_root_lookup, 639 .vop_inactive = VOP_NULL, 640 .vop_reclaim = gfs_vop_reclaim, 641#ifdef TODO 642 .vop_pathconf = zfsctl_pathconf, 643#endif 644 .vop_fid = zfsctl_common_fid, 645}; 646 647/* 648 * Gets the full dataset name that corresponds to the given snapshot name 649 * Example: 650 * zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1" 651 */ 652static int 653zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname) 654{ 655 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 656 657 if (zfs_component_namecheck(name, NULL, NULL) != 0) 658 return (SET_ERROR(EILSEQ)); 659 dmu_objset_name(os, zname); 660 if (strlen(zname) + 1 + strlen(name) >= len) 661 return (SET_ERROR(ENAMETOOLONG)); 662 (void) strcat(zname, "@"); 663 (void) strcat(zname, name); 664 return (0); 665} 666 667static int 668zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr) 669{ 670 vnode_t *svp = sep->se_root; 671 int error; 672 673 ASSERT(vn_ismntpt(svp)); 674 675 /* this will be dropped by dounmount() */ 676 if ((error = vn_vfswlock(svp)) != 0) 677 return (error); 678 679#ifdef sun 680 VN_HOLD(svp); 681 error = dounmount(vn_mountedvfs(svp), fflags, cr); 682 if (error) { 683 VN_RELE(svp); 684 return (error); 685 } 686 687 /* 688 * We can't use VN_RELE(), as that will try to invoke 689 * zfsctl_snapdir_inactive(), which would cause us to destroy 690 * the sd_lock mutex held by our caller. 691 */ 692 ASSERT(svp->v_count == 1); 693 gfs_vop_reclaim(svp, cr, NULL); 694 695 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 696 kmem_free(sep, sizeof (zfs_snapentry_t)); 697 698 return (0); 699#else /* !sun */ 700 vfs_ref(vn_mountedvfs(svp)); 701 return (dounmount(vn_mountedvfs(svp), fflags, curthread)); 702#endif /* !sun */ 703} 704 705#ifdef sun 706static void 707zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm) 708{ 709 avl_index_t where; 710 vfs_t *vfsp; 711 refstr_t *pathref; 712 char newpath[MAXNAMELEN]; 713 char *tail; 714 715 ASSERT(MUTEX_HELD(&sdp->sd_lock)); 716 ASSERT(sep != NULL); 717 718 vfsp = vn_mountedvfs(sep->se_root); 719 ASSERT(vfsp != NULL); 720 721 vfs_lock_wait(vfsp); 722 723 /* 724 * Change the name in the AVL tree. 725 */ 726 avl_remove(&sdp->sd_snaps, sep); 727 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 728 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP); 729 (void) strcpy(sep->se_name, nm); 730 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL); 731 avl_insert(&sdp->sd_snaps, sep, where); 732 733 /* 734 * Change the current mountpoint info: 735 * - update the tail of the mntpoint path 736 * - update the tail of the resource path 737 */ 738 pathref = vfs_getmntpoint(vfsp); 739 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath)); 740 VERIFY((tail = strrchr(newpath, '/')) != NULL); 741 *(tail+1) = '\0'; 742 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath)); 743 (void) strcat(newpath, nm); 744 refstr_rele(pathref); 745 vfs_setmntpoint(vfsp, newpath, 0); 746 747 pathref = vfs_getresource(vfsp); 748 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath)); 749 VERIFY((tail = strrchr(newpath, '@')) != NULL); 750 *(tail+1) = '\0'; 751 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath)); 752 (void) strcat(newpath, nm); 753 refstr_rele(pathref); 754 vfs_setresource(vfsp, newpath, 0); 755 756 vfs_unlock(vfsp); 757} 758#endif /* sun */ 759 760#ifdef sun 761/*ARGSUSED*/ 762static int 763zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, 764 cred_t *cr, caller_context_t *ct, int flags) 765{ 766 zfsctl_snapdir_t *sdp = sdvp->v_data; 767 zfs_snapentry_t search, *sep; 768 zfsvfs_t *zfsvfs; 769 avl_index_t where; 770 char from[MAXNAMELEN], to[MAXNAMELEN]; 771 char real[MAXNAMELEN], fsname[MAXNAMELEN]; 772 int err; 773 774 zfsvfs = sdvp->v_vfsp->vfs_data; 775 ZFS_ENTER(zfsvfs); 776 777 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 778 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real, 779 MAXNAMELEN, NULL); 780 if (err == 0) { 781 snm = real; 782 } else if (err != ENOTSUP) { 783 ZFS_EXIT(zfsvfs); 784 return (err); 785 } 786 } 787 788 ZFS_EXIT(zfsvfs); 789 790 dmu_objset_name(zfsvfs->z_os, fsname); 791 792 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from); 793 if (err == 0) 794 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to); 795 if (err == 0) 796 err = zfs_secpolicy_rename_perms(from, to, cr); 797 if (err != 0) 798 return (err); 799 800 /* 801 * Cannot move snapshots out of the snapdir. 802 */ 803 if (sdvp != tdvp) 804 return (SET_ERROR(EINVAL)); 805 806 if (strcmp(snm, tnm) == 0) 807 return (0); 808 809 mutex_enter(&sdp->sd_lock); 810 811 search.se_name = (char *)snm; 812 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) { 813 mutex_exit(&sdp->sd_lock); 814 return (SET_ERROR(ENOENT)); 815 } 816 817 err = dsl_dataset_rename_snapshot(fsname, snm, tnm, 0); 818 if (err == 0) 819 zfsctl_rename_snap(sdp, sep, tnm); 820 821 mutex_exit(&sdp->sd_lock); 822 823 return (err); 824} 825#endif /* sun */ 826 827#ifdef sun 828/* ARGSUSED */ 829static int 830zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 831 caller_context_t *ct, int flags) 832{ 833 zfsctl_snapdir_t *sdp = dvp->v_data; 834 zfs_snapentry_t *sep; 835 zfs_snapentry_t search; 836 zfsvfs_t *zfsvfs; 837 char snapname[MAXNAMELEN]; 838 char real[MAXNAMELEN]; 839 int err; 840 841 zfsvfs = dvp->v_vfsp->vfs_data; 842 ZFS_ENTER(zfsvfs); 843 844 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 845 846 err = dmu_snapshot_realname(zfsvfs->z_os, name, real, 847 MAXNAMELEN, NULL); 848 if (err == 0) { 849 name = real; 850 } else if (err != ENOTSUP) { 851 ZFS_EXIT(zfsvfs); 852 return (err); 853 } 854 } 855 856 ZFS_EXIT(zfsvfs); 857 858 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname); 859 if (err == 0) 860 err = zfs_secpolicy_destroy_perms(snapname, cr); 861 if (err != 0) 862 return (err); 863 864 mutex_enter(&sdp->sd_lock); 865 866 search.se_name = name; 867 sep = avl_find(&sdp->sd_snaps, &search, NULL); 868 if (sep) { 869 avl_remove(&sdp->sd_snaps, sep); 870 err = zfsctl_unmount_snap(sep, MS_FORCE, cr); 871 if (err != 0) 872 avl_add(&sdp->sd_snaps, sep); 873 else 874 err = dsl_destroy_snapshot(snapname, B_FALSE); 875 } else { 876 err = SET_ERROR(ENOENT); 877 } 878 879 mutex_exit(&sdp->sd_lock); 880 881 return (err); 882} 883#endif /* sun */ 884 885/* 886 * This creates a snapshot under '.zfs/snapshot'. 887 */ 888/* ARGSUSED */ 889static int 890zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, 891 cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp) 892{ 893 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 894 char name[MAXNAMELEN]; 895 int err; 896 static enum symfollow follow = NO_FOLLOW; 897 static enum uio_seg seg = UIO_SYSSPACE; 898 899 if (zfs_component_namecheck(dirname, NULL, NULL) != 0) 900 return (SET_ERROR(EILSEQ)); 901 902 dmu_objset_name(zfsvfs->z_os, name); 903 904 *vpp = NULL; 905 906 err = zfs_secpolicy_snapshot_perms(name, cr); 907 if (err != 0) 908 return (err); 909 910 if (err == 0) { 911 err = dmu_objset_snapshot_one(name, dirname); 912 if (err != 0) 913 return (err); 914 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp); 915 } 916 917 return (err); 918} 919 920static int 921zfsctl_freebsd_snapdir_mkdir(ap) 922 struct vop_mkdir_args /* { 923 struct vnode *a_dvp; 924 struct vnode **a_vpp; 925 struct componentname *a_cnp; 926 struct vattr *a_vap; 927 } */ *ap; 928{ 929 930 ASSERT(ap->a_cnp->cn_flags & SAVENAME); 931 932 return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL, 933 ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL)); 934} 935 936/* 937 * Lookup entry point for the 'snapshot' directory. Try to open the 938 * snapshot if it exist, creating the pseudo filesystem vnode as necessary. 939 * Perform a mount of the associated dataset on top of the vnode. 940 */ 941/* ARGSUSED */ 942int 943zfsctl_snapdir_lookup(ap) 944 struct vop_lookup_args /* { 945 struct vnode *a_dvp; 946 struct vnode **a_vpp; 947 struct componentname *a_cnp; 948 } */ *ap; 949{ 950 vnode_t *dvp = ap->a_dvp; 951 vnode_t **vpp = ap->a_vpp; 952 struct componentname *cnp = ap->a_cnp; 953 char nm[NAME_MAX + 1]; 954 zfsctl_snapdir_t *sdp = dvp->v_data; 955 objset_t *snap; 956 char snapname[MAXNAMELEN]; 957 char real[MAXNAMELEN]; 958 char *mountpoint; 959 zfs_snapentry_t *sep, search; 960 size_t mountpoint_len; 961 avl_index_t where; 962 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 963 int err; 964 int ltype, flags = 0; 965 966 /* 967 * No extended attributes allowed under .zfs 968 */ 969 if (flags & LOOKUP_XATTR) 970 return (SET_ERROR(EINVAL)); 971 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm)); 972 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1); 973 974 ASSERT(dvp->v_type == VDIR); 975 976 *vpp = NULL; 977 978 /* 979 * If we get a recursive call, that means we got called 980 * from the domount() code while it was trying to look up the 981 * spec (which looks like a local path for zfs). We need to 982 * add some flag to domount() to tell it not to do this lookup. 983 */ 984 if (MUTEX_HELD(&sdp->sd_lock)) 985 return (SET_ERROR(ENOENT)); 986 987 ZFS_ENTER(zfsvfs); 988 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) { 989 ZFS_EXIT(zfsvfs); 990 return (0); 991 } 992 993 if (flags & FIGNORECASE) { 994 boolean_t conflict = B_FALSE; 995 996 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real, 997 MAXNAMELEN, &conflict); 998 if (err == 0) { 999 strlcpy(nm, real, sizeof(nm)); 1000 } else if (err != ENOTSUP) { 1001 ZFS_EXIT(zfsvfs); 1002 return (err); 1003 } 1004#if 0 1005 if (realpnp) 1006 (void) strlcpy(realpnp->pn_buf, nm, 1007 realpnp->pn_bufsize); 1008 if (conflict && direntflags) 1009 *direntflags = ED_CASE_CONFLICT; 1010#endif 1011 } 1012 1013 mutex_enter(&sdp->sd_lock); 1014 search.se_name = (char *)nm; 1015 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) { 1016 *vpp = sep->se_root; 1017 VN_HOLD(*vpp); 1018 err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY); 1019 if (err != 0) { 1020 VN_RELE(*vpp); 1021 *vpp = NULL; 1022 } else if (*vpp == sep->se_root) { 1023 /* 1024 * The snapshot was unmounted behind our backs, 1025 * try to remount it. 1026 */ 1027 VERIFY(zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname) == 0); 1028 goto domount; 1029 } else { 1030 /* 1031 * VROOT was set during the traverse call. We need 1032 * to clear it since we're pretending to be part 1033 * of our parent's vfs. 1034 */ 1035 (*vpp)->v_flag &= ~VROOT; 1036 } 1037 mutex_exit(&sdp->sd_lock); 1038 ZFS_EXIT(zfsvfs); 1039 return (err); 1040 } 1041 1042 /* 1043 * The requested snapshot is not currently mounted, look it up. 1044 */ 1045 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname); 1046 if (err != 0) { 1047 mutex_exit(&sdp->sd_lock); 1048 ZFS_EXIT(zfsvfs); 1049 /* 1050 * handle "ls *" or "?" in a graceful manner, 1051 * forcing EILSEQ to ENOENT. 1052 * Since shell ultimately passes "*" or "?" as name to lookup 1053 */ 1054 return (err == EILSEQ ? ENOENT : err); 1055 } 1056 if (dmu_objset_hold(snapname, FTAG, &snap) != 0) { 1057 mutex_exit(&sdp->sd_lock); 1058#ifdef illumos 1059 ZFS_EXIT(zfsvfs); 1060 return (SET_ERROR(ENOENT)); 1061#else /* !illumos */ 1062 /* Translate errors and add SAVENAME when needed. */ 1063 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) { 1064 err = EJUSTRETURN; 1065 cnp->cn_flags |= SAVENAME; 1066 } else { 1067 err = SET_ERROR(ENOENT); 1068 } 1069 ZFS_EXIT(zfsvfs); 1070 return (err); 1071#endif /* !illumos */ 1072 } 1073 1074 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP); 1075 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP); 1076 (void) strcpy(sep->se_name, nm); 1077 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap)); 1078 VN_HOLD(*vpp); 1079 avl_insert(&sdp->sd_snaps, sep, where); 1080 1081 dmu_objset_rele(snap, FTAG); 1082domount: 1083 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) + 1084 strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(nm) + 1; 1085 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP); 1086 (void) snprintf(mountpoint, mountpoint_len, 1087 "%s/" ZFS_CTLDIR_NAME "/snapshot/%s", 1088 dvp->v_vfsp->mnt_stat.f_mntonname, nm); 1089 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0); 1090 kmem_free(mountpoint, mountpoint_len); 1091 if (err == 0) { 1092 /* 1093 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>. 1094 * 1095 * This is where we lie about our v_vfsp in order to 1096 * make .zfs/snapshot/<snapname> accessible over NFS 1097 * without requiring manual mounts of <snapname>. 1098 */ 1099 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs); 1100 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs; 1101 } 1102 mutex_exit(&sdp->sd_lock); 1103 ZFS_EXIT(zfsvfs); 1104 1105#ifdef illumos 1106 /* 1107 * If we had an error, drop our hold on the vnode and 1108 * zfsctl_snapshot_inactive() will clean up. 1109 */ 1110 if (err != 0) { 1111 VN_RELE(*vpp); 1112 *vpp = NULL; 1113 } 1114#else 1115 if (err != 0) 1116 *vpp = NULL; 1117#endif 1118 return (err); 1119} 1120 1121/* ARGSUSED */ 1122int 1123zfsctl_shares_lookup(ap) 1124 struct vop_lookup_args /* { 1125 struct vnode *a_dvp; 1126 struct vnode **a_vpp; 1127 struct componentname *a_cnp; 1128 } */ *ap; 1129{ 1130 vnode_t *dvp = ap->a_dvp; 1131 vnode_t **vpp = ap->a_vpp; 1132 struct componentname *cnp = ap->a_cnp; 1133 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 1134 char nm[NAME_MAX + 1]; 1135 znode_t *dzp; 1136 int error; 1137 1138 ZFS_ENTER(zfsvfs); 1139 1140 ASSERT(cnp->cn_namelen < sizeof(nm)); 1141 strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1); 1142 1143 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) { 1144 ZFS_EXIT(zfsvfs); 1145 return (0); 1146 } 1147 1148 if (zfsvfs->z_shares_dir == 0) { 1149 ZFS_EXIT(zfsvfs); 1150 return (SET_ERROR(ENOTSUP)); 1151 } 1152 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) 1153 error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp); 1154 1155 VN_RELE(ZTOV(dzp)); 1156 ZFS_EXIT(zfsvfs); 1157 1158 return (error); 1159} 1160 1161/* ARGSUSED */ 1162static int 1163zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp, 1164 offset_t *offp, offset_t *nextp, void *data, int flags) 1165{ 1166 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1167 char snapname[MAXNAMELEN]; 1168 uint64_t id, cookie; 1169 boolean_t case_conflict; 1170 int error; 1171 1172 ZFS_ENTER(zfsvfs); 1173 1174 cookie = *offp; 1175 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 1176 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id, 1177 &cookie, &case_conflict); 1178 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 1179 if (error) { 1180 ZFS_EXIT(zfsvfs); 1181 if (error == ENOENT) { 1182 *eofp = 1; 1183 return (0); 1184 } 1185 return (error); 1186 } 1187 1188 if (flags & V_RDDIR_ENTFLAGS) { 1189 edirent_t *eodp = dp; 1190 1191 (void) strcpy(eodp->ed_name, snapname); 1192 eodp->ed_ino = ZFSCTL_INO_SNAP(id); 1193 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0; 1194 } else { 1195 struct dirent64 *odp = dp; 1196 1197 (void) strcpy(odp->d_name, snapname); 1198 odp->d_ino = ZFSCTL_INO_SNAP(id); 1199 } 1200 *nextp = cookie; 1201 1202 ZFS_EXIT(zfsvfs); 1203 1204 return (0); 1205} 1206 1207/* ARGSUSED */ 1208static int 1209zfsctl_shares_readdir(ap) 1210 struct vop_readdir_args /* { 1211 struct vnode *a_vp; 1212 struct uio *a_uio; 1213 struct ucred *a_cred; 1214 int *a_eofflag; 1215 int *a_ncookies; 1216 u_long **a_cookies; 1217 } */ *ap; 1218{ 1219 vnode_t *vp = ap->a_vp; 1220 uio_t *uiop = ap->a_uio; 1221 cred_t *cr = ap->a_cred; 1222 int *eofp = ap->a_eofflag; 1223 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1224 znode_t *dzp; 1225 int error; 1226 1227 ZFS_ENTER(zfsvfs); 1228 1229 if (zfsvfs->z_shares_dir == 0) { 1230 ZFS_EXIT(zfsvfs); 1231 return (SET_ERROR(ENOTSUP)); 1232 } 1233 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 1234 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY); 1235 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies); 1236 VN_URELE(ZTOV(dzp)); 1237 } else { 1238 *eofp = 1; 1239 error = SET_ERROR(ENOENT); 1240 } 1241 1242 ZFS_EXIT(zfsvfs); 1243 return (error); 1244} 1245 1246/* 1247 * pvp is the '.zfs' directory (zfsctl_node_t). 1248 * 1249 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t). 1250 * 1251 * This function is the callback to create a GFS vnode for '.zfs/snapshot' 1252 * when a lookup is performed on .zfs for "snapshot". 1253 */ 1254vnode_t * 1255zfsctl_mknode_snapdir(vnode_t *pvp) 1256{ 1257 vnode_t *vp; 1258 zfsctl_snapdir_t *sdp; 1259 1260 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp, 1261 &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN, 1262 zfsctl_snapdir_readdir_cb, NULL); 1263 sdp = vp->v_data; 1264 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR; 1265 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime; 1266 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL); 1267 avl_create(&sdp->sd_snaps, snapentry_compare, 1268 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node)); 1269 VOP_UNLOCK(vp, 0); 1270 return (vp); 1271} 1272 1273vnode_t * 1274zfsctl_mknode_shares(vnode_t *pvp) 1275{ 1276 vnode_t *vp; 1277 zfsctl_node_t *sdp; 1278 1279 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp, 1280 &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN, 1281 NULL, NULL); 1282 sdp = vp->v_data; 1283 sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime; 1284 VOP_UNLOCK(vp, 0); 1285 return (vp); 1286 1287} 1288 1289/* ARGSUSED */ 1290static int 1291zfsctl_shares_getattr(ap) 1292 struct vop_getattr_args /* { 1293 struct vnode *a_vp; 1294 struct vattr *a_vap; 1295 struct ucred *a_cred; 1296 struct thread *a_td; 1297 } */ *ap; 1298{ 1299 vnode_t *vp = ap->a_vp; 1300 vattr_t *vap = ap->a_vap; 1301 cred_t *cr = ap->a_cred; 1302 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1303 znode_t *dzp; 1304 int error; 1305 1306 ZFS_ENTER(zfsvfs); 1307 if (zfsvfs->z_shares_dir == 0) { 1308 ZFS_EXIT(zfsvfs); 1309 return (SET_ERROR(ENOTSUP)); 1310 } 1311 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 1312 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY); 1313 error = VOP_GETATTR(ZTOV(dzp), vap, cr); 1314 VN_URELE(ZTOV(dzp)); 1315 } 1316 ZFS_EXIT(zfsvfs); 1317 return (error); 1318 1319 1320} 1321 1322/* ARGSUSED */ 1323static int 1324zfsctl_snapdir_getattr(ap) 1325 struct vop_getattr_args /* { 1326 struct vnode *a_vp; 1327 struct vattr *a_vap; 1328 struct ucred *a_cred; 1329 } */ *ap; 1330{ 1331 vnode_t *vp = ap->a_vp; 1332 vattr_t *vap = ap->a_vap; 1333 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1334 zfsctl_snapdir_t *sdp = vp->v_data; 1335 1336 ZFS_ENTER(zfsvfs); 1337 zfsctl_common_getattr(vp, vap); 1338 vap->va_nodeid = gfs_file_inode(vp); 1339 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2; 1340 vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os); 1341 vap->va_birthtime = vap->va_ctime; 1342 ZFS_EXIT(zfsvfs); 1343 1344 return (0); 1345} 1346 1347/* ARGSUSED */ 1348static int 1349zfsctl_snapdir_inactive(ap) 1350 struct vop_inactive_args /* { 1351 struct vnode *a_vp; 1352 struct thread *a_td; 1353 } */ *ap; 1354{ 1355 vnode_t *vp = ap->a_vp; 1356 zfsctl_snapdir_t *sdp = vp->v_data; 1357 zfs_snapentry_t *sep; 1358 1359 /* 1360 * On forced unmount we have to free snapshots from here. 1361 */ 1362 mutex_enter(&sdp->sd_lock); 1363 while ((sep = avl_first(&sdp->sd_snaps)) != NULL) { 1364 avl_remove(&sdp->sd_snaps, sep); 1365 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 1366 kmem_free(sep, sizeof (zfs_snapentry_t)); 1367 } 1368 mutex_exit(&sdp->sd_lock); 1369 gfs_dir_inactive(vp); 1370 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0); 1371 mutex_destroy(&sdp->sd_lock); 1372 avl_destroy(&sdp->sd_snaps); 1373 kmem_free(sdp, sizeof (zfsctl_snapdir_t)); 1374 1375 return (0); 1376} 1377 1378#ifdef sun 1379static const fs_operation_def_t zfsctl_tops_snapdir[] = { 1380 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 1381 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 1382 { VOPNAME_IOCTL, { .error = fs_inval } }, 1383 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_snapdir_getattr } }, 1384 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 1385 { VOPNAME_RENAME, { .vop_rename = zfsctl_snapdir_rename } }, 1386 { VOPNAME_RMDIR, { .vop_rmdir = zfsctl_snapdir_remove } }, 1387 { VOPNAME_MKDIR, { .vop_mkdir = zfsctl_snapdir_mkdir } }, 1388 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } }, 1389 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_snapdir_lookup } }, 1390 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 1391 { VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapdir_inactive } }, 1392 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } }, 1393 { NULL } 1394}; 1395 1396static const fs_operation_def_t zfsctl_tops_shares[] = { 1397 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 1398 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 1399 { VOPNAME_IOCTL, { .error = fs_inval } }, 1400 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_shares_getattr } }, 1401 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 1402 { VOPNAME_READDIR, { .vop_readdir = zfsctl_shares_readdir } }, 1403 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_shares_lookup } }, 1404 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 1405 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 1406 { VOPNAME_FID, { .vop_fid = zfsctl_shares_fid } }, 1407 { NULL } 1408}; 1409#else /* !sun */ 1410static struct vop_vector zfsctl_ops_snapdir = { 1411 .vop_default = &default_vnodeops, 1412 .vop_open = zfsctl_common_open, 1413 .vop_close = zfsctl_common_close, 1414 .vop_ioctl = VOP_EINVAL, 1415 .vop_getattr = zfsctl_snapdir_getattr, 1416 .vop_access = zfsctl_common_access, 1417 .vop_mkdir = zfsctl_freebsd_snapdir_mkdir, 1418 .vop_readdir = gfs_vop_readdir, 1419 .vop_lookup = zfsctl_snapdir_lookup, 1420 .vop_inactive = zfsctl_snapdir_inactive, 1421 .vop_reclaim = zfsctl_common_reclaim, 1422 .vop_fid = zfsctl_common_fid, 1423}; 1424 1425static struct vop_vector zfsctl_ops_shares = { 1426 .vop_default = &default_vnodeops, 1427 .vop_open = zfsctl_common_open, 1428 .vop_close = zfsctl_common_close, 1429 .vop_ioctl = VOP_EINVAL, 1430 .vop_getattr = zfsctl_shares_getattr, 1431 .vop_access = zfsctl_common_access, 1432 .vop_readdir = zfsctl_shares_readdir, 1433 .vop_lookup = zfsctl_shares_lookup, 1434 .vop_inactive = VOP_NULL, 1435 .vop_reclaim = gfs_vop_reclaim, 1436 .vop_fid = zfsctl_shares_fid, 1437}; 1438#endif /* !sun */ 1439 1440/* 1441 * pvp is the GFS vnode '.zfs/snapshot'. 1442 * 1443 * This creates a GFS node under '.zfs/snapshot' representing each 1444 * snapshot. This newly created GFS node is what we mount snapshot 1445 * vfs_t's ontop of. 1446 */ 1447static vnode_t * 1448zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset) 1449{ 1450 vnode_t *vp; 1451 zfsctl_node_t *zcp; 1452 1453 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp, 1454 &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL); 1455 VN_HOLD(vp); 1456 zcp = vp->v_data; 1457 zcp->zc_id = objset; 1458 VOP_UNLOCK(vp, 0); 1459 1460 return (vp); 1461} 1462 1463 1464static int 1465zfsctl_snapshot_reclaim(ap) 1466 struct vop_inactive_args /* { 1467 struct vnode *a_vp; 1468 struct thread *a_td; 1469 } */ *ap; 1470{ 1471 vnode_t *vp = ap->a_vp; 1472 cred_t *cr = ap->a_td->td_ucred; 1473 struct vop_reclaim_args iap; 1474 zfsctl_snapdir_t *sdp; 1475 zfs_snapentry_t *sep, *next; 1476 int locked; 1477 vnode_t *dvp; 1478 1479 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0); 1480 sdp = dvp->v_data; 1481 VOP_UNLOCK(dvp, 0); 1482 /* this may already have been unmounted */ 1483 if (sdp == NULL) { 1484 VN_RELE(dvp); 1485 return (0); 1486 } 1487 if (!(locked = MUTEX_HELD(&sdp->sd_lock))) 1488 mutex_enter(&sdp->sd_lock); 1489 1490 ASSERT(!vn_ismntpt(vp)); 1491 1492 sep = avl_first(&sdp->sd_snaps); 1493 while (sep != NULL) { 1494 next = AVL_NEXT(&sdp->sd_snaps, sep); 1495 1496 if (sep->se_root == vp) { 1497 avl_remove(&sdp->sd_snaps, sep); 1498 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 1499 kmem_free(sep, sizeof (zfs_snapentry_t)); 1500 break; 1501 } 1502 sep = next; 1503 } 1504 ASSERT(sep != NULL); 1505 1506 if (!locked) 1507 mutex_exit(&sdp->sd_lock); 1508 VN_RELE(dvp); 1509 1510 /* 1511 * Dispose of the vnode for the snapshot mount point. 1512 * This is safe to do because once this entry has been removed 1513 * from the AVL tree, it can't be found again, so cannot become 1514 * "active". If we lookup the same name again we will end up 1515 * creating a new vnode. 1516 */ 1517 iap.a_vp = vp; 1518 gfs_vop_reclaim(&iap); 1519 return (0); 1520 1521} 1522 1523static int 1524zfsctl_traverse_begin(vnode_t **vpp, int lktype) 1525{ 1526 1527 VN_HOLD(*vpp); 1528 /* Snapshot should be already mounted, but just in case. */ 1529 if (vn_mountedvfs(*vpp) == NULL) 1530 return (ENOENT); 1531 return (traverse(vpp, lktype)); 1532} 1533 1534static void 1535zfsctl_traverse_end(vnode_t *vp, int err) 1536{ 1537 1538 if (err == 0) 1539 vput(vp); 1540 else 1541 VN_RELE(vp); 1542} 1543 1544static int 1545zfsctl_snapshot_getattr(ap) 1546 struct vop_getattr_args /* { 1547 struct vnode *a_vp; 1548 struct vattr *a_vap; 1549 struct ucred *a_cred; 1550 } */ *ap; 1551{ 1552 vnode_t *vp = ap->a_vp; 1553 int err; 1554 1555 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY); 1556 if (err == 0) 1557 err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred); 1558 zfsctl_traverse_end(vp, err); 1559 return (err); 1560} 1561 1562static int 1563zfsctl_snapshot_fid(ap) 1564 struct vop_fid_args /* { 1565 struct vnode *a_vp; 1566 struct fid *a_fid; 1567 } */ *ap; 1568{ 1569 vnode_t *vp = ap->a_vp; 1570 int err; 1571 1572 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY); 1573 if (err == 0) 1574 err = VOP_VPTOFH(vp, (void *)ap->a_fid); 1575 zfsctl_traverse_end(vp, err); 1576 return (err); 1577} 1578 1579static int 1580zfsctl_snapshot_lookup(ap) 1581 struct vop_lookup_args /* { 1582 struct vnode *a_dvp; 1583 struct vnode **a_vpp; 1584 struct componentname *a_cnp; 1585 } */ *ap; 1586{ 1587 vnode_t *dvp = ap->a_dvp; 1588 vnode_t **vpp = ap->a_vpp; 1589 struct componentname *cnp = ap->a_cnp; 1590 cred_t *cr = ap->a_cnp->cn_cred; 1591 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 1592 int error; 1593 1594 if (cnp->cn_namelen != 2 || cnp->cn_nameptr[0] != '.' || 1595 cnp->cn_nameptr[1] != '.') { 1596 return (ENOENT); 1597 } 1598 1599 ASSERT(dvp->v_type == VDIR); 1600 ASSERT(zfsvfs->z_ctldir != NULL); 1601 1602 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", vpp, 1603 NULL, 0, NULL, cr, NULL, NULL, NULL); 1604 if (error == 0) { 1605 int ltype = VOP_ISLOCKED(dvp); 1606 VN_HOLD(*vpp); 1607 VOP_UNLOCK(dvp, 0); 1608 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 1609 VN_RELE(*vpp); 1610 vn_lock(dvp, ltype | LK_RETRY); 1611 } 1612 1613 return (error); 1614} 1615 1616static int 1617zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap) 1618{ 1619 zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data; 1620 vnode_t *dvp, *vp; 1621 zfsctl_snapdir_t *sdp; 1622 zfs_snapentry_t *sep; 1623 int error; 1624 1625 ASSERT(zfsvfs->z_ctldir != NULL); 1626 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 1627 NULL, 0, NULL, kcred, NULL, NULL, NULL); 1628 if (error != 0) 1629 return (error); 1630 sdp = dvp->v_data; 1631 1632 mutex_enter(&sdp->sd_lock); 1633 sep = avl_first(&sdp->sd_snaps); 1634 while (sep != NULL) { 1635 vp = sep->se_root; 1636 if (vp == ap->a_vp) 1637 break; 1638 sep = AVL_NEXT(&sdp->sd_snaps, sep); 1639 } 1640 if (sep == NULL) { 1641 mutex_exit(&sdp->sd_lock); 1642 error = ENOENT; 1643 } else { 1644 size_t len; 1645 1646 len = strlen(sep->se_name); 1647 *ap->a_buflen -= len; 1648 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len); 1649 mutex_exit(&sdp->sd_lock); 1650 vref(dvp); 1651 *ap->a_vpp = dvp; 1652 } 1653 VN_RELE(dvp); 1654 1655 return (error); 1656} 1657 1658/* 1659 * These VP's should never see the light of day. They should always 1660 * be covered. 1661 */ 1662static struct vop_vector zfsctl_ops_snapshot = { 1663 .vop_default = &default_vnodeops, 1664 .vop_inactive = VOP_NULL, 1665 .vop_lookup = zfsctl_snapshot_lookup, 1666 .vop_reclaim = zfsctl_snapshot_reclaim, 1667 .vop_getattr = zfsctl_snapshot_getattr, 1668 .vop_fid = zfsctl_snapshot_fid, 1669 .vop_vptocnp = zfsctl_snapshot_vptocnp, 1670}; 1671 1672int 1673zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp) 1674{ 1675 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1676 vnode_t *dvp, *vp; 1677 zfsctl_snapdir_t *sdp; 1678 zfsctl_node_t *zcp; 1679 zfs_snapentry_t *sep; 1680 int error; 1681 1682 ASSERT(zfsvfs->z_ctldir != NULL); 1683 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 1684 NULL, 0, NULL, kcred, NULL, NULL, NULL); 1685 if (error != 0) 1686 return (error); 1687 sdp = dvp->v_data; 1688 1689 mutex_enter(&sdp->sd_lock); 1690 sep = avl_first(&sdp->sd_snaps); 1691 while (sep != NULL) { 1692 vp = sep->se_root; 1693 zcp = vp->v_data; 1694 if (zcp->zc_id == objsetid) 1695 break; 1696 1697 sep = AVL_NEXT(&sdp->sd_snaps, sep); 1698 } 1699 1700 if (sep != NULL) { 1701 VN_HOLD(vp); 1702 /* 1703 * Return the mounted root rather than the covered mount point. 1704 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid> 1705 * and returns the ZFS vnode mounted on top of the GFS node. 1706 * This ZFS vnode is the root of the vfs for objset 'objsetid'. 1707 */ 1708 error = traverse(&vp, LK_SHARED | LK_RETRY); 1709 if (error == 0) { 1710 if (vp == sep->se_root) 1711 error = SET_ERROR(EINVAL); 1712 else 1713 *zfsvfsp = VTOZ(vp)->z_zfsvfs; 1714 } 1715 mutex_exit(&sdp->sd_lock); 1716 if (error == 0) 1717 VN_URELE(vp); 1718 else 1719 VN_RELE(vp); 1720 } else { 1721 error = SET_ERROR(EINVAL); 1722 mutex_exit(&sdp->sd_lock); 1723 } 1724 1725 VN_RELE(dvp); 1726 1727 return (error); 1728} 1729 1730/* 1731 * Unmount any snapshots for the given filesystem. This is called from 1732 * zfs_umount() - if we have a ctldir, then go through and unmount all the 1733 * snapshots. 1734 */ 1735int 1736zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr) 1737{ 1738 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1739 vnode_t *dvp; 1740 zfsctl_snapdir_t *sdp; 1741 zfs_snapentry_t *sep, *next; 1742 int error; 1743 1744 ASSERT(zfsvfs->z_ctldir != NULL); 1745 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 1746 NULL, 0, NULL, cr, NULL, NULL, NULL); 1747 if (error != 0) 1748 return (error); 1749 sdp = dvp->v_data; 1750 1751 mutex_enter(&sdp->sd_lock); 1752 1753 sep = avl_first(&sdp->sd_snaps); 1754 while (sep != NULL) { 1755 next = AVL_NEXT(&sdp->sd_snaps, sep); 1756 1757 /* 1758 * If this snapshot is not mounted, then it must 1759 * have just been unmounted by somebody else, and 1760 * will be cleaned up by zfsctl_snapdir_inactive(). 1761 */ 1762 if (vn_ismntpt(sep->se_root)) { 1763 error = zfsctl_unmount_snap(sep, fflags, cr); 1764 if (error) { 1765 avl_index_t where; 1766 1767 /* 1768 * Before reinserting snapshot to the tree, 1769 * check if it was actually removed. For example 1770 * when snapshot mount point is busy, we will 1771 * have an error here, but there will be no need 1772 * to reinsert snapshot. 1773 */ 1774 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL) 1775 avl_insert(&sdp->sd_snaps, sep, where); 1776 break; 1777 } 1778 } 1779 sep = next; 1780 } 1781 1782 mutex_exit(&sdp->sd_lock); 1783 VN_RELE(dvp); 1784 1785 return (error); 1786} 1787