+++ /dev/null
-From 6ac90347d410797dff70ea412aacb446f898b366 Mon Sep 17 00:00:00 2001
-From: Sasha Levin <sashal@kernel.org>
-Date: Tue, 25 May 2021 13:52:43 +0800
-Subject: btrfs: fix compressed writes that cross stripe boundary
-
-From: Qu Wenruo <wqu@suse.com>
-
-[ Upstream commit 4c80a97d7b02cf68e169118ef2bda0725fc87f6f ]
-
-[BUG]
-When running btrfs/027 with "-o compress" mount option, it always
-crashes with the following call trace:
-
- BTRFS critical (device dm-4): mapping failed logical 298901504 bio len 12288 len 8192
- ------------[ cut here ]------------
- kernel BUG at fs/btrfs/volumes.c:6651!
- invalid opcode: 0000 [#1] PREEMPT SMP NOPTI
- CPU: 5 PID: 31089 Comm: kworker/u24:10 Tainted: G OE 5.13.0-rc2-custom+ #26
- Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015
- Workqueue: btrfs-delalloc btrfs_work_helper [btrfs]
- RIP: 0010:btrfs_map_bio.cold+0x58/0x5a [btrfs]
- Call Trace:
- btrfs_submit_compressed_write+0x2d7/0x470 [btrfs]
- submit_compressed_extents+0x3b0/0x470 [btrfs]
- ? mark_held_locks+0x49/0x70
- btrfs_work_helper+0x131/0x3e0 [btrfs]
- process_one_work+0x28f/0x5d0
- worker_thread+0x55/0x3c0
- ? process_one_work+0x5d0/0x5d0
- kthread+0x141/0x160
- ? __kthread_bind_mask+0x60/0x60
- ret_from_fork+0x22/0x30
- ---[ end trace 63113a3a91f34e68 ]---
-
-[CAUSE]
-The critical message before the crash means we have a bio at logical
-bytenr 298901504 length 12288, but only 8192 bytes can fit into one
-stripe, the remaining 4096 bytes go to another stripe.
-
-In btrfs, all bios are properly split to avoid cross stripe boundary,
-but commit 764c7c9a464b ("btrfs: zoned: fix parallel compressed writes")
-changed the behavior for compressed writes.
-
-Previously if we find our new page can't be fitted into current stripe,
-ie. "submit == 1" case, we submit current bio without adding current
-page.
-
- submit = btrfs_bio_fits_in_stripe(page, PAGE_SIZE, bio, 0);
-
- page->mapping = NULL;
- if (submit || bio_add_page(bio, page, PAGE_SIZE, 0) <
- PAGE_SIZE) {
-
-But after the modification, we will add the page no matter if it crosses
-stripe boundary, leading to the above crash.
-
- submit = btrfs_bio_fits_in_stripe(page, PAGE_SIZE, bio, 0);
-
- if (pg_index == 0 && use_append)
- len = bio_add_zone_append_page(bio, page, PAGE_SIZE, 0);
- else
- len = bio_add_page(bio, page, PAGE_SIZE, 0);
-
- page->mapping = NULL;
- if (submit || len < PAGE_SIZE) {
-
-[FIX]
-It's no longer possible to revert to the original code style as we have
-two different bio_add_*_page() calls now.
-
-The new fix is to skip the bio_add_*_page() call if @submit is true.
-
-Also to avoid @len to be uninitialized, always initialize it to zero.
-
-If @submit is true, @len will not be checked.
-If @submit is not true, @len will be the return value of
-bio_add_*_page() call.
-Either way, the behavior is still the same as the old code.
-
-Reported-by: Josef Bacik <josef@toxicpanda.com>
-Fixes: 764c7c9a464b ("btrfs: zoned: fix parallel compressed writes")
-Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
-Signed-off-by: Qu Wenruo <wqu@suse.com>
-Signed-off-by: David Sterba <dsterba@suse.com>
-Signed-off-by: Sasha Levin <sashal@kernel.org>
----
- fs/btrfs/compression.c | 17 ++++++++++++-----
- 1 file changed, 12 insertions(+), 5 deletions(-)
-
-diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
-index fe0ec3d321b8..752b3cffe226 100644
---- a/fs/btrfs/compression.c
-+++ b/fs/btrfs/compression.c
-@@ -457,7 +457,7 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
- bytes_left = compressed_len;
- for (pg_index = 0; pg_index < cb->nr_pages; pg_index++) {
- int submit = 0;
-- int len;
-+ int len = 0;
-
- page = compressed_pages[pg_index];
- page->mapping = inode->vfs_inode.i_mapping;
-@@ -465,10 +465,17 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
- submit = btrfs_bio_fits_in_stripe(page, PAGE_SIZE, bio,
- 0);
-
-- if (pg_index == 0 && use_append)
-- len = bio_add_zone_append_page(bio, page, PAGE_SIZE, 0);
-- else
-- len = bio_add_page(bio, page, PAGE_SIZE, 0);
-+ /*
-+ * Page can only be added to bio if the current bio fits in
-+ * stripe.
-+ */
-+ if (!submit) {
-+ if (pg_index == 0 && use_append)
-+ len = bio_add_zone_append_page(bio, page,
-+ PAGE_SIZE, 0);
-+ else
-+ len = bio_add_page(bio, page, PAGE_SIZE, 0);
-+ }
-
- page->mapping = NULL;
- if (submit || len < PAGE_SIZE) {
---
-2.30.2
-
+++ /dev/null
-From 9061dbf92469820de013b2dfe17ee85e06c00f6e Mon Sep 17 00:00:00 2001
-From: Sasha Levin <sashal@kernel.org>
-Date: Wed, 19 May 2021 00:40:28 +0900
-Subject: btrfs: zoned: fix parallel compressed writes
-
-From: Johannes Thumshirn <johannes.thumshirn@wdc.com>
-
-[ Upstream commit 764c7c9a464b68f7c6a5a9ec0b923176a05e8e8f ]
-
-When multiple processes write data to the same block group on a
-compressed zoned filesystem, the underlying device could report I/O
-errors and data corruption is possible.
-
-This happens because on a zoned file system, compressed data writes
-where sent to the device via a REQ_OP_WRITE instead of a
-REQ_OP_ZONE_APPEND operation. But with REQ_OP_WRITE and parallel
-submission it cannot be guaranteed that the data is always submitted
-aligned to the underlying zone's write pointer.
-
-The change to using REQ_OP_ZONE_APPEND instead of REQ_OP_WRITE on a
-zoned filesystem is non intrusive on a regular file system or when
-submitting to a conventional zone on a zoned filesystem, as it is
-guarded by btrfs_use_zone_append.
-
-Reported-by: David Sterba <dsterba@suse.com>
-Fixes: 9d294a685fbc ("btrfs: zoned: enable to mount ZONED incompat flag")
-CC: stable@vger.kernel.org # 5.12.x: e380adfc213a13: btrfs: zoned: pass start block to btrfs_use_zone_append
-CC: stable@vger.kernel.org # 5.12.x
-Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
-Signed-off-by: David Sterba <dsterba@suse.com>
-Signed-off-by: Sasha Levin <sashal@kernel.org>
----
- fs/btrfs/compression.c | 42 ++++++++++++++++++++++++++++++++++++++----
- 1 file changed, 38 insertions(+), 4 deletions(-)
-
-diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
-index 81387cdf334d..fe0ec3d321b8 100644
---- a/fs/btrfs/compression.c
-+++ b/fs/btrfs/compression.c
-@@ -28,6 +28,7 @@
- #include "compression.h"
- #include "extent_io.h"
- #include "extent_map.h"
-+#include "zoned.h"
-
- static const char* const btrfs_compress_types[] = { "", "zlib", "lzo", "zstd" };
-
-@@ -349,6 +350,7 @@ static void end_compressed_bio_write(struct bio *bio)
- */
- inode = cb->inode;
- cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
-+ btrfs_record_physical_zoned(inode, cb->start, bio);
- btrfs_writepage_endio_finish_ordered(cb->compressed_pages[0],
- cb->start, cb->start + cb->len - 1,
- bio->bi_status == BLK_STS_OK);
-@@ -401,6 +403,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
- u64 first_byte = disk_start;
- blk_status_t ret;
- int skip_sum = inode->flags & BTRFS_INODE_NODATASUM;
-+ const bool use_append = btrfs_use_zone_append(inode, disk_start);
-+ const unsigned int bio_op = use_append ? REQ_OP_ZONE_APPEND : REQ_OP_WRITE;
-
- WARN_ON(!PAGE_ALIGNED(start));
- cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS);
-@@ -418,10 +422,31 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
- cb->nr_pages = nr_pages;
-
- bio = btrfs_bio_alloc(first_byte);
-- bio->bi_opf = REQ_OP_WRITE | write_flags;
-+ bio->bi_opf = bio_op | write_flags;
- bio->bi_private = cb;
- bio->bi_end_io = end_compressed_bio_write;
-
-+ if (use_append) {
-+ struct extent_map *em;
-+ struct map_lookup *map;
-+ struct block_device *bdev;
-+
-+ em = btrfs_get_chunk_map(fs_info, disk_start, PAGE_SIZE);
-+ if (IS_ERR(em)) {
-+ kfree(cb);
-+ bio_put(bio);
-+ return BLK_STS_NOTSUPP;
-+ }
-+
-+ map = em->map_lookup;
-+ /* We only support single profile for now */
-+ ASSERT(map->num_stripes == 1);
-+ bdev = map->stripes[0].dev->bdev;
-+
-+ bio_set_dev(bio, bdev);
-+ free_extent_map(em);
-+ }
-+
- if (blkcg_css) {
- bio->bi_opf |= REQ_CGROUP_PUNT;
- kthread_associate_blkcg(blkcg_css);
-@@ -432,6 +457,7 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
- bytes_left = compressed_len;
- for (pg_index = 0; pg_index < cb->nr_pages; pg_index++) {
- int submit = 0;
-+ int len;
-
- page = compressed_pages[pg_index];
- page->mapping = inode->vfs_inode.i_mapping;
-@@ -439,9 +465,13 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
- submit = btrfs_bio_fits_in_stripe(page, PAGE_SIZE, bio,
- 0);
-
-+ if (pg_index == 0 && use_append)
-+ len = bio_add_zone_append_page(bio, page, PAGE_SIZE, 0);
-+ else
-+ len = bio_add_page(bio, page, PAGE_SIZE, 0);
-+
- page->mapping = NULL;
-- if (submit || bio_add_page(bio, page, PAGE_SIZE, 0) <
-- PAGE_SIZE) {
-+ if (submit || len < PAGE_SIZE) {
- /*
- * inc the count before we submit the bio so
- * we know the end IO handler won't happen before
-@@ -465,11 +495,15 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
- }
-
- bio = btrfs_bio_alloc(first_byte);
-- bio->bi_opf = REQ_OP_WRITE | write_flags;
-+ bio->bi_opf = bio_op | write_flags;
- bio->bi_private = cb;
- bio->bi_end_io = end_compressed_bio_write;
- if (blkcg_css)
- bio->bi_opf |= REQ_CGROUP_PUNT;
-+ /*
-+ * Use bio_add_page() to ensure the bio has at least one
-+ * page.
-+ */
- bio_add_page(bio, page, PAGE_SIZE, 0);
- }
- if (bytes_left < PAGE_SIZE) {
---
-2.30.2
-