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 /*
23*12070SMark.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];
318*12070SMark.Shellenbaum@Sun.COM 			int spa_version;
31911814SChris.Kirby@sun.com 
320*12070SMark.Shellenbaum@Sun.COM 			VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
32111814SChris.Kirby@sun.com 			(void) snprintf(buf, sizeof (buf),
322*12070SMark.Shellenbaum@Sun.COM 			    dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
323*12070SMark.Shellenbaum@Sun.COM 			    "file system on a version %d pool. Pool must be"
324*12070SMark.Shellenbaum@Sun.COM 			    " upgraded to mount this file system."),
32511814SChris.Kirby@sun.com 			    (u_longlong_t)zfs_prop_get_int(zhp,
326*12070SMark.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 {
4403126Sahl 	if (ZFS_IS_VOLUME(zhp))
44111876SJames.Dunham@Sun.COM 		return (0);
4423126Sahl 
4435331Samw 	return (zfs_share_proto(zhp, share_all_proto));
4443126Sahl }
4453126Sahl 
4463126Sahl int
4473126Sahl zfs_unshare(zfs_handle_t *zhp)
4483126Sahl {
4493126Sahl 	if (ZFS_IS_VOLUME(zhp))
45011876SJames.Dunham@Sun.COM 		return (0);
4513126Sahl 
4525331Samw 	return (zfs_unshareall(zhp));
4533126Sahl }
4543126Sahl 
455789Sahrens /*
456789Sahrens  * Check to see if the filesystem is currently shared.
457789Sahrens  */
4585331Samw zfs_share_type_t
4595331Samw zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
460789Sahrens {
461789Sahrens 	char *mountpoint;
4625331Samw 	zfs_share_type_t rc;
463789Sahrens 
464789Sahrens 	if (!zfs_is_mounted(zhp, &mountpoint))
4655331Samw 		return (SHARED_NOT_SHARED);
466789Sahrens 
4675331Samw 	if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) {
468789Sahrens 		if (where != NULL)
469789Sahrens 			*where = mountpoint;
470789Sahrens 		else
471789Sahrens 			free(mountpoint);
4725331Samw 		return (rc);
473789Sahrens 	} else {
474789Sahrens 		free(mountpoint);
4755331Samw 		return (SHARED_NOT_SHARED);
476789Sahrens 	}
477789Sahrens }
478789Sahrens 
4795331Samw boolean_t
4805331Samw zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
4815331Samw {
4825331Samw 	return (zfs_is_shared_proto(zhp, where,
4835331Samw 	    PROTO_NFS) != SHARED_NOT_SHARED);
4845331Samw }
4855331Samw 
4865331Samw boolean_t
4875331Samw zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
4885331Samw {
4895331Samw 	return (zfs_is_shared_proto(zhp, where,
4905331Samw 	    PROTO_SMB) != SHARED_NOT_SHARED);
4915331Samw }
4925331Samw 
493789Sahrens /*
4944180Sdougm  * Make sure things will work if libshare isn't installed by using
4954180Sdougm  * wrapper functions that check to see that the pointers to functions
4964180Sdougm  * initialized in _zfs_init_libshare() are actually present.
4974180Sdougm  */
4984180Sdougm 
4994180Sdougm static sa_handle_t (*_sa_init)(int);
5004180Sdougm static void (*_sa_fini)(sa_handle_t);
5014180Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
5024180Sdougm static int (*_sa_enable_share)(sa_share_t, char *);
5034180Sdougm static int (*_sa_disable_share)(sa_share_t, char *);
5044180Sdougm static char *(*_sa_errorstr)(int);
5054180Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
5065951Sdougm static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
5075951Sdougm static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
5085951Sdougm static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
5095951Sdougm     char *, char *, zprop_source_t, char *, char *, char *);
5105951Sdougm static void (*_sa_update_sharetab_ts)(sa_handle_t);
5114180Sdougm 
5124180Sdougm /*
5134180Sdougm  * _zfs_init_libshare()
5144180Sdougm  *
5154180Sdougm  * Find the libshare.so.1 entry points that we use here and save the
5164180Sdougm  * values to be used later. This is triggered by the runtime loader.
5174180Sdougm  * Make sure the correct ISA version is loaded.
5184180Sdougm  */
5195951Sdougm 
5204180Sdougm #pragma init(_zfs_init_libshare)
5214180Sdougm static void
5224180Sdougm _zfs_init_libshare(void)
5234180Sdougm {
5244180Sdougm 	void *libshare;
5254180Sdougm 	char path[MAXPATHLEN];
5264180Sdougm 	char isa[MAXISALEN];
5274180Sdougm 
5284180Sdougm #if defined(_LP64)
5294180Sdougm 	if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
5304302Sdougm 		isa[0] = '\0';
5314180Sdougm #else
5324180Sdougm 	isa[0] = '\0';
5334180Sdougm #endif
5344180Sdougm 	(void) snprintf(path, MAXPATHLEN,
5354302Sdougm 	    "/usr/lib/%s/libshare.so.1", isa);
5364180Sdougm 
5374180Sdougm 	if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
5384302Sdougm 		_sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
5394302Sdougm 		_sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
5404302Sdougm 		_sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
5414302Sdougm 		    dlsym(libshare, "sa_find_share");
5424302Sdougm 		_sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
5434302Sdougm 		    "sa_enable_share");
5444302Sdougm 		_sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
5454302Sdougm 		    "sa_disable_share");
5464302Sdougm 		_sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
5474302Sdougm 		_sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
5484302Sdougm 		    dlsym(libshare, "sa_parse_legacy_options");
5495951Sdougm 		_sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
5505951Sdougm 		    dlsym(libshare, "sa_needs_refresh");
5515951Sdougm 		_sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
5525951Sdougm 		    dlsym(libshare, "sa_get_zfs_handle");
5535951Sdougm 		_sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
5545951Sdougm 		    sa_share_t, char *, char *, zprop_source_t, char *,
5555951Sdougm 		    char *, char *))dlsym(libshare, "sa_zfs_process_share");
5565951Sdougm 		_sa_update_sharetab_ts = (void (*)(sa_handle_t))
5575951Sdougm 		    dlsym(libshare, "sa_update_sharetab_ts");
5584327Sdougm 		if (_sa_init == NULL || _sa_fini == NULL ||
5594327Sdougm 		    _sa_find_share == NULL || _sa_enable_share == NULL ||
5604327Sdougm 		    _sa_disable_share == NULL || _sa_errorstr == NULL ||
5615951Sdougm 		    _sa_parse_legacy_options == NULL ||
5625951Sdougm 		    _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
5635951Sdougm 		    _sa_zfs_process_share == NULL ||
5645951Sdougm 		    _sa_update_sharetab_ts == NULL) {
5654327Sdougm 			_sa_init = NULL;
5664327Sdougm 			_sa_fini = NULL;
5674327Sdougm 			_sa_disable_share = NULL;
5684327Sdougm 			_sa_enable_share = NULL;
5694327Sdougm 			_sa_errorstr = NULL;
5704327Sdougm 			_sa_parse_legacy_options = NULL;
5714327Sdougm 			(void) dlclose(libshare);
5725951Sdougm 			_sa_needs_refresh = NULL;
5735951Sdougm 			_sa_get_zfs_handle = NULL;
5745951Sdougm 			_sa_zfs_process_share = NULL;
5755951Sdougm 			_sa_update_sharetab_ts = NULL;
5764327Sdougm 		}
5774180Sdougm 	}
5784180Sdougm }
5794180Sdougm 
5804180Sdougm /*
5814180Sdougm  * zfs_init_libshare(zhandle, service)
5824180Sdougm  *
5834180Sdougm  * Initialize the libshare API if it hasn't already been initialized.
5844180Sdougm  * In all cases it returns 0 if it succeeded and an error if not. The
5854180Sdougm  * service value is which part(s) of the API to initialize and is a
5864180Sdougm  * direct map to the libshare sa_init(service) interface.
5874180Sdougm  */
5884180Sdougm int
5894180Sdougm zfs_init_libshare(libzfs_handle_t *zhandle, int service)
5904180Sdougm {
5914180Sdougm 	int ret = SA_OK;
5924180Sdougm 
5934302Sdougm 	if (_sa_init == NULL)
5944302Sdougm 		ret = SA_CONFIG_ERR;
5954302Sdougm 
5965951Sdougm 	if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
5975951Sdougm 		/*
5985951Sdougm 		 * We had a cache miss. Most likely it is a new ZFS
5995951Sdougm 		 * dataset that was just created. We want to make sure
6005951Sdougm 		 * so check timestamps to see if a different process
6015951Sdougm 		 * has updated any of the configuration. If there was
6025951Sdougm 		 * some non-ZFS change, we need to re-initialize the
6035951Sdougm 		 * internal cache.
6045951Sdougm 		 */
6055951Sdougm 		zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
6065951Sdougm 		if (_sa_needs_refresh != NULL &&
6075951Sdougm 		    _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
6085951Sdougm 			zfs_uninit_libshare(zhandle);
6095951Sdougm 			zhandle->libzfs_sharehdl = _sa_init(service);
6105951Sdougm 		}
6115951Sdougm 	}
6125951Sdougm 
6134302Sdougm 	if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
6144302Sdougm 		zhandle->libzfs_sharehdl = _sa_init(service);
6154302Sdougm 
6164302Sdougm 	if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
6174302Sdougm 		ret = SA_NO_MEMORY;
6184302Sdougm 
6194180Sdougm 	return (ret);
6204180Sdougm }
6214180Sdougm 
6224180Sdougm /*
6234180Sdougm  * zfs_uninit_libshare(zhandle)
6244180Sdougm  *
6254180Sdougm  * Uninitialize the libshare API if it hasn't already been
6264180Sdougm  * uninitialized. It is OK to call multiple times.
6274180Sdougm  */
6284180Sdougm void
6294180Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle)
6304180Sdougm {
6314180Sdougm 	if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
6324302Sdougm 		if (_sa_fini != NULL)
6334302Sdougm 			_sa_fini(zhandle->libzfs_sharehdl);
6344302Sdougm 		zhandle->libzfs_sharehdl = NULL;
6354180Sdougm 	}
6364180Sdougm }
6374180Sdougm 
6384180Sdougm /*
6394180Sdougm  * zfs_parse_options(options, proto)
6404180Sdougm  *
6414180Sdougm  * Call the legacy parse interface to get the protocol specific
6424180Sdougm  * options using the NULL arg to indicate that this is a "parse" only.
6434180Sdougm  */
6444180Sdougm int
6455331Samw zfs_parse_options(char *options, zfs_share_proto_t proto)
6464180Sdougm {
6475367Sahrens 	if (_sa_parse_legacy_options != NULL) {
6485367Sahrens 		return (_sa_parse_legacy_options(NULL, options,
6495367Sahrens 		    proto_table[proto].p_name));
6505367Sahrens 	}
6515367Sahrens 	return (SA_CONFIG_ERR);
6524180Sdougm }
6534180Sdougm 
6544180Sdougm /*
6554180Sdougm  * zfs_sa_find_share(handle, path)
6564180Sdougm  *
6574180Sdougm  * wrapper around sa_find_share to find a share path in the
6584180Sdougm  * configuration.
6594180Sdougm  */
6604180Sdougm static sa_share_t
6614180Sdougm zfs_sa_find_share(sa_handle_t handle, char *path)
6624180Sdougm {
6634180Sdougm 	if (_sa_find_share != NULL)
6644302Sdougm 		return (_sa_find_share(handle, path));
6654180Sdougm 	return (NULL);
6664180Sdougm }
6674180Sdougm 
6684180Sdougm /*
6694180Sdougm  * zfs_sa_enable_share(share, proto)
6704180Sdougm  *
6714180Sdougm  * Wrapper for sa_enable_share which enables a share for a specified
6724180Sdougm  * protocol.
6734180Sdougm  */
6744180Sdougm static int
6754180Sdougm zfs_sa_enable_share(sa_share_t share, char *proto)
6764180Sdougm {
6774180Sdougm 	if (_sa_enable_share != NULL)
6784302Sdougm 		return (_sa_enable_share(share, proto));
6794180Sdougm 	return (SA_CONFIG_ERR);
6804180Sdougm }
6814180Sdougm 
6824180Sdougm /*
6834180Sdougm  * zfs_sa_disable_share(share, proto)
6844180Sdougm  *
6854180Sdougm  * Wrapper for sa_enable_share which disables a share for a specified
6864180Sdougm  * protocol.
6874180Sdougm  */
6884180Sdougm static int
6894180Sdougm zfs_sa_disable_share(sa_share_t share, char *proto)
6904180Sdougm {
6914180Sdougm 	if (_sa_disable_share != NULL)
6924302Sdougm 		return (_sa_disable_share(share, proto));
6934180Sdougm 	return (SA_CONFIG_ERR);
6944180Sdougm }
6954180Sdougm 
6964180Sdougm /*
6975331Samw  * Share the given filesystem according to the options in the specified
6985331Samw  * protocol specific properties (sharenfs, sharesmb).  We rely
6994180Sdougm  * on "libshare" to the dirty work for us.
700789Sahrens  */
7015331Samw static int
7025331Samw zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
703789Sahrens {
704789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
705789Sahrens 	char shareopts[ZFS_MAXPROPLEN];
7065951Sdougm 	char sourcestr[ZFS_MAXPROPLEN];
7072082Seschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
7084180Sdougm 	sa_share_t share;
7095331Samw 	zfs_share_proto_t *curr_proto;
7105951Sdougm 	zprop_source_t sourcetype;
7114180Sdougm 	int ret;
712789Sahrens 
7132676Seschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
714789Sahrens 		return (0);
715789Sahrens 
7164180Sdougm 	if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
7174302Sdougm 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
7184302Sdougm 		    dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
7195951Sdougm 		    zfs_get_name(zhp), _sa_errorstr != NULL ?
7205951Sdougm 		    _sa_errorstr(ret) : "");
7214302Sdougm 		return (-1);
7224180Sdougm 	}
7235331Samw 
7245331Samw 	for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
7255331Samw 		/*
7265331Samw 		 * Return success if there are no share options.
7275331Samw 		 */
7285331Samw 		if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
7295951Sdougm 		    shareopts, sizeof (shareopts), &sourcetype, sourcestr,
7305951Sdougm 		    ZFS_MAXPROPLEN, B_FALSE) != 0 ||
7315951Sdougm 		    strcmp(shareopts, "off") == 0)
7325331Samw 			continue;
7335331Samw 
7345331Samw 		/*
7355331Samw 		 * If the 'zoned' property is set, then zfs_is_mountable()
7365331Samw 		 * will have already bailed out if we are in the global zone.
7375331Samw 		 * But local zones cannot be NFS servers, so we ignore it for
7385331Samw 		 * local zones as well.
7395331Samw 		 */
7405331Samw 		if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
7415331Samw 			continue;
7425331Samw 
7435331Samw 		share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
7445951Sdougm 		if (share == NULL) {
7455951Sdougm 			/*
7465951Sdougm 			 * This may be a new file system that was just
7475951Sdougm 			 * created so isn't in the internal cache
7485951Sdougm 			 * (second time through). Rather than
7495951Sdougm 			 * reloading the entire configuration, we can
7505951Sdougm 			 * assume ZFS has done the checking and it is
7515951Sdougm 			 * safe to add this to the internal
7525951Sdougm 			 * configuration.
7535951Sdougm 			 */
7545951Sdougm 			if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
7555951Sdougm 			    NULL, NULL, mountpoint,
7565951Sdougm 			    proto_table[*curr_proto].p_name, sourcetype,
7575951Sdougm 			    shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
7585951Sdougm 				(void) zfs_error_fmt(hdl,
7595951Sdougm 				    proto_table[*curr_proto].p_share_err,
7605951Sdougm 				    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
7615951Sdougm 				    zfs_get_name(zhp));
7625951Sdougm 				return (-1);
7635951Sdougm 			}
7645951Sdougm 			hdl->libzfs_shareflags |= ZFSSHARE_MISS;
7655951Sdougm 			share = zfs_sa_find_share(hdl->libzfs_sharehdl,
7665951Sdougm 			    mountpoint);
7675951Sdougm 		}
7685331Samw 		if (share != NULL) {
7695331Samw 			int err;
7705331Samw 			err = zfs_sa_enable_share(share,
7715331Samw 			    proto_table[*curr_proto].p_name);
7725331Samw 			if (err != SA_OK) {
7735331Samw 				(void) zfs_error_fmt(hdl,
7745331Samw 				    proto_table[*curr_proto].p_share_err,
7755331Samw 				    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
7765331Samw 				    zfs_get_name(zhp));
7775331Samw 				return (-1);
7785331Samw 			}
7795331Samw 		} else {
7805331Samw 			(void) zfs_error_fmt(hdl,
7815331Samw 			    proto_table[*curr_proto].p_share_err,
7824302Sdougm 			    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
7834302Sdougm 			    zfs_get_name(zhp));
7844302Sdougm 			return (-1);
7854302Sdougm 		}
7865331Samw 
787789Sahrens 	}
7885331Samw 	return (0);
7895331Samw }
790789Sahrens 
7915331Samw 
7925331Samw int
7935331Samw zfs_share_nfs(zfs_handle_t *zhp)
7945331Samw {
7955331Samw 	return (zfs_share_proto(zhp, nfs_only));
7965331Samw }
7975331Samw 
7985331Samw int
7995331Samw zfs_share_smb(zfs_handle_t *zhp)
8005331Samw {
8015331Samw 	return (zfs_share_proto(zhp, smb_only));
8025331Samw }
8035331Samw 
8045331Samw int
8055331Samw zfs_shareall(zfs_handle_t *zhp)
8065331Samw {
8075331Samw 	return (zfs_share_proto(zhp, share_all_proto));
808789Sahrens }
809789Sahrens 
810789Sahrens /*
8112474Seschrock  * Unshare a filesystem by mountpoint.
8122474Seschrock  */
8132474Seschrock static int
8145331Samw unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
8155331Samw     zfs_share_proto_t proto)
8162474Seschrock {
8174180Sdougm 	sa_share_t share;
8184180Sdougm 	int err;
8194180Sdougm 	char *mntpt;
8202474Seschrock 	/*
8214180Sdougm 	 * Mountpoint could get trashed if libshare calls getmntany
8228228SEric.Taylor@Sun.COM 	 * which it does during API initialization, so strdup the
8234180Sdougm 	 * value.
8242474Seschrock 	 */
8254180Sdougm 	mntpt = zfs_strdup(hdl, mountpoint);
8262474Seschrock 
8274180Sdougm 	/* make sure libshare initialized */
8284180Sdougm 	if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
8294180Sdougm 		free(mntpt);	/* don't need the copy anymore */
8304180Sdougm 		return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
8314302Sdougm 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
8324302Sdougm 		    name, _sa_errorstr(err)));
8332474Seschrock 	}
8342474Seschrock 
8354180Sdougm 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
8364180Sdougm 	free(mntpt);	/* don't need the copy anymore */
8372474Seschrock 
8384180Sdougm 	if (share != NULL) {
8395331Samw 		err = zfs_sa_disable_share(share, proto_table[proto].p_name);
8404180Sdougm 		if (err != SA_OK) {
8414302Sdougm 			return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
8424302Sdougm 			    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
8434302Sdougm 			    name, _sa_errorstr(err)));
8444180Sdougm 		}
8454180Sdougm 	} else {
8464180Sdougm 		return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
8474302Sdougm 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
8484302Sdougm 		    name));
8494180Sdougm 	}
8502474Seschrock 	return (0);
8512474Seschrock }
8522474Seschrock 
8532474Seschrock /*
854789Sahrens  * Unshare the given filesystem.
855789Sahrens  */
856789Sahrens int
8575331Samw zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
8585331Samw     zfs_share_proto_t *proto)
859789Sahrens {
8608228SEric.Taylor@Sun.COM 	libzfs_handle_t *hdl = zhp->zfs_hdl;
8618228SEric.Taylor@Sun.COM 	struct mnttab entry;
8624180Sdougm 	char *mntpt = NULL;
863789Sahrens 
864789Sahrens 	/* check to see if need to unmount the filesystem */
8652082Seschrock 	rewind(zhp->zfs_hdl->libzfs_mnttab);
8664302Sdougm 	if (mountpoint != NULL)
8678228SEric.Taylor@Sun.COM 		mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
8684302Sdougm 
869789Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
8708228SEric.Taylor@Sun.COM 	    libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
8715331Samw 		zfs_share_proto_t *curr_proto;
872789Sahrens 
873789Sahrens 		if (mountpoint == NULL)
8745331Samw 			mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
8755331Samw 
8765331Samw 		for (curr_proto = proto; *curr_proto != PROTO_END;
8775331Samw 		    curr_proto++) {
878789Sahrens 
8798228SEric.Taylor@Sun.COM 			if (is_shared(hdl, mntpt, *curr_proto) &&
8808228SEric.Taylor@Sun.COM 			    unshare_one(hdl, zhp->zfs_name,
8815331Samw 			    mntpt, *curr_proto) != 0) {
8825331Samw 				if (mntpt != NULL)
8835331Samw 					free(mntpt);
8845331Samw 				return (-1);
8855331Samw 			}
8864180Sdougm 		}
887789Sahrens 	}
8884180Sdougm 	if (mntpt != NULL)
8894302Sdougm 		free(mntpt);
890789Sahrens 
891789Sahrens 	return (0);
892789Sahrens }
893789Sahrens 
8945331Samw int
8955331Samw zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
8965331Samw {
8975331Samw 	return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
8985331Samw }
8995331Samw 
9005331Samw int
9015331Samw zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
9025331Samw {
9035331Samw 	return (zfs_unshare_proto(zhp, mountpoint, smb_only));
9045331Samw }
9055331Samw 
906789Sahrens /*
9075331Samw  * Same as zfs_unmountall(), but for NFS and SMB unshares.
908789Sahrens  */
909789Sahrens int
9105331Samw zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
911789Sahrens {
912789Sahrens 	prop_changelist_t *clp;
913789Sahrens 	int ret;
914789Sahrens 
9157366STim.Haley@Sun.COM 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
916789Sahrens 	if (clp == NULL)
917789Sahrens 		return (-1);
918789Sahrens 
9195331Samw 	ret = changelist_unshare(clp, proto);
920789Sahrens 	changelist_free(clp);
921789Sahrens 
922789Sahrens 	return (ret);
923789Sahrens }
924789Sahrens 
9255331Samw int
9265331Samw zfs_unshareall_nfs(zfs_handle_t *zhp)
9275331Samw {
9285331Samw 	return (zfs_unshareall_proto(zhp, nfs_only));
9295331Samw }
9305331Samw 
9315331Samw int
9325331Samw zfs_unshareall_smb(zfs_handle_t *zhp)
9335331Samw {
9345331Samw 	return (zfs_unshareall_proto(zhp, smb_only));
9355331Samw }
9365331Samw 
9375331Samw int
9385331Samw zfs_unshareall(zfs_handle_t *zhp)
9395331Samw {
9405331Samw 	return (zfs_unshareall_proto(zhp, share_all_proto));
9415331Samw }
9425331Samw 
9435331Samw int
9445331Samw zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
9455331Samw {
9465331Samw 	return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
9475331Samw }
9485331Samw 
949789Sahrens /*
950789Sahrens  * Remove the mountpoint associated with the current dataset, if necessary.
951789Sahrens  * We only remove the underlying directory if:
952789Sahrens  *
953789Sahrens  *	- The mountpoint is not 'none' or 'legacy'
954789Sahrens  *	- The mountpoint is non-empty
955789Sahrens  *	- The mountpoint is the default or inherited
956789Sahrens  *	- The 'zoned' property is set, or we're in a local zone
957789Sahrens  *
958789Sahrens  * Any other directories we leave alone.
959789Sahrens  */
960789Sahrens void
961789Sahrens remove_mountpoint(zfs_handle_t *zhp)
962789Sahrens {
963789Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
9645094Slling 	zprop_source_t source;
965789Sahrens 
9662676Seschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
9672676Seschrock 	    &source))
968789Sahrens 		return;
969789Sahrens 
9705094Slling 	if (source == ZPROP_SRC_DEFAULT ||
9715094Slling 	    source == ZPROP_SRC_INHERITED) {
972789Sahrens 		/*
973789Sahrens 		 * Try to remove the directory, silently ignoring any errors.
974789Sahrens 		 * The filesystem may have since been removed or moved around,
9752676Seschrock 		 * and this error isn't really useful to the administrator in
9762676Seschrock 		 * any way.
977789Sahrens 		 */
978789Sahrens 		(void) rmdir(mountpoint);
979789Sahrens 	}
980789Sahrens }
9812474Seschrock 
9822474Seschrock typedef struct mount_cbdata {
9832474Seschrock 	zfs_handle_t	**cb_datasets;
9842474Seschrock 	int 		cb_used;
9852474Seschrock 	int		cb_alloc;
9862474Seschrock } mount_cbdata_t;
9872474Seschrock 
9882474Seschrock static int
9892474Seschrock mount_cb(zfs_handle_t *zhp, void *data)
9902474Seschrock {
9912474Seschrock 	mount_cbdata_t *cbp = data;
9922474Seschrock 
9933126Sahl 	if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
9942474Seschrock 		zfs_close(zhp);
9952474Seschrock 		return (0);
9962474Seschrock 	}
9972474Seschrock 
9986168Shs24103 	if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
9996168Shs24103 		zfs_close(zhp);
10006168Shs24103 		return (0);
10016168Shs24103 	}
10026168Shs24103 
10032474Seschrock 	if (cbp->cb_alloc == cbp->cb_used) {
10042676Seschrock 		void *ptr;
10052474Seschrock 
10062676Seschrock 		if ((ptr = zfs_realloc(zhp->zfs_hdl,
10072676Seschrock 		    cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
10082676Seschrock 		    cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
10092474Seschrock 			return (-1);
10102676Seschrock 		cbp->cb_datasets = ptr;
10112474Seschrock 
10122676Seschrock 		cbp->cb_alloc *= 2;
10132474Seschrock 	}
10142474Seschrock 
10152474Seschrock 	cbp->cb_datasets[cbp->cb_used++] = zhp;
10163126Sahl 
10176027Srm160521 	return (zfs_iter_filesystems(zhp, mount_cb, cbp));
10182474Seschrock }
10192474Seschrock 
10202474Seschrock static int
10213126Sahl dataset_cmp(const void *a, const void *b)
10222474Seschrock {
10232474Seschrock 	zfs_handle_t **za = (zfs_handle_t **)a;
10242474Seschrock 	zfs_handle_t **zb = (zfs_handle_t **)b;
10252474Seschrock 	char mounta[MAXPATHLEN];
10262474Seschrock 	char mountb[MAXPATHLEN];
10273126Sahl 	boolean_t gota, gotb;
10282474Seschrock 
10293126Sahl 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
10303126Sahl 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
10313126Sahl 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
10323126Sahl 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
10333126Sahl 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
10343126Sahl 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
10352474Seschrock 
10363126Sahl 	if (gota && gotb)
10373126Sahl 		return (strcmp(mounta, mountb));
10383126Sahl 
10393126Sahl 	if (gota)
10403126Sahl 		return (-1);
10413126Sahl 	if (gotb)
10423126Sahl 		return (1);
10433126Sahl 
10443126Sahl 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
10452474Seschrock }
10462474Seschrock 
10473126Sahl /*
10483126Sahl  * Mount and share all datasets within the given pool.  This assumes that no
10493126Sahl  * datasets within the pool are currently mounted.  Because users can create
10503126Sahl  * complicated nested hierarchies of mountpoints, we first gather all the
10513126Sahl  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
10523126Sahl  * we have the list of all filesystems, we iterate over them in order and mount
10533126Sahl  * and/or share each one.
10543126Sahl  */
10553126Sahl #pragma weak zpool_mount_datasets = zpool_enable_datasets
10562474Seschrock int
10573126Sahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
10582474Seschrock {
10592474Seschrock 	mount_cbdata_t cb = { 0 };
10602474Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10612474Seschrock 	zfs_handle_t *zfsp;
10622474Seschrock 	int i, ret = -1;
10634180Sdougm 	int *good;
10642474Seschrock 
10652474Seschrock 	/*
10666027Srm160521 	 * Gather all non-snap datasets within the pool.
10672474Seschrock 	 */
10682474Seschrock 	if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
10692474Seschrock 		return (-1);
10702474Seschrock 	cb.cb_alloc = 4;
10712474Seschrock 
10725094Slling 	if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
10732474Seschrock 		goto out;
10742474Seschrock 
10752474Seschrock 	cb.cb_datasets[0] = zfsp;
10762474Seschrock 	cb.cb_used = 1;
10772474Seschrock 
10786027Srm160521 	if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
10792474Seschrock 		goto out;
10802474Seschrock 
10812474Seschrock 	/*
10822474Seschrock 	 * Sort the datasets by mountpoint.
10832474Seschrock 	 */
10843126Sahl 	qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
10852474Seschrock 
10862474Seschrock 	/*
10874180Sdougm 	 * And mount all the datasets, keeping track of which ones
10888536SDavid.Pacheco@Sun.COM 	 * succeeded or failed.
10892474Seschrock 	 */
10908536SDavid.Pacheco@Sun.COM 	if ((good = zfs_alloc(zhp->zpool_hdl,
10918536SDavid.Pacheco@Sun.COM 	    cb.cb_used * sizeof (int))) == NULL)
10928536SDavid.Pacheco@Sun.COM 		goto out;
10938536SDavid.Pacheco@Sun.COM 
10942474Seschrock 	ret = 0;
10952474Seschrock 	for (i = 0; i < cb.cb_used; i++) {
10964302Sdougm 		if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0)
10974180Sdougm 			ret = -1;
10984302Sdougm 		else
10994180Sdougm 			good[i] = 1;
11004180Sdougm 	}
11015951Sdougm 
11024180Sdougm 	/*
11034180Sdougm 	 * Then share all the ones that need to be shared. This needs
11044180Sdougm 	 * to be a separate pass in order to avoid excessive reloading
11054180Sdougm 	 * of the configuration. Good should never be NULL since
11064180Sdougm 	 * zfs_alloc is supposed to exit if memory isn't available.
11074180Sdougm 	 */
11084180Sdougm 	for (i = 0; i < cb.cb_used; i++) {
11094180Sdougm 		if (good[i] && zfs_share(cb.cb_datasets[i]) != 0)
11102474Seschrock 			ret = -1;
11112474Seschrock 	}
11122474Seschrock 
11134180Sdougm 	free(good);
11144180Sdougm 
11152474Seschrock out:
11162474Seschrock 	for (i = 0; i < cb.cb_used; i++)
11172474Seschrock 		zfs_close(cb.cb_datasets[i]);
11182474Seschrock 	free(cb.cb_datasets);
11192474Seschrock 
11202474Seschrock 	return (ret);
11212474Seschrock }
11222474Seschrock 
11232474Seschrock static int
11242474Seschrock mountpoint_compare(const void *a, const void *b)
11252474Seschrock {
11262474Seschrock 	const char *mounta = *((char **)a);
11272474Seschrock 	const char *mountb = *((char **)b);
11282474Seschrock 
11292474Seschrock 	return (strcmp(mountb, mounta));
11302474Seschrock }
11312474Seschrock 
113210588SEric.Taylor@Sun.COM /* alias for 2002/240 */
113310588SEric.Taylor@Sun.COM #pragma weak zpool_unmount_datasets = zpool_disable_datasets
11343126Sahl /*
11353126Sahl  * Unshare and unmount all datasets within the given pool.  We don't want to
11363126Sahl  * rely on traversing the DSL to discover the filesystems within the pool,
11373126Sahl  * because this may be expensive (if not all of them are mounted), and can fail
11383126Sahl  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
11393126Sahl  * gather all the filesystems that are currently mounted.
11403126Sahl  */
11412474Seschrock int
11423126Sahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
11432474Seschrock {
11442474Seschrock 	int used, alloc;
11452474Seschrock 	struct mnttab entry;
11462474Seschrock 	size_t namelen;
11472474Seschrock 	char **mountpoints = NULL;
11482474Seschrock 	zfs_handle_t **datasets = NULL;
11492474Seschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
11502474Seschrock 	int i;
11512474Seschrock 	int ret = -1;
11522474Seschrock 	int flags = (force ? MS_FORCE : 0);
11532474Seschrock 
11542474Seschrock 	namelen = strlen(zhp->zpool_name);
11552474Seschrock 
11562474Seschrock 	rewind(hdl->libzfs_mnttab);
11572474Seschrock 	used = alloc = 0;
11582474Seschrock 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
11592474Seschrock 		/*
11602474Seschrock 		 * Ignore non-ZFS entries.
11612474Seschrock 		 */
11622474Seschrock 		if (entry.mnt_fstype == NULL ||
11632474Seschrock 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
11642474Seschrock 			continue;
11652474Seschrock 
11662474Seschrock 		/*
11672474Seschrock 		 * Ignore filesystems not within this pool.
11682474Seschrock 		 */
11692474Seschrock 		if (entry.mnt_mountp == NULL ||
11702474Seschrock 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
11712474Seschrock 		    (entry.mnt_special[namelen] != '/' &&
11722474Seschrock 		    entry.mnt_special[namelen] != '\0'))
11732474Seschrock 			continue;
11742474Seschrock 
11752474Seschrock 		/*
11762474Seschrock 		 * At this point we've found a filesystem within our pool.  Add
11772474Seschrock 		 * it to our growing list.
11782474Seschrock 		 */
11792474Seschrock 		if (used == alloc) {
11802474Seschrock 			if (alloc == 0) {
11812474Seschrock 				if ((mountpoints = zfs_alloc(hdl,
11822474Seschrock 				    8 * sizeof (void *))) == NULL)
11832474Seschrock 					goto out;
11842474Seschrock 
11852474Seschrock 				if ((datasets = zfs_alloc(hdl,
11862474Seschrock 				    8 * sizeof (void *))) == NULL)
11872474Seschrock 					goto out;
11882474Seschrock 
11892474Seschrock 				alloc = 8;
11902474Seschrock 			} else {
11912676Seschrock 				void *ptr;
11922474Seschrock 
11932676Seschrock 				if ((ptr = zfs_realloc(hdl, mountpoints,
11942676Seschrock 				    alloc * sizeof (void *),
11952474Seschrock 				    alloc * 2 * sizeof (void *))) == NULL)
11962474Seschrock 					goto out;
11972676Seschrock 				mountpoints = ptr;
11982474Seschrock 
11992676Seschrock 				if ((ptr = zfs_realloc(hdl, datasets,
12002676Seschrock 				    alloc * sizeof (void *),
12012474Seschrock 				    alloc * 2 * sizeof (void *))) == NULL)
12022474Seschrock 					goto out;
12032676Seschrock 				datasets = ptr;
12042474Seschrock 
12052474Seschrock 				alloc *= 2;
12062474Seschrock 			}
12072474Seschrock 		}
12082474Seschrock 
12092474Seschrock 		if ((mountpoints[used] = zfs_strdup(hdl,
12102474Seschrock 		    entry.mnt_mountp)) == NULL)
12112474Seschrock 			goto out;
12122474Seschrock 
12132474Seschrock 		/*
12142474Seschrock 		 * This is allowed to fail, in case there is some I/O error.  It
12152474Seschrock 		 * is only used to determine if we need to remove the underlying
12162474Seschrock 		 * mountpoint, so failure is not fatal.
12172474Seschrock 		 */
12182474Seschrock 		datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
12192474Seschrock 
12202474Seschrock 		used++;
12212474Seschrock 	}
12222474Seschrock 
12232474Seschrock 	/*
12242474Seschrock 	 * At this point, we have the entire list of filesystems, so sort it by
12252474Seschrock 	 * mountpoint.
12262474Seschrock 	 */
12272474Seschrock 	qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
12282474Seschrock 
12292474Seschrock 	/*
12302474Seschrock 	 * Walk through and first unshare everything.
12312474Seschrock 	 */
12322474Seschrock 	for (i = 0; i < used; i++) {
12335331Samw 		zfs_share_proto_t *curr_proto;
12345331Samw 		for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
12355331Samw 		    curr_proto++) {
12365331Samw 			if (is_shared(hdl, mountpoints[i], *curr_proto) &&
12375331Samw 			    unshare_one(hdl, mountpoints[i],
12385331Samw 			    mountpoints[i], *curr_proto) != 0)
12395331Samw 				goto out;
12405331Samw 		}
12412474Seschrock 	}
12422474Seschrock 
12432474Seschrock 	/*
12442474Seschrock 	 * Now unmount everything, removing the underlying directories as
12452474Seschrock 	 * appropriate.
12462474Seschrock 	 */
12472474Seschrock 	for (i = 0; i < used; i++) {
12482474Seschrock 		if (unmount_one(hdl, mountpoints[i], flags) != 0)
12492474Seschrock 			goto out;
12502676Seschrock 	}
12512474Seschrock 
12522676Seschrock 	for (i = 0; i < used; i++) {
12532474Seschrock 		if (datasets[i])
12542474Seschrock 			remove_mountpoint(datasets[i]);
12552474Seschrock 	}
12562474Seschrock 
12572474Seschrock 	ret = 0;
12582474Seschrock out:
12592474Seschrock 	for (i = 0; i < used; i++) {
12602474Seschrock 		if (datasets[i])
12612474Seschrock 			zfs_close(datasets[i]);
12622474Seschrock 		free(mountpoints[i]);
12632474Seschrock 	}
12642474Seschrock 	free(datasets);
12652474Seschrock 	free(mountpoints);
12662474Seschrock 
12672474Seschrock 	return (ret);
12682474Seschrock }
1269