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.
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.
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]
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, 2019 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright 2017 RackTop Systems.
28 * Copyright (c) 2018 Datto Inc.
29 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
33 * Routines to manage ZFS mounts. We separate all the nasty routines that have
34 * to deal with the OS. The following functions are the main entry points --
35 * they are used by mount and unmount and when changing a filesystem's
43 * This file also contains the functions used to manage sharing filesystems via
56 * zfs_unshareall_nfs()
57 * zfs_unshareall_smb()
59 * zfs_unshareall_bypath()
61 * The following functions are available for pool consumers, and will
62 * mount/unmount and share/unshare all datasets within pool:
64 * zpool_enable_datasets()
65 * zpool_disable_datasets()
79 #include <sys/mntent.h>
80 #include <sys/mount.h>
83 #include <sys/dsl_crypt.h>
87 #include "libzfs_impl.h"
88 #include <thread_pool.h>
91 #include <sys/systeminfo.h>
92 #define MAXISALEN 257 /* based on sysinfo(2) man page */
94 static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */
96 static void zfs_mount_task(void *);
97 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
98 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
102 * The share protocols table must be in the same order as the zfs_share_proto_t
103 * enum in libzfs_impl.h
112 proto_table_t proto_table[PROTO_END] = {
113 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
114 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
117 zfs_share_proto_t nfs_only[] = {
122 zfs_share_proto_t smb_only[] = {
126 zfs_share_proto_t share_all_proto[] = {
133 * Search the sharetab for the given mountpoint and protocol, returning
134 * a zfs_share_type_t value.
136 static zfs_share_type_t
137 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
139 char buf[MAXPATHLEN], *tab;
142 if (hdl->libzfs_sharetab == NULL)
143 return (SHARED_NOT_SHARED);
145 /* Reopen ZFS_SHARETAB to prevent reading stale data from open file */
146 if (freopen(ZFS_SHARETAB, "r", hdl->libzfs_sharetab) == NULL)
147 return (SHARED_NOT_SHARED);
149 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
151 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
153 /* the mountpoint is the first entry on each line */
154 if ((tab = strchr(buf, '\t')) == NULL)
158 if (strcmp(buf, mountpoint) == 0) {
160 * the protocol field is the third field
161 * skip over second field
164 if ((tab = strchr(ptr, '\t')) == NULL)
167 if ((tab = strchr(ptr, '\t')) == NULL)
171 proto_table[proto].p_name) == 0) {
184 return (SHARED_NOT_SHARED);
188 dir_is_empty_stat(const char *dirname)
193 * We only want to return false if the given path is a non empty
194 * directory, all other errors are handled elsewhere.
196 if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
201 * An empty directory will still have two entries in it, one
202 * entry for each of "." and "..".
204 if (st.st_size > 2) {
212 dir_is_empty_readdir(const char *dirname)
218 if ((dirfd = openat(AT_FDCWD, dirname,
219 O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
223 if ((dirp = fdopendir(dirfd)) == NULL) {
228 while ((dp = readdir64(dirp)) != NULL) {
230 if (strcmp(dp->d_name, ".") == 0 ||
231 strcmp(dp->d_name, "..") == 0)
234 (void) closedir(dirp);
238 (void) closedir(dirp);
243 * Returns true if the specified directory is empty. If we can't open the
244 * directory at all, return true so that the mount can fail with a more
245 * informative error message.
248 dir_is_empty(const char *dirname)
253 * If the statvfs call fails or the filesystem is not a ZFS
254 * filesystem, fall back to the slow path which uses readdir.
256 if ((statfs64(dirname, &st) != 0) ||
257 (st.f_type != ZFS_SUPER_MAGIC)) {
258 return (dir_is_empty_readdir(dirname));
262 * At this point, we know the provided path is on a ZFS
263 * filesystem, so we can use stat instead of readdir to
264 * determine if the directory is empty or not. We try to avoid
265 * using readdir because that requires opening "dirname"; this
266 * open file descriptor can potentially end up in a child
267 * process if there's a concurrent fork, thus preventing the
268 * zfs_mount() from otherwise succeeding (the open file
269 * descriptor inherited by the child process will cause the
270 * parent's mount to fail with EBUSY). The performance
271 * implications of replacing the open, read, and close with a
272 * single stat is nice; but is not the main motivation for the
275 return (dir_is_empty_stat(dirname));
279 * Checks to see if the mount is active. If the filesystem is mounted, we fill
280 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
284 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
288 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
292 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
298 zfs_is_mounted(zfs_handle_t *zhp, char **where)
300 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
304 * Returns true if the given dataset is mountable, false otherwise. Returns the
305 * mountpoint in 'buf'.
308 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
309 zprop_source_t *source, int flags)
311 char sourceloc[MAXNAMELEN];
312 zprop_source_t sourcetype;
314 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type,
318 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
319 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
321 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
322 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
325 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
328 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
329 getzoneid() == GLOBAL_ZONEID)
332 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
333 getzoneid() == GLOBAL_ZONEID)
336 if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE))
340 *source = sourcetype;
346 * The filesystem is mounted by invoking the system mount utility rather
347 * than by the system call mount(2). This ensures that the /etc/mtab
348 * file is correctly locked for the update. Performing our own locking
349 * and /etc/mtab update requires making an unsafe assumption about how
350 * the mount utility performs its locking. Unfortunately, this also means
351 * in the case of a mount failure we do not have the exact errno. We must
352 * make due with return value from the mount process.
354 * In the long term a shared library called libmount is under development
355 * which provides a common API to address the locking and errno issues.
356 * Once the standard mount utility has been updated to use this library
357 * we can add an autoconf check to conditionally use it.
359 * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
363 do_mount(const char *src, const char *mntpt, char *opts)
375 /* Return only the most critical mount error */
376 rc = libzfs_run_process(argv[0], argv, STDOUT_VERBOSE|STDERR_VERBOSE);
378 if (rc & MOUNT_FILEIO)
382 if (rc & MOUNT_SOFTWARE)
386 if (rc & MOUNT_SYSERR)
388 if (rc & MOUNT_USAGE)
391 return (ENXIO); /* Generic error */
398 do_unmount(const char *mntpt, int flags)
400 char force_opt[] = "-f";
401 char lazy_opt[] = "-l";
405 NULL, NULL, NULL, NULL };
408 if (flags & MS_FORCE) {
409 argv[count] = force_opt;
413 if (flags & MS_DETACH) {
414 argv[count] = lazy_opt;
418 argv[count] = (char *)mntpt;
419 rc = libzfs_run_process(argv[0], argv, STDOUT_VERBOSE|STDERR_VERBOSE);
421 return (rc ? EINVAL : 0);
425 zfs_add_option(zfs_handle_t *zhp, char *options, int len,
426 zfs_prop_t prop, char *on, char *off)
431 /* Skip adding duplicate default options */
432 if ((strstr(options, on) != NULL) || (strstr(options, off) != NULL))
436 * zfs_prop_get_int() is not used to ensure our mount options
437 * are not influenced by the current /proc/self/mounts contents.
439 value = getprop_uint64(zhp, prop, &source);
441 (void) strlcat(options, ",", len);
442 (void) strlcat(options, value ? on : off, len);
448 zfs_add_options(zfs_handle_t *zhp, char *options, int len)
452 error = zfs_add_option(zhp, options, len,
453 ZFS_PROP_ATIME, MNTOPT_ATIME, MNTOPT_NOATIME);
455 * don't add relatime/strictatime when atime=off, otherwise strictatime
456 * will force atime=on
458 if (strstr(options, MNTOPT_NOATIME) == NULL) {
459 error = zfs_add_option(zhp, options, len,
460 ZFS_PROP_RELATIME, MNTOPT_RELATIME, MNTOPT_STRICTATIME);
462 error = error ? error : zfs_add_option(zhp, options, len,
463 ZFS_PROP_DEVICES, MNTOPT_DEVICES, MNTOPT_NODEVICES);
464 error = error ? error : zfs_add_option(zhp, options, len,
465 ZFS_PROP_EXEC, MNTOPT_EXEC, MNTOPT_NOEXEC);
466 error = error ? error : zfs_add_option(zhp, options, len,
467 ZFS_PROP_READONLY, MNTOPT_RO, MNTOPT_RW);
468 error = error ? error : zfs_add_option(zhp, options, len,
469 ZFS_PROP_SETUID, MNTOPT_SETUID, MNTOPT_NOSETUID);
470 error = error ? error : zfs_add_option(zhp, options, len,
471 ZFS_PROP_NBMAND, MNTOPT_NBMAND, MNTOPT_NONBMAND);
477 * Mount the given filesystem.
480 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
483 char mountpoint[ZFS_MAXPROPLEN];
484 char mntopts[MNT_LINE_MAX];
485 char overlay[ZFS_MAXPROPLEN];
486 libzfs_handle_t *hdl = zhp->zfs_hdl;
490 if (options == NULL) {
491 (void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts));
493 (void) strlcpy(mntopts, options, sizeof (mntopts));
496 if (strstr(mntopts, MNTOPT_REMOUNT) != NULL)
500 * If the pool is imported read-only then all mounts must be read-only
502 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
503 (void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts));
505 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL,
511 * Append default mount options which apply to the mount point.
512 * This is done because under Linux (unlike Solaris) multiple mount
513 * points may reference a single super block. This means that just
514 * given a super block there is no back reference to update the per
515 * mount point options.
517 rc = zfs_add_options(zhp, mntopts, sizeof (mntopts));
519 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
520 "default options unavailable"));
521 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
522 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
527 * If the filesystem is encrypted the key must be loaded in order to
528 * mount. If the key isn't loaded, the MS_CRYPT flag decides whether
529 * or not we attempt to load the keys. Note: we must call
530 * zfs_refresh_properties() here since some callers of this function
531 * (most notably zpool_enable_datasets()) may implicitly load our key
532 * by loading the parent's key first.
534 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
535 zfs_refresh_properties(zhp);
536 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
539 * If the key is unavailable and MS_CRYPT is set give the
540 * user a chance to enter the key. Otherwise just fail
543 if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
544 if (flags & MS_CRYPT) {
545 rc = zfs_crypto_load_key(zhp, B_FALSE, NULL);
549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
550 "encryption key not loaded"));
551 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
552 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
560 * Append zfsutil option so the mount helper allow the mount
562 strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts));
564 /* Create the directory if it doesn't already exist */
565 if (lstat(mountpoint, &buf) != 0) {
566 if (mkdirp(mountpoint, 0755) != 0) {
567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
568 "failed to create mountpoint"));
569 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
570 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
576 * Overlay mounts are disabled by default but may be enabled
577 * via the 'overlay' property or the 'zfs mount -O' option.
579 if (!(flags & MS_OVERLAY)) {
580 if (zfs_prop_get(zhp, ZFS_PROP_OVERLAY, overlay,
581 sizeof (overlay), NULL, NULL, 0, B_FALSE) == 0) {
582 if (strcmp(overlay, "on") == 0) {
589 * Determine if the mountpoint is empty. If so, refuse to perform the
590 * mount. We don't perform this check if 'remount' is
591 * specified or if overlay option(-O) is given
593 if ((flags & MS_OVERLAY) == 0 && !remount &&
594 !dir_is_empty(mountpoint)) {
595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
596 "directory is not empty"));
597 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
598 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
601 /* perform the mount */
602 rc = do_mount(zfs_get_name(zhp), mountpoint, mntopts);
605 * Generic errors are nasty, but there are just way too many
606 * from mount(), and they're well-understood. We pick a few
607 * common ones to improve upon.
610 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
611 "mountpoint or dataset is busy"));
612 } else if (rc == EPERM) {
613 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
614 "Insufficient privileges"));
615 } else if (rc == ENOTSUP) {
619 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
620 (void) snprintf(buf, sizeof (buf),
621 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
622 "file system on a version %d pool. Pool must be"
623 " upgraded to mount this file system."),
624 (u_longlong_t)zfs_prop_get_int(zhp,
625 ZFS_PROP_VERSION), spa_version);
626 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
628 zfs_error_aux(hdl, strerror(rc));
630 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
631 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
635 /* remove the mounted entry before re-adding on remount */
637 libzfs_mnttab_remove(hdl, zhp->zfs_name);
639 /* add the mounted entry into our cache */
640 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts);
645 * Unmount a single filesystem.
648 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
652 error = do_unmount(mountpoint, flags);
654 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
655 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
663 * Unmount the given filesystem.
666 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
668 libzfs_handle_t *hdl = zhp->zfs_hdl;
671 boolean_t encroot, unmounted = B_FALSE;
673 /* check to see if we need to unmount the filesystem */
674 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
675 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
677 * mountpoint may have come from a call to
678 * getmnt/getmntany if it isn't NULL. If it is NULL,
679 * we know it comes from libzfs_mnttab_find which can
680 * then get freed later. We strdup it to play it safe.
682 if (mountpoint == NULL)
683 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
685 mntpt = zfs_strdup(hdl, mountpoint);
688 * Unshare and unmount the filesystem
690 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) {
695 if (unmount_one(hdl, mntpt, flags) != 0) {
697 (void) zfs_shareall(zhp);
701 libzfs_mnttab_remove(hdl, zhp->zfs_name);
707 * If the MS_CRYPT flag is provided we must ensure we attempt to
708 * unload the dataset's key regardless of whether we did any work
709 * to unmount it. We only do this for encryption roots.
711 if ((flags & MS_CRYPT) != 0 &&
712 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
713 zfs_refresh_properties(zhp);
715 if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0 &&
717 (void) zfs_mount(zhp, NULL, 0);
721 if (encroot && zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
722 ZFS_KEYSTATUS_AVAILABLE &&
723 zfs_crypto_unload_key(zhp) != 0) {
724 (void) zfs_mount(zhp, NULL, 0);
733 * Unmount this filesystem and any children inheriting the mountpoint property.
734 * To do this, just act like we're changing the mountpoint property, but don't
735 * remount the filesystems afterwards.
738 zfs_unmountall(zfs_handle_t *zhp, int flags)
740 prop_changelist_t *clp;
743 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
744 CL_GATHER_ITER_MOUNTED, flags);
748 ret = changelist_prefix(clp);
749 changelist_free(clp);
755 zfs_is_shared(zfs_handle_t *zhp)
757 zfs_share_type_t rc = 0;
758 zfs_share_proto_t *curr_proto;
760 if (ZFS_IS_VOLUME(zhp))
763 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
765 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
767 return (rc ? B_TRUE : B_FALSE);
771 zfs_share(zfs_handle_t *zhp)
773 assert(!ZFS_IS_VOLUME(zhp));
774 return (zfs_share_proto(zhp, share_all_proto));
778 zfs_unshare(zfs_handle_t *zhp)
780 assert(!ZFS_IS_VOLUME(zhp));
781 return (zfs_unshareall(zhp));
785 * Check to see if the filesystem is currently shared.
788 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
793 if (!zfs_is_mounted(zhp, &mountpoint))
794 return (SHARED_NOT_SHARED);
796 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))
797 != SHARED_NOT_SHARED) {
805 return (SHARED_NOT_SHARED);
810 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
812 return (zfs_is_shared_proto(zhp, where,
813 PROTO_NFS) != SHARED_NOT_SHARED);
817 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
819 return (zfs_is_shared_proto(zhp, where,
820 PROTO_SMB) != SHARED_NOT_SHARED);
824 * zfs_init_libshare(zhandle, service)
826 * Initialize the libshare API if it hasn't already been initialized.
827 * In all cases it returns 0 if it succeeded and an error if not. The
828 * service value is which part(s) of the API to initialize and is a
829 * direct map to the libshare sa_init(service) interface.
832 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
836 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
838 * We had a cache miss. Most likely it is a new ZFS
839 * dataset that was just created. We want to make sure
840 * so check timestamps to see if a different process
841 * has updated any of the configuration. If there was
842 * some non-ZFS change, we need to re-initialize the
845 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
846 if (sa_needs_refresh(zhandle->libzfs_sharehdl)) {
847 zfs_uninit_libshare(zhandle);
848 zhandle->libzfs_sharehdl = sa_init(service);
852 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
853 zhandle->libzfs_sharehdl = sa_init(service);
855 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
862 * zfs_uninit_libshare(zhandle)
864 * Uninitialize the libshare API if it hasn't already been
865 * uninitialized. It is OK to call multiple times.
868 zfs_uninit_libshare(libzfs_handle_t *zhandle)
870 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
871 sa_fini(zhandle->libzfs_sharehdl);
872 zhandle->libzfs_sharehdl = NULL;
877 * zfs_parse_options(options, proto)
879 * Call the legacy parse interface to get the protocol specific
880 * options using the NULL arg to indicate that this is a "parse" only.
883 zfs_parse_options(char *options, zfs_share_proto_t proto)
885 return (sa_parse_legacy_options(NULL, options,
886 proto_table[proto].p_name));
890 * Share the given filesystem according to the options in the specified
891 * protocol specific properties (sharenfs, sharesmb). We rely
892 * on "libshare" to do the dirty work for us.
895 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
897 char mountpoint[ZFS_MAXPROPLEN];
898 char shareopts[ZFS_MAXPROPLEN];
899 char sourcestr[ZFS_MAXPROPLEN];
900 libzfs_handle_t *hdl = zhp->zfs_hdl;
902 zfs_share_proto_t *curr_proto;
903 zprop_source_t sourcetype;
906 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0))
909 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
911 * Return success if there are no share options.
913 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
914 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
915 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
916 strcmp(shareopts, "off") == 0)
919 ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API);
921 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
922 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
923 zfs_get_name(zhp), sa_errorstr(ret));
928 * If the 'zoned' property is set, then zfs_is_mountable()
929 * will have already bailed out if we are in the global zone.
930 * But local zones cannot be NFS servers, so we ignore it for
931 * local zones as well.
933 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
936 share = sa_find_share(hdl->libzfs_sharehdl, mountpoint);
939 * This may be a new file system that was just
940 * created so isn't in the internal cache
941 * (second time through). Rather than
942 * reloading the entire configuration, we can
943 * assume ZFS has done the checking and it is
944 * safe to add this to the internal
947 if (sa_zfs_process_share(hdl->libzfs_sharehdl,
948 NULL, NULL, mountpoint,
949 proto_table[*curr_proto].p_name, sourcetype,
950 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
951 (void) zfs_error_fmt(hdl,
952 proto_table[*curr_proto].p_share_err,
953 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
957 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
958 share = sa_find_share(hdl->libzfs_sharehdl,
963 err = sa_enable_share(share,
964 proto_table[*curr_proto].p_name);
966 (void) zfs_error_fmt(hdl,
967 proto_table[*curr_proto].p_share_err,
968 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
973 (void) zfs_error_fmt(hdl,
974 proto_table[*curr_proto].p_share_err,
975 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
986 zfs_share_nfs(zfs_handle_t *zhp)
988 return (zfs_share_proto(zhp, nfs_only));
992 zfs_share_smb(zfs_handle_t *zhp)
994 return (zfs_share_proto(zhp, smb_only));
998 zfs_shareall(zfs_handle_t *zhp)
1000 return (zfs_share_proto(zhp, share_all_proto));
1004 * Unshare a filesystem by mountpoint.
1007 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
1008 zfs_share_proto_t proto)
1014 * Mountpoint could get trashed if libshare calls getmntany
1015 * which it does during API initialization, so strdup the
1018 mntpt = zfs_strdup(hdl, mountpoint);
1020 /* make sure libshare initialized */
1021 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
1022 free(mntpt); /* don't need the copy anymore */
1023 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
1024 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
1025 name, sa_errorstr(err)));
1028 share = sa_find_share(hdl->libzfs_sharehdl, mntpt);
1029 free(mntpt); /* don't need the copy anymore */
1031 if (share != NULL) {
1032 err = sa_disable_share(share, proto_table[proto].p_name);
1034 return (zfs_error_fmt(hdl,
1035 proto_table[proto].p_unshare_err,
1036 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
1037 name, sa_errorstr(err)));
1040 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
1041 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
1048 * Unshare the given filesystem.
1051 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
1052 zfs_share_proto_t *proto)
1054 libzfs_handle_t *hdl = zhp->zfs_hdl;
1055 struct mnttab entry;
1058 /* check to see if need to unmount the filesystem */
1059 if (mountpoint != NULL)
1060 mntpt = zfs_strdup(hdl, mountpoint);
1062 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
1063 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
1064 zfs_share_proto_t *curr_proto;
1066 if (mountpoint == NULL)
1067 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
1069 for (curr_proto = proto; *curr_proto != PROTO_END;
1072 if (is_shared(hdl, mntpt, *curr_proto) &&
1073 unshare_one(hdl, zhp->zfs_name,
1074 mntpt, *curr_proto) != 0) {
1088 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
1090 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
1094 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
1096 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1100 * Same as zfs_unmountall(), but for NFS and SMB unshares.
1103 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
1105 prop_changelist_t *clp;
1108 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
1112 ret = changelist_unshare(clp, proto);
1113 changelist_free(clp);
1119 zfs_unshareall_nfs(zfs_handle_t *zhp)
1121 return (zfs_unshareall_proto(zhp, nfs_only));
1125 zfs_unshareall_smb(zfs_handle_t *zhp)
1127 return (zfs_unshareall_proto(zhp, smb_only));
1131 zfs_unshareall(zfs_handle_t *zhp)
1133 return (zfs_unshareall_proto(zhp, share_all_proto));
1137 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1139 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1143 zfs_unshareall_bytype(zfs_handle_t *zhp, const char *mountpoint,
1147 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1148 if (strcmp(proto, "nfs") == 0)
1149 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
1150 else if (strcmp(proto, "smb") == 0)
1151 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1157 * Remove the mountpoint associated with the current dataset, if necessary.
1158 * We only remove the underlying directory if:
1160 * - The mountpoint is not 'none' or 'legacy'
1161 * - The mountpoint is non-empty
1162 * - The mountpoint is the default or inherited
1163 * - The 'zoned' property is set, or we're in a local zone
1165 * Any other directories we leave alone.
1168 remove_mountpoint(zfs_handle_t *zhp)
1170 char mountpoint[ZFS_MAXPROPLEN];
1171 zprop_source_t source;
1173 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), &source, 0))
1176 if (source == ZPROP_SRC_DEFAULT ||
1177 source == ZPROP_SRC_INHERITED) {
1179 * Try to remove the directory, silently ignoring any errors.
1180 * The filesystem may have since been removed or moved around,
1181 * and this error isn't really useful to the administrator in
1184 (void) rmdir(mountpoint);
1189 * Add the given zfs handle to the cb_handles array, dynamically reallocating
1190 * the array if it is out of space.
1193 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1195 if (cbp->cb_alloc == cbp->cb_used) {
1197 zfs_handle_t **newhandles;
1199 newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64;
1200 newhandles = zfs_realloc(zhp->zfs_hdl,
1201 cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *),
1202 newsz * sizeof (zfs_handle_t *));
1203 cbp->cb_handles = newhandles;
1204 cbp->cb_alloc = newsz;
1206 cbp->cb_handles[cbp->cb_used++] = zhp;
1210 * Recursive helper function used during file system enumeration
1213 zfs_iter_cb(zfs_handle_t *zhp, void *data)
1215 get_all_cb_t *cbp = data;
1217 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1222 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1227 if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
1228 ZFS_KEYSTATUS_UNAVAILABLE) {
1234 * If this filesystem is inconsistent and has a receive resume
1235 * token, we can not mount it.
1237 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1238 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1239 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1244 libzfs_add_handle(cbp, zhp);
1245 if (zfs_iter_filesystems(zhp, zfs_iter_cb, cbp) != 0) {
1253 * Sort comparator that compares two mountpoint paths. We sort these paths so
1254 * that subdirectories immediately follow their parents. This means that we
1255 * effectively treat the '/' character as the lowest value non-nul char.
1256 * Since filesystems from non-global zones can have the same mountpoint
1257 * as other filesystems, the comparator sorts global zone filesystems to
1258 * the top of the list. This means that the global zone will traverse the
1259 * filesystem list in the correct order and can stop when it sees the
1260 * first zoned filesystem. In a non-global zone, only the delegated
1261 * filesystems are seen.
1263 * An example sorted list using this comparator would look like:
1273 * The mounting code depends on this ordering to deterministically iterate
1274 * over filesystems in order to spawn parallel mount tasks.
1277 mountpoint_cmp(const void *arga, const void *argb)
1279 zfs_handle_t *const *zap = arga;
1280 zfs_handle_t *za = *zap;
1281 zfs_handle_t *const *zbp = argb;
1282 zfs_handle_t *zb = *zbp;
1283 char mounta[MAXPATHLEN];
1284 char mountb[MAXPATHLEN];
1285 const char *a = mounta;
1286 const char *b = mountb;
1287 boolean_t gota, gotb;
1288 uint64_t zoneda, zonedb;
1290 zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED);
1291 zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED);
1292 if (zoneda && !zonedb)
1294 if (!zoneda && zonedb)
1297 gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM);
1299 verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta,
1300 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1302 gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM);
1304 verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb,
1305 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1309 while (*a != '\0' && (*a == *b)) {
1323 return (*a < *b ? -1 : *a > *b);
1332 * If neither filesystem has a mountpoint, revert to sorting by
1335 return (strcmp(zfs_get_name(za), zfs_get_name(zb)));
1339 * Return true if path2 is a child of path1.
1342 libzfs_path_contains(const char *path1, const char *path2)
1344 return (strstr(path2, path1) == path2 && path2[strlen(path1)] == '/');
1348 * Given a mountpoint specified by idx in the handles array, find the first
1349 * non-descendent of that mountpoint and return its index. Descendant paths
1350 * start with the parent's path. This function relies on the ordering
1351 * enforced by mountpoint_cmp().
1354 non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx)
1356 char parent[ZFS_MAXPROPLEN];
1357 char child[ZFS_MAXPROPLEN];
1360 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent,
1361 sizeof (parent), NULL, NULL, 0, B_FALSE) == 0);
1363 for (i = idx + 1; i < num_handles; i++) {
1364 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child,
1365 sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1366 if (!libzfs_path_contains(parent, child))
1372 typedef struct mnt_param {
1373 libzfs_handle_t *mnt_hdl;
1375 zfs_handle_t **mnt_zhps; /* filesystems to mount */
1376 size_t mnt_num_handles;
1377 int mnt_idx; /* Index of selected entry to mount */
1378 zfs_iter_f mnt_func;
1383 * Allocate and populate the parameter struct for mount function, and
1384 * schedule mounting of the entry selected by idx.
1387 zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles,
1388 size_t num_handles, int idx, zfs_iter_f func, void *data, tpool_t *tp)
1390 mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t));
1392 mnt_param->mnt_hdl = hdl;
1393 mnt_param->mnt_tp = tp;
1394 mnt_param->mnt_zhps = handles;
1395 mnt_param->mnt_num_handles = num_handles;
1396 mnt_param->mnt_idx = idx;
1397 mnt_param->mnt_func = func;
1398 mnt_param->mnt_data = data;
1400 (void) tpool_dispatch(tp, zfs_mount_task, (void*)mnt_param);
1404 * This is the structure used to keep state of mounting or sharing operations
1405 * during a call to zpool_enable_datasets().
1407 typedef struct mount_state {
1409 * ms_mntstatus is set to -1 if any mount fails. While multiple threads
1410 * could update this variable concurrently, no synchronization is
1411 * needed as it's only ever set to -1.
1415 const char *ms_mntopts;
1419 zfs_mount_one(zfs_handle_t *zhp, void *arg)
1421 mount_state_t *ms = arg;
1425 * don't attempt to mount encrypted datasets with
1428 if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
1429 ZFS_KEYSTATUS_UNAVAILABLE)
1432 if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0)
1433 ret = ms->ms_mntstatus = -1;
1438 zfs_share_one(zfs_handle_t *zhp, void *arg)
1440 mount_state_t *ms = arg;
1443 if (zfs_share(zhp) != 0)
1444 ret = ms->ms_mntstatus = -1;
1449 * Thread pool function to mount one file system. On completion, it finds and
1450 * schedules its children to be mounted. This depends on the sorting done in
1451 * zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries
1452 * each descending from the previous) will have no parallelism since we always
1453 * have to wait for the parent to finish mounting before we can schedule
1457 zfs_mount_task(void *arg)
1459 mnt_param_t *mp = arg;
1460 int idx = mp->mnt_idx;
1461 zfs_handle_t **handles = mp->mnt_zhps;
1462 size_t num_handles = mp->mnt_num_handles;
1463 char mountpoint[ZFS_MAXPROPLEN];
1465 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint,
1466 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
1468 if (mp->mnt_func(handles[idx], mp->mnt_data) != 0)
1472 * We dispatch tasks to mount filesystems with mountpoints underneath
1473 * this one. We do this by dispatching the next filesystem with a
1474 * descendant mountpoint of the one we just mounted, then skip all of
1475 * its descendants, dispatch the next descendant mountpoint, and so on.
1476 * The non_descendant_idx() function skips over filesystems that are
1477 * descendants of the filesystem we just dispatched.
1479 for (int i = idx + 1; i < num_handles;
1480 i = non_descendant_idx(handles, num_handles, i)) {
1481 char child[ZFS_MAXPROPLEN];
1482 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT,
1483 child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1485 if (!libzfs_path_contains(mountpoint, child))
1486 break; /* not a descendant, return */
1487 zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i,
1488 mp->mnt_func, mp->mnt_data, mp->mnt_tp);
1494 * Issue the func callback for each ZFS handle contained in the handles
1495 * array. This function is used to mount all datasets, and so this function
1496 * guarantees that filesystems for parent mountpoints are called before their
1497 * children. As such, before issuing any callbacks, we first sort the array
1498 * of handles by mountpoint.
1500 * Callbacks are issued in one of two ways:
1502 * 1. Sequentially: If the parallel argument is B_FALSE or the ZFS_SERIAL_MOUNT
1503 * environment variable is set, then we issue callbacks sequentially.
1505 * 2. In parallel: If the parallel argument is B_TRUE and the ZFS_SERIAL_MOUNT
1506 * environment variable is not set, then we use a tpool to dispatch threads
1507 * to mount filesystems in parallel. This function dispatches tasks to mount
1508 * the filesystems at the top-level mountpoints, and these tasks in turn
1509 * are responsible for recursively mounting filesystems in their children
1513 zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles,
1514 size_t num_handles, zfs_iter_f func, void *data, boolean_t parallel)
1516 zoneid_t zoneid = getzoneid();
1519 * The ZFS_SERIAL_MOUNT environment variable is an undocumented
1520 * variable that can be used as a convenience to do a/b comparison
1521 * of serial vs. parallel mounting.
1523 boolean_t serial_mount = !parallel ||
1524 (getenv("ZFS_SERIAL_MOUNT") != NULL);
1527 * Sort the datasets by mountpoint. See mountpoint_cmp for details
1528 * of how these are sorted.
1530 qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp);
1533 for (int i = 0; i < num_handles; i++) {
1534 func(handles[i], data);
1540 * Issue the callback function for each dataset using a parallel
1541 * algorithm that uses a thread pool to manage threads.
1543 tpool_t *tp = tpool_create(1, mount_tp_nthr, 0, NULL);
1546 * There may be multiple "top level" mountpoints outside of the pool's
1547 * root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of
1550 for (int i = 0; i < num_handles;
1551 i = non_descendant_idx(handles, num_handles, i)) {
1553 * Since the mountpoints have been sorted so that the zoned
1554 * filesystems are at the end, a zoned filesystem seen from
1555 * the global zone means that we're done.
1557 if (zoneid == GLOBAL_ZONEID &&
1558 zfs_prop_get_int(handles[i], ZFS_PROP_ZONED))
1560 zfs_dispatch_mount(hdl, handles, num_handles, i, func, data,
1564 tpool_wait(tp); /* wait for all scheduled mounts to complete */
1569 * Mount and share all datasets within the given pool. This assumes that no
1570 * datasets within the pool are currently mounted.
1572 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1574 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1576 get_all_cb_t cb = { 0 };
1577 mount_state_t ms = { 0 };
1581 if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
1582 ZFS_TYPE_DATASET)) == NULL)
1586 * Gather all non-snapshot datasets within the pool. Start by adding
1587 * the root filesystem for this pool to the list, and then iterate
1588 * over all child filesystems.
1590 libzfs_add_handle(&cb, zfsp);
1591 if (zfs_iter_filesystems(zfsp, zfs_iter_cb, &cb) != 0)
1595 * Mount all filesystems
1597 ms.ms_mntopts = mntopts;
1598 ms.ms_mntflags = flags;
1599 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1600 zfs_mount_one, &ms, B_TRUE);
1601 if (ms.ms_mntstatus != 0)
1602 ret = ms.ms_mntstatus;
1605 * Share all filesystems that need to be shared. This needs to be
1606 * a separate pass because libshare is not mt-safe, and so we need
1607 * to share serially.
1609 ms.ms_mntstatus = 0;
1610 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1611 zfs_share_one, &ms, B_FALSE);
1612 if (ms.ms_mntstatus != 0)
1613 ret = ms.ms_mntstatus;
1616 for (int i = 0; i < cb.cb_used; i++)
1617 zfs_close(cb.cb_handles[i]);
1618 free(cb.cb_handles);
1624 mountpoint_compare(const void *a, const void *b)
1626 const char *mounta = *((char **)a);
1627 const char *mountb = *((char **)b);
1629 return (strcmp(mountb, mounta));
1632 /* alias for 2002/240 */
1633 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1635 * Unshare and unmount all datasets within the given pool. We don't want to
1636 * rely on traversing the DSL to discover the filesystems within the pool,
1637 * because this may be expensive (if not all of them are mounted), and can fail
1638 * arbitrarily (on I/O error, for example). Instead, we walk /proc/self/mounts
1639 * and gather all the filesystems that are currently mounted.
1642 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1645 struct mnttab entry;
1647 char **mountpoints = NULL;
1648 zfs_handle_t **datasets = NULL;
1649 libzfs_handle_t *hdl = zhp->zpool_hdl;
1652 int flags = (force ? MS_FORCE : 0);
1654 namelen = strlen(zhp->zpool_name);
1656 /* Reopen MNTTAB to prevent reading stale data from open file */
1657 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1661 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1663 * Ignore non-ZFS entries.
1665 if (entry.mnt_fstype == NULL ||
1666 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1670 * Ignore filesystems not within this pool.
1672 if (entry.mnt_mountp == NULL ||
1673 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1674 (entry.mnt_special[namelen] != '/' &&
1675 entry.mnt_special[namelen] != '\0'))
1679 * At this point we've found a filesystem within our pool. Add
1680 * it to our growing list.
1682 if (used == alloc) {
1684 if ((mountpoints = zfs_alloc(hdl,
1685 8 * sizeof (void *))) == NULL)
1688 if ((datasets = zfs_alloc(hdl,
1689 8 * sizeof (void *))) == NULL)
1696 if ((ptr = zfs_realloc(hdl, mountpoints,
1697 alloc * sizeof (void *),
1698 alloc * 2 * sizeof (void *))) == NULL)
1702 if ((ptr = zfs_realloc(hdl, datasets,
1703 alloc * sizeof (void *),
1704 alloc * 2 * sizeof (void *))) == NULL)
1712 if ((mountpoints[used] = zfs_strdup(hdl,
1713 entry.mnt_mountp)) == NULL)
1717 * This is allowed to fail, in case there is some I/O error. It
1718 * is only used to determine if we need to remove the underlying
1719 * mountpoint, so failure is not fatal.
1721 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1727 * At this point, we have the entire list of filesystems, so sort it by
1730 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1733 * Walk through and first unshare everything.
1735 for (i = 0; i < used; i++) {
1736 zfs_share_proto_t *curr_proto;
1737 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1739 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1740 unshare_one(hdl, mountpoints[i],
1741 mountpoints[i], *curr_proto) != 0)
1747 * Now unmount everything, removing the underlying directories as
1750 for (i = 0; i < used; i++) {
1751 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1755 for (i = 0; i < used; i++) {
1757 remove_mountpoint(datasets[i]);
1762 for (i = 0; i < used; i++) {
1764 zfs_close(datasets[i]);
1765 free(mountpoints[i]);