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 /* 23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 25 * Copyright (c) 2014, 2021 by Delphix. All rights reserved. 26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 27 * Copyright 2017 RackTop Systems. 28 * Copyright (c) 2018 Datto Inc. 29 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association. 30 */ 31 32 /* 33 * Routines to manage ZFS mounts. We separate all the nasty routines that have 34 * to deal with the OS. The following functions are the main entry points -- 35 * they are used by mount and unmount and when changing a filesystem's 36 * mountpoint. 37 * 38 * zfs_is_mounted() 39 * zfs_mount() 40 * zfs_mount_at() 41 * zfs_unmount() 42 * zfs_unmountall() 43 * 44 * This file also contains the functions used to manage sharing filesystems via 45 * NFS and iSCSI: 46 * 47 * zfs_is_shared() 48 * zfs_share() 49 * zfs_unshare() 50 * 51 * zfs_is_shared_nfs() 52 * zfs_is_shared_smb() 53 * zfs_share_proto() 54 * zfs_shareall(); 55 * zfs_unshare_nfs() 56 * zfs_unshare_smb() 57 * zfs_unshareall_nfs() 58 * zfs_unshareall_smb() 59 * zfs_unshareall() 60 * zfs_unshareall_bypath() 61 * 62 * The following functions are available for pool consumers, and will 63 * mount/unmount and share/unshare all datasets within pool: 64 * 65 * zpool_enable_datasets() 66 * zpool_disable_datasets() 67 */ 68 69 #include <dirent.h> 70 #include <dlfcn.h> 71 #include <errno.h> 72 #include <fcntl.h> 73 #include <libgen.h> 74 #include <libintl.h> 75 #include <stdio.h> 76 #include <stdlib.h> 77 #include <strings.h> 78 #include <unistd.h> 79 #include <zone.h> 80 #include <sys/mntent.h> 81 #include <sys/mount.h> 82 #include <sys/stat.h> 83 #include <sys/vfs.h> 84 #include <sys/dsl_crypt.h> 85 86 #include <libzfs.h> 87 88 #include "libzfs_impl.h" 89 #include <thread_pool.h> 90 91 #include <libshare.h> 92 #include <sys/systeminfo.h> 93 #define MAXISALEN 257 /* based on sysinfo(2) man page */ 94 95 static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */ 96 97 static void zfs_mount_task(void *); 98 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **, 99 zfs_share_proto_t); 100 101 /* 102 * The share protocols table must be in the same order as the zfs_share_proto_t 103 * enum in libzfs_impl.h 104 */ 105 proto_table_t proto_table[PROTO_END] = { 106 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED}, 107 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED}, 108 }; 109 110 zfs_share_proto_t nfs_only[] = { 111 PROTO_NFS, 112 PROTO_END 113 }; 114 115 zfs_share_proto_t smb_only[] = { 116 PROTO_SMB, 117 PROTO_END 118 }; 119 zfs_share_proto_t share_all_proto[] = { 120 PROTO_NFS, 121 PROTO_SMB, 122 PROTO_END 123 }; 124 125 126 127 static boolean_t 128 dir_is_empty_stat(const char *dirname) 129 { 130 struct stat st; 131 132 /* 133 * We only want to return false if the given path is a non empty 134 * directory, all other errors are handled elsewhere. 135 */ 136 if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) { 137 return (B_TRUE); 138 } 139 140 /* 141 * An empty directory will still have two entries in it, one 142 * entry for each of "." and "..". 143 */ 144 if (st.st_size > 2) { 145 return (B_FALSE); 146 } 147 148 return (B_TRUE); 149 } 150 151 static boolean_t 152 dir_is_empty_readdir(const char *dirname) 153 { 154 DIR *dirp; 155 struct dirent64 *dp; 156 int dirfd; 157 158 if ((dirfd = openat(AT_FDCWD, dirname, 159 O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) { 160 return (B_TRUE); 161 } 162 163 if ((dirp = fdopendir(dirfd)) == NULL) { 164 (void) close(dirfd); 165 return (B_TRUE); 166 } 167 168 while ((dp = readdir64(dirp)) != NULL) { 169 170 if (strcmp(dp->d_name, ".") == 0 || 171 strcmp(dp->d_name, "..") == 0) 172 continue; 173 174 (void) closedir(dirp); 175 return (B_FALSE); 176 } 177 178 (void) closedir(dirp); 179 return (B_TRUE); 180 } 181 182 /* 183 * Returns true if the specified directory is empty. If we can't open the 184 * directory at all, return true so that the mount can fail with a more 185 * informative error message. 186 */ 187 static boolean_t 188 dir_is_empty(const char *dirname) 189 { 190 struct statfs64 st; 191 192 /* 193 * If the statvfs call fails or the filesystem is not a ZFS 194 * filesystem, fall back to the slow path which uses readdir. 195 */ 196 if ((statfs64(dirname, &st) != 0) || 197 (st.f_type != ZFS_SUPER_MAGIC)) { 198 return (dir_is_empty_readdir(dirname)); 199 } 200 201 /* 202 * At this point, we know the provided path is on a ZFS 203 * filesystem, so we can use stat instead of readdir to 204 * determine if the directory is empty or not. We try to avoid 205 * using readdir because that requires opening "dirname"; this 206 * open file descriptor can potentially end up in a child 207 * process if there's a concurrent fork, thus preventing the 208 * zfs_mount() from otherwise succeeding (the open file 209 * descriptor inherited by the child process will cause the 210 * parent's mount to fail with EBUSY). The performance 211 * implications of replacing the open, read, and close with a 212 * single stat is nice; but is not the main motivation for the 213 * added complexity. 214 */ 215 return (dir_is_empty_stat(dirname)); 216 } 217 218 /* 219 * Checks to see if the mount is active. If the filesystem is mounted, we fill 220 * in 'where' with the current mountpoint, and return 1. Otherwise, we return 221 * 0. 222 */ 223 boolean_t 224 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where) 225 { 226 struct mnttab entry; 227 228 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0) 229 return (B_FALSE); 230 231 if (where != NULL) 232 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp); 233 234 return (B_TRUE); 235 } 236 237 boolean_t 238 zfs_is_mounted(zfs_handle_t *zhp, char **where) 239 { 240 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where)); 241 } 242 243 /* 244 * Checks any higher order concerns about whether the given dataset is 245 * mountable, false otherwise. zfs_is_mountable_internal specifically assumes 246 * that the caller has verified the sanity of mounting the dataset at 247 * mountpoint to the extent the caller wants. 248 */ 249 static boolean_t 250 zfs_is_mountable_internal(zfs_handle_t *zhp, const char *mountpoint) 251 { 252 253 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 254 getzoneid() == GLOBAL_ZONEID) 255 return (B_FALSE); 256 257 return (B_TRUE); 258 } 259 260 /* 261 * Returns true if the given dataset is mountable, false otherwise. Returns the 262 * mountpoint in 'buf'. 263 */ 264 boolean_t 265 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 266 zprop_source_t *source, int flags) 267 { 268 char sourceloc[MAXNAMELEN]; 269 zprop_source_t sourcetype; 270 271 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type, 272 B_FALSE)) 273 return (B_FALSE); 274 275 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 276 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 277 278 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 279 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 280 return (B_FALSE); 281 282 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF) 283 return (B_FALSE); 284 285 if (!zfs_is_mountable_internal(zhp, buf)) 286 return (B_FALSE); 287 288 if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE)) 289 return (B_FALSE); 290 291 if (source) 292 *source = sourcetype; 293 294 return (B_TRUE); 295 } 296 297 /* 298 * The filesystem is mounted by invoking the system mount utility rather 299 * than by the system call mount(2). This ensures that the /etc/mtab 300 * file is correctly locked for the update. Performing our own locking 301 * and /etc/mtab update requires making an unsafe assumption about how 302 * the mount utility performs its locking. Unfortunately, this also means 303 * in the case of a mount failure we do not have the exact errno. We must 304 * make due with return value from the mount process. 305 * 306 * In the long term a shared library called libmount is under development 307 * which provides a common API to address the locking and errno issues. 308 * Once the standard mount utility has been updated to use this library 309 * we can add an autoconf check to conditionally use it. 310 * 311 * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html 312 */ 313 314 static int 315 zfs_add_option(zfs_handle_t *zhp, char *options, int len, 316 zfs_prop_t prop, char *on, char *off) 317 { 318 char *source; 319 uint64_t value; 320 321 /* Skip adding duplicate default options */ 322 if ((strstr(options, on) != NULL) || (strstr(options, off) != NULL)) 323 return (0); 324 325 /* 326 * zfs_prop_get_int() is not used to ensure our mount options 327 * are not influenced by the current /proc/self/mounts contents. 328 */ 329 value = getprop_uint64(zhp, prop, &source); 330 331 (void) strlcat(options, ",", len); 332 (void) strlcat(options, value ? on : off, len); 333 334 return (0); 335 } 336 337 static int 338 zfs_add_options(zfs_handle_t *zhp, char *options, int len) 339 { 340 int error = 0; 341 342 error = zfs_add_option(zhp, options, len, 343 ZFS_PROP_ATIME, MNTOPT_ATIME, MNTOPT_NOATIME); 344 /* 345 * don't add relatime/strictatime when atime=off, otherwise strictatime 346 * will force atime=on 347 */ 348 if (strstr(options, MNTOPT_NOATIME) == NULL) { 349 error = zfs_add_option(zhp, options, len, 350 ZFS_PROP_RELATIME, MNTOPT_RELATIME, MNTOPT_STRICTATIME); 351 } 352 error = error ? error : zfs_add_option(zhp, options, len, 353 ZFS_PROP_DEVICES, MNTOPT_DEVICES, MNTOPT_NODEVICES); 354 error = error ? error : zfs_add_option(zhp, options, len, 355 ZFS_PROP_EXEC, MNTOPT_EXEC, MNTOPT_NOEXEC); 356 error = error ? error : zfs_add_option(zhp, options, len, 357 ZFS_PROP_READONLY, MNTOPT_RO, MNTOPT_RW); 358 error = error ? error : zfs_add_option(zhp, options, len, 359 ZFS_PROP_SETUID, MNTOPT_SETUID, MNTOPT_NOSETUID); 360 error = error ? error : zfs_add_option(zhp, options, len, 361 ZFS_PROP_NBMAND, MNTOPT_NBMAND, MNTOPT_NONBMAND); 362 363 return (error); 364 } 365 366 int 367 zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 368 { 369 char mountpoint[ZFS_MAXPROPLEN]; 370 371 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 372 flags)) 373 return (0); 374 375 return (zfs_mount_at(zhp, options, flags, mountpoint)); 376 } 377 378 /* 379 * Mount the given filesystem. 380 */ 381 int 382 zfs_mount_at(zfs_handle_t *zhp, const char *options, int flags, 383 const char *mountpoint) 384 { 385 struct stat buf; 386 char mntopts[MNT_LINE_MAX]; 387 char overlay[ZFS_MAXPROPLEN]; 388 libzfs_handle_t *hdl = zhp->zfs_hdl; 389 uint64_t keystatus; 390 int remount = 0, rc; 391 392 if (options == NULL) { 393 (void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts)); 394 } else { 395 (void) strlcpy(mntopts, options, sizeof (mntopts)); 396 } 397 398 if (strstr(mntopts, MNTOPT_REMOUNT) != NULL) 399 remount = 1; 400 401 /* Potentially duplicates some checks if invoked by zfs_mount(). */ 402 if (!zfs_is_mountable_internal(zhp, mountpoint)) 403 return (0); 404 405 /* 406 * If the pool is imported read-only then all mounts must be read-only 407 */ 408 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL)) 409 (void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts)); 410 411 /* 412 * Append default mount options which apply to the mount point. 413 * This is done because under Linux (unlike Solaris) multiple mount 414 * points may reference a single super block. This means that just 415 * given a super block there is no back reference to update the per 416 * mount point options. 417 */ 418 rc = zfs_add_options(zhp, mntopts, sizeof (mntopts)); 419 if (rc) { 420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 421 "default options unavailable")); 422 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 423 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 424 mountpoint)); 425 } 426 427 /* 428 * If the filesystem is encrypted the key must be loaded in order to 429 * mount. If the key isn't loaded, the MS_CRYPT flag decides whether 430 * or not we attempt to load the keys. Note: we must call 431 * zfs_refresh_properties() here since some callers of this function 432 * (most notably zpool_enable_datasets()) may implicitly load our key 433 * by loading the parent's key first. 434 */ 435 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) { 436 zfs_refresh_properties(zhp); 437 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS); 438 439 /* 440 * If the key is unavailable and MS_CRYPT is set give the 441 * user a chance to enter the key. Otherwise just fail 442 * immediately. 443 */ 444 if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) { 445 if (flags & MS_CRYPT) { 446 rc = zfs_crypto_load_key(zhp, B_FALSE, NULL); 447 if (rc) 448 return (rc); 449 } else { 450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 451 "encryption key not loaded")); 452 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 453 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 454 mountpoint)); 455 } 456 } 457 458 } 459 460 /* 461 * Append zfsutil option so the mount helper allow the mount 462 */ 463 strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts)); 464 465 /* Create the directory if it doesn't already exist */ 466 if (lstat(mountpoint, &buf) != 0) { 467 if (mkdirp(mountpoint, 0755) != 0) { 468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 469 "failed to create mountpoint: %s"), 470 strerror(errno)); 471 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 472 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 473 mountpoint)); 474 } 475 } 476 477 /* 478 * Overlay mounts are enabled by default but may be disabled 479 * via the 'overlay' property. The -O flag remains for compatibility. 480 */ 481 if (!(flags & MS_OVERLAY)) { 482 if (zfs_prop_get(zhp, ZFS_PROP_OVERLAY, overlay, 483 sizeof (overlay), NULL, NULL, 0, B_FALSE) == 0) { 484 if (strcmp(overlay, "on") == 0) { 485 flags |= MS_OVERLAY; 486 } 487 } 488 } 489 490 /* 491 * Determine if the mountpoint is empty. If so, refuse to perform the 492 * mount. We don't perform this check if 'remount' is 493 * specified or if overlay option (-O) is given 494 */ 495 if ((flags & MS_OVERLAY) == 0 && !remount && 496 !dir_is_empty(mountpoint)) { 497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 498 "directory is not empty")); 499 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 500 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 501 } 502 503 /* perform the mount */ 504 rc = do_mount(zhp, mountpoint, mntopts, flags); 505 if (rc) { 506 /* 507 * Generic errors are nasty, but there are just way too many 508 * from mount(), and they're well-understood. We pick a few 509 * common ones to improve upon. 510 */ 511 if (rc == EBUSY) { 512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 513 "mountpoint or dataset is busy")); 514 } else if (rc == EPERM) { 515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 516 "Insufficient privileges")); 517 } else if (rc == ENOTSUP) { 518 char buf[256]; 519 int spa_version; 520 521 VERIFY(zfs_spa_version(zhp, &spa_version) == 0); 522 (void) snprintf(buf, sizeof (buf), 523 dgettext(TEXT_DOMAIN, "Can't mount a version %lld " 524 "file system on a version %d pool. Pool must be" 525 " upgraded to mount this file system."), 526 (u_longlong_t)zfs_prop_get_int(zhp, 527 ZFS_PROP_VERSION), spa_version); 528 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf)); 529 } else { 530 zfs_error_aux(hdl, strerror(rc)); 531 } 532 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 533 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 534 zhp->zfs_name)); 535 } 536 537 /* remove the mounted entry before re-adding on remount */ 538 if (remount) 539 libzfs_mnttab_remove(hdl, zhp->zfs_name); 540 541 /* add the mounted entry into our cache */ 542 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts); 543 return (0); 544 } 545 546 /* 547 * Unmount a single filesystem. 548 */ 549 static int 550 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 551 { 552 int error; 553 554 error = do_unmount(mountpoint, flags); 555 if (error != 0) { 556 int libzfs_err; 557 558 switch (error) { 559 case EBUSY: 560 libzfs_err = EZFS_BUSY; 561 break; 562 case EIO: 563 libzfs_err = EZFS_IO; 564 break; 565 case ENOENT: 566 libzfs_err = EZFS_NOENT; 567 break; 568 case ENOMEM: 569 libzfs_err = EZFS_NOMEM; 570 break; 571 case EPERM: 572 libzfs_err = EZFS_PERM; 573 break; 574 default: 575 libzfs_err = EZFS_UMOUNTFAILED; 576 } 577 return (zfs_error_fmt(hdl, libzfs_err, 578 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 579 mountpoint)); 580 } 581 582 return (0); 583 } 584 585 /* 586 * Unmount the given filesystem. 587 */ 588 int 589 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 590 { 591 libzfs_handle_t *hdl = zhp->zfs_hdl; 592 struct mnttab entry; 593 char *mntpt = NULL; 594 boolean_t encroot, unmounted = B_FALSE; 595 596 /* check to see if we need to unmount the filesystem */ 597 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 598 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) { 599 /* 600 * mountpoint may have come from a call to 601 * getmnt/getmntany if it isn't NULL. If it is NULL, 602 * we know it comes from libzfs_mnttab_find which can 603 * then get freed later. We strdup it to play it safe. 604 */ 605 if (mountpoint == NULL) 606 mntpt = zfs_strdup(hdl, entry.mnt_mountp); 607 else 608 mntpt = zfs_strdup(hdl, mountpoint); 609 610 /* 611 * Unshare and unmount the filesystem 612 */ 613 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) { 614 free(mntpt); 615 return (-1); 616 } 617 zfs_commit_all_shares(); 618 619 if (unmount_one(hdl, mntpt, flags) != 0) { 620 free(mntpt); 621 (void) zfs_shareall(zhp); 622 zfs_commit_all_shares(); 623 return (-1); 624 } 625 626 libzfs_mnttab_remove(hdl, zhp->zfs_name); 627 free(mntpt); 628 unmounted = B_TRUE; 629 } 630 631 /* 632 * If the MS_CRYPT flag is provided we must ensure we attempt to 633 * unload the dataset's key regardless of whether we did any work 634 * to unmount it. We only do this for encryption roots. 635 */ 636 if ((flags & MS_CRYPT) != 0 && 637 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) { 638 zfs_refresh_properties(zhp); 639 640 if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0 && 641 unmounted) { 642 (void) zfs_mount(zhp, NULL, 0); 643 return (-1); 644 } 645 646 if (encroot && zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) == 647 ZFS_KEYSTATUS_AVAILABLE && 648 zfs_crypto_unload_key(zhp) != 0) { 649 (void) zfs_mount(zhp, NULL, 0); 650 return (-1); 651 } 652 } 653 654 return (0); 655 } 656 657 /* 658 * Unmount this filesystem and any children inheriting the mountpoint property. 659 * To do this, just act like we're changing the mountpoint property, but don't 660 * remount the filesystems afterwards. 661 */ 662 int 663 zfs_unmountall(zfs_handle_t *zhp, int flags) 664 { 665 prop_changelist_t *clp; 666 int ret; 667 668 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 669 CL_GATHER_ITER_MOUNTED, flags); 670 if (clp == NULL) 671 return (-1); 672 673 ret = changelist_prefix(clp); 674 changelist_free(clp); 675 676 return (ret); 677 } 678 679 boolean_t 680 zfs_is_shared(zfs_handle_t *zhp) 681 { 682 zfs_share_type_t rc = 0; 683 zfs_share_proto_t *curr_proto; 684 685 if (ZFS_IS_VOLUME(zhp)) 686 return (B_FALSE); 687 688 for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 689 curr_proto++) 690 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto); 691 692 return (rc ? B_TRUE : B_FALSE); 693 } 694 695 /* 696 * Unshare a filesystem by mountpoint. 697 */ 698 int 699 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint, 700 zfs_share_proto_t proto) 701 { 702 int err; 703 704 err = sa_disable_share(mountpoint, proto_table[proto].p_name); 705 if (err != SA_OK) { 706 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err, 707 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 708 name, sa_errorstr(err))); 709 } 710 return (0); 711 } 712 713 /* 714 * Query libshare for the given mountpoint and protocol, returning 715 * a zfs_share_type_t value. 716 */ 717 zfs_share_type_t 718 is_shared(const char *mountpoint, zfs_share_proto_t proto) 719 { 720 if (sa_is_shared(mountpoint, proto_table[proto].p_name)) { 721 switch (proto) { 722 case PROTO_NFS: 723 return (SHARED_NFS); 724 case PROTO_SMB: 725 return (SHARED_SMB); 726 default: 727 return (SHARED_NOT_SHARED); 728 } 729 } 730 return (SHARED_NOT_SHARED); 731 } 732 733 /* 734 * Share the given filesystem according to the options in the specified 735 * protocol specific properties (sharenfs, sharesmb). We rely 736 * on "libshare" to do the dirty work for us. 737 */ 738 int 739 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 740 { 741 char mountpoint[ZFS_MAXPROPLEN]; 742 char shareopts[ZFS_MAXPROPLEN]; 743 char sourcestr[ZFS_MAXPROPLEN]; 744 zfs_share_proto_t *curr_proto; 745 zprop_source_t sourcetype; 746 int err = 0; 747 748 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0)) 749 return (0); 750 751 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) { 752 /* 753 * Return success if there are no share options. 754 */ 755 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop, 756 shareopts, sizeof (shareopts), &sourcetype, sourcestr, 757 ZFS_MAXPROPLEN, B_FALSE) != 0 || 758 strcmp(shareopts, "off") == 0) 759 continue; 760 761 /* 762 * If the 'zoned' property is set, then zfs_is_mountable() 763 * will have already bailed out if we are in the global zone. 764 * But local zones cannot be NFS servers, so we ignore it for 765 * local zones as well. 766 */ 767 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 768 continue; 769 770 err = sa_enable_share(zfs_get_name(zhp), mountpoint, shareopts, 771 proto_table[*curr_proto].p_name); 772 if (err != SA_OK) { 773 return (zfs_error_fmt(zhp->zfs_hdl, 774 proto_table[*curr_proto].p_share_err, 775 dgettext(TEXT_DOMAIN, "cannot share '%s: %s'"), 776 zfs_get_name(zhp), sa_errorstr(err))); 777 } 778 779 } 780 return (0); 781 } 782 783 int 784 zfs_share(zfs_handle_t *zhp) 785 { 786 assert(!ZFS_IS_VOLUME(zhp)); 787 return (zfs_share_proto(zhp, share_all_proto)); 788 } 789 790 int 791 zfs_unshare(zfs_handle_t *zhp) 792 { 793 assert(!ZFS_IS_VOLUME(zhp)); 794 return (zfs_unshareall(zhp)); 795 } 796 797 /* 798 * Check to see if the filesystem is currently shared. 799 */ 800 zfs_share_type_t 801 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto) 802 { 803 char *mountpoint; 804 zfs_share_type_t rc; 805 806 if (!zfs_is_mounted(zhp, &mountpoint)) 807 return (SHARED_NOT_SHARED); 808 809 if ((rc = is_shared(mountpoint, proto)) 810 != SHARED_NOT_SHARED) { 811 if (where != NULL) 812 *where = mountpoint; 813 else 814 free(mountpoint); 815 return (rc); 816 } else { 817 free(mountpoint); 818 return (SHARED_NOT_SHARED); 819 } 820 } 821 822 boolean_t 823 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 824 { 825 return (zfs_is_shared_proto(zhp, where, 826 PROTO_NFS) != SHARED_NOT_SHARED); 827 } 828 829 boolean_t 830 zfs_is_shared_smb(zfs_handle_t *zhp, char **where) 831 { 832 return (zfs_is_shared_proto(zhp, where, 833 PROTO_SMB) != SHARED_NOT_SHARED); 834 } 835 836 /* 837 * zfs_parse_options(options, proto) 838 * 839 * Call the legacy parse interface to get the protocol specific 840 * options using the NULL arg to indicate that this is a "parse" only. 841 */ 842 int 843 zfs_parse_options(char *options, zfs_share_proto_t proto) 844 { 845 return (sa_validate_shareopts(options, proto_table[proto].p_name)); 846 } 847 848 void 849 zfs_commit_proto(zfs_share_proto_t *proto) 850 { 851 zfs_share_proto_t *curr_proto; 852 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) { 853 sa_commit_shares(proto_table[*curr_proto].p_name); 854 } 855 } 856 857 void 858 zfs_commit_nfs_shares(void) 859 { 860 zfs_commit_proto(nfs_only); 861 } 862 863 void 864 zfs_commit_smb_shares(void) 865 { 866 zfs_commit_proto(smb_only); 867 } 868 869 void 870 zfs_commit_all_shares(void) 871 { 872 zfs_commit_proto(share_all_proto); 873 } 874 875 void 876 zfs_commit_shares(const char *proto) 877 { 878 if (proto == NULL) 879 zfs_commit_proto(share_all_proto); 880 else if (strcmp(proto, "nfs") == 0) 881 zfs_commit_proto(nfs_only); 882 else if (strcmp(proto, "smb") == 0) 883 zfs_commit_proto(smb_only); 884 } 885 886 int 887 zfs_share_nfs(zfs_handle_t *zhp) 888 { 889 return (zfs_share_proto(zhp, nfs_only)); 890 } 891 892 int 893 zfs_share_smb(zfs_handle_t *zhp) 894 { 895 return (zfs_share_proto(zhp, smb_only)); 896 } 897 898 int 899 zfs_shareall(zfs_handle_t *zhp) 900 { 901 return (zfs_share_proto(zhp, share_all_proto)); 902 } 903 904 /* 905 * Unshare the given filesystem. 906 */ 907 int 908 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint, 909 zfs_share_proto_t *proto) 910 { 911 libzfs_handle_t *hdl = zhp->zfs_hdl; 912 struct mnttab entry; 913 char *mntpt = NULL; 914 915 /* check to see if need to unmount the filesystem */ 916 if (mountpoint != NULL) 917 mntpt = zfs_strdup(hdl, mountpoint); 918 919 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 920 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) { 921 zfs_share_proto_t *curr_proto; 922 923 if (mountpoint == NULL) 924 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 925 926 for (curr_proto = proto; *curr_proto != PROTO_END; 927 curr_proto++) { 928 929 if (is_shared(mntpt, *curr_proto)) { 930 if (unshare_one(hdl, zhp->zfs_name, 931 mntpt, *curr_proto) != 0) { 932 if (mntpt != NULL) 933 free(mntpt); 934 return (-1); 935 } 936 } 937 } 938 } 939 if (mntpt != NULL) 940 free(mntpt); 941 942 return (0); 943 } 944 945 int 946 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 947 { 948 return (zfs_unshare_proto(zhp, mountpoint, nfs_only)); 949 } 950 951 int 952 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint) 953 { 954 return (zfs_unshare_proto(zhp, mountpoint, smb_only)); 955 } 956 957 /* 958 * Same as zfs_unmountall(), but for NFS and SMB unshares. 959 */ 960 static int 961 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 962 { 963 prop_changelist_t *clp; 964 int ret; 965 966 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0); 967 if (clp == NULL) 968 return (-1); 969 970 ret = changelist_unshare(clp, proto); 971 changelist_free(clp); 972 973 return (ret); 974 } 975 976 int 977 zfs_unshareall_nfs(zfs_handle_t *zhp) 978 { 979 return (zfs_unshareall_proto(zhp, nfs_only)); 980 } 981 982 int 983 zfs_unshareall_smb(zfs_handle_t *zhp) 984 { 985 return (zfs_unshareall_proto(zhp, smb_only)); 986 } 987 988 int 989 zfs_unshareall(zfs_handle_t *zhp) 990 { 991 return (zfs_unshareall_proto(zhp, share_all_proto)); 992 } 993 994 int 995 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint) 996 { 997 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); 998 } 999 1000 int 1001 zfs_unshareall_bytype(zfs_handle_t *zhp, const char *mountpoint, 1002 const char *proto) 1003 { 1004 if (proto == NULL) 1005 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); 1006 if (strcmp(proto, "nfs") == 0) 1007 return (zfs_unshare_proto(zhp, mountpoint, nfs_only)); 1008 else if (strcmp(proto, "smb") == 0) 1009 return (zfs_unshare_proto(zhp, mountpoint, smb_only)); 1010 else 1011 return (1); 1012 } 1013 1014 /* 1015 * Remove the mountpoint associated with the current dataset, if necessary. 1016 * We only remove the underlying directory if: 1017 * 1018 * - The mountpoint is not 'none' or 'legacy' 1019 * - The mountpoint is non-empty 1020 * - The mountpoint is the default or inherited 1021 * - The 'zoned' property is set, or we're in a local zone 1022 * 1023 * Any other directories we leave alone. 1024 */ 1025 void 1026 remove_mountpoint(zfs_handle_t *zhp) 1027 { 1028 char mountpoint[ZFS_MAXPROPLEN]; 1029 zprop_source_t source; 1030 1031 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 1032 &source, 0)) 1033 return; 1034 1035 if (source == ZPROP_SRC_DEFAULT || 1036 source == ZPROP_SRC_INHERITED) { 1037 /* 1038 * Try to remove the directory, silently ignoring any errors. 1039 * The filesystem may have since been removed or moved around, 1040 * and this error isn't really useful to the administrator in 1041 * any way. 1042 */ 1043 (void) rmdir(mountpoint); 1044 } 1045 } 1046 1047 /* 1048 * Add the given zfs handle to the cb_handles array, dynamically reallocating 1049 * the array if it is out of space. 1050 */ 1051 void 1052 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp) 1053 { 1054 if (cbp->cb_alloc == cbp->cb_used) { 1055 size_t newsz; 1056 zfs_handle_t **newhandles; 1057 1058 newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64; 1059 newhandles = zfs_realloc(zhp->zfs_hdl, 1060 cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *), 1061 newsz * sizeof (zfs_handle_t *)); 1062 cbp->cb_handles = newhandles; 1063 cbp->cb_alloc = newsz; 1064 } 1065 cbp->cb_handles[cbp->cb_used++] = zhp; 1066 } 1067 1068 /* 1069 * Recursive helper function used during file system enumeration 1070 */ 1071 static int 1072 zfs_iter_cb(zfs_handle_t *zhp, void *data) 1073 { 1074 get_all_cb_t *cbp = data; 1075 1076 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) { 1077 zfs_close(zhp); 1078 return (0); 1079 } 1080 1081 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) { 1082 zfs_close(zhp); 1083 return (0); 1084 } 1085 1086 if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) == 1087 ZFS_KEYSTATUS_UNAVAILABLE) { 1088 zfs_close(zhp); 1089 return (0); 1090 } 1091 1092 /* 1093 * If this filesystem is inconsistent and has a receive resume 1094 * token, we can not mount it. 1095 */ 1096 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) && 1097 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 1098 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) { 1099 zfs_close(zhp); 1100 return (0); 1101 } 1102 1103 libzfs_add_handle(cbp, zhp); 1104 if (zfs_iter_filesystems(zhp, zfs_iter_cb, cbp) != 0) { 1105 zfs_close(zhp); 1106 return (-1); 1107 } 1108 return (0); 1109 } 1110 1111 /* 1112 * Sort comparator that compares two mountpoint paths. We sort these paths so 1113 * that subdirectories immediately follow their parents. This means that we 1114 * effectively treat the '/' character as the lowest value non-nul char. 1115 * Since filesystems from non-global zones can have the same mountpoint 1116 * as other filesystems, the comparator sorts global zone filesystems to 1117 * the top of the list. This means that the global zone will traverse the 1118 * filesystem list in the correct order and can stop when it sees the 1119 * first zoned filesystem. In a non-global zone, only the delegated 1120 * filesystems are seen. 1121 * 1122 * An example sorted list using this comparator would look like: 1123 * 1124 * /foo 1125 * /foo/bar 1126 * /foo/bar/baz 1127 * /foo/baz 1128 * /foo.bar 1129 * /foo (NGZ1) 1130 * /foo (NGZ2) 1131 * 1132 * The mounting code depends on this ordering to deterministically iterate 1133 * over filesystems in order to spawn parallel mount tasks. 1134 */ 1135 static int 1136 mountpoint_cmp(const void *arga, const void *argb) 1137 { 1138 zfs_handle_t *const *zap = arga; 1139 zfs_handle_t *za = *zap; 1140 zfs_handle_t *const *zbp = argb; 1141 zfs_handle_t *zb = *zbp; 1142 char mounta[MAXPATHLEN]; 1143 char mountb[MAXPATHLEN]; 1144 const char *a = mounta; 1145 const char *b = mountb; 1146 boolean_t gota, gotb; 1147 uint64_t zoneda, zonedb; 1148 1149 zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED); 1150 zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED); 1151 if (zoneda && !zonedb) 1152 return (1); 1153 if (!zoneda && zonedb) 1154 return (-1); 1155 1156 gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM); 1157 if (gota) { 1158 verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta, 1159 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 1160 } 1161 gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM); 1162 if (gotb) { 1163 verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb, 1164 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 1165 } 1166 1167 if (gota && gotb) { 1168 while (*a != '\0' && (*a == *b)) { 1169 a++; 1170 b++; 1171 } 1172 if (*a == *b) 1173 return (0); 1174 if (*a == '\0') 1175 return (-1); 1176 if (*b == '\0') 1177 return (1); 1178 if (*a == '/') 1179 return (-1); 1180 if (*b == '/') 1181 return (1); 1182 return (*a < *b ? -1 : *a > *b); 1183 } 1184 1185 if (gota) 1186 return (-1); 1187 if (gotb) 1188 return (1); 1189 1190 /* 1191 * If neither filesystem has a mountpoint, revert to sorting by 1192 * dataset name. 1193 */ 1194 return (strcmp(zfs_get_name(za), zfs_get_name(zb))); 1195 } 1196 1197 /* 1198 * Return true if path2 is a child of path1 or path2 equals path1 or 1199 * path1 is "/" (path2 is always a child of "/"). 1200 */ 1201 static boolean_t 1202 libzfs_path_contains(const char *path1, const char *path2) 1203 { 1204 return (strcmp(path1, path2) == 0 || strcmp(path1, "/") == 0 || 1205 (strstr(path2, path1) == path2 && path2[strlen(path1)] == '/')); 1206 } 1207 1208 /* 1209 * Given a mountpoint specified by idx in the handles array, find the first 1210 * non-descendent of that mountpoint and return its index. Descendant paths 1211 * start with the parent's path. This function relies on the ordering 1212 * enforced by mountpoint_cmp(). 1213 */ 1214 static int 1215 non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx) 1216 { 1217 char parent[ZFS_MAXPROPLEN]; 1218 char child[ZFS_MAXPROPLEN]; 1219 int i; 1220 1221 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent, 1222 sizeof (parent), NULL, NULL, 0, B_FALSE) == 0); 1223 1224 for (i = idx + 1; i < num_handles; i++) { 1225 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child, 1226 sizeof (child), NULL, NULL, 0, B_FALSE) == 0); 1227 if (!libzfs_path_contains(parent, child)) 1228 break; 1229 } 1230 return (i); 1231 } 1232 1233 typedef struct mnt_param { 1234 libzfs_handle_t *mnt_hdl; 1235 tpool_t *mnt_tp; 1236 zfs_handle_t **mnt_zhps; /* filesystems to mount */ 1237 size_t mnt_num_handles; 1238 int mnt_idx; /* Index of selected entry to mount */ 1239 zfs_iter_f mnt_func; 1240 void *mnt_data; 1241 } mnt_param_t; 1242 1243 /* 1244 * Allocate and populate the parameter struct for mount function, and 1245 * schedule mounting of the entry selected by idx. 1246 */ 1247 static void 1248 zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles, 1249 size_t num_handles, int idx, zfs_iter_f func, void *data, tpool_t *tp) 1250 { 1251 mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t)); 1252 1253 mnt_param->mnt_hdl = hdl; 1254 mnt_param->mnt_tp = tp; 1255 mnt_param->mnt_zhps = handles; 1256 mnt_param->mnt_num_handles = num_handles; 1257 mnt_param->mnt_idx = idx; 1258 mnt_param->mnt_func = func; 1259 mnt_param->mnt_data = data; 1260 1261 (void) tpool_dispatch(tp, zfs_mount_task, (void*)mnt_param); 1262 } 1263 1264 /* 1265 * This is the structure used to keep state of mounting or sharing operations 1266 * during a call to zpool_enable_datasets(). 1267 */ 1268 typedef struct mount_state { 1269 /* 1270 * ms_mntstatus is set to -1 if any mount fails. While multiple threads 1271 * could update this variable concurrently, no synchronization is 1272 * needed as it's only ever set to -1. 1273 */ 1274 int ms_mntstatus; 1275 int ms_mntflags; 1276 const char *ms_mntopts; 1277 } mount_state_t; 1278 1279 static int 1280 zfs_mount_one(zfs_handle_t *zhp, void *arg) 1281 { 1282 mount_state_t *ms = arg; 1283 int ret = 0; 1284 1285 /* 1286 * don't attempt to mount encrypted datasets with 1287 * unloaded keys 1288 */ 1289 if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) == 1290 ZFS_KEYSTATUS_UNAVAILABLE) 1291 return (0); 1292 1293 if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0) 1294 ret = ms->ms_mntstatus = -1; 1295 return (ret); 1296 } 1297 1298 static int 1299 zfs_share_one(zfs_handle_t *zhp, void *arg) 1300 { 1301 mount_state_t *ms = arg; 1302 int ret = 0; 1303 1304 if (zfs_share(zhp) != 0) 1305 ret = ms->ms_mntstatus = -1; 1306 return (ret); 1307 } 1308 1309 /* 1310 * Thread pool function to mount one file system. On completion, it finds and 1311 * schedules its children to be mounted. This depends on the sorting done in 1312 * zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries 1313 * each descending from the previous) will have no parallelism since we always 1314 * have to wait for the parent to finish mounting before we can schedule 1315 * its children. 1316 */ 1317 static void 1318 zfs_mount_task(void *arg) 1319 { 1320 mnt_param_t *mp = arg; 1321 int idx = mp->mnt_idx; 1322 zfs_handle_t **handles = mp->mnt_zhps; 1323 size_t num_handles = mp->mnt_num_handles; 1324 char mountpoint[ZFS_MAXPROPLEN]; 1325 1326 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint, 1327 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 1328 1329 if (mp->mnt_func(handles[idx], mp->mnt_data) != 0) 1330 return; 1331 1332 /* 1333 * We dispatch tasks to mount filesystems with mountpoints underneath 1334 * this one. We do this by dispatching the next filesystem with a 1335 * descendant mountpoint of the one we just mounted, then skip all of 1336 * its descendants, dispatch the next descendant mountpoint, and so on. 1337 * The non_descendant_idx() function skips over filesystems that are 1338 * descendants of the filesystem we just dispatched. 1339 */ 1340 for (int i = idx + 1; i < num_handles; 1341 i = non_descendant_idx(handles, num_handles, i)) { 1342 char child[ZFS_MAXPROPLEN]; 1343 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, 1344 child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0); 1345 1346 if (!libzfs_path_contains(mountpoint, child)) 1347 break; /* not a descendant, return */ 1348 zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i, 1349 mp->mnt_func, mp->mnt_data, mp->mnt_tp); 1350 } 1351 free(mp); 1352 } 1353 1354 /* 1355 * Issue the func callback for each ZFS handle contained in the handles 1356 * array. This function is used to mount all datasets, and so this function 1357 * guarantees that filesystems for parent mountpoints are called before their 1358 * children. As such, before issuing any callbacks, we first sort the array 1359 * of handles by mountpoint. 1360 * 1361 * Callbacks are issued in one of two ways: 1362 * 1363 * 1. Sequentially: If the parallel argument is B_FALSE or the ZFS_SERIAL_MOUNT 1364 * environment variable is set, then we issue callbacks sequentially. 1365 * 1366 * 2. In parallel: If the parallel argument is B_TRUE and the ZFS_SERIAL_MOUNT 1367 * environment variable is not set, then we use a tpool to dispatch threads 1368 * to mount filesystems in parallel. This function dispatches tasks to mount 1369 * the filesystems at the top-level mountpoints, and these tasks in turn 1370 * are responsible for recursively mounting filesystems in their children 1371 * mountpoints. 1372 */ 1373 void 1374 zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles, 1375 size_t num_handles, zfs_iter_f func, void *data, boolean_t parallel) 1376 { 1377 zoneid_t zoneid = getzoneid(); 1378 1379 /* 1380 * The ZFS_SERIAL_MOUNT environment variable is an undocumented 1381 * variable that can be used as a convenience to do a/b comparison 1382 * of serial vs. parallel mounting. 1383 */ 1384 boolean_t serial_mount = !parallel || 1385 (getenv("ZFS_SERIAL_MOUNT") != NULL); 1386 1387 /* 1388 * Sort the datasets by mountpoint. See mountpoint_cmp for details 1389 * of how these are sorted. 1390 */ 1391 qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp); 1392 1393 if (serial_mount) { 1394 for (int i = 0; i < num_handles; i++) { 1395 func(handles[i], data); 1396 } 1397 return; 1398 } 1399 1400 /* 1401 * Issue the callback function for each dataset using a parallel 1402 * algorithm that uses a thread pool to manage threads. 1403 */ 1404 tpool_t *tp = tpool_create(1, mount_tp_nthr, 0, NULL); 1405 1406 /* 1407 * There may be multiple "top level" mountpoints outside of the pool's 1408 * root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of 1409 * these. 1410 */ 1411 for (int i = 0; i < num_handles; 1412 i = non_descendant_idx(handles, num_handles, i)) { 1413 /* 1414 * Since the mountpoints have been sorted so that the zoned 1415 * filesystems are at the end, a zoned filesystem seen from 1416 * the global zone means that we're done. 1417 */ 1418 if (zoneid == GLOBAL_ZONEID && 1419 zfs_prop_get_int(handles[i], ZFS_PROP_ZONED)) 1420 break; 1421 zfs_dispatch_mount(hdl, handles, num_handles, i, func, data, 1422 tp); 1423 } 1424 1425 tpool_wait(tp); /* wait for all scheduled mounts to complete */ 1426 tpool_destroy(tp); 1427 } 1428 1429 /* 1430 * Mount and share all datasets within the given pool. This assumes that no 1431 * datasets within the pool are currently mounted. 1432 */ 1433 #pragma weak zpool_mount_datasets = zpool_enable_datasets 1434 int 1435 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 1436 { 1437 get_all_cb_t cb = { 0 }; 1438 mount_state_t ms = { 0 }; 1439 zfs_handle_t *zfsp; 1440 int ret = 0; 1441 1442 if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 1443 ZFS_TYPE_DATASET)) == NULL) 1444 goto out; 1445 1446 /* 1447 * Gather all non-snapshot datasets within the pool. Start by adding 1448 * the root filesystem for this pool to the list, and then iterate 1449 * over all child filesystems. 1450 */ 1451 libzfs_add_handle(&cb, zfsp); 1452 if (zfs_iter_filesystems(zfsp, zfs_iter_cb, &cb) != 0) 1453 goto out; 1454 1455 /* 1456 * Mount all filesystems 1457 */ 1458 ms.ms_mntopts = mntopts; 1459 ms.ms_mntflags = flags; 1460 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used, 1461 zfs_mount_one, &ms, B_TRUE); 1462 if (ms.ms_mntstatus != 0) 1463 ret = ms.ms_mntstatus; 1464 1465 /* 1466 * Share all filesystems that need to be shared. This needs to be 1467 * a separate pass because libshare is not mt-safe, and so we need 1468 * to share serially. 1469 */ 1470 ms.ms_mntstatus = 0; 1471 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used, 1472 zfs_share_one, &ms, B_FALSE); 1473 if (ms.ms_mntstatus != 0) 1474 ret = ms.ms_mntstatus; 1475 else 1476 zfs_commit_all_shares(); 1477 1478 out: 1479 for (int i = 0; i < cb.cb_used; i++) 1480 zfs_close(cb.cb_handles[i]); 1481 free(cb.cb_handles); 1482 1483 return (ret); 1484 } 1485 1486 static int 1487 mountpoint_compare(const void *a, const void *b) 1488 { 1489 const char *mounta = *((char **)a); 1490 const char *mountb = *((char **)b); 1491 1492 return (strcmp(mountb, mounta)); 1493 } 1494 1495 /* alias for 2002/240 */ 1496 #pragma weak zpool_unmount_datasets = zpool_disable_datasets 1497 /* 1498 * Unshare and unmount all datasets within the given pool. We don't want to 1499 * rely on traversing the DSL to discover the filesystems within the pool, 1500 * because this may be expensive (if not all of them are mounted), and can fail 1501 * arbitrarily (on I/O error, for example). Instead, we walk /proc/self/mounts 1502 * and gather all the filesystems that are currently mounted. 1503 */ 1504 int 1505 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 1506 { 1507 int used, alloc; 1508 struct mnttab entry; 1509 size_t namelen; 1510 char **mountpoints = NULL; 1511 zfs_handle_t **datasets = NULL; 1512 libzfs_handle_t *hdl = zhp->zpool_hdl; 1513 int i; 1514 int ret = -1; 1515 int flags = (force ? MS_FORCE : 0); 1516 1517 namelen = strlen(zhp->zpool_name); 1518 1519 /* Reopen MNTTAB to prevent reading stale data from open file */ 1520 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL) 1521 return (ENOENT); 1522 1523 used = alloc = 0; 1524 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 1525 /* 1526 * Ignore non-ZFS entries. 1527 */ 1528 if (entry.mnt_fstype == NULL || 1529 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 1530 continue; 1531 1532 /* 1533 * Ignore filesystems not within this pool. 1534 */ 1535 if (entry.mnt_mountp == NULL || 1536 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 1537 (entry.mnt_special[namelen] != '/' && 1538 entry.mnt_special[namelen] != '\0')) 1539 continue; 1540 1541 /* 1542 * At this point we've found a filesystem within our pool. Add 1543 * it to our growing list. 1544 */ 1545 if (used == alloc) { 1546 if (alloc == 0) { 1547 if ((mountpoints = zfs_alloc(hdl, 1548 8 * sizeof (void *))) == NULL) 1549 goto out; 1550 1551 if ((datasets = zfs_alloc(hdl, 1552 8 * sizeof (void *))) == NULL) 1553 goto out; 1554 1555 alloc = 8; 1556 } else { 1557 void *ptr; 1558 1559 if ((ptr = zfs_realloc(hdl, mountpoints, 1560 alloc * sizeof (void *), 1561 alloc * 2 * sizeof (void *))) == NULL) 1562 goto out; 1563 mountpoints = ptr; 1564 1565 if ((ptr = zfs_realloc(hdl, datasets, 1566 alloc * sizeof (void *), 1567 alloc * 2 * sizeof (void *))) == NULL) 1568 goto out; 1569 datasets = ptr; 1570 1571 alloc *= 2; 1572 } 1573 } 1574 1575 if ((mountpoints[used] = zfs_strdup(hdl, 1576 entry.mnt_mountp)) == NULL) 1577 goto out; 1578 1579 /* 1580 * This is allowed to fail, in case there is some I/O error. It 1581 * is only used to determine if we need to remove the underlying 1582 * mountpoint, so failure is not fatal. 1583 */ 1584 datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 1585 1586 used++; 1587 } 1588 1589 /* 1590 * At this point, we have the entire list of filesystems, so sort it by 1591 * mountpoint. 1592 */ 1593 qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 1594 1595 /* 1596 * Walk through and first unshare everything. 1597 */ 1598 for (i = 0; i < used; i++) { 1599 zfs_share_proto_t *curr_proto; 1600 for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 1601 curr_proto++) { 1602 if (is_shared(mountpoints[i], *curr_proto) && 1603 unshare_one(hdl, mountpoints[i], 1604 mountpoints[i], *curr_proto) != 0) 1605 goto out; 1606 } 1607 } 1608 zfs_commit_all_shares(); 1609 1610 /* 1611 * Now unmount everything, removing the underlying directories as 1612 * appropriate. 1613 */ 1614 for (i = 0; i < used; i++) { 1615 if (unmount_one(hdl, mountpoints[i], flags) != 0) 1616 goto out; 1617 } 1618 1619 for (i = 0; i < used; i++) { 1620 if (datasets[i]) 1621 remove_mountpoint(datasets[i]); 1622 } 1623 1624 ret = 0; 1625 out: 1626 for (i = 0; i < used; i++) { 1627 if (datasets[i]) 1628 zfs_close(datasets[i]); 1629 free(mountpoints[i]); 1630 } 1631 free(datasets); 1632 free(mountpoints); 1633 1634 return (ret); 1635 } 1636