diff mbox series

[OpenWrt-Devel,fstools,WIP] block: check for parent mount point before mounting

Message ID 20200402161513.19036-1-zajec5@gmail.com
State RFC
Delegated to: Rafał Miłecki
Headers show
Series [OpenWrt-Devel,fstools,WIP] block: check for parent mount point before mounting | expand

Commit Message

Rafał Miłecki April 2, 2020, 4:15 p.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

Some devices may have mount target set to directory used by another
device. Detect it and print a warning.

In future we should mount such "parent" devices first.

Cc: Yousong Zhou <yszhou4tech@gmail.com>
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 block.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
diff mbox series

Patch

diff --git a/block.c b/block.c
index dfee7fb..7cd1944 100644
--- a/block.c
+++ b/block.c
@@ -1029,6 +1029,34 @@  static int blockd_notify(char *device, struct mount *m, struct probe_info *pr)
 	return err;
 }
 
+/**
+ * find_parent_device - find "parent" device for a given target path
+ *
+ * Some devices may have target path set to directory that is used as another
+ * device mount point. This function allows finding such devices so they can be
+ * mounted first.
+ */
+static struct probe_info *find_parent_device(struct probe_info *pr, const char *target)
+{
+	struct probe_info *e;
+
+	list_for_each_entry(e, &devices, list) {
+		struct mount *mp;
+
+		if (e == pr)
+			continue;
+
+		mp = find_block(e->uuid, e->label, basename(e->dev), NULL);
+		if (mp && mp->target) {
+			if (strlen(mp->target) < strlen(target) &&
+			    !strncmp(mp->target, target, strlen(mp->target)))
+				return e;
+		}
+	}
+
+	return NULL;
+}
+
 static int mount_device(struct probe_info *pr, int type)
 {
 	struct mount *m;
@@ -1069,6 +1097,14 @@  static int mount_device(struct probe_info *pr, int type)
 		return err;
 	}
 
+	/* Check if there is device for parent dir */
+	if (m && m->target) {
+		struct probe_info *parent = find_parent_device(pr, m->target);
+
+		if (parent)
+			ULOG_WARN("Device %s should be mounted first!\n", parent->dev);
+	}
+
 	if (type == TYPE_HOTPLUG)
 		blockd_notify(device, m, pr);