Skip to content

Commit

Permalink
btrfs: zoned: check if bio spans across an ordered extent
Browse files Browse the repository at this point in the history
To ensure that an ordered extent maps to a contiguous region on disk, we
need to maintain a "one bio == one ordered extent" rule.

Ensure that constructing bio does not span more than an ordered extent.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
  • Loading branch information
Johannes Thumshirn authored and David Sterba committed Feb 9, 2021
1 parent d22002f commit cacb2ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3120,6 +3120,8 @@ void btrfs_split_delalloc_extent(struct inode *inode,
struct extent_state *orig, u64 split);
int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
unsigned long bio_flags);
bool btrfs_bio_fits_in_ordered_extent(struct page *page, struct bio *bio,
unsigned int size);
void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end);
vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf);
int btrfs_readpage(struct file *file, struct page *page);
Expand Down
9 changes: 7 additions & 2 deletions fs/btrfs/extent_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -3124,10 +3124,15 @@ static bool btrfs_bio_add_page(struct bio *bio, struct page *page,
if (btrfs_bio_fits_in_stripe(page, size, bio, bio_flags))
return false;

if (bio_op(bio) == REQ_OP_ZONE_APPEND)
if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
struct page *first_page = bio_first_bvec_all(bio)->bv_page;

if (!btrfs_bio_fits_in_ordered_extent(first_page, bio, size))
return false;
ret = bio_add_zone_append_page(bio, page, size, pg_offset);
else
} else {
ret = bio_add_page(bio, page, size, pg_offset);
}

return ret == size;
}
Expand Down
27 changes: 27 additions & 0 deletions fs/btrfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,33 @@ static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
}

bool btrfs_bio_fits_in_ordered_extent(struct page *page, struct bio *bio,
unsigned int size)
{
struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
struct btrfs_fs_info *fs_info = inode->root->fs_info;
struct btrfs_ordered_extent *ordered;
u64 len = bio->bi_iter.bi_size + size;
bool ret = true;

ASSERT(btrfs_is_zoned(fs_info));
ASSERT(fs_info->max_zone_append_size > 0);
ASSERT(bio_op(bio) == REQ_OP_ZONE_APPEND);

/* Ordered extent not yet created, so we're good */
ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
if (!ordered)
return ret;

if ((bio->bi_iter.bi_sector << SECTOR_SHIFT) + len >
ordered->disk_bytenr + ordered->disk_num_bytes)
ret = false;

btrfs_put_ordered_extent(ordered);

return ret;
}

static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
struct bio *bio, loff_t file_offset)
{
Expand Down

0 comments on commit cacb2ce

Please sign in to comment.