1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21f3861e1aSahl 
22fa9e4066Sahrens /*
2311d2789dSgw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e4066Sahrens 
29fa9e4066Sahrens /*
30fa9e4066Sahrens  * Routines to manage ZFS mounts.  We separate all the nasty routines that have
31f3861e1aSahl  * to deal with the OS.  The following functions are the main entry points --
32f3861e1aSahl  * they are used by mount and unmount and when changing a filesystem's
33f3861e1aSahl  * mountpoint.
34fa9e4066Sahrens  *
35fa9e4066Sahrens  * 	zfs_is_mounted()
36fa9e4066Sahrens  * 	zfs_mount()
37fa9e4066Sahrens  * 	zfs_unmount()
38fa9e4066Sahrens  * 	zfs_unmountall()
39fa9e4066Sahrens  *
40f3861e1aSahl  * This file also contains the functions used to manage sharing filesystems via
41f3861e1aSahl  * NFS and iSCSI:
42fa9e4066Sahrens  *
43fa9e4066Sahrens  * 	zfs_is_shared()
44fa9e4066Sahrens  * 	zfs_share()
45fa9e4066Sahrens  * 	zfs_unshare()
46f3861e1aSahl  *
47f3861e1aSahl  * 	zfs_is_shared_nfs()
48f3861e1aSahl  * 	zfs_share_nfs()
49f3861e1aSahl  * 	zfs_unshare_nfs()
50f3861e1aSahl  * 	zfs_unshareall_nfs()
51f3861e1aSahl  * 	zfs_is_shared_iscsi()
52f3861e1aSahl  * 	zfs_share_iscsi()
53f3861e1aSahl  * 	zfs_unshare_iscsi()
543bb79becSeschrock  *
553bb79becSeschrock  * The following functions are available for pool consumers, and will
56f3861e1aSahl  * mount/unmount and share/unshare all datasets within pool:
573bb79becSeschrock  *
58f3861e1aSahl  * 	zpool_enable_datasets()
59f3861e1aSahl  * 	zpool_disable_datasets()
60fa9e4066Sahrens  */
61fa9e4066Sahrens 
62fa9e4066Sahrens #include <dirent.h>
63d8d59944Sahl #include <dlfcn.h>
64fa9e4066Sahrens #include <errno.h>
65fa9e4066Sahrens #include <libgen.h>
66fa9e4066Sahrens #include <libintl.h>
67fa9e4066Sahrens #include <stdio.h>
68fa9e4066Sahrens #include <stdlib.h>
69fa9e4066Sahrens #include <strings.h>
70fa9e4066Sahrens #include <unistd.h>
71fa9e4066Sahrens #include <zone.h>
72fa9e4066Sahrens #include <sys/mntent.h>
73fa9e4066Sahrens #include <sys/mnttab.h>
74fa9e4066Sahrens #include <sys/mount.h>
75fa9e4066Sahrens #include <sys/stat.h>
76fa9e4066Sahrens 
77fa9e4066Sahrens #include <libzfs.h>
78fa9e4066Sahrens 
79fa9e4066Sahrens #include "libzfs_impl.h"
80fa9e4066Sahrens 
8167331909Sdougm #include <libshare.h>
8267331909Sdougm #include <sys/systeminfo.h>
8367331909Sdougm #define	MAXISALEN	257	/* based on sysinfo(2) man page */
8467331909Sdougm 
85d8d59944Sahl static int (*iscsitgt_zfs_share)(const char *);
86d8d59944Sahl static int (*iscsitgt_zfs_unshare)(const char *);
87d8d59944Sahl static int (*iscsitgt_zfs_is_shared)(const char *);
88ecd6cf80Smarks static int (*iscsitgt_svc_online)();
89d8d59944Sahl 
90d8d59944Sahl #pragma init(zfs_iscsi_init)
91d8d59944Sahl static void
92d8d59944Sahl zfs_iscsi_init(void)
93d8d59944Sahl {
94d8d59944Sahl 	void *libiscsitgt;
95d8d59944Sahl 
96d8d59944Sahl 	if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1",
97d8d59944Sahl 	    RTLD_LAZY | RTLD_GLOBAL)) == NULL ||
98d8d59944Sahl 	    (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt,
99d8d59944Sahl 	    "iscsitgt_zfs_share")) == NULL ||
100d8d59944Sahl 	    (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt,
101d8d59944Sahl 	    "iscsitgt_zfs_unshare")) == NULL ||
102d8d59944Sahl 	    (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt,
103ecd6cf80Smarks 	    "iscsitgt_zfs_is_shared")) == NULL ||
104ecd6cf80Smarks 	    (iscsitgt_svc_online = (int (*)(const char *))dlsym(libiscsitgt,
105ecd6cf80Smarks 	    "iscsitgt_svc_online")) == NULL) {
106d8d59944Sahl 		iscsitgt_zfs_share = NULL;
107d8d59944Sahl 		iscsitgt_zfs_unshare = NULL;
108d8d59944Sahl 		iscsitgt_zfs_is_shared = NULL;
109ecd6cf80Smarks 		iscsitgt_svc_online = NULL;
110d8d59944Sahl 	}
111d8d59944Sahl }
112d8d59944Sahl 
113fa9e4066Sahrens /*
11499653d4eSeschrock  * Search the sharetab for the given mountpoint, returning true if it is found.
115fa9e4066Sahrens  */
11699653d4eSeschrock static boolean_t
11799653d4eSeschrock is_shared(libzfs_handle_t *hdl, const char *mountpoint)
118fa9e4066Sahrens {
119fa9e4066Sahrens 	char buf[MAXPATHLEN], *tab;
120fa9e4066Sahrens 
12199653d4eSeschrock 	if (hdl->libzfs_sharetab == NULL)
122fa9e4066Sahrens 		return (0);
123fa9e4066Sahrens 
12499653d4eSeschrock 	(void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
125fa9e4066Sahrens 
12699653d4eSeschrock 	while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
127fa9e4066Sahrens 
128fa9e4066Sahrens 		/* the mountpoint is the first entry on each line */
129fa9e4066Sahrens 		if ((tab = strchr(buf, '\t')) != NULL) {
130fa9e4066Sahrens 			*tab = '\0';
131fa9e4066Sahrens 			if (strcmp(buf, mountpoint) == 0)
13299653d4eSeschrock 				return (B_TRUE);
133fa9e4066Sahrens 		}
134fa9e4066Sahrens 	}
135fa9e4066Sahrens 
13699653d4eSeschrock 	return (B_FALSE);
137fa9e4066Sahrens }
138fa9e4066Sahrens 
139fa9e4066Sahrens /*
14099653d4eSeschrock  * Returns true if the specified directory is empty.  If we can't open the
14199653d4eSeschrock  * directory at all, return true so that the mount can fail with a more
142fa9e4066Sahrens  * informative error message.
143fa9e4066Sahrens  */
14499653d4eSeschrock static boolean_t
145fa9e4066Sahrens dir_is_empty(const char *dirname)
146fa9e4066Sahrens {
147fa9e4066Sahrens 	DIR *dirp;
148fa9e4066Sahrens 	struct dirent64 *dp;
149fa9e4066Sahrens 
150fa9e4066Sahrens 	if ((dirp = opendir(dirname)) == NULL)
15199653d4eSeschrock 		return (B_TRUE);
152fa9e4066Sahrens 
153fa9e4066Sahrens 	while ((dp = readdir64(dirp)) != NULL) {
154fa9e4066Sahrens 
155fa9e4066Sahrens 		if (strcmp(dp->d_name, ".") == 0 ||
156fa9e4066Sahrens 		    strcmp(dp->d_name, "..") == 0)
157fa9e4066Sahrens 			continue;
158fa9e4066Sahrens 
159fa9e4066Sahrens 		(void) closedir(dirp);
16099653d4eSeschrock 		return (B_FALSE);
161fa9e4066Sahrens 	}
162fa9e4066Sahrens 
163fa9e4066Sahrens 	(void) closedir(dirp);
16499653d4eSeschrock 	return (B_TRUE);
165fa9e4066Sahrens }
166fa9e4066Sahrens 
167fa9e4066Sahrens /*
168fa9e4066Sahrens  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
169fa9e4066Sahrens  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
170fa9e4066Sahrens  * 0.
171fa9e4066Sahrens  */
17299653d4eSeschrock boolean_t
17355434c77Sek is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
174fa9e4066Sahrens {
175fa9e4066Sahrens 	struct mnttab search = { 0 }, entry;
176fa9e4066Sahrens 
177fa9e4066Sahrens 	/*
178fa9e4066Sahrens 	 * Search for the entry in /etc/mnttab.  We don't bother getting the
179fa9e4066Sahrens 	 * mountpoint, as we can just search for the special device.  This will
180fa9e4066Sahrens 	 * also let us find mounts when the mountpoint is 'legacy'.
181fa9e4066Sahrens 	 */
18255434c77Sek 	search.mnt_special = (char *)special;
18396dedd18Snd 	search.mnt_fstype = MNTTYPE_ZFS;
184fa9e4066Sahrens 
18555434c77Sek 	rewind(zfs_hdl->libzfs_mnttab);
18655434c77Sek 	if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0)
18799653d4eSeschrock 		return (B_FALSE);
188fa9e4066Sahrens 
189fa9e4066Sahrens 	if (where != NULL)
19055434c77Sek 		*where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
191fa9e4066Sahrens 
19299653d4eSeschrock 	return (B_TRUE);
193fa9e4066Sahrens }
194fa9e4066Sahrens 
19555434c77Sek boolean_t
19655434c77Sek zfs_is_mounted(zfs_handle_t *zhp, char **where)
19755434c77Sek {
19855434c77Sek 	return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
19955434c77Sek }
20055434c77Sek 
201e9dbad6fSeschrock /*
202e9dbad6fSeschrock  * Returns true if the given dataset is mountable, false otherwise.  Returns the
203e9dbad6fSeschrock  * mountpoint in 'buf'.
204e9dbad6fSeschrock  */
205e9dbad6fSeschrock static boolean_t
206e9dbad6fSeschrock zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
207*990b4856Slling     zprop_source_t *source)
208e9dbad6fSeschrock {
209e9dbad6fSeschrock 	char sourceloc[ZFS_MAXNAMELEN];
210*990b4856Slling 	zprop_source_t sourcetype;
211e9dbad6fSeschrock 
212e9dbad6fSeschrock 	if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
213e9dbad6fSeschrock 		return (B_FALSE);
214e9dbad6fSeschrock 
215e9dbad6fSeschrock 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
216e9dbad6fSeschrock 	    &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
217e9dbad6fSeschrock 
218e9dbad6fSeschrock 	if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
219e9dbad6fSeschrock 	    strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
220e9dbad6fSeschrock 		return (B_FALSE);
221e9dbad6fSeschrock 
222e9dbad6fSeschrock 	if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT))
223e9dbad6fSeschrock 		return (B_FALSE);
224e9dbad6fSeschrock 
225e9dbad6fSeschrock 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
226e9dbad6fSeschrock 	    getzoneid() == GLOBAL_ZONEID)
227e9dbad6fSeschrock 		return (B_FALSE);
228e9dbad6fSeschrock 
229e9dbad6fSeschrock 	if (source)
230e9dbad6fSeschrock 		*source = sourcetype;
231e9dbad6fSeschrock 
232e9dbad6fSeschrock 	return (B_TRUE);
233e9dbad6fSeschrock }
234e9dbad6fSeschrock 
235fa9e4066Sahrens /*
236fa9e4066Sahrens  * Mount the given filesystem.
237fa9e4066Sahrens  */
238fa9e4066Sahrens int
239fa9e4066Sahrens zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
240fa9e4066Sahrens {
241fa9e4066Sahrens 	struct stat buf;
242fa9e4066Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
243fa9e4066Sahrens 	char mntopts[MNT_LINE_MAX];
24499653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
245fa9e4066Sahrens 
246fa9e4066Sahrens 	if (options == NULL)
247fa9e4066Sahrens 		mntopts[0] = '\0';
248fa9e4066Sahrens 	else
249fa9e4066Sahrens 		(void) strlcpy(mntopts, options, sizeof (mntopts));
250fa9e4066Sahrens 
251e9dbad6fSeschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
25299653d4eSeschrock 		return (0);
253fa9e4066Sahrens 
254fa9e4066Sahrens 	/* Create the directory if it doesn't already exist */
255fa9e4066Sahrens 	if (lstat(mountpoint, &buf) != 0) {
256fa9e4066Sahrens 		if (mkdirp(mountpoint, 0755) != 0) {
25799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
25899653d4eSeschrock 			    "failed to create mountpoint"));
259ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
26099653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
26199653d4eSeschrock 			    mountpoint));
262fa9e4066Sahrens 		}
263fa9e4066Sahrens 	}
264fa9e4066Sahrens 
265fa9e4066Sahrens 	/*
266fa9e4066Sahrens 	 * Determine if the mountpoint is empty.  If so, refuse to perform the
267fa9e4066Sahrens 	 * mount.  We don't perform this check if MS_OVERLAY is specified, which
268fa9e4066Sahrens 	 * would defeat the point.  We also avoid this check if 'remount' is
269fa9e4066Sahrens 	 * specified.
270fa9e4066Sahrens 	 */
271fa9e4066Sahrens 	if ((flags & MS_OVERLAY) == 0 &&
272fa9e4066Sahrens 	    strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
273fa9e4066Sahrens 	    !dir_is_empty(mountpoint)) {
27499653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
27599653d4eSeschrock 		    "directory is not empty"));
276ece3d9b3Slling 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
27799653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
278fa9e4066Sahrens 	}
279fa9e4066Sahrens 
280fa9e4066Sahrens 	/* perform the mount */
281fa9e4066Sahrens 	if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
282fa9e4066Sahrens 	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
283fa9e4066Sahrens 		/*
284fa9e4066Sahrens 		 * Generic errors are nasty, but there are just way too many
285fa9e4066Sahrens 		 * from mount(), and they're well-understood.  We pick a few
286fa9e4066Sahrens 		 * common ones to improve upon.
287fa9e4066Sahrens 		 */
288d8689a57Sdougm 		if (errno == EBUSY) {
28999653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
29099653d4eSeschrock 			    "mountpoint or dataset is busy"));
291ecd6cf80Smarks 		} else if (errno == EPERM) {
292ecd6cf80Smarks 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
293ecd6cf80Smarks 			    "Insufficient privileges"));
294d8689a57Sdougm 		} else {
29599653d4eSeschrock 			zfs_error_aux(hdl, strerror(errno));
296d8689a57Sdougm 		}
29799653d4eSeschrock 
298ece3d9b3Slling 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
29999653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
30099653d4eSeschrock 		    zhp->zfs_name));
301fa9e4066Sahrens 	}
302fa9e4066Sahrens 
303fa9e4066Sahrens 	return (0);
304fa9e4066Sahrens }
305fa9e4066Sahrens 
3063bb79becSeschrock /*
3073bb79becSeschrock  * Unmount a single filesystem.
3083bb79becSeschrock  */
3093bb79becSeschrock static int
3103bb79becSeschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
3113bb79becSeschrock {
3123bb79becSeschrock 	if (umount2(mountpoint, flags) != 0) {
3133bb79becSeschrock 		zfs_error_aux(hdl, strerror(errno));
314ece3d9b3Slling 		return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
3153bb79becSeschrock 		    dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
3163bb79becSeschrock 		    mountpoint));
3173bb79becSeschrock 	}
3183bb79becSeschrock 
3193bb79becSeschrock 	return (0);
3203bb79becSeschrock }
3213bb79becSeschrock 
322fa9e4066Sahrens /*
323fa9e4066Sahrens  * Unmount the given filesystem.
324fa9e4066Sahrens  */
325fa9e4066Sahrens int
326fa9e4066Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
327fa9e4066Sahrens {
328fa9e4066Sahrens 	struct mnttab search = { 0 }, entry;
32967331909Sdougm 	char *mntpt = NULL;
330fa9e4066Sahrens 
331fa9e4066Sahrens 	/* check to see if need to unmount the filesystem */
3323bb79becSeschrock 	search.mnt_special = zhp->zfs_name;
33396dedd18Snd 	search.mnt_fstype = MNTTYPE_ZFS;
33499653d4eSeschrock 	rewind(zhp->zfs_hdl->libzfs_mnttab);
335fa9e4066Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
33699653d4eSeschrock 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
337fa9e4066Sahrens 
33867331909Sdougm 		/*
33967331909Sdougm 		 * mountpoint may have come from a call to
34067331909Sdougm 		 * getmnt/getmntany if it isn't NULL. If it is NULL,
34167331909Sdougm 		 * we know it comes from getmntany which can then get
34267331909Sdougm 		 * overwritten later. We strdup it to play it safe.
34367331909Sdougm 		 */
344fa9e4066Sahrens 		if (mountpoint == NULL)
345d8689a57Sdougm 			mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
34667331909Sdougm 		else
347d8689a57Sdougm 			mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
348fa9e4066Sahrens 
349fa9e4066Sahrens 		/*
3503bb79becSeschrock 		 * Unshare and unmount the filesystem
351fa9e4066Sahrens 		 */
352eb66cf86Sth 		if (zfs_unshare_nfs(zhp, mntpt) != 0)
353eb66cf86Sth 			return (-1);
354eb66cf86Sth 
355eb66cf86Sth 		if (unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) {
35667331909Sdougm 			free(mntpt);
3578ed277d8Sth 			(void) zfs_share_nfs(zhp);
358fa9e4066Sahrens 			return (-1);
35967331909Sdougm 		}
36067331909Sdougm 		free(mntpt);
361fa9e4066Sahrens 	}
362fa9e4066Sahrens 
363fa9e4066Sahrens 	return (0);
364fa9e4066Sahrens }
365fa9e4066Sahrens 
366fa9e4066Sahrens /*
367fa9e4066Sahrens  * Unmount this filesystem and any children inheriting the mountpoint property.
368fa9e4066Sahrens  * To do this, just act like we're changing the mountpoint property, but don't
369fa9e4066Sahrens  * remount the filesystems afterwards.
370fa9e4066Sahrens  */
371fa9e4066Sahrens int
372fa9e4066Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags)
373fa9e4066Sahrens {
374fa9e4066Sahrens 	prop_changelist_t *clp;
375fa9e4066Sahrens 	int ret;
376fa9e4066Sahrens 
377fa9e4066Sahrens 	clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags);
378fa9e4066Sahrens 	if (clp == NULL)
379fa9e4066Sahrens 		return (-1);
380fa9e4066Sahrens 
381fa9e4066Sahrens 	ret = changelist_prefix(clp);
382fa9e4066Sahrens 	changelist_free(clp);
383fa9e4066Sahrens 
384fa9e4066Sahrens 	return (ret);
385fa9e4066Sahrens }
386fa9e4066Sahrens 
387f3861e1aSahl boolean_t
388f3861e1aSahl zfs_is_shared(zfs_handle_t *zhp)
389f3861e1aSahl {
390f3861e1aSahl 	if (ZFS_IS_VOLUME(zhp))
391f3861e1aSahl 		return (zfs_is_shared_iscsi(zhp));
392f3861e1aSahl 
393f3861e1aSahl 	return (zfs_is_shared_nfs(zhp, NULL));
394f3861e1aSahl }
395f3861e1aSahl 
396f3861e1aSahl int
397f3861e1aSahl zfs_share(zfs_handle_t *zhp)
398f3861e1aSahl {
399f3861e1aSahl 	if (ZFS_IS_VOLUME(zhp))
400f3861e1aSahl 		return (zfs_share_iscsi(zhp));
401f3861e1aSahl 
402f3861e1aSahl 	return (zfs_share_nfs(zhp));
403f3861e1aSahl }
404f3861e1aSahl 
405f3861e1aSahl int
406f3861e1aSahl zfs_unshare(zfs_handle_t *zhp)
407f3861e1aSahl {
408f3861e1aSahl 	if (ZFS_IS_VOLUME(zhp))
409f3861e1aSahl 		return (zfs_unshare_iscsi(zhp));
410f3861e1aSahl 
411f3861e1aSahl 	return (zfs_unshare_nfs(zhp, NULL));
412f3861e1aSahl }
413f3861e1aSahl 
414fa9e4066Sahrens /*
415fa9e4066Sahrens  * Check to see if the filesystem is currently shared.
416fa9e4066Sahrens  */
41799653d4eSeschrock boolean_t
418f3861e1aSahl zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
419fa9e4066Sahrens {
420fa9e4066Sahrens 	char *mountpoint;
421fa9e4066Sahrens 
422fa9e4066Sahrens 	if (!zfs_is_mounted(zhp, &mountpoint))
42399653d4eSeschrock 		return (B_FALSE);
424fa9e4066Sahrens 
42599653d4eSeschrock 	if (is_shared(zhp->zfs_hdl, mountpoint)) {
426fa9e4066Sahrens 		if (where != NULL)
427fa9e4066Sahrens 			*where = mountpoint;
428fa9e4066Sahrens 		else
429fa9e4066Sahrens 			free(mountpoint);
43099653d4eSeschrock 		return (B_TRUE);
431fa9e4066Sahrens 	} else {
432fa9e4066Sahrens 		free(mountpoint);
43399653d4eSeschrock 		return (B_FALSE);
434fa9e4066Sahrens 	}
435fa9e4066Sahrens }
436fa9e4066Sahrens 
43767331909Sdougm /*
43867331909Sdougm  * Make sure things will work if libshare isn't installed by using
43967331909Sdougm  * wrapper functions that check to see that the pointers to functions
44067331909Sdougm  * initialized in _zfs_init_libshare() are actually present.
44167331909Sdougm  */
44267331909Sdougm 
44367331909Sdougm static sa_handle_t (*_sa_init)(int);
44467331909Sdougm static void (*_sa_fini)(sa_handle_t);
44567331909Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
44667331909Sdougm static int (*_sa_enable_share)(sa_share_t, char *);
44767331909Sdougm static int (*_sa_disable_share)(sa_share_t, char *);
44867331909Sdougm static char *(*_sa_errorstr)(int);
44967331909Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
45067331909Sdougm 
45167331909Sdougm /*
45267331909Sdougm  * _zfs_init_libshare()
45367331909Sdougm  *
45467331909Sdougm  * Find the libshare.so.1 entry points that we use here and save the
45567331909Sdougm  * values to be used later. This is triggered by the runtime loader.
45667331909Sdougm  * Make sure the correct ISA version is loaded.
45767331909Sdougm  */
45867331909Sdougm 
45967331909Sdougm #pragma init(_zfs_init_libshare)
46067331909Sdougm static void
46167331909Sdougm _zfs_init_libshare(void)
46267331909Sdougm {
46367331909Sdougm 	void *libshare;
46467331909Sdougm 	char path[MAXPATHLEN];
46567331909Sdougm 	char isa[MAXISALEN];
46667331909Sdougm 
46767331909Sdougm #if defined(_LP64)
46867331909Sdougm 	if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
469d8689a57Sdougm 		isa[0] = '\0';
47067331909Sdougm #else
47167331909Sdougm 	isa[0] = '\0';
47267331909Sdougm #endif
47367331909Sdougm 	(void) snprintf(path, MAXPATHLEN,
474d8689a57Sdougm 	    "/usr/lib/%s/libshare.so.1", isa);
47567331909Sdougm 
47667331909Sdougm 	if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
477d8689a57Sdougm 		_sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
478d8689a57Sdougm 		_sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
479d8689a57Sdougm 		_sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
480d8689a57Sdougm 		    dlsym(libshare, "sa_find_share");
481d8689a57Sdougm 		_sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
482d8689a57Sdougm 		    "sa_enable_share");
483d8689a57Sdougm 		_sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
484d8689a57Sdougm 		    "sa_disable_share");
485d8689a57Sdougm 		_sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
486d8689a57Sdougm 		_sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
487d8689a57Sdougm 		    dlsym(libshare, "sa_parse_legacy_options");
48857b448deSdougm 		if (_sa_init == NULL || _sa_fini == NULL ||
48957b448deSdougm 		    _sa_find_share == NULL || _sa_enable_share == NULL ||
49057b448deSdougm 		    _sa_disable_share == NULL || _sa_errorstr == NULL ||
49157b448deSdougm 		    _sa_parse_legacy_options == NULL) {
49257b448deSdougm 			_sa_init = NULL;
49357b448deSdougm 			_sa_fini = NULL;
49457b448deSdougm 			_sa_disable_share = NULL;
49557b448deSdougm 			_sa_enable_share = NULL;
49657b448deSdougm 			_sa_errorstr = NULL;
49757b448deSdougm 			_sa_parse_legacy_options = NULL;
49857b448deSdougm 			(void) dlclose(libshare);
49957b448deSdougm 		}
50067331909Sdougm 	}
50167331909Sdougm }
50267331909Sdougm 
50367331909Sdougm /*
50467331909Sdougm  * zfs_init_libshare(zhandle, service)
50567331909Sdougm  *
50667331909Sdougm  * Initialize the libshare API if it hasn't already been initialized.
50767331909Sdougm  * In all cases it returns 0 if it succeeded and an error if not. The
50867331909Sdougm  * service value is which part(s) of the API to initialize and is a
50967331909Sdougm  * direct map to the libshare sa_init(service) interface.
51067331909Sdougm  */
51167331909Sdougm 
51267331909Sdougm int
51367331909Sdougm zfs_init_libshare(libzfs_handle_t *zhandle, int service)
51467331909Sdougm {
51567331909Sdougm 	int ret = SA_OK;
51667331909Sdougm 
517d8689a57Sdougm 	if (_sa_init == NULL)
518d8689a57Sdougm 		ret = SA_CONFIG_ERR;
519d8689a57Sdougm 
520d8689a57Sdougm 	if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
521d8689a57Sdougm 		zhandle->libzfs_sharehdl = _sa_init(service);
522d8689a57Sdougm 
523d8689a57Sdougm 	if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
524d8689a57Sdougm 		ret = SA_NO_MEMORY;
525d8689a57Sdougm 
52667331909Sdougm 	return (ret);
52767331909Sdougm }
52867331909Sdougm 
52967331909Sdougm /*
53067331909Sdougm  * zfs_uninit_libshare(zhandle)
53167331909Sdougm  *
53267331909Sdougm  * Uninitialize the libshare API if it hasn't already been
53367331909Sdougm  * uninitialized. It is OK to call multiple times.
53467331909Sdougm  */
53567331909Sdougm 
53667331909Sdougm void
53767331909Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle)
53867331909Sdougm {
53967331909Sdougm 
54067331909Sdougm 	if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
541d8689a57Sdougm 		if (_sa_fini != NULL)
542d8689a57Sdougm 			_sa_fini(zhandle->libzfs_sharehdl);
543d8689a57Sdougm 		zhandle->libzfs_sharehdl = NULL;
54467331909Sdougm 	}
54567331909Sdougm }
54667331909Sdougm 
54767331909Sdougm /*
54867331909Sdougm  * zfs_parse_options(options, proto)
54967331909Sdougm  *
55067331909Sdougm  * Call the legacy parse interface to get the protocol specific
55167331909Sdougm  * options using the NULL arg to indicate that this is a "parse" only.
55267331909Sdougm  */
55367331909Sdougm 
55467331909Sdougm int
55567331909Sdougm zfs_parse_options(char *options, char *proto)
55667331909Sdougm {
55767331909Sdougm 	int ret;
55867331909Sdougm 
55967331909Sdougm 	if (_sa_parse_legacy_options != NULL)
560d8689a57Sdougm 		ret = _sa_parse_legacy_options(NULL, options, proto);
56167331909Sdougm 	else
562d8689a57Sdougm 		ret = SA_CONFIG_ERR;
56367331909Sdougm 	return (ret);
56467331909Sdougm }
56567331909Sdougm 
56667331909Sdougm /*
56767331909Sdougm  * zfs_sa_find_share(handle, path)
56867331909Sdougm  *
56967331909Sdougm  * wrapper around sa_find_share to find a share path in the
57067331909Sdougm  * configuration.
57167331909Sdougm  */
57267331909Sdougm 
57367331909Sdougm static sa_share_t
57467331909Sdougm zfs_sa_find_share(sa_handle_t handle, char *path)
57567331909Sdougm {
57667331909Sdougm 	if (_sa_find_share != NULL)
577d8689a57Sdougm 		return (_sa_find_share(handle, path));
57867331909Sdougm 	return (NULL);
57967331909Sdougm }
58067331909Sdougm 
58167331909Sdougm /*
58267331909Sdougm  * zfs_sa_enable_share(share, proto)
58367331909Sdougm  *
58467331909Sdougm  * Wrapper for sa_enable_share which enables a share for a specified
58567331909Sdougm  * protocol.
58667331909Sdougm  */
58767331909Sdougm 
58867331909Sdougm static int
58967331909Sdougm zfs_sa_enable_share(sa_share_t share, char *proto)
59067331909Sdougm {
59167331909Sdougm 	if (_sa_enable_share != NULL)
592d8689a57Sdougm 		return (_sa_enable_share(share, proto));
59367331909Sdougm 	return (SA_CONFIG_ERR);
59467331909Sdougm }
59567331909Sdougm 
59667331909Sdougm /*
59767331909Sdougm  * zfs_sa_disable_share(share, proto)
59867331909Sdougm  *
59967331909Sdougm  * Wrapper for sa_enable_share which disables a share for a specified
60067331909Sdougm  * protocol.
60167331909Sdougm  */
60267331909Sdougm 
60367331909Sdougm static int
60467331909Sdougm zfs_sa_disable_share(sa_share_t share, char *proto)
60567331909Sdougm {
60667331909Sdougm 	if (_sa_disable_share != NULL)
607d8689a57Sdougm 		return (_sa_disable_share(share, proto));
60867331909Sdougm 	return (SA_CONFIG_ERR);
60967331909Sdougm }
61067331909Sdougm 
611fa9e4066Sahrens /*
612fa9e4066Sahrens  * Share the given filesystem according to the options in 'sharenfs'.  We rely
61367331909Sdougm  * on "libshare" to the dirty work for us.
614fa9e4066Sahrens  */
61567331909Sdougm 
616fa9e4066Sahrens int
617f3861e1aSahl zfs_share_nfs(zfs_handle_t *zhp)
618fa9e4066Sahrens {
619fa9e4066Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
620fa9e4066Sahrens 	char shareopts[ZFS_MAXPROPLEN];
62199653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
62267331909Sdougm 	sa_share_t share;
62367331909Sdougm 	int ret;
624fa9e4066Sahrens 
625e9dbad6fSeschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
626fa9e4066Sahrens 		return (0);
627fa9e4066Sahrens 
628f3861e1aSahl 	/*
629f3861e1aSahl 	 * Return success if there are no share options.
630f3861e1aSahl 	 */
631fa9e4066Sahrens 	if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts),
63299653d4eSeschrock 	    NULL, NULL, 0, B_FALSE) != 0 ||
633fa9e4066Sahrens 	    strcmp(shareopts, "off") == 0)
634fa9e4066Sahrens 		return (0);
635fa9e4066Sahrens 
636fa9e4066Sahrens 	/*
637e9dbad6fSeschrock 	 * If the 'zoned' property is set, then zfs_is_mountable() will have
638e9dbad6fSeschrock 	 * already bailed out if we are in the global zone.  But local
639e9dbad6fSeschrock 	 * zones cannot be NFS servers, so we ignore it for local zones as well.
640fa9e4066Sahrens 	 */
641e9dbad6fSeschrock 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
642fa9e4066Sahrens 		return (0);
643fa9e4066Sahrens 
64467331909Sdougm 	if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
645d8689a57Sdougm 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
646d8689a57Sdougm 		    dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
647d8689a57Sdougm 		    zfs_get_name(zhp), _sa_errorstr(ret));
648d8689a57Sdougm 		return (-1);
64967331909Sdougm 	}
65067331909Sdougm 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
65167331909Sdougm 	if (share != NULL) {
652d8689a57Sdougm 		int err;
653d8689a57Sdougm 		err = zfs_sa_enable_share(share, "nfs");
654d8689a57Sdougm 		if (err != SA_OK) {
655d8689a57Sdougm 			(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
656d8689a57Sdougm 			    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
657d8689a57Sdougm 			    zfs_get_name(zhp));
658d8689a57Sdougm 			return (-1);
659d8689a57Sdougm 		}
66067331909Sdougm 	} else {
66167331909Sdougm 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
662d8689a57Sdougm 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
663d8689a57Sdougm 		    zfs_get_name(zhp));
664fa9e4066Sahrens 		return (-1);
665fa9e4066Sahrens 	}
666fa9e4066Sahrens 
667fa9e4066Sahrens 	return (0);
668fa9e4066Sahrens }
669fa9e4066Sahrens 
6703bb79becSeschrock /*
6713bb79becSeschrock  * Unshare a filesystem by mountpoint.
6723bb79becSeschrock  */
6733bb79becSeschrock static int
6743bb79becSeschrock unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint)
6753bb79becSeschrock {
67667331909Sdougm 	sa_share_t share;
67767331909Sdougm 	int err;
67867331909Sdougm 	char *mntpt;
6793bb79becSeschrock 
6803bb79becSeschrock 	/*
68167331909Sdougm 	 * Mountpoint could get trashed if libshare calls getmntany
68267331909Sdougm 	 * which id does during API initialization, so strdup the
68367331909Sdougm 	 * value.
6843bb79becSeschrock 	 */
68567331909Sdougm 	mntpt = zfs_strdup(hdl, mountpoint);
6863bb79becSeschrock 
68767331909Sdougm 	/* make sure libshare initialized */
68867331909Sdougm 	if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
68967331909Sdougm 		free(mntpt);	/* don't need the copy anymore */
69067331909Sdougm 		return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
691d8689a57Sdougm 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
692d8689a57Sdougm 		    name, _sa_errorstr(err)));
69367331909Sdougm 	}
6943bb79becSeschrock 
69567331909Sdougm 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
69667331909Sdougm 	free(mntpt);	/* don't need the copy anymore */
6973bb79becSeschrock 
69867331909Sdougm 	if (share != NULL) {
69967331909Sdougm 		err = zfs_sa_disable_share(share, "nfs");
70067331909Sdougm 		if (err != SA_OK) {
701d8689a57Sdougm 			return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
702d8689a57Sdougm 			    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
703d8689a57Sdougm 			    name, _sa_errorstr(err)));
70467331909Sdougm 		}
70567331909Sdougm 	} else {
706ece3d9b3Slling 		return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
707d8689a57Sdougm 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
708d8689a57Sdougm 		    name));
7093bb79becSeschrock 	}
7103bb79becSeschrock 	return (0);
7113bb79becSeschrock }
7123bb79becSeschrock 
713fa9e4066Sahrens /*
714fa9e4066Sahrens  * Unshare the given filesystem.
715fa9e4066Sahrens  */
716fa9e4066Sahrens int
717f3861e1aSahl zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
718fa9e4066Sahrens {
719fa9e4066Sahrens 	struct mnttab search = { 0 }, entry;
72067331909Sdougm 	char *mntpt = NULL;
721fa9e4066Sahrens 
722fa9e4066Sahrens 	/* check to see if need to unmount the filesystem */
723fa9e4066Sahrens 	search.mnt_special = (char *)zfs_get_name(zhp);
72496dedd18Snd 	search.mnt_fstype = MNTTYPE_ZFS;
72599653d4eSeschrock 	rewind(zhp->zfs_hdl->libzfs_mnttab);
726d8689a57Sdougm 	if (mountpoint != NULL)
727d8689a57Sdougm 		mountpoint = mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
728d8689a57Sdougm 
729fa9e4066Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
73099653d4eSeschrock 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
731fa9e4066Sahrens 
732fa9e4066Sahrens 		if (mountpoint == NULL)
733fa9e4066Sahrens 			mountpoint = entry.mnt_mountp;
734fa9e4066Sahrens 
7353bb79becSeschrock 		if (is_shared(zhp->zfs_hdl, mountpoint) &&
73667331909Sdougm 		    unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0) {
73767331909Sdougm 			if (mntpt != NULL)
73867331909Sdougm 				free(mntpt);
7393bb79becSeschrock 			return (-1);
74067331909Sdougm 		}
741fa9e4066Sahrens 	}
74267331909Sdougm 	if (mntpt != NULL)
743d8689a57Sdougm 		free(mntpt);
744fa9e4066Sahrens 
745fa9e4066Sahrens 	return (0);
746fa9e4066Sahrens }
747fa9e4066Sahrens 
748fa9e4066Sahrens /*
749f3861e1aSahl  * Same as zfs_unmountall(), but for NFS unshares.
750fa9e4066Sahrens  */
751fa9e4066Sahrens int
752f3861e1aSahl zfs_unshareall_nfs(zfs_handle_t *zhp)
753fa9e4066Sahrens {
754fa9e4066Sahrens 	prop_changelist_t *clp;
755fa9e4066Sahrens 	int ret;
756fa9e4066Sahrens 
757fa9e4066Sahrens 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0);
758fa9e4066Sahrens 	if (clp == NULL)
759fa9e4066Sahrens 		return (-1);
760fa9e4066Sahrens 
761fa9e4066Sahrens 	ret = changelist_unshare(clp);
762fa9e4066Sahrens 	changelist_free(clp);
763fa9e4066Sahrens 
764fa9e4066Sahrens 	return (ret);
765fa9e4066Sahrens }
766fa9e4066Sahrens 
767fa9e4066Sahrens /*
768fa9e4066Sahrens  * Remove the mountpoint associated with the current dataset, if necessary.
769fa9e4066Sahrens  * We only remove the underlying directory if:
770fa9e4066Sahrens  *
771fa9e4066Sahrens  *	- The mountpoint is not 'none' or 'legacy'
772fa9e4066Sahrens  *	- The mountpoint is non-empty
773fa9e4066Sahrens  *	- The mountpoint is the default or inherited
774fa9e4066Sahrens  *	- The 'zoned' property is set, or we're in a local zone
775fa9e4066Sahrens  *
776fa9e4066Sahrens  * Any other directories we leave alone.
777fa9e4066Sahrens  */
778fa9e4066Sahrens void
779fa9e4066Sahrens remove_mountpoint(zfs_handle_t *zhp)
780fa9e4066Sahrens {
781fa9e4066Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
782*990b4856Slling 	zprop_source_t source;
783fa9e4066Sahrens 
784e9dbad6fSeschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
785e9dbad6fSeschrock 	    &source))
786fa9e4066Sahrens 		return;
787fa9e4066Sahrens 
788*990b4856Slling 	if (source == ZPROP_SRC_DEFAULT ||
789*990b4856Slling 	    source == ZPROP_SRC_INHERITED) {
790fa9e4066Sahrens 		/*
791fa9e4066Sahrens 		 * Try to remove the directory, silently ignoring any errors.
792fa9e4066Sahrens 		 * The filesystem may have since been removed or moved around,
793e9dbad6fSeschrock 		 * and this error isn't really useful to the administrator in
794e9dbad6fSeschrock 		 * any way.
795fa9e4066Sahrens 		 */
796fa9e4066Sahrens 		(void) rmdir(mountpoint);
797fa9e4066Sahrens 	}
798fa9e4066Sahrens }
7993bb79becSeschrock 
800f3861e1aSahl boolean_t
801f3861e1aSahl zfs_is_shared_iscsi(zfs_handle_t *zhp)
802f3861e1aSahl {
803ecd6cf80Smarks 
804ecd6cf80Smarks 	/*
805ecd6cf80Smarks 	 * If iscsi deamon isn't running then we aren't shared
806ecd6cf80Smarks 	 */
807ecd6cf80Smarks 	if (iscsitgt_svc_online && iscsitgt_svc_online() == 1)
808ecd6cf80Smarks 		return (0);
809ecd6cf80Smarks 	else
810ecd6cf80Smarks 		return (iscsitgt_zfs_is_shared != NULL &&
811ecd6cf80Smarks 		    iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
812f3861e1aSahl }
813f3861e1aSahl 
814f3861e1aSahl int
815f3861e1aSahl zfs_share_iscsi(zfs_handle_t *zhp)
816f3861e1aSahl {
817f3861e1aSahl 	char shareopts[ZFS_MAXPROPLEN];
818f3861e1aSahl 	const char *dataset = zhp->zfs_name;
819f3861e1aSahl 	libzfs_handle_t *hdl = zhp->zfs_hdl;
820f3861e1aSahl 
821f3861e1aSahl 	/*
822f3861e1aSahl 	 * Return success if there are no share options.
823f3861e1aSahl 	 */
824f3861e1aSahl 	if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
825f3861e1aSahl 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
826f3861e1aSahl 	    strcmp(shareopts, "off") == 0)
827f3861e1aSahl 		return (0);
828f3861e1aSahl 
829ecd6cf80Smarks 	if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0) {
830ecd6cf80Smarks 		int error = EZFS_SHAREISCSIFAILED;
831ecd6cf80Smarks 
832ecd6cf80Smarks 		/*
833ecd6cf80Smarks 		 * If service isn't availabele and EPERM was
834ecd6cf80Smarks 		 * returned then use special error.
835ecd6cf80Smarks 		 */
836ecd6cf80Smarks 		if (iscsitgt_svc_online && errno == EPERM &&
837ecd6cf80Smarks 		    (iscsitgt_svc_online() != 0))
838ecd6cf80Smarks 			error = EZFS_ISCSISVCUNAVAIL;
839ecd6cf80Smarks 
840ecd6cf80Smarks 		return (zfs_error_fmt(hdl, error,
841f3861e1aSahl 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
842ecd6cf80Smarks 	}
843f3861e1aSahl 
844f3861e1aSahl 	return (0);
845f3861e1aSahl }
846f3861e1aSahl 
847f3861e1aSahl int
848f3861e1aSahl zfs_unshare_iscsi(zfs_handle_t *zhp)
849f3861e1aSahl {
850f3861e1aSahl 	const char *dataset = zfs_get_name(zhp);
851f3861e1aSahl 	libzfs_handle_t *hdl = zhp->zfs_hdl;
852f3861e1aSahl 
85311d2789dSgw 	/*
85411d2789dSgw 	 * Return if the volume is not shared
85511d2789dSgw 	 */
85611d2789dSgw 	if (!zfs_is_shared_iscsi(zhp))
85711d2789dSgw 		return (0);
85811d2789dSgw 
859f3861e1aSahl 	/*
860f3861e1aSahl 	 * If this fails with ENODEV it indicates that zvol wasn't shared so
861f3861e1aSahl 	 * we should return success in that case.
862f3861e1aSahl 	 */
863d8d59944Sahl 	if (iscsitgt_zfs_unshare == NULL ||
864ecd6cf80Smarks 	    (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV)) {
865ecd6cf80Smarks 		if (errno == EPERM)
866ecd6cf80Smarks 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
867ecd6cf80Smarks 			    "Insufficient privileges to unshare iscsi"));
868ece3d9b3Slling 		return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED,
869f3861e1aSahl 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
870ecd6cf80Smarks 	}
871f3861e1aSahl 
872f3861e1aSahl 	return (0);
873f3861e1aSahl }
874f3861e1aSahl 
8753bb79becSeschrock typedef struct mount_cbdata {
8763bb79becSeschrock 	zfs_handle_t	**cb_datasets;
8773bb79becSeschrock 	int 		cb_used;
8783bb79becSeschrock 	int		cb_alloc;
8793bb79becSeschrock } mount_cbdata_t;
8803bb79becSeschrock 
8813bb79becSeschrock static int
8823bb79becSeschrock mount_cb(zfs_handle_t *zhp, void *data)
8833bb79becSeschrock {
8843bb79becSeschrock 	mount_cbdata_t *cbp = data;
8853bb79becSeschrock 
886f3861e1aSahl 	if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
8873bb79becSeschrock 		zfs_close(zhp);
8883bb79becSeschrock 		return (0);
8893bb79becSeschrock 	}
8903bb79becSeschrock 
8913bb79becSeschrock 	if (cbp->cb_alloc == cbp->cb_used) {
892e9dbad6fSeschrock 		void *ptr;
8933bb79becSeschrock 
894e9dbad6fSeschrock 		if ((ptr = zfs_realloc(zhp->zfs_hdl,
895e9dbad6fSeschrock 		    cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
896e9dbad6fSeschrock 		    cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
8973bb79becSeschrock 			return (-1);
898e9dbad6fSeschrock 		cbp->cb_datasets = ptr;
8993bb79becSeschrock 
900e9dbad6fSeschrock 		cbp->cb_alloc *= 2;
9013bb79becSeschrock 	}
9023bb79becSeschrock 
9033bb79becSeschrock 	cbp->cb_datasets[cbp->cb_used++] = zhp;
904f3861e1aSahl 
905f3861e1aSahl 	return (zfs_iter_children(zhp, mount_cb, cbp));
9063bb79becSeschrock }
9073bb79becSeschrock 
9083bb79becSeschrock static int
909f3861e1aSahl dataset_cmp(const void *a, const void *b)
9103bb79becSeschrock {
9113bb79becSeschrock 	zfs_handle_t **za = (zfs_handle_t **)a;
9123bb79becSeschrock 	zfs_handle_t **zb = (zfs_handle_t **)b;
9133bb79becSeschrock 	char mounta[MAXPATHLEN];
9143bb79becSeschrock 	char mountb[MAXPATHLEN];
915f3861e1aSahl 	boolean_t gota, gotb;
916f3861e1aSahl 
917f3861e1aSahl 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
918f3861e1aSahl 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
919f3861e1aSahl 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
920f3861e1aSahl 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
921f3861e1aSahl 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
922f3861e1aSahl 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
923f3861e1aSahl 
924f3861e1aSahl 	if (gota && gotb)
925f3861e1aSahl 		return (strcmp(mounta, mountb));
9263bb79becSeschrock 
927f3861e1aSahl 	if (gota)
928f3861e1aSahl 		return (-1);
929f3861e1aSahl 	if (gotb)
930f3861e1aSahl 		return (1);
9313bb79becSeschrock 
932f3861e1aSahl 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
9333bb79becSeschrock }
9343bb79becSeschrock 
935f3861e1aSahl /*
936f3861e1aSahl  * Mount and share all datasets within the given pool.  This assumes that no
937f3861e1aSahl  * datasets within the pool are currently mounted.  Because users can create
938f3861e1aSahl  * complicated nested hierarchies of mountpoints, we first gather all the
939f3861e1aSahl  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
940f3861e1aSahl  * we have the list of all filesystems, we iterate over them in order and mount
941f3861e1aSahl  * and/or share each one.
942f3861e1aSahl  */
943f3861e1aSahl #pragma weak zpool_mount_datasets = zpool_enable_datasets
9443bb79becSeschrock int
945f3861e1aSahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
9463bb79becSeschrock {
9473bb79becSeschrock 	mount_cbdata_t cb = { 0 };
9483bb79becSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
9493bb79becSeschrock 	zfs_handle_t *zfsp;
9503bb79becSeschrock 	int i, ret = -1;
95167331909Sdougm 	int *good;
9523bb79becSeschrock 
9533bb79becSeschrock 	/*
9543bb79becSeschrock 	 * Gather all datasets within the pool.
9553bb79becSeschrock 	 */
9563bb79becSeschrock 	if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
9573bb79becSeschrock 		return (-1);
9583bb79becSeschrock 	cb.cb_alloc = 4;
9593bb79becSeschrock 
960*990b4856Slling 	if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
9613bb79becSeschrock 		goto out;
9623bb79becSeschrock 
9633bb79becSeschrock 	cb.cb_datasets[0] = zfsp;
9643bb79becSeschrock 	cb.cb_used = 1;
9653bb79becSeschrock 
9663bb79becSeschrock 	if (zfs_iter_children(zfsp, mount_cb, &cb) != 0)
9673bb79becSeschrock 		goto out;
9683bb79becSeschrock 
9693bb79becSeschrock 	/*
9703bb79becSeschrock 	 * Sort the datasets by mountpoint.
9713bb79becSeschrock 	 */
972f3861e1aSahl 	qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
9733bb79becSeschrock 
9743bb79becSeschrock 	/*
97567331909Sdougm 	 * And mount all the datasets, keeping track of which ones
97667331909Sdougm 	 * succeeded or failed. By using zfs_alloc(), the good pointer
97767331909Sdougm 	 * will always be non-NULL.
9783bb79becSeschrock 	 */
97967331909Sdougm 	good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int));
9803bb79becSeschrock 	ret = 0;
9813bb79becSeschrock 	for (i = 0; i < cb.cb_used; i++) {
982d8689a57Sdougm 		if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0)
9833bb79becSeschrock 			ret = -1;
984d8689a57Sdougm 		else
98567331909Sdougm 			good[i] = 1;
9863bb79becSeschrock 	}
98767331909Sdougm 	/*
98867331909Sdougm 	 * Then share all the ones that need to be shared. This needs
98967331909Sdougm 	 * to be a separate pass in order to avoid excessive reloading
99067331909Sdougm 	 * of the configuration. Good should never be NULL since
99167331909Sdougm 	 * zfs_alloc is supposed to exit if memory isn't available.
99267331909Sdougm 	 */
99367331909Sdougm 	zfs_uninit_libshare(hdl);
99467331909Sdougm 	for (i = 0; i < cb.cb_used; i++) {
99567331909Sdougm 		if (good[i] && zfs_share(cb.cb_datasets[i]) != 0)
99667331909Sdougm 			ret = -1;
99767331909Sdougm 	}
99867331909Sdougm 
99967331909Sdougm 	free(good);
10003bb79becSeschrock 
10013bb79becSeschrock out:
10023bb79becSeschrock 	for (i = 0; i < cb.cb_used; i++)
10033bb79becSeschrock 		zfs_close(cb.cb_datasets[i]);
10043bb79becSeschrock 	free(cb.cb_datasets);
10053bb79becSeschrock 
10063bb79becSeschrock 	return (ret);
10073bb79becSeschrock }
10083bb79becSeschrock 
1009f3861e1aSahl 
1010f3861e1aSahl static int
1011f3861e1aSahl zvol_cb(const char *dataset, void *data)
1012f3861e1aSahl {
1013f3861e1aSahl 	libzfs_handle_t *hdl = data;
1014f3861e1aSahl 	zfs_handle_t *zhp;
1015f3861e1aSahl 
1016f3861e1aSahl 	/*
1017f3861e1aSahl 	 * Ignore snapshots and ignore failures from non-existant datasets.
1018f3861e1aSahl 	 */
1019f3861e1aSahl 	if (strchr(dataset, '@') != NULL ||
1020f3861e1aSahl 	    (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
1021f3861e1aSahl 		return (0);
1022f3861e1aSahl 
1023ecd6cf80Smarks 	if (zfs_unshare_iscsi(zhp) != 0)
1024ecd6cf80Smarks 		return (-1);
1025f3861e1aSahl 
1026f3861e1aSahl 	zfs_close(zhp);
1027f3861e1aSahl 
1028f3861e1aSahl 	return (0);
1029f3861e1aSahl }
1030f3861e1aSahl 
10313bb79becSeschrock static int
10323bb79becSeschrock mountpoint_compare(const void *a, const void *b)
10333bb79becSeschrock {
10343bb79becSeschrock 	const char *mounta = *((char **)a);
10353bb79becSeschrock 	const char *mountb = *((char **)b);
10363bb79becSeschrock 
10373bb79becSeschrock 	return (strcmp(mountb, mounta));
10383bb79becSeschrock }
10393bb79becSeschrock 
1040f3861e1aSahl /*
1041f3861e1aSahl  * Unshare and unmount all datasets within the given pool.  We don't want to
1042f3861e1aSahl  * rely on traversing the DSL to discover the filesystems within the pool,
1043f3861e1aSahl  * because this may be expensive (if not all of them are mounted), and can fail
1044f3861e1aSahl  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
1045f3861e1aSahl  * gather all the filesystems that are currently mounted.
1046f3861e1aSahl  */
1047f3861e1aSahl #pragma weak zpool_unmount_datasets = zpool_disable_datasets
10483bb79becSeschrock int
1049f3861e1aSahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
10503bb79becSeschrock {
10513bb79becSeschrock 	int used, alloc;
10523bb79becSeschrock 	struct mnttab entry;
10533bb79becSeschrock 	size_t namelen;
10543bb79becSeschrock 	char **mountpoints = NULL;
10553bb79becSeschrock 	zfs_handle_t **datasets = NULL;
10563bb79becSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
10573bb79becSeschrock 	int i;
10583bb79becSeschrock 	int ret = -1;
10593bb79becSeschrock 	int flags = (force ? MS_FORCE : 0);
10603bb79becSeschrock 
1061f3861e1aSahl 	/*
1062f3861e1aSahl 	 * First unshare all zvols.
1063f3861e1aSahl 	 */
1064f3861e1aSahl 	if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
1065f3861e1aSahl 		return (-1);
1066f3861e1aSahl 
10673bb79becSeschrock 	namelen = strlen(zhp->zpool_name);
10683bb79becSeschrock 
10693bb79becSeschrock 	rewind(hdl->libzfs_mnttab);
10703bb79becSeschrock 	used = alloc = 0;
10713bb79becSeschrock 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
10723bb79becSeschrock 		/*
10733bb79becSeschrock 		 * Ignore non-ZFS entries.
10743bb79becSeschrock 		 */
10753bb79becSeschrock 		if (entry.mnt_fstype == NULL ||
10763bb79becSeschrock 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
10773bb79becSeschrock 			continue;
10783bb79becSeschrock 
10793bb79becSeschrock 		/*
10803bb79becSeschrock 		 * Ignore filesystems not within this pool.
10813bb79becSeschrock 		 */
10823bb79becSeschrock 		if (entry.mnt_mountp == NULL ||
10833bb79becSeschrock 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
10843bb79becSeschrock 		    (entry.mnt_special[namelen] != '/' &&
10853bb79becSeschrock 		    entry.mnt_special[namelen] != '\0'))
10863bb79becSeschrock 			continue;
10873bb79becSeschrock 
10883bb79becSeschrock 		/*
10893bb79becSeschrock 		 * At this point we've found a filesystem within our pool.  Add
10903bb79becSeschrock 		 * it to our growing list.
10913bb79becSeschrock 		 */
10923bb79becSeschrock 		if (used == alloc) {
10933bb79becSeschrock 			if (alloc == 0) {
10943bb79becSeschrock 				if ((mountpoints = zfs_alloc(hdl,
10953bb79becSeschrock 				    8 * sizeof (void *))) == NULL)
10963bb79becSeschrock 					goto out;
10973bb79becSeschrock 
10983bb79becSeschrock 				if ((datasets = zfs_alloc(hdl,
10993bb79becSeschrock 				    8 * sizeof (void *))) == NULL)
11003bb79becSeschrock 					goto out;
11013bb79becSeschrock 
11023bb79becSeschrock 				alloc = 8;
11033bb79becSeschrock 			} else {
1104e9dbad6fSeschrock 				void *ptr;
11053bb79becSeschrock 
1106e9dbad6fSeschrock 				if ((ptr = zfs_realloc(hdl, mountpoints,
1107e9dbad6fSeschrock 				    alloc * sizeof (void *),
11083bb79becSeschrock 				    alloc * 2 * sizeof (void *))) == NULL)
11093bb79becSeschrock 					goto out;
1110e9dbad6fSeschrock 				mountpoints = ptr;
11113bb79becSeschrock 
1112e9dbad6fSeschrock 				if ((ptr = zfs_realloc(hdl, datasets,
1113e9dbad6fSeschrock 				    alloc * sizeof (void *),
11143bb79becSeschrock 				    alloc * 2 * sizeof (void *))) == NULL)
11153bb79becSeschrock 					goto out;
1116e9dbad6fSeschrock 				datasets = ptr;
11173bb79becSeschrock 
11183bb79becSeschrock 				alloc *= 2;
11193bb79becSeschrock 			}
11203bb79becSeschrock 		}
11213bb79becSeschrock 
11223bb79becSeschrock 		if ((mountpoints[used] = zfs_strdup(hdl,
11233bb79becSeschrock 		    entry.mnt_mountp)) == NULL)
11243bb79becSeschrock 			goto out;
11253bb79becSeschrock 
11263bb79becSeschrock 		/*
11273bb79becSeschrock 		 * This is allowed to fail, in case there is some I/O error.  It
11283bb79becSeschrock 		 * is only used to determine if we need to remove the underlying
11293bb79becSeschrock 		 * mountpoint, so failure is not fatal.
11303bb79becSeschrock 		 */
11313bb79becSeschrock 		datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
11323bb79becSeschrock 
11333bb79becSeschrock 		used++;
11343bb79becSeschrock 	}
11353bb79becSeschrock 
11363bb79becSeschrock 	/*
11373bb79becSeschrock 	 * At this point, we have the entire list of filesystems, so sort it by
11383bb79becSeschrock 	 * mountpoint.
11393bb79becSeschrock 	 */
11403bb79becSeschrock 	qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
11413bb79becSeschrock 
11423bb79becSeschrock 	/*
11433bb79becSeschrock 	 * Walk through and first unshare everything.
11443bb79becSeschrock 	 */
11453bb79becSeschrock 	for (i = 0; i < used; i++) {
11463bb79becSeschrock 		if (is_shared(hdl, mountpoints[i]) &&
1147e9dbad6fSeschrock 		    unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0)
11483bb79becSeschrock 			goto out;
11493bb79becSeschrock 	}
11503bb79becSeschrock 
11513bb79becSeschrock 	/*
11523bb79becSeschrock 	 * Now unmount everything, removing the underlying directories as
11533bb79becSeschrock 	 * appropriate.
11543bb79becSeschrock 	 */
11553bb79becSeschrock 	for (i = 0; i < used; i++) {
11563bb79becSeschrock 		if (unmount_one(hdl, mountpoints[i], flags) != 0)
11573bb79becSeschrock 			goto out;
1158e9dbad6fSeschrock 	}
11593bb79becSeschrock 
1160e9dbad6fSeschrock 	for (i = 0; i < used; i++) {
11613bb79becSeschrock 		if (datasets[i])
11623bb79becSeschrock 			remove_mountpoint(datasets[i]);
11633bb79becSeschrock 	}
11643bb79becSeschrock 
11653bb79becSeschrock 	ret = 0;
11663bb79becSeschrock out:
11673bb79becSeschrock 	for (i = 0; i < used; i++) {
11683bb79becSeschrock 		if (datasets[i])
11693bb79becSeschrock 			zfs_close(datasets[i]);
11703bb79becSeschrock 		free(mountpoints[i]);
11713bb79becSeschrock 	}
11723bb79becSeschrock 	free(datasets);
11733bb79becSeschrock 	free(mountpoints);
11743bb79becSeschrock 
11753bb79becSeschrock 	return (ret);
11763bb79becSeschrock }
1177