]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
btrfs: fix incorrect buffered IO fallback for append direct writes
authorQu Wenruo <wqu@suse.com>
Thu, 4 Jun 2026 00:29:47 +0000 (09:59 +0930)
committerJohannes Thumshirn <johannes.thumshirn@wdc.com>
Tue, 9 Jun 2026 16:22:46 +0000 (18:22 +0200)
[BUG]
With the previous bug of short direct writes fixed, test case
generic/362 (*) still fails with the following error with nodatasum
mount option:

 generic/362  0s ... - output mismatch (see /home/adam/xfstests/results//generic/362.out.bad)
 - output mismatch (see /home/adam/xfstests/results//generic/362.out.bad)
    --- tests/generic/362.out 2024-08-24 15:31:37.200000000 +0930
    +++ /home/adam/xfstests/results//generic/362.out.bad 2026-05-27 10:13:09.072485767 +0930
    @@ -1,2 +1,3 @@
     QA output created by 362
    +Wrong file size after first write, got 8192 expected 4096
     Silence is golden
    ...

*: If the test case has been executed before with default data checksum,
the failure will not reproduce. Need the following fix to make it
reliably reproducible:
https://lore.kernel.org/linux-btrfs/20260528111659.87113-1-wqu@suse.com/

[CAUSE]
Inside btrfs_dio_iomap_begin() for a direct write, we increase the isize
if it's beyond the current isize.

But if the direct io finished short, we do not revert the isize to the
previous value nor to the short write end.

Then if we need to fall back to buffered writes, and the write has
IOCB_APPEND flag, then the buffered write will be positioned at the
incorrect isize.

The call chain looks like this:

 btrfs_direct_write(pos=0, length=4K)
 |- __iomap_dio_rw()
 |  |- iomap_iter()
 |  |  |- btrfs_dio_iomap_begin()
 |  |     |- btrfs_get_blocks_direct_write()
 |  |        |- i_size_write()
 |  |           Which updates the isize to the write end (4K).
 |  |
 |  |- iomap_dio_iter()
 |  |  Failed with -EFAULT on the first page.
 |  |
 |  |- iomap_iter()
 |  |  |- btrfs_dio_iomap_end()
 |  |     Detects a short write, return -ENOTBLK
 |  |- if (ret == -ENOTBLK) { ret = 0;}
 |     Which resets the return value.
 |
 |- ret = iomap_dio_complet()
 |  Which returns 0.
 |
 |- btrfs_buffered_write(iocb, from);
    |- generic_write_checks()
       |- iocb->ki_pos = i_size_read()
          Which is still the new size (4K), other than the original
  isize 0.

[FIX]
Introduce the following btrfs_dio_data members:

- old_isize

- updated_isize
  If the direct write has enlarged the isize.

Then if we got a short write, and btrfs_dio_data::updated_isize is set,
revert to the correct isize based on old_isize and current file
position.

And here we call i_size_write() without holding an extent lock, which is
a very special case that we're safe to do:

 - Only a single writer can be enlarging isize
   Enlarging isize will take the exclusive inode lock.

 - Buffered readers need to wait for the OE we're holding
   Buffered readers will lock extent and wait for OE of the folio range.
   Sometimes we can skip the OE wait, but since all page cache is
   invalidated, the OE wait can not be skipped.

But I do not think this is the most elegant solution, nor covers all
cases. E.g. if the bio is submitted but IO failed, we are unable to do
the revert.

I believe the more elegant one would be extend the EXTENT_DIO_LOCKED
lifespan for direct writes, so that we can update the isize when a
write beyond EOF finished successfully.

However that change is too huge for a small bug fix.
So only implement the minimal partial fix for now.

[REASON FOR NO FIXES TAG]
The bug is again very old, before commit f85781fb505e ("btrfs: switch to
iomap for direct IO") we are already increasing isize without a
proper rollback for short writes.

Thus only a CC to stable.

CC: stable@vger.kernel.org # 5.15+
Reviewed-by: Boris Burkov <boris@bur.io>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/direct-io.c

index 88cb2e82a50768221ae4399c83925e90280df06c..412309825d6fa36bd18ceb0172bd30d919996848 100644 (file)
 
 struct btrfs_dio_data {
        ssize_t submitted;
+       loff_t old_isize;
        struct extent_changeset *data_reserved;
        struct btrfs_ordered_extent *ordered;
        bool data_space_reserved;
        bool nocow_done;
+       bool updated_isize;
 };
 
 struct btrfs_dio_private {
@@ -228,6 +230,7 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map,
        bool space_reserved = false;
        u64 len = *lenp;
        u64 prev_len;
+       loff_t old_isize;
        int ret = 0;
 
        /*
@@ -341,8 +344,14 @@ static int btrfs_get_blocks_direct_write(struct extent_map **map,
         * Need to update the i_size under the extent lock so buffered
         * readers will get the updated i_size when we unlock.
         */
-       if (start + len > i_size_read(inode))
+       old_isize = i_size_read(inode);
+       if (start + len > old_isize) {
+               if (!dio_data->updated_isize) {
+                       dio_data->old_isize = old_isize;
+                       dio_data->updated_isize = true;
+               }
                i_size_write(inode, start + len);
+       }
 out:
        if (ret && space_reserved) {
                btrfs_delalloc_release_extents(BTRFS_I(inode), len);
@@ -625,6 +634,38 @@ static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
                pos += submitted;
                length -= submitted;
                if (write) {
+                       /*
+                        * Got a short write and have updated the isize, need to
+                        * revert the isize change.
+                        *
+                        * Normally we need to update isize with extent lock hold,
+                        * but we're safe due to the following factors:
+                        *
+                        * - Only a single writer can be enlarging isize
+                        *   Enlarging isize will take the exclusive inode lock.
+                        *
+                        * - Buffered readers need to wait for the OE we're holding
+                        *   Buffered readers will lock extent and wait for OE
+                        *   of the folio range, and since page cache is invalidated
+                        *   the OE wait can not be skipped.
+                        *
+                        * So here we are safe to revert the isize before
+                        * finishing the OE, and no reader of the remaining range
+                        * can see the enlarged size.
+                        *
+                        * TODO: Extend the DIO_LOCKED lifespan for direct writes,
+                        * and only enlarge isize after a successful write.
+                        */
+                       if (dio_data->updated_isize) {
+                               u64 new_isize;
+
+                               if (submitted == 0)
+                                       new_isize = dio_data->old_isize;
+                               else
+                                       new_isize = max(dio_data->old_isize, pos);
+                               i_size_write(inode, new_isize);
+                               dio_data->updated_isize = false;
+                       }
                        /*
                         * We have a short write, if there is any range
                         * that is submitted properly, that part will have