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