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>
40 #define ZS_COMMENT 0x00000000 /* comment */
41 #define ZS_ZFSUTIL 0x00000001 /* caller is zfs(8) */
43 libzfs_handle_t
*g_zfs
;
45 typedef struct option_map
{
47 unsigned long mntmask
;
48 unsigned long zfsmask
;
51 static const option_map_t option_map
[] = {
52 /* Canonicalized filesystem independent options from mount(8) */
53 { MNTOPT_NOAUTO
, MS_COMMENT
, ZS_COMMENT
},
54 { MNTOPT_DEFAULTS
, MS_COMMENT
, ZS_COMMENT
},
55 { MNTOPT_NODEVICES
, MS_NODEV
, ZS_COMMENT
},
56 { MNTOPT_DIRSYNC
, MS_DIRSYNC
, ZS_COMMENT
},
57 { MNTOPT_NOEXEC
, MS_NOEXEC
, ZS_COMMENT
},
58 { MNTOPT_GROUP
, MS_GROUP
, ZS_COMMENT
},
59 { MNTOPT_NETDEV
, MS_COMMENT
, ZS_COMMENT
},
60 { MNTOPT_NOFAIL
, MS_COMMENT
, ZS_COMMENT
},
61 { MNTOPT_NOSUID
, MS_NOSUID
, ZS_COMMENT
},
62 { MNTOPT_OWNER
, MS_OWNER
, ZS_COMMENT
},
63 { MNTOPT_REMOUNT
, MS_REMOUNT
, ZS_COMMENT
},
64 { MNTOPT_RO
, MS_RDONLY
, ZS_COMMENT
},
65 { MNTOPT_RW
, MS_COMMENT
, ZS_COMMENT
},
66 { MNTOPT_SYNC
, MS_SYNCHRONOUS
, ZS_COMMENT
},
67 { MNTOPT_USER
, MS_USERS
, ZS_COMMENT
},
68 { MNTOPT_USERS
, MS_USERS
, ZS_COMMENT
},
69 /* acl flags passed with util-linux-2.24 mount command */
70 { MNTOPT_ACL
, MS_POSIXACL
, ZS_COMMENT
},
71 { MNTOPT_NOACL
, MS_COMMENT
, ZS_COMMENT
},
72 { MNTOPT_POSIXACL
, MS_POSIXACL
, ZS_COMMENT
},
74 { MNTOPT_NOATIME
, MS_NOATIME
, ZS_COMMENT
},
77 { MNTOPT_NODIRATIME
, MS_NODIRATIME
, ZS_COMMENT
},
80 { MNTOPT_RELATIME
, MS_RELATIME
, ZS_COMMENT
},
83 { MNTOPT_STRICTATIME
, MS_STRICTATIME
, ZS_COMMENT
},
86 { MNTOPT_LAZYTIME
, MS_LAZYTIME
, ZS_COMMENT
},
88 { MNTOPT_CONTEXT
, MS_COMMENT
, ZS_COMMENT
},
89 { MNTOPT_FSCONTEXT
, MS_COMMENT
, ZS_COMMENT
},
90 { MNTOPT_DEFCONTEXT
, MS_COMMENT
, ZS_COMMENT
},
91 { MNTOPT_ROOTCONTEXT
, MS_COMMENT
, ZS_COMMENT
},
93 { MNTOPT_IVERSION
, MS_I_VERSION
, ZS_COMMENT
},
96 { MNTOPT_NBMAND
, MS_MANDLOCK
, ZS_COMMENT
},
98 /* Valid options not found in mount(8) */
99 { MNTOPT_BIND
, MS_BIND
, ZS_COMMENT
},
101 { MNTOPT_RBIND
, MS_BIND
|MS_REC
, ZS_COMMENT
},
103 { MNTOPT_COMMENT
, MS_COMMENT
, ZS_COMMENT
},
105 { MNTOPT_NOSUB
, MS_NOSUB
, ZS_COMMENT
},
108 { MNTOPT_QUIET
, MS_SILENT
, ZS_COMMENT
},
110 /* Custom zfs options */
111 { MNTOPT_XATTR
, MS_COMMENT
, ZS_COMMENT
},
112 { MNTOPT_NOXATTR
, MS_COMMENT
, ZS_COMMENT
},
113 { MNTOPT_ZFSUTIL
, MS_COMMENT
, ZS_ZFSUTIL
},
117 * Break the mount option in to a name/value pair. The name is
118 * validated against the option map and mount flags set accordingly.
121 parse_option(char *mntopt
, unsigned long *mntflags
,
122 unsigned long *zfsflags
, int sloppy
)
124 const option_map_t
*opt
;
125 char *ptr
, *name
, *value
= NULL
;
128 name
= strdup(mntopt
);
132 for (ptr
= name
; ptr
&& *ptr
; ptr
++) {
136 VERIFY3P(value
, !=, NULL
);
141 for (opt
= option_map
; opt
->name
!= NULL
; opt
++) {
142 if (strncmp(name
, opt
->name
, strlen(name
)) == 0) {
143 *mntflags
|= opt
->mntmask
;
144 *zfsflags
|= opt
->zfsmask
;
153 /* If required further process on the value may be done here */
159 * Translate the mount option string in to MS_* mount flags for the
160 * kernel vfs. When sloppy is non-zero unknown options will be ignored
161 * otherwise they are considered fatal are copied in to badopt.
164 parse_options(char *mntopts
, unsigned long *mntflags
, unsigned long *zfsflags
,
165 int sloppy
, char *badopt
, char *mtabopt
)
167 int error
= 0, quote
= 0, flag
= 0, count
= 0;
168 char *ptr
, *opt
, *opts
;
170 opts
= strdup(mntopts
);
178 * Scan through all mount options which must be comma delimited.
179 * We must be careful to notice regions which are double quoted
180 * and skip commas in these regions. Each option is then checked
181 * to determine if it is a known option.
183 for (ptr
= opts
; ptr
&& !flag
; ptr
++) {
196 if ((*ptr
== ',') || (*ptr
== '\0')) {
199 error
= parse_option(opt
, mntflags
, zfsflags
, sloppy
);
206 if (!(*mntflags
& MS_REMOUNT
) &&
207 !(*zfsflags
& ZS_ZFSUTIL
)) {
209 strlcat(mtabopt
, ",", MNT_LINE_MAX
);
211 strlcat(mtabopt
, opt
, MNT_LINE_MAX
);
225 * Return the pool/dataset to mount given the name passed to mount. This
226 * is expected to be of the form pool/dataset, however may also refer to
227 * a block device if that device contains a valid zfs label.
230 parse_dataset(char *dataset
)
233 struct stat64 statbuf
;
238 * We expect a pool/dataset to be provided, however if we're
239 * given a device which is a member of a zpool we attempt to
240 * extract the pool name stored in the label. Given the pool
241 * name we can mount the root dataset.
243 error
= stat64(dataset
, &statbuf
);
249 fd
= open(dataset
, O_RDONLY
);
253 error
= zpool_read_label(fd
, &config
, NULL
);
258 error
= nvlist_lookup_string(config
,
259 ZPOOL_CONFIG_POOL_NAME
, &name
);
263 dataset
= strdup(name
);
270 * If a file or directory in your current working directory is
271 * named 'dataset' then mount(8) will prepend your current working
272 * directory to the dataset. There is no way to prevent this
273 * behavior so we simply check for it and strip the prepended
274 * patch when it is added.
276 if (getcwd(cwd
, PATH_MAX
) == NULL
)
281 /* Do not add one when cwd already ends in a trailing '/' */
282 if (strncmp(cwd
, dataset
, len
) == 0)
283 return (dataset
+ len
+ (cwd
[len
-1] != '/'));
289 * Update the mtab_* code to use the libmount library when it is commonly
290 * available otherwise fallback to legacy mode. The mount(8) utility will
291 * manage the lock file for us to prevent racing updates to /etc/mtab.
294 mtab_is_writeable(void)
299 error
= lstat("/etc/mtab", &st
);
300 if (error
|| S_ISLNK(st
.st_mode
))
303 fd
= open("/etc/mtab", O_RDWR
| O_CREAT
, 0644);
312 mtab_update(char *dataset
, char *mntpoint
, char *type
, char *mntopts
)
318 mnt
.mnt_fsname
= dataset
;
319 mnt
.mnt_dir
= mntpoint
;
321 mnt
.mnt_opts
= mntopts
? mntopts
: "";
325 fp
= setmntent("/etc/mtab", "a+");
327 (void) fprintf(stderr
, gettext(
328 "filesystem '%s' was mounted, but /etc/mtab "
329 "could not be opened due to error %d\n"),
331 return (MOUNT_FILEIO
);
334 error
= addmntent(fp
, &mnt
);
336 (void) fprintf(stderr
, gettext(
337 "filesystem '%s' was mounted, but /etc/mtab "
338 "could not be updated due to error %d\n"),
340 return (MOUNT_FILEIO
);
343 (void) endmntent(fp
);
345 return (MOUNT_SUCCESS
);
349 append_mntopt(const char *name
, const char *val
, char *mntopts
,
350 char *mtabopt
, boolean_t quote
)
352 char tmp
[MNT_LINE_MAX
];
354 snprintf(tmp
, MNT_LINE_MAX
, quote
? ",%s=\"%s\"" : ",%s=%s", name
, val
);
357 strlcat(mntopts
, tmp
, MNT_LINE_MAX
);
360 strlcat(mtabopt
, tmp
, MNT_LINE_MAX
);
364 zfs_selinux_setcontext(zfs_handle_t
*zhp
, zfs_prop_t zpt
, const char *name
,
365 char *mntopts
, char *mtabopt
)
367 char context
[ZFS_MAXPROPLEN
];
369 if (zfs_prop_get(zhp
, zpt
, context
, sizeof (context
),
370 NULL
, NULL
, 0, B_FALSE
) == 0) {
371 if (strcmp(context
, "none") != 0)
372 append_mntopt(name
, context
, mntopts
, mtabopt
, B_TRUE
);
377 main(int argc
, char **argv
)
380 char prop
[ZFS_MAXPROPLEN
];
381 uint64_t zfs_version
= 0;
382 char mntopts
[MNT_LINE_MAX
] = { '\0' };
383 char badopt
[MNT_LINE_MAX
] = { '\0' };
384 char mtabopt
[MNT_LINE_MAX
] = { '\0' };
385 char mntpoint
[PATH_MAX
];
387 unsigned long mntflags
= 0, zfsflags
= 0, remount
= 0;
388 int sloppy
= 0, fake
= 0, verbose
= 0, nomtab
= 0, zfsutil
= 0;
391 (void) setlocale(LC_ALL
, "");
392 (void) textdomain(TEXT_DOMAIN
);
397 while ((c
= getopt_long(argc
, argv
, "sfnvo:h?", 0, 0)) != -1) {
412 (void) strlcpy(mntopts
, optarg
, sizeof (mntopts
));
416 (void) fprintf(stderr
, gettext("Invalid option '%c'\n"),
418 (void) fprintf(stderr
, gettext("Usage: mount.zfs "
419 "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
420 return (MOUNT_USAGE
);
427 /* check that we only have two arguments */
430 (void) fprintf(stderr
, gettext("missing dataset "
433 (void) fprintf(stderr
,
434 gettext("missing mountpoint argument\n"));
436 (void) fprintf(stderr
, gettext("too many arguments\n"));
437 (void) fprintf(stderr
, "usage: mount <dataset> <mountpoint>\n");
438 return (MOUNT_USAGE
);
441 dataset
= parse_dataset(argv
[0]);
443 /* canonicalize the mount point */
444 if (realpath(argv
[1], mntpoint
) == NULL
) {
445 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
446 "mounted at '%s' due to canonicalization error %d.\n"),
447 dataset
, argv
[1], errno
);
448 return (MOUNT_SYSERR
);
451 /* validate mount options and set mntflags */
452 error
= parse_options(mntopts
, &mntflags
, &zfsflags
, sloppy
,
457 (void) fprintf(stderr
, gettext("filesystem '%s' "
458 "cannot be mounted due to a memory allocation "
459 "failure.\n"), dataset
);
460 return (MOUNT_SYSERR
);
462 (void) fprintf(stderr
, gettext("filesystem '%s' "
463 "cannot be mounted due to invalid option "
464 "'%s'.\n"), dataset
, badopt
);
465 (void) fprintf(stderr
, gettext("Use the '-s' option "
466 "to ignore the bad mount option.\n"));
467 return (MOUNT_USAGE
);
469 (void) fprintf(stderr
, gettext("filesystem '%s' "
470 "cannot be mounted due to internal error %d.\n"),
472 return (MOUNT_SOFTWARE
);
477 (void) fprintf(stdout
, gettext("mount.zfs:\n"
478 " dataset: \"%s\"\n mountpoint: \"%s\"\n"
479 " mountflags: 0x%lx\n zfsflags: 0x%lx\n"
480 " mountopts: \"%s\"\n mtabopts: \"%s\"\n"),
481 dataset
, mntpoint
, mntflags
, zfsflags
, mntopts
, mtabopt
);
483 if (mntflags
& MS_REMOUNT
) {
488 if (zfsflags
& ZS_ZFSUTIL
)
491 if ((g_zfs
= libzfs_init()) == NULL
) {
492 (void) fprintf(stderr
, "%s", libzfs_error_init(errno
));
493 return (MOUNT_SYSERR
);
496 /* try to open the dataset to access the mount point */
497 if ((zhp
= zfs_open(g_zfs
, dataset
,
498 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_SNAPSHOT
)) == NULL
) {
499 (void) fprintf(stderr
, gettext("filesystem '%s' cannot be "
500 "mounted, unable to open the dataset\n"), dataset
);
502 return (MOUNT_USAGE
);
506 * Checks to see if the ZFS_PROP_SELINUX_CONTEXT exists
507 * if it does, create a tmp variable in case it's needed
508 * checks to see if the selinux context is set to the default
509 * if it is, allow the setting of the other context properties
510 * this is needed because the 'context' property overrides others
511 * if it is not the default, set the 'context' property
513 if (zfs_prop_get(zhp
, ZFS_PROP_SELINUX_CONTEXT
, prop
, sizeof (prop
),
514 NULL
, NULL
, 0, B_FALSE
) == 0) {
515 if (strcmp(prop
, "none") == 0) {
516 zfs_selinux_setcontext(zhp
, ZFS_PROP_SELINUX_FSCONTEXT
,
517 MNTOPT_FSCONTEXT
, mntopts
, mtabopt
);
518 zfs_selinux_setcontext(zhp
, ZFS_PROP_SELINUX_DEFCONTEXT
,
519 MNTOPT_DEFCONTEXT
, mntopts
, mtabopt
);
520 zfs_selinux_setcontext(zhp
,
521 ZFS_PROP_SELINUX_ROOTCONTEXT
, MNTOPT_ROOTCONTEXT
,
524 append_mntopt(MNTOPT_CONTEXT
, prop
,
525 mntopts
, mtabopt
, B_TRUE
);
529 /* A hint used to determine an auto-mounted snapshot mount point */
530 append_mntopt(MNTOPT_MNTPOINT
, mntpoint
, mntopts
, NULL
, B_FALSE
);
532 /* treat all snapshots as legacy mount points */
533 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
)
534 (void) strlcpy(prop
, ZFS_MOUNTPOINT_LEGACY
, ZFS_MAXPROPLEN
);
536 (void) zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, prop
,
537 sizeof (prop
), NULL
, NULL
, 0, B_FALSE
);
540 * Fetch the max supported zfs version in case we get ENOTSUP
541 * back from the mount command, since we need the zfs handle
544 zfs_version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
545 if (zfs_version
== 0) {
546 fprintf(stderr
, gettext("unable to fetch "
547 "ZFS version for filesystem '%s'\n"), dataset
);
548 return (MOUNT_SYSERR
);
555 * Legacy mount points may only be mounted using 'mount', never using
556 * 'zfs mount'. However, since 'zfs mount' actually invokes 'mount'
557 * we differentiate the two cases using the 'zfsutil' mount option.
558 * This mount option should only be supplied by the 'zfs mount' util.
560 * The only exception to the above rule is '-o remount' which is
561 * always allowed for non-legacy datasets. This is done because when
562 * using zfs as your root file system both rc.sysinit/umountroot and
563 * systemd depend on 'mount -o remount <mountpoint>' to work.
565 if (zfsutil
&& (strcmp(prop
, ZFS_MOUNTPOINT_LEGACY
) == 0)) {
566 (void) fprintf(stderr
, gettext(
567 "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
568 "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
569 "See zfs(8) for more information.\n"),
570 dataset
, mntpoint
, dataset
, mntpoint
);
571 return (MOUNT_USAGE
);
574 if (!zfsutil
&& !(remount
|| fake
) &&
575 strcmp(prop
, ZFS_MOUNTPOINT_LEGACY
)) {
576 (void) fprintf(stderr
, gettext(
577 "filesystem '%s' cannot be mounted using 'mount'.\n"
578 "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
579 "See zfs(8) for more information.\n"),
580 dataset
, "legacy", dataset
);
581 return (MOUNT_USAGE
);
585 error
= mount(dataset
, mntpoint
, MNTTYPE_ZFS
,
592 (void) fprintf(stderr
, gettext("mount point "
593 "'%s' does not exist\n"), mntpoint
);
594 return (MOUNT_SYSERR
);
596 (void) fprintf(stderr
, gettext("filesystem "
597 "'%s' is already mounted\n"), dataset
);
600 if (zfs_version
> ZPL_VERSION
) {
601 (void) fprintf(stderr
,
602 gettext("filesystem '%s' (v%d) is not "
603 "supported by this implementation of "
604 "ZFS (max v%d).\n"), dataset
,
605 (int)zfs_version
, (int)ZPL_VERSION
);
607 (void) fprintf(stderr
,
608 gettext("filesystem '%s' mount "
609 "failed for unknown reason.\n"), dataset
);
611 return (MOUNT_SYSERR
);
614 if (mntflags
& MS_MANDLOCK
) {
615 (void) fprintf(stderr
, gettext("filesystem "
616 "'%s' has the 'nbmand=on' property set, "
617 "this mount\noption may be disabled in "
618 "your kernel. Use 'zfs set nbmand=off'\n"
619 "to disable this option and try to "
620 "mount the filesystem again.\n"), dataset
);
621 return (MOUNT_SYSERR
);
626 (void) fprintf(stderr
, gettext("filesystem "
627 "'%s' can not be mounted: %s\n"), dataset
,
629 return (MOUNT_USAGE
);
633 if (!nomtab
&& mtab_is_writeable()) {
634 error
= mtab_update(dataset
, mntpoint
, MNTTYPE_ZFS
, mtabopt
);
639 return (MOUNT_SUCCESS
);