Commit 4aaedfb0 authored by Liu Bo's avatar Liu Bo Committed by David Sterba

Btrfs: fix another race between truncate and lockless dio write

Dio writes can update i_size in btrfs_get_blocks_direct when it
writes to offset beyond EOF so that endio can update disk_i_size
correctly (because we don't udpate disk_i_size beyond i_size).

However, when truncating down a file, we firstly update i_size
and then wait for in-flight lockless dio reads/writes, according
to the above, i_size may have been changed in dio writes, and
file extents don't get truncated.

For lockless dio writes are always overwrites, i_size is not
supposed to be changed, so this adds a check to filter out this
case.

The race could be reproduced by fstests/generic/299 with patch
"Btrfs: fix btrfs_ordered_update_i_size to update disk_i_size properly"
 applied.
Signed-off-by: default avatarLiu Bo <bo.li.liu@oracle.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 62c821a8
...@@ -71,6 +71,7 @@ struct btrfs_dio_data { ...@@ -71,6 +71,7 @@ struct btrfs_dio_data {
u64 reserve; u64 reserve;
u64 unsubmitted_oe_range_start; u64 unsubmitted_oe_range_start;
u64 unsubmitted_oe_range_end; u64 unsubmitted_oe_range_end;
int overwrite;
}; };
static const struct inode_operations btrfs_dir_inode_operations; static const struct inode_operations btrfs_dir_inode_operations;
...@@ -7809,7 +7810,7 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, ...@@ -7809,7 +7810,7 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
* Need to update the i_size under the extent lock so buffered * Need to update the i_size under the extent lock so buffered
* readers will get the updated i_size when we unlock. * readers will get the updated i_size when we unlock.
*/ */
if (start + len > i_size_read(inode)) if (!dio_data->overwrite && start + len > i_size_read(inode))
i_size_write(inode, start + len); i_size_write(inode, start + len);
adjust_dio_outstanding_extents(inode, dio_data, len); adjust_dio_outstanding_extents(inode, dio_data, len);
...@@ -8685,6 +8686,7 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) ...@@ -8685,6 +8686,7 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
* not unlock the i_mutex at this case. * not unlock the i_mutex at this case.
*/ */
if (offset + count <= inode->i_size) { if (offset + count <= inode->i_size) {
dio_data.overwrite = 1;
inode_unlock(inode); inode_unlock(inode);
relock = true; relock = true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment