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 https://opensource.org/licenses/CDDL-1.0. 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 * 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (C) 2011 Lawrence Livermore National Security, LLC. 25 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). 26 * LLNL-CODE-403049. 27 * Rewritten for Linux by: 28 * Rohan Puri <rohan.puri15@gmail.com> 29 * Brian Behlendorf <behlendorf1@llnl.gov> 30 * Copyright (c) 2013 by Delphix. All rights reserved. 31 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved. 32 * Copyright (c) 2018 George Melikov. All Rights Reserved. 33 * Copyright (c) 2019 Datto, Inc. All rights reserved. 34 * Copyright (c) 2020 The MathWorks, Inc. All rights reserved. 35 */ 36 37 /* 38 * ZFS control directory (a.k.a. ".zfs") 39 * 40 * This directory provides a common location for all ZFS meta-objects. 41 * Currently, this is only the 'snapshot' and 'shares' directory, but this may 42 * expand in the future. The elements are built dynamically, as the hierarchy 43 * does not actually exist on disk. 44 * 45 * For 'snapshot', we don't want to have all snapshots always mounted, because 46 * this would take up a huge amount of space in /etc/mnttab. We have three 47 * types of objects: 48 * 49 * ctldir ------> snapshotdir -------> snapshot 50 * | 51 * | 52 * V 53 * mounted fs 54 * 55 * The 'snapshot' node contains just enough information to lookup '..' and act 56 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we 57 * perform an automount of the underlying filesystem and return the 58 * corresponding inode. 59 * 60 * All mounts are handled automatically by an user mode helper which invokes 61 * the mount procedure. Unmounts are handled by allowing the mount 62 * point to expire so the kernel may automatically unmount it. 63 * 64 * The '.zfs', '.zfs/snapshot', and all directories created under 65 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') all share the same 66 * zfsvfs_t as the head filesystem (what '.zfs' lives under). 67 * 68 * File systems mounted on top of the '.zfs/snapshot/<snapname>' paths 69 * (ie: snapshots) are complete ZFS filesystems and have their own unique 70 * zfsvfs_t. However, the fsid reported by these mounts will be the same 71 * as that used by the parent zfsvfs_t to make NFS happy. 72 */ 73 74 #include <sys/types.h> 75 #include <sys/param.h> 76 #include <sys/time.h> 77 #include <sys/sysmacros.h> 78 #include <sys/pathname.h> 79 #include <sys/vfs.h> 80 #include <sys/zfs_ctldir.h> 81 #include <sys/zfs_ioctl.h> 82 #include <sys/zfs_vfsops.h> 83 #include <sys/zfs_vnops.h> 84 #include <sys/stat.h> 85 #include <sys/dmu.h> 86 #include <sys/dmu_objset.h> 87 #include <sys/dsl_destroy.h> 88 #include <sys/dsl_deleg.h> 89 #include <sys/zpl.h> 90 #include <sys/mntent.h> 91 #include "zfs_namecheck.h" 92 93 /* 94 * Two AVL trees are maintained which contain all currently automounted 95 * snapshots. Every automounted snapshots maps to a single zfs_snapentry_t 96 * entry which MUST: 97 * 98 * - be attached to both trees, and 99 * - be unique, no duplicate entries are allowed. 100 * 101 * The zfs_snapshots_by_name tree is indexed by the full dataset name 102 * while the zfs_snapshots_by_objsetid tree is indexed by the unique 103 * objsetid. This allows for fast lookups either by name or objsetid. 104 */ 105 static avl_tree_t zfs_snapshots_by_name; 106 static avl_tree_t zfs_snapshots_by_objsetid; 107 static krwlock_t zfs_snapshot_lock; 108 109 /* 110 * Control Directory Tunables (.zfs) 111 */ 112 int zfs_expire_snapshot = ZFSCTL_EXPIRE_SNAPSHOT; 113 static int zfs_admin_snapshot = 0; 114 115 typedef struct { 116 char *se_name; /* full snapshot name */ 117 char *se_path; /* full mount path */ 118 spa_t *se_spa; /* pool spa */ 119 uint64_t se_objsetid; /* snapshot objset id */ 120 struct dentry *se_root_dentry; /* snapshot root dentry */ 121 krwlock_t se_taskqid_lock; /* scheduled unmount taskqid lock */ 122 taskqid_t se_taskqid; /* scheduled unmount taskqid */ 123 avl_node_t se_node_name; /* zfs_snapshots_by_name link */ 124 avl_node_t se_node_objsetid; /* zfs_snapshots_by_objsetid link */ 125 zfs_refcount_t se_refcount; /* reference count */ 126 } zfs_snapentry_t; 127 128 static void zfsctl_snapshot_unmount_delay_impl(zfs_snapentry_t *se, int delay); 129 130 /* 131 * Allocate a new zfs_snapentry_t being careful to make a copy of the 132 * the snapshot name and provided mount point. No reference is taken. 133 */ 134 static zfs_snapentry_t * 135 zfsctl_snapshot_alloc(const char *full_name, const char *full_path, spa_t *spa, 136 uint64_t objsetid, struct dentry *root_dentry) 137 { 138 zfs_snapentry_t *se; 139 140 se = kmem_zalloc(sizeof (zfs_snapentry_t), KM_SLEEP); 141 142 se->se_name = kmem_strdup(full_name); 143 se->se_path = kmem_strdup(full_path); 144 se->se_spa = spa; 145 se->se_objsetid = objsetid; 146 se->se_root_dentry = root_dentry; 147 se->se_taskqid = TASKQID_INVALID; 148 rw_init(&se->se_taskqid_lock, NULL, RW_DEFAULT, NULL); 149 150 zfs_refcount_create(&se->se_refcount); 151 152 return (se); 153 } 154 155 /* 156 * Free a zfs_snapentry_t the caller must ensure there are no active 157 * references. 158 */ 159 static void 160 zfsctl_snapshot_free(zfs_snapentry_t *se) 161 { 162 zfs_refcount_destroy(&se->se_refcount); 163 kmem_strfree(se->se_name); 164 kmem_strfree(se->se_path); 165 rw_destroy(&se->se_taskqid_lock); 166 167 kmem_free(se, sizeof (zfs_snapentry_t)); 168 } 169 170 /* 171 * Hold a reference on the zfs_snapentry_t. 172 */ 173 static void 174 zfsctl_snapshot_hold(zfs_snapentry_t *se) 175 { 176 zfs_refcount_add(&se->se_refcount, NULL); 177 } 178 179 /* 180 * Release a reference on the zfs_snapentry_t. When the number of 181 * references drops to zero the structure will be freed. 182 */ 183 static void 184 zfsctl_snapshot_rele(zfs_snapentry_t *se) 185 { 186 if (zfs_refcount_remove(&se->se_refcount, NULL) == 0) 187 zfsctl_snapshot_free(se); 188 } 189 190 /* 191 * Add a zfs_snapentry_t to both the zfs_snapshots_by_name and 192 * zfs_snapshots_by_objsetid trees. While the zfs_snapentry_t is part 193 * of the trees a reference is held. 194 */ 195 static void 196 zfsctl_snapshot_add(zfs_snapentry_t *se) 197 { 198 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock)); 199 zfsctl_snapshot_hold(se); 200 avl_add(&zfs_snapshots_by_name, se); 201 avl_add(&zfs_snapshots_by_objsetid, se); 202 } 203 204 /* 205 * Remove a zfs_snapentry_t from both the zfs_snapshots_by_name and 206 * zfs_snapshots_by_objsetid trees. Upon removal a reference is dropped, 207 * this can result in the structure being freed if that was the last 208 * remaining reference. 209 */ 210 static void 211 zfsctl_snapshot_remove(zfs_snapentry_t *se) 212 { 213 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock)); 214 avl_remove(&zfs_snapshots_by_name, se); 215 avl_remove(&zfs_snapshots_by_objsetid, se); 216 zfsctl_snapshot_rele(se); 217 } 218 219 /* 220 * Snapshot name comparison function for the zfs_snapshots_by_name. 221 */ 222 static int 223 snapentry_compare_by_name(const void *a, const void *b) 224 { 225 const zfs_snapentry_t *se_a = a; 226 const zfs_snapentry_t *se_b = b; 227 int ret; 228 229 ret = strcmp(se_a->se_name, se_b->se_name); 230 231 if (ret < 0) 232 return (-1); 233 else if (ret > 0) 234 return (1); 235 else 236 return (0); 237 } 238 239 /* 240 * Snapshot name comparison function for the zfs_snapshots_by_objsetid. 241 */ 242 static int 243 snapentry_compare_by_objsetid(const void *a, const void *b) 244 { 245 const zfs_snapentry_t *se_a = a; 246 const zfs_snapentry_t *se_b = b; 247 248 if (se_a->se_spa != se_b->se_spa) 249 return ((ulong_t)se_a->se_spa < (ulong_t)se_b->se_spa ? -1 : 1); 250 251 if (se_a->se_objsetid < se_b->se_objsetid) 252 return (-1); 253 else if (se_a->se_objsetid > se_b->se_objsetid) 254 return (1); 255 else 256 return (0); 257 } 258 259 /* 260 * Find a zfs_snapentry_t in zfs_snapshots_by_name. If the snapname 261 * is found a pointer to the zfs_snapentry_t is returned and a reference 262 * taken on the structure. The caller is responsible for dropping the 263 * reference with zfsctl_snapshot_rele(). If the snapname is not found 264 * NULL will be returned. 265 */ 266 static zfs_snapentry_t * 267 zfsctl_snapshot_find_by_name(const char *snapname) 268 { 269 zfs_snapentry_t *se, search; 270 271 ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock)); 272 273 search.se_name = (char *)snapname; 274 se = avl_find(&zfs_snapshots_by_name, &search, NULL); 275 if (se) 276 zfsctl_snapshot_hold(se); 277 278 return (se); 279 } 280 281 /* 282 * Find a zfs_snapentry_t in zfs_snapshots_by_objsetid given the objset id 283 * rather than the snapname. In all other respects it behaves the same 284 * as zfsctl_snapshot_find_by_name(). 285 */ 286 static zfs_snapentry_t * 287 zfsctl_snapshot_find_by_objsetid(spa_t *spa, uint64_t objsetid) 288 { 289 zfs_snapentry_t *se, search; 290 291 ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock)); 292 293 search.se_spa = spa; 294 search.se_objsetid = objsetid; 295 se = avl_find(&zfs_snapshots_by_objsetid, &search, NULL); 296 if (se) 297 zfsctl_snapshot_hold(se); 298 299 return (se); 300 } 301 302 /* 303 * Rename a zfs_snapentry_t in the zfs_snapshots_by_name. The structure is 304 * removed, renamed, and added back to the new correct location in the tree. 305 */ 306 static int 307 zfsctl_snapshot_rename(const char *old_snapname, const char *new_snapname) 308 { 309 zfs_snapentry_t *se; 310 311 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock)); 312 313 se = zfsctl_snapshot_find_by_name(old_snapname); 314 if (se == NULL) 315 return (SET_ERROR(ENOENT)); 316 317 zfsctl_snapshot_remove(se); 318 kmem_strfree(se->se_name); 319 se->se_name = kmem_strdup(new_snapname); 320 zfsctl_snapshot_add(se); 321 zfsctl_snapshot_rele(se); 322 323 return (0); 324 } 325 326 /* 327 * Delayed task responsible for unmounting an expired automounted snapshot. 328 */ 329 static void 330 snapentry_expire(void *data) 331 { 332 zfs_snapentry_t *se = (zfs_snapentry_t *)data; 333 spa_t *spa = se->se_spa; 334 uint64_t objsetid = se->se_objsetid; 335 336 if (zfs_expire_snapshot <= 0) { 337 zfsctl_snapshot_rele(se); 338 return; 339 } 340 341 rw_enter(&se->se_taskqid_lock, RW_WRITER); 342 se->se_taskqid = TASKQID_INVALID; 343 rw_exit(&se->se_taskqid_lock); 344 (void) zfsctl_snapshot_unmount(se->se_name, MNT_EXPIRE); 345 zfsctl_snapshot_rele(se); 346 347 /* 348 * Reschedule the unmount if the zfs_snapentry_t wasn't removed. 349 * This can occur when the snapshot is busy. 350 */ 351 rw_enter(&zfs_snapshot_lock, RW_READER); 352 if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) { 353 zfsctl_snapshot_unmount_delay_impl(se, zfs_expire_snapshot); 354 zfsctl_snapshot_rele(se); 355 } 356 rw_exit(&zfs_snapshot_lock); 357 } 358 359 /* 360 * Cancel an automatic unmount of a snapname. This callback is responsible 361 * for dropping the reference on the zfs_snapentry_t which was taken when 362 * during dispatch. 363 */ 364 static void 365 zfsctl_snapshot_unmount_cancel(zfs_snapentry_t *se) 366 { 367 int err = 0; 368 rw_enter(&se->se_taskqid_lock, RW_WRITER); 369 err = taskq_cancel_id(system_delay_taskq, se->se_taskqid); 370 /* 371 * if we get ENOENT, the taskq couldn't be found to be 372 * canceled, so we can just mark it as invalid because 373 * it's already gone. If we got EBUSY, then we already 374 * blocked until it was gone _anyway_, so we don't care. 375 */ 376 se->se_taskqid = TASKQID_INVALID; 377 rw_exit(&se->se_taskqid_lock); 378 if (err == 0) { 379 zfsctl_snapshot_rele(se); 380 } 381 } 382 383 /* 384 * Dispatch the unmount task for delayed handling with a hold protecting it. 385 */ 386 static void 387 zfsctl_snapshot_unmount_delay_impl(zfs_snapentry_t *se, int delay) 388 { 389 390 if (delay <= 0) 391 return; 392 393 zfsctl_snapshot_hold(se); 394 rw_enter(&se->se_taskqid_lock, RW_WRITER); 395 ASSERT3S(se->se_taskqid, ==, TASKQID_INVALID); 396 se->se_taskqid = taskq_dispatch_delay(system_delay_taskq, 397 snapentry_expire, se, TQ_SLEEP, ddi_get_lbolt() + delay * HZ); 398 rw_exit(&se->se_taskqid_lock); 399 } 400 401 /* 402 * Schedule an automatic unmount of objset id to occur in delay seconds from 403 * now. Any previous delayed unmount will be cancelled in favor of the 404 * updated deadline. A reference is taken by zfsctl_snapshot_find_by_name() 405 * and held until the outstanding task is handled or cancelled. 406 */ 407 int 408 zfsctl_snapshot_unmount_delay(spa_t *spa, uint64_t objsetid, int delay) 409 { 410 zfs_snapentry_t *se; 411 int error = ENOENT; 412 413 rw_enter(&zfs_snapshot_lock, RW_READER); 414 if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) { 415 zfsctl_snapshot_unmount_cancel(se); 416 zfsctl_snapshot_unmount_delay_impl(se, delay); 417 zfsctl_snapshot_rele(se); 418 error = 0; 419 } 420 rw_exit(&zfs_snapshot_lock); 421 422 return (error); 423 } 424 425 /* 426 * Check if snapname is currently mounted. Returned non-zero when mounted 427 * and zero when unmounted. 428 */ 429 static boolean_t 430 zfsctl_snapshot_ismounted(const char *snapname) 431 { 432 zfs_snapentry_t *se; 433 boolean_t ismounted = B_FALSE; 434 435 rw_enter(&zfs_snapshot_lock, RW_READER); 436 if ((se = zfsctl_snapshot_find_by_name(snapname)) != NULL) { 437 zfsctl_snapshot_rele(se); 438 ismounted = B_TRUE; 439 } 440 rw_exit(&zfs_snapshot_lock); 441 442 return (ismounted); 443 } 444 445 /* 446 * Check if the given inode is a part of the virtual .zfs directory. 447 */ 448 boolean_t 449 zfsctl_is_node(struct inode *ip) 450 { 451 return (ITOZ(ip)->z_is_ctldir); 452 } 453 454 /* 455 * Check if the given inode is a .zfs/snapshots/snapname directory. 456 */ 457 boolean_t 458 zfsctl_is_snapdir(struct inode *ip) 459 { 460 return (zfsctl_is_node(ip) && (ip->i_ino <= ZFSCTL_INO_SNAPDIRS)); 461 } 462 463 /* 464 * Allocate a new inode with the passed id and ops. 465 */ 466 static struct inode * 467 zfsctl_inode_alloc(zfsvfs_t *zfsvfs, uint64_t id, 468 const struct file_operations *fops, const struct inode_operations *ops) 469 { 470 inode_timespec_t now; 471 struct inode *ip; 472 znode_t *zp; 473 474 ip = new_inode(zfsvfs->z_sb); 475 if (ip == NULL) 476 return (NULL); 477 478 now = current_time(ip); 479 zp = ITOZ(ip); 480 ASSERT3P(zp->z_dirlocks, ==, NULL); 481 ASSERT3P(zp->z_acl_cached, ==, NULL); 482 ASSERT3P(zp->z_xattr_cached, ==, NULL); 483 zp->z_id = id; 484 zp->z_unlinked = B_FALSE; 485 zp->z_atime_dirty = B_FALSE; 486 zp->z_zn_prefetch = B_FALSE; 487 zp->z_is_sa = B_FALSE; 488 zp->z_is_mapped = B_FALSE; 489 zp->z_is_ctldir = B_TRUE; 490 zp->z_is_stale = B_FALSE; 491 zp->z_sa_hdl = NULL; 492 zp->z_blksz = 0; 493 zp->z_seq = 0; 494 zp->z_mapcnt = 0; 495 zp->z_size = 0; 496 zp->z_pflags = 0; 497 zp->z_mode = 0; 498 zp->z_sync_cnt = 0; 499 zp->z_sync_writes_cnt = 0; 500 zp->z_async_writes_cnt = 0; 501 ip->i_generation = 0; 502 ip->i_ino = id; 503 ip->i_mode = (S_IFDIR | S_IRWXUGO); 504 ip->i_uid = SUID_TO_KUID(0); 505 ip->i_gid = SGID_TO_KGID(0); 506 ip->i_blkbits = SPA_MINBLOCKSHIFT; 507 ip->i_atime = now; 508 ip->i_mtime = now; 509 ip->i_ctime = now; 510 ip->i_fop = fops; 511 ip->i_op = ops; 512 #if defined(IOP_XATTR) 513 ip->i_opflags &= ~IOP_XATTR; 514 #endif 515 516 if (insert_inode_locked(ip)) { 517 unlock_new_inode(ip); 518 iput(ip); 519 return (NULL); 520 } 521 522 mutex_enter(&zfsvfs->z_znodes_lock); 523 list_insert_tail(&zfsvfs->z_all_znodes, zp); 524 zfsvfs->z_nr_znodes++; 525 membar_producer(); 526 mutex_exit(&zfsvfs->z_znodes_lock); 527 528 unlock_new_inode(ip); 529 530 return (ip); 531 } 532 533 /* 534 * Lookup the inode with given id, it will be allocated if needed. 535 */ 536 static struct inode * 537 zfsctl_inode_lookup(zfsvfs_t *zfsvfs, uint64_t id, 538 const struct file_operations *fops, const struct inode_operations *ops) 539 { 540 struct inode *ip = NULL; 541 542 while (ip == NULL) { 543 ip = ilookup(zfsvfs->z_sb, (unsigned long)id); 544 if (ip) 545 break; 546 547 /* May fail due to concurrent zfsctl_inode_alloc() */ 548 ip = zfsctl_inode_alloc(zfsvfs, id, fops, ops); 549 } 550 551 return (ip); 552 } 553 554 /* 555 * Create the '.zfs' directory. This directory is cached as part of the VFS 556 * structure. This results in a hold on the zfsvfs_t. The code in zfs_umount() 557 * therefore checks against a vfs_count of 2 instead of 1. This reference 558 * is removed when the ctldir is destroyed in the unmount. All other entities 559 * under the '.zfs' directory are created dynamically as needed. 560 * 561 * Because the dynamically created '.zfs' directory entries assume the use 562 * of 64-bit inode numbers this support must be disabled on 32-bit systems. 563 */ 564 int 565 zfsctl_create(zfsvfs_t *zfsvfs) 566 { 567 ASSERT(zfsvfs->z_ctldir == NULL); 568 569 zfsvfs->z_ctldir = zfsctl_inode_alloc(zfsvfs, ZFSCTL_INO_ROOT, 570 &zpl_fops_root, &zpl_ops_root); 571 if (zfsvfs->z_ctldir == NULL) 572 return (SET_ERROR(ENOENT)); 573 574 return (0); 575 } 576 577 /* 578 * Destroy the '.zfs' directory or remove a snapshot from zfs_snapshots_by_name. 579 * Only called when the filesystem is unmounted. 580 */ 581 void 582 zfsctl_destroy(zfsvfs_t *zfsvfs) 583 { 584 if (zfsvfs->z_issnap) { 585 zfs_snapentry_t *se; 586 spa_t *spa = zfsvfs->z_os->os_spa; 587 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 588 589 rw_enter(&zfs_snapshot_lock, RW_WRITER); 590 se = zfsctl_snapshot_find_by_objsetid(spa, objsetid); 591 if (se != NULL) 592 zfsctl_snapshot_remove(se); 593 rw_exit(&zfs_snapshot_lock); 594 if (se != NULL) { 595 zfsctl_snapshot_unmount_cancel(se); 596 zfsctl_snapshot_rele(se); 597 } 598 } else if (zfsvfs->z_ctldir) { 599 iput(zfsvfs->z_ctldir); 600 zfsvfs->z_ctldir = NULL; 601 } 602 } 603 604 /* 605 * Given a root znode, retrieve the associated .zfs directory. 606 * Add a hold to the vnode and return it. 607 */ 608 struct inode * 609 zfsctl_root(znode_t *zp) 610 { 611 ASSERT(zfs_has_ctldir(zp)); 612 /* Must have an existing ref, so igrab() cannot return NULL */ 613 VERIFY3P(igrab(ZTOZSB(zp)->z_ctldir), !=, NULL); 614 return (ZTOZSB(zp)->z_ctldir); 615 } 616 617 /* 618 * Generate a long fid to indicate a snapdir. We encode whether snapdir is 619 * already mounted in gen field. We do this because nfsd lookup will not 620 * trigger automount. Next time the nfsd does fh_to_dentry, we will notice 621 * this and do automount and return ESTALE to force nfsd revalidate and follow 622 * mount. 623 */ 624 static int 625 zfsctl_snapdir_fid(struct inode *ip, fid_t *fidp) 626 { 627 zfid_short_t *zfid = (zfid_short_t *)fidp; 628 zfid_long_t *zlfid = (zfid_long_t *)fidp; 629 uint32_t gen = 0; 630 uint64_t object; 631 uint64_t objsetid; 632 int i; 633 struct dentry *dentry; 634 635 if (fidp->fid_len < LONG_FID_LEN) { 636 fidp->fid_len = LONG_FID_LEN; 637 return (SET_ERROR(ENOSPC)); 638 } 639 640 object = ip->i_ino; 641 objsetid = ZFSCTL_INO_SNAPDIRS - ip->i_ino; 642 zfid->zf_len = LONG_FID_LEN; 643 644 dentry = d_obtain_alias(igrab(ip)); 645 if (!IS_ERR(dentry)) { 646 gen = !!d_mountpoint(dentry); 647 dput(dentry); 648 } 649 650 for (i = 0; i < sizeof (zfid->zf_object); i++) 651 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 652 653 for (i = 0; i < sizeof (zfid->zf_gen); i++) 654 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 655 656 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 657 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 658 659 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 660 zlfid->zf_setgen[i] = 0; 661 662 return (0); 663 } 664 665 /* 666 * Generate an appropriate fid for an entry in the .zfs directory. 667 */ 668 int 669 zfsctl_fid(struct inode *ip, fid_t *fidp) 670 { 671 znode_t *zp = ITOZ(ip); 672 zfsvfs_t *zfsvfs = ITOZSB(ip); 673 uint64_t object = zp->z_id; 674 zfid_short_t *zfid; 675 int i; 676 677 ZFS_ENTER(zfsvfs); 678 679 if (zfsctl_is_snapdir(ip)) { 680 ZFS_EXIT(zfsvfs); 681 return (zfsctl_snapdir_fid(ip, fidp)); 682 } 683 684 if (fidp->fid_len < SHORT_FID_LEN) { 685 fidp->fid_len = SHORT_FID_LEN; 686 ZFS_EXIT(zfsvfs); 687 return (SET_ERROR(ENOSPC)); 688 } 689 690 zfid = (zfid_short_t *)fidp; 691 692 zfid->zf_len = SHORT_FID_LEN; 693 694 for (i = 0; i < sizeof (zfid->zf_object); i++) 695 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 696 697 /* .zfs znodes always have a generation number of 0 */ 698 for (i = 0; i < sizeof (zfid->zf_gen); i++) 699 zfid->zf_gen[i] = 0; 700 701 ZFS_EXIT(zfsvfs); 702 return (0); 703 } 704 705 /* 706 * Construct a full dataset name in full_name: "pool/dataset@snap_name" 707 */ 708 static int 709 zfsctl_snapshot_name(zfsvfs_t *zfsvfs, const char *snap_name, int len, 710 char *full_name) 711 { 712 objset_t *os = zfsvfs->z_os; 713 714 if (zfs_component_namecheck(snap_name, NULL, NULL) != 0) 715 return (SET_ERROR(EILSEQ)); 716 717 dmu_objset_name(os, full_name); 718 if ((strlen(full_name) + 1 + strlen(snap_name)) >= len) 719 return (SET_ERROR(ENAMETOOLONG)); 720 721 (void) strcat(full_name, "@"); 722 (void) strcat(full_name, snap_name); 723 724 return (0); 725 } 726 727 /* 728 * Returns full path in full_path: "/pool/dataset/.zfs/snapshot/snap_name/" 729 */ 730 static int 731 zfsctl_snapshot_path_objset(zfsvfs_t *zfsvfs, uint64_t objsetid, 732 int path_len, char *full_path) 733 { 734 objset_t *os = zfsvfs->z_os; 735 fstrans_cookie_t cookie; 736 char *snapname; 737 boolean_t case_conflict; 738 uint64_t id, pos = 0; 739 int error = 0; 740 741 if (zfsvfs->z_vfs->vfs_mntpoint == NULL) 742 return (SET_ERROR(ENOENT)); 743 744 cookie = spl_fstrans_mark(); 745 snapname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 746 747 while (error == 0) { 748 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 749 error = dmu_snapshot_list_next(zfsvfs->z_os, 750 ZFS_MAX_DATASET_NAME_LEN, snapname, &id, &pos, 751 &case_conflict); 752 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 753 if (error) 754 goto out; 755 756 if (id == objsetid) 757 break; 758 } 759 760 snprintf(full_path, path_len, "%s/.zfs/snapshot/%s", 761 zfsvfs->z_vfs->vfs_mntpoint, snapname); 762 out: 763 kmem_free(snapname, ZFS_MAX_DATASET_NAME_LEN); 764 spl_fstrans_unmark(cookie); 765 766 return (error); 767 } 768 769 /* 770 * Special case the handling of "..". 771 */ 772 int 773 zfsctl_root_lookup(struct inode *dip, const char *name, struct inode **ipp, 774 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp) 775 { 776 zfsvfs_t *zfsvfs = ITOZSB(dip); 777 int error = 0; 778 779 ZFS_ENTER(zfsvfs); 780 781 if (strcmp(name, "..") == 0) { 782 *ipp = dip->i_sb->s_root->d_inode; 783 } else if (strcmp(name, ZFS_SNAPDIR_NAME) == 0) { 784 *ipp = zfsctl_inode_lookup(zfsvfs, ZFSCTL_INO_SNAPDIR, 785 &zpl_fops_snapdir, &zpl_ops_snapdir); 786 } else if (strcmp(name, ZFS_SHAREDIR_NAME) == 0) { 787 *ipp = zfsctl_inode_lookup(zfsvfs, ZFSCTL_INO_SHARES, 788 &zpl_fops_shares, &zpl_ops_shares); 789 } else { 790 *ipp = NULL; 791 } 792 793 if (*ipp == NULL) 794 error = SET_ERROR(ENOENT); 795 796 ZFS_EXIT(zfsvfs); 797 798 return (error); 799 } 800 801 /* 802 * Lookup entry point for the 'snapshot' directory. Try to open the 803 * snapshot if it exist, creating the pseudo filesystem inode as necessary. 804 */ 805 int 806 zfsctl_snapdir_lookup(struct inode *dip, const char *name, struct inode **ipp, 807 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp) 808 { 809 zfsvfs_t *zfsvfs = ITOZSB(dip); 810 uint64_t id; 811 int error; 812 813 ZFS_ENTER(zfsvfs); 814 815 error = dmu_snapshot_lookup(zfsvfs->z_os, name, &id); 816 if (error) { 817 ZFS_EXIT(zfsvfs); 818 return (error); 819 } 820 821 *ipp = zfsctl_inode_lookup(zfsvfs, ZFSCTL_INO_SNAPDIRS - id, 822 &simple_dir_operations, &simple_dir_inode_operations); 823 if (*ipp == NULL) 824 error = SET_ERROR(ENOENT); 825 826 ZFS_EXIT(zfsvfs); 827 828 return (error); 829 } 830 831 /* 832 * Renaming a directory under '.zfs/snapshot' will automatically trigger 833 * a rename of the snapshot to the new given name. The rename is confined 834 * to the '.zfs/snapshot' directory snapshots cannot be moved elsewhere. 835 */ 836 int 837 zfsctl_snapdir_rename(struct inode *sdip, const char *snm, 838 struct inode *tdip, const char *tnm, cred_t *cr, int flags) 839 { 840 zfsvfs_t *zfsvfs = ITOZSB(sdip); 841 char *to, *from, *real, *fsname; 842 int error; 843 844 if (!zfs_admin_snapshot) 845 return (SET_ERROR(EACCES)); 846 847 ZFS_ENTER(zfsvfs); 848 849 to = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 850 from = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 851 real = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 852 fsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 853 854 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 855 error = dmu_snapshot_realname(zfsvfs->z_os, snm, real, 856 ZFS_MAX_DATASET_NAME_LEN, NULL); 857 if (error == 0) { 858 snm = real; 859 } else if (error != ENOTSUP) { 860 goto out; 861 } 862 } 863 864 dmu_objset_name(zfsvfs->z_os, fsname); 865 866 error = zfsctl_snapshot_name(ITOZSB(sdip), snm, 867 ZFS_MAX_DATASET_NAME_LEN, from); 868 if (error == 0) 869 error = zfsctl_snapshot_name(ITOZSB(tdip), tnm, 870 ZFS_MAX_DATASET_NAME_LEN, to); 871 if (error == 0) 872 error = zfs_secpolicy_rename_perms(from, to, cr); 873 if (error != 0) 874 goto out; 875 876 /* 877 * Cannot move snapshots out of the snapdir. 878 */ 879 if (sdip != tdip) { 880 error = SET_ERROR(EINVAL); 881 goto out; 882 } 883 884 /* 885 * No-op when names are identical. 886 */ 887 if (strcmp(snm, tnm) == 0) { 888 error = 0; 889 goto out; 890 } 891 892 rw_enter(&zfs_snapshot_lock, RW_WRITER); 893 894 error = dsl_dataset_rename_snapshot(fsname, snm, tnm, B_FALSE); 895 if (error == 0) 896 (void) zfsctl_snapshot_rename(snm, tnm); 897 898 rw_exit(&zfs_snapshot_lock); 899 out: 900 kmem_free(from, ZFS_MAX_DATASET_NAME_LEN); 901 kmem_free(to, ZFS_MAX_DATASET_NAME_LEN); 902 kmem_free(real, ZFS_MAX_DATASET_NAME_LEN); 903 kmem_free(fsname, ZFS_MAX_DATASET_NAME_LEN); 904 905 ZFS_EXIT(zfsvfs); 906 907 return (error); 908 } 909 910 /* 911 * Removing a directory under '.zfs/snapshot' will automatically trigger 912 * the removal of the snapshot with the given name. 913 */ 914 int 915 zfsctl_snapdir_remove(struct inode *dip, const char *name, cred_t *cr, 916 int flags) 917 { 918 zfsvfs_t *zfsvfs = ITOZSB(dip); 919 char *snapname, *real; 920 int error; 921 922 if (!zfs_admin_snapshot) 923 return (SET_ERROR(EACCES)); 924 925 ZFS_ENTER(zfsvfs); 926 927 snapname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 928 real = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 929 930 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 931 error = dmu_snapshot_realname(zfsvfs->z_os, name, real, 932 ZFS_MAX_DATASET_NAME_LEN, NULL); 933 if (error == 0) { 934 name = real; 935 } else if (error != ENOTSUP) { 936 goto out; 937 } 938 } 939 940 error = zfsctl_snapshot_name(ITOZSB(dip), name, 941 ZFS_MAX_DATASET_NAME_LEN, snapname); 942 if (error == 0) 943 error = zfs_secpolicy_destroy_perms(snapname, cr); 944 if (error != 0) 945 goto out; 946 947 error = zfsctl_snapshot_unmount(snapname, MNT_FORCE); 948 if ((error == 0) || (error == ENOENT)) 949 error = dsl_destroy_snapshot(snapname, B_FALSE); 950 out: 951 kmem_free(snapname, ZFS_MAX_DATASET_NAME_LEN); 952 kmem_free(real, ZFS_MAX_DATASET_NAME_LEN); 953 954 ZFS_EXIT(zfsvfs); 955 956 return (error); 957 } 958 959 /* 960 * Creating a directory under '.zfs/snapshot' will automatically trigger 961 * the creation of a new snapshot with the given name. 962 */ 963 int 964 zfsctl_snapdir_mkdir(struct inode *dip, const char *dirname, vattr_t *vap, 965 struct inode **ipp, cred_t *cr, int flags) 966 { 967 zfsvfs_t *zfsvfs = ITOZSB(dip); 968 char *dsname; 969 int error; 970 971 if (!zfs_admin_snapshot) 972 return (SET_ERROR(EACCES)); 973 974 dsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 975 976 if (zfs_component_namecheck(dirname, NULL, NULL) != 0) { 977 error = SET_ERROR(EILSEQ); 978 goto out; 979 } 980 981 dmu_objset_name(zfsvfs->z_os, dsname); 982 983 error = zfs_secpolicy_snapshot_perms(dsname, cr); 984 if (error != 0) 985 goto out; 986 987 if (error == 0) { 988 error = dmu_objset_snapshot_one(dsname, dirname); 989 if (error != 0) 990 goto out; 991 992 error = zfsctl_snapdir_lookup(dip, dirname, ipp, 993 0, cr, NULL, NULL); 994 } 995 out: 996 kmem_free(dsname, ZFS_MAX_DATASET_NAME_LEN); 997 998 return (error); 999 } 1000 1001 /* 1002 * Flush everything out of the kernel's export table and such. 1003 * This is needed as once the snapshot is used over NFS, its 1004 * entries in svc_export and svc_expkey caches hold reference 1005 * to the snapshot mount point. There is no known way of flushing 1006 * only the entries related to the snapshot. 1007 */ 1008 static void 1009 exportfs_flush(void) 1010 { 1011 char *argv[] = { "/usr/sbin/exportfs", "-f", NULL }; 1012 char *envp[] = { NULL }; 1013 1014 (void) call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); 1015 } 1016 1017 /* 1018 * Attempt to unmount a snapshot by making a call to user space. 1019 * There is no assurance that this can or will succeed, is just a 1020 * best effort. In the case where it does fail, perhaps because 1021 * it's in use, the unmount will fail harmlessly. 1022 */ 1023 int 1024 zfsctl_snapshot_unmount(const char *snapname, int flags) 1025 { 1026 char *argv[] = { "/usr/bin/env", "umount", "-t", "zfs", "-n", NULL, 1027 NULL }; 1028 char *envp[] = { NULL }; 1029 zfs_snapentry_t *se; 1030 int error; 1031 1032 rw_enter(&zfs_snapshot_lock, RW_READER); 1033 if ((se = zfsctl_snapshot_find_by_name(snapname)) == NULL) { 1034 rw_exit(&zfs_snapshot_lock); 1035 return (SET_ERROR(ENOENT)); 1036 } 1037 rw_exit(&zfs_snapshot_lock); 1038 1039 exportfs_flush(); 1040 1041 if (flags & MNT_FORCE) 1042 argv[4] = "-fn"; 1043 argv[5] = se->se_path; 1044 dprintf("unmount; path=%s\n", se->se_path); 1045 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); 1046 zfsctl_snapshot_rele(se); 1047 1048 1049 /* 1050 * The umount system utility will return 256 on error. We must 1051 * assume this error is because the file system is busy so it is 1052 * converted to the more sensible EBUSY. 1053 */ 1054 if (error) 1055 error = SET_ERROR(EBUSY); 1056 1057 return (error); 1058 } 1059 1060 int 1061 zfsctl_snapshot_mount(struct path *path, int flags) 1062 { 1063 struct dentry *dentry = path->dentry; 1064 struct inode *ip = dentry->d_inode; 1065 zfsvfs_t *zfsvfs; 1066 zfsvfs_t *snap_zfsvfs; 1067 zfs_snapentry_t *se; 1068 char *full_name, *full_path; 1069 char *argv[] = { "/usr/bin/env", "mount", "-t", "zfs", "-n", NULL, NULL, 1070 NULL }; 1071 char *envp[] = { NULL }; 1072 int error; 1073 struct path spath; 1074 1075 if (ip == NULL) 1076 return (SET_ERROR(EISDIR)); 1077 1078 zfsvfs = ITOZSB(ip); 1079 ZFS_ENTER(zfsvfs); 1080 1081 full_name = kmem_zalloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); 1082 full_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 1083 1084 error = zfsctl_snapshot_name(zfsvfs, dname(dentry), 1085 ZFS_MAX_DATASET_NAME_LEN, full_name); 1086 if (error) 1087 goto error; 1088 1089 /* 1090 * Construct a mount point path from sb of the ctldir inode and dirent 1091 * name, instead of from d_path(), so that chroot'd process doesn't fail 1092 * on mount.zfs(8). 1093 */ 1094 snprintf(full_path, MAXPATHLEN, "%s/.zfs/snapshot/%s", 1095 zfsvfs->z_vfs->vfs_mntpoint ? zfsvfs->z_vfs->vfs_mntpoint : "", 1096 dname(dentry)); 1097 1098 /* 1099 * Multiple concurrent automounts of a snapshot are never allowed. 1100 * The snapshot may be manually mounted as many times as desired. 1101 */ 1102 if (zfsctl_snapshot_ismounted(full_name)) { 1103 error = 0; 1104 goto error; 1105 } 1106 1107 /* 1108 * Attempt to mount the snapshot from user space. Normally this 1109 * would be done using the vfs_kern_mount() function, however that 1110 * function is marked GPL-only and cannot be used. On error we 1111 * careful to log the real error to the console and return EISDIR 1112 * to safely abort the automount. This should be very rare. 1113 * 1114 * If the user mode helper happens to return EBUSY, a concurrent 1115 * mount is already in progress in which case the error is ignored. 1116 * Take note that if the program was executed successfully the return 1117 * value from call_usermodehelper() will be (exitcode << 8 + signal). 1118 */ 1119 dprintf("mount; name=%s path=%s\n", full_name, full_path); 1120 argv[5] = full_name; 1121 argv[6] = full_path; 1122 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); 1123 if (error) { 1124 if (!(error & MOUNT_BUSY << 8)) { 1125 zfs_dbgmsg("Unable to automount %s error=%d", 1126 full_path, error); 1127 error = SET_ERROR(EISDIR); 1128 } else { 1129 /* 1130 * EBUSY, this could mean a concurrent mount, or the 1131 * snapshot has already been mounted at completely 1132 * different place. We return 0 so VFS will retry. For 1133 * the latter case the VFS will retry several times 1134 * and return ELOOP, which is probably not a very good 1135 * behavior. 1136 */ 1137 error = 0; 1138 } 1139 goto error; 1140 } 1141 1142 /* 1143 * Follow down in to the mounted snapshot and set MNT_SHRINKABLE 1144 * to identify this as an automounted filesystem. 1145 */ 1146 spath = *path; 1147 path_get(&spath); 1148 if (follow_down_one(&spath)) { 1149 snap_zfsvfs = ITOZSB(spath.dentry->d_inode); 1150 snap_zfsvfs->z_parent = zfsvfs; 1151 dentry = spath.dentry; 1152 spath.mnt->mnt_flags |= MNT_SHRINKABLE; 1153 1154 rw_enter(&zfs_snapshot_lock, RW_WRITER); 1155 se = zfsctl_snapshot_alloc(full_name, full_path, 1156 snap_zfsvfs->z_os->os_spa, dmu_objset_id(snap_zfsvfs->z_os), 1157 dentry); 1158 zfsctl_snapshot_add(se); 1159 zfsctl_snapshot_unmount_delay_impl(se, zfs_expire_snapshot); 1160 rw_exit(&zfs_snapshot_lock); 1161 } 1162 path_put(&spath); 1163 error: 1164 kmem_free(full_name, ZFS_MAX_DATASET_NAME_LEN); 1165 kmem_free(full_path, MAXPATHLEN); 1166 1167 ZFS_EXIT(zfsvfs); 1168 1169 return (error); 1170 } 1171 1172 /* 1173 * Get the snapdir inode from fid 1174 */ 1175 int 1176 zfsctl_snapdir_vget(struct super_block *sb, uint64_t objsetid, int gen, 1177 struct inode **ipp) 1178 { 1179 int error; 1180 struct path path; 1181 char *mnt; 1182 struct dentry *dentry; 1183 1184 mnt = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1185 1186 error = zfsctl_snapshot_path_objset(sb->s_fs_info, objsetid, 1187 MAXPATHLEN, mnt); 1188 if (error) 1189 goto out; 1190 1191 /* Trigger automount */ 1192 error = -kern_path(mnt, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &path); 1193 if (error) 1194 goto out; 1195 1196 path_put(&path); 1197 /* 1198 * Get the snapdir inode. Note, we don't want to use the above 1199 * path because it contains the root of the snapshot rather 1200 * than the snapdir. 1201 */ 1202 *ipp = ilookup(sb, ZFSCTL_INO_SNAPDIRS - objsetid); 1203 if (*ipp == NULL) { 1204 error = SET_ERROR(ENOENT); 1205 goto out; 1206 } 1207 1208 /* check gen, see zfsctl_snapdir_fid */ 1209 dentry = d_obtain_alias(igrab(*ipp)); 1210 if (gen != (!IS_ERR(dentry) && d_mountpoint(dentry))) { 1211 iput(*ipp); 1212 *ipp = NULL; 1213 error = SET_ERROR(ENOENT); 1214 } 1215 if (!IS_ERR(dentry)) 1216 dput(dentry); 1217 out: 1218 kmem_free(mnt, MAXPATHLEN); 1219 return (error); 1220 } 1221 1222 int 1223 zfsctl_shares_lookup(struct inode *dip, char *name, struct inode **ipp, 1224 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp) 1225 { 1226 zfsvfs_t *zfsvfs = ITOZSB(dip); 1227 znode_t *zp; 1228 znode_t *dzp; 1229 int error; 1230 1231 ZFS_ENTER(zfsvfs); 1232 1233 if (zfsvfs->z_shares_dir == 0) { 1234 ZFS_EXIT(zfsvfs); 1235 return (SET_ERROR(ENOTSUP)); 1236 } 1237 1238 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) { 1239 error = zfs_lookup(dzp, name, &zp, 0, cr, NULL, NULL); 1240 zrele(dzp); 1241 } 1242 1243 ZFS_EXIT(zfsvfs); 1244 1245 return (error); 1246 } 1247 1248 /* 1249 * Initialize the various pieces we'll need to create and manipulate .zfs 1250 * directories. Currently this is unused but available. 1251 */ 1252 void 1253 zfsctl_init(void) 1254 { 1255 avl_create(&zfs_snapshots_by_name, snapentry_compare_by_name, 1256 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, 1257 se_node_name)); 1258 avl_create(&zfs_snapshots_by_objsetid, snapentry_compare_by_objsetid, 1259 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, 1260 se_node_objsetid)); 1261 rw_init(&zfs_snapshot_lock, NULL, RW_DEFAULT, NULL); 1262 } 1263 1264 /* 1265 * Cleanup the various pieces we needed for .zfs directories. In particular 1266 * ensure the expiry timer is canceled safely. 1267 */ 1268 void 1269 zfsctl_fini(void) 1270 { 1271 avl_destroy(&zfs_snapshots_by_name); 1272 avl_destroy(&zfs_snapshots_by_objsetid); 1273 rw_destroy(&zfs_snapshot_lock); 1274 } 1275 1276 module_param(zfs_admin_snapshot, int, 0644); 1277 MODULE_PARM_DESC(zfs_admin_snapshot, "Enable mkdir/rmdir/mv in .zfs/snapshot"); 1278 1279 module_param(zfs_expire_snapshot, int, 0644); 1280 MODULE_PARM_DESC(zfs_expire_snapshot, "Seconds to expire .zfs/snapshot"); 1281