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.
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.
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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011 Lawrence Livermore National Security, LLC.
30 #include <sys/mount.h>
33 #ifdef HAVE_LIBSELINUX
34 #include <selinux/selinux.h>
35 #endif /* HAVE_LIBSELINUX */
37 libzfs_handle_t *g_zfs;
39 typedef struct option_map {
41 unsigned long mntmask;
42 unsigned long zfsmask;
45 static const option_map_t option_map[] = {
46 /* Canonicalized filesystem independent options from mount(8) */
47 { MNTOPT_NOAUTO, MS_COMMENT, ZS_COMMENT },
48 { MNTOPT_DEFAULTS, MS_COMMENT, ZS_COMMENT },
49 { MNTOPT_NODEVICES, MS_NODEV, ZS_COMMENT },
50 { MNTOPT_DIRSYNC, MS_DIRSYNC, ZS_COMMENT },
51 { MNTOPT_NOEXEC, MS_NOEXEC, ZS_COMMENT },
52 { MNTOPT_GROUP, MS_GROUP, ZS_COMMENT },
53 { MNTOPT_NETDEV, MS_COMMENT, ZS_COMMENT },
54 { MNTOPT_NOFAIL, MS_COMMENT, ZS_COMMENT },
55 { MNTOPT_NOSUID, MS_NOSUID, ZS_COMMENT },
56 { MNTOPT_OWNER, MS_OWNER, ZS_COMMENT },
57 { MNTOPT_REMOUNT, MS_REMOUNT, ZS_COMMENT },
58 { MNTOPT_RO, MS_RDONLY, ZS_COMMENT },
59 { MNTOPT_RW, MS_COMMENT, ZS_COMMENT },
60 { MNTOPT_SYNC, MS_SYNCHRONOUS, ZS_COMMENT },
61 { MNTOPT_USER, MS_USERS, ZS_COMMENT },
62 { MNTOPT_USERS, MS_USERS, ZS_COMMENT },
64 { MNTOPT_NOATIME, MS_NOATIME, ZS_COMMENT },
67 { MNTOPT_NODIRATIME, MS_NODIRATIME, ZS_COMMENT },
70 { MNTOPT_RELATIME, MS_RELATIME, ZS_COMMENT },
73 { MNTOPT_DFRATIME, MS_STRICTATIME, ZS_COMMENT },
75 { MNTOPT_CONTEXT, MS_COMMENT, ZS_NOCONTEXT },
76 { MNTOPT_NOCONTEXT, MS_COMMENT, ZS_NOCONTEXT },
77 { MNTOPT_FSCONTEXT, MS_COMMENT, ZS_NOCONTEXT },
78 { MNTOPT_DEFCONTEXT, MS_COMMENT, ZS_NOCONTEXT },
79 { MNTOPT_ROOTCONTEXT, MS_COMMENT, ZS_NOCONTEXT },
81 { MNTOPT_IVERSION, MS_I_VERSION, ZS_COMMENT },
84 { MNTOPT_NBMAND, MS_MANDLOCK, ZS_COMMENT },
86 /* Valid options not found in mount(8) */
87 { MNTOPT_BIND, MS_BIND, ZS_COMMENT },
89 { MNTOPT_RBIND, MS_BIND|MS_REC, ZS_COMMENT },
91 { MNTOPT_COMMENT, MS_COMMENT, ZS_COMMENT },
93 { MNTOPT_NOSUB, MS_NOSUB, ZS_COMMENT },
96 { MNTOPT_QUIET, MS_SILENT, ZS_COMMENT },
98 /* Custom zfs options */
99 { MNTOPT_NOXATTR, MS_COMMENT, ZS_COMMENT },
100 { MNTOPT_ZFSUTIL, MS_COMMENT, ZS_ZFSUTIL },
104 * Break the mount option in to a name/value pair. The name is
105 * validated against the option map and mount flags set accordingly.
108 parse_option(char *mntopt, unsigned long *mntflags,
109 unsigned long *zfsflags, int sloppy)
111 const option_map_t *opt;
112 char *ptr, *name, *value = NULL;
115 name = strdup(mntopt);
119 for (ptr = name; ptr && *ptr; ptr++) {
127 for (opt = option_map; opt->name != NULL; opt++) {
128 if (strncmp(name, opt->name, strlen(name)) == 0) {
129 *mntflags |= opt->mntmask;
130 *zfsflags |= opt->zfsmask;
132 /* MS_USERS implies default user options */
133 if (opt->mntmask & (MS_USERS))
134 *mntflags |= (MS_NOEXEC|MS_NOSUID|MS_NODEV);
136 /* MS_OWNER|MS_GROUP imply default owner options */
137 if (opt->mntmask & (MS_OWNER | MS_GROUP))
138 *mntflags |= (MS_NOSUID|MS_NODEV);
148 /* If required further process on the value may be done here */
154 * Translate the mount option string in to MS_* mount flags for the
155 * kernel vfs. When sloppy is non-zero unknown options will be ignored
156 * otherwise they are considered fatal are copied in to badopt.
159 parse_options(char *mntopts, unsigned long *mntflags, unsigned long *zfsflags,
160 int sloppy, char *badopt, char *mtabopt)
162 int error = 0, quote = 0, flag = 0, count = 0;
163 char *ptr, *opt, *opts;
165 opts = strdup(mntopts);
173 * Scan through all mount options which must be comma delimited.
174 * We must be careful to notice regions which are double quoted
175 * and skip commas in these regions. Each option is then checked
176 * to determine if it is a known option.
178 for (ptr = opts; ptr && !flag; ptr++) {
191 if ((*ptr == ',') || (*ptr == '\0')) {
194 error = parse_option(opt, mntflags, zfsflags, sloppy);
201 if (!(*mntflags & MS_REMOUNT) &&
202 !(*zfsflags & ZS_ZFSUTIL)) {
204 strlcat(mtabopt, ",", MNT_LINE_MAX);
206 strlcat(mtabopt, opt, MNT_LINE_MAX);
220 * If a file or directory in your current working directory is named
221 * 'dataset' then mount(8) will prepend your current working directory
222 * to dataset. The is no way to prevent this behavior so we simply
223 * check for it and strip the prepended patch when it is added.
226 parse_dataset(char *dataset)
231 (void) getcwd(cwd, PATH_MAX);
234 /* Do not add one when cwd already ends in a trailing '/' */
235 if (!strncmp(cwd, dataset, len))
236 return (dataset + len + (cwd[len-1] != '/'));
242 * Update the mtab_* code to use the libmount library when it is commonly
243 * available otherwise fallback to legacy mode. The mount(8) utility will
244 * manage the lock file for us to prevent racing updates to /etc/mtab.
247 mtab_is_writeable(void)
252 error = stat(MNTTAB, &st);
253 if (error || S_ISLNK(st.st_mode))
256 fd = open(MNTTAB, O_RDWR | O_CREAT, 0644);
265 mtab_update(char *dataset, char *mntpoint, char *type, char *mntopts)
271 mnt.mnt_fsname = dataset;
272 mnt.mnt_dir = mntpoint;
274 mnt.mnt_opts = mntopts ? mntopts : "";
278 fp = setmntent(MNTTAB, "a+");
280 (void) fprintf(stderr, gettext(
281 "filesystem '%s' was mounted, but %s "
282 "could not be opened due to error %d\n"),
283 dataset, MNTTAB, errno);
284 return (MOUNT_FILEIO);
287 error = addmntent(fp, &mnt);
289 (void) fprintf(stderr, gettext(
290 "filesystem '%s' was mounted, but %s "
291 "could not be updated due to error %d\n"),
292 dataset, MNTTAB, errno);
293 return (MOUNT_FILEIO);
296 (void) endmntent(fp);
298 return (MOUNT_SUCCESS);
302 main(int argc, char **argv)
305 char legacy[ZFS_MAXPROPLEN];
306 char mntopts[MNT_LINE_MAX] = { '\0' };
307 char badopt[MNT_LINE_MAX] = { '\0' };
308 char mtabopt[MNT_LINE_MAX] = { '\0' };
309 char *dataset, *mntpoint;
310 unsigned long mntflags = 0, zfsflags = 0, remount_ro = 0;
311 int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0;
314 (void) setlocale(LC_ALL, "");
315 (void) textdomain(TEXT_DOMAIN);
320 while ((c = getopt(argc, argv, "sfnvo:h?")) != -1) {
335 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
339 (void) fprintf(stderr, gettext("Invalid option '%c'\n"),
341 (void) fprintf(stderr, gettext("Usage: mount.zfs "
342 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
343 return (MOUNT_USAGE);
350 /* check that we only have two arguments */
353 (void) fprintf(stderr, gettext("missing dataset "
356 (void) fprintf(stderr,
357 gettext("missing mountpoint argument\n"));
359 (void) fprintf(stderr, gettext("too many arguments\n"));
360 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
361 return (MOUNT_USAGE);
364 dataset = parse_dataset(argv[0]);
367 /* validate mount options and set mntflags */
368 error = parse_options(mntopts, &mntflags, &zfsflags, sloppy,
373 (void) fprintf(stderr, gettext("filesystem '%s' "
374 "cannot be mounted due to a memory allocation "
375 "failure.\n"), dataset);
376 return (MOUNT_SYSERR);
378 (void) fprintf(stderr, gettext("filesystem '%s' "
379 "cannot be mounted of due invalid option "
380 "'%s'.\n"), dataset, badopt);
381 (void) fprintf(stderr, gettext("Use the '-s' option "
382 "to ignore the bad mount option.\n"));
383 return (MOUNT_USAGE);
385 (void) fprintf(stderr, gettext("filesystem '%s' "
386 "cannot be mounted due to internal error %d.\n"),
388 return (MOUNT_SOFTWARE);
392 #ifdef HAVE_LIBSELINUX
394 * Automatically add the default zfs context when selinux is enabled
395 * and the caller has not specified their own context. This must be
396 * done until zfs is added to the default selinux policy configuration
397 * as a known filesystem type which supports xattrs.
399 if (is_selinux_enabled() && !(zfsflags & ZS_NOCONTEXT)) {
400 (void) strlcat(mntopts, ",context=\"system_u:"
401 "object_r:file_t:s0\"", sizeof (mntopts));
402 (void) strlcat(mtabopt, ",context=\"system_u:"
403 "object_r:file_t:s0\"", sizeof (mtabopt));
405 #endif /* HAVE_LIBSELINUX */
409 (void) fprintf(stdout, gettext("mount.zfs:\n"
410 " dataset: \"%s\"\n mountpoint: \"%s\"\n"
411 " mountflags: 0x%lx\n zfsflags: 0x%lx\n"
412 " mountopts: \"%s\"\n mtabopts: \"%s\"\n"),
413 dataset, mntpoint, mntflags, zfsflags, mntopts, mtabopt);
415 if (mntflags & MS_REMOUNT)
418 if ((mntflags & MS_REMOUNT) && (mntflags & MS_RDONLY))
421 if (zfsflags & ZS_ZFSUTIL)
424 if ((g_zfs = libzfs_init()) == NULL)
425 return (MOUNT_SYSERR);
427 /* try to open the dataset to access the mount point */
428 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) {
429 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
430 "mounted, unable to open the dataset\n"), dataset);
432 return (MOUNT_USAGE);
435 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, legacy,
436 sizeof (legacy), NULL, NULL, 0, B_FALSE);
442 * Legacy mount points may only be mounted using 'mount', never using
443 * 'zfs mount'. However, since 'zfs mount' actually invokes 'mount'
444 * we differentiate the two cases using the 'zfsutil' mount option.
445 * This mount option should only be supplied by the 'zfs mount' util.
447 * The only exception to the above rule is '-o remount,ro'. This is
448 * always allowed for non-legacy datasets for rc.sysinit/umountroot
449 * to safely remount the root filesystem and flush its cache.
451 if (zfsutil && !strcmp(legacy, ZFS_MOUNTPOINT_LEGACY)) {
452 (void) fprintf(stderr, gettext(
453 "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
454 "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
455 "See zfs(8) for more information.\n"),
456 dataset, mntpoint, dataset, mntpoint);
457 return (MOUNT_USAGE);
460 if (!zfsutil && strcmp(legacy, ZFS_MOUNTPOINT_LEGACY) && !remount_ro) {
461 (void) fprintf(stderr, gettext(
462 "filesystem '%s' cannot be mounted using 'mount'.\n"
463 "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
464 "See zfs(8) for more information.\n"),
465 dataset, "legacy", dataset);
466 return (MOUNT_USAGE);
470 error = mount(dataset, mntpoint, MNTTYPE_ZFS,
475 (void) fprintf(stderr, gettext("filesystem "
476 "'%s' is already mounted\n"), dataset);
477 return (MOUNT_SYSERR);
479 (void) fprintf(stderr, gettext("filesystem "
480 "'%s' can not be mounted due to error "
481 "%d\n"), dataset, errno);
482 return (MOUNT_USAGE);
487 if (!nomtab && mtab_is_writeable()) {
488 error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mtabopt);
493 return (MOUNT_SUCCESS);