From: Johannes Thumshirn <johannes.thumsh...@wdc.com>

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 <jo...@toxicpanda.com>
Signed-off-by: Johannes Thumshirn <johannes.thumsh...@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.a...@wdc.com>
---
 fs/btrfs/ctree.h     |  2 ++
 fs/btrfs/extent_io.c |  9 +++++++--
 fs/btrfs/inode.c     | 27 +++++++++++++++++++++++++++
 3 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index a9b0521d9e89..10da47ab093a 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -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);
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 15503a435e98..72b1a23d17f9 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -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;
 }
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 750482a06d67..31545e503b9e 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -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)
 {
-- 
2.30.0

Reply via email to