X-Git-Url: https://git.camperquake.de/gitweb.cgi?a=blobdiff_plain;f=cmd%2Fmount_zfs%2Fmount_zfs.c;h=45c291e04ce000ab3d56d84d6da58d4c8d7b1a28;hb=92e91da20839ee50536223bedf2ba4fb7d2fae71;hp=fbb954acd2200529f8b110de48ae9335c79eadb5;hpb=a6cba65ccae5a5458f9ab4ac11020716d30ea7f7;p=zfs.git diff --git a/cmd/mount_zfs/mount_zfs.c b/cmd/mount_zfs/mount_zfs.c index fbb954a..45c291e 100644 --- a/cmd/mount_zfs/mount_zfs.c +++ b/cmd/mount_zfs/mount_zfs.c @@ -30,6 +30,7 @@ #include #include #include +#include #ifdef HAVE_LIBSELINUX #include #endif /* HAVE_LIBSELINUX */ @@ -72,13 +73,11 @@ static const option_map_t option_map[] = { #ifdef MS_STRICTATIME { MNTOPT_DFRATIME, MS_STRICTATIME, ZS_COMMENT }, #endif -#ifdef HAVE_SELINUX { MNTOPT_CONTEXT, MS_COMMENT, ZS_NOCONTEXT }, { MNTOPT_NOCONTEXT, MS_COMMENT, ZS_NOCONTEXT }, { MNTOPT_FSCONTEXT, MS_COMMENT, ZS_NOCONTEXT }, { MNTOPT_DEFCONTEXT, MS_COMMENT, ZS_NOCONTEXT }, { MNTOPT_ROOTCONTEXT, MS_COMMENT, ZS_NOCONTEXT }, -#endif #ifdef MS_I_VERSION { MNTOPT_IVERSION, MS_I_VERSION, ZS_COMMENT }, #endif @@ -98,6 +97,7 @@ static const option_map_t option_map[] = { { MNTOPT_QUIET, MS_SILENT, ZS_COMMENT }, #endif /* Custom zfs options */ + { MNTOPT_XATTR, MS_COMMENT, ZS_COMMENT }, { MNTOPT_NOXATTR, MS_COMMENT, ZS_COMMENT }, { MNTOPT_ZFSUTIL, MS_COMMENT, ZS_ZFSUTIL }, { NULL, 0, 0 } }; @@ -122,6 +122,7 @@ parse_option(char *mntopt, unsigned long *mntflags, if (*ptr == '=') { *ptr = '\0'; value = ptr+1; + VERIFY3P(value, !=, NULL); break; } } @@ -158,10 +159,10 @@ out: * otherwise they are considered fatal are copied in to badopt. */ static int -parse_options(char *mntopts, unsigned long *mntflags, - unsigned long *zfsflags, int sloppy, char *badopt) +parse_options(char *mntopts, unsigned long *mntflags, unsigned long *zfsflags, + int sloppy, char *badopt, char *mtabopt) { - int error = 0, quote = 0, flag = 0; + int error = 0, quote = 0, flag = 0, count = 0; char *ptr, *opt, *opts; opts = strdup(mntopts); @@ -197,6 +198,16 @@ parse_options(char *mntopts, unsigned long *mntflags, if (error) { strcpy(badopt, opt); goto out; + + } + + if (!(*mntflags & MS_REMOUNT) && + !(*zfsflags & ZS_ZFSUTIL)) { + if (count > 0) + strlcat(mtabopt, ",", MNT_LINE_MAX); + + strlcat(mtabopt, opt, MNT_LINE_MAX); + count++; } opt = NULL; @@ -220,7 +231,9 @@ parse_dataset(char *dataset) char cwd[PATH_MAX]; int len; - (void) getcwd(cwd, PATH_MAX); + if (getcwd(cwd, PATH_MAX) == NULL) + return (dataset); + len = strlen(cwd); /* Do not add one when cwd already ends in a trailing '/' */ @@ -241,7 +254,7 @@ mtab_is_writeable(void) struct stat st; int error, fd; - error = stat(MNTTAB, &st); + error = lstat(MNTTAB, &st); if (error || S_ISLNK(st.st_mode)) return (0); @@ -297,8 +310,9 @@ main(int argc, char **argv) char legacy[ZFS_MAXPROPLEN]; char mntopts[MNT_LINE_MAX] = { '\0' }; char badopt[MNT_LINE_MAX] = { '\0' }; + char mtabopt[MNT_LINE_MAX] = { '\0' }; char *dataset, *mntpoint; - unsigned long mntflags = 0, zfsflags = 0; + unsigned long mntflags = 0, zfsflags = 0, remount_ro = 0; int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0; int error, c; @@ -356,24 +370,25 @@ main(int argc, char **argv) mntpoint = argv[1]; /* validate mount options and set mntflags */ - error = parse_options(mntopts, &mntflags, &zfsflags, sloppy, badopt); + error = parse_options(mntopts, &mntflags, &zfsflags, sloppy, + badopt, mtabopt); if (error) { switch (error) { case ENOMEM: (void) fprintf(stderr, gettext("filesystem '%s' " "cannot be mounted due to a memory allocation " - "failure\n"), dataset); + "failure.\n"), dataset); return (MOUNT_SYSERR); - case EINVAL: + case ENOENT: (void) fprintf(stderr, gettext("filesystem '%s' " - "cannot be mounted of due to the invalid option " - "'%s'\n"), dataset, badopt); + "cannot be mounted of due invalid option " + "'%s'.\n"), dataset, badopt); (void) fprintf(stderr, gettext("Use the '-s' option " "to ignore the bad mount option.\n")); return (MOUNT_USAGE); default: (void) fprintf(stderr, gettext("filesystem '%s' " - "cannot be mounted due to internal error %d\n"), + "cannot be mounted due to internal error %d.\n"), dataset, error); return (MOUNT_SOFTWARE); } @@ -386,9 +401,12 @@ main(int argc, char **argv) * done until zfs is added to the default selinux policy configuration * as a known filesystem type which supports xattrs. */ - if (is_selinux_enabled() && !(zfsflags & ZS_NOCONTEXT)) + if (is_selinux_enabled() && !(zfsflags & ZS_NOCONTEXT)) { (void) strlcat(mntopts, ",context=\"system_u:" "object_r:file_t:s0\"", sizeof (mntopts)); + (void) strlcat(mtabopt, ",context=\"system_u:" + "object_r:file_t:s0\"", sizeof (mtabopt)); + } #endif /* HAVE_LIBSELINUX */ @@ -396,28 +414,36 @@ main(int argc, char **argv) (void) fprintf(stdout, gettext("mount.zfs:\n" " dataset: \"%s\"\n mountpoint: \"%s\"\n" " mountflags: 0x%lx\n zfsflags: 0x%lx\n" - " mountopts: \"%s\"\n\n"), - dataset, mntpoint, mntflags, zfsflags, mntopts); + " mountopts: \"%s\"\n mtabopts: \"%s\"\n"), + dataset, mntpoint, mntflags, zfsflags, mntopts, mtabopt); if (mntflags & MS_REMOUNT) nomtab = 1; - if (zfsflags * ZS_ZFSUTIL) + if ((mntflags & MS_REMOUNT) && (mntflags & MS_RDONLY)) + remount_ro = 1; + + if (zfsflags & ZS_ZFSUTIL) zfsutil = 1; if ((g_zfs = libzfs_init()) == NULL) return (MOUNT_SYSERR); /* try to open the dataset to access the mount point */ - if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) { + if ((zhp = zfs_open(g_zfs, dataset, + ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) { (void) fprintf(stderr, gettext("filesystem '%s' cannot be " "mounted, unable to open the dataset\n"), dataset); libzfs_fini(g_zfs); return (MOUNT_USAGE); } - (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, legacy, - sizeof (legacy), NULL, NULL, 0, B_FALSE); + /* treat all snapshots as legacy mount points */ + if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) + (void) strlcpy(legacy, ZFS_MOUNTPOINT_LEGACY, ZFS_MAXPROPLEN); + else + (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, legacy, + sizeof (legacy), NULL, NULL, 0, B_FALSE); zfs_close(zhp); libzfs_fini(g_zfs); @@ -427,6 +453,10 @@ main(int argc, char **argv) * 'zfs mount'. However, since 'zfs mount' actually invokes 'mount' * we differentiate the two cases using the 'zfsutil' mount option. * This mount option should only be supplied by the 'zfs mount' util. + * + * The only exception to the above rule is '-o remount,ro'. This is + * always allowed for non-legacy datasets for rc.sysinit/umountroot + * to safely remount the root filesystem and flush its cache. */ if (zfsutil && !strcmp(legacy, ZFS_MOUNTPOINT_LEGACY)) { (void) fprintf(stderr, gettext( @@ -437,7 +467,7 @@ main(int argc, char **argv) return (MOUNT_USAGE); } - if (!zfsutil && strcmp(legacy, ZFS_MOUNTPOINT_LEGACY)) { + if (!zfsutil && strcmp(legacy, ZFS_MOUNTPOINT_LEGACY) && !remount_ro) { (void) fprintf(stderr, gettext( "filesystem '%s' cannot be mounted using 'mount'.\n" "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n" @@ -451,6 +481,10 @@ main(int argc, char **argv) mntflags, mntopts); if (error) { switch (errno) { + case ENOENT: + (void) fprintf(stderr, gettext("mount point " + "'%s' does not exist\n"), mntpoint); + return (MOUNT_SYSERR); case EBUSY: (void) fprintf(stderr, gettext("filesystem " "'%s' is already mounted\n"), dataset); @@ -465,7 +499,7 @@ main(int argc, char **argv) } if (!nomtab && mtab_is_writeable()) { - error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mntopts); + error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mtabopt); if (error) return (error); }