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