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>
34 #ifdef HAVE_LIBSELINUX
35 #include <selinux/selinux.h>
36 #endif /* HAVE_LIBSELINUX */
38 libzfs_handle_t *g_zfs;
40 typedef struct option_map {
42 unsigned long mntmask;
43 unsigned long zfsmask;
46 static const option_map_t option_map[] = {
47 /* Canonicalized filesystem independent options from mount(8) */
48 { MNTOPT_NOAUTO, MS_COMMENT, ZS_COMMENT },
49 { MNTOPT_DEFAULTS, MS_COMMENT, ZS_COMMENT },
50 { MNTOPT_NODEVICES, MS_NODEV, ZS_COMMENT },
51 { MNTOPT_DIRSYNC, MS_DIRSYNC, ZS_COMMENT },
52 { MNTOPT_NOEXEC, MS_NOEXEC, ZS_COMMENT },
53 { MNTOPT_GROUP, MS_GROUP, ZS_COMMENT },
54 { MNTOPT_NETDEV, MS_COMMENT, ZS_COMMENT },
55 { MNTOPT_NOFAIL, MS_COMMENT, ZS_COMMENT },
56 { MNTOPT_NOSUID, MS_NOSUID, ZS_COMMENT },
57 { MNTOPT_OWNER, MS_OWNER, ZS_COMMENT },
58 { MNTOPT_REMOUNT, MS_REMOUNT, ZS_COMMENT },
59 { MNTOPT_RO, MS_RDONLY, ZS_COMMENT },
60 { MNTOPT_RW, MS_COMMENT, ZS_COMMENT },
61 { MNTOPT_SYNC, MS_SYNCHRONOUS, ZS_COMMENT },
62 { MNTOPT_USER, MS_USERS, ZS_COMMENT },
63 { MNTOPT_USERS, MS_USERS, ZS_COMMENT },
65 { MNTOPT_NOATIME, MS_NOATIME, ZS_COMMENT },
68 { MNTOPT_NODIRATIME, MS_NODIRATIME, ZS_COMMENT },
71 { MNTOPT_RELATIME, MS_RELATIME, ZS_COMMENT },
74 { MNTOPT_DFRATIME, MS_STRICTATIME, ZS_COMMENT },
76 { MNTOPT_CONTEXT, MS_COMMENT, ZS_NOCONTEXT },
77 { MNTOPT_NOCONTEXT, MS_COMMENT, ZS_NOCONTEXT },
78 { MNTOPT_FSCONTEXT, MS_COMMENT, ZS_NOCONTEXT },
79 { MNTOPT_DEFCONTEXT, MS_COMMENT, ZS_NOCONTEXT },
80 { MNTOPT_ROOTCONTEXT, MS_COMMENT, ZS_NOCONTEXT },
82 { MNTOPT_IVERSION, MS_I_VERSION, ZS_COMMENT },
85 { MNTOPT_NBMAND, MS_MANDLOCK, ZS_COMMENT },
87 /* Valid options not found in mount(8) */
88 { MNTOPT_BIND, MS_BIND, ZS_COMMENT },
90 { MNTOPT_RBIND, MS_BIND|MS_REC, ZS_COMMENT },
92 { MNTOPT_COMMENT, MS_COMMENT, ZS_COMMENT },
94 { MNTOPT_NOSUB, MS_NOSUB, ZS_COMMENT },
97 { MNTOPT_QUIET, MS_SILENT, ZS_COMMENT },
99 /* Custom zfs options */
100 { MNTOPT_XATTR, MS_COMMENT, ZS_COMMENT },
101 { MNTOPT_NOXATTR, MS_COMMENT, ZS_COMMENT },
102 { MNTOPT_ZFSUTIL, MS_COMMENT, ZS_ZFSUTIL },
106 * Break the mount option in to a name/value pair. The name is
107 * validated against the option map and mount flags set accordingly.
110 parse_option(char *mntopt, unsigned long *mntflags,
111 unsigned long *zfsflags, int sloppy)
113 const option_map_t *opt;
114 char *ptr, *name, *value = NULL;
117 name = strdup(mntopt);
121 for (ptr = name; ptr && *ptr; ptr++) {
125 VERIFY3P(value, !=, NULL);
130 for (opt = option_map; opt->name != NULL; opt++) {
131 if (strncmp(name, opt->name, strlen(name)) == 0) {
132 *mntflags |= opt->mntmask;
133 *zfsflags |= opt->zfsmask;
135 /* MS_USERS implies default user options */
136 if (opt->mntmask & (MS_USERS))
137 *mntflags |= (MS_NOEXEC|MS_NOSUID|MS_NODEV);
139 /* MS_OWNER|MS_GROUP imply default owner options */
140 if (opt->mntmask & (MS_OWNER | MS_GROUP))
141 *mntflags |= (MS_NOSUID|MS_NODEV);
151 /* If required further process on the value may be done here */
157 * Translate the mount option string in to MS_* mount flags for the
158 * kernel vfs. When sloppy is non-zero unknown options will be ignored
159 * otherwise they are considered fatal are copied in to badopt.
162 parse_options(char *mntopts, unsigned long *mntflags, unsigned long *zfsflags,
163 int sloppy, char *badopt, char *mtabopt)
165 int error = 0, quote = 0, flag = 0, count = 0;
166 char *ptr, *opt, *opts;
168 opts = strdup(mntopts);
176 * Scan through all mount options which must be comma delimited.
177 * We must be careful to notice regions which are double quoted
178 * and skip commas in these regions. Each option is then checked
179 * to determine if it is a known option.
181 for (ptr = opts; ptr && !flag; ptr++) {
194 if ((*ptr == ',') || (*ptr == '\0')) {
197 error = parse_option(opt, mntflags, zfsflags, sloppy);
204 if (!(*mntflags & MS_REMOUNT) &&
205 !(*zfsflags & ZS_ZFSUTIL)) {
207 strlcat(mtabopt, ",", MNT_LINE_MAX);
209 strlcat(mtabopt, opt, MNT_LINE_MAX);
223 * If a file or directory in your current working directory is named
224 * 'dataset' then mount(8) will prepend your current working directory
225 * to dataset. The is no way to prevent this behavior so we simply
226 * check for it and strip the prepended patch when it is added.
229 parse_dataset(char *dataset)
234 if (getcwd(cwd, PATH_MAX) == NULL)
239 /* Do not add one when cwd already ends in a trailing '/' */
240 if (!strncmp(cwd, dataset, len))
241 return (dataset + len + (cwd[len-1] != '/'));
247 * Update the mtab_* code to use the libmount library when it is commonly
248 * available otherwise fallback to legacy mode. The mount(8) utility will
249 * manage the lock file for us to prevent racing updates to /etc/mtab.
252 mtab_is_writeable(void)
257 error = lstat(MNTTAB, &st);
258 if (error || S_ISLNK(st.st_mode))
261 fd = open(MNTTAB, O_RDWR | O_CREAT, 0644);
270 mtab_update(char *dataset, char *mntpoint, char *type, char *mntopts)
276 mnt.mnt_fsname = dataset;
277 mnt.mnt_dir = mntpoint;
279 mnt.mnt_opts = mntopts ? mntopts : "";
283 fp = setmntent(MNTTAB, "a+");
285 (void) fprintf(stderr, gettext(
286 "filesystem '%s' was mounted, but %s "
287 "could not be opened due to error %d\n"),
288 dataset, MNTTAB, errno);
289 return (MOUNT_FILEIO);
292 error = addmntent(fp, &mnt);
294 (void) fprintf(stderr, gettext(
295 "filesystem '%s' was mounted, but %s "
296 "could not be updated due to error %d\n"),
297 dataset, MNTTAB, errno);
298 return (MOUNT_FILEIO);
301 (void) endmntent(fp);
303 return (MOUNT_SUCCESS);
307 main(int argc, char **argv)
310 char legacy[ZFS_MAXPROPLEN];
311 char mntopts[MNT_LINE_MAX] = { '\0' };
312 char badopt[MNT_LINE_MAX] = { '\0' };
313 char mtabopt[MNT_LINE_MAX] = { '\0' };
314 char mntpoint[PATH_MAX];
316 unsigned long mntflags = 0, zfsflags = 0, remount = 0;
317 int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0;
320 (void) setlocale(LC_ALL, "");
321 (void) textdomain(TEXT_DOMAIN);
326 while ((c = getopt(argc, argv, "sfnvo:h?")) != -1) {
341 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
345 (void) fprintf(stderr, gettext("Invalid option '%c'\n"),
347 (void) fprintf(stderr, gettext("Usage: mount.zfs "
348 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
349 return (MOUNT_USAGE);
356 /* check that we only have two arguments */
359 (void) fprintf(stderr, gettext("missing dataset "
362 (void) fprintf(stderr,
363 gettext("missing mountpoint argument\n"));
365 (void) fprintf(stderr, gettext("too many arguments\n"));
366 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
367 return (MOUNT_USAGE);
370 dataset = parse_dataset(argv[0]);
372 /* canonicalize the mount point */
373 if (realpath(argv[1], mntpoint) == NULL) {
374 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
375 "mounted at '%s' due to canonicalization error %d.\n"),
376 dataset, argv[1], errno);
377 return (MOUNT_SYSERR);
380 /* validate mount options and set mntflags */
381 error = parse_options(mntopts, &mntflags, &zfsflags, sloppy,
386 (void) fprintf(stderr, gettext("filesystem '%s' "
387 "cannot be mounted due to a memory allocation "
388 "failure.\n"), dataset);
389 return (MOUNT_SYSERR);
391 (void) fprintf(stderr, gettext("filesystem '%s' "
392 "cannot be mounted of due invalid option "
393 "'%s'.\n"), dataset, badopt);
394 (void) fprintf(stderr, gettext("Use the '-s' option "
395 "to ignore the bad mount option.\n"));
396 return (MOUNT_USAGE);
398 (void) fprintf(stderr, gettext("filesystem '%s' "
399 "cannot be mounted due to internal error %d.\n"),
401 return (MOUNT_SOFTWARE);
405 #ifdef HAVE_LIBSELINUX
407 * Automatically add the default zfs context when selinux is enabled
408 * and the caller has not specified their own context. This must be
409 * done until zfs is added to the default selinux policy configuration
410 * as a known filesystem type which supports xattrs.
412 if (is_selinux_enabled() && !(zfsflags & ZS_NOCONTEXT)) {
413 (void) strlcat(mntopts, ",context=\"system_u:"
414 "object_r:file_t:s0\"", sizeof (mntopts));
415 (void) strlcat(mtabopt, ",context=\"system_u:"
416 "object_r:file_t:s0\"", sizeof (mtabopt));
418 #endif /* HAVE_LIBSELINUX */
422 (void) fprintf(stdout, gettext("mount.zfs:\n"
423 " dataset: \"%s\"\n mountpoint: \"%s\"\n"
424 " mountflags: 0x%lx\n zfsflags: 0x%lx\n"
425 " mountopts: \"%s\"\n mtabopts: \"%s\"\n"),
426 dataset, mntpoint, mntflags, zfsflags, mntopts, mtabopt);
428 if (mntflags & MS_REMOUNT) {
433 if (zfsflags & ZS_ZFSUTIL)
436 if ((g_zfs = libzfs_init()) == NULL)
437 return (MOUNT_SYSERR);
439 /* try to open the dataset to access the mount point */
440 if ((zhp = zfs_open(g_zfs, dataset,
441 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) {
442 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
443 "mounted, unable to open the dataset\n"), dataset);
445 return (MOUNT_USAGE);
448 /* treat all snapshots as legacy mount points */
449 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT)
450 (void) strlcpy(legacy, ZFS_MOUNTPOINT_LEGACY, ZFS_MAXPROPLEN);
452 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, legacy,
453 sizeof (legacy), NULL, NULL, 0, B_FALSE);
459 * Legacy mount points may only be mounted using 'mount', never using
460 * 'zfs mount'. However, since 'zfs mount' actually invokes 'mount'
461 * we differentiate the two cases using the 'zfsutil' mount option.
462 * This mount option should only be supplied by the 'zfs mount' util.
464 * The only exception to the above rule is '-o remount' which is
465 * always allowed for non-legacy datasets. This is done because when
466 * using zfs as your root file system both rc.sysinit/umountroot and
467 * systemd depend on 'mount -o remount <mountpoint>' to work.
469 if (zfsutil && !strcmp(legacy, ZFS_MOUNTPOINT_LEGACY)) {
470 (void) fprintf(stderr, gettext(
471 "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
472 "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
473 "See zfs(8) for more information.\n"),
474 dataset, mntpoint, dataset, mntpoint);
475 return (MOUNT_USAGE);
478 if (!zfsutil && !(remount || fake) &&
479 strcmp(legacy, ZFS_MOUNTPOINT_LEGACY)) {
480 (void) fprintf(stderr, gettext(
481 "filesystem '%s' cannot be mounted using 'mount'.\n"
482 "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
483 "See zfs(8) for more information.\n"),
484 dataset, "legacy", dataset);
485 return (MOUNT_USAGE);
489 error = mount(dataset, mntpoint, MNTTYPE_ZFS,
494 (void) fprintf(stderr, gettext("mount point "
495 "'%s' does not exist\n"), mntpoint);
496 return (MOUNT_SYSERR);
498 (void) fprintf(stderr, gettext("filesystem "
499 "'%s' is already mounted\n"), dataset);
500 return (MOUNT_SYSERR);
502 (void) fprintf(stderr, gettext("filesystem "
503 "'%s' can not be mounted due to error "
504 "%d\n"), dataset, errno);
505 return (MOUNT_USAGE);
510 if (!nomtab && mtab_is_writeable()) {
511 error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mtabopt);
516 return (MOUNT_SUCCESS);