xref: /freebsd/sys/contrib/openzfs/lib/libzfs/libzfs_mount.c (revision 681ce946f33e75c590e97c53076e86dff1fe8f4a)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
259db44a8eSMartin Matuska  * Copyright (c) 2014, 2021 by Delphix. All rights reserved.
26eda14cbcSMatt Macy  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27eda14cbcSMatt Macy  * Copyright 2017 RackTop Systems.
28eda14cbcSMatt Macy  * Copyright (c) 2018 Datto Inc.
29eda14cbcSMatt Macy  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
30eda14cbcSMatt Macy  */
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy /*
33eda14cbcSMatt Macy  * Routines to manage ZFS mounts.  We separate all the nasty routines that have
34eda14cbcSMatt Macy  * to deal with the OS.  The following functions are the main entry points --
35eda14cbcSMatt Macy  * they are used by mount and unmount and when changing a filesystem's
36eda14cbcSMatt Macy  * mountpoint.
37eda14cbcSMatt Macy  *
38eda14cbcSMatt Macy  *	zfs_is_mounted()
39eda14cbcSMatt Macy  *	zfs_mount()
40eda14cbcSMatt Macy  *	zfs_mount_at()
41eda14cbcSMatt Macy  *	zfs_unmount()
42eda14cbcSMatt Macy  *	zfs_unmountall()
43eda14cbcSMatt Macy  *
44eda14cbcSMatt Macy  * This file also contains the functions used to manage sharing filesystems via
45eda14cbcSMatt Macy  * NFS and iSCSI:
46eda14cbcSMatt Macy  *
47eda14cbcSMatt Macy  *	zfs_is_shared()
48eda14cbcSMatt Macy  *	zfs_share()
49eda14cbcSMatt Macy  *	zfs_unshare()
50eda14cbcSMatt Macy  *
51eda14cbcSMatt Macy  *	zfs_is_shared_nfs()
52eda14cbcSMatt Macy  *	zfs_is_shared_smb()
53eda14cbcSMatt Macy  *	zfs_share_proto()
54eda14cbcSMatt Macy  *	zfs_shareall();
55eda14cbcSMatt Macy  *	zfs_unshare_nfs()
56eda14cbcSMatt Macy  *	zfs_unshare_smb()
57eda14cbcSMatt Macy  *	zfs_unshareall_nfs()
58eda14cbcSMatt Macy  *	zfs_unshareall_smb()
59eda14cbcSMatt Macy  *	zfs_unshareall()
60eda14cbcSMatt Macy  *	zfs_unshareall_bypath()
61eda14cbcSMatt Macy  *
62eda14cbcSMatt Macy  * The following functions are available for pool consumers, and will
63eda14cbcSMatt Macy  * mount/unmount and share/unshare all datasets within pool:
64eda14cbcSMatt Macy  *
65eda14cbcSMatt Macy  *	zpool_enable_datasets()
66eda14cbcSMatt Macy  *	zpool_disable_datasets()
67eda14cbcSMatt Macy  */
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy #include <dirent.h>
70eda14cbcSMatt Macy #include <dlfcn.h>
71eda14cbcSMatt Macy #include <errno.h>
72eda14cbcSMatt Macy #include <fcntl.h>
73eda14cbcSMatt Macy #include <libgen.h>
74eda14cbcSMatt Macy #include <libintl.h>
75eda14cbcSMatt Macy #include <stdio.h>
76eda14cbcSMatt Macy #include <stdlib.h>
77eda14cbcSMatt Macy #include <strings.h>
78eda14cbcSMatt Macy #include <unistd.h>
79eda14cbcSMatt Macy #include <zone.h>
80eda14cbcSMatt Macy #include <sys/mntent.h>
81eda14cbcSMatt Macy #include <sys/mount.h>
82eda14cbcSMatt Macy #include <sys/stat.h>
83eda14cbcSMatt Macy #include <sys/vfs.h>
84eda14cbcSMatt Macy #include <sys/dsl_crypt.h>
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy #include <libzfs.h>
87eda14cbcSMatt Macy 
88eda14cbcSMatt Macy #include "libzfs_impl.h"
89eda14cbcSMatt Macy #include <thread_pool.h>
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy #include <libshare.h>
92eda14cbcSMatt Macy #include <sys/systeminfo.h>
93eda14cbcSMatt Macy #define	MAXISALEN	257	/* based on sysinfo(2) man page */
94eda14cbcSMatt Macy 
95eda14cbcSMatt Macy static int mount_tp_nthr = 512;	/* tpool threads for multi-threaded mounting */
96eda14cbcSMatt Macy 
97eda14cbcSMatt Macy static void zfs_mount_task(void *);
9816038816SMartin Matuska static zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
99eda14cbcSMatt Macy     zfs_share_proto_t);
100eda14cbcSMatt Macy 
101eda14cbcSMatt Macy /*
102eda14cbcSMatt Macy  * The share protocols table must be in the same order as the zfs_share_proto_t
103eda14cbcSMatt Macy  * enum in libzfs_impl.h
104eda14cbcSMatt Macy  */
105eda14cbcSMatt Macy proto_table_t proto_table[PROTO_END] = {
106eda14cbcSMatt Macy 	{ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
107eda14cbcSMatt Macy 	{ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
108eda14cbcSMatt Macy };
109eda14cbcSMatt Macy 
11016038816SMartin Matuska static zfs_share_proto_t nfs_only[] = {
111eda14cbcSMatt Macy 	PROTO_NFS,
112eda14cbcSMatt Macy 	PROTO_END
113eda14cbcSMatt Macy };
114eda14cbcSMatt Macy 
11516038816SMartin Matuska static zfs_share_proto_t smb_only[] = {
116eda14cbcSMatt Macy 	PROTO_SMB,
117eda14cbcSMatt Macy 	PROTO_END
118eda14cbcSMatt Macy };
11916038816SMartin Matuska static zfs_share_proto_t share_all_proto[] = {
120eda14cbcSMatt Macy 	PROTO_NFS,
121eda14cbcSMatt Macy 	PROTO_SMB,
122eda14cbcSMatt Macy 	PROTO_END
123eda14cbcSMatt Macy };
124eda14cbcSMatt Macy 
125eda14cbcSMatt Macy 
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy static boolean_t
128eda14cbcSMatt Macy dir_is_empty_stat(const char *dirname)
129eda14cbcSMatt Macy {
130eda14cbcSMatt Macy 	struct stat st;
131eda14cbcSMatt Macy 
132eda14cbcSMatt Macy 	/*
133eda14cbcSMatt Macy 	 * We only want to return false if the given path is a non empty
134eda14cbcSMatt Macy 	 * directory, all other errors are handled elsewhere.
135eda14cbcSMatt Macy 	 */
136eda14cbcSMatt Macy 	if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
137eda14cbcSMatt Macy 		return (B_TRUE);
138eda14cbcSMatt Macy 	}
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy 	/*
141eda14cbcSMatt Macy 	 * An empty directory will still have two entries in it, one
142eda14cbcSMatt Macy 	 * entry for each of "." and "..".
143eda14cbcSMatt Macy 	 */
144eda14cbcSMatt Macy 	if (st.st_size > 2) {
145eda14cbcSMatt Macy 		return (B_FALSE);
146eda14cbcSMatt Macy 	}
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy 	return (B_TRUE);
149eda14cbcSMatt Macy }
150eda14cbcSMatt Macy 
151eda14cbcSMatt Macy static boolean_t
152eda14cbcSMatt Macy dir_is_empty_readdir(const char *dirname)
153eda14cbcSMatt Macy {
154eda14cbcSMatt Macy 	DIR *dirp;
155eda14cbcSMatt Macy 	struct dirent64 *dp;
156eda14cbcSMatt Macy 	int dirfd;
157eda14cbcSMatt Macy 
158eda14cbcSMatt Macy 	if ((dirfd = openat(AT_FDCWD, dirname,
159eda14cbcSMatt Macy 	    O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
160eda14cbcSMatt Macy 		return (B_TRUE);
161eda14cbcSMatt Macy 	}
162eda14cbcSMatt Macy 
163eda14cbcSMatt Macy 	if ((dirp = fdopendir(dirfd)) == NULL) {
164eda14cbcSMatt Macy 		(void) close(dirfd);
165eda14cbcSMatt Macy 		return (B_TRUE);
166eda14cbcSMatt Macy 	}
167eda14cbcSMatt Macy 
168eda14cbcSMatt Macy 	while ((dp = readdir64(dirp)) != NULL) {
169eda14cbcSMatt Macy 
170eda14cbcSMatt Macy 		if (strcmp(dp->d_name, ".") == 0 ||
171eda14cbcSMatt Macy 		    strcmp(dp->d_name, "..") == 0)
172eda14cbcSMatt Macy 			continue;
173eda14cbcSMatt Macy 
174eda14cbcSMatt Macy 		(void) closedir(dirp);
175eda14cbcSMatt Macy 		return (B_FALSE);
176eda14cbcSMatt Macy 	}
177eda14cbcSMatt Macy 
178eda14cbcSMatt Macy 	(void) closedir(dirp);
179eda14cbcSMatt Macy 	return (B_TRUE);
180eda14cbcSMatt Macy }
181eda14cbcSMatt Macy 
182eda14cbcSMatt Macy /*
183eda14cbcSMatt Macy  * Returns true if the specified directory is empty.  If we can't open the
184eda14cbcSMatt Macy  * directory at all, return true so that the mount can fail with a more
185eda14cbcSMatt Macy  * informative error message.
186eda14cbcSMatt Macy  */
187eda14cbcSMatt Macy static boolean_t
188eda14cbcSMatt Macy dir_is_empty(const char *dirname)
189eda14cbcSMatt Macy {
190eda14cbcSMatt Macy 	struct statfs64 st;
191eda14cbcSMatt Macy 
192eda14cbcSMatt Macy 	/*
193eda14cbcSMatt Macy 	 * If the statvfs call fails or the filesystem is not a ZFS
194eda14cbcSMatt Macy 	 * filesystem, fall back to the slow path which uses readdir.
195eda14cbcSMatt Macy 	 */
196eda14cbcSMatt Macy 	if ((statfs64(dirname, &st) != 0) ||
197eda14cbcSMatt Macy 	    (st.f_type != ZFS_SUPER_MAGIC)) {
198eda14cbcSMatt Macy 		return (dir_is_empty_readdir(dirname));
199eda14cbcSMatt Macy 	}
200eda14cbcSMatt Macy 
201eda14cbcSMatt Macy 	/*
202eda14cbcSMatt Macy 	 * At this point, we know the provided path is on a ZFS
203eda14cbcSMatt Macy 	 * filesystem, so we can use stat instead of readdir to
204eda14cbcSMatt Macy 	 * determine if the directory is empty or not. We try to avoid
205eda14cbcSMatt Macy 	 * using readdir because that requires opening "dirname"; this
206eda14cbcSMatt Macy 	 * open file descriptor can potentially end up in a child
207eda14cbcSMatt Macy 	 * process if there's a concurrent fork, thus preventing the
208eda14cbcSMatt Macy 	 * zfs_mount() from otherwise succeeding (the open file
209eda14cbcSMatt Macy 	 * descriptor inherited by the child process will cause the
210eda14cbcSMatt Macy 	 * parent's mount to fail with EBUSY). The performance
211eda14cbcSMatt Macy 	 * implications of replacing the open, read, and close with a
212eda14cbcSMatt Macy 	 * single stat is nice; but is not the main motivation for the
213eda14cbcSMatt Macy 	 * added complexity.
214eda14cbcSMatt Macy 	 */
215eda14cbcSMatt Macy 	return (dir_is_empty_stat(dirname));
216eda14cbcSMatt Macy }
217eda14cbcSMatt Macy 
218eda14cbcSMatt Macy /*
219eda14cbcSMatt Macy  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
220eda14cbcSMatt Macy  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
221eda14cbcSMatt Macy  * 0.
222eda14cbcSMatt Macy  */
223eda14cbcSMatt Macy boolean_t
224eda14cbcSMatt Macy is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
225eda14cbcSMatt Macy {
226eda14cbcSMatt Macy 	struct mnttab entry;
227eda14cbcSMatt Macy 
228eda14cbcSMatt Macy 	if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
229eda14cbcSMatt Macy 		return (B_FALSE);
230eda14cbcSMatt Macy 
231eda14cbcSMatt Macy 	if (where != NULL)
232eda14cbcSMatt Macy 		*where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
233eda14cbcSMatt Macy 
234eda14cbcSMatt Macy 	return (B_TRUE);
235eda14cbcSMatt Macy }
236eda14cbcSMatt Macy 
237eda14cbcSMatt Macy boolean_t
238eda14cbcSMatt Macy zfs_is_mounted(zfs_handle_t *zhp, char **where)
239eda14cbcSMatt Macy {
240eda14cbcSMatt Macy 	return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
241eda14cbcSMatt Macy }
242eda14cbcSMatt Macy 
243eda14cbcSMatt Macy /*
244eda14cbcSMatt Macy  * Checks any higher order concerns about whether the given dataset is
245eda14cbcSMatt Macy  * mountable, false otherwise.  zfs_is_mountable_internal specifically assumes
246eda14cbcSMatt Macy  * that the caller has verified the sanity of mounting the dataset at
247eda14cbcSMatt Macy  * mountpoint to the extent the caller wants.
248eda14cbcSMatt Macy  */
249eda14cbcSMatt Macy static boolean_t
250eda14cbcSMatt Macy zfs_is_mountable_internal(zfs_handle_t *zhp, const char *mountpoint)
251eda14cbcSMatt Macy {
252eda14cbcSMatt Macy 
253eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
254eda14cbcSMatt Macy 	    getzoneid() == GLOBAL_ZONEID)
255eda14cbcSMatt Macy 		return (B_FALSE);
256eda14cbcSMatt Macy 
257eda14cbcSMatt Macy 	return (B_TRUE);
258eda14cbcSMatt Macy }
259eda14cbcSMatt Macy 
260eda14cbcSMatt Macy /*
261eda14cbcSMatt Macy  * Returns true if the given dataset is mountable, false otherwise.  Returns the
262eda14cbcSMatt Macy  * mountpoint in 'buf'.
263eda14cbcSMatt Macy  */
264eda14cbcSMatt Macy boolean_t
265eda14cbcSMatt Macy zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
266eda14cbcSMatt Macy     zprop_source_t *source, int flags)
267eda14cbcSMatt Macy {
268eda14cbcSMatt Macy 	char sourceloc[MAXNAMELEN];
269eda14cbcSMatt Macy 	zprop_source_t sourcetype;
270eda14cbcSMatt Macy 
271eda14cbcSMatt Macy 	if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type,
272eda14cbcSMatt Macy 	    B_FALSE))
273eda14cbcSMatt Macy 		return (B_FALSE);
274eda14cbcSMatt Macy 
275eda14cbcSMatt Macy 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
276eda14cbcSMatt Macy 	    &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
277eda14cbcSMatt Macy 
278eda14cbcSMatt Macy 	if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
279eda14cbcSMatt Macy 	    strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
280eda14cbcSMatt Macy 		return (B_FALSE);
281eda14cbcSMatt Macy 
282eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
283eda14cbcSMatt Macy 		return (B_FALSE);
284eda14cbcSMatt Macy 
285eda14cbcSMatt Macy 	if (!zfs_is_mountable_internal(zhp, buf))
286eda14cbcSMatt Macy 		return (B_FALSE);
287eda14cbcSMatt Macy 
288eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE))
289eda14cbcSMatt Macy 		return (B_FALSE);
290eda14cbcSMatt Macy 
291eda14cbcSMatt Macy 	if (source)
292eda14cbcSMatt Macy 		*source = sourcetype;
293eda14cbcSMatt Macy 
294eda14cbcSMatt Macy 	return (B_TRUE);
295eda14cbcSMatt Macy }
296eda14cbcSMatt Macy 
297eda14cbcSMatt Macy /*
298eda14cbcSMatt Macy  * The filesystem is mounted by invoking the system mount utility rather
299eda14cbcSMatt Macy  * than by the system call mount(2).  This ensures that the /etc/mtab
300eda14cbcSMatt Macy  * file is correctly locked for the update.  Performing our own locking
301eda14cbcSMatt Macy  * and /etc/mtab update requires making an unsafe assumption about how
302eda14cbcSMatt Macy  * the mount utility performs its locking.  Unfortunately, this also means
303eda14cbcSMatt Macy  * in the case of a mount failure we do not have the exact errno.  We must
304eda14cbcSMatt Macy  * make due with return value from the mount process.
305eda14cbcSMatt Macy  *
306eda14cbcSMatt Macy  * In the long term a shared library called libmount is under development
307eda14cbcSMatt Macy  * which provides a common API to address the locking and errno issues.
308eda14cbcSMatt Macy  * Once the standard mount utility has been updated to use this library
309eda14cbcSMatt Macy  * we can add an autoconf check to conditionally use it.
310eda14cbcSMatt Macy  *
311eda14cbcSMatt Macy  * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
312eda14cbcSMatt Macy  */
313eda14cbcSMatt Macy 
314eda14cbcSMatt Macy static int
315eda14cbcSMatt Macy zfs_add_option(zfs_handle_t *zhp, char *options, int len,
316eda14cbcSMatt Macy     zfs_prop_t prop, char *on, char *off)
317eda14cbcSMatt Macy {
318eda14cbcSMatt Macy 	char *source;
319eda14cbcSMatt Macy 	uint64_t value;
320eda14cbcSMatt Macy 
321eda14cbcSMatt Macy 	/* Skip adding duplicate default options */
322eda14cbcSMatt Macy 	if ((strstr(options, on) != NULL) || (strstr(options, off) != NULL))
323eda14cbcSMatt Macy 		return (0);
324eda14cbcSMatt Macy 
325eda14cbcSMatt Macy 	/*
326eda14cbcSMatt Macy 	 * zfs_prop_get_int() is not used to ensure our mount options
327eda14cbcSMatt Macy 	 * are not influenced by the current /proc/self/mounts contents.
328eda14cbcSMatt Macy 	 */
329eda14cbcSMatt Macy 	value = getprop_uint64(zhp, prop, &source);
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy 	(void) strlcat(options, ",", len);
332eda14cbcSMatt Macy 	(void) strlcat(options, value ? on : off, len);
333eda14cbcSMatt Macy 
334eda14cbcSMatt Macy 	return (0);
335eda14cbcSMatt Macy }
336eda14cbcSMatt Macy 
337eda14cbcSMatt Macy static int
338eda14cbcSMatt Macy zfs_add_options(zfs_handle_t *zhp, char *options, int len)
339eda14cbcSMatt Macy {
340eda14cbcSMatt Macy 	int error = 0;
341eda14cbcSMatt Macy 
342eda14cbcSMatt Macy 	error = zfs_add_option(zhp, options, len,
343eda14cbcSMatt Macy 	    ZFS_PROP_ATIME, MNTOPT_ATIME, MNTOPT_NOATIME);
344eda14cbcSMatt Macy 	/*
345eda14cbcSMatt Macy 	 * don't add relatime/strictatime when atime=off, otherwise strictatime
346eda14cbcSMatt Macy 	 * will force atime=on
347eda14cbcSMatt Macy 	 */
348eda14cbcSMatt Macy 	if (strstr(options, MNTOPT_NOATIME) == NULL) {
349eda14cbcSMatt Macy 		error = zfs_add_option(zhp, options, len,
350eda14cbcSMatt Macy 		    ZFS_PROP_RELATIME, MNTOPT_RELATIME, MNTOPT_STRICTATIME);
351eda14cbcSMatt Macy 	}
352eda14cbcSMatt Macy 	error = error ? error : zfs_add_option(zhp, options, len,
353eda14cbcSMatt Macy 	    ZFS_PROP_DEVICES, MNTOPT_DEVICES, MNTOPT_NODEVICES);
354eda14cbcSMatt Macy 	error = error ? error : zfs_add_option(zhp, options, len,
355eda14cbcSMatt Macy 	    ZFS_PROP_EXEC, MNTOPT_EXEC, MNTOPT_NOEXEC);
356eda14cbcSMatt Macy 	error = error ? error : zfs_add_option(zhp, options, len,
357eda14cbcSMatt Macy 	    ZFS_PROP_READONLY, MNTOPT_RO, MNTOPT_RW);
358eda14cbcSMatt Macy 	error = error ? error : zfs_add_option(zhp, options, len,
359eda14cbcSMatt Macy 	    ZFS_PROP_SETUID, MNTOPT_SETUID, MNTOPT_NOSETUID);
360eda14cbcSMatt Macy 	error = error ? error : zfs_add_option(zhp, options, len,
361eda14cbcSMatt Macy 	    ZFS_PROP_NBMAND, MNTOPT_NBMAND, MNTOPT_NONBMAND);
362eda14cbcSMatt Macy 
363eda14cbcSMatt Macy 	return (error);
364eda14cbcSMatt Macy }
365eda14cbcSMatt Macy 
366eda14cbcSMatt Macy int
367eda14cbcSMatt Macy zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
368eda14cbcSMatt Macy {
369eda14cbcSMatt Macy 	char mountpoint[ZFS_MAXPROPLEN];
370eda14cbcSMatt Macy 
371eda14cbcSMatt Macy 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL,
372eda14cbcSMatt Macy 	    flags))
373eda14cbcSMatt Macy 		return (0);
374eda14cbcSMatt Macy 
375eda14cbcSMatt Macy 	return (zfs_mount_at(zhp, options, flags, mountpoint));
376eda14cbcSMatt Macy }
377eda14cbcSMatt Macy 
378eda14cbcSMatt Macy /*
379eda14cbcSMatt Macy  * Mount the given filesystem.
380eda14cbcSMatt Macy  */
381eda14cbcSMatt Macy int
382eda14cbcSMatt Macy zfs_mount_at(zfs_handle_t *zhp, const char *options, int flags,
383eda14cbcSMatt Macy     const char *mountpoint)
384eda14cbcSMatt Macy {
385eda14cbcSMatt Macy 	struct stat buf;
386eda14cbcSMatt Macy 	char mntopts[MNT_LINE_MAX];
387eda14cbcSMatt Macy 	char overlay[ZFS_MAXPROPLEN];
38816038816SMartin Matuska 	char prop_encroot[MAXNAMELEN];
38916038816SMartin Matuska 	boolean_t is_encroot;
39016038816SMartin Matuska 	zfs_handle_t *encroot_hp = zhp;
391eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
392eda14cbcSMatt Macy 	uint64_t keystatus;
393eda14cbcSMatt Macy 	int remount = 0, rc;
394eda14cbcSMatt Macy 
395eda14cbcSMatt Macy 	if (options == NULL) {
396eda14cbcSMatt Macy 		(void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts));
397eda14cbcSMatt Macy 	} else {
398eda14cbcSMatt Macy 		(void) strlcpy(mntopts, options, sizeof (mntopts));
399eda14cbcSMatt Macy 	}
400eda14cbcSMatt Macy 
401eda14cbcSMatt Macy 	if (strstr(mntopts, MNTOPT_REMOUNT) != NULL)
402eda14cbcSMatt Macy 		remount = 1;
403eda14cbcSMatt Macy 
404eda14cbcSMatt Macy 	/* Potentially duplicates some checks if invoked by zfs_mount(). */
405eda14cbcSMatt Macy 	if (!zfs_is_mountable_internal(zhp, mountpoint))
406eda14cbcSMatt Macy 		return (0);
407eda14cbcSMatt Macy 
408eda14cbcSMatt Macy 	/*
409eda14cbcSMatt Macy 	 * If the pool is imported read-only then all mounts must be read-only
410eda14cbcSMatt Macy 	 */
411eda14cbcSMatt Macy 	if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
412eda14cbcSMatt Macy 		(void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts));
413eda14cbcSMatt Macy 
414eda14cbcSMatt Macy 	/*
415eda14cbcSMatt Macy 	 * Append default mount options which apply to the mount point.
416eda14cbcSMatt Macy 	 * This is done because under Linux (unlike Solaris) multiple mount
417eda14cbcSMatt Macy 	 * points may reference a single super block.  This means that just
418eda14cbcSMatt Macy 	 * given a super block there is no back reference to update the per
419eda14cbcSMatt Macy 	 * mount point options.
420eda14cbcSMatt Macy 	 */
421eda14cbcSMatt Macy 	rc = zfs_add_options(zhp, mntopts, sizeof (mntopts));
422eda14cbcSMatt Macy 	if (rc) {
423eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
424eda14cbcSMatt Macy 		    "default options unavailable"));
425eda14cbcSMatt Macy 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
426eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
427eda14cbcSMatt Macy 		    mountpoint));
428eda14cbcSMatt Macy 	}
429eda14cbcSMatt Macy 
430eda14cbcSMatt Macy 	/*
431eda14cbcSMatt Macy 	 * If the filesystem is encrypted the key must be loaded  in order to
432eda14cbcSMatt Macy 	 * mount. If the key isn't loaded, the MS_CRYPT flag decides whether
433eda14cbcSMatt Macy 	 * or not we attempt to load the keys. Note: we must call
434eda14cbcSMatt Macy 	 * zfs_refresh_properties() here since some callers of this function
435eda14cbcSMatt Macy 	 * (most notably zpool_enable_datasets()) may implicitly load our key
436eda14cbcSMatt Macy 	 * by loading the parent's key first.
437eda14cbcSMatt Macy 	 */
438eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
439eda14cbcSMatt Macy 		zfs_refresh_properties(zhp);
440eda14cbcSMatt Macy 		keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
441eda14cbcSMatt Macy 
442eda14cbcSMatt Macy 		/*
443eda14cbcSMatt Macy 		 * If the key is unavailable and MS_CRYPT is set give the
444eda14cbcSMatt Macy 		 * user a chance to enter the key. Otherwise just fail
445eda14cbcSMatt Macy 		 * immediately.
446eda14cbcSMatt Macy 		 */
447eda14cbcSMatt Macy 		if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
448eda14cbcSMatt Macy 			if (flags & MS_CRYPT) {
44916038816SMartin Matuska 				rc = zfs_crypto_get_encryption_root(zhp,
45016038816SMartin Matuska 				    &is_encroot, prop_encroot);
45116038816SMartin Matuska 				if (rc) {
45216038816SMartin Matuska 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
45316038816SMartin Matuska 					    "Failed to get encryption root for "
45416038816SMartin Matuska 					    "'%s'."), zfs_get_name(zhp));
45516038816SMartin Matuska 					return (rc);
45616038816SMartin Matuska 				}
45716038816SMartin Matuska 
45816038816SMartin Matuska 				if (!is_encroot) {
45916038816SMartin Matuska 					encroot_hp = zfs_open(hdl, prop_encroot,
46016038816SMartin Matuska 					    ZFS_TYPE_DATASET);
46116038816SMartin Matuska 					if (encroot_hp == NULL)
46216038816SMartin Matuska 						return (hdl->libzfs_error);
46316038816SMartin Matuska 				}
46416038816SMartin Matuska 
46516038816SMartin Matuska 				rc = zfs_crypto_load_key(encroot_hp,
46616038816SMartin Matuska 				    B_FALSE, NULL);
46716038816SMartin Matuska 
46816038816SMartin Matuska 				if (!is_encroot)
46916038816SMartin Matuska 					zfs_close(encroot_hp);
470eda14cbcSMatt Macy 				if (rc)
471eda14cbcSMatt Macy 					return (rc);
472eda14cbcSMatt Macy 			} else {
473eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
474eda14cbcSMatt Macy 				    "encryption key not loaded"));
475eda14cbcSMatt Macy 				return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
476eda14cbcSMatt Macy 				    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
477eda14cbcSMatt Macy 				    mountpoint));
478eda14cbcSMatt Macy 			}
479eda14cbcSMatt Macy 		}
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy 	}
482eda14cbcSMatt Macy 
483eda14cbcSMatt Macy 	/*
484eda14cbcSMatt Macy 	 * Append zfsutil option so the mount helper allow the mount
485eda14cbcSMatt Macy 	 */
486eda14cbcSMatt Macy 	strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts));
487eda14cbcSMatt Macy 
488eda14cbcSMatt Macy 	/* Create the directory if it doesn't already exist */
489eda14cbcSMatt Macy 	if (lstat(mountpoint, &buf) != 0) {
490eda14cbcSMatt Macy 		if (mkdirp(mountpoint, 0755) != 0) {
491eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
492eda14cbcSMatt Macy 			    "failed to create mountpoint: %s"),
493eda14cbcSMatt Macy 			    strerror(errno));
494eda14cbcSMatt Macy 			return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
495eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
496eda14cbcSMatt Macy 			    mountpoint));
497eda14cbcSMatt Macy 		}
498eda14cbcSMatt Macy 	}
499eda14cbcSMatt Macy 
500eda14cbcSMatt Macy 	/*
501eda14cbcSMatt Macy 	 * Overlay mounts are enabled by default but may be disabled
502eda14cbcSMatt Macy 	 * via the 'overlay' property. The -O flag remains for compatibility.
503eda14cbcSMatt Macy 	 */
504eda14cbcSMatt Macy 	if (!(flags & MS_OVERLAY)) {
505eda14cbcSMatt Macy 		if (zfs_prop_get(zhp, ZFS_PROP_OVERLAY, overlay,
506eda14cbcSMatt Macy 		    sizeof (overlay), NULL, NULL, 0, B_FALSE) == 0) {
507eda14cbcSMatt Macy 			if (strcmp(overlay, "on") == 0) {
508eda14cbcSMatt Macy 				flags |= MS_OVERLAY;
509eda14cbcSMatt Macy 			}
510eda14cbcSMatt Macy 		}
511eda14cbcSMatt Macy 	}
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy 	/*
514eda14cbcSMatt Macy 	 * Determine if the mountpoint is empty.  If so, refuse to perform the
515eda14cbcSMatt Macy 	 * mount.  We don't perform this check if 'remount' is
516eda14cbcSMatt Macy 	 * specified or if overlay option (-O) is given
517eda14cbcSMatt Macy 	 */
518eda14cbcSMatt Macy 	if ((flags & MS_OVERLAY) == 0 && !remount &&
519eda14cbcSMatt Macy 	    !dir_is_empty(mountpoint)) {
520eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
521eda14cbcSMatt Macy 		    "directory is not empty"));
522eda14cbcSMatt Macy 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
523eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
524eda14cbcSMatt Macy 	}
525eda14cbcSMatt Macy 
526eda14cbcSMatt Macy 	/* perform the mount */
527eda14cbcSMatt Macy 	rc = do_mount(zhp, mountpoint, mntopts, flags);
528eda14cbcSMatt Macy 	if (rc) {
529eda14cbcSMatt Macy 		/*
530eda14cbcSMatt Macy 		 * Generic errors are nasty, but there are just way too many
531eda14cbcSMatt Macy 		 * from mount(), and they're well-understood.  We pick a few
532eda14cbcSMatt Macy 		 * common ones to improve upon.
533eda14cbcSMatt Macy 		 */
534eda14cbcSMatt Macy 		if (rc == EBUSY) {
535eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
536eda14cbcSMatt Macy 			    "mountpoint or dataset is busy"));
537eda14cbcSMatt Macy 		} else if (rc == EPERM) {
538eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
539eda14cbcSMatt Macy 			    "Insufficient privileges"));
540eda14cbcSMatt Macy 		} else if (rc == ENOTSUP) {
541eda14cbcSMatt Macy 			int spa_version;
542eda14cbcSMatt Macy 
543eda14cbcSMatt Macy 			VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
54416038816SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
54516038816SMartin Matuska 			    "Can't mount a version %llu "
546eda14cbcSMatt Macy 			    "file system on a version %d pool. Pool must be"
547eda14cbcSMatt Macy 			    " upgraded to mount this file system."),
548eda14cbcSMatt Macy 			    (u_longlong_t)zfs_prop_get_int(zhp,
549eda14cbcSMatt Macy 			    ZFS_PROP_VERSION), spa_version);
550eda14cbcSMatt Macy 		} else {
55116038816SMartin Matuska 			zfs_error_aux(hdl, "%s", strerror(rc));
552eda14cbcSMatt Macy 		}
553eda14cbcSMatt Macy 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
554eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
555eda14cbcSMatt Macy 		    zhp->zfs_name));
556eda14cbcSMatt Macy 	}
557eda14cbcSMatt Macy 
558eda14cbcSMatt Macy 	/* remove the mounted entry before re-adding on remount */
559eda14cbcSMatt Macy 	if (remount)
560eda14cbcSMatt Macy 		libzfs_mnttab_remove(hdl, zhp->zfs_name);
561eda14cbcSMatt Macy 
562eda14cbcSMatt Macy 	/* add the mounted entry into our cache */
563eda14cbcSMatt Macy 	libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts);
564eda14cbcSMatt Macy 	return (0);
565eda14cbcSMatt Macy }
566eda14cbcSMatt Macy 
567eda14cbcSMatt Macy /*
568eda14cbcSMatt Macy  * Unmount a single filesystem.
569eda14cbcSMatt Macy  */
570eda14cbcSMatt Macy static int
5713f9d360cSMartin Matuska unmount_one(zfs_handle_t *zhp, const char *mountpoint, int flags)
572eda14cbcSMatt Macy {
573eda14cbcSMatt Macy 	int error;
574eda14cbcSMatt Macy 
5753f9d360cSMartin Matuska 	error = do_unmount(zhp, mountpoint, flags);
576eda14cbcSMatt Macy 	if (error != 0) {
5779db44a8eSMartin Matuska 		int libzfs_err;
5789db44a8eSMartin Matuska 
5799db44a8eSMartin Matuska 		switch (error) {
5809db44a8eSMartin Matuska 		case EBUSY:
5819db44a8eSMartin Matuska 			libzfs_err = EZFS_BUSY;
5829db44a8eSMartin Matuska 			break;
5839db44a8eSMartin Matuska 		case EIO:
5849db44a8eSMartin Matuska 			libzfs_err = EZFS_IO;
5859db44a8eSMartin Matuska 			break;
5869db44a8eSMartin Matuska 		case ENOENT:
5879db44a8eSMartin Matuska 			libzfs_err = EZFS_NOENT;
5889db44a8eSMartin Matuska 			break;
5899db44a8eSMartin Matuska 		case ENOMEM:
5909db44a8eSMartin Matuska 			libzfs_err = EZFS_NOMEM;
5919db44a8eSMartin Matuska 			break;
5929db44a8eSMartin Matuska 		case EPERM:
5939db44a8eSMartin Matuska 			libzfs_err = EZFS_PERM;
5949db44a8eSMartin Matuska 			break;
5959db44a8eSMartin Matuska 		default:
5969db44a8eSMartin Matuska 			libzfs_err = EZFS_UMOUNTFAILED;
5979db44a8eSMartin Matuska 		}
598*681ce946SMartin Matuska 		if (zhp) {
5993f9d360cSMartin Matuska 			return (zfs_error_fmt(zhp->zfs_hdl, libzfs_err,
600eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
601eda14cbcSMatt Macy 			    mountpoint));
602*681ce946SMartin Matuska 		} else {
603*681ce946SMartin Matuska 			return (-1);
604*681ce946SMartin Matuska 		}
605eda14cbcSMatt Macy 	}
606eda14cbcSMatt Macy 
607eda14cbcSMatt Macy 	return (0);
608eda14cbcSMatt Macy }
609eda14cbcSMatt Macy 
610eda14cbcSMatt Macy /*
611eda14cbcSMatt Macy  * Unmount the given filesystem.
612eda14cbcSMatt Macy  */
613eda14cbcSMatt Macy int
614eda14cbcSMatt Macy zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
615eda14cbcSMatt Macy {
616eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
617eda14cbcSMatt Macy 	struct mnttab entry;
618eda14cbcSMatt Macy 	char *mntpt = NULL;
619eda14cbcSMatt Macy 	boolean_t encroot, unmounted = B_FALSE;
620eda14cbcSMatt Macy 
621eda14cbcSMatt Macy 	/* check to see if we need to unmount the filesystem */
622eda14cbcSMatt Macy 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
623eda14cbcSMatt Macy 	    libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
624eda14cbcSMatt Macy 		/*
625eda14cbcSMatt Macy 		 * mountpoint may have come from a call to
626eda14cbcSMatt Macy 		 * getmnt/getmntany if it isn't NULL. If it is NULL,
627eda14cbcSMatt Macy 		 * we know it comes from libzfs_mnttab_find which can
628eda14cbcSMatt Macy 		 * then get freed later. We strdup it to play it safe.
629eda14cbcSMatt Macy 		 */
630eda14cbcSMatt Macy 		if (mountpoint == NULL)
631eda14cbcSMatt Macy 			mntpt = zfs_strdup(hdl, entry.mnt_mountp);
632eda14cbcSMatt Macy 		else
633eda14cbcSMatt Macy 			mntpt = zfs_strdup(hdl, mountpoint);
634eda14cbcSMatt Macy 
635eda14cbcSMatt Macy 		/*
636eda14cbcSMatt Macy 		 * Unshare and unmount the filesystem
637eda14cbcSMatt Macy 		 */
638eda14cbcSMatt Macy 		if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) {
639eda14cbcSMatt Macy 			free(mntpt);
640eda14cbcSMatt Macy 			return (-1);
641eda14cbcSMatt Macy 		}
642eda14cbcSMatt Macy 		zfs_commit_all_shares();
643eda14cbcSMatt Macy 
6443f9d360cSMartin Matuska 		if (unmount_one(zhp, mntpt, flags) != 0) {
645eda14cbcSMatt Macy 			free(mntpt);
646eda14cbcSMatt Macy 			(void) zfs_shareall(zhp);
647eda14cbcSMatt Macy 			zfs_commit_all_shares();
648eda14cbcSMatt Macy 			return (-1);
649eda14cbcSMatt Macy 		}
650eda14cbcSMatt Macy 
651eda14cbcSMatt Macy 		libzfs_mnttab_remove(hdl, zhp->zfs_name);
652eda14cbcSMatt Macy 		free(mntpt);
653eda14cbcSMatt Macy 		unmounted = B_TRUE;
654eda14cbcSMatt Macy 	}
655eda14cbcSMatt Macy 
656eda14cbcSMatt Macy 	/*
657eda14cbcSMatt Macy 	 * If the MS_CRYPT flag is provided we must ensure we attempt to
658eda14cbcSMatt Macy 	 * unload the dataset's key regardless of whether we did any work
659eda14cbcSMatt Macy 	 * to unmount it. We only do this for encryption roots.
660eda14cbcSMatt Macy 	 */
661eda14cbcSMatt Macy 	if ((flags & MS_CRYPT) != 0 &&
662eda14cbcSMatt Macy 	    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
663eda14cbcSMatt Macy 		zfs_refresh_properties(zhp);
664eda14cbcSMatt Macy 
665eda14cbcSMatt Macy 		if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0 &&
666eda14cbcSMatt Macy 		    unmounted) {
667eda14cbcSMatt Macy 			(void) zfs_mount(zhp, NULL, 0);
668eda14cbcSMatt Macy 			return (-1);
669eda14cbcSMatt Macy 		}
670eda14cbcSMatt Macy 
671eda14cbcSMatt Macy 		if (encroot && zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
672eda14cbcSMatt Macy 		    ZFS_KEYSTATUS_AVAILABLE &&
673eda14cbcSMatt Macy 		    zfs_crypto_unload_key(zhp) != 0) {
674eda14cbcSMatt Macy 			(void) zfs_mount(zhp, NULL, 0);
675eda14cbcSMatt Macy 			return (-1);
676eda14cbcSMatt Macy 		}
677eda14cbcSMatt Macy 	}
678eda14cbcSMatt Macy 
67953b70c86SMartin Matuska 	zpool_disable_volume_os(zhp->zfs_name);
68053b70c86SMartin Matuska 
681eda14cbcSMatt Macy 	return (0);
682eda14cbcSMatt Macy }
683eda14cbcSMatt Macy 
684eda14cbcSMatt Macy /*
685eda14cbcSMatt Macy  * Unmount this filesystem and any children inheriting the mountpoint property.
686eda14cbcSMatt Macy  * To do this, just act like we're changing the mountpoint property, but don't
687eda14cbcSMatt Macy  * remount the filesystems afterwards.
688eda14cbcSMatt Macy  */
689eda14cbcSMatt Macy int
690eda14cbcSMatt Macy zfs_unmountall(zfs_handle_t *zhp, int flags)
691eda14cbcSMatt Macy {
692eda14cbcSMatt Macy 	prop_changelist_t *clp;
693eda14cbcSMatt Macy 	int ret;
694eda14cbcSMatt Macy 
695eda14cbcSMatt Macy 	clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
696eda14cbcSMatt Macy 	    CL_GATHER_ITER_MOUNTED, flags);
697eda14cbcSMatt Macy 	if (clp == NULL)
698eda14cbcSMatt Macy 		return (-1);
699eda14cbcSMatt Macy 
700eda14cbcSMatt Macy 	ret = changelist_prefix(clp);
701eda14cbcSMatt Macy 	changelist_free(clp);
702eda14cbcSMatt Macy 
703eda14cbcSMatt Macy 	return (ret);
704eda14cbcSMatt Macy }
705eda14cbcSMatt Macy 
706eda14cbcSMatt Macy boolean_t
707eda14cbcSMatt Macy zfs_is_shared(zfs_handle_t *zhp)
708eda14cbcSMatt Macy {
709eda14cbcSMatt Macy 	zfs_share_type_t rc = 0;
710eda14cbcSMatt Macy 	zfs_share_proto_t *curr_proto;
711eda14cbcSMatt Macy 
712eda14cbcSMatt Macy 	if (ZFS_IS_VOLUME(zhp))
713eda14cbcSMatt Macy 		return (B_FALSE);
714eda14cbcSMatt Macy 
715eda14cbcSMatt Macy 	for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
716eda14cbcSMatt Macy 	    curr_proto++)
717eda14cbcSMatt Macy 		rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
718eda14cbcSMatt Macy 
719eda14cbcSMatt Macy 	return (rc ? B_TRUE : B_FALSE);
720eda14cbcSMatt Macy }
721eda14cbcSMatt Macy 
722eda14cbcSMatt Macy /*
723eda14cbcSMatt Macy  * Unshare a filesystem by mountpoint.
724eda14cbcSMatt Macy  */
725eda14cbcSMatt Macy int
726eda14cbcSMatt Macy unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
727eda14cbcSMatt Macy     zfs_share_proto_t proto)
728eda14cbcSMatt Macy {
729eda14cbcSMatt Macy 	int err;
730eda14cbcSMatt Macy 
731eda14cbcSMatt Macy 	err = sa_disable_share(mountpoint, proto_table[proto].p_name);
732eda14cbcSMatt Macy 	if (err != SA_OK) {
733eda14cbcSMatt Macy 		return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
734eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
735eda14cbcSMatt Macy 		    name, sa_errorstr(err)));
736eda14cbcSMatt Macy 	}
737eda14cbcSMatt Macy 	return (0);
738eda14cbcSMatt Macy }
739eda14cbcSMatt Macy 
740eda14cbcSMatt Macy /*
741eda14cbcSMatt Macy  * Query libshare for the given mountpoint and protocol, returning
742eda14cbcSMatt Macy  * a zfs_share_type_t value.
743eda14cbcSMatt Macy  */
744eda14cbcSMatt Macy zfs_share_type_t
745eda14cbcSMatt Macy is_shared(const char *mountpoint, zfs_share_proto_t proto)
746eda14cbcSMatt Macy {
747eda14cbcSMatt Macy 	if (sa_is_shared(mountpoint, proto_table[proto].p_name)) {
748eda14cbcSMatt Macy 		switch (proto) {
749eda14cbcSMatt Macy 		case PROTO_NFS:
750eda14cbcSMatt Macy 			return (SHARED_NFS);
751eda14cbcSMatt Macy 		case PROTO_SMB:
752eda14cbcSMatt Macy 			return (SHARED_SMB);
753eda14cbcSMatt Macy 		default:
754eda14cbcSMatt Macy 			return (SHARED_NOT_SHARED);
755eda14cbcSMatt Macy 		}
756eda14cbcSMatt Macy 	}
757eda14cbcSMatt Macy 	return (SHARED_NOT_SHARED);
758eda14cbcSMatt Macy }
759eda14cbcSMatt Macy 
760eda14cbcSMatt Macy /*
761eda14cbcSMatt Macy  * Share the given filesystem according to the options in the specified
762eda14cbcSMatt Macy  * protocol specific properties (sharenfs, sharesmb).  We rely
763eda14cbcSMatt Macy  * on "libshare" to do the dirty work for us.
764eda14cbcSMatt Macy  */
765eda14cbcSMatt Macy int
766eda14cbcSMatt Macy zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
767eda14cbcSMatt Macy {
768eda14cbcSMatt Macy 	char mountpoint[ZFS_MAXPROPLEN];
769eda14cbcSMatt Macy 	char shareopts[ZFS_MAXPROPLEN];
770eda14cbcSMatt Macy 	char sourcestr[ZFS_MAXPROPLEN];
771eda14cbcSMatt Macy 	zfs_share_proto_t *curr_proto;
772eda14cbcSMatt Macy 	zprop_source_t sourcetype;
773eda14cbcSMatt Macy 	int err = 0;
774eda14cbcSMatt Macy 
775eda14cbcSMatt Macy 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0))
776eda14cbcSMatt Macy 		return (0);
777eda14cbcSMatt Macy 
778eda14cbcSMatt Macy 	for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
779eda14cbcSMatt Macy 		/*
780eda14cbcSMatt Macy 		 * Return success if there are no share options.
781eda14cbcSMatt Macy 		 */
782eda14cbcSMatt Macy 		if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
783eda14cbcSMatt Macy 		    shareopts, sizeof (shareopts), &sourcetype, sourcestr,
784eda14cbcSMatt Macy 		    ZFS_MAXPROPLEN, B_FALSE) != 0 ||
785eda14cbcSMatt Macy 		    strcmp(shareopts, "off") == 0)
786eda14cbcSMatt Macy 			continue;
787eda14cbcSMatt Macy 
788eda14cbcSMatt Macy 		/*
789eda14cbcSMatt Macy 		 * If the 'zoned' property is set, then zfs_is_mountable()
790eda14cbcSMatt Macy 		 * will have already bailed out if we are in the global zone.
791eda14cbcSMatt Macy 		 * But local zones cannot be NFS servers, so we ignore it for
792eda14cbcSMatt Macy 		 * local zones as well.
793eda14cbcSMatt Macy 		 */
794eda14cbcSMatt Macy 		if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
795eda14cbcSMatt Macy 			continue;
796eda14cbcSMatt Macy 
797eda14cbcSMatt Macy 		err = sa_enable_share(zfs_get_name(zhp), mountpoint, shareopts,
798eda14cbcSMatt Macy 		    proto_table[*curr_proto].p_name);
799eda14cbcSMatt Macy 		if (err != SA_OK) {
800eda14cbcSMatt Macy 			return (zfs_error_fmt(zhp->zfs_hdl,
801eda14cbcSMatt Macy 			    proto_table[*curr_proto].p_share_err,
802eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot share '%s: %s'"),
803eda14cbcSMatt Macy 			    zfs_get_name(zhp), sa_errorstr(err)));
804eda14cbcSMatt Macy 		}
805eda14cbcSMatt Macy 
806eda14cbcSMatt Macy 	}
807eda14cbcSMatt Macy 	return (0);
808eda14cbcSMatt Macy }
809eda14cbcSMatt Macy 
810eda14cbcSMatt Macy int
811eda14cbcSMatt Macy zfs_share(zfs_handle_t *zhp)
812eda14cbcSMatt Macy {
813eda14cbcSMatt Macy 	assert(!ZFS_IS_VOLUME(zhp));
814eda14cbcSMatt Macy 	return (zfs_share_proto(zhp, share_all_proto));
815eda14cbcSMatt Macy }
816eda14cbcSMatt Macy 
817eda14cbcSMatt Macy int
818eda14cbcSMatt Macy zfs_unshare(zfs_handle_t *zhp)
819eda14cbcSMatt Macy {
820eda14cbcSMatt Macy 	assert(!ZFS_IS_VOLUME(zhp));
821eda14cbcSMatt Macy 	return (zfs_unshareall(zhp));
822eda14cbcSMatt Macy }
823eda14cbcSMatt Macy 
824eda14cbcSMatt Macy /*
825eda14cbcSMatt Macy  * Check to see if the filesystem is currently shared.
826eda14cbcSMatt Macy  */
82716038816SMartin Matuska static zfs_share_type_t
828eda14cbcSMatt Macy zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
829eda14cbcSMatt Macy {
830eda14cbcSMatt Macy 	char *mountpoint;
831eda14cbcSMatt Macy 	zfs_share_type_t rc;
832eda14cbcSMatt Macy 
833eda14cbcSMatt Macy 	if (!zfs_is_mounted(zhp, &mountpoint))
834eda14cbcSMatt Macy 		return (SHARED_NOT_SHARED);
835eda14cbcSMatt Macy 
836eda14cbcSMatt Macy 	if ((rc = is_shared(mountpoint, proto))
837eda14cbcSMatt Macy 	    != SHARED_NOT_SHARED) {
838eda14cbcSMatt Macy 		if (where != NULL)
839eda14cbcSMatt Macy 			*where = mountpoint;
840eda14cbcSMatt Macy 		else
841eda14cbcSMatt Macy 			free(mountpoint);
842eda14cbcSMatt Macy 		return (rc);
843eda14cbcSMatt Macy 	} else {
844eda14cbcSMatt Macy 		free(mountpoint);
845eda14cbcSMatt Macy 		return (SHARED_NOT_SHARED);
846eda14cbcSMatt Macy 	}
847eda14cbcSMatt Macy }
848eda14cbcSMatt Macy 
849eda14cbcSMatt Macy boolean_t
850eda14cbcSMatt Macy zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
851eda14cbcSMatt Macy {
852eda14cbcSMatt Macy 	return (zfs_is_shared_proto(zhp, where,
853eda14cbcSMatt Macy 	    PROTO_NFS) != SHARED_NOT_SHARED);
854eda14cbcSMatt Macy }
855eda14cbcSMatt Macy 
856eda14cbcSMatt Macy boolean_t
857eda14cbcSMatt Macy zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
858eda14cbcSMatt Macy {
859eda14cbcSMatt Macy 	return (zfs_is_shared_proto(zhp, where,
860eda14cbcSMatt Macy 	    PROTO_SMB) != SHARED_NOT_SHARED);
861eda14cbcSMatt Macy }
862eda14cbcSMatt Macy 
863eda14cbcSMatt Macy /*
864eda14cbcSMatt Macy  * zfs_parse_options(options, proto)
865eda14cbcSMatt Macy  *
866eda14cbcSMatt Macy  * Call the legacy parse interface to get the protocol specific
867eda14cbcSMatt Macy  * options using the NULL arg to indicate that this is a "parse" only.
868eda14cbcSMatt Macy  */
869eda14cbcSMatt Macy int
870eda14cbcSMatt Macy zfs_parse_options(char *options, zfs_share_proto_t proto)
871eda14cbcSMatt Macy {
872eda14cbcSMatt Macy 	return (sa_validate_shareopts(options, proto_table[proto].p_name));
873eda14cbcSMatt Macy }
874eda14cbcSMatt Macy 
875eda14cbcSMatt Macy void
876eda14cbcSMatt Macy zfs_commit_proto(zfs_share_proto_t *proto)
877eda14cbcSMatt Macy {
878eda14cbcSMatt Macy 	zfs_share_proto_t *curr_proto;
879eda14cbcSMatt Macy 	for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
880eda14cbcSMatt Macy 		sa_commit_shares(proto_table[*curr_proto].p_name);
881eda14cbcSMatt Macy 	}
882eda14cbcSMatt Macy }
883eda14cbcSMatt Macy 
884eda14cbcSMatt Macy void
885eda14cbcSMatt Macy zfs_commit_nfs_shares(void)
886eda14cbcSMatt Macy {
887eda14cbcSMatt Macy 	zfs_commit_proto(nfs_only);
888eda14cbcSMatt Macy }
889eda14cbcSMatt Macy 
890eda14cbcSMatt Macy void
891eda14cbcSMatt Macy zfs_commit_smb_shares(void)
892eda14cbcSMatt Macy {
893eda14cbcSMatt Macy 	zfs_commit_proto(smb_only);
894eda14cbcSMatt Macy }
895eda14cbcSMatt Macy 
896eda14cbcSMatt Macy void
897eda14cbcSMatt Macy zfs_commit_all_shares(void)
898eda14cbcSMatt Macy {
899eda14cbcSMatt Macy 	zfs_commit_proto(share_all_proto);
900eda14cbcSMatt Macy }
901eda14cbcSMatt Macy 
902eda14cbcSMatt Macy void
903eda14cbcSMatt Macy zfs_commit_shares(const char *proto)
904eda14cbcSMatt Macy {
905eda14cbcSMatt Macy 	if (proto == NULL)
906eda14cbcSMatt Macy 		zfs_commit_proto(share_all_proto);
907eda14cbcSMatt Macy 	else if (strcmp(proto, "nfs") == 0)
908eda14cbcSMatt Macy 		zfs_commit_proto(nfs_only);
909eda14cbcSMatt Macy 	else if (strcmp(proto, "smb") == 0)
910eda14cbcSMatt Macy 		zfs_commit_proto(smb_only);
911eda14cbcSMatt Macy }
912eda14cbcSMatt Macy 
913eda14cbcSMatt Macy int
914eda14cbcSMatt Macy zfs_share_nfs(zfs_handle_t *zhp)
915eda14cbcSMatt Macy {
916eda14cbcSMatt Macy 	return (zfs_share_proto(zhp, nfs_only));
917eda14cbcSMatt Macy }
918eda14cbcSMatt Macy 
919eda14cbcSMatt Macy int
920eda14cbcSMatt Macy zfs_share_smb(zfs_handle_t *zhp)
921eda14cbcSMatt Macy {
922eda14cbcSMatt Macy 	return (zfs_share_proto(zhp, smb_only));
923eda14cbcSMatt Macy }
924eda14cbcSMatt Macy 
925eda14cbcSMatt Macy int
926eda14cbcSMatt Macy zfs_shareall(zfs_handle_t *zhp)
927eda14cbcSMatt Macy {
928eda14cbcSMatt Macy 	return (zfs_share_proto(zhp, share_all_proto));
929eda14cbcSMatt Macy }
930eda14cbcSMatt Macy 
931eda14cbcSMatt Macy /*
932eda14cbcSMatt Macy  * Unshare the given filesystem.
933eda14cbcSMatt Macy  */
934eda14cbcSMatt Macy int
935eda14cbcSMatt Macy zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
936eda14cbcSMatt Macy     zfs_share_proto_t *proto)
937eda14cbcSMatt Macy {
938eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
939eda14cbcSMatt Macy 	struct mnttab entry;
940eda14cbcSMatt Macy 	char *mntpt = NULL;
941eda14cbcSMatt Macy 
942eda14cbcSMatt Macy 	/* check to see if need to unmount the filesystem */
943eda14cbcSMatt Macy 	if (mountpoint != NULL)
944eda14cbcSMatt Macy 		mntpt = zfs_strdup(hdl, mountpoint);
945eda14cbcSMatt Macy 
946eda14cbcSMatt Macy 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
947eda14cbcSMatt Macy 	    libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
948eda14cbcSMatt Macy 		zfs_share_proto_t *curr_proto;
949eda14cbcSMatt Macy 
950eda14cbcSMatt Macy 		if (mountpoint == NULL)
951eda14cbcSMatt Macy 			mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
952eda14cbcSMatt Macy 
953eda14cbcSMatt Macy 		for (curr_proto = proto; *curr_proto != PROTO_END;
954eda14cbcSMatt Macy 		    curr_proto++) {
955eda14cbcSMatt Macy 
956eda14cbcSMatt Macy 			if (is_shared(mntpt, *curr_proto)) {
957eda14cbcSMatt Macy 				if (unshare_one(hdl, zhp->zfs_name,
958eda14cbcSMatt Macy 				    mntpt, *curr_proto) != 0) {
959eda14cbcSMatt Macy 					if (mntpt != NULL)
960eda14cbcSMatt Macy 						free(mntpt);
961eda14cbcSMatt Macy 					return (-1);
962eda14cbcSMatt Macy 				}
963eda14cbcSMatt Macy 			}
964eda14cbcSMatt Macy 		}
965eda14cbcSMatt Macy 	}
966eda14cbcSMatt Macy 	if (mntpt != NULL)
967eda14cbcSMatt Macy 		free(mntpt);
968eda14cbcSMatt Macy 
969eda14cbcSMatt Macy 	return (0);
970eda14cbcSMatt Macy }
971eda14cbcSMatt Macy 
972eda14cbcSMatt Macy int
973eda14cbcSMatt Macy zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
974eda14cbcSMatt Macy {
975eda14cbcSMatt Macy 	return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
976eda14cbcSMatt Macy }
977eda14cbcSMatt Macy 
978eda14cbcSMatt Macy int
979eda14cbcSMatt Macy zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
980eda14cbcSMatt Macy {
981eda14cbcSMatt Macy 	return (zfs_unshare_proto(zhp, mountpoint, smb_only));
982eda14cbcSMatt Macy }
983eda14cbcSMatt Macy 
984eda14cbcSMatt Macy /*
985eda14cbcSMatt Macy  * Same as zfs_unmountall(), but for NFS and SMB unshares.
986eda14cbcSMatt Macy  */
987eda14cbcSMatt Macy static int
988eda14cbcSMatt Macy zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
989eda14cbcSMatt Macy {
990eda14cbcSMatt Macy 	prop_changelist_t *clp;
991eda14cbcSMatt Macy 	int ret;
992eda14cbcSMatt Macy 
993eda14cbcSMatt Macy 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
994eda14cbcSMatt Macy 	if (clp == NULL)
995eda14cbcSMatt Macy 		return (-1);
996eda14cbcSMatt Macy 
997eda14cbcSMatt Macy 	ret = changelist_unshare(clp, proto);
998eda14cbcSMatt Macy 	changelist_free(clp);
999eda14cbcSMatt Macy 
1000eda14cbcSMatt Macy 	return (ret);
1001eda14cbcSMatt Macy }
1002eda14cbcSMatt Macy 
1003eda14cbcSMatt Macy int
1004eda14cbcSMatt Macy zfs_unshareall_nfs(zfs_handle_t *zhp)
1005eda14cbcSMatt Macy {
1006eda14cbcSMatt Macy 	return (zfs_unshareall_proto(zhp, nfs_only));
1007eda14cbcSMatt Macy }
1008eda14cbcSMatt Macy 
1009eda14cbcSMatt Macy int
1010eda14cbcSMatt Macy zfs_unshareall_smb(zfs_handle_t *zhp)
1011eda14cbcSMatt Macy {
1012eda14cbcSMatt Macy 	return (zfs_unshareall_proto(zhp, smb_only));
1013eda14cbcSMatt Macy }
1014eda14cbcSMatt Macy 
1015eda14cbcSMatt Macy int
1016eda14cbcSMatt Macy zfs_unshareall(zfs_handle_t *zhp)
1017eda14cbcSMatt Macy {
1018eda14cbcSMatt Macy 	return (zfs_unshareall_proto(zhp, share_all_proto));
1019eda14cbcSMatt Macy }
1020eda14cbcSMatt Macy 
1021eda14cbcSMatt Macy int
1022eda14cbcSMatt Macy zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1023eda14cbcSMatt Macy {
1024eda14cbcSMatt Macy 	return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1025eda14cbcSMatt Macy }
1026eda14cbcSMatt Macy 
1027eda14cbcSMatt Macy int
1028eda14cbcSMatt Macy zfs_unshareall_bytype(zfs_handle_t *zhp, const char *mountpoint,
1029eda14cbcSMatt Macy     const char *proto)
1030eda14cbcSMatt Macy {
1031eda14cbcSMatt Macy 	if (proto == NULL)
1032eda14cbcSMatt Macy 		return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1033eda14cbcSMatt Macy 	if (strcmp(proto, "nfs") == 0)
1034eda14cbcSMatt Macy 		return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
1035eda14cbcSMatt Macy 	else if (strcmp(proto, "smb") == 0)
1036eda14cbcSMatt Macy 		return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1037eda14cbcSMatt Macy 	else
1038eda14cbcSMatt Macy 		return (1);
1039eda14cbcSMatt Macy }
1040eda14cbcSMatt Macy 
1041eda14cbcSMatt Macy /*
1042eda14cbcSMatt Macy  * Remove the mountpoint associated with the current dataset, if necessary.
1043eda14cbcSMatt Macy  * We only remove the underlying directory if:
1044eda14cbcSMatt Macy  *
1045eda14cbcSMatt Macy  *	- The mountpoint is not 'none' or 'legacy'
1046eda14cbcSMatt Macy  *	- The mountpoint is non-empty
1047eda14cbcSMatt Macy  *	- The mountpoint is the default or inherited
1048eda14cbcSMatt Macy  *	- The 'zoned' property is set, or we're in a local zone
1049eda14cbcSMatt Macy  *
1050eda14cbcSMatt Macy  * Any other directories we leave alone.
1051eda14cbcSMatt Macy  */
1052eda14cbcSMatt Macy void
1053eda14cbcSMatt Macy remove_mountpoint(zfs_handle_t *zhp)
1054eda14cbcSMatt Macy {
1055eda14cbcSMatt Macy 	char mountpoint[ZFS_MAXPROPLEN];
1056eda14cbcSMatt Macy 	zprop_source_t source;
1057eda14cbcSMatt Macy 
1058eda14cbcSMatt Macy 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1059eda14cbcSMatt Macy 	    &source, 0))
1060eda14cbcSMatt Macy 		return;
1061eda14cbcSMatt Macy 
1062eda14cbcSMatt Macy 	if (source == ZPROP_SRC_DEFAULT ||
1063eda14cbcSMatt Macy 	    source == ZPROP_SRC_INHERITED) {
1064eda14cbcSMatt Macy 		/*
1065eda14cbcSMatt Macy 		 * Try to remove the directory, silently ignoring any errors.
1066eda14cbcSMatt Macy 		 * The filesystem may have since been removed or moved around,
1067eda14cbcSMatt Macy 		 * and this error isn't really useful to the administrator in
1068eda14cbcSMatt Macy 		 * any way.
1069eda14cbcSMatt Macy 		 */
1070eda14cbcSMatt Macy 		(void) rmdir(mountpoint);
1071eda14cbcSMatt Macy 	}
1072eda14cbcSMatt Macy }
1073eda14cbcSMatt Macy 
1074eda14cbcSMatt Macy /*
1075eda14cbcSMatt Macy  * Add the given zfs handle to the cb_handles array, dynamically reallocating
1076eda14cbcSMatt Macy  * the array if it is out of space.
1077eda14cbcSMatt Macy  */
1078eda14cbcSMatt Macy void
1079eda14cbcSMatt Macy libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1080eda14cbcSMatt Macy {
1081eda14cbcSMatt Macy 	if (cbp->cb_alloc == cbp->cb_used) {
1082eda14cbcSMatt Macy 		size_t newsz;
1083eda14cbcSMatt Macy 		zfs_handle_t **newhandles;
1084eda14cbcSMatt Macy 
1085eda14cbcSMatt Macy 		newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64;
1086eda14cbcSMatt Macy 		newhandles = zfs_realloc(zhp->zfs_hdl,
1087eda14cbcSMatt Macy 		    cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *),
1088eda14cbcSMatt Macy 		    newsz * sizeof (zfs_handle_t *));
1089eda14cbcSMatt Macy 		cbp->cb_handles = newhandles;
1090eda14cbcSMatt Macy 		cbp->cb_alloc = newsz;
1091eda14cbcSMatt Macy 	}
1092eda14cbcSMatt Macy 	cbp->cb_handles[cbp->cb_used++] = zhp;
1093eda14cbcSMatt Macy }
1094eda14cbcSMatt Macy 
1095eda14cbcSMatt Macy /*
1096eda14cbcSMatt Macy  * Recursive helper function used during file system enumeration
1097eda14cbcSMatt Macy  */
1098eda14cbcSMatt Macy static int
1099eda14cbcSMatt Macy zfs_iter_cb(zfs_handle_t *zhp, void *data)
1100eda14cbcSMatt Macy {
1101eda14cbcSMatt Macy 	get_all_cb_t *cbp = data;
1102eda14cbcSMatt Macy 
1103eda14cbcSMatt Macy 	if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1104eda14cbcSMatt Macy 		zfs_close(zhp);
1105eda14cbcSMatt Macy 		return (0);
1106eda14cbcSMatt Macy 	}
1107eda14cbcSMatt Macy 
1108eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1109eda14cbcSMatt Macy 		zfs_close(zhp);
1110eda14cbcSMatt Macy 		return (0);
1111eda14cbcSMatt Macy 	}
1112eda14cbcSMatt Macy 
1113eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
1114eda14cbcSMatt Macy 	    ZFS_KEYSTATUS_UNAVAILABLE) {
1115eda14cbcSMatt Macy 		zfs_close(zhp);
1116eda14cbcSMatt Macy 		return (0);
1117eda14cbcSMatt Macy 	}
1118eda14cbcSMatt Macy 
1119eda14cbcSMatt Macy 	/*
1120eda14cbcSMatt Macy 	 * If this filesystem is inconsistent and has a receive resume
1121eda14cbcSMatt Macy 	 * token, we can not mount it.
1122eda14cbcSMatt Macy 	 */
1123eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1124eda14cbcSMatt Macy 	    zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1125eda14cbcSMatt Macy 	    NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1126eda14cbcSMatt Macy 		zfs_close(zhp);
1127eda14cbcSMatt Macy 		return (0);
1128eda14cbcSMatt Macy 	}
1129eda14cbcSMatt Macy 
1130eda14cbcSMatt Macy 	libzfs_add_handle(cbp, zhp);
1131eda14cbcSMatt Macy 	if (zfs_iter_filesystems(zhp, zfs_iter_cb, cbp) != 0) {
1132eda14cbcSMatt Macy 		zfs_close(zhp);
1133eda14cbcSMatt Macy 		return (-1);
1134eda14cbcSMatt Macy 	}
1135eda14cbcSMatt Macy 	return (0);
1136eda14cbcSMatt Macy }
1137eda14cbcSMatt Macy 
1138eda14cbcSMatt Macy /*
1139eda14cbcSMatt Macy  * Sort comparator that compares two mountpoint paths. We sort these paths so
1140eda14cbcSMatt Macy  * that subdirectories immediately follow their parents. This means that we
1141eda14cbcSMatt Macy  * effectively treat the '/' character as the lowest value non-nul char.
1142eda14cbcSMatt Macy  * Since filesystems from non-global zones can have the same mountpoint
1143eda14cbcSMatt Macy  * as other filesystems, the comparator sorts global zone filesystems to
1144eda14cbcSMatt Macy  * the top of the list. This means that the global zone will traverse the
1145eda14cbcSMatt Macy  * filesystem list in the correct order and can stop when it sees the
1146eda14cbcSMatt Macy  * first zoned filesystem. In a non-global zone, only the delegated
1147eda14cbcSMatt Macy  * filesystems are seen.
1148eda14cbcSMatt Macy  *
1149eda14cbcSMatt Macy  * An example sorted list using this comparator would look like:
1150eda14cbcSMatt Macy  *
1151eda14cbcSMatt Macy  * /foo
1152eda14cbcSMatt Macy  * /foo/bar
1153eda14cbcSMatt Macy  * /foo/bar/baz
1154eda14cbcSMatt Macy  * /foo/baz
1155eda14cbcSMatt Macy  * /foo.bar
1156eda14cbcSMatt Macy  * /foo (NGZ1)
1157eda14cbcSMatt Macy  * /foo (NGZ2)
1158eda14cbcSMatt Macy  *
1159eda14cbcSMatt Macy  * The mounting code depends on this ordering to deterministically iterate
1160eda14cbcSMatt Macy  * over filesystems in order to spawn parallel mount tasks.
1161eda14cbcSMatt Macy  */
1162eda14cbcSMatt Macy static int
1163eda14cbcSMatt Macy mountpoint_cmp(const void *arga, const void *argb)
1164eda14cbcSMatt Macy {
1165eda14cbcSMatt Macy 	zfs_handle_t *const *zap = arga;
1166eda14cbcSMatt Macy 	zfs_handle_t *za = *zap;
1167eda14cbcSMatt Macy 	zfs_handle_t *const *zbp = argb;
1168eda14cbcSMatt Macy 	zfs_handle_t *zb = *zbp;
1169eda14cbcSMatt Macy 	char mounta[MAXPATHLEN];
1170eda14cbcSMatt Macy 	char mountb[MAXPATHLEN];
1171eda14cbcSMatt Macy 	const char *a = mounta;
1172eda14cbcSMatt Macy 	const char *b = mountb;
1173eda14cbcSMatt Macy 	boolean_t gota, gotb;
1174eda14cbcSMatt Macy 	uint64_t zoneda, zonedb;
1175eda14cbcSMatt Macy 
1176eda14cbcSMatt Macy 	zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED);
1177eda14cbcSMatt Macy 	zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED);
1178eda14cbcSMatt Macy 	if (zoneda && !zonedb)
1179eda14cbcSMatt Macy 		return (1);
1180eda14cbcSMatt Macy 	if (!zoneda && zonedb)
1181eda14cbcSMatt Macy 		return (-1);
1182eda14cbcSMatt Macy 
1183eda14cbcSMatt Macy 	gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM);
1184eda14cbcSMatt Macy 	if (gota) {
1185eda14cbcSMatt Macy 		verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta,
1186eda14cbcSMatt Macy 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1187eda14cbcSMatt Macy 	}
1188eda14cbcSMatt Macy 	gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM);
1189eda14cbcSMatt Macy 	if (gotb) {
1190eda14cbcSMatt Macy 		verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb,
1191eda14cbcSMatt Macy 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1192eda14cbcSMatt Macy 	}
1193eda14cbcSMatt Macy 
1194eda14cbcSMatt Macy 	if (gota && gotb) {
1195eda14cbcSMatt Macy 		while (*a != '\0' && (*a == *b)) {
1196eda14cbcSMatt Macy 			a++;
1197eda14cbcSMatt Macy 			b++;
1198eda14cbcSMatt Macy 		}
1199eda14cbcSMatt Macy 		if (*a == *b)
1200eda14cbcSMatt Macy 			return (0);
1201eda14cbcSMatt Macy 		if (*a == '\0')
1202eda14cbcSMatt Macy 			return (-1);
1203eda14cbcSMatt Macy 		if (*b == '\0')
1204eda14cbcSMatt Macy 			return (1);
1205eda14cbcSMatt Macy 		if (*a == '/')
1206eda14cbcSMatt Macy 			return (-1);
1207eda14cbcSMatt Macy 		if (*b == '/')
1208eda14cbcSMatt Macy 			return (1);
1209eda14cbcSMatt Macy 		return (*a < *b ? -1 : *a > *b);
1210eda14cbcSMatt Macy 	}
1211eda14cbcSMatt Macy 
1212eda14cbcSMatt Macy 	if (gota)
1213eda14cbcSMatt Macy 		return (-1);
1214eda14cbcSMatt Macy 	if (gotb)
1215eda14cbcSMatt Macy 		return (1);
1216eda14cbcSMatt Macy 
1217eda14cbcSMatt Macy 	/*
1218eda14cbcSMatt Macy 	 * If neither filesystem has a mountpoint, revert to sorting by
1219eda14cbcSMatt Macy 	 * dataset name.
1220eda14cbcSMatt Macy 	 */
1221eda14cbcSMatt Macy 	return (strcmp(zfs_get_name(za), zfs_get_name(zb)));
1222eda14cbcSMatt Macy }
1223eda14cbcSMatt Macy 
1224eda14cbcSMatt Macy /*
1225eda14cbcSMatt Macy  * Return true if path2 is a child of path1 or path2 equals path1 or
1226eda14cbcSMatt Macy  * path1 is "/" (path2 is always a child of "/").
1227eda14cbcSMatt Macy  */
1228eda14cbcSMatt Macy static boolean_t
1229eda14cbcSMatt Macy libzfs_path_contains(const char *path1, const char *path2)
1230eda14cbcSMatt Macy {
1231eda14cbcSMatt Macy 	return (strcmp(path1, path2) == 0 || strcmp(path1, "/") == 0 ||
1232eda14cbcSMatt Macy 	    (strstr(path2, path1) == path2 && path2[strlen(path1)] == '/'));
1233eda14cbcSMatt Macy }
1234eda14cbcSMatt Macy 
1235eda14cbcSMatt Macy /*
1236eda14cbcSMatt Macy  * Given a mountpoint specified by idx in the handles array, find the first
1237eda14cbcSMatt Macy  * non-descendent of that mountpoint and return its index. Descendant paths
1238eda14cbcSMatt Macy  * start with the parent's path. This function relies on the ordering
1239eda14cbcSMatt Macy  * enforced by mountpoint_cmp().
1240eda14cbcSMatt Macy  */
1241eda14cbcSMatt Macy static int
1242eda14cbcSMatt Macy non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx)
1243eda14cbcSMatt Macy {
1244eda14cbcSMatt Macy 	char parent[ZFS_MAXPROPLEN];
1245eda14cbcSMatt Macy 	char child[ZFS_MAXPROPLEN];
1246eda14cbcSMatt Macy 	int i;
1247eda14cbcSMatt Macy 
1248eda14cbcSMatt Macy 	verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent,
1249eda14cbcSMatt Macy 	    sizeof (parent), NULL, NULL, 0, B_FALSE) == 0);
1250eda14cbcSMatt Macy 
1251eda14cbcSMatt Macy 	for (i = idx + 1; i < num_handles; i++) {
1252eda14cbcSMatt Macy 		verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child,
1253eda14cbcSMatt Macy 		    sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1254eda14cbcSMatt Macy 		if (!libzfs_path_contains(parent, child))
1255eda14cbcSMatt Macy 			break;
1256eda14cbcSMatt Macy 	}
1257eda14cbcSMatt Macy 	return (i);
1258eda14cbcSMatt Macy }
1259eda14cbcSMatt Macy 
1260eda14cbcSMatt Macy typedef struct mnt_param {
1261eda14cbcSMatt Macy 	libzfs_handle_t	*mnt_hdl;
1262eda14cbcSMatt Macy 	tpool_t		*mnt_tp;
1263eda14cbcSMatt Macy 	zfs_handle_t	**mnt_zhps; /* filesystems to mount */
1264eda14cbcSMatt Macy 	size_t		mnt_num_handles;
1265eda14cbcSMatt Macy 	int		mnt_idx;	/* Index of selected entry to mount */
1266eda14cbcSMatt Macy 	zfs_iter_f	mnt_func;
1267eda14cbcSMatt Macy 	void		*mnt_data;
1268eda14cbcSMatt Macy } mnt_param_t;
1269eda14cbcSMatt Macy 
1270eda14cbcSMatt Macy /*
1271eda14cbcSMatt Macy  * Allocate and populate the parameter struct for mount function, and
1272eda14cbcSMatt Macy  * schedule mounting of the entry selected by idx.
1273eda14cbcSMatt Macy  */
1274eda14cbcSMatt Macy static void
1275eda14cbcSMatt Macy zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles,
1276eda14cbcSMatt Macy     size_t num_handles, int idx, zfs_iter_f func, void *data, tpool_t *tp)
1277eda14cbcSMatt Macy {
1278eda14cbcSMatt Macy 	mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t));
1279eda14cbcSMatt Macy 
1280eda14cbcSMatt Macy 	mnt_param->mnt_hdl = hdl;
1281eda14cbcSMatt Macy 	mnt_param->mnt_tp = tp;
1282eda14cbcSMatt Macy 	mnt_param->mnt_zhps = handles;
1283eda14cbcSMatt Macy 	mnt_param->mnt_num_handles = num_handles;
1284eda14cbcSMatt Macy 	mnt_param->mnt_idx = idx;
1285eda14cbcSMatt Macy 	mnt_param->mnt_func = func;
1286eda14cbcSMatt Macy 	mnt_param->mnt_data = data;
1287eda14cbcSMatt Macy 
1288eda14cbcSMatt Macy 	(void) tpool_dispatch(tp, zfs_mount_task, (void*)mnt_param);
1289eda14cbcSMatt Macy }
1290eda14cbcSMatt Macy 
1291eda14cbcSMatt Macy /*
1292eda14cbcSMatt Macy  * This is the structure used to keep state of mounting or sharing operations
1293eda14cbcSMatt Macy  * during a call to zpool_enable_datasets().
1294eda14cbcSMatt Macy  */
1295eda14cbcSMatt Macy typedef struct mount_state {
1296eda14cbcSMatt Macy 	/*
1297eda14cbcSMatt Macy 	 * ms_mntstatus is set to -1 if any mount fails. While multiple threads
1298eda14cbcSMatt Macy 	 * could update this variable concurrently, no synchronization is
1299eda14cbcSMatt Macy 	 * needed as it's only ever set to -1.
1300eda14cbcSMatt Macy 	 */
1301eda14cbcSMatt Macy 	int		ms_mntstatus;
1302eda14cbcSMatt Macy 	int		ms_mntflags;
1303eda14cbcSMatt Macy 	const char	*ms_mntopts;
1304eda14cbcSMatt Macy } mount_state_t;
1305eda14cbcSMatt Macy 
1306eda14cbcSMatt Macy static int
1307eda14cbcSMatt Macy zfs_mount_one(zfs_handle_t *zhp, void *arg)
1308eda14cbcSMatt Macy {
1309eda14cbcSMatt Macy 	mount_state_t *ms = arg;
1310eda14cbcSMatt Macy 	int ret = 0;
1311eda14cbcSMatt Macy 
1312eda14cbcSMatt Macy 	/*
1313eda14cbcSMatt Macy 	 * don't attempt to mount encrypted datasets with
1314eda14cbcSMatt Macy 	 * unloaded keys
1315eda14cbcSMatt Macy 	 */
1316eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
1317eda14cbcSMatt Macy 	    ZFS_KEYSTATUS_UNAVAILABLE)
1318eda14cbcSMatt Macy 		return (0);
1319eda14cbcSMatt Macy 
1320eda14cbcSMatt Macy 	if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0)
1321eda14cbcSMatt Macy 		ret = ms->ms_mntstatus = -1;
1322eda14cbcSMatt Macy 	return (ret);
1323eda14cbcSMatt Macy }
1324eda14cbcSMatt Macy 
1325eda14cbcSMatt Macy static int
1326eda14cbcSMatt Macy zfs_share_one(zfs_handle_t *zhp, void *arg)
1327eda14cbcSMatt Macy {
1328eda14cbcSMatt Macy 	mount_state_t *ms = arg;
1329eda14cbcSMatt Macy 	int ret = 0;
1330eda14cbcSMatt Macy 
1331eda14cbcSMatt Macy 	if (zfs_share(zhp) != 0)
1332eda14cbcSMatt Macy 		ret = ms->ms_mntstatus = -1;
1333eda14cbcSMatt Macy 	return (ret);
1334eda14cbcSMatt Macy }
1335eda14cbcSMatt Macy 
1336eda14cbcSMatt Macy /*
1337eda14cbcSMatt Macy  * Thread pool function to mount one file system. On completion, it finds and
1338eda14cbcSMatt Macy  * schedules its children to be mounted. This depends on the sorting done in
1339eda14cbcSMatt Macy  * zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries
1340eda14cbcSMatt Macy  * each descending from the previous) will have no parallelism since we always
1341eda14cbcSMatt Macy  * have to wait for the parent to finish mounting before we can schedule
1342eda14cbcSMatt Macy  * its children.
1343eda14cbcSMatt Macy  */
1344eda14cbcSMatt Macy static void
1345eda14cbcSMatt Macy zfs_mount_task(void *arg)
1346eda14cbcSMatt Macy {
1347eda14cbcSMatt Macy 	mnt_param_t *mp = arg;
1348eda14cbcSMatt Macy 	int idx = mp->mnt_idx;
1349eda14cbcSMatt Macy 	zfs_handle_t **handles = mp->mnt_zhps;
1350eda14cbcSMatt Macy 	size_t num_handles = mp->mnt_num_handles;
1351eda14cbcSMatt Macy 	char mountpoint[ZFS_MAXPROPLEN];
1352eda14cbcSMatt Macy 
1353eda14cbcSMatt Macy 	verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint,
1354eda14cbcSMatt Macy 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
1355eda14cbcSMatt Macy 
1356eda14cbcSMatt Macy 	if (mp->mnt_func(handles[idx], mp->mnt_data) != 0)
1357eda14cbcSMatt Macy 		return;
1358eda14cbcSMatt Macy 
1359eda14cbcSMatt Macy 	/*
1360eda14cbcSMatt Macy 	 * We dispatch tasks to mount filesystems with mountpoints underneath
1361eda14cbcSMatt Macy 	 * this one. We do this by dispatching the next filesystem with a
1362eda14cbcSMatt Macy 	 * descendant mountpoint of the one we just mounted, then skip all of
1363eda14cbcSMatt Macy 	 * its descendants, dispatch the next descendant mountpoint, and so on.
1364eda14cbcSMatt Macy 	 * The non_descendant_idx() function skips over filesystems that are
1365eda14cbcSMatt Macy 	 * descendants of the filesystem we just dispatched.
1366eda14cbcSMatt Macy 	 */
1367eda14cbcSMatt Macy 	for (int i = idx + 1; i < num_handles;
1368eda14cbcSMatt Macy 	    i = non_descendant_idx(handles, num_handles, i)) {
1369eda14cbcSMatt Macy 		char child[ZFS_MAXPROPLEN];
1370eda14cbcSMatt Macy 		verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT,
1371eda14cbcSMatt Macy 		    child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1372eda14cbcSMatt Macy 
1373eda14cbcSMatt Macy 		if (!libzfs_path_contains(mountpoint, child))
1374eda14cbcSMatt Macy 			break; /* not a descendant, return */
1375eda14cbcSMatt Macy 		zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i,
1376eda14cbcSMatt Macy 		    mp->mnt_func, mp->mnt_data, mp->mnt_tp);
1377eda14cbcSMatt Macy 	}
1378eda14cbcSMatt Macy 	free(mp);
1379eda14cbcSMatt Macy }
1380eda14cbcSMatt Macy 
1381eda14cbcSMatt Macy /*
1382eda14cbcSMatt Macy  * Issue the func callback for each ZFS handle contained in the handles
1383eda14cbcSMatt Macy  * array. This function is used to mount all datasets, and so this function
1384eda14cbcSMatt Macy  * guarantees that filesystems for parent mountpoints are called before their
1385eda14cbcSMatt Macy  * children. As such, before issuing any callbacks, we first sort the array
1386eda14cbcSMatt Macy  * of handles by mountpoint.
1387eda14cbcSMatt Macy  *
1388eda14cbcSMatt Macy  * Callbacks are issued in one of two ways:
1389eda14cbcSMatt Macy  *
1390eda14cbcSMatt Macy  * 1. Sequentially: If the parallel argument is B_FALSE or the ZFS_SERIAL_MOUNT
1391eda14cbcSMatt Macy  *    environment variable is set, then we issue callbacks sequentially.
1392eda14cbcSMatt Macy  *
1393eda14cbcSMatt Macy  * 2. In parallel: If the parallel argument is B_TRUE and the ZFS_SERIAL_MOUNT
1394eda14cbcSMatt Macy  *    environment variable is not set, then we use a tpool to dispatch threads
1395eda14cbcSMatt Macy  *    to mount filesystems in parallel. This function dispatches tasks to mount
1396eda14cbcSMatt Macy  *    the filesystems at the top-level mountpoints, and these tasks in turn
1397eda14cbcSMatt Macy  *    are responsible for recursively mounting filesystems in their children
1398eda14cbcSMatt Macy  *    mountpoints.
1399eda14cbcSMatt Macy  */
1400eda14cbcSMatt Macy void
1401eda14cbcSMatt Macy zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles,
1402eda14cbcSMatt Macy     size_t num_handles, zfs_iter_f func, void *data, boolean_t parallel)
1403eda14cbcSMatt Macy {
1404eda14cbcSMatt Macy 	zoneid_t zoneid = getzoneid();
1405eda14cbcSMatt Macy 
1406eda14cbcSMatt Macy 	/*
1407eda14cbcSMatt Macy 	 * The ZFS_SERIAL_MOUNT environment variable is an undocumented
1408eda14cbcSMatt Macy 	 * variable that can be used as a convenience to do a/b comparison
1409eda14cbcSMatt Macy 	 * of serial vs. parallel mounting.
1410eda14cbcSMatt Macy 	 */
1411eda14cbcSMatt Macy 	boolean_t serial_mount = !parallel ||
1412eda14cbcSMatt Macy 	    (getenv("ZFS_SERIAL_MOUNT") != NULL);
1413eda14cbcSMatt Macy 
1414eda14cbcSMatt Macy 	/*
1415eda14cbcSMatt Macy 	 * Sort the datasets by mountpoint. See mountpoint_cmp for details
1416eda14cbcSMatt Macy 	 * of how these are sorted.
1417eda14cbcSMatt Macy 	 */
1418eda14cbcSMatt Macy 	qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp);
1419eda14cbcSMatt Macy 
1420eda14cbcSMatt Macy 	if (serial_mount) {
1421eda14cbcSMatt Macy 		for (int i = 0; i < num_handles; i++) {
1422eda14cbcSMatt Macy 			func(handles[i], data);
1423eda14cbcSMatt Macy 		}
1424eda14cbcSMatt Macy 		return;
1425eda14cbcSMatt Macy 	}
1426eda14cbcSMatt Macy 
1427eda14cbcSMatt Macy 	/*
1428eda14cbcSMatt Macy 	 * Issue the callback function for each dataset using a parallel
1429eda14cbcSMatt Macy 	 * algorithm that uses a thread pool to manage threads.
1430eda14cbcSMatt Macy 	 */
1431eda14cbcSMatt Macy 	tpool_t *tp = tpool_create(1, mount_tp_nthr, 0, NULL);
1432eda14cbcSMatt Macy 
1433eda14cbcSMatt Macy 	/*
1434eda14cbcSMatt Macy 	 * There may be multiple "top level" mountpoints outside of the pool's
1435eda14cbcSMatt Macy 	 * root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of
1436eda14cbcSMatt Macy 	 * these.
1437eda14cbcSMatt Macy 	 */
1438eda14cbcSMatt Macy 	for (int i = 0; i < num_handles;
1439eda14cbcSMatt Macy 	    i = non_descendant_idx(handles, num_handles, i)) {
1440eda14cbcSMatt Macy 		/*
1441eda14cbcSMatt Macy 		 * Since the mountpoints have been sorted so that the zoned
1442eda14cbcSMatt Macy 		 * filesystems are at the end, a zoned filesystem seen from
1443eda14cbcSMatt Macy 		 * the global zone means that we're done.
1444eda14cbcSMatt Macy 		 */
1445eda14cbcSMatt Macy 		if (zoneid == GLOBAL_ZONEID &&
1446eda14cbcSMatt Macy 		    zfs_prop_get_int(handles[i], ZFS_PROP_ZONED))
1447eda14cbcSMatt Macy 			break;
1448eda14cbcSMatt Macy 		zfs_dispatch_mount(hdl, handles, num_handles, i, func, data,
1449eda14cbcSMatt Macy 		    tp);
1450eda14cbcSMatt Macy 	}
1451eda14cbcSMatt Macy 
1452eda14cbcSMatt Macy 	tpool_wait(tp);	/* wait for all scheduled mounts to complete */
1453eda14cbcSMatt Macy 	tpool_destroy(tp);
1454eda14cbcSMatt Macy }
1455eda14cbcSMatt Macy 
1456eda14cbcSMatt Macy /*
1457eda14cbcSMatt Macy  * Mount and share all datasets within the given pool.  This assumes that no
1458eda14cbcSMatt Macy  * datasets within the pool are currently mounted.
1459eda14cbcSMatt Macy  */
1460eda14cbcSMatt Macy int
1461eda14cbcSMatt Macy zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1462eda14cbcSMatt Macy {
1463eda14cbcSMatt Macy 	get_all_cb_t cb = { 0 };
1464eda14cbcSMatt Macy 	mount_state_t ms = { 0 };
1465eda14cbcSMatt Macy 	zfs_handle_t *zfsp;
1466eda14cbcSMatt Macy 	int ret = 0;
1467eda14cbcSMatt Macy 
1468eda14cbcSMatt Macy 	if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
1469eda14cbcSMatt Macy 	    ZFS_TYPE_DATASET)) == NULL)
1470eda14cbcSMatt Macy 		goto out;
1471eda14cbcSMatt Macy 
1472eda14cbcSMatt Macy 	/*
1473eda14cbcSMatt Macy 	 * Gather all non-snapshot datasets within the pool. Start by adding
1474eda14cbcSMatt Macy 	 * the root filesystem for this pool to the list, and then iterate
1475eda14cbcSMatt Macy 	 * over all child filesystems.
1476eda14cbcSMatt Macy 	 */
1477eda14cbcSMatt Macy 	libzfs_add_handle(&cb, zfsp);
1478eda14cbcSMatt Macy 	if (zfs_iter_filesystems(zfsp, zfs_iter_cb, &cb) != 0)
1479eda14cbcSMatt Macy 		goto out;
1480eda14cbcSMatt Macy 
1481eda14cbcSMatt Macy 	/*
1482eda14cbcSMatt Macy 	 * Mount all filesystems
1483eda14cbcSMatt Macy 	 */
1484eda14cbcSMatt Macy 	ms.ms_mntopts = mntopts;
1485eda14cbcSMatt Macy 	ms.ms_mntflags = flags;
1486eda14cbcSMatt Macy 	zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1487eda14cbcSMatt Macy 	    zfs_mount_one, &ms, B_TRUE);
1488eda14cbcSMatt Macy 	if (ms.ms_mntstatus != 0)
1489eda14cbcSMatt Macy 		ret = ms.ms_mntstatus;
1490eda14cbcSMatt Macy 
1491eda14cbcSMatt Macy 	/*
1492eda14cbcSMatt Macy 	 * Share all filesystems that need to be shared. This needs to be
1493eda14cbcSMatt Macy 	 * a separate pass because libshare is not mt-safe, and so we need
1494eda14cbcSMatt Macy 	 * to share serially.
1495eda14cbcSMatt Macy 	 */
1496eda14cbcSMatt Macy 	ms.ms_mntstatus = 0;
1497eda14cbcSMatt Macy 	zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1498eda14cbcSMatt Macy 	    zfs_share_one, &ms, B_FALSE);
1499eda14cbcSMatt Macy 	if (ms.ms_mntstatus != 0)
1500eda14cbcSMatt Macy 		ret = ms.ms_mntstatus;
1501eda14cbcSMatt Macy 	else
1502eda14cbcSMatt Macy 		zfs_commit_all_shares();
1503eda14cbcSMatt Macy 
1504eda14cbcSMatt Macy out:
1505eda14cbcSMatt Macy 	for (int i = 0; i < cb.cb_used; i++)
1506eda14cbcSMatt Macy 		zfs_close(cb.cb_handles[i]);
1507eda14cbcSMatt Macy 	free(cb.cb_handles);
1508eda14cbcSMatt Macy 
1509eda14cbcSMatt Macy 	return (ret);
1510eda14cbcSMatt Macy }
1511eda14cbcSMatt Macy 
15123f9d360cSMartin Matuska struct sets_s {
15133f9d360cSMartin Matuska 	char *mountpoint;
15143f9d360cSMartin Matuska 	zfs_handle_t *dataset;
15153f9d360cSMartin Matuska };
15163f9d360cSMartin Matuska 
1517eda14cbcSMatt Macy static int
1518eda14cbcSMatt Macy mountpoint_compare(const void *a, const void *b)
1519eda14cbcSMatt Macy {
15203f9d360cSMartin Matuska 	const struct sets_s *mounta = (struct sets_s *)a;
15213f9d360cSMartin Matuska 	const struct sets_s *mountb = (struct sets_s *)b;
1522eda14cbcSMatt Macy 
15233f9d360cSMartin Matuska 	return (strcmp(mountb->mountpoint, mounta->mountpoint));
1524eda14cbcSMatt Macy }
1525eda14cbcSMatt Macy 
1526eda14cbcSMatt Macy /*
1527eda14cbcSMatt Macy  * Unshare and unmount all datasets within the given pool.  We don't want to
1528eda14cbcSMatt Macy  * rely on traversing the DSL to discover the filesystems within the pool,
1529eda14cbcSMatt Macy  * because this may be expensive (if not all of them are mounted), and can fail
1530eda14cbcSMatt Macy  * arbitrarily (on I/O error, for example).  Instead, we walk /proc/self/mounts
1531eda14cbcSMatt Macy  * and gather all the filesystems that are currently mounted.
1532eda14cbcSMatt Macy  */
1533eda14cbcSMatt Macy int
1534eda14cbcSMatt Macy zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1535eda14cbcSMatt Macy {
1536eda14cbcSMatt Macy 	int used, alloc;
153716038816SMartin Matuska 	FILE *mnttab;
1538eda14cbcSMatt Macy 	struct mnttab entry;
1539eda14cbcSMatt Macy 	size_t namelen;
15403f9d360cSMartin Matuska 	struct sets_s *sets = NULL;
1541eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1542eda14cbcSMatt Macy 	int i;
1543eda14cbcSMatt Macy 	int ret = -1;
1544eda14cbcSMatt Macy 	int flags = (force ? MS_FORCE : 0);
1545eda14cbcSMatt Macy 
1546eda14cbcSMatt Macy 	namelen = strlen(zhp->zpool_name);
1547eda14cbcSMatt Macy 
154816038816SMartin Matuska 	if ((mnttab = fopen(MNTTAB, "re")) == NULL)
1549eda14cbcSMatt Macy 		return (ENOENT);
1550eda14cbcSMatt Macy 
1551eda14cbcSMatt Macy 	used = alloc = 0;
155216038816SMartin Matuska 	while (getmntent(mnttab, &entry) == 0) {
1553eda14cbcSMatt Macy 		/*
1554eda14cbcSMatt Macy 		 * Ignore non-ZFS entries.
1555eda14cbcSMatt Macy 		 */
1556eda14cbcSMatt Macy 		if (entry.mnt_fstype == NULL ||
1557eda14cbcSMatt Macy 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1558eda14cbcSMatt Macy 			continue;
1559eda14cbcSMatt Macy 
1560eda14cbcSMatt Macy 		/*
1561eda14cbcSMatt Macy 		 * Ignore filesystems not within this pool.
1562eda14cbcSMatt Macy 		 */
1563eda14cbcSMatt Macy 		if (entry.mnt_mountp == NULL ||
1564eda14cbcSMatt Macy 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1565eda14cbcSMatt Macy 		    (entry.mnt_special[namelen] != '/' &&
1566eda14cbcSMatt Macy 		    entry.mnt_special[namelen] != '\0'))
1567eda14cbcSMatt Macy 			continue;
1568eda14cbcSMatt Macy 
1569eda14cbcSMatt Macy 		/*
1570eda14cbcSMatt Macy 		 * At this point we've found a filesystem within our pool.  Add
1571eda14cbcSMatt Macy 		 * it to our growing list.
1572eda14cbcSMatt Macy 		 */
1573eda14cbcSMatt Macy 		if (used == alloc) {
1574eda14cbcSMatt Macy 			if (alloc == 0) {
1575eda14cbcSMatt Macy 
15763f9d360cSMartin Matuska 				if ((sets = zfs_alloc(hdl,
15773f9d360cSMartin Matuska 				    8 * sizeof (struct sets_s))) == NULL)
1578eda14cbcSMatt Macy 					goto out;
1579eda14cbcSMatt Macy 
1580eda14cbcSMatt Macy 				alloc = 8;
1581eda14cbcSMatt Macy 			} else {
1582eda14cbcSMatt Macy 				void *ptr;
1583eda14cbcSMatt Macy 
15843f9d360cSMartin Matuska 				if ((ptr = zfs_realloc(hdl, sets,
15853f9d360cSMartin Matuska 				    alloc * sizeof (struct sets_s),
15863f9d360cSMartin Matuska 				    alloc * 2 * sizeof (struct sets_s)))
15873f9d360cSMartin Matuska 				    == NULL)
1588eda14cbcSMatt Macy 					goto out;
15893f9d360cSMartin Matuska 				sets = ptr;
1590eda14cbcSMatt Macy 
1591eda14cbcSMatt Macy 				alloc *= 2;
1592eda14cbcSMatt Macy 			}
1593eda14cbcSMatt Macy 		}
1594eda14cbcSMatt Macy 
15953f9d360cSMartin Matuska 		if ((sets[used].mountpoint = zfs_strdup(hdl,
1596eda14cbcSMatt Macy 		    entry.mnt_mountp)) == NULL)
1597eda14cbcSMatt Macy 			goto out;
1598eda14cbcSMatt Macy 
1599eda14cbcSMatt Macy 		/*
1600eda14cbcSMatt Macy 		 * This is allowed to fail, in case there is some I/O error.  It
1601eda14cbcSMatt Macy 		 * is only used to determine if we need to remove the underlying
1602eda14cbcSMatt Macy 		 * mountpoint, so failure is not fatal.
1603eda14cbcSMatt Macy 		 */
16043f9d360cSMartin Matuska 		sets[used].dataset = make_dataset_handle(hdl,
16053f9d360cSMartin Matuska 		    entry.mnt_special);
1606eda14cbcSMatt Macy 
1607eda14cbcSMatt Macy 		used++;
1608eda14cbcSMatt Macy 	}
1609eda14cbcSMatt Macy 
1610eda14cbcSMatt Macy 	/*
1611eda14cbcSMatt Macy 	 * At this point, we have the entire list of filesystems, so sort it by
1612eda14cbcSMatt Macy 	 * mountpoint.
1613eda14cbcSMatt Macy 	 */
16143f9d360cSMartin Matuska 	qsort(sets, used, sizeof (struct sets_s), mountpoint_compare);
1615eda14cbcSMatt Macy 
1616eda14cbcSMatt Macy 	/*
1617eda14cbcSMatt Macy 	 * Walk through and first unshare everything.
1618eda14cbcSMatt Macy 	 */
1619eda14cbcSMatt Macy 	for (i = 0; i < used; i++) {
1620eda14cbcSMatt Macy 		zfs_share_proto_t *curr_proto;
1621eda14cbcSMatt Macy 		for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1622eda14cbcSMatt Macy 		    curr_proto++) {
16233f9d360cSMartin Matuska 			if (is_shared(sets[i].mountpoint, *curr_proto) &&
16243f9d360cSMartin Matuska 			    unshare_one(hdl, sets[i].mountpoint,
16253f9d360cSMartin Matuska 			    sets[i].mountpoint, *curr_proto) != 0)
1626eda14cbcSMatt Macy 				goto out;
1627eda14cbcSMatt Macy 		}
1628eda14cbcSMatt Macy 	}
1629eda14cbcSMatt Macy 	zfs_commit_all_shares();
1630eda14cbcSMatt Macy 
1631eda14cbcSMatt Macy 	/*
1632eda14cbcSMatt Macy 	 * Now unmount everything, removing the underlying directories as
1633eda14cbcSMatt Macy 	 * appropriate.
1634eda14cbcSMatt Macy 	 */
1635eda14cbcSMatt Macy 	for (i = 0; i < used; i++) {
16363f9d360cSMartin Matuska 		if (unmount_one(sets[i].dataset, sets[i].mountpoint,
16373f9d360cSMartin Matuska 		    flags) != 0)
1638eda14cbcSMatt Macy 			goto out;
1639eda14cbcSMatt Macy 	}
1640eda14cbcSMatt Macy 
1641eda14cbcSMatt Macy 	for (i = 0; i < used; i++) {
16423f9d360cSMartin Matuska 		if (sets[i].dataset)
16433f9d360cSMartin Matuska 			remove_mountpoint(sets[i].dataset);
1644eda14cbcSMatt Macy 	}
1645eda14cbcSMatt Macy 
164653b70c86SMartin Matuska 	zpool_disable_datasets_os(zhp, force);
164753b70c86SMartin Matuska 
1648eda14cbcSMatt Macy 	ret = 0;
1649eda14cbcSMatt Macy out:
165016038816SMartin Matuska 	(void) fclose(mnttab);
1651eda14cbcSMatt Macy 	for (i = 0; i < used; i++) {
16523f9d360cSMartin Matuska 		if (sets[i].dataset)
16533f9d360cSMartin Matuska 			zfs_close(sets[i].dataset);
16543f9d360cSMartin Matuska 		free(sets[i].mountpoint);
1655eda14cbcSMatt Macy 	}
16563f9d360cSMartin Matuska 	free(sets);
1657eda14cbcSMatt Macy 
1658eda14cbcSMatt Macy 	return (ret);
1659eda14cbcSMatt Macy }
1660