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 },
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 },
83 { MNTOPT_IVERSION, MS_I_VERSION, ZS_COMMENT },
86 { MNTOPT_NBMAND, MS_MANDLOCK, ZS_COMMENT },
88 /* Valid options not found in mount(8) */
89 { MNTOPT_BIND, MS_BIND, ZS_COMMENT },
91 { MNTOPT_RBIND, MS_BIND|MS_REC, ZS_COMMENT },
93 { MNTOPT_COMMENT, MS_COMMENT, ZS_COMMENT },
95 { MNTOPT_NOSUB, MS_NOSUB, ZS_COMMENT },
98 { MNTOPT_QUIET, MS_SILENT, ZS_COMMENT },
100 /* Custom zfs options */
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++) {
129 for (opt = option_map; opt->name != NULL; opt++) {
130 if (strncmp(name, opt->name, strlen(name)) == 0) {
131 *mntflags |= opt->mntmask;
132 *zfsflags |= opt->zfsmask;
134 /* MS_USERS implies default user options */
135 if (opt->mntmask & (MS_USERS))
136 *mntflags |= (MS_NOEXEC|MS_NOSUID|MS_NODEV);
138 /* MS_OWNER|MS_GROUP imply default owner options */
139 if (opt->mntmask & (MS_OWNER | MS_GROUP))
140 *mntflags |= (MS_NOSUID|MS_NODEV);
150 /* If required further process on the value may be done here */
156 * Translate the mount option string in to MS_* mount flags for the
157 * kernel vfs. When sloppy is non-zero unknown options will be ignored
158 * otherwise they are considered fatal are copied in to badopt.
161 parse_options(char *mntopts, unsigned long *mntflags,
162 unsigned long *zfsflags, int sloppy, char *badopt)
164 int error = 0, quote = 0, flag = 0;
165 char *ptr, *opt, *opts;
167 opts = strdup(mntopts);
175 * Scan through all mount options which must be comma delimited.
176 * We must be careful to notice regions which are double quoted
177 * and skip commas in these regions. Each option is then checked
178 * to determine if it is a known option.
180 for (ptr = opts; ptr && !flag; ptr++) {
193 if ((*ptr == ',') || (*ptr == '\0')) {
196 error = parse_option(opt, mntflags, zfsflags, sloppy);
212 * If a file or directory in your current working directory is named
213 * 'dataset' then mount(8) will prepend your current working directory
214 * to dataset. The is no way to prevent this behavior so we simply
215 * check for it and strip the prepended patch when it is added.
218 parse_dataset(char *dataset)
222 (void) getcwd(cwd, PATH_MAX);
223 if (!strncmp(cwd, dataset, strlen(cwd)))
224 return (dataset + strlen(cwd) + 1);
230 * Update the mtab_* code to use the libmount library when it is commonly
231 * available otherwise fallback to legacy mode. The mount(8) utility will
232 * manage the lock file for us to prevent racing updates to /etc/mtab.
235 mtab_is_writeable(void)
240 error = stat(MNTTAB, &st);
241 if (error || S_ISLNK(st.st_mode))
244 fd = open(MNTTAB, O_RDWR | O_CREAT, 0644);
253 mtab_update(char *dataset, char *mntpoint, char *type, char *mntopts)
259 mnt.mnt_fsname = dataset;
260 mnt.mnt_dir = mntpoint;
262 mnt.mnt_opts = mntopts ? mntopts : "";
266 fp = setmntent(MNTTAB, "a+");
268 (void) fprintf(stderr, gettext(
269 "filesystem '%s' was mounted, but %s "
270 "could not be opened due to error %d\n"),
271 dataset, MNTTAB, errno);
272 return (MOUNT_FILEIO);
275 error = addmntent(fp, &mnt);
277 (void) fprintf(stderr, gettext(
278 "filesystem '%s' was mounted, but %s "
279 "could not be updated due to error %d\n"),
280 dataset, MNTTAB, errno);
281 return (MOUNT_FILEIO);
284 (void) endmntent(fp);
286 return (MOUNT_SUCCESS);
290 main(int argc, char **argv)
293 char legacy[ZFS_MAXPROPLEN];
294 char mntopts[MNT_LINE_MAX] = { '\0' };
295 char badopt[MNT_LINE_MAX] = { '\0' };
296 char *dataset, *mntpoint;
297 unsigned long mntflags = 0, zfsflags = 0;
298 int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0;
301 (void) setlocale(LC_ALL, "");
302 (void) textdomain(TEXT_DOMAIN);
307 while ((c = getopt(argc, argv, "sfnvo:h?")) != -1) {
322 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
326 (void) fprintf(stderr, gettext("Invalid option '%c'\n"),
328 (void) fprintf(stderr, gettext("Usage: mount.zfs "
329 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
330 return (MOUNT_USAGE);
337 /* check that we only have two arguments */
340 (void) fprintf(stderr, gettext("missing dataset "
343 (void) fprintf(stderr,
344 gettext("missing mountpoint argument\n"));
346 (void) fprintf(stderr, gettext("too many arguments\n"));
347 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
348 return (MOUNT_USAGE);
351 dataset = parse_dataset(argv[0]);
354 /* validate mount options and set mntflags */
355 error = parse_options(mntopts, &mntflags, &zfsflags, sloppy, badopt);
359 (void) fprintf(stderr, gettext("filesystem '%s' "
360 "cannot be mounted due to a memory allocation "
361 "failure\n"), dataset);
362 return (MOUNT_SYSERR);
364 (void) fprintf(stderr, gettext("filesystem '%s' "
365 "cannot be mounted of due to the invalid option "
366 "'%s'\n"), dataset, badopt);
367 (void) fprintf(stderr, gettext("Use the '-s' option "
368 "to ignore the bad mount option.\n"));
369 return (MOUNT_USAGE);
371 (void) fprintf(stderr, gettext("filesystem '%s' "
372 "cannot be mounted due to internal error %d\n"),
374 return (MOUNT_SOFTWARE);
378 #ifdef HAVE_LIBSELINUX
380 * Automatically add the default zfs context when selinux is enabled
381 * and the caller has not specified their own context. This must be
382 * done until zfs is added to the default selinux policy configuration
383 * as a known filesystem type which supports xattrs.
385 if (is_selinux_enabled() && !(zfsflags & ZS_NOCONTEXT))
386 (void) strlcat(mntopts, ",context=\"system_u:"
387 "object_r:file_t:s0\"", sizeof (mntopts));
388 #endif /* HAVE_LIBSELINUX */
392 (void) fprintf(stdout, gettext("mount.zfs:\n"
393 " dataset: \"%s\"\n mountpoint: \"%s\"\n"
394 " mountflags: 0x%lx\n zfsflags: 0x%lx\n"
395 " mountopts: \"%s\"\n\n"),
396 dataset, mntpoint, mntflags, zfsflags, mntopts);
398 if (mntflags & MS_REMOUNT)
401 if (zfsflags * ZS_ZFSUTIL)
404 if ((g_zfs = libzfs_init()) == NULL)
405 return (MOUNT_SYSERR);
407 /* try to open the dataset to access the mount point */
408 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) {
409 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
410 "mounted, unable to open the dataset\n"), dataset);
412 return (MOUNT_USAGE);
415 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, legacy,
416 sizeof (legacy), NULL, NULL, 0, B_FALSE);
422 * Legacy mount points may only be mounted using 'mount', never using
423 * 'zfs mount'. However, since 'zfs mount' actually invokes 'mount'
424 * we differentiate the two cases using the 'zfsutil' mount option.
425 * This mount option should only be supplied by the 'zfs mount' util.
427 if (zfsutil && !strcmp(legacy, ZFS_MOUNTPOINT_LEGACY)) {
428 (void) fprintf(stderr, gettext(
429 "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
430 "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
431 "See zfs(8) for more information.\n"),
432 dataset, mntpoint, dataset, mntpoint);
433 return (MOUNT_USAGE);
436 if (!zfsutil && strcmp(legacy, ZFS_MOUNTPOINT_LEGACY)) {
437 (void) fprintf(stderr, gettext(
438 "filesystem '%s' cannot be mounted using 'mount'.\n"
439 "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
440 "See zfs(8) for more information.\n"),
441 dataset, "legacy", dataset);
442 return (MOUNT_USAGE);
446 error = mount(dataset, mntpoint, MNTTYPE_ZFS,
451 (void) fprintf(stderr, gettext("filesystem "
452 "'%s' is already mounted\n"), dataset);
453 return (MOUNT_SYSERR);
455 (void) fprintf(stderr, gettext("filesystem "
456 "'%s' can not be mounted due to error "
457 "%d\n"), dataset, errno);
458 return (MOUNT_USAGE);
463 if (!nomtab && mtab_is_writeable()) {
464 error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mntopts);
469 return (MOUNT_SUCCESS);