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 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * Routines to manage ZFS mounts. We separate all the nasty routines that have 31 * to deal with the OS. The following functions are the main entry points -- 32 * they are used by mount and unmount and when changing a filesystem's 33 * mountpoint. 34 * 35 * zfs_is_mounted() 36 * zfs_mount() 37 * zfs_unmount() 38 * zfs_unmountall() 39 * 40 * This file also contains the functions used to manage sharing filesystems via 41 * NFS and iSCSI: 42 * 43 * zfs_is_shared() 44 * zfs_share() 45 * zfs_unshare() 46 * 47 * zfs_is_shared_nfs() 48 * zfs_share_nfs() 49 * zfs_unshare_nfs() 50 * zfs_unshareall_nfs() 51 * zfs_is_shared_iscsi() 52 * zfs_share_iscsi() 53 * zfs_unshare_iscsi() 54 * 55 * The following functions are available for pool consumers, and will 56 * mount/unmount and share/unshare all datasets within pool: 57 * 58 * zpool_enable_datasets() 59 * zpool_disable_datasets() 60 */ 61 62 #include <dirent.h> 63 #include <errno.h> 64 #include <libgen.h> 65 #include <libintl.h> 66 #include <libiscsitgt.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <strings.h> 70 #include <unistd.h> 71 #include <zone.h> 72 #include <sys/mntent.h> 73 #include <sys/mnttab.h> 74 #include <sys/mount.h> 75 #include <sys/stat.h> 76 77 #include <libzfs.h> 78 79 #include "libzfs_impl.h" 80 81 /* 82 * Search the sharetab for the given mountpoint, returning true if it is found. 83 */ 84 static boolean_t 85 is_shared(libzfs_handle_t *hdl, const char *mountpoint) 86 { 87 char buf[MAXPATHLEN], *tab; 88 89 if (hdl->libzfs_sharetab == NULL) 90 return (0); 91 92 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET); 93 94 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) { 95 96 /* the mountpoint is the first entry on each line */ 97 if ((tab = strchr(buf, '\t')) != NULL) { 98 *tab = '\0'; 99 if (strcmp(buf, mountpoint) == 0) 100 return (B_TRUE); 101 } 102 } 103 104 return (B_FALSE); 105 } 106 107 /* 108 * Returns true if the specified directory is empty. If we can't open the 109 * directory at all, return true so that the mount can fail with a more 110 * informative error message. 111 */ 112 static boolean_t 113 dir_is_empty(const char *dirname) 114 { 115 DIR *dirp; 116 struct dirent64 *dp; 117 118 if ((dirp = opendir(dirname)) == NULL) 119 return (B_TRUE); 120 121 while ((dp = readdir64(dirp)) != NULL) { 122 123 if (strcmp(dp->d_name, ".") == 0 || 124 strcmp(dp->d_name, "..") == 0) 125 continue; 126 127 (void) closedir(dirp); 128 return (B_FALSE); 129 } 130 131 (void) closedir(dirp); 132 return (B_TRUE); 133 } 134 135 /* 136 * Checks to see if the mount is active. If the filesystem is mounted, we fill 137 * in 'where' with the current mountpoint, and return 1. Otherwise, we return 138 * 0. 139 */ 140 boolean_t 141 zfs_is_mounted(zfs_handle_t *zhp, char **where) 142 { 143 struct mnttab search = { 0 }, entry; 144 145 /* 146 * Search for the entry in /etc/mnttab. We don't bother getting the 147 * mountpoint, as we can just search for the special device. This will 148 * also let us find mounts when the mountpoint is 'legacy'. 149 */ 150 search.mnt_special = (char *)zfs_get_name(zhp); 151 search.mnt_fstype = MNTTYPE_ZFS; 152 153 rewind(zhp->zfs_hdl->libzfs_mnttab); 154 if (getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) != 0) 155 return (B_FALSE); 156 157 if (where != NULL) 158 *where = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 159 160 return (B_TRUE); 161 } 162 163 /* 164 * Returns true if the given dataset is mountable, false otherwise. Returns the 165 * mountpoint in 'buf'. 166 */ 167 static boolean_t 168 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 169 zfs_source_t *source) 170 { 171 char sourceloc[ZFS_MAXNAMELEN]; 172 zfs_source_t sourcetype; 173 174 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type)) 175 return (B_FALSE); 176 177 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 178 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 179 180 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 181 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 182 return (B_FALSE); 183 184 if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT)) 185 return (B_FALSE); 186 187 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 188 getzoneid() == GLOBAL_ZONEID) 189 return (B_FALSE); 190 191 if (source) 192 *source = sourcetype; 193 194 return (B_TRUE); 195 } 196 197 /* 198 * Mount the given filesystem. 199 */ 200 int 201 zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 202 { 203 struct stat buf; 204 char mountpoint[ZFS_MAXPROPLEN]; 205 char mntopts[MNT_LINE_MAX]; 206 libzfs_handle_t *hdl = zhp->zfs_hdl; 207 208 if (options == NULL) 209 mntopts[0] = '\0'; 210 else 211 (void) strlcpy(mntopts, options, sizeof (mntopts)); 212 213 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 214 return (0); 215 216 /* Create the directory if it doesn't already exist */ 217 if (lstat(mountpoint, &buf) != 0) { 218 if (mkdirp(mountpoint, 0755) != 0) { 219 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 220 "failed to create mountpoint")); 221 return (zfs_error(hdl, EZFS_MOUNTFAILED, 222 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 223 mountpoint)); 224 } 225 } 226 227 /* 228 * Determine if the mountpoint is empty. If so, refuse to perform the 229 * mount. We don't perform this check if MS_OVERLAY is specified, which 230 * would defeat the point. We also avoid this check if 'remount' is 231 * specified. 232 */ 233 if ((flags & MS_OVERLAY) == 0 && 234 strstr(mntopts, MNTOPT_REMOUNT) == NULL && 235 !dir_is_empty(mountpoint)) { 236 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 237 "directory is not empty")); 238 return (zfs_error(hdl, EZFS_MOUNTFAILED, 239 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 240 } 241 242 /* perform the mount */ 243 if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags, 244 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 245 /* 246 * Generic errors are nasty, but there are just way too many 247 * from mount(), and they're well-understood. We pick a few 248 * common ones to improve upon. 249 */ 250 if (errno == EBUSY) 251 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 252 "mountpoint or dataset is busy")); 253 else 254 zfs_error_aux(hdl, strerror(errno)); 255 256 return (zfs_error(hdl, EZFS_MOUNTFAILED, 257 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 258 zhp->zfs_name)); 259 } 260 261 return (0); 262 } 263 264 /* 265 * Unmount a single filesystem. 266 */ 267 static int 268 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 269 { 270 if (umount2(mountpoint, flags) != 0) { 271 zfs_error_aux(hdl, strerror(errno)); 272 return (zfs_error(hdl, EZFS_UMOUNTFAILED, 273 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 274 mountpoint)); 275 } 276 277 return (0); 278 } 279 280 /* 281 * Unmount the given filesystem. 282 */ 283 int 284 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 285 { 286 struct mnttab search = { 0 }, entry; 287 288 /* check to see if need to unmount the filesystem */ 289 search.mnt_special = zhp->zfs_name; 290 search.mnt_fstype = MNTTYPE_ZFS; 291 rewind(zhp->zfs_hdl->libzfs_mnttab); 292 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 293 getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) { 294 295 if (mountpoint == NULL) 296 mountpoint = entry.mnt_mountp; 297 298 /* 299 * Unshare and unmount the filesystem 300 */ 301 if (zfs_unshare_nfs(zhp, mountpoint) != 0 || 302 unmount_one(zhp->zfs_hdl, mountpoint, flags) != 0) 303 return (-1); 304 } 305 306 return (0); 307 } 308 309 /* 310 * Unmount this filesystem and any children inheriting the mountpoint property. 311 * To do this, just act like we're changing the mountpoint property, but don't 312 * remount the filesystems afterwards. 313 */ 314 int 315 zfs_unmountall(zfs_handle_t *zhp, int flags) 316 { 317 prop_changelist_t *clp; 318 int ret; 319 320 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags); 321 if (clp == NULL) 322 return (-1); 323 324 ret = changelist_prefix(clp); 325 changelist_free(clp); 326 327 return (ret); 328 } 329 330 boolean_t 331 zfs_is_shared(zfs_handle_t *zhp) 332 { 333 if (ZFS_IS_VOLUME(zhp)) 334 return (zfs_is_shared_iscsi(zhp)); 335 336 return (zfs_is_shared_nfs(zhp, NULL)); 337 } 338 339 int 340 zfs_share(zfs_handle_t *zhp) 341 { 342 if (ZFS_IS_VOLUME(zhp)) 343 return (zfs_share_iscsi(zhp)); 344 345 return (zfs_share_nfs(zhp)); 346 } 347 348 int 349 zfs_unshare(zfs_handle_t *zhp) 350 { 351 if (ZFS_IS_VOLUME(zhp)) 352 return (zfs_unshare_iscsi(zhp)); 353 354 return (zfs_unshare_nfs(zhp, NULL)); 355 } 356 357 /* 358 * Check to see if the filesystem is currently shared. 359 */ 360 boolean_t 361 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 362 { 363 char *mountpoint; 364 365 if (!zfs_is_mounted(zhp, &mountpoint)) 366 return (B_FALSE); 367 368 if (is_shared(zhp->zfs_hdl, mountpoint)) { 369 if (where != NULL) 370 *where = mountpoint; 371 else 372 free(mountpoint); 373 return (B_TRUE); 374 } else { 375 free(mountpoint); 376 return (B_FALSE); 377 } 378 } 379 380 /* 381 * Share the given filesystem according to the options in 'sharenfs'. We rely 382 * on share(1M) to the dirty work for us. 383 */ 384 int 385 zfs_share_nfs(zfs_handle_t *zhp) 386 { 387 char mountpoint[ZFS_MAXPROPLEN]; 388 char shareopts[ZFS_MAXPROPLEN]; 389 char buf[MAXPATHLEN]; 390 FILE *fp; 391 libzfs_handle_t *hdl = zhp->zfs_hdl; 392 393 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 394 return (0); 395 396 /* 397 * Return success if there are no share options. 398 */ 399 if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts), 400 NULL, NULL, 0, B_FALSE) != 0 || 401 strcmp(shareopts, "off") == 0) 402 return (0); 403 404 /* 405 * If the 'zoned' property is set, then zfs_is_mountable() will have 406 * already bailed out if we are in the global zone. But local 407 * zones cannot be NFS servers, so we ignore it for local zones as well. 408 */ 409 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 410 return (0); 411 412 /* 413 * Invoke the share(1M) command. We always do this, even if it's 414 * currently shared, as the options may have changed. 415 */ 416 if (strcmp(shareopts, "on") == 0) 417 (void) snprintf(buf, sizeof (buf), "/usr/sbin/share " 418 "-F nfs \"%s\" 2>&1", mountpoint); 419 else 420 (void) snprintf(buf, sizeof (buf), "/usr/sbin/share " 421 "-F nfs -o \"%s\" \"%s\" 2>&1", shareopts, 422 mountpoint); 423 424 if ((fp = popen(buf, "r")) == NULL) 425 return (zfs_error(hdl, EZFS_SHARENFSFAILED, 426 dgettext(TEXT_DOMAIN, "cannot share '%s'"), 427 zfs_get_name(zhp))); 428 429 /* 430 * share(1M) should only produce output if there is some kind 431 * of error. All output begins with "share_nfs: ", so we trim 432 * this off to get to the real error. 433 */ 434 if (fgets(buf, sizeof (buf), fp) != NULL) { 435 char *colon = strchr(buf, ':'); 436 437 while (buf[strlen(buf) - 1] == '\n') 438 buf[strlen(buf) - 1] = '\0'; 439 440 if (colon != NULL) 441 zfs_error_aux(hdl, colon + 2); 442 443 (void) zfs_error(hdl, EZFS_SHARENFSFAILED, 444 dgettext(TEXT_DOMAIN, "cannot share '%s'"), 445 zfs_get_name(zhp)); 446 447 verify(pclose(fp) != 0); 448 return (-1); 449 } 450 451 verify(pclose(fp) == 0); 452 453 return (0); 454 } 455 456 /* 457 * Unshare a filesystem by mountpoint. 458 */ 459 static int 460 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint) 461 { 462 char buf[MAXPATHLEN]; 463 FILE *fp; 464 465 (void) snprintf(buf, sizeof (buf), 466 "/usr/sbin/unshare \"%s\" 2>&1", 467 mountpoint); 468 469 if ((fp = popen(buf, "r")) == NULL) 470 return (zfs_error(hdl, EZFS_UNSHARENFSFAILED, 471 dgettext(TEXT_DOMAIN, 472 "cannot unshare '%s'"), name)); 473 474 /* 475 * unshare(1M) should only produce output if there is 476 * some kind of error. All output begins with "unshare 477 * nfs: ", so we trim this off to get to the real error. 478 */ 479 if (fgets(buf, sizeof (buf), fp) != NULL) { 480 char *colon = strchr(buf, ':'); 481 482 while (buf[strlen(buf) - 1] == '\n') 483 buf[strlen(buf) - 1] = '\0'; 484 485 if (colon != NULL) 486 zfs_error_aux(hdl, colon + 2); 487 488 verify(pclose(fp) != 0); 489 490 return (zfs_error(hdl, EZFS_UNSHARENFSFAILED, 491 dgettext(TEXT_DOMAIN, 492 "cannot unshare '%s'"), name)); 493 } 494 495 verify(pclose(fp) == 0); 496 497 return (0); 498 } 499 500 /* 501 * Unshare the given filesystem. 502 */ 503 int 504 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 505 { 506 struct mnttab search = { 0 }, entry; 507 508 /* check to see if need to unmount the filesystem */ 509 search.mnt_special = (char *)zfs_get_name(zhp); 510 search.mnt_fstype = MNTTYPE_ZFS; 511 rewind(zhp->zfs_hdl->libzfs_mnttab); 512 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 513 getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) { 514 515 if (mountpoint == NULL) 516 mountpoint = entry.mnt_mountp; 517 518 if (is_shared(zhp->zfs_hdl, mountpoint) && 519 unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0) 520 return (-1); 521 } 522 523 return (0); 524 } 525 526 /* 527 * Same as zfs_unmountall(), but for NFS unshares. 528 */ 529 int 530 zfs_unshareall_nfs(zfs_handle_t *zhp) 531 { 532 prop_changelist_t *clp; 533 int ret; 534 535 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0); 536 if (clp == NULL) 537 return (-1); 538 539 ret = changelist_unshare(clp); 540 changelist_free(clp); 541 542 return (ret); 543 } 544 545 /* 546 * Remove the mountpoint associated with the current dataset, if necessary. 547 * We only remove the underlying directory if: 548 * 549 * - The mountpoint is not 'none' or 'legacy' 550 * - The mountpoint is non-empty 551 * - The mountpoint is the default or inherited 552 * - The 'zoned' property is set, or we're in a local zone 553 * 554 * Any other directories we leave alone. 555 */ 556 void 557 remove_mountpoint(zfs_handle_t *zhp) 558 { 559 char mountpoint[ZFS_MAXPROPLEN]; 560 zfs_source_t source; 561 562 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 563 &source)) 564 return; 565 566 if (source == ZFS_SRC_DEFAULT || 567 source == ZFS_SRC_INHERITED) { 568 /* 569 * Try to remove the directory, silently ignoring any errors. 570 * The filesystem may have since been removed or moved around, 571 * and this error isn't really useful to the administrator in 572 * any way. 573 */ 574 (void) rmdir(mountpoint); 575 } 576 } 577 578 boolean_t 579 zfs_is_shared_iscsi(zfs_handle_t *zhp) 580 { 581 return (iscsitgt_zfs_is_shared(zhp->zfs_name) != 0); 582 } 583 584 int 585 zfs_share_iscsi(zfs_handle_t *zhp) 586 { 587 char shareopts[ZFS_MAXPROPLEN]; 588 const char *dataset = zhp->zfs_name; 589 libzfs_handle_t *hdl = zhp->zfs_hdl; 590 591 /* 592 * Return success if there are no share options. 593 */ 594 if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts, 595 sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 || 596 strcmp(shareopts, "off") == 0) 597 return (0); 598 599 if (iscsitgt_zfs_share(dataset) != 0) 600 return (zfs_error(hdl, EZFS_SHAREISCSIFAILED, 601 dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset)); 602 603 return (0); 604 } 605 606 int 607 zfs_unshare_iscsi(zfs_handle_t *zhp) 608 { 609 const char *dataset = zfs_get_name(zhp); 610 libzfs_handle_t *hdl = zhp->zfs_hdl; 611 612 /* 613 * If this fails with ENODEV it indicates that zvol wasn't shared so 614 * we should return success in that case. 615 */ 616 if (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV) 617 return (zfs_error(hdl, EZFS_UNSHAREISCSIFAILED, 618 dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset)); 619 620 return (0); 621 } 622 623 typedef struct mount_cbdata { 624 zfs_handle_t **cb_datasets; 625 int cb_used; 626 int cb_alloc; 627 } mount_cbdata_t; 628 629 static int 630 mount_cb(zfs_handle_t *zhp, void *data) 631 { 632 mount_cbdata_t *cbp = data; 633 634 if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) { 635 zfs_close(zhp); 636 return (0); 637 } 638 639 if (cbp->cb_alloc == cbp->cb_used) { 640 void *ptr; 641 642 if ((ptr = zfs_realloc(zhp->zfs_hdl, 643 cbp->cb_datasets, cbp->cb_alloc * sizeof (void *), 644 cbp->cb_alloc * 2 * sizeof (void *))) == NULL) 645 return (-1); 646 cbp->cb_datasets = ptr; 647 648 cbp->cb_alloc *= 2; 649 } 650 651 cbp->cb_datasets[cbp->cb_used++] = zhp; 652 653 return (zfs_iter_children(zhp, mount_cb, cbp)); 654 } 655 656 static int 657 dataset_cmp(const void *a, const void *b) 658 { 659 zfs_handle_t **za = (zfs_handle_t **)a; 660 zfs_handle_t **zb = (zfs_handle_t **)b; 661 char mounta[MAXPATHLEN]; 662 char mountb[MAXPATHLEN]; 663 boolean_t gota, gotb; 664 665 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 666 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 667 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 668 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 669 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 670 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 671 672 if (gota && gotb) 673 return (strcmp(mounta, mountb)); 674 675 if (gota) 676 return (-1); 677 if (gotb) 678 return (1); 679 680 return (strcmp(zfs_get_name(a), zfs_get_name(b))); 681 } 682 683 /* 684 * Mount and share all datasets within the given pool. This assumes that no 685 * datasets within the pool are currently mounted. Because users can create 686 * complicated nested hierarchies of mountpoints, we first gather all the 687 * datasets and mountpoints within the pool, and sort them by mountpoint. Once 688 * we have the list of all filesystems, we iterate over them in order and mount 689 * and/or share each one. 690 */ 691 #pragma weak zpool_mount_datasets = zpool_enable_datasets 692 int 693 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 694 { 695 mount_cbdata_t cb = { 0 }; 696 libzfs_handle_t *hdl = zhp->zpool_hdl; 697 zfs_handle_t *zfsp; 698 int i, ret = -1; 699 700 /* 701 * Gather all datasets within the pool. 702 */ 703 if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL) 704 return (-1); 705 cb.cb_alloc = 4; 706 707 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_ANY)) == NULL) 708 goto out; 709 710 cb.cb_datasets[0] = zfsp; 711 cb.cb_used = 1; 712 713 if (zfs_iter_children(zfsp, mount_cb, &cb) != 0) 714 goto out; 715 716 /* 717 * Sort the datasets by mountpoint. 718 */ 719 qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp); 720 721 /* 722 * And mount all the datasets. 723 */ 724 ret = 0; 725 for (i = 0; i < cb.cb_used; i++) { 726 if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0 || 727 zfs_share(cb.cb_datasets[i]) != 0) 728 ret = -1; 729 } 730 731 out: 732 for (i = 0; i < cb.cb_used; i++) 733 zfs_close(cb.cb_datasets[i]); 734 free(cb.cb_datasets); 735 736 return (ret); 737 } 738 739 740 static int 741 zvol_cb(const char *dataset, void *data) 742 { 743 libzfs_handle_t *hdl = data; 744 zfs_handle_t *zhp; 745 746 /* 747 * Ignore snapshots and ignore failures from non-existant datasets. 748 */ 749 if (strchr(dataset, '@') != NULL || 750 (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL) 751 return (0); 752 753 (void) zfs_unshare_iscsi(zhp); 754 755 zfs_close(zhp); 756 757 return (0); 758 } 759 760 static int 761 mountpoint_compare(const void *a, const void *b) 762 { 763 const char *mounta = *((char **)a); 764 const char *mountb = *((char **)b); 765 766 return (strcmp(mountb, mounta)); 767 } 768 769 /* 770 * Unshare and unmount all datasets within the given pool. We don't want to 771 * rely on traversing the DSL to discover the filesystems within the pool, 772 * because this may be expensive (if not all of them are mounted), and can fail 773 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and 774 * gather all the filesystems that are currently mounted. 775 */ 776 #pragma weak zpool_unmount_datasets = zpool_disable_datasets 777 int 778 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 779 { 780 int used, alloc; 781 struct mnttab entry; 782 size_t namelen; 783 char **mountpoints = NULL; 784 zfs_handle_t **datasets = NULL; 785 libzfs_handle_t *hdl = zhp->zpool_hdl; 786 int i; 787 int ret = -1; 788 int flags = (force ? MS_FORCE : 0); 789 790 /* 791 * First unshare all zvols. 792 */ 793 if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0) 794 return (-1); 795 796 namelen = strlen(zhp->zpool_name); 797 798 rewind(hdl->libzfs_mnttab); 799 used = alloc = 0; 800 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 801 /* 802 * Ignore non-ZFS entries. 803 */ 804 if (entry.mnt_fstype == NULL || 805 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 806 continue; 807 808 /* 809 * Ignore filesystems not within this pool. 810 */ 811 if (entry.mnt_mountp == NULL || 812 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 813 (entry.mnt_special[namelen] != '/' && 814 entry.mnt_special[namelen] != '\0')) 815 continue; 816 817 /* 818 * At this point we've found a filesystem within our pool. Add 819 * it to our growing list. 820 */ 821 if (used == alloc) { 822 if (alloc == 0) { 823 if ((mountpoints = zfs_alloc(hdl, 824 8 * sizeof (void *))) == NULL) 825 goto out; 826 827 if ((datasets = zfs_alloc(hdl, 828 8 * sizeof (void *))) == NULL) 829 goto out; 830 831 alloc = 8; 832 } else { 833 void *ptr; 834 835 if ((ptr = zfs_realloc(hdl, mountpoints, 836 alloc * sizeof (void *), 837 alloc * 2 * sizeof (void *))) == NULL) 838 goto out; 839 mountpoints = ptr; 840 841 if ((ptr = zfs_realloc(hdl, datasets, 842 alloc * sizeof (void *), 843 alloc * 2 * sizeof (void *))) == NULL) 844 goto out; 845 datasets = ptr; 846 847 alloc *= 2; 848 } 849 } 850 851 if ((mountpoints[used] = zfs_strdup(hdl, 852 entry.mnt_mountp)) == NULL) 853 goto out; 854 855 /* 856 * This is allowed to fail, in case there is some I/O error. It 857 * is only used to determine if we need to remove the underlying 858 * mountpoint, so failure is not fatal. 859 */ 860 datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 861 862 used++; 863 } 864 865 /* 866 * At this point, we have the entire list of filesystems, so sort it by 867 * mountpoint. 868 */ 869 qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 870 871 /* 872 * Walk through and first unshare everything. 873 */ 874 for (i = 0; i < used; i++) { 875 if (is_shared(hdl, mountpoints[i]) && 876 unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0) 877 goto out; 878 } 879 880 /* 881 * Now unmount everything, removing the underlying directories as 882 * appropriate. 883 */ 884 for (i = 0; i < used; i++) { 885 if (unmount_one(hdl, mountpoints[i], flags) != 0) 886 goto out; 887 } 888 889 for (i = 0; i < used; i++) { 890 if (datasets[i]) 891 remove_mountpoint(datasets[i]); 892 } 893 894 ret = 0; 895 out: 896 for (i = 0; i < used; i++) { 897 if (datasets[i]) 898 zfs_close(datasets[i]); 899 free(mountpoints[i]); 900 } 901 free(datasets); 902 free(mountpoints); 903 904 return (ret); 905 } 906