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