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