xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_mount.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 
547 #pragma init(_zfs_init_libshare)
548 static void
549 _zfs_init_libshare(void)
550 {
551 	void *libshare;
552 	char path[MAXPATHLEN];
553 	char isa[MAXISALEN];
554 
555 #if defined(_LP64)
556 	if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
557 		isa[0] = '\0';
558 #else
559 	isa[0] = '\0';
560 #endif
561 	(void) snprintf(path, MAXPATHLEN,
562 	    "/usr/lib/%s/libshare.so.1", isa);
563 
564 	if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
565 		_sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
566 		_sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
567 		_sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
568 		    dlsym(libshare, "sa_find_share");
569 		_sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
570 		    "sa_enable_share");
571 		_sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
572 		    "sa_disable_share");
573 		_sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
574 		_sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
575 		    dlsym(libshare, "sa_parse_legacy_options");
576 		if (_sa_init == NULL || _sa_fini == NULL ||
577 		    _sa_find_share == NULL || _sa_enable_share == NULL ||
578 		    _sa_disable_share == NULL || _sa_errorstr == NULL ||
579 		    _sa_parse_legacy_options == NULL) {
580 			_sa_init = NULL;
581 			_sa_fini = NULL;
582 			_sa_disable_share = NULL;
583 			_sa_enable_share = NULL;
584 			_sa_errorstr = NULL;
585 			_sa_parse_legacy_options = NULL;
586 			(void) dlclose(libshare);
587 		}
588 	}
589 }
590 
591 /*
592  * zfs_init_libshare(zhandle, service)
593  *
594  * Initialize the libshare API if it hasn't already been initialized.
595  * In all cases it returns 0 if it succeeded and an error if not. The
596  * service value is which part(s) of the API to initialize and is a
597  * direct map to the libshare sa_init(service) interface.
598  */
599 
600 int
601 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
602 {
603 	int ret = SA_OK;
604 
605 	if (_sa_init == NULL)
606 		ret = SA_CONFIG_ERR;
607 
608 	if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
609 		zhandle->libzfs_sharehdl = _sa_init(service);
610 
611 	if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
612 		ret = SA_NO_MEMORY;
613 
614 	return (ret);
615 }
616 
617 /*
618  * zfs_uninit_libshare(zhandle)
619  *
620  * Uninitialize the libshare API if it hasn't already been
621  * uninitialized. It is OK to call multiple times.
622  */
623 
624 void
625 zfs_uninit_libshare(libzfs_handle_t *zhandle)
626 {
627 
628 	if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
629 		if (_sa_fini != NULL)
630 			_sa_fini(zhandle->libzfs_sharehdl);
631 		zhandle->libzfs_sharehdl = NULL;
632 	}
633 }
634 
635 /*
636  * zfs_parse_options(options, proto)
637  *
638  * Call the legacy parse interface to get the protocol specific
639  * options using the NULL arg to indicate that this is a "parse" only.
640  */
641 
642 int
643 zfs_parse_options(char *options, zfs_share_proto_t proto)
644 {
645 	int ret;
646 
647 	if (_sa_parse_legacy_options != NULL)
648 		ret = _sa_parse_legacy_options(NULL, options,
649 		    proto_table[proto].p_name);
650 	else
651 		ret = SA_CONFIG_ERR;
652 	return (ret);
653 }
654 
655 /*
656  * zfs_sa_find_share(handle, path)
657  *
658  * wrapper around sa_find_share to find a share path in the
659  * configuration.
660  */
661 
662 static sa_share_t
663 zfs_sa_find_share(sa_handle_t handle, char *path)
664 {
665 	if (_sa_find_share != NULL)
666 		return (_sa_find_share(handle, path));
667 	return (NULL);
668 }
669 
670 /*
671  * zfs_sa_enable_share(share, proto)
672  *
673  * Wrapper for sa_enable_share which enables a share for a specified
674  * protocol.
675  */
676 
677 static int
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 
692 static int
693 zfs_sa_disable_share(sa_share_t share, char *proto)
694 {
695 	if (_sa_disable_share != NULL)
696 		return (_sa_disable_share(share, proto));
697 	return (SA_CONFIG_ERR);
698 }
699 
700 /*
701  * Share the given filesystem according to the options in the specified
702  * protocol specific properties (sharenfs, sharesmb).  We rely
703  * on "libshare" to the dirty work for us.
704  */
705 
706 static int
707 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
708 {
709 	char mountpoint[ZFS_MAXPROPLEN];
710 	char shareopts[ZFS_MAXPROPLEN];
711 	libzfs_handle_t *hdl = zhp->zfs_hdl;
712 	sa_share_t share;
713 	zfs_share_proto_t *curr_proto;
714 	int ret;
715 
716 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
717 		return (0);
718 
719 	if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
720 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
721 		    dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
722 		    zfs_get_name(zhp), _sa_errorstr(ret));
723 		return (-1);
724 	}
725 
726 	for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
727 		/*
728 		 * Return success if there are no share options.
729 		 */
730 		if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
731 		    shareopts, sizeof (shareopts), NULL, NULL,
732 		    0, B_FALSE) != 0 || strcmp(shareopts, "off") == 0)
733 			continue;
734 
735 		/*
736 		 * If the 'zoned' property is set, then zfs_is_mountable()
737 		 * will have already bailed out if we are in the global zone.
738 		 * But local zones cannot be NFS servers, so we ignore it for
739 		 * local zones as well.
740 		 */
741 		if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
742 			continue;
743 
744 		share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
745 		if (share != NULL) {
746 			int err;
747 			err = zfs_sa_enable_share(share,
748 			    proto_table[*curr_proto].p_name);
749 			if (err != SA_OK) {
750 				(void) zfs_error_fmt(hdl,
751 				    proto_table[*curr_proto].p_share_err,
752 				    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
753 				    zfs_get_name(zhp));
754 				return (-1);
755 			}
756 		} else {
757 			(void) zfs_error_fmt(hdl,
758 			    proto_table[*curr_proto].p_share_err,
759 			    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
760 			    zfs_get_name(zhp));
761 			return (-1);
762 		}
763 
764 	}
765 	return (0);
766 }
767 
768 
769 int
770 zfs_share_nfs(zfs_handle_t *zhp)
771 {
772 	return (zfs_share_proto(zhp, nfs_only));
773 }
774 
775 int
776 zfs_share_smb(zfs_handle_t *zhp)
777 {
778 	return (zfs_share_proto(zhp, smb_only));
779 }
780 
781 int
782 zfs_shareall(zfs_handle_t *zhp)
783 {
784 	return (zfs_share_proto(zhp, share_all_proto));
785 }
786 
787 /*
788  * Unshare a filesystem by mountpoint.
789  */
790 static int
791 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
792     zfs_share_proto_t proto)
793 {
794 	sa_share_t share;
795 	int err;
796 	char *mntpt;
797 	/*
798 	 * Mountpoint could get trashed if libshare calls getmntany
799 	 * which id does during API initialization, so strdup the
800 	 * value.
801 	 */
802 	mntpt = zfs_strdup(hdl, mountpoint);
803 
804 	/* make sure libshare initialized */
805 	if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
806 		free(mntpt);	/* don't need the copy anymore */
807 		return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
808 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
809 		    name, _sa_errorstr(err)));
810 	}
811 
812 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
813 	free(mntpt);	/* don't need the copy anymore */
814 
815 	if (share != NULL) {
816 		err = zfs_sa_disable_share(share, proto_table[proto].p_name);
817 		if (err != SA_OK) {
818 			return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
819 			    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
820 			    name, _sa_errorstr(err)));
821 		}
822 	} else {
823 		return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
824 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
825 		    name));
826 	}
827 	return (0);
828 }
829 
830 /*
831  * Unshare the given filesystem.
832  */
833 int
834 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
835     zfs_share_proto_t *proto)
836 {
837 	struct mnttab search = { 0 }, entry;
838 	char *mntpt = NULL;
839 
840 	/* check to see if need to unmount the filesystem */
841 	search.mnt_special = (char *)zfs_get_name(zhp);
842 	search.mnt_fstype = MNTTYPE_ZFS;
843 	rewind(zhp->zfs_hdl->libzfs_mnttab);
844 	if (mountpoint != NULL)
845 		mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
846 
847 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
848 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
849 		zfs_share_proto_t *curr_proto;
850 
851 		if (mountpoint == NULL)
852 			mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
853 
854 		for (curr_proto = proto; *curr_proto != PROTO_END;
855 		    curr_proto++) {
856 
857 			if (is_shared(zhp->zfs_hdl, mntpt, *curr_proto) &&
858 			    unshare_one(zhp->zfs_hdl, zhp->zfs_name,
859 			    mntpt, *curr_proto) != 0) {
860 				if (mntpt != NULL)
861 					free(mntpt);
862 				return (-1);
863 			}
864 		}
865 	}
866 	if (mntpt != NULL)
867 		free(mntpt);
868 
869 	return (0);
870 }
871 
872 int
873 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
874 {
875 	return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
876 }
877 
878 int
879 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
880 {
881 	return (zfs_unshare_proto(zhp, mountpoint, smb_only));
882 }
883 
884 /*
885  * Same as zfs_unmountall(), but for NFS and SMB unshares.
886  */
887 int
888 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
889 {
890 	prop_changelist_t *clp;
891 	int ret;
892 
893 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0);
894 	if (clp == NULL)
895 		return (-1);
896 
897 	ret = changelist_unshare(clp, proto);
898 	changelist_free(clp);
899 
900 	return (ret);
901 }
902 
903 int
904 zfs_unshareall_nfs(zfs_handle_t *zhp)
905 {
906 	return (zfs_unshareall_proto(zhp, nfs_only));
907 }
908 
909 int
910 zfs_unshareall_smb(zfs_handle_t *zhp)
911 {
912 	return (zfs_unshareall_proto(zhp, smb_only));
913 }
914 
915 int
916 zfs_unshareall(zfs_handle_t *zhp)
917 {
918 	return (zfs_unshareall_proto(zhp, share_all_proto));
919 }
920 
921 int
922 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
923 {
924 	return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
925 }
926 
927 /*
928  * Remove the mountpoint associated with the current dataset, if necessary.
929  * We only remove the underlying directory if:
930  *
931  *	- The mountpoint is not 'none' or 'legacy'
932  *	- The mountpoint is non-empty
933  *	- The mountpoint is the default or inherited
934  *	- The 'zoned' property is set, or we're in a local zone
935  *
936  * Any other directories we leave alone.
937  */
938 void
939 remove_mountpoint(zfs_handle_t *zhp)
940 {
941 	char mountpoint[ZFS_MAXPROPLEN];
942 	zprop_source_t source;
943 
944 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
945 	    &source))
946 		return;
947 
948 	if (source == ZPROP_SRC_DEFAULT ||
949 	    source == ZPROP_SRC_INHERITED) {
950 		/*
951 		 * Try to remove the directory, silently ignoring any errors.
952 		 * The filesystem may have since been removed or moved around,
953 		 * and this error isn't really useful to the administrator in
954 		 * any way.
955 		 */
956 		(void) rmdir(mountpoint);
957 	}
958 }
959 
960 boolean_t
961 zfs_is_shared_iscsi(zfs_handle_t *zhp)
962 {
963 
964 	/*
965 	 * If iscsi deamon isn't running then we aren't shared
966 	 */
967 	if (iscsitgt_svc_online && iscsitgt_svc_online() == 1)
968 		return (B_FALSE);
969 	else
970 		return (iscsitgt_zfs_is_shared != NULL &&
971 		    iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
972 }
973 
974 int
975 zfs_share_iscsi(zfs_handle_t *zhp)
976 {
977 	char shareopts[ZFS_MAXPROPLEN];
978 	const char *dataset = zhp->zfs_name;
979 	libzfs_handle_t *hdl = zhp->zfs_hdl;
980 
981 	/*
982 	 * Return success if there are no share options.
983 	 */
984 	if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
985 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
986 	    strcmp(shareopts, "off") == 0)
987 		return (0);
988 
989 	if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0) {
990 		int error = EZFS_SHAREISCSIFAILED;
991 
992 		/*
993 		 * If service isn't availabele and EPERM was
994 		 * returned then use special error.
995 		 */
996 		if (iscsitgt_svc_online && errno == EPERM &&
997 		    (iscsitgt_svc_online() != 0))
998 			error = EZFS_ISCSISVCUNAVAIL;
999 
1000 		return (zfs_error_fmt(hdl, error,
1001 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
1002 	}
1003 
1004 	return (0);
1005 }
1006 
1007 int
1008 zfs_unshare_iscsi(zfs_handle_t *zhp)
1009 {
1010 	const char *dataset = zfs_get_name(zhp);
1011 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1012 
1013 	/*
1014 	 * Return if the volume is not shared
1015 	 */
1016 	if (zfs_is_shared_iscsi(zhp) != SHARED_ISCSI)
1017 		return (0);
1018 
1019 	/*
1020 	 * If this fails with ENODEV it indicates that zvol wasn't shared so
1021 	 * we should return success in that case.
1022 	 */
1023 	if (iscsitgt_zfs_unshare == NULL ||
1024 	    (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV)) {
1025 		if (errno == EPERM)
1026 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1027 			    "Insufficient privileges to unshare iscsi"));
1028 		return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED,
1029 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
1030 	}
1031 
1032 	return (0);
1033 }
1034 
1035 typedef struct mount_cbdata {
1036 	zfs_handle_t	**cb_datasets;
1037 	int 		cb_used;
1038 	int		cb_alloc;
1039 } mount_cbdata_t;
1040 
1041 static int
1042 mount_cb(zfs_handle_t *zhp, void *data)
1043 {
1044 	mount_cbdata_t *cbp = data;
1045 
1046 	if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
1047 		zfs_close(zhp);
1048 		return (0);
1049 	}
1050 
1051 	if (cbp->cb_alloc == cbp->cb_used) {
1052 		void *ptr;
1053 
1054 		if ((ptr = zfs_realloc(zhp->zfs_hdl,
1055 		    cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
1056 		    cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
1057 			return (-1);
1058 		cbp->cb_datasets = ptr;
1059 
1060 		cbp->cb_alloc *= 2;
1061 	}
1062 
1063 	cbp->cb_datasets[cbp->cb_used++] = zhp;
1064 
1065 	return (zfs_iter_children(zhp, mount_cb, cbp));
1066 }
1067 
1068 static int
1069 dataset_cmp(const void *a, const void *b)
1070 {
1071 	zfs_handle_t **za = (zfs_handle_t **)a;
1072 	zfs_handle_t **zb = (zfs_handle_t **)b;
1073 	char mounta[MAXPATHLEN];
1074 	char mountb[MAXPATHLEN];
1075 	boolean_t gota, gotb;
1076 
1077 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1078 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1079 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1080 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1081 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1082 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1083 
1084 	if (gota && gotb)
1085 		return (strcmp(mounta, mountb));
1086 
1087 	if (gota)
1088 		return (-1);
1089 	if (gotb)
1090 		return (1);
1091 
1092 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1093 }
1094 
1095 /*
1096  * Mount and share all datasets within the given pool.  This assumes that no
1097  * datasets within the pool are currently mounted.  Because users can create
1098  * complicated nested hierarchies of mountpoints, we first gather all the
1099  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
1100  * we have the list of all filesystems, we iterate over them in order and mount
1101  * and/or share each one.
1102  */
1103 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1104 int
1105 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1106 {
1107 	mount_cbdata_t cb = { 0 };
1108 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1109 	zfs_handle_t *zfsp;
1110 	int i, ret = -1;
1111 	int *good;
1112 
1113 	/*
1114 	 * Gather all datasets within the pool.
1115 	 */
1116 	if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
1117 		return (-1);
1118 	cb.cb_alloc = 4;
1119 
1120 	if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1121 		goto out;
1122 
1123 	cb.cb_datasets[0] = zfsp;
1124 	cb.cb_used = 1;
1125 
1126 	if (zfs_iter_children(zfsp, mount_cb, &cb) != 0)
1127 		goto out;
1128 
1129 	/*
1130 	 * Sort the datasets by mountpoint.
1131 	 */
1132 	qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
1133 
1134 	/*
1135 	 * And mount all the datasets, keeping track of which ones
1136 	 * succeeded or failed. By using zfs_alloc(), the good pointer
1137 	 * will always be non-NULL.
1138 	 */
1139 	good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int));
1140 	ret = 0;
1141 	for (i = 0; i < cb.cb_used; i++) {
1142 		if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0)
1143 			ret = -1;
1144 		else
1145 			good[i] = 1;
1146 	}
1147 	/*
1148 	 * Then share all the ones that need to be shared. This needs
1149 	 * to be a separate pass in order to avoid excessive reloading
1150 	 * of the configuration. Good should never be NULL since
1151 	 * zfs_alloc is supposed to exit if memory isn't available.
1152 	 */
1153 	zfs_uninit_libshare(hdl);
1154 	for (i = 0; i < cb.cb_used; i++) {
1155 		if (good[i] && zfs_share(cb.cb_datasets[i]) != 0)
1156 			ret = -1;
1157 	}
1158 
1159 	free(good);
1160 
1161 out:
1162 	for (i = 0; i < cb.cb_used; i++)
1163 		zfs_close(cb.cb_datasets[i]);
1164 	free(cb.cb_datasets);
1165 
1166 	return (ret);
1167 }
1168 
1169 
1170 static int
1171 zvol_cb(const char *dataset, void *data)
1172 {
1173 	libzfs_handle_t *hdl = data;
1174 	zfs_handle_t *zhp;
1175 
1176 	/*
1177 	 * Ignore snapshots and ignore failures from non-existant datasets.
1178 	 */
1179 	if (strchr(dataset, '@') != NULL ||
1180 	    (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
1181 		return (0);
1182 
1183 	if (zfs_unshare_iscsi(zhp) != 0)
1184 		return (-1);
1185 
1186 	zfs_close(zhp);
1187 
1188 	return (0);
1189 }
1190 
1191 static int
1192 mountpoint_compare(const void *a, const void *b)
1193 {
1194 	const char *mounta = *((char **)a);
1195 	const char *mountb = *((char **)b);
1196 
1197 	return (strcmp(mountb, mounta));
1198 }
1199 
1200 /*
1201  * Unshare and unmount all datasets within the given pool.  We don't want to
1202  * rely on traversing the DSL to discover the filesystems within the pool,
1203  * because this may be expensive (if not all of them are mounted), and can fail
1204  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
1205  * gather all the filesystems that are currently mounted.
1206  */
1207 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1208 int
1209 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1210 {
1211 	int used, alloc;
1212 	struct mnttab entry;
1213 	size_t namelen;
1214 	char **mountpoints = NULL;
1215 	zfs_handle_t **datasets = NULL;
1216 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1217 	int i;
1218 	int ret = -1;
1219 	int flags = (force ? MS_FORCE : 0);
1220 
1221 	/*
1222 	 * First unshare all zvols.
1223 	 */
1224 	if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
1225 		return (-1);
1226 
1227 	namelen = strlen(zhp->zpool_name);
1228 
1229 	rewind(hdl->libzfs_mnttab);
1230 	used = alloc = 0;
1231 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1232 		/*
1233 		 * Ignore non-ZFS entries.
1234 		 */
1235 		if (entry.mnt_fstype == NULL ||
1236 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1237 			continue;
1238 
1239 		/*
1240 		 * Ignore filesystems not within this pool.
1241 		 */
1242 		if (entry.mnt_mountp == NULL ||
1243 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1244 		    (entry.mnt_special[namelen] != '/' &&
1245 		    entry.mnt_special[namelen] != '\0'))
1246 			continue;
1247 
1248 		/*
1249 		 * At this point we've found a filesystem within our pool.  Add
1250 		 * it to our growing list.
1251 		 */
1252 		if (used == alloc) {
1253 			if (alloc == 0) {
1254 				if ((mountpoints = zfs_alloc(hdl,
1255 				    8 * sizeof (void *))) == NULL)
1256 					goto out;
1257 
1258 				if ((datasets = zfs_alloc(hdl,
1259 				    8 * sizeof (void *))) == NULL)
1260 					goto out;
1261 
1262 				alloc = 8;
1263 			} else {
1264 				void *ptr;
1265 
1266 				if ((ptr = zfs_realloc(hdl, mountpoints,
1267 				    alloc * sizeof (void *),
1268 				    alloc * 2 * sizeof (void *))) == NULL)
1269 					goto out;
1270 				mountpoints = ptr;
1271 
1272 				if ((ptr = zfs_realloc(hdl, datasets,
1273 				    alloc * sizeof (void *),
1274 				    alloc * 2 * sizeof (void *))) == NULL)
1275 					goto out;
1276 				datasets = ptr;
1277 
1278 				alloc *= 2;
1279 			}
1280 		}
1281 
1282 		if ((mountpoints[used] = zfs_strdup(hdl,
1283 		    entry.mnt_mountp)) == NULL)
1284 			goto out;
1285 
1286 		/*
1287 		 * This is allowed to fail, in case there is some I/O error.  It
1288 		 * is only used to determine if we need to remove the underlying
1289 		 * mountpoint, so failure is not fatal.
1290 		 */
1291 		datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1292 
1293 		used++;
1294 	}
1295 
1296 	/*
1297 	 * At this point, we have the entire list of filesystems, so sort it by
1298 	 * mountpoint.
1299 	 */
1300 	qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1301 
1302 	/*
1303 	 * Walk through and first unshare everything.
1304 	 */
1305 	for (i = 0; i < used; i++) {
1306 		zfs_share_proto_t *curr_proto;
1307 		for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1308 		    curr_proto++) {
1309 			if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1310 			    unshare_one(hdl, mountpoints[i],
1311 			    mountpoints[i], *curr_proto) != 0)
1312 				goto out;
1313 		}
1314 	}
1315 
1316 	/*
1317 	 * Now unmount everything, removing the underlying directories as
1318 	 * appropriate.
1319 	 */
1320 	for (i = 0; i < used; i++) {
1321 		if (unmount_one(hdl, mountpoints[i], flags) != 0)
1322 			goto out;
1323 	}
1324 
1325 	for (i = 0; i < used; i++) {
1326 		if (datasets[i])
1327 			remove_mountpoint(datasets[i]);
1328 	}
1329 
1330 	ret = 0;
1331 out:
1332 	for (i = 0; i < used; i++) {
1333 		if (datasets[i])
1334 			zfs_close(datasets[i]);
1335 		free(mountpoints[i]);
1336 	}
1337 	free(datasets);
1338 	free(mountpoints);
1339 
1340 	return (ret);
1341 }
1342