diff mbox

[OpenWrt-Devel] block: support to mount read-only devices

Message ID 1433039207-14268-1-git-send-email-nossiac@163.com
State Changes Requested
Delegated to: John Crispin
Headers show

Commit Message

Hua Shao May 31, 2015, 2:26 a.m. UTC
A read-only device will fail to mount without MS_RDONLY flag.

Signed-off-by: Hua Shao <nossiac@163.com>
---
 block.c | 49 ++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 38 insertions(+), 11 deletions(-)
diff mbox

Patch

diff --git a/block.c b/block.c
index 4c8dda9..a3c0ea5 100644
--- a/block.c
+++ b/block.c
@@ -734,11 +734,24 @@  static int mount_device(struct blkid_struct_probe *pr, int hotplug)
 
 		err = mount(pr->dev, target, pr->id->name, m->flags,
 		            (m->options) ? (m->options) : (""));
-		if (err)
-			ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
-			         pr->dev, pr->id->name, target, err, strerror(err));
-		else
-			handle_swapfiles(true);
+		while (1) {
+			err = mount(pr->dev, target, pr->id->name, mflags,
+						(m->options) ? (m->options) : (""));
+			if (err) {
+				if (!(mflags & MS_RDONLY)) {
+					ERROR("fall back on RO mount. %s.\n", strerror(errno));
+					mflags |= MS_RDONLY;
+					continue;
+				} else {
+					ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
+						pr->dev, pr->id->name, target, err, strerror(errno));
+					break;
+				}
+			} else {
+				handle_swapfiles(true);
+				break;
+			}
+		}
 		return err;
 	}
 
@@ -752,12 +765,26 @@  static int mount_device(struct blkid_struct_probe *pr, int hotplug)
 		if (check_fs)
 			check_filesystem(pr);
 
-		err = mount(pr->dev, target, pr->id->name, 0, "");
-		if (err)
-			ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
-			         pr->dev, pr->id->name, target, err, strerror(err));
-		else
-			handle_swapfiles(true);
+		err = mount(pr->dev, target, pr->id->name, m->flags,
+		            (m->options) ? (m->options) : (""));
+		while (1) {
+			err = mount(pr->dev, target, pr->id->name, mflags,
+						(m->options) ? (m->options) : (""));
+			if (err) {
+				if (!(mflags & MS_RDONLY)) {
+					ERROR("fall back on RO mount. %s.\n", strerror(errno));
+					mflags |= MS_RDONLY;
+					continue;
+				} else {
+					ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
+						pr->dev, pr->id->name, target, err, strerror(errno));
+					break;
+				}
+			} else {
+				handle_swapfiles(true);
+				break;
+			}
+		}
 		return err;
 	}