xref: /freebsd/sys/contrib/openzfs/cmd/mount_zfs.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright (c) 2011 Lawrence Livermore National Security, LLC.
26  */
27 
28 #include <libintl.h>
29 #include <unistd.h>
30 #include <sys/file.h>
31 #include <sys/mount.h>
32 #include <sys/mntent.h>
33 #include <sys/stat.h>
34 #include <libzfs.h>
35 #include <libzutil.h>
36 #include <locale.h>
37 #include <getopt.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 
41 #define	ZS_COMMENT	0x00000000	/* comment */
42 #define	ZS_ZFSUTIL	0x00000001	/* caller is zfs(8) */
43 
44 libzfs_handle_t *g_zfs;
45 
46 /*
47  * Opportunistically convert a target string into a pool name. If the
48  * string does not represent a block device with a valid zfs label
49  * then it is passed through without modification.
50  */
51 static void
parse_dataset(const char * target,char ** dataset)52 parse_dataset(const char *target, char **dataset)
53 {
54 	/*
55 	 * Prior to util-linux 2.36.2, if a file or directory in the
56 	 * current working directory was named 'dataset' then mount(8)
57 	 * would prepend the current working directory to the dataset.
58 	 * Check for it and strip the prepended path when it is added.
59 	 */
60 	char cwd[PATH_MAX];
61 	if (getcwd(cwd, PATH_MAX) == NULL) {
62 		perror("getcwd");
63 		return;
64 	}
65 	int len = strlen(cwd);
66 	if (strncmp(cwd, target, len) == 0)
67 		target += len;
68 
69 	/* Assume pool/dataset is more likely */
70 	strlcpy(*dataset, target, PATH_MAX);
71 
72 	int fd = open(target, O_RDONLY | O_CLOEXEC);
73 	if (fd < 0)
74 		return;
75 
76 	nvlist_t *cfg = NULL;
77 	if (zpool_read_label(fd, &cfg, NULL) == 0) {
78 		const char *nm = NULL;
79 		if (!nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &nm))
80 			strlcpy(*dataset, nm, PATH_MAX);
81 		nvlist_free(cfg);
82 	}
83 
84 	if (close(fd))
85 		perror("close");
86 }
87 
88 /*
89  * Update the mtab_* code to use the libmount library when it is commonly
90  * available otherwise fallback to legacy mode.  The mount(8) utility will
91  * manage the lock file for us to prevent racing updates to /etc/mtab.
92  */
93 static int
mtab_is_writeable(void)94 mtab_is_writeable(void)
95 {
96 	struct stat st;
97 	int error, fd;
98 
99 	error = lstat("/etc/mtab", &st);
100 	if (error || S_ISLNK(st.st_mode))
101 		return (0);
102 
103 	fd = open("/etc/mtab", O_RDWR | O_CREAT, 0644);
104 	if (fd < 0)
105 		return (0);
106 
107 	close(fd);
108 	return (1);
109 }
110 
111 static int
mtab_update(const char * dataset,const char * mntpoint,const char * type,const char * mntopts)112 mtab_update(const char *dataset, const char *mntpoint, const char *type,
113     const char *mntopts)
114 {
115 	struct mntent mnt;
116 	FILE *fp;
117 	int error;
118 
119 	mnt.mnt_fsname = (char *)dataset;
120 	mnt.mnt_dir = (char *)mntpoint;
121 	mnt.mnt_type = (char *)type;
122 	mnt.mnt_opts = (char *)(mntopts ?: "");
123 	mnt.mnt_freq = 0;
124 	mnt.mnt_passno = 0;
125 
126 	fp = setmntent("/etc/mtab", "a+e");
127 	if (!fp) {
128 		(void) fprintf(stderr, gettext(
129 		    "filesystem '%s' was mounted, but /etc/mtab "
130 		    "could not be opened due to error: %s\n"),
131 		    dataset, strerror(errno));
132 		return (MOUNT_FILEIO);
133 	}
134 
135 	error = addmntent(fp, &mnt);
136 	if (error) {
137 		(void) fprintf(stderr, gettext(
138 		    "filesystem '%s' was mounted, but /etc/mtab "
139 		    "could not be updated due to error: %s\n"),
140 		    dataset, strerror(errno));
141 		return (MOUNT_FILEIO);
142 	}
143 
144 	(void) endmntent(fp);
145 
146 	return (MOUNT_SUCCESS);
147 }
148 
149 int
main(int argc,char ** argv)150 main(int argc, char **argv)
151 {
152 	zfs_handle_t *zhp;
153 	char prop[ZFS_MAXPROPLEN];
154 	uint64_t zfs_version = 0;
155 	char mntopts[MNT_LINE_MAX] = { '\0' };
156 	char badopt[MNT_LINE_MAX] = { '\0' };
157 	char mtabopt[MNT_LINE_MAX] = { '\0' };
158 	char mntpoint[PATH_MAX];
159 	char dataset[PATH_MAX], *pdataset = dataset;
160 	unsigned long mntflags = 0, zfsflags = 0, remount = 0;
161 	int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0;
162 	int error, c;
163 
164 	(void) setlocale(LC_ALL, "");
165 	(void) setlocale(LC_NUMERIC, "C");
166 	(void) textdomain(TEXT_DOMAIN);
167 
168 	opterr = 0;
169 
170 	/* check options */
171 	while ((c = getopt_long(argc, argv, "sfnvo:h?", 0, 0)) != -1) {
172 		switch (c) {
173 		case 's':
174 			sloppy = 1;
175 			break;
176 		case 'f':
177 			fake = 1;
178 			break;
179 		case 'n':
180 			nomtab = 1;
181 			break;
182 		case 'v':
183 			verbose++;
184 			break;
185 		case 'o':
186 			(void) strlcpy(mntopts, optarg, sizeof (mntopts));
187 			break;
188 		case 'h':
189 		case '?':
190 			if (optopt)
191 				(void) fprintf(stderr,
192 				    gettext("Invalid option '%c'\n"), optopt);
193 			(void) fprintf(stderr, gettext("Usage: mount.zfs "
194 			    "[-sfnvh] [-o options] <dataset> <mountpoint>\n"));
195 			return (MOUNT_USAGE);
196 		}
197 	}
198 
199 	argc -= optind;
200 	argv += optind;
201 
202 	/* check that we only have two arguments */
203 	if (argc != 2) {
204 		if (argc == 0)
205 			(void) fprintf(stderr, gettext("missing dataset "
206 			    "argument\n"));
207 		else if (argc == 1)
208 			(void) fprintf(stderr,
209 			    gettext("missing mountpoint argument\n"));
210 		else
211 			(void) fprintf(stderr, gettext("too many arguments\n"));
212 		(void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
213 		return (MOUNT_USAGE);
214 	}
215 
216 	parse_dataset(argv[0], &pdataset);
217 
218 	/* canonicalize the mount point */
219 	if (realpath(argv[1], mntpoint) == NULL) {
220 		(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
221 		    "mounted at '%s' due to canonicalization error: %s\n"),
222 		    dataset, argv[1], strerror(errno));
223 		return (MOUNT_SYSERR);
224 	}
225 
226 	/* validate mount options and set mntflags */
227 	error = zfs_parse_mount_options(mntopts, &mntflags, &zfsflags, sloppy,
228 	    badopt, mtabopt);
229 	if (error) {
230 		switch (error) {
231 		case ENOMEM:
232 			(void) fprintf(stderr, gettext("filesystem '%s' "
233 			    "cannot be mounted due to a memory allocation "
234 			    "failure.\n"), dataset);
235 			return (MOUNT_SYSERR);
236 		case ENOENT:
237 			(void) fprintf(stderr, gettext("filesystem '%s' "
238 			    "cannot be mounted due to invalid option "
239 			    "'%s'.\n"), dataset, badopt);
240 			(void) fprintf(stderr, gettext("Use the '-s' option "
241 			    "to ignore the bad mount option.\n"));
242 			return (MOUNT_USAGE);
243 		default:
244 			(void) fprintf(stderr, gettext("filesystem '%s' "
245 			    "cannot be mounted due to internal error %d.\n"),
246 			    dataset, error);
247 			return (MOUNT_SOFTWARE);
248 		}
249 	}
250 
251 	if (mntflags & MS_REMOUNT) {
252 		nomtab = 1;
253 		remount = 1;
254 	}
255 
256 	if (zfsflags & ZS_ZFSUTIL)
257 		zfsutil = 1;
258 
259 	if ((g_zfs = libzfs_init()) == NULL) {
260 		(void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
261 		return (MOUNT_SYSERR);
262 	}
263 
264 	/* try to open the dataset to access the mount point */
265 	if ((zhp = zfs_open(g_zfs, dataset,
266 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) {
267 		(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
268 		    "mounted, unable to open the dataset\n"), dataset);
269 		libzfs_fini(g_zfs);
270 		return (MOUNT_USAGE);
271 	}
272 
273 	if (sloppy || libzfs_envvar_is_set("ZFS_MOUNT_HELPER")) {
274 		zfs_adjust_mount_options(zhp, mntpoint, mntopts, mtabopt);
275 	}
276 
277 	/* treat all snapshots as legacy mount points */
278 	if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT)
279 		(void) strlcpy(prop, ZFS_MOUNTPOINT_LEGACY, ZFS_MAXPROPLEN);
280 	else
281 		(void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, prop,
282 		    sizeof (prop), NULL, NULL, 0, B_FALSE);
283 
284 	/*
285 	 * Fetch the max supported zfs version in case we get ENOTSUP
286 	 * back from the mount command, since we need the zfs handle
287 	 * to do so.
288 	 */
289 	zfs_version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
290 	if (zfs_version == 0) {
291 		fprintf(stderr, gettext("unable to fetch "
292 		    "ZFS version for filesystem '%s'\n"), dataset);
293 		zfs_close(zhp);
294 		libzfs_fini(g_zfs);
295 		return (MOUNT_SYSERR);
296 	}
297 
298 	/*
299 	 * Legacy mount points may only be mounted using 'mount', never using
300 	 * 'zfs mount'.  However, since 'zfs mount' actually invokes 'mount'
301 	 * we differentiate the two cases using the 'zfsutil' mount option.
302 	 * This mount option should only be supplied by the 'zfs mount' util.
303 	 *
304 	 * The only exception to the above rule is '-o remount' which is
305 	 * always allowed for non-legacy datasets.  This is done because when
306 	 * using zfs as your root file system both rc.sysinit/umountroot and
307 	 * systemd depend on 'mount -o remount <mountpoint>' to work.
308 	 */
309 	if (zfsutil && (strcmp(prop, ZFS_MOUNTPOINT_LEGACY) == 0)) {
310 		(void) fprintf(stderr, gettext(
311 		    "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
312 		    "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
313 		    "See zfs(8) for more information.\n"),
314 		    dataset, mntpoint, dataset, mntpoint);
315 		zfs_close(zhp);
316 		libzfs_fini(g_zfs);
317 		return (MOUNT_USAGE);
318 	}
319 
320 	if (!zfsutil && !(remount || fake) &&
321 	    strcmp(prop, ZFS_MOUNTPOINT_LEGACY)) {
322 		(void) fprintf(stderr, gettext(
323 		    "filesystem '%s' cannot be mounted using 'mount'.\n"
324 		    "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
325 		    "See zfs(8) for more information.\n"),
326 		    dataset, "legacy", dataset);
327 		zfs_close(zhp);
328 		libzfs_fini(g_zfs);
329 		return (MOUNT_USAGE);
330 	}
331 
332 	if (verbose)
333 		(void) fprintf(stdout, gettext("mount.zfs:\n"
334 		    "  dataset:    \"%s\"\n  mountpoint: \"%s\"\n"
335 		    "  mountflags: 0x%lx\n  zfsflags:   0x%lx\n"
336 		    "  mountopts:  \"%s\"\n  mtabopts:   \"%s\"\n"),
337 		    dataset, mntpoint, mntflags, zfsflags, mntopts, mtabopt);
338 
339 	if (!fake) {
340 		if (!remount && !sloppy &&
341 		    !libzfs_envvar_is_set("ZFS_MOUNT_HELPER")) {
342 			error = zfs_mount_at(zhp, mntopts, mntflags, mntpoint);
343 			if (error) {
344 				(void) fprintf(stderr, "zfs_mount_at() failed: "
345 				    "%s", libzfs_error_description(g_zfs));
346 				zfs_close(zhp);
347 				libzfs_fini(g_zfs);
348 				return (MOUNT_SYSERR);
349 			}
350 		} else {
351 			error = mount(dataset, mntpoint, MNTTYPE_ZFS,
352 			    mntflags, mntopts);
353 		}
354 	}
355 
356 	zfs_close(zhp);
357 	libzfs_fini(g_zfs);
358 
359 	if (error) {
360 		switch (errno) {
361 		case ENOENT:
362 			(void) fprintf(stderr, gettext("mount point "
363 			    "'%s' does not exist\n"), mntpoint);
364 			return (MOUNT_SYSERR);
365 		case EBUSY:
366 			(void) fprintf(stderr, gettext("filesystem "
367 			    "'%s' is already mounted\n"), dataset);
368 			return (MOUNT_BUSY);
369 		case ENOTSUP:
370 			if (zfs_version > ZPL_VERSION) {
371 				(void) fprintf(stderr,
372 				    gettext("filesystem '%s' (v%d) is not "
373 				    "supported by this implementation of "
374 				    "ZFS (max v%d).\n"), dataset,
375 				    (int)zfs_version, (int)ZPL_VERSION);
376 			} else {
377 				(void) fprintf(stderr,
378 				    gettext("filesystem '%s' mount "
379 				    "failed for unknown reason.\n"), dataset);
380 			}
381 			return (MOUNT_SYSERR);
382 #ifdef MS_MANDLOCK
383 		case EPERM:
384 			if (mntflags & MS_MANDLOCK) {
385 				(void) fprintf(stderr, gettext("filesystem "
386 				    "'%s' has the 'nbmand=on' property set, "
387 				    "this mount\noption may be disabled in "
388 				    "your kernel.  Use 'zfs set nbmand=off'\n"
389 				    "to disable this option and try to "
390 				    "mount the filesystem again.\n"), dataset);
391 				return (MOUNT_SYSERR);
392 			}
393 #endif
394 			zfs_fallthrough;
395 		default:
396 			(void) fprintf(stderr, gettext("filesystem "
397 			    "'%s' can not be mounted: %s\n"), dataset,
398 			    strerror(errno));
399 			return (MOUNT_USAGE);
400 		}
401 	}
402 
403 	if (!nomtab && mtab_is_writeable()) {
404 		error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mtabopt);
405 		if (error)
406 			return (error);
407 	}
408 
409 	return (MOUNT_SUCCESS);
410 }
411