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