[PATCH 2/3] fuse2fs: track our own writable state

Darrick J. Wong
2025-11-06T22:41:44Z

Track our own willingness to write to the filesystem in a separate
variable from that of libext2fs. There's a small window between opening
the filesystem and mounting it where the library is rw but we might fail
a mount task and therefore don't want to write anything more.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
misc/fuse2fs.c | 30 +++++++++++++++++++-----------
1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index 1aa391b5a56456..8d5b705280b72f 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -219,6 +219,11 @@ struct fuse2fs_file_handle {
int check_flags;
};

+enum fuse2fs_opstate {
+ F2OP_READONLY,
+ F2OP_WRITABLE,
+};
+
/* Main program context */
#define FUSE2FS_MAGIC (0xEF53DEADUL)
struct fuse2fs {
@@ -242,6 +247,7 @@ struct fuse2fs {
int acl;
int dirsync;

+ enum fuse2fs_opstate opstate;
int logfd;
int blocklog;
int oom_score_adj;
@@ -553,7 +559,7 @@ static bool fuse2fs_mmp_wanted(const struct fuse2fs *ff)
ext2_filsys fs = ff->fs;

if (!fs || !ext2fs_has_feature_mmp(fs->super) ||
- !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP))
+ ff->opstate != F2OP_WRITABLE || (fs->flags & EXT2_FLAG_SKIP_MMP))
return false;
return true;
}
@@ -767,8 +773,6 @@ static int update_atime(ext2_filsys fs, ext2_ino_t ino)
struct timespec atime, mtime, now;
double datime, dmtime, dnow;

- if (!(fs->flags & EXT2_FLAG_RW))
- return 0;
err = fuse2fs_read_inode(fs, ino, &inode);
if (err)
return translate_error(fs, ino, err);
@@ -884,9 +888,8 @@ static int fs_can_allocate(struct fuse2fs *ff, blk64_t num)

static int fuse2fs_is_writeable(struct fuse2fs *ff)
{
- ext2_filsys fs = ff->fs;
-
- return (fs->flags & EXT2_FLAG_RW) && (fs->super->s_error_count == 0);
+ return ff->opstate == F2OP_WRITABLE &&
+ (ff->fs->super->s_error_count == 0);
}

static inline int is_superuser(struct fuse2fs *ff, struct fuse_context *ctxt)
@@ -1135,6 +1138,7 @@ static errcode_t fuse2fs_open(struct fuse2fs *ff)
}

snprintf(options, sizeof(options) - 1, "offset=%lu", ff->offset);
+ ff->opstate = F2OP_READONLY;

if (ff->directio)
flags |= EXT2_FLAG_DIRECT_IO;
@@ -1337,6 +1341,7 @@ static int fuse2fs_mount(struct fuse2fs *ff)
_("Warning: fuse2fs does not support using the journal.\n"
"There may be file system corruption or data loss if\n"
"the file system is not gracefully unmounted.\n"));
+ ff->opstate = F2OP_WRITABLE;
}

if (!(fs->super->s_state & EXT2_VALID_FS))
@@ -1359,7 +1364,7 @@ static int fuse2fs_mount(struct fuse2fs *ff)
ff->errors_behavior = fs->super->s_errors;

/* Clear the valid flag so that an unclean shutdown forces a fsck */
- if (fs->flags & EXT2_FLAG_RW) {
+ if (ff->opstate == F2OP_WRITABLE) {
fs->super->s_mnt_count++;
ext2fs_set_tstamp(fs->super, s_mtime, time(NULL));
fs->super->s_state &= ~EXT2_VALID_FS;
@@ -1383,7 +1388,7 @@ static void op_destroy(void *p EXT2FS_ATTR((unused)))
fs = fuse2fs_start(ff);

dbg_printf(ff, "%s: dev=%s\n", __func__, fs->device_name);
- if (fs->flags & EXT2_FLAG_RW) {
+ if (ff->opstate == F2OP_WRITABLE) {
fs->super->s_state |= EXT2_VALID_FS;
if (fs->super->s_error_count)
fs->super->s_state |= EXT2_ERROR_FS;
@@ -1574,7 +1579,7 @@ static void *op_init(struct fuse_conn_info *conn
cfg->nullpath_ok = 1;
#endif

- if (ff->fs->flags & EXT2_FLAG_RW)
+ if (ff->opstate == F2OP_WRITABLE)
fuse2fs_read_bitmaps(ff);

/*
@@ -3724,7 +3729,7 @@ static int op_statfs(const char *path EXT2FS_ATTR((unused)),
fsid ^= *f;
buf->f_fsid = fsid;
buf->f_flag = 0;
- if (!(fs->flags & EXT2_FLAG_RW))
+ if (ff->opstate != F2OP_WRITABLE)
buf->f_flag |= ST_RDONLY;
buf->f_namemax = EXT2_NAME_LEN;
fuse2fs_finish(ff, 0);
@@ -5568,6 +5573,7 @@ int main(int argc, char *argv[])
.logfd = -1,
.bfl = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER,
.oom_score_adj = -500,
+ .opstate = F2OP_WRITABLE,
};
errcode_t err;
FILE *orig_stderr = stderr;
@@ -5861,9 +5867,11 @@ static int __translate_error(ext2_filsys fs, ext2_ino_t ino, errcode_t err,
_("Continuing after errors; is this a good idea?"));
break;
case EXT2_ERRORS_RO:
- if (fs->flags & EXT2_FLAG_RW)
+ if (ff->opstate == F2OP_WRITABLE) {
err_printf(ff, "%s\n",
_("Remounting read-only due to errors."));
+ ff->opstate = F2OP_READONLY;
+ }
fuse2fs_mmp_cancel(ff);
fs->flags &= ~EXT2_FLAG_RW;
break;