FAILED: patch "[PATCH] f2fs: disallow mmap write and data-modifying fallocate on atomic files" failed to apply to 6.6-stable tree
Subsystems (2)
Sasha Levin
maintainerThe patch below does not apply to the 6.6-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y
git checkout FETCH_HEAD
git cherry-pick -x 06dfe4a7eb696d868b196c2a49873142f19bd862
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 6.6.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From 06dfe4a7eb696d868b196c2a49873142f19bd862 Mon Sep 17 00:00:00 2001
From: Daeho Jeong <[email protected]>
Date: Sat, 10 Oct 2026 02:41:33 +0000
Subject: [PATCH] f2fs: disallow mmap write and data-modifying fallocate on
atomic files
Stores through a shared writable mapping and fallocate with FALLOC_FL_PUNCH_HOLE, FALLOC_FL_ZERO_RANGE, FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE do not go through ->write_begin, so they bypass the COW inode and modify the original inode directly. Reject them while the file is in atomic write mode, as fallocate already does for pinned and compressed files. A write fault gets SIGBUS and fallocate gets -EOPNOTSUPP.
fallocate without these flags only preallocates blocks and stays allowed.
f2fs_vm_page_mkwrite() cannot take inode_lock, so check the flag under invalidate_lock, and hold invalidate_lock in f2fs_ioc_start_atomic_write() from the flush until the flag is set. Otherwise a fault could check the flag, then start_atomic_write could flush and set the flag, and the fault would dirty the folio afterwards.
--- fs/f2fs/file.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
fs/f2fs/file.c +18 -2
@@ -188,6 +188,17 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)file_update_time (vmf->vma->vm_file);filemap_invalidate_lock_shared (inode->i_mapping); +/* +* mmap stores bypass the COW inode and would break atomicity. Check +* under invalidate_lock: f2fs_ioc_start_atomic_write() holds it while +* flushing dirty folios and setting FI_ATOMIC_FILE, so either it sees +* our dirty folio and writes it back, or we see the flag. +*/ +if (unlikely (f2fs_is_atomic_file (inode))) { + err = -EOPNOTSUPP ; + goto out_sem; + } +folio_lock (folio);/* * A concurrent buffered write can extend isize, so we store isize in a
@@ -2113,9 +2124,11 @@ static long f2fs_fallocate(struct file *file, int mode,/* * Pinned file should not support partial truncation since the block -* can be used by applications. +* can be used by applications. Atomic files should not either, since +* these modify the original inode directly and break atomicity. */ -if ((f2fs_compressed_file (inode) ||f2fs_is_pinned_file (inode)) && +if ((f2fs_compressed_file (inode) ||f2fs_is_pinned_file (inode) || +f2fs_is_atomic_file (inode)) && (mode & (FALLOC_FL_PUNCH_HOLE |FALLOC_FL_COLLAPSE_RANGE |FALLOC_FL_ZERO_RANGE |FALLOC_FL_INSERT_RANGE ))) { ret = -EOPNOTSUPP ;
@@ -2403,6 +2416,8 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)f2fs_down_write (&fi->i_gc_rwsem[WRITE ]);f2fs_down_write (&fi->i_gc_rwsem[READ ]); +/* serialize with f2fs_vm_page_mkwrite(), see the comment there */ +filemap_invalidate_lock (inode->i_mapping);/* * Should wait end_io to count F2FS_WB_CP_DATA correctly by
@@ -2458,6 +2473,7 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)f2fs_i_size_write (fi->cow_inode, isize); out_unlock: +filemap_invalidate_unlock (inode->i_mapping);f2fs_up_write (&fi->i_gc_rwsem[READ ]);f2fs_up_write (&fi->i_gc_rwsem[WRITE ]);if (ret)
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/2 f2fs: atomic: fix to forbid dio in atomic_file
From: Chao Yu <[email protected]>
[ Upstream commit b2c160f4f3cfe9334b3e6bb52cb89a3a20ef0385 ]
atomic write can only be used via buffered IO, let's fail direct IO on atomic_file and return -EOPNOTSUPP.
Stable-dep-of: 06dfe4a7eb69 ("f2fs: disallow mmap write and data-modifying fallocate on atomic files")
--- fs/f2fs/file.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-)
fs/f2fs/file.c +24 -12
@@ -2281,6 +2281,7 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate) goto out;f2fs_down_write (&fi->i_gc_rwsem[WRITE ]); +f2fs_down_write (&fi->i_gc_rwsem[READ ]);/* * Should wait end_io to count F2FS_WB_CP_DATA correctly by
@@ -2290,10 +2291,8 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)f2fs_warn (sbi,"Unexpected flush for atomic writes: ino=%lu, npages=%u" , inode->i_ino,get_dirty_pages (inode)); ret =filemap_write_and_wait_range (inode->i_mapping,0 ,LLONG_MAX ); -if (ret) { -f2fs_up_write (&fi->i_gc_rwsem[WRITE ]); - goto out; - } +if (ret) + goto out_unlock;/* Check if the inode already has a COW inode */ if (fi->cow_inode ==NULL ) {
@@ -2302,10 +2301,8 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)struct inode *dir =d_inode (dentry->d_parent); ret =f2fs_get_tmpfile (idmap, dir, &fi->cow_inode); -if (ret) { -f2fs_up_write (&fi->i_gc_rwsem[WRITE ]); - goto out; - } +if (ret) + goto out_unlock;set_inode_flag (fi->cow_inode,FI_COW_FILE );clear_inode_flag (fi->cow_inode,FI_INLINE_DATA );
@@ -2319,10 +2316,8 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)invalidate_mapping_pages (fi->cow_inode->i_mapping,0 ,-1 ); ret =f2fs_do_truncate_blocks (fi->cow_inode,0 , true); -if (ret) { -f2fs_up_write (&fi->i_gc_rwsem[WRITE ]); - goto out; - } +if (ret) + goto out_unlock; }f2fs_write_inode (inode,NULL );
@@ -2341,7 +2336,11 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate) }f2fs_i_size_write (fi->cow_inode, isize); +out_unlock: +f2fs_up_write (&fi->i_gc_rwsem[READ ]);f2fs_up_write (&fi->i_gc_rwsem[WRITE ]); +if (ret) + goto out;f2fs_update_time (sbi,REQ_TIME ); fi->atomic_write_task = current;
@@ -4735,6 +4734,13 @@ static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)f2fs_down_read (&fi->i_gc_rwsem[READ ]); } +/* dio is not compatible w/ atomic file */ +if (f2fs_is_atomic_file (inode)) { +f2fs_up_read (&fi->i_gc_rwsem[READ ]); + ret = -EOPNOTSUPP ; + goto out; + } +/* * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of * the higher-level function iomap_dio_rw() in order to ensure that the
@@ -5140,6 +5146,12 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)/* Determine whether we will do a direct write or a buffered write. */ dio =f2fs_should_use_dio (inode, iocb, from); +/* dio is not compatible w/ atomic write */ +if (dio &&f2fs_is_atomic_file (inode)) { + ret = -EOPNOTSUPP ; + goto out_unlock; + } +/* Possibly preallocate the blocks for the write. */ target_size = iocb->ki_pos +iov_iter_count (from); preallocated =f2fs_preallocate_blocks (iocb, from, dio);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/2 f2fs: disallow mmap write and data-modifying fallocate on atomic files
From: Daeho Jeong <[email protected]>
[ Upstream commit 06dfe4a7eb696d868b196c2a49873142f19bd862 ]
Stores through a shared writable mapping and fallocate with FALLOC_FL_PUNCH_HOLE, FALLOC_FL_ZERO_RANGE, FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE do not go through ->write_begin, so they bypass the COW inode and modify the original inode directly. Reject them while the file is in atomic write mode, as fallocate already does for pinned and compressed files. A write fault gets SIGBUS and fallocate gets -EOPNOTSUPP.
fallocate without these flags only preallocates blocks and stays allowed.
f2fs_vm_page_mkwrite() cannot take inode_lock, so check the flag under invalidate_lock, and hold invalidate_lock in f2fs_ioc_start_atomic_write() from the flush until the flag is set. Otherwise a fault could check the flag, then start_atomic_write could flush and set the flag, and the fault would dirty the folio afterwards.
--- fs/f2fs/file.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
fs/f2fs/file.c +18 -2
@@ -178,6 +178,17 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)file_update_time (vmf->vma->vm_file);filemap_invalidate_lock_shared (inode->i_mapping); +/* +* mmap stores bypass the COW inode and would break atomicity. Check +* under invalidate_lock: f2fs_ioc_start_atomic_write() holds it while +* flushing dirty folios and setting FI_ATOMIC_FILE, so either it sees +* our dirty folio and writes it back, or we see the flag. +*/ +if (unlikely (f2fs_is_atomic_file (inode))) { + err = -EOPNOTSUPP ; + goto out_sem; + } +folio_lock (folio);/* * A concurrent buffered write can extend isize, so we store isize in a
@@ -1996,9 +2007,11 @@ static long f2fs_fallocate(struct file *file, int mode,/* * Pinned file should not support partial truncation since the block -* can be used by applications. +* can be used by applications. Atomic files should not either, since +* these modify the original inode directly and break atomicity. */ -if ((f2fs_compressed_file (inode) ||f2fs_is_pinned_file (inode)) && +if ((f2fs_compressed_file (inode) ||f2fs_is_pinned_file (inode) || +f2fs_is_atomic_file (inode)) && (mode & (FALLOC_FL_PUNCH_HOLE |FALLOC_FL_COLLAPSE_RANGE |FALLOC_FL_ZERO_RANGE |FALLOC_FL_INSERT_RANGE ))) { ret = -EOPNOTSUPP ;
@@ -2282,6 +2295,8 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)f2fs_down_write (&fi->i_gc_rwsem[WRITE ]);f2fs_down_write (&fi->i_gc_rwsem[READ ]); +/* serialize with f2fs_vm_page_mkwrite(), see the comment there */ +filemap_invalidate_lock (inode->i_mapping);/* * Should wait end_io to count F2FS_WB_CP_DATA correctly by
@@ -2337,6 +2352,7 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)f2fs_i_size_write (fi->cow_inode, isize); out_unlock: +filemap_invalidate_unlock (inode->i_mapping);f2fs_up_write (&fi->i_gc_rwsem[READ ]);f2fs_up_write (&fi->i_gc_rwsem[WRITE ]);if (ret)
Signature · 1 line
2.53.0