[PATCH v2 029/113] fs: opendir: reference mount point until closedir is called

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


We currently don't keep the path used for opendir alive beyond
the function, because all files are read and added into a linked
list at once. If an unmount happens before closedir though, the file
names will become invalidated, which shouldn't happen. Keep the path
alive for longer to block unmount until directory iteration is over.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 fs/fs.c          | 4 ++--
 include/dirent.h | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/fs/fs.c b/fs/fs.c
index 7d9bee539db5..a31343e2e272 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -2757,6 +2757,7 @@ DIR *opendir(const char *pathname)
 	file.f_op = dir->d_inode->i_fop;
 
 	d = xzalloc(sizeof(*d));
+	d->path = path;
 
 	INIT_LIST_HEAD(&d->entries);
 	rd.dir = d;
@@ -2765,8 +2766,6 @@ DIR *opendir(const char *pathname)
 	if (ret)
 		goto out_release;
 
-	path_put(&path);
-
 	return d;
 
 out_release:
@@ -2785,6 +2784,7 @@ int closedir(DIR *dir)
 	if (!dir)
 		return errno_set(-EBADF);
 
+	path_put(&dir->path);
 	release_dir(dir);
 
 	return 0;
diff --git a/include/dirent.h b/include/dirent.h
index 75627730f84b..6e77058d2903 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -3,6 +3,7 @@
 #define __DIRENT_H
 
 #include <linux/list.h>
+#include <linux/path.h>
 
 struct dirent {
 	char d_name[256];
@@ -13,6 +14,7 @@ typedef struct dir {
 	struct fs_driver *fsdrv;
 	struct dirent d;
 	void *priv; /* private data for the fs driver */
+	struct path path;
 	struct list_head entries;
 } DIR;
 
-- 
2.39.2




More information about the barebox mailing list