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