[PATCH 03/13] fs: keep the file size when O_TRUNC fails

Sascha Hauer s.hauer at pengutronix.de
Wed Nov 18 06:51:53 PST 2026


do_dentry_open() sets the file size to zero for O_TRUNC before checking
whether truncating the file worked. f_size is the size of the inode, so
when it fails, e.g. on a read-only file system, the file stays intact
on the medium, but appears empty until it's evicted from the cache:

  open("file", O_WRONLY | O_TRUNC)  -> -EROFS
  stat("file")                      -> st_size 0
  read()                            -> 0

Only set the size once truncating succeeded.

Fixes: 7d91d56436 ("fs: replace FILE.size by f_inode.i_size")

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 fs/fs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/fs.c b/fs/fs.c
index e05d3b7a4a..88969fc287 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -2736,9 +2736,9 @@ static int do_dentry_open(struct file *f)
 
 	if (f->f_flags & O_TRUNC) {
 		error = fsdev_truncate(f, 0);
-		f->f_size = 0;
 		if (error)
 			goto err_release;
+		f->f_size = 0;
 	}
 
 	if (f->f_flags & O_APPEND)

-- 
2.47.3




More information about the barebox mailing list