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