xref: /freebsd/sys/contrib/openzfs/lib/libzfs/libzfs_mount.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  * CDDL HEADER START
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy  *
9eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy  * See the License for the specific language governing permissions
12eda14cbcSMatt Macy  * and limitations under the License.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy  *
20eda14cbcSMatt Macy  * CDDL HEADER END
21eda14cbcSMatt Macy  */
22eda14cbcSMatt Macy 
23eda14cbcSMatt Macy /*
24eda14cbcSMatt Macy  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26c7046f76SMartin Matuska  * Copyright (c) 2014, 2022 by Delphix. All rights reserved.
27eda14cbcSMatt Macy  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
28eda14cbcSMatt Macy  * Copyright 2017 RackTop Systems.
29eda14cbcSMatt Macy  * Copyright (c) 2018 Datto Inc.
30eda14cbcSMatt Macy  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
31eda14cbcSMatt Macy  */
32eda14cbcSMatt Macy 
33eda14cbcSMatt Macy /*
34eda14cbcSMatt Macy  * Routines to manage ZFS mounts.  We separate all the nasty routines that have
35eda14cbcSMatt Macy  * to deal with the OS.  The following functions are the main entry points --
36eda14cbcSMatt Macy  * they are used by mount and unmount and when changing a filesystem's
37eda14cbcSMatt Macy  * mountpoint.
38eda14cbcSMatt Macy  *
39eda14cbcSMatt Macy  *	zfs_is_mounted()
40eda14cbcSMatt Macy  *	zfs_mount()
41eda14cbcSMatt Macy  *	zfs_mount_at()
42eda14cbcSMatt Macy  *	zfs_unmount()
43eda14cbcSMatt Macy  *	zfs_unmountall()
44eda14cbcSMatt Macy  *
45716fd348SMartin Matuska  * This file also contains the functions used to manage sharing filesystems:
46eda14cbcSMatt Macy  *
47eda14cbcSMatt Macy  *	zfs_is_shared()
48eda14cbcSMatt Macy  *	zfs_share()
49eda14cbcSMatt Macy  *	zfs_unshare()
50eda14cbcSMatt Macy  *	zfs_unshareall()
51716fd348SMartin Matuska  *	zfs_commit_shares()
52eda14cbcSMatt Macy  *
53eda14cbcSMatt Macy  * The following functions are available for pool consumers, and will
54eda14cbcSMatt Macy  * mount/unmount and share/unshare all datasets within pool:
55eda14cbcSMatt Macy  *
56eda14cbcSMatt Macy  *	zpool_enable_datasets()
57eda14cbcSMatt Macy  *	zpool_disable_datasets()
58eda14cbcSMatt Macy  */
59eda14cbcSMatt Macy 
60eda14cbcSMatt Macy #include <dirent.h>
61eda14cbcSMatt Macy #include <dlfcn.h>
62eda14cbcSMatt Macy #include <errno.h>
63eda14cbcSMatt Macy #include <fcntl.h>
64eda14cbcSMatt Macy #include <libgen.h>
65eda14cbcSMatt Macy #include <libintl.h>
66eda14cbcSMatt Macy #include <stdio.h>
67eda14cbcSMatt Macy #include <stdlib.h>
68da5137abSMartin Matuska #include <string.h>
69eda14cbcSMatt Macy #include <unistd.h>
70eda14cbcSMatt Macy #include <zone.h>
71eda14cbcSMatt Macy #include <sys/mntent.h>
72eda14cbcSMatt Macy #include <sys/mount.h>
73eda14cbcSMatt Macy #include <sys/stat.h>
74eda14cbcSMatt Macy #include <sys/vfs.h>
75eda14cbcSMatt Macy #include <sys/dsl_crypt.h>
76eda14cbcSMatt Macy 
77eda14cbcSMatt Macy #include <libzfs.h>
78fd45b686SMartin Matuska #include <libzutil.h>
79eda14cbcSMatt Macy 
80eda14cbcSMatt Macy #include "libzfs_impl.h"
81eda14cbcSMatt Macy #include <thread_pool.h>
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy #include <libshare.h>
84eda14cbcSMatt Macy #include <sys/systeminfo.h>
85eda14cbcSMatt Macy #define	MAXISALEN	257	/* based on sysinfo(2) man page */
86eda14cbcSMatt Macy 
87eda14cbcSMatt Macy static void zfs_mount_task(void *);
88eda14cbcSMatt Macy 
89716fd348SMartin Matuska static const proto_table_t proto_table[SA_PROTOCOL_COUNT] = {
90716fd348SMartin Matuska 	[SA_PROTOCOL_NFS] =
91716fd348SMartin Matuska 	    {ZFS_PROP_SHARENFS, EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
92716fd348SMartin Matuska 	[SA_PROTOCOL_SMB] =
93716fd348SMartin Matuska 	    {ZFS_PROP_SHARESMB, EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
94eda14cbcSMatt Macy };
95eda14cbcSMatt Macy 
96716fd348SMartin Matuska static const enum sa_protocol share_all_proto[SA_PROTOCOL_COUNT + 1] = {
97716fd348SMartin Matuska 	SA_PROTOCOL_NFS,
98716fd348SMartin Matuska 	SA_PROTOCOL_SMB,
99716fd348SMartin Matuska 	SA_NO_PROTOCOL
100eda14cbcSMatt Macy };
101eda14cbcSMatt Macy 
102eda14cbcSMatt Macy 
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy static boolean_t
dir_is_empty_stat(const char * dirname)105eda14cbcSMatt Macy dir_is_empty_stat(const char *dirname)
106eda14cbcSMatt Macy {
107eda14cbcSMatt Macy 	struct stat st;
108eda14cbcSMatt Macy 
109eda14cbcSMatt Macy 	/*
110eda14cbcSMatt Macy 	 * We only want to return false if the given path is a non empty
111eda14cbcSMatt Macy 	 * directory, all other errors are handled elsewhere.
112eda14cbcSMatt Macy 	 */
113eda14cbcSMatt Macy 	if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
114eda14cbcSMatt Macy 		return (B_TRUE);
115eda14cbcSMatt Macy 	}
116eda14cbcSMatt Macy 
117eda14cbcSMatt Macy 	/*
118eda14cbcSMatt Macy 	 * An empty directory will still have two entries in it, one
119eda14cbcSMatt Macy 	 * entry for each of "." and "..".
120eda14cbcSMatt Macy 	 */
121eda14cbcSMatt Macy 	if (st.st_size > 2) {
122eda14cbcSMatt Macy 		return (B_FALSE);
123eda14cbcSMatt Macy 	}
124eda14cbcSMatt Macy 
125eda14cbcSMatt Macy 	return (B_TRUE);
126eda14cbcSMatt Macy }
127eda14cbcSMatt Macy 
128eda14cbcSMatt Macy static boolean_t
dir_is_empty_readdir(const char * dirname)129eda14cbcSMatt Macy dir_is_empty_readdir(const char *dirname)
130eda14cbcSMatt Macy {
131eda14cbcSMatt Macy 	DIR *dirp;
132eda14cbcSMatt Macy 	struct dirent64 *dp;
133eda14cbcSMatt Macy 	int dirfd;
134eda14cbcSMatt Macy 
135eda14cbcSMatt Macy 	if ((dirfd = openat(AT_FDCWD, dirname,
136eda14cbcSMatt Macy 	    O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
137eda14cbcSMatt Macy 		return (B_TRUE);
138eda14cbcSMatt Macy 	}
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy 	if ((dirp = fdopendir(dirfd)) == NULL) {
141eda14cbcSMatt Macy 		(void) close(dirfd);
142eda14cbcSMatt Macy 		return (B_TRUE);
143eda14cbcSMatt Macy 	}
144eda14cbcSMatt Macy 
145eda14cbcSMatt Macy 	while ((dp = readdir64(dirp)) != NULL) {
146eda14cbcSMatt Macy 
147eda14cbcSMatt Macy 		if (strcmp(dp->d_name, ".") == 0 ||
148eda14cbcSMatt Macy 		    strcmp(dp->d_name, "..") == 0)
149eda14cbcSMatt Macy 			continue;
150eda14cbcSMatt Macy 
151eda14cbcSMatt Macy 		(void) closedir(dirp);
152eda14cbcSMatt Macy 		return (B_FALSE);
153eda14cbcSMatt Macy 	}
154eda14cbcSMatt Macy 
155eda14cbcSMatt Macy 	(void) closedir(dirp);
156eda14cbcSMatt Macy 	return (B_TRUE);
157eda14cbcSMatt Macy }
158eda14cbcSMatt Macy 
159eda14cbcSMatt Macy /*
160eda14cbcSMatt Macy  * Returns true if the specified directory is empty.  If we can't open the
161eda14cbcSMatt Macy  * directory at all, return true so that the mount can fail with a more
162eda14cbcSMatt Macy  * informative error message.
163eda14cbcSMatt Macy  */
164eda14cbcSMatt Macy static boolean_t
dir_is_empty(const char * dirname)165eda14cbcSMatt Macy dir_is_empty(const char *dirname)
166eda14cbcSMatt Macy {
167eda14cbcSMatt Macy 	struct statfs64 st;
168eda14cbcSMatt Macy 
169eda14cbcSMatt Macy 	/*
170eda14cbcSMatt Macy 	 * If the statvfs call fails or the filesystem is not a ZFS
171eda14cbcSMatt Macy 	 * filesystem, fall back to the slow path which uses readdir.
172eda14cbcSMatt Macy 	 */
173eda14cbcSMatt Macy 	if ((statfs64(dirname, &st) != 0) ||
174eda14cbcSMatt Macy 	    (st.f_type != ZFS_SUPER_MAGIC)) {
175eda14cbcSMatt Macy 		return (dir_is_empty_readdir(dirname));
176eda14cbcSMatt Macy 	}
177eda14cbcSMatt Macy 
178eda14cbcSMatt Macy 	/*
179eda14cbcSMatt Macy 	 * At this point, we know the provided path is on a ZFS
180eda14cbcSMatt Macy 	 * filesystem, so we can use stat instead of readdir to
181eda14cbcSMatt Macy 	 * determine if the directory is empty or not. We try to avoid
182eda14cbcSMatt Macy 	 * using readdir because that requires opening "dirname"; this
183eda14cbcSMatt Macy 	 * open file descriptor can potentially end up in a child
184eda14cbcSMatt Macy 	 * process if there's a concurrent fork, thus preventing the
185eda14cbcSMatt Macy 	 * zfs_mount() from otherwise succeeding (the open file
186eda14cbcSMatt Macy 	 * descriptor inherited by the child process will cause the
187eda14cbcSMatt Macy 	 * parent's mount to fail with EBUSY). The performance
188eda14cbcSMatt Macy 	 * implications of replacing the open, read, and close with a
189eda14cbcSMatt Macy 	 * single stat is nice; but is not the main motivation for the
190eda14cbcSMatt Macy 	 * added complexity.
191eda14cbcSMatt Macy 	 */
192eda14cbcSMatt Macy 	return (dir_is_empty_stat(dirname));
193eda14cbcSMatt Macy }
194eda14cbcSMatt Macy 
195eda14cbcSMatt Macy /*
196eda14cbcSMatt Macy  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
197eda14cbcSMatt Macy  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
198eda14cbcSMatt Macy  * 0.
199eda14cbcSMatt Macy  */
200eda14cbcSMatt Macy boolean_t
is_mounted(libzfs_handle_t * zfs_hdl,const char * special,char ** where)201eda14cbcSMatt Macy is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
202eda14cbcSMatt Macy {
203eda14cbcSMatt Macy 	struct mnttab entry;
204eda14cbcSMatt Macy 
205eda14cbcSMatt Macy 	if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
206eda14cbcSMatt Macy 		return (B_FALSE);
207eda14cbcSMatt Macy 
208eda14cbcSMatt Macy 	if (where != NULL)
209eda14cbcSMatt Macy 		*where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
210eda14cbcSMatt Macy 
211eda14cbcSMatt Macy 	return (B_TRUE);
212eda14cbcSMatt Macy }
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy boolean_t
zfs_is_mounted(zfs_handle_t * zhp,char ** where)215eda14cbcSMatt Macy zfs_is_mounted(zfs_handle_t *zhp, char **where)
216eda14cbcSMatt Macy {
217eda14cbcSMatt Macy 	return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
218eda14cbcSMatt Macy }
219eda14cbcSMatt Macy 
220eda14cbcSMatt Macy /*
221eda14cbcSMatt Macy  * Checks any higher order concerns about whether the given dataset is
222eda14cbcSMatt Macy  * mountable, false otherwise.  zfs_is_mountable_internal specifically assumes
223eda14cbcSMatt Macy  * that the caller has verified the sanity of mounting the dataset at
224e92ffd9bSMartin Matuska  * its mountpoint to the extent the caller wants.
225eda14cbcSMatt Macy  */
226eda14cbcSMatt Macy static boolean_t
zfs_is_mountable_internal(zfs_handle_t * zhp)227e92ffd9bSMartin Matuska zfs_is_mountable_internal(zfs_handle_t *zhp)
228eda14cbcSMatt Macy {
229eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
230eda14cbcSMatt Macy 	    getzoneid() == GLOBAL_ZONEID)
231eda14cbcSMatt Macy 		return (B_FALSE);
232eda14cbcSMatt Macy 
233eda14cbcSMatt Macy 	return (B_TRUE);
234eda14cbcSMatt Macy }
235eda14cbcSMatt Macy 
236eda14cbcSMatt Macy /*
237eda14cbcSMatt Macy  * Returns true if the given dataset is mountable, false otherwise.  Returns the
238eda14cbcSMatt Macy  * mountpoint in 'buf'.
239eda14cbcSMatt Macy  */
240716fd348SMartin Matuska static boolean_t
zfs_is_mountable(zfs_handle_t * zhp,char * buf,size_t buflen,zprop_source_t * source,int flags)241eda14cbcSMatt Macy zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
242eda14cbcSMatt Macy     zprop_source_t *source, int flags)
243eda14cbcSMatt Macy {
244eda14cbcSMatt Macy 	char sourceloc[MAXNAMELEN];
245eda14cbcSMatt Macy 	zprop_source_t sourcetype;
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy 	if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type,
248eda14cbcSMatt Macy 	    B_FALSE))
249eda14cbcSMatt Macy 		return (B_FALSE);
250eda14cbcSMatt Macy 
251eda14cbcSMatt Macy 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
252eda14cbcSMatt Macy 	    &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy 	if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
255eda14cbcSMatt Macy 	    strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
256eda14cbcSMatt Macy 		return (B_FALSE);
257eda14cbcSMatt Macy 
258eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
259eda14cbcSMatt Macy 		return (B_FALSE);
260eda14cbcSMatt Macy 
261e92ffd9bSMartin Matuska 	if (!zfs_is_mountable_internal(zhp))
262eda14cbcSMatt Macy 		return (B_FALSE);
263eda14cbcSMatt Macy 
264eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_REDACTED) && !(flags & MS_FORCE))
265eda14cbcSMatt Macy 		return (B_FALSE);
266eda14cbcSMatt Macy 
267eda14cbcSMatt Macy 	if (source)
268eda14cbcSMatt Macy 		*source = sourcetype;
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy 	return (B_TRUE);
271eda14cbcSMatt Macy }
272eda14cbcSMatt Macy 
273eda14cbcSMatt Macy /*
274eda14cbcSMatt Macy  * The filesystem is mounted by invoking the system mount utility rather
275eda14cbcSMatt Macy  * than by the system call mount(2).  This ensures that the /etc/mtab
276eda14cbcSMatt Macy  * file is correctly locked for the update.  Performing our own locking
277eda14cbcSMatt Macy  * and /etc/mtab update requires making an unsafe assumption about how
278eda14cbcSMatt Macy  * the mount utility performs its locking.  Unfortunately, this also means
279eda14cbcSMatt Macy  * in the case of a mount failure we do not have the exact errno.  We must
280eda14cbcSMatt Macy  * make due with return value from the mount process.
281eda14cbcSMatt Macy  *
282eda14cbcSMatt Macy  * In the long term a shared library called libmount is under development
283eda14cbcSMatt Macy  * which provides a common API to address the locking and errno issues.
284eda14cbcSMatt Macy  * Once the standard mount utility has been updated to use this library
285eda14cbcSMatt Macy  * we can add an autoconf check to conditionally use it.
286eda14cbcSMatt Macy  *
287eda14cbcSMatt Macy  * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
288eda14cbcSMatt Macy  */
289eda14cbcSMatt Macy 
290eda14cbcSMatt Macy static int
zfs_add_option(zfs_handle_t * zhp,char * options,int len,zfs_prop_t prop,const char * on,const char * off)291eda14cbcSMatt Macy zfs_add_option(zfs_handle_t *zhp, char *options, int len,
292a0b956f5SMartin Matuska     zfs_prop_t prop, const char *on, const char *off)
293eda14cbcSMatt Macy {
2942a58b312SMartin Matuska 	const char *source;
295eda14cbcSMatt Macy 	uint64_t value;
296eda14cbcSMatt Macy 
297eda14cbcSMatt Macy 	/* Skip adding duplicate default options */
298eda14cbcSMatt Macy 	if ((strstr(options, on) != NULL) || (strstr(options, off) != NULL))
299eda14cbcSMatt Macy 		return (0);
300eda14cbcSMatt Macy 
301eda14cbcSMatt Macy 	/*
302eda14cbcSMatt Macy 	 * zfs_prop_get_int() is not used to ensure our mount options
303eda14cbcSMatt Macy 	 * are not influenced by the current /proc/self/mounts contents.
304eda14cbcSMatt Macy 	 */
305eda14cbcSMatt Macy 	value = getprop_uint64(zhp, prop, &source);
306eda14cbcSMatt Macy 
307eda14cbcSMatt Macy 	(void) strlcat(options, ",", len);
308eda14cbcSMatt Macy 	(void) strlcat(options, value ? on : off, len);
309eda14cbcSMatt Macy 
310eda14cbcSMatt Macy 	return (0);
311eda14cbcSMatt Macy }
312eda14cbcSMatt Macy 
313eda14cbcSMatt Macy static int
zfs_add_options(zfs_handle_t * zhp,char * options,int len)314eda14cbcSMatt Macy zfs_add_options(zfs_handle_t *zhp, char *options, int len)
315eda14cbcSMatt Macy {
316eda14cbcSMatt Macy 	int error = 0;
317eda14cbcSMatt Macy 
318eda14cbcSMatt Macy 	error = zfs_add_option(zhp, options, len,
319eda14cbcSMatt Macy 	    ZFS_PROP_ATIME, MNTOPT_ATIME, MNTOPT_NOATIME);
320eda14cbcSMatt Macy 	/*
321eda14cbcSMatt Macy 	 * don't add relatime/strictatime when atime=off, otherwise strictatime
322eda14cbcSMatt Macy 	 * will force atime=on
323eda14cbcSMatt Macy 	 */
324eda14cbcSMatt Macy 	if (strstr(options, MNTOPT_NOATIME) == NULL) {
325eda14cbcSMatt Macy 		error = zfs_add_option(zhp, options, len,
326eda14cbcSMatt Macy 		    ZFS_PROP_RELATIME, MNTOPT_RELATIME, MNTOPT_STRICTATIME);
327eda14cbcSMatt Macy 	}
328eda14cbcSMatt Macy 	error = error ? error : zfs_add_option(zhp, options, len,
329eda14cbcSMatt Macy 	    ZFS_PROP_DEVICES, MNTOPT_DEVICES, MNTOPT_NODEVICES);
330eda14cbcSMatt Macy 	error = error ? error : zfs_add_option(zhp, options, len,
331eda14cbcSMatt Macy 	    ZFS_PROP_EXEC, MNTOPT_EXEC, MNTOPT_NOEXEC);
332eda14cbcSMatt Macy 	error = error ? error : zfs_add_option(zhp, options, len,
333eda14cbcSMatt Macy 	    ZFS_PROP_READONLY, MNTOPT_RO, MNTOPT_RW);
334eda14cbcSMatt Macy 	error = error ? error : zfs_add_option(zhp, options, len,
335eda14cbcSMatt Macy 	    ZFS_PROP_SETUID, MNTOPT_SETUID, MNTOPT_NOSETUID);
336eda14cbcSMatt Macy 	error = error ? error : zfs_add_option(zhp, options, len,
337eda14cbcSMatt Macy 	    ZFS_PROP_NBMAND, MNTOPT_NBMAND, MNTOPT_NONBMAND);
338eda14cbcSMatt Macy 
339eda14cbcSMatt Macy 	return (error);
340eda14cbcSMatt Macy }
341eda14cbcSMatt Macy 
342eda14cbcSMatt Macy int
zfs_mount(zfs_handle_t * zhp,const char * options,int flags)343eda14cbcSMatt Macy zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
344eda14cbcSMatt Macy {
345eda14cbcSMatt Macy 	char mountpoint[ZFS_MAXPROPLEN];
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL,
348eda14cbcSMatt Macy 	    flags))
349eda14cbcSMatt Macy 		return (0);
350eda14cbcSMatt Macy 
351eda14cbcSMatt Macy 	return (zfs_mount_at(zhp, options, flags, mountpoint));
352eda14cbcSMatt Macy }
353eda14cbcSMatt Macy 
354eda14cbcSMatt Macy /*
355eda14cbcSMatt Macy  * Mount the given filesystem.
356eda14cbcSMatt Macy  */
357eda14cbcSMatt Macy int
zfs_mount_at(zfs_handle_t * zhp,const char * options,int flags,const char * mountpoint)358eda14cbcSMatt Macy zfs_mount_at(zfs_handle_t *zhp, const char *options, int flags,
359eda14cbcSMatt Macy     const char *mountpoint)
360eda14cbcSMatt Macy {
361eda14cbcSMatt Macy 	struct stat buf;
362eda14cbcSMatt Macy 	char mntopts[MNT_LINE_MAX];
363eda14cbcSMatt Macy 	char overlay[ZFS_MAXPROPLEN];
36416038816SMartin Matuska 	char prop_encroot[MAXNAMELEN];
36516038816SMartin Matuska 	boolean_t is_encroot;
36616038816SMartin Matuska 	zfs_handle_t *encroot_hp = zhp;
367eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
368eda14cbcSMatt Macy 	uint64_t keystatus;
369eda14cbcSMatt Macy 	int remount = 0, rc;
370eda14cbcSMatt Macy 
371eda14cbcSMatt Macy 	if (options == NULL) {
372eda14cbcSMatt Macy 		(void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts));
373eda14cbcSMatt Macy 	} else {
374eda14cbcSMatt Macy 		(void) strlcpy(mntopts, options, sizeof (mntopts));
375eda14cbcSMatt Macy 	}
376eda14cbcSMatt Macy 
377eda14cbcSMatt Macy 	if (strstr(mntopts, MNTOPT_REMOUNT) != NULL)
378eda14cbcSMatt Macy 		remount = 1;
379eda14cbcSMatt Macy 
380eda14cbcSMatt Macy 	/* Potentially duplicates some checks if invoked by zfs_mount(). */
381e92ffd9bSMartin Matuska 	if (!zfs_is_mountable_internal(zhp))
382eda14cbcSMatt Macy 		return (0);
383eda14cbcSMatt Macy 
384eda14cbcSMatt Macy 	/*
385eda14cbcSMatt Macy 	 * If the pool is imported read-only then all mounts must be read-only
386eda14cbcSMatt Macy 	 */
387eda14cbcSMatt Macy 	if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
388eda14cbcSMatt Macy 		(void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts));
389eda14cbcSMatt Macy 
390eda14cbcSMatt Macy 	/*
391eda14cbcSMatt Macy 	 * Append default mount options which apply to the mount point.
392eda14cbcSMatt Macy 	 * This is done because under Linux (unlike Solaris) multiple mount
393eda14cbcSMatt Macy 	 * points may reference a single super block.  This means that just
394eda14cbcSMatt Macy 	 * given a super block there is no back reference to update the per
395eda14cbcSMatt Macy 	 * mount point options.
396eda14cbcSMatt Macy 	 */
397eda14cbcSMatt Macy 	rc = zfs_add_options(zhp, mntopts, sizeof (mntopts));
398eda14cbcSMatt Macy 	if (rc) {
399eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
400eda14cbcSMatt Macy 		    "default options unavailable"));
401eda14cbcSMatt Macy 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
402eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
403eda14cbcSMatt Macy 		    mountpoint));
404eda14cbcSMatt Macy 	}
405eda14cbcSMatt Macy 
406eda14cbcSMatt Macy 	/*
407eda14cbcSMatt Macy 	 * If the filesystem is encrypted the key must be loaded  in order to
408eda14cbcSMatt Macy 	 * mount. If the key isn't loaded, the MS_CRYPT flag decides whether
409eda14cbcSMatt Macy 	 * or not we attempt to load the keys. Note: we must call
410eda14cbcSMatt Macy 	 * zfs_refresh_properties() here since some callers of this function
411eda14cbcSMatt Macy 	 * (most notably zpool_enable_datasets()) may implicitly load our key
412eda14cbcSMatt Macy 	 * by loading the parent's key first.
413eda14cbcSMatt Macy 	 */
414eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
415eda14cbcSMatt Macy 		zfs_refresh_properties(zhp);
416eda14cbcSMatt Macy 		keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
417eda14cbcSMatt Macy 
418eda14cbcSMatt Macy 		/*
419eda14cbcSMatt Macy 		 * If the key is unavailable and MS_CRYPT is set give the
420eda14cbcSMatt Macy 		 * user a chance to enter the key. Otherwise just fail
421eda14cbcSMatt Macy 		 * immediately.
422eda14cbcSMatt Macy 		 */
423eda14cbcSMatt Macy 		if (keystatus == ZFS_KEYSTATUS_UNAVAILABLE) {
424eda14cbcSMatt Macy 			if (flags & MS_CRYPT) {
42516038816SMartin Matuska 				rc = zfs_crypto_get_encryption_root(zhp,
42616038816SMartin Matuska 				    &is_encroot, prop_encroot);
42716038816SMartin Matuska 				if (rc) {
42816038816SMartin Matuska 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42916038816SMartin Matuska 					    "Failed to get encryption root for "
43016038816SMartin Matuska 					    "'%s'."), zfs_get_name(zhp));
43116038816SMartin Matuska 					return (rc);
43216038816SMartin Matuska 				}
43316038816SMartin Matuska 
43416038816SMartin Matuska 				if (!is_encroot) {
43516038816SMartin Matuska 					encroot_hp = zfs_open(hdl, prop_encroot,
43616038816SMartin Matuska 					    ZFS_TYPE_DATASET);
43716038816SMartin Matuska 					if (encroot_hp == NULL)
43816038816SMartin Matuska 						return (hdl->libzfs_error);
43916038816SMartin Matuska 				}
44016038816SMartin Matuska 
44116038816SMartin Matuska 				rc = zfs_crypto_load_key(encroot_hp,
44216038816SMartin Matuska 				    B_FALSE, NULL);
44316038816SMartin Matuska 
44416038816SMartin Matuska 				if (!is_encroot)
44516038816SMartin Matuska 					zfs_close(encroot_hp);
446eda14cbcSMatt Macy 				if (rc)
447eda14cbcSMatt Macy 					return (rc);
448eda14cbcSMatt Macy 			} else {
449eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
450eda14cbcSMatt Macy 				    "encryption key not loaded"));
451eda14cbcSMatt Macy 				return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
452eda14cbcSMatt Macy 				    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
453eda14cbcSMatt Macy 				    mountpoint));
454eda14cbcSMatt Macy 			}
455eda14cbcSMatt Macy 		}
456eda14cbcSMatt Macy 
457eda14cbcSMatt Macy 	}
458eda14cbcSMatt Macy 
459eda14cbcSMatt Macy 	/*
460eda14cbcSMatt Macy 	 * Append zfsutil option so the mount helper allow the mount
461eda14cbcSMatt Macy 	 */
462eda14cbcSMatt Macy 	strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts));
463eda14cbcSMatt Macy 
464eda14cbcSMatt Macy 	/* Create the directory if it doesn't already exist */
465eda14cbcSMatt Macy 	if (lstat(mountpoint, &buf) != 0) {
466eda14cbcSMatt Macy 		if (mkdirp(mountpoint, 0755) != 0) {
467eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
468eda14cbcSMatt Macy 			    "failed to create mountpoint: %s"),
469fd45b686SMartin Matuska 			    zfs_strerror(errno));
470eda14cbcSMatt Macy 			return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
471eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
472eda14cbcSMatt Macy 			    mountpoint));
473eda14cbcSMatt Macy 		}
474eda14cbcSMatt Macy 	}
475eda14cbcSMatt Macy 
476eda14cbcSMatt Macy 	/*
477eda14cbcSMatt Macy 	 * Overlay mounts are enabled by default but may be disabled
478eda14cbcSMatt Macy 	 * via the 'overlay' property. The -O flag remains for compatibility.
479eda14cbcSMatt Macy 	 */
480eda14cbcSMatt Macy 	if (!(flags & MS_OVERLAY)) {
481eda14cbcSMatt Macy 		if (zfs_prop_get(zhp, ZFS_PROP_OVERLAY, overlay,
482eda14cbcSMatt Macy 		    sizeof (overlay), NULL, NULL, 0, B_FALSE) == 0) {
483eda14cbcSMatt Macy 			if (strcmp(overlay, "on") == 0) {
484eda14cbcSMatt Macy 				flags |= MS_OVERLAY;
485eda14cbcSMatt Macy 			}
486eda14cbcSMatt Macy 		}
487eda14cbcSMatt Macy 	}
488eda14cbcSMatt Macy 
489eda14cbcSMatt Macy 	/*
490eda14cbcSMatt Macy 	 * Determine if the mountpoint is empty.  If so, refuse to perform the
491eda14cbcSMatt Macy 	 * mount.  We don't perform this check if 'remount' is
492eda14cbcSMatt Macy 	 * specified or if overlay option (-O) is given
493eda14cbcSMatt Macy 	 */
494eda14cbcSMatt Macy 	if ((flags & MS_OVERLAY) == 0 && !remount &&
495eda14cbcSMatt Macy 	    !dir_is_empty(mountpoint)) {
496eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
497eda14cbcSMatt Macy 		    "directory is not empty"));
498eda14cbcSMatt Macy 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
499eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
500eda14cbcSMatt Macy 	}
501eda14cbcSMatt Macy 
502eda14cbcSMatt Macy 	/* perform the mount */
503eda14cbcSMatt Macy 	rc = do_mount(zhp, mountpoint, mntopts, flags);
504eda14cbcSMatt Macy 	if (rc) {
505eda14cbcSMatt Macy 		/*
506eda14cbcSMatt Macy 		 * Generic errors are nasty, but there are just way too many
507eda14cbcSMatt Macy 		 * from mount(), and they're well-understood.  We pick a few
508eda14cbcSMatt Macy 		 * common ones to improve upon.
509eda14cbcSMatt Macy 		 */
510eda14cbcSMatt Macy 		if (rc == EBUSY) {
511eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
512eda14cbcSMatt Macy 			    "mountpoint or dataset is busy"));
513eda14cbcSMatt Macy 		} else if (rc == EPERM) {
514eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
515eda14cbcSMatt Macy 			    "Insufficient privileges"));
516eda14cbcSMatt Macy 		} else if (rc == ENOTSUP) {
517eda14cbcSMatt Macy 			int spa_version;
518eda14cbcSMatt Macy 
519eda14cbcSMatt Macy 			VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
52016038816SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
52116038816SMartin Matuska 			    "Can't mount a version %llu "
522eda14cbcSMatt Macy 			    "file system on a version %d pool. Pool must be"
523eda14cbcSMatt Macy 			    " upgraded to mount this file system."),
524eda14cbcSMatt Macy 			    (u_longlong_t)zfs_prop_get_int(zhp,
525eda14cbcSMatt Macy 			    ZFS_PROP_VERSION), spa_version);
526eda14cbcSMatt Macy 		} else {
527fd45b686SMartin Matuska 			zfs_error_aux(hdl, "%s", zfs_strerror(rc));
528eda14cbcSMatt Macy 		}
529eda14cbcSMatt Macy 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
530eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
531eda14cbcSMatt Macy 		    zhp->zfs_name));
532eda14cbcSMatt Macy 	}
533eda14cbcSMatt Macy 
534eda14cbcSMatt Macy 	/* remove the mounted entry before re-adding on remount */
535eda14cbcSMatt Macy 	if (remount)
536eda14cbcSMatt Macy 		libzfs_mnttab_remove(hdl, zhp->zfs_name);
537eda14cbcSMatt Macy 
538eda14cbcSMatt Macy 	/* add the mounted entry into our cache */
539eda14cbcSMatt Macy 	libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts);
540eda14cbcSMatt Macy 	return (0);
541eda14cbcSMatt Macy }
542eda14cbcSMatt Macy 
543eda14cbcSMatt Macy /*
544eda14cbcSMatt Macy  * Unmount a single filesystem.
545eda14cbcSMatt Macy  */
546eda14cbcSMatt Macy static int
unmount_one(zfs_handle_t * zhp,const char * mountpoint,int flags)5473f9d360cSMartin Matuska unmount_one(zfs_handle_t *zhp, const char *mountpoint, int flags)
548eda14cbcSMatt Macy {
549eda14cbcSMatt Macy 	int error;
550eda14cbcSMatt Macy 
5513f9d360cSMartin Matuska 	error = do_unmount(zhp, mountpoint, flags);
552eda14cbcSMatt Macy 	if (error != 0) {
5539db44a8eSMartin Matuska 		int libzfs_err;
5549db44a8eSMartin Matuska 
5559db44a8eSMartin Matuska 		switch (error) {
5569db44a8eSMartin Matuska 		case EBUSY:
5579db44a8eSMartin Matuska 			libzfs_err = EZFS_BUSY;
5589db44a8eSMartin Matuska 			break;
5599db44a8eSMartin Matuska 		case EIO:
5609db44a8eSMartin Matuska 			libzfs_err = EZFS_IO;
5619db44a8eSMartin Matuska 			break;
5629db44a8eSMartin Matuska 		case ENOENT:
5639db44a8eSMartin Matuska 			libzfs_err = EZFS_NOENT;
5649db44a8eSMartin Matuska 			break;
5659db44a8eSMartin Matuska 		case ENOMEM:
5669db44a8eSMartin Matuska 			libzfs_err = EZFS_NOMEM;
5679db44a8eSMartin Matuska 			break;
5689db44a8eSMartin Matuska 		case EPERM:
5699db44a8eSMartin Matuska 			libzfs_err = EZFS_PERM;
5709db44a8eSMartin Matuska 			break;
5719db44a8eSMartin Matuska 		default:
5729db44a8eSMartin Matuska 			libzfs_err = EZFS_UMOUNTFAILED;
5739db44a8eSMartin Matuska 		}
574681ce946SMartin Matuska 		if (zhp) {
5753f9d360cSMartin Matuska 			return (zfs_error_fmt(zhp->zfs_hdl, libzfs_err,
576eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
577eda14cbcSMatt Macy 			    mountpoint));
578681ce946SMartin Matuska 		} else {
579681ce946SMartin Matuska 			return (-1);
580681ce946SMartin Matuska 		}
581eda14cbcSMatt Macy 	}
582eda14cbcSMatt Macy 
583eda14cbcSMatt Macy 	return (0);
584eda14cbcSMatt Macy }
585eda14cbcSMatt Macy 
586eda14cbcSMatt Macy /*
587eda14cbcSMatt Macy  * Unmount the given filesystem.
588eda14cbcSMatt Macy  */
589eda14cbcSMatt Macy int
zfs_unmount(zfs_handle_t * zhp,const char * mountpoint,int flags)590eda14cbcSMatt Macy zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
591eda14cbcSMatt Macy {
592eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
593eda14cbcSMatt Macy 	struct mnttab entry;
594eda14cbcSMatt Macy 	char *mntpt = NULL;
595eda14cbcSMatt Macy 	boolean_t encroot, unmounted = B_FALSE;
596eda14cbcSMatt Macy 
597eda14cbcSMatt Macy 	/* check to see if we need to unmount the filesystem */
598eda14cbcSMatt Macy 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
599eda14cbcSMatt Macy 	    libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
600eda14cbcSMatt Macy 		/*
601eda14cbcSMatt Macy 		 * mountpoint may have come from a call to
602eda14cbcSMatt Macy 		 * getmnt/getmntany if it isn't NULL. If it is NULL,
603eda14cbcSMatt Macy 		 * we know it comes from libzfs_mnttab_find which can
604eda14cbcSMatt Macy 		 * then get freed later. We strdup it to play it safe.
605eda14cbcSMatt Macy 		 */
606eda14cbcSMatt Macy 		if (mountpoint == NULL)
607eda14cbcSMatt Macy 			mntpt = zfs_strdup(hdl, entry.mnt_mountp);
608eda14cbcSMatt Macy 		else
609eda14cbcSMatt Macy 			mntpt = zfs_strdup(hdl, mountpoint);
610eda14cbcSMatt Macy 
611eda14cbcSMatt Macy 		/*
612eda14cbcSMatt Macy 		 * Unshare and unmount the filesystem
613eda14cbcSMatt Macy 		 */
614716fd348SMartin Matuska 		if (zfs_unshare(zhp, mntpt, share_all_proto) != 0) {
615eda14cbcSMatt Macy 			free(mntpt);
616eda14cbcSMatt Macy 			return (-1);
617eda14cbcSMatt Macy 		}
618716fd348SMartin Matuska 		zfs_commit_shares(NULL);
619eda14cbcSMatt Macy 
6203f9d360cSMartin Matuska 		if (unmount_one(zhp, mntpt, flags) != 0) {
621eda14cbcSMatt Macy 			free(mntpt);
622716fd348SMartin Matuska 			(void) zfs_share(zhp, NULL);
623716fd348SMartin Matuska 			zfs_commit_shares(NULL);
624eda14cbcSMatt Macy 			return (-1);
625eda14cbcSMatt Macy 		}
626eda14cbcSMatt Macy 
627eda14cbcSMatt Macy 		libzfs_mnttab_remove(hdl, zhp->zfs_name);
628eda14cbcSMatt Macy 		free(mntpt);
629eda14cbcSMatt Macy 		unmounted = B_TRUE;
630eda14cbcSMatt Macy 	}
631eda14cbcSMatt Macy 
632eda14cbcSMatt Macy 	/*
633eda14cbcSMatt Macy 	 * If the MS_CRYPT flag is provided we must ensure we attempt to
634eda14cbcSMatt Macy 	 * unload the dataset's key regardless of whether we did any work
635eda14cbcSMatt Macy 	 * to unmount it. We only do this for encryption roots.
636eda14cbcSMatt Macy 	 */
637eda14cbcSMatt Macy 	if ((flags & MS_CRYPT) != 0 &&
638eda14cbcSMatt Macy 	    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
639eda14cbcSMatt Macy 		zfs_refresh_properties(zhp);
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy 		if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0 &&
642eda14cbcSMatt Macy 		    unmounted) {
643eda14cbcSMatt Macy 			(void) zfs_mount(zhp, NULL, 0);
644eda14cbcSMatt Macy 			return (-1);
645eda14cbcSMatt Macy 		}
646eda14cbcSMatt Macy 
647eda14cbcSMatt Macy 		if (encroot && zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
648eda14cbcSMatt Macy 		    ZFS_KEYSTATUS_AVAILABLE &&
649eda14cbcSMatt Macy 		    zfs_crypto_unload_key(zhp) != 0) {
650eda14cbcSMatt Macy 			(void) zfs_mount(zhp, NULL, 0);
651eda14cbcSMatt Macy 			return (-1);
652eda14cbcSMatt Macy 		}
653eda14cbcSMatt Macy 	}
654eda14cbcSMatt Macy 
65553b70c86SMartin Matuska 	zpool_disable_volume_os(zhp->zfs_name);
65653b70c86SMartin Matuska 
657eda14cbcSMatt Macy 	return (0);
658eda14cbcSMatt Macy }
659eda14cbcSMatt Macy 
660eda14cbcSMatt Macy /*
661eda14cbcSMatt Macy  * Unmount this filesystem and any children inheriting the mountpoint property.
662eda14cbcSMatt Macy  * To do this, just act like we're changing the mountpoint property, but don't
663eda14cbcSMatt Macy  * remount the filesystems afterwards.
664eda14cbcSMatt Macy  */
665eda14cbcSMatt Macy int
zfs_unmountall(zfs_handle_t * zhp,int flags)666eda14cbcSMatt Macy zfs_unmountall(zfs_handle_t *zhp, int flags)
667eda14cbcSMatt Macy {
668eda14cbcSMatt Macy 	prop_changelist_t *clp;
669eda14cbcSMatt Macy 	int ret;
670eda14cbcSMatt Macy 
671eda14cbcSMatt Macy 	clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
672eda14cbcSMatt Macy 	    CL_GATHER_ITER_MOUNTED, flags);
673eda14cbcSMatt Macy 	if (clp == NULL)
674eda14cbcSMatt Macy 		return (-1);
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy 	ret = changelist_prefix(clp);
677eda14cbcSMatt Macy 	changelist_free(clp);
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy 	return (ret);
680eda14cbcSMatt Macy }
681eda14cbcSMatt Macy 
682eda14cbcSMatt Macy /*
683eda14cbcSMatt Macy  * Unshare a filesystem by mountpoint.
684eda14cbcSMatt Macy  */
685716fd348SMartin Matuska static int
unshare_one(libzfs_handle_t * hdl,const char * name,const char * mountpoint,enum sa_protocol proto)686eda14cbcSMatt Macy unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
687716fd348SMartin Matuska     enum sa_protocol proto)
688eda14cbcSMatt Macy {
689716fd348SMartin Matuska 	int err = sa_disable_share(mountpoint, proto);
690716fd348SMartin Matuska 	if (err != SA_OK)
691eda14cbcSMatt Macy 		return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
692eda14cbcSMatt Macy 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
693eda14cbcSMatt Macy 		    name, sa_errorstr(err)));
694eda14cbcSMatt Macy 
695716fd348SMartin Matuska 	return (0);
696eda14cbcSMatt Macy }
697eda14cbcSMatt Macy 
698eda14cbcSMatt Macy /*
699eda14cbcSMatt Macy  * Share the given filesystem according to the options in the specified
700eda14cbcSMatt Macy  * protocol specific properties (sharenfs, sharesmb).  We rely
701eda14cbcSMatt Macy  * on "libshare" to do the dirty work for us.
702eda14cbcSMatt Macy  */
703eda14cbcSMatt Macy int
zfs_share(zfs_handle_t * zhp,const enum sa_protocol * proto)704716fd348SMartin Matuska zfs_share(zfs_handle_t *zhp, const enum sa_protocol *proto)
705eda14cbcSMatt Macy {
706eda14cbcSMatt Macy 	char mountpoint[ZFS_MAXPROPLEN];
707eda14cbcSMatt Macy 	char shareopts[ZFS_MAXPROPLEN];
708eda14cbcSMatt Macy 	char sourcestr[ZFS_MAXPROPLEN];
709716fd348SMartin Matuska 	const enum sa_protocol *curr_proto;
710eda14cbcSMatt Macy 	zprop_source_t sourcetype;
711eda14cbcSMatt Macy 	int err = 0;
712eda14cbcSMatt Macy 
713716fd348SMartin Matuska 	if (proto == NULL)
714716fd348SMartin Matuska 		proto = share_all_proto;
715716fd348SMartin Matuska 
716eda14cbcSMatt Macy 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL, 0))
717eda14cbcSMatt Macy 		return (0);
718eda14cbcSMatt Macy 
719716fd348SMartin Matuska 	for (curr_proto = proto; *curr_proto != SA_NO_PROTOCOL; curr_proto++) {
720eda14cbcSMatt Macy 		/*
721eda14cbcSMatt Macy 		 * Return success if there are no share options.
722eda14cbcSMatt Macy 		 */
723eda14cbcSMatt Macy 		if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
724eda14cbcSMatt Macy 		    shareopts, sizeof (shareopts), &sourcetype, sourcestr,
725eda14cbcSMatt Macy 		    ZFS_MAXPROPLEN, B_FALSE) != 0 ||
726eda14cbcSMatt Macy 		    strcmp(shareopts, "off") == 0)
727eda14cbcSMatt Macy 			continue;
728eda14cbcSMatt Macy 
729eda14cbcSMatt Macy 		/*
730eda14cbcSMatt Macy 		 * If the 'zoned' property is set, then zfs_is_mountable()
731eda14cbcSMatt Macy 		 * will have already bailed out if we are in the global zone.
732eda14cbcSMatt Macy 		 * But local zones cannot be NFS servers, so we ignore it for
733eda14cbcSMatt Macy 		 * local zones as well.
734eda14cbcSMatt Macy 		 */
735eda14cbcSMatt Macy 		if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
736eda14cbcSMatt Macy 			continue;
737eda14cbcSMatt Macy 
738eda14cbcSMatt Macy 		err = sa_enable_share(zfs_get_name(zhp), mountpoint, shareopts,
739716fd348SMartin Matuska 		    *curr_proto);
740eda14cbcSMatt Macy 		if (err != SA_OK) {
741eda14cbcSMatt Macy 			return (zfs_error_fmt(zhp->zfs_hdl,
742eda14cbcSMatt Macy 			    proto_table[*curr_proto].p_share_err,
743eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot share '%s: %s'"),
744eda14cbcSMatt Macy 			    zfs_get_name(zhp), sa_errorstr(err)));
745eda14cbcSMatt Macy 		}
746eda14cbcSMatt Macy 
747eda14cbcSMatt Macy 	}
748eda14cbcSMatt Macy 	return (0);
749eda14cbcSMatt Macy }
750eda14cbcSMatt Macy 
751eda14cbcSMatt Macy /*
752eda14cbcSMatt Macy  * Check to see if the filesystem is currently shared.
753eda14cbcSMatt Macy  */
754716fd348SMartin Matuska boolean_t
zfs_is_shared(zfs_handle_t * zhp,char ** where,const enum sa_protocol * proto)755716fd348SMartin Matuska zfs_is_shared(zfs_handle_t *zhp, char **where,
756716fd348SMartin Matuska     const enum sa_protocol *proto)
757eda14cbcSMatt Macy {
758eda14cbcSMatt Macy 	char *mountpoint;
759716fd348SMartin Matuska 	if (proto == NULL)
760716fd348SMartin Matuska 		proto = share_all_proto;
761716fd348SMartin Matuska 
762716fd348SMartin Matuska 	if (ZFS_IS_VOLUME(zhp))
763716fd348SMartin Matuska 		return (B_FALSE);
764eda14cbcSMatt Macy 
765eda14cbcSMatt Macy 	if (!zfs_is_mounted(zhp, &mountpoint))
766716fd348SMartin Matuska 		return (B_FALSE);
767eda14cbcSMatt Macy 
768716fd348SMartin Matuska 	for (const enum sa_protocol *p = proto; *p != SA_NO_PROTOCOL; ++p)
769716fd348SMartin Matuska 		if (sa_is_shared(mountpoint, *p)) {
770eda14cbcSMatt Macy 			if (where != NULL)
771eda14cbcSMatt Macy 				*where = mountpoint;
772eda14cbcSMatt Macy 			else
773eda14cbcSMatt Macy 				free(mountpoint);
774716fd348SMartin Matuska 			return (B_TRUE);
775716fd348SMartin Matuska 		}
776716fd348SMartin Matuska 
777eda14cbcSMatt Macy 	free(mountpoint);
778716fd348SMartin Matuska 	return (B_FALSE);
779eda14cbcSMatt Macy }
780eda14cbcSMatt Macy 
781eda14cbcSMatt Macy void
zfs_commit_shares(const enum sa_protocol * proto)782716fd348SMartin Matuska zfs_commit_shares(const enum sa_protocol *proto)
783eda14cbcSMatt Macy {
784eda14cbcSMatt Macy 	if (proto == NULL)
785716fd348SMartin Matuska 		proto = share_all_proto;
786eda14cbcSMatt Macy 
787716fd348SMartin Matuska 	for (const enum sa_protocol *p = proto; *p != SA_NO_PROTOCOL; ++p)
788716fd348SMartin Matuska 		sa_commit_shares(*p);
789eda14cbcSMatt Macy }
790eda14cbcSMatt Macy 
791c7046f76SMartin Matuska void
zfs_truncate_shares(const enum sa_protocol * proto)792c7046f76SMartin Matuska zfs_truncate_shares(const enum sa_protocol *proto)
793c7046f76SMartin Matuska {
794c7046f76SMartin Matuska 	if (proto == NULL)
795c7046f76SMartin Matuska 		proto = share_all_proto;
796c7046f76SMartin Matuska 
797c7046f76SMartin Matuska 	for (const enum sa_protocol *p = proto; *p != SA_NO_PROTOCOL; ++p)
798c7046f76SMartin Matuska 		sa_truncate_shares(*p);
799c7046f76SMartin Matuska }
800c7046f76SMartin Matuska 
801eda14cbcSMatt Macy /*
802eda14cbcSMatt Macy  * Unshare the given filesystem.
803eda14cbcSMatt Macy  */
804eda14cbcSMatt Macy int
zfs_unshare(zfs_handle_t * zhp,const char * mountpoint,const enum sa_protocol * proto)805716fd348SMartin Matuska zfs_unshare(zfs_handle_t *zhp, const char *mountpoint,
806716fd348SMartin Matuska     const enum sa_protocol *proto)
807eda14cbcSMatt Macy {
808eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
809eda14cbcSMatt Macy 	struct mnttab entry;
810eda14cbcSMatt Macy 
811716fd348SMartin Matuska 	if (proto == NULL)
812716fd348SMartin Matuska 		proto = share_all_proto;
813eda14cbcSMatt Macy 
814eda14cbcSMatt Macy 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
815eda14cbcSMatt Macy 	    libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
816eda14cbcSMatt Macy 
817716fd348SMartin Matuska 		/* check to see if need to unmount the filesystem */
818716fd348SMartin Matuska 		const char *mntpt = mountpoint ?: entry.mnt_mountp;
819eda14cbcSMatt Macy 
820716fd348SMartin Matuska 		for (const enum sa_protocol *curr_proto = proto;
821716fd348SMartin Matuska 		    *curr_proto != SA_NO_PROTOCOL; curr_proto++)
822716fd348SMartin Matuska 			if (sa_is_shared(mntpt, *curr_proto) &&
823716fd348SMartin Matuska 			    unshare_one(hdl, zhp->zfs_name,
824716fd348SMartin Matuska 			    mntpt, *curr_proto) != 0)
825eda14cbcSMatt Macy 					return (-1);
826eda14cbcSMatt Macy 	}
827eda14cbcSMatt Macy 
828eda14cbcSMatt Macy 	return (0);
829eda14cbcSMatt Macy }
830eda14cbcSMatt Macy 
831eda14cbcSMatt Macy /*
832eda14cbcSMatt Macy  * Same as zfs_unmountall(), but for NFS and SMB unshares.
833eda14cbcSMatt Macy  */
834716fd348SMartin Matuska int
zfs_unshareall(zfs_handle_t * zhp,const enum sa_protocol * proto)835716fd348SMartin Matuska zfs_unshareall(zfs_handle_t *zhp, const enum sa_protocol *proto)
836eda14cbcSMatt Macy {
837eda14cbcSMatt Macy 	prop_changelist_t *clp;
838eda14cbcSMatt Macy 	int ret;
839eda14cbcSMatt Macy 
840716fd348SMartin Matuska 	if (proto == NULL)
841716fd348SMartin Matuska 		proto = share_all_proto;
842716fd348SMartin Matuska 
843eda14cbcSMatt Macy 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
844eda14cbcSMatt Macy 	if (clp == NULL)
845eda14cbcSMatt Macy 		return (-1);
846eda14cbcSMatt Macy 
847eda14cbcSMatt Macy 	ret = changelist_unshare(clp, proto);
848eda14cbcSMatt Macy 	changelist_free(clp);
849eda14cbcSMatt Macy 
850eda14cbcSMatt Macy 	return (ret);
851eda14cbcSMatt Macy }
852eda14cbcSMatt Macy 
853eda14cbcSMatt Macy /*
854eda14cbcSMatt Macy  * Remove the mountpoint associated with the current dataset, if necessary.
855eda14cbcSMatt Macy  * We only remove the underlying directory if:
856eda14cbcSMatt Macy  *
857eda14cbcSMatt Macy  *	- The mountpoint is not 'none' or 'legacy'
858eda14cbcSMatt Macy  *	- The mountpoint is non-empty
859eda14cbcSMatt Macy  *	- The mountpoint is the default or inherited
860eda14cbcSMatt Macy  *	- The 'zoned' property is set, or we're in a local zone
861eda14cbcSMatt Macy  *
862eda14cbcSMatt Macy  * Any other directories we leave alone.
863eda14cbcSMatt Macy  */
864eda14cbcSMatt Macy void
remove_mountpoint(zfs_handle_t * zhp)865eda14cbcSMatt Macy remove_mountpoint(zfs_handle_t *zhp)
866eda14cbcSMatt Macy {
867eda14cbcSMatt Macy 	char mountpoint[ZFS_MAXPROPLEN];
868eda14cbcSMatt Macy 	zprop_source_t source;
869eda14cbcSMatt Macy 
870eda14cbcSMatt Macy 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
871eda14cbcSMatt Macy 	    &source, 0))
872eda14cbcSMatt Macy 		return;
873eda14cbcSMatt Macy 
874eda14cbcSMatt Macy 	if (source == ZPROP_SRC_DEFAULT ||
875eda14cbcSMatt Macy 	    source == ZPROP_SRC_INHERITED) {
876eda14cbcSMatt Macy 		/*
877eda14cbcSMatt Macy 		 * Try to remove the directory, silently ignoring any errors.
878eda14cbcSMatt Macy 		 * The filesystem may have since been removed or moved around,
879eda14cbcSMatt Macy 		 * and this error isn't really useful to the administrator in
880eda14cbcSMatt Macy 		 * any way.
881eda14cbcSMatt Macy 		 */
882eda14cbcSMatt Macy 		(void) rmdir(mountpoint);
883eda14cbcSMatt Macy 	}
884eda14cbcSMatt Macy }
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy /*
887eda14cbcSMatt Macy  * Add the given zfs handle to the cb_handles array, dynamically reallocating
888eda14cbcSMatt Macy  * the array if it is out of space.
889eda14cbcSMatt Macy  */
890eda14cbcSMatt Macy void
libzfs_add_handle(get_all_cb_t * cbp,zfs_handle_t * zhp)891eda14cbcSMatt Macy libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
892eda14cbcSMatt Macy {
893eda14cbcSMatt Macy 	if (cbp->cb_alloc == cbp->cb_used) {
894eda14cbcSMatt Macy 		size_t newsz;
895eda14cbcSMatt Macy 		zfs_handle_t **newhandles;
896eda14cbcSMatt Macy 
897eda14cbcSMatt Macy 		newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64;
898eda14cbcSMatt Macy 		newhandles = zfs_realloc(zhp->zfs_hdl,
899eda14cbcSMatt Macy 		    cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *),
900eda14cbcSMatt Macy 		    newsz * sizeof (zfs_handle_t *));
901eda14cbcSMatt Macy 		cbp->cb_handles = newhandles;
902eda14cbcSMatt Macy 		cbp->cb_alloc = newsz;
903eda14cbcSMatt Macy 	}
904eda14cbcSMatt Macy 	cbp->cb_handles[cbp->cb_used++] = zhp;
905eda14cbcSMatt Macy }
906eda14cbcSMatt Macy 
907eda14cbcSMatt Macy /*
908eda14cbcSMatt Macy  * Recursive helper function used during file system enumeration
909eda14cbcSMatt Macy  */
910eda14cbcSMatt Macy static int
zfs_iter_cb(zfs_handle_t * zhp,void * data)911eda14cbcSMatt Macy zfs_iter_cb(zfs_handle_t *zhp, void *data)
912eda14cbcSMatt Macy {
913eda14cbcSMatt Macy 	get_all_cb_t *cbp = data;
914eda14cbcSMatt Macy 
915eda14cbcSMatt Macy 	if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
916eda14cbcSMatt Macy 		zfs_close(zhp);
917eda14cbcSMatt Macy 		return (0);
918eda14cbcSMatt Macy 	}
919eda14cbcSMatt Macy 
920eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
921eda14cbcSMatt Macy 		zfs_close(zhp);
922eda14cbcSMatt Macy 		return (0);
923eda14cbcSMatt Macy 	}
924eda14cbcSMatt Macy 
925eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
926eda14cbcSMatt Macy 	    ZFS_KEYSTATUS_UNAVAILABLE) {
927eda14cbcSMatt Macy 		zfs_close(zhp);
928eda14cbcSMatt Macy 		return (0);
929eda14cbcSMatt Macy 	}
930eda14cbcSMatt Macy 
931eda14cbcSMatt Macy 	/*
932eda14cbcSMatt Macy 	 * If this filesystem is inconsistent and has a receive resume
933eda14cbcSMatt Macy 	 * token, we can not mount it.
934eda14cbcSMatt Macy 	 */
935eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
936eda14cbcSMatt Macy 	    zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
937eda14cbcSMatt Macy 	    NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
938eda14cbcSMatt Macy 		zfs_close(zhp);
939eda14cbcSMatt Macy 		return (0);
940eda14cbcSMatt Macy 	}
941eda14cbcSMatt Macy 
942eda14cbcSMatt Macy 	libzfs_add_handle(cbp, zhp);
943d411c1d6SMartin Matuska 	if (zfs_iter_filesystems_v2(zhp, 0, zfs_iter_cb, cbp) != 0) {
944eda14cbcSMatt Macy 		zfs_close(zhp);
945eda14cbcSMatt Macy 		return (-1);
946eda14cbcSMatt Macy 	}
947eda14cbcSMatt Macy 	return (0);
948eda14cbcSMatt Macy }
949eda14cbcSMatt Macy 
950eda14cbcSMatt Macy /*
951eda14cbcSMatt Macy  * Sort comparator that compares two mountpoint paths. We sort these paths so
952eda14cbcSMatt Macy  * that subdirectories immediately follow their parents. This means that we
953eda14cbcSMatt Macy  * effectively treat the '/' character as the lowest value non-nul char.
954eda14cbcSMatt Macy  * Since filesystems from non-global zones can have the same mountpoint
955eda14cbcSMatt Macy  * as other filesystems, the comparator sorts global zone filesystems to
956eda14cbcSMatt Macy  * the top of the list. This means that the global zone will traverse the
957eda14cbcSMatt Macy  * filesystem list in the correct order and can stop when it sees the
958eda14cbcSMatt Macy  * first zoned filesystem. In a non-global zone, only the delegated
959eda14cbcSMatt Macy  * filesystems are seen.
960eda14cbcSMatt Macy  *
961eda14cbcSMatt Macy  * An example sorted list using this comparator would look like:
962eda14cbcSMatt Macy  *
963eda14cbcSMatt Macy  * /foo
964eda14cbcSMatt Macy  * /foo/bar
965eda14cbcSMatt Macy  * /foo/bar/baz
966eda14cbcSMatt Macy  * /foo/baz
967eda14cbcSMatt Macy  * /foo.bar
968eda14cbcSMatt Macy  * /foo (NGZ1)
969eda14cbcSMatt Macy  * /foo (NGZ2)
970eda14cbcSMatt Macy  *
971eda14cbcSMatt Macy  * The mounting code depends on this ordering to deterministically iterate
972eda14cbcSMatt Macy  * over filesystems in order to spawn parallel mount tasks.
973eda14cbcSMatt Macy  */
974eda14cbcSMatt Macy static int
mountpoint_cmp(const void * arga,const void * argb)975eda14cbcSMatt Macy mountpoint_cmp(const void *arga, const void *argb)
976eda14cbcSMatt Macy {
977eda14cbcSMatt Macy 	zfs_handle_t *const *zap = arga;
978eda14cbcSMatt Macy 	zfs_handle_t *za = *zap;
979eda14cbcSMatt Macy 	zfs_handle_t *const *zbp = argb;
980eda14cbcSMatt Macy 	zfs_handle_t *zb = *zbp;
981eda14cbcSMatt Macy 	char mounta[MAXPATHLEN];
982eda14cbcSMatt Macy 	char mountb[MAXPATHLEN];
983eda14cbcSMatt Macy 	const char *a = mounta;
984eda14cbcSMatt Macy 	const char *b = mountb;
985eda14cbcSMatt Macy 	boolean_t gota, gotb;
986eda14cbcSMatt Macy 	uint64_t zoneda, zonedb;
987eda14cbcSMatt Macy 
988eda14cbcSMatt Macy 	zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED);
989eda14cbcSMatt Macy 	zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED);
990eda14cbcSMatt Macy 	if (zoneda && !zonedb)
991eda14cbcSMatt Macy 		return (1);
992eda14cbcSMatt Macy 	if (!zoneda && zonedb)
993eda14cbcSMatt Macy 		return (-1);
994eda14cbcSMatt Macy 
995eda14cbcSMatt Macy 	gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM);
996eda14cbcSMatt Macy 	if (gota) {
997eda14cbcSMatt Macy 		verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta,
998eda14cbcSMatt Macy 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
999eda14cbcSMatt Macy 	}
1000eda14cbcSMatt Macy 	gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM);
1001eda14cbcSMatt Macy 	if (gotb) {
1002eda14cbcSMatt Macy 		verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb,
1003eda14cbcSMatt Macy 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1004eda14cbcSMatt Macy 	}
1005eda14cbcSMatt Macy 
1006eda14cbcSMatt Macy 	if (gota && gotb) {
1007eda14cbcSMatt Macy 		while (*a != '\0' && (*a == *b)) {
1008eda14cbcSMatt Macy 			a++;
1009eda14cbcSMatt Macy 			b++;
1010eda14cbcSMatt Macy 		}
1011eda14cbcSMatt Macy 		if (*a == *b)
1012eda14cbcSMatt Macy 			return (0);
1013eda14cbcSMatt Macy 		if (*a == '\0')
1014eda14cbcSMatt Macy 			return (-1);
1015eda14cbcSMatt Macy 		if (*b == '\0')
1016eda14cbcSMatt Macy 			return (1);
1017eda14cbcSMatt Macy 		if (*a == '/')
1018eda14cbcSMatt Macy 			return (-1);
1019eda14cbcSMatt Macy 		if (*b == '/')
1020eda14cbcSMatt Macy 			return (1);
1021eda14cbcSMatt Macy 		return (*a < *b ? -1 : *a > *b);
1022eda14cbcSMatt Macy 	}
1023eda14cbcSMatt Macy 
1024eda14cbcSMatt Macy 	if (gota)
1025eda14cbcSMatt Macy 		return (-1);
1026eda14cbcSMatt Macy 	if (gotb)
1027eda14cbcSMatt Macy 		return (1);
1028eda14cbcSMatt Macy 
1029eda14cbcSMatt Macy 	/*
1030eda14cbcSMatt Macy 	 * If neither filesystem has a mountpoint, revert to sorting by
1031eda14cbcSMatt Macy 	 * dataset name.
1032eda14cbcSMatt Macy 	 */
1033eda14cbcSMatt Macy 	return (strcmp(zfs_get_name(za), zfs_get_name(zb)));
1034eda14cbcSMatt Macy }
1035eda14cbcSMatt Macy 
1036eda14cbcSMatt Macy /*
1037eda14cbcSMatt Macy  * Return true if path2 is a child of path1 or path2 equals path1 or
1038eda14cbcSMatt Macy  * path1 is "/" (path2 is always a child of "/").
1039eda14cbcSMatt Macy  */
1040eda14cbcSMatt Macy static boolean_t
libzfs_path_contains(const char * path1,const char * path2)1041eda14cbcSMatt Macy libzfs_path_contains(const char *path1, const char *path2)
1042eda14cbcSMatt Macy {
1043eda14cbcSMatt Macy 	return (strcmp(path1, path2) == 0 || strcmp(path1, "/") == 0 ||
1044eda14cbcSMatt Macy 	    (strstr(path2, path1) == path2 && path2[strlen(path1)] == '/'));
1045eda14cbcSMatt Macy }
1046eda14cbcSMatt Macy 
1047eda14cbcSMatt Macy /*
1048eda14cbcSMatt Macy  * Given a mountpoint specified by idx in the handles array, find the first
1049eda14cbcSMatt Macy  * non-descendent of that mountpoint and return its index. Descendant paths
1050eda14cbcSMatt Macy  * start with the parent's path. This function relies on the ordering
1051eda14cbcSMatt Macy  * enforced by mountpoint_cmp().
1052eda14cbcSMatt Macy  */
1053eda14cbcSMatt Macy static int
non_descendant_idx(zfs_handle_t ** handles,size_t num_handles,int idx)1054eda14cbcSMatt Macy non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx)
1055eda14cbcSMatt Macy {
1056eda14cbcSMatt Macy 	char parent[ZFS_MAXPROPLEN];
1057eda14cbcSMatt Macy 	char child[ZFS_MAXPROPLEN];
1058eda14cbcSMatt Macy 	int i;
1059eda14cbcSMatt Macy 
1060eda14cbcSMatt Macy 	verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent,
1061eda14cbcSMatt Macy 	    sizeof (parent), NULL, NULL, 0, B_FALSE) == 0);
1062eda14cbcSMatt Macy 
1063eda14cbcSMatt Macy 	for (i = idx + 1; i < num_handles; i++) {
1064eda14cbcSMatt Macy 		verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child,
1065eda14cbcSMatt Macy 		    sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1066eda14cbcSMatt Macy 		if (!libzfs_path_contains(parent, child))
1067eda14cbcSMatt Macy 			break;
1068eda14cbcSMatt Macy 	}
1069eda14cbcSMatt Macy 	return (i);
1070eda14cbcSMatt Macy }
1071eda14cbcSMatt Macy 
1072eda14cbcSMatt Macy typedef struct mnt_param {
1073eda14cbcSMatt Macy 	libzfs_handle_t	*mnt_hdl;
1074eda14cbcSMatt Macy 	tpool_t		*mnt_tp;
1075eda14cbcSMatt Macy 	zfs_handle_t	**mnt_zhps; /* filesystems to mount */
1076eda14cbcSMatt Macy 	size_t		mnt_num_handles;
1077eda14cbcSMatt Macy 	int		mnt_idx;	/* Index of selected entry to mount */
1078eda14cbcSMatt Macy 	zfs_iter_f	mnt_func;
1079eda14cbcSMatt Macy 	void		*mnt_data;
1080eda14cbcSMatt Macy } mnt_param_t;
1081eda14cbcSMatt Macy 
1082eda14cbcSMatt Macy /*
1083eda14cbcSMatt Macy  * Allocate and populate the parameter struct for mount function, and
1084eda14cbcSMatt Macy  * schedule mounting of the entry selected by idx.
1085eda14cbcSMatt Macy  */
1086eda14cbcSMatt Macy static void
zfs_dispatch_mount(libzfs_handle_t * hdl,zfs_handle_t ** handles,size_t num_handles,int idx,zfs_iter_f func,void * data,tpool_t * tp)1087eda14cbcSMatt Macy zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles,
1088eda14cbcSMatt Macy     size_t num_handles, int idx, zfs_iter_f func, void *data, tpool_t *tp)
1089eda14cbcSMatt Macy {
1090eda14cbcSMatt Macy 	mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t));
1091eda14cbcSMatt Macy 
1092eda14cbcSMatt Macy 	mnt_param->mnt_hdl = hdl;
1093eda14cbcSMatt Macy 	mnt_param->mnt_tp = tp;
1094eda14cbcSMatt Macy 	mnt_param->mnt_zhps = handles;
1095eda14cbcSMatt Macy 	mnt_param->mnt_num_handles = num_handles;
1096eda14cbcSMatt Macy 	mnt_param->mnt_idx = idx;
1097eda14cbcSMatt Macy 	mnt_param->mnt_func = func;
1098eda14cbcSMatt Macy 	mnt_param->mnt_data = data;
1099eda14cbcSMatt Macy 
1100aca928a5SMartin Matuska 	if (tpool_dispatch(tp, zfs_mount_task, (void*)mnt_param)) {
1101aca928a5SMartin Matuska 		/* Could not dispatch to thread pool; execute directly */
1102aca928a5SMartin Matuska 		zfs_mount_task((void*)mnt_param);
1103aca928a5SMartin Matuska 	}
1104eda14cbcSMatt Macy }
1105eda14cbcSMatt Macy 
1106eda14cbcSMatt Macy /*
1107eda14cbcSMatt Macy  * This is the structure used to keep state of mounting or sharing operations
1108eda14cbcSMatt Macy  * during a call to zpool_enable_datasets().
1109eda14cbcSMatt Macy  */
1110eda14cbcSMatt Macy typedef struct mount_state {
1111eda14cbcSMatt Macy 	/*
1112eda14cbcSMatt Macy 	 * ms_mntstatus is set to -1 if any mount fails. While multiple threads
1113eda14cbcSMatt Macy 	 * could update this variable concurrently, no synchronization is
1114eda14cbcSMatt Macy 	 * needed as it's only ever set to -1.
1115eda14cbcSMatt Macy 	 */
1116eda14cbcSMatt Macy 	int		ms_mntstatus;
1117eda14cbcSMatt Macy 	int		ms_mntflags;
1118eda14cbcSMatt Macy 	const char	*ms_mntopts;
1119eda14cbcSMatt Macy } mount_state_t;
1120eda14cbcSMatt Macy 
1121eda14cbcSMatt Macy static int
zfs_mount_one(zfs_handle_t * zhp,void * arg)1122eda14cbcSMatt Macy zfs_mount_one(zfs_handle_t *zhp, void *arg)
1123eda14cbcSMatt Macy {
1124eda14cbcSMatt Macy 	mount_state_t *ms = arg;
1125eda14cbcSMatt Macy 	int ret = 0;
1126eda14cbcSMatt Macy 
1127eda14cbcSMatt Macy 	/*
1128eda14cbcSMatt Macy 	 * don't attempt to mount encrypted datasets with
1129eda14cbcSMatt Macy 	 * unloaded keys
1130eda14cbcSMatt Macy 	 */
1131eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
1132eda14cbcSMatt Macy 	    ZFS_KEYSTATUS_UNAVAILABLE)
1133eda14cbcSMatt Macy 		return (0);
1134eda14cbcSMatt Macy 
1135eda14cbcSMatt Macy 	if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0)
1136eda14cbcSMatt Macy 		ret = ms->ms_mntstatus = -1;
1137eda14cbcSMatt Macy 	return (ret);
1138eda14cbcSMatt Macy }
1139eda14cbcSMatt Macy 
1140eda14cbcSMatt Macy static int
zfs_share_one(zfs_handle_t * zhp,void * arg)1141eda14cbcSMatt Macy zfs_share_one(zfs_handle_t *zhp, void *arg)
1142eda14cbcSMatt Macy {
1143eda14cbcSMatt Macy 	mount_state_t *ms = arg;
1144eda14cbcSMatt Macy 	int ret = 0;
1145eda14cbcSMatt Macy 
1146716fd348SMartin Matuska 	if (zfs_share(zhp, NULL) != 0)
1147eda14cbcSMatt Macy 		ret = ms->ms_mntstatus = -1;
1148eda14cbcSMatt Macy 	return (ret);
1149eda14cbcSMatt Macy }
1150eda14cbcSMatt Macy 
1151eda14cbcSMatt Macy /*
1152eda14cbcSMatt Macy  * Thread pool function to mount one file system. On completion, it finds and
1153eda14cbcSMatt Macy  * schedules its children to be mounted. This depends on the sorting done in
1154eda14cbcSMatt Macy  * zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries
1155eda14cbcSMatt Macy  * each descending from the previous) will have no parallelism since we always
1156eda14cbcSMatt Macy  * have to wait for the parent to finish mounting before we can schedule
1157eda14cbcSMatt Macy  * its children.
1158eda14cbcSMatt Macy  */
1159eda14cbcSMatt Macy static void
zfs_mount_task(void * arg)1160eda14cbcSMatt Macy zfs_mount_task(void *arg)
1161eda14cbcSMatt Macy {
1162eda14cbcSMatt Macy 	mnt_param_t *mp = arg;
1163eda14cbcSMatt Macy 	int idx = mp->mnt_idx;
1164eda14cbcSMatt Macy 	zfs_handle_t **handles = mp->mnt_zhps;
1165eda14cbcSMatt Macy 	size_t num_handles = mp->mnt_num_handles;
1166eda14cbcSMatt Macy 	char mountpoint[ZFS_MAXPROPLEN];
1167eda14cbcSMatt Macy 
1168eda14cbcSMatt Macy 	verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint,
1169eda14cbcSMatt Macy 	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
1170eda14cbcSMatt Macy 
1171eda14cbcSMatt Macy 	if (mp->mnt_func(handles[idx], mp->mnt_data) != 0)
1172c03c5b1cSMartin Matuska 		goto out;
1173eda14cbcSMatt Macy 
1174eda14cbcSMatt Macy 	/*
1175eda14cbcSMatt Macy 	 * We dispatch tasks to mount filesystems with mountpoints underneath
1176eda14cbcSMatt Macy 	 * this one. We do this by dispatching the next filesystem with a
1177eda14cbcSMatt Macy 	 * descendant mountpoint of the one we just mounted, then skip all of
1178eda14cbcSMatt Macy 	 * its descendants, dispatch the next descendant mountpoint, and so on.
1179eda14cbcSMatt Macy 	 * The non_descendant_idx() function skips over filesystems that are
1180eda14cbcSMatt Macy 	 * descendants of the filesystem we just dispatched.
1181eda14cbcSMatt Macy 	 */
1182eda14cbcSMatt Macy 	for (int i = idx + 1; i < num_handles;
1183eda14cbcSMatt Macy 	    i = non_descendant_idx(handles, num_handles, i)) {
1184eda14cbcSMatt Macy 		char child[ZFS_MAXPROPLEN];
1185eda14cbcSMatt Macy 		verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT,
1186eda14cbcSMatt Macy 		    child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1187eda14cbcSMatt Macy 
1188eda14cbcSMatt Macy 		if (!libzfs_path_contains(mountpoint, child))
1189eda14cbcSMatt Macy 			break; /* not a descendant, return */
1190eda14cbcSMatt Macy 		zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i,
1191eda14cbcSMatt Macy 		    mp->mnt_func, mp->mnt_data, mp->mnt_tp);
1192eda14cbcSMatt Macy 	}
1193c03c5b1cSMartin Matuska 
1194c03c5b1cSMartin Matuska out:
1195eda14cbcSMatt Macy 	free(mp);
1196eda14cbcSMatt Macy }
1197eda14cbcSMatt Macy 
1198eda14cbcSMatt Macy /*
1199eda14cbcSMatt Macy  * Issue the func callback for each ZFS handle contained in the handles
1200eda14cbcSMatt Macy  * array. This function is used to mount all datasets, and so this function
1201eda14cbcSMatt Macy  * guarantees that filesystems for parent mountpoints are called before their
1202eda14cbcSMatt Macy  * children. As such, before issuing any callbacks, we first sort the array
1203eda14cbcSMatt Macy  * of handles by mountpoint.
1204eda14cbcSMatt Macy  *
1205eda14cbcSMatt Macy  * Callbacks are issued in one of two ways:
1206eda14cbcSMatt Macy  *
1207aca928a5SMartin Matuska  * 1. Sequentially: If the nthr argument is <= 1 or the ZFS_SERIAL_MOUNT
1208eda14cbcSMatt Macy  *    environment variable is set, then we issue callbacks sequentially.
1209eda14cbcSMatt Macy  *
1210aca928a5SMartin Matuska  * 2. In parallel: If the nthr argument is > 1 and the ZFS_SERIAL_MOUNT
1211eda14cbcSMatt Macy  *    environment variable is not set, then we use a tpool to dispatch threads
1212eda14cbcSMatt Macy  *    to mount filesystems in parallel. This function dispatches tasks to mount
1213eda14cbcSMatt Macy  *    the filesystems at the top-level mountpoints, and these tasks in turn
1214eda14cbcSMatt Macy  *    are responsible for recursively mounting filesystems in their children
1215aca928a5SMartin Matuska  *    mountpoints.  The value of the nthr argument will be the number of worker
1216aca928a5SMartin Matuska  *    threads for the thread pool.
1217eda14cbcSMatt Macy  */
1218eda14cbcSMatt Macy void
zfs_foreach_mountpoint(libzfs_handle_t * hdl,zfs_handle_t ** handles,size_t num_handles,zfs_iter_f func,void * data,uint_t nthr)1219eda14cbcSMatt Macy zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles,
1220aca928a5SMartin Matuska     size_t num_handles, zfs_iter_f func, void *data, uint_t nthr)
1221eda14cbcSMatt Macy {
1222eda14cbcSMatt Macy 	zoneid_t zoneid = getzoneid();
1223eda14cbcSMatt Macy 
1224eda14cbcSMatt Macy 	/*
1225eda14cbcSMatt Macy 	 * The ZFS_SERIAL_MOUNT environment variable is an undocumented
1226eda14cbcSMatt Macy 	 * variable that can be used as a convenience to do a/b comparison
1227eda14cbcSMatt Macy 	 * of serial vs. parallel mounting.
1228eda14cbcSMatt Macy 	 */
1229aca928a5SMartin Matuska 	boolean_t serial_mount = nthr <= 1 ||
1230eda14cbcSMatt Macy 	    (getenv("ZFS_SERIAL_MOUNT") != NULL);
1231eda14cbcSMatt Macy 
1232eda14cbcSMatt Macy 	/*
1233eda14cbcSMatt Macy 	 * Sort the datasets by mountpoint. See mountpoint_cmp for details
1234eda14cbcSMatt Macy 	 * of how these are sorted.
1235eda14cbcSMatt Macy 	 */
1236eda14cbcSMatt Macy 	qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp);
1237eda14cbcSMatt Macy 
1238eda14cbcSMatt Macy 	if (serial_mount) {
1239eda14cbcSMatt Macy 		for (int i = 0; i < num_handles; i++) {
1240eda14cbcSMatt Macy 			func(handles[i], data);
1241eda14cbcSMatt Macy 		}
1242eda14cbcSMatt Macy 		return;
1243eda14cbcSMatt Macy 	}
1244eda14cbcSMatt Macy 
1245eda14cbcSMatt Macy 	/*
1246eda14cbcSMatt Macy 	 * Issue the callback function for each dataset using a parallel
1247eda14cbcSMatt Macy 	 * algorithm that uses a thread pool to manage threads.
1248eda14cbcSMatt Macy 	 */
1249aca928a5SMartin Matuska 	tpool_t *tp = tpool_create(1, nthr, 0, NULL);
1250eda14cbcSMatt Macy 
1251eda14cbcSMatt Macy 	/*
1252eda14cbcSMatt Macy 	 * There may be multiple "top level" mountpoints outside of the pool's
1253eda14cbcSMatt Macy 	 * root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of
1254eda14cbcSMatt Macy 	 * these.
1255eda14cbcSMatt Macy 	 */
1256eda14cbcSMatt Macy 	for (int i = 0; i < num_handles;
1257eda14cbcSMatt Macy 	    i = non_descendant_idx(handles, num_handles, i)) {
1258eda14cbcSMatt Macy 		/*
1259eda14cbcSMatt Macy 		 * Since the mountpoints have been sorted so that the zoned
1260eda14cbcSMatt Macy 		 * filesystems are at the end, a zoned filesystem seen from
1261eda14cbcSMatt Macy 		 * the global zone means that we're done.
1262eda14cbcSMatt Macy 		 */
1263eda14cbcSMatt Macy 		if (zoneid == GLOBAL_ZONEID &&
1264eda14cbcSMatt Macy 		    zfs_prop_get_int(handles[i], ZFS_PROP_ZONED))
1265eda14cbcSMatt Macy 			break;
1266eda14cbcSMatt Macy 		zfs_dispatch_mount(hdl, handles, num_handles, i, func, data,
1267eda14cbcSMatt Macy 		    tp);
1268eda14cbcSMatt Macy 	}
1269eda14cbcSMatt Macy 
1270eda14cbcSMatt Macy 	tpool_wait(tp);	/* wait for all scheduled mounts to complete */
1271eda14cbcSMatt Macy 	tpool_destroy(tp);
1272eda14cbcSMatt Macy }
1273eda14cbcSMatt Macy 
1274eda14cbcSMatt Macy /*
1275eda14cbcSMatt Macy  * Mount and share all datasets within the given pool.  This assumes that no
1276aca928a5SMartin Matuska  * datasets within the pool are currently mounted.  nthr will be number of
1277aca928a5SMartin Matuska  * worker threads to use while mounting datasets.
1278eda14cbcSMatt Macy  */
1279eda14cbcSMatt Macy int
zpool_enable_datasets(zpool_handle_t * zhp,const char * mntopts,int flags,uint_t nthr)1280aca928a5SMartin Matuska zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags,
1281aca928a5SMartin Matuska     uint_t nthr)
1282eda14cbcSMatt Macy {
1283eda14cbcSMatt Macy 	get_all_cb_t cb = { 0 };
1284eda14cbcSMatt Macy 	mount_state_t ms = { 0 };
1285eda14cbcSMatt Macy 	zfs_handle_t *zfsp;
1286eda14cbcSMatt Macy 	int ret = 0;
1287eda14cbcSMatt Macy 
1288eda14cbcSMatt Macy 	if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
1289eda14cbcSMatt Macy 	    ZFS_TYPE_DATASET)) == NULL)
1290eda14cbcSMatt Macy 		goto out;
1291eda14cbcSMatt Macy 
1292eda14cbcSMatt Macy 	/*
1293eda14cbcSMatt Macy 	 * Gather all non-snapshot datasets within the pool. Start by adding
1294eda14cbcSMatt Macy 	 * the root filesystem for this pool to the list, and then iterate
1295eda14cbcSMatt Macy 	 * over all child filesystems.
1296eda14cbcSMatt Macy 	 */
1297eda14cbcSMatt Macy 	libzfs_add_handle(&cb, zfsp);
1298d411c1d6SMartin Matuska 	if (zfs_iter_filesystems_v2(zfsp, 0, zfs_iter_cb, &cb) != 0)
1299eda14cbcSMatt Macy 		goto out;
1300eda14cbcSMatt Macy 
1301eda14cbcSMatt Macy 	/*
1302eda14cbcSMatt Macy 	 * Mount all filesystems
1303eda14cbcSMatt Macy 	 */
1304eda14cbcSMatt Macy 	ms.ms_mntopts = mntopts;
1305eda14cbcSMatt Macy 	ms.ms_mntflags = flags;
1306eda14cbcSMatt Macy 	zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1307aca928a5SMartin Matuska 	    zfs_mount_one, &ms, nthr);
1308eda14cbcSMatt Macy 	if (ms.ms_mntstatus != 0)
13092ad756a6SMartin Matuska 		ret = EZFS_MOUNTFAILED;
1310eda14cbcSMatt Macy 
1311eda14cbcSMatt Macy 	/*
1312eda14cbcSMatt Macy 	 * Share all filesystems that need to be shared. This needs to be
1313eda14cbcSMatt Macy 	 * a separate pass because libshare is not mt-safe, and so we need
1314eda14cbcSMatt Macy 	 * to share serially.
1315eda14cbcSMatt Macy 	 */
1316eda14cbcSMatt Macy 	ms.ms_mntstatus = 0;
1317eda14cbcSMatt Macy 	zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1318aca928a5SMartin Matuska 	    zfs_share_one, &ms, 1);
1319eda14cbcSMatt Macy 	if (ms.ms_mntstatus != 0)
13202ad756a6SMartin Matuska 		ret = EZFS_SHAREFAILED;
1321eda14cbcSMatt Macy 	else
1322716fd348SMartin Matuska 		zfs_commit_shares(NULL);
1323eda14cbcSMatt Macy 
1324eda14cbcSMatt Macy out:
1325eda14cbcSMatt Macy 	for (int i = 0; i < cb.cb_used; i++)
1326eda14cbcSMatt Macy 		zfs_close(cb.cb_handles[i]);
1327eda14cbcSMatt Macy 	free(cb.cb_handles);
1328eda14cbcSMatt Macy 
1329eda14cbcSMatt Macy 	return (ret);
1330eda14cbcSMatt Macy }
1331eda14cbcSMatt Macy 
13323f9d360cSMartin Matuska struct sets_s {
13333f9d360cSMartin Matuska 	char *mountpoint;
13343f9d360cSMartin Matuska 	zfs_handle_t *dataset;
13353f9d360cSMartin Matuska };
13363f9d360cSMartin Matuska 
1337eda14cbcSMatt Macy static int
mountpoint_compare(const void * a,const void * b)1338eda14cbcSMatt Macy mountpoint_compare(const void *a, const void *b)
1339eda14cbcSMatt Macy {
13403f9d360cSMartin Matuska 	const struct sets_s *mounta = (struct sets_s *)a;
13413f9d360cSMartin Matuska 	const struct sets_s *mountb = (struct sets_s *)b;
1342eda14cbcSMatt Macy 
13433f9d360cSMartin Matuska 	return (strcmp(mountb->mountpoint, mounta->mountpoint));
1344eda14cbcSMatt Macy }
1345eda14cbcSMatt Macy 
1346eda14cbcSMatt Macy /*
1347eda14cbcSMatt Macy  * Unshare and unmount all datasets within the given pool.  We don't want to
1348eda14cbcSMatt Macy  * rely on traversing the DSL to discover the filesystems within the pool,
1349eda14cbcSMatt Macy  * because this may be expensive (if not all of them are mounted), and can fail
1350eda14cbcSMatt Macy  * arbitrarily (on I/O error, for example).  Instead, we walk /proc/self/mounts
1351eda14cbcSMatt Macy  * and gather all the filesystems that are currently mounted.
1352eda14cbcSMatt Macy  */
1353eda14cbcSMatt Macy int
zpool_disable_datasets(zpool_handle_t * zhp,boolean_t force)1354eda14cbcSMatt Macy zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1355eda14cbcSMatt Macy {
1356eda14cbcSMatt Macy 	int used, alloc;
135716038816SMartin Matuska 	FILE *mnttab;
1358eda14cbcSMatt Macy 	struct mnttab entry;
1359eda14cbcSMatt Macy 	size_t namelen;
13603f9d360cSMartin Matuska 	struct sets_s *sets = NULL;
1361eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1362eda14cbcSMatt Macy 	int i;
1363eda14cbcSMatt Macy 	int ret = -1;
1364eda14cbcSMatt Macy 	int flags = (force ? MS_FORCE : 0);
1365eda14cbcSMatt Macy 
1366eda14cbcSMatt Macy 	namelen = strlen(zhp->zpool_name);
1367eda14cbcSMatt Macy 
136816038816SMartin Matuska 	if ((mnttab = fopen(MNTTAB, "re")) == NULL)
1369eda14cbcSMatt Macy 		return (ENOENT);
1370eda14cbcSMatt Macy 
1371eda14cbcSMatt Macy 	used = alloc = 0;
137216038816SMartin Matuska 	while (getmntent(mnttab, &entry) == 0) {
1373eda14cbcSMatt Macy 		/*
1374eda14cbcSMatt Macy 		 * Ignore non-ZFS entries.
1375eda14cbcSMatt Macy 		 */
1376eda14cbcSMatt Macy 		if (entry.mnt_fstype == NULL ||
1377eda14cbcSMatt Macy 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1378eda14cbcSMatt Macy 			continue;
1379eda14cbcSMatt Macy 
1380eda14cbcSMatt Macy 		/*
1381eda14cbcSMatt Macy 		 * Ignore filesystems not within this pool.
1382eda14cbcSMatt Macy 		 */
1383eda14cbcSMatt Macy 		if (entry.mnt_mountp == NULL ||
1384eda14cbcSMatt Macy 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1385eda14cbcSMatt Macy 		    (entry.mnt_special[namelen] != '/' &&
1386eda14cbcSMatt Macy 		    entry.mnt_special[namelen] != '\0'))
1387eda14cbcSMatt Macy 			continue;
1388eda14cbcSMatt Macy 
1389eda14cbcSMatt Macy 		/*
1390eda14cbcSMatt Macy 		 * At this point we've found a filesystem within our pool.  Add
1391eda14cbcSMatt Macy 		 * it to our growing list.
1392eda14cbcSMatt Macy 		 */
1393eda14cbcSMatt Macy 		if (used == alloc) {
1394eda14cbcSMatt Macy 			if (alloc == 0) {
1395716fd348SMartin Matuska 				sets = zfs_alloc(hdl,
1396716fd348SMartin Matuska 				    8 * sizeof (struct sets_s));
1397eda14cbcSMatt Macy 				alloc = 8;
1398eda14cbcSMatt Macy 			} else {
1399716fd348SMartin Matuska 				sets = zfs_realloc(hdl, sets,
14003f9d360cSMartin Matuska 				    alloc * sizeof (struct sets_s),
1401716fd348SMartin Matuska 				    alloc * 2 * sizeof (struct sets_s));
1402eda14cbcSMatt Macy 
1403eda14cbcSMatt Macy 				alloc *= 2;
1404eda14cbcSMatt Macy 			}
1405eda14cbcSMatt Macy 		}
1406eda14cbcSMatt Macy 
1407716fd348SMartin Matuska 		sets[used].mountpoint = zfs_strdup(hdl, entry.mnt_mountp);
1408eda14cbcSMatt Macy 
1409eda14cbcSMatt Macy 		/*
1410eda14cbcSMatt Macy 		 * This is allowed to fail, in case there is some I/O error.  It
1411eda14cbcSMatt Macy 		 * is only used to determine if we need to remove the underlying
1412eda14cbcSMatt Macy 		 * mountpoint, so failure is not fatal.
1413eda14cbcSMatt Macy 		 */
14143f9d360cSMartin Matuska 		sets[used].dataset = make_dataset_handle(hdl,
14153f9d360cSMartin Matuska 		    entry.mnt_special);
1416eda14cbcSMatt Macy 
1417eda14cbcSMatt Macy 		used++;
1418eda14cbcSMatt Macy 	}
1419eda14cbcSMatt Macy 
1420eda14cbcSMatt Macy 	/*
1421eda14cbcSMatt Macy 	 * At this point, we have the entire list of filesystems, so sort it by
1422eda14cbcSMatt Macy 	 * mountpoint.
1423eda14cbcSMatt Macy 	 */
1424c03c5b1cSMartin Matuska 	if (used != 0)
14253f9d360cSMartin Matuska 		qsort(sets, used, sizeof (struct sets_s), mountpoint_compare);
1426eda14cbcSMatt Macy 
1427eda14cbcSMatt Macy 	/*
1428eda14cbcSMatt Macy 	 * Walk through and first unshare everything.
1429eda14cbcSMatt Macy 	 */
1430eda14cbcSMatt Macy 	for (i = 0; i < used; i++) {
1431c9539b89SMartin Matuska 		for (enum sa_protocol p = 0; p < SA_PROTOCOL_COUNT; ++p) {
1432c9539b89SMartin Matuska 			if (sa_is_shared(sets[i].mountpoint, p) &&
14333f9d360cSMartin Matuska 			    unshare_one(hdl, sets[i].mountpoint,
1434c9539b89SMartin Matuska 			    sets[i].mountpoint, p) != 0)
1435eda14cbcSMatt Macy 				goto out;
1436eda14cbcSMatt Macy 		}
1437eda14cbcSMatt Macy 	}
1438716fd348SMartin Matuska 	zfs_commit_shares(NULL);
1439eda14cbcSMatt Macy 
1440eda14cbcSMatt Macy 	/*
1441eda14cbcSMatt Macy 	 * Now unmount everything, removing the underlying directories as
1442eda14cbcSMatt Macy 	 * appropriate.
1443eda14cbcSMatt Macy 	 */
1444eda14cbcSMatt Macy 	for (i = 0; i < used; i++) {
14453f9d360cSMartin Matuska 		if (unmount_one(sets[i].dataset, sets[i].mountpoint,
14463f9d360cSMartin Matuska 		    flags) != 0)
1447eda14cbcSMatt Macy 			goto out;
1448eda14cbcSMatt Macy 	}
1449eda14cbcSMatt Macy 
1450eda14cbcSMatt Macy 	for (i = 0; i < used; i++) {
14513f9d360cSMartin Matuska 		if (sets[i].dataset)
14523f9d360cSMartin Matuska 			remove_mountpoint(sets[i].dataset);
1453eda14cbcSMatt Macy 	}
1454eda14cbcSMatt Macy 
145553b70c86SMartin Matuska 	zpool_disable_datasets_os(zhp, force);
145653b70c86SMartin Matuska 
1457eda14cbcSMatt Macy 	ret = 0;
1458eda14cbcSMatt Macy out:
145916038816SMartin Matuska 	(void) fclose(mnttab);
1460eda14cbcSMatt Macy 	for (i = 0; i < used; i++) {
14613f9d360cSMartin Matuska 		if (sets[i].dataset)
14623f9d360cSMartin Matuska 			zfs_close(sets[i].dataset);
14633f9d360cSMartin Matuska 		free(sets[i].mountpoint);
1464eda14cbcSMatt Macy 	}
14653f9d360cSMartin Matuska 	free(sets);
1466eda14cbcSMatt Macy 
1467eda14cbcSMatt Macy 	return (ret);
1468eda14cbcSMatt Macy }
1469