Skip to content

Commit 939b656

Browse files
fdmananakdave
authored andcommitted
btrfs: fix corruption after buffer fault in during direct IO append write
During an append (O_APPEND write flag) direct IO write if the input buffer was not previously faulted in, we can corrupt the file in a way that the final size is unexpected and it includes an unexpected hole. The problem happens like this: 1) We have an empty file, with size 0, for example; 2) We do an O_APPEND direct IO with a length of 4096 bytes and the input buffer is not currently faulted in; 3) We enter btrfs_direct_write(), lock the inode and call generic_write_checks(), which calls generic_write_checks_count(), and that function sets the iocb position to 0 with the following code: if (iocb->ki_flags & IOCB_APPEND) iocb->ki_pos = i_size_read(inode); 4) We call btrfs_dio_write() and enter into iomap, which will end up calling btrfs_dio_iomap_begin() and that calls btrfs_get_blocks_direct_write(), where we update the i_size of the inode to 4096 bytes; 5) After btrfs_dio_iomap_begin() returns, iomap will attempt to access the page of the write input buffer (at iomap_dio_bio_iter(), with a call to bio_iov_iter_get_pages()) and fail with -EFAULT, which gets returned to btrfs at btrfs_direct_write() via btrfs_dio_write(); 6) At btrfs_direct_write() we get the -EFAULT error, unlock the inode, fault in the write buffer and then goto to the label 'relock'; 7) We lock again the inode, do all the necessary checks again and call again generic_write_checks(), which calls generic_write_checks_count() again, and there we set the iocb's position to 4K, which is the current i_size of the inode, with the following code pointed above: if (iocb->ki_flags & IOCB_APPEND) iocb->ki_pos = i_size_read(inode); 8) Then we go again to btrfs_dio_write() and enter iomap and the write succeeds, but it wrote to the file range [4K, 8K), leaving a hole in the [0, 4K) range and an i_size of 8K, which goes against the expectations of having the data written to the range [0, 4K) and get an i_size of 4K. Fix this by not unlocking the inode before faulting in the input buffer, in case we get -EFAULT or an incomplete write, and not jumping to the 'relock' label after faulting in the buffer - instead jump to a location immediately before calling iomap, skipping all the write checks and relocking. This solves this problem and it's fine even in case the input buffer is memory mapped to the same file range, since only holding the range locked in the inode's io tree can cause a deadlock, it's safe to keep the inode lock (VFS lock), as was fixed and described in commit 51bd956 ("btrfs: fix deadlock due to page faults during direct IO reads and writes"). A sample reproducer provided by a reporter is the following: $ cat test.c #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include <fcntl.h> #include <stdio.h> #include <sys/mman.h> #include <sys/stat.h> #include <unistd.h> int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s <test file>\n", argv[0]); return 1; } int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_DIRECT | O_APPEND, 0644); if (fd < 0) { perror("creating test file"); return 1; } char *buf = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); ssize_t ret = write(fd, buf, 4096); if (ret < 0) { perror("pwritev2"); return 1; } struct stat stbuf; ret = fstat(fd, &stbuf); if (ret < 0) { perror("stat"); return 1; } printf("size: %llu\n", (unsigned long long)stbuf.st_size); return stbuf.st_size == 4096 ? 0 : 1; } A test case for fstests will be sent soon. Reported-by: Hanna Czenczek <[email protected]> Link: https://lore.kernel.org/linux-btrfs/[email protected]/ Fixes: 8184620 ("btrfs: fix lost file sync on direct IO write with nowait and dsync iocb") CC: [email protected] # 6.1+ Tested-by: Hanna Czenczek <[email protected]> Reviewed-by: Josef Bacik <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 8cd44dd commit 939b656

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

fs/btrfs/ctree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ struct btrfs_file_private {
459459
void *filldir_buf;
460460
u64 last_index;
461461
struct extent_state *llseek_cached_state;
462+
bool fsync_skip_inode_lock;
462463
};
463464

464465
static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)

