btrfs: scrub: use a more reader friendly code to implement scrub_simple_mirror()
v2 cover btrfs: scrub: use a more reader friendly code to implement scrub_simple_mirror()
This series can be found in my github repo:
https://github.com/adam900710/linux/tree/scrub_stripe
It's recommended to fetch from the repo, as our misc-next seems to change pretty rapidly.
[Changelog]
v2:
- Use batched scrub_stripe submission
This allows much better performance compared to the old scrub code
- Add scrub specific bio layer helpers
This makes the scrub code to be completely rely on logical bytenr + mirror_num.
[PROBLEMS OF OLD SCRUB]
- Too many delayed jumps, making it hard to read
Even starting from scrub_simple_mirror(), we have the following functions:
scrub_extent()
|
v
scrub_sectors()
|
v
scrub_add_sector_to_rd_bio()
| endio function
v
scrub_bio_end_io()
| delayed work
v
scrub_bio_end_io_worker()
|
v
scrub_block_complete()
|
v
scrub_handle_errored_blocks()
|
v
scrub_recheck_block()
|
v scrub_repair_sector_from_good_copy()
Not to mention the hidden jumps in certain branches.
- IOPS inefficient for fragmented extents
The real block size of scrub read is between 4K and 128K. If the extents are not adjacent, the blocksize drops to 4K and would be an IOPS disaster.
- All hardcoded to do the logical -> physical mapping by scrub itself No usage of any existing bio facilities.
And even implemented a RAID56 recovery wrapper.
[NEW SCRUB_STRIPE BASED SOLUTION]
- Overall streamlined code base
queue_scrub_stripe()
|
v
scrub_find_fill_first_stripe()
|
vdone
Or
queue_scrub_stripe()
|
v
flush_scrub_stripes()
|
v
scrub_submit_initial_read()
| endio function
v
scrub_read_endio()
| delayed work
v
scrub_stripe_read_repair_worker()
|
v
scrub_verify_one_stripe()
|
v
scrub_stripe_submit_repair_read()
|
v
scrub_write_sectors()
|
v scrub_stripe_report_errors()
Only one endio and delayed work, all other work are properly done in a sequential workflow.
- Always read in 64KiB block size
The real blocksize of read starts at 64KiB, and ends at 512K. This already results a better performance even for the worst case:
With patchset: 404.81MiB/s
Without patchset: 369.30MiB/s
Around 10% performance improvement on an SATA SSD.
- All logical bytenr/mirror_num based read and write
With the new single stripe fast path in btrfs_submit_bio(), scrub can reuse most of the bio layer code, result much simpler scrub code.
[TODO]
- More testing on zoned devices
Now the patchset can already pass all scrub/replace groups with regular devices.
- More cleanup on RAID56 path
Now RAID56 still uses some old facility, resulting things like scrub_sector and scrub_bio can not be fully cleaned up.
Qu Wenruo (12):
btrfs: scrub: use dedicated super block verification function to scrub
one super block
btrfs: introduce a new helper to submit bio for scrub
btrfs: introduce a new helper to submit write bio for scrub
btrfs: scrub: introduce the structure for new BTRFS_STRIPE_LEN based
interface
btrfs: scrub: introduce a helper to find and fill the sector info for
a scrub_stripe
btrfs: scrub: introduce a helper to verify one metadata
btrfs: scrub: introduce a helper to verify one scrub_stripe
btrfs: scrub: introduce the main read repair worker for scrub_stripe
btrfs: scrub: introduce a writeback helper for scrub_stripe
btrfs: scrub: introduce error reporting functionality for scrub_stripe
btrfs: scrub: introduce the helper to queue a stripe for scrub
btrfs: scrub: switch scrub_simple_mirror() to scrub_stripe
infrastructure
fs/btrfs/bio.c | 142 +++- fs/btrfs/bio.h | 22 +- fs/btrfs/file-item.c | 9 +- fs/btrfs/file-item.h | 3 +- fs/btrfs/raid56.c | 2 +- fs/btrfs/scrub.c | 1656 ++++++++++++++++++++++++++++++------------ 6 files changed, 1348 insertions(+), 486 deletions(-)
Signature · 1 line
2.39.2
PATCH v2 1/12 btrfs: scrub: use dedicated super block verification function to scrub one super block
There is really no need to go through the super complex scrub_sectors() to just handle super blocks.
This patch will introduce a dedicated function (less than 50 lines) to handle super block scrubing.
This new function will introduce a behavior change, instead of using the complex but concurrent scrub_bio system, here we just go submit-and-wait.
There is really not much sense to care the performance of super block scrubbing. It only has 3 super blocks at most, and they are all scattered around the devices already.
--- fs/btrfs/scrub.c | 54 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-)
fs/btrfs/scrub.c +46 -8
@@ -4243,18 +4243,59 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,return ret; } +static int scrub_one_super (struct scrub_ctx *sctx,struct btrfs_device *dev, +struct page *page,u64 physical,u64 generation) +{ +struct btrfs_fs_info *fs_info = sctx->fs_info; +struct bio_vec bvec; +struct bio bio; +struct btrfs_super_block *sb =page_address (page); +int ret; + +bio_init (&bio, dev->bdev, &bvec,1 ,REQ_OP_READ ); + bio.bi_iter.bi_sector = physical >>SECTOR_SHIFT ; +bio_add_page (&bio, page,BTRFS_SUPER_INFO_SIZE ,0 ); + ret =submit_bio_wait (&bio); +bio_uninit (&bio); + +if (ret <0 ) +return ret; + ret =btrfs_check_super_csum (fs_info, sb); +if (ret !=0 ) { +btrfs_err_rl (fs_info, +"super block at physical %llu devid %llu has bad csum" , + physical, dev->devid); +return -EIO ; + } +if (btrfs_super_generation (sb) != generation) { +btrfs_err_rl (fs_info, +"super block at physical %llu devid %llu has bad generation, has %llu expect %llu" , + physical, dev->devid, +btrfs_super_generation (sb), generation); +return -EUCLEAN ; + } + + ret =btrfs_validate_super (fs_info, sb,-1 ); +return ret; +} + static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,struct btrfs_device *scrub_dev) {int i;u64 bytenr;u64 gen; -int ret; +int ret =0 ; +struct page *page;struct btrfs_fs_info *fs_info = sctx->fs_info;if (BTRFS_FS_ERROR (fs_info))return -EROFS ; + page =alloc_page (GFP_KERNEL ); +if (!page) +return -ENOMEM ; +/* Seed devices of a new filesystem has their own generation. */ if (scrub_dev->fs_devices != fs_info->fs_devices) gen = scrub_dev->generation;
@@ -4269,15 +4310,12 @@ static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,if (!btrfs_check_super_location (scrub_dev, bytenr))continue ; - ret =scrub_sectors (sctx, bytenr,BTRFS_SUPER_INFO_SIZE , bytenr, - scrub_dev,BTRFS_EXTENT_FLAG_SUPER , gen, i, -NULL , bytenr); + ret =scrub_one_super (sctx, scrub_dev, page, bytenr, gen);if (ret) -return ret; +break ; } -wait_event (sctx->list_wait,atomic_read (&sctx->bios_in_flight) ==0 ); - -return 0 ; +__free_page (page); +return ret; }static void scrub_workers_put (struct btrfs_fs_info *fs_info)
Signature · 1 line
2.39.2
Re: [PATCH v2 01/12] btrfs: scrub: use dedicated super block verification function to scrub one super block
Quoted 32 lines · Qu Wenruo
There is really no need to go through the super complex
scrub_sectors()to just handle super blocks.This patch will introduce a dedicated function (less than 50 lines) to handle super block scrubing.
This new function will introduce a behavior change, instead of using the complex but concurrent
scrub_biosystem, here we just go submit-and-wait.There is really not much sense to care the performance of super block scrubbing. It only has 3 super blocks at most, and they are all scattered around the devices already.
Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/scrub.c | 54 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-)fs/btrfs/scrub.c +5
-0@@ -4243,18 +4243,59 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,return ret; } +static int scrub_one_super (struct scrub_ctx *sctx,struct btrfs_device *dev, +struct page *page,u64 physical,u64 generation) +{ +struct btrfs_fs_info *fs_info = sctx->fs_info; +struct bio_vec bvec;
fs/btrfs/scrub.c int scrub_enumerate_chunks(struct scrub_ctx *sctx, · line 4260
Quoted 10 lines
fs/btrfs/scrub.c +10
-0+struct bio bio; +struct btrfs_super_block *sb =page_address (page); +int ret; + +bio_init (&bio, dev->bdev, &bvec,1 ,REQ_OP_READ ); + bio.bi_iter.bi_sector = physical >>SECTOR_SHIFT ; +bio_add_page (&bio, page,BTRFS_SUPER_INFO_SIZE ,0 ); + ret =submit_bio_wait (&bio); +bio_uninit (&bio); +
I don't think bio_uninit() is needed here. You're not attaching any cgroup information, bio integrity or crypto context to it. Or can that be attached down the stack?
Re: [PATCH v2 01/12] btrfs: scrub: use dedicated super block verification function to scrub one super block
Quoted 38 lines · Johannes Thumshirn
Quoted 37 lines · Qu Wenruo
There is really no need to go through the super complex
scrub_sectors()to just handle super blocks.This patch will introduce a dedicated function (less than 50 lines) to handle super block scrubing.
This new function will introduce a behavior change, instead of using the complex but concurrent
scrub_biosystem, here we just go submit-and-wait.There is really not much sense to care the performance of super block scrubbing. It only has 3 super blocks at most, and they are all scattered around the devices already.
Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/scrub.c | 54 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-)fs/btrfs/scrub.c +10
-0@@ -4243,18 +4243,59 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,return ret; } +static int scrub_one_super (struct scrub_ctx *sctx,struct btrfs_device *dev, +struct page *page,u64 physical,u64 generation) +{ +struct btrfs_fs_info *fs_info = sctx->fs_info; +struct bio_vec bvec; +struct bio bio; +struct btrfs_super_block *sb =page_address (page); +int ret; + +bio_init (&bio, dev->bdev, &bvec,1 ,REQ_OP_READ );
Quoted 8 lines
fs/btrfs/scrub.c int scrub_enumerate_chunks(struct scrub_ctx *sctx, · line 4260
Quoted 5 lines
fs/btrfs/scrub.c +5
-0+ bio.bi_iter.bi_sector = physical >>SECTOR_SHIFT ; +bio_add_page (&bio, page,BTRFS_SUPER_INFO_SIZE ,0 ); + ret =submit_bio_wait (&bio); +bio_uninit (&bio); +I don't think
bio_uninit()is needed here. You're not attaching any cgroup information, bio integrity or crypto context to it. Or can that be attached down the stack?
It may not be needed due to functional reasons but I'd rather keep it there to pair with bio_init.
Re: [PATCH v2 01/12] btrfs: scrub: use dedicated super block verification function to scrub one super block
In reply to Johannes ThumshirnQuoted 38 lines · Johannes Thumshirn
Quoted 37 lines · Qu Wenruo
There is really no need to go through the super complex
scrub_sectors()to just handle super blocks.This patch will introduce a dedicated function (less than 50 lines) to handle super block scrubing.
This new function will introduce a behavior change, instead of using the complex but concurrent
scrub_biosystem, here we just go submit-and-wait.There is really not much sense to care the performance of super block scrubbing. It only has 3 super blocks at most, and they are all scattered around the devices already.
Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/scrub.c | 54 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-)fs/btrfs/scrub.c +10
-0@@ -4243,18 +4243,59 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,return ret; } +static int scrub_one_super (struct scrub_ctx *sctx,struct btrfs_device *dev, +struct page *page,u64 physical,u64 generation) +{ +struct btrfs_fs_info *fs_info = sctx->fs_info; +struct bio_vec bvec; +struct bio bio; +struct btrfs_super_block *sb =page_address (page); +int ret; + +bio_init (&bio, dev->bdev, &bvec,1 ,REQ_OP_READ );
Quoted 9 lines
fs/btrfs/scrub.c int scrub_enumerate_chunks(struct scrub_ctx *sctx, · line 4260
Quoted 5 lines
fs/btrfs/scrub.c +5
-0+ bio.bi_iter.bi_sector = physical >>SECTOR_SHIFT ; +bio_add_page (&bio, page,BTRFS_SUPER_INFO_SIZE ,0 ); + ret =submit_bio_wait (&bio); +bio_uninit (&bio); +I don't think
bio_uninit()is needed here. You're not attaching any cgroup information, bio integrity or crypto context to it. Or can that be attached down the stack?
It's mostly to pair with bio_init().
Although the bio has no CGROUP nor crypto context, it's still better to explicitly pair bio_init() with bio_uninit().
Thanks,
Qu
PATCH v2 2/12 btrfs: introduce a new helper to submit bio for scrub
In reply to Qu WenruoThe new helper, btrfs_submit_scrub_read(), would be mostly a subset of btrfs_submit_bio(), with the following limitations:
- Only supports read
- @mirror_num must be > 0
- No read-time repair nor checksum verification
- The @bbio must not cross stripe boundary
This would provide the basis for unified read repair for scrub, as we no longer needs to handle RAID56 recovery all by scrub, and RAID56 data stripes scrub can share the same code of read and repair.
The repair part would be the same as non-RAID56, as we only need to try the next mirror.
--- fs/btrfs/bio.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- fs/btrfs/bio.h | 19 +++++++++++++++++-- 2 files changed, 63 insertions(+), 6 deletions(-)
fs/btrfs/bio.c +46 -4
@@ -305,8 +305,8 @@ static void btrfs_end_bio_work(struct work_struct *work) { struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, end_io_work); -/* Metadata reads are checked and repaired by the submitter. */ -if (bbio->bio.bi_opf &REQ_META ) +/* Metadata or scrub reads are checked and repaired by the submitter. */ +if (bbio->bio.bi_opf &REQ_META || !bbio->inode) bbio->end_io (bbio);else btrfs_check_read_bio (bbio, bbio->bio.bi_private);
@@ -316,7 +316,8 @@ static void btrfs_simple_end_io(struct bio *bio) {struct btrfs_bio *bbio =btrfs_bio (bio);struct btrfs_device *dev = bio->bi_private; -struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info; +struct btrfs_fs_info *fs_info = bbio->inode ? + bbio->inode->root->fs_info : bbio->fs_info;btrfs_bio_counter_dec (fs_info);
@@ -340,7 +341,8 @@ static void btrfs_raid56_end_io(struct bio *bio)btrfs_bio_counter_dec (bioc->fs_info); bbio->mirror_num = bioc->mirror_num; -if (bio_op (bio) ==REQ_OP_READ && !(bbio->bio.bi_opf &REQ_META )) +if (bio_op (bio) ==REQ_OP_READ && bbio->inode && + !(bbio->bio.bi_opf &REQ_META ))btrfs_check_read_bio (bbio,NULL );else btrfs_orig_bbio_end_io (bbio);
@@ -686,6 +688,46 @@ static bool btrfs_submit_chunk(struct bio *bio, int mirror_num)return true; } +/* +* Scrub read special version, with extra limits: +* +* - Only support read for scrub usage +* - @mirror_num must be >0 +* - No read-time repair nor checksum verification. +* - The @bbio must not cross stripe boundary. +*/ +void btrfs_submit_scrub_read (struct btrfs_fs_info *fs_info, +struct btrfs_bio *bbio,int mirror_num) +{ +struct btrfs_bio *orig_bbio = bbio; +u64 logical = bbio->bio.bi_iter.bi_sector <<SECTOR_SHIFT ; +u64 length = bbio->bio.bi_iter.bi_size; +u64 map_length = length; +struct btrfs_io_context *bioc =NULL ; +struct btrfs_io_stripe smap; +int ret; + +ASSERT (mirror_num >0 ); +ASSERT (btrfs_op (&bbio->bio) ==BTRFS_MAP_READ ); +ASSERT (!bbio->inode); + + bbio->fs_info = fs_info; +btrfs_bio_counter_inc_blocked (fs_info); + ret =__btrfs_map_block (fs_info,btrfs_op (&bbio->bio), logical, + &map_length, &bioc, &smap, &mirror_num,1 ); +if (ret) + goto fail; + +/* Caller should ensure the @bbio doesn't cross stripe boundary. */ +ASSERT (map_length >= length); +__btrfs_submit_bio (&bbio->bio, bioc, &smap, mirror_num); +return ; + +fail: +btrfs_bio_counter_dec (fs_info); +btrfs_bio_end_io (orig_bbio, ret); +} +void btrfs_submit_bio (struct bio *bio,int mirror_num) {while (!btrfs_submit_chunk (bio, mirror_num))
fs/btrfs/bio.h +17 -2
@@ -30,9 +30,22 @@ typedef void (*btrfs_bio_end_io_t)(struct btrfs_bio *bbio);* passed to btrfs_submit_bio for mapping to the physical devices. */ struct btrfs_bio { -/* Inode and offset into it that this I/O operates on. */ +/* +* Inode and offset into it that this I/O operates on. +* +* @inode can be NULL for callers who don't want any advanced features +* like read-time repair. +* In that case, @fs_info must be properly initialized. +*/ struct btrfs_inode *inode; -u64 file_offset; + +union { +/* If @inode is initialized. */ +u64 file_offset; + +/* If @inode is NULL. */ +struct btrfs_fs_info *fs_info; + };union {/*
@@ -89,6 +102,8 @@ static inline void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)#define REQ_BTRFS_ONE_ORDERED REQ_DRVvoid btrfs_submit_bio (struct bio *bio,int mirror_num); +void btrfs_submit_scrub_read (struct btrfs_fs_info *fs_info, +struct btrfs_bio *bbio,int mirror_num);int btrfs_repair_io_failure (struct btrfs_fs_info *fs_info,u64 ino,u64 start,u64 length,u64 logical,struct page *page,unsigned int pg_offset,int mirror_num);
Signature · 1 line
2.39.2
Re: [PATCH v2 02/12] btrfs: introduce a new helper to submit bio for scrub
Quoted 4 lines
fs/btrfs/bio.h +4
-0+* @inode can be NULL for callers who don't want any advanced features +* like read-time repair. +* In that case, @fs_info must be properly initialized. +*/
fs/btrfs/bio.h typedef void (*btrfs_bio_end_io_t)(struct btrfs_bio *bbio); · line 48
Quoted 10 lines
fs/btrfs/bio.h +8
-1struct btrfs_inode *inode; -u64 file_offset; + +union { +/* If @inode is initialized. */ +u64 file_offset; + +/* If @inode is NULL. */ +struct btrfs_fs_info *fs_info; + };
Ugg. Let's not go into too crazy unions here that make the code alsmost impossible to use correctly. If we want to get away without the inode we just need an extra unconditional fs_info field, which is what most users of ->inode actually use. I can take care of that.
Re: [PATCH v2 02/12] btrfs: introduce a new helper to submit bio for scrub
Quoted 11 lines · Christoph Hellwig
Quoted 11 lines
fs/btrfs/bio.h +9
-1+* @inode can be NULL for callers who don't want any advanced features +* like read-time repair. +* In that case, @fs_info must be properly initialized. +*/ struct btrfs_inode *inode; -u64 file_offset; + +union { +/* If @inode is initialized. */ +u64 file_offset; +
Quoted 8 lines
fs/btrfs/bio.h typedef void (*btrfs_bio_end_io_t)(struct btrfs_bio *bbio); · line 48
Quoted 3 lines
fs/btrfs/bio.h +3
-0+/* If @inode is NULL. */ +struct btrfs_fs_info *fs_info; + };Ugg. Let's not go into too crazy unions here that make the code alsmost impossible to use correctly. If we want to get away without the inode we just need an extra unconditional
fs_infofield, which is what most users of ->inode actually use. I can take care of that.
If the memory usage is not a big deal, then I'm totally fine with a dedicated fs_info member.
Thanks,
Qu
Re: [PATCH v2 02/12] btrfs: introduce a new helper to submit bio for scrub
Quoted 7 lines · Qu Wenruo
Quoted 4 lines
Ugg. Let's not go into too crazy unions here that make the code alsmost impossible to use correctly. If we want to get away without the inode we just need an extra unconditional
fs_infofield, which is what most users of ->inode actually use. I can take care of that.If the memory usage is not a big deal, then I'm totally fine with a dedicated
fs_infomember.
According to pahole we still have a fair amount of space in the btrfs_bio before increasing the allocation. While I don't want to use it lightly, this seems like a good use case.
With a little more work I might be able to move the inode into a union leg only used for data writes as well.
Re: [PATCH v2 02/12] btrfs: introduce a new helper to submit bio for scrub
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Qu Wenruo
Quoted 4 lines
Ugg. Let's not go into too crazy unions here that make the code alsmost impossible to use correctly. If we want to get away without the inode we just need an extra unconditional
fs_infofield, which is what most users of ->inode actually use. I can take care of that.
Quoted 6 lines
Quoted 2 lines
If the memory usage is not a big deal, then I'm totally fine with a dedicated
fs_infomember.According to pahole we still have a fair amount of space in the
btrfs_biobefore increasing the allocation. While I don't want to use it lightly, this seems like a good use case.
Sounds pretty reasonable.
So that's would be the way to go.
Thanks,
Qu
Quoted 3 lines
With a little more work I might be able to move the inode into a union leg only used for data writes as well.
PATCH v2 3/12 btrfs: introduce a new helper to submit write bio for scrub
In reply to Qu WenruoJust like the special scrub read, scrub write also has its extra niches:
- Only write back to single device
Even for read-repair on RAID56, we only update the corrupted data stripe itself, not triggering the full RMW path.
This makes scrub writeback a perfect match for the single stripe quick path.
- Requires a valid @mirror_num
For RAID56 case, only @mirror_num == 1 is supported. For non-RAID56 cases, we need @mirror_num to locate our stripe.
- Need to manually specify if it's for dev-replace
For scrub path we can write back to the original device (for read-repair) and to the target device (for replace) at the same time, but with different sectors (read-repair only writes repaired sectors, while dev-replace writes all good sectors).
So here we need a bool to specify the case.
- No data csum generation
--- fs/btrfs/bio.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/bio.h | 3 ++ 2 files changed, 95 insertions(+)
fs/btrfs/bio.c +92 -0
@@ -728,6 +728,98 @@ void btrfs_submit_scrub_read(struct btrfs_fs_info *fs_info,btrfs_bio_end_io (orig_bbio, ret); } +/* +* Scrub write special version. Some extra limits: +* +* - Only support write back for dev-replace and read-repair. +* This means, the write bio, even for RAID56, would only +* be mapped to single device. +* +* - @mirror_num must be >0. +* To indicate which mirror to be written. +* If it's RAID56, it must be 1 (data stripes). +* +* - The @bbio must not cross stripe boundary. +* +* - If @dev_replace is true, the resulted stripe must be mapped to +* replace source device. +* +* - No csum geneartion. +*/ +void btrfs_submit_scrub_write (struct btrfs_fs_info *fs_info, +struct btrfs_bio *bbio,int mirror_num, +bool dev_replace) +{ +struct btrfs_bio *orig_bbio = bbio; +u64 logical = bbio->bio.bi_iter.bi_sector <<SECTOR_SHIFT ; +u64 length = bbio->bio.bi_iter.bi_size; +u64 map_length = length; +struct btrfs_io_context *bioc =NULL ; +struct btrfs_io_stripe smap; +int ret; + +ASSERT (mirror_num >0 ); +ASSERT (btrfs_op (&bbio->bio) ==BTRFS_MAP_WRITE ); +ASSERT (!bbio->inode); + + bbio->fs_info = fs_info; +btrfs_bio_counter_inc_blocked (fs_info); + ret =__btrfs_map_block (fs_info,btrfs_op (&bbio->bio), logical, + &map_length, &bioc, &smap, &mirror_num,1 ); +if (ret) + goto fail; + +/* Caller should ensure the @bbio doesn't cross stripe boundary. */ +ASSERT (map_length >= length); +if (btrfs_op (&bbio->bio) ==BTRFS_MAP_WRITE &&btrfs_is_zoned (fs_info)) { + bbio->bio.bi_opf &= ~REQ_OP_WRITE ; + bbio->bio.bi_opf |=REQ_OP_ZONE_APPEND ; + } + +if (!bioc) + goto submit; +/* Map the RAID56 multi-stripe writes to a single one. */ +if (bioc->map_type &BTRFS_BLOCK_GROUP_RAID56_MASK ) { +int data_stripes = bioc->map_type &BTRFS_BLOCK_GROUP_RAID5 ? + bioc->num_stripes -1 : bioc->num_stripes -2 ; +int i; + +/* This special write only works for data stripes. */ +ASSERT (mirror_num ==1 ); +for (i =0 ; i < data_stripes; i++) { +u64 stripe_start = bioc->full_stripe_logical + + (i <<BTRFS_STRIPE_LEN_SHIFT ); + +if (logical >= stripe_start && + logical < stripe_start +BTRFS_STRIPE_LEN ) +break ; + } +ASSERT (i < data_stripes); + smap.dev = bioc->stripes[i].dev; + smap.physical = bioc->stripes[i].physical + + ((logical - bioc->full_stripe_logical) & +BTRFS_STRIPE_LEN_MASK ); + goto submit; + } +ASSERT (mirror_num <= bioc->num_stripes); + smap.dev = bioc->stripes[mirror_num -1 ].dev; + smap.physical = bioc->stripes[mirror_num -1 ].physical; +submit: +ASSERT (smap.dev); +btrfs_put_bioc (bioc); + bioc =NULL ; +if (dev_replace) { +ASSERT (smap.dev == fs_info->dev_replace.srcdev); + smap.dev = fs_info->dev_replace.tgtdev; + } +__btrfs_submit_bio (&bbio->bio, bioc, &smap, mirror_num); +return ; + +fail: +btrfs_bio_counter_dec (fs_info); +btrfs_bio_end_io (orig_bbio, ret); +} +void btrfs_submit_bio (struct bio *bio,int mirror_num) {while (!btrfs_submit_chunk (bio, mirror_num))
fs/btrfs/bio.h +3 -0
@@ -104,6 +104,9 @@ static inline void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)void btrfs_submit_bio (struct bio *bio,int mirror_num);void btrfs_submit_scrub_read (struct btrfs_fs_info *fs_info,struct btrfs_bio *bbio,int mirror_num); +void btrfs_submit_scrub_write (struct btrfs_fs_info *fs_info, +struct btrfs_bio *bbio,int mirror_num, +bool dev_replace);int btrfs_repair_io_failure (struct btrfs_fs_info *fs_info,u64 ino,u64 start,u64 length,u64 logical,struct page *page,unsigned int pg_offset,int mirror_num);
Signature · 1 line
2.39.2
PATCH v2 4/12 btrfs: scrub: introduce the structure for new BTRFS_STRIPE_LEN based interface
In reply to Qu WenruoThis patch introduces the following structures:
- scrub_sector_verification
Contains all the needed info to verify one sector (data or metadata).
- scrub_stripe
Contains all needed members (mostly bitmap based) to scrub one stripe (with a length of BTRFS_STRIPE_LEN).
The basic idea is, we keep the existing per-device scrub behavior, but merge all the scrub_bio/scrub_bio into one generic structure, and read the full BTRFS_STRIPE_LEN stripe in the first try.
This means we will read some sectors which is not scrub target, but that's fine. At dev-replace time we only writeback the utilized and good sectors, and for read-repair we only writeback the repaired sectors.
With every read submitted in BTRFS_STRIPE_LEN, the need for complex bio formshaping would be gone.
Although to get the same performance of the old scrub behavior, we would need to submit the initial read for two stripes at once.
--- fs/btrfs/scrub.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/scrub.h | 8 +++ 2 files changed, 148 insertions(+)
fs/btrfs/scrub.c +140 -0
@@ -70,6 +70,88 @@ struct scrub_ctx;*/ #define BTRFS_MAX_MIRRORS (4 + 1) +/* Represent one sector and its needed info to verify the content. */ +struct scrub_sector_verification { +bool is_metadata; + +union { +/* +* Csum pointer for data csum verification. +* Should point to a sector csum inside scrub_stripe::csums. +* +* NULL if this data sector has no csum. +*/ +u8 *csum; + +/* +* Extra info for metadata verification. +* All sectors inside a tree block shares the same +* geneartion. +*/ +u64 generation; + }; +}; + +/* Set when @mirror_num, @dev, @physical and @logical is set. */ +#define SCRUB_STRIPE_FLAG_INITIALIZED (0) + +/* Set when the read-repair is finished. */ +#define SCRUB_STRIPE_FLAG_REPAIR_DONE (1) + +/* +* Represent one continuous range with a length of BTRFS_STRIPE_LEN. +*/ +struct scrub_stripe { +struct btrfs_block_group *bg; + +struct page *pages[BTRFS_STRIPE_LEN /PAGE_SIZE ]; +struct scrub_sector_verification *sectors; + +struct btrfs_device *dev; +u64 logical; +u64 physical; + +u16 mirror_num; + +/* Should be BTRFS_STRIPE_LEN / sectorsize. */ +u16 nr_sectors; + +atomic_t pending_io; +wait_queue_head_t io_wait; + +/* Indicates the states of the stripe. */ +unsigned long state; + +/* Indicates which sectors are covered by extent items. */ +unsigned long extent_sector_bitmap; + +/* +* The errors hit during the initial read of the stripe. +* +* Would be utilized for error reporting and repair. +*/ +unsigned long init_error_bitmap; + +/* +* The following error bitmaps are all for the current status. +* Every time we submit a new read, those bitmaps may be updated. +* +* error_bitmap = io_error_bitmap | csum_error_bitmap | meta_error_bitmap; +* +* IO and csum errors can happen for both metadata and data. +*/ +unsigned long error_bitmap; +unsigned long io_error_bitmap; +unsigned long csum_error_bitmap; +unsigned long meta_error_bitmap; + +/* +* Checksum for the whole stripe if this stripe is inside a data block +* group. +*/ +u8 *csums; +}; +struct scrub_recover {refcount_t refs;struct btrfs_io_context *bioc;
@@ -266,6 +348,64 @@ static void detach_scrub_page_private(struct page *page)#endif } +static void release_scrub_stripe (struct scrub_stripe *stripe) +{ +int i; + +if (!stripe) +return ; + +for (i =0 ; i <BTRFS_STRIPE_LEN >>PAGE_SHIFT ; i++) { +if (stripe->pages[i]) +__free_page (stripe->pages[i]); + stripe->pages[i] =NULL ; + } +kfree (stripe->sectors); +kfree (stripe->csums); + stripe->sectors =NULL ; + stripe->csums =NULL ; + stripe->state =0 ; +} + +int init_scrub_stripe (struct btrfs_fs_info *fs_info,struct scrub_stripe *stripe) +{ +int ret; + +memset (stripe,0 ,sizeof (*stripe)); + + stripe->nr_sectors =BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits; + stripe->state =0 ; + +init_waitqueue_head (&stripe->io_wait); +atomic_set (&stripe->pending_io,0 ); + + + ret =btrfs_alloc_page_array (BTRFS_STRIPE_LEN >>PAGE_SHIFT , + stripe->pages); +if (ret <0 ) + goto error; + + stripe->sectors =kcalloc (stripe->nr_sectors, +sizeof (struct scrub_sector_verification ), +GFP_KERNEL ); +if (!stripe->sectors) + goto error; + + stripe->csums =kzalloc ((BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits) * + fs_info->csum_size,GFP_KERNEL ); +if (!stripe->csums) + goto error; +return 0 ; +error: +release_scrub_stripe (stripe); +return -ENOMEM ; +} + +void wait_scrub_stripe_io (struct scrub_stripe *stripe) +{ +wait_event (stripe->io_wait,atomic_read (&stripe->pending_io) ==0 ); +} + static struct scrub_block *alloc_scrub_block(struct scrub_ctx *sctx, struct btrfs_device *dev, u64 logical, u64 physical,
fs/btrfs/scrub.h +8 -0
@@ -13,4 +13,12 @@ int btrfs_scrub_cancel_dev(struct btrfs_device *dev);int btrfs_scrub_progress (struct btrfs_fs_info *fs_info,u64 devid,struct btrfs_scrub_progress *progress); +/* +* The following functions are temporary exports to avoid warning on unused +* static functions. +*/ +struct scrub_stripe ; +int init_scrub_stripe (struct btrfs_fs_info *fs_info,struct scrub_stripe *stripe); +void wait_scrub_stripe_io (struct scrub_stripe *stripe); +#endif
Signature · 1 line
2.39.2
PATCH v2 5/12 btrfs: scrub: introduce a helper to find and fill the sector info for a scrub_stripe
In reply to Qu WenruoThe new helper will search the extent tree to find the first extent of a logical range, then fill the sectors array by two loops:
- Loop 1 to fill common bits and metadata generation
- Loop 2 to fill csum data (only for data bgs)
This loop will use the new btrfs_lookup_csums_bitmap() to fill the full csum buffer, and set scrub_sector_verification::csum.
With all the needed info fulfilled by this function, later we only need to submit and verify the stripe.
Here we temporarily export the helper to avoid wanring on unused static function.
--- fs/btrfs/file-item.c | 9 ++- fs/btrfs/file-item.h | 3 +- fs/btrfs/raid56.c | 2 +- fs/btrfs/scrub.c | 148 +++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/scrub.h | 4 ++ 5 files changed, 163 insertions(+), 3 deletions(-)
fs/btrfs/file-item.c +8 -1
@@ -597,7 +597,8 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end,* in is large enough to contain all csums. */ int btrfs_lookup_csums_bitmap (struct btrfs_root *root,u64 start,u64 end, -u8 *csum_buf,unsigned long *csum_bitmap) +u8 *csum_buf,unsigned long *csum_bitmap, +bool search_commit) {struct btrfs_fs_info *fs_info = root->fs_info;struct btrfs_key key;
@@ -614,6 +615,12 @@ int btrfs_lookup_csums_bitmap(struct btrfs_root *root, u64 start, u64 end,if (!path)return -ENOMEM ; +if (search_commit) { + path->skip_locking =1 ; + path->reada =READA_FORWARD ; + path->search_commit_root =1 ; + } + key.objectid =BTRFS_EXTENT_CSUM_OBJECTID ; key.type =BTRFS_EXTENT_CSUM_KEY ; key.offset = start;
fs/btrfs/file-item.h +2 -1
@@ -57,7 +57,8 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, struct list_head *list, int search_commit, bool nowait);int btrfs_lookup_csums_bitmap (struct btrfs_root *root,u64 start,u64 end, -u8 *csum_buf,unsigned long *csum_bitmap); +u8 *csum_buf,unsigned long *csum_bitmap, +bool search_commit); void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, const struct btrfs_path *path, struct btrfs_file_extent_item *fi,
fs/btrfs/raid56.c +1 -1
@@ -2113,7 +2113,7 @@ static void fill_data_csums(struct btrfs_raid_bio *rbio) } ret =btrfs_lookup_csums_bitmap (csum_root, start, start + len -1 , - rbio->csum_buf, rbio->csum_bitmap); + rbio->csum_buf, rbio->csum_bitmap, false);if (ret <0 ) goto error;if (bitmap_empty (rbio->csum_bitmap, len >> fs_info->sectorsize_bits))
fs/btrfs/scrub.c +148 -0
154 lines not on this page · raw message
fs/btrfs/scrub.h +4 -0
@@ -20,5 +20,9 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,struct scrub_stripe ;int init_scrub_stripe (struct btrfs_fs_info *fs_info,struct scrub_stripe *stripe);void wait_scrub_stripe_io (struct scrub_stripe *stripe); +int scrub_find_fill_first_stripe (struct btrfs_block_group *bg, +struct btrfs_device *dev,u64 physical, +int mirror_num,u64 logical_start, +u32 logical_len,struct scrub_stripe *stripe);#endif
Signature · 1 line
2.39.2
PATCH v2 6/12 btrfs: scrub: introduce a helper to verify one metadata
In reply to Qu WenruoThe new helper, scrub_verify_one_metadata(), is almost the same as scrub_checksum_tree_block().
The difference is in how we grab the pages.
--- fs/btrfs/scrub.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/scrub.h | 1 + 2 files changed, 117 insertions(+)
fs/btrfs/scrub.c +116 -0
@@ -2157,6 +2157,122 @@ static int scrub_checksum_data(struct scrub_block *sblock)return sblock->checksum_error; } +static struct page *scrub_stripe_get_page (struct scrub_stripe *stripe, +int sector_nr) +{ +struct btrfs_fs_info *fs_info = stripe->bg->fs_info; +int page_index = sector_nr << fs_info->sectorsize_bits >>PAGE_SHIFT ; + +return stripe->pages[page_index]; +} + +static unsigned int scrub_stripe_get_page_offset (struct scrub_stripe *stripe, +int sector_nr) +{ +struct btrfs_fs_info *fs_info = stripe->bg->fs_info; + +return offset_in_page (sector_nr << fs_info->sectorsize_bits); +} + +void scrub_verify_one_metadata (struct scrub_stripe *stripe,int sector_nr) +{ +struct btrfs_fs_info *fs_info = stripe->bg->fs_info; +const unsigned int sectors_per_tree = fs_info->nodesize >> + fs_info->sectorsize_bits; +const u64 logical = stripe->logical + (sector_nr << fs_info->sectorsize_bits); +const struct page *first_page =scrub_stripe_get_page (stripe, sector_nr); +const unsigned int first_off =scrub_stripe_get_page_offset (stripe, sector_nr); +SHASH_DESC_ON_STACK (shash, fs_info->csum_shash); +u8 on_disk_csum[BTRFS_CSUM_SIZE ]; +u8 calculated_csum[BTRFS_CSUM_SIZE ]; +struct btrfs_header *h; +int i; + +/* +* Here we don't have a good way to attach the pages (and subpages) +* to a dummy extent buffer, thus we have to directly grab the members +* from pages. +*/ + h = (struct btrfs_header *)(page_address (first_page) + first_off); +memcpy (on_disk_csum, h->csum, fs_info->csum_size); + +if (logical !=btrfs_stack_header_bytenr (h)) { +bitmap_set (&stripe->csum_error_bitmap, sector_nr, + sectors_per_tree); +bitmap_set (&stripe->error_bitmap, sector_nr, + sectors_per_tree); +btrfs_warn_rl (fs_info, +"tree block %llu mirror %u has bad bytenr, has %llu want %llu" , + logical, stripe->mirror_num, +btrfs_stack_header_bytenr (h), logical); +return ; + } +if (memcmp (h->fsid, fs_info->fs_devices->fsid,BTRFS_FSID_SIZE )) { +bitmap_set (&stripe->meta_error_bitmap, sector_nr, + sectors_per_tree); +bitmap_set (&stripe->error_bitmap, sector_nr, + sectors_per_tree); +btrfs_warn_rl (fs_info, +"tree block %llu mirror %u has bad fsid, has %pU want %pU" , + logical, stripe->mirror_num, + h->fsid, fs_info->fs_devices->fsid); +return ; + } +if (memcmp (h->chunk_tree_uuid, fs_info->chunk_tree_uuid, +BTRFS_UUID_SIZE )) { +bitmap_set (&stripe->meta_error_bitmap, sector_nr, + sectors_per_tree); +bitmap_set (&stripe->error_bitmap, sector_nr, + sectors_per_tree); +btrfs_warn_rl (fs_info, +"tree block %llu mirror %u has bad chunk tree uuid, has %pU want %pU" , + logical, stripe->mirror_num, + h->chunk_tree_uuid, fs_info->chunk_tree_uuid); +return ; + } + +/* Now check tree block csum. */ + shash->tfm = fs_info->csum_shash; +crypto_shash_init (shash); +crypto_shash_update (shash,page_address (first_page) + first_off + +BTRFS_CSUM_SIZE , fs_info->sectorsize -BTRFS_CSUM_SIZE ); + +for (i = sector_nr +1 ; i < sector_nr + sectors_per_tree; i++) { +struct page *page =scrub_stripe_get_page (stripe, i); +unsigned int page_off =scrub_stripe_get_page_offset (stripe, i); + +crypto_shash_update (shash,page_address (page) + page_off, + fs_info->sectorsize); + } +crypto_shash_final (shash, calculated_csum); +if (memcmp (calculated_csum, on_disk_csum, fs_info->csum_size)) { +bitmap_set (&stripe->meta_error_bitmap, sector_nr, + sectors_per_tree); +bitmap_set (&stripe->error_bitmap, sector_nr, + sectors_per_tree); +btrfs_warn_rl (fs_info, +"tree block %llu mirror %u has bad csum, has " CSUM_FMT " want " CSUM_FMT , + logical, stripe->mirror_num, +CSUM_FMT_VALUE (fs_info->csum_size, on_disk_csum), +CSUM_FMT_VALUE (fs_info->csum_size, calculated_csum)); +return ; + } +if (stripe->sectors[sector_nr].generation !=btrfs_stack_header_generation (h)) { +bitmap_set (&stripe->meta_error_bitmap, sector_nr, + sectors_per_tree); +bitmap_set (&stripe->error_bitmap, sector_nr, + sectors_per_tree); +btrfs_warn_rl (fs_info, +"tree block %llu mirror %u has bad geneartion, has %llu want %llu" , + logical, stripe->mirror_num, +btrfs_stack_header_generation (h), + stripe->sectors[sector_nr].generation); + } +bitmap_clear (&stripe->error_bitmap, sector_nr, sectors_per_tree); +bitmap_clear (&stripe->csum_error_bitmap, sector_nr, sectors_per_tree); +bitmap_clear (&stripe->meta_error_bitmap, sector_nr, sectors_per_tree); +} +static int scrub_checksum_tree_block (struct scrub_block *sblock) {struct scrub_ctx *sctx = sblock->sctx;
fs/btrfs/scrub.h +1 -0
@@ -24,5 +24,6 @@ int scrub_find_fill_first_stripe(struct btrfs_block_group *bg, struct btrfs_device *dev, u64 physical, int mirror_num, u64 logical_start, u32 logical_len, struct scrub_stripe *stripe); +void scrub_verify_one_metadata (struct scrub_stripe *stripe,int sector_nr);#endif
Signature · 1 line
2.39.2
PATCH v2 7/12 btrfs: scrub: introduce a helper to verify one scrub_stripe
In reply to Qu WenruoThe new helper, scrub_verify_stripe(), shares the same main workflow of the old scrub code.
The major differences are:
- How pages/page_offset is grabbed
Everything can be grabbed from scrub_stripe easily.
- When error report happens
Currently the helper only verify the sectors, not really doing any error reporting.
The error reporting would be done after we have done the repair.
--- fs/btrfs/scrub.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++- fs/btrfs/scrub.h | 2 +- 2 files changed, 80 insertions(+), 2 deletions(-)
fs/btrfs/scrub.c +79 -1
@@ -2174,7 +2174,7 @@ static unsigned int scrub_stripe_get_page_offset(struct scrub_stripe *stripe,return offset_in_page (sector_nr << fs_info->sectorsize_bits); } -void scrub_verify_one_metadata (struct scrub_stripe *stripe,int sector_nr) +static void scrub_verify_one_metadata (struct scrub_stripe *stripe,int sector_nr) {struct btrfs_fs_info *fs_info = stripe->bg->fs_info; const unsigned int sectors_per_tree = fs_info->nodesize >>
@@ -2273,6 +2273,84 @@ void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nr)bitmap_clear (&stripe->meta_error_bitmap, sector_nr, sectors_per_tree); } +static void scrub_verify_one_sector (struct scrub_stripe *stripe, +int sector_nr) +{ +struct btrfs_fs_info *fs_info = stripe->bg->fs_info; +struct scrub_sector_verification *sector = &stripe->sectors[sector_nr]; +const unsigned int sectors_per_tree = fs_info->nodesize >> + fs_info->sectorsize_bits; +struct page *page =scrub_stripe_get_page (stripe, sector_nr); +unsigned int pgoff =scrub_stripe_get_page_offset (stripe, sector_nr); +u8 csum_buf[BTRFS_CSUM_SIZE ]; +int ret; + +ASSERT (sector_nr >=0 && sector_nr < stripe->nr_sectors); + +/* Sector not utilized, skip it. */ +if (!test_bit (sector_nr, &stripe->extent_sector_bitmap)) +return ; + +/* IO error, no need to check. */ +if (test_bit (sector_nr, &stripe->io_error_bitmap)) +return ; + +/* Metadata, verify the full tree block. */ +if (sector->is_metadata) { +/* +* Check if the tree block crosses the stripe boudary. +* If crossed the boundary, we can not verify it but only +* gives a warning. +* +* This can only happen in very old fs where chunks are not +* ensured to be stripe aligned. +*/ +if (unlikely (sector_nr + sectors_per_tree > stripe->nr_sectors)) { +btrfs_warn_rl (fs_info, +"tree block at %llu crosses stripe boundary %llu" , + stripe->logical + + (sector_nr << fs_info->sectorsize_bits), + stripe->logical); +return ; + } +scrub_verify_one_metadata (stripe, sector_nr); +return ; + } + +/* +* Data is much easier, we just verify the data csum (if we have). +* For cases without csum, we have no other choice but to trust it. +*/ +if (!sector->csum) { +clear_bit (sector_nr, &stripe->error_bitmap); +return ; + } + + ret =btrfs_check_sector_csum (fs_info, page, pgoff, csum_buf, sector->csum); +if (ret <0 ) { +set_bit (sector_nr, &stripe->csum_error_bitmap); +set_bit (sector_nr, &stripe->error_bitmap); + }else { +clear_bit (sector_nr, &stripe->csum_error_bitmap); +clear_bit (sector_nr, &stripe->error_bitmap); + } +} + +/* Verify specified sectors of a stripe. */ +void scrub_verify_one_stripe (struct scrub_stripe *stripe,unsigned long bitmap) +{ +struct btrfs_fs_info *fs_info = stripe->bg->fs_info; +const unsigned int sectors_per_tree = fs_info->nodesize >> + fs_info->sectorsize_bits; +int sector_nr; + +for_each_set_bit (sector_nr, &bitmap, stripe->nr_sectors) { +scrub_verify_one_sector (stripe, sector_nr); +if (stripe->sectors[sector_nr].is_metadata) + sector_nr += sectors_per_tree -1 ; + } +} +static int scrub_checksum_tree_block (struct scrub_block *sblock) {struct scrub_ctx *sctx = sblock->sctx;
fs/btrfs/scrub.h +1 -1
@@ -24,6 +24,6 @@ int scrub_find_fill_first_stripe(struct btrfs_block_group *bg, struct btrfs_device *dev, u64 physical, int mirror_num, u64 logical_start, u32 logical_len, struct scrub_stripe *stripe); -void scrub_verify_one_metadata (struct scrub_stripe *stripe,int sector_nr); +void scrub_verify_one_stripe (struct scrub_stripe *stripe,unsigned long bitmap);#endif
Signature · 1 line
2.39.2
PATCH v2 8/12 btrfs: scrub: introduce the main read repair worker for scrub_stripe
In reply to Qu WenruoThe new helper, scrub_stripe_read_repair_worker(), would handle the read-repair part:
- Wait for the previous submitted read IO to finish
- Verify the contents of the stripe
- Go through the remaining mirrors, using as large blocksize as possible At this stage, we just read out all the failed sectors from each mirror and re-verify.
If no more failed sector, we can exit.
- Go through all mirrors again, sector-by-sector this time This time, we read sector by sector, this is to address cases where one bad sector mismatches the drive's internal checksum, and cause the whole read range to fail.
We put this recovery method as the last resort, as sector-by-sector reading is slow, and read from other mirrors may have already fixed the errors.
--- fs/btrfs/scrub.c | 213 ++++++++++++++++++++++++++++++++++++++++++++++- fs/btrfs/scrub.h | 3 +- 2 files changed, 211 insertions(+), 5 deletions(-)
fs/btrfs/scrub.c +210 -3
@@ -118,6 +118,7 @@ struct scrub_stripe {atomic_t pending_io;wait_queue_head_t io_wait; +wait_queue_head_t repair_wait;/* Indicates the states of the stripe. */ unsigned long state;
@@ -150,6 +151,8 @@ struct scrub_stripe {* group. */ u8 *csums; + +struct work_struct work; };struct scrub_recover {
@@ -377,9 +380,9 @@ int init_scrub_stripe(struct btrfs_fs_info *fs_info, struct scrub_stripe *stripe stripe->state =0 ;init_waitqueue_head (&stripe->io_wait); +init_waitqueue_head (&stripe->repair_wait);atomic_set (&stripe->pending_io,0 ); - ret =btrfs_alloc_page_array (BTRFS_STRIPE_LEN >>PAGE_SHIFT , stripe->pages);if (ret <0 )
@@ -401,7 +404,7 @@ int init_scrub_stripe(struct btrfs_fs_info *fs_info, struct scrub_stripe *stripereturn -ENOMEM ; } -void wait_scrub_stripe_io (struct scrub_stripe *stripe) +static void wait_scrub_stripe_io (struct scrub_stripe *stripe) {wait_event (stripe->io_wait,atomic_read (&stripe->pending_io) ==0 ); }
@@ -2337,7 +2340,8 @@ static void scrub_verify_one_sector(struct scrub_stripe *stripe, }/* Verify specified sectors of a stripe. */ -void scrub_verify_one_stripe (struct scrub_stripe *stripe,unsigned long bitmap) +static void scrub_verify_one_stripe (struct scrub_stripe *stripe, +unsigned long bitmap) {struct btrfs_fs_info *fs_info = stripe->bg->fs_info; const unsigned int sectors_per_tree = fs_info->nodesize >>
@@ -2351,6 +2355,209 @@ void scrub_verify_one_stripe(struct scrub_stripe *stripe, unsigned long bitmap) } } +static int calc_sector_number (struct scrub_stripe *stripe, +struct bio_vec *first_bvec) +{ +int i; + +for (i =0 ; i < stripe->nr_sectors; i++) { +if (scrub_stripe_get_page (stripe, i) == first_bvec->bv_page && +scrub_stripe_get_page_offset (stripe, i) == first_bvec->bv_offset) +break ; + } +ASSERT (i < stripe->nr_sectors); +return i; +} + +/* +* Repair read is different to the regular read by: +* +* - Only reads the failed sectors +* - May have extra blocksize limits +*/ +static void scrub_repair_read_endio (struct btrfs_bio *bbio) +{ +struct scrub_stripe *stripe = bbio->private; +struct btrfs_fs_info *fs_info = stripe->bg->fs_info; +struct bio_vec *bvec; +int sector_nr =calc_sector_number (stripe, +bio_first_bvec_all (&bbio->bio)); +int bio_size =0 ; +int i; + +ASSERT (sector_nr < stripe->nr_sectors); + +bio_for_each_bvec_all (bvec, &bbio->bio, i) + bio_size += bvec->bv_len; + +if (bbio->bio.bi_status) { +bitmap_set (&stripe->io_error_bitmap, sector_nr, + bio_size >> fs_info->sectorsize_bits); +bitmap_set (&stripe->error_bitmap, sector_nr, + bio_size >> fs_info->sectorsize_bits); + }else { +bitmap_clear (&stripe->io_error_bitmap, sector_nr, + bio_size >> fs_info->sectorsize_bits); + } +bio_put (&bbio->bio); +if (atomic_dec_and_test (&stripe->pending_io)) +wake_up (&stripe->io_wait); +} + +static int calc_next_mirror (int mirror,int num_copies) +{ +ASSERT (mirror <= num_copies); +return (mirror +1 > num_copies) ?1 : mirror +1 ; +} + +static void scrub_stripe_submit_repair_read (struct scrub_stripe *stripe, +int mirror,int blocksize, +bool wait) +{ +struct btrfs_fs_info *fs_info = stripe->bg->fs_info; +struct bio *bio =NULL ; +const unsigned long old_error_bitmap = stripe->error_bitmap; +int i; + +ASSERT (stripe->mirror_num >=1 ); +ASSERT (atomic_read (&stripe->pending_io) ==0 ); + +for_each_set_bit (i, &old_error_bitmap, stripe->nr_sectors) { +struct page *page; +int pgoff; +int ret; + + page =scrub_stripe_get_page (stripe, i); + pgoff =scrub_stripe_get_page_offset (stripe, i); + +/* The current sector can not be merged, submit the bio. */ +if (bio && ((i >0 && !test_bit (i -1 , &stripe->error_bitmap)) || + bio->bi_iter.bi_size >= blocksize)) { +ASSERT (bio->bi_iter.bi_size); +atomic_inc (&stripe->pending_io); +btrfs_submit_scrub_read (fs_info,btrfs_bio (bio), mirror); +if (wait) +wait_scrub_stripe_io (stripe); + bio =NULL ; + } + +if (!bio) { + bio =btrfs_bio_alloc (stripe->nr_sectors,REQ_OP_READ , +NULL , scrub_repair_read_endio, stripe); +/* Backed by mempool. */ +ASSERT (bio); + bio->bi_iter.bi_sector = (stripe->logical + + (i << fs_info->sectorsize_bits)) >>SECTOR_SHIFT ; + } + + ret =bio_add_page (bio, page, fs_info->sectorsize, pgoff); +ASSERT (ret == fs_info->sectorsize); + } +if (bio) { +ASSERT (bio->bi_iter.bi_size); +atomic_inc (&stripe->pending_io); +btrfs_submit_scrub_read (fs_info,btrfs_bio (bio), mirror); +if (wait) +wait_scrub_stripe_io (stripe); + } +} + +/* +* The main entrance for all read related scrub work, including: +* +* - Wait for the initial read to finish +* - Verify and locate any bad sectors +* - Go through the remaining mirrors and try to read as large blocksize as +* possible +* +* - Go through all mirrors (including the failed mirror) sector-by-sector +* +* Writeback does not happen here, they need extra synchronization. +*/ +static void scrub_stripe_read_repair_worker (struct work_struct *work) +{ + struct scrub_stripe *stripe = container_of(work, struct scrub_stripe, + work); +struct btrfs_fs_info *fs_info = stripe->bg->fs_info; +int num_copies =btrfs_num_copies (fs_info, stripe->bg->start, + stripe->bg->length); +int mirror; +int i; + +ASSERT (stripe->mirror_num >0 ); + +wait_scrub_stripe_io (stripe); +scrub_verify_one_stripe (stripe, stripe->extent_sector_bitmap); +/* Save the initial failed bitmap for later repair and report usage. */ + stripe->init_error_bitmap = stripe->error_bitmap; + +if (bitmap_empty (&stripe->init_error_bitmap, stripe->nr_sectors)) + goto out; + +/* +* Try all remaining mirrors. +* +* Here we still try read as large block as possible, as this is faster +* and we have extra safe nets to rely on. +*/ +for (mirror =calc_next_mirror (stripe->mirror_num, num_copies); + mirror != stripe->mirror_num; + mirror =calc_next_mirror (mirror, num_copies)) { +const unsigned long old_error_bitmap = stripe->error_bitmap; + +scrub_stripe_submit_repair_read (stripe, mirror, +BTRFS_STRIPE_LEN , false); +wait_scrub_stripe_io (stripe); +scrub_verify_one_stripe (stripe, old_error_bitmap); +if (bitmap_empty (&stripe->error_bitmap, stripe->nr_sectors)) + goto out; + } + +/* +* Last safenet, try re-check all mirrors, including the failed one, +* sector-by-sector. +* +* As if one sector failed the drive's internal csum, the whole read +* containing the offending sector would be marked error. +* Thus here we do sector-by-sector read. +* +* This can be slow, thus we only try it as the last resort. +*/ + +for (i =0 , mirror = stripe->mirror_num; i < num_copies; + i++, mirror =calc_next_mirror (mirror, num_copies)) { +const unsigned long old_error_bitmap = stripe->error_bitmap; + +scrub_stripe_submit_repair_read (stripe, mirror, + fs_info->sectorsize, true); +wait_scrub_stripe_io (stripe); +scrub_verify_one_stripe (stripe, old_error_bitmap); +if (bitmap_empty (&stripe->error_bitmap, stripe->nr_sectors)) + goto out; + } +out: +set_bit (SCRUB_STRIPE_FLAG_REPAIR_DONE , &stripe->state); +wake_up (&stripe->repair_wait); +} + +void scrub_read_endio (struct btrfs_bio *bbio) +{ +struct scrub_stripe *stripe = bbio->private; + +if (bbio->bio.bi_status) { +bitmap_set (&stripe->io_error_bitmap,0 , stripe->nr_sectors); +bitmap_set (&stripe->error_bitmap,0 , stripe->nr_sectors); + }else { +bitmap_clear (&stripe->io_error_bitmap,0 , stripe->nr_sectors); + } +bio_put (&bbio->bio); +if (atomic_dec_and_test (&stripe->pending_io)) { +wake_up (&stripe->io_wait); +INIT_WORK (&stripe->work, scrub_stripe_read_repair_worker); +queue_work (stripe->bg->fs_info->scrub_workers, &stripe->work); + } +} +static int scrub_checksum_tree_block (struct scrub_block *sblock) {struct scrub_ctx *sctx = sblock->sctx;
fs/btrfs/scrub.h +1 -2
@@ -19,11 +19,10 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,*/ struct scrub_stripe ;int init_scrub_stripe (struct btrfs_fs_info *fs_info,struct scrub_stripe *stripe); -void wait_scrub_stripe_io (struct scrub_stripe *stripe);int scrub_find_fill_first_stripe (struct btrfs_block_group *bg,struct btrfs_device *dev,u64 physical,int mirror_num,u64 logical_start,u32 logical_len,struct scrub_stripe *stripe); -void scrub_verify_one_stripe (struct scrub_stripe *stripe,unsigned long bitmap); +void scrub_read_endio (struct btrfs_bio *bbio);#endif
Signature · 1 line
2.39.2
PATCH v2 9/12 btrfs: scrub: introduce a writeback helper for scrub_stripe
In reply to Qu WenruoAdd a new helper, scrub_write_sectors(), to submit write bios for specified sectors to the target disk
There are several differences compared to read path:
- Utilize btrfs_submit_scrub_write()
Now we still rely on the @mirror_num based writeback, but the requirement is also a little different than regualar writeback or read, thus we have to call btrfs_submit_scrub_write().
- We can not write the full stripe back
We can only write the sectors we have.
There will be two call sites later, one for repaired sectors, one for all utilzied sectors of dev-replace.
Thus the callers should specify their own write_bitmap.
This function only submit the bios, will not wait for them unless for zoned case.
Caller must explicitly wait for the IO to finish.
--- fs/btrfs/scrub.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/scrub.h | 3 ++ 2 files changed, 100 insertions(+)
fs/btrfs/scrub.c +97 -0
@@ -146,6 +146,12 @@ struct scrub_stripe {unsigned long csum_error_bitmap;unsigned long meta_error_bitmap; +/* For writeback (repair or replace) error report. */ +unsigned long write_error_bitmap; + +/* Writeback can be concurrent, thus we need to protect the bitmap. */ +spinlock_t write_error_lock; +/* * Checksum for the whole stripe if this stripe is inside a data block * group.
@@ -382,6 +388,7 @@ int init_scrub_stripe(struct btrfs_fs_info *fs_info, struct scrub_stripe *stripeinit_waitqueue_head (&stripe->io_wait);init_waitqueue_head (&stripe->repair_wait);atomic_set (&stripe->pending_io,0 ); +spin_lock_init (&stripe->write_error_lock); ret =btrfs_alloc_page_array (BTRFS_STRIPE_LEN >>PAGE_SHIFT , stripe->pages);
@@ -2558,6 +2565,96 @@ void scrub_read_endio(struct btrfs_bio *bbio) } } +static void scrub_write_endio (struct btrfs_bio *bbio) +{ +struct scrub_stripe *stripe = bbio->private; +struct btrfs_fs_info *fs_info = stripe->bg->fs_info; +struct bio_vec *bvec; +unsigned long flags; +int sector_nr =calc_sector_number (stripe,bio_first_bvec_all (&bbio->bio)); +int bio_size =0 ; +int i; + +bio_for_each_bvec_all (bvec, &bbio->bio, i) + bio_size += bvec->bv_len; + +if (bbio->bio.bi_status) { +spin_lock_irqsave (&stripe->write_error_lock, flags); +bitmap_set (&stripe->write_error_bitmap, sector_nr, + bio_size >> fs_info->sectorsize_bits); +spin_unlock_irqrestore (&stripe->write_error_lock, flags); + } +bio_put (&bbio->bio); + +if (atomic_dec_and_test (&stripe->pending_io)) +wake_up (&stripe->io_wait); +} + +/* +* Submit the write bio(s) for the sectors specified by @write_bitmap. +* +* Here we utilize btrfs_submit_scrub_write(), which has some extra benefits: +* +* - Only needs logical bytenr and mirror_num +* Just like the scrub read path +* +* - Would only result writes to the specified mirror +* Unlike the regular writeback path, which would write back to all stripes +* +* - Handle dev-replace and read-repair writeback differently +*/ +void scrub_write_sectors (struct scrub_ctx *sctx, +struct scrub_stripe *stripe, +unsigned long write_bitmap,bool dev_replace) +{ +struct btrfs_fs_info *fs_info = stripe->bg->fs_info; +struct bio *bio =NULL ; +bool zoned =btrfs_is_zoned (fs_info); +int sector_nr; + +for_each_set_bit (sector_nr, &write_bitmap, stripe->nr_sectors) { +struct page *page =scrub_stripe_get_page (stripe, sector_nr); +unsigned int pgoff =scrub_stripe_get_page_offset (stripe, + sector_nr); +int ret; + +/* We should only writeback sectors covered by an extent. */ +ASSERT (test_bit (sector_nr, &stripe->extent_sector_bitmap)); + +/* Can not merge with previous sector, submit the current one. */ +if (bio && sector_nr && !test_bit (sector_nr -1 , &write_bitmap)) { +fill_writer_pointer_gap (sctx, stripe->physical + + (sector_nr << fs_info->sectorsize_bits)); +atomic_inc (&stripe->pending_io); +btrfs_submit_scrub_write (fs_info,btrfs_bio (bio), + stripe->mirror_num, dev_replace); +/* For zoned writeback, QD must be 1. */ +if (zoned) +wait_scrub_stripe_io (stripe); + bio =NULL ; + } +if (!bio) { + bio =btrfs_bio_alloc (BTRFS_STRIPE_LEN >>PAGE_SHIFT , +REQ_OP_WRITE ,NULL , scrub_write_endio, stripe); +ASSERT (bio); + + bio->bi_iter.bi_sector = (stripe->logical + + (sector_nr << fs_info->sectorsize_bits)) >> +SECTOR_SHIFT ; + } + ret =bio_add_page (bio, page, fs_info->sectorsize, pgoff); +ASSERT (ret == fs_info->sectorsize); + } +if (bio) { +fill_writer_pointer_gap (sctx, bio->bi_iter.bi_sector <<SECTOR_SHIFT ); +atomic_inc (&stripe->pending_io); +btrfs_submit_scrub_write (fs_info,btrfs_bio (bio), + stripe->mirror_num, dev_replace); +if (zoned) +wait_scrub_stripe_io (stripe); + } +} +static int scrub_checksum_tree_block (struct scrub_block *sblock) {struct scrub_ctx *sctx = sblock->sctx;
fs/btrfs/scrub.h +3 -0
@@ -24,5 +24,8 @@ int scrub_find_fill_first_stripe(struct btrfs_block_group *bg, int mirror_num, u64 logical_start, u32 logical_len, struct scrub_stripe *stripe);void scrub_read_endio (struct btrfs_bio *bbio); +void scrub_write_sectors (struct scrub_ctx *sctx, +struct scrub_stripe *stripe, +unsigned long write_bitmap,bool dev_replace);#endif
Signature · 1 line
2.39.2
PATCH v2 10/12 btrfs: scrub: introduce error reporting functionality for scrub_stripe
In reply to Qu WenruoThe new helper, scrub_stripe_report_errors(), will report the result of the scrub to dmesg.
The main reporting is done by introducing a new helper, scrub_print_common_warning(), which is mostly the same content from scrub_print_wanring(), but without the need for a scrub_block.
Since we're reporting the errors, it's the perfect timing to update the scrub stat too.
--- fs/btrfs/scrub.c | 167 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 158 insertions(+), 9 deletions(-)
fs/btrfs/scrub.c +158 -9
@@ -102,6 +102,7 @@ struct scrub_sector_verification {* Represent one continuous range with a length of BTRFS_STRIPE_LEN. */ struct scrub_stripe { +struct scrub_ctx *sctx;struct btrfs_block_group *bg;struct page *pages[BTRFS_STRIPE_LEN /PAGE_SIZE ];
@@ -116,6 +117,13 @@ struct scrub_stripe {/* Should be BTRFS_STRIPE_LEN / sectorsize. */ u16 nr_sectors; +/* +* How many data/meta extents are in this stripe. +* Only for scrub stat report purpose. +*/ +u16 nr_data_extents; +u16 nr_meta_extents; +atomic_t pending_io;wait_queue_head_t io_wait;wait_queue_head_t repair_wait;
@@ -373,6 +381,7 @@ static void release_scrub_stripe(struct scrub_stripe *stripe)kfree (stripe->csums); stripe->sectors =NULL ; stripe->csums =NULL ; + stripe->sctx =NULL ; stripe->state =0 ; }
@@ -1043,9 +1052,9 @@ static int scrub_print_warning_inode(u64 inum, u64 offset, u64 num_bytes,return 0 ; } -static void scrub_print_warning (const char *errstr,struct scrub_block *sblock) +static void scrub_print_common_warning (const char *errstr,struct btrfs_device *dev, +bool is_super,u64 logical,u64 physical) { -struct btrfs_device *dev;struct btrfs_fs_info *fs_info;struct btrfs_path *path;struct btrfs_key found_key;
@@ -1059,22 +1068,20 @@ static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)u8 ref_level =0 ;int ret; -WARN_ON (sblock->sector_count <1 ); - dev = sblock->dev; - fs_info = sblock->sctx->fs_info; + fs_info = dev->fs_info;/* Super block error, no need to search extent tree. */ -if (sblock->sectors[0 ]->flags &BTRFS_EXTENT_FLAG_SUPER ) { +if (is_super) {btrfs_warn_in_rcu (fs_info,"%s on device %s, physical %llu" , - errstr,btrfs_dev_name (dev), sblock->physical); + errstr,btrfs_dev_name (dev), physical);return ; } path =btrfs_alloc_path ();if (!path)return ; - swarn.physical = sblock->physical; - swarn.logical = sblock->logical; + swarn.physical = physical; + swarn.logical = logical; swarn.errstr = errstr; swarn.dev =NULL ;
@@ -1123,6 +1130,13 @@ static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)btrfs_free_path (path); } +static void scrub_print_warning (const char *errstr,struct scrub_block *sblock) +{ +scrub_print_common_warning (errstr, sblock->dev, + sblock->sectors[0 ]->flags &BTRFS_EXTENT_FLAG_SUPER , + sblock->logical, sblock->physical); +} +static inline void scrub_get_recover (struct scrub_recover *recover) {refcount_inc (&recover->refs);
@@ -2469,6 +2483,132 @@ static void scrub_stripe_submit_repair_read(struct scrub_stripe *stripe, } } +static void scrub_stripe_report_errors (struct scrub_ctx *sctx, +struct scrub_stripe *stripe) +{ + static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, + DEFAULT_RATELIMIT_BURST); +struct btrfs_fs_info *fs_info = sctx->fs_info; +struct btrfs_device *dev =NULL ; +u64 physical =0 ; +int nr_data_sectors =0 ; +int nr_meta_sectors =0 ; +int nr_nodatacsum_sectors =0 ; +int nr_repaired_sectors =0 ; +int sector_nr; + +/* +* Init needed infos for error reporting. +* +* Although our scrub_stripe infrastucture is mostly based on btrfs_submit_bio() +* thus no need for dev/physical, error reporting still needs dev and physical. +*/ +if (!bitmap_empty (&stripe->init_error_bitmap, stripe->nr_sectors)) { +u64 mapped_len = fs_info->sectorsize; +struct btrfs_io_context *bioc =NULL ; +int stripe_index = stripe->mirror_num -1 ; +int ret; + +/* For scrub, our mirror_num should always start at 1. */ +ASSERT (stripe->mirror_num >=1 ); + ret =btrfs_map_sblock (fs_info,BTRFS_MAP_GET_READ_MIRRORS , + stripe->logical, &mapped_len, &bioc); +/* +* If we failed, dev will be NULL, and later detailed reports +* will just be skipped. +*/ +if (ret <0 ) + goto skip; + physical = bioc->stripes[stripe_index].physical; + dev = bioc->stripes[stripe_index].dev; +btrfs_put_bioc (bioc); + } + +skip: +for_each_set_bit (sector_nr, &stripe->extent_sector_bitmap, + stripe->nr_sectors) { +bool repaired = false; + +if (stripe->sectors[sector_nr].is_metadata) { + nr_meta_sectors++; + }else { + nr_data_sectors++; +if (!stripe->sectors[sector_nr].csum) + nr_nodatacsum_sectors++; + } + +if (test_bit (sector_nr, &stripe->init_error_bitmap) && + !test_bit (sector_nr, &stripe->error_bitmap)) { + nr_repaired_sectors++; + repaired = true; + } + +/* Good sector from the beginning, nothing need to be done. */ +if (!test_bit (sector_nr, &stripe->init_error_bitmap)) +continue ; + +/* +* Report error for the corrupted sectors. +* If repaired, just output the message of repaired message. +*/ +if (repaired) { +if (dev) +btrfs_err_rl_in_rcu (fs_info, +"fixed up error at logical %llu on dev %s physical %llu" , + stripe->logical,btrfs_dev_name (dev), + physical); +else +btrfs_err_rl_in_rcu (fs_info, +"fixed up error at logical %llu on mirror %u" , + stripe->logical, stripe->mirror_num); +continue ; + } + +/* The remaining are all for unrepaired. */ +if (dev) +btrfs_err_rl_in_rcu (fs_info, +"unable to fixup (regular) error at logical %llu on dev %s physical %llu" , + stripe->logical,btrfs_dev_name (dev), + physical); +else +btrfs_err_rl_in_rcu (fs_info, +"unable to fixup (regular) error at logical %llu on mirror %u" , + stripe->logical, stripe->mirror_num); + +if (test_bit (sector_nr, &stripe->io_error_bitmap)) +if (__ratelimit (&rs) && dev) +scrub_print_common_warning ("i/o error" , dev, false, + stripe->logical, physical); +if (test_bit (sector_nr, &stripe->csum_error_bitmap)) +if (__ratelimit (&rs) && dev) +scrub_print_common_warning ("checksum error" , dev, false, + stripe->logical, physical); +if (test_bit (sector_nr, &stripe->meta_error_bitmap)) +if (__ratelimit (&rs) && dev) +scrub_print_common_warning ("header error" , dev, false, + stripe->logical, physical); + } + +spin_lock (&sctx->stat_lock); + sctx->stat.data_extents_scrubbed += stripe->nr_data_extents; + sctx->stat.tree_extents_scrubbed += stripe->nr_meta_extents; + sctx->stat.data_bytes_scrubbed += nr_data_sectors << + fs_info->sectorsize_bits; + sctx->stat.tree_bytes_scrubbed += nr_meta_sectors << + fs_info->sectorsize_bits; + sctx->stat.no_csum += nr_nodatacsum_sectors; + sctx->stat.read_errors += +bitmap_weight (&stripe->io_error_bitmap, stripe->nr_sectors); + sctx->stat.csum_errors += +bitmap_weight (&stripe->csum_error_bitmap, stripe->nr_sectors); + sctx->stat.verify_errors += +bitmap_weight (&stripe->meta_error_bitmap, stripe->nr_sectors); + sctx->stat.uncorrectable_errors += +bitmap_weight (&stripe->error_bitmap, stripe->nr_sectors); + sctx->stat.corrected_errors += nr_repaired_sectors; +spin_unlock (&sctx->stat_lock); +} +/* * The main entrance for all read related scrub work, including: *
@@ -2543,6 +2683,7 @@ static void scrub_stripe_read_repair_worker(struct work_struct *work) goto out; } out: +scrub_stripe_report_errors (stripe->sctx, stripe);set_bit (SCRUB_STRIPE_FLAG_REPAIR_DONE , &stripe->state);wake_up (&stripe->repair_wait); }
@@ -4213,6 +4354,10 @@ int scrub_find_fill_first_stripe(struct btrfs_block_group *bg, goto out;get_extent_info (&path, &extent_start, &extent_len, &extent_flags, &extent_gen); +if (extent_flags &BTRFS_EXTENT_FLAG_TREE_BLOCK ) + stripe->nr_meta_extents++; +if (extent_flags &BTRFS_EXTENT_FLAG_DATA ) + stripe->nr_data_extents++; cur_logical =max (extent_start, cur_logical);/*
@@ -4246,6 +4391,10 @@ int scrub_find_fill_first_stripe(struct btrfs_block_group *bg, }get_extent_info (&path, &extent_start, &extent_len, &extent_flags, &extent_gen); +if (extent_flags &BTRFS_EXTENT_FLAG_TREE_BLOCK ) + stripe->nr_meta_extents++; +if (extent_flags &BTRFS_EXTENT_FLAG_DATA ) + stripe->nr_data_extents++;fill_one_extent_info (fs_info, stripe, extent_start, extent_len, extent_flags, extent_gen); cur_logical = extent_start + extent_len;
Signature · 1 line
2.39.2
PATCH v2 11/12 btrfs: scrub: introduce the helper to queue a stripe for scrub
In reply to Qu WenruoThe new helper, queue_scrub_stripe(), would try to queue a stripe for scrub.
If all stripes are already in use, we will submit all the existing stripes and wait them to finish.
Currently we would queue up to 8 stripes, to enlarge the blocksize to 512KiB to improve the performance.
--- fs/btrfs/scrub.c | 181 ++++++++++++++++++++++++++++++++++++++++++++--- fs/btrfs/scrub.h | 13 ++-- 2 files changed, 176 insertions(+), 18 deletions(-)
fs/btrfs/scrub.c +172 -9
@@ -50,6 +50,7 @@ struct scrub_ctx;*/ #define SCRUB_SECTORS_PER_BIO 32/* 128KiB per bio for 4KiB pages */ #define SCRUB_BIOS_PER_SCTX 64/* 8MiB per device in flight for 4KiB pages */ +#define SCRUB_STRIPES_PER_SCTX 8/* That would be 8 64K stripe per-device. */ /* * The following value times PAGE_SIZE needs to be large enough to match the
@@ -271,9 +272,11 @@ struct scrub_parity {struct scrub_ctx {struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX ]; +struct scrub_stripe stripes[SCRUB_STRIPES_PER_SCTX ];struct btrfs_fs_info *fs_info;int first_free;int curr; +int cur_stripe;atomic_t bios_in_flight;atomic_t workers_pending;spinlock_t list_lock;
@@ -385,7 +388,8 @@ static void release_scrub_stripe(struct scrub_stripe *stripe) stripe->state =0 ; } -int init_scrub_stripe (struct btrfs_fs_info *fs_info,struct scrub_stripe *stripe) +static int init_scrub_stripe (struct btrfs_fs_info *fs_info, +struct scrub_stripe *stripe) {int ret;
@@ -892,6 +896,9 @@ static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)kfree (sbio); } +for (i =0 ; i <SCRUB_STRIPES_PER_SCTX ; i++) +release_scrub_stripe (&sctx->stripes[i]); +kfree (sctx->wr_curr_bio);scrub_free_csums (sctx);kfree (sctx);
@@ -936,6 +943,14 @@ static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(else sctx->bios[i]->next_free =-1 ; } +for (i =0 ; i <SCRUB_STRIPES_PER_SCTX ; i++) { +int ret; + + ret =init_scrub_stripe (fs_info, &sctx->stripes[i]); +if (ret <0 ) + goto nomem; + sctx->stripes[i].sctx = sctx; + } sctx->first_free =0 ;atomic_set (&sctx->bios_in_flight,0 );atomic_set (&sctx->workers_pending,0 );
@@ -2688,7 +2703,7 @@ static void scrub_stripe_read_repair_worker(struct work_struct *work)wake_up (&stripe->repair_wait); } -void scrub_read_endio (struct btrfs_bio *bbio) +static void scrub_read_endio (struct btrfs_bio *bbio) {struct scrub_stripe *stripe = bbio->private;
@@ -2744,9 +2759,9 @@ static void scrub_write_endio(struct btrfs_bio *bbio)* * - Handle dev-replace and read-repair writeback differently */ -void scrub_write_sectors (struct scrub_ctx *sctx, -struct scrub_stripe *stripe, -unsigned long write_bitmap,bool dev_replace) +static void scrub_write_sectors (struct scrub_ctx *sctx, +struct scrub_stripe *stripe, +unsigned long write_bitmap,bool dev_replace) {struct btrfs_fs_info *fs_info = stripe->bg->fs_info;struct bio *bio =NULL ;
@@ -4319,10 +4334,11 @@ static void scrub_stripe_reset_bitmaps(struct scrub_stripe *stripe)* Return >0 if there is no such stripe in the specified range. * Return <0 for error. */ -int scrub_find_fill_first_stripe (struct btrfs_block_group *bg, -struct btrfs_device *dev,u64 physical, -int mirror_num,u64 logical_start, -u32 logical_len,struct scrub_stripe *stripe) +static int scrub_find_fill_first_stripe (struct btrfs_block_group *bg, +struct btrfs_device *dev,u64 physical, +int mirror_num,u64 logical_start, +u32 logical_len, +struct scrub_stripe *stripe) {struct btrfs_fs_info *fs_info = bg->fs_info;struct btrfs_root *extent_root =btrfs_extent_root (fs_info, bg->start);
@@ -4434,6 +4450,153 @@ int scrub_find_fill_first_stripe(struct btrfs_block_group *bg,return ret; } +static void scrub_reset_stripe (struct scrub_stripe *stripe) +{ +int i; + +scrub_stripe_reset_bitmaps (stripe); + + stripe->nr_meta_extents =0 ; + stripe->nr_data_extents =0 ; + stripe->state =0 ; + +for (i =0 ; i < stripe->nr_sectors; i++) { + stripe->sectors[i].is_metadata = false; + stripe->sectors[i].csum =NULL ; + stripe->sectors[i].generation =0 ; + } +} + +static void scrub_submit_initial_read (struct scrub_ctx *sctx, +struct scrub_stripe *stripe) +{ +struct btrfs_fs_info *fs_info = sctx->fs_info; +struct bio *bio; +int mirror = stripe->mirror_num; +int i; + +ASSERT (stripe->bg); +ASSERT (stripe->mirror_num >0 ); +ASSERT (test_bit (SCRUB_STRIPE_FLAG_INITIALIZED , &stripe->state)); + + bio =btrfs_bio_alloc (BTRFS_STRIPE_LEN >>PAGE_SHIFT ,REQ_OP_READ ,NULL , + scrub_read_endio, stripe); +/* Backed by mempool. */ +ASSERT (bio); + +/* Read the whole stripe. */ + bio->bi_iter.bi_sector = stripe->logical >>SECTOR_SHIFT ; +for (i =0 ; i <BTRFS_STRIPE_LEN >>PAGE_SHIFT ; i++) { +int ret; + + ret =bio_add_page (bio, stripe->pages[i],PAGE_SIZE ,0 ); +/* We should have allocated enough bio vectors. */ +ASSERT (ret ==PAGE_SIZE ); + } +atomic_inc (&stripe->pending_io); +if (sctx->is_dev_replace && + (fs_info->dev_replace.cont_reading_from_srcdev_mode == +BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID || + !stripe->dev->bdev)) { +int num_copies =btrfs_num_copies (fs_info, stripe->bg->start, + stripe->bg->length); + mirror =calc_next_mirror (mirror, num_copies); + } +btrfs_submit_scrub_read (fs_info,btrfs_bio (bio), mirror); +} + +static void flush_scrub_stripes (struct scrub_ctx *sctx) +{ +struct btrfs_fs_info *fs_info = sctx->fs_info; +struct scrub_stripe *stripe; +const int nr_stripes = sctx->cur_stripe; +int i; + +if (!nr_stripes) +return ; + +ASSERT (test_bit (SCRUB_STRIPE_FLAG_INITIALIZED , &sctx->stripes[0 ].state)); +for (i =0 ; i < nr_stripes; i++) { + stripe = &sctx->stripes[i]; +scrub_submit_initial_read (sctx, stripe); + } + +for (i =0 ; i < nr_stripes; i++) { + stripe = &sctx->stripes[i]; + +wait_event (stripe->repair_wait, +test_bit (SCRUB_STRIPE_FLAG_REPAIR_DONE , + &stripe->state)); + } + +/* +* Submit the repaired sectors. +* For zoned case, we can not do repair in-place, but +* queue the bg to be relocated. +*/ +if (btrfs_is_zoned (fs_info)) { +btrfs_repair_one_zone (fs_info, sctx->stripes[0 ].bg->start); + }else { +for (i =0 ; i < nr_stripes; i++) { +unsigned long repaired; + + stripe = &sctx->stripes[i]; + +bitmap_andnot (&repaired, &stripe->init_error_bitmap, + &stripe->error_bitmap, stripe->nr_sectors); +scrub_write_sectors (sctx, stripe, repaired, false); + } + } + +/* Submit for dev-replace. */ +if (sctx->is_dev_replace) { +for (i =0 ; i < nr_stripes; i++) { +unsigned long good; + + stripe = &sctx->stripes[i]; + +ASSERT (stripe->dev == fs_info->dev_replace.srcdev); + +bitmap_andnot (&good, &stripe->extent_sector_bitmap, + &stripe->error_bitmap, stripe->nr_sectors); +scrub_write_sectors (sctx, stripe, good, true); + } + } + +/* For for above writebacks to finish. */ +for (i =0 ; i < nr_stripes; i++) { + stripe = &sctx->stripes[i]; + +wait_scrub_stripe_io (stripe); +scrub_reset_stripe (stripe); + } + sctx->cur_stripe =0 ; +} + +int queue_scrub_stripe (struct scrub_ctx *sctx, +struct btrfs_block_group *bg, +struct btrfs_device *dev,int mirror_num, +u64 logical,u32 length,u64 physical) +{ +struct scrub_stripe *stripe; +int ret; + +/* No available slot, submit all stripes and wait for them. */ +if (sctx->cur_stripe >=SCRUB_STRIPES_PER_SCTX ) +flush_scrub_stripes (sctx); + + stripe = &sctx->stripes[sctx->cur_stripe]; + +/* We can queue one stripe using the remaining slot. */ +scrub_reset_stripe (stripe); + ret =scrub_find_fill_first_stripe (bg, dev, physical, mirror_num, + logical, length, stripe); +/* Either >0 as no more extent or <0 for error. */ +if (ret) +return ret; + sctx->cur_stripe++; +return 0 ; +}/* * Scrub one range which can only has simple mirror based profile.
fs/btrfs/scrub.h +4 -9
@@ -18,14 +18,9 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,* static functions. */ struct scrub_stripe ; -int init_scrub_stripe (struct btrfs_fs_info *fs_info,struct scrub_stripe *stripe); -int scrub_find_fill_first_stripe (struct btrfs_block_group *bg, -struct btrfs_device *dev,u64 physical, -int mirror_num,u64 logical_start, -u32 logical_len,struct scrub_stripe *stripe); -void scrub_read_endio (struct btrfs_bio *bbio); -void scrub_write_sectors (struct scrub_ctx *sctx, -struct scrub_stripe *stripe, -unsigned long write_bitmap,bool dev_replace); +int queue_scrub_stripe (struct scrub_ctx *sctx, +struct btrfs_block_group *bg, +struct btrfs_device *dev,int mirror_num, +u64 logical,u32 length,u64 physical);#endif
Signature · 1 line
2.39.2
PATCH v2 12/12 btrfs: scrub: switch scrub_simple_mirror() to scrub_stripe infrastructure
In reply to Qu WenruoSwitch scrub_simple_mirror() to the new scrub_stripe infrastructure.
Since scrub_simple_mirror() is the core part of scrub (only RAID56 P/Q stripes doesn't utilize it), we can get rid of a big hunk of code, mostly scrub_extent() and scrub_sectors().
There is a functionality change:
- Scrub speed throttle now only affects read on the scrubbing device Writes (for repair and replace), and reads from other mirrors won't be limited by the limits.
--- fs/btrfs/scrub.c | 494 +++-------------------------------------------- fs/btrfs/scrub.h | 10 - 2 files changed, 30 insertions(+), 474 deletions(-)
fs/btrfs/scrub.c +30 -464
@@ -579,10 +579,6 @@ static void scrub_sector_get(struct scrub_sector *sector);static void scrub_sector_put (struct scrub_sector *sector);static void scrub_parity_get (struct scrub_parity *sparity);static void scrub_parity_put (struct scrub_parity *sparity); -static int scrub_sectors (struct scrub_ctx *sctx,u64 logical,u32 len, -u64 physical,struct btrfs_device *dev,u64 flags, -u64 gen,int mirror_num,u8 *csum, -u64 physical_for_dev_replace);static void scrub_bio_end_io (struct bio *bio);static void scrub_bio_end_io_worker (struct work_struct *work);static void scrub_block_complete (struct scrub_block *sblock);
@@ -2976,22 +2972,16 @@ static void scrub_sector_put(struct scrub_sector *sector)kfree (sector); } -/* -* Throttling of IO submission, bandwidth-limit based, the timeslice is 1 -* second. Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max. -*/ -static void scrub_throttle (struct scrub_ctx *sctx) +static void scrub_throttle_dev_io (struct scrub_ctx *sctx, +struct btrfs_device *device, +unsigned int bio_size) {const int time_slice =1000 ; -struct scrub_bio *sbio; -struct btrfs_device *device;s64 delta;ktime_t now;u32 div;u64 bwlimit; - sbio = sctx->bios[sctx->curr]; - device = sbio->dev; bwlimit =READ_ONCE (device->scrub_speed_max);if (bwlimit ==0 )return ;
@@ -3013,7 +3003,7 @@ static void scrub_throttle(struct scrub_ctx *sctx)/* Still in the time to send? */ if (ktime_before (now, sctx->throttle_deadline)) {/* If current bio is within the limit, send it */ - sctx->throttle_sent += sbio->bio->bi_iter.bi_size; + sctx->throttle_sent += bio_size;if (sctx->throttle_sent <=div_u64 (bwlimit, div))return ;
@@ -3035,6 +3025,17 @@ static void scrub_throttle(struct scrub_ctx *sctx) sctx->throttle_deadline =0 ; } +/* +* Throttling of IO submission, bandwidth-limit based, the timeslice is 1 +* second. Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max. +*/ +static void scrub_throttle (struct scrub_ctx *sctx) +{ +struct scrub_bio *sbio = sctx->bios[sctx->curr]; + +scrub_throttle_dev_io (sctx, sbio->dev, sbio->bio->bi_iter.bi_size); +} +static void scrub_submit (struct scrub_ctx *sctx) {struct scrub_bio *sbio;
@@ -3119,202 +3120,6 @@ static int scrub_add_sector_to_rd_bio(struct scrub_ctx *sctx,return 0 ; } -static void scrub_missing_raid56_end_io (struct bio *bio) -{ -struct scrub_block *sblock = bio->bi_private; -struct btrfs_fs_info *fs_info = sblock->sctx->fs_info; - -btrfs_bio_counter_dec (fs_info); -if (bio->bi_status) - sblock->no_io_error_seen =0 ; - -bio_put (bio); - -queue_work (fs_info->scrub_workers, &sblock->work); -} - -static void scrub_missing_raid56_worker (struct work_struct *work) -{ - struct scrub_block *sblock = container_of(work, struct scrub_block, work); -struct scrub_ctx *sctx = sblock->sctx; -struct btrfs_fs_info *fs_info = sctx->fs_info; -u64 logical; -struct btrfs_device *dev; - - logical = sblock->logical; - dev = sblock->dev; - -if (sblock->no_io_error_seen) -scrub_recheck_block_checksum (sblock); - -if (!sblock->no_io_error_seen) { -spin_lock (&sctx->stat_lock); - sctx->stat.read_errors++; -spin_unlock (&sctx->stat_lock); -btrfs_err_rl_in_rcu (fs_info, -"IO error rebuilding logical %llu for dev %s" , - logical,btrfs_dev_name (dev)); - }else if (sblock->header_error || sblock->checksum_error) { -spin_lock (&sctx->stat_lock); - sctx->stat.uncorrectable_errors++; -spin_unlock (&sctx->stat_lock); -btrfs_err_rl_in_rcu (fs_info, -"failed to rebuild valid logical %llu for dev %s" , - logical,btrfs_dev_name (dev)); - }else { -scrub_write_block_to_dev_replace (sblock); - } - -if (sctx->is_dev_replace && sctx->flush_all_writes) { -mutex_lock (&sctx->wr_lock); -scrub_wr_submit (sctx); -mutex_unlock (&sctx->wr_lock); - } - -scrub_block_put (sblock); -scrub_pending_bio_dec (sctx); -} - -static void scrub_missing_raid56_pages (struct scrub_block *sblock) -{ -struct scrub_ctx *sctx = sblock->sctx; -struct btrfs_fs_info *fs_info = sctx->fs_info; -u64 length = sblock->sector_count << fs_info->sectorsize_bits; -u64 logical = sblock->logical; -struct btrfs_io_context *bioc =NULL ; -struct bio *bio; -struct btrfs_raid_bio *rbio; -int ret; -int i; - -btrfs_bio_counter_inc_blocked (fs_info); - ret =btrfs_map_sblock (fs_info,BTRFS_MAP_GET_READ_MIRRORS , logical, - &length, &bioc); -if (ret || !bioc) - goto bioc_out; - -if (WARN_ON (!sctx->is_dev_replace || - !(bioc->map_type &BTRFS_BLOCK_GROUP_RAID56_MASK ))) { -/* -* We shouldn't be scrubbing a missing device. Even for dev -* replace, we should only get here for RAID 5/6. We either -* managed to mount something with no mirrors remaining or -* there's a bug in scrub_find_good_copy()/btrfs_map_block(). -*/ - goto bioc_out; - } - - bio =bio_alloc (NULL ,BIO_MAX_VECS ,REQ_OP_READ ,GFP_NOFS ); - bio->bi_iter.bi_sector = logical >>9 ; - bio->bi_private = sblock; - bio->bi_end_io = scrub_missing_raid56_end_io; - - rbio =raid56_alloc_missing_rbio (bio, bioc); -if (!rbio) - goto rbio_out; - -for (i =0 ; i < sblock->sector_count; i++) { -struct scrub_sector *sector = sblock->sectors[i]; - -raid56_add_scrub_pages (rbio,scrub_sector_get_page (sector), -scrub_sector_get_page_offset (sector), - sector->offset + sector->sblock->logical); - } - -INIT_WORK (&sblock->work, scrub_missing_raid56_worker); -scrub_block_get (sblock); -scrub_pending_bio_inc (sctx); -raid56_submit_missing_rbio (rbio); -btrfs_put_bioc (bioc); -return ; - -rbio_out: -bio_put (bio); -bioc_out: -btrfs_bio_counter_dec (fs_info); -btrfs_put_bioc (bioc); -spin_lock (&sctx->stat_lock); - sctx->stat.malloc_errors++; -spin_unlock (&sctx->stat_lock); -} - -static int scrub_sectors (struct scrub_ctx *sctx,u64 logical,u32 len, -u64 physical,struct btrfs_device *dev,u64 flags, -u64 gen,int mirror_num,u8 *csum, -u64 physical_for_dev_replace) -{ -struct scrub_block *sblock; -const u32 sectorsize = sctx->fs_info->sectorsize; -int index; - - sblock =alloc_scrub_block (sctx, dev, logical, physical, - physical_for_dev_replace, mirror_num); -if (!sblock) { -spin_lock (&sctx->stat_lock); - sctx->stat.malloc_errors++; -spin_unlock (&sctx->stat_lock); -return -ENOMEM ; - } - -for (index =0 ; len >0 ; index++) { -struct scrub_sector *sector; -/* -* Here we will allocate one page for one sector to scrub. -* This is fine if PAGE_SIZE == sectorsize, but will cost -* more memory for PAGE_SIZE > sectorsize case. -*/ -u32 l =min (sectorsize, len); - - sector =alloc_scrub_sector (sblock, logical); -if (!sector) { -spin_lock (&sctx->stat_lock); - sctx->stat.malloc_errors++; -spin_unlock (&sctx->stat_lock); -scrub_block_put (sblock); -return -ENOMEM ; - } - sector->flags = flags; - sector->generation = gen; -if (csum) { - sector->have_csum =1 ; -memcpy (sector->csum, csum, sctx->fs_info->csum_size); - }else { - sector->have_csum =0 ; - } - len -= l; - logical += l; - physical += l; - physical_for_dev_replace += l; - } - -WARN_ON (sblock->sector_count ==0 ); -if (test_bit (BTRFS_DEV_STATE_MISSING , &dev->dev_state)) { -/* -* This case should only be hit for RAID 5/6 device replace. See -* the comment in scrub_missing_raid56_pages() for details. -*/ -scrub_missing_raid56_pages (sblock); - }else { -for (index =0 ; index < sblock->sector_count; index++) { -struct scrub_sector *sector = sblock->sectors[index]; -int ret; - - ret =scrub_add_sector_to_rd_bio (sctx, sector); -if (ret) { -scrub_block_put (sblock); -return ret; - } - } - -if (flags &BTRFS_EXTENT_FLAG_SUPER ) -scrub_submit (sctx); - } - -/* last one frees, either here or in bio completion for last page */ -scrub_block_put (sblock); -return 0 ; -} -static void scrub_bio_end_io (struct bio *bio) {struct scrub_bio *sbio = bio->bi_private;
@@ -3499,179 +3304,6 @@ static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)return 1 ; } -static bool should_use_device (struct btrfs_fs_info *fs_info, -struct btrfs_device *dev, -bool follow_replace_read_mode) -{ -struct btrfs_device *replace_srcdev = fs_info->dev_replace.srcdev; -struct btrfs_device *replace_tgtdev = fs_info->dev_replace.tgtdev; - -if (!dev->bdev) -return false; - -/* -* We're doing scrub/replace, if it's pure scrub, no tgtdev should be -* here. If it's replace, we're going to write data to tgtdev, thus -* the current data of the tgtdev is all garbage, thus we can not use -* it at all. -*/ -if (dev == replace_tgtdev) -return false; - -/* No need to follow replace read mode, any existing device is fine. */ -if (!follow_replace_read_mode) -return true; - -/* Need to follow the mode. */ -if (fs_info->dev_replace.cont_reading_from_srcdev_mode == -BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID ) -return dev != replace_srcdev; -return true; -} -static int scrub_find_good_copy (struct btrfs_fs_info *fs_info, -u64 extent_logical,u32 extent_len, -u64 *extent_physical, -struct btrfs_device **extent_dev, -int *extent_mirror_num) -{ -u64 mapped_length; -struct btrfs_io_context *bioc =NULL ; -int ret; -int i; - - mapped_length = extent_len; - ret =btrfs_map_block (fs_info,BTRFS_MAP_GET_READ_MIRRORS , - extent_logical, &mapped_length, &bioc,0 ); -if (ret || !bioc || mapped_length < extent_len) { -btrfs_put_bioc (bioc); -btrfs_err_rl (fs_info,"btrfs_map_block() failed for logical %llu: %d" , - extent_logical, ret); -return -EIO ; - } - -/* -* First loop to exclude all missing devices and the source device if -* needed. And we don't want to use target device as mirror either, as -* we're doing the replace, the target device range contains nothing. -*/ -for (i =0 ; i < bioc->num_stripes - bioc->replace_nr_stripes; i++) { -struct btrfs_io_stripe *stripe = &bioc->stripes[i]; - -if (!should_use_device (fs_info, stripe->dev, true)) -continue ; - goto found; - } -/* -* We didn't find any alternative mirrors, we have to break our replace -* read mode, or we can not read at all. -*/ -for (i =0 ; i < bioc->num_stripes - bioc->replace_nr_stripes; i++) { -struct btrfs_io_stripe *stripe = &bioc->stripes[i]; - -if (!should_use_device (fs_info, stripe->dev, false)) -continue ; - goto found; - } - -btrfs_err_rl (fs_info,"failed to find any live mirror for logical %llu" , - extent_logical); -return -EIO ; - -found: - *extent_physical = bioc->stripes[i].physical; - *extent_mirror_num = i +1 ; - *extent_dev = bioc->stripes[i].dev; -btrfs_put_bioc (bioc); -return 0 ; -} - -static bool scrub_need_different_mirror (struct scrub_ctx *sctx, -struct map_lookup *map, -struct btrfs_device *dev) -{ -/* -* For RAID56, all the extra mirrors are rebuilt from other P/Q, -* cannot utilize other mirrors directly. -*/ -if (map->type &BTRFS_BLOCK_GROUP_RAID56_MASK ) -return false; - -if (!dev->bdev) -return true; - -return sctx->fs_info->dev_replace.cont_reading_from_srcdev_mode == -BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID ; -} - -/* scrub extent tries to collect up to 64 kB for each bio */ -static int scrub_extent (struct scrub_ctx *sctx,struct map_lookup *map, -u64 logical,u32 len, -u64 physical,struct btrfs_device *dev,u64 flags, -u64 gen,int mirror_num) -{ -struct btrfs_device *src_dev = dev; -u64 src_physical = physical; -int src_mirror = mirror_num; -int ret; -u8 csum[BTRFS_CSUM_SIZE ]; -u32 blocksize; - -if (flags &BTRFS_EXTENT_FLAG_DATA ) { -if (map->type &BTRFS_BLOCK_GROUP_RAID56_MASK ) - blocksize =BTRFS_STRIPE_LEN ; -else - blocksize = sctx->fs_info->sectorsize; -spin_lock (&sctx->stat_lock); - sctx->stat.data_extents_scrubbed++; - sctx->stat.data_bytes_scrubbed += len; -spin_unlock (&sctx->stat_lock); - }else if (flags &BTRFS_EXTENT_FLAG_TREE_BLOCK ) { -if (map->type &BTRFS_BLOCK_GROUP_RAID56_MASK ) - blocksize =BTRFS_STRIPE_LEN ; -else - blocksize = sctx->fs_info->nodesize; -spin_lock (&sctx->stat_lock); - sctx->stat.tree_extents_scrubbed++; - sctx->stat.tree_bytes_scrubbed += len; -spin_unlock (&sctx->stat_lock); - }else { - blocksize = sctx->fs_info->sectorsize; -WARN_ON (1 ); - } - -/* -* For dev-replace case, we can have @dev being a missing device, or -* we want to avoid reading from the source device if possible. -*/ -if (sctx->is_dev_replace &&scrub_need_different_mirror (sctx, map, dev)) { - ret =scrub_find_good_copy (sctx->fs_info, logical, len, - &src_physical, &src_dev, &src_mirror); -if (ret <0 ) -return ret; - } -while (len) { -u32 l =min (len, blocksize); -int have_csum =0 ; - -if (flags &BTRFS_EXTENT_FLAG_DATA ) { -/* push csums to sbio */ - have_csum =scrub_find_csum (sctx, logical, csum); -if (have_csum ==0 ) - ++sctx->stat.no_csum; - } - ret =scrub_sectors (sctx, logical, l, src_physical, src_dev, - flags, gen, src_mirror, - have_csum ? csum :NULL , physical); -if (ret) -return ret; - len -= l; - logical += l; - physical += l; - src_physical += l; - } -return 0 ; -} - static int scrub_sectors_for_parity(struct scrub_parity *sparity, u64 logical, u32 len, u64 physical, struct btrfs_device *dev,
@@ -4254,20 +3886,6 @@ static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,return ret <0 ? ret :0 ; } -static void sync_replace_for_zoned (struct scrub_ctx *sctx) -{ -if (!btrfs_is_zoned (sctx->fs_info)) -return ; - - sctx->flush_all_writes = true; -scrub_submit (sctx); -mutex_lock (&sctx->wr_lock); -scrub_wr_submit (sctx); -mutex_unlock (&sctx->wr_lock); - -wait_event (sctx->list_wait,atomic_read (&sctx->bios_in_flight) ==0 ); -} -static int sync_write_pointer_for_zoned (struct scrub_ctx *sctx,u64 logical,u64 physical,u64 physical_end) {
@@ -4516,6 +4134,9 @@ static void flush_scrub_stripes(struct scrub_ctx *sctx)return ;ASSERT (test_bit (SCRUB_STRIPE_FLAG_INITIALIZED , &sctx->stripes[0 ].state)); + +scrub_throttle_dev_io (sctx, sctx->stripes[0 ].dev, + nr_stripes <<BTRFS_STRIPE_LEN_SHIFT );for (i =0 ; i < nr_stripes; i++) { stripe = &sctx->stripes[i];scrub_submit_initial_read (sctx, stripe);
@@ -4573,10 +4194,10 @@ static void flush_scrub_stripes(struct scrub_ctx *sctx) sctx->cur_stripe =0 ; } -int queue_scrub_stripe (struct scrub_ctx *sctx, -struct btrfs_block_group *bg, -struct btrfs_device *dev,int mirror_num, -u64 logical,u32 length,u64 physical) +static int queue_scrub_stripe (struct scrub_ctx *sctx, +struct btrfs_block_group *bg, +struct btrfs_device *dev,int mirror_num, +u64 logical,u32 length,u64 physical) {struct scrub_stripe *stripe;int ret;
@@ -4614,11 +4235,8 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx, u64 physical, int mirror_num) {struct btrfs_fs_info *fs_info = sctx->fs_info; -struct btrfs_root *csum_root =btrfs_csum_root (fs_info, bg->start); -struct btrfs_root *extent_root =btrfs_extent_root (fs_info, bg->start);const u64 logical_end = logical_start + logical_length;/* An artificial limit, inherit from old scrub behavior */ -const u32 max_length =SZ_64K ;struct btrfs_path path = {0 };u64 cur_logical = logical_start;int ret;
@@ -4630,11 +4248,7 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx, path.skip_locking =1 ;/* Go through each extent items inside the logical range */ while (cur_logical < logical_end) { -u64 extent_start; -u64 extent_len; -u64 extent_flags; -u64 extent_gen; -u64 scrub_len; +u64 cur_physical = physical + cur_logical - logical_start;/* Canceled? */ if (atomic_read(&fs_info->scrub_cancel_req) ||
@@ -4664,8 +4278,9 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx, }spin_unlock (&bg->lock); - ret =find_first_extent_item (extent_root, &path, cur_logical, - logical_end - cur_logical); + ret =queue_scrub_stripe (sctx, bg, device, mirror_num, + cur_logical, logical_end - cur_logical, + cur_physical);if (ret >0 ) {/* No more extent, just update the accounting */ sctx->stat.last_physical = physical + logical_length;
@@ -4674,52 +4289,11 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx, }if (ret <0 )break ; -get_extent_info (&path, &extent_start, &extent_len, - &extent_flags, &extent_gen); -/* Skip hole range which doesn't have any extent */ - cur_logical =max (extent_start, cur_logical); -/* -* Scrub len has three limits: -* - Extent size limit -* - Scrub range limit -* This is especially imporatant for RAID0/RAID10 to reuse -* this function -* - Max scrub size limit -*/ - scrub_len =min (min (extent_start + extent_len, - logical_end), cur_logical + max_length) - - cur_logical; +ASSERT (sctx->cur_stripe >0 ); + cur_logical = sctx->stripes[sctx->cur_stripe -1 ].logical + +BTRFS_STRIPE_LEN ; -if (extent_flags &BTRFS_EXTENT_FLAG_DATA ) { - ret =btrfs_lookup_csums_list (csum_root, cur_logical, - cur_logical + scrub_len -1 , - &sctx->csum_list,1 , false); -if (ret) -break ; - } -if ((extent_flags &BTRFS_EXTENT_FLAG_TREE_BLOCK ) && -does_range_cross_boundary (extent_start, extent_len, - logical_start, logical_length)) { -btrfs_err (fs_info, -"scrub: tree block %llu spanning boundaries, ignored. boundary=[%llu, %llu)" , - extent_start, logical_start, logical_end); -spin_lock (&sctx->stat_lock); - sctx->stat.uncorrectable_errors++; -spin_unlock (&sctx->stat_lock); - cur_logical += scrub_len; -continue ; - } - ret =scrub_extent (sctx, map, cur_logical, scrub_len, - cur_logical - logical_start + physical, - device, extent_flags, extent_gen, - mirror_num); -scrub_free_csums (sctx); -if (ret) -break ; -if (sctx->is_dev_replace) -sync_replace_for_zoned (sctx); - cur_logical += scrub_len;/* Don't hold CPU for too long time */ cond_resched (); }
@@ -4804,7 +4378,6 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, int stripe_index) {struct btrfs_fs_info *fs_info = sctx->fs_info; -struct blk_plug plug;struct map_lookup *map = em->map_lookup;const u64 profile = map->type &BTRFS_BLOCK_GROUP_PROFILE_MASK ;const u64 chunk_logical = bg->start;
@@ -4826,12 +4399,6 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, atomic_read(&sctx->bios_in_flight) == 0);scrub_blocked_if_needed (fs_info); -/* -* collect all data csums for the stripe to avoid seeking during -* the scrub. This might currently (crc32) end up to be about 1MB -*/ -blk_start_plug (&plug); -if (sctx->is_dev_replace &&btrfs_dev_is_sequential (sctx->wr_tgtdev, physical)) {mutex_lock (&sctx->wr_lock);
@@ -4933,8 +4500,7 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,mutex_lock (&sctx->wr_lock);scrub_wr_submit (sctx);mutex_unlock (&sctx->wr_lock); - -blk_finish_plug (&plug); +flush_scrub_stripes (sctx);if (sctx->is_dev_replace && ret >=0 ) {int ret2;
fs/btrfs/scrub.h +0 -10
@@ -13,14 +13,4 @@ int btrfs_scrub_cancel_dev(struct btrfs_device *dev);int btrfs_scrub_progress (struct btrfs_fs_info *fs_info,u64 devid,struct btrfs_scrub_progress *progress); -/* -* The following functions are temporary exports to avoid warning on unused -* static functions. -*/ -struct scrub_stripe ; -int queue_scrub_stripe (struct scrub_ctx *sctx, -struct btrfs_block_group *bg, -struct btrfs_device *dev,int mirror_num, -u64 logical,u32 length,u64 physical); -#endif
Signature · 1 line
2.39.2
Quoted 3 lines · Qu Wenruo
- More testing on zoned devices
Now the patchset can already pass all scrub/replace groups with regular devices.
While probably not being the ultimate solution for you here, but you can use qemu to emulate ZNS drives [1].
The TL;DR is:
qemu-system-x86_64 -device nvme,id=nvme0,serial=01234 \
-drive file=${znsimg},id=nvmezns0,format=raw,if=none \
-device nvme-ns,drive=nvmezns0,bus=nvme0,nsid=1,zoned=trueQuoted 8 lines · Johannes Thumshirn
Quoted 3 lines · Qu Wenruo
- More testing on zoned devices
Now the patchset can already pass all scrub/replace groups with regular devices.While probably not being the ultimate solution for you here, but you can use qemu to emulate ZNS drives [1].
Quoted 8 lines
The TL;DR is:
qemu-system-x86_64 -device nvme,id=nvme0,serial=01234 \ -drive file=${znsimg},id=nvmezns0,format=raw,if=none \ -device nvme-ns,drive=nvmezns0,bus=nvme0,nsid=1,zoned=true
Is there a libvirt xml binding for it?
Still prefer libvirt xml based emulation, which would benefit a lot for all my daily runs.
Thanks,
Qu
Quoted 17 lines · Qu Wenruo
Quoted 14 lines · Johannes Thumshirn
Quoted 3 lines · Qu Wenruo
- More testing on zoned devices
Now the patchset can already pass all scrub/replace groups with regular devices.While probably not being the ultimate solution for you here, but you can use qemu to emulate ZNS drives [1].
The TL;DR is:
qemu-system-x86_64 -device nvme,id=nvme0,serial=01234 \ -drive file=${znsimg},id=nvmezns0,format=raw,if=none \ -device nvme-ns,drive=nvmezns0,bus=nvme0,nsid=1,zoned=true
Quoted 6 lines
Quoted 2 lines
Is there a libvirt xml binding for it?
Still prefer libvirt xml based emulation, which would benefit a lot for all my daily runs.
Not that I know of, but you can add qemu commandline stuff into libvirt.
Something like that (untested):
<qemu:commandline>
<qemu:arg value='-drive'/>
<qemu:arg value='file=/path/to/nvme.img,format=raw,if=none,id=nvmezns0'/> <qemu:arg value='-device'/>
<qemu:arg value='nvme-ns,drive=nvmezns0,bus=nvme0,nsid=1,zoned=true'/>
</qemu:commandline>
Quoted 27 lines · Johannes Thumshirn
Quoted 17 lines · Qu Wenruo
Quoted 14 lines · Johannes Thumshirn
Quoted 3 lines · Qu Wenruo
- More testing on zoned devices
Now the patchset can already pass all scrub/replace groups with regular devices.While probably not being the ultimate solution for you here, but you can use qemu to emulate ZNS drives [1].
The TL;DR is:
qemu-system-x86_64 -device nvme,id=nvme0,serial=01234 \ -drive file=${znsimg},id=nvmezns0,format=raw,if=none \ -device nvme-ns,drive=nvmezns0,bus=nvme0,nsid=1,zoned=trueQuoted 6 lines
Quoted 2 lines
Is there a libvirt xml binding for it?
Still prefer libvirt xml based emulation, which would benefit a lot for all my daily runs.Not that I know of, but you can add qemu commandline stuff into libvirt.
Quoted 8 lines
Something like that (untested):
<qemu:commandline>
<qemu:arg value='-drive'/>
<qemu:arg value='file=/path/to/nvme.img,format=raw,if=none,id=nvmezns0'/> <qemu:arg value='-device'/>
<qemu:arg value='nvme-ns,drive=nvmezns0,bus=nvme0,nsid=1,zoned=true'/>
</qemu:commandline>
I guess that's the only way to go.
I'll report back after I got it running.
If everything went well, we will see super niche combination, like 64K page size aarch64 with ZNS running for tests...
Thanks,
Qu
Quoted 6 lines · Qu Wenruo
This series can be found in my github repo:
https://github.com/adam900710/linux/tree/scrub_stripe
It's recommended to fetch from the repo, as our misc-next seems to change pretty rapidly.
There's a cleanup series that changed return value type of btrfs_bio_alloc which is used in 3 patches and it fails to compile. The bio pointer needs to be switched to btrfs_bio and touches the logic so it's not a trivial change I'd otherwise do.
There's also some trailing whitespace in some patches and 'git am' refuses to apply them. Pulling the series won't unfortunatelly help, please refresh the series.
Regarding misc-next updates: it gets rebased each Monday after a -rc is released so the potentially duplicated patches merged in the last week disappear from misc-next. Otherwise I don't rebase it, only append patches and occasionally update tags or some trivial bits in the code.
If you work on a patchset for a long time it may become a chasing game for the stable base, in that case we need to coordinate and/or postpone some series.
Quoted 5 lines · David Sterba
Quoted 4 lines · Qu Wenruo
This series can be found in my github repo:
Quoted 7 lines
Quoted 2 lines
It's recommended to fetch from the repo, as our misc-next seems to change pretty rapidly.
There's a cleanup series that changed return value type of
btrfs_bio_allocwhich is used in 3 patches and it fails to compile. The bio pointer needs to be switched tobtrfs_bioand touches the logic so it's not a trivial change I'd otherwise do.
I'm aware of that type safe patchset and that's expected, and I'm already ready to rebase.
The reason I sent the series as is, is to get some more feedback, especially considering how large the series is, and it's touching the core functionality of scrub.
Quoted 4 lines
There's also some trailing whitespace in some patches and 'git am' refuses to apply them. Pulling the series won't unfortunatelly help, please refresh the series.
That's a total surprise here.
Shouldn't the btrfs workflow catch such problems?
Or the hook is not triggered for rebase?
Anyway thanks for catching this new problem.
Thanks,
Qu
Quoted 9 lines
Regarding misc-next updates: it gets rebased each Monday after a -rc is released so the potentially duplicated patches merged in the last week disappear from misc-next. Otherwise I don't rebase it, only append patches and occasionally update tags or some trivial bits in the code.
If you work on a patchset for a long time it may become a chasing game for the stable base, in that case we need to coordinate and/or postpone some series.
Quoted 3 lines · Qu Wenruo
- More cleanup on RAID56 path
Now RAID56 still uses some old facility, resulting things likescrub_sectorandscrub_biocan not be fully cleaned up.
I think converting the raid path is something that should be done before merging the series, instead of leaving the parallel infrastructure in.
Quoted 8 lines · Christoph Hellwig
Quoted 3 lines · Qu Wenruo
- More cleanup on RAID56 path
Now RAID56 still uses some old facility, resulting things likescrub_sectorandscrub_biocan not be fully cleaned up.I think converting the raid path is something that should be done before merging the series, instead of leaving the parallel infrastructure in.
The RAID56 scrub path is indeed a little messy, but I'm not sure what's the better way to clean it up.
For now, if it's a data stripe, it's already using the scrub_simple_mirror(), so that's not a big deal.
The problem is in scrub_raid56_data_stripes_for_parity(), which is doing the same data stripes scrubbing, but with a slightly different behavior dedicated for parity.
My current plan is, go with scrub_stripe for the low hanging fruit (everything except P/Q scrub, perf and readability improvement).
Then convert the remaining RAID56 code to the scrub_stripe facility.
The conversion itself maybe even larger than this patchset, although most of the change would be just dropping the old facility.
So for now, I prefer to perfect the scrub_stripe for the low hanging fruits first, leaving the days of the old facility counted, then do a proper final conversion.
Thanks,
Qu
Quoted 8 lines · Christoph Hellwig
Quoted 3 lines · Qu Wenruo
- More cleanup on RAID56 path
Now RAID56 still uses some old facility, resulting things likescrub_sectorandscrub_biocan not be fully cleaned up.I think converting the raid path is something that should be done before merging the series, instead of leaving the parallel infrastructure in.
BTW, finally I have a local branch with the remaining path converted to the new infrastructure.
The real problem is not converting the raid path.
The patch doing the convert is pretty small, mostly thanks to the new scrub_stripe infrastructure, we can get rid of the complex scrub_parity and scrub_recover related code.
fs/btrfs/scrub.c | 168 ++++++++++++++++++++++++++++++++++++++++++++--- fs/btrfs/scrub.h | 4 ++ 2 files changed, 162 insertions(+), 10 deletions(-)
The problem is how I cleanup the existing scrub infrastructure (scrub_sector, scrub_block, scrub_recover, scrub_parity structures and involved calls).
Currently I just do a single patch to cleanup, the result is super aweful:
fs/btrfs/scrub.c | 2513 +--------------------------------------------- fs/btrfs/scrub.h | 4 - 2 files changed, 5 insertions(+), 2512 deletions(-)
To be honest, I'm more concerned on how to split the cleanup patch.
Thanks,
Qu
Quoted 5 lines · Qu Wenruo
Currently I just do a single patch to cleanup, the result is super aweful:
fs/btrfs/scrub.c | 2513 +--------------------------------------------- fs/btrfs/scrub.h | 4 - 2 files changed, 5 insertions(+), 2512 deletions(-)
To me this looks perfect. A patch that just removes a lot of code is always good.
Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Qu Wenruo
Currently I just do a single patch to cleanup, the result is super aweful:
fs/btrfs/scrub.c | 2513 +--------------------------------------------- fs/btrfs/scrub.h | 4 - 2 files changed, 5 insertions(+), 2512 deletions(-)To me this looks perfect. A patch that just removes a lot of code is always good.
Isn't the patch split also important?
Anyway I will try some different methods to split that.
Thanks,
Qu
Quoted 3 lines · Qu Wenruo
Isn't the patch split also important?
Anyway I will try some different methods to split that.
I'll have to defer to David as btrfs is sometimes a little different from the rest of the kernel in it's requirements, but everywhere else a patch that just removes code can be as big as it gets.