From dbdf9e0ab1bfe65b75ba20cdf26c8fded2174c60 Mon Sep 17 00:00:00 2001
From: Denys Vlasenko <vda.linux@googlemail.com>
Date: Mon, 23 Aug 2021 02:30:13 +0200
Subject: mount: with -w, do not fall back to read-only mounts

function                                             old     new   delta
mount_it_now                                         364     358      -6

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
---
 util-linux/mount.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/util-linux/mount.c b/util-linux/mount.c
index 831dab9e2..5bc60de59 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -713,10 +713,12 @@ static int mount_it_now(struct mntent *mp, unsigned long vfsflags, char *filtero
 		errno = 0;
 		rc = verbose_mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
 				vfsflags, filteropts);
+		if (rc == 0)
+			goto mtab; // success
 
-		// If mount failed, try
-		// helper program mount.<mnt_type>
-		if (HELPERS_ALLOWED && rc && mp->mnt_type) {
+		// mount failed, try helper program
+		// mount.<mnt_type>
+		if (HELPERS_ALLOWED && mp->mnt_type) {
 			char *args[8];
 			int errno_save = errno;
 			args[0] = xasprintf("mount.%s", mp->mnt_type);
@@ -734,13 +736,19 @@ static int mount_it_now(struct mntent *mp, unsigned long vfsflags, char *filtero
 			args[rc] = NULL;
 			rc = spawn_and_wait(args);
 			free(args[0]);
-			if (!rc)
-				break;
+			if (rc == 0)
+				goto mtab; // success
 			errno = errno_save;
 		}
 
-		if (!rc || (vfsflags & MS_RDONLY) || (errno != EACCES && errno != EROFS))
-			break;
+		// Should we retry read-only mount?
+		if (vfsflags & MS_RDONLY)
+			break;		// no, already was tried
+		if (option_mask32 & OPT_w)
+			break;		// no, "mount -w" never falls back to RO
+		if (errno != EACCES && errno != EROFS)
+			break;		// no, error isn't hinting that RO may work
+
 		if (!(vfsflags & MS_SILENT))
 			bb_error_msg("%s is write-protected, mounting read-only",
 						mp->mnt_fsname);
-- 
cgit v1.2.3-55-g6feb