zfs_ctldir.c revision 330071
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) 2012, 2015 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/stat.h> 74#include <sys/dmu.h> 75#include <sys/dsl_dataset.h> 76#include <sys/dsl_destroy.h> 77#include <sys/dsl_deleg.h> 78#include <sys/mount.h> 79#include <sys/zap.h> 80 81#include "zfs_namecheck.h" 82 83/* 84 * "Synthetic" filesystem implementation. 85 */ 86 87/* 88 * Assert that A implies B. 89 */ 90#define KASSERT_IMPLY(A, B, msg) KASSERT(!(A) || (B), (msg)); 91 92static MALLOC_DEFINE(M_SFSNODES, "sfs_nodes", "synthetic-fs nodes"); 93 94typedef struct sfs_node { 95 char sn_name[ZFS_MAX_DATASET_NAME_LEN]; 96 uint64_t sn_parent_id; 97 uint64_t sn_id; 98} sfs_node_t; 99 100/* 101 * Check the parent's ID as well as the node's to account for a chance 102 * that IDs originating from different domains (snapshot IDs, artifical 103 * IDs, znode IDs) may clash. 104 */ 105static int 106sfs_compare_ids(struct vnode *vp, void *arg) 107{ 108 sfs_node_t *n1 = vp->v_data; 109 sfs_node_t *n2 = arg; 110 bool equal; 111 112 equal = n1->sn_id == n2->sn_id && 113 n1->sn_parent_id == n2->sn_parent_id; 114 115 /* Zero means equality. */ 116 return (!equal); 117} 118 119static int 120sfs_vnode_get(const struct mount *mp, int flags, uint64_t parent_id, 121 uint64_t id, struct vnode **vpp) 122{ 123 sfs_node_t search; 124 int err; 125 126 search.sn_id = id; 127 search.sn_parent_id = parent_id; 128 err = vfs_hash_get(mp, (u_int)id, flags, curthread, vpp, 129 sfs_compare_ids, &search); 130 return (err); 131} 132 133static int 134sfs_vnode_insert(struct vnode *vp, int flags, uint64_t parent_id, 135 uint64_t id, struct vnode **vpp) 136{ 137 int err; 138 139 KASSERT(vp->v_data != NULL, ("sfs_vnode_insert with NULL v_data")); 140 err = vfs_hash_insert(vp, (u_int)id, flags, curthread, vpp, 141 sfs_compare_ids, vp->v_data); 142 return (err); 143} 144 145static void 146sfs_vnode_remove(struct vnode *vp) 147{ 148 vfs_hash_remove(vp); 149} 150 151typedef void sfs_vnode_setup_fn(vnode_t *vp, void *arg); 152 153static int 154sfs_vgetx(struct mount *mp, int flags, uint64_t parent_id, uint64_t id, 155 const char *tag, struct vop_vector *vops, 156 sfs_vnode_setup_fn setup, void *arg, 157 struct vnode **vpp) 158{ 159 struct vnode *vp; 160 int error; 161 162 error = sfs_vnode_get(mp, flags, parent_id, id, vpp); 163 if (error != 0 || *vpp != NULL) { 164 KASSERT_IMPLY(error == 0, (*vpp)->v_data != NULL, 165 "sfs vnode with no data"); 166 return (error); 167 } 168 169 /* Allocate a new vnode/inode. */ 170 error = getnewvnode(tag, mp, vops, &vp); 171 if (error != 0) { 172 *vpp = NULL; 173 return (error); 174 } 175 176 /* 177 * Exclusively lock the vnode vnode while it's being constructed. 178 */ 179 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL); 180 error = insmntque(vp, mp); 181 if (error != 0) { 182 *vpp = NULL; 183 return (error); 184 } 185 186 setup(vp, arg); 187 188 error = sfs_vnode_insert(vp, flags, parent_id, id, vpp); 189 if (error != 0 || *vpp != NULL) { 190 KASSERT_IMPLY(error == 0, (*vpp)->v_data != NULL, 191 "sfs vnode with no data"); 192 return (error); 193 } 194 195 *vpp = vp; 196 return (0); 197} 198 199static void 200sfs_print_node(sfs_node_t *node) 201{ 202 printf("\tname = %s\n", node->sn_name); 203 printf("\tparent_id = %ju\n", (uintmax_t)node->sn_parent_id); 204 printf("\tid = %ju\n", (uintmax_t)node->sn_id); 205} 206 207static sfs_node_t * 208sfs_alloc_node(size_t size, const char *name, uint64_t parent_id, uint64_t id) 209{ 210 struct sfs_node *node; 211 212 KASSERT(strlen(name) < sizeof(node->sn_name), 213 ("sfs node name is too long")); 214 KASSERT(size >= sizeof(*node), ("sfs node size is too small")); 215 node = malloc(size, M_SFSNODES, M_WAITOK | M_ZERO); 216 strlcpy(node->sn_name, name, sizeof(node->sn_name)); 217 node->sn_parent_id = parent_id; 218 node->sn_id = id; 219 220 return (node); 221} 222 223static void 224sfs_destroy_node(sfs_node_t *node) 225{ 226 free(node, M_SFSNODES); 227} 228 229static void * 230sfs_reclaim_vnode(vnode_t *vp) 231{ 232 sfs_node_t *node; 233 void *data; 234 235 sfs_vnode_remove(vp); 236 data = vp->v_data; 237 vp->v_data = NULL; 238 return (data); 239} 240 241static int 242sfs_readdir_common(uint64_t parent_id, uint64_t id, struct vop_readdir_args *ap, 243 uio_t *uio, off_t *offp) 244{ 245 struct dirent entry; 246 int error; 247 248 /* Reset ncookies for subsequent use of vfs_read_dirent. */ 249 if (ap->a_ncookies != NULL) 250 *ap->a_ncookies = 0; 251 252 if (uio->uio_resid < sizeof(entry)) 253 return (SET_ERROR(EINVAL)); 254 255 if (uio->uio_offset < 0) 256 return (SET_ERROR(EINVAL)); 257 if (uio->uio_offset == 0) { 258 entry.d_fileno = id; 259 entry.d_type = DT_DIR; 260 entry.d_name[0] = '.'; 261 entry.d_name[1] = '\0'; 262 entry.d_namlen = 1; 263 entry.d_reclen = sizeof(entry); 264 error = vfs_read_dirent(ap, &entry, uio->uio_offset); 265 if (error != 0) 266 return (SET_ERROR(error)); 267 } 268 269 if (uio->uio_offset < sizeof(entry)) 270 return (SET_ERROR(EINVAL)); 271 if (uio->uio_offset == sizeof(entry)) { 272 entry.d_fileno = parent_id; 273 entry.d_type = DT_DIR; 274 entry.d_name[0] = '.'; 275 entry.d_name[1] = '.'; 276 entry.d_name[2] = '\0'; 277 entry.d_namlen = 2; 278 entry.d_reclen = sizeof(entry); 279 error = vfs_read_dirent(ap, &entry, uio->uio_offset); 280 if (error != 0) 281 return (SET_ERROR(error)); 282 } 283 284 if (offp != NULL) 285 *offp = 2 * sizeof(entry); 286 return (0); 287} 288 289 290/* 291 * .zfs inode namespace 292 * 293 * We need to generate unique inode numbers for all files and directories 294 * within the .zfs pseudo-filesystem. We use the following scheme: 295 * 296 * ENTRY ZFSCTL_INODE 297 * .zfs 1 298 * .zfs/snapshot 2 299 * .zfs/snapshot/<snap> objectid(snap) 300 */ 301#define ZFSCTL_INO_SNAP(id) (id) 302 303static struct vop_vector zfsctl_ops_root; 304static struct vop_vector zfsctl_ops_snapdir; 305static struct vop_vector zfsctl_ops_snapshot; 306static struct vop_vector zfsctl_ops_shares_dir; 307 308void 309zfsctl_init(void) 310{ 311} 312 313void 314zfsctl_fini(void) 315{ 316} 317 318boolean_t 319zfsctl_is_node(vnode_t *vp) 320{ 321 return (vn_matchops(vp, zfsctl_ops_root) || 322 vn_matchops(vp, zfsctl_ops_snapdir) || 323 vn_matchops(vp, zfsctl_ops_snapshot) || 324 vn_matchops(vp, zfsctl_ops_shares_dir)); 325 326} 327 328typedef struct zfsctl_root { 329 sfs_node_t node; 330 sfs_node_t *snapdir; 331 timestruc_t cmtime; 332} zfsctl_root_t; 333 334 335/* 336 * Create the '.zfs' directory. 337 */ 338void 339zfsctl_create(zfsvfs_t *zfsvfs) 340{ 341 zfsctl_root_t *dot_zfs; 342 sfs_node_t *snapdir; 343 vnode_t *rvp; 344 uint64_t crtime[2]; 345 346 ASSERT(zfsvfs->z_ctldir == NULL); 347 348 snapdir = sfs_alloc_node(sizeof(*snapdir), "snapshot", ZFSCTL_INO_ROOT, 349 ZFSCTL_INO_SNAPDIR); 350 dot_zfs = (zfsctl_root_t *)sfs_alloc_node(sizeof(*dot_zfs), ".zfs", 0, 351 ZFSCTL_INO_ROOT); 352 dot_zfs->snapdir = snapdir; 353 354 VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp) == 0); 355 VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), 356 &crtime, sizeof(crtime))); 357 ZFS_TIME_DECODE(&dot_zfs->cmtime, crtime); 358 vput(rvp); 359 360 zfsvfs->z_ctldir = dot_zfs; 361} 362 363/* 364 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted. 365 * The nodes must not have any associated vnodes by now as they should be 366 * vflush-ed. 367 */ 368void 369zfsctl_destroy(zfsvfs_t *zfsvfs) 370{ 371 sfs_destroy_node(zfsvfs->z_ctldir->snapdir); 372 sfs_destroy_node((sfs_node_t *)zfsvfs->z_ctldir); 373 zfsvfs->z_ctldir = NULL; 374} 375 376static int 377zfsctl_fs_root_vnode(struct mount *mp, void *arg __unused, int flags, 378 struct vnode **vpp) 379{ 380 return (VFS_ROOT(mp, flags, vpp)); 381} 382 383static void 384zfsctl_common_vnode_setup(vnode_t *vp, void *arg) 385{ 386 ASSERT_VOP_ELOCKED(vp, __func__); 387 388 /* We support shared locking. */ 389 VN_LOCK_ASHARE(vp); 390 vp->v_type = VDIR; 391 vp->v_data = arg; 392} 393 394static int 395zfsctl_root_vnode(struct mount *mp, void *arg __unused, int flags, 396 struct vnode **vpp) 397{ 398 void *node; 399 int err; 400 401 node = ((zfsvfs_t*)mp->mnt_data)->z_ctldir; 402 err = sfs_vgetx(mp, flags, 0, ZFSCTL_INO_ROOT, "zfs", &zfsctl_ops_root, 403 zfsctl_common_vnode_setup, node, vpp); 404 return (err); 405} 406 407static int 408zfsctl_snapdir_vnode(struct mount *mp, void *arg __unused, int flags, 409 struct vnode **vpp) 410{ 411 void *node; 412 int err; 413 414 node = ((zfsvfs_t*)mp->mnt_data)->z_ctldir->snapdir; 415 err = sfs_vgetx(mp, flags, ZFSCTL_INO_ROOT, ZFSCTL_INO_SNAPDIR, "zfs", 416 &zfsctl_ops_snapdir, zfsctl_common_vnode_setup, node, vpp); 417 return (err); 418} 419 420/* 421 * Given a root znode, retrieve the associated .zfs directory. 422 * Add a hold to the vnode and return it. 423 */ 424int 425zfsctl_root(zfsvfs_t *zfsvfs, int flags, vnode_t **vpp) 426{ 427 vnode_t *vp; 428 int error; 429 430 error = zfsctl_root_vnode(zfsvfs->z_vfs, NULL, flags, vpp); 431 return (error); 432} 433 434/* 435 * Common open routine. Disallow any write access. 436 */ 437static int 438zfsctl_common_open(struct vop_open_args *ap) 439{ 440 int flags = ap->a_mode; 441 442 if (flags & FWRITE) 443 return (SET_ERROR(EACCES)); 444 445 return (0); 446} 447 448/* 449 * Common close routine. Nothing to do here. 450 */ 451/* ARGSUSED */ 452static int 453zfsctl_common_close(struct vop_close_args *ap) 454{ 455 return (0); 456} 457 458/* 459 * Common access routine. Disallow writes. 460 */ 461static int 462zfsctl_common_access(ap) 463 struct vop_access_args /* { 464 struct vnode *a_vp; 465 accmode_t a_accmode; 466 struct ucred *a_cred; 467 struct thread *a_td; 468 } */ *ap; 469{ 470 accmode_t accmode = ap->a_accmode; 471 472 if (accmode & VWRITE) 473 return (SET_ERROR(EACCES)); 474 return (0); 475} 476 477/* 478 * Common getattr function. Fill in basic information. 479 */ 480static void 481zfsctl_common_getattr(vnode_t *vp, vattr_t *vap) 482{ 483 timestruc_t now; 484 sfs_node_t *node; 485 486 node = vp->v_data; 487 488 vap->va_uid = 0; 489 vap->va_gid = 0; 490 vap->va_rdev = 0; 491 /* 492 * We are a purely virtual object, so we have no 493 * blocksize or allocated blocks. 494 */ 495 vap->va_blksize = 0; 496 vap->va_nblocks = 0; 497 vap->va_seq = 0; 498 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 499 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | 500 S_IROTH | S_IXOTH; 501 vap->va_type = VDIR; 502 /* 503 * We live in the now (for atime). 504 */ 505 gethrestime(&now); 506 vap->va_atime = now; 507 /* FreeBSD: Reset chflags(2) flags. */ 508 vap->va_flags = 0; 509 510 vap->va_nodeid = node->sn_id; 511 512 /* At least '.' and '..'. */ 513 vap->va_nlink = 2; 514} 515 516static int 517zfsctl_common_fid(ap) 518 struct vop_fid_args /* { 519 struct vnode *a_vp; 520 struct fid *a_fid; 521 } */ *ap; 522{ 523 vnode_t *vp = ap->a_vp; 524 fid_t *fidp = (void *)ap->a_fid; 525 sfs_node_t *node = vp->v_data; 526 uint64_t object = node->sn_id; 527 zfid_short_t *zfid; 528 int i; 529 530 zfid = (zfid_short_t *)fidp; 531 zfid->zf_len = SHORT_FID_LEN; 532 533 for (i = 0; i < sizeof(zfid->zf_object); i++) 534 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 535 536 /* .zfs nodes always have a generation number of 0 */ 537 for (i = 0; i < sizeof(zfid->zf_gen); i++) 538 zfid->zf_gen[i] = 0; 539 540 return (0); 541} 542 543static int 544zfsctl_common_reclaim(ap) 545 struct vop_reclaim_args /* { 546 struct vnode *a_vp; 547 struct thread *a_td; 548 } */ *ap; 549{ 550 vnode_t *vp = ap->a_vp; 551 552 (void) sfs_reclaim_vnode(vp); 553 return (0); 554} 555 556static int 557zfsctl_common_print(ap) 558 struct vop_print_args /* { 559 struct vnode *a_vp; 560 } */ *ap; 561{ 562 sfs_print_node(ap->a_vp->v_data); 563 return (0); 564} 565 566/* 567 * Get root directory attributes. 568 */ 569static int 570zfsctl_root_getattr(ap) 571 struct vop_getattr_args /* { 572 struct vnode *a_vp; 573 struct vattr *a_vap; 574 struct ucred *a_cred; 575 } */ *ap; 576{ 577 struct vnode *vp = ap->a_vp; 578 struct vattr *vap = ap->a_vap; 579 zfsctl_root_t *node = vp->v_data; 580 581 zfsctl_common_getattr(vp, vap); 582 vap->va_ctime = node->cmtime; 583 vap->va_mtime = vap->va_ctime; 584 vap->va_birthtime = vap->va_ctime; 585 vap->va_nlink += 1; /* snapdir */ 586 vap->va_size = vap->va_nlink; 587 return (0); 588} 589 590/* 591 * When we lookup "." we still can be asked to lock it 592 * differently, can't we? 593 */ 594int 595zfsctl_relock_dot(vnode_t *dvp, int ltype) 596{ 597 vref(dvp); 598 if (ltype != VOP_ISLOCKED(dvp)) { 599 if (ltype == LK_EXCLUSIVE) 600 vn_lock(dvp, LK_UPGRADE | LK_RETRY); 601 else /* if (ltype == LK_SHARED) */ 602 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY); 603 604 /* Relock for the "." case may left us with reclaimed vnode. */ 605 if ((dvp->v_iflag & VI_DOOMED) != 0) { 606 vrele(dvp); 607 return (SET_ERROR(ENOENT)); 608 } 609 } 610 return (0); 611} 612 613/* 614 * Special case the handling of "..". 615 */ 616int 617zfsctl_root_lookup(ap) 618 struct vop_lookup_args /* { 619 struct vnode *a_dvp; 620 struct vnode **a_vpp; 621 struct componentname *a_cnp; 622 } */ *ap; 623{ 624 struct componentname *cnp = ap->a_cnp; 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 lkflags = ap->a_cnp->cn_lkflags; 630 int nameiop = ap->a_cnp->cn_nameiop; 631 int err; 632 int ltype; 633 634 ASSERT(dvp->v_type == VDIR); 635 636 if ((flags & ISLASTCN) != 0 && nameiop != LOOKUP) 637 return (SET_ERROR(ENOTSUP)); 638 639 if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') { 640 err = zfsctl_relock_dot(dvp, lkflags & LK_TYPE_MASK); 641 if (err == 0) 642 *vpp = dvp; 643 } else if ((flags & ISDOTDOT) != 0) { 644 err = vn_vget_ino_gen(dvp, zfsctl_fs_root_vnode, NULL, 645 lkflags, vpp); 646 } else if (strncmp(cnp->cn_nameptr, "snapshot", cnp->cn_namelen) == 0) { 647 err = zfsctl_snapdir_vnode(dvp->v_mount, NULL, lkflags, vpp); 648 } else { 649 err = SET_ERROR(ENOENT); 650 } 651 if (err != 0) 652 *vpp = NULL; 653 return (err); 654} 655 656static int 657zfsctl_root_readdir(ap) 658 struct vop_readdir_args /* { 659 struct vnode *a_vp; 660 struct uio *a_uio; 661 struct ucred *a_cred; 662 int *a_eofflag; 663 int *ncookies; 664 u_long **a_cookies; 665 } */ *ap; 666{ 667 struct dirent entry; 668 vnode_t *vp = ap->a_vp; 669 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 670 zfsctl_root_t *node = vp->v_data; 671 uio_t *uio = ap->a_uio; 672 int *eofp = ap->a_eofflag; 673 off_t dots_offset; 674 int error; 675 676 ASSERT(vp->v_type == VDIR); 677 678 error = sfs_readdir_common(zfsvfs->z_root, ZFSCTL_INO_ROOT, ap, uio, 679 &dots_offset); 680 if (error != 0) { 681 if (error == ENAMETOOLONG) /* ran out of destination space */ 682 error = 0; 683 return (error); 684 } 685 if (uio->uio_offset != dots_offset) 686 return (SET_ERROR(EINVAL)); 687 688 CTASSERT(sizeof(node->snapdir->sn_name) <= sizeof(entry.d_name)); 689 entry.d_fileno = node->snapdir->sn_id; 690 entry.d_type = DT_DIR; 691 strcpy(entry.d_name, node->snapdir->sn_name); 692 entry.d_namlen = strlen(entry.d_name); 693 entry.d_reclen = sizeof(entry); 694 error = vfs_read_dirent(ap, &entry, uio->uio_offset); 695 if (error != 0) { 696 if (error == ENAMETOOLONG) 697 error = 0; 698 return (SET_ERROR(error)); 699 } 700 if (eofp != NULL) 701 *eofp = 1; 702 return (0); 703} 704 705static int 706zfsctl_root_vptocnp(struct vop_vptocnp_args *ap) 707{ 708 static const char dotzfs_name[4] = ".zfs"; 709 vnode_t *dvp; 710 int error; 711 712 if (*ap->a_buflen < sizeof (dotzfs_name)) 713 return (SET_ERROR(ENOMEM)); 714 715 error = vn_vget_ino_gen(ap->a_vp, zfsctl_fs_root_vnode, NULL, 716 LK_SHARED, &dvp); 717 if (error != 0) 718 return (SET_ERROR(error)); 719 720 VOP_UNLOCK(dvp, 0); 721 *ap->a_vpp = dvp; 722 *ap->a_buflen -= sizeof (dotzfs_name); 723 bcopy(dotzfs_name, ap->a_buf + *ap->a_buflen, sizeof (dotzfs_name)); 724 return (0); 725} 726 727static struct vop_vector zfsctl_ops_root = { 728 .vop_default = &default_vnodeops, 729 .vop_open = zfsctl_common_open, 730 .vop_close = zfsctl_common_close, 731 .vop_ioctl = VOP_EINVAL, 732 .vop_getattr = zfsctl_root_getattr, 733 .vop_access = zfsctl_common_access, 734 .vop_readdir = zfsctl_root_readdir, 735 .vop_lookup = zfsctl_root_lookup, 736 .vop_inactive = VOP_NULL, 737 .vop_reclaim = zfsctl_common_reclaim, 738 .vop_fid = zfsctl_common_fid, 739 .vop_print = zfsctl_common_print, 740 .vop_vptocnp = zfsctl_root_vptocnp, 741}; 742 743static int 744zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname) 745{ 746 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 747 748 dmu_objset_name(os, zname); 749 if (strlen(zname) + 1 + strlen(name) >= len) 750 return (SET_ERROR(ENAMETOOLONG)); 751 (void) strcat(zname, "@"); 752 (void) strcat(zname, name); 753 return (0); 754} 755 756static int 757zfsctl_snapshot_lookup(vnode_t *vp, const char *name, uint64_t *id) 758{ 759 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 760 int err; 761 762 err = dsl_dataset_snap_lookup(dmu_objset_ds(os), name, id); 763 return (err); 764} 765 766/* 767 * Given a vnode get a root vnode of a filesystem mounted on top of 768 * the vnode, if any. The root vnode is referenced and locked. 769 * If no filesystem is mounted then the orinal vnode remains referenced 770 * and locked. If any error happens the orinal vnode is unlocked and 771 * released. 772 */ 773static int 774zfsctl_mounted_here(vnode_t **vpp, int flags) 775{ 776 struct mount *mp; 777 int err; 778 779 ASSERT_VOP_LOCKED(*vpp, __func__); 780 ASSERT3S((*vpp)->v_type, ==, VDIR); 781 782 if ((mp = (*vpp)->v_mountedhere) != NULL) { 783 err = vfs_busy(mp, 0); 784 KASSERT(err == 0, ("vfs_busy(mp, 0) failed with %d", err)); 785 KASSERT(vrefcnt(*vpp) > 1, ("unreferenced mountpoint")); 786 vput(*vpp); 787 err = VFS_ROOT(mp, flags, vpp); 788 vfs_unbusy(mp); 789 return (err); 790 } 791 return (EJUSTRETURN); 792} 793 794typedef struct { 795 const char *snap_name; 796 uint64_t snap_id; 797} snapshot_setup_arg_t; 798 799static void 800zfsctl_snapshot_vnode_setup(vnode_t *vp, void *arg) 801{ 802 snapshot_setup_arg_t *ssa = arg; 803 sfs_node_t *node; 804 805 ASSERT_VOP_ELOCKED(vp, __func__); 806 807 node = sfs_alloc_node(sizeof(sfs_node_t), 808 ssa->snap_name, ZFSCTL_INO_SNAPDIR, ssa->snap_id); 809 zfsctl_common_vnode_setup(vp, node); 810 811 /* We have to support recursive locking. */ 812 VN_LOCK_AREC(vp); 813} 814 815/* 816 * Lookup entry point for the 'snapshot' directory. Try to open the 817 * snapshot if it exist, creating the pseudo filesystem vnode as necessary. 818 * Perform a mount of the associated dataset on top of the vnode. 819 * There are four possibilities: 820 * - the snapshot node and vnode do not exist 821 * - the snapshot vnode is covered by the mounted snapshot 822 * - the snapshot vnode is not covered yet, the mount operation is in progress 823 * - the snapshot vnode is not covered, because the snapshot has been unmounted 824 * The last two states are transient and should be relatively short-lived. 825 */ 826int 827zfsctl_snapdir_lookup(ap) 828 struct vop_lookup_args /* { 829 struct vnode *a_dvp; 830 struct vnode **a_vpp; 831 struct componentname *a_cnp; 832 } */ *ap; 833{ 834 vnode_t *dvp = ap->a_dvp; 835 vnode_t **vpp = ap->a_vpp; 836 struct componentname *cnp = ap->a_cnp; 837 char name[NAME_MAX + 1]; 838 char fullname[ZFS_MAX_DATASET_NAME_LEN]; 839 char *mountpoint; 840 size_t mountpoint_len; 841 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 842 uint64_t snap_id; 843 int nameiop = cnp->cn_nameiop; 844 int lkflags = cnp->cn_lkflags; 845 int flags = cnp->cn_flags; 846 int err; 847 848 ASSERT(dvp->v_type == VDIR); 849 850 if ((flags & ISLASTCN) != 0 && nameiop != LOOKUP) 851 return (SET_ERROR(ENOTSUP)); 852 853 if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') { 854 err = zfsctl_relock_dot(dvp, lkflags & LK_TYPE_MASK); 855 if (err == 0) 856 *vpp = dvp; 857 return (err); 858 } 859 if (flags & ISDOTDOT) { 860 err = vn_vget_ino_gen(dvp, zfsctl_root_vnode, NULL, lkflags, 861 vpp); 862 return (err); 863 } 864 865 if (cnp->cn_namelen >= sizeof(name)) 866 return (SET_ERROR(ENAMETOOLONG)); 867 868 strlcpy(name, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1); 869 err = zfsctl_snapshot_lookup(dvp, name, &snap_id); 870 if (err != 0) 871 return (SET_ERROR(ENOENT)); 872 873 for (;;) { 874 snapshot_setup_arg_t ssa; 875 876 ssa.snap_name = name; 877 ssa.snap_id = snap_id; 878 err = sfs_vgetx(dvp->v_mount, LK_SHARED, ZFSCTL_INO_SNAPDIR, 879 snap_id, "zfs", &zfsctl_ops_snapshot, 880 zfsctl_snapshot_vnode_setup, &ssa, vpp); 881 if (err != 0) 882 return (err); 883 884 /* Check if a new vnode has just been created. */ 885 if (VOP_ISLOCKED(*vpp) == LK_EXCLUSIVE) 886 break; 887 888 /* 889 * Check if a snapshot is already mounted on top of the vnode. 890 */ 891 err = zfsctl_mounted_here(vpp, lkflags); 892 if (err != EJUSTRETURN) 893 return (err); 894 895 /* 896 * If the vnode is not covered, then either the mount operation 897 * is in progress or the snapshot has already been unmounted 898 * but the vnode hasn't been inactivated and reclaimed yet. 899 * We can try to re-use the vnode in the latter case. 900 */ 901 VI_LOCK(*vpp); 902 if (((*vpp)->v_iflag & VI_MOUNT) == 0) { 903 /* Upgrade to exclusive lock in order to: 904 * - avoid race conditions 905 * - satisfy the contract of mount_snapshot() 906 */ 907 err = VOP_LOCK(*vpp, LK_TRYUPGRADE | LK_INTERLOCK); 908 if (err == 0) 909 break; 910 } else { 911 VI_UNLOCK(*vpp); 912 } 913 914 /* 915 * In this state we can loop on uncontested locks and starve 916 * the thread doing the lengthy, non-trivial mount operation. 917 * So, yield to prevent that from happening. 918 */ 919 vput(*vpp); 920 kern_yield(PRI_USER); 921 } 922 923 VERIFY0(zfsctl_snapshot_zname(dvp, name, sizeof(fullname), fullname)); 924 925 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) + 926 strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(name) + 1; 927 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP); 928 (void) snprintf(mountpoint, mountpoint_len, 929 "%s/" ZFS_CTLDIR_NAME "/snapshot/%s", 930 dvp->v_vfsp->mnt_stat.f_mntonname, name); 931 932 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, fullname, 0); 933 kmem_free(mountpoint, mountpoint_len); 934 if (err == 0) { 935 /* 936 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>. 937 * 938 * This is where we lie about our v_vfsp in order to 939 * make .zfs/snapshot/<snapname> accessible over NFS 940 * without requiring manual mounts of <snapname>. 941 */ 942 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs); 943 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs; 944 945 /* Clear the root flag (set via VFS_ROOT) as well. */ 946 (*vpp)->v_vflag &= ~VV_ROOT; 947 } 948 949 if (err != 0) 950 *vpp = NULL; 951 return (err); 952} 953 954static int 955zfsctl_snapdir_readdir(ap) 956 struct vop_readdir_args /* { 957 struct vnode *a_vp; 958 struct uio *a_uio; 959 struct ucred *a_cred; 960 int *a_eofflag; 961 int *ncookies; 962 u_long **a_cookies; 963 } */ *ap; 964{ 965 char snapname[ZFS_MAX_DATASET_NAME_LEN]; 966 struct dirent entry; 967 vnode_t *vp = ap->a_vp; 968 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 969 uio_t *uio = ap->a_uio; 970 int *eofp = ap->a_eofflag; 971 off_t dots_offset; 972 int error; 973 974 ASSERT(vp->v_type == VDIR); 975 976 error = sfs_readdir_common(ZFSCTL_INO_ROOT, ZFSCTL_INO_SNAPDIR, ap, uio, 977 &dots_offset); 978 if (error != 0) { 979 if (error == ENAMETOOLONG) /* ran out of destination space */ 980 error = 0; 981 return (error); 982 } 983 984 for (;;) { 985 uint64_t cookie; 986 uint64_t id; 987 988 cookie = uio->uio_offset - dots_offset; 989 990 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 991 error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof(snapname), 992 snapname, &id, &cookie, NULL); 993 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 994 if (error != 0) { 995 if (error == ENOENT) { 996 if (eofp != NULL) 997 *eofp = 1; 998 error = 0; 999 } 1000 return (error); 1001 } 1002 1003 entry.d_fileno = id; 1004 entry.d_type = DT_DIR; 1005 strcpy(entry.d_name, snapname); 1006 entry.d_namlen = strlen(entry.d_name); 1007 entry.d_reclen = sizeof(entry); 1008 error = vfs_read_dirent(ap, &entry, uio->uio_offset); 1009 if (error != 0) { 1010 if (error == ENAMETOOLONG) 1011 error = 0; 1012 return (SET_ERROR(error)); 1013 } 1014 uio->uio_offset = cookie + dots_offset; 1015 } 1016 /* NOTREACHED */ 1017} 1018 1019static int 1020zfsctl_snapdir_getattr(ap) 1021 struct vop_getattr_args /* { 1022 struct vnode *a_vp; 1023 struct vattr *a_vap; 1024 struct ucred *a_cred; 1025 } */ *ap; 1026{ 1027 vnode_t *vp = ap->a_vp; 1028 vattr_t *vap = ap->a_vap; 1029 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1030 dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os); 1031 sfs_node_t *node = vp->v_data; 1032 uint64_t snap_count; 1033 int err; 1034 1035 zfsctl_common_getattr(vp, vap); 1036 vap->va_ctime = dmu_objset_snap_cmtime(zfsvfs->z_os); 1037 vap->va_mtime = vap->va_ctime; 1038 vap->va_birthtime = vap->va_ctime; 1039 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) { 1040 err = zap_count(dmu_objset_pool(ds->ds_objset)->dp_meta_objset, 1041 dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count); 1042 if (err != 0) 1043 return (err); 1044 vap->va_nlink += snap_count; 1045 } 1046 vap->va_size = vap->va_nlink; 1047 1048 return (0); 1049} 1050 1051static struct vop_vector zfsctl_ops_snapdir = { 1052 .vop_default = &default_vnodeops, 1053 .vop_open = zfsctl_common_open, 1054 .vop_close = zfsctl_common_close, 1055 .vop_getattr = zfsctl_snapdir_getattr, 1056 .vop_access = zfsctl_common_access, 1057 .vop_readdir = zfsctl_snapdir_readdir, 1058 .vop_lookup = zfsctl_snapdir_lookup, 1059 .vop_reclaim = zfsctl_common_reclaim, 1060 .vop_fid = zfsctl_common_fid, 1061 .vop_print = zfsctl_common_print, 1062}; 1063 1064static int 1065zfsctl_snapshot_inactive(ap) 1066 struct vop_inactive_args /* { 1067 struct vnode *a_vp; 1068 struct thread *a_td; 1069 } */ *ap; 1070{ 1071 vnode_t *vp = ap->a_vp; 1072 1073 VERIFY(vrecycle(vp) == 1); 1074 return (0); 1075} 1076 1077static int 1078zfsctl_snapshot_reclaim(ap) 1079 struct vop_reclaim_args /* { 1080 struct vnode *a_vp; 1081 struct thread *a_td; 1082 } */ *ap; 1083{ 1084 vnode_t *vp = ap->a_vp; 1085 void *data = vp->v_data; 1086 1087 sfs_reclaim_vnode(vp); 1088 sfs_destroy_node(data); 1089 return (0); 1090} 1091 1092static int 1093zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap) 1094{ 1095 struct mount *mp; 1096 vnode_t *dvp; 1097 vnode_t *vp; 1098 sfs_node_t *node; 1099 size_t len; 1100 int locked; 1101 int error; 1102 1103 vp = ap->a_vp; 1104 node = vp->v_data; 1105 len = strlen(node->sn_name); 1106 if (*ap->a_buflen < len) 1107 return (SET_ERROR(ENOMEM)); 1108 1109 /* 1110 * Prevent unmounting of the snapshot while the vnode lock 1111 * is not held. That is not strictly required, but allows 1112 * us to assert that an uncovered snapshot vnode is never 1113 * "leaked". 1114 */ 1115 mp = vp->v_mountedhere; 1116 if (mp == NULL) 1117 return (SET_ERROR(ENOENT)); 1118 error = vfs_busy(mp, 0); 1119 KASSERT(error == 0, ("vfs_busy(mp, 0) failed with %d", error)); 1120 1121 /* 1122 * We can vput the vnode as we can now depend on the reference owned 1123 * by the busied mp. But we also need to hold the vnode, because 1124 * the reference may go after vfs_unbusy() which has to be called 1125 * before we can lock the vnode again. 1126 */ 1127 locked = VOP_ISLOCKED(vp); 1128 vhold(vp); 1129 vput(vp); 1130 1131 /* Look up .zfs/snapshot, our parent. */ 1132 error = zfsctl_snapdir_vnode(vp->v_mount, NULL, LK_SHARED, &dvp); 1133 if (error == 0) { 1134 VOP_UNLOCK(dvp, 0); 1135 *ap->a_vpp = dvp; 1136 *ap->a_buflen -= len; 1137 bcopy(node->sn_name, ap->a_buf + *ap->a_buflen, len); 1138 } 1139 vfs_unbusy(mp); 1140 vget(vp, locked | LK_RETRY, curthread); 1141 vdrop(vp); 1142 return (error); 1143} 1144 1145/* 1146 * These VP's should never see the light of day. They should always 1147 * be covered. 1148 */ 1149static struct vop_vector zfsctl_ops_snapshot = { 1150 .vop_default = NULL, /* ensure very restricted access */ 1151 .vop_inactive = zfsctl_snapshot_inactive, 1152 .vop_reclaim = zfsctl_snapshot_reclaim, 1153 .vop_vptocnp = zfsctl_snapshot_vptocnp, 1154 .vop_lock1 = vop_stdlock, 1155 .vop_unlock = vop_stdunlock, 1156 .vop_islocked = vop_stdislocked, 1157 .vop_advlockpurge = vop_stdadvlockpurge, /* called by vgone */ 1158 .vop_print = zfsctl_common_print, 1159}; 1160 1161int 1162zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp) 1163{ 1164 struct mount *mp; 1165 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1166 vnode_t *vp; 1167 int error; 1168 1169 ASSERT(zfsvfs->z_ctldir != NULL); 1170 *zfsvfsp = NULL; 1171 error = sfs_vnode_get(vfsp, LK_EXCLUSIVE, 1172 ZFSCTL_INO_SNAPDIR, objsetid, &vp); 1173 if (error == 0 && vp != NULL) { 1174 /* 1175 * XXX Probably need to at least reference, if not busy, the mp. 1176 */ 1177 if (vp->v_mountedhere != NULL) 1178 *zfsvfsp = vp->v_mountedhere->mnt_data; 1179 vput(vp); 1180 } 1181 if (*zfsvfsp == NULL) 1182 return (SET_ERROR(EINVAL)); 1183 return (0); 1184} 1185 1186/* 1187 * Unmount any snapshots for the given filesystem. This is called from 1188 * zfs_umount() - if we have a ctldir, then go through and unmount all the 1189 * snapshots. 1190 */ 1191int 1192zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr) 1193{ 1194 char snapname[ZFS_MAX_DATASET_NAME_LEN]; 1195 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1196 struct mount *mp; 1197 vnode_t *dvp; 1198 vnode_t *vp; 1199 sfs_node_t *node; 1200 sfs_node_t *snap; 1201 uint64_t cookie; 1202 int error; 1203 1204 ASSERT(zfsvfs->z_ctldir != NULL); 1205 1206 cookie = 0; 1207 for (;;) { 1208 uint64_t id; 1209 1210 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 1211 error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof(snapname), 1212 snapname, &id, &cookie, NULL); 1213 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 1214 if (error != 0) { 1215 if (error == ENOENT) 1216 error = 0; 1217 break; 1218 } 1219 1220 for (;;) { 1221 error = sfs_vnode_get(vfsp, LK_EXCLUSIVE, 1222 ZFSCTL_INO_SNAPDIR, id, &vp); 1223 if (error != 0 || vp == NULL) 1224 break; 1225 1226 mp = vp->v_mountedhere; 1227 1228 /* 1229 * v_mountedhere being NULL means that the 1230 * (uncovered) vnode is in a transient state 1231 * (mounting or unmounting), so loop until it 1232 * settles down. 1233 */ 1234 if (mp != NULL) 1235 break; 1236 vput(vp); 1237 } 1238 if (error != 0) 1239 break; 1240 if (vp == NULL) 1241 continue; /* no mountpoint, nothing to do */ 1242 1243 /* 1244 * The mount-point vnode is kept locked to avoid spurious EBUSY 1245 * from a concurrent umount. 1246 * The vnode lock must have recursive locking enabled. 1247 */ 1248 vfs_ref(mp); 1249 error = dounmount(mp, fflags, curthread); 1250 KASSERT_IMPLY(error == 0, vrefcnt(vp) == 1, 1251 ("extra references after unmount")); 1252 vput(vp); 1253 if (error != 0) 1254 break; 1255 } 1256 KASSERT_IMPLY((fflags & MS_FORCE) != 0, error == 0, 1257 ("force unmounting failed")); 1258 return (error); 1259} 1260 1261