zfs_ctldir.c revision 315844
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 struct vop_vector zfsctl_ops_root = { 710 .vop_default = &default_vnodeops, 711 .vop_open = zfsctl_common_open, 712 .vop_close = zfsctl_common_close, 713 .vop_ioctl = VOP_EINVAL, 714 .vop_getattr = zfsctl_root_getattr, 715 .vop_access = zfsctl_common_access, 716 .vop_readdir = zfsctl_root_readdir, 717 .vop_lookup = zfsctl_root_lookup, 718 .vop_inactive = VOP_NULL, 719 .vop_reclaim = zfsctl_common_reclaim, 720 .vop_fid = zfsctl_common_fid, 721 .vop_print = zfsctl_common_print, 722}; 723 724static int 725zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname) 726{ 727 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 728 729 dmu_objset_name(os, zname); 730 if (strlen(zname) + 1 + strlen(name) >= len) 731 return (SET_ERROR(ENAMETOOLONG)); 732 (void) strcat(zname, "@"); 733 (void) strcat(zname, name); 734 return (0); 735} 736 737static int 738zfsctl_snapshot_lookup(vnode_t *vp, const char *name, uint64_t *id) 739{ 740 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 741 int err; 742 743 err = dsl_dataset_snap_lookup(dmu_objset_ds(os), name, id); 744 return (err); 745} 746 747/* 748 * Given a vnode get a root vnode of a filesystem mounted on top of 749 * the vnode, if any. The root vnode is referenced and locked. 750 * If no filesystem is mounted then the orinal vnode remains referenced 751 * and locked. If any error happens the orinal vnode is unlocked and 752 * released. 753 */ 754static int 755zfsctl_mounted_here(vnode_t **vpp, int flags) 756{ 757 struct mount *mp; 758 int err; 759 760 ASSERT_VOP_LOCKED(*vpp, __func__); 761 ASSERT3S((*vpp)->v_type, ==, VDIR); 762 763 if ((mp = (*vpp)->v_mountedhere) != NULL) { 764 err = vfs_busy(mp, 0); 765 KASSERT(err == 0, ("vfs_busy(mp, 0) failed with %d", err)); 766 KASSERT(vrefcnt(*vpp) > 1, ("unreferenced mountpoint")); 767 vput(*vpp); 768 err = VFS_ROOT(mp, flags, vpp); 769 vfs_unbusy(mp); 770 return (err); 771 } 772 return (EJUSTRETURN); 773} 774 775typedef struct { 776 const char *snap_name; 777 uint64_t snap_id; 778} snapshot_setup_arg_t; 779 780static void 781zfsctl_snapshot_vnode_setup(vnode_t *vp, void *arg) 782{ 783 snapshot_setup_arg_t *ssa = arg; 784 sfs_node_t *node; 785 786 ASSERT_VOP_ELOCKED(vp, __func__); 787 788 node = sfs_alloc_node(sizeof(sfs_node_t), 789 ssa->snap_name, ZFSCTL_INO_SNAPDIR, ssa->snap_id); 790 zfsctl_common_vnode_setup(vp, node); 791 792 /* We have to support recursive locking. */ 793 VN_LOCK_AREC(vp); 794} 795 796/* 797 * Lookup entry point for the 'snapshot' directory. Try to open the 798 * snapshot if it exist, creating the pseudo filesystem vnode as necessary. 799 * Perform a mount of the associated dataset on top of the vnode. 800 */ 801/* ARGSUSED */ 802int 803zfsctl_snapdir_lookup(ap) 804 struct vop_lookup_args /* { 805 struct vnode *a_dvp; 806 struct vnode **a_vpp; 807 struct componentname *a_cnp; 808 } */ *ap; 809{ 810 vnode_t *dvp = ap->a_dvp; 811 vnode_t **vpp = ap->a_vpp; 812 struct componentname *cnp = ap->a_cnp; 813 char name[NAME_MAX + 1]; 814 char fullname[ZFS_MAX_DATASET_NAME_LEN]; 815 char *mountpoint; 816 size_t mountpoint_len; 817 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 818 uint64_t snap_id; 819 int nameiop = cnp->cn_nameiop; 820 int lkflags = cnp->cn_lkflags; 821 int flags = cnp->cn_flags; 822 int err; 823 824 ASSERT(dvp->v_type == VDIR); 825 826 if ((flags & ISLASTCN) != 0 && nameiop != LOOKUP) 827 return (SET_ERROR(ENOTSUP)); 828 829 if (cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.') { 830 err = zfsctl_relock_dot(dvp, lkflags & LK_TYPE_MASK); 831 if (err == 0) 832 *vpp = dvp; 833 return (err); 834 } 835 if (flags & ISDOTDOT) { 836 err = vn_vget_ino_gen(dvp, zfsctl_root_vnode, NULL, lkflags, 837 vpp); 838 return (err); 839 } 840 841 if (cnp->cn_namelen >= sizeof(name)) 842 return (SET_ERROR(ENAMETOOLONG)); 843 844 strlcpy(name, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1); 845 err = zfsctl_snapshot_lookup(dvp, name, &snap_id); 846 if (err != 0) 847 return (SET_ERROR(ENOENT)); 848 849 for (;;) { 850 snapshot_setup_arg_t ssa; 851 852 ssa.snap_name = name; 853 ssa.snap_id = snap_id; 854 err = sfs_vgetx(dvp->v_mount, LK_SHARED, ZFSCTL_INO_SNAPDIR, 855 snap_id, "zfs", &zfsctl_ops_snapshot, 856 zfsctl_snapshot_vnode_setup, &ssa, vpp); 857 if (err != 0) 858 return (err); 859 860 /* Check if a new vnode has just been created. */ 861 if (VOP_ISLOCKED(*vpp) == LK_EXCLUSIVE) 862 break; 863 864 /* 865 * The vnode must be referenced at least by this thread and 866 * the mounted snapshot or the thread doing the mounting. 867 * There can be more references from concurrent lookups. 868 */ 869 KASSERT(vrefcnt(*vpp) > 1, ("found unreferenced mountpoint")); 870 871 /* 872 * Check if a snapshot is already mounted on top of the vnode. 873 */ 874 err = zfsctl_mounted_here(vpp, lkflags); 875 if (err != EJUSTRETURN) 876 return (err); 877 878#ifdef INVARIANTS 879 /* 880 * If the vnode not covered yet, then the mount operation 881 * must be in progress. 882 */ 883 VI_LOCK(*vpp); 884 KASSERT(((*vpp)->v_iflag & VI_MOUNT) != 0, 885 ("snapshot vnode not covered")); 886 VI_UNLOCK(*vpp); 887#endif 888 vput(*vpp); 889 890 /* 891 * In this situation we can loop on uncontested locks and starve 892 * the thread doing the lengthy, non-trivial mount operation. 893 */ 894 kern_yield(PRI_USER); 895 } 896 897 VERIFY0(zfsctl_snapshot_zname(dvp, name, sizeof(fullname), fullname)); 898 899 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) + 900 strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(name) + 1; 901 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP); 902 (void) snprintf(mountpoint, mountpoint_len, 903 "%s/" ZFS_CTLDIR_NAME "/snapshot/%s", 904 dvp->v_vfsp->mnt_stat.f_mntonname, name); 905 906 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, fullname, 0); 907 kmem_free(mountpoint, mountpoint_len); 908 if (err == 0) { 909 /* 910 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>. 911 * 912 * This is where we lie about our v_vfsp in order to 913 * make .zfs/snapshot/<snapname> accessible over NFS 914 * without requiring manual mounts of <snapname>. 915 */ 916 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs); 917 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs; 918 919 /* Clear the root flag (set via VFS_ROOT) as well. */ 920 (*vpp)->v_vflag &= ~VV_ROOT; 921 } 922 923 if (err != 0) 924 *vpp = NULL; 925 return (err); 926} 927 928static int 929zfsctl_snapdir_readdir(ap) 930 struct vop_readdir_args /* { 931 struct vnode *a_vp; 932 struct uio *a_uio; 933 struct ucred *a_cred; 934 int *a_eofflag; 935 int *ncookies; 936 u_long **a_cookies; 937 } */ *ap; 938{ 939 char snapname[ZFS_MAX_DATASET_NAME_LEN]; 940 struct dirent entry; 941 vnode_t *vp = ap->a_vp; 942 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 943 uio_t *uio = ap->a_uio; 944 int *eofp = ap->a_eofflag; 945 off_t dots_offset; 946 int error; 947 948 ASSERT(vp->v_type == VDIR); 949 950 error = sfs_readdir_common(ZFSCTL_INO_ROOT, ZFSCTL_INO_SNAPDIR, ap, uio, 951 &dots_offset); 952 if (error != 0) { 953 if (error == ENAMETOOLONG) /* ran out of destination space */ 954 error = 0; 955 return (error); 956 } 957 958 for (;;) { 959 uint64_t cookie; 960 uint64_t id; 961 962 cookie = uio->uio_offset - dots_offset; 963 964 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 965 error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof(snapname), 966 snapname, &id, &cookie, NULL); 967 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 968 if (error != 0) { 969 if (error == ENOENT) { 970 if (eofp != NULL) 971 *eofp = 1; 972 error = 0; 973 } 974 return (error); 975 } 976 977 entry.d_fileno = id; 978 entry.d_type = DT_DIR; 979 strcpy(entry.d_name, snapname); 980 entry.d_namlen = strlen(entry.d_name); 981 entry.d_reclen = sizeof(entry); 982 error = vfs_read_dirent(ap, &entry, uio->uio_offset); 983 if (error != 0) { 984 if (error == ENAMETOOLONG) 985 error = 0; 986 return (SET_ERROR(error)); 987 } 988 uio->uio_offset = cookie + dots_offset; 989 } 990 /* NOTREACHED */ 991} 992 993/* ARGSUSED */ 994static int 995zfsctl_snapdir_getattr(ap) 996 struct vop_getattr_args /* { 997 struct vnode *a_vp; 998 struct vattr *a_vap; 999 struct ucred *a_cred; 1000 } */ *ap; 1001{ 1002 vnode_t *vp = ap->a_vp; 1003 vattr_t *vap = ap->a_vap; 1004 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1005 dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os); 1006 sfs_node_t *node = vp->v_data; 1007 uint64_t snap_count; 1008 int err; 1009 1010 zfsctl_common_getattr(vp, vap); 1011 vap->va_ctime = dmu_objset_snap_cmtime(zfsvfs->z_os); 1012 vap->va_mtime = vap->va_ctime; 1013 vap->va_birthtime = vap->va_ctime; 1014 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) { 1015 err = zap_count(dmu_objset_pool(ds->ds_objset)->dp_meta_objset, 1016 dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count); 1017 if (err != 0) 1018 return (err); 1019 vap->va_nlink += snap_count; 1020 } 1021 vap->va_size = vap->va_nlink; 1022 1023 return (0); 1024} 1025 1026static struct vop_vector zfsctl_ops_snapdir = { 1027 .vop_default = &default_vnodeops, 1028 .vop_open = zfsctl_common_open, 1029 .vop_close = zfsctl_common_close, 1030 .vop_getattr = zfsctl_snapdir_getattr, 1031 .vop_access = zfsctl_common_access, 1032 .vop_readdir = zfsctl_snapdir_readdir, 1033 .vop_lookup = zfsctl_snapdir_lookup, 1034 .vop_reclaim = zfsctl_common_reclaim, 1035 .vop_fid = zfsctl_common_fid, 1036 .vop_print = zfsctl_common_print, 1037}; 1038 1039static int 1040zfsctl_snapshot_inactive(ap) 1041 struct vop_inactive_args /* { 1042 struct vnode *a_vp; 1043 struct thread *a_td; 1044 } */ *ap; 1045{ 1046 vnode_t *vp = ap->a_vp; 1047 1048 VERIFY(vrecycle(vp) == 1); 1049 return (0); 1050} 1051 1052static int 1053zfsctl_snapshot_reclaim(ap) 1054 struct vop_reclaim_args /* { 1055 struct vnode *a_vp; 1056 struct thread *a_td; 1057 } */ *ap; 1058{ 1059 vnode_t *vp = ap->a_vp; 1060 void *data = vp->v_data; 1061 1062 sfs_reclaim_vnode(vp); 1063 sfs_destroy_node(data); 1064 return (0); 1065} 1066 1067static int 1068zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap) 1069{ 1070 struct mount *mp; 1071 vnode_t *dvp; 1072 vnode_t *vp; 1073 sfs_node_t *node; 1074 size_t len; 1075 int locked; 1076 int error; 1077 1078 vp = ap->a_vp; 1079 node = vp->v_data; 1080 len = strlen(node->sn_name); 1081 if (*ap->a_buflen < len) 1082 return (SET_ERROR(ENOMEM)); 1083 1084 /* 1085 * Prevent unmounting of the snapshot while the vnode lock 1086 * is not held. That is not strictly required, but allows 1087 * us to assert that an uncovered snapshot vnode is never 1088 * "leaked". 1089 */ 1090 mp = vp->v_mountedhere; 1091 if (mp == NULL) 1092 return (SET_ERROR(ENOENT)); 1093 error = vfs_busy(mp, 0); 1094 KASSERT(error == 0, ("vfs_busy(mp, 0) failed with %d", error)); 1095 1096 /* 1097 * We can vput the vnode as we can now depend on the reference owned 1098 * by the busied mp. But we also need to hold the vnode, because 1099 * the reference may go after vfs_unbusy() which has to be called 1100 * before we can lock the vnode again. 1101 */ 1102 locked = VOP_ISLOCKED(vp); 1103 vhold(vp); 1104 vput(vp); 1105 1106 /* Look up .zfs/snapshot, our parent. */ 1107 error = zfsctl_snapdir_vnode(vp->v_mount, NULL, LK_SHARED, &dvp); 1108 if (error == 0) { 1109 VOP_UNLOCK(dvp, 0); 1110 *ap->a_vpp = dvp; 1111 *ap->a_buflen -= len; 1112 bcopy(node->sn_name, ap->a_buf + *ap->a_buflen, len); 1113 } 1114 vfs_unbusy(mp); 1115 vget(vp, locked | LK_RETRY, curthread); 1116 vdrop(vp); 1117 return (error); 1118} 1119 1120/* 1121 * These VP's should never see the light of day. They should always 1122 * be covered. 1123 */ 1124static struct vop_vector zfsctl_ops_snapshot = { 1125 .vop_default = NULL, /* ensure very restricted access */ 1126 .vop_inactive = zfsctl_snapshot_inactive, 1127 .vop_reclaim = zfsctl_snapshot_reclaim, 1128 .vop_vptocnp = zfsctl_snapshot_vptocnp, 1129 .vop_lock1 = vop_stdlock, 1130 .vop_unlock = vop_stdunlock, 1131 .vop_islocked = vop_stdislocked, 1132 .vop_advlockpurge = vop_stdadvlockpurge, /* called by vgone */ 1133 .vop_print = zfsctl_common_print, 1134}; 1135 1136int 1137zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp) 1138{ 1139 struct mount *mp; 1140 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1141 vnode_t *vp; 1142 int error; 1143 1144 ASSERT(zfsvfs->z_ctldir != NULL); 1145 *zfsvfsp = NULL; 1146 error = sfs_vnode_get(vfsp, LK_EXCLUSIVE, 1147 ZFSCTL_INO_SNAPDIR, objsetid, &vp); 1148 if (error == 0 && vp != NULL) { 1149 /* 1150 * XXX Probably need to at least reference, if not busy, the mp. 1151 */ 1152 if (vp->v_mountedhere != NULL) 1153 *zfsvfsp = vp->v_mountedhere->mnt_data; 1154 vput(vp); 1155 } 1156 if (*zfsvfsp == NULL) 1157 return (SET_ERROR(EINVAL)); 1158 return (0); 1159} 1160 1161/* 1162 * Unmount any snapshots for the given filesystem. This is called from 1163 * zfs_umount() - if we have a ctldir, then go through and unmount all the 1164 * snapshots. 1165 */ 1166int 1167zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr) 1168{ 1169 char snapname[ZFS_MAX_DATASET_NAME_LEN]; 1170 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1171 struct mount *mp; 1172 vnode_t *dvp; 1173 vnode_t *vp; 1174 sfs_node_t *node; 1175 sfs_node_t *snap; 1176 uint64_t cookie; 1177 int error; 1178 1179 ASSERT(zfsvfs->z_ctldir != NULL); 1180 1181 cookie = 0; 1182 for (;;) { 1183 uint64_t id; 1184 1185 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 1186 error = dmu_snapshot_list_next(zfsvfs->z_os, sizeof(snapname), 1187 snapname, &id, &cookie, NULL); 1188 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 1189 if (error != 0) { 1190 if (error == ENOENT) 1191 error = 0; 1192 break; 1193 } 1194 1195 for (;;) { 1196 error = sfs_vnode_get(vfsp, LK_EXCLUSIVE, 1197 ZFSCTL_INO_SNAPDIR, id, &vp); 1198 if (error != 0 || vp == NULL) 1199 break; 1200 1201 mp = vp->v_mountedhere; 1202 1203 /* 1204 * v_mountedhere being NULL means that the 1205 * (uncovered) vnode is in a transient state 1206 * (mounting or unmounting), so loop until it 1207 * settles down. 1208 */ 1209 if (mp != NULL) 1210 break; 1211 vput(vp); 1212 } 1213 if (error != 0) 1214 break; 1215 if (vp == NULL) 1216 continue; /* no mountpoint, nothing to do */ 1217 1218 /* 1219 * The mount-point vnode is kept locked to avoid spurious EBUSY 1220 * from a concurrent umount. 1221 * The vnode lock must have recursive locking enabled. 1222 */ 1223 vfs_ref(mp); 1224 error = dounmount(mp, fflags, curthread); 1225 KASSERT_IMPLY(error == 0, vrefcnt(vp) == 1, 1226 ("extra references after unmount")); 1227 vput(vp); 1228 if (error != 0) 1229 break; 1230 } 1231 KASSERT_IMPLY((fflags & MS_FORCE) != 0, error == 0, 1232 ("force unmounting failed")); 1233 return (error); 1234} 1235 1236