xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_mount.c (revision 1c18e8fbd8db41a9fb39bd3ef7a18ee71ece20da)
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 /*
23d1672efbSMarcel Telka  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
24dc7cd546SMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2509c9e6dcSChris Williamson  * Copyright (c) 2014, 2016 by Delphix. All rights reserved.
2688f61deeSIgor Kozhukhov  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27*1c18e8fbSJerry Jelinek  * Copyright 2017 Joyent, Inc.
284f4378ccSAndrew Stormont  * Copyright 2017 RackTop Systems.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens /*
32fa9e4066Sahrens  * Routines to manage ZFS mounts.  We separate all the nasty routines that have
33f3861e1aSahl  * to deal with the OS.  The following functions are the main entry points --
34f3861e1aSahl  * they are used by mount and unmount and when changing a filesystem's
35f3861e1aSahl  * mountpoint.
36fa9e4066Sahrens  *
37fa9e4066Sahrens  * 	zfs_is_mounted()
38fa9e4066Sahrens  * 	zfs_mount()
39fa9e4066Sahrens  * 	zfs_unmount()
40fa9e4066Sahrens  * 	zfs_unmountall()
41fa9e4066Sahrens  *
42f3861e1aSahl  * This file also contains the functions used to manage sharing filesystems via
43f3861e1aSahl  * NFS and iSCSI:
44fa9e4066Sahrens  *
45fa9e4066Sahrens  * 	zfs_is_shared()
46fa9e4066Sahrens  * 	zfs_share()
47fa9e4066Sahrens  * 	zfs_unshare()
48f3861e1aSahl  *
49f3861e1aSahl  * 	zfs_is_shared_nfs()
50da6c28aaSamw  * 	zfs_is_shared_smb()
51da6c28aaSamw  * 	zfs_share_proto()
52da6c28aaSamw  * 	zfs_shareall();
53da6c28aaSamw  * 	zfs_unshare_nfs()
54da6c28aaSamw  * 	zfs_unshare_smb()
55da6c28aaSamw  * 	zfs_unshareall_nfs()
56da6c28aaSamw  *	zfs_unshareall_smb()
57da6c28aaSamw  *	zfs_unshareall()
58da6c28aaSamw  *	zfs_unshareall_bypath()
593bb79becSeschrock  *
603bb79becSeschrock  * The following functions are available for pool consumers, and will
61f3861e1aSahl  * mount/unmount and share/unshare all datasets within pool:
623bb79becSeschrock  *
63f3861e1aSahl  * 	zpool_enable_datasets()
64f3861e1aSahl  * 	zpool_disable_datasets()
65fa9e4066Sahrens  */
66fa9e4066Sahrens 
67fa9e4066Sahrens #include <dirent.h>
68d8d59944Sahl #include <dlfcn.h>
69fa9e4066Sahrens #include <errno.h>
70d420209dSAlex Reece #include <fcntl.h>
71fa9e4066Sahrens #include <libgen.h>
72fa9e4066Sahrens #include <libintl.h>
73fa9e4066Sahrens #include <stdio.h>
74fa9e4066Sahrens #include <stdlib.h>
75fa9e4066Sahrens #include <strings.h>
76fa9e4066Sahrens #include <unistd.h>
77fa9e4066Sahrens #include <zone.h>
78fa9e4066Sahrens #include <sys/mntent.h>
79fa9e4066Sahrens #include <sys/mount.h>
80fa9e4066Sahrens #include <sys/stat.h>
81873c4903SPrakash Surya #include <sys/statvfs.h>
82fa9e4066Sahrens 
83fa9e4066Sahrens #include <libzfs.h>
84fa9e4066Sahrens 
85fa9e4066Sahrens #include "libzfs_impl.h"
86fa9e4066Sahrens 
8767331909Sdougm #include <libshare.h>
8867331909Sdougm #include <sys/systeminfo.h>
8967331909Sdougm #define	MAXISALEN	257	/* based on sysinfo(2) man page */
9067331909Sdougm 
91da6c28aaSamw static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
92da6c28aaSamw zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
93da6c28aaSamw     zfs_share_proto_t);
94da6c28aaSamw 
95da6c28aaSamw /*
964f4378ccSAndrew Stormont  * The share protocols table must be in the same order as the zfs_share_proto_t
97da6c28aaSamw  * enum in libzfs_impl.h
98da6c28aaSamw  */
99da6c28aaSamw typedef struct {
100da6c28aaSamw 	zfs_prop_t p_prop;
101da6c28aaSamw 	char *p_name;
102da6c28aaSamw 	int p_share_err;
103da6c28aaSamw 	int p_unshare_err;
104da6c28aaSamw } proto_table_t;
105da6c28aaSamw 
106da6c28aaSamw proto_table_t proto_table[PROTO_END] = {
107da6c28aaSamw 	{ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
108da6c28aaSamw 	{ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
109da6c28aaSamw };
110da6c28aaSamw 
111da6c28aaSamw zfs_share_proto_t nfs_only[] = {
112da6c28aaSamw 	PROTO_NFS,
113da6c28aaSamw 	PROTO_END
114da6c28aaSamw };
115da6c28aaSamw 
116da6c28aaSamw zfs_share_proto_t smb_only[] = {
117da6c28aaSamw 	PROTO_SMB,
118da6c28aaSamw 	PROTO_END
119da6c28aaSamw };
120da6c28aaSamw zfs_share_proto_t share_all_proto[] = {
121da6c28aaSamw 	PROTO_NFS,
122da6c28aaSamw 	PROTO_SMB,
123da6c28aaSamw 	PROTO_END
124da6c28aaSamw };
125da6c28aaSamw 
126fa9e4066Sahrens /*
127da6c28aaSamw  * Search the sharetab for the given mountpoint and protocol, returning
128da6c28aaSamw  * a zfs_share_type_t value.
129fa9e4066Sahrens  */
130da6c28aaSamw static zfs_share_type_t
131da6c28aaSamw is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
132fa9e4066Sahrens {
133fa9e4066Sahrens 	char buf[MAXPATHLEN], *tab;
134da6c28aaSamw 	char *ptr;
135fa9e4066Sahrens 
13699653d4eSeschrock 	if (hdl->libzfs_sharetab == NULL)
137da6c28aaSamw 		return (SHARED_NOT_SHARED);
138fa9e4066Sahrens 
13999653d4eSeschrock 	(void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
140fa9e4066Sahrens 
14199653d4eSeschrock 	while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
142fa9e4066Sahrens 
143fa9e4066Sahrens 		/* the mountpoint is the first entry on each line */
144da6c28aaSamw 		if ((tab = strchr(buf, '\t')) == NULL)
145da6c28aaSamw 			continue;
146da6c28aaSamw 
147da6c28aaSamw 		*tab = '\0';
148da6c28aaSamw 		if (strcmp(buf, mountpoint) == 0) {
149da6c28aaSamw 			/*
150da6c28aaSamw 			 * the protocol field is the third field
151da6c28aaSamw 			 * skip over second field
152da6c28aaSamw 			 */
153da6c28aaSamw 			ptr = ++tab;
154da6c28aaSamw 			if ((tab = strchr(ptr, '\t')) == NULL)
155da6c28aaSamw 				continue;
156da6c28aaSamw 			ptr = ++tab;
157da6c28aaSamw 			if ((tab = strchr(ptr, '\t')) == NULL)
158da6c28aaSamw 				continue;
159fa9e4066Sahrens 			*tab = '\0';
160da6c28aaSamw 			if (strcmp(ptr,
161da6c28aaSamw 			    proto_table[proto].p_name) == 0) {
162da6c28aaSamw 				switch (proto) {
163da6c28aaSamw 				case PROTO_NFS:
164da6c28aaSamw 					return (SHARED_NFS);
165da6c28aaSamw 				case PROTO_SMB:
166da6c28aaSamw 					return (SHARED_SMB);
167da6c28aaSamw 				default:
168da6c28aaSamw 					return (0);
169da6c28aaSamw 				}
170da6c28aaSamw 			}
171fa9e4066Sahrens 		}
172fa9e4066Sahrens 	}
173fa9e4066Sahrens 
174da6c28aaSamw 	return (SHARED_NOT_SHARED);
175fa9e4066Sahrens }
176fa9e4066Sahrens 
17799653d4eSeschrock static boolean_t
178873c4903SPrakash Surya dir_is_empty_stat(const char *dirname)
179873c4903SPrakash Surya {
180873c4903SPrakash Surya 	struct stat st;
181873c4903SPrakash Surya 
182873c4903SPrakash Surya 	/*
183873c4903SPrakash Surya 	 * We only want to return false if the given path is a non empty
184873c4903SPrakash Surya 	 * directory, all other errors are handled elsewhere.
185873c4903SPrakash Surya 	 */
186873c4903SPrakash Surya 	if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
187873c4903SPrakash Surya 		return (B_TRUE);
188873c4903SPrakash Surya 	}
189873c4903SPrakash Surya 
190873c4903SPrakash Surya 	/*
191873c4903SPrakash Surya 	 * An empty directory will still have two entries in it, one
192873c4903SPrakash Surya 	 * entry for each of "." and "..".
193873c4903SPrakash Surya 	 */
194873c4903SPrakash Surya 	if (st.st_size > 2) {
195873c4903SPrakash Surya 		return (B_FALSE);
196873c4903SPrakash Surya 	}
197873c4903SPrakash Surya 
198873c4903SPrakash Surya 	return (B_TRUE);
199873c4903SPrakash Surya }
200873c4903SPrakash Surya 
201873c4903SPrakash Surya static boolean_t
202873c4903SPrakash Surya dir_is_empty_readdir(const char *dirname)
203fa9e4066Sahrens {
204fa9e4066Sahrens 	DIR *dirp;
205fa9e4066Sahrens 	struct dirent64 *dp;
206d420209dSAlex Reece 	int dirfd;
207fa9e4066Sahrens 
208d420209dSAlex Reece 	if ((dirfd = openat(AT_FDCWD, dirname,
209d420209dSAlex Reece 	    O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
21099653d4eSeschrock 		return (B_TRUE);
211d420209dSAlex Reece 	}
212d420209dSAlex Reece 
213d420209dSAlex Reece 	if ((dirp = fdopendir(dirfd)) == NULL) {
214ba6e7e65SSowrabha Gopal 		(void) close(dirfd);
215d420209dSAlex Reece 		return (B_TRUE);
216d420209dSAlex Reece 	}
217fa9e4066Sahrens 
218fa9e4066Sahrens 	while ((dp = readdir64(dirp)) != NULL) {
219fa9e4066Sahrens 
220fa9e4066Sahrens 		if (strcmp(dp->d_name, ".") == 0 ||
221fa9e4066Sahrens 		    strcmp(dp->d_name, "..") == 0)
222fa9e4066Sahrens 			continue;
223fa9e4066Sahrens 
224fa9e4066Sahrens 		(void) closedir(dirp);
22599653d4eSeschrock 		return (B_FALSE);
226fa9e4066Sahrens 	}
227fa9e4066Sahrens 
228fa9e4066Sahrens 	(void) closedir(dirp);
22999653d4eSeschrock 	return (B_TRUE);
230fa9e4066Sahrens }
231fa9e4066Sahrens 
232873c4903SPrakash Surya /*
233873c4903SPrakash Surya  * Returns true if the specified directory is empty.  If we can't open the
234873c4903SPrakash Surya  * directory at all, return true so that the mount can fail with a more
235873c4903SPrakash Surya  * informative error message.
236873c4903SPrakash Surya  */
237873c4903SPrakash Surya static boolean_t
238873c4903SPrakash Surya dir_is_empty(const char *dirname)
239873c4903SPrakash Surya {
240873c4903SPrakash Surya 	struct statvfs64 st;
241873c4903SPrakash Surya 
242873c4903SPrakash Surya 	/*
243873c4903SPrakash Surya 	 * If the statvfs call fails or the filesystem is not a ZFS
244873c4903SPrakash Surya 	 * filesystem, fall back to the slow path which uses readdir.
245873c4903SPrakash Surya 	 */
246873c4903SPrakash Surya 	if ((statvfs64(dirname, &st) != 0) ||
247873c4903SPrakash Surya 	    (strcmp(st.f_basetype, "zfs") != 0)) {
248873c4903SPrakash Surya 		return (dir_is_empty_readdir(dirname));
249873c4903SPrakash Surya 	}
250873c4903SPrakash Surya 
251873c4903SPrakash Surya 	/*
252873c4903SPrakash Surya 	 * At this point, we know the provided path is on a ZFS
253873c4903SPrakash Surya 	 * filesystem, so we can use stat instead of readdir to
254873c4903SPrakash Surya 	 * determine if the directory is empty or not. We try to avoid
255873c4903SPrakash Surya 	 * using readdir because that requires opening "dirname"; this
256873c4903SPrakash Surya 	 * open file descriptor can potentially end up in a child
257873c4903SPrakash Surya 	 * process if there's a concurrent fork, thus preventing the
258873c4903SPrakash Surya 	 * zfs_mount() from otherwise succeeding (the open file
259873c4903SPrakash Surya 	 * descriptor inherited by the child process will cause the
260873c4903SPrakash Surya 	 * parent's mount to fail with EBUSY). The performance
261873c4903SPrakash Surya 	 * implications of replacing the open, read, and close with a
262873c4903SPrakash Surya 	 * single stat is nice; but is not the main motivation for the
263873c4903SPrakash Surya 	 * added complexity.
264873c4903SPrakash Surya 	 */
265873c4903SPrakash Surya 	return (dir_is_empty_stat(dirname));
266873c4903SPrakash Surya }
267873c4903SPrakash Surya 
268fa9e4066Sahrens /*
269fa9e4066Sahrens  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
270fa9e4066Sahrens  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
271fa9e4066Sahrens  * 0.
272fa9e4066Sahrens  */
27399653d4eSeschrock boolean_t
27455434c77Sek is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
275fa9e4066Sahrens {
276ebedde84SEric Taylor 	struct mnttab entry;
277fa9e4066Sahrens 
278ebedde84SEric Taylor 	if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
27999653d4eSeschrock 		return (B_FALSE);
280fa9e4066Sahrens 
281fa9e4066Sahrens 	if (where != NULL)
28255434c77Sek 		*where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
283fa9e4066Sahrens 
28499653d4eSeschrock 	return (B_TRUE);
285fa9e4066Sahrens }
286fa9e4066Sahrens 
28755434c77Sek boolean_t
28855434c77Sek zfs_is_mounted(zfs_handle_t *zhp, char **where)
28955434c77Sek {
29055434c77Sek 	return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
29155434c77Sek }
29255434c77Sek 
293e9dbad6fSeschrock /*
294e9dbad6fSeschrock  * Returns true if the given dataset is mountable, false otherwise.  Returns the
295e9dbad6fSeschrock  * mountpoint in 'buf'.
296e9dbad6fSeschrock  */
297e9dbad6fSeschrock static boolean_t
298e9dbad6fSeschrock zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
299990b4856Slling     zprop_source_t *source)
300e9dbad6fSeschrock {
3019adfa60dSMatthew Ahrens 	char sourceloc[MAXNAMELEN];
302990b4856Slling 	zprop_source_t sourcetype;
303e9dbad6fSeschrock 
304e9dbad6fSeschrock 	if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
305e9dbad6fSeschrock 		return (B_FALSE);
306e9dbad6fSeschrock 
307e9dbad6fSeschrock 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
308e9dbad6fSeschrock 	    &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
309e9dbad6fSeschrock 
310e9dbad6fSeschrock 	if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
311e9dbad6fSeschrock 	    strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
312e9dbad6fSeschrock 		return (B_FALSE);
313e9dbad6fSeschrock 
314a227b7f4Shs 	if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
315e9dbad6fSeschrock 		return (B_FALSE);
316e9dbad6fSeschrock 
317e9dbad6fSeschrock 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
318e9dbad6fSeschrock 	    getzoneid() == GLOBAL_ZONEID)
319e9dbad6fSeschrock 		return (B_FALSE);
320e9dbad6fSeschrock 
321e9dbad6fSeschrock 	if (source)
322e9dbad6fSeschrock 		*source = sourcetype;
323e9dbad6fSeschrock 
324e9dbad6fSeschrock 	return (B_TRUE);
325e9dbad6fSeschrock }
326e9dbad6fSeschrock 
327fa9e4066Sahrens /*
328fa9e4066Sahrens  * Mount the given filesystem.
329fa9e4066Sahrens  */
330fa9e4066Sahrens int
331fa9e4066Sahrens zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
332fa9e4066Sahrens {
333fa9e4066Sahrens 	struct stat buf;
334fa9e4066Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
335fa9e4066Sahrens 	char mntopts[MNT_LINE_MAX];
33699653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
337fa9e4066Sahrens 
338fa9e4066Sahrens 	if (options == NULL)
339fa9e4066Sahrens 		mntopts[0] = '\0';
340fa9e4066Sahrens 	else
341fa9e4066Sahrens 		(void) strlcpy(mntopts, options, sizeof (mntopts));
342fa9e4066Sahrens 
343f9af39baSGeorge Wilson 	/*
344f9af39baSGeorge Wilson 	 * If the pool is imported read-only then all mounts must be read-only
345f9af39baSGeorge Wilson 	 */
346f9af39baSGeorge Wilson 	if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
347f9af39baSGeorge Wilson 		flags |= MS_RDONLY;
348f9af39baSGeorge Wilson 
349e9dbad6fSeschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
35099653d4eSeschrock 		return (0);
351fa9e4066Sahrens 
352fa9e4066Sahrens 	/* Create the directory if it doesn't already exist */
353fa9e4066Sahrens 	if (lstat(mountpoint, &buf) != 0) {
354fa9e4066Sahrens 		if (mkdirp(mountpoint, 0755) != 0) {
35599653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35699653d4eSeschrock 			    "failed to create mountpoint"));
357ece3d9b3Slling 			return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
35899653d4eSeschrock 			    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
35999653d4eSeschrock 			    mountpoint));
360fa9e4066Sahrens 		}
361fa9e4066Sahrens 	}
362fa9e4066Sahrens 
363fa9e4066Sahrens 	/*
364fa9e4066Sahrens 	 * Determine if the mountpoint is empty.  If so, refuse to perform the
365fa9e4066Sahrens 	 * mount.  We don't perform this check if MS_OVERLAY is specified, which
366fa9e4066Sahrens 	 * would defeat the point.  We also avoid this check if 'remount' is
367fa9e4066Sahrens 	 * specified.
368fa9e4066Sahrens 	 */
369fa9e4066Sahrens 	if ((flags & MS_OVERLAY) == 0 &&
370fa9e4066Sahrens 	    strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
371fa9e4066Sahrens 	    !dir_is_empty(mountpoint)) {
37299653d4eSeschrock 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
37399653d4eSeschrock 		    "directory is not empty"));
374ece3d9b3Slling 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
37599653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
376fa9e4066Sahrens 	}
377fa9e4066Sahrens 
378fa9e4066Sahrens 	/* perform the mount */
379fa9e4066Sahrens 	if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
380fa9e4066Sahrens 	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
381fa9e4066Sahrens 		/*
382fa9e4066Sahrens 		 * Generic errors are nasty, but there are just way too many
383fa9e4066Sahrens 		 * from mount(), and they're well-understood.  We pick a few
384fa9e4066Sahrens 		 * common ones to improve upon.
385fa9e4066Sahrens 		 */
386d8689a57Sdougm 		if (errno == EBUSY) {
38799653d4eSeschrock 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38899653d4eSeschrock 			    "mountpoint or dataset is busy"));
389ecd6cf80Smarks 		} else if (errno == EPERM) {
390ecd6cf80Smarks 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
391ecd6cf80Smarks 			    "Insufficient privileges"));
39254a91118SChris Kirby 		} else if (errno == ENOTSUP) {
39354a91118SChris Kirby 			char buf[256];
394dc7cd546SMark Shellenbaum 			int spa_version;
39554a91118SChris Kirby 
396dc7cd546SMark Shellenbaum 			VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
39754a91118SChris Kirby 			(void) snprintf(buf, sizeof (buf),
398dc7cd546SMark Shellenbaum 			    dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
399dc7cd546SMark Shellenbaum 			    "file system on a version %d pool. Pool must be"
400dc7cd546SMark Shellenbaum 			    " upgraded to mount this file system."),
40154a91118SChris Kirby 			    (u_longlong_t)zfs_prop_get_int(zhp,
402dc7cd546SMark Shellenbaum 			    ZFS_PROP_VERSION), spa_version);
40354a91118SChris Kirby 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
404d8689a57Sdougm 		} else {
40599653d4eSeschrock 			zfs_error_aux(hdl, strerror(errno));
406d8689a57Sdougm 		}
407ece3d9b3Slling 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
40899653d4eSeschrock 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
40999653d4eSeschrock 		    zhp->zfs_name));
410fa9e4066Sahrens 	}
411fa9e4066Sahrens 
412ebedde84SEric Taylor 	/* add the mounted entry into our cache */
413ebedde84SEric Taylor 	libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
414ebedde84SEric Taylor 	    mntopts);
415fa9e4066Sahrens 	return (0);
416fa9e4066Sahrens }
417fa9e4066Sahrens 
4183bb79becSeschrock /*
4193bb79becSeschrock  * Unmount a single filesystem.
4203bb79becSeschrock  */
4213bb79becSeschrock static int
4223bb79becSeschrock unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
4233bb79becSeschrock {
4243bb79becSeschrock 	if (umount2(mountpoint, flags) != 0) {
4253bb79becSeschrock 		zfs_error_aux(hdl, strerror(errno));
426ece3d9b3Slling 		return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
4273bb79becSeschrock 		    dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
4283bb79becSeschrock 		    mountpoint));
4293bb79becSeschrock 	}
4303bb79becSeschrock 
4313bb79becSeschrock 	return (0);
4323bb79becSeschrock }
4333bb79becSeschrock 
434fa9e4066Sahrens /*
435fa9e4066Sahrens  * Unmount the given filesystem.
436fa9e4066Sahrens  */
437fa9e4066Sahrens int
438fa9e4066Sahrens zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
439fa9e4066Sahrens {
440ebedde84SEric Taylor 	libzfs_handle_t *hdl = zhp->zfs_hdl;
441ebedde84SEric Taylor 	struct mnttab entry;
44267331909Sdougm 	char *mntpt = NULL;
443fa9e4066Sahrens 
444ebedde84SEric Taylor 	/* check to see if we need to unmount the filesystem */
445fa9e4066Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
446ebedde84SEric Taylor 	    libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
44767331909Sdougm 		/*
44867331909Sdougm 		 * mountpoint may have come from a call to
44967331909Sdougm 		 * getmnt/getmntany if it isn't NULL. If it is NULL,
450ebedde84SEric Taylor 		 * we know it comes from libzfs_mnttab_find which can
451ebedde84SEric Taylor 		 * then get freed later. We strdup it to play it safe.
45267331909Sdougm 		 */
453fa9e4066Sahrens 		if (mountpoint == NULL)
454ebedde84SEric Taylor 			mntpt = zfs_strdup(hdl, entry.mnt_mountp);
45567331909Sdougm 		else
456ebedde84SEric Taylor 			mntpt = zfs_strdup(hdl, mountpoint);
457fa9e4066Sahrens 
458fa9e4066Sahrens 		/*
4593bb79becSeschrock 		 * Unshare and unmount the filesystem
460fa9e4066Sahrens 		 */
461da6c28aaSamw 		if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
462eb66cf86Sth 			return (-1);
463eb66cf86Sth 
464ebedde84SEric Taylor 		if (unmount_one(hdl, mntpt, flags) != 0) {
46567331909Sdougm 			free(mntpt);
466da6c28aaSamw 			(void) zfs_shareall(zhp);
467fa9e4066Sahrens 			return (-1);
46867331909Sdougm 		}
469ebedde84SEric Taylor 		libzfs_mnttab_remove(hdl, zhp->zfs_name);
47067331909Sdougm 		free(mntpt);
471fa9e4066Sahrens 	}
472fa9e4066Sahrens 
473fa9e4066Sahrens 	return (0);
474fa9e4066Sahrens }
475fa9e4066Sahrens 
476fa9e4066Sahrens /*
477fa9e4066Sahrens  * Unmount this filesystem and any children inheriting the mountpoint property.
478fa9e4066Sahrens  * To do this, just act like we're changing the mountpoint property, but don't
479fa9e4066Sahrens  * remount the filesystems afterwards.
480fa9e4066Sahrens  */
481fa9e4066Sahrens int
482fa9e4066Sahrens zfs_unmountall(zfs_handle_t *zhp, int flags)
483fa9e4066Sahrens {
484fa9e4066Sahrens 	prop_changelist_t *clp;
485fa9e4066Sahrens 	int ret;
486fa9e4066Sahrens 
4870069fd67STim Haley 	clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
488fa9e4066Sahrens 	if (clp == NULL)
489fa9e4066Sahrens 		return (-1);
490fa9e4066Sahrens 
491fa9e4066Sahrens 	ret = changelist_prefix(clp);
492fa9e4066Sahrens 	changelist_free(clp);
493fa9e4066Sahrens 
494fa9e4066Sahrens 	return (ret);
495fa9e4066Sahrens }
496fa9e4066Sahrens 
497f3861e1aSahl boolean_t
498f3861e1aSahl zfs_is_shared(zfs_handle_t *zhp)
499f3861e1aSahl {
500da6c28aaSamw 	zfs_share_type_t rc = 0;
501da6c28aaSamw 	zfs_share_proto_t *curr_proto;
502da6c28aaSamw 
503f3861e1aSahl 	if (ZFS_IS_VOLUME(zhp))
504ab003da8SJim Dunham 		return (B_FALSE);
505f3861e1aSahl 
506da6c28aaSamw 	for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
507da6c28aaSamw 	    curr_proto++)
508da6c28aaSamw 		rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
509da6c28aaSamw 
510da6c28aaSamw 	return (rc ? B_TRUE : B_FALSE);
511f3861e1aSahl }
512f3861e1aSahl 
513f3861e1aSahl int
514f3861e1aSahl zfs_share(zfs_handle_t *zhp)
515f3861e1aSahl {
5169d9a58e3SEric Taylor 	assert(!ZFS_IS_VOLUME(zhp));
517da6c28aaSamw 	return (zfs_share_proto(zhp, share_all_proto));
518f3861e1aSahl }
519f3861e1aSahl 
520f3861e1aSahl int
521f3861e1aSahl zfs_unshare(zfs_handle_t *zhp)
522f3861e1aSahl {
5239d9a58e3SEric Taylor 	assert(!ZFS_IS_VOLUME(zhp));
524da6c28aaSamw 	return (zfs_unshareall(zhp));
525f3861e1aSahl }
526f3861e1aSahl 
527fa9e4066Sahrens /*
528fa9e4066Sahrens  * Check to see if the filesystem is currently shared.
529fa9e4066Sahrens  */
530da6c28aaSamw zfs_share_type_t
531da6c28aaSamw zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
532fa9e4066Sahrens {
533fa9e4066Sahrens 	char *mountpoint;
534da6c28aaSamw 	zfs_share_type_t rc;
535fa9e4066Sahrens 
536fa9e4066Sahrens 	if (!zfs_is_mounted(zhp, &mountpoint))
537da6c28aaSamw 		return (SHARED_NOT_SHARED);
538fa9e4066Sahrens 
53988f61deeSIgor Kozhukhov 	if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))
54088f61deeSIgor Kozhukhov 	    != SHARED_NOT_SHARED) {
541fa9e4066Sahrens 		if (where != NULL)
542fa9e4066Sahrens 			*where = mountpoint;
543fa9e4066Sahrens 		else
544fa9e4066Sahrens 			free(mountpoint);
545da6c28aaSamw 		return (rc);
546fa9e4066Sahrens 	} else {
547fa9e4066Sahrens 		free(mountpoint);
548da6c28aaSamw 		return (SHARED_NOT_SHARED);
549fa9e4066Sahrens 	}
550fa9e4066Sahrens }
551fa9e4066Sahrens 
552da6c28aaSamw boolean_t
553da6c28aaSamw zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
554da6c28aaSamw {
555da6c28aaSamw 	return (zfs_is_shared_proto(zhp, where,
556da6c28aaSamw 	    PROTO_NFS) != SHARED_NOT_SHARED);
557da6c28aaSamw }
558da6c28aaSamw 
559da6c28aaSamw boolean_t
560da6c28aaSamw zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
561da6c28aaSamw {
562da6c28aaSamw 	return (zfs_is_shared_proto(zhp, where,
563da6c28aaSamw 	    PROTO_SMB) != SHARED_NOT_SHARED);
564da6c28aaSamw }
565da6c28aaSamw 
56667331909Sdougm /*
56767331909Sdougm  * Make sure things will work if libshare isn't installed by using
56867331909Sdougm  * wrapper functions that check to see that the pointers to functions
56967331909Sdougm  * initialized in _zfs_init_libshare() are actually present.
57067331909Sdougm  */
57167331909Sdougm 
57267331909Sdougm static sa_handle_t (*_sa_init)(int);
5738a981c33SDaniel Hoffman static sa_handle_t (*_sa_init_arg)(int, void *);
57467331909Sdougm static void (*_sa_fini)(sa_handle_t);
57567331909Sdougm static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
57667331909Sdougm static int (*_sa_enable_share)(sa_share_t, char *);
57767331909Sdougm static int (*_sa_disable_share)(sa_share_t, char *);
57867331909Sdougm static char *(*_sa_errorstr)(int);
57967331909Sdougm static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
5805b6e0c46Sdougm static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
5815b6e0c46Sdougm static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
5825b6e0c46Sdougm static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
5835b6e0c46Sdougm     char *, char *, zprop_source_t, char *, char *, char *);
5845b6e0c46Sdougm static void (*_sa_update_sharetab_ts)(sa_handle_t);
58567331909Sdougm 
58667331909Sdougm /*
58767331909Sdougm  * _zfs_init_libshare()
58867331909Sdougm  *
58967331909Sdougm  * Find the libshare.so.1 entry points that we use here and save the
59067331909Sdougm  * values to be used later. This is triggered by the runtime loader.
59167331909Sdougm  * Make sure the correct ISA version is loaded.
59267331909Sdougm  */
5935b6e0c46Sdougm 
59467331909Sdougm #pragma init(_zfs_init_libshare)
59567331909Sdougm static void
59667331909Sdougm _zfs_init_libshare(void)
59767331909Sdougm {
59867331909Sdougm 	void *libshare;
59967331909Sdougm 	char path[MAXPATHLEN];
60067331909Sdougm 	char isa[MAXISALEN];
60167331909Sdougm 
60267331909Sdougm #if defined(_LP64)
60367331909Sdougm 	if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
604d8689a57Sdougm 		isa[0] = '\0';
60567331909Sdougm #else
60667331909Sdougm 	isa[0] = '\0';
60767331909Sdougm #endif
60867331909Sdougm 	(void) snprintf(path, MAXPATHLEN,
609d8689a57Sdougm 	    "/usr/lib/%s/libshare.so.1", isa);
61067331909Sdougm 
61167331909Sdougm 	if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
612d8689a57Sdougm 		_sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
6138a981c33SDaniel Hoffman 		_sa_init_arg = (sa_handle_t (*)(int, void *))dlsym(libshare,
6148a981c33SDaniel Hoffman 		    "sa_init_arg");
615d8689a57Sdougm 		_sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
616d8689a57Sdougm 		_sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
617d8689a57Sdougm 		    dlsym(libshare, "sa_find_share");
618d8689a57Sdougm 		_sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
619d8689a57Sdougm 		    "sa_enable_share");
620d8689a57Sdougm 		_sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
621d8689a57Sdougm 		    "sa_disable_share");
622d8689a57Sdougm 		_sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
623d8689a57Sdougm 		_sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
624d8689a57Sdougm 		    dlsym(libshare, "sa_parse_legacy_options");
6255b6e0c46Sdougm 		_sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
6265b6e0c46Sdougm 		    dlsym(libshare, "sa_needs_refresh");
6275b6e0c46Sdougm 		_sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
6285b6e0c46Sdougm 		    dlsym(libshare, "sa_get_zfs_handle");
6295b6e0c46Sdougm 		_sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
6305b6e0c46Sdougm 		    sa_share_t, char *, char *, zprop_source_t, char *,
6315b6e0c46Sdougm 		    char *, char *))dlsym(libshare, "sa_zfs_process_share");
6325b6e0c46Sdougm 		_sa_update_sharetab_ts = (void (*)(sa_handle_t))
6335b6e0c46Sdougm 		    dlsym(libshare, "sa_update_sharetab_ts");
6348a981c33SDaniel Hoffman 		if (_sa_init == NULL || _sa_init_arg == NULL ||
6358a981c33SDaniel Hoffman 		    _sa_fini == NULL || _sa_find_share == NULL ||
6368a981c33SDaniel Hoffman 		    _sa_enable_share == NULL || _sa_disable_share == NULL ||
6378a981c33SDaniel Hoffman 		    _sa_errorstr == NULL || _sa_parse_legacy_options == NULL ||
6385b6e0c46Sdougm 		    _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
6395b6e0c46Sdougm 		    _sa_zfs_process_share == NULL ||
6405b6e0c46Sdougm 		    _sa_update_sharetab_ts == NULL) {
64157b448deSdougm 			_sa_init = NULL;
6428a981c33SDaniel Hoffman 			_sa_init_arg = NULL;
64357b448deSdougm 			_sa_fini = NULL;
64457b448deSdougm 			_sa_disable_share = NULL;
64557b448deSdougm 			_sa_enable_share = NULL;
64657b448deSdougm 			_sa_errorstr = NULL;
64757b448deSdougm 			_sa_parse_legacy_options = NULL;
64857b448deSdougm 			(void) dlclose(libshare);
6495b6e0c46Sdougm 			_sa_needs_refresh = NULL;
6505b6e0c46Sdougm 			_sa_get_zfs_handle = NULL;
6515b6e0c46Sdougm 			_sa_zfs_process_share = NULL;
6525b6e0c46Sdougm 			_sa_update_sharetab_ts = NULL;
65357b448deSdougm 		}
65467331909Sdougm 	}
65567331909Sdougm }
65667331909Sdougm 
65767331909Sdougm /*
65867331909Sdougm  * zfs_init_libshare(zhandle, service)
65967331909Sdougm  *
66067331909Sdougm  * Initialize the libshare API if it hasn't already been initialized.
66167331909Sdougm  * In all cases it returns 0 if it succeeded and an error if not. The
66267331909Sdougm  * service value is which part(s) of the API to initialize and is a
66367331909Sdougm  * direct map to the libshare sa_init(service) interface.
66467331909Sdougm  */
6658a981c33SDaniel Hoffman static int
6668a981c33SDaniel Hoffman zfs_init_libshare_impl(libzfs_handle_t *zhandle, int service, void *arg)
66767331909Sdougm {
668*1c18e8fbSJerry Jelinek 	/*
669*1c18e8fbSJerry Jelinek 	 * libshare is either not installed or we're in a branded zone. The
670*1c18e8fbSJerry Jelinek 	 * rest of the wrapper functions around the libshare calls already
671*1c18e8fbSJerry Jelinek 	 * handle NULL function pointers, but we don't want the callers of
672*1c18e8fbSJerry Jelinek 	 * zfs_init_libshare() to fail prematurely if libshare is not available.
673*1c18e8fbSJerry Jelinek 	 */
674d8689a57Sdougm 	if (_sa_init == NULL)
675*1c18e8fbSJerry Jelinek 		return (SA_OK);
676d8689a57Sdougm 
67709c9e6dcSChris Williamson 	/*
67809c9e6dcSChris Williamson 	 * Attempt to refresh libshare. This is necessary if there was a cache
67909c9e6dcSChris Williamson 	 * miss for a new ZFS dataset that was just created, or if state of the
68009c9e6dcSChris Williamson 	 * sharetab file has changed since libshare was last initialized. We
68109c9e6dcSChris Williamson 	 * want to make sure so check timestamps to see if a different process
68209c9e6dcSChris Williamson 	 * has updated any of the configuration. If there was some non-ZFS
68309c9e6dcSChris Williamson 	 * change, we need to re-initialize the internal cache.
68409c9e6dcSChris Williamson 	 */
68509c9e6dcSChris Williamson 	if (_sa_needs_refresh != NULL &&
68609c9e6dcSChris Williamson 	    _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
68709c9e6dcSChris Williamson 		zfs_uninit_libshare(zhandle);
6888a981c33SDaniel Hoffman 		zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
6895b6e0c46Sdougm 	}
6905b6e0c46Sdougm 
69109c9e6dcSChris Williamson 	if (zhandle && zhandle->libzfs_sharehdl == NULL)
6928a981c33SDaniel Hoffman 		zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
693d8689a57Sdougm 
69409c9e6dcSChris Williamson 	if (zhandle->libzfs_sharehdl == NULL)
69509c9e6dcSChris Williamson 		return (SA_NO_MEMORY);
696d8689a57Sdougm 
69709c9e6dcSChris Williamson 	return (SA_OK);
69867331909Sdougm }
6998a981c33SDaniel Hoffman int
7008a981c33SDaniel Hoffman zfs_init_libshare(libzfs_handle_t *zhandle, int service)
7018a981c33SDaniel Hoffman {
7028a981c33SDaniel Hoffman 	return (zfs_init_libshare_impl(zhandle, service, NULL));
7038a981c33SDaniel Hoffman }
7048a981c33SDaniel Hoffman 
7058a981c33SDaniel Hoffman int
7068a981c33SDaniel Hoffman zfs_init_libshare_arg(libzfs_handle_t *zhandle, int service, void *arg)
7078a981c33SDaniel Hoffman {
7088a981c33SDaniel Hoffman 	return (zfs_init_libshare_impl(zhandle, service, arg));
7098a981c33SDaniel Hoffman }
7108a981c33SDaniel Hoffman 
71167331909Sdougm 
71267331909Sdougm /*
71367331909Sdougm  * zfs_uninit_libshare(zhandle)
71467331909Sdougm  *
71567331909Sdougm  * Uninitialize the libshare API if it hasn't already been
71667331909Sdougm  * uninitialized. It is OK to call multiple times.
71767331909Sdougm  */
71867331909Sdougm void
71967331909Sdougm zfs_uninit_libshare(libzfs_handle_t *zhandle)
72067331909Sdougm {
72167331909Sdougm 	if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
722d8689a57Sdougm 		if (_sa_fini != NULL)
723d8689a57Sdougm 			_sa_fini(zhandle->libzfs_sharehdl);
724d8689a57Sdougm 		zhandle->libzfs_sharehdl = NULL;
72567331909Sdougm 	}
72667331909Sdougm }
72767331909Sdougm 
72867331909Sdougm /*
72967331909Sdougm  * zfs_parse_options(options, proto)
73067331909Sdougm  *
73167331909Sdougm  * Call the legacy parse interface to get the protocol specific
73267331909Sdougm  * options using the NULL arg to indicate that this is a "parse" only.
73367331909Sdougm  */
73467331909Sdougm int
735da6c28aaSamw zfs_parse_options(char *options, zfs_share_proto_t proto)
73667331909Sdougm {
7373cb34c60Sahrens 	if (_sa_parse_legacy_options != NULL) {
7383cb34c60Sahrens 		return (_sa_parse_legacy_options(NULL, options,
7393cb34c60Sahrens 		    proto_table[proto].p_name));
7403cb34c60Sahrens 	}
7413cb34c60Sahrens 	return (SA_CONFIG_ERR);
74267331909Sdougm }
74367331909Sdougm 
74467331909Sdougm /*
74567331909Sdougm  * zfs_sa_find_share(handle, path)
74667331909Sdougm  *
74767331909Sdougm  * wrapper around sa_find_share to find a share path in the
74867331909Sdougm  * configuration.
74967331909Sdougm  */
75067331909Sdougm static sa_share_t
75167331909Sdougm zfs_sa_find_share(sa_handle_t handle, char *path)
75267331909Sdougm {
75367331909Sdougm 	if (_sa_find_share != NULL)
754d8689a57Sdougm 		return (_sa_find_share(handle, path));
75567331909Sdougm 	return (NULL);
75667331909Sdougm }
75767331909Sdougm 
75867331909Sdougm /*
75967331909Sdougm  * zfs_sa_enable_share(share, proto)
76067331909Sdougm  *
76167331909Sdougm  * Wrapper for sa_enable_share which enables a share for a specified
76267331909Sdougm  * protocol.
76367331909Sdougm  */
76467331909Sdougm static int
76567331909Sdougm zfs_sa_enable_share(sa_share_t share, char *proto)
76667331909Sdougm {
76767331909Sdougm 	if (_sa_enable_share != NULL)
768d8689a57Sdougm 		return (_sa_enable_share(share, proto));
76967331909Sdougm 	return (SA_CONFIG_ERR);
77067331909Sdougm }
77167331909Sdougm 
77267331909Sdougm /*
77367331909Sdougm  * zfs_sa_disable_share(share, proto)
77467331909Sdougm  *
77567331909Sdougm  * Wrapper for sa_enable_share which disables a share for a specified
77667331909Sdougm  * protocol.
77767331909Sdougm  */
77867331909Sdougm static int
77967331909Sdougm zfs_sa_disable_share(sa_share_t share, char *proto)
78067331909Sdougm {
78167331909Sdougm 	if (_sa_disable_share != NULL)
782d8689a57Sdougm 		return (_sa_disable_share(share, proto));
78367331909Sdougm 	return (SA_CONFIG_ERR);
78467331909Sdougm }
78567331909Sdougm 
786fa9e4066Sahrens /*
787da6c28aaSamw  * Share the given filesystem according to the options in the specified
788da6c28aaSamw  * protocol specific properties (sharenfs, sharesmb).  We rely
78967331909Sdougm  * on "libshare" to the dirty work for us.
790fa9e4066Sahrens  */
791da6c28aaSamw static int
792da6c28aaSamw zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
793fa9e4066Sahrens {
794fa9e4066Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
795fa9e4066Sahrens 	char shareopts[ZFS_MAXPROPLEN];
7965b6e0c46Sdougm 	char sourcestr[ZFS_MAXPROPLEN];
79799653d4eSeschrock 	libzfs_handle_t *hdl = zhp->zfs_hdl;
79867331909Sdougm 	sa_share_t share;
799da6c28aaSamw 	zfs_share_proto_t *curr_proto;
8005b6e0c46Sdougm 	zprop_source_t sourcetype;
80167331909Sdougm 	int ret;
802fa9e4066Sahrens 
803e9dbad6fSeschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
804fa9e4066Sahrens 		return (0);
805fa9e4066Sahrens 
806da6c28aaSamw 	for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
807da6c28aaSamw 		/*
808da6c28aaSamw 		 * Return success if there are no share options.
809da6c28aaSamw 		 */
810da6c28aaSamw 		if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
8115b6e0c46Sdougm 		    shareopts, sizeof (shareopts), &sourcetype, sourcestr,
8125b6e0c46Sdougm 		    ZFS_MAXPROPLEN, B_FALSE) != 0 ||
8135b6e0c46Sdougm 		    strcmp(shareopts, "off") == 0)
814da6c28aaSamw 			continue;
8158a981c33SDaniel Hoffman 		ret = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_HANDLE,
8168a981c33SDaniel Hoffman 		    zhp);
81733cde0d0SMatthew Ahrens 		if (ret != SA_OK) {
81833cde0d0SMatthew Ahrens 			(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
81933cde0d0SMatthew Ahrens 			    dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
82033cde0d0SMatthew Ahrens 			    zfs_get_name(zhp), _sa_errorstr != NULL ?
82133cde0d0SMatthew Ahrens 			    _sa_errorstr(ret) : "");
82233cde0d0SMatthew Ahrens 			return (-1);
82333cde0d0SMatthew Ahrens 		}
82433cde0d0SMatthew Ahrens 
825da6c28aaSamw 		/*
826da6c28aaSamw 		 * If the 'zoned' property is set, then zfs_is_mountable()
827da6c28aaSamw 		 * will have already bailed out if we are in the global zone.
828da6c28aaSamw 		 * But local zones cannot be NFS servers, so we ignore it for
829da6c28aaSamw 		 * local zones as well.
830da6c28aaSamw 		 */
831da6c28aaSamw 		if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
832da6c28aaSamw 			continue;
833da6c28aaSamw 
834da6c28aaSamw 		share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
8355b6e0c46Sdougm 		if (share == NULL) {
8365b6e0c46Sdougm 			/*
8375b6e0c46Sdougm 			 * This may be a new file system that was just
8385b6e0c46Sdougm 			 * created so isn't in the internal cache
8395b6e0c46Sdougm 			 * (second time through). Rather than
8405b6e0c46Sdougm 			 * reloading the entire configuration, we can
8415b6e0c46Sdougm 			 * assume ZFS has done the checking and it is
8425b6e0c46Sdougm 			 * safe to add this to the internal
8435b6e0c46Sdougm 			 * configuration.
8445b6e0c46Sdougm 			 */
8455b6e0c46Sdougm 			if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
8465b6e0c46Sdougm 			    NULL, NULL, mountpoint,
8475b6e0c46Sdougm 			    proto_table[*curr_proto].p_name, sourcetype,
8485b6e0c46Sdougm 			    shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
8495b6e0c46Sdougm 				(void) zfs_error_fmt(hdl,
8505b6e0c46Sdougm 				    proto_table[*curr_proto].p_share_err,
8515b6e0c46Sdougm 				    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
8525b6e0c46Sdougm 				    zfs_get_name(zhp));
8535b6e0c46Sdougm 				return (-1);
8545b6e0c46Sdougm 			}
8555b6e0c46Sdougm 			share = zfs_sa_find_share(hdl->libzfs_sharehdl,
8565b6e0c46Sdougm 			    mountpoint);
8575b6e0c46Sdougm 		}
858da6c28aaSamw 		if (share != NULL) {
859da6c28aaSamw 			int err;
860da6c28aaSamw 			err = zfs_sa_enable_share(share,
861da6c28aaSamw 			    proto_table[*curr_proto].p_name);
862da6c28aaSamw 			if (err != SA_OK) {
863da6c28aaSamw 				(void) zfs_error_fmt(hdl,
864da6c28aaSamw 				    proto_table[*curr_proto].p_share_err,
865da6c28aaSamw 				    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
866da6c28aaSamw 				    zfs_get_name(zhp));
867da6c28aaSamw 				return (-1);
868da6c28aaSamw 			}
869da6c28aaSamw 		} else {
870da6c28aaSamw 			(void) zfs_error_fmt(hdl,
871da6c28aaSamw 			    proto_table[*curr_proto].p_share_err,
872d8689a57Sdougm 			    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
873d8689a57Sdougm 			    zfs_get_name(zhp));
874d8689a57Sdougm 			return (-1);
875d8689a57Sdougm 		}
876fa9e4066Sahrens 
877da6c28aaSamw 	}
878fa9e4066Sahrens 	return (0);
879fa9e4066Sahrens }
880fa9e4066Sahrens 
881da6c28aaSamw 
882da6c28aaSamw int
883da6c28aaSamw zfs_share_nfs(zfs_handle_t *zhp)
884da6c28aaSamw {
885da6c28aaSamw 	return (zfs_share_proto(zhp, nfs_only));
886da6c28aaSamw }
887da6c28aaSamw 
888da6c28aaSamw int
889da6c28aaSamw zfs_share_smb(zfs_handle_t *zhp)
890da6c28aaSamw {
891da6c28aaSamw 	return (zfs_share_proto(zhp, smb_only));
892da6c28aaSamw }
893da6c28aaSamw 
894da6c28aaSamw int
895da6c28aaSamw zfs_shareall(zfs_handle_t *zhp)
896da6c28aaSamw {
897da6c28aaSamw 	return (zfs_share_proto(zhp, share_all_proto));
898da6c28aaSamw }
899da6c28aaSamw 
9003bb79becSeschrock /*
9013bb79becSeschrock  * Unshare a filesystem by mountpoint.
9023bb79becSeschrock  */
9033bb79becSeschrock static int
904da6c28aaSamw unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
905da6c28aaSamw     zfs_share_proto_t proto)
9063bb79becSeschrock {
90767331909Sdougm 	sa_share_t share;
90867331909Sdougm 	int err;
90967331909Sdougm 	char *mntpt;
9108a981c33SDaniel Hoffman 
9113bb79becSeschrock 	/*
91267331909Sdougm 	 * Mountpoint could get trashed if libshare calls getmntany
913ebedde84SEric Taylor 	 * which it does during API initialization, so strdup the
91467331909Sdougm 	 * value.
9153bb79becSeschrock 	 */
91667331909Sdougm 	mntpt = zfs_strdup(hdl, mountpoint);
9173bb79becSeschrock 
9188a981c33SDaniel Hoffman 	/*
9198a981c33SDaniel Hoffman 	 * make sure libshare initialized, initialize everything because we
9208a981c33SDaniel Hoffman 	 * don't know what other unsharing may happen later. Functions up the
9218a981c33SDaniel Hoffman 	 * stack are allowed to initialize instead a subset of shares at the
9228a981c33SDaniel Hoffman 	 * time the set is known.
9238a981c33SDaniel Hoffman 	 */
9248a981c33SDaniel Hoffman 	if ((err = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_NAME,
9258a981c33SDaniel Hoffman 	    (void *)name)) != SA_OK) {
92667331909Sdougm 		free(mntpt);	/* don't need the copy anymore */
9274f4378ccSAndrew Stormont 		return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
928d8689a57Sdougm 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
929d8689a57Sdougm 		    name, _sa_errorstr(err)));
93067331909Sdougm 	}
9313bb79becSeschrock 
93267331909Sdougm 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
93367331909Sdougm 	free(mntpt);	/* don't need the copy anymore */
9343bb79becSeschrock 
93567331909Sdougm 	if (share != NULL) {
936da6c28aaSamw 		err = zfs_sa_disable_share(share, proto_table[proto].p_name);
93767331909Sdougm 		if (err != SA_OK) {
9384f4378ccSAndrew Stormont 			return (zfs_error_fmt(hdl,
9394f4378ccSAndrew Stormont 			    proto_table[proto].p_unshare_err,
940d8689a57Sdougm 			    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
941d8689a57Sdougm 			    name, _sa_errorstr(err)));
94267331909Sdougm 		}
94367331909Sdougm 	} else {
9444f4378ccSAndrew Stormont 		return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
945d8689a57Sdougm 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
946d8689a57Sdougm 		    name));
9473bb79becSeschrock 	}
9483bb79becSeschrock 	return (0);
9493bb79becSeschrock }
9503bb79becSeschrock 
951fa9e4066Sahrens /*
952fa9e4066Sahrens  * Unshare the given filesystem.
953fa9e4066Sahrens  */
954fa9e4066Sahrens int
955da6c28aaSamw zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
956da6c28aaSamw     zfs_share_proto_t *proto)
957fa9e4066Sahrens {
958ebedde84SEric Taylor 	libzfs_handle_t *hdl = zhp->zfs_hdl;
959ebedde84SEric Taylor 	struct mnttab entry;
96067331909Sdougm 	char *mntpt = NULL;
961fa9e4066Sahrens 
962fa9e4066Sahrens 	/* check to see if need to unmount the filesystem */
96399653d4eSeschrock 	rewind(zhp->zfs_hdl->libzfs_mnttab);
964d8689a57Sdougm 	if (mountpoint != NULL)
965ebedde84SEric Taylor 		mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
966d8689a57Sdougm 
967fa9e4066Sahrens 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
968ebedde84SEric Taylor 	    libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
969da6c28aaSamw 		zfs_share_proto_t *curr_proto;
970fa9e4066Sahrens 
971fa9e4066Sahrens 		if (mountpoint == NULL)
972da6c28aaSamw 			mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
973fa9e4066Sahrens 
974da6c28aaSamw 		for (curr_proto = proto; *curr_proto != PROTO_END;
975da6c28aaSamw 		    curr_proto++) {
976da6c28aaSamw 
977ebedde84SEric Taylor 			if (is_shared(hdl, mntpt, *curr_proto) &&
978ebedde84SEric Taylor 			    unshare_one(hdl, zhp->zfs_name,
979da6c28aaSamw 			    mntpt, *curr_proto) != 0) {
980da6c28aaSamw 				if (mntpt != NULL)
981da6c28aaSamw 					free(mntpt);
982da6c28aaSamw 				return (-1);
983da6c28aaSamw 			}
98467331909Sdougm 		}
985fa9e4066Sahrens 	}
98667331909Sdougm 	if (mntpt != NULL)
987d8689a57Sdougm 		free(mntpt);
988fa9e4066Sahrens 
989fa9e4066Sahrens 	return (0);
990fa9e4066Sahrens }
991fa9e4066Sahrens 
992da6c28aaSamw int
993da6c28aaSamw zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
994da6c28aaSamw {
995da6c28aaSamw 	return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
996da6c28aaSamw }
997da6c28aaSamw 
998da6c28aaSamw int
999da6c28aaSamw zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
1000da6c28aaSamw {
1001da6c28aaSamw 	return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1002da6c28aaSamw }
1003da6c28aaSamw 
1004fa9e4066Sahrens /*
1005da6c28aaSamw  * Same as zfs_unmountall(), but for NFS and SMB unshares.
1006fa9e4066Sahrens  */
1007fa9e4066Sahrens int
1008da6c28aaSamw zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
1009fa9e4066Sahrens {
1010fa9e4066Sahrens 	prop_changelist_t *clp;
1011fa9e4066Sahrens 	int ret;
1012fa9e4066Sahrens 
10130069fd67STim Haley 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
1014fa9e4066Sahrens 	if (clp == NULL)
1015fa9e4066Sahrens 		return (-1);
1016fa9e4066Sahrens 
1017da6c28aaSamw 	ret = changelist_unshare(clp, proto);
1018fa9e4066Sahrens 	changelist_free(clp);
1019fa9e4066Sahrens 
1020fa9e4066Sahrens 	return (ret);
1021fa9e4066Sahrens }
1022fa9e4066Sahrens 
1023da6c28aaSamw int
1024da6c28aaSamw zfs_unshareall_nfs(zfs_handle_t *zhp)
1025da6c28aaSamw {
1026da6c28aaSamw 	return (zfs_unshareall_proto(zhp, nfs_only));
1027da6c28aaSamw }
1028da6c28aaSamw 
1029da6c28aaSamw int
1030da6c28aaSamw zfs_unshareall_smb(zfs_handle_t *zhp)
1031da6c28aaSamw {
1032da6c28aaSamw 	return (zfs_unshareall_proto(zhp, smb_only));
1033da6c28aaSamw }
1034da6c28aaSamw 
1035da6c28aaSamw int
1036da6c28aaSamw zfs_unshareall(zfs_handle_t *zhp)
1037da6c28aaSamw {
1038da6c28aaSamw 	return (zfs_unshareall_proto(zhp, share_all_proto));
1039da6c28aaSamw }
1040da6c28aaSamw 
1041da6c28aaSamw int
1042da6c28aaSamw zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1043da6c28aaSamw {
1044da6c28aaSamw 	return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1045da6c28aaSamw }
1046da6c28aaSamw 
1047fa9e4066Sahrens /*
1048fa9e4066Sahrens  * Remove the mountpoint associated with the current dataset, if necessary.
1049fa9e4066Sahrens  * We only remove the underlying directory if:
1050fa9e4066Sahrens  *
1051fa9e4066Sahrens  *	- The mountpoint is not 'none' or 'legacy'
1052fa9e4066Sahrens  *	- The mountpoint is non-empty
1053fa9e4066Sahrens  *	- The mountpoint is the default or inherited
1054fa9e4066Sahrens  *	- The 'zoned' property is set, or we're in a local zone
1055fa9e4066Sahrens  *
1056fa9e4066Sahrens  * Any other directories we leave alone.
1057fa9e4066Sahrens  */
1058fa9e4066Sahrens void
1059fa9e4066Sahrens remove_mountpoint(zfs_handle_t *zhp)
1060fa9e4066Sahrens {
1061fa9e4066Sahrens 	char mountpoint[ZFS_MAXPROPLEN];
1062990b4856Slling 	zprop_source_t source;
1063fa9e4066Sahrens 
1064e9dbad6fSeschrock 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1065e9dbad6fSeschrock 	    &source))
1066fa9e4066Sahrens 		return;
1067fa9e4066Sahrens 
1068990b4856Slling 	if (source == ZPROP_SRC_DEFAULT ||
1069990b4856Slling 	    source == ZPROP_SRC_INHERITED) {
1070fa9e4066Sahrens 		/*
1071fa9e4066Sahrens 		 * Try to remove the directory, silently ignoring any errors.
1072fa9e4066Sahrens 		 * The filesystem may have since been removed or moved around,
1073e9dbad6fSeschrock 		 * and this error isn't really useful to the administrator in
1074e9dbad6fSeschrock 		 * any way.
1075fa9e4066Sahrens 		 */
1076fa9e4066Sahrens 		(void) rmdir(mountpoint);
1077fa9e4066Sahrens 	}
1078fa9e4066Sahrens }
10793bb79becSeschrock 
10809d9a58e3SEric Taylor void
10819d9a58e3SEric Taylor libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
10829d9a58e3SEric Taylor {
10839d9a58e3SEric Taylor 	if (cbp->cb_alloc == cbp->cb_used) {
10849d9a58e3SEric Taylor 		size_t newsz;
10859d9a58e3SEric Taylor 		void *ptr;
10869d9a58e3SEric Taylor 
10879d9a58e3SEric Taylor 		newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
10889d9a58e3SEric Taylor 		ptr = zfs_realloc(zhp->zfs_hdl,
10899d9a58e3SEric Taylor 		    cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
10909d9a58e3SEric Taylor 		    newsz * sizeof (void *));
10919d9a58e3SEric Taylor 		cbp->cb_handles = ptr;
10929d9a58e3SEric Taylor 		cbp->cb_alloc = newsz;
10939d9a58e3SEric Taylor 	}
10949d9a58e3SEric Taylor 	cbp->cb_handles[cbp->cb_used++] = zhp;
10959d9a58e3SEric Taylor }
10963bb79becSeschrock 
10973bb79becSeschrock static int
10983bb79becSeschrock mount_cb(zfs_handle_t *zhp, void *data)
10993bb79becSeschrock {
11009d9a58e3SEric Taylor 	get_all_cb_t *cbp = data;
11013bb79becSeschrock 
11029d9a58e3SEric Taylor 	if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
11033bb79becSeschrock 		zfs_close(zhp);
11043bb79becSeschrock 		return (0);
11053bb79becSeschrock 	}
11063bb79becSeschrock 
1107a227b7f4Shs 	if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1108a227b7f4Shs 		zfs_close(zhp);
1109a227b7f4Shs 		return (0);
1110a227b7f4Shs 	}
1111a227b7f4Shs 
11129c3fd121SMatthew Ahrens 	/*
11139c3fd121SMatthew Ahrens 	 * If this filesystem is inconsistent and has a receive resume
11149c3fd121SMatthew Ahrens 	 * token, we can not mount it.
11159c3fd121SMatthew Ahrens 	 */
11169c3fd121SMatthew Ahrens 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
11179c3fd121SMatthew Ahrens 	    zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
11189c3fd121SMatthew Ahrens 	    NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
11199c3fd121SMatthew Ahrens 		zfs_close(zhp);
11209c3fd121SMatthew Ahrens 		return (0);
11219c3fd121SMatthew Ahrens 	}
11229c3fd121SMatthew Ahrens 
11239d9a58e3SEric Taylor 	libzfs_add_handle(cbp, zhp);
11249d9a58e3SEric Taylor 	if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
11259d9a58e3SEric Taylor 		zfs_close(zhp);
11269d9a58e3SEric Taylor 		return (-1);
11273bb79becSeschrock 	}
11289d9a58e3SEric Taylor 	return (0);
11293bb79becSeschrock }
11303bb79becSeschrock 
11319d9a58e3SEric Taylor int
11329d9a58e3SEric Taylor libzfs_dataset_cmp(const void *a, const void *b)
11333bb79becSeschrock {
11343bb79becSeschrock 	zfs_handle_t **za = (zfs_handle_t **)a;
11353bb79becSeschrock 	zfs_handle_t **zb = (zfs_handle_t **)b;
11363bb79becSeschrock 	char mounta[MAXPATHLEN];
11373bb79becSeschrock 	char mountb[MAXPATHLEN];
1138f3861e1aSahl 	boolean_t gota, gotb;
1139f3861e1aSahl 
1140f3861e1aSahl 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1141f3861e1aSahl 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1142f3861e1aSahl 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1143f3861e1aSahl 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1144f3861e1aSahl 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1145f3861e1aSahl 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1146f3861e1aSahl 
1147f3861e1aSahl 	if (gota && gotb)
1148f3861e1aSahl 		return (strcmp(mounta, mountb));
11493bb79becSeschrock 
1150f3861e1aSahl 	if (gota)
1151f3861e1aSahl 		return (-1);
1152f3861e1aSahl 	if (gotb)
1153f3861e1aSahl 		return (1);
11543bb79becSeschrock 
1155f3861e1aSahl 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
11563bb79becSeschrock }
11573bb79becSeschrock 
1158f3861e1aSahl /*
1159f3861e1aSahl  * Mount and share all datasets within the given pool.  This assumes that no
1160f3861e1aSahl  * datasets within the pool are currently mounted.  Because users can create
1161f3861e1aSahl  * complicated nested hierarchies of mountpoints, we first gather all the
1162f3861e1aSahl  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
1163f3861e1aSahl  * we have the list of all filesystems, we iterate over them in order and mount
1164f3861e1aSahl  * and/or share each one.
1165f3861e1aSahl  */
1166f3861e1aSahl #pragma weak zpool_mount_datasets = zpool_enable_datasets
11673bb79becSeschrock int
1168f3861e1aSahl zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
11693bb79becSeschrock {
11709d9a58e3SEric Taylor 	get_all_cb_t cb = { 0 };
11713bb79becSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
11723bb79becSeschrock 	zfs_handle_t *zfsp;
11733bb79becSeschrock 	int i, ret = -1;
117467331909Sdougm 	int *good;
11753bb79becSeschrock 
11763bb79becSeschrock 	/*
1177d87468daSrm 	 * Gather all non-snap datasets within the pool.
11783bb79becSeschrock 	 */
1179990b4856Slling 	if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
11803bb79becSeschrock 		goto out;
11813bb79becSeschrock 
11829d9a58e3SEric Taylor 	libzfs_add_handle(&cb, zfsp);
1183d87468daSrm 	if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
11843bb79becSeschrock 		goto out;
11853bb79becSeschrock 	/*
11863bb79becSeschrock 	 * Sort the datasets by mountpoint.
11873bb79becSeschrock 	 */
11889d9a58e3SEric Taylor 	qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
11899d9a58e3SEric Taylor 	    libzfs_dataset_cmp);
11903bb79becSeschrock 
11913bb79becSeschrock 	/*
119267331909Sdougm 	 * And mount all the datasets, keeping track of which ones
11936e77af0aSDavid Pacheco 	 * succeeded or failed.
11943bb79becSeschrock 	 */
11956e77af0aSDavid Pacheco 	if ((good = zfs_alloc(zhp->zpool_hdl,
11966e77af0aSDavid Pacheco 	    cb.cb_used * sizeof (int))) == NULL)
11976e77af0aSDavid Pacheco 		goto out;
11986e77af0aSDavid Pacheco 
11993bb79becSeschrock 	ret = 0;
12003bb79becSeschrock 	for (i = 0; i < cb.cb_used; i++) {
12019d9a58e3SEric Taylor 		if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
12023bb79becSeschrock 			ret = -1;
1203d8689a57Sdougm 		else
120467331909Sdougm 			good[i] = 1;
12053bb79becSeschrock 	}
12065b6e0c46Sdougm 
120767331909Sdougm 	/*
120867331909Sdougm 	 * Then share all the ones that need to be shared. This needs
120967331909Sdougm 	 * to be a separate pass in order to avoid excessive reloading
121067331909Sdougm 	 * of the configuration. Good should never be NULL since
121167331909Sdougm 	 * zfs_alloc is supposed to exit if memory isn't available.
121267331909Sdougm 	 */
121367331909Sdougm 	for (i = 0; i < cb.cb_used; i++) {
12149d9a58e3SEric Taylor 		if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
121567331909Sdougm 			ret = -1;
121667331909Sdougm 	}
121767331909Sdougm 
121867331909Sdougm 	free(good);
12193bb79becSeschrock 
12203bb79becSeschrock out:
12213bb79becSeschrock 	for (i = 0; i < cb.cb_used; i++)
12229d9a58e3SEric Taylor 		zfs_close(cb.cb_handles[i]);
12239d9a58e3SEric Taylor 	free(cb.cb_handles);
12243bb79becSeschrock 
12253bb79becSeschrock 	return (ret);
12263bb79becSeschrock }
12273bb79becSeschrock 
12283bb79becSeschrock static int
12293bb79becSeschrock mountpoint_compare(const void *a, const void *b)
12303bb79becSeschrock {
12313bb79becSeschrock 	const char *mounta = *((char **)a);
12323bb79becSeschrock 	const char *mountb = *((char **)b);
12333bb79becSeschrock 
12343bb79becSeschrock 	return (strcmp(mountb, mounta));
12353bb79becSeschrock }
12363bb79becSeschrock 
1237681d9761SEric Taylor /* alias for 2002/240 */
1238681d9761SEric Taylor #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1239f3861e1aSahl /*
1240f3861e1aSahl  * Unshare and unmount all datasets within the given pool.  We don't want to
1241f3861e1aSahl  * rely on traversing the DSL to discover the filesystems within the pool,
1242f3861e1aSahl  * because this may be expensive (if not all of them are mounted), and can fail
1243f3861e1aSahl  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
1244f3861e1aSahl  * gather all the filesystems that are currently mounted.
1245f3861e1aSahl  */
12463bb79becSeschrock int
1247f3861e1aSahl zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
12483bb79becSeschrock {
12493bb79becSeschrock 	int used, alloc;
12503bb79becSeschrock 	struct mnttab entry;
12513bb79becSeschrock 	size_t namelen;
12523bb79becSeschrock 	char **mountpoints = NULL;
12533bb79becSeschrock 	zfs_handle_t **datasets = NULL;
12543bb79becSeschrock 	libzfs_handle_t *hdl = zhp->zpool_hdl;
12553bb79becSeschrock 	int i;
12563bb79becSeschrock 	int ret = -1;
12573bb79becSeschrock 	int flags = (force ? MS_FORCE : 0);
12588a981c33SDaniel Hoffman 	sa_init_selective_arg_t sharearg;
12593bb79becSeschrock 
12603bb79becSeschrock 	namelen = strlen(zhp->zpool_name);
12613bb79becSeschrock 
12623bb79becSeschrock 	rewind(hdl->libzfs_mnttab);
12633bb79becSeschrock 	used = alloc = 0;
12643bb79becSeschrock 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
12653bb79becSeschrock 		/*
12663bb79becSeschrock 		 * Ignore non-ZFS entries.
12673bb79becSeschrock 		 */
12683bb79becSeschrock 		if (entry.mnt_fstype == NULL ||
12693bb79becSeschrock 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
12703bb79becSeschrock 			continue;
12713bb79becSeschrock 
12723bb79becSeschrock 		/*
12733bb79becSeschrock 		 * Ignore filesystems not within this pool.
12743bb79becSeschrock 		 */
12753bb79becSeschrock 		if (entry.mnt_mountp == NULL ||
12763bb79becSeschrock 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
12773bb79becSeschrock 		    (entry.mnt_special[namelen] != '/' &&
12783bb79becSeschrock 		    entry.mnt_special[namelen] != '\0'))
12793bb79becSeschrock 			continue;
12803bb79becSeschrock 
12813bb79becSeschrock 		/*
12823bb79becSeschrock 		 * At this point we've found a filesystem within our pool.  Add
12833bb79becSeschrock 		 * it to our growing list.
12843bb79becSeschrock 		 */
12853bb79becSeschrock 		if (used == alloc) {
12863bb79becSeschrock 			if (alloc == 0) {
12873bb79becSeschrock 				if ((mountpoints = zfs_alloc(hdl,
12883bb79becSeschrock 				    8 * sizeof (void *))) == NULL)
12893bb79becSeschrock 					goto out;
12903bb79becSeschrock 
12913bb79becSeschrock 				if ((datasets = zfs_alloc(hdl,
12923bb79becSeschrock 				    8 * sizeof (void *))) == NULL)
12933bb79becSeschrock 					goto out;
12943bb79becSeschrock 
12953bb79becSeschrock 				alloc = 8;
12963bb79becSeschrock 			} else {
1297e9dbad6fSeschrock 				void *ptr;
12983bb79becSeschrock 
1299e9dbad6fSeschrock 				if ((ptr = zfs_realloc(hdl, mountpoints,
1300e9dbad6fSeschrock 				    alloc * sizeof (void *),
13013bb79becSeschrock 				    alloc * 2 * sizeof (void *))) == NULL)
13023bb79becSeschrock 					goto out;
1303e9dbad6fSeschrock 				mountpoints = ptr;
13043bb79becSeschrock 
1305e9dbad6fSeschrock 				if ((ptr = zfs_realloc(hdl, datasets,
1306e9dbad6fSeschrock 				    alloc * sizeof (void *),
13073bb79becSeschrock 				    alloc * 2 * sizeof (void *))) == NULL)
13083bb79becSeschrock 					goto out;
1309e9dbad6fSeschrock 				datasets = ptr;
13103bb79becSeschrock 
13113bb79becSeschrock 				alloc *= 2;
13123bb79becSeschrock 			}
13133bb79becSeschrock 		}
13143bb79becSeschrock 
13153bb79becSeschrock 		if ((mountpoints[used] = zfs_strdup(hdl,
13163bb79becSeschrock 		    entry.mnt_mountp)) == NULL)
13173bb79becSeschrock 			goto out;
13183bb79becSeschrock 
13193bb79becSeschrock 		/*
13203bb79becSeschrock 		 * This is allowed to fail, in case there is some I/O error.  It
13213bb79becSeschrock 		 * is only used to determine if we need to remove the underlying
13223bb79becSeschrock 		 * mountpoint, so failure is not fatal.
13233bb79becSeschrock 		 */
13243bb79becSeschrock 		datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
13253bb79becSeschrock 
13263bb79becSeschrock 		used++;
13273bb79becSeschrock 	}
13283bb79becSeschrock 
13293bb79becSeschrock 	/*
13303bb79becSeschrock 	 * At this point, we have the entire list of filesystems, so sort it by
13313bb79becSeschrock 	 * mountpoint.
13323bb79becSeschrock 	 */
13338a981c33SDaniel Hoffman 	sharearg.zhandle_arr = datasets;
13348a981c33SDaniel Hoffman 	sharearg.zhandle_len = used;
13358a981c33SDaniel Hoffman 	ret = zfs_init_libshare_arg(hdl, SA_INIT_SHARE_API_SELECTIVE,
13368a981c33SDaniel Hoffman 	    &sharearg);
13378a981c33SDaniel Hoffman 	if (ret != 0)
13388a981c33SDaniel Hoffman 		goto out;
13393bb79becSeschrock 	qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
13403bb79becSeschrock 
13413bb79becSeschrock 	/*
13423bb79becSeschrock 	 * Walk through and first unshare everything.
13433bb79becSeschrock 	 */
13443bb79becSeschrock 	for (i = 0; i < used; i++) {
1345da6c28aaSamw 		zfs_share_proto_t *curr_proto;
1346da6c28aaSamw 		for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1347da6c28aaSamw 		    curr_proto++) {
1348da6c28aaSamw 			if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1349da6c28aaSamw 			    unshare_one(hdl, mountpoints[i],
1350da6c28aaSamw 			    mountpoints[i], *curr_proto) != 0)
1351da6c28aaSamw 				goto out;
1352da6c28aaSamw 		}
13533bb79becSeschrock 	}
13543bb79becSeschrock 
13553bb79becSeschrock 	/*
13563bb79becSeschrock 	 * Now unmount everything, removing the underlying directories as
13573bb79becSeschrock 	 * appropriate.
13583bb79becSeschrock 	 */
13593bb79becSeschrock 	for (i = 0; i < used; i++) {
13603bb79becSeschrock 		if (unmount_one(hdl, mountpoints[i], flags) != 0)
13613bb79becSeschrock 			goto out;
1362e9dbad6fSeschrock 	}
13633bb79becSeschrock 
1364e9dbad6fSeschrock 	for (i = 0; i < used; i++) {
13653bb79becSeschrock 		if (datasets[i])
13663bb79becSeschrock 			remove_mountpoint(datasets[i]);
13673bb79becSeschrock 	}
13683bb79becSeschrock 
13693bb79becSeschrock 	ret = 0;
13703bb79becSeschrock out:
13713bb79becSeschrock 	for (i = 0; i < used; i++) {
13723bb79becSeschrock 		if (datasets[i])
13733bb79becSeschrock 			zfs_close(datasets[i]);
13743bb79becSeschrock 		free(mountpoints[i]);
13753bb79becSeschrock 	}
13763bb79becSeschrock 	free(datasets);
13773bb79becSeschrock 	free(mountpoints);
13783bb79becSeschrock 
13793bb79becSeschrock 	return (ret);
13803bb79becSeschrock }
1381