1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
213126Sahl 
22789Sahrens /*
233380Sgw25295  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
29789Sahrens /*
30789Sahrens  * Routines to manage ZFS mounts.  We separate all the nasty routines that have
313126Sahl  * to deal with the OS.  The following functions are the main entry points --
323126Sahl  * they are used by mount and unmount and when changing a filesystem's
333126Sahl  * mountpoint.
34789Sahrens  *
35789Sahrens  * 	zfs_is_mounted()
36789Sahrens  * 	zfs_mount()
37789Sahrens  * 	zfs_unmount()
38789Sahrens  * 	zfs_unmountall()
39789Sahrens  *
403126Sahl  * This file also contains the functions used to manage sharing filesystems via
413126Sahl  * NFS and iSCSI:
42789Sahrens  *
43789Sahrens  * 	zfs_is_shared()
44789Sahrens  * 	zfs_share()
45789Sahrens  * 	zfs_unshare()
463126Sahl  *
473126Sahl  * 	zfs_is_shared_nfs()
483126Sahl  * 	zfs_share_nfs()
493126Sahl  * 	zfs_unshare_nfs()
503126Sahl  * 	zfs_unshareall_nfs()
513126Sahl  * 	zfs_is_shared_iscsi()
523126Sahl  * 	zfs_share_iscsi()
533126Sahl  * 	zfs_unshare_iscsi()
542474Seschrock  *
552474Seschrock  * The following functions are available for pool consumers, and will
563126Sahl  * mount/unmount and share/unshare all datasets within pool:
572474Seschrock  *
583126Sahl  * 	zpool_enable_datasets()
593126Sahl  * 	zpool_disable_datasets()
60789Sahrens  */
61789Sahrens 
62789Sahrens #include <dirent.h>
633134Sahl #include <dlfcn.h>
64789Sahrens #include <errno.h>
65789Sahrens #include <libgen.h>
66789Sahrens #include <libintl.h>
67789Sahrens #include <stdio.h>
68789Sahrens #include <stdlib.h>
69789Sahrens #include <strings.h>
70789Sahrens #include <unistd.h>
71789Sahrens #include <zone.h>
72789Sahrens #include <sys/mntent.h>
73789Sahrens #include <sys/mnttab.h>
74789Sahrens #include <sys/mount.h>
75789Sahrens #include <sys/stat.h>
76789Sahrens 
77789Sahrens #include <libzfs.h>
78789Sahrens 
79789Sahrens #include "libzfs_impl.h"
80789Sahrens 
814180Sdougm #include <libshare.h>
824180Sdougm #include <sys/systeminfo.h>
834180Sdougm #define	MAXISALEN	257	/* based on sysinfo(2) man page */
844180Sdougm 
853134Sahl static int (*iscsitgt_zfs_share)(const char *);
863134Sahl static int (*iscsitgt_zfs_unshare)(const char *);
873134Sahl static int (*iscsitgt_zfs_is_shared)(const char *);
883134Sahl 
893134Sahl #pragma init(zfs_iscsi_init)
903134Sahl static void
913134Sahl zfs_iscsi_init(void)
923134Sahl {
933134Sahl 	void *libiscsitgt;
943134Sahl 
953134Sahl 	if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1",
963134Sahl 	    RTLD_LAZY | RTLD_GLOBAL)) == NULL ||
973134Sahl 	    (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt,
983134Sahl 	    "iscsitgt_zfs_share")) == NULL ||
993134Sahl 	    (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt,
1003134Sahl 	    "iscsitgt_zfs_unshare")) == NULL ||
1013134Sahl 	    (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt,
1023134Sahl 	    "iscsitgt_zfs_is_shared")) == NULL) {
1033134Sahl 		iscsitgt_zfs_share = NULL;
1043134Sahl 		iscsitgt_zfs_unshare = NULL;
1053134Sahl 		iscsitgt_zfs_is_shared = NULL;
1063134Sahl 	}
1073134Sahl }
1083134Sahl 
109789Sahrens /*
1102082Seschrock  * Search the sharetab for the given mountpoint, returning true if it is found.
111789Sahrens  */
1122082Seschrock static boolean_t
1132082Seschrock is_shared(libzfs_handle_t *hdl, const char *mountpoint)
114789Sahrens {
115789Sahrens 	char buf[MAXPATHLEN], *tab;
116789Sahrens 
1172082Seschrock 	if (hdl->libzfs_sharetab == NULL)
118789Sahrens 		return (0);
119789Sahrens 
1202082Seschrock 	(void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
121789Sahrens 
1222082Seschrock 	while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
123789Sahrens 
124789Sahrens 		/* the mountpoint is the first entry on each line */
125789Sahrens 		if ((tab = strchr(buf, '\t')) != NULL) {
126789Sahrens 			*tab = '\0';
127789Sahrens 			if (strcmp(buf, mountpoint) == 0)
1282082Seschrock 				return (B_TRUE);
129789Sahrens 		}
130789Sahrens 	}
131789Sahrens 
1322082Seschrock 	return (B_FALSE);
133789Sahrens }
134789Sahrens 
135789Sahrens /*
1362082Seschrock  * Returns true if the specified directory is empty.  If we can't open the
1372082Seschrock  * directory at all, return true so that the mount can fail with a more
138789Sahrens  * informative error message.
139789Sahrens  */
1402082Seschrock static boolean_t
141789Sahrens dir_is_empty(const char *dirname)
142789Sahrens {
143789Sahrens 	DIR *dirp;
144789Sahrens 	struct dirent64 *dp;
145789Sahrens 
146789Sahrens 	if ((dirp = opendir(dirname)) == NULL)
1472082Seschrock 		return (B_TRUE);
148789Sahrens 
149789Sahrens 	while ((dp = readdir64(dirp)) != NULL) {
150789Sahrens 
151789Sahrens 		if (strcmp(dp->d_name, ".") == 0 ||
152789Sahrens 		    strcmp(dp->d_name, "..") == 0)
153789Sahrens 			continue;
154789Sahrens 
155789Sahrens 		(void) closedir(dirp);
1562082Seschrock 		return (B_FALSE);
157789Sahrens 	}
158789Sahrens 
159789Sahrens 	(void) closedir(dirp);
1602082Seschrock 	return (B_TRUE);
161789Sahrens }
162789Sahrens 
163789Sahrens /*
164789Sahrens  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
165789Sahrens  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
166789Sahrens  * 0.
167789Sahrens  */
1682082Seschrock boolean_t
1693444Sek110237 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
170789Sahrens {
171789Sahrens 	struct mnttab search = { 0 }, entry;
172789Sahrens 
173789Sahrens 	/*
174789Sahrens 	 * Search for the entry in /etc/mnttab.  We don't bother getting the
175789Sahrens 	 * mountpoint, as we can just search for the special device.  This will
176789Sahrens 	 * also let us find mounts when the mountpoint is 'legacy'.
177789Sahrens 	 */
1783444Sek110237 	search.mnt_special = (char *)special;
1791407Snd150628 	search.mnt_fstype = MNTTYPE_ZFS;
180789Sahrens 
1813444Sek110237 	rewind(zfs_hdl->libzfs_mnttab);
1823444Sek110237 	if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0)
1832082Seschrock 		return (B_FALSE);
184789Sahrens 
185789Sahrens 	if (where != NULL)
1863444Sek110237 		*where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
187789Sahrens 
1882082Seschrock 	return (B_TRUE);
189789Sahrens }
190789Sahrens 
1913444Sek110237 boolean_t
1923444Sek110237 zfs_is_mounted(zfs_handle_t *zhp, char **where)
1933444Sek110237 {
1943444Sek110237 	return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
1953444Sek110237 }
1963444Sek110237 
197789Sahrens /*
1982676Seschrock  * Returns true if the given dataset is mountable, false otherwise.  Returns the
1992676Seschrock  * mountpoint in 'buf'.
2002676Seschrock  */
2012676Seschrock static boolean_t
2022676Seschrock zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
2032676Seschrock     zfs_source_t *source)
2042676Seschrock {
2052676Seschrock 	char sourceloc[ZFS_MAXNAMELEN];
2062676Seschrock 	zfs_source_t sourcetype;
2072676Seschrock 
2082676Seschrock 	if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
2092676Seschrock 		return (B_FALSE);
2102676Seschrock 
2112676Seschrock 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
2122676Seschrock 	    &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
2132676Seschrock 
2142676Seschrock 	if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
2152676Seschrock 	    strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
2162676Seschrock 		return (B_FALSE);
2172676Seschrock 
2182676Seschrock 	if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT))
2192676Seschrock 		return (B_FALSE);
2202676Seschrock 
2212676Seschrock 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
2222676Seschrock 	    getzoneid() == GLOBAL_ZONEID)
2232676Seschrock 		return (B_FALSE);
2242676Seschrock 
2252676Seschrock 	if (source)
2262676Seschrock 		*source = sourcetype;
2272676Seschrock 
2282676Seschrock 	return (B_TRUE);
2292676Seschrock }
2302676Seschrock 
2312676Seschrock /*
232789Sahrens  * Mount the given filesystem.
233789Sahrens  */
234789Sahrens int
235789Sahrens zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
236789Sahrens {
237789Sahrens 	struct stat buf;
238789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
239789Sahrens 	char mntopts[MNT_LINE_MAX];
2402082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
241789Sahrens 
242789Sahrens 	if (options == NULL)
243789Sahrens 		mntopts[0] = '\0';
244789Sahrens 	else
245789Sahrens 		(void) strlcpy(mntopts, options, sizeof (mntopts));
246789Sahrens 
2472676Seschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
2482082Seschrock 		return (0);
249789Sahrens 
250789Sahrens 	/* Create the directory if it doesn't already exist */
251789Sahrens 	if (lstat(mountpoint, &buf) != 0) {
252789Sahrens 		if (mkdirp(mountpoint, 0755) != 0) {
2532082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2542082Seschrock 			    "failed to create mountpoint"));
2553237Slling 			return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2562082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
2572082Seschrock 			    mountpoint));
258789Sahrens 		}
259789Sahrens 	}
260789Sahrens 
261789Sahrens 	/*
262789Sahrens 	 * Determine if the mountpoint is empty.  If so, refuse to perform the
263789Sahrens 	 * mount.  We don't perform this check if MS_OVERLAY is specified, which
264789Sahrens 	 * would defeat the point.  We also avoid this check if 'remount' is
265789Sahrens 	 * specified.
266789Sahrens 	 */
267789Sahrens 	if ((flags & MS_OVERLAY) == 0 &&
268789Sahrens 	    strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
269789Sahrens 	    !dir_is_empty(mountpoint)) {
2702082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2712082Seschrock 		    "directory is not empty"));
2723237Slling 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2732082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
274789Sahrens 	}
275789Sahrens 
276789Sahrens 	/* perform the mount */
277789Sahrens 	if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
278789Sahrens 	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
279789Sahrens 		/*
280789Sahrens 		 * Generic errors are nasty, but there are just way too many
281789Sahrens 		 * from mount(), and they're well-understood.  We pick a few
282789Sahrens 		 * common ones to improve upon.
283789Sahrens 		 */
284*4302Sdougm 		if (errno == EBUSY) {
2852082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2862082Seschrock 			    "mountpoint or dataset is busy"));
287*4302Sdougm 		} else {
2882082Seschrock 			zfs_error_aux(hdl, strerror(errno));
289*4302Sdougm 		}
2902082Seschrock 
2913237Slling 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2922082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
2932082Seschrock 		    zhp->zfs_name));
294789Sahrens 	}
295789Sahrens 
296789Sahrens 	return (0);
297789Sahrens }
298789Sahrens 
299789Sahrens /*
3002474Seschrock  * Unmount a single filesystem.
3012474Seschrock  */
3022474Seschrock static int
3032474Seschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
3042474Seschrock {
3052474Seschrock 	if (umount2(mountpoint, flags) != 0) {
3062474Seschrock 		zfs_error_aux(hdl, strerror(errno));
3073237Slling 		return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
3082474Seschrock 		    dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
3092474Seschrock 		    mountpoint));
3102474Seschrock 	}
3112474Seschrock 
3122474Seschrock 	return (0);
3132474Seschrock }
3142474Seschrock 
3152474Seschrock /*
316789Sahrens  * Unmount the given filesystem.
317789Sahrens  */
318789Sahrens int
319789Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
320789Sahrens {
321789Sahrens 	struct mnttab search = { 0 }, entry;
3224180Sdougm 	char *mntpt = NULL;
323789Sahrens 
324789Sahrens 	/* check to see if need to unmount the filesystem */
3252474Seschrock 	search.mnt_special = zhp->zfs_name;
3261407Snd150628 	search.mnt_fstype = MNTTYPE_ZFS;
3272082Seschrock 	rewind(zhp->zfs_hdl->libzfs_mnttab);
328789Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
3292082Seschrock 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
330789Sahrens 
3314180Sdougm 		/*
3324180Sdougm 		 * mountpoint may have come from a call to
3334180Sdougm 		 * getmnt/getmntany if it isn't NULL. If it is NULL,
3344180Sdougm 		 * we know it comes from getmntany which can then get
3354180Sdougm 		 * overwritten later. We strdup it to play it safe.
3364180Sdougm 		 */
337789Sahrens 		if (mountpoint == NULL)
338*4302Sdougm 			mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
3394180Sdougm 		else
340*4302Sdougm 			mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
341789Sahrens 
342789Sahrens 		/*
3432474Seschrock 		 * Unshare and unmount the filesystem
344789Sahrens 		 */
3454180Sdougm 		if (zfs_unshare_nfs(zhp, mntpt) != 0 ||
3464180Sdougm 		    unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) {
3474180Sdougm 			free(mntpt);
348789Sahrens 			return (-1);
3494180Sdougm 		}
3504180Sdougm 		free(mntpt);
351789Sahrens 	}
352789Sahrens 
353789Sahrens 	return (0);
354789Sahrens }
355789Sahrens 
356789Sahrens /*
357789Sahrens  * Unmount this filesystem and any children inheriting the mountpoint property.
358789Sahrens  * To do this, just act like we're changing the mountpoint property, but don't
359789Sahrens  * remount the filesystems afterwards.
360789Sahrens  */
361789Sahrens int
362789Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags)
363789Sahrens {
364789Sahrens 	prop_changelist_t *clp;
365789Sahrens 	int ret;
366789Sahrens 
367789Sahrens 	clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags);
368789Sahrens 	if (clp == NULL)
369789Sahrens 		return (-1);
370789Sahrens 
371789Sahrens 	ret = changelist_prefix(clp);
372789Sahrens 	changelist_free(clp);
373789Sahrens 
374789Sahrens 	return (ret);
375789Sahrens }
376789Sahrens 
3773126Sahl boolean_t
3783126Sahl zfs_is_shared(zfs_handle_t *zhp)
3793126Sahl {
3803126Sahl 	if (ZFS_IS_VOLUME(zhp))
3813126Sahl 		return (zfs_is_shared_iscsi(zhp));
3823126Sahl 
3833126Sahl 	return (zfs_is_shared_nfs(zhp, NULL));
3843126Sahl }
3853126Sahl 
3863126Sahl int
3873126Sahl zfs_share(zfs_handle_t *zhp)
3883126Sahl {
3893126Sahl 	if (ZFS_IS_VOLUME(zhp))
3903126Sahl 		return (zfs_share_iscsi(zhp));
3913126Sahl 
3923126Sahl 	return (zfs_share_nfs(zhp));
3933126Sahl }
3943126Sahl 
3953126Sahl int
3963126Sahl zfs_unshare(zfs_handle_t *zhp)
3973126Sahl {
3983126Sahl 	if (ZFS_IS_VOLUME(zhp))
3993126Sahl 		return (zfs_unshare_iscsi(zhp));
4003126Sahl 
4013126Sahl 	return (zfs_unshare_nfs(zhp, NULL));
4023126Sahl }
4033126Sahl 
404789Sahrens /*
405789Sahrens  * Check to see if the filesystem is currently shared.
406789Sahrens  */
4072082Seschrock boolean_t
4083126Sahl zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
409789Sahrens {
410789Sahrens 	char *mountpoint;
411789Sahrens 
412789Sahrens 	if (!zfs_is_mounted(zhp, &mountpoint))
4132082Seschrock 		return (B_FALSE);
414789Sahrens 
4152082Seschrock 	if (is_shared(zhp->zfs_hdl, mountpoint)) {
416789Sahrens 		if (where != NULL)
417789Sahrens 			*where = mountpoint;
418789Sahrens 		else
419789Sahrens 			free(mountpoint);
4202082Seschrock 		return (B_TRUE);
421789Sahrens 	} else {
422789Sahrens 		free(mountpoint);
4232082Seschrock 		return (B_FALSE);
424789Sahrens 	}
425789Sahrens }
426789Sahrens 
427789Sahrens /*
4284180Sdougm  * Make sure things will work if libshare isn't installed by using
4294180Sdougm  * wrapper functions that check to see that the pointers to functions
4304180Sdougm  * initialized in _zfs_init_libshare() are actually present.
4314180Sdougm  */
4324180Sdougm 
4334180Sdougm static sa_handle_t (*_sa_init)(int);
4344180Sdougm static void (*_sa_fini)(sa_handle_t);
4354180Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
4364180Sdougm static int (*_sa_enable_share)(sa_share_t, char *);
4374180Sdougm static int (*_sa_disable_share)(sa_share_t, char *);
4384180Sdougm static char *(*_sa_errorstr)(int);
4394180Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
4404180Sdougm 
4414180Sdougm /*
4424180Sdougm  * _zfs_init_libshare()
4434180Sdougm  *
4444180Sdougm  * Find the libshare.so.1 entry points that we use here and save the
4454180Sdougm  * values to be used later. This is triggered by the runtime loader.
4464180Sdougm  * Make sure the correct ISA version is loaded.
4474180Sdougm  */
4484180Sdougm 
4494180Sdougm #pragma init(_zfs_init_libshare)
4504180Sdougm static void
4514180Sdougm _zfs_init_libshare(void)
4524180Sdougm {
4534180Sdougm 	void *libshare;
4544180Sdougm 	char path[MAXPATHLEN];
4554180Sdougm 	char isa[MAXISALEN];
4564180Sdougm 
4574180Sdougm #if defined(_LP64)
4584180Sdougm 	if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
459*4302Sdougm 		isa[0] = '\0';
4604180Sdougm #else
4614180Sdougm 	isa[0] = '\0';
4624180Sdougm #endif
4634180Sdougm 	(void) snprintf(path, MAXPATHLEN,
464*4302Sdougm 	    "/usr/lib/%s/libshare.so.1", isa);
4654180Sdougm 
4664180Sdougm 	if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
467*4302Sdougm 		_sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
468*4302Sdougm 		_sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
469*4302Sdougm 		_sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
470*4302Sdougm 		    dlsym(libshare, "sa_find_share");
471*4302Sdougm 		_sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
472*4302Sdougm 		    "sa_enable_share");
473*4302Sdougm 		_sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
474*4302Sdougm 		    "sa_disable_share");
475*4302Sdougm 		_sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
476*4302Sdougm 		_sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
477*4302Sdougm 		    dlsym(libshare, "sa_parse_legacy_options");
4784180Sdougm 	}
4794180Sdougm }
4804180Sdougm 
4814180Sdougm /*
4824180Sdougm  * zfs_init_libshare(zhandle, service)
4834180Sdougm  *
4844180Sdougm  * Initialize the libshare API if it hasn't already been initialized.
4854180Sdougm  * In all cases it returns 0 if it succeeded and an error if not. The
4864180Sdougm  * service value is which part(s) of the API to initialize and is a
4874180Sdougm  * direct map to the libshare sa_init(service) interface.
4884180Sdougm  */
4894180Sdougm 
4904180Sdougm int
4914180Sdougm zfs_init_libshare(libzfs_handle_t *zhandle, int service)
4924180Sdougm {
4934180Sdougm 	int ret = SA_OK;
4944180Sdougm 
495*4302Sdougm 	if (_sa_init == NULL)
496*4302Sdougm 		ret = SA_CONFIG_ERR;
497*4302Sdougm 
498*4302Sdougm 	if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
499*4302Sdougm 		zhandle->libzfs_sharehdl = _sa_init(service);
500*4302Sdougm 
501*4302Sdougm 	if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
502*4302Sdougm 		ret = SA_NO_MEMORY;
503*4302Sdougm 
5044180Sdougm 	return (ret);
5054180Sdougm }
5064180Sdougm 
5074180Sdougm /*
5084180Sdougm  * zfs_uninit_libshare(zhandle)
5094180Sdougm  *
5104180Sdougm  * Uninitialize the libshare API if it hasn't already been
5114180Sdougm  * uninitialized. It is OK to call multiple times.
5124180Sdougm  */
5134180Sdougm 
5144180Sdougm void
5154180Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle)
5164180Sdougm {
5174180Sdougm 
5184180Sdougm 	if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
519*4302Sdougm 		if (_sa_fini != NULL)
520*4302Sdougm 			_sa_fini(zhandle->libzfs_sharehdl);
521*4302Sdougm 		zhandle->libzfs_sharehdl = NULL;
5224180Sdougm 	}
5234180Sdougm }
5244180Sdougm 
5254180Sdougm /*
5264180Sdougm  * zfs_parse_options(options, proto)
5274180Sdougm  *
5284180Sdougm  * Call the legacy parse interface to get the protocol specific
5294180Sdougm  * options using the NULL arg to indicate that this is a "parse" only.
5304180Sdougm  */
5314180Sdougm 
5324180Sdougm int
5334180Sdougm zfs_parse_options(char *options, char *proto)
5344180Sdougm {
5354180Sdougm 	int ret;
5364180Sdougm 
5374180Sdougm 	if (_sa_parse_legacy_options != NULL)
538*4302Sdougm 		ret = _sa_parse_legacy_options(NULL, options, proto);
5394180Sdougm 	else
540*4302Sdougm 		ret = SA_CONFIG_ERR;
5414180Sdougm 	return (ret);
5424180Sdougm }
5434180Sdougm 
5444180Sdougm /*
5454180Sdougm  * zfs_sa_find_share(handle, path)
5464180Sdougm  *
5474180Sdougm  * wrapper around sa_find_share to find a share path in the
5484180Sdougm  * configuration.
5494180Sdougm  */
5504180Sdougm 
5514180Sdougm static sa_share_t
5524180Sdougm zfs_sa_find_share(sa_handle_t handle, char *path)
5534180Sdougm {
5544180Sdougm 	if (_sa_find_share != NULL)
555*4302Sdougm 		return (_sa_find_share(handle, path));
5564180Sdougm 	return (NULL);
5574180Sdougm }
5584180Sdougm 
5594180Sdougm /*
5604180Sdougm  * zfs_sa_enable_share(share, proto)
5614180Sdougm  *
5624180Sdougm  * Wrapper for sa_enable_share which enables a share for a specified
5634180Sdougm  * protocol.
5644180Sdougm  */
5654180Sdougm 
5664180Sdougm static int
5674180Sdougm zfs_sa_enable_share(sa_share_t share, char *proto)
5684180Sdougm {
5694180Sdougm 	if (_sa_enable_share != NULL)
570*4302Sdougm 		return (_sa_enable_share(share, proto));
5714180Sdougm 	return (SA_CONFIG_ERR);
5724180Sdougm }
5734180Sdougm 
5744180Sdougm /*
5754180Sdougm  * zfs_sa_disable_share(share, proto)
5764180Sdougm  *
5774180Sdougm  * Wrapper for sa_enable_share which disables a share for a specified
5784180Sdougm  * protocol.
5794180Sdougm  */
5804180Sdougm 
5814180Sdougm static int
5824180Sdougm zfs_sa_disable_share(sa_share_t share, char *proto)
5834180Sdougm {
5844180Sdougm 	if (_sa_disable_share != NULL)
585*4302Sdougm 		return (_sa_disable_share(share, proto));
5864180Sdougm 	return (SA_CONFIG_ERR);
5874180Sdougm }
5884180Sdougm 
5894180Sdougm /*
590789Sahrens  * Share the given filesystem according to the options in 'sharenfs'.  We rely
5914180Sdougm  * on "libshare" to the dirty work for us.
592789Sahrens  */
5934180Sdougm 
594789Sahrens int
5953126Sahl zfs_share_nfs(zfs_handle_t *zhp)
596789Sahrens {
597789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
598789Sahrens 	char shareopts[ZFS_MAXPROPLEN];
5992082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
6004180Sdougm 	sa_share_t share;
6014180Sdougm 	int ret;
602789Sahrens 
6032676Seschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
604789Sahrens 		return (0);
605789Sahrens 
6063126Sahl 	/*
6073126Sahl 	 * Return success if there are no share options.
6083126Sahl 	 */
609789Sahrens 	if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts),
6102082Seschrock 	    NULL, NULL, 0, B_FALSE) != 0 ||
611789Sahrens 	    strcmp(shareopts, "off") == 0)
612789Sahrens 		return (0);
613789Sahrens 
614789Sahrens 	/*
6152676Seschrock 	 * If the 'zoned' property is set, then zfs_is_mountable() will have
6162676Seschrock 	 * already bailed out if we are in the global zone.  But local
6172676Seschrock 	 * zones cannot be NFS servers, so we ignore it for local zones as well.
618789Sahrens 	 */
6192676Seschrock 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
620789Sahrens 		return (0);
621789Sahrens 
6224180Sdougm 	if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
623*4302Sdougm 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
624*4302Sdougm 		    dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
625*4302Sdougm 		    zfs_get_name(zhp), _sa_errorstr(ret));
626*4302Sdougm 		return (-1);
6274180Sdougm 	}
6284180Sdougm 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
6294180Sdougm 	if (share != NULL) {
630*4302Sdougm 		int err;
631*4302Sdougm 		err = zfs_sa_enable_share(share, "nfs");
632*4302Sdougm 		if (err != SA_OK) {
633*4302Sdougm 			(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
634*4302Sdougm 			    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
635*4302Sdougm 			    zfs_get_name(zhp));
636*4302Sdougm 			return (-1);
637*4302Sdougm 		}
6384180Sdougm 	} else {
6394180Sdougm 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
640*4302Sdougm 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
641*4302Sdougm 		    zfs_get_name(zhp));
642789Sahrens 		return (-1);
643789Sahrens 	}
644789Sahrens 
645789Sahrens 	return (0);
646789Sahrens }
647789Sahrens 
648789Sahrens /*
6492474Seschrock  * Unshare a filesystem by mountpoint.
6502474Seschrock  */
6512474Seschrock static int
6522474Seschrock unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint)
6532474Seschrock {
6544180Sdougm 	sa_share_t share;
6554180Sdougm 	int err;
6564180Sdougm 	char *mntpt;
6572474Seschrock 
6582474Seschrock 	/*
6594180Sdougm 	 * Mountpoint could get trashed if libshare calls getmntany
6604180Sdougm 	 * which id does during API initialization, so strdup the
6614180Sdougm 	 * value.
6622474Seschrock 	 */
6634180Sdougm 	mntpt = zfs_strdup(hdl, mountpoint);
6642474Seschrock 
6654180Sdougm 	/* make sure libshare initialized */
6664180Sdougm 	if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
6674180Sdougm 		free(mntpt);	/* don't need the copy anymore */
6684180Sdougm 		return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
669*4302Sdougm 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
670*4302Sdougm 		    name, _sa_errorstr(err)));
6712474Seschrock 	}
6722474Seschrock 
6734180Sdougm 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
6744180Sdougm 	free(mntpt);	/* don't need the copy anymore */
6752474Seschrock 
6764180Sdougm 	if (share != NULL) {
6774180Sdougm 		err = zfs_sa_disable_share(share, "nfs");
6784180Sdougm 		if (err != SA_OK) {
679*4302Sdougm 			return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
680*4302Sdougm 			    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
681*4302Sdougm 			    name, _sa_errorstr(err)));
6824180Sdougm 		}
6834180Sdougm 	} else {
6844180Sdougm 		return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
685*4302Sdougm 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
686*4302Sdougm 		    name));
6874180Sdougm 	}
6882474Seschrock 	return (0);
6892474Seschrock }
6902474Seschrock 
6912474Seschrock /*
692789Sahrens  * Unshare the given filesystem.
693789Sahrens  */
694789Sahrens int
6953126Sahl zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
696789Sahrens {
697789Sahrens 	struct mnttab search = { 0 }, entry;
6984180Sdougm 	char *mntpt = NULL;
699789Sahrens 
700789Sahrens 	/* check to see if need to unmount the filesystem */
701789Sahrens 	search.mnt_special = (char *)zfs_get_name(zhp);
7021407Snd150628 	search.mnt_fstype = MNTTYPE_ZFS;
7032082Seschrock 	rewind(zhp->zfs_hdl->libzfs_mnttab);
704*4302Sdougm 	if (mountpoint != NULL)
705*4302Sdougm 		mountpoint = mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
706*4302Sdougm 
707789Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
7082082Seschrock 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
709789Sahrens 
710789Sahrens 		if (mountpoint == NULL)
711789Sahrens 			mountpoint = entry.mnt_mountp;
712789Sahrens 
7132474Seschrock 		if (is_shared(zhp->zfs_hdl, mountpoint) &&
7144180Sdougm 		    unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0) {
7154180Sdougm 			if (mntpt != NULL)
7164180Sdougm 				free(mntpt);
7172474Seschrock 			return (-1);
7184180Sdougm 		}
719789Sahrens 	}
7204180Sdougm 	if (mntpt != NULL)
721*4302Sdougm 		free(mntpt);
722789Sahrens 
723789Sahrens 	return (0);
724789Sahrens }
725789Sahrens 
726789Sahrens /*
7273126Sahl  * Same as zfs_unmountall(), but for NFS unshares.
728789Sahrens  */
729789Sahrens int
7303126Sahl zfs_unshareall_nfs(zfs_handle_t *zhp)
731789Sahrens {
732789Sahrens 	prop_changelist_t *clp;
733789Sahrens 	int ret;
734789Sahrens 
735789Sahrens 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0);
736789Sahrens 	if (clp == NULL)
737789Sahrens 		return (-1);
738789Sahrens 
739789Sahrens 	ret = changelist_unshare(clp);
740789Sahrens 	changelist_free(clp);
741789Sahrens 
742789Sahrens 	return (ret);
743789Sahrens }
744789Sahrens 
745789Sahrens /*
746789Sahrens  * Remove the mountpoint associated with the current dataset, if necessary.
747789Sahrens  * We only remove the underlying directory if:
748789Sahrens  *
749789Sahrens  *	- The mountpoint is not 'none' or 'legacy'
750789Sahrens  *	- The mountpoint is non-empty
751789Sahrens  *	- The mountpoint is the default or inherited
752789Sahrens  *	- The 'zoned' property is set, or we're in a local zone
753789Sahrens  *
754789Sahrens  * Any other directories we leave alone.
755789Sahrens  */
756789Sahrens void
757789Sahrens remove_mountpoint(zfs_handle_t *zhp)
758789Sahrens {
759789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
7602676Seschrock 	zfs_source_t source;
761789Sahrens 
7622676Seschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
7632676Seschrock 	    &source))
764789Sahrens 		return;
765789Sahrens 
7662676Seschrock 	if (source == ZFS_SRC_DEFAULT ||
7672676Seschrock 	    source == ZFS_SRC_INHERITED) {
768789Sahrens 		/*
769789Sahrens 		 * Try to remove the directory, silently ignoring any errors.
770789Sahrens 		 * The filesystem may have since been removed or moved around,
7712676Seschrock 		 * and this error isn't really useful to the administrator in
7722676Seschrock 		 * any way.
773789Sahrens 		 */
774789Sahrens 		(void) rmdir(mountpoint);
775789Sahrens 	}
776789Sahrens }
7772474Seschrock 
7783126Sahl boolean_t
7793126Sahl zfs_is_shared_iscsi(zfs_handle_t *zhp)
7803126Sahl {
7813134Sahl 	return (iscsitgt_zfs_is_shared != NULL &&
7823134Sahl 	    iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
7833126Sahl }
7843126Sahl 
7853126Sahl int
7863126Sahl zfs_share_iscsi(zfs_handle_t *zhp)
7873126Sahl {
7883126Sahl 	char shareopts[ZFS_MAXPROPLEN];
7893126Sahl 	const char *dataset = zhp->zfs_name;
7903126Sahl 	libzfs_handle_t *hdl = zhp->zfs_hdl;
7913126Sahl 
7923126Sahl 	/*
7933126Sahl 	 * Return success if there are no share options.
7943126Sahl 	 */
7953126Sahl 	if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
7963126Sahl 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
7973126Sahl 	    strcmp(shareopts, "off") == 0)
7983126Sahl 		return (0);
7993126Sahl 
8003134Sahl 	if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0)
8013237Slling 		return (zfs_error_fmt(hdl, EZFS_SHAREISCSIFAILED,
8023126Sahl 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
8033126Sahl 
8043126Sahl 	return (0);
8053126Sahl }
8063126Sahl 
8073126Sahl int
8083126Sahl zfs_unshare_iscsi(zfs_handle_t *zhp)
8093126Sahl {
8103126Sahl 	const char *dataset = zfs_get_name(zhp);
8113126Sahl 	libzfs_handle_t *hdl = zhp->zfs_hdl;
8123126Sahl 
8133126Sahl 	/*
8143380Sgw25295 	 * Return if the volume is not shared
8153380Sgw25295 	 */
8163380Sgw25295 	if (!zfs_is_shared_iscsi(zhp))
8173380Sgw25295 		return (0);
8183380Sgw25295 
8193380Sgw25295 	/*
8203126Sahl 	 * If this fails with ENODEV it indicates that zvol wasn't shared so
8213126Sahl 	 * we should return success in that case.
8223126Sahl 	 */
8233134Sahl 	if (iscsitgt_zfs_unshare == NULL ||
8243134Sahl 	    (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV))
8253237Slling 		return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED,
8263126Sahl 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
8273126Sahl 
8283126Sahl 	return (0);
8293126Sahl }
8303126Sahl 
8312474Seschrock typedef struct mount_cbdata {
8322474Seschrock 	zfs_handle_t	**cb_datasets;
8332474Seschrock 	int 		cb_used;
8342474Seschrock 	int		cb_alloc;
8352474Seschrock } mount_cbdata_t;
8362474Seschrock 
8372474Seschrock static int
8382474Seschrock mount_cb(zfs_handle_t *zhp, void *data)
8392474Seschrock {
8402474Seschrock 	mount_cbdata_t *cbp = data;
8412474Seschrock 
8423126Sahl 	if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
8432474Seschrock 		zfs_close(zhp);
8442474Seschrock 		return (0);
8452474Seschrock 	}
8462474Seschrock 
8472474Seschrock 	if (cbp->cb_alloc == cbp->cb_used) {
8482676Seschrock 		void *ptr;
8492474Seschrock 
8502676Seschrock 		if ((ptr = zfs_realloc(zhp->zfs_hdl,
8512676Seschrock 		    cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
8522676Seschrock 		    cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
8532474Seschrock 			return (-1);
8542676Seschrock 		cbp->cb_datasets = ptr;
8552474Seschrock 
8562676Seschrock 		cbp->cb_alloc *= 2;
8572474Seschrock 	}
8582474Seschrock 
8592474Seschrock 	cbp->cb_datasets[cbp->cb_used++] = zhp;
8603126Sahl 
8613126Sahl 	return (zfs_iter_children(zhp, mount_cb, cbp));
8622474Seschrock }
8632474Seschrock 
8642474Seschrock static int
8653126Sahl dataset_cmp(const void *a, const void *b)
8662474Seschrock {
8672474Seschrock 	zfs_handle_t **za = (zfs_handle_t **)a;
8682474Seschrock 	zfs_handle_t **zb = (zfs_handle_t **)b;
8692474Seschrock 	char mounta[MAXPATHLEN];
8702474Seschrock 	char mountb[MAXPATHLEN];
8713126Sahl 	boolean_t gota, gotb;
8722474Seschrock 
8733126Sahl 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
8743126Sahl 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
8753126Sahl 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
8763126Sahl 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
8773126Sahl 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
8783126Sahl 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
8792474Seschrock 
8803126Sahl 	if (gota && gotb)
8813126Sahl 		return (strcmp(mounta, mountb));
8823126Sahl 
8833126Sahl 	if (gota)
8843126Sahl 		return (-1);
8853126Sahl 	if (gotb)
8863126Sahl 		return (1);
8873126Sahl 
8883126Sahl 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
8892474Seschrock }
8902474Seschrock 
8913126Sahl /*
8923126Sahl  * Mount and share all datasets within the given pool.  This assumes that no
8933126Sahl  * datasets within the pool are currently mounted.  Because users can create
8943126Sahl  * complicated nested hierarchies of mountpoints, we first gather all the
8953126Sahl  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
8963126Sahl  * we have the list of all filesystems, we iterate over them in order and mount
8973126Sahl  * and/or share each one.
8983126Sahl  */
8993126Sahl #pragma weak zpool_mount_datasets = zpool_enable_datasets
9002474Seschrock int
9013126Sahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
9022474Seschrock {
9032474Seschrock 	mount_cbdata_t cb = { 0 };
9042474Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9052474Seschrock 	zfs_handle_t *zfsp;
9062474Seschrock 	int i, ret = -1;
9074180Sdougm 	int *good;
9082474Seschrock 
9092474Seschrock 	/*
9102474Seschrock 	 * Gather all datasets within the pool.
9112474Seschrock 	 */
9122474Seschrock 	if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
9132474Seschrock 		return (-1);
9142474Seschrock 	cb.cb_alloc = 4;
9152474Seschrock 
9162474Seschrock 	if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_ANY)) == NULL)
9172474Seschrock 		goto out;
9182474Seschrock 
9192474Seschrock 	cb.cb_datasets[0] = zfsp;
9202474Seschrock 	cb.cb_used = 1;
9212474Seschrock 
9222474Seschrock 	if (zfs_iter_children(zfsp, mount_cb, &cb) != 0)
9232474Seschrock 		goto out;
9242474Seschrock 
9252474Seschrock 	/*
9262474Seschrock 	 * Sort the datasets by mountpoint.
9272474Seschrock 	 */
9283126Sahl 	qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
9292474Seschrock 
9302474Seschrock 	/*
9314180Sdougm 	 * And mount all the datasets, keeping track of which ones
9324180Sdougm 	 * succeeded or failed. By using zfs_alloc(), the good pointer
9334180Sdougm 	 * will always be non-NULL.
9342474Seschrock 	 */
9354180Sdougm 	good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int));
9362474Seschrock 	ret = 0;
9372474Seschrock 	for (i = 0; i < cb.cb_used; i++) {
938*4302Sdougm 		if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0)
9394180Sdougm 			ret = -1;
940*4302Sdougm 		else
9414180Sdougm 			good[i] = 1;
9424180Sdougm 	}
9434180Sdougm 	/*
9444180Sdougm 	 * Then share all the ones that need to be shared. This needs
9454180Sdougm 	 * to be a separate pass in order to avoid excessive reloading
9464180Sdougm 	 * of the configuration. Good should never be NULL since
9474180Sdougm 	 * zfs_alloc is supposed to exit if memory isn't available.
9484180Sdougm 	 */
9494180Sdougm 	zfs_uninit_libshare(hdl);
9504180Sdougm 	for (i = 0; i < cb.cb_used; i++) {
9514180Sdougm 		if (good[i] && zfs_share(cb.cb_datasets[i]) != 0)
9522474Seschrock 			ret = -1;
9532474Seschrock 	}
9542474Seschrock 
9554180Sdougm 	free(good);
9564180Sdougm 
9572474Seschrock out:
9582474Seschrock 	for (i = 0; i < cb.cb_used; i++)
9592474Seschrock 		zfs_close(cb.cb_datasets[i]);
9602474Seschrock 	free(cb.cb_datasets);
9612474Seschrock 
9622474Seschrock 	return (ret);
9632474Seschrock }
9642474Seschrock 
9653126Sahl 
9663126Sahl static int
9673126Sahl zvol_cb(const char *dataset, void *data)
9683126Sahl {
9693126Sahl 	libzfs_handle_t *hdl = data;
9703126Sahl 	zfs_handle_t *zhp;
9713126Sahl 
9723126Sahl 	/*
9733126Sahl 	 * Ignore snapshots and ignore failures from non-existant datasets.
9743126Sahl 	 */
9753126Sahl 	if (strchr(dataset, '@') != NULL ||
9763126Sahl 	    (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
9773126Sahl 		return (0);
9783126Sahl 
9793126Sahl 	(void) zfs_unshare_iscsi(zhp);
9803126Sahl 
9813126Sahl 	zfs_close(zhp);
9823126Sahl 
9833126Sahl 	return (0);
9843126Sahl }
9853126Sahl 
9862474Seschrock static int
9872474Seschrock mountpoint_compare(const void *a, const void *b)
9882474Seschrock {
9892474Seschrock 	const char *mounta = *((char **)a);
9902474Seschrock 	const char *mountb = *((char **)b);
9912474Seschrock 
9922474Seschrock 	return (strcmp(mountb, mounta));
9932474Seschrock }
9942474Seschrock 
9953126Sahl /*
9963126Sahl  * Unshare and unmount all datasets within the given pool.  We don't want to
9973126Sahl  * rely on traversing the DSL to discover the filesystems within the pool,
9983126Sahl  * because this may be expensive (if not all of them are mounted), and can fail
9993126Sahl  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
10003126Sahl  * gather all the filesystems that are currently mounted.
10013126Sahl  */
10023126Sahl #pragma weak zpool_unmount_datasets = zpool_disable_datasets
10032474Seschrock int
10043126Sahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
10052474Seschrock {
10062474Seschrock 	int used, alloc;
10072474Seschrock 	struct mnttab entry;
10082474Seschrock 	size_t namelen;
10092474Seschrock 	char **mountpoints = NULL;
10102474Seschrock 	zfs_handle_t **datasets = NULL;
10112474Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10122474Seschrock 	int i;
10132474Seschrock 	int ret = -1;
10142474Seschrock 	int flags = (force ? MS_FORCE : 0);
10152474Seschrock 
10163126Sahl 	/*
10173126Sahl 	 * First unshare all zvols.
10183126Sahl 	 */
10193126Sahl 	if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
10203126Sahl 		return (-1);
10213126Sahl 
10222474Seschrock 	namelen = strlen(zhp->zpool_name);
10232474Seschrock 
10242474Seschrock 	rewind(hdl->libzfs_mnttab);
10252474Seschrock 	used = alloc = 0;
10262474Seschrock 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
10272474Seschrock 		/*
10282474Seschrock 		 * Ignore non-ZFS entries.
10292474Seschrock 		 */
10302474Seschrock 		if (entry.mnt_fstype == NULL ||
10312474Seschrock 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
10322474Seschrock 			continue;
10332474Seschrock 
10342474Seschrock 		/*
10352474Seschrock 		 * Ignore filesystems not within this pool.
10362474Seschrock 		 */
10372474Seschrock 		if (entry.mnt_mountp == NULL ||
10382474Seschrock 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
10392474Seschrock 		    (entry.mnt_special[namelen] != '/' &&
10402474Seschrock 		    entry.mnt_special[namelen] != '\0'))
10412474Seschrock 			continue;
10422474Seschrock 
10432474Seschrock 		/*
10442474Seschrock 		 * At this point we've found a filesystem within our pool.  Add
10452474Seschrock 		 * it to our growing list.
10462474Seschrock 		 */
10472474Seschrock 		if (used == alloc) {
10482474Seschrock 			if (alloc == 0) {
10492474Seschrock 				if ((mountpoints = zfs_alloc(hdl,
10502474Seschrock 				    8 * sizeof (void *))) == NULL)
10512474Seschrock 					goto out;
10522474Seschrock 
10532474Seschrock 				if ((datasets = zfs_alloc(hdl,
10542474Seschrock 				    8 * sizeof (void *))) == NULL)
10552474Seschrock 					goto out;
10562474Seschrock 
10572474Seschrock 				alloc = 8;
10582474Seschrock 			} else {
10592676Seschrock 				void *ptr;
10602474Seschrock 
10612676Seschrock 				if ((ptr = zfs_realloc(hdl, mountpoints,
10622676Seschrock 				    alloc * sizeof (void *),
10632474Seschrock 				    alloc * 2 * sizeof (void *))) == NULL)
10642474Seschrock 					goto out;
10652676Seschrock 				mountpoints = ptr;
10662474Seschrock 
10672676Seschrock 				if ((ptr = zfs_realloc(hdl, datasets,
10682676Seschrock 				    alloc * sizeof (void *),
10692474Seschrock 				    alloc * 2 * sizeof (void *))) == NULL)
10702474Seschrock 					goto out;
10712676Seschrock 				datasets = ptr;
10722474Seschrock 
10732474Seschrock 				alloc *= 2;
10742474Seschrock 			}
10752474Seschrock 		}
10762474Seschrock 
10772474Seschrock 		if ((mountpoints[used] = zfs_strdup(hdl,
10782474Seschrock 		    entry.mnt_mountp)) == NULL)
10792474Seschrock 			goto out;
10802474Seschrock 
10812474Seschrock 		/*
10822474Seschrock 		 * This is allowed to fail, in case there is some I/O error.  It
10832474Seschrock 		 * is only used to determine if we need to remove the underlying
10842474Seschrock 		 * mountpoint, so failure is not fatal.
10852474Seschrock 		 */
10862474Seschrock 		datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
10872474Seschrock 
10882474Seschrock 		used++;
10892474Seschrock 	}
10902474Seschrock 
10912474Seschrock 	/*
10922474Seschrock 	 * At this point, we have the entire list of filesystems, so sort it by
10932474Seschrock 	 * mountpoint.
10942474Seschrock 	 */
10952474Seschrock 	qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
10962474Seschrock 
10972474Seschrock 	/*
10982474Seschrock 	 * Walk through and first unshare everything.
10992474Seschrock 	 */
11002474Seschrock 	for (i = 0; i < used; i++) {
11012474Seschrock 		if (is_shared(hdl, mountpoints[i]) &&
11022676Seschrock 		    unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0)
11032474Seschrock 			goto out;
11042474Seschrock 	}
11052474Seschrock 
11062474Seschrock 	/*
11072474Seschrock 	 * Now unmount everything, removing the underlying directories as
11082474Seschrock 	 * appropriate.
11092474Seschrock 	 */
11102474Seschrock 	for (i = 0; i < used; i++) {
11112474Seschrock 		if (unmount_one(hdl, mountpoints[i], flags) != 0)
11122474Seschrock 			goto out;
11132676Seschrock 	}
11142474Seschrock 
11152676Seschrock 	for (i = 0; i < used; i++) {
11162474Seschrock 		if (datasets[i])
11172474Seschrock 			remove_mountpoint(datasets[i]);
11182474Seschrock 	}
11192474Seschrock 
11202474Seschrock 	ret = 0;
11212474Seschrock out:
11222474Seschrock 	for (i = 0; i < used; i++) {
11232474Seschrock 		if (datasets[i])
11242474Seschrock 			zfs_close(datasets[i]);
11252474Seschrock 		free(mountpoints[i]);
11262474Seschrock 	}
11272474Seschrock 	free(datasets);
11282474Seschrock 	free(mountpoints);
11292474Seschrock 
11302474Seschrock 	return (ret);
11312474Seschrock }
1132