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