zfs_ctldir.c revision 315848
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 */ 437/* ARGSUSED */ 438static int 439zfsctl_common_open(struct vop_open_args *ap) 440{ 441 int flags = ap->a_mode; 442 443 if (flags & FWRITE) 444 return (SET_ERROR(EACCES)); 445 446 return (0); 447} 448 449/* 450 * Common close routine. Nothing to do here. 451 */ 452/* ARGSUSED */ 453static int 454zfsctl_common_close(struct vop_close_args *ap) 455{ 456 return (0); 457} 458 459/* 460 * Common access routine. Disallow writes. 461 */ 462/* ARGSUSED */ 463static int 464zfsctl_common_access(ap) 465 struct vop_access_args /* { 466 struct vnode *a_vp; 467 accmode_t a_accmode; 468 struct ucred *a_cred; 469 struct thread *a_td; 470 } */ *ap; 471{ 472 accmode_t accmode = ap->a_accmode; 473 474 if (accmode & VWRITE) 475 return (SET_ERROR(EACCES)); 476 return (0); 477} 478 479/* 480 * Common getattr function. Fill in basic information. 481 */ 482static void 483zfsctl_common_getattr(vnode_t *vp, vattr_t *vap) 484{ 485 timestruc_t now; 486 sfs_node_t *node; 487 488 node = vp->v_data; 489 490 vap->va_uid = 0; 491 vap->va_gid = 0; 492 vap->va_rdev = 0; 493 /* 494 * We are a purely virtual object, so we have no 495 * blocksize or allocated blocks. 496 */ 497 vap->va_blksize = 0; 498 vap->va_nblocks = 0; 499 vap->va_seq = 0; 500 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 501 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | 502 S_IROTH | S_IXOTH; 503 vap->va_type = VDIR; 504 /* 505 * We live in the now (for atime). 506 */ 507 gethrestime(&now); 508 vap->va_atime = now; 509 /* FreeBSD: Reset chflags(2) flags. */ 510 vap->va_flags = 0; 511 512 vap->va_nodeid = node->sn_id; 513 514 /* At least '.' and '..'. */ 515 vap->va_nlink = 2; 516} 517 518/*ARGSUSED*/ 519static int 520zfsctl_common_fid(ap) 521 struct vop_fid_args /* { 522 struct vnode *a_vp; 523 struct fid *a_fid; 524 } */ *ap; 525{ 526 vnode_t *vp = ap->a_vp; 527 fid_t *fidp = (void *)ap->a_fid; 528 sfs_node_t *node = vp->v_data; 529 uint64_t object = node->sn_id; 530 zfid_short_t *zfid; 531 int i; 532 533 zfid = (zfid_short_t *)fidp; 534 zfid->zf_len = SHORT_FID_LEN; 535 536 for (i = 0; i < sizeof(zfid->zf_object); i++) 537 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 538 539 /* .zfs nodes always have a generation number of 0 */ 540 for (i = 0; i < sizeof(zfid->zf_gen); i++) 541 zfid->zf_gen[i] = 0; 542 543 return (0); 544} 545 546static int 547zfsctl_common_reclaim(ap) 548 struct vop_reclaim_args /* { 549 struct vnode *a_vp; 550 struct thread *a_td; 551 } */ *ap; 552{ 553 vnode_t *vp = ap->a_vp; 554 555 (void) sfs_reclaim_vnode(vp); 556 return (0); 557} 558 559static int 560zfsctl_common_print(ap) 561 struct vop_print_args /* { 562 struct vnode *a_vp; 563 } */ *ap; 564{ 565 sfs_print_node(ap->a_vp->v_data); 566 return (0); 567} 568 569/* 570 * Get root directory attributes. 571 */ 572/* ARGSUSED */ 573static int 574zfsctl_root_getattr(ap) 575 struct vop_getattr_args /* { 576 struct vnode *a_vp; 577 struct vattr *a_vap; 578 struct ucred *a_cred; 579 } */ *ap; 580{ 581 struct vnode *vp = ap->a_vp; 582 struct vattr *vap = ap->a_vap; 583 zfsctl_root_t *node = vp->v_data; 584 585 zfsctl_common_getattr(vp, vap); 586 vap->va_ctime = node->cmtime; 587 vap->va_mtime = vap->va_ctime; 588 vap->va_birthtime = vap->va_ctime; 589 vap->va_nlink += 1; /* snapdir */ 590 vap->va_size = vap->va_nlink; 591 return (0); 592} 593 594/* 595 * When we lookup "." we still can be asked to lock it 596 * differently, can't we? 597 */ 598int 599zfsctl_relock_dot(vnode_t *dvp, int ltype) 600{ 601 vref(dvp); 602 if (ltype != VOP_ISLOCKED(dvp)) { 603 if (ltype == LK_EXCLUSIVE) 604 vn_lock(dvp, LK_UPGRADE | LK_RETRY); 605 else /* if (ltype == LK_SHARED) */ 606 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY); 607 608 /* Relock for the "." case may left us with reclaimed vnode. */ 609 if ((dvp->v_iflag & VI_DOOMED) != 0) { 610 vrele(dvp); 611 return (SET_ERROR(ENOENT)); 612 } 613 } 614 return (0); 615} 616 617/* 618 * Special case the handling of "..". 619 */ 620int 621zfsctl_root_lookup(ap) 622 struct vop_lookup_args /* { 623 struct vnode *a_dvp; 624 struct vnode **a_vpp; 625 struct componentname *a_cnp; 626 } */ *ap; 627{ 628 struct componentname *cnp = ap->a_cnp; 629 vnode_t *dvp = ap->a_dvp; 630 vnode_t **vpp = ap->a_vpp; 631 cred_t *cr = ap->a_cnp->cn_cred; 632 int flags = ap->a_cnp->cn_flags; 633 int lkflags = ap->a_cnp->cn_lkflags; 634 int nameiop = ap->a_cnp->cn_nameiop; 635 int err; 636 int ltype; 637 638 ASSERT(dvp->v_type == VDIR); 639 640 if ((flags & ISLASTCN) != 0 && nameiop != LOOKUP) 641 return (SET_ERROR(ENOTSUP)); 642 643 if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') { 644 err = zfsctl_relock_dot(dvp, lkflags & LK_TYPE_MASK); 645 if (err == 0) 646 *vpp = dvp; 647 } else if ((flags & ISDOTDOT) != 0) { 648 err = vn_vget_ino_gen(dvp, zfsctl_fs_root_vnode, NULL, 649 lkflags, vpp); 650 } else if (strncmp(cnp->cn_nameptr, "snapshot", cnp->cn_namelen) == 0) { 651 err = zfsctl_snapdir_vnode(dvp->v_mount, NULL, lkflags, vpp); 652 } else { 653 err = SET_ERROR(ENOENT); 654 } 655 if (err != 0) 656 *vpp = NULL; 657 return (err); 658} 659 660static int 661zfsctl_root_readdir(ap) 662 struct vop_readdir_args /* { 663 struct vnode *a_vp; 664 struct uio *a_uio; 665 struct ucred *a_cred; 666 int *a_eofflag; 667 int *ncookies; 668 u_long **a_cookies; 669 } */ *ap; 670{ 671 struct dirent entry; 672 vnode_t *vp = ap->a_vp; 673 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 674 zfsctl_root_t *node = vp->v_data; 675 uio_t *uio = ap->a_uio; 676 int *eofp = ap->a_eofflag; 677 off_t dots_offset; 678 int error; 679 680 ASSERT(vp->v_type == VDIR); 681 682 error = sfs_readdir_common(zfsvfs->z_root, ZFSCTL_INO_ROOT, ap, uio, 683 &dots_offset); 684 if (error != 0) { 685 if (error == ENAMETOOLONG) /* ran out of destination space */ 686 error = 0; 687 return (error); 688 } 689 if (uio->uio_offset != dots_offset) 690 return (SET_ERROR(EINVAL)); 691 692 CTASSERT(sizeof(node->snapdir->sn_name) <= sizeof(entry.d_name)); 693 entry.d_fileno = node->snapdir->sn_id; 694 entry.d_type = DT_DIR; 695 strcpy(entry.d_name, node->snapdir->sn_name); 696 entry.d_namlen = strlen(entry.d_name); 697 entry.d_reclen = sizeof(entry); 698 error = vfs_read_dirent(ap, &entry, uio->uio_offset); 699 if (error != 0) { 700 if (error == ENAMETOOLONG) 701 error = 0; 702 return (SET_ERROR(error)); 703 } 704 if (eofp != NULL) 705 *eofp = 1; 706 return (0); 707} 708 709static int 710zfsctl_root_vptocnp(struct vop_vptocnp_args *ap) 711{ 712 static const char dotzfs_name[4] = ".zfs"; 713 vnode_t *dvp; 714 int error; 715 716 if (*ap->a_buflen < sizeof (dotzfs_name)) 717 return (SET_ERROR(ENOMEM)); 718 719 error = vn_vget_ino_gen(ap->a_vp, zfsctl_fs_root_vnode, NULL, 720 LK_SHARED, &dvp); 721 if (error != 0) 722 return (SET_ERROR(error)); 723 724 VOP_UNLOCK(dvp, 0); 725 *ap->a_vpp = dvp; 726 *ap->a_buflen -= sizeof (dotzfs_name); 727 bcopy(dotzfs_name, ap->a_buf + *ap->a_buflen, sizeof (dotzfs_name)); 728 return (0); 729} 730 731static struct vop_vector zfsctl_ops_root = { 732 .vop_default = &default_vnodeops, 733 .vop_open = zfsctl_common_open, 734 .vop_close = zfsctl_common_close, 735 .vop_ioctl = VOP_EINVAL, 736 .vop_getattr = zfsctl_root_getattr, 737 .vop_access = zfsctl_common_access, 738 .vop_readdir = zfsctl_root_readdir, 739 .vop_lookup = zfsctl_root_lookup, 740 .vop_inactive = VOP_NULL, 741 .vop_reclaim = zfsctl_common_reclaim, 742 .vop_fid = zfsctl_common_fid, 743 .vop_print = zfsctl_common_print, 744 .vop_vptocnp = zfsctl_root_vptocnp, 745}; 746 747static int 748zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname) 749{ 750 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 751 752 dmu_objset_name(os, zname); 753 if (strlen(zname) + 1 + strlen(name) >= len) 754 return (SET_ERROR(ENAMETOOLONG)); 755 (void) strcat(zname, "@"); 756 (void) strcat(zname, name); 757 return (0); 758} 759 760static int 761zfsctl_snapshot_lookup(vnode_t *vp, const char *name, uint64_t *id) 762{ 763 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 764 int err; 765 766 err = dsl_dataset_snap_lookup(dmu_objset_ds(os), name, id); 767 return (err); 768} 769 770/* 771 * Given a vnode get a root vnode of a filesystem mounted on top of 772 * the vnode, if any. The root vnode is referenced and locked. 773 * If no filesystem is mounted then the orinal vnode remains referenced 774 * and locked. If any error happens the orinal vnode is unlocked and 775 * released. 776 */ 777static int 778zfsctl_mounted_here(vnode_t **vpp, int flags) 779{ 780 struct mount *mp; 781 int err; 782 783 ASSERT_VOP_LOCKED(*vpp, __func__); 784 ASSERT3S((*vpp)->v_type, ==, VDIR); 785 786 if ((mp = (*vpp)->v_mountedhere) != NULL) { 787 err = vfs_busy(mp, 0); 788 KASSERT(err == 0, ("vfs_busy(mp, 0) failed with %d", err)); 789 KASSERT(vrefcnt(*vpp) > 1, ("unreferenced mountpoint")); 790 vput(*vpp); 791 err = VFS_ROOT(mp, flags, vpp); 792 vfs_unbusy(mp); 793 return (err); 794 } 795 return (EJUSTRETURN); 796} 797 798typedef struct { 799 const char *snap_name; 800 uint64_t snap_id; 801} snapshot_setup_arg_t; 802 803static void 804zfsctl_snapshot_vnode_setup(vnode_t *vp, void *arg) 805{ 806 snapshot_setup_arg_t *ssa = arg; 807 sfs_node_t *node; 808 809 ASSERT_VOP_ELOCKED(vp, __func__); 810 811 node = sfs_alloc_node(sizeof(sfs_node_t), 812 ssa->snap_name, ZFSCTL_INO_SNAPDIR, ssa->snap_id); 813 zfsctl_common_vnode_setup(vp, node); 814 815 /* We have to support recursive locking. */ 816 VN_LOCK_AREC(vp); 817} 818 819/* 820 * Lookup entry point for the 'snapshot' directory. Try to open the 821 * snapshot if it exist, creating the pseudo filesystem vnode as necessary. 822 * Perform a mount of the associated dataset on top of the vnode. 823 */ 824/* ARGSUSED */ 825int 826zfsctl_snapdir_lookup(ap) 827 struct vop_lookup_args /* { 828 struct vnode *a_dvp; 829 struct vnode **a_vpp; 830 struct componentname *a_cnp; 831 } */ *ap; 832{ 833 vnode_t *dvp = ap->a_dvp; 834 vnode_t **vpp = ap->a_vpp; 835 struct componentname *cnp = ap->a_cnp; 836 char name[NAME_MAX + 1]; 837 char fullname[ZFS_MAX_DATASET_NAME_LEN]; 838 char *mountpoint; 839 size_t mountpoint_len; 840 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 841 uint64_t snap_id; 842 int nameiop = cnp->cn_nameiop; 843 int lkflags = cnp->cn_lkflags; 844 int flags = cnp->cn_flags; 845 int err; 846 847 ASSERT(dvp->v_type == VDIR); 848 849 if ((flags & ISLASTCN) != 0 && nameiop != LOOKUP) 850 return (SET_ERROR(ENOTSUP)); 851 852 if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') { 853 err = zfsctl_relock_dot(dvp, lkflags & LK_TYPE_MASK); 854 if (err == 0) 855 *vpp = dvp; 856 return (err); 857 } 858 if (flags & ISDOTDOT) { 859 err = vn_vget_ino_gen(dvp, zfsctl_root_vnode, NULL, lkflags, 860 vpp); 861 return (err); 862 } 863 864 if (cnp->cn_namelen >= sizeof(name)) 865 return (SET_ERROR(ENAMETOOLONG)); 866 867 strlcpy(name, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1); 868 err = zfsctl_snapshot_lookup(dvp, name, &snap_id); 869 if (err != 0) 870 return (SET_ERROR(ENOENT)); 871 872 for (;;) { 873 snapshot_setup_arg_t ssa; 874 875 ssa.snap_name = name; 876 ssa.snap_id = snap_id; 877 err = sfs_vgetx(dvp->v_mount, LK_SHARED, ZFSCTL_INO_SNAPDIR, 878 snap_id, "zfs", &zfsctl_ops_snapshot, 879 zfsctl_snapshot_vnode_setup, &ssa, vpp); 880 if (err != 0) 881 return (err); 882 883 /* Check if a new vnode has just been created. */ 884 if (VOP_ISLOCKED(*vpp) == LK_EXCLUSIVE) 885 break; 886 887 /* 888 * The vnode must be referenced at least by this thread and 889 * the mounted snapshot or the thread doing the mounting. 890 * There can be more references from concurrent lookups. 891 */ 892 KASSERT(vrefcnt(*vpp) > 1, ("found unreferenced mountpoint")); 893 894 /* 895 * Check if a snapshot is already mounted on top of the vnode. 896 */ 897 err = zfsctl_mounted_here(vpp, lkflags); 898 if (err != EJUSTRETURN) 899 return (err); 900 901#ifdef INVARIANTS 902 /* 903 * If the vnode not covered yet, then the mount operation 904 * must be in progress. 905 */ 906 VI_LOCK(*vpp); 907 KASSERT(((*vpp)->v_iflag & VI_MOUNT) != 0, 908 ("snapshot vnode not covered")); 909 VI_UNLOCK(*vpp); 910#endif 911 vput(*vpp); 912 913 /* 914 * In this situation we can loop on uncontested locks and starve 915 * the thread doing the lengthy, non-trivial mount operation. 916 */ 917 kern_yield(PRI_USER); 918 } 919 920 VERIFY0(zfsctl_snapshot_zname(dvp, name, sizeof(fullname), fullname)); 921 922 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) + 923 strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(name) + 1; 924 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP); 925 (void) snprintf(mountpoint, mountpoint_len, 926 "%s/" ZFS_CTLDIR_NAME "/snapshot/%s", 927 dvp->v_vfsp->mnt_stat.f_mntonname, name); 928 929 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, fullname, 0); 930 kmem_free(mountpoint, mountpoint_len); 931 if (err == 0) { 932 /* 933 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>. 934 * 935 * This is where we lie about our v_vfsp in order to 936 * make .zfs/snapshot/<snapname> accessible over NFS 937 * without requiring manual mounts of <snapname>. 938 */ 939 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs); 940 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs; 941 942 /* Clear the root flag (set via VFS_ROOT) as well. */ 943 (*vpp)->v_vflag &= ~VV_ROOT; 944 } 945 946 if (err != 0) 947 *vpp = NULL; 948 return (err); 949} 950 951static int 952zfsctl_snapdir_readdir(ap) 953 struct vop_readdir_args /* { 954 struct vnode *a_vp; 955 struct uio *a_uio; 956 struct ucred *a_cred; 957 int *a_eofflag; 958 int *ncookies; 959 u_long **a_cookies; 960 } */ *ap; 961{ 962 char snapname[ZFS_MAX_DATASET_NAME_LEN]; 963 struct dirent entry; 964 vnode_t *vp = ap->a_vp; 965 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 966 uio_t *uio = ap->a_uio; 967 int *eofp = ap->a_eofflag; 968 off_t dots_offset; 969 int error; 970 971 ASSERT(vp->v_type == VDIR); 972 973 error = sfs_readdir_common(ZFSCTL_INO_ROOT, ZFSCTL_INO_SNAPDIR, ap, uio, 974 &dots_offset); 975 if (error != 0) { 976 if (error == ENAMETOOLONG) /* ran out of destination space */ 977 error = 0; 978 return (error); 979 } 980 981 for (;;) { 982 uint64_t cookie; 983 uint64_t id; 984 985 cookie = uio->uio_offset - dots_offset; 986 987 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 988 error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof(snapname), 989 snapname, &id, &cookie, NULL); 990 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 991 if (error != 0) { 992 if (error == ENOENT) { 993 if (eofp != NULL) 994 *eofp = 1; 995 error = 0; 996 } 997 return (error); 998 } 999 1000 entry.d_fileno = id; 1001 entry.d_type = DT_DIR; 1002 strcpy(entry.d_name, snapname); 1003 entry.d_namlen = strlen(entry.d_name); 1004 entry.d_reclen = sizeof(entry); 1005 error = vfs_read_dirent(ap, &entry, uio->uio_offset); 1006 if (error != 0) { 1007 if (error == ENAMETOOLONG) 1008 error = 0; 1009 return (SET_ERROR(error)); 1010 } 1011 uio->uio_offset = cookie + dots_offset; 1012 } 1013 /* NOTREACHED */ 1014} 1015 1016/* ARGSUSED */ 1017static int 1018zfsctl_snapdir_getattr(ap) 1019 struct vop_getattr_args /* { 1020 struct vnode *a_vp; 1021 struct vattr *a_vap; 1022 struct ucred *a_cred; 1023 } */ *ap; 1024{ 1025 vnode_t *vp = ap->a_vp; 1026 vattr_t *vap = ap->a_vap; 1027 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1028 dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os); 1029 sfs_node_t *node = vp->v_data; 1030 uint64_t snap_count; 1031 int err; 1032 1033 zfsctl_common_getattr(vp, vap); 1034 vap->va_ctime = dmu_objset_snap_cmtime(zfsvfs->z_os); 1035 vap->va_mtime = vap->va_ctime; 1036 vap->va_birthtime = vap->va_ctime; 1037 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) { 1038 err = zap_count(dmu_objset_pool(ds->ds_objset)->dp_meta_objset, 1039 dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count); 1040 if (err != 0) 1041 return (err); 1042 vap->va_nlink += snap_count; 1043 } 1044 vap->va_size = vap->va_nlink; 1045 1046 return (0); 1047} 1048 1049static struct vop_vector zfsctl_ops_snapdir = { 1050 .vop_default = &default_vnodeops, 1051 .vop_open = zfsctl_common_open, 1052 .vop_close = zfsctl_common_close, 1053 .vop_getattr = zfsctl_snapdir_getattr, 1054 .vop_access = zfsctl_common_access, 1055 .vop_readdir = zfsctl_snapdir_readdir, 1056 .vop_lookup = zfsctl_snapdir_lookup, 1057 .vop_reclaim = zfsctl_common_reclaim, 1058 .vop_fid = zfsctl_common_fid, 1059 .vop_print = zfsctl_common_print, 1060}; 1061 1062static int 1063zfsctl_snapshot_inactive(ap) 1064 struct vop_inactive_args /* { 1065 struct vnode *a_vp; 1066 struct thread *a_td; 1067 } */ *ap; 1068{ 1069 vnode_t *vp = ap->a_vp; 1070 1071 VERIFY(vrecycle(vp) == 1); 1072 return (0); 1073} 1074 1075static int 1076zfsctl_snapshot_reclaim(ap) 1077 struct vop_reclaim_args /* { 1078 struct vnode *a_vp; 1079 struct thread *a_td; 1080 } */ *ap; 1081{ 1082 vnode_t *vp = ap->a_vp; 1083 void *data = vp->v_data; 1084 1085 sfs_reclaim_vnode(vp); 1086 sfs_destroy_node(data); 1087 return (0); 1088} 1089 1090static int 1091zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap) 1092{ 1093 struct mount *mp; 1094 vnode_t *dvp; 1095 vnode_t *vp; 1096 sfs_node_t *node; 1097 size_t len; 1098 int locked; 1099 int error; 1100 1101 vp = ap->a_vp; 1102 node = vp->v_data; 1103 len = strlen(node->sn_name); 1104 if (*ap->a_buflen < len) 1105 return (SET_ERROR(ENOMEM)); 1106 1107 /* 1108 * Prevent unmounting of the snapshot while the vnode lock 1109 * is not held. That is not strictly required, but allows 1110 * us to assert that an uncovered snapshot vnode is never 1111 * "leaked". 1112 */ 1113 mp = vp->v_mountedhere; 1114 if (mp == NULL) 1115 return (SET_ERROR(ENOENT)); 1116 error = vfs_busy(mp, 0); 1117 KASSERT(error == 0, ("vfs_busy(mp, 0) failed with %d", error)); 1118 1119 /* 1120 * We can vput the vnode as we can now depend on the reference owned 1121 * by the busied mp. But we also need to hold the vnode, because 1122 * the reference may go after vfs_unbusy() which has to be called 1123 * before we can lock the vnode again. 1124 */ 1125 locked = VOP_ISLOCKED(vp); 1126 vhold(vp); 1127 vput(vp); 1128 1129 /* Look up .zfs/snapshot, our parent. */ 1130 error = zfsctl_snapdir_vnode(vp->v_mount, NULL, LK_SHARED, &dvp); 1131 if (error == 0) { 1132 VOP_UNLOCK(dvp, 0); 1133 *ap->a_vpp = dvp; 1134 *ap->a_buflen -= len; 1135 bcopy(node->sn_name, ap->a_buf + *ap->a_buflen, len); 1136 } 1137 vfs_unbusy(mp); 1138 vget(vp, locked | LK_RETRY, curthread); 1139 vdrop(vp); 1140 return (error); 1141} 1142 1143/* 1144 * These VP's should never see the light of day. They should always 1145 * be covered. 1146 */ 1147static struct vop_vector zfsctl_ops_snapshot = { 1148 .vop_default = NULL, /* ensure very restricted access */ 1149 .vop_inactive = zfsctl_snapshot_inactive, 1150 .vop_reclaim = zfsctl_snapshot_reclaim, 1151 .vop_vptocnp = zfsctl_snapshot_vptocnp, 1152 .vop_lock1 = vop_stdlock, 1153 .vop_unlock = vop_stdunlock, 1154 .vop_islocked = vop_stdislocked, 1155 .vop_advlockpurge = vop_stdadvlockpurge, /* called by vgone */ 1156 .vop_print = zfsctl_common_print, 1157}; 1158 1159int 1160zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp) 1161{ 1162 struct mount *mp; 1163 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1164 vnode_t *vp; 1165 int error; 1166 1167 ASSERT(zfsvfs->z_ctldir != NULL); 1168 *zfsvfsp = NULL; 1169 error = sfs_vnode_get(vfsp, LK_EXCLUSIVE, 1170 ZFSCTL_INO_SNAPDIR, objsetid, &vp); 1171 if (error == 0 && vp != NULL) { 1172 /* 1173 * XXX Probably need to at least reference, if not busy, the mp. 1174 */ 1175 if (vp->v_mountedhere != NULL) 1176 *zfsvfsp = vp->v_mountedhere->mnt_data; 1177 vput(vp); 1178 } 1179 if (*zfsvfsp == NULL) 1180 return (SET_ERROR(EINVAL)); 1181 return (0); 1182} 1183 1184/* 1185 * Unmount any snapshots for the given filesystem. This is called from 1186 * zfs_umount() - if we have a ctldir, then go through and unmount all the 1187 * snapshots. 1188 */ 1189int 1190zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr) 1191{ 1192 char snapname[ZFS_MAX_DATASET_NAME_LEN]; 1193 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1194 struct mount *mp; 1195 vnode_t *dvp; 1196 vnode_t *vp; 1197 sfs_node_t *node; 1198 sfs_node_t *snap; 1199 uint64_t cookie; 1200 int error; 1201 1202 ASSERT(zfsvfs->z_ctldir != NULL); 1203 1204 cookie = 0; 1205 for (;;) { 1206 uint64_t id; 1207 1208 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 1209 error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof(snapname), 1210 snapname, &id, &cookie, NULL); 1211 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 1212 if (error != 0) { 1213 if (error == ENOENT) 1214 error = 0; 1215 break; 1216 } 1217 1218 for (;;) { 1219 error = sfs_vnode_get(vfsp, LK_EXCLUSIVE, 1220 ZFSCTL_INO_SNAPDIR, id, &vp); 1221 if (error != 0 || vp == NULL) 1222 break; 1223 1224 mp = vp->v_mountedhere; 1225 1226 /* 1227 * v_mountedhere being NULL means that the 1228 * (uncovered) vnode is in a transient state 1229 * (mounting or unmounting), so loop until it 1230 * settles down. 1231 */ 1232 if (mp != NULL) 1233 break; 1234 vput(vp); 1235 } 1236 if (error != 0) 1237 break; 1238 if (vp == NULL) 1239 continue; /* no mountpoint, nothing to do */ 1240 1241 /* 1242 * The mount-point vnode is kept locked to avoid spurious EBUSY 1243 * from a concurrent umount. 1244 * The vnode lock must have recursive locking enabled. 1245 */ 1246 vfs_ref(mp); 1247 error = dounmount(mp, fflags, curthread); 1248 KASSERT_IMPLY(error == 0, vrefcnt(vp) == 1, 1249 ("extra references after unmount")); 1250 vput(vp); 1251 if (error != 0) 1252 break; 1253 } 1254 KASSERT_IMPLY((fflags & MS_FORCE) != 0, error == 0, 1255 ("force unmounting failed")); 1256 return (error); 1257} 1258 1259