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 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Routines to manage ZFS mounts. We separate all the nasty routines that have 29 * to deal with the OS. The following functions are the main entry points -- 30 * they are used by mount and unmount and when changing a filesystem's 31 * mountpoint. 32 * 33 * zfs_is_mounted() 34 * zfs_mount() 35 * zfs_unmount() 36 * zfs_unmountall() 37 * 38 * This file also contains the functions used to manage sharing filesystems via 39 * NFS and iSCSI: 40 * 41 * zfs_is_shared() 42 * zfs_share() 43 * zfs_unshare() 44 * 45 * zfs_is_shared_nfs() 46 * zfs_is_shared_smb() 47 * zfs_is_shared_iscsi() 48 * zfs_share_proto() 49 * zfs_shareall(); 50 * zfs_share_iscsi() 51 * zfs_unshare_nfs() 52 * zfs_unshare_smb() 53 * zfs_unshareall_nfs() 54 * zfs_unshareall_smb() 55 * zfs_unshareall() 56 * zfs_unshareall_bypath() 57 * zfs_unshare_iscsi() 58 * 59 * The following functions are available for pool consumers, and will 60 * mount/unmount and share/unshare all datasets within pool: 61 * 62 * zpool_enable_datasets() 63 * zpool_disable_datasets() 64 */ 65 66 #include <dirent.h> 67 #include <dlfcn.h> 68 #include <errno.h> 69 #include <libgen.h> 70 #include <libintl.h> 71 #include <stdio.h> 72 #include <stdlib.h> 73 #include <strings.h> 74 #include <unistd.h> 75 #include <zone.h> 76 #include <sys/mntent.h> 77 #include <sys/mount.h> 78 #include <sys/stat.h> 79 80 #include <libzfs.h> 81 82 #include "libzfs_impl.h" 83 84 #include <libshare.h> 85 #include <sys/systeminfo.h> 86 #define MAXISALEN 257 /* based on sysinfo(2) man page */ 87 88 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *); 89 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **, 90 zfs_share_proto_t); 91 92 static int (*iscsitgt_zfs_share)(const char *); 93 static int (*iscsitgt_zfs_unshare)(const char *); 94 static int (*iscsitgt_zfs_is_shared)(const char *); 95 static int (*iscsitgt_svc_online)(); 96 97 /* 98 * The share protocols table must be in the same order as the zfs_share_prot_t 99 * enum in libzfs_impl.h 100 */ 101 typedef struct { 102 zfs_prop_t p_prop; 103 char *p_name; 104 int p_share_err; 105 int p_unshare_err; 106 } proto_table_t; 107 108 proto_table_t proto_table[PROTO_END] = { 109 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED}, 110 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED}, 111 }; 112 113 zfs_share_proto_t nfs_only[] = { 114 PROTO_NFS, 115 PROTO_END 116 }; 117 118 zfs_share_proto_t smb_only[] = { 119 PROTO_SMB, 120 PROTO_END 121 }; 122 zfs_share_proto_t share_all_proto[] = { 123 PROTO_NFS, 124 PROTO_SMB, 125 PROTO_END 126 }; 127 128 #pragma init(zfs_iscsi_init) 129 static void 130 zfs_iscsi_init(void) 131 { 132 void *libiscsitgt; 133 134 if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1", 135 RTLD_LAZY | RTLD_GLOBAL)) == NULL || 136 (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt, 137 "iscsitgt_zfs_share")) == NULL || 138 (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt, 139 "iscsitgt_zfs_unshare")) == NULL || 140 (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt, 141 "iscsitgt_zfs_is_shared")) == NULL || 142 (iscsitgt_svc_online = (int (*)(const char *))dlsym(libiscsitgt, 143 "iscsitgt_svc_online")) == NULL) { 144 iscsitgt_zfs_share = NULL; 145 iscsitgt_zfs_unshare = NULL; 146 iscsitgt_zfs_is_shared = NULL; 147 iscsitgt_svc_online = NULL; 148 } 149 } 150 151 /* 152 * Search the sharetab for the given mountpoint and protocol, returning 153 * a zfs_share_type_t value. 154 */ 155 static zfs_share_type_t 156 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto) 157 { 158 char buf[MAXPATHLEN], *tab; 159 char *ptr; 160 161 if (hdl->libzfs_sharetab == NULL) 162 return (SHARED_NOT_SHARED); 163 164 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET); 165 166 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) { 167 168 /* the mountpoint is the first entry on each line */ 169 if ((tab = strchr(buf, '\t')) == NULL) 170 continue; 171 172 *tab = '\0'; 173 if (strcmp(buf, mountpoint) == 0) { 174 /* 175 * the protocol field is the third field 176 * skip over second field 177 */ 178 ptr = ++tab; 179 if ((tab = strchr(ptr, '\t')) == NULL) 180 continue; 181 ptr = ++tab; 182 if ((tab = strchr(ptr, '\t')) == NULL) 183 continue; 184 *tab = '\0'; 185 if (strcmp(ptr, 186 proto_table[proto].p_name) == 0) { 187 switch (proto) { 188 case PROTO_NFS: 189 return (SHARED_NFS); 190 case PROTO_SMB: 191 return (SHARED_SMB); 192 default: 193 return (0); 194 } 195 } 196 } 197 } 198 199 return (SHARED_NOT_SHARED); 200 } 201 202 /* 203 * Returns true if the specified directory is empty. If we can't open the 204 * directory at all, return true so that the mount can fail with a more 205 * informative error message. 206 */ 207 static boolean_t 208 dir_is_empty(const char *dirname) 209 { 210 DIR *dirp; 211 struct dirent64 *dp; 212 213 if ((dirp = opendir(dirname)) == NULL) 214 return (B_TRUE); 215 216 while ((dp = readdir64(dirp)) != NULL) { 217 218 if (strcmp(dp->d_name, ".") == 0 || 219 strcmp(dp->d_name, "..") == 0) 220 continue; 221 222 (void) closedir(dirp); 223 return (B_FALSE); 224 } 225 226 (void) closedir(dirp); 227 return (B_TRUE); 228 } 229 230 /* 231 * Checks to see if the mount is active. If the filesystem is mounted, we fill 232 * in 'where' with the current mountpoint, and return 1. Otherwise, we return 233 * 0. 234 */ 235 boolean_t 236 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where) 237 { 238 struct mnttab entry; 239 240 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0) 241 return (B_FALSE); 242 243 if (where != NULL) 244 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp); 245 246 return (B_TRUE); 247 } 248 249 boolean_t 250 zfs_is_mounted(zfs_handle_t *zhp, char **where) 251 { 252 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where)); 253 } 254 255 /* 256 * Returns true if the given dataset is mountable, false otherwise. Returns the 257 * mountpoint in 'buf'. 258 */ 259 static boolean_t 260 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 261 zprop_source_t *source) 262 { 263 char sourceloc[ZFS_MAXNAMELEN]; 264 zprop_source_t sourcetype; 265 266 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type)) 267 return (B_FALSE); 268 269 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 270 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 271 272 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 273 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 274 return (B_FALSE); 275 276 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF) 277 return (B_FALSE); 278 279 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 280 getzoneid() == GLOBAL_ZONEID) 281 return (B_FALSE); 282 283 if (source) 284 *source = sourcetype; 285 286 return (B_TRUE); 287 } 288 289 /* 290 * Mount the given filesystem. 291 */ 292 int 293 zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 294 { 295 struct stat buf; 296 char mountpoint[ZFS_MAXPROPLEN]; 297 char mntopts[MNT_LINE_MAX]; 298 libzfs_handle_t *hdl = zhp->zfs_hdl; 299 300 if (options == NULL) 301 mntopts[0] = '\0'; 302 else 303 (void) strlcpy(mntopts, options, sizeof (mntopts)); 304 305 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 306 return (0); 307 308 /* Create the directory if it doesn't already exist */ 309 if (lstat(mountpoint, &buf) != 0) { 310 if (mkdirp(mountpoint, 0755) != 0) { 311 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 312 "failed to create mountpoint")); 313 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 314 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 315 mountpoint)); 316 } 317 } 318 319 /* 320 * Determine if the mountpoint is empty. If so, refuse to perform the 321 * mount. We don't perform this check if MS_OVERLAY is specified, which 322 * would defeat the point. We also avoid this check if 'remount' is 323 * specified. 324 */ 325 if ((flags & MS_OVERLAY) == 0 && 326 strstr(mntopts, MNTOPT_REMOUNT) == NULL && 327 !dir_is_empty(mountpoint)) { 328 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 329 "directory is not empty")); 330 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 331 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 332 } 333 334 /* perform the mount */ 335 if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags, 336 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 337 /* 338 * Generic errors are nasty, but there are just way too many 339 * from mount(), and they're well-understood. We pick a few 340 * common ones to improve upon. 341 */ 342 if (errno == EBUSY) { 343 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 344 "mountpoint or dataset is busy")); 345 } else if (errno == EPERM) { 346 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 347 "Insufficient privileges")); 348 } else if (errno == ENOTSUP) { 349 char buf[256]; 350 351 (void) snprintf(buf, sizeof (buf), 352 dgettext(TEXT_DOMAIN, "Mismatched versions: File " 353 "system is version %llu on-disk format, which is " 354 "incompatible with this software version %lld!"), 355 (u_longlong_t)zfs_prop_get_int(zhp, 356 ZFS_PROP_VERSION), ZPL_VERSION); 357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf)); 358 } else { 359 zfs_error_aux(hdl, strerror(errno)); 360 } 361 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 362 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 363 zhp->zfs_name)); 364 } 365 366 /* add the mounted entry into our cache */ 367 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, 368 mntopts); 369 return (0); 370 } 371 372 /* 373 * Unmount a single filesystem. 374 */ 375 static int 376 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 377 { 378 if (umount2(mountpoint, flags) != 0) { 379 zfs_error_aux(hdl, strerror(errno)); 380 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED, 381 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 382 mountpoint)); 383 } 384 385 return (0); 386 } 387 388 /* 389 * Unmount the given filesystem. 390 */ 391 int 392 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 393 { 394 libzfs_handle_t *hdl = zhp->zfs_hdl; 395 struct mnttab entry; 396 char *mntpt = NULL; 397 398 /* check to see if we need to unmount the filesystem */ 399 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 400 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) { 401 /* 402 * mountpoint may have come from a call to 403 * getmnt/getmntany if it isn't NULL. If it is NULL, 404 * we know it comes from libzfs_mnttab_find which can 405 * then get freed later. We strdup it to play it safe. 406 */ 407 if (mountpoint == NULL) 408 mntpt = zfs_strdup(hdl, entry.mnt_mountp); 409 else 410 mntpt = zfs_strdup(hdl, mountpoint); 411 412 /* 413 * Unshare and unmount the filesystem 414 */ 415 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) 416 return (-1); 417 418 if (unmount_one(hdl, mntpt, flags) != 0) { 419 free(mntpt); 420 (void) zfs_shareall(zhp); 421 return (-1); 422 } 423 libzfs_mnttab_remove(hdl, zhp->zfs_name); 424 free(mntpt); 425 } 426 427 return (0); 428 } 429 430 /* 431 * Unmount this filesystem and any children inheriting the mountpoint property. 432 * To do this, just act like we're changing the mountpoint property, but don't 433 * remount the filesystems afterwards. 434 */ 435 int 436 zfs_unmountall(zfs_handle_t *zhp, int flags) 437 { 438 prop_changelist_t *clp; 439 int ret; 440 441 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags); 442 if (clp == NULL) 443 return (-1); 444 445 ret = changelist_prefix(clp); 446 changelist_free(clp); 447 448 return (ret); 449 } 450 451 boolean_t 452 zfs_is_shared(zfs_handle_t *zhp) 453 { 454 zfs_share_type_t rc = 0; 455 zfs_share_proto_t *curr_proto; 456 457 if (ZFS_IS_VOLUME(zhp)) 458 return (zfs_is_shared_iscsi(zhp)); 459 460 for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 461 curr_proto++) 462 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto); 463 464 return (rc ? B_TRUE : B_FALSE); 465 } 466 467 int 468 zfs_share(zfs_handle_t *zhp) 469 { 470 if (ZFS_IS_VOLUME(zhp)) 471 return (zfs_share_iscsi(zhp)); 472 473 return (zfs_share_proto(zhp, share_all_proto)); 474 } 475 476 int 477 zfs_unshare(zfs_handle_t *zhp) 478 { 479 if (ZFS_IS_VOLUME(zhp)) 480 return (zfs_unshare_iscsi(zhp)); 481 482 return (zfs_unshareall(zhp)); 483 } 484 485 /* 486 * Check to see if the filesystem is currently shared. 487 */ 488 zfs_share_type_t 489 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto) 490 { 491 char *mountpoint; 492 zfs_share_type_t rc; 493 494 if (!zfs_is_mounted(zhp, &mountpoint)) 495 return (SHARED_NOT_SHARED); 496 497 if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) { 498 if (where != NULL) 499 *where = mountpoint; 500 else 501 free(mountpoint); 502 return (rc); 503 } else { 504 free(mountpoint); 505 return (SHARED_NOT_SHARED); 506 } 507 } 508 509 boolean_t 510 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 511 { 512 return (zfs_is_shared_proto(zhp, where, 513 PROTO_NFS) != SHARED_NOT_SHARED); 514 } 515 516 boolean_t 517 zfs_is_shared_smb(zfs_handle_t *zhp, char **where) 518 { 519 return (zfs_is_shared_proto(zhp, where, 520 PROTO_SMB) != SHARED_NOT_SHARED); 521 } 522 523 /* 524 * Make sure things will work if libshare isn't installed by using 525 * wrapper functions that check to see that the pointers to functions 526 * initialized in _zfs_init_libshare() are actually present. 527 */ 528 529 static sa_handle_t (*_sa_init)(int); 530 static void (*_sa_fini)(sa_handle_t); 531 static sa_share_t (*_sa_find_share)(sa_handle_t, char *); 532 static int (*_sa_enable_share)(sa_share_t, char *); 533 static int (*_sa_disable_share)(sa_share_t, char *); 534 static char *(*_sa_errorstr)(int); 535 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *); 536 static boolean_t (*_sa_needs_refresh)(sa_handle_t *); 537 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t); 538 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t, 539 char *, char *, zprop_source_t, char *, char *, char *); 540 static void (*_sa_update_sharetab_ts)(sa_handle_t); 541 542 /* 543 * _zfs_init_libshare() 544 * 545 * Find the libshare.so.1 entry points that we use here and save the 546 * values to be used later. This is triggered by the runtime loader. 547 * Make sure the correct ISA version is loaded. 548 */ 549 550 #pragma init(_zfs_init_libshare) 551 static void 552 _zfs_init_libshare(void) 553 { 554 void *libshare; 555 char path[MAXPATHLEN]; 556 char isa[MAXISALEN]; 557 558 #if defined(_LP64) 559 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1) 560 isa[0] = '\0'; 561 #else 562 isa[0] = '\0'; 563 #endif 564 (void) snprintf(path, MAXPATHLEN, 565 "/usr/lib/%s/libshare.so.1", isa); 566 567 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) { 568 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init"); 569 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini"); 570 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *)) 571 dlsym(libshare, "sa_find_share"); 572 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 573 "sa_enable_share"); 574 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 575 "sa_disable_share"); 576 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr"); 577 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *)) 578 dlsym(libshare, "sa_parse_legacy_options"); 579 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *)) 580 dlsym(libshare, "sa_needs_refresh"); 581 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t)) 582 dlsym(libshare, "sa_get_zfs_handle"); 583 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t, 584 sa_share_t, char *, char *, zprop_source_t, char *, 585 char *, char *))dlsym(libshare, "sa_zfs_process_share"); 586 _sa_update_sharetab_ts = (void (*)(sa_handle_t)) 587 dlsym(libshare, "sa_update_sharetab_ts"); 588 if (_sa_init == NULL || _sa_fini == NULL || 589 _sa_find_share == NULL || _sa_enable_share == NULL || 590 _sa_disable_share == NULL || _sa_errorstr == NULL || 591 _sa_parse_legacy_options == NULL || 592 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL || 593 _sa_zfs_process_share == NULL || 594 _sa_update_sharetab_ts == NULL) { 595 _sa_init = NULL; 596 _sa_fini = NULL; 597 _sa_disable_share = NULL; 598 _sa_enable_share = NULL; 599 _sa_errorstr = NULL; 600 _sa_parse_legacy_options = NULL; 601 (void) dlclose(libshare); 602 _sa_needs_refresh = NULL; 603 _sa_get_zfs_handle = NULL; 604 _sa_zfs_process_share = NULL; 605 _sa_update_sharetab_ts = NULL; 606 } 607 } 608 } 609 610 /* 611 * zfs_init_libshare(zhandle, service) 612 * 613 * Initialize the libshare API if it hasn't already been initialized. 614 * In all cases it returns 0 if it succeeded and an error if not. The 615 * service value is which part(s) of the API to initialize and is a 616 * direct map to the libshare sa_init(service) interface. 617 */ 618 int 619 zfs_init_libshare(libzfs_handle_t *zhandle, int service) 620 { 621 int ret = SA_OK; 622 623 if (_sa_init == NULL) 624 ret = SA_CONFIG_ERR; 625 626 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) { 627 /* 628 * We had a cache miss. Most likely it is a new ZFS 629 * dataset that was just created. We want to make sure 630 * so check timestamps to see if a different process 631 * has updated any of the configuration. If there was 632 * some non-ZFS change, we need to re-initialize the 633 * internal cache. 634 */ 635 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS; 636 if (_sa_needs_refresh != NULL && 637 _sa_needs_refresh(zhandle->libzfs_sharehdl)) { 638 zfs_uninit_libshare(zhandle); 639 zhandle->libzfs_sharehdl = _sa_init(service); 640 } 641 } 642 643 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL) 644 zhandle->libzfs_sharehdl = _sa_init(service); 645 646 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL) 647 ret = SA_NO_MEMORY; 648 649 return (ret); 650 } 651 652 /* 653 * zfs_uninit_libshare(zhandle) 654 * 655 * Uninitialize the libshare API if it hasn't already been 656 * uninitialized. It is OK to call multiple times. 657 */ 658 void 659 zfs_uninit_libshare(libzfs_handle_t *zhandle) 660 { 661 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) { 662 if (_sa_fini != NULL) 663 _sa_fini(zhandle->libzfs_sharehdl); 664 zhandle->libzfs_sharehdl = NULL; 665 } 666 } 667 668 /* 669 * zfs_parse_options(options, proto) 670 * 671 * Call the legacy parse interface to get the protocol specific 672 * options using the NULL arg to indicate that this is a "parse" only. 673 */ 674 int 675 zfs_parse_options(char *options, zfs_share_proto_t proto) 676 { 677 if (_sa_parse_legacy_options != NULL) { 678 return (_sa_parse_legacy_options(NULL, options, 679 proto_table[proto].p_name)); 680 } 681 return (SA_CONFIG_ERR); 682 } 683 684 /* 685 * zfs_sa_find_share(handle, path) 686 * 687 * wrapper around sa_find_share to find a share path in the 688 * configuration. 689 */ 690 static sa_share_t 691 zfs_sa_find_share(sa_handle_t handle, char *path) 692 { 693 if (_sa_find_share != NULL) 694 return (_sa_find_share(handle, path)); 695 return (NULL); 696 } 697 698 /* 699 * zfs_sa_enable_share(share, proto) 700 * 701 * Wrapper for sa_enable_share which enables a share for a specified 702 * protocol. 703 */ 704 static int 705 zfs_sa_enable_share(sa_share_t share, char *proto) 706 { 707 if (_sa_enable_share != NULL) 708 return (_sa_enable_share(share, proto)); 709 return (SA_CONFIG_ERR); 710 } 711 712 /* 713 * zfs_sa_disable_share(share, proto) 714 * 715 * Wrapper for sa_enable_share which disables a share for a specified 716 * protocol. 717 */ 718 static int 719 zfs_sa_disable_share(sa_share_t share, char *proto) 720 { 721 if (_sa_disable_share != NULL) 722 return (_sa_disable_share(share, proto)); 723 return (SA_CONFIG_ERR); 724 } 725 726 /* 727 * Share the given filesystem according to the options in the specified 728 * protocol specific properties (sharenfs, sharesmb). We rely 729 * on "libshare" to the dirty work for us. 730 */ 731 static int 732 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 733 { 734 char mountpoint[ZFS_MAXPROPLEN]; 735 char shareopts[ZFS_MAXPROPLEN]; 736 char sourcestr[ZFS_MAXPROPLEN]; 737 libzfs_handle_t *hdl = zhp->zfs_hdl; 738 sa_share_t share; 739 zfs_share_proto_t *curr_proto; 740 zprop_source_t sourcetype; 741 int ret; 742 743 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 744 return (0); 745 746 if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 747 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 748 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"), 749 zfs_get_name(zhp), _sa_errorstr != NULL ? 750 _sa_errorstr(ret) : ""); 751 return (-1); 752 } 753 754 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) { 755 /* 756 * Return success if there are no share options. 757 */ 758 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop, 759 shareopts, sizeof (shareopts), &sourcetype, sourcestr, 760 ZFS_MAXPROPLEN, B_FALSE) != 0 || 761 strcmp(shareopts, "off") == 0) 762 continue; 763 764 /* 765 * If the 'zoned' property is set, then zfs_is_mountable() 766 * will have already bailed out if we are in the global zone. 767 * But local zones cannot be NFS servers, so we ignore it for 768 * local zones as well. 769 */ 770 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 771 continue; 772 773 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint); 774 if (share == NULL) { 775 /* 776 * This may be a new file system that was just 777 * created so isn't in the internal cache 778 * (second time through). Rather than 779 * reloading the entire configuration, we can 780 * assume ZFS has done the checking and it is 781 * safe to add this to the internal 782 * configuration. 783 */ 784 if (_sa_zfs_process_share(hdl->libzfs_sharehdl, 785 NULL, NULL, mountpoint, 786 proto_table[*curr_proto].p_name, sourcetype, 787 shareopts, sourcestr, zhp->zfs_name) != SA_OK) { 788 (void) zfs_error_fmt(hdl, 789 proto_table[*curr_proto].p_share_err, 790 dgettext(TEXT_DOMAIN, "cannot share '%s'"), 791 zfs_get_name(zhp)); 792 return (-1); 793 } 794 hdl->libzfs_shareflags |= ZFSSHARE_MISS; 795 share = zfs_sa_find_share(hdl->libzfs_sharehdl, 796 mountpoint); 797 } 798 if (share != NULL) { 799 int err; 800 err = zfs_sa_enable_share(share, 801 proto_table[*curr_proto].p_name); 802 if (err != SA_OK) { 803 (void) zfs_error_fmt(hdl, 804 proto_table[*curr_proto].p_share_err, 805 dgettext(TEXT_DOMAIN, "cannot share '%s'"), 806 zfs_get_name(zhp)); 807 return (-1); 808 } 809 } else { 810 (void) zfs_error_fmt(hdl, 811 proto_table[*curr_proto].p_share_err, 812 dgettext(TEXT_DOMAIN, "cannot share '%s'"), 813 zfs_get_name(zhp)); 814 return (-1); 815 } 816 817 } 818 return (0); 819 } 820 821 822 int 823 zfs_share_nfs(zfs_handle_t *zhp) 824 { 825 return (zfs_share_proto(zhp, nfs_only)); 826 } 827 828 int 829 zfs_share_smb(zfs_handle_t *zhp) 830 { 831 return (zfs_share_proto(zhp, smb_only)); 832 } 833 834 int 835 zfs_shareall(zfs_handle_t *zhp) 836 { 837 return (zfs_share_proto(zhp, share_all_proto)); 838 } 839 840 /* 841 * Unshare a filesystem by mountpoint. 842 */ 843 static int 844 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint, 845 zfs_share_proto_t proto) 846 { 847 sa_share_t share; 848 int err; 849 char *mntpt; 850 /* 851 * Mountpoint could get trashed if libshare calls getmntany 852 * which it does during API initialization, so strdup the 853 * value. 854 */ 855 mntpt = zfs_strdup(hdl, mountpoint); 856 857 /* make sure libshare initialized */ 858 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 859 free(mntpt); /* don't need the copy anymore */ 860 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 861 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 862 name, _sa_errorstr(err))); 863 } 864 865 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt); 866 free(mntpt); /* don't need the copy anymore */ 867 868 if (share != NULL) { 869 err = zfs_sa_disable_share(share, proto_table[proto].p_name); 870 if (err != SA_OK) { 871 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 872 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 873 name, _sa_errorstr(err))); 874 } 875 } else { 876 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 877 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"), 878 name)); 879 } 880 return (0); 881 } 882 883 /* 884 * Unshare the given filesystem. 885 */ 886 int 887 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint, 888 zfs_share_proto_t *proto) 889 { 890 libzfs_handle_t *hdl = zhp->zfs_hdl; 891 struct mnttab entry; 892 char *mntpt = NULL; 893 894 /* check to see if need to unmount the filesystem */ 895 rewind(zhp->zfs_hdl->libzfs_mnttab); 896 if (mountpoint != NULL) 897 mountpoint = mntpt = zfs_strdup(hdl, mountpoint); 898 899 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 900 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) { 901 zfs_share_proto_t *curr_proto; 902 903 if (mountpoint == NULL) 904 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 905 906 for (curr_proto = proto; *curr_proto != PROTO_END; 907 curr_proto++) { 908 909 if (is_shared(hdl, mntpt, *curr_proto) && 910 unshare_one(hdl, zhp->zfs_name, 911 mntpt, *curr_proto) != 0) { 912 if (mntpt != NULL) 913 free(mntpt); 914 return (-1); 915 } 916 } 917 } 918 if (mntpt != NULL) 919 free(mntpt); 920 921 return (0); 922 } 923 924 int 925 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 926 { 927 return (zfs_unshare_proto(zhp, mountpoint, nfs_only)); 928 } 929 930 int 931 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint) 932 { 933 return (zfs_unshare_proto(zhp, mountpoint, smb_only)); 934 } 935 936 /* 937 * Same as zfs_unmountall(), but for NFS and SMB unshares. 938 */ 939 int 940 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto) 941 { 942 prop_changelist_t *clp; 943 int ret; 944 945 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0); 946 if (clp == NULL) 947 return (-1); 948 949 ret = changelist_unshare(clp, proto); 950 changelist_free(clp); 951 952 return (ret); 953 } 954 955 int 956 zfs_unshareall_nfs(zfs_handle_t *zhp) 957 { 958 return (zfs_unshareall_proto(zhp, nfs_only)); 959 } 960 961 int 962 zfs_unshareall_smb(zfs_handle_t *zhp) 963 { 964 return (zfs_unshareall_proto(zhp, smb_only)); 965 } 966 967 int 968 zfs_unshareall(zfs_handle_t *zhp) 969 { 970 return (zfs_unshareall_proto(zhp, share_all_proto)); 971 } 972 973 int 974 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint) 975 { 976 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto)); 977 } 978 979 /* 980 * Remove the mountpoint associated with the current dataset, if necessary. 981 * We only remove the underlying directory if: 982 * 983 * - The mountpoint is not 'none' or 'legacy' 984 * - The mountpoint is non-empty 985 * - The mountpoint is the default or inherited 986 * - The 'zoned' property is set, or we're in a local zone 987 * 988 * Any other directories we leave alone. 989 */ 990 void 991 remove_mountpoint(zfs_handle_t *zhp) 992 { 993 char mountpoint[ZFS_MAXPROPLEN]; 994 zprop_source_t source; 995 996 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 997 &source)) 998 return; 999 1000 if (source == ZPROP_SRC_DEFAULT || 1001 source == ZPROP_SRC_INHERITED) { 1002 /* 1003 * Try to remove the directory, silently ignoring any errors. 1004 * The filesystem may have since been removed or moved around, 1005 * and this error isn't really useful to the administrator in 1006 * any way. 1007 */ 1008 (void) rmdir(mountpoint); 1009 } 1010 } 1011 1012 boolean_t 1013 zfs_is_shared_iscsi(zfs_handle_t *zhp) 1014 { 1015 1016 /* 1017 * If iscsi deamon isn't running then we aren't shared 1018 */ 1019 if (iscsitgt_svc_online && iscsitgt_svc_online() == 1) 1020 return (B_FALSE); 1021 else 1022 return (iscsitgt_zfs_is_shared != NULL && 1023 iscsitgt_zfs_is_shared(zhp->zfs_name) != 0); 1024 } 1025 1026 int 1027 zfs_share_iscsi(zfs_handle_t *zhp) 1028 { 1029 char shareopts[ZFS_MAXPROPLEN]; 1030 const char *dataset = zhp->zfs_name; 1031 libzfs_handle_t *hdl = zhp->zfs_hdl; 1032 1033 /* 1034 * Return success if there are no share options. 1035 */ 1036 if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts, 1037 sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 || 1038 strcmp(shareopts, "off") == 0) 1039 return (0); 1040 1041 if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0) { 1042 int error = EZFS_SHAREISCSIFAILED; 1043 1044 /* 1045 * If service isn't availabele and EPERM was 1046 * returned then use special error. 1047 */ 1048 if (iscsitgt_svc_online && errno == EPERM && 1049 (iscsitgt_svc_online() != 0)) 1050 error = EZFS_ISCSISVCUNAVAIL; 1051 1052 return (zfs_error_fmt(hdl, error, 1053 dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset)); 1054 } 1055 1056 return (0); 1057 } 1058 1059 int 1060 zfs_unshare_iscsi(zfs_handle_t *zhp) 1061 { 1062 const char *dataset = zfs_get_name(zhp); 1063 libzfs_handle_t *hdl = zhp->zfs_hdl; 1064 1065 /* 1066 * Return if the volume is not shared 1067 */ 1068 if (zfs_is_shared_iscsi(zhp) != SHARED_ISCSI) 1069 return (0); 1070 1071 /* 1072 * If this fails with ENODEV it indicates that zvol wasn't shared so 1073 * we should return success in that case. 1074 */ 1075 if (iscsitgt_zfs_unshare == NULL || 1076 (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV)) { 1077 if (errno == EPERM) 1078 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1079 "Insufficient privileges to unshare iscsi")); 1080 return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED, 1081 dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset)); 1082 } 1083 1084 return (0); 1085 } 1086 1087 typedef struct mount_cbdata { 1088 zfs_handle_t **cb_datasets; 1089 int cb_used; 1090 int cb_alloc; 1091 } mount_cbdata_t; 1092 1093 static int 1094 mount_cb(zfs_handle_t *zhp, void *data) 1095 { 1096 mount_cbdata_t *cbp = data; 1097 1098 if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) { 1099 zfs_close(zhp); 1100 return (0); 1101 } 1102 1103 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) { 1104 zfs_close(zhp); 1105 return (0); 1106 } 1107 1108 if (cbp->cb_alloc == cbp->cb_used) { 1109 void *ptr; 1110 1111 if ((ptr = zfs_realloc(zhp->zfs_hdl, 1112 cbp->cb_datasets, cbp->cb_alloc * sizeof (void *), 1113 cbp->cb_alloc * 2 * sizeof (void *))) == NULL) 1114 return (-1); 1115 cbp->cb_datasets = ptr; 1116 1117 cbp->cb_alloc *= 2; 1118 } 1119 1120 cbp->cb_datasets[cbp->cb_used++] = zhp; 1121 1122 return (zfs_iter_filesystems(zhp, mount_cb, cbp)); 1123 } 1124 1125 static int 1126 dataset_cmp(const void *a, const void *b) 1127 { 1128 zfs_handle_t **za = (zfs_handle_t **)a; 1129 zfs_handle_t **zb = (zfs_handle_t **)b; 1130 char mounta[MAXPATHLEN]; 1131 char mountb[MAXPATHLEN]; 1132 boolean_t gota, gotb; 1133 1134 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 1135 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 1136 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 1137 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 1138 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 1139 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 1140 1141 if (gota && gotb) 1142 return (strcmp(mounta, mountb)); 1143 1144 if (gota) 1145 return (-1); 1146 if (gotb) 1147 return (1); 1148 1149 return (strcmp(zfs_get_name(a), zfs_get_name(b))); 1150 } 1151 1152 /* 1153 * Mount and share all datasets within the given pool. This assumes that no 1154 * datasets within the pool are currently mounted. Because users can create 1155 * complicated nested hierarchies of mountpoints, we first gather all the 1156 * datasets and mountpoints within the pool, and sort them by mountpoint. Once 1157 * we have the list of all filesystems, we iterate over them in order and mount 1158 * and/or share each one. 1159 */ 1160 #pragma weak zpool_mount_datasets = zpool_enable_datasets 1161 int 1162 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 1163 { 1164 mount_cbdata_t cb = { 0 }; 1165 libzfs_handle_t *hdl = zhp->zpool_hdl; 1166 zfs_handle_t *zfsp; 1167 int i, ret = -1; 1168 int *good; 1169 1170 /* 1171 * Gather all non-snap datasets within the pool. 1172 */ 1173 if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL) 1174 return (-1); 1175 cb.cb_alloc = 4; 1176 1177 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL) 1178 goto out; 1179 1180 cb.cb_datasets[0] = zfsp; 1181 cb.cb_used = 1; 1182 1183 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0) 1184 goto out; 1185 1186 /* 1187 * Sort the datasets by mountpoint. 1188 */ 1189 qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp); 1190 1191 /* 1192 * And mount all the datasets, keeping track of which ones 1193 * succeeded or failed. 1194 */ 1195 if ((good = zfs_alloc(zhp->zpool_hdl, 1196 cb.cb_used * sizeof (int))) == NULL) 1197 goto out; 1198 1199 ret = 0; 1200 for (i = 0; i < cb.cb_used; i++) { 1201 if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0) 1202 ret = -1; 1203 else 1204 good[i] = 1; 1205 } 1206 1207 /* 1208 * Then share all the ones that need to be shared. This needs 1209 * to be a separate pass in order to avoid excessive reloading 1210 * of the configuration. Good should never be NULL since 1211 * zfs_alloc is supposed to exit if memory isn't available. 1212 */ 1213 for (i = 0; i < cb.cb_used; i++) { 1214 if (good[i] && zfs_share(cb.cb_datasets[i]) != 0) 1215 ret = -1; 1216 } 1217 1218 free(good); 1219 1220 out: 1221 for (i = 0; i < cb.cb_used; i++) 1222 zfs_close(cb.cb_datasets[i]); 1223 free(cb.cb_datasets); 1224 1225 return (ret); 1226 } 1227 1228 /*ARGSUSED1*/ 1229 static int 1230 zvol_cb(zfs_handle_t *zhp, void *unused) 1231 { 1232 int error = 0; 1233 1234 if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) 1235 (void) zfs_iter_children(zhp, zvol_cb, NULL); 1236 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) 1237 error = zfs_unshare_iscsi(zhp); 1238 zfs_close(zhp); 1239 1240 return (error); 1241 } 1242 1243 static int 1244 mountpoint_compare(const void *a, const void *b) 1245 { 1246 const char *mounta = *((char **)a); 1247 const char *mountb = *((char **)b); 1248 1249 return (strcmp(mountb, mounta)); 1250 } 1251 1252 /* alias for 2002/240 */ 1253 #pragma weak zpool_unmount_datasets = zpool_disable_datasets 1254 /* 1255 * Unshare and unmount all datasets within the given pool. We don't want to 1256 * rely on traversing the DSL to discover the filesystems within the pool, 1257 * because this may be expensive (if not all of them are mounted), and can fail 1258 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and 1259 * gather all the filesystems that are currently mounted. 1260 */ 1261 int 1262 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 1263 { 1264 int used, alloc; 1265 struct mnttab entry; 1266 size_t namelen; 1267 char **mountpoints = NULL; 1268 zfs_handle_t *zfp; 1269 zfs_handle_t **datasets = NULL; 1270 libzfs_handle_t *hdl = zhp->zpool_hdl; 1271 int i; 1272 int ret = -1; 1273 int flags = (force ? MS_FORCE : 0); 1274 1275 /* 1276 * First unshare all zvols. 1277 */ 1278 zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name, 1279 ZFS_TYPE_FILESYSTEM); 1280 if (zfp != NULL) { 1281 (void) zfs_iter_children(zfp, zvol_cb, NULL); 1282 zfs_close(zfp); 1283 } 1284 1285 namelen = strlen(zhp->zpool_name); 1286 1287 rewind(hdl->libzfs_mnttab); 1288 used = alloc = 0; 1289 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 1290 /* 1291 * Ignore non-ZFS entries. 1292 */ 1293 if (entry.mnt_fstype == NULL || 1294 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 1295 continue; 1296 1297 /* 1298 * Ignore filesystems not within this pool. 1299 */ 1300 if (entry.mnt_mountp == NULL || 1301 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 1302 (entry.mnt_special[namelen] != '/' && 1303 entry.mnt_special[namelen] != '\0')) 1304 continue; 1305 1306 /* 1307 * At this point we've found a filesystem within our pool. Add 1308 * it to our growing list. 1309 */ 1310 if (used == alloc) { 1311 if (alloc == 0) { 1312 if ((mountpoints = zfs_alloc(hdl, 1313 8 * sizeof (void *))) == NULL) 1314 goto out; 1315 1316 if ((datasets = zfs_alloc(hdl, 1317 8 * sizeof (void *))) == NULL) 1318 goto out; 1319 1320 alloc = 8; 1321 } else { 1322 void *ptr; 1323 1324 if ((ptr = zfs_realloc(hdl, mountpoints, 1325 alloc * sizeof (void *), 1326 alloc * 2 * sizeof (void *))) == NULL) 1327 goto out; 1328 mountpoints = ptr; 1329 1330 if ((ptr = zfs_realloc(hdl, datasets, 1331 alloc * sizeof (void *), 1332 alloc * 2 * sizeof (void *))) == NULL) 1333 goto out; 1334 datasets = ptr; 1335 1336 alloc *= 2; 1337 } 1338 } 1339 1340 if ((mountpoints[used] = zfs_strdup(hdl, 1341 entry.mnt_mountp)) == NULL) 1342 goto out; 1343 1344 /* 1345 * This is allowed to fail, in case there is some I/O error. It 1346 * is only used to determine if we need to remove the underlying 1347 * mountpoint, so failure is not fatal. 1348 */ 1349 datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 1350 1351 used++; 1352 } 1353 1354 /* 1355 * At this point, we have the entire list of filesystems, so sort it by 1356 * mountpoint. 1357 */ 1358 qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 1359 1360 /* 1361 * Walk through and first unshare everything. 1362 */ 1363 for (i = 0; i < used; i++) { 1364 zfs_share_proto_t *curr_proto; 1365 for (curr_proto = share_all_proto; *curr_proto != PROTO_END; 1366 curr_proto++) { 1367 if (is_shared(hdl, mountpoints[i], *curr_proto) && 1368 unshare_one(hdl, mountpoints[i], 1369 mountpoints[i], *curr_proto) != 0) 1370 goto out; 1371 } 1372 } 1373 1374 /* 1375 * Now unmount everything, removing the underlying directories as 1376 * appropriate. 1377 */ 1378 for (i = 0; i < used; i++) { 1379 if (unmount_one(hdl, mountpoints[i], flags) != 0) 1380 goto out; 1381 } 1382 1383 for (i = 0; i < used; i++) { 1384 if (datasets[i]) 1385 remove_mountpoint(datasets[i]); 1386 } 1387 1388 ret = 0; 1389 out: 1390 for (i = 0; i < used; i++) { 1391 if (datasets[i]) 1392 zfs_close(datasets[i]); 1393 free(mountpoints[i]); 1394 } 1395 free(datasets); 1396 free(mountpoints); 1397 1398 return (ret); 1399 } 1400