btrfs: scrub: finish the switch to scrub_stripe and cleanup the old code
cover btrfs: scrub: finish the switch to scrub_stripe and cleanup the old code
This series is based on my previous patchset "v7 btrfs: scrub: use a more reader friendly code to implement scrub_simple_mirror()", the whole series can be fetched from github:
https://github.com/adam900710/linux/tree/scrub_stripe
After the mentioned patchset, only one path, scrub_raid56_parity(), is still using the old facility.
[PATCH SPLIT]
This series would firstly migrate the only path to the new scrub_stripe based solution.
The change should only affect RAID56, thus zoned mode should not be affected.
Then the remaining 5 patches to remove all the remaining code. I have tried my best to split the cleanup, starting from RAID56 specific code, to writeback, then approach the core scrub_bio facility.
If the split is not preferred, it should still be possible to fold all the last 5 patches into one big cleanup.
[CODE SIZE REDUCTION]
The number of deletion should explain itself, and even with my previous patchset included, it is still a big win:
9 files changed, 1488 insertions(+), 2931 deletions(-)
Still a net reduce of almost 1500 lines.
Qu Wenruo (6):
btrfs: scrub: use scrub_stripe to implement RAID56 P/Q scrub
btrfs: scrub: remove scrub_parity structure
btrfs: scrub: remove the old writeback infrastructure
btrfs: scrub: remove the old scrub recheck code
btrfs: scrub: remove scrub_block and scrub_sector structures
btrfs: scrub: remove scrub_bio structure
fs/btrfs/block-group.c | 11 - fs/btrfs/block-group.h | 8 - fs/btrfs/fs.h | 1 - fs/btrfs/scrub.c | 2740 ++++------------------------------------ 4 files changed, 212 insertions(+), 2548 deletions(-)
Signature · 1 line
2.39.2
PATCH 1/6 btrfs: scrub: use scrub_stripe to implement RAID56 P/Q scrub
This patch would implement the only missing part for scrub: RAID56 P/Q stripe scrub.
The workflow is pretty straightforward for the new function, scrub_raid56_parity_stripe():
- Go through the regular scrub path for each data stripe
- Wait for the verification and repair to finish
- Writeback the repaired sectors to data stripes
- Make sure all stripes are properly repaired
If we have sectors unrepaired, we can not continue, or we may further corrupt the P/Q stripe.
- Submit the rbio for P/Q stripe
The dev-replace would be handled inside
raid56_parity_submit_scrub_rbio() path.
- Wait for the above bio to finish
Although the old code is no longer used, we still keep the declaration, as the cleanup can be several times larger than this patch itself.
--- fs/btrfs/scrub.c | 217 ++++++++++++++++++++++++++++++++++++++++++++--- fs/btrfs/scrub.h | 5 ++ 2 files changed, 212 insertions(+), 10 deletions(-)
fs/btrfs/scrub.c +207 -10
@@ -99,6 +99,13 @@ enum scrub_stripe_flags {/* Set when the read-repair is finished. */ SCRUB_STRIPE_FLAG_REPAIR_DONE , + +/* +* Set for data stripes if it's triggered from P/Q stripe. +* During such scrub, we should not report error in data stripes, nor +* update the accounting. +*/ + SCRUB_STRIPE_FLAG_NO_REPORT, };#define SCRUB_STRIPE_PAGES (BTRFS_STRIPE_LEN / PAGE_SIZE)
@@ -279,6 +286,7 @@ struct scrub_parity {struct scrub_ctx {struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX ];struct scrub_stripe stripes[SCRUB_STRIPES_PER_SCTX ]; +struct scrub_stripe *raid56_data_stripes;struct btrfs_fs_info *fs_info;int first_free;int curr;
@@ -2509,6 +2517,9 @@ static void scrub_stripe_report_errors(struct scrub_ctx *sctx,int nr_repaired_sectors =0 ;int sector_nr; +if (test_bit (SCRUB_STRIPE_FLAG_NO_REPORT , &stripe->state)) +return ; +/* * Init needed infos for error reporting. *
@@ -3823,11 +3834,9 @@ static int scrub_raid56_data_stripe_for_parity(struct scrub_ctx *sctx,return ret; } -static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx, -struct map_lookup *map, -struct btrfs_device *sdev, -u64 logic_start, -u64 logic_end) +int scrub_raid56_parity (struct scrub_ctx *sctx,struct map_lookup *map, +struct btrfs_device *sdev,u64 logic_start, +u64 logic_end) {struct btrfs_fs_info *fs_info = sctx->fs_info;struct btrfs_path *path;
@@ -4200,6 +4209,11 @@ static void flush_scrub_stripes(struct scrub_ctx *sctx) sctx->cur_stripe =0 ; } +static void raid56_scrub_wait_endio (struct bio *bio) +{ +complete (bio->bi_private); +} + static int queue_scrub_stripe(struct scrub_ctx *sctx, struct btrfs_block_group *bg, struct btrfs_device *dev, int mirror_num,
@@ -4225,6 +4239,167 @@ static int queue_scrub_stripe(struct scrub_ctx *sctx,return 0 ; } +static int scrub_raid56_parity_stripe (struct scrub_ctx *sctx, +struct btrfs_device *scrub_dev, +struct btrfs_block_group *bg, +struct map_lookup *map, +u64 full_stripe_start) +{ +DECLARE_COMPLETION_ONSTACK (io_done); +struct btrfs_fs_info *fs_info = sctx->fs_info; +struct btrfs_raid_bio *rbio; +struct btrfs_io_context *bioc =NULL ; +struct bio *bio; +struct scrub_stripe *stripe; +bool all_empty = true; +const int data_stripes =nr_data_stripes (map); +unsigned long extent_bitmap =0 ; +u64 length = data_stripes <<BTRFS_STRIPE_LEN_SHIFT ; +int ret; + +ASSERT (sctx->raid56_data_stripes); + +for (int i =0 ; i < data_stripes; i++) { +int stripe_index; +int rot; +u64 physical; + + stripe = &sctx->raid56_data_stripes[i]; + rot =div_u64 (full_stripe_start - bg->start, data_stripes) >> +BTRFS_STRIPE_LEN_SHIFT ; + stripe_index = (i + rot) % map->num_stripes; + physical = map->stripes[stripe_index].physical + + (rot <<BTRFS_STRIPE_LEN_SHIFT ); + +scrub_reset_stripe (stripe); +set_bit (SCRUB_STRIPE_FLAG_NO_REPORT , &stripe->state); + ret =scrub_find_fill_first_stripe (bg, + map->stripes[stripe_index].dev, physical,1 , + full_stripe_start + (i <<BTRFS_STRIPE_LEN_SHIFT ), +BTRFS_STRIPE_LEN , stripe); +if (ret <0 ) + goto out; +/* +* No extent in this data stripe, need to manually mark them +* initialized to make later read submission happy. +*/ +if (ret >0 ) { + stripe->logical = full_stripe_start + + (i <<BTRFS_STRIPE_LEN_SHIFT ); + stripe->dev = map->stripes[stripe_index].dev; + stripe->mirror_num =1 ; +set_bit (SCRUB_STRIPE_FLAG_INITIALIZED , &stripe->state); + } + } + +/* Check if all data stripes are empty. */ +for (int i =0 ; i < data_stripes; i++) { + stripe = &sctx->raid56_data_stripes[i]; +if (!bitmap_empty (&stripe->extent_sector_bitmap, + stripe->nr_sectors)) { + all_empty = false; +break ; + } + } +if (all_empty) { + ret =0 ; + goto out; + } + +for (int i =0 ; i < data_stripes; i++) { + stripe = &sctx->raid56_data_stripes[i]; +scrub_submit_initial_read (sctx, stripe); + } +for (int i =0 ; i < data_stripes; i++) { + stripe = &sctx->raid56_data_stripes[i]; + +wait_event (stripe->repair_wait, +test_bit (SCRUB_STRIPE_FLAG_REPAIR_DONE , + &stripe->state)); + } +/* For now, no zoned support for RAID56. */ +ASSERT (!btrfs_is_zoned (sctx->fs_info)); + +/* Writeback for the repaired sectors. */ +for (int i =0 ; i < data_stripes; i++) { +unsigned long repaired; + + stripe = &sctx->raid56_data_stripes[i]; + +bitmap_andnot (&repaired, &stripe->init_error_bitmap, + &stripe->error_bitmap, stripe->nr_sectors); +scrub_write_sectors (sctx, stripe, repaired, false); + } + +/* Wait for above writebacks to finish. */ +for (int i =0 ; i < data_stripes; i++) { + stripe = &sctx->raid56_data_stripes[i]; + +wait_scrub_stripe_io (stripe); + } + +/* +* Now all data stripes are properly verify. Check if we have any +* unrepaired, if so abort immediate or we can further corrupt the +* P/Q stripes. +* +* During the loop, also populate extent_bitmap. +*/ +for (int i =0 ; i < data_stripes; i++) { +unsigned long error; + + stripe = &sctx->raid56_data_stripes[i]; + +/* +* We should only check the errors where there is an extent. +* As we may hit an empty data stripe while it's missing. +*/ +bitmap_and (&error, &stripe->error_bitmap, + &stripe->extent_sector_bitmap, stripe->nr_sectors); +if (!bitmap_empty (&error, stripe->nr_sectors)) { +btrfs_err (fs_info, +"unrepaired sectors detected, full stripe %llu data stripe %u errors %*pbl" , + full_stripe_start, i, stripe->nr_sectors, + &error); + ret = -EIO ; + goto out; + } +bitmap_or (&extent_bitmap, &extent_bitmap, + &stripe->extent_sector_bitmap, stripe->nr_sectors); + } + +/* Now we can check and regenerate the P/Q stripe. */ + bio =bio_alloc (NULL ,1 ,REQ_OP_READ ,GFP_NOFS ); + bio->bi_iter.bi_sector = full_stripe_start >>SECTOR_SHIFT ; + bio->bi_private = &io_done; + bio->bi_end_io = raid56_scrub_wait_endio; + +btrfs_bio_counter_inc_blocked (fs_info); + ret =btrfs_map_sblock (fs_info,BTRFS_MAP_WRITE , full_stripe_start, + &length, &bioc); +if (ret <0 ) { +btrfs_put_bioc (bioc); +btrfs_bio_counter_dec (fs_info); + goto out; + } + rbio =raid56_parity_alloc_scrub_rbio (bio, bioc, scrub_dev, &extent_bitmap, +BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits); +btrfs_put_bioc (bioc); +if (!rbio) { + ret = -ENOMEM ; +btrfs_bio_counter_dec (fs_info); + goto out; + } +raid56_parity_submit_scrub_rbio (rbio); +wait_for_completion_io (&io_done); + ret =blk_status_to_errno (bio->bi_status); +bio_put (bio); +btrfs_bio_counter_dec (fs_info); + +out: +return ret; +} +/* * Scrub one range which can only has simple mirror based profile. * (Including all range in SINGLE/DUP/RAID1/RAID1C*, and each stripe in
@@ -4398,7 +4573,6 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,/* Offset inside the chunk */ u64 offset;u64 stripe_logical; -u64 stripe_end;int stop_loop =0 ; wait_event(sctx->list_wait,
@@ -4413,6 +4587,25 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, sctx->flush_all_writes = true; } +/* Prepare the extra data stripes used by RAID56. */ +if (profile &BTRFS_BLOCK_GROUP_RAID56_MASK ) { +ASSERT (sctx->raid56_data_stripes ==NULL ); + + sctx->raid56_data_stripes =kcalloc (nr_data_stripes (map), +sizeof (struct scrub_stripe ),GFP_KERNEL ); +if (!sctx->raid56_data_stripes) { + ret = -ENOMEM ; + goto out; + } +for (int i =0 ; i <nr_data_stripes (map); i++) { + ret =init_scrub_stripe (fs_info, + &sctx->raid56_data_stripes[i]); +if (ret <0 ) + goto out; + sctx->raid56_data_stripes[i].bg = bg; + sctx->raid56_data_stripes[i].sctx = sctx; + } + }/* * There used to be a big double loop to handle all profiles using the * same routine, which grows larger and more gross over time.
@@ -4466,10 +4659,8 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,if (ret) {/* it is parity strip */ stripe_logical += chunk_logical; - stripe_end = stripe_logical + increment; - ret =scrub_raid56_parity (sctx, map, scrub_dev, - stripe_logical, - stripe_end); + ret =scrub_raid56_parity_stripe (sctx, scrub_dev, bg, + map, stripe_logical);if (ret) goto out; goto next;
@@ -4507,6 +4698,12 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,scrub_wr_submit (sctx);mutex_unlock (&sctx->wr_lock);flush_scrub_stripes (sctx); +if (sctx->raid56_data_stripes) { +for (int i =0 ; i <nr_data_stripes (map); i++) +release_scrub_stripe (&sctx->raid56_data_stripes[i]); +kfree (sctx->raid56_data_stripes); + sctx->raid56_data_stripes =NULL ; + }if (sctx->is_dev_replace && ret >=0 ) {int ret2;
fs/btrfs/scrub.h +5 -0
@@ -13,4 +13,9 @@ 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); +/* Temporary declaration, would be deleted later. */ +int scrub_raid56_parity (struct scrub_ctx *sctx,struct map_lookup *map, +struct btrfs_device *sdev,u64 logic_start, +u64 logic_end); +#endif
Signature · 1 line
2.39.2
PATCH 2/6 btrfs: scrub: remove scrub_parity structure
In reply to Qu WenruoThe structure scrub_parity is used to indicate that some extents are scrubbed for the purpose of RAID56 P/Q scrubbing.
Since the whole RAID56 P/Q scrubbing path is replaced with new scrub_stripe infrastructure, and we no longer needs to use scrub_parity to modify the behavior of data stripes, we can remove the structure completely.
This removal involves:
- scrub_parity_workers
Now only one worker would be utilized, scrub_workers, to do the read and repair.
All writeback would happen at the main scrub thread.
- scrub_block::sparity member
- scrub_parity structure
- function scrub_parity_get()
- function scrub_parity_put()
- function scrub_free_parity()
- function __scrub_mark_bitmap()
- function scrub_parity_mark_sectors_error()
- function scrub_parity_mark_sectors_data()
Those helpers are no longer needed, scrub_stripe has its bitmaps and we can use bitmap helpers to get the error/data status.
- scrub_parity_bio_endio()
- scrub_parity_check_and_repair()
- function scrub_sectors_for_parity()
- function scrub_extent_for_parity()
- function scrub_raid56_data_stripe_for_parity()
- function scrub_raid56_parity()
The new code would reuse the scrub read-repair and writeback path. Just skip the dev-replace phase.
And scrub_stripe infrastructure allows us to submit and wait for those data stripes before scrubbing P/Q, without extra infrastructures.
The following two functions are temporary exported for later cleanup:
- scrub_find_csum()
- scrub_add_sector_to_rd_bio()
--- fs/btrfs/fs.h | 1 - fs/btrfs/scrub.c | 526 +---------------------------------------------- fs/btrfs/scrub.h | 8 +- 3 files changed, 10 insertions(+), 525 deletions(-)
fs/btrfs/fs.h +0 -1
@@ -633,7 +633,6 @@ struct btrfs_fs_info {refcount_t scrub_workers_refcnt;struct workqueue_struct *scrub_workers;struct workqueue_struct *scrub_wr_completion_workers; -struct workqueue_struct *scrub_parity_workers;struct btrfs_subpage_info *subpage_info;struct btrfs_discard_ctl discard_ctl;
fs/btrfs/scrub.c +5 -521
636 lines not on this page · raw message
fs/btrfs/scrub.h +5 -3
@@ -14,8 +14,10 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid, struct btrfs_scrub_progress *progress);/* Temporary declaration, would be deleted later. */ -int scrub_raid56_parity (struct scrub_ctx *sctx,struct map_lookup *map, -struct btrfs_device *sdev,u64 logic_start, -u64 logic_end); +struct scrub_ctx ; +struct scrub_sector ; +int scrub_find_csum (struct scrub_ctx *sctx,u64 logical,u8 *csum); +int scrub_add_sector_to_rd_bio (struct scrub_ctx *sctx, +struct scrub_sector *sector);#endif
Signature · 1 line
2.39.2
PATCH 3/6 btrfs: scrub: remove the old writeback infrastructure
In reply to Qu WenruoSince the whole scrub path is switched to scrub_stripe based solution, the old writeback path can be removed completely, which involves:
- scrub_ctx::wr_curr_bio member
- scrub_ctx::flush_all_writes member
- function scrub_write_block_to_dev_replace()
- function scrub_write_sector_to_dev_replace()
- function scrub_add_sector_to_wr_bio()
- function scrub_wr_submit()
- function scrub_wr_bio_end_io()
- function scrub_wr_bio_end_io_worker()
And one more function needs to be exported temporarily:
- scrub_sector_get()
--- fs/btrfs/scrub.c | 221 +---------------------------------------------- fs/btrfs/scrub.h | 1 + 2 files changed, 3 insertions(+), 219 deletions(-)
fs/btrfs/scrub.c +2 -219
@@ -275,10 +275,8 @@ struct scrub_ctx {int is_dev_replace;u64 write_pointer; -struct scrub_bio *wr_curr_bio;struct mutex wr_lock;struct btrfs_device *wr_tgtdev; -bool flush_all_writes;/* * statistics
@@ -547,23 +545,14 @@ static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,static int scrub_repair_sector_from_good_copy (struct scrub_block *sblock_bad,struct scrub_block *sblock_good,int sector_num,int force_write); -static void scrub_write_block_to_dev_replace (struct scrub_block *sblock); -static int scrub_write_sector_to_dev_replace (struct scrub_block *sblock, -int sector_num);static int scrub_checksum_data (struct scrub_block *sblock);static int scrub_checksum_tree_block (struct scrub_block *sblock);static int scrub_checksum_super (struct scrub_block *sblock);static void scrub_block_put (struct scrub_block *sblock); -static void scrub_sector_get (struct scrub_sector *sector);static void scrub_sector_put (struct scrub_sector *sector);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); -static int scrub_add_sector_to_wr_bio (struct scrub_ctx *sctx, -struct scrub_sector *sector); -static void scrub_wr_submit (struct scrub_ctx *sctx); -static void scrub_wr_bio_end_io (struct bio *bio); -static void scrub_wr_bio_end_io_worker (struct work_struct *work);static void scrub_put_ctx (struct scrub_ctx *sctx);static inline int scrub_is_page_on_raid56 (struct scrub_sector *sector)
@@ -872,7 +861,6 @@ static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)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); }
@@ -934,13 +922,10 @@ static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(init_waitqueue_head (&sctx->list_wait); sctx->throttle_deadline =0 ; -WARN_ON (sctx->wr_curr_bio !=NULL );mutex_init (&sctx->wr_lock); - sctx->wr_curr_bio =NULL ;if (is_dev_replace) {WARN_ON (!fs_info->dev_replace.tgtdev); sctx->wr_tgtdev = fs_info->dev_replace.tgtdev; - sctx->flush_all_writes = false; }return sctx;
@@ -1306,8 +1291,6 @@ static int scrub_handle_errored_block(struct scrub_block *sblock_to_check) sblock_to_check->data_corrected =1 ;spin_unlock (&sctx->stat_lock); -if (sctx->is_dev_replace) -scrub_write_block_to_dev_replace (sblock_bad); goto out; }
@@ -1396,7 +1379,6 @@ static int scrub_handle_errored_block(struct scrub_block *sblock_to_check) !sblock_other->checksum_error && sblock_other->no_io_error_seen) {if (sctx->is_dev_replace) { -scrub_write_block_to_dev_replace (sblock_other); goto corrected_error; }else { ret = scrub_repair_block_from_good_copy(
@@ -1478,13 +1460,6 @@ static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)*/ if (!sblock_other) sblock_other = sblock_bad; - -if (scrub_write_sector_to_dev_replace (sblock_other, - sector_num) !=0 ) { -atomic64_inc ( - &fs_info->dev_replace.num_write_errors); - success =0 ; - } }else if (sblock_other) { ret = scrub_repair_sector_from_good_copy(sblock_bad, sblock_other,
@@ -1906,31 +1881,6 @@ static int scrub_repair_sector_from_good_copy(struct scrub_block *sblock_bad,return 0 ; } -static void scrub_write_block_to_dev_replace (struct scrub_block *sblock) -{ -struct btrfs_fs_info *fs_info = sblock->sctx->fs_info; -int i; - -for (i =0 ; i < sblock->sector_count; i++) { -int ret; - - ret =scrub_write_sector_to_dev_replace (sblock, i); -if (ret) -atomic64_inc (&fs_info->dev_replace.num_write_errors); - } -} - -static int scrub_write_sector_to_dev_replace (struct scrub_block *sblock,int sector_num) -{ -const u32 sectorsize = sblock->sctx->fs_info->sectorsize; -struct scrub_sector *sector = sblock->sectors[sector_num]; - -if (sector->io_error) -memset (scrub_sector_get_kaddr (sector),0 , sectorsize); - -return scrub_add_sector_to_wr_bio (sblock->sctx, sector); -} -static int fill_writer_pointer_gap (struct scrub_ctx *sctx,u64 physical) {int ret =0 ;
@@ -1958,150 +1908,6 @@ static void scrub_block_get(struct scrub_block *sblock)refcount_inc (&sblock->refs); } -static int scrub_add_sector_to_wr_bio (struct scrub_ctx *sctx, -struct scrub_sector *sector) -{ -struct scrub_block *sblock = sector->sblock; -struct scrub_bio *sbio; -int ret; -const u32 sectorsize = sctx->fs_info->sectorsize; - -mutex_lock (&sctx->wr_lock); -again: -if (!sctx->wr_curr_bio) { - sctx->wr_curr_bio =kzalloc (sizeof (*sctx->wr_curr_bio), -GFP_KERNEL ); -if (!sctx->wr_curr_bio) { -mutex_unlock (&sctx->wr_lock); -return -ENOMEM ; - } - sctx->wr_curr_bio->sctx = sctx; - sctx->wr_curr_bio->sector_count =0 ; - } - sbio = sctx->wr_curr_bio; -if (sbio->sector_count ==0 ) { - ret =fill_writer_pointer_gap (sctx, sector->offset + - sblock->physical_for_dev_replace); -if (ret) { -mutex_unlock (&sctx->wr_lock); -return ret; - } - - sbio->physical = sblock->physical_for_dev_replace + sector->offset; - sbio->logical = sblock->logical + sector->offset; - sbio->dev = sctx->wr_tgtdev; -if (!sbio->bio) { - sbio->bio =bio_alloc (sbio->dev->bdev, sctx->sectors_per_bio, -REQ_OP_WRITE ,GFP_NOFS ); - } - sbio->bio->bi_private = sbio; - sbio->bio->bi_end_io = scrub_wr_bio_end_io; - sbio->bio->bi_iter.bi_sector = sbio->physical >>9 ; - sbio->status =0 ; - }else if (sbio->physical + sbio->sector_count * sectorsize != - sblock->physical_for_dev_replace + sector->offset || - sbio->logical + sbio->sector_count * sectorsize != - sblock->logical + sector->offset) { -scrub_wr_submit (sctx); - goto again; - } - - ret =bio_add_scrub_sector (sbio->bio, sector, sectorsize); -if (ret != sectorsize) { -if (sbio->sector_count <1 ) { -bio_put (sbio->bio); - sbio->bio =NULL ; -mutex_unlock (&sctx->wr_lock); -return -EIO ; - } -scrub_wr_submit (sctx); - goto again; - } - - sbio->sectors[sbio->sector_count] = sector; -scrub_sector_get (sector); -/* -* Since ssector no longer holds a page, but uses sblock::pages, we -* have to ensure the sblock had not been freed before our write bio -* finished. -*/ -scrub_block_get (sector->sblock); - - sbio->sector_count++; -if (sbio->sector_count == sctx->sectors_per_bio) -scrub_wr_submit (sctx); -mutex_unlock (&sctx->wr_lock); - -return 0 ; -} - -static void scrub_wr_submit (struct scrub_ctx *sctx) -{ -struct scrub_bio *sbio; - -if (!sctx->wr_curr_bio) -return ; - - sbio = sctx->wr_curr_bio; - sctx->wr_curr_bio =NULL ; -scrub_pending_bio_inc (sctx); -/* process all writes in a single worker thread. Then the block layer -* orders the requests before sending them to the driver which -* doubled the write performance on spinning disks when measured -* with Linux 3.5 */ -btrfsic_check_bio (sbio->bio); -submit_bio (sbio->bio); - -if (btrfs_is_zoned (sctx->fs_info)) - sctx->write_pointer = sbio->physical + sbio->sector_count * - sctx->fs_info->sectorsize; -} - -static void scrub_wr_bio_end_io (struct bio *bio) -{ -struct scrub_bio *sbio = bio->bi_private; -struct btrfs_fs_info *fs_info = sbio->dev->fs_info; - - sbio->status = bio->bi_status; - sbio->bio = bio; - -INIT_WORK (&sbio->work, scrub_wr_bio_end_io_worker); -queue_work (fs_info->scrub_wr_completion_workers, &sbio->work); -} - -static void scrub_wr_bio_end_io_worker (struct work_struct *work) -{ - struct scrub_bio *sbio = container_of(work, struct scrub_bio, work); -struct scrub_ctx *sctx = sbio->sctx; -int i; - -ASSERT (sbio->sector_count <=SCRUB_SECTORS_PER_BIO ); -if (sbio->status) { -struct btrfs_dev_replace *dev_replace = - &sbio->sctx->fs_info->dev_replace; - -for (i =0 ; i < sbio->sector_count; i++) { -struct scrub_sector *sector = sbio->sectors[i]; - - sector->io_error =1 ; -atomic64_inc (&dev_replace->num_write_errors); - } - } - -/* -* In scrub_add_sector_to_wr_bio() we grab extra ref for sblock, now in -* endio we should put the sblock. -*/ -for (i =0 ; i < sbio->sector_count; i++) { -scrub_block_put (sbio->sectors[i]->sblock); -scrub_sector_put (sbio->sectors[i]); - } - -bio_put (sbio->bio); -kfree (sbio); -scrub_pending_bio_dec (sctx); -} -static int scrub_checksum (struct scrub_block *sblock) {u64 flags;
@@ -2927,7 +2733,7 @@ static void scrub_block_put(struct scrub_block *sblock) } } -static void scrub_sector_get (struct scrub_sector *sector) +void scrub_sector_get (struct scrub_sector *sector) {atomic_inc (§or->refs); }
@@ -3130,21 +2936,12 @@ static void scrub_bio_end_io_worker(struct work_struct *work) sctx->first_free = sbio->index;spin_unlock (&sctx->list_lock); -if (sctx->is_dev_replace && sctx->flush_all_writes) { -mutex_lock (&sctx->wr_lock); -scrub_wr_submit (sctx); -mutex_unlock (&sctx->wr_lock); - } -scrub_pending_bio_dec (sctx); }static void scrub_block_complete (struct scrub_block *sblock) { -int corrupted =0 ; -if (!sblock->no_io_error_seen) { - corrupted =1 ;scrub_handle_errored_block (sblock); }else {/*
@@ -3152,9 +2949,7 @@ static void scrub_block_complete(struct scrub_block *sblock)* dev replace case, otherwise write here in dev replace * case. */ - corrupted =scrub_checksum (sblock); -if (!corrupted && sblock->sctx->is_dev_replace) -scrub_write_block_to_dev_replace (sblock); +scrub_checksum (sblock); } }
@@ -3937,14 +3732,11 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx,/* Paused? */ if (atomic_read (&fs_info->scrub_pause_req)) {/* Push queued extents */ - 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 ); - sctx->flush_all_writes = false;scrub_blocked_if_needed (fs_info); }/* Block group removed? */
@@ -4081,7 +3873,6 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,mutex_lock (&sctx->wr_lock); sctx->write_pointer = physical;mutex_unlock (&sctx->wr_lock); - sctx->flush_all_writes = true; }/* Prepare the extra data stripes used by RAID56. */
@@ -4191,9 +3982,6 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, out:/* push queued extents */ scrub_submit (sctx); -mutex_lock (&sctx->wr_lock); -scrub_wr_submit (sctx); -mutex_unlock (&sctx->wr_lock);flush_scrub_stripes (sctx);if (sctx->raid56_data_stripes) {for (int i =0 ; i <nr_data_stripes (map); i++)
@@ -4529,11 +4317,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,* write requests are really completed when bios_in_flight * changes to 0. */ - 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 );
@@ -4547,7 +4331,6 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,*/ wait_event (sctx->list_wait,atomic_read (&sctx->workers_pending) ==0 ); - sctx->flush_all_writes = false;scrub_pause_off (fs_info);
fs/btrfs/scrub.h +1 -0
@@ -19,5 +19,6 @@ struct scrub_sector;int scrub_find_csum (struct scrub_ctx *sctx,u64 logical,u8 *csum);int scrub_add_sector_to_rd_bio (struct scrub_ctx *sctx,struct scrub_sector *sector); +void scrub_sector_get (struct scrub_sector *sector);#endif
Signature · 1 line
2.39.2
PATCH 4/6 btrfs: scrub: remove the old scrub recheck code
In reply to Qu WenruoThe old scrub code has different entrance to verify the content, and since we have removed the writeback path, now we can start removing the re-check part, including:
- scrub_recover structure
- scrub_sector::recover member
- function scrub_setup_recheck_block()
- function scrub_recheck_block()
- function scrub_recheck_block_checksum()
- function scrub_repair_block_group_good_copy()
- function scrub_repair_sector_from_good_copy()
- function scrub_is_page_on_raid56()
- function full_stripe_lock()
- function search_full_stripe_lock()
- function get_full_stripe_logical()
- function insert_full_stripe_lock()
- function lock_full_stripe()
- function unlock_full_stripe()
- btrfs_block_group::full_stripe_locks_root member
- btrfs_full_stripe_locks_tree structure
This infrastructure is to ensure RAID56 scrub is properly handling recovery and P/Q scrub correctly.
This is no longer needed, before P/Q scrub we will wait for all the involved data stripes to be scrubbed first, and RAID56 code has internal lock to ensure no race in the same full stripe.
- function scrub_print_warning()
- function scrub_get_recover()
- function scrub_put_recover()
- function scrub_handle_errored_block()
- function scrub_setup_recheck_block()
- function scrub_bio_wait_endio()
- function scrub_submit_raid56_bio_wait()
- function scrub_recheck_block_on_raid56()
- function scrub_recheck_block()
- function scrub_recheck_block_checksum()
- function scrub_repair_block_from_good_copy()
- function scrub_repair_sector_from_good_copy()
And two more functions exported temporarily for later cleanup:
- alloc_scrub_sector()
- alloc_scrub_block()
--- fs/btrfs/block-group.c | 11 - fs/btrfs/block-group.h | 8 - fs/btrfs/scrub.c | 997 +---------------------------------------- fs/btrfs/scrub.h | 7 + 4 files changed, 14 insertions(+), 1009 deletions(-)
fs/btrfs/block-group.c +0 -11
@@ -160,15 +160,6 @@ void btrfs_put_block_group(struct btrfs_block_group *cache)btrfs_discard_cancel_work (&cache->fs_info->discard_ctl, cache); -/* -* If not empty, someone is still holding mutex of -* full_stripe_lock, which can only be released by caller. -* And it will definitely cause use-after-free when caller -* tries to release full stripe lock. -* -* No better way to resolve, but only to warn. -*/ -WARN_ON (!RB_EMPTY_ROOT (&cache->full_stripe_locks_root.root));kfree (cache->free_space_ctl);kfree (cache->physical_map);kfree (cache);
@@ -2124,8 +2115,6 @@ static struct btrfs_block_group *btrfs_create_block_group_cache(btrfs_init_free_space_ctl (cache, cache->free_space_ctl);atomic_set (&cache->frozen,0 );mutex_init (&cache->free_space_lock); - cache->full_stripe_locks_root.root =RB_ROOT ; -mutex_init (&cache->full_stripe_locks_root.lock);return cache; }
fs/btrfs/block-group.h +0 -8
@@ -94,11 +94,6 @@ struct btrfs_caching_control {/* * Tree to record all locked full stripes of a RAID5/6 block group */ -struct btrfs_full_stripe_locks_tree { -struct rb_root root; -struct mutex lock; -}; -struct btrfs_block_group {struct btrfs_fs_info *fs_info;struct inode *inode;
@@ -229,9 +224,6 @@ struct btrfs_block_group {*/ int swap_extents; -/* Record locked full stripes for RAID5/6 block group */ -struct btrfs_full_stripe_locks_tree full_stripe_locks_root; -/* * Allocation offset for the block group to implement sequential * allocation. This is used only on a zoned filesystem.
fs/btrfs/scrub.c +7 -990
1,081 lines not on this page · raw message
fs/btrfs/scrub.h +7 -0
@@ -16,9 +16,16 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,/* Temporary declaration, would be deleted later. */ struct scrub_ctx ;struct scrub_sector ; +struct scrub_block ;int scrub_find_csum (struct scrub_ctx *sctx,u64 logical,u8 *csum);int scrub_add_sector_to_rd_bio (struct scrub_ctx *sctx,struct scrub_sector *sector);void scrub_sector_get (struct scrub_sector *sector); +struct scrub_sector *alloc_scrub_sector (struct scrub_block *sblock,u64 logical); +struct scrub_block *alloc_scrub_block (struct scrub_ctx *sctx, +struct btrfs_device *dev, +u64 logical,u64 physical, +u64 physical_for_dev_replace, +int mirror_num);#endif
Signature · 1 line
2.39.2
PATCH 5/6 btrfs: scrub: remove scrub_block and scrub_sector structures
In reply to Qu WenruoThose two structures are used to represent a bunch of sectors for scrub, but now they are fully replaced by scrub_stripe in one go, so we can remove them now. This involves:
- structure scrub_block
- structure scrub_sector
- structure scrub_page_private
- function attach_scrub_page_private()
- function detach_scrub_page_private()
Now we no longer need to use page::private to handle subpage.
- function alloc_scrub_block()
- function alloc_scrub_sector()
- function scrub_sector_get_page()
- function scrub_sector_get_page_offset()
- function scrub_sector_get_kaddr()
- function bio_add_scrub_sector()
- function scrub_checksum_data()
- function scrub_checksum_tree_block()
- function scrub_checksum_super()
- function scrub_check_fsid()
- function scrub_block_get()
- function scrub_block_put()
- function scrub_sector_get()
- function scrub_sector_put()
- function scrub_bio_end_io()
- function scrub_block_complete()
- function scrub_add_sector_to_rd_bio()
--- fs/btrfs/scrub.c | 564 ----------------------------------------------- fs/btrfs/scrub.h | 10 - 2 files changed, 574 deletions(-)
fs/btrfs/scrub.c +0 -564
@@ -38,7 +38,6 @@* - add a mode to also read unallocated space */ -struct scrub_block ;struct scrub_ctx ;/*
@@ -183,19 +182,6 @@ struct scrub_stripe {struct work_struct work; }; -struct scrub_sector { -struct scrub_block *sblock; -struct list_head list; -u64 flags;/* extent flags */ -u64 generation; -/* Offset in bytes to @sblock. */ -u32 offset; -atomic_t refs; -unsigned int have_csum:1 ; -unsigned int io_error:1 ; -u8 csum[BTRFS_CSUM_SIZE ]; -}; -struct scrub_bio {int index;struct scrub_ctx *sctx;
@@ -204,45 +190,11 @@ struct scrub_bio {blk_status_t status;u64 logical;u64 physical; -struct scrub_sector *sectors[SCRUB_SECTORS_PER_BIO ];int sector_count;int next_free;struct work_struct work; }; -struct scrub_block { -/* -* Each page will have its page::private used to record the logical -* bytenr. -*/ -struct page *pages[SCRUB_MAX_PAGES ]; -struct scrub_sector *sectors[SCRUB_MAX_SECTORS_PER_BLOCK ]; -struct btrfs_device *dev; -/* Logical bytenr of the sblock */ -u64 logical; -u64 physical; -u64 physical_for_dev_replace; -/* Length of sblock in bytes */ -u32 len; -int sector_count; -int mirror_num; - -atomic_t outstanding_sectors; -refcount_t refs;/* free mem on transition to zero */ -struct scrub_ctx *sctx; -struct { -unsigned int header_error:1 ; -unsigned int checksum_error:1 ; -unsigned int no_io_error_seen:1 ; -unsigned int generation_error:1 ;/* also sets header_error */ - -/* The following is for the data used to check parity */ -/* It is for the data with checksum */ -unsigned int data_corrected:1 ; - }; -struct work_struct work; -}; -struct scrub_ctx {struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX ];struct scrub_stripe stripes[SCRUB_STRIPES_PER_SCTX ];
@@ -295,44 +247,6 @@ struct scrub_warning {struct btrfs_device *dev; }; -#ifndef CONFIG_64BIT -/* This structure is for architectures whose (void *) is smaller than u64 */ -struct scrub_page_private { -u64 logical; -}; -#endif - -static int attach_scrub_page_private (struct page *page,u64 logical) -{ -#ifdef CONFIG_64BIT -attach_page_private (page, (void *)logical); -return 0 ; -#else -struct scrub_page_private *spp; - - spp =kmalloc (sizeof (*spp),GFP_KERNEL ); -if (!spp) -return -ENOMEM ; - spp->logical = logical; -attach_page_private (page, (void *)spp); -return 0 ; -#endif -} - -static void detach_scrub_page_private (struct page *page) -{ -#ifdef CONFIG_64BIT -detach_page_private (page); -return ; -#else -struct scrub_page_private *spp; - - spp =detach_page_private (page); -kfree (spp); -return ; -#endif -} -static void release_scrub_stripe (struct scrub_stripe *stripe) {if (!stripe)
@@ -391,141 +305,7 @@ static void wait_scrub_stripe_io(struct scrub_stripe *stripe)wait_event (stripe->io_wait,atomic_read (&stripe->pending_io) ==0 ); } -struct scrub_block *alloc_scrub_block (struct scrub_ctx *sctx, -struct btrfs_device *dev, -u64 logical,u64 physical, -u64 physical_for_dev_replace, -int mirror_num) -{ -struct scrub_block *sblock; - - sblock =kzalloc (sizeof (*sblock),GFP_KERNEL ); -if (!sblock) -return NULL ; -refcount_set (&sblock->refs,1 ); - sblock->sctx = sctx; - sblock->logical = logical; - sblock->physical = physical; - sblock->physical_for_dev_replace = physical_for_dev_replace; - sblock->dev = dev; - sblock->mirror_num = mirror_num; - sblock->no_io_error_seen =1 ; -/* -* Scrub_block::pages will be allocated at alloc_scrub_sector() when -* the corresponding page is not allocated. -*/ -return sblock; -} - -/* -* Allocate a new scrub sector and attach it to @sblock. -* -* Will also allocate new pages for @sblock if needed. -*/ -struct scrub_sector *alloc_scrub_sector (struct scrub_block *sblock,u64 logical) -{ -const pgoff_t page_index = (logical - sblock->logical) >>PAGE_SHIFT ; -struct scrub_sector *ssector; - -/* We must never have scrub_block exceed U32_MAX in size. */ -ASSERT (logical - sblock->logical <U32_MAX ); - - ssector =kzalloc (sizeof (*ssector),GFP_KERNEL ); -if (!ssector) -return NULL ; - -/* Allocate a new page if the slot is not allocated */ -if (!sblock->pages[page_index]) { -int ret; - - sblock->pages[page_index] =alloc_page (GFP_KERNEL ); -if (!sblock->pages[page_index]) { -kfree (ssector); -return NULL ; - } - ret =attach_scrub_page_private (sblock->pages[page_index], - sblock->logical + (page_index <<PAGE_SHIFT )); -if (ret <0 ) { -kfree (ssector); -__free_page (sblock->pages[page_index]); - sblock->pages[page_index] =NULL ; -return NULL ; - } - } - -atomic_set (&ssector->refs,1 ); - ssector->sblock = sblock; -/* The sector to be added should not be used */ -ASSERT (sblock->sectors[sblock->sector_count] ==NULL ); - ssector->offset = logical - sblock->logical; - -/* The sector count must be smaller than the limit */ -ASSERT (sblock->sector_count <SCRUB_MAX_SECTORS_PER_BLOCK ); - - sblock->sectors[sblock->sector_count] = ssector; - sblock->sector_count++; - sblock->len += sblock->sctx->fs_info->sectorsize; - -return ssector; -} - -static struct page *scrub_sector_get_page (struct scrub_sector *ssector) -{ -struct scrub_block *sblock = ssector->sblock; -pgoff_t index; -/* -* When calling this function, ssector must be alreaday attached to the -* parent sblock. -*/ -ASSERT (sblock); - -/* The range should be inside the sblock range */ -ASSERT (ssector->offset < sblock->len); - - index = ssector->offset >>PAGE_SHIFT ; -ASSERT (index <SCRUB_MAX_PAGES ); -ASSERT (sblock->pages[index]); -ASSERT (PagePrivate (sblock->pages[index])); -return sblock->pages[index]; -} - -static unsigned int scrub_sector_get_page_offset (struct scrub_sector *ssector) -{ -struct scrub_block *sblock = ssector->sblock; - -/* -* When calling this function, ssector must be already attached to the -* parent sblock. -*/ -ASSERT (sblock); - -/* The range should be inside the sblock range */ -ASSERT (ssector->offset < sblock->len); - -return offset_in_page (ssector->offset); -} - -static char *scrub_sector_get_kaddr (struct scrub_sector *ssector) -{ -return page_address (scrub_sector_get_page (ssector)) + -scrub_sector_get_page_offset (ssector); -} - -static int bio_add_scrub_sector (struct bio *bio,struct scrub_sector *ssector, -unsigned int len) -{ -return bio_add_page (bio,scrub_sector_get_page (ssector), len, -scrub_sector_get_page_offset (ssector)); -} - -static int scrub_checksum_data (struct scrub_block *sblock); -static int scrub_checksum_tree_block (struct scrub_block *sblock); -static int scrub_checksum_super (struct scrub_block *sblock); -static void scrub_block_put (struct scrub_block *sblock); -static void scrub_sector_put (struct scrub_sector *sector); -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);static void scrub_put_ctx (struct scrub_ctx *sctx);static void scrub_pending_bio_inc (struct scrub_ctx *sctx)
@@ -595,8 +375,6 @@ static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)if (sctx->curr !=-1 ) {struct scrub_bio *sbio = sctx->bios[sctx->curr]; -for (i =0 ; i < sbio->sector_count; i++) -scrub_block_put (sbio->sectors[i]->sblock);bio_put (sbio->bio); }
@@ -895,15 +673,6 @@ static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type, } } -static inline int scrub_check_fsid (u8 fsid[],struct scrub_sector *sector) -{ -struct btrfs_fs_devices *fs_devices = sector->sblock->dev->fs_devices; -int ret; - - ret =memcmp (fsid, fs_devices->fsid,BTRFS_FSID_SIZE ); -return !ret; -} -static int fill_writer_pointer_gap (struct scrub_ctx *sctx,u64 physical) {int ret =0 ;
@@ -926,68 +695,6 @@ static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)return ret; } -static void scrub_block_get (struct scrub_block *sblock) -{ -refcount_inc (&sblock->refs); -} - -static int scrub_checksum (struct scrub_block *sblock) -{ -u64 flags; -int ret; - -/* -* No need to initialize these stats currently, -* because this function only use return value -* instead of these stats value. -* -* Todo: -* always use stats -*/ - sblock->header_error =0 ; - sblock->generation_error =0 ; - sblock->checksum_error =0 ; - -WARN_ON (sblock->sector_count <1 ); - flags = sblock->sectors[0 ]->flags; - ret =0 ; -if (flags &BTRFS_EXTENT_FLAG_DATA ) - ret =scrub_checksum_data (sblock); -else if (flags &BTRFS_EXTENT_FLAG_TREE_BLOCK ) - ret =scrub_checksum_tree_block (sblock); -else if (flags &BTRFS_EXTENT_FLAG_SUPER ) - ret =scrub_checksum_super (sblock); -else -WARN_ON (1 ); -return ret; -} - -static int scrub_checksum_data (struct scrub_block *sblock) -{ -struct scrub_ctx *sctx = sblock->sctx; -struct btrfs_fs_info *fs_info = sctx->fs_info; -SHASH_DESC_ON_STACK (shash, fs_info->csum_shash); -u8 csum[BTRFS_CSUM_SIZE ]; -struct scrub_sector *sector; -char *kaddr; - -BUG_ON (sblock->sector_count <1 ); - sector = sblock->sectors[0 ]; -if (!sector->have_csum) -return 0 ; - - kaddr =scrub_sector_get_kaddr (sector); - - shash->tfm = fs_info->csum_shash; -crypto_shash_init (shash); - -crypto_shash_digest (shash, kaddr, fs_info->sectorsize, csum); - -if (memcmp (csum, sector->csum, fs_info->csum_size)) - sblock->checksum_error =1 ; -return sblock->checksum_error; -} -static struct page *scrub_stripe_get_page (struct scrub_stripe *stripe,int sector_nr) {
@@ -1602,168 +1309,6 @@ static void scrub_write_sectors(struct scrub_ctx *sctx, } } -static int scrub_checksum_tree_block (struct scrub_block *sblock) -{ -struct scrub_ctx *sctx = sblock->sctx; -struct btrfs_header *h; -struct btrfs_fs_info *fs_info = sctx->fs_info; -SHASH_DESC_ON_STACK (shash, fs_info->csum_shash); -u8 calculated_csum[BTRFS_CSUM_SIZE ]; -u8 on_disk_csum[BTRFS_CSUM_SIZE ]; -/* -* This is done in sectorsize steps even for metadata as there's a -* constraint for nodesize to be aligned to sectorsize. This will need -* to change so we don't misuse data and metadata units like that. -*/ -const u32 sectorsize = sctx->fs_info->sectorsize; -const int num_sectors = fs_info->nodesize >> fs_info->sectorsize_bits; -int i; -struct scrub_sector *sector; -char *kaddr; - -BUG_ON (sblock->sector_count <1 ); - -/* Each member in sectors is just one sector */ -ASSERT (sblock->sector_count == num_sectors); - - sector = sblock->sectors[0 ]; - kaddr =scrub_sector_get_kaddr (sector); - h = (struct btrfs_header *)kaddr; -memcpy (on_disk_csum, h->csum, sctx->fs_info->csum_size); - -/* -* we don't use the getter functions here, as we -* a) don't have an extent buffer and -* b) the page is already kmapped -*/ -if (sblock->logical !=btrfs_stack_header_bytenr (h)) { - sblock->header_error =1 ; -btrfs_warn_rl (fs_info, -"tree block %llu mirror %u has bad bytenr, has %llu want %llu" , - sblock->logical, sblock->mirror_num, -btrfs_stack_header_bytenr (h), - sblock->logical); - goto out; - } - -if (!scrub_check_fsid (h->fsid, sector)) { - sblock->header_error =1 ; -btrfs_warn_rl (fs_info, -"tree block %llu mirror %u has bad fsid, has %pU want %pU" , - sblock->logical, sblock->mirror_num, - h->fsid, sblock->dev->fs_devices->fsid); - goto out; - } - -if (memcmp (h->chunk_tree_uuid, fs_info->chunk_tree_uuid,BTRFS_UUID_SIZE )) { - sblock->header_error =1 ; -btrfs_warn_rl (fs_info, -"tree block %llu mirror %u has bad chunk tree uuid, has %pU want %pU" , - sblock->logical, sblock->mirror_num, - h->chunk_tree_uuid, fs_info->chunk_tree_uuid); - goto out; - } - - shash->tfm = fs_info->csum_shash; -crypto_shash_init (shash); -crypto_shash_update (shash, kaddr +BTRFS_CSUM_SIZE , - sectorsize -BTRFS_CSUM_SIZE ); - -for (i =1 ; i < num_sectors; i++) { - kaddr =scrub_sector_get_kaddr (sblock->sectors[i]); -crypto_shash_update (shash, kaddr, sectorsize); - } - -crypto_shash_final (shash, calculated_csum); -if (memcmp (calculated_csum, on_disk_csum, sctx->fs_info->csum_size)) { - sblock->checksum_error =1 ; -btrfs_warn_rl (fs_info, -"tree block %llu mirror %u has bad csum, has " CSUM_FMT " want " CSUM_FMT , - sblock->logical, sblock->mirror_num, -CSUM_FMT_VALUE (fs_info->csum_size, on_disk_csum), -CSUM_FMT_VALUE (fs_info->csum_size, calculated_csum)); - goto out; - } - -if (sector->generation !=btrfs_stack_header_generation (h)) { - sblock->header_error =1 ; - sblock->generation_error =1 ; -btrfs_warn_rl (fs_info, -"tree block %llu mirror %u has bad generation, has %llu want %llu" , - sblock->logical, sblock->mirror_num, -btrfs_stack_header_generation (h), - sector->generation); - } - -out: -return sblock->header_error || sblock->checksum_error; -} - -static int scrub_checksum_super (struct scrub_block *sblock) -{ -struct btrfs_super_block *s; -struct scrub_ctx *sctx = sblock->sctx; -struct btrfs_fs_info *fs_info = sctx->fs_info; -SHASH_DESC_ON_STACK (shash, fs_info->csum_shash); -u8 calculated_csum[BTRFS_CSUM_SIZE ]; -struct scrub_sector *sector; -char *kaddr; -int fail_gen =0 ; -int fail_cor =0 ; - -BUG_ON (sblock->sector_count <1 ); - sector = sblock->sectors[0 ]; - kaddr =scrub_sector_get_kaddr (sector); - s = (struct btrfs_super_block *)kaddr; - -if (sblock->logical !=btrfs_super_bytenr (s)) - ++fail_cor; - -if (sector->generation !=btrfs_super_generation (s)) - ++fail_gen; - -if (!scrub_check_fsid (s->fsid, sector)) - ++fail_cor; - - shash->tfm = fs_info->csum_shash; -crypto_shash_init (shash); -crypto_shash_digest (shash, kaddr +BTRFS_CSUM_SIZE , -BTRFS_SUPER_INFO_SIZE -BTRFS_CSUM_SIZE , calculated_csum); - -if (memcmp (calculated_csum, s->csum, sctx->fs_info->csum_size)) - ++fail_cor; - -return fail_cor + fail_gen; -} - -static void scrub_block_put (struct scrub_block *sblock) -{ -if (refcount_dec_and_test (&sblock->refs)) { -int i; - -for (i =0 ; i < sblock->sector_count; i++) -scrub_sector_put (sblock->sectors[i]); -for (i =0 ; i <DIV_ROUND_UP (sblock->len,PAGE_SIZE ); i++) { -if (sblock->pages[i]) { -detach_scrub_page_private (sblock->pages[i]); -__free_page (sblock->pages[i]); - } - } -kfree (sblock); - } -} - -void scrub_sector_get (struct scrub_sector *sector) -{ -atomic_inc (§or->refs); -} - -static void scrub_sector_put (struct scrub_sector *sector) -{ -if (atomic_dec_and_test (§or->refs)) -kfree (sector); -} -static void scrub_throttle_dev_io (struct scrub_ctx *sctx,struct btrfs_device *device,unsigned int bio_size)
@@ -1844,110 +1389,12 @@ static void scrub_submit(struct scrub_ctx *sctx)submit_bio (sbio->bio); } -int scrub_add_sector_to_rd_bio (struct scrub_ctx *sctx, -struct scrub_sector *sector) -{ -struct scrub_block *sblock = sector->sblock; -struct scrub_bio *sbio; -const u32 sectorsize = sctx->fs_info->sectorsize; -int ret; - -again: -/* -* grab a fresh bio or wait for one to become available -*/ -while (sctx->curr ==-1 ) { -spin_lock (&sctx->list_lock); - sctx->curr = sctx->first_free; -if (sctx->curr !=-1 ) { - sctx->first_free = sctx->bios[sctx->curr]->next_free; - sctx->bios[sctx->curr]->next_free =-1 ; - sctx->bios[sctx->curr]->sector_count =0 ; -spin_unlock (&sctx->list_lock); - }else { -spin_unlock (&sctx->list_lock); -wait_event (sctx->list_wait, sctx->first_free !=-1 ); - } - } - sbio = sctx->bios[sctx->curr]; -if (sbio->sector_count ==0 ) { - sbio->physical = sblock->physical + sector->offset; - sbio->logical = sblock->logical + sector->offset; - sbio->dev = sblock->dev; -if (!sbio->bio) { - sbio->bio =bio_alloc (sbio->dev->bdev, sctx->sectors_per_bio, -REQ_OP_READ ,GFP_NOFS ); - } - sbio->bio->bi_private = sbio; - sbio->bio->bi_end_io = scrub_bio_end_io; - sbio->bio->bi_iter.bi_sector = sbio->physical >>9 ; - sbio->status =0 ; - }else if (sbio->physical + sbio->sector_count * sectorsize != - sblock->physical + sector->offset || - sbio->logical + sbio->sector_count * sectorsize != - sblock->logical + sector->offset || - sbio->dev != sblock->dev) { -scrub_submit (sctx); - goto again; - } - - sbio->sectors[sbio->sector_count] = sector; - ret =bio_add_scrub_sector (sbio->bio, sector, sectorsize); -if (ret != sectorsize) { -if (sbio->sector_count <1 ) { -bio_put (sbio->bio); - sbio->bio =NULL ; -return -EIO ; - } -scrub_submit (sctx); - goto again; - } - -scrub_block_get (sblock);/* one for the page added to the bio */ -atomic_inc (&sblock->outstanding_sectors); - sbio->sector_count++; -if (sbio->sector_count == sctx->sectors_per_bio) -scrub_submit (sctx); - -return 0 ; -} - -static void scrub_bio_end_io (struct bio *bio) -{ -struct scrub_bio *sbio = bio->bi_private; -struct btrfs_fs_info *fs_info = sbio->dev->fs_info; - - sbio->status = bio->bi_status; - sbio->bio = bio; - -queue_work (fs_info->scrub_workers, &sbio->work); -} -static void scrub_bio_end_io_worker (struct work_struct *work) { struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);struct scrub_ctx *sctx = sbio->sctx; -int i;ASSERT (sbio->sector_count <=SCRUB_SECTORS_PER_BIO ); -if (sbio->status) { -for (i =0 ; i < sbio->sector_count; i++) { -struct scrub_sector *sector = sbio->sectors[i]; - - sector->io_error =1 ; - sector->sblock->no_io_error_seen =0 ; - } - } - -/* Now complete the scrub_block items that have all pages completed */ -for (i =0 ; i < sbio->sector_count; i++) { -struct scrub_sector *sector = sbio->sectors[i]; -struct scrub_block *sblock = sector->sblock; - -if (atomic_dec_and_test (&sblock->outstanding_sectors)) -scrub_block_complete (sblock); -scrub_block_put (sblock); - }bio_put (sbio->bio); sbio->bio =NULL ;
@@ -1959,17 +1406,6 @@ static void scrub_bio_end_io_worker(struct work_struct *work)scrub_pending_bio_dec (sctx); } -static void scrub_block_complete (struct scrub_block *sblock) -{ -if (sblock->no_io_error_seen) -/* -* if has checksum error, write via repair mechanism in -* dev replace case, otherwise write here in dev replace -* case. -*/ -scrub_checksum (sblock); -} -static void drop_csum_range (struct scrub_ctx *sctx,struct btrfs_ordered_sum *sum) { sctx->stat.csum_discards += sum->len >> sctx->fs_info->sectorsize_bits;
fs/btrfs/scrub.h +0 -10
@@ -15,17 +15,7 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,/* Temporary declaration, would be deleted later. */ struct scrub_ctx ; -struct scrub_sector ;struct scrub_block ;int scrub_find_csum (struct scrub_ctx *sctx,u64 logical,u8 *csum); -int scrub_add_sector_to_rd_bio (struct scrub_ctx *sctx, -struct scrub_sector *sector); -void scrub_sector_get (struct scrub_sector *sector); -struct scrub_sector *alloc_scrub_sector (struct scrub_block *sblock,u64 logical); -struct scrub_block *alloc_scrub_block (struct scrub_ctx *sctx, -struct btrfs_device *dev, -u64 logical,u64 physical, -u64 physical_for_dev_replace, -int mirror_num);#endif
Signature · 1 line
2.39.2
PATCH 6/6 btrfs: scrub: remove scrub_bio structure
In reply to Qu WenruoSince scrub path has been fully moved to scrub_stripe based facilities, no more scrub_bio would be submitted.
Thus we can remove it completely, this involves:
- SCRUB_SECTORS_PER_BIO macro
- SCRUB_BIOS_PER_SCTX macro
- SCRUB_MAX_PAGES macro
- BTRFS_MAX_MIRRORS macro
- scrub_bio structure
- scrub_ctx::bios member
- scrub_ctx::curr member
- scrub_ctx::bios_in_flight member
- scrub_ctx::workers_pending member
- scrub_ctx::list_lock member
- scrub_ctx::list_wait member
- function scrub_bio_end_io_worker()
- function scrub_pending_bio_inc()
- function scrub_pending_bio_dec()
- function scrub_throttle()
- function scrub_submit()
- function scrub_find_csum()
- function drop_csum_range()
- Some unnecessary flush and scrub pauses
--- fs/btrfs/scrub.c | 245 ++--------------------------------------------- fs/btrfs/scrub.h | 5 - 2 files changed, 6 insertions(+), 244 deletions(-)
fs/btrfs/scrub.c +6 -239
@@ -41,14 +41,10 @@struct scrub_ctx ;/* -* The following three values only influence the performance. +* The following value only influence the performance. * -* The last one configures the number of parallel and outstanding I/O -* operations. The first one configures an upper limit for the number -* of (dynamically allocated) pages that are added to a bio. +* This determines the batch size for stripe submitted in one go. */ -#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. */ /*
@@ -57,19 +53,6 @@ struct scrub_ctx;*/ #define SCRUB_MAX_SECTORS_PER_BLOCK (BTRFS_MAX_METADATA_BLOCKSIZE / SZ_4K) -#define SCRUB_MAX_PAGES (DIV_ROUND_UP(BTRFS_MAX_METADATA_BLOCKSIZE, PAGE_SIZE)) - -/* -* Maximum number of mirrors that can be available for all profiles counting -* the target device of dev-replace as one. During an active device replace -* procedure, the target device of the copy operation is a mirror for the -* filesystem data as well that can be used to read data in order to repair -* read errors on other disks. -* -* Current value is derived from RAID1C4 with 4 copies. -*/ -#define BTRFS_MAX_MIRRORS (4 + 1) -/* Represent one sector and its needed info to verify the content. */ struct scrub_sector_verification {bool is_metadata;
@@ -182,31 +165,12 @@ struct scrub_stripe {struct work_struct work; }; -struct scrub_bio { -int index; -struct scrub_ctx *sctx; -struct btrfs_device *dev; -struct bio *bio; -blk_status_t status; -u64 logical; -u64 physical; -int sector_count; -int next_free; -struct work_struct work; -}; -struct scrub_ctx { -struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX ];struct scrub_stripe stripes[SCRUB_STRIPES_PER_SCTX ];struct scrub_stripe *raid56_data_stripes;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; -wait_queue_head_t list_wait;struct list_head csum_list;atomic_t cancel_req;int readonly;
@@ -305,22 +269,8 @@ static void wait_scrub_stripe_io(struct scrub_stripe *stripe)wait_event (stripe->io_wait,atomic_read (&stripe->pending_io) ==0 ); } -static void scrub_bio_end_io_worker (struct work_struct *work);static void scrub_put_ctx (struct scrub_ctx *sctx); -static void scrub_pending_bio_inc (struct scrub_ctx *sctx) -{ -refcount_inc (&sctx->refs); -atomic_inc (&sctx->bios_in_flight); -} - -static void scrub_pending_bio_dec (struct scrub_ctx *sctx) -{ -atomic_dec (&sctx->bios_in_flight); -wake_up (&sctx->list_wait); -scrub_put_ctx (sctx); -} -static void __scrub_blocked_if_needed (struct btrfs_fs_info *fs_info) {while (atomic_read (&fs_info->scrub_pause_req)) {
@@ -371,21 +321,6 @@ static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)if (!sctx)return ; -/* this can happen when scrub is cancelled */ -if (sctx->curr !=-1 ) { -struct scrub_bio *sbio = sctx->bios[sctx->curr]; - -bio_put (sbio->bio); - } - -for (i =0 ; i <SCRUB_BIOS_PER_SCTX ; ++i) { -struct scrub_bio *sbio = sctx->bios[i]; - -if (!sbio) -break ; -kfree (sbio); - } -for (i =0 ; i <SCRUB_STRIPES_PER_SCTX ; i++)release_scrub_stripe (&sctx->stripes[i]);
@@ -410,28 +345,8 @@ static noinline_for_stack struct scrub_ctx *scrub_setup_ctx( goto nomem;refcount_set (&sctx->refs,1 ); sctx->is_dev_replace = is_dev_replace; - sctx->sectors_per_bio =SCRUB_SECTORS_PER_BIO ; - sctx->curr =-1 ; sctx->fs_info = fs_info;INIT_LIST_HEAD (&sctx->csum_list); -for (i =0 ; i <SCRUB_BIOS_PER_SCTX ; ++i) { -struct scrub_bio *sbio; - - sbio =kzalloc (sizeof (*sbio),GFP_KERNEL ); -if (!sbio) - goto nomem; - sctx->bios[i] = sbio; - - sbio->index = i; - sbio->sctx = sctx; - sbio->sector_count =0 ; -INIT_WORK (&sbio->work, scrub_bio_end_io_worker); - -if (i !=SCRUB_BIOS_PER_SCTX -1 ) - sctx->bios[i]->next_free = i +1 ; -else - sctx->bios[i]->next_free =-1 ; - }for (i =0 ; i <SCRUB_STRIPES_PER_SCTX ; i++) {int ret;
@@ -441,13 +356,9 @@ static noinline_for_stack struct scrub_ctx *scrub_setup_ctx( sctx->stripes[i].sctx = sctx; } sctx->first_free =0 ; -atomic_set (&sctx->bios_in_flight,0 ); -atomic_set (&sctx->workers_pending,0 );atomic_set (&sctx->cancel_req,0 ); -spin_lock_init (&sctx->list_lock);spin_lock_init (&sctx->stat_lock); -init_waitqueue_head (&sctx->list_wait); sctx->throttle_deadline =0 ;mutex_init (&sctx->wr_lock);
@@ -1309,6 +1220,10 @@ static void scrub_write_sectors(struct scrub_ctx *sctx, } } +/* +* 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_dev_io (struct scrub_ctx *sctx,struct btrfs_device *device,unsigned int bio_size)
@@ -1362,112 +1277,6 @@ static void scrub_throttle_dev_io(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; - -if (sctx->curr ==-1 ) -return ; - -scrub_throttle (sctx); - - sbio = sctx->bios[sctx->curr]; - sctx->curr =-1 ; -scrub_pending_bio_inc (sctx); -btrfsic_check_bio (sbio->bio); -submit_bio (sbio->bio); -} - -static void scrub_bio_end_io_worker (struct work_struct *work) -{ - struct scrub_bio *sbio = container_of(work, struct scrub_bio, work); -struct scrub_ctx *sctx = sbio->sctx; - -ASSERT (sbio->sector_count <=SCRUB_SECTORS_PER_BIO ); - -bio_put (sbio->bio); - sbio->bio =NULL ; -spin_lock (&sctx->list_lock); - sbio->next_free = sctx->first_free; - sctx->first_free = sbio->index; -spin_unlock (&sctx->list_lock); - -scrub_pending_bio_dec (sctx); -} - -static void drop_csum_range (struct scrub_ctx *sctx,struct btrfs_ordered_sum *sum) -{ - sctx->stat.csum_discards += sum->len >> sctx->fs_info->sectorsize_bits; -list_del (&sum->list); -kfree (sum); -} - -/* -* Find the desired csum for range [logical, logical + sectorsize), and store -* the csum into @csum. -* -* The search source is sctx->csum_list, which is a pre-populated list -* storing bytenr ordered csum ranges. We're responsible to cleanup any range -* that is before @logical. -* -* Return 0 if there is no csum for the range. -* Return 1 if there is csum for the range and copied to @csum. -*/ -int scrub_find_csum (struct scrub_ctx *sctx,u64 logical,u8 *csum) -{ -bool found = false; - -while (!list_empty (&sctx->csum_list)) { -struct btrfs_ordered_sum *sum =NULL ; -unsigned long index; -unsigned long num_sectors; - - sum =list_first_entry (&sctx->csum_list, - struct btrfs_ordered_sum, list); -/* The current csum range is beyond our range, no csum found */ -if (sum->bytenr > logical) -break ; - -/* -* The current sum is before our bytenr, since scrub is always -* done in bytenr order, the csum will never be used anymore, -* clean it up so that later calls won't bother with the range, -* and continue search the next range. -*/ -if (sum->bytenr + sum->len <= logical) { -drop_csum_range (sctx, sum); -continue ; - } - -/* Now the csum range covers our bytenr, copy the csum */ - found = true; - index = (logical - sum->bytenr) >> sctx->fs_info->sectorsize_bits; - num_sectors = sum->len >> sctx->fs_info->sectorsize_bits; - -memcpy (csum, sum->sums + index * sctx->fs_info->csum_size, - sctx->fs_info->csum_size); - -/* Cleanup the range if we're at the end of the csum range */ -if (index == num_sectors -1 ) -drop_csum_range (sctx, sum); -break ; - } -if (!found) -return 0 ; -return 1 ; -} -/* * Given a physical address, this will calculate it's * logical offset. if this is a parity stripe, it will return
@@ -1648,8 +1457,6 @@ static int sync_write_pointer_for_zoned(struct scrub_ctx *sctx, u64 logical,if (!btrfs_is_zoned (fs_info))return 0 ; -wait_event (sctx->list_wait,atomic_read (&sctx->bios_in_flight) ==0 ); -mutex_lock (&sctx->wr_lock);if (sctx->write_pointer < physical_end) { ret = btrfs_sync_zone_write_pointer(sctx->wr_tgtdev, logical,
@@ -2185,11 +1992,6 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx,/* Paused? */ if (atomic_read (&fs_info->scrub_pause_req)) {/* Push queued extents */ -scrub_submit (sctx); -mutex_lock (&sctx->wr_lock); -mutex_unlock (&sctx->wr_lock); -wait_event (sctx->list_wait, -atomic_read (&sctx->bios_in_flight) ==0 );scrub_blocked_if_needed (fs_info); }/* Block group removed? */
@@ -2317,8 +2119,6 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,u64 stripe_logical;int stop_loop =0 ; -wait_event (sctx->list_wait, -atomic_read (&sctx->bios_in_flight) ==0 );scrub_blocked_if_needed (fs_info); if (sctx->is_dev_replace &&
@@ -2433,8 +2233,6 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,break ; } out: -/* push queued extents */ -scrub_submit (sctx);flush_scrub_stripes (sctx);if (sctx->raid56_data_stripes) {for (int i =0 ; i <nr_data_stripes (map); i++)
@@ -2759,34 +2557,6 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx, ret =scrub_chunk (sctx, cache, scrub_dev, found_key.offset, dev_extent_len); - -/* -* flush, submit all pending read and write bios, afterwards -* wait for them. -* Note that in the dev replace case, a read request causes -* write requests that are submitted in the read completion -* worker. Therefore in the current situation, it is required -* that all write requests are flushed, so that all read and -* write requests are really completed when bios_in_flight -* changes to 0. -*/ -scrub_submit (sctx); - -wait_event (sctx->list_wait, -atomic_read (&sctx->bios_in_flight) ==0 ); - -scrub_pause_on (fs_info); - -/* -* must be called before we decrease @scrub_paused. -* make sure we don't block transaction commit while -* we are waiting pending workers finished. -*/ -wait_event (sctx->list_wait, -atomic_read (&sctx->workers_pending) ==0 ); - -scrub_pause_off (fs_info); -if (sctx->is_dev_replace && !btrfs_finish_block_group_to_copy (dev_replace->srcdev, cache, found_key.offset))
@@ -3111,12 +2881,9 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, ret =scrub_enumerate_chunks (sctx, dev, start, end);memalloc_nofs_restore (nofs_flag); -wait_event (sctx->list_wait,atomic_read (&sctx->bios_in_flight) ==0 );atomic_dec (&fs_info->scrubs_running);wake_up (&fs_info->scrub_pause_wait); -wait_event (sctx->list_wait,atomic_read (&sctx->workers_pending) ==0 ); -if (progress)memcpy (progress, &sctx->stat,sizeof (*progress));
fs/btrfs/scrub.h +0 -5
@@ -13,9 +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); -/* Temporary declaration, would be deleted later. */ -struct scrub_ctx ; -struct scrub_block ; -int scrub_find_csum (struct scrub_ctx *sctx,u64 logical,u8 *csum); -#endif
Signature · 1 line
2.39.2