[RFC 1/5] hostfs: sort directory entries

Johannes Berg johannes at sipsolutions.net
Thu Nov 19 05:24:38 PST 2026


From: Benjamin Berg <benjamin.berg at intel.com>

Otherwise the order of directory entries is the one returned by the host
file system. This order is consistent, but not well-defined, causing
possible issues when reproducing a run in a different or re-created
setup.

Signed-off-by: Benjamin Berg <benjamin.berg at intel.com>
Signed-off-by: Johannes Berg <johannes.berg at intel.com>
---
 fs/hostfs/hostfs_kern.c | 147 ++++++++++++++++++++++++++++++++++------
 1 file changed, 127 insertions(+), 20 deletions(-)

diff --git a/fs/hostfs/hostfs_kern.c b/fs/hostfs/hostfs_kern.c
index 613146e76dec..e8419b96b869 100644
--- a/fs/hostfs/hostfs_kern.c
+++ b/fs/hostfs/hostfs_kern.c
@@ -19,6 +19,7 @@
 #include <linux/fs_context.h>
 #include <linux/fs_parser.h>
 #include <linux/namei.h>
+#include <linux/list_sort.h>
 #include "hostfs.h"
 #include <init.h>
 #include <kern.h>
@@ -27,6 +28,15 @@ struct hostfs_fs_info {
 	char *host_root_path;
 };
 
+struct hostfs_dentry_list {
+	struct list_head list;
+	unsigned long long ino;
+	unsigned int type;
+
+	int name_len;
+	char name[];
+};
+
 struct hostfs_inode_info {
 	int fd;
 	fmode_t mode;
@@ -38,7 +48,7 @@ struct hostfs_inode_info {
 
 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
 {
-	return list_entry(inode, struct hostfs_inode_info, vfs_inode);
+	return container_of(inode, struct hostfs_inode_info, vfs_inode);
 }
 
 #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
@@ -235,6 +245,25 @@ static void hostfs_evict_inode(struct inode *inode)
 	}
 }
 
+static void hostfs_file_free_dentries(struct file *file)
+{
+	struct hostfs_dentry_list *entry;
+	struct list_head *list = file->private_data;
+
+	if (!list)
+		return;
+	file->private_data = NULL;
+
+	while ((entry = list_first_entry_or_null(list,
+						 struct hostfs_dentry_list,
+						 list))) {
+		list_del(&entry->list);
+		kfree(entry);
+	}
+
+	kfree(list);
+}
+
 static void hostfs_free_inode(struct inode *inode)
 {
 	kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode));
@@ -267,30 +296,100 @@ static const struct super_operations hostfs_sbops = {
 	.show_options	= hostfs_show_options,
 };
 
+static int hostfs_dentry_sort(void *priv,
+			      const struct list_head *a,
+			      const struct list_head *b)
+{
+	struct hostfs_dentry_list *hdl_a =
+		list_entry(a, struct hostfs_dentry_list, list);
+	struct hostfs_dentry_list *hdl_b =
+		list_entry(b, struct hostfs_dentry_list, list);
+
+	return strcmp(hdl_a->name, hdl_b->name);
+}
+
+static loff_t hostfs_seekdir(struct file *file, loff_t offset, int whence)
+{
+	struct inode *inode = file->f_mapping->host;
+
+	/* Seeking to the start discards the cache */
+	if (offset == 0 && whence == SEEK_SET)
+		hostfs_file_free_dentries(file);
+
+	return generic_file_llseek_size(file, offset, whence,
+					inode->i_sb->s_maxbytes,
+					i_size_read(inode));
+}
+
 static int hostfs_readdir(struct file *file, struct dir_context *ctx)
 {
-	void *dir;
-	char *name;
-	unsigned long long next, ino;
-	int error, len;
+	struct hostfs_dentry_list *hdl;
+	struct list_head *dentry_list = file->private_data;
 	unsigned int type;
+	loff_t pos;
+	int error;
 
-	name = dentry_name(file->f_path.dentry);
-	if (name == NULL)
-		return -ENOMEM;
-	dir = open_dir(name, &error);
-	__putname(name);
-	if (dir == NULL)
-		return -error;
-	next = ctx->pos;
-	seek_dir(dir, next);
-	while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
-		if (!dir_emit(ctx, name, len, ino, type))
-			break;
-		ctx->pos = next;
+	if (!dentry_list) {
+		unsigned long long next, ino;
+		int len;
+		void *dir;
+		char *name;
+
+		dentry_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
+		if (!dentry_list)
+			return -ENOMEM;
+		INIT_LIST_HEAD(dentry_list);
+		file->private_data = dentry_list;
+
+		name = dentry_name(file->f_path.dentry);
+		if (!name) {
+			error = ENOMEM;
+			goto discard_cache;
+		}
+		dir = open_dir(name, &error);
+		__putname(name);
+		if (!dir)
+			goto discard_cache;
+
+		while ((name = read_dir(dir, &next, &ino, &len, &type))) {
+			hdl = kzalloc(struct_size(hdl, name, len + 1),
+				      GFP_KERNEL);
+			if (!hdl) {
+				error = ENOMEM;
+				goto discard_cache;
+			}
+
+			hdl->name_len = len;
+			hdl->ino = ino;
+			hdl->type = type;
+			memcpy(hdl->name, name, len + 1);
+			INIT_LIST_HEAD(&hdl->list);
+			list_add_tail(&hdl->list, dentry_list);
+		}
+
+		close_dir(dir);
+
+		list_sort(NULL, dentry_list, hostfs_dentry_sort);
 	}
-	close_dir(dir);
+
+	pos = -1;
+	list_for_each_entry(hdl, dentry_list, list) {
+		pos++;
+		if (pos < ctx->pos)
+			continue;
+
+		if (!dir_emit(ctx, hdl->name, hdl->name_len, hdl->ino,
+			      hdl->type))
+			break;
+
+		ctx->pos = pos + 1;
+	}
+
 	return 0;
+
+discard_cache:
+	hostfs_file_free_dentries(file);
+	return -error;
 }
 
 static int hostfs_open(struct inode *ino, struct file *file)
@@ -359,6 +458,13 @@ static int hostfs_file_release(struct inode *inode, struct file *file)
 	return 0;
 }
 
+static int hostfs_dir_release(struct inode *inode, struct file *file)
+{
+	hostfs_file_free_dentries(file);
+
+	return 0;
+}
+
 static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
 			int datasync)
 {
@@ -389,10 +495,11 @@ static const struct file_operations hostfs_file_fops = {
 };
 
 static const struct file_operations hostfs_dir_fops = {
-	.llseek		= generic_file_llseek,
+	.llseek		= hostfs_seekdir,
 	.iterate_shared	= hostfs_readdir,
 	.read		= generic_read_dir,
 	.open		= hostfs_open,
+	.release	= hostfs_dir_release,
 	.fsync		= hostfs_fsync,
 };
 
-- 
2.56.1




More information about the linux-um mailing list