btrfs: fix double accounting of ordered extent for subpage case in btrfs_invalidapge
authorQu Wenruo <wqu@suse.com>
Wed, 27 Jan 2021 06:38:48 +0000 (14:38 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 8 Feb 2021 21:58:59 +0000 (22:58 +0100)
commit951c80f83d61bd4b21794c8aba829c3c1a45c2d0
tree821d2c45748bb8db1b2fa87663667afe720612eb
parenta4559e6f6f3a4e84cb788ac158fb419ece473527
btrfs: fix double accounting of ordered extent for subpage case in btrfs_invalidapge

Commit dbfdb6d1b369 ("Btrfs: Search for all ordered extents that could
span across a page") make btrfs_invalidapage() to search all ordered
extents.

The offending code looks like this:

  again:
  start = page_start;
  ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
  if (ordred) {
  end = min(page_end,
    ordered->file_offset + ordered->num_bytes - 1);

  /* Do the cleanup */

  start = end + 1;
  if (start < page_end)
  goto again;
  }

The behavior is indeed necessary for the incoming subpage support, but
when it iterates through all the ordered extents, it also resets the
search range @start.

This means, for the following cases, we can double account the ordered
extents, causing its bytes_left underflow:

Page offset
0 16K 32K
|<--- OE 1  --->|<--- OE 2 ---->|

As the first iteration will find ordered extent (OE) 1, which doesn't
cover the full page, thus after cleanup code, we need to retry again.
But again label will reset start to page_start, and we got OE 1 again,
which causes double accounting on OE 1, and cause OE 1's byte_left to
underflow.

This problem can only happen for subpage case, as for regular sectorsize
== PAGE_SIZE case, we will always find a OE ends at or after page end,
thus no way to trigger the problem.

Move the again label after start = page_start.  There will be more
comprehensive rework to convert the open coded loop to a proper while
loop for subpage support.

Fixes: dbfdb6d1b369 ("Btrfs: Search for all ordered extents that could span across a page")
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/inode.c