Linux EXT4 FS development
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <djwong@kernel.org>
To: tytso@mit.edu
Cc: linux-ext4@vger.kernel.org
Subject: [PATCH 20/29] fuse2fs: implement O_APPEND correctly
Date: Wed, 21 May 2025 15:40:08 -0700	[thread overview]
Message-ID: <174786677905.1383760.14021746902005874478.stgit@frogsfrogsfrogs> (raw)
In-Reply-To: <174786677421.1383760.15289906755026332870.stgit@frogsfrogsfrogs>

From: Darrick J. Wong <djwong@kernel.org>

Try to implement append-only files correctly.

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


diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index 8567d2a8801bb6..52c24715fbc109 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -41,6 +41,7 @@
 #include <inttypes.h>
 #include "ext2fs/ext2fs.h"
 #include "ext2fs/ext2_fs.h"
+#include "ext2fs/ext2fsP.h"
 #if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
 # define FUSE_PLATFORM_OPTS	""
 #else
@@ -507,20 +508,26 @@ static inline int want_check_owner(struct fuse2fs *ff,
 	return !is_superuser(ff, ctxt);
 }
 
+/* Test for append permission */
+#define A_OK	16
+
 static int check_iflags_access(struct fuse2fs *ff, ext2_ino_t ino,
 			       const struct ext2_inode *inode, int mask)
 {
 	ext2_filsys fs = ff->fs;
 
-	/* no writing to read-only or broken fs */
-	if ((mask & W_OK) && !fs_writeable(fs))
+	EXT2FS_BUILD_BUG_ON((A_OK & (R_OK | W_OK | X_OK | F_OK)) != 0);
+
+	/* no writing or metadata changes to read-only or broken fs */
+	if ((mask & (W_OK | A_OK)) && !fs_writeable(fs))
 		return -EROFS;
 
-	dbg_printf(ff, "access ino=%d mask=e%s%s%s iflags=0x%x\n",
+	dbg_printf(ff, "access ino=%d mask=e%s%s%s%s iflags=0x%x\n",
 		   ino,
 		   (mask & R_OK ? "r" : ""),
 		   (mask & W_OK ? "w" : ""),
 		   (mask & X_OK ? "x" : ""),
+		   (mask & A_OK ? "a" : ""),
 		   inode->i_flags);
 
 	/* is immutable? */
@@ -528,6 +535,10 @@ static int check_iflags_access(struct fuse2fs *ff, ext2_ino_t ino,
 	    (inode->i_flags & EXT2_IMMUTABLE_FL))
 		return -EPERM;
 
+	/* is append-only? */
+	if ((inode->i_flags & EXT2_APPEND_FL) && (mask & W_OK) && !(mask & A_OK))
+		return -EPERM;
+
 	return 0;
 }
 
@@ -541,7 +552,7 @@ static int check_inum_access(struct fuse2fs *ff, ext2_ino_t ino, int mask)
 	int ret;
 
 	/* no writing to read-only or broken fs */
-	if ((mask & W_OK) && !fs_writeable(fs))
+	if ((mask & (W_OK | A_OK)) && !fs_writeable(fs))
 		return -EROFS;
 
 	err = ext2fs_read_inode(fs, ino, &inode);
@@ -549,11 +560,12 @@ static int check_inum_access(struct fuse2fs *ff, ext2_ino_t ino, int mask)
 		return translate_error(fs, ino, err);
 	perms = inode.i_mode & 0777;
 
-	dbg_printf(ff, "access ino=%d mask=e%s%s%s perms=0%o iflags=0x%x "
+	dbg_printf(ff, "access ino=%d mask=e%s%s%s%s perms=0%o iflags=0x%x "
 		   "fuid=%d fgid=%d uid=%d gid=%d\n", ino,
 		   (mask & R_OK ? "r" : ""),
 		   (mask & W_OK ? "w" : ""),
 		   (mask & X_OK ? "x" : ""),
+		   (mask & A_OK ? "a" : ""),
 		   perms, inode.i_flags,
 		   inode_uid(inode), inode_gid(inode),
 		   ctxt->uid, ctxt->gid);
@@ -898,7 +910,7 @@ static int op_mknod(const char *path, mode_t mode, dev_t dev)
 		goto out2;
 	}
 
-	ret = check_inum_access(ff, parent, W_OK);
+	ret = check_inum_access(ff, parent, A_OK | W_OK);
 	if (ret)
 		goto out2;
 
@@ -1029,7 +1041,7 @@ static int op_mkdir(const char *path, mode_t mode)
 		goto out2;
 	}
 
-	ret = check_inum_access(ff, parent, W_OK);
+	ret = check_inum_access(ff, parent, A_OK | W_OK);
 	if (ret)
 		goto out2;
 
@@ -1432,7 +1444,7 @@ static int op_symlink(const char *src, const char *dest)
 		goto out2;
 	}
 
-	ret = check_inum_access(ff, parent, W_OK);
+	ret = check_inum_access(ff, parent, A_OK | W_OK);
 	if (ret)
 		goto out2;
 
@@ -1807,7 +1819,7 @@ static int op_link(const char *src, const char *dest)
 		goto out2;
 	}
 
-	ret = check_inum_access(ff, parent, W_OK);
+	ret = check_inum_access(ff, parent, A_OK | W_OK);
 	if (ret)
 		goto out2;
 
@@ -2128,6 +2140,15 @@ static int __op_open(struct fuse2fs *ff, const char *path,
 		file->open_flags |= EXT2_FILE_WRITE;
 		break;
 	}
+	if (fp->flags & O_APPEND) {
+		/* the kernel doesn't allow truncation of an append-only file */
+		if (fp->flags & O_TRUNC) {
+			ret = -EPERM;
+			goto out;
+		}
+
+		check |= A_OK;
+	}
 
 	detect_linux_executable_open(fp->flags, &check, &file->open_flags);
 
