Skip to contentSkip to conversation

fs: make dpath always panic on OOM

PATCH Applied Ahmad Fatoum 2 participants 3 messages Oct 15, 2025 – Oct 20, 2025 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 fs: make dpath always panic on OOM

This is effectively already the case since basprintf was changed to be equivalent to xasprintf. Replace basprintf with xasprintf to make this clearer and use xstrdup instead of strdup for the root node.

All users, except one, didn't bother to check dpath return value for NULL anyway.

---
 fs/fs.c     | 6 +++---
 fs/legacy.c | 2 --
 2 files changed, 3 insertions(+), 5 deletions(-)
fs/fs.c +3 -3
index 529c328f26fc..528299e039d2 100644
@@ -3022,9 +3022,9 @@ static char *__dpath(struct dentry *dentry, struct dentry *root)
 
 	ppath = __dpath(dentry->d_parent, root);
 	if (ppath)
-		res = basprintf("%s/%s", ppath, dentry->d_name.name);
+		res = xasprintf("%s/%s", ppath, dentry->d_name.name);
 	else
-		res = basprintf("/%s", dentry->d_name.name);
+		res = xasprintf("/%s", dentry->d_name.name);
 	free(ppath);
 
 	return res;
@@ -3058,7 +3058,7 @@ char *dpath(struct dentry *dentry, struct dentry *root)
 	char *res;
 
 	if (dentry == root)
-		return strdup("/");
+		return xstrdup("/");
 
 	res = __dpath(dentry, root);
 
fs/legacy.c +0 -2
index 7c73ad7c41e4..c2170f52a40b 100644
@@ -60,8 +60,6 @@ static struct dentry *legacy_lookup(struct inode *dir, struct dentry *dentry,
 	int ret;
 
 	pathname = dpath(dentry, fsdev->vfsmount.mnt_root);
-	if (!pathname)
-		return NULL;
 
 	ret = legacy_ops->stat(&fsdev->dev, pathname, &s);
 	if (!ret) {
Signature · 1 line
2.47.3
#1 lore raw

PATCH 2/2 fs: add filepath helper

This helper can be useful for debugging code, so export it in a header and make first use of it.

---
 fs/legacy.c  | 8 ++++----
 include/fs.h | 5 +++++
 2 files changed, 9 insertions(+), 4 deletions(-)
fs/legacy.c +4 -4
index c2170f52a40b..dce14706ca20 100644
@@ -16,8 +16,7 @@ static struct inode *legacy_get_inode(struct super_block *sb, const struct inode
 
 static int legacy_iterate(struct file *file, struct dir_context *ctx)
 {
-	struct dentry *dentry = file->f_path.dentry;
-	struct inode *dir = d_inode(dentry);
+	struct inode *dir = d_inode(file->f_path.dentry);
 	struct super_block *sb = dir->i_sb;
 	struct fs_device *fsdev = container_of(sb, struct fs_device, sb);
 	const struct fs_legacy_ops *legacy_ops = fsdev->driver->legacy_ops;
@@ -27,7 +26,7 @@ static int legacy_iterate(struct file *file, struct dir_context *ctx)
 
 	dir_emit_dots(file, ctx);
 
-	pathname = dpath(dentry, fsdev->vfsmount.mnt_root);
+	pathname = filepath(file);
 
 	d = legacy_ops->opendir(&fsdev->dev, pathname);
 	if (!d)
@@ -87,7 +86,8 @@ static int legacy_open(struct inode *inode, struct file *file)
 	if (!legacy_ops->open)
 		return 0;
 
-	pathname = dpath(file->f_path.dentry, fsdev->vfsmount.mnt_root);
+	pathname = filepath(file);
+
 	error = legacy_ops->open(&file->fsdev->dev, file, pathname);
 	free(pathname);
 
include/fs.h +5 -0
index 86442eacddf9..98c482bbf2a9 100644
@@ -172,4 +172,9 @@ static inline const char *devpath_to_name(const char *devpath)
 
 const char *fs_detect(const char *filename, const char *fsoptions);
 
+static inline char *filepath(struct file *f)
+{
+	return dpath(f->f_path.dentry, f->fsdev->vfsmount.mnt_root);
+}
+
 #endif /* __FS_H */
Signature · 1 line
2.47.3
Applied
#2 lore raw
In reply to Ahmad Fatoum
Quoted 8 lines · Ahmad Fatoum

This is effectively already the case since basprintf was changed to be equivalent to xasprintf. Replace basprintf with xasprintf to make this clearer and use xstrdup instead of strdup for the root node.

All users, except one, didn't bother to check dpath return value for NULL anyway.

[...]

Applied, thanks!

[1/2] fs: make dpath always panic on OOM
https://git.pengutronix.de/cgit/barebox/commit/?id=d8b66050032b (link may not be stable)
[2/2] fs: add filepath helper
https://git.pengutronix.de/cgit/barebox/commit/?id=60dd427b634d (link may not be stable)

Best regards,

Signature · 1 line
Sascha Hauer <[email protected]>