xref: /freebsd-src/sys/contrib/openzfs/lib/libzfs/os/linux/libzfs_mount_os.c (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
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
9*271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
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 #include <dirent.h>
33eda14cbcSMatt Macy #include <dlfcn.h>
34eda14cbcSMatt Macy #include <errno.h>
35eda14cbcSMatt Macy #include <fcntl.h>
36eda14cbcSMatt Macy #include <libgen.h>
37eda14cbcSMatt Macy #include <libintl.h>
38eda14cbcSMatt Macy #include <stdio.h>
39eda14cbcSMatt Macy #include <stdlib.h>
40da5137abSMartin Matuska #include <string.h>
41eda14cbcSMatt Macy #include <unistd.h>
42eda14cbcSMatt Macy #include <zone.h>
43eda14cbcSMatt Macy #include <sys/mntent.h>
44eda14cbcSMatt Macy #include <sys/mount.h>
45eda14cbcSMatt Macy #include <sys/stat.h>
46eda14cbcSMatt Macy #include <sys/vfs.h>
47eda14cbcSMatt Macy #include <sys/dsl_crypt.h>
48eda14cbcSMatt Macy #include <libzfs.h>
49eda14cbcSMatt Macy 
5016038816SMartin Matuska #include "../../libzfs_impl.h"
51eda14cbcSMatt Macy #include <thread_pool.h>
52eda14cbcSMatt Macy 
53eda14cbcSMatt Macy #define	ZS_COMMENT	0x00000000	/* comment */
54eda14cbcSMatt Macy #define	ZS_ZFSUTIL	0x00000001	/* caller is zfs(8) */
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy typedef struct option_map {
57eda14cbcSMatt Macy 	const char *name;
58eda14cbcSMatt Macy 	unsigned long mntmask;
59eda14cbcSMatt Macy 	unsigned long zfsmask;
60eda14cbcSMatt Macy } option_map_t;
61eda14cbcSMatt Macy 
62eda14cbcSMatt Macy static const option_map_t option_map[] = {
63eda14cbcSMatt Macy 	/* Canonicalized filesystem independent options from mount(8) */
64eda14cbcSMatt Macy 	{ MNTOPT_NOAUTO,	MS_COMMENT,	ZS_COMMENT	},
65eda14cbcSMatt Macy 	{ MNTOPT_DEFAULTS,	MS_COMMENT,	ZS_COMMENT	},
66eda14cbcSMatt Macy 	{ MNTOPT_NODEVICES,	MS_NODEV,	ZS_COMMENT	},
67eda14cbcSMatt Macy 	{ MNTOPT_DEVICES,	MS_COMMENT,	ZS_COMMENT	},
68eda14cbcSMatt Macy 	{ MNTOPT_DIRSYNC,	MS_DIRSYNC,	ZS_COMMENT	},
69eda14cbcSMatt Macy 	{ MNTOPT_NOEXEC,	MS_NOEXEC,	ZS_COMMENT	},
70eda14cbcSMatt Macy 	{ MNTOPT_EXEC,		MS_COMMENT,	ZS_COMMENT	},
71eda14cbcSMatt Macy 	{ MNTOPT_GROUP,		MS_GROUP,	ZS_COMMENT	},
72eda14cbcSMatt Macy 	{ MNTOPT_NETDEV,	MS_COMMENT,	ZS_COMMENT	},
73eda14cbcSMatt Macy 	{ MNTOPT_NOFAIL,	MS_COMMENT,	ZS_COMMENT	},
74eda14cbcSMatt Macy 	{ MNTOPT_NOSUID,	MS_NOSUID,	ZS_COMMENT	},
75eda14cbcSMatt Macy 	{ MNTOPT_SUID,		MS_COMMENT,	ZS_COMMENT	},
76eda14cbcSMatt Macy 	{ MNTOPT_OWNER,		MS_OWNER,	ZS_COMMENT	},
77eda14cbcSMatt Macy 	{ MNTOPT_REMOUNT,	MS_REMOUNT,	ZS_COMMENT	},
78eda14cbcSMatt Macy 	{ MNTOPT_RO,		MS_RDONLY,	ZS_COMMENT	},
79eda14cbcSMatt Macy 	{ MNTOPT_RW,		MS_COMMENT,	ZS_COMMENT	},
80eda14cbcSMatt Macy 	{ MNTOPT_SYNC,		MS_SYNCHRONOUS,	ZS_COMMENT	},
81eda14cbcSMatt Macy 	{ MNTOPT_USER,		MS_USERS,	ZS_COMMENT	},
82eda14cbcSMatt Macy 	{ MNTOPT_USERS,		MS_USERS,	ZS_COMMENT	},
83eda14cbcSMatt Macy 	/* acl flags passed with util-linux-2.24 mount command */
84eda14cbcSMatt Macy 	{ MNTOPT_ACL,		MS_POSIXACL,	ZS_COMMENT	},
85eda14cbcSMatt Macy 	{ MNTOPT_NOACL,		MS_COMMENT,	ZS_COMMENT	},
86eda14cbcSMatt Macy 	{ MNTOPT_POSIXACL,	MS_POSIXACL,	ZS_COMMENT	},
87*271171e0SMartin Matuska 	/*
88*271171e0SMartin Matuska 	 * Case sensitive options are just listed here to silently
89*271171e0SMartin Matuska 	 * ignore the error if passed with zfs mount command.
90*271171e0SMartin Matuska 	 */
91*271171e0SMartin Matuska 	{ MNTOPT_CASESENSITIVE,		MS_COMMENT,	ZS_COMMENT	},
92*271171e0SMartin Matuska 	{ MNTOPT_CASEINSENSITIVE,	MS_COMMENT,	ZS_COMMENT	},
93*271171e0SMartin Matuska 	{ MNTOPT_CASEMIXED,		MS_COMMENT,	ZS_COMMENT	},
94eda14cbcSMatt Macy #ifdef MS_NOATIME
95eda14cbcSMatt Macy 	{ MNTOPT_NOATIME,	MS_NOATIME,	ZS_COMMENT	},
96eda14cbcSMatt Macy 	{ MNTOPT_ATIME,		MS_COMMENT,	ZS_COMMENT	},
97eda14cbcSMatt Macy #endif
98eda14cbcSMatt Macy #ifdef MS_NODIRATIME
99eda14cbcSMatt Macy 	{ MNTOPT_NODIRATIME,	MS_NODIRATIME,	ZS_COMMENT	},
100eda14cbcSMatt Macy 	{ MNTOPT_DIRATIME,	MS_COMMENT,	ZS_COMMENT	},
101eda14cbcSMatt Macy #endif
102eda14cbcSMatt Macy #ifdef MS_RELATIME
103eda14cbcSMatt Macy 	{ MNTOPT_RELATIME,	MS_RELATIME,	ZS_COMMENT	},
104eda14cbcSMatt Macy 	{ MNTOPT_NORELATIME,	MS_COMMENT,	ZS_COMMENT	},
105eda14cbcSMatt Macy #endif
106eda14cbcSMatt Macy #ifdef MS_STRICTATIME
107eda14cbcSMatt Macy 	{ MNTOPT_STRICTATIME,	MS_STRICTATIME,	ZS_COMMENT	},
108eda14cbcSMatt Macy 	{ MNTOPT_NOSTRICTATIME,	MS_COMMENT,	ZS_COMMENT	},
109eda14cbcSMatt Macy #endif
110eda14cbcSMatt Macy #ifdef MS_LAZYTIME
111eda14cbcSMatt Macy 	{ MNTOPT_LAZYTIME,	MS_LAZYTIME,	ZS_COMMENT	},
112eda14cbcSMatt Macy #endif
113eda14cbcSMatt Macy 	{ MNTOPT_CONTEXT,	MS_COMMENT,	ZS_COMMENT	},
114eda14cbcSMatt Macy 	{ MNTOPT_FSCONTEXT,	MS_COMMENT,	ZS_COMMENT	},
115eda14cbcSMatt Macy 	{ MNTOPT_DEFCONTEXT,	MS_COMMENT,	ZS_COMMENT	},
116eda14cbcSMatt Macy 	{ MNTOPT_ROOTCONTEXT,	MS_COMMENT,	ZS_COMMENT	},
117eda14cbcSMatt Macy #ifdef MS_I_VERSION
118eda14cbcSMatt Macy 	{ MNTOPT_IVERSION,	MS_I_VERSION,	ZS_COMMENT	},
119eda14cbcSMatt Macy #endif
120eda14cbcSMatt Macy #ifdef MS_MANDLOCK
121eda14cbcSMatt Macy 	{ MNTOPT_NBMAND,	MS_MANDLOCK,	ZS_COMMENT	},
122eda14cbcSMatt Macy 	{ MNTOPT_NONBMAND,	MS_COMMENT,	ZS_COMMENT	},
123eda14cbcSMatt Macy #endif
124eda14cbcSMatt Macy 	/* Valid options not found in mount(8) */
125eda14cbcSMatt Macy 	{ MNTOPT_BIND,		MS_BIND,	ZS_COMMENT	},
126eda14cbcSMatt Macy #ifdef MS_REC
127eda14cbcSMatt Macy 	{ MNTOPT_RBIND,		MS_BIND|MS_REC,	ZS_COMMENT	},
128eda14cbcSMatt Macy #endif
129eda14cbcSMatt Macy 	{ MNTOPT_COMMENT,	MS_COMMENT,	ZS_COMMENT	},
130eda14cbcSMatt Macy #ifdef MS_NOSUB
131eda14cbcSMatt Macy 	{ MNTOPT_NOSUB,		MS_NOSUB,	ZS_COMMENT	},
132eda14cbcSMatt Macy #endif
133eda14cbcSMatt Macy #ifdef MS_SILENT
134eda14cbcSMatt Macy 	{ MNTOPT_QUIET,		MS_SILENT,	ZS_COMMENT	},
135eda14cbcSMatt Macy #endif
136eda14cbcSMatt Macy 	/* Custom zfs options */
137eda14cbcSMatt Macy 	{ MNTOPT_XATTR,		MS_COMMENT,	ZS_COMMENT	},
138eda14cbcSMatt Macy 	{ MNTOPT_NOXATTR,	MS_COMMENT,	ZS_COMMENT	},
139eda14cbcSMatt Macy 	{ MNTOPT_ZFSUTIL,	MS_COMMENT,	ZS_ZFSUTIL	},
140eda14cbcSMatt Macy 	{ NULL,			0,		0		} };
141eda14cbcSMatt Macy 
142eda14cbcSMatt Macy /*
143eda14cbcSMatt Macy  * Break the mount option in to a name/value pair.  The name is
144eda14cbcSMatt Macy  * validated against the option map and mount flags set accordingly.
145eda14cbcSMatt Macy  */
146eda14cbcSMatt Macy static int
parse_option(char * mntopt,unsigned long * mntflags,unsigned long * zfsflags,int sloppy)147eda14cbcSMatt Macy parse_option(char *mntopt, unsigned long *mntflags,
148eda14cbcSMatt Macy     unsigned long *zfsflags, int sloppy)
149eda14cbcSMatt Macy {
150eda14cbcSMatt Macy 	const option_map_t *opt;
151eda14cbcSMatt Macy 	char *ptr, *name, *value = NULL;
152eda14cbcSMatt Macy 	int error = 0;
153eda14cbcSMatt Macy 
154eda14cbcSMatt Macy 	name = strdup(mntopt);
155eda14cbcSMatt Macy 	if (name == NULL)
156eda14cbcSMatt Macy 		return (ENOMEM);
157eda14cbcSMatt Macy 
158eda14cbcSMatt Macy 	for (ptr = name; ptr && *ptr; ptr++) {
159eda14cbcSMatt Macy 		if (*ptr == '=') {
160eda14cbcSMatt Macy 			*ptr = '\0';
161eda14cbcSMatt Macy 			value = ptr+1;
162eda14cbcSMatt Macy 			VERIFY3P(value, !=, NULL);
163eda14cbcSMatt Macy 			break;
164eda14cbcSMatt Macy 		}
165eda14cbcSMatt Macy 	}
166eda14cbcSMatt Macy 
167eda14cbcSMatt Macy 	for (opt = option_map; opt->name != NULL; opt++) {
168eda14cbcSMatt Macy 		if (strncmp(name, opt->name, strlen(name)) == 0) {
169eda14cbcSMatt Macy 			*mntflags |= opt->mntmask;
170eda14cbcSMatt Macy 			*zfsflags |= opt->zfsmask;
171eda14cbcSMatt Macy 			error = 0;
172eda14cbcSMatt Macy 			goto out;
173eda14cbcSMatt Macy 		}
174eda14cbcSMatt Macy 	}
175eda14cbcSMatt Macy 
176eda14cbcSMatt Macy 	if (!sloppy)
177eda14cbcSMatt Macy 		error = ENOENT;
178eda14cbcSMatt Macy out:
179eda14cbcSMatt Macy 	/* If required further process on the value may be done here */
180eda14cbcSMatt Macy 	free(name);
181eda14cbcSMatt Macy 	return (error);
182eda14cbcSMatt Macy }
183eda14cbcSMatt Macy 
184eda14cbcSMatt Macy /*
185eda14cbcSMatt Macy  * Translate the mount option string in to MS_* mount flags for the
186eda14cbcSMatt Macy  * kernel vfs.  When sloppy is non-zero unknown options will be ignored
187eda14cbcSMatt Macy  * otherwise they are considered fatal are copied in to badopt.
188eda14cbcSMatt Macy  */
189eda14cbcSMatt Macy int
zfs_parse_mount_options(const char * mntopts,unsigned long * mntflags,unsigned long * zfsflags,int sloppy,char * badopt,char * mtabopt)190a0b956f5SMartin Matuska zfs_parse_mount_options(const char *mntopts, unsigned long *mntflags,
191eda14cbcSMatt Macy     unsigned long *zfsflags, int sloppy, char *badopt, char *mtabopt)
192eda14cbcSMatt Macy {
193eda14cbcSMatt Macy 	int error = 0, quote = 0, flag = 0, count = 0;
194eda14cbcSMatt Macy 	char *ptr, *opt, *opts;
195eda14cbcSMatt Macy 
196eda14cbcSMatt Macy 	opts = strdup(mntopts);
197eda14cbcSMatt Macy 	if (opts == NULL)
198eda14cbcSMatt Macy 		return (ENOMEM);
199eda14cbcSMatt Macy 
200eda14cbcSMatt Macy 	*mntflags = 0;
201eda14cbcSMatt Macy 	opt = NULL;
202eda14cbcSMatt Macy 
203eda14cbcSMatt Macy 	/*
204eda14cbcSMatt Macy 	 * Scan through all mount options which must be comma delimited.
205eda14cbcSMatt Macy 	 * We must be careful to notice regions which are double quoted
206eda14cbcSMatt Macy 	 * and skip commas in these regions.  Each option is then checked
207eda14cbcSMatt Macy 	 * to determine if it is a known option.
208eda14cbcSMatt Macy 	 */
209eda14cbcSMatt Macy 	for (ptr = opts; ptr && !flag; ptr++) {
210eda14cbcSMatt Macy 		if (opt == NULL)
211eda14cbcSMatt Macy 			opt = ptr;
212eda14cbcSMatt Macy 
213eda14cbcSMatt Macy 		if (*ptr == '"')
214eda14cbcSMatt Macy 			quote = !quote;
215eda14cbcSMatt Macy 
216eda14cbcSMatt Macy 		if (quote)
217eda14cbcSMatt Macy 			continue;
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 		if (*ptr == '\0')
220eda14cbcSMatt Macy 			flag = 1;
221eda14cbcSMatt Macy 
222eda14cbcSMatt Macy 		if ((*ptr == ',') || (*ptr == '\0')) {
223eda14cbcSMatt Macy 			*ptr = '\0';
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy 			error = parse_option(opt, mntflags, zfsflags, sloppy);
226eda14cbcSMatt Macy 			if (error) {
227eda14cbcSMatt Macy 				strcpy(badopt, opt);
228eda14cbcSMatt Macy 				goto out;
229eda14cbcSMatt Macy 
230eda14cbcSMatt Macy 			}
231eda14cbcSMatt Macy 
232eda14cbcSMatt Macy 			if (!(*mntflags & MS_REMOUNT) &&
233eda14cbcSMatt Macy 			    !(*zfsflags & ZS_ZFSUTIL) &&
234eda14cbcSMatt Macy 			    mtabopt != NULL) {
235eda14cbcSMatt Macy 				if (count > 0)
236eda14cbcSMatt Macy 					strlcat(mtabopt, ",", MNT_LINE_MAX);
237eda14cbcSMatt Macy 
238eda14cbcSMatt Macy 				strlcat(mtabopt, opt, MNT_LINE_MAX);
239eda14cbcSMatt Macy 				count++;
240eda14cbcSMatt Macy 			}
241eda14cbcSMatt Macy 
242eda14cbcSMatt Macy 			opt = NULL;
243eda14cbcSMatt Macy 		}
244eda14cbcSMatt Macy 	}
245eda14cbcSMatt Macy 
246eda14cbcSMatt Macy out:
247eda14cbcSMatt Macy 	free(opts);
248eda14cbcSMatt Macy 	return (error);
249eda14cbcSMatt Macy }
250eda14cbcSMatt Macy 
251eda14cbcSMatt Macy static void
append_mntopt(const char * name,const char * val,char * mntopts,char * mtabopt,boolean_t quote)252eda14cbcSMatt Macy append_mntopt(const char *name, const char *val, char *mntopts,
253eda14cbcSMatt Macy     char *mtabopt, boolean_t quote)
254eda14cbcSMatt Macy {
255eda14cbcSMatt Macy 	char tmp[MNT_LINE_MAX];
256eda14cbcSMatt Macy 
257eda14cbcSMatt Macy 	snprintf(tmp, MNT_LINE_MAX, quote ? ",%s=\"%s\"" : ",%s=%s", name, val);
258eda14cbcSMatt Macy 
259eda14cbcSMatt Macy 	if (mntopts)
260eda14cbcSMatt Macy 		strlcat(mntopts, tmp, MNT_LINE_MAX);
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy 	if (mtabopt)
263eda14cbcSMatt Macy 		strlcat(mtabopt, tmp, MNT_LINE_MAX);
264eda14cbcSMatt Macy }
265eda14cbcSMatt Macy 
266eda14cbcSMatt Macy static void
zfs_selinux_setcontext(zfs_handle_t * zhp,zfs_prop_t zpt,const char * name,char * mntopts,char * mtabopt)267eda14cbcSMatt Macy zfs_selinux_setcontext(zfs_handle_t *zhp, zfs_prop_t zpt, const char *name,
268eda14cbcSMatt Macy     char *mntopts, char *mtabopt)
269eda14cbcSMatt Macy {
270eda14cbcSMatt Macy 	char context[ZFS_MAXPROPLEN];
271eda14cbcSMatt Macy 
272eda14cbcSMatt Macy 	if (zfs_prop_get(zhp, zpt, context, sizeof (context),
273eda14cbcSMatt Macy 	    NULL, NULL, 0, B_FALSE) == 0) {
274eda14cbcSMatt Macy 		if (strcmp(context, "none") != 0)
275eda14cbcSMatt Macy 			append_mntopt(name, context, mntopts, mtabopt, B_TRUE);
276eda14cbcSMatt Macy 	}
277eda14cbcSMatt Macy }
278eda14cbcSMatt Macy 
279eda14cbcSMatt Macy void
zfs_adjust_mount_options(zfs_handle_t * zhp,const char * mntpoint,char * mntopts,char * mtabopt)280eda14cbcSMatt Macy zfs_adjust_mount_options(zfs_handle_t *zhp, const char *mntpoint,
281eda14cbcSMatt Macy     char *mntopts, char *mtabopt)
282eda14cbcSMatt Macy {
283eda14cbcSMatt Macy 	char prop[ZFS_MAXPROPLEN];
284eda14cbcSMatt Macy 
285eda14cbcSMatt Macy 	/*
286eda14cbcSMatt Macy 	 * Checks to see if the ZFS_PROP_SELINUX_CONTEXT exists
287eda14cbcSMatt Macy 	 * if it does, create a tmp variable in case it's needed
288eda14cbcSMatt Macy 	 * checks to see if the selinux context is set to the default
289eda14cbcSMatt Macy 	 * if it is, allow the setting of the other context properties
290eda14cbcSMatt Macy 	 * this is needed because the 'context' property overrides others
291eda14cbcSMatt Macy 	 * if it is not the default, set the 'context' property
292eda14cbcSMatt Macy 	 */
293eda14cbcSMatt Macy 	if (zfs_prop_get(zhp, ZFS_PROP_SELINUX_CONTEXT, prop, sizeof (prop),
294eda14cbcSMatt Macy 	    NULL, NULL, 0, B_FALSE) == 0) {
295eda14cbcSMatt Macy 		if (strcmp(prop, "none") == 0) {
296eda14cbcSMatt Macy 			zfs_selinux_setcontext(zhp, ZFS_PROP_SELINUX_FSCONTEXT,
297eda14cbcSMatt Macy 			    MNTOPT_FSCONTEXT, mntopts, mtabopt);
298eda14cbcSMatt Macy 			zfs_selinux_setcontext(zhp, ZFS_PROP_SELINUX_DEFCONTEXT,
299eda14cbcSMatt Macy 			    MNTOPT_DEFCONTEXT, mntopts, mtabopt);
300eda14cbcSMatt Macy 			zfs_selinux_setcontext(zhp,
301eda14cbcSMatt Macy 			    ZFS_PROP_SELINUX_ROOTCONTEXT, MNTOPT_ROOTCONTEXT,
302eda14cbcSMatt Macy 			    mntopts, mtabopt);
303eda14cbcSMatt Macy 		} else {
304eda14cbcSMatt Macy 			append_mntopt(MNTOPT_CONTEXT, prop,
305eda14cbcSMatt Macy 			    mntopts, mtabopt, B_TRUE);
306eda14cbcSMatt Macy 		}
307eda14cbcSMatt Macy 	}
308eda14cbcSMatt Macy 
309eda14cbcSMatt Macy 	/* A hint used to determine an auto-mounted snapshot mount point */
310eda14cbcSMatt Macy 	append_mntopt(MNTOPT_MNTPOINT, mntpoint, mntopts, NULL, B_FALSE);
311eda14cbcSMatt Macy }
312eda14cbcSMatt Macy 
313eda14cbcSMatt Macy /*
314eda14cbcSMatt Macy  * By default the filesystem by preparing the mount options (i.e. parsing
315eda14cbcSMatt Macy  * some flags from the "opts" parameter into the "flags" parameter) and then
316eda14cbcSMatt Macy  * directly calling the system call mount(2). We don't need the mount utility
317eda14cbcSMatt Macy  * or update /etc/mtab, because this is a symlink on all modern systems.
318eda14cbcSMatt Macy  *
319eda14cbcSMatt Macy  * If the environment variable ZFS_MOUNT_HELPER is set, we fall back to the
320eda14cbcSMatt Macy  * previous behavior:
321eda14cbcSMatt Macy  * The filesystem is mounted by invoking the system mount utility rather
322eda14cbcSMatt Macy  * than by the system call mount(2).  This ensures that the /etc/mtab
323eda14cbcSMatt Macy  * file is correctly locked for the update.  Performing our own locking
324eda14cbcSMatt Macy  * and /etc/mtab update requires making an unsafe assumption about how
325eda14cbcSMatt Macy  * the mount utility performs its locking.  Unfortunately, this also means
326eda14cbcSMatt Macy  * in the case of a mount failure we do not have the exact errno.  We must
327eda14cbcSMatt Macy  * make due with return value from the mount process.
328eda14cbcSMatt Macy  */
329eda14cbcSMatt Macy int
do_mount(zfs_handle_t * zhp,const char * mntpt,const char * opts,int flags)330a0b956f5SMartin Matuska do_mount(zfs_handle_t *zhp, const char *mntpt, const char *opts, int flags)
331eda14cbcSMatt Macy {
332eda14cbcSMatt Macy 	const char *src = zfs_get_name(zhp);
333eda14cbcSMatt Macy 	int error = 0;
334eda14cbcSMatt Macy 
335eda14cbcSMatt Macy 	if (!libzfs_envvar_is_set("ZFS_MOUNT_HELPER")) {
336eda14cbcSMatt Macy 		char badopt[MNT_LINE_MAX] = {0};
337e92ffd9bSMartin Matuska 		unsigned long mntflags = flags, zfsflags = 0;
338eda14cbcSMatt Macy 		char myopts[MNT_LINE_MAX] = {0};
339eda14cbcSMatt Macy 
340eda14cbcSMatt Macy 		if (zfs_parse_mount_options(opts, &mntflags,
341eda14cbcSMatt Macy 		    &zfsflags, 0, badopt, NULL)) {
342eda14cbcSMatt Macy 			return (EINVAL);
343eda14cbcSMatt Macy 		}
344eda14cbcSMatt Macy 		strlcat(myopts, opts, MNT_LINE_MAX);
345eda14cbcSMatt Macy 		zfs_adjust_mount_options(zhp, mntpt, myopts, NULL);
346eda14cbcSMatt Macy 		if (mount(src, mntpt, MNTTYPE_ZFS, mntflags, myopts)) {
347eda14cbcSMatt Macy 			return (errno);
348eda14cbcSMatt Macy 		}
349eda14cbcSMatt Macy 	} else {
350eda14cbcSMatt Macy 		char *argv[9] = {
351a0b956f5SMartin Matuska 		    (char *)"/bin/mount",
352a0b956f5SMartin Matuska 		    (char *)"--no-canonicalize",
353a0b956f5SMartin Matuska 		    (char *)"-t", (char *)MNTTYPE_ZFS,
354a0b956f5SMartin Matuska 		    (char *)"-o", (char *)opts,
355eda14cbcSMatt Macy 		    (char *)src,
356eda14cbcSMatt Macy 		    (char *)mntpt,
357eda14cbcSMatt Macy 		    (char *)NULL };
358eda14cbcSMatt Macy 
359eda14cbcSMatt Macy 		/* Return only the most critical mount error */
360eda14cbcSMatt Macy 		error = libzfs_run_process(argv[0], argv,
361eda14cbcSMatt Macy 		    STDOUT_VERBOSE|STDERR_VERBOSE);
362eda14cbcSMatt Macy 		if (error) {
363eda14cbcSMatt Macy 			if (error & MOUNT_FILEIO) {
364eda14cbcSMatt Macy 				error = EIO;
365eda14cbcSMatt Macy 			} else if (error & MOUNT_USER) {
366eda14cbcSMatt Macy 				error = EINTR;
367eda14cbcSMatt Macy 			} else if (error & MOUNT_SOFTWARE) {
368eda14cbcSMatt Macy 				error = EPIPE;
369eda14cbcSMatt Macy 			} else if (error & MOUNT_BUSY) {
370eda14cbcSMatt Macy 				error = EBUSY;
371eda14cbcSMatt Macy 			} else if (error & MOUNT_SYSERR) {
372eda14cbcSMatt Macy 				error = EAGAIN;
373eda14cbcSMatt Macy 			} else if (error & MOUNT_USAGE) {
374eda14cbcSMatt Macy 				error = EINVAL;
375eda14cbcSMatt Macy 			} else
376eda14cbcSMatt Macy 				error = ENXIO; /* Generic error */
377eda14cbcSMatt Macy 		}
378eda14cbcSMatt Macy 	}
379eda14cbcSMatt Macy 
380eda14cbcSMatt Macy 	return (error);
381eda14cbcSMatt Macy }
382eda14cbcSMatt Macy 
383eda14cbcSMatt Macy int
do_unmount(zfs_handle_t * zhp,const char * mntpt,int flags)3843f9d360cSMartin Matuska do_unmount(zfs_handle_t *zhp, const char *mntpt, int flags)
385eda14cbcSMatt Macy {
386e92ffd9bSMartin Matuska 	(void) zhp;
387e92ffd9bSMartin Matuska 
388eda14cbcSMatt Macy 	if (!libzfs_envvar_is_set("ZFS_MOUNT_HELPER")) {
3899db44a8eSMartin Matuska 		int rv = umount2(mntpt, flags);
3909db44a8eSMartin Matuska 
3919db44a8eSMartin Matuska 		return (rv < 0 ? errno : 0);
392eda14cbcSMatt Macy 	}
393eda14cbcSMatt Macy 
394eda14cbcSMatt Macy 	char *argv[7] = {
395a0b956f5SMartin Matuska 	    (char *)"/bin/umount",
396a0b956f5SMartin Matuska 	    (char *)"-t", (char *)MNTTYPE_ZFS,
397eda14cbcSMatt Macy 	    NULL, NULL, NULL, NULL };
398eda14cbcSMatt Macy 	int rc, count = 3;
399eda14cbcSMatt Macy 
400a0b956f5SMartin Matuska 	if (flags & MS_FORCE)
401a0b956f5SMartin Matuska 		argv[count++] = (char *)"-f";
402eda14cbcSMatt Macy 
403a0b956f5SMartin Matuska 	if (flags & MS_DETACH)
404a0b956f5SMartin Matuska 		argv[count++] = (char *)"-l";
405eda14cbcSMatt Macy 
406eda14cbcSMatt Macy 	argv[count] = (char *)mntpt;
407eda14cbcSMatt Macy 	rc = libzfs_run_process(argv[0], argv, STDOUT_VERBOSE|STDERR_VERBOSE);
408eda14cbcSMatt Macy 
409eda14cbcSMatt Macy 	return (rc ? EINVAL : 0);
410eda14cbcSMatt Macy }
411eda14cbcSMatt Macy 
412eda14cbcSMatt Macy int
zfs_mount_delegation_check(void)413eda14cbcSMatt Macy zfs_mount_delegation_check(void)
414eda14cbcSMatt Macy {
415eda14cbcSMatt Macy 	return ((geteuid() != 0) ? EACCES : 0);
416eda14cbcSMatt Macy }
41753b70c86SMartin Matuska 
41853b70c86SMartin Matuska /* Called from the tail end of zpool_disable_datasets() */
41953b70c86SMartin Matuska void
zpool_disable_datasets_os(zpool_handle_t * zhp,boolean_t force)42053b70c86SMartin Matuska zpool_disable_datasets_os(zpool_handle_t *zhp, boolean_t force)
42153b70c86SMartin Matuska {
422e92ffd9bSMartin Matuska 	(void) zhp, (void) force;
42353b70c86SMartin Matuska }
42453b70c86SMartin Matuska 
42553b70c86SMartin Matuska /* Called from the tail end of zfs_unmount() */
42653b70c86SMartin Matuska void
zpool_disable_volume_os(const char * name)42753b70c86SMartin Matuska zpool_disable_volume_os(const char *name)
42853b70c86SMartin Matuska {
429e92ffd9bSMartin Matuska 	(void) name;
43053b70c86SMartin Matuska }
431