[PATCH] fs: combine __d_alloc's allocations into one
Ahmad Fatoum
a.fatoum at pengutronix.de
Wed Mar 12 23:45:40 PDT 2025
Instead of doing two allocations, one for the struct dentry and one for
the full name, just do one allocation and place them after each other.
This has the extra benefit of fixing a leak of the dentry allocation
when the second allocation for the file name fails.
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
fs/fs.c | 13 ++++---------
include/linux/dcache.h | 2 +-
2 files changed, 5 insertions(+), 10 deletions(-)
diff --git a/fs/fs.c b/fs/fs.c
index 96ca60341ea4..f8b7340d809d 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -795,7 +795,6 @@ static void dentry_kill(struct dentry *dentry)
dput(dentry->d_parent);
list_del(&dentry->d_child);
- free(dentry->name);
free(dentry);
}
@@ -1427,15 +1426,11 @@ static struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
{
struct dentry *dentry;
- dentry = xzalloc(sizeof(*dentry));
- if (!dentry)
- return NULL;
-
if (!name)
name = &slash_name;
- dentry->name = malloc(name->len + 1);
- if (!dentry->name)
+ dentry = xzalloc(struct_size(dentry, name, name->len + 1));
+ if (!dentry)
return NULL;
memcpy(dentry->name, name->name, name->len);
@@ -3006,9 +3001,9 @@ static char *__dpath(struct dentry *dentry, struct dentry *root)
ppath = __dpath(dentry->d_parent, root);
if (ppath)
- res = basprintf("%s/%s", ppath, dentry->name);
+ res = basprintf("%s/%s", ppath, dentry->d_name.name);
else
- res = basprintf("/%s", dentry->name);
+ res = basprintf("/%s", dentry->d_name.name);
free(ppath);
return res;
diff --git a/include/linux/dcache.h b/include/linux/dcache.h
index ed7e5c2cbcff..1b51a5656703 100644
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -80,7 +80,7 @@ struct dentry {
struct dcookie_struct *d_cookie; /* cookie, if any */
#endif
int d_mounted;
- unsigned char *name; /* all names */
+ unsigned char name[]; /* all names */
};
struct dentry_operations {
--
2.39.5
More information about the barebox
mailing list