@@ -2938,7 +2959,7 @@ static int op_create(const char *path, mode_t mode, struct fuse_file_info *fp)
 		goto out2;
 	}
 
-	ret = check_inum_access(ff, parent, W_OK);
+	ret = check_inum_access(ff, parent, A_OK | W_OK);
 	if (ret)
 		goto out2;
 
@@ -3110,6 +3131,7 @@ static int op_utimens(const char *path, const struct timespec ctv[2]
 	errcode_t err;
 	ext2_ino_t ino;
 	struct ext2_inode_large inode;
+	int access = W_OK;
 	int ret = 0;
 
 	FUSE2FS_CHECK_CONTEXT(ff);
@@ -3125,7 +3147,13 @@ static int op_utimens(const char *path, const struct timespec ctv[2]
 			(long long int)ctv[0].tv_sec, ctv[0].tv_nsec,
 			(long long int)ctv[1].tv_sec, ctv[1].tv_nsec);
 
-	ret = check_inum_access(ff, ino, W_OK);
+	/*
+	 * ext4 allows timestamp updates of append-only files but only if we're
+	 * setting to current time
+	 */
+	if (ctv[0].tv_nsec == UTIME_NOW && ctv[1].tv_nsec == UTIME_NOW)
+		access |= A_OK;
+	ret = check_inum_access(ff, ino, access);
 	if (ret)
 		goto out;
 


  parent reply	other threads:[~2025-05-21 22:40 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-21 22:34 [PATCHSET 1/6] fuse2fs: even more bug fixes Darrick J. Wong
2025-05-21 22:35 ` [PATCH 01/29] libext2fs: fix unix io manager invalidation Darrick J. Wong
2025-05-21 22:35 ` [PATCH 02/29] libext2fs: fix livelock in the unix io manager Darrick J. Wong
2025-05-21 22:35 ` [PATCH 03/29] fuse2fs: clean up error messages Darrick J. Wong
2025-05-21 22:35 ` [PATCH 04/29] fuse2fs: fix cache size parsing Darrick J. Wong
2025-05-21 22:36 ` [PATCH 05/29] fuse2fs: compact all the boolean flags in struct fuse2fs Darrick J. Wong
2025-05-21 22:36 ` [PATCH 06/29] fuse2fs: support XATTR_CREATE/REPLACE in setxattr Darrick J. Wong
2025-05-21 22:36 ` [PATCH 07/29] fuse2fs: fix error return handling in op_truncate Darrick J. Wong
2025-05-21 22:37 ` [PATCH 08/29] fuse2fs: flip parameter order in __translate_error Darrick J. Wong
2025-05-21 22:37 ` [PATCH 09/29] fuse2fs: fix CLI argument parsing leaks Darrick J. Wong
2025-05-21 22:37 ` [PATCH 10/29] fuse2fs: allow some control over acls Darrick J. Wong
2025-05-21 22:37 ` [PATCH 11/29] fuse2fs: enable processing of acls in the kernel Darrick J. Wong
2025-05-21 22:38 ` [PATCH 12/29] fuse2fs: make removexattr work correctly Darrick J. Wong
2025-05-21 22:38 ` [PATCH 13/29] fuse2fs: implement O_TRUNC correctly Darrick J. Wong
2025-05-21 22:38 ` [PATCH 14/29] fuse2fs: rearrange check_inum_access parameters a bit Darrick J. Wong
2025-05-21 22:38 ` [PATCH 15/29] fuse2fs: make filesystem corruption a hard error Darrick J. Wong
2025-05-21 22:39 ` [PATCH 16/29] fuse2fs: make internal state " Darrick J. Wong
2025-05-21 22:39 ` [PATCH 17/29] fuse2fs: make bad magic numbers report a corruption error too Darrick J. Wong
2025-05-21 22:39 ` [PATCH 18/29] fuse2fs: return EPERM for write access to EXT2_IMMUTABLE_FL files Darrick J. Wong
2025-05-21 22:39 ` [PATCH 19/29] fuse2fs: check the immutable flag in more places Darrick J. Wong
2025-05-21 22:40 ` Darrick J. Wong [this message]
2025-05-21 22:40 ` [PATCH 21/29] fuse2fs: decode fuse_main error codes Darrick J. Wong
2025-05-21 22:40 ` [PATCH 22/29] fuse2fs: fix fallocate zero range Darrick J. Wong
2025-05-21 22:40 ` [PATCH 23/29] fuse2fs: check for supported xattr name prefixes Darrick J. Wong
2025-05-21 22:41 ` [PATCH 24/29] fuse2fs: fix return value handling Darrick J. Wong
2025-05-21 22:41 ` [PATCH 25/29] fuse2fs: fix removing ea inodes when freeing a file Darrick J. Wong
2025-05-21 22:41 ` [PATCH 26/29] fuse2fs: fix post-EOF preallocation clearing on truncation Darrick J. Wong
2025-05-21 22:41 ` [PATCH 27/29] fuse2fs: also ignore the nodelalloc mount option Darrick J. Wong
2025-05-21 22:42 ` [PATCH 28/29] fuse2fs: propagate default ACLs to new children Darrick J. Wong
2025-05-21 22:42 ` [PATCH 29/29] fuse2fs: fix group membership checking in op_chmod Darrick J. Wong
2025-05-23 14:03 ` [PATCHSET 1/6] fuse2fs: even more bug fixes Theodore Ts'o
2025-05-29  1:37   ` Darrick J. Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=174786677905.1383760.14021746902005874478.stgit@frogsfrogsfrogs \
    --to=djwong@kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox