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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * ZFS control directory (a.k.a. ".zfs") 30 * 31 * This directory provides a common location for all ZFS meta-objects. 32 * Currently, this is only the 'snapshot' directory, but this may expand in the 33 * future. The elements are built using the GFS primitives, as the hierarchy 34 * does not actually exist on disk. 35 * 36 * For 'snapshot', we don't want to have all snapshots always mounted, because 37 * this would take up a huge amount of space in /etc/mnttab. We have three 38 * types of objects: 39 * 40 * ctldir ------> snapshotdir -------> snapshot 41 * | 42 * | 43 * V 44 * mounted fs 45 * 46 * The 'snapshot' node contains just enough information to lookup '..' and act 47 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we 48 * perform an automount of the underlying filesystem and return the 49 * corresponding vnode. 50 * 51 * All mounts are handled automatically by the kernel, but unmounts are 52 * (currently) handled from user land. The main reason is that there is no 53 * reliable way to auto-unmount the filesystem when it's "no longer in use". 54 * When the user unmounts a filesystem, we call zfsctl_unmount(), which 55 * unmounts any snapshots within the snapshot directory. 56 * 57 * The '.zfs', '.zfs/snapshot', and all directories created under 58 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and 59 * share the same vfs_t as the head filesystem (what '.zfs' lives under). 60 * 61 * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>' 62 * (ie: snapshots) are ZFS nodes and have their own unique vfs_t. 63 * However, vnodes within these mounted on file systems have their v_vfsp 64 * fields set to the head filesystem to make NFS happy (see 65 * zfsctl_snapdir_lookup()). 66 */ 67 68 #include <fs/fs_subr.h> 69 #include <sys/zfs_ctldir.h> 70 #include <sys/zfs_ioctl.h> 71 #include <sys/zfs_vfsops.h> 72 #include <sys/vfs_opreg.h> 73 #include <sys/gfs.h> 74 #include <sys/stat.h> 75 #include <sys/dmu.h> 76 #include <sys/dsl_deleg.h> 77 #include <sys/mount.h> 78 79 typedef struct { 80 char *se_name; 81 vnode_t *se_root; 82 avl_node_t se_node; 83 } zfs_snapentry_t; 84 85 static int 86 snapentry_compare(const void *a, const void *b) 87 { 88 const zfs_snapentry_t *sa = a; 89 const zfs_snapentry_t *sb = b; 90 int ret = strcmp(sa->se_name, sb->se_name); 91 92 if (ret < 0) 93 return (-1); 94 else if (ret > 0) 95 return (1); 96 else 97 return (0); 98 } 99 100 vnodeops_t *zfsctl_ops_root; 101 vnodeops_t *zfsctl_ops_snapdir; 102 vnodeops_t *zfsctl_ops_snapshot; 103 104 static const fs_operation_def_t zfsctl_tops_root[]; 105 static const fs_operation_def_t zfsctl_tops_snapdir[]; 106 static const fs_operation_def_t zfsctl_tops_snapshot[]; 107 108 static vnode_t *zfsctl_mknode_snapdir(vnode_t *); 109 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset); 110 111 static gfs_opsvec_t zfsctl_opsvec[] = { 112 { ".zfs", zfsctl_tops_root, &zfsctl_ops_root }, 113 { ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir }, 114 { ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot }, 115 { NULL } 116 }; 117 118 typedef struct zfsctl_node { 119 gfs_dir_t zc_gfs_private; 120 uint64_t zc_id; 121 timestruc_t zc_cmtime; /* ctime and mtime, always the same */ 122 } zfsctl_node_t; 123 124 typedef struct zfsctl_snapdir { 125 zfsctl_node_t sd_node; 126 kmutex_t sd_lock; 127 avl_tree_t sd_snaps; 128 } zfsctl_snapdir_t; 129 130 /* 131 * Root directory elements. We have only a single static entry, 'snapshot'. 132 */ 133 static gfs_dirent_t zfsctl_root_entries[] = { 134 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE }, 135 { NULL } 136 }; 137 138 /* include . and .. in the calculation */ 139 #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \ 140 sizeof (gfs_dirent_t)) + 1) 141 142 143 /* 144 * Initialize the various GFS pieces we'll need to create and manipulate .zfs 145 * directories. This is called from the ZFS init routine, and initializes the 146 * vnode ops vectors that we'll be using. 147 */ 148 void 149 zfsctl_init(void) 150 { 151 VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0); 152 } 153 154 void 155 zfsctl_fini(void) 156 { 157 /* 158 * Remove vfsctl vnode ops 159 */ 160 if (zfsctl_ops_root) 161 vn_freevnodeops(zfsctl_ops_root); 162 if (zfsctl_ops_snapdir) 163 vn_freevnodeops(zfsctl_ops_snapdir); 164 if (zfsctl_ops_snapshot) 165 vn_freevnodeops(zfsctl_ops_snapshot); 166 167 zfsctl_ops_root = NULL; 168 zfsctl_ops_snapdir = NULL; 169 zfsctl_ops_snapshot = NULL; 170 } 171 172 /* 173 * Return the inode number associated with the 'snapshot' directory. 174 */ 175 /* ARGSUSED */ 176 static ino64_t 177 zfsctl_root_inode_cb(vnode_t *vp, int index) 178 { 179 ASSERT(index == 0); 180 return (ZFSCTL_INO_SNAPDIR); 181 } 182 183 /* 184 * Create the '.zfs' directory. This directory is cached as part of the VFS 185 * structure. This results in a hold on the vfs_t. The code in zfs_umount() 186 * therefore checks against a vfs_count of 2 instead of 1. This reference 187 * is removed when the ctldir is destroyed in the unmount. 188 */ 189 void 190 zfsctl_create(zfsvfs_t *zfsvfs) 191 { 192 vnode_t *vp, *rvp; 193 zfsctl_node_t *zcp; 194 195 ASSERT(zfsvfs->z_ctldir == NULL); 196 197 vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs, 198 zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries, 199 zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL); 200 zcp = vp->v_data; 201 zcp->zc_id = ZFSCTL_INO_ROOT; 202 203 VERIFY(VFS_ROOT(zfsvfs->z_vfs, &rvp) == 0); 204 ZFS_TIME_DECODE(&zcp->zc_cmtime, VTOZ(rvp)->z_phys->zp_crtime); 205 VN_RELE(rvp); 206 207 /* 208 * We're only faking the fact that we have a root of a filesystem for 209 * the sake of the GFS interfaces. Undo the flag manipulation it did 210 * for us. 211 */ 212 vp->v_flag &= ~(VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT); 213 214 zfsvfs->z_ctldir = vp; 215 } 216 217 /* 218 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted. 219 * There might still be more references if we were force unmounted, but only 220 * new zfs_inactive() calls can occur and they don't reference .zfs 221 */ 222 void 223 zfsctl_destroy(zfsvfs_t *zfsvfs) 224 { 225 VN_RELE(zfsvfs->z_ctldir); 226 zfsvfs->z_ctldir = NULL; 227 } 228 229 /* 230 * Given a root znode, retrieve the associated .zfs directory. 231 * Add a hold to the vnode and return it. 232 */ 233 vnode_t * 234 zfsctl_root(znode_t *zp) 235 { 236 ASSERT(zfs_has_ctldir(zp)); 237 VN_HOLD(zp->z_zfsvfs->z_ctldir); 238 return (zp->z_zfsvfs->z_ctldir); 239 } 240 241 /* 242 * Common open routine. Disallow any write access. 243 */ 244 /* ARGSUSED */ 245 static int 246 zfsctl_common_open(vnode_t **vpp, int flags, cred_t *cr) 247 { 248 if (flags & FWRITE) 249 return (EACCES); 250 251 return (0); 252 } 253 254 /* 255 * Common close routine. Nothing to do here. 256 */ 257 /* ARGSUSED */ 258 static int 259 zfsctl_common_close(vnode_t *vpp, int flags, int count, offset_t off, 260 cred_t *cr) 261 { 262 return (0); 263 } 264 265 /* 266 * Common access routine. Disallow writes. 267 */ 268 /* ARGSUSED */ 269 static int 270 zfsctl_common_access(vnode_t *vp, int mode, int flags, cred_t *cr) 271 { 272 if (mode & VWRITE) 273 return (EACCES); 274 275 return (0); 276 } 277 278 /* 279 * Common getattr function. Fill in basic information. 280 */ 281 static void 282 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap) 283 { 284 zfsctl_node_t *zcp = vp->v_data; 285 timestruc_t now; 286 287 vap->va_uid = 0; 288 vap->va_gid = 0; 289 vap->va_rdev = 0; 290 /* 291 * We are a purly virtual object, so we have no 292 * blocksize or allocated blocks. 293 */ 294 vap->va_blksize = 0; 295 vap->va_nblocks = 0; 296 vap->va_seq = 0; 297 vap->va_fsid = vp->v_vfsp->vfs_dev; 298 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | 299 S_IROTH | S_IXOTH; 300 vap->va_type = VDIR; 301 /* 302 * We live in the now (for atime). 303 */ 304 gethrestime(&now); 305 vap->va_atime = now; 306 vap->va_mtime = vap->va_ctime = zcp->zc_cmtime; 307 } 308 309 static int 310 zfsctl_common_fid(vnode_t *vp, fid_t *fidp) 311 { 312 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 313 zfsctl_node_t *zcp = vp->v_data; 314 uint64_t object = zcp->zc_id; 315 zfid_short_t *zfid; 316 int i; 317 318 ZFS_ENTER(zfsvfs); 319 320 if (fidp->fid_len < SHORT_FID_LEN) { 321 fidp->fid_len = SHORT_FID_LEN; 322 ZFS_EXIT(zfsvfs); 323 return (ENOSPC); 324 } 325 326 zfid = (zfid_short_t *)fidp; 327 328 zfid->zf_len = SHORT_FID_LEN; 329 330 for (i = 0; i < sizeof (zfid->zf_object); i++) 331 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 332 333 /* .zfs znodes always have a generation number of 0 */ 334 for (i = 0; i < sizeof (zfid->zf_gen); i++) 335 zfid->zf_gen[i] = 0; 336 337 ZFS_EXIT(zfsvfs); 338 return (0); 339 } 340 341 /* 342 * .zfs inode namespace 343 * 344 * We need to generate unique inode numbers for all files and directories 345 * within the .zfs pseudo-filesystem. We use the following scheme: 346 * 347 * ENTRY ZFSCTL_INODE 348 * .zfs 1 349 * .zfs/snapshot 2 350 * .zfs/snapshot/<snap> objectid(snap) 351 */ 352 353 #define ZFSCTL_INO_SNAP(id) (id) 354 355 /* 356 * Get root directory attributes. 357 */ 358 /* ARGSUSED */ 359 static int 360 zfsctl_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 361 { 362 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 363 364 ZFS_ENTER(zfsvfs); 365 vap->va_nodeid = ZFSCTL_INO_ROOT; 366 vap->va_nlink = vap->va_size = NROOT_ENTRIES; 367 368 zfsctl_common_getattr(vp, vap); 369 ZFS_EXIT(zfsvfs); 370 371 return (0); 372 } 373 374 /* 375 * Special case the handling of "..". 376 */ 377 /* ARGSUSED */ 378 int 379 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp, 380 int flags, vnode_t *rdir, cred_t *cr) 381 { 382 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 383 int err; 384 385 ZFS_ENTER(zfsvfs); 386 387 if (strcmp(nm, "..") == 0) { 388 err = VFS_ROOT(dvp->v_vfsp, vpp); 389 } else { 390 err = gfs_dir_lookup(dvp, nm, vpp); 391 } 392 393 ZFS_EXIT(zfsvfs); 394 395 return (err); 396 } 397 398 static const fs_operation_def_t zfsctl_tops_root[] = { 399 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 400 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 401 { VOPNAME_IOCTL, { .error = fs_inval } }, 402 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } }, 403 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 404 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } }, 405 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } }, 406 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 407 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 408 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } }, 409 { NULL } 410 }; 411 412 static int 413 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname) 414 { 415 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 416 417 dmu_objset_name(os, zname); 418 if (strlen(zname) + 1 + strlen(name) >= len) 419 return (ENAMETOOLONG); 420 (void) strcat(zname, "@"); 421 (void) strcat(zname, name); 422 return (0); 423 } 424 425 int 426 zfsctl_unmount_snap(vnode_t *dvp, const char *name, int force, cred_t *cr) 427 { 428 zfsctl_snapdir_t *sdp = dvp->v_data; 429 zfs_snapentry_t search, *sep; 430 avl_index_t where; 431 int err; 432 433 ASSERT(MUTEX_HELD(&sdp->sd_lock)); 434 435 search.se_name = (char *)name; 436 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) 437 return (ENOENT); 438 439 ASSERT(vn_ismntpt(sep->se_root)); 440 441 /* this will be dropped by dounmount() */ 442 if ((err = vn_vfswlock(sep->se_root)) != 0) 443 return (err); 444 445 VN_HOLD(sep->se_root); 446 err = dounmount(vn_mountedvfs(sep->se_root), force, kcred); 447 if (err) { 448 VN_RELE(sep->se_root); 449 return (err); 450 } 451 ASSERT(sep->se_root->v_count == 1); 452 gfs_vop_inactive(sep->se_root, cr); 453 454 avl_remove(&sdp->sd_snaps, sep); 455 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 456 kmem_free(sep, sizeof (zfs_snapentry_t)); 457 458 return (0); 459 } 460 461 462 static void 463 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm) 464 { 465 avl_index_t where; 466 vfs_t *vfsp; 467 refstr_t *pathref; 468 char newpath[MAXNAMELEN]; 469 char *tail; 470 471 ASSERT(MUTEX_HELD(&sdp->sd_lock)); 472 ASSERT(sep != NULL); 473 474 vfsp = vn_mountedvfs(sep->se_root); 475 ASSERT(vfsp != NULL); 476 477 vfs_lock_wait(vfsp); 478 479 /* 480 * Change the name in the AVL tree. 481 */ 482 avl_remove(&sdp->sd_snaps, sep); 483 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 484 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP); 485 (void) strcpy(sep->se_name, nm); 486 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL); 487 avl_insert(&sdp->sd_snaps, sep, where); 488 489 /* 490 * Change the current mountpoint info: 491 * - update the tail of the mntpoint path 492 * - update the tail of the resource path 493 */ 494 pathref = vfs_getmntpoint(vfsp); 495 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath)); 496 VERIFY((tail = strrchr(newpath, '/')) != NULL); 497 *(tail+1) = '\0'; 498 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath)); 499 (void) strcat(newpath, nm); 500 refstr_rele(pathref); 501 vfs_setmntpoint(vfsp, newpath); 502 503 pathref = vfs_getresource(vfsp); 504 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath)); 505 VERIFY((tail = strrchr(newpath, '@')) != NULL); 506 *(tail+1) = '\0'; 507 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath)); 508 (void) strcat(newpath, nm); 509 refstr_rele(pathref); 510 vfs_setresource(vfsp, newpath); 511 512 vfs_unlock(vfsp); 513 } 514 515 static int 516 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, 517 cred_t *cr) 518 { 519 zfsctl_snapdir_t *sdp = sdvp->v_data; 520 zfs_snapentry_t search, *sep; 521 avl_index_t where; 522 char from[MAXNAMELEN], to[MAXNAMELEN]; 523 int err; 524 525 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from); 526 if (err) 527 return (err); 528 529 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to); 530 if (err) 531 return (err); 532 533 if (err = zfs_secpolicy_rename_perms(from, to, cr)) 534 return (err); 535 /* 536 * Cannot move snapshots out of the snapdir. 537 */ 538 if (sdvp != tdvp) 539 return (EINVAL); 540 541 if (strcmp(snm, tnm) == 0) 542 return (0); 543 544 mutex_enter(&sdp->sd_lock); 545 546 search.se_name = (char *)snm; 547 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) { 548 mutex_exit(&sdp->sd_lock); 549 return (ENOENT); 550 } 551 552 err = dmu_objset_rename(from, to, B_FALSE); 553 if (err == 0) 554 zfsctl_rename_snap(sdp, sep, tnm); 555 556 mutex_exit(&sdp->sd_lock); 557 558 return (err); 559 } 560 561 /* ARGSUSED */ 562 static int 563 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr) 564 { 565 zfsctl_snapdir_t *sdp = dvp->v_data; 566 char snapname[MAXNAMELEN]; 567 int err; 568 569 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname); 570 if (err) 571 return (err); 572 573 if (err = zfs_secpolicy_destroy_perms(snapname, cr)) 574 return (err); 575 576 mutex_enter(&sdp->sd_lock); 577 578 err = zfsctl_unmount_snap(dvp, name, MS_FORCE, cr); 579 if (err) { 580 mutex_exit(&sdp->sd_lock); 581 return (err); 582 } 583 584 err = dmu_objset_destroy(snapname); 585 586 mutex_exit(&sdp->sd_lock); 587 588 return (err); 589 } 590 591 /* 592 * This creates a snapshot under '.zfs/snapshot'. 593 */ 594 /* ARGSUSED */ 595 static int 596 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, 597 cred_t *cr) 598 { 599 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 600 char name[MAXNAMELEN]; 601 int err; 602 static enum symfollow follow = NO_FOLLOW; 603 static enum uio_seg seg = UIO_SYSSPACE; 604 605 dmu_objset_name(zfsvfs->z_os, name); 606 607 *vpp = NULL; 608 609 err = zfs_secpolicy_snapshot_perms(name, cr); 610 if (err) 611 return (err); 612 613 if (err == 0) { 614 err = dmu_objset_snapshot(name, dirname, B_FALSE); 615 if (err) 616 return (err); 617 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp); 618 } 619 620 return (err); 621 } 622 623 /* 624 * Lookup entry point for the 'snapshot' directory. Try to open the 625 * snapshot if it exist, creating the pseudo filesystem vnode as necessary. 626 * Perform a mount of the associated dataset on top of the vnode. 627 */ 628 /* ARGSUSED */ 629 static int 630 zfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp, 631 int flags, vnode_t *rdir, cred_t *cr) 632 { 633 zfsctl_snapdir_t *sdp = dvp->v_data; 634 objset_t *snap; 635 char snapname[MAXNAMELEN]; 636 char *mountpoint; 637 zfs_snapentry_t *sep, search; 638 struct mounta margs; 639 vfs_t *vfsp; 640 size_t mountpoint_len; 641 avl_index_t where; 642 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 643 int err; 644 645 ASSERT(dvp->v_type == VDIR); 646 647 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) 648 return (0); 649 650 /* 651 * If we get a recursive call, that means we got called 652 * from the domount() code while it was trying to look up the 653 * spec (which looks like a local path for zfs). We need to 654 * add some flag to domount() to tell it not to do this lookup. 655 */ 656 if (MUTEX_HELD(&sdp->sd_lock)) 657 return (ENOENT); 658 659 ZFS_ENTER(zfsvfs); 660 661 mutex_enter(&sdp->sd_lock); 662 search.se_name = (char *)nm; 663 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) { 664 *vpp = sep->se_root; 665 VN_HOLD(*vpp); 666 err = traverse(vpp); 667 if (err) { 668 VN_RELE(*vpp); 669 *vpp = NULL; 670 } else if (*vpp == sep->se_root) { 671 /* 672 * The snapshot was unmounted behind our backs, 673 * try to remount it. 674 */ 675 goto domount; 676 } 677 mutex_exit(&sdp->sd_lock); 678 ZFS_EXIT(zfsvfs); 679 return (err); 680 } 681 682 /* 683 * The requested snapshot is not currently mounted, look it up. 684 */ 685 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname); 686 if (err) { 687 mutex_exit(&sdp->sd_lock); 688 ZFS_EXIT(zfsvfs); 689 return (err); 690 } 691 if (dmu_objset_open(snapname, DMU_OST_ZFS, 692 DS_MODE_STANDARD | DS_MODE_READONLY, &snap) != 0) { 693 mutex_exit(&sdp->sd_lock); 694 ZFS_EXIT(zfsvfs); 695 return (ENOENT); 696 } 697 698 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP); 699 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP); 700 (void) strcpy(sep->se_name, nm); 701 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap)); 702 avl_insert(&sdp->sd_snaps, sep, where); 703 704 dmu_objset_close(snap); 705 domount: 706 mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) + 707 strlen("/.zfs/snapshot/") + strlen(nm) + 1; 708 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP); 709 (void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s", 710 refstr_value(dvp->v_vfsp->vfs_mntpt), nm); 711 712 margs.spec = snapname; 713 margs.dir = mountpoint; 714 margs.flags = MS_SYSSPACE | MS_NOMNTTAB; 715 margs.fstype = "zfs"; 716 margs.dataptr = NULL; 717 margs.datalen = 0; 718 margs.optptr = NULL; 719 margs.optlen = 0; 720 721 err = domount("zfs", &margs, *vpp, kcred, &vfsp); 722 kmem_free(mountpoint, mountpoint_len); 723 724 if (err == 0) { 725 /* 726 * Return the mounted root rather than the covered mount point. 727 * Takes the GFS vnode at .zfs/snapshot/<snapname> and returns 728 * the ZFS vnode mounted on top of the GFS node. This ZFS 729 * vnode is the root the newly created vfsp. 730 */ 731 VFS_RELE(vfsp); 732 err = traverse(vpp); 733 } 734 735 if (err == 0) { 736 /* 737 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>. 738 * 739 * This is where we lie about our v_vfsp in order to 740 * make .zfs/snapshot/<snapname> accessible over NFS 741 * without requiring manual mounts of <snapname>. 742 */ 743 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs); 744 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs; 745 (*vpp)->v_vfsp = zfsvfs->z_vfs; 746 (*vpp)->v_flag &= ~VROOT; 747 } 748 mutex_exit(&sdp->sd_lock); 749 ZFS_EXIT(zfsvfs); 750 751 /* 752 * If we had an error, drop our hold on the vnode and 753 * zfsctl_snapshot_inactive() will clean up. 754 */ 755 if (err) { 756 VN_RELE(*vpp); 757 *vpp = NULL; 758 } 759 return (err); 760 } 761 762 /* ARGSUSED */ 763 static int 764 zfsctl_snapdir_readdir_cb(vnode_t *vp, struct dirent64 *dp, int *eofp, 765 offset_t *offp, offset_t *nextp, void *data) 766 { 767 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 768 char snapname[MAXNAMELEN]; 769 uint64_t id, cookie; 770 771 ZFS_ENTER(zfsvfs); 772 773 cookie = *offp; 774 if (dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id, 775 &cookie) == ENOENT) { 776 *eofp = 1; 777 ZFS_EXIT(zfsvfs); 778 return (0); 779 } 780 781 (void) strcpy(dp->d_name, snapname); 782 dp->d_ino = ZFSCTL_INO_SNAP(id); 783 *nextp = cookie; 784 785 ZFS_EXIT(zfsvfs); 786 787 return (0); 788 } 789 790 /* 791 * pvp is the '.zfs' directory (zfsctl_node_t). 792 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t). 793 * 794 * This function is the callback to create a GFS vnode for '.zfs/snapshot' 795 * when a lookup is performed on .zfs for "snapshot". 796 */ 797 vnode_t * 798 zfsctl_mknode_snapdir(vnode_t *pvp) 799 { 800 vnode_t *vp; 801 zfsctl_snapdir_t *sdp; 802 803 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, 804 zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN, 805 zfsctl_snapdir_readdir_cb, NULL); 806 sdp = vp->v_data; 807 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR; 808 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime; 809 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL); 810 avl_create(&sdp->sd_snaps, snapentry_compare, 811 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node)); 812 return (vp); 813 } 814 815 /* ARGSUSED */ 816 static int 817 zfsctl_snapdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 818 { 819 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 820 zfsctl_snapdir_t *sdp = vp->v_data; 821 822 ZFS_ENTER(zfsvfs); 823 zfsctl_common_getattr(vp, vap); 824 vap->va_nodeid = gfs_file_inode(vp); 825 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2; 826 ZFS_EXIT(zfsvfs); 827 828 return (0); 829 } 830 831 /* ARGSUSED */ 832 static void 833 zfsctl_snapdir_inactive(vnode_t *vp, cred_t *cr) 834 { 835 zfsctl_snapdir_t *sdp = vp->v_data; 836 void *private; 837 838 private = gfs_dir_inactive(vp); 839 if (private != NULL) { 840 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0); 841 mutex_destroy(&sdp->sd_lock); 842 avl_destroy(&sdp->sd_snaps); 843 kmem_free(private, sizeof (zfsctl_snapdir_t)); 844 } 845 } 846 847 static const fs_operation_def_t zfsctl_tops_snapdir[] = { 848 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 849 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 850 { VOPNAME_IOCTL, { .error = fs_inval } }, 851 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_snapdir_getattr } }, 852 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 853 { VOPNAME_RENAME, { .vop_rename = zfsctl_snapdir_rename } }, 854 { VOPNAME_RMDIR, { .vop_rmdir = zfsctl_snapdir_remove } }, 855 { VOPNAME_MKDIR, { .vop_mkdir = zfsctl_snapdir_mkdir } }, 856 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } }, 857 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_snapdir_lookup } }, 858 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 859 { VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapdir_inactive } }, 860 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } }, 861 { NULL } 862 }; 863 864 /* 865 * pvp is the GFS vnode '.zfs/snapshot'. 866 * 867 * This creates a GFS node under '.zfs/snapshot' representing each 868 * snapshot. This newly created GFS node is what we mount snapshot 869 * vfs_t's ontop of. 870 */ 871 static vnode_t * 872 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset) 873 { 874 vnode_t *vp; 875 zfsctl_node_t *zcp; 876 877 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, 878 zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL); 879 zcp = vp->v_data; 880 zcp->zc_id = objset; 881 882 return (vp); 883 } 884 885 static void 886 zfsctl_snapshot_inactive(vnode_t *vp, cred_t *cr) 887 { 888 zfsctl_snapdir_t *sdp; 889 zfs_snapentry_t *sep, *next; 890 vnode_t *dvp; 891 892 VERIFY(gfs_dir_lookup(vp, "..", &dvp) == 0); 893 sdp = dvp->v_data; 894 895 mutex_enter(&sdp->sd_lock); 896 897 if (vp->v_count > 1) { 898 mutex_exit(&sdp->sd_lock); 899 return; 900 } 901 ASSERT(!vn_ismntpt(vp)); 902 903 sep = avl_first(&sdp->sd_snaps); 904 while (sep != NULL) { 905 next = AVL_NEXT(&sdp->sd_snaps, sep); 906 907 if (sep->se_root == vp) { 908 avl_remove(&sdp->sd_snaps, sep); 909 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 910 kmem_free(sep, sizeof (zfs_snapentry_t)); 911 break; 912 } 913 sep = next; 914 } 915 ASSERT(sep != NULL); 916 917 mutex_exit(&sdp->sd_lock); 918 VN_RELE(dvp); 919 920 /* 921 * Dispose of the vnode for the snapshot mount point. 922 * This is safe to do because once this entry has been removed 923 * from the AVL tree, it can't be found again, so cannot become 924 * "active". If we lookup the same name again we will end up 925 * creating a new vnode. 926 */ 927 gfs_vop_inactive(vp, cr); 928 } 929 930 931 /* 932 * These VP's should never see the light of day. They should always 933 * be covered. 934 */ 935 static const fs_operation_def_t zfsctl_tops_snapshot[] = { 936 VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapshot_inactive }, 937 NULL, NULL 938 }; 939 940 int 941 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp) 942 { 943 zfsvfs_t *zfsvfs = vfsp->vfs_data; 944 vnode_t *dvp, *vp; 945 zfsctl_snapdir_t *sdp; 946 zfsctl_node_t *zcp; 947 zfs_snapentry_t *sep; 948 int error; 949 950 ASSERT(zfsvfs->z_ctldir != NULL); 951 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 952 NULL, 0, NULL, kcred); 953 if (error != 0) 954 return (error); 955 sdp = dvp->v_data; 956 957 mutex_enter(&sdp->sd_lock); 958 sep = avl_first(&sdp->sd_snaps); 959 while (sep != NULL) { 960 vp = sep->se_root; 961 zcp = vp->v_data; 962 if (zcp->zc_id == objsetid) 963 break; 964 965 sep = AVL_NEXT(&sdp->sd_snaps, sep); 966 } 967 968 if (sep != NULL) { 969 VN_HOLD(vp); 970 /* 971 * Return the mounted root rather than the covered mount point. 972 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid> 973 * and returns the ZFS vnode mounted on top of the GFS node. 974 * This ZFS vnode is the root of the vfs for objset 'objsetid'. 975 */ 976 error = traverse(&vp); 977 if (error == 0) { 978 if (vp == sep->se_root) 979 error = EINVAL; 980 else 981 *zfsvfsp = VTOZ(vp)->z_zfsvfs; 982 } 983 mutex_exit(&sdp->sd_lock); 984 VN_RELE(vp); 985 } else { 986 error = EINVAL; 987 mutex_exit(&sdp->sd_lock); 988 } 989 990 VN_RELE(dvp); 991 992 return (error); 993 } 994 995 /* 996 * Unmount any snapshots for the given filesystem. This is called from 997 * zfs_umount() - if we have a ctldir, then go through and unmount all the 998 * snapshots. 999 */ 1000 int 1001 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr) 1002 { 1003 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1004 vnode_t *dvp, *svp; 1005 zfsctl_snapdir_t *sdp; 1006 zfs_snapentry_t *sep, *next; 1007 int error; 1008 1009 ASSERT(zfsvfs->z_ctldir != NULL); 1010 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 1011 NULL, 0, NULL, cr); 1012 if (error != 0) 1013 return (error); 1014 sdp = dvp->v_data; 1015 1016 mutex_enter(&sdp->sd_lock); 1017 1018 sep = avl_first(&sdp->sd_snaps); 1019 while (sep != NULL) { 1020 svp = sep->se_root; 1021 next = AVL_NEXT(&sdp->sd_snaps, sep); 1022 1023 /* 1024 * If this snapshot is not mounted, then it must 1025 * have just been unmounted by somebody else, and 1026 * will be cleaned up by zfsctl_snapdir_inactive(). 1027 */ 1028 if (vn_ismntpt(svp)) { 1029 if ((error = vn_vfswlock(svp)) != 0) 1030 goto out; 1031 1032 VN_HOLD(svp); 1033 error = dounmount(vn_mountedvfs(svp), fflags, cr); 1034 if (error) { 1035 VN_RELE(svp); 1036 goto out; 1037 } 1038 1039 avl_remove(&sdp->sd_snaps, sep); 1040 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 1041 kmem_free(sep, sizeof (zfs_snapentry_t)); 1042 1043 /* 1044 * We can't use VN_RELE(), as that will try to 1045 * invoke zfsctl_snapdir_inactive(), and that 1046 * would lead to an attempt to re-grab the sd_lock. 1047 */ 1048 ASSERT3U(svp->v_count, ==, 1); 1049 gfs_vop_inactive(svp, cr); 1050 } 1051 sep = next; 1052 } 1053 out: 1054 mutex_exit(&sdp->sd_lock); 1055 VN_RELE(dvp); 1056 1057 return (error); 1058 } 1059