From 1aaa9531bb1621a9d90ffc801955b750504816c9 Mon Sep 17 00:00:00 2001 From: avg Date: Thu, 19 Apr 2018 05:52:47 +0000 Subject: [PATCH] MFC r331616: vfs_donmount: in certain cases try r/o mount if r/w mount fails If the operation is not an update, if neither r/w nor r/o mode is explicitly requested, if the error code hints at the possibility of the media being read-only, and if the fallback is allowed, then we can try to automatically downgrade to the readonly mode. This is especially useful for auto-mounting of removable media that sometimes can happen to be write-protected. The fallback to r/o is not enabled by default. It can be requested on a per-mount basis with a new mount option, 'autoro'. Or it can be globally allowed by setting vfs.default_autoro. stable/10 note: this branch does not have SYSCTL_BOOL, so SYSCTL_INT is used instead. Relnotes: yes git-svn-id: svn://svn.freebsd.org/base/stable/10@332754 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- sbin/mount/mount.8 | 4 +++ sys/kern/vfs_mount.c | 61 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/sbin/mount/mount.8 b/sbin/mount/mount.8 index 51ae7c57f..18b069920 100644 --- a/sbin/mount/mount.8 +++ b/sbin/mount/mount.8 @@ -155,6 +155,10 @@ This flag indicates that the file system was mounted by .Xr automountd 8 . Automounted file systems are automatically unmounted by .Xr autounmountd 8 . +.It Cm autoro +Mount the file system read-write. +If that fails with an error that suggests that the media could be read-only, +then automatically try to mount the file system read-only. .It Cm current When used with the .Fl u diff --git a/sys/kern/vfs_mount.c b/sys/kern/vfs_mount.c index e01dac1b6..dc62cfa5f 100644 --- a/sys/kern/vfs_mount.c +++ b/sys/kern/vfs_mount.c @@ -78,6 +78,10 @@ static int usermount = 0; SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "Unprivileged users may mount and unmount file systems"); +static int default_autoro = false; +SYSCTL_INT(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0, + "Retry failed r/w mount as r/o if no explicit ro/rw option is specified"); + MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); static uma_zone_t mount_zone; @@ -532,6 +536,31 @@ vfs_mount_destroy(struct mount *mp) uma_zfree(mount_zone, mp); } +static bool +vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error) +{ + /* This is an upgrade of an exisiting mount. */ + if ((fsflags & MNT_UPDATE) != 0) + return (false); + /* This is already an R/O mount. */ + if ((fsflags & MNT_RDONLY) != 0) + return (false); + + switch (error) { + case ENODEV: /* generic, geom, ... */ + case EACCES: /* cam/scsi, ... */ + case EROFS: /* md, mmcsd, ... */ + /* + * These errors can be returned by the storage layer to signal + * that the media is read-only. No harm in the R/O mount + * attempt if the error was returned for some other reason. + */ + return (true); + default: + return (false); + } +} + int vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions) { @@ -539,10 +568,12 @@ vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions) struct vfsopt *opt, *tmp_opt; char *fstype, *fspath, *errmsg; int error, fstypelen, fspathlen, errmsg_len, errmsg_pos; + bool autoro; errmsg = fspath = NULL; errmsg_len = fspathlen = 0; errmsg_pos = -1; + autoro = default_autoro; error = vfs_buildopts(fsoptions, &optlist); if (error) @@ -634,16 +665,27 @@ vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions) free(opt->name, M_MOUNT); opt->name = strdup("nonosymfollow", M_MOUNT); } - else if (strcmp(opt->name, "noro") == 0) + else if (strcmp(opt->name, "noro") == 0) { fsflags &= ~MNT_RDONLY; - else if (strcmp(opt->name, "rw") == 0) + autoro = false; + } + else if (strcmp(opt->name, "rw") == 0) { fsflags &= ~MNT_RDONLY; - else if (strcmp(opt->name, "ro") == 0) + autoro = false; + } + else if (strcmp(opt->name, "ro") == 0) { fsflags |= MNT_RDONLY; + autoro = false; + } else if (strcmp(opt->name, "rdonly") == 0) { free(opt->name, M_MOUNT); opt->name = strdup("ro", M_MOUNT); fsflags |= MNT_RDONLY; + autoro = false; + } + else if (strcmp(opt->name, "autoro") == 0) { + vfs_freeopt(optlist, opt); + autoro = true; } else if (strcmp(opt->name, "suiddir") == 0) fsflags |= MNT_SUIDDIR; @@ -668,6 +710,19 @@ vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions) } error = vfs_domount(td, fstype, fspath, fsflags, &optlist); + + /* + * See if we can mount in the read-only mode if the error code suggests + * that it could be possible and the mount options allow for that. + * Never try it if "[no]{ro|rw}" has been explicitly requested and not + * overridden by "autoro". + */ + if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) { + printf("%s: R/W mount failed, possibly R/O media," + " trying R/O mount\n", __func__); + fsflags |= MNT_RDONLY; + error = vfs_domount(td, fstype, fspath, fsflags, &optlist); + } bail: /* copyout the errmsg */ if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt) -- 2.45.0