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>
31 #include <sys/mntent.h>
38 #define ZS_COMMENT 0x00000000 /* comment */
39 #define ZS_ZFSUTIL 0x00000001 /* caller is zfs(8) */
41 libzfs_handle_t
*g_zfs
;
43 typedef struct option_map
{
45 unsigned long mntmask
;
46 unsigned long zfsmask
;
49 static const option_map_t option_map
[] = {
50 /* Canonicalized filesystem independent options from mount(8) */
51 { MNTOPT_NOAUTO
, MS_COMMENT
, ZS_COMMENT
},
52 { MNTOPT_DEFAULTS
, MS_COMMENT
, ZS_COMMENT
},
53 { MNTOPT_NODEVICES
, MS_NODEV
, ZS_COMMENT
},
54 { MNTOPT_DIRSYNC
, MS_DIRSYNC
, ZS_COMMENT
},
55 { MNTOPT_NOEXEC
, MS_NOEXEC
, ZS_COMMENT
},
56 { MNTOPT_GROUP
, MS_GROUP
, ZS_COMMENT
},
57 { MNTOPT_NETDEV
, MS_COMMENT
, ZS_COMMENT
},
58 { MNTOPT_NOFAIL
, MS_COMMENT
, ZS_COMMENT
},
59 { MNTOPT_NOSUID
, MS_NOSUID
, ZS_COMMENT
},
60 { MNTOPT_OWNER
, MS_OWNER
, ZS_COMMENT
},
61 { MNTOPT_REMOUNT
, MS_REMOUNT
, ZS_COMMENT
},
62 { MNTOPT_RO
, MS_RDONLY
, ZS_COMMENT
},
63 { MNTOPT_RW
, MS_COMMENT
, ZS_COMMENT
},
64 { MNTOPT_SYNC
, MS_SYNCHRONOUS
, ZS_COMMENT
},
65 { MNTOPT_USER
, MS_USERS
, ZS_COMMENT
},
66 { MNTOPT_USERS
, MS_USERS
, ZS_COMMENT
},
67 /* acl flags passed with util-linux-2.24 mount command */
68 { MNTOPT_ACL
, MS_POSIXACL
, ZS_COMMENT
},
69 { MNTOPT_NOACL
, MS_COMMENT
, ZS_COMMENT
},
70 { MNTOPT_POSIXACL
, MS_POSIXACL
, ZS_COMMENT
},
72 { MNTOPT_NOATIME
, MS_NOATIME
, ZS_COMMENT
},
75 { MNTOPT_NODIRATIME
, MS_NODIRATIME
, ZS_COMMENT
},
78 { MNTOPT_RELATIME
, MS_RELATIME
, ZS_COMMENT
},
81 { MNTOPT_STRICTATIME
, MS_STRICTATIME
, ZS_COMMENT
},
84 { MNTOPT_LAZYTIME
, MS_LAZYTIME
, ZS_COMMENT
},
86 { MNTOPT_CONTEXT
, MS_COMMENT
, ZS_COMMENT
},
87 { MNTOPT_FSCONTEXT
, MS_COMMENT
, ZS_COMMENT
},
88 { MNTOPT_DEFCONTEXT
, MS_COMMENT
, ZS_COMMENT
},
89 { MNTOPT_ROOTCONTEXT
, MS_COMMENT
, ZS_COMMENT
},
91 { MNTOPT_IVERSION
, MS_I_VERSION
, ZS_COMMENT
},
94 { MNTOPT_NBMAND
, MS_MANDLOCK
, ZS_COMMENT
},
96 /* Valid options not found in mount(8) */
97 { MNTOPT_BIND
, MS_BIND
, ZS_COMMENT
},
99 { MNTOPT_RBIND
, MS_BIND
|MS_REC
, ZS_COMMENT
},
101 { MNTOPT_COMMENT
, MS_COMMENT
, ZS_COMMENT
},
103 { MNTOPT_NOSUB
, MS_NOSUB
, ZS_COMMENT
},
106 { MNTOPT_QUIET
, MS_SILENT
, ZS_COMMENT
},
108 /* Custom zfs options */
109 { MNTOPT_XATTR
, MS_COMMENT
, ZS_COMMENT
},
110 { MNTOPT_NOXATTR
, MS_COMMENT
, ZS_COMMENT
},
111 { MNTOPT_ZFSUTIL
, MS_COMMENT
, ZS_ZFSUTIL
},
115 * Break the mount option in to a name/value pair. The name is
116 * validated against the option map and mount flags set accordingly.
119 parse_option(char *mntopt
, unsigned long *mntflags
,
120 unsigned long *zfsflags
, int sloppy
)
122 const option_map_t
*opt
;
123 char *ptr
, *name
, *value
= NULL
;
126 name
= strdup(mntopt
);
130 for (ptr
= name
; ptr
&& *ptr
; ptr
++) {
134 VERIFY3P(value
, !=, NULL
);
139 for (opt
= option_map
; opt
->name
!= NULL
; opt
++) {
140 if (strncmp(name
, opt
->name
, strlen(name
)) == 0) {
141 *mntflags
|= opt
->mntmask
;
142 *zfsflags
|= opt
->zfsmask
;
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 * Return the pool/dataset to mount given the name passed to mount. This
224 * is expected to be of the form pool/dataset, however may also refer to
225 * a block device if that device contains a valid zfs label.
228 parse_dataset(char *dataset
)
231 struct stat64 statbuf
;
236 * We expect a pool/dataset to be provided, however if we're
237 * given a device which is a member of a zpool we attempt to
238 * extract the pool name stored in the label. Given the pool
239 * name we can mount the root dataset.
241 error
= stat64(dataset
, &statbuf
);
247 fd
= open(dataset
, O_RDONLY
);
251 error
= zpool_read_label(fd
, &config
, NULL
);
256 error
= nvlist_lookup_string(config
,
257 ZPOOL_CONFIG_POOL_NAME
, &name
);
261 dataset
= strdup(name
);
268 * If a file or directory in your current working directory is
269 * named 'dataset' then mount(8) will prepend your current working
270 * directory to the dataset. There is no way to prevent this
271 * behavior so we simply check for it and strip the prepended
272 * patch when it is added.
274 if (getcwd(cwd
, PATH_MAX
) == NULL
)
279 /* Do not add one when cwd already ends in a trailing '/' */
280 if (strncmp(cwd
, dataset
, len
) == 0)
281 return (dataset
+ len
+ (cwd
[len
-1] != '/'));
287 * Update the mtab_* code to use the libmount library when it is commonly
288 * available otherwise fallback to legacy mode. The mount(8) utility will
289 * manage the lock file for us to prevent racing updates to /etc/mtab.
292 mtab_is_writeable(void)
297 error
= lstat("/etc/mtab", &st
);
298 if (error
|| S_ISLNK(st
.st_mode
))
301 fd
= open("/etc/mtab", O_RDWR
| O_CREAT
, 0644);
310 mtab_update(char *dataset
, char *mntpoint
, char *type
, char *mntopts
)
316 mnt
.mnt_fsname
= dataset
;
317 mnt
.mnt_dir
= mntpoint
;
319 mnt
.mnt_opts
= mntopts
? mntopts
: "";
323 fp
= setmntent("/etc/mtab", "a+");
325 (void) fprintf(stderr
, gettext(
326 "filesystem '%s' was mounted, but /etc/mtab "
327 "could not be opened due to error %d\n"),
329 return (MOUNT_FILEIO
);
332 error
= addmntent(fp
, &mnt
);
334 (void) fprintf(stderr
, gettext(
335 "filesystem '%s' was mounted, but /etc/mtab "
336 "could not be updated due to error %d\n"),
338 return (MOUNT_FILEIO
);
341 (void) endmntent(fp
);
343 return (MOUNT_SUCCESS
);
347 append_mntopt(const char *name
, const char *val
, char *mntopts
,
348 char *mtabopt
, boolean_t quote
)
350 char tmp
[MNT_LINE_MAX
];
352 snprintf(tmp
, MNT_LINE_MAX
, quote
? ",%s=\"%s\"" : ",%s=%s", name
, val
);
355 strlcat(mntopts
, tmp
, MNT_LINE_MAX
);
358 strlcat(mtabopt
, tmp
, MNT_LINE_MAX
);
362 zfs_selinux_setcontext(zfs_handle_t
*zhp
, zfs_prop_t zpt
, const char *name
,
363 char *mntopts
, char *mtabopt
)
365 char context
[ZFS_MAXPROPLEN
];
367 if (zfs_prop_get(zhp
, zpt
, context
, sizeof (context
),
368 NULL
, NULL
, 0, B_FALSE
) == 0) {
369 if (strcmp(context
, "none") != 0)
370 append_mntopt(name
, context
, mntopts
, mtabopt
, B_TRUE
);
375 main(int argc
, char **argv
)
378 char prop
[ZFS_MAXPROPLEN
];
379 uint64_t zfs_version
= 0;
380 char mntopts
[MNT_LINE_MAX
] = { '\0' };
381 char badopt
[MNT_LINE_MAX
] = { '\0' };
382 char mtabopt
[MNT_LINE_MAX
] = { '\0' };
383 char mntpoint
[PATH_MAX
];
385 unsigned long mntflags
= 0, zfsflags
= 0, remount
= 0;
386 int sloppy
= 0, fake
= 0, verbose
= 0, nomtab
= 0, zfsutil
= 0;
389 (void) setlocale(LC_ALL
, "");
390 (void) textdomain(TEXT_DOMAIN
);
395 while ((c
= getopt_long(argc
, argv
, "sfnvo:h?", 0, 0)) != -1) {
410 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
414 (void) fprintf(stderr
, gettext("Invalid option '%c'\n"),
416 (void) fprintf(stderr
, gettext("Usage: mount.zfs "
417 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
418 return (MOUNT_USAGE
);
425 /* check that we only have two arguments */
428 (void) fprintf(stderr
, gettext("missing dataset "
431 (void) fprintf(stderr
,
432 gettext("missing mountpoint argument\n"));
434 (void) fprintf(stderr
, gettext("too many arguments\n"));
435 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
436 return (MOUNT_USAGE
);
439 dataset
= parse_dataset(argv
[0]);
441 /* canonicalize the mount point */
442 if (realpath(argv
[1], mntpoint
) == NULL
) {
443 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
444 "mounted at '%s' due to canonicalization error %d.\n"),
445 dataset
, argv
[1], errno
);
446 return (MOUNT_SYSERR
);
449 /* validate mount options and set mntflags */
450 error
= parse_options(mntopts
, &mntflags
, &zfsflags
, sloppy
,
455 (void) fprintf(stderr
, gettext("filesystem '%s' "
456 "cannot be mounted due to a memory allocation "
457 "failure.\n"), dataset
);
458 return (MOUNT_SYSERR
);
460 (void) fprintf(stderr
, gettext("filesystem '%s' "
461 "cannot be mounted due to invalid option "
462 "'%s'.\n"), dataset
, badopt
);
463 (void) fprintf(stderr
, gettext("Use the '-s' option "
464 "to ignore the bad mount option.\n"));
465 return (MOUNT_USAGE
);
467 (void) fprintf(stderr
, gettext("filesystem '%s' "
468 "cannot be mounted due to internal error %d.\n"),
470 return (MOUNT_SOFTWARE
);
475 (void) fprintf(stdout
, gettext("mount.zfs:\n"
476 " dataset: \"%s\"\n mountpoint: \"%s\"\n"
477 " mountflags: 0x%lx\n zfsflags: 0x%lx\n"
478 " mountopts: \"%s\"\n mtabopts: \"%s\"\n"),
479 dataset
, mntpoint
, mntflags
, zfsflags
, mntopts
, mtabopt
);
481 if (mntflags
& MS_REMOUNT
) {
486 if (zfsflags
& ZS_ZFSUTIL
)
489 if ((g_zfs
= libzfs_init()) == NULL
) {
490 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
491 return (MOUNT_SYSERR
);
494 /* try to open the dataset to access the mount point */
495 if ((zhp
= zfs_open(g_zfs
, dataset
,
496 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_SNAPSHOT
)) == NULL
) {
497 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
498 "mounted, unable to open the dataset\n"), dataset
);
500 return (MOUNT_USAGE
);
504 * Checks to see if the ZFS_PROP_SELINUX_CONTEXT exists
505 * if it does, create a tmp variable in case it's needed
506 * checks to see if the selinux context is set to the default
507 * if it is, allow the setting of the other context properties
508 * this is needed because the 'context' property overrides others
509 * if it is not the default, set the 'context' property
511 if (zfs_prop_get(zhp
, ZFS_PROP_SELINUX_CONTEXT
, prop
, sizeof (prop
),
512 NULL
, NULL
, 0, B_FALSE
) == 0) {
513 if (strcmp(prop
, "none") == 0) {
514 zfs_selinux_setcontext(zhp
, ZFS_PROP_SELINUX_FSCONTEXT
,
515 MNTOPT_FSCONTEXT
, mntopts
, mtabopt
);
516 zfs_selinux_setcontext(zhp
, ZFS_PROP_SELINUX_DEFCONTEXT
,
517 MNTOPT_DEFCONTEXT
, mntopts
, mtabopt
);
518 zfs_selinux_setcontext(zhp
,
519 ZFS_PROP_SELINUX_ROOTCONTEXT
, MNTOPT_ROOTCONTEXT
,
522 append_mntopt(MNTOPT_CONTEXT
, prop
,
523 mntopts
, mtabopt
, B_TRUE
);
527 /* A hint used to determine an auto-mounted snapshot mount point */
528 append_mntopt(MNTOPT_MNTPOINT
, mntpoint
, mntopts
, NULL
, B_FALSE
);
530 /* treat all snapshots as legacy mount points */
531 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
)
532 (void) strlcpy(prop
, ZFS_MOUNTPOINT_LEGACY
, ZFS_MAXPROPLEN
);
534 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, prop
,
535 sizeof (prop
), NULL
, NULL
, 0, B_FALSE
);
538 * Fetch the max supported zfs version in case we get ENOTSUP
539 * back from the mount command, since we need the zfs handle
542 zfs_version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
543 if (zfs_version
== 0) {
544 fprintf(stderr
, gettext("unable to fetch "
545 "ZFS version for filesystem '%s'\n"), dataset
);
546 return (MOUNT_SYSERR
);
553 * Legacy mount points may only be mounted using 'mount', never using
554 * 'zfs mount'. However, since 'zfs mount' actually invokes 'mount'
555 * we differentiate the two cases using the 'zfsutil' mount option.
556 * This mount option should only be supplied by the 'zfs mount' util.
558 * The only exception to the above rule is '-o remount' which is
559 * always allowed for non-legacy datasets. This is done because when
560 * using zfs as your root file system both rc.sysinit/umountroot and
561 * systemd depend on 'mount -o remount <mountpoint>' to work.
563 if (zfsutil
&& (strcmp(prop
, ZFS_MOUNTPOINT_LEGACY
) == 0)) {
564 (void) fprintf(stderr
, gettext(
565 "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
566 "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
567 "See zfs(8) for more information.\n"),
568 dataset
, mntpoint
, dataset
, mntpoint
);
569 return (MOUNT_USAGE
);
572 if (!zfsutil
&& !(remount
|| fake
) &&
573 strcmp(prop
, ZFS_MOUNTPOINT_LEGACY
)) {
574 (void) fprintf(stderr
, gettext(
575 "filesystem '%s' cannot be mounted using 'mount'.\n"
576 "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
577 "See zfs(8) for more information.\n"),
578 dataset
, "legacy", dataset
);
579 return (MOUNT_USAGE
);
583 error
= mount(dataset
, mntpoint
, MNTTYPE_ZFS
,
590 (void) fprintf(stderr
, gettext("mount point "
591 "'%s' does not exist\n"), mntpoint
);
592 return (MOUNT_SYSERR
);
594 (void) fprintf(stderr
, gettext("filesystem "
595 "'%s' is already mounted\n"), dataset
);
598 if (zfs_version
> ZPL_VERSION
) {
599 (void) fprintf(stderr
,
600 gettext("filesystem '%s' (v%d) is not "
601 "supported by this implementation of "
602 "ZFS (max v%d).\n"), dataset
,
603 (int) zfs_version
, (int) ZPL_VERSION
);
605 (void) fprintf(stderr
,
606 gettext("filesystem '%s' mount "
607 "failed for unknown reason.\n"), dataset
);
609 return (MOUNT_SYSERR
);
611 (void) fprintf(stderr
, gettext("filesystem "
612 "'%s' can not be mounted due to error "
613 "%d\n"), dataset
, errno
);
614 return (MOUNT_USAGE
);
618 if (!nomtab
&& mtab_is_writeable()) {
619 error
= mtab_update(dataset
, mntpoint
, MNTTYPE_ZFS
, mtabopt
);
624 return (MOUNT_SUCCESS
);