Skip to content

btrfs: fix corruption caused by partial dio writes

Boris Burkov linux-btrfs 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest5 patches 9 repliesIn review
  2. v46 patches 1 replySuperseded

Changes in v5 from the cover letter

- skip splitting em on nocow writes, this removes the need to refactor split_em, so remove that patch, and just rename split_zoned_em to split_em.

Patches in v5 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5btrfs: add function to create and return an ordered extentfs/btrfs/ordered-data.c fs/btrfs/ordered-data.h+43 -100 replies
2/5btrfs: stash ordered extent in dio_data during iomap diofs/btrfs/inode.c+24 -130 replies
3/5btrfs: return ordered_extent splits from bio extractionfs/btrfs/bio.c fs/btrfs/btrfs_inode.h fs/btrfs/inode.c +2 more+59 -267 replies
4/5btrfs: fix crash with non-zero pre in btrfs_split_ordered_extentfs/btrfs/ordered-data.c+13 -72 replies
5/5btrfs: split partial dio bios before submitfs/btrfs/inode.c+20 -10 replies

Reviews trailers per patch

1 of 5 patches has a Reviewed-by or Acked-by.

PatchFilipe MananaChristoph Hellwig
1/5R-b R-b
2/5
3/5
4/5
5/5

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v5 thread

Cover letter [PATCH v5 0/5] btrfs: fix corruption caused by partial dio writes

The final patch in this series ensures that bios submitted by btrfs dio match the corresponding ordered_extent and extent_map exactly. As a result, there is no hole or deadlock as a result of partial writes, even if the write buffer is a partly overlapping mapping of the range being written to.

This required a bit of refactoring and setup. Specifically, the zoned device code for "extracting" an ordered extent matching a bio could be reused with some refactoring to return the new ordered extents after the split.

Patch 1: Generic patch for returning an ordered extent while creating it Patch 2: Cache the dio ordered extent so that we can efficiently detect
partial writes during bio submission, without an extra lookup.
Patch 3: Use Patch 1 to track the new ordered_extent(s) that result from
splitting an ordered_extent.
Patch 4: Fix a bug in ordered extent splitting
Patch 5: Use the new more general split logic of Patch 4 and the stashed
ordered extent from Patch 2 to split partial dio bios to fix the corruption while avoiding the deadlock.

---
Changelog:
v5:
- skip splitting em on nocow writes, this removes the need to refactor split_em, so remove that patch, and just rename split_zoned_em to split_em.
v4:
- significant changes; redesign the fix to use bio splitting instead of extending the ordered_extent lifetime across calls into iomap.
- all the oe/em splitting refactoring and fixes
v3:
- handle BTRFS_IOERR set on the ordered_extent in btrfs_dio_iomap_end. If the bio fails before we loop in the submission loop and exit from the loop early, we never submit a second bio covering the rest of the extent range, resulting in leaking the ordered_extent, which hangs umount. We can distinguish this from a short write in btrfs_dio_iomap_end by checking the ordered_extent.
v2:
- rename new ordered extent function
- pull the new function into a prep patch
- reorganize how the ordered_extent is stored/passed around to avoid so many annoying memsets and exposing it to fs/btrfs/file.c
- lots of small code style improvements
- remove unintentional whitespace changes
- commit message improvements
- various ASSERTs for clarity/debugging

Boris Burkov (5):
btrfs: add function to create and return an ordered extent
btrfs: stash ordered extent in dio_data during iomap dio
btrfs: return ordered_extent splits from bio extraction
btrfs: fix crash with non-zero pre in btrfs_split_ordered_extent
btrfs: split partial dio bios before submit

 fs/btrfs/bio.c          |  2 +-
 fs/btrfs/btrfs_inode.h  |  5 ++-
 fs/btrfs/inode.c        | 94 +++++++++++++++++++++++++++++++----------
 fs/btrfs/ordered-data.c | 88 ++++++++++++++++++++++++++++----------
 fs/btrfs/ordered-data.h | 13 ++++--
 5 files changed, 152 insertions(+), 50 deletions(-)
Signature · 1 line
2.38.1