[PATCH 16/19] fuse4fs: don't use inode number translation when possible

Darrick J. Wong
2025-08-21T01:19:53Z

Prior to the integration of iomap into fuse, the fuse client (aka the
kernel) required that the root directory have an inumber of
FUSE_ROOT_ID, which is 1. However, the ext2 filesystem defines the root
inode number to be EXT2_ROOT_INO, which is 2. This dissonance means
that we have to have translator functions, and that any access to
inumber 1 (the ext2 badblocks file) will instead redirect to the root
directory.

That's horrible. Use the new mount option to set the root directory
nodeid to EXT2_ROOT_INO so that we don't need this translation.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
misc/fuse4fs.c | 29 +++++++++++++++++++++++------
1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/misc/fuse4fs.c b/misc/fuse4fs.c
index 304bac191e7c4c..5127712e19e6f9 100644
--- a/misc/fuse4fs.c
+++ b/misc/fuse4fs.c
@@ -263,6 +263,7 @@ struct fuse4fs {
uint8_t unmount_in_destroy;
uint8_t noblkdev;
uint8_t iomap_passthrough_options;
+ uint8_t translate_inums;

enum fuse4fs_opstate opstate;
int logfd;
@@ -324,17 +325,19 @@ struct fuse4fs {
#define FUSE4FS_CHECK_CONTEXT_ABORT(ff) \
__FUSE4FS_CHECK_CONTEXT((ff), abort(), abort())

-static inline void fuse4fs_ino_from_fuse(ext2_ino_t *inop, fuse_ino_t fino)
+static inline void fuse4fs_ino_from_fuse(const struct fuse4fs *ff,
+ ext2_ino_t *inop, fuse_ino_t fino)
{
- if (fino == FUSE_ROOT_ID)
+ if (ff->translate_inums && fino == FUSE_ROOT_ID)
*inop = EXT2_ROOT_INO;
else
*inop = fino;
}

-static inline void fuse4fs_ino_to_fuse(fuse_ino_t *finop, ext2_ino_t ino)
+static inline void fuse4fs_ino_to_fuse(const struct fuse4fs *ff,
+ fuse_ino_t *finop, ext2_ino_t ino)
{
- if (ino == EXT2_ROOT_INO)
+ if (ff->translate_inums && ino == EXT2_ROOT_INO)
*finop = FUSE_ROOT_ID;
else
*finop = ino;
@@ -350,7 +353,7 @@ static inline void fuse4fs_ino_to_fuse(fuse_ino_t *finop, ext2_ino_t ino)
fuse_reply_err((req), EIO); \
return; \
} \
- fuse4fs_ino_from_fuse(ext2_inop, fuse_ino); \
+ fuse4fs_ino_from_fuse(fuse4fs_get(req), ext2_inop, fuse_ino); \
} while (0)

static int __translate_error(ext2_filsys fs, ext2_ino_t ino, errcode_t err,
@@ -1723,7 +1726,7 @@ static int fuse4fs_stat_inode(struct fuse4fs *ff, ext2_ino_t ino,
statbuf->st_rdev = inodep->i_block[1];
}

- fuse4fs_ino_to_fuse(&entry->ino, ino);
+ fuse4fs_ino_to_fuse(ff, &entry->ino, ino);
entry->generation = inodep->i_generation;
entry->attr_timeout = FUSE4FS_ATTR_TIMEOUT;
entry->entry_timeout = FUSE4FS_ATTR_TIMEOUT;
@@ -7101,6 +7104,7 @@ int main(int argc, char *argv[])
.iomap_state = IOMAP_UNKNOWN,
.iomap_dev = FUSE_IOMAP_DEV_NULL,
#endif
+ .translate_inums = 1,
};
errcode_t err;
FILE *orig_stderr = stderr;
@@ -7206,6 +7210,19 @@ int main(int argc, char *argv[])
goto out;
}

+ if (iomap_detected) {
+ /*
+ * The root_nodeid mount option was added when iomap support
+ * was added to fuse. This enables us to control the root
+ * nodeid in the kernel, which enables a 1:1 translation of
+ * ext2 to kernel inumbers.
+ */
+ snprintf(extra_args, BUFSIZ, "-oroot_nodeid=%d",
+ EXT2_ROOT_INO);
+ fuse_opt_add_arg(&args, extra_args);
+ fctx.translate_inums = 0;
+ }
+
if (!fctx.cache_size)
fctx.cache_size = default_cache_size();
if (fctx.cache_size) {