[PATCH v2 041/113] fs: support opening /

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Mar 4 10:59:26 PST 2024


With open() on / no longer accessing uninitialized memory, it now always
fails with errno == ENOENT. This is because d_lookup only compares
children of /, but never / itself.

Fix this by comparing the parent and while at it, rework the existing
code to be more concise.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 fs/fs.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/fs/fs.c b/fs/fs.c
index acd89dd1c9a4..fa1d01d892dc 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -1502,7 +1502,6 @@ void d_add(struct dentry *dentry, struct inode *inode)
 }
 
 static bool d_same_name(const struct dentry *dentry,
-			const struct dentry *parent,
 			const struct qstr *name)
 {
 	if (dentry->d_name.len != name->len)
@@ -1511,17 +1510,16 @@ static bool d_same_name(const struct dentry *dentry,
 	return strncmp(dentry->d_name.name, name->name, name->len) == 0;
 }
 
-static struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
+static struct dentry *d_lookup(struct dentry *parent, const struct qstr *name)
 {
 	struct dentry *dentry;
 
+	if (d_same_name(parent, name))
+		return dget(parent);
+
 	list_for_each_entry(dentry, &parent->d_subdirs, d_child) {
-		if (!d_same_name(dentry, parent, name))
-			continue;
-
-		dget(dentry);
-
-		return dentry;
+		if (d_same_name(dentry, name))
+			return dget(dentry);
 	}
 
 	return NULL;
-- 
2.39.2




More information about the barebox mailing list