zfs_ctldir.c revision 302719
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) 2013 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/gfs.h> 74#include <sys/stat.h> 75#include <sys/dmu.h> 76#include <sys/dsl_destroy.h> 77#include <sys/dsl_deleg.h> 78#include <sys/mount.h> 79#include <sys/sunddi.h> 80 81#include "zfs_namecheck.h" 82 83typedef struct zfsctl_node { 84 gfs_dir_t zc_gfs_private; 85 uint64_t zc_id; 86 timestruc_t zc_cmtime; /* ctime and mtime, always the same */ 87} zfsctl_node_t; 88 89typedef struct zfsctl_snapdir { 90 zfsctl_node_t sd_node; 91 kmutex_t sd_lock; 92 avl_tree_t sd_snaps; 93} zfsctl_snapdir_t; 94 95typedef struct { 96 char *se_name; 97 vnode_t *se_root; 98 avl_node_t se_node; 99} zfs_snapentry_t; 100 101static int 102snapentry_compare(const void *a, const void *b) 103{ 104 const zfs_snapentry_t *sa = a; 105 const zfs_snapentry_t *sb = b; 106 int ret = strcmp(sa->se_name, sb->se_name); 107 108 if (ret < 0) 109 return (-1); 110 else if (ret > 0) 111 return (1); 112 else 113 return (0); 114} 115 116#ifdef illumos 117vnodeops_t *zfsctl_ops_root; 118vnodeops_t *zfsctl_ops_snapdir; 119vnodeops_t *zfsctl_ops_snapshot; 120vnodeops_t *zfsctl_ops_shares; 121vnodeops_t *zfsctl_ops_shares_dir; 122 123static const fs_operation_def_t zfsctl_tops_root[]; 124static const fs_operation_def_t zfsctl_tops_snapdir[]; 125static const fs_operation_def_t zfsctl_tops_snapshot[]; 126static const fs_operation_def_t zfsctl_tops_shares[]; 127#else 128static struct vop_vector zfsctl_ops_root; 129static struct vop_vector zfsctl_ops_snapdir; 130static struct vop_vector zfsctl_ops_snapshot; 131static struct vop_vector zfsctl_ops_shares; 132static struct vop_vector zfsctl_ops_shares_dir; 133#endif 134 135static vnode_t *zfsctl_mknode_snapdir(vnode_t *); 136static vnode_t *zfsctl_mknode_shares(vnode_t *); 137static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset); 138static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *); 139 140#ifdef illumos 141static gfs_opsvec_t zfsctl_opsvec[] = { 142 { ".zfs", zfsctl_tops_root, &zfsctl_ops_root }, 143 { ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir }, 144 { ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot }, 145 { ".zfs/shares", zfsctl_tops_shares, &zfsctl_ops_shares_dir }, 146 { ".zfs/shares/vnode", zfsctl_tops_shares, &zfsctl_ops_shares }, 147 { NULL } 148}; 149#endif 150 151/* 152 * Root directory elements. We only have two entries 153 * snapshot and shares. 154 */ 155static gfs_dirent_t zfsctl_root_entries[] = { 156 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE }, 157 { "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE }, 158 { NULL } 159}; 160 161/* include . and .. in the calculation */ 162#define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \ 163 sizeof (gfs_dirent_t)) + 1) 164 165 166/* 167 * Initialize the various GFS pieces we'll need to create and manipulate .zfs 168 * directories. This is called from the ZFS init routine, and initializes the 169 * vnode ops vectors that we'll be using. 170 */ 171void 172zfsctl_init(void) 173{ 174#ifdef illumos 175 VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0); 176#endif 177} 178 179void 180zfsctl_fini(void) 181{ 182#ifdef illumos 183 /* 184 * Remove vfsctl vnode ops 185 */ 186 if (zfsctl_ops_root) 187 vn_freevnodeops(zfsctl_ops_root); 188 if (zfsctl_ops_snapdir) 189 vn_freevnodeops(zfsctl_ops_snapdir); 190 if (zfsctl_ops_snapshot) 191 vn_freevnodeops(zfsctl_ops_snapshot); 192 if (zfsctl_ops_shares) 193 vn_freevnodeops(zfsctl_ops_shares); 194 if (zfsctl_ops_shares_dir) 195 vn_freevnodeops(zfsctl_ops_shares_dir); 196 197 zfsctl_ops_root = NULL; 198 zfsctl_ops_snapdir = NULL; 199 zfsctl_ops_snapshot = NULL; 200 zfsctl_ops_shares = NULL; 201 zfsctl_ops_shares_dir = NULL; 202#endif /* illumos */ 203} 204 205boolean_t 206zfsctl_is_node(vnode_t *vp) 207{ 208 return (vn_matchops(vp, zfsctl_ops_root) || 209 vn_matchops(vp, zfsctl_ops_snapdir) || 210 vn_matchops(vp, zfsctl_ops_snapshot) || 211 vn_matchops(vp, zfsctl_ops_shares) || 212 vn_matchops(vp, zfsctl_ops_shares_dir)); 213 214} 215 216/* 217 * Return the inode number associated with the 'snapshot' or 218 * 'shares' directory. 219 */ 220/* ARGSUSED */ 221static ino64_t 222zfsctl_root_inode_cb(vnode_t *vp, int index) 223{ 224 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 225 226 ASSERT(index < 2); 227 228 if (index == 0) 229 return (ZFSCTL_INO_SNAPDIR); 230 231 return (zfsvfs->z_shares_dir); 232} 233 234/* 235 * Create the '.zfs' directory. This directory is cached as part of the VFS 236 * structure. This results in a hold on the vfs_t. The code in zfs_umount() 237 * therefore checks against a vfs_count of 2 instead of 1. This reference 238 * is removed when the ctldir is destroyed in the unmount. 239 */ 240void 241zfsctl_create(zfsvfs_t *zfsvfs) 242{ 243 vnode_t *vp, *rvp; 244 zfsctl_node_t *zcp; 245 uint64_t crtime[2]; 246 247 ASSERT(zfsvfs->z_ctldir == NULL); 248 249 vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs, 250 &zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries, 251 zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL); 252 zcp = vp->v_data; 253 zcp->zc_id = ZFSCTL_INO_ROOT; 254 255 VERIFY(VFS_ROOT(zfsvfs->z_vfs, LK_EXCLUSIVE, &rvp) == 0); 256 VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), 257 &crtime, sizeof (crtime))); 258 ZFS_TIME_DECODE(&zcp->zc_cmtime, crtime); 259 VN_URELE(rvp); 260 261 /* 262 * We're only faking the fact that we have a root of a filesystem for 263 * the sake of the GFS interfaces. Undo the flag manipulation it did 264 * for us. 265 */ 266 vp->v_vflag &= ~VV_ROOT; 267 268 zfsvfs->z_ctldir = vp; 269 270 VOP_UNLOCK(vp, 0); 271} 272 273/* 274 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted. 275 * There might still be more references if we were force unmounted, but only 276 * new zfs_inactive() calls can occur and they don't reference .zfs 277 */ 278void 279zfsctl_destroy(zfsvfs_t *zfsvfs) 280{ 281 VN_RELE(zfsvfs->z_ctldir); 282 zfsvfs->z_ctldir = NULL; 283} 284 285/* 286 * Given a root znode, retrieve the associated .zfs directory. 287 * Add a hold to the vnode and return it. 288 */ 289vnode_t * 290zfsctl_root(znode_t *zp) 291{ 292 ASSERT(zfs_has_ctldir(zp)); 293 VN_HOLD(zp->z_zfsvfs->z_ctldir); 294 return (zp->z_zfsvfs->z_ctldir); 295} 296 297/* 298 * Common open routine. Disallow any write access. 299 */ 300/* ARGSUSED */ 301static int 302zfsctl_common_open(struct vop_open_args *ap) 303{ 304 int flags = ap->a_mode; 305 306 if (flags & FWRITE) 307 return (SET_ERROR(EACCES)); 308 309 return (0); 310} 311 312/* 313 * Common close routine. Nothing to do here. 314 */ 315/* ARGSUSED */ 316static int 317zfsctl_common_close(struct vop_close_args *ap) 318{ 319 return (0); 320} 321 322/* 323 * Common access routine. Disallow writes. 324 */ 325/* ARGSUSED */ 326static int 327zfsctl_common_access(ap) 328 struct vop_access_args /* { 329 struct vnode *a_vp; 330 accmode_t a_accmode; 331 struct ucred *a_cred; 332 struct thread *a_td; 333 } */ *ap; 334{ 335 accmode_t accmode = ap->a_accmode; 336 337#ifdef TODO 338 if (flags & V_ACE_MASK) { 339 if (accmode & ACE_ALL_WRITE_PERMS) 340 return (SET_ERROR(EACCES)); 341 } else { 342#endif 343 if (accmode & VWRITE) 344 return (SET_ERROR(EACCES)); 345#ifdef TODO 346 } 347#endif 348 349 return (0); 350} 351 352/* 353 * Common getattr function. Fill in basic information. 354 */ 355static void 356zfsctl_common_getattr(vnode_t *vp, vattr_t *vap) 357{ 358 timestruc_t now; 359 360 vap->va_uid = 0; 361 vap->va_gid = 0; 362 vap->va_rdev = 0; 363 /* 364 * We are a purely virtual object, so we have no 365 * blocksize or allocated blocks. 366 */ 367 vap->va_blksize = 0; 368 vap->va_nblocks = 0; 369 vap->va_seq = 0; 370 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 371 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | 372 S_IROTH | S_IXOTH; 373 vap->va_type = VDIR; 374 /* 375 * We live in the now (for atime). 376 */ 377 gethrestime(&now); 378 vap->va_atime = now; 379 /* FreeBSD: Reset chflags(2) flags. */ 380 vap->va_flags = 0; 381} 382 383/*ARGSUSED*/ 384static int 385zfsctl_common_fid(ap) 386 struct vop_fid_args /* { 387 struct vnode *a_vp; 388 struct fid *a_fid; 389 } */ *ap; 390{ 391 vnode_t *vp = ap->a_vp; 392 fid_t *fidp = (void *)ap->a_fid; 393 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 394 zfsctl_node_t *zcp = vp->v_data; 395 uint64_t object = zcp->zc_id; 396 zfid_short_t *zfid; 397 int i; 398 399 ZFS_ENTER(zfsvfs); 400 401#ifdef illumos 402 if (fidp->fid_len < SHORT_FID_LEN) { 403 fidp->fid_len = SHORT_FID_LEN; 404 ZFS_EXIT(zfsvfs); 405 return (SET_ERROR(ENOSPC)); 406 } 407#endif 408 409 zfid = (zfid_short_t *)fidp; 410 411 zfid->zf_len = SHORT_FID_LEN; 412 413 for (i = 0; i < sizeof (zfid->zf_object); i++) 414 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 415 416 /* .zfs znodes always have a generation number of 0 */ 417 for (i = 0; i < sizeof (zfid->zf_gen); i++) 418 zfid->zf_gen[i] = 0; 419 420 ZFS_EXIT(zfsvfs); 421 return (0); 422} 423 424 425/*ARGSUSED*/ 426static int 427zfsctl_shares_fid(ap) 428 struct vop_fid_args /* { 429 struct vnode *a_vp; 430 struct fid *a_fid; 431 } */ *ap; 432{ 433 vnode_t *vp = ap->a_vp; 434 fid_t *fidp = (void *)ap->a_fid; 435 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 436 znode_t *dzp; 437 int error; 438 439 ZFS_ENTER(zfsvfs); 440 441 if (zfsvfs->z_shares_dir == 0) { 442 ZFS_EXIT(zfsvfs); 443 return (SET_ERROR(ENOTSUP)); 444 } 445 446 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 447 error = VOP_FID(ZTOV(dzp), fidp); 448 VN_RELE(ZTOV(dzp)); 449 } 450 451 ZFS_EXIT(zfsvfs); 452 return (error); 453} 454 455static int 456zfsctl_common_reclaim(ap) 457 struct vop_reclaim_args /* { 458 struct vnode *a_vp; 459 struct thread *a_td; 460 } */ *ap; 461{ 462 vnode_t *vp = ap->a_vp; 463 464 /* 465 * Destroy the vm object and flush associated pages. 466 */ 467 vnode_destroy_vobject(vp); 468 VI_LOCK(vp); 469 vp->v_data = NULL; 470 VI_UNLOCK(vp); 471 return (0); 472} 473 474/* 475 * .zfs inode namespace 476 * 477 * We need to generate unique inode numbers for all files and directories 478 * within the .zfs pseudo-filesystem. We use the following scheme: 479 * 480 * ENTRY ZFSCTL_INODE 481 * .zfs 1 482 * .zfs/snapshot 2 483 * .zfs/snapshot/<snap> objectid(snap) 484 */ 485 486#define ZFSCTL_INO_SNAP(id) (id) 487 488/* 489 * Get root directory attributes. 490 */ 491/* ARGSUSED */ 492static int 493zfsctl_root_getattr(ap) 494 struct vop_getattr_args /* { 495 struct vnode *a_vp; 496 struct vattr *a_vap; 497 struct ucred *a_cred; 498 } */ *ap; 499{ 500 struct vnode *vp = ap->a_vp; 501 struct vattr *vap = ap->a_vap; 502 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 503 zfsctl_node_t *zcp = vp->v_data; 504 505 ZFS_ENTER(zfsvfs); 506 vap->va_nodeid = ZFSCTL_INO_ROOT; 507 vap->va_nlink = vap->va_size = NROOT_ENTRIES; 508 vap->va_mtime = vap->va_ctime = zcp->zc_cmtime; 509 vap->va_birthtime = vap->va_ctime; 510 511 zfsctl_common_getattr(vp, vap); 512 ZFS_EXIT(zfsvfs); 513 514 return (0); 515} 516 517/* 518 * Special case the handling of "..". 519 */ 520/* ARGSUSED */ 521int 522zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp, 523 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 524 int *direntflags, pathname_t *realpnp) 525{ 526 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 527 int err; 528 529 /* 530 * No extended attributes allowed under .zfs 531 */ 532 if (flags & LOOKUP_XATTR) 533 return (SET_ERROR(EINVAL)); 534 535 ZFS_ENTER(zfsvfs); 536 537 if (strcmp(nm, "..") == 0) { 538 err = VFS_ROOT(dvp->v_vfsp, LK_EXCLUSIVE, vpp); 539 if (err == 0) 540 VOP_UNLOCK(*vpp, 0); 541 } else { 542 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir, 543 cr, ct, direntflags, realpnp); 544 } 545 546 ZFS_EXIT(zfsvfs); 547 548 return (err); 549} 550 551#ifdef illumos 552static int 553zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 554 caller_context_t *ct) 555{ 556 /* 557 * We only care about ACL_ENABLED so that libsec can 558 * display ACL correctly and not default to POSIX draft. 559 */ 560 if (cmd == _PC_ACL_ENABLED) { 561 *valp = _ACL_ACE_ENABLED; 562 return (0); 563 } 564 565 return (fs_pathconf(vp, cmd, valp, cr, ct)); 566} 567#endif /* illumos */ 568 569#ifdef illumos 570static const fs_operation_def_t zfsctl_tops_root[] = { 571 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 572 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 573 { VOPNAME_IOCTL, { .error = fs_inval } }, 574 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } }, 575 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 576 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } }, 577 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } }, 578 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 579 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 580 { VOPNAME_PATHCONF, { .vop_pathconf = zfsctl_pathconf } }, 581 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } }, 582 { NULL } 583}; 584#endif /* illumos */ 585 586/* 587 * Special case the handling of "..". 588 */ 589/* ARGSUSED */ 590int 591zfsctl_freebsd_root_lookup(ap) 592 struct vop_lookup_args /* { 593 struct vnode *a_dvp; 594 struct vnode **a_vpp; 595 struct componentname *a_cnp; 596 } */ *ap; 597{ 598 vnode_t *dvp = ap->a_dvp; 599 vnode_t **vpp = ap->a_vpp; 600 cred_t *cr = ap->a_cnp->cn_cred; 601 int flags = ap->a_cnp->cn_flags; 602 int nameiop = ap->a_cnp->cn_nameiop; 603 char nm[NAME_MAX + 1]; 604 int err; 605 int ltype; 606 607 if ((flags & ISLASTCN) && (nameiop == RENAME || nameiop == CREATE)) 608 return (EOPNOTSUPP); 609 610 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm)); 611 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1); 612 err = zfsctl_root_lookup(dvp, nm, vpp, NULL, 0, NULL, cr, NULL, NULL, NULL); 613 if (err == 0 && (nm[0] != '.' || nm[1] != '\0')) { 614 ltype = VOP_ISLOCKED(dvp); 615 if (flags & ISDOTDOT) { 616 VN_HOLD(*vpp); 617 VOP_UNLOCK(dvp, 0); 618 } 619 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 620 if (flags & ISDOTDOT) { 621 VN_RELE(*vpp); 622 vn_lock(dvp, ltype| LK_RETRY); 623 } 624 } 625 626 return (err); 627} 628 629static struct vop_vector zfsctl_ops_root = { 630 .vop_default = &default_vnodeops, 631 .vop_open = zfsctl_common_open, 632 .vop_close = zfsctl_common_close, 633 .vop_ioctl = VOP_EINVAL, 634 .vop_getattr = zfsctl_root_getattr, 635 .vop_access = zfsctl_common_access, 636 .vop_readdir = gfs_vop_readdir, 637 .vop_lookup = zfsctl_freebsd_root_lookup, 638 .vop_inactive = VOP_NULL, 639 .vop_reclaim = gfs_vop_reclaim, 640#ifdef TODO 641 .vop_pathconf = zfsctl_pathconf, 642#endif 643 .vop_fid = zfsctl_common_fid, 644}; 645 646/* 647 * Gets the full dataset name that corresponds to the given snapshot name 648 * Example: 649 * zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1" 650 */ 651static int 652zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname) 653{ 654 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os; 655 656 if (zfs_component_namecheck(name, NULL, NULL) != 0) 657 return (SET_ERROR(EILSEQ)); 658 dmu_objset_name(os, zname); 659 if (strlen(zname) + 1 + strlen(name) >= len) 660 return (SET_ERROR(ENAMETOOLONG)); 661 (void) strcat(zname, "@"); 662 (void) strcat(zname, name); 663 return (0); 664} 665 666static int 667zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr) 668{ 669 vnode_t *svp = sep->se_root; 670 int error; 671 672 ASSERT(vn_ismntpt(svp)); 673 674 /* this will be dropped by dounmount() */ 675 if ((error = vn_vfswlock(svp)) != 0) 676 return (error); 677 678#ifdef illumos 679 VN_HOLD(svp); 680 error = dounmount(vn_mountedvfs(svp), fflags, cr); 681 if (error) { 682 VN_RELE(svp); 683 return (error); 684 } 685 686 /* 687 * We can't use VN_RELE(), as that will try to invoke 688 * zfsctl_snapdir_inactive(), which would cause us to destroy 689 * the sd_lock mutex held by our caller. 690 */ 691 ASSERT(svp->v_count == 1); 692 gfs_vop_reclaim(svp, cr, NULL); 693 694 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 695 kmem_free(sep, sizeof (zfs_snapentry_t)); 696 697 return (0); 698#else 699 vfs_ref(vn_mountedvfs(svp)); 700 return (dounmount(vn_mountedvfs(svp), fflags, curthread)); 701#endif 702} 703 704#ifdef illumos 705static void 706zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm) 707{ 708 avl_index_t where; 709 vfs_t *vfsp; 710 refstr_t *pathref; 711 char newpath[MAXNAMELEN]; 712 char *tail; 713 714 ASSERT(MUTEX_HELD(&sdp->sd_lock)); 715 ASSERT(sep != NULL); 716 717 vfsp = vn_mountedvfs(sep->se_root); 718 ASSERT(vfsp != NULL); 719 720 vfs_lock_wait(vfsp); 721 722 /* 723 * Change the name in the AVL tree. 724 */ 725 avl_remove(&sdp->sd_snaps, sep); 726 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 727 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP); 728 (void) strcpy(sep->se_name, nm); 729 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL); 730 avl_insert(&sdp->sd_snaps, sep, where); 731 732 /* 733 * Change the current mountpoint info: 734 * - update the tail of the mntpoint path 735 * - update the tail of the resource path 736 */ 737 pathref = vfs_getmntpoint(vfsp); 738 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath)); 739 VERIFY((tail = strrchr(newpath, '/')) != NULL); 740 *(tail+1) = '\0'; 741 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath)); 742 (void) strcat(newpath, nm); 743 refstr_rele(pathref); 744 vfs_setmntpoint(vfsp, newpath, 0); 745 746 pathref = vfs_getresource(vfsp); 747 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath)); 748 VERIFY((tail = strrchr(newpath, '@')) != NULL); 749 *(tail+1) = '\0'; 750 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath)); 751 (void) strcat(newpath, nm); 752 refstr_rele(pathref); 753 vfs_setresource(vfsp, newpath, 0); 754 755 vfs_unlock(vfsp); 756} 757#endif /* illumos */ 758 759#ifdef illumos 760/*ARGSUSED*/ 761static int 762zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, 763 cred_t *cr, caller_context_t *ct, int flags) 764{ 765 zfsctl_snapdir_t *sdp = sdvp->v_data; 766 zfs_snapentry_t search, *sep; 767 zfsvfs_t *zfsvfs; 768 avl_index_t where; 769 char from[MAXNAMELEN], to[MAXNAMELEN]; 770 char real[MAXNAMELEN], fsname[MAXNAMELEN]; 771 int err; 772 773 zfsvfs = sdvp->v_vfsp->vfs_data; 774 ZFS_ENTER(zfsvfs); 775 776 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 777 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real, 778 MAXNAMELEN, NULL); 779 if (err == 0) { 780 snm = real; 781 } else if (err != ENOTSUP) { 782 ZFS_EXIT(zfsvfs); 783 return (err); 784 } 785 } 786 787 ZFS_EXIT(zfsvfs); 788 789 dmu_objset_name(zfsvfs->z_os, fsname); 790 791 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from); 792 if (err == 0) 793 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to); 794 if (err == 0) 795 err = zfs_secpolicy_rename_perms(from, to, cr); 796 if (err != 0) 797 return (err); 798 799 /* 800 * Cannot move snapshots out of the snapdir. 801 */ 802 if (sdvp != tdvp) 803 return (SET_ERROR(EINVAL)); 804 805 if (strcmp(snm, tnm) == 0) 806 return (0); 807 808 mutex_enter(&sdp->sd_lock); 809 810 search.se_name = (char *)snm; 811 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) { 812 mutex_exit(&sdp->sd_lock); 813 return (SET_ERROR(ENOENT)); 814 } 815 816 err = dsl_dataset_rename_snapshot(fsname, snm, tnm, 0); 817 if (err == 0) 818 zfsctl_rename_snap(sdp, sep, tnm); 819 820 mutex_exit(&sdp->sd_lock); 821 822 return (err); 823} 824#endif /* illumos */ 825 826#ifdef illumos 827/* ARGSUSED */ 828static int 829zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 830 caller_context_t *ct, int flags) 831{ 832 zfsctl_snapdir_t *sdp = dvp->v_data; 833 zfs_snapentry_t *sep; 834 zfs_snapentry_t search; 835 zfsvfs_t *zfsvfs; 836 char snapname[MAXNAMELEN]; 837 char real[MAXNAMELEN]; 838 int err; 839 840 zfsvfs = dvp->v_vfsp->vfs_data; 841 ZFS_ENTER(zfsvfs); 842 843 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 844 845 err = dmu_snapshot_realname(zfsvfs->z_os, name, real, 846 MAXNAMELEN, NULL); 847 if (err == 0) { 848 name = real; 849 } else if (err != ENOTSUP) { 850 ZFS_EXIT(zfsvfs); 851 return (err); 852 } 853 } 854 855 ZFS_EXIT(zfsvfs); 856 857 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname); 858 if (err == 0) 859 err = zfs_secpolicy_destroy_perms(snapname, cr); 860 if (err != 0) 861 return (err); 862 863 mutex_enter(&sdp->sd_lock); 864 865 search.se_name = name; 866 sep = avl_find(&sdp->sd_snaps, &search, NULL); 867 if (sep) { 868 avl_remove(&sdp->sd_snaps, sep); 869 err = zfsctl_unmount_snap(sep, MS_FORCE, cr); 870 if (err != 0) 871 avl_add(&sdp->sd_snaps, sep); 872 else 873 err = dsl_destroy_snapshot(snapname, B_FALSE); 874 } else { 875 err = SET_ERROR(ENOENT); 876 } 877 878 mutex_exit(&sdp->sd_lock); 879 880 return (err); 881} 882#endif /* illumos */ 883 884/* 885 * This creates a snapshot under '.zfs/snapshot'. 886 */ 887/* ARGSUSED */ 888static int 889zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, 890 cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp) 891{ 892 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 893 char name[MAXNAMELEN]; 894 int err; 895 static enum symfollow follow = NO_FOLLOW; 896 static enum uio_seg seg = UIO_SYSSPACE; 897 898 if (zfs_component_namecheck(dirname, NULL, NULL) != 0) 899 return (SET_ERROR(EILSEQ)); 900 901 dmu_objset_name(zfsvfs->z_os, name); 902 903 *vpp = NULL; 904 905 err = zfs_secpolicy_snapshot_perms(name, cr); 906 if (err != 0) 907 return (err); 908 909 if (err == 0) { 910 err = dmu_objset_snapshot_one(name, dirname); 911 if (err != 0) 912 return (err); 913 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp); 914 } 915 916 return (err); 917} 918 919static int 920zfsctl_freebsd_snapdir_mkdir(ap) 921 struct vop_mkdir_args /* { 922 struct vnode *a_dvp; 923 struct vnode **a_vpp; 924 struct componentname *a_cnp; 925 struct vattr *a_vap; 926 } */ *ap; 927{ 928 929 ASSERT(ap->a_cnp->cn_flags & SAVENAME); 930 931 return (zfsctl_snapdir_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, NULL, 932 ap->a_vpp, ap->a_cnp->cn_cred, NULL, 0, NULL)); 933} 934 935/* 936 * Lookup entry point for the 'snapshot' directory. Try to open the 937 * snapshot if it exist, creating the pseudo filesystem vnode as necessary. 938 * Perform a mount of the associated dataset on top of the vnode. 939 */ 940/* ARGSUSED */ 941int 942zfsctl_snapdir_lookup(ap) 943 struct vop_lookup_args /* { 944 struct vnode *a_dvp; 945 struct vnode **a_vpp; 946 struct componentname *a_cnp; 947 } */ *ap; 948{ 949 vnode_t *dvp = ap->a_dvp; 950 vnode_t **vpp = ap->a_vpp; 951 struct componentname *cnp = ap->a_cnp; 952 char nm[NAME_MAX + 1]; 953 zfsctl_snapdir_t *sdp = dvp->v_data; 954 objset_t *snap; 955 char snapname[MAXNAMELEN]; 956 char real[MAXNAMELEN]; 957 char *mountpoint; 958 zfs_snapentry_t *sep, search; 959 size_t mountpoint_len; 960 avl_index_t where; 961 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 962 int err; 963 int ltype, flags = 0; 964 965 /* 966 * No extended attributes allowed under .zfs 967 */ 968 if (flags & LOOKUP_XATTR) 969 return (SET_ERROR(EINVAL)); 970 ASSERT(ap->a_cnp->cn_namelen < sizeof(nm)); 971 strlcpy(nm, ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen + 1); 972 973 ASSERT(dvp->v_type == VDIR); 974 975 *vpp = NULL; 976 977 /* 978 * If we get a recursive call, that means we got called 979 * from the domount() code while it was trying to look up the 980 * spec (which looks like a local path for zfs). We need to 981 * add some flag to domount() to tell it not to do this lookup. 982 */ 983 if (MUTEX_HELD(&sdp->sd_lock)) 984 return (SET_ERROR(ENOENT)); 985 986 ZFS_ENTER(zfsvfs); 987 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) { 988 ZFS_EXIT(zfsvfs); 989 return (0); 990 } 991 992 if (flags & FIGNORECASE) { 993 boolean_t conflict = B_FALSE; 994 995 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real, 996 MAXNAMELEN, &conflict); 997 if (err == 0) { 998 strlcpy(nm, real, sizeof(nm)); 999 } else if (err != ENOTSUP) { 1000 ZFS_EXIT(zfsvfs); 1001 return (err); 1002 } 1003#if 0 1004 if (realpnp) 1005 (void) strlcpy(realpnp->pn_buf, nm, 1006 realpnp->pn_bufsize); 1007 if (conflict && direntflags) 1008 *direntflags = ED_CASE_CONFLICT; 1009#endif 1010 } 1011 1012 mutex_enter(&sdp->sd_lock); 1013 search.se_name = (char *)nm; 1014 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) { 1015 *vpp = sep->se_root; 1016 VN_HOLD(*vpp); 1017 err = traverse(vpp, LK_EXCLUSIVE | LK_RETRY); 1018 if (err != 0) { 1019 VN_RELE(*vpp); 1020 *vpp = NULL; 1021 } else if (*vpp == sep->se_root) { 1022 /* 1023 * The snapshot was unmounted behind our backs, 1024 * try to remount it. 1025 */ 1026 VERIFY(zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname) == 0); 1027 goto domount; 1028 } else { 1029 /* 1030 * VROOT was set during the traverse call. We need 1031 * to clear it since we're pretending to be part 1032 * of our parent's vfs. 1033 */ 1034 (*vpp)->v_flag &= ~VROOT; 1035 } 1036 mutex_exit(&sdp->sd_lock); 1037 ZFS_EXIT(zfsvfs); 1038 return (err); 1039 } 1040 1041 /* 1042 * The requested snapshot is not currently mounted, look it up. 1043 */ 1044 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname); 1045 if (err != 0) { 1046 mutex_exit(&sdp->sd_lock); 1047 ZFS_EXIT(zfsvfs); 1048 /* 1049 * handle "ls *" or "?" in a graceful manner, 1050 * forcing EILSEQ to ENOENT. 1051 * Since shell ultimately passes "*" or "?" as name to lookup 1052 */ 1053 return (err == EILSEQ ? ENOENT : err); 1054 } 1055 if (dmu_objset_hold(snapname, FTAG, &snap) != 0) { 1056 mutex_exit(&sdp->sd_lock); 1057#ifdef illumos 1058 ZFS_EXIT(zfsvfs); 1059 return (SET_ERROR(ENOENT)); 1060#else /* !illumos */ 1061 /* Translate errors and add SAVENAME when needed. */ 1062 if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) { 1063 err = EJUSTRETURN; 1064 cnp->cn_flags |= SAVENAME; 1065 } else { 1066 err = SET_ERROR(ENOENT); 1067 } 1068 ZFS_EXIT(zfsvfs); 1069 return (err); 1070#endif /* illumos */ 1071 } 1072 1073 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP); 1074 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP); 1075 (void) strcpy(sep->se_name, nm); 1076 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap)); 1077 VN_HOLD(*vpp); 1078 avl_insert(&sdp->sd_snaps, sep, where); 1079 1080 dmu_objset_rele(snap, FTAG); 1081domount: 1082 mountpoint_len = strlen(dvp->v_vfsp->mnt_stat.f_mntonname) + 1083 strlen("/" ZFS_CTLDIR_NAME "/snapshot/") + strlen(nm) + 1; 1084 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP); 1085 (void) snprintf(mountpoint, mountpoint_len, 1086 "%s/" ZFS_CTLDIR_NAME "/snapshot/%s", 1087 dvp->v_vfsp->mnt_stat.f_mntonname, nm); 1088 err = mount_snapshot(curthread, vpp, "zfs", mountpoint, snapname, 0); 1089 kmem_free(mountpoint, mountpoint_len); 1090 if (err == 0) { 1091 /* 1092 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>. 1093 * 1094 * This is where we lie about our v_vfsp in order to 1095 * make .zfs/snapshot/<snapname> accessible over NFS 1096 * without requiring manual mounts of <snapname>. 1097 */ 1098 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs); 1099 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs; 1100 } 1101 mutex_exit(&sdp->sd_lock); 1102 ZFS_EXIT(zfsvfs); 1103 1104#ifdef illumos 1105 /* 1106 * If we had an error, drop our hold on the vnode and 1107 * zfsctl_snapshot_inactive() will clean up. 1108 */ 1109 if (err != 0) { 1110 VN_RELE(*vpp); 1111 *vpp = NULL; 1112 } 1113#else 1114 if (err != 0) 1115 *vpp = NULL; 1116#endif 1117 return (err); 1118} 1119 1120/* ARGSUSED */ 1121int 1122zfsctl_shares_lookup(ap) 1123 struct vop_lookup_args /* { 1124 struct vnode *a_dvp; 1125 struct vnode **a_vpp; 1126 struct componentname *a_cnp; 1127 } */ *ap; 1128{ 1129 vnode_t *dvp = ap->a_dvp; 1130 vnode_t **vpp = ap->a_vpp; 1131 struct componentname *cnp = ap->a_cnp; 1132 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 1133 char nm[NAME_MAX + 1]; 1134 znode_t *dzp; 1135 int error; 1136 1137 ZFS_ENTER(zfsvfs); 1138 1139 ASSERT(cnp->cn_namelen < sizeof(nm)); 1140 strlcpy(nm, cnp->cn_nameptr, cnp->cn_namelen + 1); 1141 1142 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) { 1143 ZFS_EXIT(zfsvfs); 1144 return (0); 1145 } 1146 1147 if (zfsvfs->z_shares_dir == 0) { 1148 ZFS_EXIT(zfsvfs); 1149 return (SET_ERROR(ENOTSUP)); 1150 } 1151 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 1152 error = VOP_LOOKUP(ZTOV(dzp), vpp, cnp); 1153 VN_RELE(ZTOV(dzp)); 1154 } 1155 1156 ZFS_EXIT(zfsvfs); 1157 1158 return (error); 1159} 1160 1161/* ARGSUSED */ 1162static int 1163zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp, 1164 offset_t *offp, offset_t *nextp, void *data, int flags) 1165{ 1166 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1167 char snapname[MAXNAMELEN]; 1168 uint64_t id, cookie; 1169 boolean_t case_conflict; 1170 int error; 1171 1172 ZFS_ENTER(zfsvfs); 1173 1174 cookie = *offp; 1175 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG); 1176 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id, 1177 &cookie, &case_conflict); 1178 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG); 1179 if (error) { 1180 ZFS_EXIT(zfsvfs); 1181 if (error == ENOENT) { 1182 *eofp = 1; 1183 return (0); 1184 } 1185 return (error); 1186 } 1187 1188 if (flags & V_RDDIR_ENTFLAGS) { 1189 edirent_t *eodp = dp; 1190 1191 (void) strcpy(eodp->ed_name, snapname); 1192 eodp->ed_ino = ZFSCTL_INO_SNAP(id); 1193 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0; 1194 } else { 1195 struct dirent64 *odp = dp; 1196 1197 (void) strcpy(odp->d_name, snapname); 1198 odp->d_ino = ZFSCTL_INO_SNAP(id); 1199 } 1200 *nextp = cookie; 1201 1202 ZFS_EXIT(zfsvfs); 1203 1204 return (0); 1205} 1206 1207/* ARGSUSED */ 1208static int 1209zfsctl_shares_readdir(ap) 1210 struct vop_readdir_args /* { 1211 struct vnode *a_vp; 1212 struct uio *a_uio; 1213 struct ucred *a_cred; 1214 int *a_eofflag; 1215 int *a_ncookies; 1216 u_long **a_cookies; 1217 } */ *ap; 1218{ 1219 vnode_t *vp = ap->a_vp; 1220 uio_t *uiop = ap->a_uio; 1221 cred_t *cr = ap->a_cred; 1222 int *eofp = ap->a_eofflag; 1223 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1224 znode_t *dzp; 1225 int error; 1226 1227 ZFS_ENTER(zfsvfs); 1228 1229 if (zfsvfs->z_shares_dir == 0) { 1230 ZFS_EXIT(zfsvfs); 1231 return (SET_ERROR(ENOTSUP)); 1232 } 1233 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 1234 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY); 1235 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ap->a_ncookies, ap->a_cookies); 1236 VN_URELE(ZTOV(dzp)); 1237 } else { 1238 *eofp = 1; 1239 error = SET_ERROR(ENOENT); 1240 } 1241 1242 ZFS_EXIT(zfsvfs); 1243 return (error); 1244} 1245 1246/* 1247 * pvp is the '.zfs' directory (zfsctl_node_t). 1248 * 1249 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t). 1250 * 1251 * This function is the callback to create a GFS vnode for '.zfs/snapshot' 1252 * when a lookup is performed on .zfs for "snapshot". 1253 */ 1254vnode_t * 1255zfsctl_mknode_snapdir(vnode_t *pvp) 1256{ 1257 vnode_t *vp; 1258 zfsctl_snapdir_t *sdp; 1259 1260 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp, pvp->v_vfsp, 1261 &zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN, 1262 zfsctl_snapdir_readdir_cb, NULL); 1263 sdp = vp->v_data; 1264 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR; 1265 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime; 1266 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL); 1267 avl_create(&sdp->sd_snaps, snapentry_compare, 1268 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node)); 1269 VOP_UNLOCK(vp, 0); 1270 return (vp); 1271} 1272 1273vnode_t * 1274zfsctl_mknode_shares(vnode_t *pvp) 1275{ 1276 vnode_t *vp; 1277 zfsctl_node_t *sdp; 1278 1279 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp, 1280 &zfsctl_ops_shares, NULL, NULL, MAXNAMELEN, 1281 NULL, NULL); 1282 sdp = vp->v_data; 1283 sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime; 1284 VOP_UNLOCK(vp, 0); 1285 return (vp); 1286 1287} 1288 1289/* ARGSUSED */ 1290static int 1291zfsctl_shares_getattr(ap) 1292 struct vop_getattr_args /* { 1293 struct vnode *a_vp; 1294 struct vattr *a_vap; 1295 struct ucred *a_cred; 1296 struct thread *a_td; 1297 } */ *ap; 1298{ 1299 vnode_t *vp = ap->a_vp; 1300 vattr_t *vap = ap->a_vap; 1301 cred_t *cr = ap->a_cred; 1302 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1303 znode_t *dzp; 1304 int error; 1305 1306 ZFS_ENTER(zfsvfs); 1307 if (zfsvfs->z_shares_dir == 0) { 1308 ZFS_EXIT(zfsvfs); 1309 return (SET_ERROR(ENOTSUP)); 1310 } 1311 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 1312 vn_lock(ZTOV(dzp), LK_SHARED | LK_RETRY); 1313 error = VOP_GETATTR(ZTOV(dzp), vap, cr); 1314 VN_URELE(ZTOV(dzp)); 1315 } 1316 ZFS_EXIT(zfsvfs); 1317 return (error); 1318 1319 1320} 1321 1322/* ARGSUSED */ 1323static int 1324zfsctl_snapdir_getattr(ap) 1325 struct vop_getattr_args /* { 1326 struct vnode *a_vp; 1327 struct vattr *a_vap; 1328 struct ucred *a_cred; 1329 } */ *ap; 1330{ 1331 vnode_t *vp = ap->a_vp; 1332 vattr_t *vap = ap->a_vap; 1333 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 1334 zfsctl_snapdir_t *sdp = vp->v_data; 1335 1336 ZFS_ENTER(zfsvfs); 1337 zfsctl_common_getattr(vp, vap); 1338 vap->va_nodeid = gfs_file_inode(vp); 1339 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2; 1340 vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os); 1341 vap->va_birthtime = vap->va_ctime; 1342 ZFS_EXIT(zfsvfs); 1343 1344 return (0); 1345} 1346 1347/* ARGSUSED */ 1348static int 1349zfsctl_snapdir_inactive(ap) 1350 struct vop_inactive_args /* { 1351 struct vnode *a_vp; 1352 struct thread *a_td; 1353 } */ *ap; 1354{ 1355 vnode_t *vp = ap->a_vp; 1356 zfsctl_snapdir_t *sdp = vp->v_data; 1357 zfs_snapentry_t *sep; 1358 1359 /* 1360 * On forced unmount we have to free snapshots from here. 1361 */ 1362 mutex_enter(&sdp->sd_lock); 1363 while ((sep = avl_first(&sdp->sd_snaps)) != NULL) { 1364 avl_remove(&sdp->sd_snaps, sep); 1365 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 1366 kmem_free(sep, sizeof (zfs_snapentry_t)); 1367 } 1368 mutex_exit(&sdp->sd_lock); 1369 gfs_dir_inactive(vp); 1370 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0); 1371 mutex_destroy(&sdp->sd_lock); 1372 avl_destroy(&sdp->sd_snaps); 1373 kmem_free(sdp, sizeof (zfsctl_snapdir_t)); 1374 1375 return (0); 1376} 1377 1378#ifdef illumos 1379static const fs_operation_def_t zfsctl_tops_snapdir[] = { 1380 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 1381 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 1382 { VOPNAME_IOCTL, { .error = fs_inval } }, 1383 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_snapdir_getattr } }, 1384 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 1385 { VOPNAME_RENAME, { .vop_rename = zfsctl_snapdir_rename } }, 1386 { VOPNAME_RMDIR, { .vop_rmdir = zfsctl_snapdir_remove } }, 1387 { VOPNAME_MKDIR, { .vop_mkdir = zfsctl_snapdir_mkdir } }, 1388 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } }, 1389 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_snapdir_lookup } }, 1390 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 1391 { VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapdir_inactive } }, 1392 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } }, 1393 { NULL } 1394}; 1395 1396static const fs_operation_def_t zfsctl_tops_shares[] = { 1397 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } }, 1398 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } }, 1399 { VOPNAME_IOCTL, { .error = fs_inval } }, 1400 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_shares_getattr } }, 1401 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } }, 1402 { VOPNAME_READDIR, { .vop_readdir = zfsctl_shares_readdir } }, 1403 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_shares_lookup } }, 1404 { VOPNAME_SEEK, { .vop_seek = fs_seek } }, 1405 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 1406 { VOPNAME_FID, { .vop_fid = zfsctl_shares_fid } }, 1407 { NULL } 1408}; 1409#else /* !illumos */ 1410static struct vop_vector zfsctl_ops_snapdir = { 1411 .vop_default = &default_vnodeops, 1412 .vop_open = zfsctl_common_open, 1413 .vop_close = zfsctl_common_close, 1414 .vop_ioctl = VOP_EINVAL, 1415 .vop_getattr = zfsctl_snapdir_getattr, 1416 .vop_access = zfsctl_common_access, 1417 .vop_mkdir = zfsctl_freebsd_snapdir_mkdir, 1418 .vop_readdir = gfs_vop_readdir, 1419 .vop_lookup = zfsctl_snapdir_lookup, 1420 .vop_inactive = zfsctl_snapdir_inactive, 1421 .vop_reclaim = zfsctl_common_reclaim, 1422 .vop_fid = zfsctl_common_fid, 1423}; 1424 1425static struct vop_vector zfsctl_ops_shares = { 1426 .vop_default = &default_vnodeops, 1427 .vop_open = zfsctl_common_open, 1428 .vop_close = zfsctl_common_close, 1429 .vop_ioctl = VOP_EINVAL, 1430 .vop_getattr = zfsctl_shares_getattr, 1431 .vop_access = zfsctl_common_access, 1432 .vop_readdir = zfsctl_shares_readdir, 1433 .vop_lookup = zfsctl_shares_lookup, 1434 .vop_inactive = VOP_NULL, 1435 .vop_reclaim = gfs_vop_reclaim, 1436 .vop_fid = zfsctl_shares_fid, 1437}; 1438#endif /* illumos */ 1439 1440/* 1441 * pvp is the GFS vnode '.zfs/snapshot'. 1442 * 1443 * This creates a GFS node under '.zfs/snapshot' representing each 1444 * snapshot. This newly created GFS node is what we mount snapshot 1445 * vfs_t's ontop of. 1446 */ 1447static vnode_t * 1448zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset) 1449{ 1450 vnode_t *vp; 1451 zfsctl_node_t *zcp; 1452 1453 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp, pvp->v_vfsp, 1454 &zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL); 1455 VN_HOLD(vp); 1456 zcp = vp->v_data; 1457 zcp->zc_id = objset; 1458 VOP_UNLOCK(vp, 0); 1459 1460 return (vp); 1461} 1462 1463 1464static int 1465zfsctl_snapshot_reclaim(ap) 1466 struct vop_inactive_args /* { 1467 struct vnode *a_vp; 1468 struct thread *a_td; 1469 } */ *ap; 1470{ 1471 vnode_t *vp = ap->a_vp; 1472 cred_t *cr = ap->a_td->td_ucred; 1473 struct vop_reclaim_args iap; 1474 zfsctl_snapdir_t *sdp; 1475 zfs_snapentry_t *sep, *next; 1476 int locked; 1477 vnode_t *dvp; 1478 1479 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0); 1480 sdp = dvp->v_data; 1481 VOP_UNLOCK(dvp, 0); 1482 /* this may already have been unmounted */ 1483 if (sdp == NULL) { 1484 VN_RELE(dvp); 1485 return (0); 1486 } 1487 if (!(locked = MUTEX_HELD(&sdp->sd_lock))) 1488 mutex_enter(&sdp->sd_lock); 1489 1490 ASSERT(!vn_ismntpt(vp)); 1491 1492 sep = avl_first(&sdp->sd_snaps); 1493 while (sep != NULL) { 1494 next = AVL_NEXT(&sdp->sd_snaps, sep); 1495 1496 if (sep->se_root == vp) { 1497 avl_remove(&sdp->sd_snaps, sep); 1498 kmem_free(sep->se_name, strlen(sep->se_name) + 1); 1499 kmem_free(sep, sizeof (zfs_snapentry_t)); 1500 break; 1501 } 1502 sep = next; 1503 } 1504 ASSERT(sep != NULL); 1505 1506 if (!locked) 1507 mutex_exit(&sdp->sd_lock); 1508 VN_RELE(dvp); 1509 1510 /* 1511 * Dispose of the vnode for the snapshot mount point. 1512 * This is safe to do because once this entry has been removed 1513 * from the AVL tree, it can't be found again, so cannot become 1514 * "active". If we lookup the same name again we will end up 1515 * creating a new vnode. 1516 */ 1517 iap.a_vp = vp; 1518 gfs_vop_reclaim(&iap); 1519 return (0); 1520 1521} 1522 1523static int 1524zfsctl_traverse_begin(vnode_t **vpp, int lktype) 1525{ 1526 1527 VN_HOLD(*vpp); 1528 /* Snapshot should be already mounted, but just in case. */ 1529 if (vn_mountedvfs(*vpp) == NULL) 1530 return (ENOENT); 1531 return (traverse(vpp, lktype)); 1532} 1533 1534static void 1535zfsctl_traverse_end(vnode_t *vp, int err) 1536{ 1537 1538 if (err == 0) 1539 vput(vp); 1540 else 1541 VN_RELE(vp); 1542} 1543 1544static int 1545zfsctl_snapshot_getattr(ap) 1546 struct vop_getattr_args /* { 1547 struct vnode *a_vp; 1548 struct vattr *a_vap; 1549 struct ucred *a_cred; 1550 } */ *ap; 1551{ 1552 vnode_t *vp = ap->a_vp; 1553 int err; 1554 1555 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY); 1556 if (err == 0) 1557 err = VOP_GETATTR(vp, ap->a_vap, ap->a_cred); 1558 zfsctl_traverse_end(vp, err); 1559 return (err); 1560} 1561 1562static int 1563zfsctl_snapshot_fid(ap) 1564 struct vop_fid_args /* { 1565 struct vnode *a_vp; 1566 struct fid *a_fid; 1567 } */ *ap; 1568{ 1569 vnode_t *vp = ap->a_vp; 1570 int err; 1571 1572 err = zfsctl_traverse_begin(&vp, LK_SHARED | LK_RETRY); 1573 if (err == 0) 1574 err = VOP_VPTOFH(vp, (void *)ap->a_fid); 1575 zfsctl_traverse_end(vp, err); 1576 return (err); 1577} 1578 1579static int 1580zfsctl_snapshot_lookup(ap) 1581 struct vop_lookup_args /* { 1582 struct vnode *a_dvp; 1583 struct vnode **a_vpp; 1584 struct componentname *a_cnp; 1585 } */ *ap; 1586{ 1587 vnode_t *dvp = ap->a_dvp; 1588 vnode_t **vpp = ap->a_vpp; 1589 struct componentname *cnp = ap->a_cnp; 1590 cred_t *cr = ap->a_cnp->cn_cred; 1591 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data; 1592 int error; 1593 1594 if (cnp->cn_namelen != 2 || cnp->cn_nameptr[0] != '.' || 1595 cnp->cn_nameptr[1] != '.') { 1596 return (ENOENT); 1597 } 1598 1599 ASSERT(dvp->v_type == VDIR); 1600 ASSERT(zfsvfs->z_ctldir != NULL); 1601 1602 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", vpp, 1603 NULL, 0, NULL, cr, NULL, NULL, NULL); 1604 if (error == 0) { 1605 int ltype = VOP_ISLOCKED(dvp); 1606 VN_HOLD(*vpp); 1607 VOP_UNLOCK(dvp, 0); 1608 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 1609 VN_RELE(*vpp); 1610 vn_lock(dvp, ltype | LK_RETRY); 1611 } 1612 1613 return (error); 1614} 1615 1616static int 1617zfsctl_snapshot_vptocnp(struct vop_vptocnp_args *ap) 1618{ 1619 zfsvfs_t *zfsvfs = ap->a_vp->v_vfsp->vfs_data; 1620 vnode_t *dvp, *vp; 1621 zfsctl_snapdir_t *sdp; 1622 zfs_snapentry_t *sep; 1623 int error; 1624 1625 ASSERT(zfsvfs->z_ctldir != NULL); 1626 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 1627 NULL, 0, NULL, kcred, NULL, NULL, NULL); 1628 if (error != 0) 1629 return (error); 1630 sdp = dvp->v_data; 1631 1632 mutex_enter(&sdp->sd_lock); 1633 sep = avl_first(&sdp->sd_snaps); 1634 while (sep != NULL) { 1635 vp = sep->se_root; 1636 if (vp == ap->a_vp) 1637 break; 1638 sep = AVL_NEXT(&sdp->sd_snaps, sep); 1639 } 1640 if (sep == NULL) { 1641 mutex_exit(&sdp->sd_lock); 1642 error = ENOENT; 1643 } else { 1644 size_t len; 1645 1646 len = strlen(sep->se_name); 1647 *ap->a_buflen -= len; 1648 bcopy(sep->se_name, ap->a_buf + *ap->a_buflen, len); 1649 mutex_exit(&sdp->sd_lock); 1650 vref(dvp); 1651 *ap->a_vpp = dvp; 1652 } 1653 VN_RELE(dvp); 1654 1655 return (error); 1656} 1657 1658/* 1659 * These VP's should never see the light of day. They should always 1660 * be covered. 1661 */ 1662static struct vop_vector zfsctl_ops_snapshot = { 1663 .vop_default = &default_vnodeops, 1664 .vop_inactive = VOP_NULL, 1665 .vop_lookup = zfsctl_snapshot_lookup, 1666 .vop_reclaim = zfsctl_snapshot_reclaim, 1667 .vop_getattr = zfsctl_snapshot_getattr, 1668 .vop_fid = zfsctl_snapshot_fid, 1669 .vop_vptocnp = zfsctl_snapshot_vptocnp, 1670}; 1671 1672int 1673zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp) 1674{ 1675 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1676 vnode_t *dvp, *vp; 1677 zfsctl_snapdir_t *sdp; 1678 zfsctl_node_t *zcp; 1679 zfs_snapentry_t *sep; 1680 int error; 1681 1682 ASSERT(zfsvfs->z_ctldir != NULL); 1683 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 1684 NULL, 0, NULL, kcred, NULL, NULL, NULL); 1685 if (error != 0) 1686 return (error); 1687 sdp = dvp->v_data; 1688 1689 mutex_enter(&sdp->sd_lock); 1690 sep = avl_first(&sdp->sd_snaps); 1691 while (sep != NULL) { 1692 vp = sep->se_root; 1693 zcp = vp->v_data; 1694 if (zcp->zc_id == objsetid) 1695 break; 1696 1697 sep = AVL_NEXT(&sdp->sd_snaps, sep); 1698 } 1699 1700 if (sep != NULL) { 1701 VN_HOLD(vp); 1702 /* 1703 * Return the mounted root rather than the covered mount point. 1704 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid> 1705 * and returns the ZFS vnode mounted on top of the GFS node. 1706 * This ZFS vnode is the root of the vfs for objset 'objsetid'. 1707 */ 1708 error = traverse(&vp, LK_SHARED | LK_RETRY); 1709 if (error == 0) { 1710 if (vp == sep->se_root) 1711 error = SET_ERROR(EINVAL); 1712 else 1713 *zfsvfsp = VTOZ(vp)->z_zfsvfs; 1714 } 1715 mutex_exit(&sdp->sd_lock); 1716 if (error == 0) 1717 VN_URELE(vp); 1718 else 1719 VN_RELE(vp); 1720 } else { 1721 error = SET_ERROR(EINVAL); 1722 mutex_exit(&sdp->sd_lock); 1723 } 1724 1725 VN_RELE(dvp); 1726 1727 return (error); 1728} 1729 1730/* 1731 * Unmount any snapshots for the given filesystem. This is called from 1732 * zfs_umount() - if we have a ctldir, then go through and unmount all the 1733 * snapshots. 1734 */ 1735int 1736zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr) 1737{ 1738 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1739 vnode_t *dvp; 1740 zfsctl_snapdir_t *sdp; 1741 zfs_snapentry_t *sep, *next; 1742 int error; 1743 1744 ASSERT(zfsvfs->z_ctldir != NULL); 1745 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp, 1746 NULL, 0, NULL, cr, NULL, NULL, NULL); 1747 if (error != 0) 1748 return (error); 1749 sdp = dvp->v_data; 1750 1751 mutex_enter(&sdp->sd_lock); 1752 1753 sep = avl_first(&sdp->sd_snaps); 1754 while (sep != NULL) { 1755 next = AVL_NEXT(&sdp->sd_snaps, sep); 1756 1757 /* 1758 * If this snapshot is not mounted, then it must 1759 * have just been unmounted by somebody else, and 1760 * will be cleaned up by zfsctl_snapdir_inactive(). 1761 */ 1762 if (vn_ismntpt(sep->se_root)) { 1763 error = zfsctl_unmount_snap(sep, fflags, cr); 1764 if (error) { 1765 avl_index_t where; 1766 1767 /* 1768 * Before reinserting snapshot to the tree, 1769 * check if it was actually removed. For example 1770 * when snapshot mount point is busy, we will 1771 * have an error here, but there will be no need 1772 * to reinsert snapshot. 1773 */ 1774 if (avl_find(&sdp->sd_snaps, sep, &where) == NULL) 1775 avl_insert(&sdp->sd_snaps, sep, where); 1776 break; 1777 } 1778 } 1779 sep = next; 1780 } 1781 1782 mutex_exit(&sdp->sd_lock); 1783 VN_RELE(dvp); 1784 1785 return (error); 1786} 1787