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