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