zfs_ctldir.c revision 302735
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 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp); 555 if (err == 0) 556 VOP_UNLOCK(*vpp, 0); 557 } else { 558 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir, 559 cr, ct, direntflags, realpnp); 560 } 561 562 ZFS_EXIT(zfsvfs); 563 564 return (err); 565} 566 567static int 568zfsctl_root_print(ap) 569 struct vop_print_args /* { 570 struct vnode *a_vp; 571 } */ *ap; 572{ 573 printf(" .zfs node\n"); 574 zfsctl_common_print(ap); 575 return (0); 576} 577 578#ifdef illumos 579static int 580zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 581 caller_context_t *ct) 582{ 583 /* 584 * We only care about ACL_ENABLED so that libsec can 585 * display ACL correctly and not default to POSIX draft. 586 */ 587 if (cmd == _PC_ACL_ENABLED) { 588 *valp = _ACL_ACE_ENABLED; 589 return (0); 590 } 591 592 return (fs_pathconf(vp, cmd, valp, cr, ct)); 593} 594#endif /* illumos */ 595 596#ifdef illumos 597static const fs_operation_def_t zfsctl_tops_root[] = { 598 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 599 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 600 { VOPNAME_IOCTL, { .error = fs_inval } }, 601 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } }, 602 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 603 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } }, 604 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } }, 605 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 606 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 607 { VOPNAME_PATHCONF, { .vop_pathconf = zfsctl_pathconf } }, 608 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } }, 609 { NULL } 610}; 611#endif /* illumos */ 612 613/* 614 * Special case the handling of "..". 615 */ 616/* ARGSUSED */ 617int 618zfsctl_freebsd_root_lookup(ap) 619 struct vop_lookup_args /* { 620 struct vnode *a_dvp; 621 struct vnode **a_vpp; 622 struct componentname *a_cnp; 623 } */ *ap; 624{ 625 vnode_t *dvp = ap->a_dvp; 626 vnode_t **vpp = ap->a_vpp; 627 cred_t *cr = ap->a_cnp->cn_cred; 628 int flags = ap->a_cnp->cn_flags; 629 int nameiop = ap->a_cnp->cn_nameiop; 630 char nm[NAME_MAX + 1]; 631 int err; 632 int ltype; 633 634 if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE)) 635 return (EOPNOTSUPP); 636 637 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm)); 638 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1); 639 err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL); 640 if (err == 0 && (nm[0] != '.' || nm[1] != '\0')) { 641 ltype = VOP_ISLOCKED(dvp); 642 if (flags & ISDOTDOT) { 643 VN_HOLD(*vpp); 644 VOP_UNLOCK(dvp, 0); 645 } 646 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 647 if (flags & ISDOTDOT) { 648 VN_RELE(*vpp); 649 vn_lock(dvp, ltype| LK_RETRY); 650 } 651 } 652 653 return (err); 654} 655 656static struct vop_vector zfsctl_ops_root = { 657 .vop_default = &default_vnodeops, 658 .vop_open = zfsctl_common_open, 659 .vop_close = zfsctl_common_close, 660 .vop_ioctl = VOP_EINVAL, 661 .vop_getattr = zfsctl_root_getattr, 662 .vop_access = zfsctl_common_access, 663 .vop_readdir = gfs_vop_readdir, 664 .vop_lookup = zfsctl_freebsd_root_lookup, 665 .vop_inactive = VOP_NULL, 666 .vop_reclaim = gfs_vop_reclaim, 667#ifdef TODO 668 .vop_pathconf = zfsctl_pathconf, 669#endif 670 .vop_fid = zfsctl_common_fid, 671 .vop_print = zfsctl_root_print, 672}; 673 674/* 675 * Gets the full dataset name that corresponds to the given snapshot name 676 * Example: 677 * zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1" 678 */ 679static int 680zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname) 681{ 682 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 683 684 if (zfs_component_namecheck(name, NULL, NULL) != 0) 685 return (SET_ERROR(EILSEQ)); 686 dmu_objset_name(os, zname); 687 if (strlen(zname) + 1 + strlen(name) >= len) 688 return (SET_ERROR(ENAMETOOLONG)); 689 (void) strcat(zname, "@"); 690 (void) strcat(zname, name); 691 return (0); 692} 693 694static int 695zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr) 696{ 697 vnode_t *svp = sep->se_root; 698 int error; 699 700 ASSERT(vn_ismntpt(svp)); 701 702 /* this will be dropped by dounmount() */ 703 if ((error = vn_vfswlock(svp)) != 0) 704 return (error); 705 706#ifdef illumos 707 VN_HOLD(svp); 708 error = dounmount(vn_mountedvfs(svp), fflags, cr); 709 if (error) { 710 VN_RELE(svp); 711 return (error); 712 } 713 714 /* 715 * We can't use VN_RELE(), as that will try to invoke 716 * zfsctl_snapdir_inactive(), which would cause us to destroy 717 * the sd_lock mutex held by our caller. 718 */ 719 ASSERT(svp->v_count == 1); 720 gfs_vop_reclaim(svp, cr, NULL); 721 722 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 723 kmem_free(sep, sizeof (zfs_snapentry_t)); 724 725 return (0); 726#else 727 vfs_ref(vn_mountedvfs(svp)); 728 return (dounmount(vn_mountedvfs(svp), fflags, curthread)); 729#endif 730} 731 732#ifdef illumos 733static void 734zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm) 735{ 736 avl_index_t where; 737 vfs_t *vfsp; 738 refstr_t *pathref; 739 char newpath[MAXNAMELEN]; 740 char *tail; 741 742 ASSERT(MUTEX_HELD(&sdp->sd_lock)); 743 ASSERT(sep != NULL); 744 745 vfsp = vn_mountedvfs(sep->se_root); 746 ASSERT(vfsp != NULL); 747 748 vfs_lock_wait(vfsp); 749 750 /* 751 * Change the name in the AVL tree. 752 */ 753 avl_remove(&sdp->sd_snaps, sep); 754 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 755 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP); 756 (void) strcpy(sep->se_name, nm); 757 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL); 758 avl_insert(&sdp->sd_snaps, sep, where); 759 760 /* 761 * Change the current mountpoint info: 762 * - update the tail of the mntpoint path 763 * - update the tail of the resource path 764 */ 765 pathref = vfs_getmntpoint(vfsp); 766 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath)); 767 VERIFY((tail = strrchr(newpath, '/')) != NULL); 768 *(tail+1) = '\0'; 769 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath)); 770 (void) strcat(newpath, nm); 771 refstr_rele(pathref); 772 vfs_setmntpoint(vfsp, newpath, 0); 773 774 pathref = vfs_getresource(vfsp); 775 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath)); 776 VERIFY((tail = strrchr(newpath, '@')) != NULL); 777 *(tail+1) = '\0'; 778 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath)); 779 (void) strcat(newpath, nm); 780 refstr_rele(pathref); 781 vfs_setresource(vfsp, newpath, 0); 782 783 vfs_unlock(vfsp); 784} 785#endif /* illumos */ 786 787#ifdef illumos 788/*ARGSUSED*/ 789static int 790zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, 791 cred_t *cr, caller_context_t *ct, int flags) 792{ 793 zfsctl_snapdir_t *sdp = sdvp->v_data; 794 zfs_snapentry_t search, *sep; 795 zfsvfs_t *zfsvfs; 796 avl_index_t where; 797 char from[MAXNAMELEN], to[MAXNAMELEN]; 798 char real[MAXNAMELEN], fsname[MAXNAMELEN]; 799 int err; 800 801 zfsvfs = sdvp->v_vfsp->vfs_data; 802 ZFS_ENTER(zfsvfs); 803 804 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 805 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real, 806 MAXNAMELEN, NULL); 807 if (err == 0) { 808 snm = real; 809 } else if (err != ENOTSUP) { 810 ZFS_EXIT(zfsvfs); 811 return (err); 812 } 813 } 814 815 ZFS_EXIT(zfsvfs); 816 817 dmu_objset_name(zfsvfs->z_os, fsname); 818 819 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from); 820 if (err == 0) 821 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to); 822 if (err == 0) 823 err = zfs_secpolicy_rename_perms(from, to, cr); 824 if (err != 0) 825 return (err); 826 827 /* 828 * Cannot move snapshots out of the snapdir. 829 */ 830 if (sdvp != tdvp) 831 return (SET_ERROR(EINVAL)); 832 833 if (strcmp(snm, tnm) == 0) 834 return (0); 835 836 mutex_enter(&sdp->sd_lock); 837 838 search.se_name = (char *)snm; 839 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) { 840 mutex_exit(&sdp->sd_lock); 841 return (SET_ERROR(ENOENT)); 842 } 843 844 err = dsl_dataset_rename_snapshot(fsname, snm, tnm, 0); 845 if (err == 0) 846 zfsctl_rename_snap(sdp, sep, tnm); 847 848 mutex_exit(&sdp->sd_lock); 849 850 return (err); 851} 852#endif /* illumos */ 853 854#ifdef illumos 855/* ARGSUSED */ 856static int 857zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 858 caller_context_t *ct, int flags) 859{ 860 zfsctl_snapdir_t *sdp = dvp->v_data; 861 zfs_snapentry_t *sep; 862 zfs_snapentry_t search; 863 zfsvfs_t *zfsvfs; 864 char snapname[MAXNAMELEN]; 865 char real[MAXNAMELEN]; 866 int err; 867 868 zfsvfs = dvp->v_vfsp->vfs_data; 869 ZFS_ENTER(zfsvfs); 870 871 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 872 873 err = dmu_snapshot_realname(zfsvfs->z_os, name, real, 874 MAXNAMELEN, NULL); 875 if (err == 0) { 876 name = real; 877 } else if (err != ENOTSUP) { 878 ZFS_EXIT(zfsvfs); 879 return (err); 880 } 881 } 882 883 ZFS_EXIT(zfsvfs); 884 885 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname); 886 if (err == 0) 887 err = zfs_secpolicy_destroy_perms(snapname, cr); 888 if (err != 0) 889 return (err); 890 891 mutex_enter(&sdp->sd_lock); 892 893 search.se_name = name; 894 sep = avl_find(&sdp->sd_snaps, &search, NULL); 895 if (sep) { 896 avl_remove(&sdp->sd_snaps, sep); 897 err = zfsctl_unmount_snap(sep, MS_FORCE, cr); 898 if (err != 0) 899 avl_add(&sdp->sd_snaps, sep); 900 else 901 err = dsl_destroy_snapshot(snapname, B_FALSE); 902 } else { 903 err = SET_ERROR(ENOENT); 904 } 905 906 mutex_exit(&sdp->sd_lock); 907 908 return (err); 909} 910#endif /* illumos */ 911 912/* 913 * This creates a snapshot under '.zfs/snapshot'. 914 */ 915/* ARGSUSED */ 916static int 917zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, 918 cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp) 919{ 920 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 921 char name[MAXNAMELEN]; 922 int err; 923 static enum symfollow follow = NO_FOLLOW; 924 static enum uio_seg seg = UIO_SYSSPACE; 925 926 if (zfs_component_namecheck(dirname, NULL, NULL) != 0) 927 return (SET_ERROR(EILSEQ)); 928 929 dmu_objset_name(zfsvfs->z_os, name); 930 931 *vpp = NULL; 932 933 err = zfs_secpolicy_snapshot_perms(name, cr); 934 if (err != 0) 935 return (err); 936 937 if (err == 0) { 938 err = dmu_objset_snapshot_one(name, dirname); 939 if (err != 0) 940 return (err); 941 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp); 942 } 943 944 return (err); 945} 946 947static int 948zfsctl_freebsd_snapdir_mkdir(ap) 949 struct vop_mkdir_args /* { 950 struct vnode *a_dvp; 951 struct vnode **a_vpp; 952 struct componentname *a_cnp; 953 struct vattr *a_vap; 954 } */ *ap; 955{ 956 957 ASSERT(ap->a_cnp->cn_flags & SAVENAME); 958 959 return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL, 960 ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL)); 961} 962 963/* 964 * Lookup entry point for the 'snapshot' directory. Try to open the 965 * snapshot if it exist, creating the pseudo filesystem vnode as necessary. 966 * Perform a mount of the associated dataset on top of the vnode. 967 */ 968/* ARGSUSED */ 969int 970zfsctl_snapdir_lookup(ap) 971 struct vop_lookup_args /* { 972 struct vnode *a_dvp; 973 struct vnode **a_vpp; 974 struct componentname *a_cnp; 975 } */ *ap; 976{ 977 vnode_t *dvp = ap->a_dvp; 978 vnode_t **vpp = ap->a_vpp; 979 struct componentname *cnp = ap->a_cnp; 980 char nm[NAME_MAX + 1]; 981 zfsctl_snapdir_t *sdp = dvp->v_data; 982 objset_t *snap; 983 char snapname[MAXNAMELEN]; 984 char real[MAXNAMELEN]; 985 char *mountpoint; 986 zfs_snapentry_t *sep, search; 987 size_t mountpoint_len; 988 avl_index_t where; 989 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 990 int err; 991 int ltype, flags = 0; 992 993 /* 994 * No extended attributes allowed under .zfs 995 */ 996 if (flags & LOOKUP_XATTR) 997 return (SET_ERROR(EINVAL)); 998 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm)); 999 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1); 1000 1001 ASSERT(dvp->v_type == VDIR); 1002 1003 *vpp = NULL; 1004 1005 /* 1006 * If we get a recursive call, that means we got called 1007 * from the domount() code while it was trying to look up the 1008 * spec (which looks like a local path for zfs). We need to 1009 * add some flag to domount() to tell it not to do this lookup. 1010 */ 1011 if (MUTEX_HELD(&sdp->sd_lock)) 1012 return (SET_ERROR(ENOENT)); 1013 1014 ZFS_ENTER(zfsvfs); 1015 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) { 1016 ZFS_EXIT(zfsvfs); 1017 return (0); 1018 } 1019 1020 if (flags & FIGNORECASE) { 1021 boolean_t conflict = B_FALSE; 1022 1023 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real, 1024 MAXNAMELEN, &conflict); 1025 if (err == 0) { 1026 strlcpy(nm, real, sizeof(nm)); 1027 } else if (err != ENOTSUP) { 1028 ZFS_EXIT(zfsvfs); 1029 return (err); 1030 } 1031#if 0 1032 if (realpnp) 1033 (void) strlcpy(realpnp->pn_buf, nm, 1034 realpnp->pn_bufsize); 1035 if (conflict && direntflags) 1036 *direntflags = ED_CASE_CONFLICT; 1037#endif 1038 } 1039 1040 mutex_enter(&sdp->sd_lock); 1041 search.se_name = (char *)nm; 1042 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) { 1043 *vpp = sep->se_root; 1044 VN_HOLD(*vpp); 1045 err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY); 1046 if (err != 0) { 1047 VN_RELE(*vpp); 1048 *vpp = NULL; 1049 } else if (*vpp == sep->se_root) { 1050 /* 1051 * The snapshot was unmounted behind our backs, 1052 * try to remount it. 1053 */ 1054 VERIFY(zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname) == 0); 1055 goto domount; 1056 } else { 1057 /* 1058 * VROOT was set during the traverse call. We need 1059 * to clear it since we're pretending to be part 1060 * of our parent's vfs. 1061 */ 1062 (*vpp)->v_flag &= ~VROOT; 1063 } 1064 mutex_exit(&sdp->sd_lock); 1065 ZFS_EXIT(zfsvfs); 1066 return (err); 1067 } 1068 1069 /* 1070 * The requested snapshot is not currently mounted, look it up. 1071 */ 1072 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname); 1073 if (err != 0) { 1074 mutex_exit(&sdp->sd_lock); 1075 ZFS_EXIT(zfsvfs); 1076 /* 1077 * handle "ls *" or "?" in a graceful manner, 1078 * forcing EILSEQ to ENOENT. 1079 * Since shell ultimately passes "*" or "?" as name to lookup 1080 */ 1081 return (err == EILSEQ ? ENOENT : err); 1082 } 1083 if (dmu_objset_hold(snapname, FTAG, &snap) != 0) { 1084 mutex_exit(&sdp->sd_lock); 1085#ifdef illumos 1086 ZFS_EXIT(zfsvfs); 1087 return (SET_ERROR(ENOENT)); 1088#else /* !illumos */ 1089 /* Translate errors and add SAVENAME when needed. */ 1090 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) { 1091 err = EJUSTRETURN; 1092 cnp->cn_flags |= SAVENAME; 1093 } else { 1094 err = SET_ERROR(ENOENT); 1095 } 1096 ZFS_EXIT(zfsvfs); 1097 return (err); 1098#endif /* illumos */ 1099 } 1100 1101 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP); 1102 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP); 1103 (void) strcpy(sep->se_name, nm); 1104 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap)); 1105 avl_insert(&sdp->sd_snaps, sep, where); 1106 1107 dmu_objset_rele(snap, FTAG); 1108domount: 1109 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) + 1110 strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(nm) + 1; 1111 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP); 1112 (void) snprintf(mountpoint, mountpoint_len, 1113 "%s/" ZFS_CTLDIR_NAME "/snapshot/%s", 1114 dvp->v_vfsp->mnt_stat.f_mntonname, nm); 1115 VERIFY0(vn_lock(*vpp, LK_EXCLUSIVE)); 1116 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0); 1117 kmem_free(mountpoint, mountpoint_len); 1118 if (err == 0) { 1119 /* 1120 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>. 1121 * 1122 * This is where we lie about our v_vfsp in order to 1123 * make .zfs/snapshot/<snapname> accessible over NFS 1124 * without requiring manual mounts of <snapname>. 1125 */ 1126 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs); 1127 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs; 1128 } 1129 mutex_exit(&sdp->sd_lock); 1130 ZFS_EXIT(zfsvfs); 1131 1132#ifdef illumos 1133 /* 1134 * If we had an error, drop our hold on the vnode and 1135 * zfsctl_snapshot_inactive() will clean up. 1136 */ 1137 if (err != 0) { 1138 VN_RELE(*vpp); 1139 *vpp = NULL; 1140 } 1141#else 1142 if (err != 0) 1143 *vpp = NULL; 1144#endif 1145 return (err); 1146} 1147 1148/* ARGSUSED */ 1149int 1150zfsctl_shares_lookup(ap) 1151 struct vop_lookup_args /* { 1152 struct vnode *a_dvp; 1153 struct vnode **a_vpp; 1154 struct componentname *a_cnp; 1155 } */ *ap; 1156{ 1157 vnode_t *dvp = ap->a_dvp; 1158 vnode_t **vpp = ap->a_vpp; 1159 struct componentname *cnp = ap->a_cnp; 1160 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 1161 char nm[NAME_MAX + 1]; 1162 znode_t *dzp; 1163 int error; 1164 1165 ZFS_ENTER(zfsvfs); 1166 1167 ASSERT(cnp->cn_namelen < sizeof(nm)); 1168 strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1); 1169 1170 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) { 1171 ZFS_EXIT(zfsvfs); 1172 return (0); 1173 } 1174 1175 if (zfsvfs->z_shares_dir == 0) { 1176 ZFS_EXIT(zfsvfs); 1177 return (SET_ERROR(ENOTSUP)); 1178 } 1179 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 1180 error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp); 1181 VN_RELE(ZTOV(dzp)); 1182 } 1183 1184 ZFS_EXIT(zfsvfs); 1185 1186 return (error); 1187} 1188 1189/* ARGSUSED */ 1190static int 1191zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp, 1192 offset_t *offp, offset_t *nextp, void *data, int flags) 1193{ 1194 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1195 char snapname[MAXNAMELEN]; 1196 uint64_t id, cookie; 1197 boolean_t case_conflict; 1198 int error; 1199 1200 ZFS_ENTER(zfsvfs); 1201 1202 cookie = *offp; 1203 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 1204 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id, 1205 &cookie, &case_conflict); 1206 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 1207 if (error) { 1208 ZFS_EXIT(zfsvfs); 1209 if (error == ENOENT) { 1210 *eofp = 1; 1211 return (0); 1212 } 1213 return (error); 1214 } 1215 1216 if (flags & V_RDDIR_ENTFLAGS) { 1217 edirent_t *eodp = dp; 1218 1219 (void) strcpy(eodp->ed_name, snapname); 1220 eodp->ed_ino = ZFSCTL_INO_SNAP(id); 1221 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0; 1222 } else { 1223 struct dirent64 *odp = dp; 1224 1225 (void) strcpy(odp->d_name, snapname); 1226 odp->d_ino = ZFSCTL_INO_SNAP(id); 1227 } 1228 *nextp = cookie; 1229 1230 ZFS_EXIT(zfsvfs); 1231 1232 return (0); 1233} 1234 1235/* ARGSUSED */ 1236static int 1237zfsctl_shares_readdir(ap) 1238 struct vop_readdir_args /* { 1239 struct vnode *a_vp; 1240 struct uio *a_uio; 1241 struct ucred *a_cred; 1242 int *a_eofflag; 1243 int *a_ncookies; 1244 u_long **a_cookies; 1245 } */ *ap; 1246{ 1247 vnode_t *vp = ap->a_vp; 1248 uio_t *uiop = ap->a_uio; 1249 cred_t *cr = ap->a_cred; 1250 int *eofp = ap->a_eofflag; 1251 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1252 znode_t *dzp; 1253 int error; 1254 1255 ZFS_ENTER(zfsvfs); 1256 1257 if (zfsvfs->z_shares_dir == 0) { 1258 ZFS_EXIT(zfsvfs); 1259 return (SET_ERROR(ENOTSUP)); 1260 } 1261 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 1262 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY); 1263 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies); 1264 VN_URELE(ZTOV(dzp)); 1265 } else { 1266 *eofp = 1; 1267 error = SET_ERROR(ENOENT); 1268 } 1269 1270 ZFS_EXIT(zfsvfs); 1271 return (error); 1272} 1273 1274/* 1275 * pvp is the '.zfs' directory (zfsctl_node_t). 1276 * 1277 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t). 1278 * 1279 * This function is the callback to create a GFS vnode for '.zfs/snapshot' 1280 * when a lookup is performed on .zfs for "snapshot". 1281 */ 1282vnode_t * 1283zfsctl_mknode_snapdir(vnode_t *pvp) 1284{ 1285 vnode_t *vp; 1286 zfsctl_snapdir_t *sdp; 1287 1288 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp, 1289 &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN, 1290 zfsctl_snapdir_readdir_cb, NULL); 1291 sdp = vp->v_data; 1292 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR; 1293 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime; 1294 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL); 1295 avl_create(&sdp->sd_snaps, snapentry_compare, 1296 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node)); 1297 VOP_UNLOCK(vp, 0); 1298 return (vp); 1299} 1300 1301vnode_t * 1302zfsctl_mknode_shares(vnode_t *pvp) 1303{ 1304 vnode_t *vp; 1305 zfsctl_node_t *sdp; 1306 1307 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp, 1308 &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN, 1309 NULL, NULL); 1310 sdp = vp->v_data; 1311 sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime; 1312 VOP_UNLOCK(vp, 0); 1313 return (vp); 1314 1315} 1316 1317/* ARGSUSED */ 1318static int 1319zfsctl_shares_getattr(ap) 1320 struct vop_getattr_args /* { 1321 struct vnode *a_vp; 1322 struct vattr *a_vap; 1323 struct ucred *a_cred; 1324 struct thread *a_td; 1325 } */ *ap; 1326{ 1327 vnode_t *vp = ap->a_vp; 1328 vattr_t *vap = ap->a_vap; 1329 cred_t *cr = ap->a_cred; 1330 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1331 znode_t *dzp; 1332 int error; 1333 1334 ZFS_ENTER(zfsvfs); 1335 if (zfsvfs->z_shares_dir == 0) { 1336 ZFS_EXIT(zfsvfs); 1337 return (SET_ERROR(ENOTSUP)); 1338 } 1339 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 1340 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY); 1341 error = VOP_GETATTR(ZTOV(dzp), vap, cr); 1342 VN_URELE(ZTOV(dzp)); 1343 } 1344 ZFS_EXIT(zfsvfs); 1345 return (error); 1346 1347 1348} 1349 1350/* ARGSUSED */ 1351static int 1352zfsctl_snapdir_getattr(ap) 1353 struct vop_getattr_args /* { 1354 struct vnode *a_vp; 1355 struct vattr *a_vap; 1356 struct ucred *a_cred; 1357 } */ *ap; 1358{ 1359 vnode_t *vp = ap->a_vp; 1360 vattr_t *vap = ap->a_vap; 1361 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1362 zfsctl_snapdir_t *sdp = vp->v_data; 1363 1364 ZFS_ENTER(zfsvfs); 1365 zfsctl_common_getattr(vp, vap); 1366 vap->va_nodeid = gfs_file_inode(vp); 1367 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2; 1368 vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os); 1369 vap->va_birthtime = vap->va_ctime; 1370 ZFS_EXIT(zfsvfs); 1371 1372 return (0); 1373} 1374 1375/* ARGSUSED */ 1376static int 1377zfsctl_snapdir_inactive(ap) 1378 struct vop_inactive_args /* { 1379 struct vnode *a_vp; 1380 struct thread *a_td; 1381 } */ *ap; 1382{ 1383 vnode_t *vp = ap->a_vp; 1384 zfsctl_snapdir_t *sdp = vp->v_data; 1385 zfs_snapentry_t *sep; 1386 1387 /* 1388 * On forced unmount we have to free snapshots from here. 1389 */ 1390 mutex_enter(&sdp->sd_lock); 1391 while ((sep = avl_first(&sdp->sd_snaps)) != NULL) { 1392 avl_remove(&sdp->sd_snaps, sep); 1393 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 1394 kmem_free(sep, sizeof (zfs_snapentry_t)); 1395 } 1396 mutex_exit(&sdp->sd_lock); 1397 gfs_dir_inactive(vp); 1398 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0); 1399 mutex_destroy(&sdp->sd_lock); 1400 avl_destroy(&sdp->sd_snaps); 1401 kmem_free(sdp, sizeof (zfsctl_snapdir_t)); 1402 1403 return (0); 1404} 1405 1406static int 1407zfsctl_shares_print(ap) 1408 struct vop_print_args /* { 1409 struct vnode *a_vp; 1410 } */ *ap; 1411{ 1412 printf(" .zfs/shares node\n"); 1413 zfsctl_common_print(ap); 1414 return (0); 1415} 1416 1417static int 1418zfsctl_snapdir_print(ap) 1419 struct vop_print_args /* { 1420 struct vnode *a_vp; 1421 } */ *ap; 1422{ 1423 vnode_t *vp = ap->a_vp; 1424 zfsctl_snapdir_t *sdp = vp->v_data; 1425 1426 printf(" .zfs/snapshot node\n"); 1427 printf(" number of children = %lu\n", avl_numnodes(&sdp->sd_snaps)); 1428 zfsctl_common_print(ap); 1429 return (0); 1430} 1431 1432#ifdef illumos 1433static const fs_operation_def_t zfsctl_tops_snapdir[] = { 1434 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 1435 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 1436 { VOPNAME_IOCTL, { .error = fs_inval } }, 1437 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_snapdir_getattr } }, 1438 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 1439 { VOPNAME_RENAME, { .vop_rename = zfsctl_snapdir_rename } }, 1440 { VOPNAME_RMDIR, { .vop_rmdir = zfsctl_snapdir_remove } }, 1441 { VOPNAME_MKDIR, { .vop_mkdir = zfsctl_snapdir_mkdir } }, 1442 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } }, 1443 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_snapdir_lookup } }, 1444 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 1445 { VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapdir_inactive } }, 1446 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } }, 1447 { NULL } 1448}; 1449 1450static const fs_operation_def_t zfsctl_tops_shares[] = { 1451 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 1452 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 1453 { VOPNAME_IOCTL, { .error = fs_inval } }, 1454 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_shares_getattr } }, 1455 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 1456 { VOPNAME_READDIR, { .vop_readdir = zfsctl_shares_readdir } }, 1457 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_shares_lookup } }, 1458 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 1459 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 1460 { VOPNAME_FID, { .vop_fid = zfsctl_shares_fid } }, 1461 { NULL } 1462}; 1463#else /* !illumos */ 1464static struct vop_vector zfsctl_ops_snapdir = { 1465 .vop_default = &default_vnodeops, 1466 .vop_open = zfsctl_common_open, 1467 .vop_close = zfsctl_common_close, 1468 .vop_ioctl = VOP_EINVAL, 1469 .vop_getattr = zfsctl_snapdir_getattr, 1470 .vop_access = zfsctl_common_access, 1471 .vop_mkdir = zfsctl_freebsd_snapdir_mkdir, 1472 .vop_readdir = gfs_vop_readdir, 1473 .vop_lookup = zfsctl_snapdir_lookup, 1474 .vop_inactive = zfsctl_snapdir_inactive, 1475 .vop_reclaim = zfsctl_common_reclaim, 1476 .vop_fid = zfsctl_common_fid, 1477 .vop_print = zfsctl_snapdir_print, 1478}; 1479 1480static struct vop_vector zfsctl_ops_shares = { 1481 .vop_default = &default_vnodeops, 1482 .vop_open = zfsctl_common_open, 1483 .vop_close = zfsctl_common_close, 1484 .vop_ioctl = VOP_EINVAL, 1485 .vop_getattr = zfsctl_shares_getattr, 1486 .vop_access = zfsctl_common_access, 1487 .vop_readdir = zfsctl_shares_readdir, 1488 .vop_lookup = zfsctl_shares_lookup, 1489 .vop_inactive = VOP_NULL, 1490 .vop_reclaim = gfs_vop_reclaim, 1491 .vop_fid = zfsctl_shares_fid, 1492 .vop_print = zfsctl_shares_print, 1493}; 1494#endif /* illumos */ 1495 1496/* 1497 * pvp is the GFS vnode '.zfs/snapshot'. 1498 * 1499 * This creates a GFS node under '.zfs/snapshot' representing each 1500 * snapshot. This newly created GFS node is what we mount snapshot 1501 * vfs_t's ontop of. 1502 */ 1503static vnode_t * 1504zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset) 1505{ 1506 vnode_t *vp; 1507 zfsctl_node_t *zcp; 1508 1509 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp, 1510 &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL); 1511 zcp = vp->v_data; 1512 zcp->zc_id = objset; 1513 VOP_UNLOCK(vp, 0); 1514 1515 return (vp); 1516} 1517 1518 1519static int 1520zfsctl_snapshot_reclaim(ap) 1521 struct vop_inactive_args /* { 1522 struct vnode *a_vp; 1523 struct thread *a_td; 1524 } */ *ap; 1525{ 1526 vnode_t *vp = ap->a_vp; 1527 cred_t *cr = ap->a_td->td_ucred; 1528 struct vop_reclaim_args iap; 1529 zfsctl_snapdir_t *sdp; 1530 zfs_snapentry_t *sep, *next; 1531 int locked; 1532 vnode_t *dvp; 1533 1534 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0); 1535 sdp = dvp->v_data; 1536 VOP_UNLOCK(dvp, 0); 1537 /* this may already have been unmounted */ 1538 if (sdp == NULL) { 1539 VN_RELE(dvp); 1540 return (0); 1541 } 1542 if (!(locked = MUTEX_HELD(&sdp->sd_lock))) 1543 mutex_enter(&sdp->sd_lock); 1544 1545 ASSERT(!vn_ismntpt(vp)); 1546 1547 sep = avl_first(&sdp->sd_snaps); 1548 while (sep != NULL) { 1549 next = AVL_NEXT(&sdp->sd_snaps, sep); 1550 1551 if (sep->se_root == vp) { 1552 avl_remove(&sdp->sd_snaps, sep); 1553 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 1554 kmem_free(sep, sizeof (zfs_snapentry_t)); 1555 break; 1556 } 1557 sep = next; 1558 } 1559 ASSERT(sep != NULL); 1560 1561 if (!locked) 1562 mutex_exit(&sdp->sd_lock); 1563 VN_RELE(dvp); 1564 1565 /* 1566 * Dispose of the vnode for the snapshot mount point. 1567 * This is safe to do because once this entry has been removed 1568 * from the AVL tree, it can't be found again, so cannot become 1569 * "active". If we lookup the same name again we will end up 1570 * creating a new vnode. 1571 */ 1572 iap.a_vp = vp; 1573 gfs_vop_reclaim(&iap); 1574 return (0); 1575 1576} 1577 1578static int 1579zfsctl_traverse_begin(vnode_t **vpp, int lktype) 1580{ 1581 1582 VN_HOLD(*vpp); 1583 /* Snapshot should be already mounted, but just in case. */ 1584 if (vn_mountedvfs(*vpp) == NULL) 1585 return (ENOENT); 1586 return (traverse(vpp, lktype)); 1587} 1588 1589static void 1590zfsctl_traverse_end(vnode_t *vp, int err) 1591{ 1592 1593 if (err == 0) 1594 vput(vp); 1595 else 1596 VN_RELE(vp); 1597} 1598 1599static int 1600zfsctl_snapshot_getattr(ap) 1601 struct vop_getattr_args /* { 1602 struct vnode *a_vp; 1603 struct vattr *a_vap; 1604 struct ucred *a_cred; 1605 } */ *ap; 1606{ 1607 vnode_t *vp = ap->a_vp; 1608 int err; 1609 1610 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY); 1611 if (err == 0) 1612 err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred); 1613 zfsctl_traverse_end(vp, err); 1614 return (err); 1615} 1616 1617static int 1618zfsctl_snapshot_fid(ap) 1619 struct vop_fid_args /* { 1620 struct vnode *a_vp; 1621 struct fid *a_fid; 1622 } */ *ap; 1623{ 1624 vnode_t *vp = ap->a_vp; 1625 int err; 1626 1627 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY); 1628 if (err == 0) 1629 err = VOP_VPTOFH(vp, (void *)ap->a_fid); 1630 zfsctl_traverse_end(vp, err); 1631 return (err); 1632} 1633 1634static int 1635zfsctl_snapshot_lookup(ap) 1636 struct vop_lookup_args /* { 1637 struct vnode *a_dvp; 1638 struct vnode **a_vpp; 1639 struct componentname *a_cnp; 1640 } */ *ap; 1641{ 1642 vnode_t *dvp = ap->a_dvp; 1643 vnode_t **vpp = ap->a_vpp; 1644 struct componentname *cnp = ap->a_cnp; 1645 cred_t *cr = ap->a_cnp->cn_cred; 1646 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 1647 int error; 1648 1649 if (cnp->cn_namelen != 2 || cnp->cn_nameptr[0] != '.' || 1650 cnp->cn_nameptr[1] != '.') { 1651 return (ENOENT); 1652 } 1653 1654 ASSERT(dvp->v_type == VDIR); 1655 ASSERT(zfsvfs->z_ctldir != NULL); 1656 1657 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", vpp, 1658 NULL, 0, NULL, cr, NULL, NULL, NULL); 1659 if (error == 0) { 1660 int ltype = VOP_ISLOCKED(dvp); 1661 VN_HOLD(*vpp); 1662 VOP_UNLOCK(dvp, 0); 1663 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 1664 VN_RELE(*vpp); 1665 vn_lock(dvp, ltype | LK_RETRY); 1666 } 1667 1668 return (error); 1669} 1670 1671static int 1672zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap) 1673{ 1674 zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data; 1675 vnode_t *dvp, *vp; 1676 zfsctl_snapdir_t *sdp; 1677 zfs_snapentry_t *sep; 1678 int error; 1679 1680 ASSERT(zfsvfs->z_ctldir != NULL); 1681 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 1682 NULL, 0, NULL, kcred, NULL, NULL, NULL); 1683 if (error != 0) 1684 return (error); 1685 sdp = dvp->v_data; 1686 1687 mutex_enter(&sdp->sd_lock); 1688 sep = avl_first(&sdp->sd_snaps); 1689 while (sep != NULL) { 1690 vp = sep->se_root; 1691 if (vp == ap->a_vp) 1692 break; 1693 sep = AVL_NEXT(&sdp->sd_snaps, sep); 1694 } 1695 if (sep == NULL) { 1696 mutex_exit(&sdp->sd_lock); 1697 error = ENOENT; 1698 } else { 1699 size_t len; 1700 1701 len = strlen(sep->se_name); 1702 *ap->a_buflen -= len; 1703 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len); 1704 mutex_exit(&sdp->sd_lock); 1705 vref(dvp); 1706 *ap->a_vpp = dvp; 1707 } 1708 VN_RELE(dvp); 1709 1710 return (error); 1711} 1712 1713static int 1714zfsctl_snaphot_print(ap) 1715 struct vop_print_args /* { 1716 struct vnode *a_vp; 1717 } */ *ap; 1718{ 1719 vnode_t *vp = ap->a_vp; 1720 zfsctl_node_t *zcp = vp->v_data; 1721 1722 printf(" .zfs/snapshot/<snap> node\n"); 1723 printf(" id = %ju\n", (uintmax_t)zcp->zc_id); 1724 zfsctl_common_print(ap); 1725 return (0); 1726} 1727 1728/* 1729 * These VP's should never see the light of day. They should always 1730 * be covered. 1731 */ 1732static struct vop_vector zfsctl_ops_snapshot = { 1733 .vop_default = &default_vnodeops, 1734 .vop_inactive = VOP_NULL, 1735 .vop_lookup = zfsctl_snapshot_lookup, 1736 .vop_reclaim = zfsctl_snapshot_reclaim, 1737 .vop_getattr = zfsctl_snapshot_getattr, 1738 .vop_fid = zfsctl_snapshot_fid, 1739 .vop_vptocnp = zfsctl_snapshot_vptocnp, 1740 .vop_print = zfsctl_snaphot_print, 1741}; 1742 1743int 1744zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp) 1745{ 1746 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1747 vnode_t *dvp, *vp; 1748 zfsctl_snapdir_t *sdp; 1749 zfsctl_node_t *zcp; 1750 zfs_snapentry_t *sep; 1751 int error; 1752 1753 ASSERT(zfsvfs->z_ctldir != NULL); 1754 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 1755 NULL, 0, NULL, kcred, NULL, NULL, NULL); 1756 if (error != 0) 1757 return (error); 1758 sdp = dvp->v_data; 1759 1760 mutex_enter(&sdp->sd_lock); 1761 sep = avl_first(&sdp->sd_snaps); 1762 while (sep != NULL) { 1763 vp = sep->se_root; 1764 zcp = vp->v_data; 1765 if (zcp->zc_id == objsetid) 1766 break; 1767 1768 sep = AVL_NEXT(&sdp->sd_snaps, sep); 1769 } 1770 1771 if (sep != NULL) { 1772 VN_HOLD(vp); 1773 /* 1774 * Return the mounted root rather than the covered mount point. 1775 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid> 1776 * and returns the ZFS vnode mounted on top of the GFS node. 1777 * This ZFS vnode is the root of the vfs for objset 'objsetid'. 1778 */ 1779 error = traverse(&vp, LK_SHARED | LK_RETRY); 1780 if (error == 0) { 1781 if (vp == sep->se_root) 1782 error = SET_ERROR(EINVAL); 1783 else 1784 *zfsvfsp = VTOZ(vp)->z_zfsvfs; 1785 } 1786 mutex_exit(&sdp->sd_lock); 1787 if (error == 0) 1788 VN_URELE(vp); 1789 else 1790 VN_RELE(vp); 1791 } else { 1792 error = SET_ERROR(EINVAL); 1793 mutex_exit(&sdp->sd_lock); 1794 } 1795 1796 VN_RELE(dvp); 1797 1798 return (error); 1799} 1800 1801/* 1802 * Unmount any snapshots for the given filesystem. This is called from 1803 * zfs_umount() - if we have a ctldir, then go through and unmount all the 1804 * snapshots. 1805 */ 1806int 1807zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr) 1808{ 1809 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1810 vnode_t *dvp; 1811 zfsctl_snapdir_t *sdp; 1812 zfs_snapentry_t *sep, *next; 1813 int error; 1814 1815 ASSERT(zfsvfs->z_ctldir != NULL); 1816 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 1817 NULL, 0, NULL, cr, NULL, NULL, NULL); 1818 if (error != 0) 1819 return (error); 1820 sdp = dvp->v_data; 1821 1822 mutex_enter(&sdp->sd_lock); 1823 1824 sep = avl_first(&sdp->sd_snaps); 1825 while (sep != NULL) { 1826 next = AVL_NEXT(&sdp->sd_snaps, sep); 1827 1828 /* 1829 * If this snapshot is not mounted, then it must 1830 * have just been unmounted by somebody else, and 1831 * will be cleaned up by zfsctl_snapdir_inactive(). 1832 */ 1833 if (vn_ismntpt(sep->se_root)) { 1834 error = zfsctl_unmount_snap(sep, fflags, cr); 1835 if (error) { 1836 avl_index_t where; 1837 1838 /* 1839 * Before reinserting snapshot to the tree, 1840 * check if it was actually removed. For example 1841 * when snapshot mount point is busy, we will 1842 * have an error here, but there will be no need 1843 * to reinsert snapshot. 1844 */ 1845 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL) 1846 avl_insert(&sdp->sd_snaps, sep, where); 1847 break; 1848 } 1849 } 1850 sep = next; 1851 } 1852 1853 mutex_exit(&sdp->sd_lock); 1854 VN_RELE(dvp); 1855 1856 return (error); 1857} 1858