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 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Routines to manage ZFS mounts. We separate all the nasty routines that have
31 * to deal with the OS. The following functions are the main entry points --
32 * they are used by mount and unmount and when changing a filesystem's
40 * This file also contains the functions used to manage sharing filesystems via
50 * zfs_unshareall_nfs()
51 * zfs_is_shared_iscsi()
55 * The following functions are available for pool consumers, and will
56 * mount/unmount and share/unshare all datasets within pool:
58 * zpool_enable_datasets()
59 * zpool_disable_datasets()
72 #include <sys/mntent.h>
73 #include <sys/mnttab.h>
74 #include <sys/mount.h>
79 #include "libzfs_impl.h"
81 static int (*iscsitgt_zfs_share)(const char *);
82 static int (*iscsitgt_zfs_unshare)(const char *);
83 static int (*iscsitgt_zfs_is_shared)(const char *);
85 #pragma init(zfs_iscsi_init)
91 if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1",
92 RTLD_LAZY | RTLD_GLOBAL)) == NULL ||
93 (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt,
94 "iscsitgt_zfs_share")) == NULL ||
95 (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt,
96 "iscsitgt_zfs_unshare")) == NULL ||
97 (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt,
98 "iscsitgt_zfs_is_shared")) == NULL) {
99 iscsitgt_zfs_share = NULL;
100 iscsitgt_zfs_unshare = NULL;
101 iscsitgt_zfs_is_shared = NULL;
106 * Search the sharetab for the given mountpoint, returning true if it is found.
109 is_shared(libzfs_handle_t *hdl, const char *mountpoint)
111 char buf[MAXPATHLEN], *tab;
113 if (hdl->libzfs_sharetab == NULL)
116 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
118 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
120 /* the mountpoint is the first entry on each line */
121 if ((tab = strchr(buf, '\t')) != NULL) {
123 if (strcmp(buf, mountpoint) == 0)
133 * Returns true if the specified directory is empty. If we can't open the
134 * directory at all, return true so that the mount can fail with a more
135 * informative error message.
138 dir_is_empty(const char *dirname)
143 if ((dirp = opendir(dirname)) == NULL)
146 while ((dp = readdir64(dirp)) != NULL) {
148 if (strcmp(dp->d_name, ".") == 0 ||
149 strcmp(dp->d_name, "..") == 0)
152 (void) closedir(dirp);
156 (void) closedir(dirp);
162 * Checks to see if the mount is active. If the filesystem is mounted, we fill
163 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
167 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
169 struct mnttab search = { 0 }, entry;
172 * Search for the entry in /etc/mnttab. We don't bother getting the
173 * mountpoint, as we can just search for the special device. This will
174 * also let us find mounts when the mountpoint is 'legacy'.
176 search.mnt_special = (char *)special;
177 search.mnt_fstype = MNTTYPE_ZFS;
179 rewind(zfs_hdl->libzfs_mnttab);
180 if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0)
184 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
190 zfs_is_mounted(zfs_handle_t *zhp, char **where)
192 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
196 * Returns true if the given dataset is mountable, false otherwise. Returns the
197 * mountpoint in 'buf'.
200 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
201 zfs_source_t *source)
203 char sourceloc[ZFS_MAXNAMELEN];
204 zfs_source_t sourcetype;
206 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
209 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
210 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
212 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
213 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
216 if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT))
219 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
220 getzoneid() == GLOBAL_ZONEID)
224 *source = sourcetype;
230 * Mount the given filesystem.
233 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
236 char mountpoint[ZFS_MAXPROPLEN];
237 char mntopts[MNT_LINE_MAX];
238 libzfs_handle_t *hdl = zhp->zfs_hdl;
243 (void) strlcpy(mntopts, options, sizeof (mntopts));
245 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
248 /* Create the directory if it doesn't already exist */
249 if (lstat(mountpoint, &buf) != 0) {
250 if (mkdirp(mountpoint, 0755) != 0) {
251 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
252 "failed to create mountpoint"));
253 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
254 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
259 #if 0 /* FreeBSD: overlay mounts are not checked. */
261 * Determine if the mountpoint is empty. If so, refuse to perform the
262 * mount. We don't perform this check if MS_OVERLAY is specified, which
263 * would defeat the point. We also avoid this check if 'remount' is
266 if ((flags & MS_OVERLAY) == 0 &&
267 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
268 !dir_is_empty(mountpoint)) {
269 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
270 "directory is not empty"));
271 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
272 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
276 /* perform the mount */
277 if (zmount(zfs_get_name(zhp), mountpoint, flags,
278 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
280 * Generic errors are nasty, but there are just way too many
281 * from mount(), and they're well-understood. We pick a few
282 * common ones to improve upon.
285 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
286 "mountpoint or dataset is busy"));
288 zfs_error_aux(hdl, strerror(errno));
290 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
291 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
299 * Unmount a single filesystem.
302 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
304 if (unmount(mountpoint, flags) != 0) {
305 zfs_error_aux(hdl, strerror(errno));
306 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
307 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
315 * Unmount the given filesystem.
318 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
320 struct mnttab search = { 0 }, entry;
322 /* check to see if need to unmount the filesystem */
323 search.mnt_special = zhp->zfs_name;
324 search.mnt_fstype = MNTTYPE_ZFS;
325 rewind(zhp->zfs_hdl->libzfs_mnttab);
326 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
327 getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
329 if (mountpoint == NULL)
330 mountpoint = entry.mnt_mountp;
333 * Unshare and unmount the filesystem
335 if (zfs_unshare_nfs(zhp, mountpoint) != 0 ||
336 unmount_one(zhp->zfs_hdl, mountpoint, flags) != 0)
344 * Unmount this filesystem and any children inheriting the mountpoint property.
345 * To do this, just act like we're changing the mountpoint property, but don't
346 * remount the filesystems afterwards.
349 zfs_unmountall(zfs_handle_t *zhp, int flags)
351 prop_changelist_t *clp;
354 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags);
358 ret = changelist_prefix(clp);
359 changelist_free(clp);
365 zfs_is_shared(zfs_handle_t *zhp)
367 if (ZFS_IS_VOLUME(zhp))
368 return (zfs_is_shared_iscsi(zhp));
370 return (zfs_is_shared_nfs(zhp, NULL));
374 zfs_share(zfs_handle_t *zhp)
376 if (ZFS_IS_VOLUME(zhp))
377 return (zfs_share_iscsi(zhp));
379 return (zfs_share_nfs(zhp));
383 zfs_unshare(zfs_handle_t *zhp)
385 if (ZFS_IS_VOLUME(zhp))
386 return (zfs_unshare_iscsi(zhp));
388 return (zfs_unshare_nfs(zhp, NULL));
392 * Check to see if the filesystem is currently shared.
395 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
399 if (!zfs_is_mounted(zhp, &mountpoint))
402 if (is_shared(zhp->zfs_hdl, mountpoint)) {
415 * Share the given filesystem according to the options in 'sharenfs'. We rely
416 * on share(1M) to the dirty work for us.
419 zfs_share_nfs(zfs_handle_t *zhp)
421 char mountpoint[ZFS_MAXPROPLEN];
422 char shareopts[ZFS_MAXPROPLEN];
423 char buf[MAXPATHLEN];
425 libzfs_handle_t *hdl = zhp->zfs_hdl;
427 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
431 * Return success if there are no share options.
433 if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts),
434 NULL, NULL, 0, B_FALSE) != 0 ||
435 strcmp(shareopts, "off") == 0)
439 * If the 'zoned' property is set, then zfs_is_mountable() will have
440 * already bailed out if we are in the global zone. But local
441 * zones cannot be NFS servers, so we ignore it for local zones as well.
443 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
450 if (strcmp(shareopts, "on") == 0)
451 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
453 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
455 zfs_error_aux(hdl, "%s", strerror(error));
456 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
457 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
464 * Invoke the share(1M) command. We always do this, even if it's
465 * currently shared, as the options may have changed.
467 if (strcmp(shareopts, "on") == 0)
468 (void) snprintf(buf, sizeof (buf), "/usr/sbin/share "
469 "-F nfs \"%s\" 2>&1", mountpoint);
471 (void) snprintf(buf, sizeof (buf), "/usr/sbin/share "
472 "-F nfs -o \"%s\" \"%s\" 2>&1", shareopts,
475 if ((fp = popen(buf, "r")) == NULL)
476 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
477 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
481 * share(1M) should only produce output if there is some kind
482 * of error. All output begins with "share_nfs: ", so we trim
483 * this off to get to the real error.
485 if (fgets(buf, sizeof (buf), fp) != NULL) {
486 char *colon = strchr(buf, ':');
488 while (buf[strlen(buf) - 1] == '\n')
489 buf[strlen(buf) - 1] = '\0';
492 zfs_error_aux(hdl, colon + 2);
494 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
495 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
498 verify(pclose(fp) != 0);
502 verify(pclose(fp) == 0);
509 * Unshare a filesystem by mountpoint.
512 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint)
514 char buf[MAXPATHLEN];
521 error = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
523 zfs_error_aux(hdl, "%s", strerror(error));
524 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
525 dgettext(TEXT_DOMAIN,
526 "cannot unshare '%s'"), name));
530 (void) snprintf(buf, sizeof (buf),
531 "/usr/sbin/unshare \"%s\" 2>&1",
534 if ((fp = popen(buf, "r")) == NULL)
535 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
536 dgettext(TEXT_DOMAIN,
537 "cannot unshare '%s'"), name));
540 * unshare(1M) should only produce output if there is
541 * some kind of error. All output begins with "unshare
542 * nfs: ", so we trim this off to get to the real error.
544 if (fgets(buf, sizeof (buf), fp) != NULL) {
545 char *colon = strchr(buf, ':');
547 while (buf[strlen(buf) - 1] == '\n')
548 buf[strlen(buf) - 1] = '\0';
551 zfs_error_aux(hdl, colon + 2);
553 verify(pclose(fp) != 0);
555 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
556 dgettext(TEXT_DOMAIN,
557 "cannot unshare '%s'"), name));
560 verify(pclose(fp) == 0);
567 * Unshare the given filesystem.
570 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
572 struct mnttab search = { 0 }, entry;
574 /* check to see if need to unmount the filesystem */
575 search.mnt_special = (char *)zfs_get_name(zhp);
576 search.mnt_fstype = MNTTYPE_ZFS;
577 rewind(zhp->zfs_hdl->libzfs_mnttab);
578 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
579 getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
581 if (mountpoint == NULL)
582 mountpoint = entry.mnt_mountp;
584 if (is_shared(zhp->zfs_hdl, mountpoint) &&
585 unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0)
593 * Same as zfs_unmountall(), but for NFS unshares.
596 zfs_unshareall_nfs(zfs_handle_t *zhp)
598 prop_changelist_t *clp;
601 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0);
605 ret = changelist_unshare(clp);
606 changelist_free(clp);
612 * Remove the mountpoint associated with the current dataset, if necessary.
613 * We only remove the underlying directory if:
615 * - The mountpoint is not 'none' or 'legacy'
616 * - The mountpoint is non-empty
617 * - The mountpoint is the default or inherited
618 * - The 'zoned' property is set, or we're in a local zone
620 * Any other directories we leave alone.
623 remove_mountpoint(zfs_handle_t *zhp)
625 char mountpoint[ZFS_MAXPROPLEN];
628 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
632 if (source == ZFS_SRC_DEFAULT ||
633 source == ZFS_SRC_INHERITED) {
635 * Try to remove the directory, silently ignoring any errors.
636 * The filesystem may have since been removed or moved around,
637 * and this error isn't really useful to the administrator in
640 (void) rmdir(mountpoint);
645 zfs_is_shared_iscsi(zfs_handle_t *zhp)
647 return (iscsitgt_zfs_is_shared != NULL &&
648 iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
652 zfs_share_iscsi(zfs_handle_t *zhp)
654 char shareopts[ZFS_MAXPROPLEN];
655 const char *dataset = zhp->zfs_name;
656 libzfs_handle_t *hdl = zhp->zfs_hdl;
659 * Return success if there are no share options.
661 if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
662 sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
663 strcmp(shareopts, "off") == 0)
666 /* We don't support iSCSI on FreeBSD yet. */
668 if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0)
669 return (zfs_error_fmt(hdl, EZFS_SHAREISCSIFAILED,
670 dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
677 zfs_unshare_iscsi(zfs_handle_t *zhp)
679 const char *dataset = zfs_get_name(zhp);
680 libzfs_handle_t *hdl = zhp->zfs_hdl;
682 /* We don't support iSCSI on FreeBSD yet. */
685 * Return if the volume is not shared
687 if (!zfs_is_shared_iscsi(zhp))
691 * If this fails with ENODEV it indicates that zvol wasn't shared so
692 * we should return success in that case.
694 if (iscsitgt_zfs_unshare == NULL ||
695 (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV))
696 return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED,
697 dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
703 typedef struct mount_cbdata {
704 zfs_handle_t **cb_datasets;
710 mount_cb(zfs_handle_t *zhp, void *data)
712 mount_cbdata_t *cbp = data;
714 if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
719 if (cbp->cb_alloc == cbp->cb_used) {
722 if ((ptr = zfs_realloc(zhp->zfs_hdl,
723 cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
724 cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
726 cbp->cb_datasets = ptr;
731 cbp->cb_datasets[cbp->cb_used++] = zhp;
733 return (zfs_iter_children(zhp, mount_cb, cbp));
737 dataset_cmp(const void *a, const void *b)
739 zfs_handle_t **za = (zfs_handle_t **)a;
740 zfs_handle_t **zb = (zfs_handle_t **)b;
741 char mounta[MAXPATHLEN];
742 char mountb[MAXPATHLEN];
743 boolean_t gota, gotb;
745 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
746 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
747 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
748 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
749 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
750 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
753 return (strcmp(mounta, mountb));
760 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
764 * Mount and share all datasets within the given pool. This assumes that no
765 * datasets within the pool are currently mounted. Because users can create
766 * complicated nested hierarchies of mountpoints, we first gather all the
767 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
768 * we have the list of all filesystems, we iterate over them in order and mount
769 * and/or share each one.
771 #pragma weak zpool_mount_datasets = zpool_enable_datasets
773 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
775 mount_cbdata_t cb = { 0 };
776 libzfs_handle_t *hdl = zhp->zpool_hdl;
781 * Gather all datasets within the pool.
783 if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
787 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_ANY)) == NULL)
790 cb.cb_datasets[0] = zfsp;
793 if (zfs_iter_children(zfsp, mount_cb, &cb) != 0)
797 * Sort the datasets by mountpoint.
799 qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
802 * And mount all the datasets.
805 for (i = 0; i < cb.cb_used; i++) {
806 if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0 ||
807 zfs_share(cb.cb_datasets[i]) != 0)
812 for (i = 0; i < cb.cb_used; i++)
813 zfs_close(cb.cb_datasets[i]);
814 free(cb.cb_datasets);
821 zvol_cb(const char *dataset, void *data)
823 libzfs_handle_t *hdl = data;
827 * Ignore snapshots and ignore failures from non-existant datasets.
829 if (strchr(dataset, '@') != NULL ||
830 (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
833 (void) zfs_unshare_iscsi(zhp);
841 mountpoint_compare(const void *a, const void *b)
843 const char *mounta = *((char **)a);
844 const char *mountb = *((char **)b);
846 return (strcmp(mountb, mounta));
850 * Unshare and unmount all datasets within the given pool. We don't want to
851 * rely on traversing the DSL to discover the filesystems within the pool,
852 * because this may be expensive (if not all of them are mounted), and can fail
853 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
854 * gather all the filesystems that are currently mounted.
856 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
858 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
863 char **mountpoints = NULL;
864 zfs_handle_t **datasets = NULL;
865 libzfs_handle_t *hdl = zhp->zpool_hdl;
868 int flags = (force ? MS_FORCE : 0);
871 * First unshare all zvols.
873 if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
876 namelen = strlen(zhp->zpool_name);
879 if ((n = getmntinfo(&sfs, MNT_WAIT)) == 0) {
880 fprintf(stderr, "getmntinfo(): %s\n", strerror(errno));
883 for (j = 0; j < n; j++) {
885 * Ignore non-ZFS entries.
887 if (strcmp(sfs[j].f_fstypename, MNTTYPE_ZFS) != 0)
891 * Ignore filesystems not within this pool.
893 if (strncmp(sfs[j].f_mntfromname, zhp->zpool_name, namelen) != 0 ||
894 (sfs[j].f_mntfromname[namelen] != '/' &&
895 sfs[j].f_mntfromname[namelen] != '\0'))
899 * At this point we've found a filesystem within our pool. Add
900 * it to our growing list.
904 if ((mountpoints = zfs_alloc(hdl,
905 8 * sizeof (void *))) == NULL)
908 if ((datasets = zfs_alloc(hdl,
909 8 * sizeof (void *))) == NULL)
916 if ((ptr = zfs_realloc(hdl, mountpoints,
917 alloc * sizeof (void *),
918 alloc * 2 * sizeof (void *))) == NULL)
922 if ((ptr = zfs_realloc(hdl, datasets,
923 alloc * sizeof (void *),
924 alloc * 2 * sizeof (void *))) == NULL)
932 if ((mountpoints[used] = zfs_strdup(hdl,
933 sfs[j].f_mntonname)) == NULL)
937 * This is allowed to fail, in case there is some I/O error. It
938 * is only used to determine if we need to remove the underlying
939 * mountpoint, so failure is not fatal.
941 datasets[used] = make_dataset_handle(hdl, sfs[j].f_mntfromname);
947 * At this point, we have the entire list of filesystems, so sort it by
950 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
953 * Walk through and first unshare everything.
955 for (i = 0; i < used; i++) {
956 if (is_shared(hdl, mountpoints[i]) &&
957 unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0)
962 * Now unmount everything, removing the underlying directories as
965 for (i = 0; i < used; i++) {
966 if (unmount_one(hdl, mountpoints[i], flags) != 0)
970 for (i = 0; i < used; i++) {
972 remove_mountpoint(datasets[i]);
977 for (i = 0; i < used; i++) {
979 zfs_close(datasets[i]);
980 free(mountpoints[i]);