Skip to content

Commit 260a633

Browse files
fdmananakdave
authored andcommitted
btrfs: fix RWF_NOWAIT write not failling when we need to cow
If we attempt to do a RWF_NOWAIT write against a file range for which we can only do NOCOW for a part of it, due to the existence of holes or shared extents for example, we proceed with the write as if it were possible to NOCOW the whole range. Example: $ mkfs.btrfs -f /dev/sdb $ mount /dev/sdb /mnt $ touch /mnt/sdj/bar $ chattr +C /mnt/sdj/bar $ xfs_io -d -c "pwrite -S 0xab -b 256K 0 256K" /mnt/bar wrote 262144/262144 bytes at offset 0 256 KiB, 1 ops; 0.0003 sec (694.444 MiB/sec and 2777.7778 ops/sec) $ xfs_io -c "fpunch 64K 64K" /mnt/bar $ sync $ xfs_io -d -c "pwrite -N -V 1 -b 128K -S 0xfe 0 128K" /mnt/bar wrote 131072/131072 bytes at offset 0 128 KiB, 1 ops; 0.0007 sec (160.051 MiB/sec and 1280.4097 ops/sec) This last write should fail with -EAGAIN since the file range from 64K to 128K is a hole. On xfs it fails, as expected, but on ext4 it currently succeeds because apparently it is expensive to check if there are extents allocated for the whole range, but I'll check with the ext4 people. Fix the issue by checking if check_can_nocow() returns a number of NOCOW'able bytes smaller then the requested number of bytes, and if it does return -EAGAIN. Fixes: edf064e ("btrfs: nowait aio support") CC: [email protected] # 4.14+ Signed-off-by: Filipe Manana <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 4b19462 commit 260a633

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

fs/btrfs/file.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1904,18 +1904,29 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
19041904
pos = iocb->ki_pos;
19051905
count = iov_iter_count(from);
19061906
if (iocb->ki_flags & IOCB_NOWAIT) {
1907+
size_t nocow_bytes = count;
1908+
19071909
/*
19081910
* We will allocate space in case nodatacow is not set,
19091911
* so bail
19101912
*/
19111913
if (!(BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW |
19121914
BTRFS_INODE_PREALLOC)) ||
1913-
check_can_nocow(BTRFS_I(inode), pos, &count) <= 0) {
1915+
check_can_nocow(BTRFS_I(inode), pos, &nocow_bytes) <= 0) {
19141916
inode_unlock(inode);
19151917
return -EAGAIN;
19161918
}
19171919
/* check_can_nocow() locks the snapshot lock on success */
19181920
btrfs_drew_write_unlock(&root->snapshot_lock);
1921+
/*
1922+
* There are holes in the range or parts of the range that must
1923+
* be COWed (shared extents, RO block groups, etc), so just bail
1924+
* out.
1925+
*/
1926+
if (nocow_bytes < count) {
1927+
inode_unlock(inode);
1928+
return -EAGAIN;
1929+
}
19191930
}
19201931

19211932
current->backing_dev_info = inode_to_bdi(inode);

0 commit comments

Comments
 (0)