fs/btrfs/direct-io.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -856,21 +856,37 @@ ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
856856
* So here we disable page faults in the iov_iter and then retry if we
857857
* got -EFAULT, faulting in the pages before the retry.
858858
*/
859+
again:
859860
from->nofault = true;
860861
dio = btrfs_dio_write(iocb, from, written);
861862
from->nofault = false;
862863

863-
/*
864-
* iomap_dio_complete() will call btrfs_sync_file() if we have a dsync
865-
* iocb, and that needs to lock the inode. So unlock it before calling
866-
* iomap_dio_complete() to avoid a deadlock.
867-
*/
868-
btrfs_inode_unlock(BTRFS_I(inode), ilock_flags);
869-
870-
if (IS_ERR_OR_NULL(dio))
864+
if (IS_ERR_OR_NULL(dio)) {
871865
ret = PTR_ERR_OR_ZERO(dio);
872-
else
866+
} else {
867+
struct btrfs_file_private stack_private = { 0 };
868+
struct btrfs_file_private *private;
869+
const bool have_private = (file->private_data != NULL);
870+
871+
if (!have_private)
872+
file->private_data = &stack_private;
873+
874+
/*
875+
* If we have a synchronous write, we must make sure the fsync
876+
* triggered by the iomap_dio_complete() call below doesn't
877+
* deadlock on the inode lock - we are already holding it and we
878+
* can't call it after unlocking because we may need to complete
879+
* partial writes due to the input buffer (or parts of it) not
880+
* being already faulted in.
881+
*/
882+
private = file->private_data;
883+
private->fsync_skip_inode_lock = true;
873884
ret = iomap_dio_complete(dio);
885+
private->fsync_skip_inode_lock = false;
886+
887+
if (!have_private)
888+
file->private_data = NULL;
889+
}
874890

875891
/* No increment (+=) because iomap returns a cumulative value. */
876892
if (ret > 0)
@@ -897,10 +913,12 @@ ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
897913
} else {
898914
fault_in_iov_iter_readable(from, left);
899915
prev_left = left;
900-
goto relock;
916+
goto again;
901917
}
902918
}
903919

920+
btrfs_inode_unlock(BTRFS_I(inode), ilock_flags);
921+
904922
/*
905923
* If 'ret' is -ENOTBLK or we have not written all data, then it means
906924
* we must fallback to buffered IO.

fs/btrfs/file.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,7 @@ static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx)
16031603
*/
16041604
int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
16051605
{
1606+
struct btrfs_file_private *private = file->private_data;
16061607
struct dentry *dentry = file_dentry(file);
16071608
struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
16081609
struct btrfs_root *root = inode->root;
@@ -1612,6 +1613,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
16121613
int ret = 0, err;
16131614
u64 len;
16141615
bool full_sync;
1616+
const bool skip_ilock = (private ? private->fsync_skip_inode_lock : false);
16151617

16161618
trace_btrfs_sync_file(file, datasync);
16171619

@@ -1639,7 +1641,10 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
16391641
if (ret)
16401642
goto out;
16411643

1642-
btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
1644+
if (skip_ilock)
1645+
down_write(&inode->i_mmap_lock);
1646+
else
1647+
btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
16431648

16441649
atomic_inc(&root->log_batch);
16451650

@@ -1663,7 +1668,10 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
16631668
*/
16641669
ret = start_ordered_ops(inode, start, end);
16651670
if (ret) {
1666-
btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
1671+
if (skip_ilock)
1672+
up_write(&inode->i_mmap_lock);
1673+
else
1674+
btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
16671675
goto out;
16681676
}
16691677

@@ -1788,7 +1796,10 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
17881796
* file again, but that will end up using the synchronization
17891797
* inside btrfs_sync_log to keep things safe.
17901798
*/
1791-
btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
1799+
if (skip_ilock)
1800+
up_write(&inode->i_mmap_lock);
1801+
else
1802+
btrfs_inode_unlock(inode, BTRFS_ILOCK_MMAP);
17921803

17931804
if (ret == BTRFS_NO_LOG_SYNC) {
17941805
ret = btrfs_end_transaction(trans);

0 commit comments

Comments
 (0)