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 
81*4180Sdougm #include <libshare.h>
82*4180Sdougm #include <sys/systeminfo.h>
83*4180Sdougm #define	MAXISALEN	257	/* based on sysinfo(2) man page */
84*4180Sdougm 
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 		 */
2842082Seschrock 		if (errno == EBUSY)
2852082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2862082Seschrock 			    "mountpoint or dataset is busy"));
2872082Seschrock 		else
2882082Seschrock 			zfs_error_aux(hdl, strerror(errno));
2892082Seschrock 
2903237Slling 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2912082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
2922082Seschrock 		    zhp->zfs_name));
293789Sahrens 	}
294789Sahrens 
295789Sahrens 	return (0);
296789Sahrens }
297789Sahrens 
298789Sahrens /*
2992474Seschrock  * Unmount a single filesystem.
3002474Seschrock  */
3012474Seschrock static int
3022474Seschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
3032474Seschrock {
3042474Seschrock 	if (umount2(mountpoint, flags) != 0) {
3052474Seschrock 		zfs_error_aux(hdl, strerror(errno));
3063237Slling 		return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
3072474Seschrock 		    dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
3082474Seschrock 		    mountpoint));
3092474Seschrock 	}
3102474Seschrock 
3112474Seschrock 	return (0);
3122474Seschrock }
3132474Seschrock 
3142474Seschrock /*
315789Sahrens  * Unmount the given filesystem.
316789Sahrens  */
317789Sahrens int
318789Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
319789Sahrens {
320789Sahrens 	struct mnttab search = { 0 }, entry;
321*4180Sdougm 	char *mntpt = NULL;
322789Sahrens 
323789Sahrens 	/* check to see if need to unmount the filesystem */
3242474Seschrock 	search.mnt_special = zhp->zfs_name;
3251407Snd150628 	search.mnt_fstype = MNTTYPE_ZFS;
3262082Seschrock 	rewind(zhp->zfs_hdl->libzfs_mnttab);
327789Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
3282082Seschrock 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
329789Sahrens 
330*4180Sdougm 		/*
331*4180Sdougm 		 * mountpoint may have come from a call to
332*4180Sdougm 		 * getmnt/getmntany if it isn't NULL. If it is NULL,
333*4180Sdougm 		 * we know it comes from getmntany which can then get
334*4180Sdougm 		 * overwritten later. We strdup it to play it safe.
335*4180Sdougm 		 */
336789Sahrens 		if (mountpoint == NULL)
337*4180Sdougm 		    mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
338*4180Sdougm 		else
339*4180Sdougm 		    mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
340789Sahrens 
341789Sahrens 		/*
3422474Seschrock 		 * Unshare and unmount the filesystem
343789Sahrens 		 */
344*4180Sdougm 		if (zfs_unshare_nfs(zhp, mntpt) != 0 ||
345*4180Sdougm 		    unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) {
346*4180Sdougm 			free(mntpt);
347789Sahrens 			return (-1);
348*4180Sdougm 		}
349*4180Sdougm 		free(mntpt);
350789Sahrens 	}
351789Sahrens 
352789Sahrens 	return (0);
353789Sahrens }
354789Sahrens 
355789Sahrens /*
356789Sahrens  * Unmount this filesystem and any children inheriting the mountpoint property.
357789Sahrens  * To do this, just act like we're changing the mountpoint property, but don't
358789Sahrens  * remount the filesystems afterwards.
359789Sahrens  */
360789Sahrens int
361789Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags)
362789Sahrens {
363789Sahrens 	prop_changelist_t *clp;
364789Sahrens 	int ret;
365789Sahrens 
366789Sahrens 	clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags);
367789Sahrens 	if (clp == NULL)
368789Sahrens 		return (-1);
369789Sahrens 
370789Sahrens 	ret = changelist_prefix(clp);
371789Sahrens 	changelist_free(clp);
372789Sahrens 
373789Sahrens 	return (ret);
374789Sahrens }
375789Sahrens 
3763126Sahl boolean_t
3773126Sahl zfs_is_shared(zfs_handle_t *zhp)
3783126Sahl {
3793126Sahl 	if (ZFS_IS_VOLUME(zhp))
3803126Sahl 		return (zfs_is_shared_iscsi(zhp));
3813126Sahl 
3823126Sahl 	return (zfs_is_shared_nfs(zhp, NULL));
3833126Sahl }
3843126Sahl 
3853126Sahl int
3863126Sahl zfs_share(zfs_handle_t *zhp)
3873126Sahl {
3883126Sahl 	if (ZFS_IS_VOLUME(zhp))
3893126Sahl 		return (zfs_share_iscsi(zhp));
3903126Sahl 
3913126Sahl 	return (zfs_share_nfs(zhp));
3923126Sahl }
3933126Sahl 
3943126Sahl int
3953126Sahl zfs_unshare(zfs_handle_t *zhp)
3963126Sahl {
3973126Sahl 	if (ZFS_IS_VOLUME(zhp))
3983126Sahl 		return (zfs_unshare_iscsi(zhp));
3993126Sahl 
4003126Sahl 	return (zfs_unshare_nfs(zhp, NULL));
4013126Sahl }
4023126Sahl 
403789Sahrens /*
404789Sahrens  * Check to see if the filesystem is currently shared.
405789Sahrens  */
4062082Seschrock boolean_t
4073126Sahl zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
408789Sahrens {
409789Sahrens 	char *mountpoint;
410789Sahrens 
411789Sahrens 	if (!zfs_is_mounted(zhp, &mountpoint))
4122082Seschrock 		return (B_FALSE);
413789Sahrens 
4142082Seschrock 	if (is_shared(zhp->zfs_hdl, mountpoint)) {
415789Sahrens 		if (where != NULL)
416789Sahrens 			*where = mountpoint;
417789Sahrens 		else
418789Sahrens 			free(mountpoint);
4192082Seschrock 		return (B_TRUE);
420789Sahrens 	} else {
421789Sahrens 		free(mountpoint);
4222082Seschrock 		return (B_FALSE);
423789Sahrens 	}
424789Sahrens }
425789Sahrens 
426789Sahrens /*
427*4180Sdougm  * Make sure things will work if libshare isn't installed by using
428*4180Sdougm  * wrapper functions that check to see that the pointers to functions
429*4180Sdougm  * initialized in _zfs_init_libshare() are actually present.
430*4180Sdougm  */
431*4180Sdougm 
432*4180Sdougm static sa_handle_t (*_sa_init)(int);
433*4180Sdougm static void (*_sa_fini)(sa_handle_t);
434*4180Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
435*4180Sdougm static int (*_sa_enable_share)(sa_share_t, char *);
436*4180Sdougm static int (*_sa_disable_share)(sa_share_t, char *);
437*4180Sdougm static char *(*_sa_errorstr)(int);
438*4180Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
439*4180Sdougm 
440*4180Sdougm /*
441*4180Sdougm  * _zfs_init_libshare()
442*4180Sdougm  *
443*4180Sdougm  * Find the libshare.so.1 entry points that we use here and save the
444*4180Sdougm  * values to be used later. This is triggered by the runtime loader.
445*4180Sdougm  * Make sure the correct ISA version is loaded.
446*4180Sdougm  */
447*4180Sdougm 
448*4180Sdougm #pragma init(_zfs_init_libshare)
449*4180Sdougm static void
450*4180Sdougm _zfs_init_libshare(void)
451*4180Sdougm {
452*4180Sdougm 	void *libshare;
453*4180Sdougm 	char path[MAXPATHLEN];
454*4180Sdougm 	char isa[MAXISALEN];
455*4180Sdougm 
456*4180Sdougm #if defined(_LP64)
457*4180Sdougm 	if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
458*4180Sdougm 	    isa[0] = '\0';
459*4180Sdougm #else
460*4180Sdougm 	isa[0] = '\0';
461*4180Sdougm #endif
462*4180Sdougm 	(void) snprintf(path, MAXPATHLEN,
463*4180Sdougm 			"/usr/lib/%s/libshare.so.1", isa);
464*4180Sdougm 
465*4180Sdougm 	if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
466*4180Sdougm 	    _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
467*4180Sdougm 	    _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
468*4180Sdougm 	    _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
469*4180Sdougm 					dlsym(libshare, "sa_find_share");
470*4180Sdougm 	    _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
471*4180Sdougm 					"sa_enable_share");
472*4180Sdougm 	    _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
473*4180Sdougm 					"sa_disable_share");
474*4180Sdougm 	    _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
475*4180Sdougm 	    _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
476*4180Sdougm 				dlsym(libshare, "sa_parse_legacy_options");
477*4180Sdougm 	}
478*4180Sdougm }
479*4180Sdougm 
480*4180Sdougm /*
481*4180Sdougm  * zfs_init_libshare(zhandle, service)
482*4180Sdougm  *
483*4180Sdougm  * Initialize the libshare API if it hasn't already been initialized.
484*4180Sdougm  * In all cases it returns 0 if it succeeded and an error if not. The
485*4180Sdougm  * service value is which part(s) of the API to initialize and is a
486*4180Sdougm  * direct map to the libshare sa_init(service) interface.
487*4180Sdougm  */
488*4180Sdougm 
489*4180Sdougm int
490*4180Sdougm zfs_init_libshare(libzfs_handle_t *zhandle, int service)
491*4180Sdougm {
492*4180Sdougm 	int ret = SA_OK;
493*4180Sdougm 
494*4180Sdougm 	if (_sa_init == NULL) {
495*4180Sdougm 	    ret = SA_CONFIG_ERR;
496*4180Sdougm 	}
497*4180Sdougm 	if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL) {
498*4180Sdougm 	    zhandle->libzfs_sharehdl = _sa_init(service);
499*4180Sdougm 	}
500*4180Sdougm 	if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL) {
501*4180Sdougm 	    ret = SA_NO_MEMORY;
502*4180Sdougm 	}
503*4180Sdougm 	return (ret);
504*4180Sdougm }
505*4180Sdougm 
506*4180Sdougm /*
507*4180Sdougm  * zfs_uninit_libshare(zhandle)
508*4180Sdougm  *
509*4180Sdougm  * Uninitialize the libshare API if it hasn't already been
510*4180Sdougm  * uninitialized. It is OK to call multiple times.
511*4180Sdougm  */
512*4180Sdougm 
513*4180Sdougm void
514*4180Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle)
515*4180Sdougm {
516*4180Sdougm 
517*4180Sdougm 	if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
518*4180Sdougm 	    if (_sa_fini != NULL)
519*4180Sdougm 		_sa_fini(zhandle->libzfs_sharehdl);
520*4180Sdougm 	    zhandle->libzfs_sharehdl = NULL;
521*4180Sdougm 	}
522*4180Sdougm }
523*4180Sdougm 
524*4180Sdougm /*
525*4180Sdougm  * zfs_parse_options(options, proto)
526*4180Sdougm  *
527*4180Sdougm  * Call the legacy parse interface to get the protocol specific
528*4180Sdougm  * options using the NULL arg to indicate that this is a "parse" only.
529*4180Sdougm  */
530*4180Sdougm 
531*4180Sdougm int
532*4180Sdougm zfs_parse_options(char *options, char *proto)
533*4180Sdougm {
534*4180Sdougm 	int ret;
535*4180Sdougm 
536*4180Sdougm 	if (_sa_parse_legacy_options != NULL)
537*4180Sdougm 	    ret = _sa_parse_legacy_options(NULL, options, proto);
538*4180Sdougm 	else
539*4180Sdougm 	    ret = SA_CONFIG_ERR;
540*4180Sdougm 	return (ret);
541*4180Sdougm }
542*4180Sdougm 
543*4180Sdougm /*
544*4180Sdougm  * zfs_sa_find_share(handle, path)
545*4180Sdougm  *
546*4180Sdougm  * wrapper around sa_find_share to find a share path in the
547*4180Sdougm  * configuration.
548*4180Sdougm  */
549*4180Sdougm 
550*4180Sdougm static sa_share_t
551*4180Sdougm zfs_sa_find_share(sa_handle_t handle, char *path)
552*4180Sdougm {
553*4180Sdougm 	if (_sa_find_share != NULL)
554*4180Sdougm 	    return (_sa_find_share(handle, path));
555*4180Sdougm 	return (NULL);
556*4180Sdougm }
557*4180Sdougm 
558*4180Sdougm /*
559*4180Sdougm  * zfs_sa_enable_share(share, proto)
560*4180Sdougm  *
561*4180Sdougm  * Wrapper for sa_enable_share which enables a share for a specified
562*4180Sdougm  * protocol.
563*4180Sdougm  */
564*4180Sdougm 
565*4180Sdougm static int
566*4180Sdougm zfs_sa_enable_share(sa_share_t share, char *proto)
567*4180Sdougm {
568*4180Sdougm 	if (_sa_enable_share != NULL)
569*4180Sdougm 	    return (_sa_enable_share(share, proto));
570*4180Sdougm 	return (SA_CONFIG_ERR);
571*4180Sdougm }
572*4180Sdougm 
573*4180Sdougm /*
574*4180Sdougm  * zfs_sa_disable_share(share, proto)
575*4180Sdougm  *
576*4180Sdougm  * Wrapper for sa_enable_share which disables a share for a specified
577*4180Sdougm  * protocol.
578*4180Sdougm  */
579*4180Sdougm 
580*4180Sdougm static int
581*4180Sdougm zfs_sa_disable_share(sa_share_t share, char *proto)
582*4180Sdougm {
583*4180Sdougm 	if (_sa_disable_share != NULL)
584*4180Sdougm 	    return (_sa_disable_share(share, proto));
585*4180Sdougm 	return (SA_CONFIG_ERR);
586*4180Sdougm }
587*4180Sdougm 
588*4180Sdougm /*
589789Sahrens  * Share the given filesystem according to the options in 'sharenfs'.  We rely
590*4180Sdougm  * on "libshare" to the dirty work for us.
591789Sahrens  */
592*4180Sdougm 
593789Sahrens int
5943126Sahl zfs_share_nfs(zfs_handle_t *zhp)
595789Sahrens {
596789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
597789Sahrens 	char shareopts[ZFS_MAXPROPLEN];
5982082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
599*4180Sdougm 	sa_share_t share;
600*4180Sdougm 	int ret;
601789Sahrens 
6022676Seschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
603789Sahrens 		return (0);
604789Sahrens 
6053126Sahl 	/*
6063126Sahl 	 * Return success if there are no share options.
6073126Sahl 	 */
608789Sahrens 	if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts),
6092082Seschrock 	    NULL, NULL, 0, B_FALSE) != 0 ||
610789Sahrens 	    strcmp(shareopts, "off") == 0)
611789Sahrens 		return (0);
612789Sahrens 
613789Sahrens 	/*
6142676Seschrock 	 * If the 'zoned' property is set, then zfs_is_mountable() will have
6152676Seschrock 	 * already bailed out if we are in the global zone.  But local
6162676Seschrock 	 * zones cannot be NFS servers, so we ignore it for local zones as well.
617789Sahrens 	 */
6182676Seschrock 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
619789Sahrens 		return (0);
620789Sahrens 
621*4180Sdougm 	if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
622*4180Sdougm 	    (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
623*4180Sdougm 			dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
624*4180Sdougm 			zfs_get_name(zhp), _sa_errorstr(ret));
625*4180Sdougm 	    return (-1);
626*4180Sdougm 	}
627*4180Sdougm 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
628*4180Sdougm 	if (share != NULL) {
629*4180Sdougm 	    int err;
630*4180Sdougm 	    err = zfs_sa_enable_share(share, "nfs");
631*4180Sdougm 	    if (err != SA_OK) {
6323237Slling 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
633*4180Sdougm 			dgettext(TEXT_DOMAIN, "cannot share '%s'"),
634*4180Sdougm 			zfs_get_name(zhp));
635*4180Sdougm 		return (-1);
636*4180Sdougm 	    }
637*4180Sdougm 	} else {
638*4180Sdougm 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
639*4180Sdougm 			dgettext(TEXT_DOMAIN, "cannot share '%s'"),
640*4180Sdougm 			zfs_get_name(zhp));
641789Sahrens 		return (-1);
642789Sahrens 	}
643789Sahrens 
644789Sahrens 	return (0);
645789Sahrens }
646789Sahrens 
647789Sahrens /*
6482474Seschrock  * Unshare a filesystem by mountpoint.
6492474Seschrock  */
6502474Seschrock static int
6512474Seschrock unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint)
6522474Seschrock {
653*4180Sdougm 	sa_share_t share;
654*4180Sdougm 	int err;
655*4180Sdougm 	char *mntpt;
6562474Seschrock 
6572474Seschrock 	/*
658*4180Sdougm 	 * Mountpoint could get trashed if libshare calls getmntany
659*4180Sdougm 	 * which id does during API initialization, so strdup the
660*4180Sdougm 	 * value.
6612474Seschrock 	 */
662*4180Sdougm 	mntpt = zfs_strdup(hdl, mountpoint);
6632474Seschrock 
664*4180Sdougm 	/* make sure libshare initialized */
665*4180Sdougm 	if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
666*4180Sdougm 		free(mntpt);	/* don't need the copy anymore */
667*4180Sdougm 		return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
668*4180Sdougm 				dgettext(TEXT_DOMAIN,
669*4180Sdougm 					"cannot unshare '%s': %s"),
670*4180Sdougm 					name, _sa_errorstr(err)));
6712474Seschrock 	}
6722474Seschrock 
673*4180Sdougm 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
674*4180Sdougm 	free(mntpt);	/* don't need the copy anymore */
6752474Seschrock 
676*4180Sdougm 	if (share != NULL) {
677*4180Sdougm 		err = zfs_sa_disable_share(share, "nfs");
678*4180Sdougm 		if (err != SA_OK) {
679*4180Sdougm 		    return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
680*4180Sdougm 				dgettext(TEXT_DOMAIN,
681*4180Sdougm 				"cannot unshare '%s': %s"), name,
682*4180Sdougm 				_sa_errorstr(err)));
683*4180Sdougm 		}
684*4180Sdougm 	} else {
685*4180Sdougm 		return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
686*4180Sdougm 			    dgettext(TEXT_DOMAIN,
687*4180Sdougm 			    "cannot unshare '%s': not found"), name));
688*4180Sdougm 	}
6892474Seschrock 	return (0);
6902474Seschrock }
6912474Seschrock 
6922474Seschrock /*
693789Sahrens  * Unshare the given filesystem.
694789Sahrens  */
695789Sahrens int
6963126Sahl zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
697789Sahrens {
698789Sahrens 	struct mnttab search = { 0 }, entry;
699*4180Sdougm 	char *mntpt = NULL;
700789Sahrens 
701789Sahrens 	/* check to see if need to unmount the filesystem */
702789Sahrens 	search.mnt_special = (char *)zfs_get_name(zhp);
7031407Snd150628 	search.mnt_fstype = MNTTYPE_ZFS;
7042082Seschrock 	rewind(zhp->zfs_hdl->libzfs_mnttab);
705*4180Sdougm 	if (mountpoint != NULL) {
706*4180Sdougm 	    mountpoint = mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
707*4180Sdougm 	}
708789Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
7092082Seschrock 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
710789Sahrens 
711789Sahrens 		if (mountpoint == NULL)
712789Sahrens 			mountpoint = entry.mnt_mountp;
713789Sahrens 
7142474Seschrock 		if (is_shared(zhp->zfs_hdl, mountpoint) &&
715*4180Sdougm 		    unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0) {
716*4180Sdougm 			if (mntpt != NULL)
717*4180Sdougm 				free(mntpt);
7182474Seschrock 			return (-1);
719*4180Sdougm 		}
720789Sahrens 	}
721*4180Sdougm 	if (mntpt != NULL)
722*4180Sdougm 	    free(mntpt);
723789Sahrens 
724789Sahrens 	return (0);
725789Sahrens }
726789Sahrens 
727789Sahrens /*
7283126Sahl  * Same as zfs_unmountall(), but for NFS unshares.
729789Sahrens  */
730789Sahrens int
7313126Sahl zfs_unshareall_nfs(zfs_handle_t *zhp)
732789Sahrens {
733789Sahrens 	prop_changelist_t *clp;
734789Sahrens 	int ret;
735789Sahrens 
736789Sahrens 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0);
737789Sahrens 	if (clp == NULL)
738789Sahrens 		return (-1);
739789Sahrens 
740789Sahrens 	ret = changelist_unshare(clp);
741789Sahrens 	changelist_free(clp);
742789Sahrens 
743789Sahrens 	return (ret);
744789Sahrens }
745789Sahrens 
746789Sahrens /*
747789Sahrens  * Remove the mountpoint associated with the current dataset, if necessary.
748789Sahrens  * We only remove the underlying directory if:
749789Sahrens  *
750789Sahrens  *	- The mountpoint is not 'none' or 'legacy'
751789Sahrens  *	- The mountpoint is non-empty
752789Sahrens  *	- The mountpoint is the default or inherited
753789Sahrens  *	- The 'zoned' property is set, or we're in a local zone
754789Sahrens  *
755789Sahrens  * Any other directories we leave alone.
756789Sahrens  */
757789Sahrens void
758789Sahrens remove_mountpoint(zfs_handle_t *zhp)
759789Sahrens {
760789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
7612676Seschrock 	zfs_source_t source;
762789Sahrens 
7632676Seschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
7642676Seschrock 	    &source))
765789Sahrens 		return;
766789Sahrens 
7672676Seschrock 	if (source == ZFS_SRC_DEFAULT ||
7682676Seschrock 	    source == ZFS_SRC_INHERITED) {
769789Sahrens 		/*
770789Sahrens 		 * Try to remove the directory, silently ignoring any errors.
771789Sahrens 		 * The filesystem may have since been removed or moved around,
7722676Seschrock 		 * and this error isn't really useful to the administrator in
7732676Seschrock 		 * any way.
774789Sahrens 		 */
775789Sahrens 		(void) rmdir(mountpoint);
776789Sahrens 	}
777789Sahrens }
7782474Seschrock 
7793126Sahl boolean_t
7803126Sahl zfs_is_shared_iscsi(zfs_handle_t *zhp)
7813126Sahl {
7823134Sahl 	return (iscsitgt_zfs_is_shared != NULL &&
7833134Sahl 	    iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
7843126Sahl }
7853126Sahl 
7863126Sahl int
7873126Sahl zfs_share_iscsi(zfs_handle_t *zhp)
7883126Sahl {
7893126Sahl 	char shareopts[ZFS_MAXPROPLEN];
7903126Sahl 	const char *dataset = zhp->zfs_name;
7913126Sahl 	libzfs_handle_t *hdl = zhp->zfs_hdl;
7923126Sahl 
7933126Sahl 	/*
7943126Sahl 	 * Return success if there are no share options.
7953126Sahl 	 */
7963126Sahl 	if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
7973126Sahl 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
7983126Sahl 	    strcmp(shareopts, "off") == 0)
7993126Sahl 		return (0);
8003126Sahl 
8013134Sahl 	if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0)
8023237Slling 		return (zfs_error_fmt(hdl, EZFS_SHAREISCSIFAILED,
8033126Sahl 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
8043126Sahl 
8053126Sahl 	return (0);
8063126Sahl }
8073126Sahl 
8083126Sahl int
8093126Sahl zfs_unshare_iscsi(zfs_handle_t *zhp)
8103126Sahl {
8113126Sahl 	const char *dataset = zfs_get_name(zhp);
8123126Sahl 	libzfs_handle_t *hdl = zhp->zfs_hdl;
8133126Sahl 
8143126Sahl 	/*
8153380Sgw25295 	 * Return if the volume is not shared
8163380Sgw25295 	 */
8173380Sgw25295 	if (!zfs_is_shared_iscsi(zhp))
8183380Sgw25295 		return (0);
8193380Sgw25295 
8203380Sgw25295 	/*
8213126Sahl 	 * If this fails with ENODEV it indicates that zvol wasn't shared so
8223126Sahl 	 * we should return success in that case.
8233126Sahl 	 */
8243134Sahl 	if (iscsitgt_zfs_unshare == NULL ||
8253134Sahl 	    (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV))
8263237Slling 		return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED,
8273126Sahl 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
8283126Sahl 
8293126Sahl 	return (0);
8303126Sahl }
8313126Sahl 
8322474Seschrock typedef struct mount_cbdata {
8332474Seschrock 	zfs_handle_t	**cb_datasets;
8342474Seschrock 	int 		cb_used;
8352474Seschrock 	int		cb_alloc;
8362474Seschrock } mount_cbdata_t;
8372474Seschrock 
8382474Seschrock static int
8392474Seschrock mount_cb(zfs_handle_t *zhp, void *data)
8402474Seschrock {
8412474Seschrock 	mount_cbdata_t *cbp = data;
8422474Seschrock 
8433126Sahl 	if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
8442474Seschrock 		zfs_close(zhp);
8452474Seschrock 		return (0);
8462474Seschrock 	}
8472474Seschrock 
8482474Seschrock 	if (cbp->cb_alloc == cbp->cb_used) {
8492676Seschrock 		void *ptr;
8502474Seschrock 
8512676Seschrock 		if ((ptr = zfs_realloc(zhp->zfs_hdl,
8522676Seschrock 		    cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
8532676Seschrock 		    cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
8542474Seschrock 			return (-1);
8552676Seschrock 		cbp->cb_datasets = ptr;
8562474Seschrock 
8572676Seschrock 		cbp->cb_alloc *= 2;
8582474Seschrock 	}
8592474Seschrock 
8602474Seschrock 	cbp->cb_datasets[cbp->cb_used++] = zhp;
8613126Sahl 
8623126Sahl 	return (zfs_iter_children(zhp, mount_cb, cbp));
8632474Seschrock }
8642474Seschrock 
8652474Seschrock static int
8663126Sahl dataset_cmp(const void *a, const void *b)
8672474Seschrock {
8682474Seschrock 	zfs_handle_t **za = (zfs_handle_t **)a;
8692474Seschrock 	zfs_handle_t **zb = (zfs_handle_t **)b;
8702474Seschrock 	char mounta[MAXPATHLEN];
8712474Seschrock 	char mountb[MAXPATHLEN];
8723126Sahl 	boolean_t gota, gotb;
8732474Seschrock 
8743126Sahl 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
8753126Sahl 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
8763126Sahl 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
8773126Sahl 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
8783126Sahl 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
8793126Sahl 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
8802474Seschrock 
8813126Sahl 	if (gota && gotb)
8823126Sahl 		return (strcmp(mounta, mountb));
8833126Sahl 
8843126Sahl 	if (gota)
8853126Sahl 		return (-1);
8863126Sahl 	if (gotb)
8873126Sahl 		return (1);
8883126Sahl 
8893126Sahl 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
8902474Seschrock }
8912474Seschrock 
8923126Sahl /*
8933126Sahl  * Mount and share all datasets within the given pool.  This assumes that no
8943126Sahl  * datasets within the pool are currently mounted.  Because users can create
8953126Sahl  * complicated nested hierarchies of mountpoints, we first gather all the
8963126Sahl  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
8973126Sahl  * we have the list of all filesystems, we iterate over them in order and mount
8983126Sahl  * and/or share each one.
8993126Sahl  */
9003126Sahl #pragma weak zpool_mount_datasets = zpool_enable_datasets
9012474Seschrock int
9023126Sahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
9032474Seschrock {
9042474Seschrock 	mount_cbdata_t cb = { 0 };
9052474Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9062474Seschrock 	zfs_handle_t *zfsp;
9072474Seschrock 	int i, ret = -1;
908*4180Sdougm 	int *good;
9092474Seschrock 
9102474Seschrock 	/*
9112474Seschrock 	 * Gather all datasets within the pool.
9122474Seschrock 	 */
9132474Seschrock 	if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
9142474Seschrock 		return (-1);
9152474Seschrock 	cb.cb_alloc = 4;
9162474Seschrock 
9172474Seschrock 	if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_ANY)) == NULL)
9182474Seschrock 		goto out;
9192474Seschrock 
9202474Seschrock 	cb.cb_datasets[0] = zfsp;
9212474Seschrock 	cb.cb_used = 1;
9222474Seschrock 
9232474Seschrock 	if (zfs_iter_children(zfsp, mount_cb, &cb) != 0)
9242474Seschrock 		goto out;
9252474Seschrock 
9262474Seschrock 	/*
9272474Seschrock 	 * Sort the datasets by mountpoint.
9282474Seschrock 	 */
9293126Sahl 	qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
9302474Seschrock 
9312474Seschrock 	/*
932*4180Sdougm 	 * And mount all the datasets, keeping track of which ones
933*4180Sdougm 	 * succeeded or failed. By using zfs_alloc(), the good pointer
934*4180Sdougm 	 * will always be non-NULL.
9352474Seschrock 	 */
936*4180Sdougm 	good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int));
9372474Seschrock 	ret = 0;
9382474Seschrock 	for (i = 0; i < cb.cb_used; i++) {
939*4180Sdougm 		if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0) {
940*4180Sdougm 			ret = -1;
941*4180Sdougm 		} else {
942*4180Sdougm 			good[i] = 1;
943*4180Sdougm 		}
944*4180Sdougm 	}
945*4180Sdougm 	/*
946*4180Sdougm 	 * Then share all the ones that need to be shared. This needs
947*4180Sdougm 	 * to be a separate pass in order to avoid excessive reloading
948*4180Sdougm 	 * of the configuration. Good should never be NULL since
949*4180Sdougm 	 * zfs_alloc is supposed to exit if memory isn't available.
950*4180Sdougm 	 */
951*4180Sdougm 	zfs_uninit_libshare(hdl);
952*4180Sdougm 	for (i = 0; i < cb.cb_used; i++) {
953*4180Sdougm 		if (good[i] && zfs_share(cb.cb_datasets[i]) != 0)
9542474Seschrock 			ret = -1;
9552474Seschrock 	}
9562474Seschrock 
957*4180Sdougm 	free(good);
958*4180Sdougm 
9592474Seschrock out:
9602474Seschrock 	for (i = 0; i < cb.cb_used; i++)
9612474Seschrock 		zfs_close(cb.cb_datasets[i]);
9622474Seschrock 	free(cb.cb_datasets);
9632474Seschrock 
9642474Seschrock 	return (ret);
9652474Seschrock }
9662474Seschrock 
9673126Sahl 
9683126Sahl static int
9693126Sahl zvol_cb(const char *dataset, void *data)
9703126Sahl {
9713126Sahl 	libzfs_handle_t *hdl = data;
9723126Sahl 	zfs_handle_t *zhp;
9733126Sahl 
9743126Sahl 	/*
9753126Sahl 	 * Ignore snapshots and ignore failures from non-existant datasets.
9763126Sahl 	 */
9773126Sahl 	if (strchr(dataset, '@') != NULL ||
9783126Sahl 	    (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
9793126Sahl 		return (0);
9803126Sahl 
9813126Sahl 	(void) zfs_unshare_iscsi(zhp);
9823126Sahl 
9833126Sahl 	zfs_close(zhp);
9843126Sahl 
9853126Sahl 	return (0);
9863126Sahl }
9873126Sahl 
9882474Seschrock static int
9892474Seschrock mountpoint_compare(const void *a, const void *b)
9902474Seschrock {
9912474Seschrock 	const char *mounta = *((char **)a);
9922474Seschrock 	const char *mountb = *((char **)b);
9932474Seschrock 
9942474Seschrock 	return (strcmp(mountb, mounta));
9952474Seschrock }
9962474Seschrock 
9973126Sahl /*
9983126Sahl  * Unshare and unmount all datasets within the given pool.  We don't want to
9993126Sahl  * rely on traversing the DSL to discover the filesystems within the pool,
10003126Sahl  * because this may be expensive (if not all of them are mounted), and can fail
10013126Sahl  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
10023126Sahl  * gather all the filesystems that are currently mounted.
10033126Sahl  */
10043126Sahl #pragma weak zpool_unmount_datasets = zpool_disable_datasets
10052474Seschrock int
10063126Sahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
10072474Seschrock {
10082474Seschrock 	int used, alloc;
10092474Seschrock 	struct mnttab entry;
10102474Seschrock 	size_t namelen;
10112474Seschrock 	char **mountpoints = NULL;
10122474Seschrock 	zfs_handle_t **datasets = NULL;
10132474Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10142474Seschrock 	int i;
10152474Seschrock 	int ret = -1;
10162474Seschrock 	int flags = (force ? MS_FORCE : 0);
10172474Seschrock 
10183126Sahl 	/*
10193126Sahl 	 * First unshare all zvols.
10203126Sahl 	 */
10213126Sahl 	if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
10223126Sahl 		return (-1);
10233126Sahl 
10242474Seschrock 	namelen = strlen(zhp->zpool_name);
10252474Seschrock 
10262474Seschrock 	rewind(hdl->libzfs_mnttab);
10272474Seschrock 	used = alloc = 0;
10282474Seschrock 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
10292474Seschrock 		/*
10302474Seschrock 		 * Ignore non-ZFS entries.
10312474Seschrock 		 */
10322474Seschrock 		if (entry.mnt_fstype == NULL ||
10332474Seschrock 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
10342474Seschrock 			continue;
10352474Seschrock 
10362474Seschrock 		/*
10372474Seschrock 		 * Ignore filesystems not within this pool.
10382474Seschrock 		 */
10392474Seschrock 		if (entry.mnt_mountp == NULL ||
10402474Seschrock 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
10412474Seschrock 		    (entry.mnt_special[namelen] != '/' &&
10422474Seschrock 		    entry.mnt_special[namelen] != '\0'))
10432474Seschrock 			continue;
10442474Seschrock 
10452474Seschrock 		/*
10462474Seschrock 		 * At this point we've found a filesystem within our pool.  Add
10472474Seschrock 		 * it to our growing list.
10482474Seschrock 		 */
10492474Seschrock 		if (used == alloc) {
10502474Seschrock 			if (alloc == 0) {
10512474Seschrock 				if ((mountpoints = zfs_alloc(hdl,
10522474Seschrock 				    8 * sizeof (void *))) == NULL)
10532474Seschrock 					goto out;
10542474Seschrock 
10552474Seschrock 				if ((datasets = zfs_alloc(hdl,
10562474Seschrock 				    8 * sizeof (void *))) == NULL)
10572474Seschrock 					goto out;
10582474Seschrock 
10592474Seschrock 				alloc = 8;
10602474Seschrock 			} else {
10612676Seschrock 				void *ptr;
10622474Seschrock 
10632676Seschrock 				if ((ptr = zfs_realloc(hdl, mountpoints,
10642676Seschrock 				    alloc * sizeof (void *),
10652474Seschrock 				    alloc * 2 * sizeof (void *))) == NULL)
10662474Seschrock 					goto out;
10672676Seschrock 				mountpoints = ptr;
10682474Seschrock 
10692676Seschrock 				if ((ptr = zfs_realloc(hdl, datasets,
10702676Seschrock 				    alloc * sizeof (void *),
10712474Seschrock 				    alloc * 2 * sizeof (void *))) == NULL)
10722474Seschrock 					goto out;
10732676Seschrock 				datasets = ptr;
10742474Seschrock 
10752474Seschrock 				alloc *= 2;
10762474Seschrock 			}
10772474Seschrock 		}
10782474Seschrock 
10792474Seschrock 		if ((mountpoints[used] = zfs_strdup(hdl,
10802474Seschrock 		    entry.mnt_mountp)) == NULL)
10812474Seschrock 			goto out;
10822474Seschrock 
10832474Seschrock 		/*
10842474Seschrock 		 * This is allowed to fail, in case there is some I/O error.  It
10852474Seschrock 		 * is only used to determine if we need to remove the underlying
10862474Seschrock 		 * mountpoint, so failure is not fatal.
10872474Seschrock 		 */
10882474Seschrock 		datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
10892474Seschrock 
10902474Seschrock 		used++;
10912474Seschrock 	}
10922474Seschrock 
10932474Seschrock 	/*
10942474Seschrock 	 * At this point, we have the entire list of filesystems, so sort it by
10952474Seschrock 	 * mountpoint.
10962474Seschrock 	 */
10972474Seschrock 	qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
10982474Seschrock 
10992474Seschrock 	/*
11002474Seschrock 	 * Walk through and first unshare everything.
11012474Seschrock 	 */
11022474Seschrock 	for (i = 0; i < used; i++) {
11032474Seschrock 		if (is_shared(hdl, mountpoints[i]) &&
11042676Seschrock 		    unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0)
11052474Seschrock 			goto out;
11062474Seschrock 	}
11072474Seschrock 
11082474Seschrock 	/*
11092474Seschrock 	 * Now unmount everything, removing the underlying directories as
11102474Seschrock 	 * appropriate.
11112474Seschrock 	 */
11122474Seschrock 	for (i = 0; i < used; i++) {
11132474Seschrock 		if (unmount_one(hdl, mountpoints[i], flags) != 0)
11142474Seschrock 			goto out;
11152676Seschrock 	}
11162474Seschrock 
11172676Seschrock 	for (i = 0; i < used; i++) {
11182474Seschrock 		if (datasets[i])
11192474Seschrock 			remove_mountpoint(datasets[i]);
11202474Seschrock 	}
11212474Seschrock 
11222474Seschrock 	ret = 0;
11232474Seschrock out:
11242474Seschrock 	for (i = 0; i < used; i++) {
11252474Seschrock 		if (datasets[i])
11262474Seschrock 			zfs_close(datasets[i]);
11272474Seschrock 		free(mountpoints[i]);
11282474Seschrock 	}
11292474Seschrock 	free(datasets);
11302474Seschrock 	free(mountpoints);
11312474Seschrock 
11322474Seschrock 	return (ret);
11332474Seschrock }
1134