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 /*
2312070SMark.Shellenbaum@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24789Sahrens  */
25789Sahrens 
26789Sahrens /*
27789Sahrens  * Routines to manage ZFS mounts.  We separate all the nasty routines that have
283126Sahl  * to deal with the OS.  The following functions are the main entry points --
293126Sahl  * they are used by mount and unmount and when changing a filesystem's
303126Sahl  * mountpoint.
31789Sahrens  *
32789Sahrens  * 	zfs_is_mounted()
33789Sahrens  * 	zfs_mount()
34789Sahrens  * 	zfs_unmount()
35789Sahrens  * 	zfs_unmountall()
36789Sahrens  *
373126Sahl  * This file also contains the functions used to manage sharing filesystems via
383126Sahl  * NFS and iSCSI:
39789Sahrens  *
40789Sahrens  * 	zfs_is_shared()
41789Sahrens  * 	zfs_share()
42789Sahrens  * 	zfs_unshare()
433126Sahl  *
443126Sahl  * 	zfs_is_shared_nfs()
455331Samw  * 	zfs_is_shared_smb()
465331Samw  * 	zfs_share_proto()
475331Samw  * 	zfs_shareall();
483126Sahl  * 	zfs_unshare_nfs()
495331Samw  * 	zfs_unshare_smb()
503126Sahl  * 	zfs_unshareall_nfs()
515331Samw  *	zfs_unshareall_smb()
525331Samw  *	zfs_unshareall()
535331Samw  *	zfs_unshareall_bypath()
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/mount.h>
74789Sahrens #include <sys/stat.h>
75789Sahrens 
76789Sahrens #include <libzfs.h>
77789Sahrens 
78789Sahrens #include "libzfs_impl.h"
79789Sahrens 
804180Sdougm #include <libshare.h>
814180Sdougm #include <sys/systeminfo.h>
824180Sdougm #define	MAXISALEN	257	/* based on sysinfo(2) man page */
834180Sdougm 
845331Samw static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
855331Samw zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
865331Samw     zfs_share_proto_t);
875331Samw 
885331Samw /*
895331Samw  * The share protocols table must be in the same order as the zfs_share_prot_t
905331Samw  * enum in libzfs_impl.h
915331Samw  */
925331Samw typedef struct {
935331Samw 	zfs_prop_t p_prop;
945331Samw 	char *p_name;
955331Samw 	int p_share_err;
965331Samw 	int p_unshare_err;
975331Samw } proto_table_t;
985331Samw 
995331Samw proto_table_t proto_table[PROTO_END] = {
1005331Samw 	{ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
1015331Samw 	{ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
1025331Samw };
1035331Samw 
1045331Samw zfs_share_proto_t nfs_only[] = {
1055331Samw 	PROTO_NFS,
1065331Samw 	PROTO_END
1075331Samw };
1085331Samw 
1095331Samw zfs_share_proto_t smb_only[] = {
1105331Samw 	PROTO_SMB,
1115331Samw 	PROTO_END
1125331Samw };
1135331Samw zfs_share_proto_t share_all_proto[] = {
1145331Samw 	PROTO_NFS,
1155331Samw 	PROTO_SMB,
1165331Samw 	PROTO_END
1175331Samw };
1185331Samw 
119789Sahrens /*
1205331Samw  * Search the sharetab for the given mountpoint and protocol, returning
1215331Samw  * a zfs_share_type_t value.
122789Sahrens  */
1235331Samw static zfs_share_type_t
1245331Samw is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
125789Sahrens {
126789Sahrens 	char buf[MAXPATHLEN], *tab;
1275331Samw 	char *ptr;
128789Sahrens 
1292082Seschrock 	if (hdl->libzfs_sharetab == NULL)
1305331Samw 		return (SHARED_NOT_SHARED);
131789Sahrens 
1322082Seschrock 	(void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
133789Sahrens 
1342082Seschrock 	while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
135789Sahrens 
136789Sahrens 		/* the mountpoint is the first entry on each line */
1375331Samw 		if ((tab = strchr(buf, '\t')) == NULL)
1385331Samw 			continue;
1395331Samw 
1405331Samw 		*tab = '\0';
1415331Samw 		if (strcmp(buf, mountpoint) == 0) {
1425331Samw 			/*
1435331Samw 			 * the protocol field is the third field
1445331Samw 			 * skip over second field
1455331Samw 			 */
1465331Samw 			ptr = ++tab;
1475331Samw 			if ((tab = strchr(ptr, '\t')) == NULL)
1485331Samw 				continue;
1495331Samw 			ptr = ++tab;
1505331Samw 			if ((tab = strchr(ptr, '\t')) == NULL)
1515331Samw 				continue;
152789Sahrens 			*tab = '\0';
1535331Samw 			if (strcmp(ptr,
1545331Samw 			    proto_table[proto].p_name) == 0) {
1555331Samw 				switch (proto) {
1565331Samw 				case PROTO_NFS:
1575331Samw 					return (SHARED_NFS);
1585331Samw 				case PROTO_SMB:
1595331Samw 					return (SHARED_SMB);
1605331Samw 				default:
1615331Samw 					return (0);
1625331Samw 				}
1635331Samw 			}
164789Sahrens 		}
165789Sahrens 	}
166789Sahrens 
1675331Samw 	return (SHARED_NOT_SHARED);
168789Sahrens }
169789Sahrens 
170789Sahrens /*
1712082Seschrock  * Returns true if the specified directory is empty.  If we can't open the
1722082Seschrock  * directory at all, return true so that the mount can fail with a more
173789Sahrens  * informative error message.
174789Sahrens  */
1752082Seschrock static boolean_t
176789Sahrens dir_is_empty(const char *dirname)
177789Sahrens {
178789Sahrens 	DIR *dirp;
179789Sahrens 	struct dirent64 *dp;
180789Sahrens 
181789Sahrens 	if ((dirp = opendir(dirname)) == NULL)
1822082Seschrock 		return (B_TRUE);
183789Sahrens 
184789Sahrens 	while ((dp = readdir64(dirp)) != NULL) {
185789Sahrens 
186789Sahrens 		if (strcmp(dp->d_name, ".") == 0 ||
187789Sahrens 		    strcmp(dp->d_name, "..") == 0)
188789Sahrens 			continue;
189789Sahrens 
190789Sahrens 		(void) closedir(dirp);
1912082Seschrock 		return (B_FALSE);
192789Sahrens 	}
193789Sahrens 
194789Sahrens 	(void) closedir(dirp);
1952082Seschrock 	return (B_TRUE);
196789Sahrens }
197789Sahrens 
198789Sahrens /*
199789Sahrens  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
200789Sahrens  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
201789Sahrens  * 0.
202789Sahrens  */
2032082Seschrock boolean_t
2043444Sek110237 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
205789Sahrens {
2068228SEric.Taylor@Sun.COM 	struct mnttab entry;
207789Sahrens 
2088228SEric.Taylor@Sun.COM 	if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
2092082Seschrock 		return (B_FALSE);
210789Sahrens 
211789Sahrens 	if (where != NULL)
2123444Sek110237 		*where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
213789Sahrens 
2142082Seschrock 	return (B_TRUE);
215789Sahrens }
216789Sahrens 
2173444Sek110237 boolean_t
2183444Sek110237 zfs_is_mounted(zfs_handle_t *zhp, char **where)
2193444Sek110237 {
2203444Sek110237 	return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
2213444Sek110237 }
2223444Sek110237 
223789Sahrens /*
2242676Seschrock  * Returns true if the given dataset is mountable, false otherwise.  Returns the
2252676Seschrock  * mountpoint in 'buf'.
2262676Seschrock  */
2272676Seschrock static boolean_t
2282676Seschrock zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
2295094Slling     zprop_source_t *source)
2302676Seschrock {
2312676Seschrock 	char sourceloc[ZFS_MAXNAMELEN];
2325094Slling 	zprop_source_t sourcetype;
2332676Seschrock 
2342676Seschrock 	if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
2352676Seschrock 		return (B_FALSE);
2362676Seschrock 
2372676Seschrock 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
2382676Seschrock 	    &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
2392676Seschrock 
2402676Seschrock 	if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
2412676Seschrock 	    strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
2422676Seschrock 		return (B_FALSE);
2432676Seschrock 
2446168Shs24103 	if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
2452676Seschrock 		return (B_FALSE);
2462676Seschrock 
2472676Seschrock 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
2482676Seschrock 	    getzoneid() == GLOBAL_ZONEID)
2492676Seschrock 		return (B_FALSE);
2502676Seschrock 
2512676Seschrock 	if (source)
2522676Seschrock 		*source = sourcetype;
2532676Seschrock 
2542676Seschrock 	return (B_TRUE);
2552676Seschrock }
2562676Seschrock 
2572676Seschrock /*
258789Sahrens  * Mount the given filesystem.
259789Sahrens  */
260789Sahrens int
261789Sahrens zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
262789Sahrens {
263789Sahrens 	struct stat buf;
264789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
265789Sahrens 	char mntopts[MNT_LINE_MAX];
2662082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
267789Sahrens 
268789Sahrens 	if (options == NULL)
269789Sahrens 		mntopts[0] = '\0';
270789Sahrens 	else
271789Sahrens 		(void) strlcpy(mntopts, options, sizeof (mntopts));
272789Sahrens 
2732676Seschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
2742082Seschrock 		return (0);
275789Sahrens 
276789Sahrens 	/* Create the directory if it doesn't already exist */
277789Sahrens 	if (lstat(mountpoint, &buf) != 0) {
278789Sahrens 		if (mkdirp(mountpoint, 0755) != 0) {
2792082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2802082Seschrock 			    "failed to create mountpoint"));
2813237Slling 			return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2822082Seschrock 			    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
2832082Seschrock 			    mountpoint));
284789Sahrens 		}
285789Sahrens 	}
286789Sahrens 
287789Sahrens 	/*
288789Sahrens 	 * Determine if the mountpoint is empty.  If so, refuse to perform the
289789Sahrens 	 * mount.  We don't perform this check if MS_OVERLAY is specified, which
290789Sahrens 	 * would defeat the point.  We also avoid this check if 'remount' is
291789Sahrens 	 * specified.
292789Sahrens 	 */
293789Sahrens 	if ((flags & MS_OVERLAY) == 0 &&
294789Sahrens 	    strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
295789Sahrens 	    !dir_is_empty(mountpoint)) {
2962082Seschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2972082Seschrock 		    "directory is not empty"));
2983237Slling 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
2992082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
300789Sahrens 	}
301789Sahrens 
302789Sahrens 	/* perform the mount */
303789Sahrens 	if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
304789Sahrens 	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
305789Sahrens 		/*
306789Sahrens 		 * Generic errors are nasty, but there are just way too many
307789Sahrens 		 * from mount(), and they're well-understood.  We pick a few
308789Sahrens 		 * common ones to improve upon.
309789Sahrens 		 */
3104302Sdougm 		if (errno == EBUSY) {
3112082Seschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3122082Seschrock 			    "mountpoint or dataset is busy"));
3134543Smarks 		} else if (errno == EPERM) {
3144543Smarks 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3154543Smarks 			    "Insufficient privileges"));
31611814SChris.Kirby@sun.com 		} else if (errno == ENOTSUP) {
31711814SChris.Kirby@sun.com 			char buf[256];
31812070SMark.Shellenbaum@Sun.COM 			int spa_version;
31911814SChris.Kirby@sun.com 
32012070SMark.Shellenbaum@Sun.COM 			VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
32111814SChris.Kirby@sun.com 			(void) snprintf(buf, sizeof (buf),
32212070SMark.Shellenbaum@Sun.COM 			    dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
32312070SMark.Shellenbaum@Sun.COM 			    "file system on a version %d pool. Pool must be"
32412070SMark.Shellenbaum@Sun.COM 			    " upgraded to mount this file system."),
32511814SChris.Kirby@sun.com 			    (u_longlong_t)zfs_prop_get_int(zhp,
32612070SMark.Shellenbaum@Sun.COM 			    ZFS_PROP_VERSION), spa_version);
32711814SChris.Kirby@sun.com 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
3284302Sdougm 		} else {
3292082Seschrock 			zfs_error_aux(hdl, strerror(errno));
3304302Sdougm 		}
3313237Slling 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
3322082Seschrock 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
3332082Seschrock 		    zhp->zfs_name));
334789Sahrens 	}
335789Sahrens 
3368228SEric.Taylor@Sun.COM 	/* add the mounted entry into our cache */
3378228SEric.Taylor@Sun.COM 	libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
3388228SEric.Taylor@Sun.COM 	    mntopts);
339789Sahrens 	return (0);
340789Sahrens }
341789Sahrens 
342789Sahrens /*
3432474Seschrock  * Unmount a single filesystem.
3442474Seschrock  */
3452474Seschrock static int
3462474Seschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
3472474Seschrock {
3482474Seschrock 	if (umount2(mountpoint, flags) != 0) {
3492474Seschrock 		zfs_error_aux(hdl, strerror(errno));
3503237Slling 		return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
3512474Seschrock 		    dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
3522474Seschrock 		    mountpoint));
3532474Seschrock 	}
3542474Seschrock 
3552474Seschrock 	return (0);
3562474Seschrock }
3572474Seschrock 
3582474Seschrock /*
359789Sahrens  * Unmount the given filesystem.
360789Sahrens  */
361789Sahrens int
362789Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
363789Sahrens {
3648228SEric.Taylor@Sun.COM 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3658228SEric.Taylor@Sun.COM 	struct mnttab entry;
3664180Sdougm 	char *mntpt = NULL;
367789Sahrens 
3688228SEric.Taylor@Sun.COM 	/* check to see if we need to unmount the filesystem */
369789Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
3708228SEric.Taylor@Sun.COM 	    libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
3714180Sdougm 		/*
3724180Sdougm 		 * mountpoint may have come from a call to
3734180Sdougm 		 * getmnt/getmntany if it isn't NULL. If it is NULL,
3748228SEric.Taylor@Sun.COM 		 * we know it comes from libzfs_mnttab_find which can
3758228SEric.Taylor@Sun.COM 		 * then get freed later. We strdup it to play it safe.
3764180Sdougm 		 */
377789Sahrens 		if (mountpoint == NULL)
3788228SEric.Taylor@Sun.COM 			mntpt = zfs_strdup(hdl, entry.mnt_mountp);
3794180Sdougm 		else
3808228SEric.Taylor@Sun.COM 			mntpt = zfs_strdup(hdl, mountpoint);
381789Sahrens 
382789Sahrens 		/*
3832474Seschrock 		 * Unshare and unmount the filesystem
384789Sahrens 		 */
3855331Samw 		if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
3864331Sth199096 			return (-1);
3874331Sth199096 
3888228SEric.Taylor@Sun.COM 		if (unmount_one(hdl, mntpt, flags) != 0) {
3894180Sdougm 			free(mntpt);
3905331Samw 			(void) zfs_shareall(zhp);
391789Sahrens 			return (-1);
3924180Sdougm 		}
3938228SEric.Taylor@Sun.COM 		libzfs_mnttab_remove(hdl, zhp->zfs_name);
3944180Sdougm 		free(mntpt);
395789Sahrens 	}
396789Sahrens 
397789Sahrens 	return (0);
398789Sahrens }
399789Sahrens 
400789Sahrens /*
401789Sahrens  * Unmount this filesystem and any children inheriting the mountpoint property.
402789Sahrens  * To do this, just act like we're changing the mountpoint property, but don't
403789Sahrens  * remount the filesystems afterwards.
404789Sahrens  */
405789Sahrens int
406789Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags)
407789Sahrens {
408789Sahrens 	prop_changelist_t *clp;
409789Sahrens 	int ret;
410789Sahrens 
4117366STim.Haley@Sun.COM 	clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
412789Sahrens 	if (clp == NULL)
413789Sahrens 		return (-1);
414789Sahrens 
415789Sahrens 	ret = changelist_prefix(clp);
416789Sahrens 	changelist_free(clp);
417789Sahrens 
418789Sahrens 	return (ret);
419789Sahrens }
420789Sahrens 
4213126Sahl boolean_t
4223126Sahl zfs_is_shared(zfs_handle_t *zhp)
4233126Sahl {
4245331Samw 	zfs_share_type_t rc = 0;
4255331Samw 	zfs_share_proto_t *curr_proto;
4265331Samw 
4273126Sahl 	if (ZFS_IS_VOLUME(zhp))
42811876SJames.Dunham@Sun.COM 		return (B_FALSE);
4293126Sahl 
4305331Samw 	for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
4315331Samw 	    curr_proto++)
4325331Samw 		rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
4335331Samw 
4345331Samw 	return (rc ? B_TRUE : B_FALSE);
4353126Sahl }
4363126Sahl 
4373126Sahl int
4383126Sahl zfs_share(zfs_handle_t *zhp)
4393126Sahl {
440*13025SEric.Taylor@oracle.com 	assert(!ZFS_IS_VOLUME(zhp));
4415331Samw 	return (zfs_share_proto(zhp, share_all_proto));
4423126Sahl }
4433126Sahl 
4443126Sahl int
4453126Sahl zfs_unshare(zfs_handle_t *zhp)
4463126Sahl {
447*13025SEric.Taylor@oracle.com 	assert(!ZFS_IS_VOLUME(zhp));
4485331Samw 	return (zfs_unshareall(zhp));
4493126Sahl }
4503126Sahl 
451789Sahrens /*
452789Sahrens  * Check to see if the filesystem is currently shared.
453789Sahrens  */
4545331Samw zfs_share_type_t
4555331Samw zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
456789Sahrens {
457789Sahrens 	char *mountpoint;
4585331Samw 	zfs_share_type_t rc;
459789Sahrens 
460789Sahrens 	if (!zfs_is_mounted(zhp, &mountpoint))
4615331Samw 		return (SHARED_NOT_SHARED);
462789Sahrens 
4635331Samw 	if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) {
464789Sahrens 		if (where != NULL)
465789Sahrens 			*where = mountpoint;
466789Sahrens 		else
467789Sahrens 			free(mountpoint);
4685331Samw 		return (rc);
469789Sahrens 	} else {
470789Sahrens 		free(mountpoint);
4715331Samw 		return (SHARED_NOT_SHARED);
472789Sahrens 	}
473789Sahrens }
474789Sahrens 
4755331Samw boolean_t
4765331Samw zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
4775331Samw {
4785331Samw 	return (zfs_is_shared_proto(zhp, where,
4795331Samw 	    PROTO_NFS) != SHARED_NOT_SHARED);
4805331Samw }
4815331Samw 
4825331Samw boolean_t
4835331Samw zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
4845331Samw {
4855331Samw 	return (zfs_is_shared_proto(zhp, where,
4865331Samw 	    PROTO_SMB) != SHARED_NOT_SHARED);
4875331Samw }
4885331Samw 
489789Sahrens /*
4904180Sdougm  * Make sure things will work if libshare isn't installed by using
4914180Sdougm  * wrapper functions that check to see that the pointers to functions
4924180Sdougm  * initialized in _zfs_init_libshare() are actually present.
4934180Sdougm  */
4944180Sdougm 
4954180Sdougm static sa_handle_t (*_sa_init)(int);
4964180Sdougm static void (*_sa_fini)(sa_handle_t);
4974180Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
4984180Sdougm static int (*_sa_enable_share)(sa_share_t, char *);
4994180Sdougm static int (*_sa_disable_share)(sa_share_t, char *);
5004180Sdougm static char *(*_sa_errorstr)(int);
5014180Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
5025951Sdougm static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
5035951Sdougm static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
5045951Sdougm static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
5055951Sdougm     char *, char *, zprop_source_t, char *, char *, char *);
5065951Sdougm static void (*_sa_update_sharetab_ts)(sa_handle_t);
5074180Sdougm 
5084180Sdougm /*
5094180Sdougm  * _zfs_init_libshare()
5104180Sdougm  *
5114180Sdougm  * Find the libshare.so.1 entry points that we use here and save the
5124180Sdougm  * values to be used later. This is triggered by the runtime loader.
5134180Sdougm  * Make sure the correct ISA version is loaded.
5144180Sdougm  */
5155951Sdougm 
5164180Sdougm #pragma init(_zfs_init_libshare)
5174180Sdougm static void
5184180Sdougm _zfs_init_libshare(void)
5194180Sdougm {
5204180Sdougm 	void *libshare;
5214180Sdougm 	char path[MAXPATHLEN];
5224180Sdougm 	char isa[MAXISALEN];
5234180Sdougm 
5244180Sdougm #if defined(_LP64)
5254180Sdougm 	if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
5264302Sdougm 		isa[0] = '\0';
5274180Sdougm #else
5284180Sdougm 	isa[0] = '\0';
5294180Sdougm #endif
5304180Sdougm 	(void) snprintf(path, MAXPATHLEN,
5314302Sdougm 	    "/usr/lib/%s/libshare.so.1", isa);
5324180Sdougm 
5334180Sdougm 	if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
5344302Sdougm 		_sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
5354302Sdougm 		_sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
5364302Sdougm 		_sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
5374302Sdougm 		    dlsym(libshare, "sa_find_share");
5384302Sdougm 		_sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
5394302Sdougm 		    "sa_enable_share");
5404302Sdougm 		_sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
5414302Sdougm 		    "sa_disable_share");
5424302Sdougm 		_sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
5434302Sdougm 		_sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
5444302Sdougm 		    dlsym(libshare, "sa_parse_legacy_options");
5455951Sdougm 		_sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
5465951Sdougm 		    dlsym(libshare, "sa_needs_refresh");
5475951Sdougm 		_sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
5485951Sdougm 		    dlsym(libshare, "sa_get_zfs_handle");
5495951Sdougm 		_sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
5505951Sdougm 		    sa_share_t, char *, char *, zprop_source_t, char *,
5515951Sdougm 		    char *, char *))dlsym(libshare, "sa_zfs_process_share");
5525951Sdougm 		_sa_update_sharetab_ts = (void (*)(sa_handle_t))
5535951Sdougm 		    dlsym(libshare, "sa_update_sharetab_ts");
5544327Sdougm 		if (_sa_init == NULL || _sa_fini == NULL ||
5554327Sdougm 		    _sa_find_share == NULL || _sa_enable_share == NULL ||
5564327Sdougm 		    _sa_disable_share == NULL || _sa_errorstr == NULL ||
5575951Sdougm 		    _sa_parse_legacy_options == NULL ||
5585951Sdougm 		    _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
5595951Sdougm 		    _sa_zfs_process_share == NULL ||
5605951Sdougm 		    _sa_update_sharetab_ts == NULL) {
5614327Sdougm 			_sa_init = NULL;
5624327Sdougm 			_sa_fini = NULL;
5634327Sdougm 			_sa_disable_share = NULL;
5644327Sdougm 			_sa_enable_share = NULL;
5654327Sdougm 			_sa_errorstr = NULL;
5664327Sdougm 			_sa_parse_legacy_options = NULL;
5674327Sdougm 			(void) dlclose(libshare);
5685951Sdougm 			_sa_needs_refresh = NULL;
5695951Sdougm 			_sa_get_zfs_handle = NULL;
5705951Sdougm 			_sa_zfs_process_share = NULL;
5715951Sdougm 			_sa_update_sharetab_ts = NULL;
5724327Sdougm 		}
5734180Sdougm 	}
5744180Sdougm }
5754180Sdougm 
5764180Sdougm /*
5774180Sdougm  * zfs_init_libshare(zhandle, service)
5784180Sdougm  *
5794180Sdougm  * Initialize the libshare API if it hasn't already been initialized.
5804180Sdougm  * In all cases it returns 0 if it succeeded and an error if not. The
5814180Sdougm  * service value is which part(s) of the API to initialize and is a
5824180Sdougm  * direct map to the libshare sa_init(service) interface.
5834180Sdougm  */
5844180Sdougm int
5854180Sdougm zfs_init_libshare(libzfs_handle_t *zhandle, int service)
5864180Sdougm {
5874180Sdougm 	int ret = SA_OK;
5884180Sdougm 
5894302Sdougm 	if (_sa_init == NULL)
5904302Sdougm 		ret = SA_CONFIG_ERR;
5914302Sdougm 
5925951Sdougm 	if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
5935951Sdougm 		/*
5945951Sdougm 		 * We had a cache miss. Most likely it is a new ZFS
5955951Sdougm 		 * dataset that was just created. We want to make sure
5965951Sdougm 		 * so check timestamps to see if a different process
5975951Sdougm 		 * has updated any of the configuration. If there was
5985951Sdougm 		 * some non-ZFS change, we need to re-initialize the
5995951Sdougm 		 * internal cache.
6005951Sdougm 		 */
6015951Sdougm 		zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
6025951Sdougm 		if (_sa_needs_refresh != NULL &&
6035951Sdougm 		    _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
6045951Sdougm 			zfs_uninit_libshare(zhandle);
6055951Sdougm 			zhandle->libzfs_sharehdl = _sa_init(service);
6065951Sdougm 		}
6075951Sdougm 	}
6085951Sdougm 
6094302Sdougm 	if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
6104302Sdougm 		zhandle->libzfs_sharehdl = _sa_init(service);
6114302Sdougm 
6124302Sdougm 	if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
6134302Sdougm 		ret = SA_NO_MEMORY;
6144302Sdougm 
6154180Sdougm 	return (ret);
6164180Sdougm }
6174180Sdougm 
6184180Sdougm /*
6194180Sdougm  * zfs_uninit_libshare(zhandle)
6204180Sdougm  *
6214180Sdougm  * Uninitialize the libshare API if it hasn't already been
6224180Sdougm  * uninitialized. It is OK to call multiple times.
6234180Sdougm  */
6244180Sdougm void
6254180Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle)
6264180Sdougm {
6274180Sdougm 	if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
6284302Sdougm 		if (_sa_fini != NULL)
6294302Sdougm 			_sa_fini(zhandle->libzfs_sharehdl);
6304302Sdougm 		zhandle->libzfs_sharehdl = NULL;
6314180Sdougm 	}
6324180Sdougm }
6334180Sdougm 
6344180Sdougm /*
6354180Sdougm  * zfs_parse_options(options, proto)
6364180Sdougm  *
6374180Sdougm  * Call the legacy parse interface to get the protocol specific
6384180Sdougm  * options using the NULL arg to indicate that this is a "parse" only.
6394180Sdougm  */
6404180Sdougm int
6415331Samw zfs_parse_options(char *options, zfs_share_proto_t proto)
6424180Sdougm {
6435367Sahrens 	if (_sa_parse_legacy_options != NULL) {
6445367Sahrens 		return (_sa_parse_legacy_options(NULL, options,
6455367Sahrens 		    proto_table[proto].p_name));
6465367Sahrens 	}
6475367Sahrens 	return (SA_CONFIG_ERR);
6484180Sdougm }
6494180Sdougm 
6504180Sdougm /*
6514180Sdougm  * zfs_sa_find_share(handle, path)
6524180Sdougm  *
6534180Sdougm  * wrapper around sa_find_share to find a share path in the
6544180Sdougm  * configuration.
6554180Sdougm  */
6564180Sdougm static sa_share_t
6574180Sdougm zfs_sa_find_share(sa_handle_t handle, char *path)
6584180Sdougm {
6594180Sdougm 	if (_sa_find_share != NULL)
6604302Sdougm 		return (_sa_find_share(handle, path));
6614180Sdougm 	return (NULL);
6624180Sdougm }
6634180Sdougm 
6644180Sdougm /*
6654180Sdougm  * zfs_sa_enable_share(share, proto)
6664180Sdougm  *
6674180Sdougm  * Wrapper for sa_enable_share which enables a share for a specified
6684180Sdougm  * protocol.
6694180Sdougm  */
6704180Sdougm static int
6714180Sdougm zfs_sa_enable_share(sa_share_t share, char *proto)
6724180Sdougm {
6734180Sdougm 	if (_sa_enable_share != NULL)
6744302Sdougm 		return (_sa_enable_share(share, proto));
6754180Sdougm 	return (SA_CONFIG_ERR);
6764180Sdougm }
6774180Sdougm 
6784180Sdougm /*
6794180Sdougm  * zfs_sa_disable_share(share, proto)
6804180Sdougm  *
6814180Sdougm  * Wrapper for sa_enable_share which disables a share for a specified
6824180Sdougm  * protocol.
6834180Sdougm  */
6844180Sdougm static int
6854180Sdougm zfs_sa_disable_share(sa_share_t share, char *proto)
6864180Sdougm {
6874180Sdougm 	if (_sa_disable_share != NULL)
6884302Sdougm 		return (_sa_disable_share(share, proto));
6894180Sdougm 	return (SA_CONFIG_ERR);
6904180Sdougm }
6914180Sdougm 
6924180Sdougm /*
6935331Samw  * Share the given filesystem according to the options in the specified
6945331Samw  * protocol specific properties (sharenfs, sharesmb).  We rely
6954180Sdougm  * on "libshare" to the dirty work for us.
696789Sahrens  */
6975331Samw static int
6985331Samw zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
699789Sahrens {
700789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
701789Sahrens 	char shareopts[ZFS_MAXPROPLEN];
7025951Sdougm 	char sourcestr[ZFS_MAXPROPLEN];
7032082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
7044180Sdougm 	sa_share_t share;
7055331Samw 	zfs_share_proto_t *curr_proto;
7065951Sdougm 	zprop_source_t sourcetype;
7074180Sdougm 	int ret;
708789Sahrens 
7092676Seschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
710789Sahrens 		return (0);
711789Sahrens 
7124180Sdougm 	if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
7134302Sdougm 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
7144302Sdougm 		    dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
7155951Sdougm 		    zfs_get_name(zhp), _sa_errorstr != NULL ?
7165951Sdougm 		    _sa_errorstr(ret) : "");
7174302Sdougm 		return (-1);
7184180Sdougm 	}
7195331Samw 
7205331Samw 	for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
7215331Samw 		/*
7225331Samw 		 * Return success if there are no share options.
7235331Samw 		 */
7245331Samw 		if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
7255951Sdougm 		    shareopts, sizeof (shareopts), &sourcetype, sourcestr,
7265951Sdougm 		    ZFS_MAXPROPLEN, B_FALSE) != 0 ||
7275951Sdougm 		    strcmp(shareopts, "off") == 0)
7285331Samw 			continue;
7295331Samw 
7305331Samw 		/*
7315331Samw 		 * If the 'zoned' property is set, then zfs_is_mountable()
7325331Samw 		 * will have already bailed out if we are in the global zone.
7335331Samw 		 * But local zones cannot be NFS servers, so we ignore it for
7345331Samw 		 * local zones as well.
7355331Samw 		 */
7365331Samw 		if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
7375331Samw 			continue;
7385331Samw 
7395331Samw 		share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
7405951Sdougm 		if (share == NULL) {
7415951Sdougm 			/*
7425951Sdougm 			 * This may be a new file system that was just
7435951Sdougm 			 * created so isn't in the internal cache
7445951Sdougm 			 * (second time through). Rather than
7455951Sdougm 			 * reloading the entire configuration, we can
7465951Sdougm 			 * assume ZFS has done the checking and it is
7475951Sdougm 			 * safe to add this to the internal
7485951Sdougm 			 * configuration.
7495951Sdougm 			 */
7505951Sdougm 			if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
7515951Sdougm 			    NULL, NULL, mountpoint,
7525951Sdougm 			    proto_table[*curr_proto].p_name, sourcetype,
7535951Sdougm 			    shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
7545951Sdougm 				(void) zfs_error_fmt(hdl,
7555951Sdougm 				    proto_table[*curr_proto].p_share_err,
7565951Sdougm 				    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
7575951Sdougm 				    zfs_get_name(zhp));
7585951Sdougm 				return (-1);
7595951Sdougm 			}
7605951Sdougm 			hdl->libzfs_shareflags |= ZFSSHARE_MISS;
7615951Sdougm 			share = zfs_sa_find_share(hdl->libzfs_sharehdl,
7625951Sdougm 			    mountpoint);
7635951Sdougm 		}
7645331Samw 		if (share != NULL) {
7655331Samw 			int err;
7665331Samw 			err = zfs_sa_enable_share(share,
7675331Samw 			    proto_table[*curr_proto].p_name);
7685331Samw 			if (err != SA_OK) {
7695331Samw 				(void) zfs_error_fmt(hdl,
7705331Samw 				    proto_table[*curr_proto].p_share_err,
7715331Samw 				    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
7725331Samw 				    zfs_get_name(zhp));
7735331Samw 				return (-1);
7745331Samw 			}
7755331Samw 		} else {
7765331Samw 			(void) zfs_error_fmt(hdl,
7775331Samw 			    proto_table[*curr_proto].p_share_err,
7784302Sdougm 			    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
7794302Sdougm 			    zfs_get_name(zhp));
7804302Sdougm 			return (-1);
7814302Sdougm 		}
7825331Samw 
783789Sahrens 	}
7845331Samw 	return (0);
7855331Samw }
786789Sahrens 
7875331Samw 
7885331Samw int
7895331Samw zfs_share_nfs(zfs_handle_t *zhp)
7905331Samw {
7915331Samw 	return (zfs_share_proto(zhp, nfs_only));
7925331Samw }
7935331Samw 
7945331Samw int
7955331Samw zfs_share_smb(zfs_handle_t *zhp)
7965331Samw {
7975331Samw 	return (zfs_share_proto(zhp, smb_only));
7985331Samw }
7995331Samw 
8005331Samw int
8015331Samw zfs_shareall(zfs_handle_t *zhp)
8025331Samw {
8035331Samw 	return (zfs_share_proto(zhp, share_all_proto));
804789Sahrens }
805789Sahrens 
806789Sahrens /*
8072474Seschrock  * Unshare a filesystem by mountpoint.
8082474Seschrock  */
8092474Seschrock static int
8105331Samw unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
8115331Samw     zfs_share_proto_t proto)
8122474Seschrock {
8134180Sdougm 	sa_share_t share;
8144180Sdougm 	int err;
8154180Sdougm 	char *mntpt;
8162474Seschrock 	/*
8174180Sdougm 	 * Mountpoint could get trashed if libshare calls getmntany
8188228SEric.Taylor@Sun.COM 	 * which it does during API initialization, so strdup the
8194180Sdougm 	 * value.
8202474Seschrock 	 */
8214180Sdougm 	mntpt = zfs_strdup(hdl, mountpoint);
8222474Seschrock 
8234180Sdougm 	/* make sure libshare initialized */
8244180Sdougm 	if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
8254180Sdougm 		free(mntpt);	/* don't need the copy anymore */
8264180Sdougm 		return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
8274302Sdougm 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
8284302Sdougm 		    name, _sa_errorstr(err)));
8292474Seschrock 	}
8302474Seschrock 
8314180Sdougm 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
8324180Sdougm 	free(mntpt);	/* don't need the copy anymore */
8332474Seschrock 
8344180Sdougm 	if (share != NULL) {
8355331Samw 		err = zfs_sa_disable_share(share, proto_table[proto].p_name);
8364180Sdougm 		if (err != SA_OK) {
8374302Sdougm 			return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
8384302Sdougm 			    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
8394302Sdougm 			    name, _sa_errorstr(err)));
8404180Sdougm 		}
8414180Sdougm 	} else {
8424180Sdougm 		return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
8434302Sdougm 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
8444302Sdougm 		    name));
8454180Sdougm 	}
8462474Seschrock 	return (0);
8472474Seschrock }
8482474Seschrock 
8492474Seschrock /*
850789Sahrens  * Unshare the given filesystem.
851789Sahrens  */
852789Sahrens int
8535331Samw zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
8545331Samw     zfs_share_proto_t *proto)
855789Sahrens {
8568228SEric.Taylor@Sun.COM 	libzfs_handle_t *hdl = zhp->zfs_hdl;
8578228SEric.Taylor@Sun.COM 	struct mnttab entry;
8584180Sdougm 	char *mntpt = NULL;
859789Sahrens 
860789Sahrens 	/* check to see if need to unmount the filesystem */
8612082Seschrock 	rewind(zhp->zfs_hdl->libzfs_mnttab);
8624302Sdougm 	if (mountpoint != NULL)
8638228SEric.Taylor@Sun.COM 		mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
8644302Sdougm 
865789Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
8668228SEric.Taylor@Sun.COM 	    libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
8675331Samw 		zfs_share_proto_t *curr_proto;
868789Sahrens 
869789Sahrens 		if (mountpoint == NULL)
8705331Samw 			mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
8715331Samw 
8725331Samw 		for (curr_proto = proto; *curr_proto != PROTO_END;
8735331Samw 		    curr_proto++) {
874789Sahrens 
8758228SEric.Taylor@Sun.COM 			if (is_shared(hdl, mntpt, *curr_proto) &&
8768228SEric.Taylor@Sun.COM 			    unshare_one(hdl, zhp->zfs_name,
8775331Samw 			    mntpt, *curr_proto) != 0) {
8785331Samw 				if (mntpt != NULL)
8795331Samw 					free(mntpt);
8805331Samw 				return (-1);
8815331Samw 			}
8824180Sdougm 		}
883789Sahrens 	}
8844180Sdougm 	if (mntpt != NULL)
8854302Sdougm 		free(mntpt);
886789Sahrens 
887789Sahrens 	return (0);
888789Sahrens }
889789Sahrens 
8905331Samw int
8915331Samw zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
8925331Samw {
8935331Samw 	return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
8945331Samw }
8955331Samw 
8965331Samw int
8975331Samw zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
8985331Samw {
8995331Samw 	return (zfs_unshare_proto(zhp, mountpoint, smb_only));
9005331Samw }
9015331Samw 
902789Sahrens /*
9035331Samw  * Same as zfs_unmountall(), but for NFS and SMB unshares.
904789Sahrens  */
905789Sahrens int
9065331Samw zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
907789Sahrens {
908789Sahrens 	prop_changelist_t *clp;
909789Sahrens 	int ret;
910789Sahrens 
9117366STim.Haley@Sun.COM 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
912789Sahrens 	if (clp == NULL)
913789Sahrens 		return (-1);
914789Sahrens 
9155331Samw 	ret = changelist_unshare(clp, proto);
916789Sahrens 	changelist_free(clp);
917789Sahrens 
918789Sahrens 	return (ret);
919789Sahrens }
920789Sahrens 
9215331Samw int
9225331Samw zfs_unshareall_nfs(zfs_handle_t *zhp)
9235331Samw {
9245331Samw 	return (zfs_unshareall_proto(zhp, nfs_only));
9255331Samw }
9265331Samw 
9275331Samw int
9285331Samw zfs_unshareall_smb(zfs_handle_t *zhp)
9295331Samw {
9305331Samw 	return (zfs_unshareall_proto(zhp, smb_only));
9315331Samw }
9325331Samw 
9335331Samw int
9345331Samw zfs_unshareall(zfs_handle_t *zhp)
9355331Samw {
9365331Samw 	return (zfs_unshareall_proto(zhp, share_all_proto));
9375331Samw }
9385331Samw 
9395331Samw int
9405331Samw zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
9415331Samw {
9425331Samw 	return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
9435331Samw }
9445331Samw 
945789Sahrens /*
946789Sahrens  * Remove the mountpoint associated with the current dataset, if necessary.
947789Sahrens  * We only remove the underlying directory if:
948789Sahrens  *
949789Sahrens  *	- The mountpoint is not 'none' or 'legacy'
950789Sahrens  *	- The mountpoint is non-empty
951789Sahrens  *	- The mountpoint is the default or inherited
952789Sahrens  *	- The 'zoned' property is set, or we're in a local zone
953789Sahrens  *
954789Sahrens  * Any other directories we leave alone.
955789Sahrens  */
956789Sahrens void
957789Sahrens remove_mountpoint(zfs_handle_t *zhp)
958789Sahrens {
959789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
9605094Slling 	zprop_source_t source;
961789Sahrens 
9622676Seschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
9632676Seschrock 	    &source))
964789Sahrens 		return;
965789Sahrens 
9665094Slling 	if (source == ZPROP_SRC_DEFAULT ||
9675094Slling 	    source == ZPROP_SRC_INHERITED) {
968789Sahrens 		/*
969789Sahrens 		 * Try to remove the directory, silently ignoring any errors.
970789Sahrens 		 * The filesystem may have since been removed or moved around,
9712676Seschrock 		 * and this error isn't really useful to the administrator in
9722676Seschrock 		 * any way.
973789Sahrens 		 */
974789Sahrens 		(void) rmdir(mountpoint);
975789Sahrens 	}
976789Sahrens }
9772474Seschrock 
978*13025SEric.Taylor@oracle.com void
979*13025SEric.Taylor@oracle.com libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
980*13025SEric.Taylor@oracle.com {
981*13025SEric.Taylor@oracle.com 	if (cbp->cb_alloc == cbp->cb_used) {
982*13025SEric.Taylor@oracle.com 		size_t newsz;
983*13025SEric.Taylor@oracle.com 		void *ptr;
984*13025SEric.Taylor@oracle.com 
985*13025SEric.Taylor@oracle.com 		newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
986*13025SEric.Taylor@oracle.com 		ptr = zfs_realloc(zhp->zfs_hdl,
987*13025SEric.Taylor@oracle.com 		    cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
988*13025SEric.Taylor@oracle.com 		    newsz * sizeof (void *));
989*13025SEric.Taylor@oracle.com 		cbp->cb_handles = ptr;
990*13025SEric.Taylor@oracle.com 		cbp->cb_alloc = newsz;
991*13025SEric.Taylor@oracle.com 	}
992*13025SEric.Taylor@oracle.com 	cbp->cb_handles[cbp->cb_used++] = zhp;
993*13025SEric.Taylor@oracle.com }
9942474Seschrock 
9952474Seschrock static int
9962474Seschrock mount_cb(zfs_handle_t *zhp, void *data)
9972474Seschrock {
998*13025SEric.Taylor@oracle.com 	get_all_cb_t *cbp = data;
9992474Seschrock 
1000*13025SEric.Taylor@oracle.com 	if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
10012474Seschrock 		zfs_close(zhp);
10022474Seschrock 		return (0);
10032474Seschrock 	}
10042474Seschrock 
10056168Shs24103 	if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
10066168Shs24103 		zfs_close(zhp);
10076168Shs24103 		return (0);
10086168Shs24103 	}
10096168Shs24103 
1010*13025SEric.Taylor@oracle.com 	libzfs_add_handle(cbp, zhp);
1011*13025SEric.Taylor@oracle.com 	if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1012*13025SEric.Taylor@oracle.com 		zfs_close(zhp);
1013*13025SEric.Taylor@oracle.com 		return (-1);
10142474Seschrock 	}
1015*13025SEric.Taylor@oracle.com 	return (0);
10162474Seschrock }
10172474Seschrock 
1018*13025SEric.Taylor@oracle.com int
1019*13025SEric.Taylor@oracle.com libzfs_dataset_cmp(const void *a, const void *b)
10202474Seschrock {
10212474Seschrock 	zfs_handle_t **za = (zfs_handle_t **)a;
10222474Seschrock 	zfs_handle_t **zb = (zfs_handle_t **)b;
10232474Seschrock 	char mounta[MAXPATHLEN];
10242474Seschrock 	char mountb[MAXPATHLEN];
10253126Sahl 	boolean_t gota, gotb;
10262474Seschrock 
10273126Sahl 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
10283126Sahl 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
10293126Sahl 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
10303126Sahl 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
10313126Sahl 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
10323126Sahl 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
10332474Seschrock 
10343126Sahl 	if (gota && gotb)
10353126Sahl 		return (strcmp(mounta, mountb));
10363126Sahl 
10373126Sahl 	if (gota)
10383126Sahl 		return (-1);
10393126Sahl 	if (gotb)
10403126Sahl 		return (1);
10413126Sahl 
10423126Sahl 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
10432474Seschrock }
10442474Seschrock 
10453126Sahl /*
10463126Sahl  * Mount and share all datasets within the given pool.  This assumes that no
10473126Sahl  * datasets within the pool are currently mounted.  Because users can create
10483126Sahl  * complicated nested hierarchies of mountpoints, we first gather all the
10493126Sahl  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
10503126Sahl  * we have the list of all filesystems, we iterate over them in order and mount
10513126Sahl  * and/or share each one.
10523126Sahl  */
10533126Sahl #pragma weak zpool_mount_datasets = zpool_enable_datasets
10542474Seschrock int
10553126Sahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
10562474Seschrock {
1057*13025SEric.Taylor@oracle.com 	get_all_cb_t cb = { 0 };
10582474Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10592474Seschrock 	zfs_handle_t *zfsp;
10602474Seschrock 	int i, ret = -1;
10614180Sdougm 	int *good;
10622474Seschrock 
10632474Seschrock 	/*
10646027Srm160521 	 * Gather all non-snap datasets within the pool.
10652474Seschrock 	 */
10665094Slling 	if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
10672474Seschrock 		goto out;
10682474Seschrock 
1069*13025SEric.Taylor@oracle.com 	libzfs_add_handle(&cb, zfsp);
10706027Srm160521 	if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
10712474Seschrock 		goto out;
10722474Seschrock 	/*
10732474Seschrock 	 * Sort the datasets by mountpoint.
10742474Seschrock 	 */
1075*13025SEric.Taylor@oracle.com 	qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1076*13025SEric.Taylor@oracle.com 	    libzfs_dataset_cmp);
10772474Seschrock 
10782474Seschrock 	/*
10794180Sdougm 	 * And mount all the datasets, keeping track of which ones
10808536SDavid.Pacheco@Sun.COM 	 * succeeded or failed.
10812474Seschrock 	 */
10828536SDavid.Pacheco@Sun.COM 	if ((good = zfs_alloc(zhp->zpool_hdl,
10838536SDavid.Pacheco@Sun.COM 	    cb.cb_used * sizeof (int))) == NULL)
10848536SDavid.Pacheco@Sun.COM 		goto out;
10858536SDavid.Pacheco@Sun.COM 
10862474Seschrock 	ret = 0;
10872474Seschrock 	for (i = 0; i < cb.cb_used; i++) {
1088*13025SEric.Taylor@oracle.com 		if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
10894180Sdougm 			ret = -1;
10904302Sdougm 		else
10914180Sdougm 			good[i] = 1;
10924180Sdougm 	}
10935951Sdougm 
10944180Sdougm 	/*
10954180Sdougm 	 * Then share all the ones that need to be shared. This needs
10964180Sdougm 	 * to be a separate pass in order to avoid excessive reloading
10974180Sdougm 	 * of the configuration. Good should never be NULL since
10984180Sdougm 	 * zfs_alloc is supposed to exit if memory isn't available.
10994180Sdougm 	 */
11004180Sdougm 	for (i = 0; i < cb.cb_used; i++) {
1101*13025SEric.Taylor@oracle.com 		if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
11022474Seschrock 			ret = -1;
11032474Seschrock 	}
11042474Seschrock 
11054180Sdougm 	free(good);
11064180Sdougm 
11072474Seschrock out:
11082474Seschrock 	for (i = 0; i < cb.cb_used; i++)
1109*13025SEric.Taylor@oracle.com 		zfs_close(cb.cb_handles[i]);
1110*13025SEric.Taylor@oracle.com 	free(cb.cb_handles);
11112474Seschrock 
11122474Seschrock 	return (ret);
11132474Seschrock }
11142474Seschrock 
11152474Seschrock static int
11162474Seschrock mountpoint_compare(const void *a, const void *b)
11172474Seschrock {
11182474Seschrock 	const char *mounta = *((char **)a);
11192474Seschrock 	const char *mountb = *((char **)b);
11202474Seschrock 
11212474Seschrock 	return (strcmp(mountb, mounta));
11222474Seschrock }
11232474Seschrock 
112410588SEric.Taylor@Sun.COM /* alias for 2002/240 */
112510588SEric.Taylor@Sun.COM #pragma weak zpool_unmount_datasets = zpool_disable_datasets
11263126Sahl /*
11273126Sahl  * Unshare and unmount all datasets within the given pool.  We don't want to
11283126Sahl  * rely on traversing the DSL to discover the filesystems within the pool,
11293126Sahl  * because this may be expensive (if not all of them are mounted), and can fail
11303126Sahl  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
11313126Sahl  * gather all the filesystems that are currently mounted.
11323126Sahl  */
11332474Seschrock int
11343126Sahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
11352474Seschrock {
11362474Seschrock 	int used, alloc;
11372474Seschrock 	struct mnttab entry;
11382474Seschrock 	size_t namelen;
11392474Seschrock 	char **mountpoints = NULL;
11402474Seschrock 	zfs_handle_t **datasets = NULL;
11412474Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
11422474Seschrock 	int i;
11432474Seschrock 	int ret = -1;
11442474Seschrock 	int flags = (force ? MS_FORCE : 0);
11452474Seschrock 
11462474Seschrock 	namelen = strlen(zhp->zpool_name);
11472474Seschrock 
11482474Seschrock 	rewind(hdl->libzfs_mnttab);
11492474Seschrock 	used = alloc = 0;
11502474Seschrock 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
11512474Seschrock 		/*
11522474Seschrock 		 * Ignore non-ZFS entries.
11532474Seschrock 		 */
11542474Seschrock 		if (entry.mnt_fstype == NULL ||
11552474Seschrock 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
11562474Seschrock 			continue;
11572474Seschrock 
11582474Seschrock 		/*
11592474Seschrock 		 * Ignore filesystems not within this pool.
11602474Seschrock 		 */
11612474Seschrock 		if (entry.mnt_mountp == NULL ||
11622474Seschrock 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
11632474Seschrock 		    (entry.mnt_special[namelen] != '/' &&
11642474Seschrock 		    entry.mnt_special[namelen] != '\0'))
11652474Seschrock 			continue;
11662474Seschrock 
11672474Seschrock 		/*
11682474Seschrock 		 * At this point we've found a filesystem within our pool.  Add
11692474Seschrock 		 * it to our growing list.
11702474Seschrock 		 */
11712474Seschrock 		if (used == alloc) {
11722474Seschrock 			if (alloc == 0) {
11732474Seschrock 				if ((mountpoints = zfs_alloc(hdl,
11742474Seschrock 				    8 * sizeof (void *))) == NULL)
11752474Seschrock 					goto out;
11762474Seschrock 
11772474Seschrock 				if ((datasets = zfs_alloc(hdl,
11782474Seschrock 				    8 * sizeof (void *))) == NULL)
11792474Seschrock 					goto out;
11802474Seschrock 
11812474Seschrock 				alloc = 8;
11822474Seschrock 			} else {
11832676Seschrock 				void *ptr;
11842474Seschrock 
11852676Seschrock 				if ((ptr = zfs_realloc(hdl, mountpoints,
11862676Seschrock 				    alloc * sizeof (void *),
11872474Seschrock 				    alloc * 2 * sizeof (void *))) == NULL)
11882474Seschrock 					goto out;
11892676Seschrock 				mountpoints = ptr;
11902474Seschrock 
11912676Seschrock 				if ((ptr = zfs_realloc(hdl, datasets,
11922676Seschrock 				    alloc * sizeof (void *),
11932474Seschrock 				    alloc * 2 * sizeof (void *))) == NULL)
11942474Seschrock 					goto out;
11952676Seschrock 				datasets = ptr;
11962474Seschrock 
11972474Seschrock 				alloc *= 2;
11982474Seschrock 			}
11992474Seschrock 		}
12002474Seschrock 
12012474Seschrock 		if ((mountpoints[used] = zfs_strdup(hdl,
12022474Seschrock 		    entry.mnt_mountp)) == NULL)
12032474Seschrock 			goto out;
12042474Seschrock 
12052474Seschrock 		/*
12062474Seschrock 		 * This is allowed to fail, in case there is some I/O error.  It
12072474Seschrock 		 * is only used to determine if we need to remove the underlying
12082474Seschrock 		 * mountpoint, so failure is not fatal.
12092474Seschrock 		 */
12102474Seschrock 		datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
12112474Seschrock 
12122474Seschrock 		used++;
12132474Seschrock 	}
12142474Seschrock 
12152474Seschrock 	/*
12162474Seschrock 	 * At this point, we have the entire list of filesystems, so sort it by
12172474Seschrock 	 * mountpoint.
12182474Seschrock 	 */
12192474Seschrock 	qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
12202474Seschrock 
12212474Seschrock 	/*
12222474Seschrock 	 * Walk through and first unshare everything.
12232474Seschrock 	 */
12242474Seschrock 	for (i = 0; i < used; i++) {
12255331Samw 		zfs_share_proto_t *curr_proto;
12265331Samw 		for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
12275331Samw 		    curr_proto++) {
12285331Samw 			if (is_shared(hdl, mountpoints[i], *curr_proto) &&
12295331Samw 			    unshare_one(hdl, mountpoints[i],
12305331Samw 			    mountpoints[i], *curr_proto) != 0)
12315331Samw 				goto out;
12325331Samw 		}
12332474Seschrock 	}
12342474Seschrock 
12352474Seschrock 	/*
12362474Seschrock 	 * Now unmount everything, removing the underlying directories as
12372474Seschrock 	 * appropriate.
12382474Seschrock 	 */
12392474Seschrock 	for (i = 0; i < used; i++) {
12402474Seschrock 		if (unmount_one(hdl, mountpoints[i], flags) != 0)
12412474Seschrock 			goto out;
12422676Seschrock 	}
12432474Seschrock 
12442676Seschrock 	for (i = 0; i < used; i++) {
12452474Seschrock 		if (datasets[i])
12462474Seschrock 			remove_mountpoint(datasets[i]);
12472474Seschrock 	}
12482474Seschrock 
12492474Seschrock 	ret = 0;
12502474Seschrock out:
12512474Seschrock 	for (i = 0; i < used; i++) {
12522474Seschrock 		if (datasets[i])
12532474Seschrock 			zfs_close(datasets[i]);
12542474Seschrock 		free(mountpoints[i]);
12552474Seschrock 	}
12562474Seschrock 	free(datasets);
12572474Seschrock 	free(mountpoints);
12582474Seschrock 
12592474Seschrock 	return (ret);
12602474Seschrock }
1261