From 2b66e2f9d33d4e31d721f29278994d527e84b8f8 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 12 Jul 2025 10:37:15 +0200 Subject: [PATCH] fs: Reuse a directory device when accessing the same dir At present a new directory device is created for every access to a directory. This means that a new device is always created when 'ls' is used. Where the directory being accessed matches an existing device, this is not necessary. Check that path against existing directories and reuse the device if possible. Signed-off-by: Simon Glass --- fs/fs-uclass.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/fs/fs-uclass.c b/fs/fs-uclass.c index 96fb04a106c..a335391bd74 100644 --- a/fs/fs-uclass.c +++ b/fs/fs-uclass.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -41,8 +42,35 @@ int fs_split_path(const char *fname, char **subdirp, const char **leafp) int fs_lookup_dir(struct udevice *dev, const char *path, struct udevice **dirp) { struct fs_ops *ops = fs_get_ops(dev); + struct udevice *dir; + int ret; - return ops->lookup_dir(dev, path, dirp); + if (!path || !strcmp("/", path)) + path = ""; + + /* see if we already have this directory */ + device_foreach_child(dir, dev) { + struct dir_uc_priv *priv; + + if (!device_active(dir)) + continue; + + priv = dev_get_uclass_priv(dir); + log_debug("dir %s '%s' '%s'\n", dir->name, path, priv->path); + if (!strcmp(path, priv->path)) { + *dirp = dir; + log_debug("found: dev '%s'\n", dir->name); + return 0; + } + } + + ret = ops->lookup_dir(dev, path, &dir); + if (ret) + return log_msg_ret("fld", ret); + + *dirp = dir; + + return 0; } int fs_mount(struct udevice *dev)