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