1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
2a2de733cSArne Jansen /*
3b6bfebc1SStefan Behrens * Copyright (C) 2011, 2012 STRATO. All rights reserved.
4a2de733cSArne Jansen */
5a2de733cSArne Jansen
6a2de733cSArne Jansen #include <linux/blkdev.h>
7558540c1SJan Schmidt #include <linux/ratelimit.h>
8de2491fdSDavid Sterba #include <linux/sched/mm.h>
9d5178578SJohannes Thumshirn #include <crypto/hash.h>
10a2de733cSArne Jansen #include "ctree.h"
116e80d4f8SDennis Zhou #include "discard.h"
12a2de733cSArne Jansen #include "volumes.h"
13a2de733cSArne Jansen #include "disk-io.h"
14a2de733cSArne Jansen #include "ordered-data.h"
150ef8e451SJan Schmidt #include "transaction.h"
16558540c1SJan Schmidt #include "backref.h"
175da6fcbcSJan Schmidt #include "extent_io.h"
18ff023aacSStefan Behrens #include "dev-replace.h"
1921adbd5cSStefan Behrens #include "check-integrity.h"
2053b381b3SDavid Woodhouse #include "raid56.h"
21aac0023cSJosef Bacik #include "block-group.h"
2212659251SNaohiro Aota #include "zoned.h"
23c7f13d42SJosef Bacik #include "fs.h"
2407e81dc9SJosef Bacik #include "accessors.h"
257c8ede16SJosef Bacik #include "file-item.h"
262fc6822cSJosef Bacik #include "scrub.h"
27a2de733cSArne Jansen
28a2de733cSArne Jansen /*
29a2de733cSArne Jansen * This is only the first step towards a full-features scrub. It reads all
30a2de733cSArne Jansen * extent and super block and verifies the checksums. In case a bad checksum
31a2de733cSArne Jansen * is found or the extent cannot be read, good data will be written back if
32a2de733cSArne Jansen * any can be found.
33a2de733cSArne Jansen *
34a2de733cSArne Jansen * Future enhancements:
35a2de733cSArne Jansen * - In case an unrepairable extent is encountered, track which files are
36a2de733cSArne Jansen * affected and report them
37a2de733cSArne Jansen * - track and record media errors, throw out bad devices
38a2de733cSArne Jansen * - add a mode to also read unallocated space
39a2de733cSArne Jansen */
40a2de733cSArne Jansen
41d9d181c1SStefan Behrens struct scrub_ctx;
42a2de733cSArne Jansen
43ff023aacSStefan Behrens /*
4413a62fd9SQu Wenruo * The following value only influences the performance.
45c9d328c0SQu Wenruo *
46ae76d8e3SQu Wenruo * This detemines how many stripes would be submitted in one go,
47ae76d8e3SQu Wenruo * which is 512KiB (BTRFS_STRIPE_LEN * SCRUB_STRIPES_PER_GROUP).
48ff023aacSStefan Behrens */
49ae76d8e3SQu Wenruo #define SCRUB_STRIPES_PER_GROUP 8
50ae76d8e3SQu Wenruo
51ae76d8e3SQu Wenruo /*
52ae76d8e3SQu Wenruo * How many groups we have for each sctx.
53ae76d8e3SQu Wenruo *
54ae76d8e3SQu Wenruo * This would be 8M per device, the same value as the old scrub in-flight bios
55ae76d8e3SQu Wenruo * size limit.
56ae76d8e3SQu Wenruo */
57ae76d8e3SQu Wenruo #define SCRUB_GROUPS_PER_SCTX 16
58ae76d8e3SQu Wenruo
59ae76d8e3SQu Wenruo #define SCRUB_TOTAL_STRIPES (SCRUB_GROUPS_PER_SCTX * SCRUB_STRIPES_PER_GROUP)
607a9e9987SStefan Behrens
617a9e9987SStefan Behrens /*
620bb3acdcSQu Wenruo * The following value times PAGE_SIZE needs to be large enough to match the
637a9e9987SStefan Behrens * largest node/leaf/sector size that shall be supported.
647a9e9987SStefan Behrens */
657e737cbcSQu Wenruo #define SCRUB_MAX_SECTORS_PER_BLOCK (BTRFS_MAX_METADATA_BLOCKSIZE / SZ_4K)
66a2de733cSArne Jansen
672af2aaf9SQu Wenruo /* Represent one sector and its needed info to verify the content. */
682af2aaf9SQu Wenruo struct scrub_sector_verification {
692af2aaf9SQu Wenruo bool is_metadata;
702af2aaf9SQu Wenruo
712af2aaf9SQu Wenruo union {
722af2aaf9SQu Wenruo /*
732af2aaf9SQu Wenruo * Csum pointer for data csum verification. Should point to a
742af2aaf9SQu Wenruo * sector csum inside scrub_stripe::csums.
752af2aaf9SQu Wenruo *
762af2aaf9SQu Wenruo * NULL if this data sector has no csum.
772af2aaf9SQu Wenruo */
782af2aaf9SQu Wenruo u8 *csum;
792af2aaf9SQu Wenruo
802af2aaf9SQu Wenruo /*
812af2aaf9SQu Wenruo * Extra info for metadata verification. All sectors inside a
822af2aaf9SQu Wenruo * tree block share the same generation.
832af2aaf9SQu Wenruo */
842af2aaf9SQu Wenruo u64 generation;
852af2aaf9SQu Wenruo };
862af2aaf9SQu Wenruo };
872af2aaf9SQu Wenruo
882af2aaf9SQu Wenruo enum scrub_stripe_flags {
892af2aaf9SQu Wenruo /* Set when @mirror_num, @dev, @physical and @logical are set. */
902af2aaf9SQu Wenruo SCRUB_STRIPE_FLAG_INITIALIZED,
912af2aaf9SQu Wenruo
922af2aaf9SQu Wenruo /* Set when the read-repair is finished. */
932af2aaf9SQu Wenruo SCRUB_STRIPE_FLAG_REPAIR_DONE,
941009254bSQu Wenruo
951009254bSQu Wenruo /*
961009254bSQu Wenruo * Set for data stripes if it's triggered from P/Q stripe.
971009254bSQu Wenruo * During such scrub, we should not report errors in data stripes, nor
981009254bSQu Wenruo * update the accounting.
991009254bSQu Wenruo */
1001009254bSQu Wenruo SCRUB_STRIPE_FLAG_NO_REPORT,
1012af2aaf9SQu Wenruo };
1022af2aaf9SQu Wenruo
1032af2aaf9SQu Wenruo #define SCRUB_STRIPE_PAGES (BTRFS_STRIPE_LEN / PAGE_SIZE)
1042af2aaf9SQu Wenruo
1052af2aaf9SQu Wenruo /*
1062af2aaf9SQu Wenruo * Represent one contiguous range with a length of BTRFS_STRIPE_LEN.
1072af2aaf9SQu Wenruo */
1082af2aaf9SQu Wenruo struct scrub_stripe {
10900965807SQu Wenruo struct scrub_ctx *sctx;
1102af2aaf9SQu Wenruo struct btrfs_block_group *bg;
1112af2aaf9SQu Wenruo
1122af2aaf9SQu Wenruo struct page *pages[SCRUB_STRIPE_PAGES];
1132af2aaf9SQu Wenruo struct scrub_sector_verification *sectors;
1142af2aaf9SQu Wenruo
1152af2aaf9SQu Wenruo struct btrfs_device *dev;
1162af2aaf9SQu Wenruo u64 logical;
1172af2aaf9SQu Wenruo u64 physical;
1182af2aaf9SQu Wenruo
1192af2aaf9SQu Wenruo u16 mirror_num;
1202af2aaf9SQu Wenruo
1212af2aaf9SQu Wenruo /* Should be BTRFS_STRIPE_LEN / sectorsize. */
1222af2aaf9SQu Wenruo u16 nr_sectors;
1232af2aaf9SQu Wenruo
12400965807SQu Wenruo /*
12500965807SQu Wenruo * How many data/meta extents are in this stripe. Only for scrub status
12600965807SQu Wenruo * reporting purposes.
12700965807SQu Wenruo */
12800965807SQu Wenruo u16 nr_data_extents;
12900965807SQu Wenruo u16 nr_meta_extents;
13000965807SQu Wenruo
1312af2aaf9SQu Wenruo atomic_t pending_io;
1322af2aaf9SQu Wenruo wait_queue_head_t io_wait;
1339ecb5ef5SQu Wenruo wait_queue_head_t repair_wait;
1342af2aaf9SQu Wenruo
1352af2aaf9SQu Wenruo /*
1362af2aaf9SQu Wenruo * Indicate the states of the stripe. Bits are defined in
1372af2aaf9SQu Wenruo * scrub_stripe_flags enum.
1382af2aaf9SQu Wenruo */
1392af2aaf9SQu Wenruo unsigned long state;
1402af2aaf9SQu Wenruo
1412af2aaf9SQu Wenruo /* Indicate which sectors are covered by extent items. */
1422af2aaf9SQu Wenruo unsigned long extent_sector_bitmap;
1432af2aaf9SQu Wenruo
1442af2aaf9SQu Wenruo /*
1452af2aaf9SQu Wenruo * The errors hit during the initial read of the stripe.
1462af2aaf9SQu Wenruo *
1472af2aaf9SQu Wenruo * Would be utilized for error reporting and repair.
14879b8ee70SQu Wenruo *
14979b8ee70SQu Wenruo * The remaining init_nr_* records the number of errors hit, only used
15079b8ee70SQu Wenruo * by error reporting.
1512af2aaf9SQu Wenruo */
1522af2aaf9SQu Wenruo unsigned long init_error_bitmap;
15379b8ee70SQu Wenruo unsigned int init_nr_io_errors;
15479b8ee70SQu Wenruo unsigned int init_nr_csum_errors;
15579b8ee70SQu Wenruo unsigned int init_nr_meta_errors;
1562af2aaf9SQu Wenruo
1572af2aaf9SQu Wenruo /*
1582af2aaf9SQu Wenruo * The following error bitmaps are all for the current status.
1592af2aaf9SQu Wenruo * Every time we submit a new read, these bitmaps may be updated.
1602af2aaf9SQu Wenruo *
1612af2aaf9SQu Wenruo * error_bitmap = io_error_bitmap | csum_error_bitmap | meta_error_bitmap;
1622af2aaf9SQu Wenruo *
1632af2aaf9SQu Wenruo * IO and csum errors can happen for both metadata and data.
1642af2aaf9SQu Wenruo */
1652af2aaf9SQu Wenruo unsigned long error_bitmap;
1662af2aaf9SQu Wenruo unsigned long io_error_bitmap;
1672af2aaf9SQu Wenruo unsigned long csum_error_bitmap;
1682af2aaf9SQu Wenruo unsigned long meta_error_bitmap;
1692af2aaf9SQu Wenruo
170058e09e6SQu Wenruo /* For writeback (repair or replace) error reporting. */
171058e09e6SQu Wenruo unsigned long write_error_bitmap;
172058e09e6SQu Wenruo
173058e09e6SQu Wenruo /* Writeback can be concurrent, thus we need to protect the bitmap. */
174058e09e6SQu Wenruo spinlock_t write_error_lock;
175058e09e6SQu Wenruo
1762af2aaf9SQu Wenruo /*
1772af2aaf9SQu Wenruo * Checksum for the whole stripe if this stripe is inside a data block
1782af2aaf9SQu Wenruo * group.
1792af2aaf9SQu Wenruo */
1802af2aaf9SQu Wenruo u8 *csums;
1819ecb5ef5SQu Wenruo
1829ecb5ef5SQu Wenruo struct work_struct work;
1832af2aaf9SQu Wenruo };
1842af2aaf9SQu Wenruo
185d9d181c1SStefan Behrens struct scrub_ctx {
186ae76d8e3SQu Wenruo struct scrub_stripe stripes[SCRUB_TOTAL_STRIPES];
1871009254bSQu Wenruo struct scrub_stripe *raid56_data_stripes;
188fb456252SJeff Mahoney struct btrfs_fs_info *fs_info;
1891dc4888eSQu Wenruo struct btrfs_path extent_path;
1903c771c19SQu Wenruo struct btrfs_path csum_path;
191a2de733cSArne Jansen int first_free;
19254765392SQu Wenruo int cur_stripe;
193a2de733cSArne Jansen atomic_t cancel_req;
1948628764eSArne Jansen int readonly;
195e360d2f5SQu Wenruo int sectors_per_bio;
19663a212abSStefan Behrens
197eb3b5053SDavid Sterba /* State of IO submission throttling affecting the associated device */
198eb3b5053SDavid Sterba ktime_t throttle_deadline;
199eb3b5053SDavid Sterba u64 throttle_sent;
200eb3b5053SDavid Sterba
20163a212abSStefan Behrens int is_dev_replace;
202de17addcSNaohiro Aota u64 write_pointer;
2033fb99303SDavid Sterba
2043fb99303SDavid Sterba struct mutex wr_lock;
2053fb99303SDavid Sterba struct btrfs_device *wr_tgtdev;
20663a212abSStefan Behrens
207a2de733cSArne Jansen /*
208a2de733cSArne Jansen * statistics
209a2de733cSArne Jansen */
210a2de733cSArne Jansen struct btrfs_scrub_progress stat;
211a2de733cSArne Jansen spinlock_t stat_lock;
212f55985f4SFilipe Manana
213f55985f4SFilipe Manana /*
214f55985f4SFilipe Manana * Use a ref counter to avoid use-after-free issues. Scrub workers
215f55985f4SFilipe Manana * decrement bios_in_flight and workers_pending and then do a wakeup
216f55985f4SFilipe Manana * on the list_wait wait queue. We must ensure the main scrub task
217f55985f4SFilipe Manana * doesn't free the scrub context before or while the workers are
218f55985f4SFilipe Manana * doing the wakeup() call.
219f55985f4SFilipe Manana */
22099f4cdb1SElena Reshetova refcount_t refs;
221a2de733cSArne Jansen };
222a2de733cSArne Jansen
223558540c1SJan Schmidt struct scrub_warning {
224558540c1SJan Schmidt struct btrfs_path *path;
225558540c1SJan Schmidt u64 extent_item_size;
226558540c1SJan Schmidt const char *errstr;
2276aa21263SDavid Sterba u64 physical;
228558540c1SJan Schmidt u64 logical;
229558540c1SJan Schmidt struct btrfs_device *dev;
230558540c1SJan Schmidt };
231558540c1SJan Schmidt
release_scrub_stripe(struct scrub_stripe * stripe)2322af2aaf9SQu Wenruo static void release_scrub_stripe(struct scrub_stripe *stripe)
2332af2aaf9SQu Wenruo {
2342af2aaf9SQu Wenruo if (!stripe)
2352af2aaf9SQu Wenruo return;
2362af2aaf9SQu Wenruo
2372af2aaf9SQu Wenruo for (int i = 0; i < SCRUB_STRIPE_PAGES; i++) {
2382af2aaf9SQu Wenruo if (stripe->pages[i])
2392af2aaf9SQu Wenruo __free_page(stripe->pages[i]);
2402af2aaf9SQu Wenruo stripe->pages[i] = NULL;
2412af2aaf9SQu Wenruo }
2422af2aaf9SQu Wenruo kfree(stripe->sectors);
2432af2aaf9SQu Wenruo kfree(stripe->csums);
2442af2aaf9SQu Wenruo stripe->sectors = NULL;
2452af2aaf9SQu Wenruo stripe->csums = NULL;
24600965807SQu Wenruo stripe->sctx = NULL;
2472af2aaf9SQu Wenruo stripe->state = 0;
2482af2aaf9SQu Wenruo }
2492af2aaf9SQu Wenruo
init_scrub_stripe(struct btrfs_fs_info * fs_info,struct scrub_stripe * stripe)25054765392SQu Wenruo static int init_scrub_stripe(struct btrfs_fs_info *fs_info,
25154765392SQu Wenruo struct scrub_stripe *stripe)
2522af2aaf9SQu Wenruo {
2532af2aaf9SQu Wenruo int ret;
2542af2aaf9SQu Wenruo
2552af2aaf9SQu Wenruo memset(stripe, 0, sizeof(*stripe));
2562af2aaf9SQu Wenruo
2572af2aaf9SQu Wenruo stripe->nr_sectors = BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits;
2582af2aaf9SQu Wenruo stripe->state = 0;
2592af2aaf9SQu Wenruo
2602af2aaf9SQu Wenruo init_waitqueue_head(&stripe->io_wait);
2619ecb5ef5SQu Wenruo init_waitqueue_head(&stripe->repair_wait);
2622af2aaf9SQu Wenruo atomic_set(&stripe->pending_io, 0);
263058e09e6SQu Wenruo spin_lock_init(&stripe->write_error_lock);
2642af2aaf9SQu Wenruo
2652af2aaf9SQu Wenruo ret = btrfs_alloc_page_array(SCRUB_STRIPE_PAGES, stripe->pages);
2662af2aaf9SQu Wenruo if (ret < 0)
2672af2aaf9SQu Wenruo goto error;
2682af2aaf9SQu Wenruo
2692af2aaf9SQu Wenruo stripe->sectors = kcalloc(stripe->nr_sectors,
2702af2aaf9SQu Wenruo sizeof(struct scrub_sector_verification),
2712af2aaf9SQu Wenruo GFP_KERNEL);
2722af2aaf9SQu Wenruo if (!stripe->sectors)
2732af2aaf9SQu Wenruo goto error;
2742af2aaf9SQu Wenruo
2752af2aaf9SQu Wenruo stripe->csums = kcalloc(BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits,
2762af2aaf9SQu Wenruo fs_info->csum_size, GFP_KERNEL);
2772af2aaf9SQu Wenruo if (!stripe->csums)
2782af2aaf9SQu Wenruo goto error;
2792af2aaf9SQu Wenruo return 0;
2802af2aaf9SQu Wenruo error:
2812af2aaf9SQu Wenruo release_scrub_stripe(stripe);
2822af2aaf9SQu Wenruo return -ENOMEM;
2832af2aaf9SQu Wenruo }
2842af2aaf9SQu Wenruo
wait_scrub_stripe_io(struct scrub_stripe * stripe)2859ecb5ef5SQu Wenruo static void wait_scrub_stripe_io(struct scrub_stripe *stripe)
2862af2aaf9SQu Wenruo {
2872af2aaf9SQu Wenruo wait_event(stripe->io_wait, atomic_read(&stripe->pending_io) == 0);
2882af2aaf9SQu Wenruo }
2892af2aaf9SQu Wenruo
290f55985f4SFilipe Manana static void scrub_put_ctx(struct scrub_ctx *sctx);
2911623edebSStefan Behrens
__scrub_blocked_if_needed(struct btrfs_fs_info * fs_info)292cb7ab021SWang Shilong static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
2933cb0929aSWang Shilong {
2943cb0929aSWang Shilong while (atomic_read(&fs_info->scrub_pause_req)) {
2953cb0929aSWang Shilong mutex_unlock(&fs_info->scrub_lock);
2963cb0929aSWang Shilong wait_event(fs_info->scrub_pause_wait,
2973cb0929aSWang Shilong atomic_read(&fs_info->scrub_pause_req) == 0);
2983cb0929aSWang Shilong mutex_lock(&fs_info->scrub_lock);
2993cb0929aSWang Shilong }
3003cb0929aSWang Shilong }
3013cb0929aSWang Shilong
scrub_pause_on(struct btrfs_fs_info * fs_info)3020e22be89SZhaolei static void scrub_pause_on(struct btrfs_fs_info *fs_info)
303cb7ab021SWang Shilong {
304cb7ab021SWang Shilong atomic_inc(&fs_info->scrubs_paused);
305cb7ab021SWang Shilong wake_up(&fs_info->scrub_pause_wait);
3060e22be89SZhaolei }
307cb7ab021SWang Shilong
scrub_pause_off(struct btrfs_fs_info * fs_info)3080e22be89SZhaolei static void scrub_pause_off(struct btrfs_fs_info *fs_info)
3090e22be89SZhaolei {
310cb7ab021SWang Shilong mutex_lock(&fs_info->scrub_lock);
311cb7ab021SWang Shilong __scrub_blocked_if_needed(fs_info);
312cb7ab021SWang Shilong atomic_dec(&fs_info->scrubs_paused);
313cb7ab021SWang Shilong mutex_unlock(&fs_info->scrub_lock);
314cb7ab021SWang Shilong
315cb7ab021SWang Shilong wake_up(&fs_info->scrub_pause_wait);
316cb7ab021SWang Shilong }
317cb7ab021SWang Shilong
scrub_blocked_if_needed(struct btrfs_fs_info * fs_info)3180e22be89SZhaolei static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
3190e22be89SZhaolei {
3200e22be89SZhaolei scrub_pause_on(fs_info);
3210e22be89SZhaolei scrub_pause_off(fs_info);
3220e22be89SZhaolei }
3230e22be89SZhaolei
scrub_free_ctx(struct scrub_ctx * sctx)324d9d181c1SStefan Behrens static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
325a2de733cSArne Jansen {
326a2de733cSArne Jansen int i;
327a2de733cSArne Jansen
328d9d181c1SStefan Behrens if (!sctx)
329a2de733cSArne Jansen return;
330a2de733cSArne Jansen
331ae76d8e3SQu Wenruo for (i = 0; i < SCRUB_TOTAL_STRIPES; i++)
33254765392SQu Wenruo release_scrub_stripe(&sctx->stripes[i]);
33354765392SQu Wenruo
334ae76d8e3SQu Wenruo kvfree(sctx);
335a2de733cSArne Jansen }
336a2de733cSArne Jansen
scrub_put_ctx(struct scrub_ctx * sctx)337f55985f4SFilipe Manana static void scrub_put_ctx(struct scrub_ctx *sctx)
338f55985f4SFilipe Manana {
33999f4cdb1SElena Reshetova if (refcount_dec_and_test(&sctx->refs))
340f55985f4SFilipe Manana scrub_free_ctx(sctx);
341f55985f4SFilipe Manana }
342f55985f4SFilipe Manana
scrub_setup_ctx(struct btrfs_fs_info * fs_info,int is_dev_replace)34392f7ba43SDavid Sterba static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
34492f7ba43SDavid Sterba struct btrfs_fs_info *fs_info, int is_dev_replace)
345a2de733cSArne Jansen {
346d9d181c1SStefan Behrens struct scrub_ctx *sctx;
347a2de733cSArne Jansen int i;
348a2de733cSArne Jansen
349ae76d8e3SQu Wenruo /* Since sctx has inline 128 stripes, it can go beyond 64K easily. Use
350ae76d8e3SQu Wenruo * kvzalloc().
351ae76d8e3SQu Wenruo */
352ae76d8e3SQu Wenruo sctx = kvzalloc(sizeof(*sctx), GFP_KERNEL);
353d9d181c1SStefan Behrens if (!sctx)
354a2de733cSArne Jansen goto nomem;
35599f4cdb1SElena Reshetova refcount_set(&sctx->refs, 1);
35663a212abSStefan Behrens sctx->is_dev_replace = is_dev_replace;
35792f7ba43SDavid Sterba sctx->fs_info = fs_info;
3581dc4888eSQu Wenruo sctx->extent_path.search_commit_root = 1;
3591dc4888eSQu Wenruo sctx->extent_path.skip_locking = 1;
3603c771c19SQu Wenruo sctx->csum_path.search_commit_root = 1;
3613c771c19SQu Wenruo sctx->csum_path.skip_locking = 1;
362ae76d8e3SQu Wenruo for (i = 0; i < SCRUB_TOTAL_STRIPES; i++) {
36354765392SQu Wenruo int ret;
36454765392SQu Wenruo
36554765392SQu Wenruo ret = init_scrub_stripe(fs_info, &sctx->stripes[i]);
36654765392SQu Wenruo if (ret < 0)
36754765392SQu Wenruo goto nomem;
36854765392SQu Wenruo sctx->stripes[i].sctx = sctx;
36954765392SQu Wenruo }
370d9d181c1SStefan Behrens sctx->first_free = 0;
371d9d181c1SStefan Behrens atomic_set(&sctx->cancel_req, 0);
372a2de733cSArne Jansen
373d9d181c1SStefan Behrens spin_lock_init(&sctx->stat_lock);
374eb3b5053SDavid Sterba sctx->throttle_deadline = 0;
375ff023aacSStefan Behrens
3763fb99303SDavid Sterba mutex_init(&sctx->wr_lock);
3778fcdac3fSDavid Sterba if (is_dev_replace) {
378ded56184SDavid Sterba WARN_ON(!fs_info->dev_replace.tgtdev);
379ded56184SDavid Sterba sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
380ff023aacSStefan Behrens }
3818fcdac3fSDavid Sterba
382d9d181c1SStefan Behrens return sctx;
383a2de733cSArne Jansen
384a2de733cSArne Jansen nomem:
385d9d181c1SStefan Behrens scrub_free_ctx(sctx);
386a2de733cSArne Jansen return ERR_PTR(-ENOMEM);
387a2de733cSArne Jansen }
388a2de733cSArne Jansen
scrub_print_warning_inode(u64 inum,u64 offset,u64 num_bytes,u64 root,void * warn_ctx)389c7499a64SFilipe Manana static int scrub_print_warning_inode(u64 inum, u64 offset, u64 num_bytes,
390c7499a64SFilipe Manana u64 root, void *warn_ctx)
391558540c1SJan Schmidt {
392558540c1SJan Schmidt u32 nlink;
393558540c1SJan Schmidt int ret;
394558540c1SJan Schmidt int i;
395de2491fdSDavid Sterba unsigned nofs_flag;
396558540c1SJan Schmidt struct extent_buffer *eb;
397558540c1SJan Schmidt struct btrfs_inode_item *inode_item;
398ff023aacSStefan Behrens struct scrub_warning *swarn = warn_ctx;
399fb456252SJeff Mahoney struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
400558540c1SJan Schmidt struct inode_fs_paths *ipath = NULL;
401558540c1SJan Schmidt struct btrfs_root *local_root;
4021d4c08e0SDavid Sterba struct btrfs_key key;
403558540c1SJan Schmidt
40456e9357aSDavid Sterba local_root = btrfs_get_fs_root(fs_info, root, true);
405558540c1SJan Schmidt if (IS_ERR(local_root)) {
406558540c1SJan Schmidt ret = PTR_ERR(local_root);
407558540c1SJan Schmidt goto err;
408558540c1SJan Schmidt }
409558540c1SJan Schmidt
41014692cc1SDavid Sterba /*
41114692cc1SDavid Sterba * this makes the path point to (inum INODE_ITEM ioff)
41214692cc1SDavid Sterba */
4131d4c08e0SDavid Sterba key.objectid = inum;
4141d4c08e0SDavid Sterba key.type = BTRFS_INODE_ITEM_KEY;
4151d4c08e0SDavid Sterba key.offset = 0;
4161d4c08e0SDavid Sterba
4171d4c08e0SDavid Sterba ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
418558540c1SJan Schmidt if (ret) {
41900246528SJosef Bacik btrfs_put_root(local_root);
420558540c1SJan Schmidt btrfs_release_path(swarn->path);
421558540c1SJan Schmidt goto err;
422558540c1SJan Schmidt }
423558540c1SJan Schmidt
424558540c1SJan Schmidt eb = swarn->path->nodes[0];
425558540c1SJan Schmidt inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
426558540c1SJan Schmidt struct btrfs_inode_item);
427558540c1SJan Schmidt nlink = btrfs_inode_nlink(eb, inode_item);
428558540c1SJan Schmidt btrfs_release_path(swarn->path);
429558540c1SJan Schmidt
430de2491fdSDavid Sterba /*
431de2491fdSDavid Sterba * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
432de2491fdSDavid Sterba * uses GFP_NOFS in this context, so we keep it consistent but it does
433de2491fdSDavid Sterba * not seem to be strictly necessary.
434de2491fdSDavid Sterba */
435de2491fdSDavid Sterba nofs_flag = memalloc_nofs_save();
436558540c1SJan Schmidt ipath = init_ipath(4096, local_root, swarn->path);
437de2491fdSDavid Sterba memalloc_nofs_restore(nofs_flag);
43826bdef54SDan Carpenter if (IS_ERR(ipath)) {
43900246528SJosef Bacik btrfs_put_root(local_root);
44026bdef54SDan Carpenter ret = PTR_ERR(ipath);
44126bdef54SDan Carpenter ipath = NULL;
44226bdef54SDan Carpenter goto err;
44326bdef54SDan Carpenter }
444558540c1SJan Schmidt ret = paths_from_inode(inum, ipath);
445558540c1SJan Schmidt
446558540c1SJan Schmidt if (ret < 0)
447558540c1SJan Schmidt goto err;
448558540c1SJan Schmidt
449558540c1SJan Schmidt /*
450558540c1SJan Schmidt * we deliberately ignore the bit ipath might have been too small to
451558540c1SJan Schmidt * hold all of the paths here
452558540c1SJan Schmidt */
453558540c1SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i)
4545d163e0eSJeff Mahoney btrfs_warn_in_rcu(fs_info,
4558df507cbSQu Wenruo "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %u, links %u (path: %s)",
4565d163e0eSJeff Mahoney swarn->errstr, swarn->logical,
457cb3e217bSQu Wenruo btrfs_dev_name(swarn->dev),
4586aa21263SDavid Sterba swarn->physical,
4595d163e0eSJeff Mahoney root, inum, offset,
4608df507cbSQu Wenruo fs_info->sectorsize, nlink,
461745c4d8eSJeff Mahoney (char *)(unsigned long)ipath->fspath->val[i]);
462558540c1SJan Schmidt
46300246528SJosef Bacik btrfs_put_root(local_root);
464558540c1SJan Schmidt free_ipath(ipath);
465558540c1SJan Schmidt return 0;
466558540c1SJan Schmidt
467558540c1SJan Schmidt err:
4685d163e0eSJeff Mahoney btrfs_warn_in_rcu(fs_info,
4696aa21263SDavid Sterba "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
4705d163e0eSJeff Mahoney swarn->errstr, swarn->logical,
471cb3e217bSQu Wenruo btrfs_dev_name(swarn->dev),
4726aa21263SDavid Sterba swarn->physical,
4735d163e0eSJeff Mahoney root, inum, offset, ret);
474558540c1SJan Schmidt
475558540c1SJan Schmidt free_ipath(ipath);
476558540c1SJan Schmidt return 0;
477558540c1SJan Schmidt }
478558540c1SJan Schmidt
scrub_print_common_warning(const char * errstr,struct btrfs_device * dev,bool is_super,u64 logical,u64 physical)47900965807SQu Wenruo static void scrub_print_common_warning(const char *errstr, struct btrfs_device *dev,
48000965807SQu Wenruo bool is_super, u64 logical, u64 physical)
481558540c1SJan Schmidt {
48200965807SQu Wenruo struct btrfs_fs_info *fs_info = dev->fs_info;
483558540c1SJan Schmidt struct btrfs_path *path;
484558540c1SJan Schmidt struct btrfs_key found_key;
485558540c1SJan Schmidt struct extent_buffer *eb;
486558540c1SJan Schmidt struct btrfs_extent_item *ei;
487558540c1SJan Schmidt struct scrub_warning swarn;
48869917e43SLiu Bo u64 flags = 0;
48969917e43SLiu Bo u32 item_size;
49069917e43SLiu Bo int ret;
491558540c1SJan Schmidt
492e69bf81cSQu Wenruo /* Super block error, no need to search extent tree. */
49300965807SQu Wenruo if (is_super) {
494e69bf81cSQu Wenruo btrfs_warn_in_rcu(fs_info, "%s on device %s, physical %llu",
49500965807SQu Wenruo errstr, btrfs_dev_name(dev), physical);
496e69bf81cSQu Wenruo return;
497e69bf81cSQu Wenruo }
498558540c1SJan Schmidt path = btrfs_alloc_path();
4998b9456daSDavid Sterba if (!path)
5008b9456daSDavid Sterba return;
501558540c1SJan Schmidt
50200965807SQu Wenruo swarn.physical = physical;
50300965807SQu Wenruo swarn.logical = logical;
504558540c1SJan Schmidt swarn.errstr = errstr;
505a36cf8b8SStefan Behrens swarn.dev = NULL;
506558540c1SJan Schmidt
50769917e43SLiu Bo ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
50869917e43SLiu Bo &flags);
509558540c1SJan Schmidt if (ret < 0)
510558540c1SJan Schmidt goto out;
511558540c1SJan Schmidt
512558540c1SJan Schmidt swarn.extent_item_size = found_key.offset;
513558540c1SJan Schmidt
514558540c1SJan Schmidt eb = path->nodes[0];
515558540c1SJan Schmidt ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
5163212fa14SJosef Bacik item_size = btrfs_item_size(eb, path->slots[0]);
517558540c1SJan Schmidt
51869917e43SLiu Bo if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
519b7f9945aSQu Wenruo unsigned long ptr = 0;
520b7f9945aSQu Wenruo u8 ref_level;
521b7f9945aSQu Wenruo u64 ref_root;
522b7f9945aSQu Wenruo
523b7f9945aSQu Wenruo while (true) {
5246eda71d0SLiu Bo ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
5256eda71d0SLiu Bo item_size, &ref_root,
5266eda71d0SLiu Bo &ref_level);
527b7f9945aSQu Wenruo if (ret < 0) {
528b7f9945aSQu Wenruo btrfs_warn(fs_info,
529b7f9945aSQu Wenruo "failed to resolve tree backref for logical %llu: %d",
530b7f9945aSQu Wenruo swarn.logical, ret);
531b7f9945aSQu Wenruo break;
532b7f9945aSQu Wenruo }
533b7f9945aSQu Wenruo if (ret > 0)
534b7f9945aSQu Wenruo break;
535ecaeb14bSDavid Sterba btrfs_warn_in_rcu(fs_info,
5366aa21263SDavid Sterba "%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
537b7f9945aSQu Wenruo errstr, swarn.logical, btrfs_dev_name(dev),
538b7f9945aSQu Wenruo swarn.physical, (ref_level ? "node" : "leaf"),
539b7f9945aSQu Wenruo ref_level, ref_root);
540b7f9945aSQu Wenruo }
541d8fe29e9SJosef Bacik btrfs_release_path(path);
542558540c1SJan Schmidt } else {
543a2c8d27eSFilipe Manana struct btrfs_backref_walk_ctx ctx = { 0 };
544a2c8d27eSFilipe Manana
545d8fe29e9SJosef Bacik btrfs_release_path(path);
546a2c8d27eSFilipe Manana
547a2c8d27eSFilipe Manana ctx.bytenr = found_key.objectid;
548a2c8d27eSFilipe Manana ctx.extent_item_pos = swarn.logical - found_key.objectid;
549a2c8d27eSFilipe Manana ctx.fs_info = fs_info;
550a2c8d27eSFilipe Manana
551558540c1SJan Schmidt swarn.path = path;
552a36cf8b8SStefan Behrens swarn.dev = dev;
553a2c8d27eSFilipe Manana
554a2c8d27eSFilipe Manana iterate_extent_inodes(&ctx, true, scrub_print_warning_inode, &swarn);
555558540c1SJan Schmidt }
556558540c1SJan Schmidt
557558540c1SJan Schmidt out:
558558540c1SJan Schmidt btrfs_free_path(path);
559558540c1SJan Schmidt }
560558540c1SJan Schmidt
fill_writer_pointer_gap(struct scrub_ctx * sctx,u64 physical)561de17addcSNaohiro Aota static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
562de17addcSNaohiro Aota {
563de17addcSNaohiro Aota int ret = 0;
564de17addcSNaohiro Aota u64 length;
565de17addcSNaohiro Aota
566de17addcSNaohiro Aota if (!btrfs_is_zoned(sctx->fs_info))
567de17addcSNaohiro Aota return 0;
568de17addcSNaohiro Aota
5697db1c5d1SNaohiro Aota if (!btrfs_dev_is_sequential(sctx->wr_tgtdev, physical))
5707db1c5d1SNaohiro Aota return 0;
5717db1c5d1SNaohiro Aota
572de17addcSNaohiro Aota if (sctx->write_pointer < physical) {
573de17addcSNaohiro Aota length = physical - sctx->write_pointer;
574de17addcSNaohiro Aota
575de17addcSNaohiro Aota ret = btrfs_zoned_issue_zeroout(sctx->wr_tgtdev,
576de17addcSNaohiro Aota sctx->write_pointer, length);
577de17addcSNaohiro Aota if (!ret)
578de17addcSNaohiro Aota sctx->write_pointer = physical;
579de17addcSNaohiro Aota }
580de17addcSNaohiro Aota return ret;
581de17addcSNaohiro Aota }
582de17addcSNaohiro Aota
scrub_stripe_get_page(struct scrub_stripe * stripe,int sector_nr)583a3ddbaebSQu Wenruo static struct page *scrub_stripe_get_page(struct scrub_stripe *stripe, int sector_nr)
584a3ddbaebSQu Wenruo {
585a3ddbaebSQu Wenruo struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
586a3ddbaebSQu Wenruo int page_index = (sector_nr << fs_info->sectorsize_bits) >> PAGE_SHIFT;
587a3ddbaebSQu Wenruo
588a3ddbaebSQu Wenruo return stripe->pages[page_index];
589a3ddbaebSQu Wenruo }
590a3ddbaebSQu Wenruo
scrub_stripe_get_page_offset(struct scrub_stripe * stripe,int sector_nr)591a3ddbaebSQu Wenruo static unsigned int scrub_stripe_get_page_offset(struct scrub_stripe *stripe,
592a3ddbaebSQu Wenruo int sector_nr)
593a3ddbaebSQu Wenruo {
594a3ddbaebSQu Wenruo struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
595a3ddbaebSQu Wenruo
596a3ddbaebSQu Wenruo return offset_in_page(sector_nr << fs_info->sectorsize_bits);
597a3ddbaebSQu Wenruo }
598a3ddbaebSQu Wenruo
scrub_verify_one_metadata(struct scrub_stripe * stripe,int sector_nr)59997cf8f37SQu Wenruo static void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nr)
600a3ddbaebSQu Wenruo {
601a3ddbaebSQu Wenruo struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
602a3ddbaebSQu Wenruo const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
603a3ddbaebSQu Wenruo const u64 logical = stripe->logical + (sector_nr << fs_info->sectorsize_bits);
604a3ddbaebSQu Wenruo const struct page *first_page = scrub_stripe_get_page(stripe, sector_nr);
605a3ddbaebSQu Wenruo const unsigned int first_off = scrub_stripe_get_page_offset(stripe, sector_nr);
606a3ddbaebSQu Wenruo SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
607a3ddbaebSQu Wenruo u8 on_disk_csum[BTRFS_CSUM_SIZE];
608a3ddbaebSQu Wenruo u8 calculated_csum[BTRFS_CSUM_SIZE];
609a3ddbaebSQu Wenruo struct btrfs_header *header;
610a3ddbaebSQu Wenruo
611a3ddbaebSQu Wenruo /*
612a3ddbaebSQu Wenruo * Here we don't have a good way to attach the pages (and subpages)
613a3ddbaebSQu Wenruo * to a dummy extent buffer, thus we have to directly grab the members
614a3ddbaebSQu Wenruo * from pages.
615a3ddbaebSQu Wenruo */
616a3ddbaebSQu Wenruo header = (struct btrfs_header *)(page_address(first_page) + first_off);
617a3ddbaebSQu Wenruo memcpy(on_disk_csum, header->csum, fs_info->csum_size);
618a3ddbaebSQu Wenruo
619a3ddbaebSQu Wenruo if (logical != btrfs_stack_header_bytenr(header)) {
620a3ddbaebSQu Wenruo bitmap_set(&stripe->csum_error_bitmap, sector_nr, sectors_per_tree);
621a3ddbaebSQu Wenruo bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree);
622a3ddbaebSQu Wenruo btrfs_warn_rl(fs_info,
623a3ddbaebSQu Wenruo "tree block %llu mirror %u has bad bytenr, has %llu want %llu",
624a3ddbaebSQu Wenruo logical, stripe->mirror_num,
625a3ddbaebSQu Wenruo btrfs_stack_header_bytenr(header), logical);
626a3ddbaebSQu Wenruo return;
627a3ddbaebSQu Wenruo }
628b471965fSAnand Jain if (memcmp(header->fsid, fs_info->fs_devices->metadata_uuid,
629b471965fSAnand Jain BTRFS_FSID_SIZE) != 0) {
630a3ddbaebSQu Wenruo bitmap_set(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree);
631a3ddbaebSQu Wenruo bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree);
632a3ddbaebSQu Wenruo btrfs_warn_rl(fs_info,
633a3ddbaebSQu Wenruo "tree block %llu mirror %u has bad fsid, has %pU want %pU",
634a3ddbaebSQu Wenruo logical, stripe->mirror_num,
635a3ddbaebSQu Wenruo header->fsid, fs_info->fs_devices->fsid);
636a3ddbaebSQu Wenruo return;
637a3ddbaebSQu Wenruo }
638a3ddbaebSQu Wenruo if (memcmp(header->chunk_tree_uuid, fs_info->chunk_tree_uuid,
639a3ddbaebSQu Wenruo BTRFS_UUID_SIZE) != 0) {
640a3ddbaebSQu Wenruo bitmap_set(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree);
641a3ddbaebSQu Wenruo bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree);
642a3ddbaebSQu Wenruo btrfs_warn_rl(fs_info,
643a3ddbaebSQu Wenruo "tree block %llu mirror %u has bad chunk tree uuid, has %pU want %pU",
644a3ddbaebSQu Wenruo logical, stripe->mirror_num,
645a3ddbaebSQu Wenruo header->chunk_tree_uuid, fs_info->chunk_tree_uuid);
646a3ddbaebSQu Wenruo return;
647a3ddbaebSQu Wenruo }
648a3ddbaebSQu Wenruo
649a3ddbaebSQu Wenruo /* Now check tree block csum. */
650a3ddbaebSQu Wenruo shash->tfm = fs_info->csum_shash;
651a3ddbaebSQu Wenruo crypto_shash_init(shash);
652a3ddbaebSQu Wenruo crypto_shash_update(shash, page_address(first_page) + first_off +
653a3ddbaebSQu Wenruo BTRFS_CSUM_SIZE, fs_info->sectorsize - BTRFS_CSUM_SIZE);
654a3ddbaebSQu Wenruo
655a3ddbaebSQu Wenruo for (int i = sector_nr + 1; i < sector_nr + sectors_per_tree; i++) {
656a3ddbaebSQu Wenruo struct page *page = scrub_stripe_get_page(stripe, i);
657a3ddbaebSQu Wenruo unsigned int page_off = scrub_stripe_get_page_offset(stripe, i);
658a3ddbaebSQu Wenruo
659a3ddbaebSQu Wenruo crypto_shash_update(shash, page_address(page) + page_off,
660a3ddbaebSQu Wenruo fs_info->sectorsize);
661a3ddbaebSQu Wenruo }
662a3ddbaebSQu Wenruo
663a3ddbaebSQu Wenruo crypto_shash_final(shash, calculated_csum);
664a3ddbaebSQu Wenruo if (memcmp(calculated_csum, on_disk_csum, fs_info->csum_size) != 0) {
665a3ddbaebSQu Wenruo bitmap_set(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree);
666a3ddbaebSQu Wenruo bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree);
667a3ddbaebSQu Wenruo btrfs_warn_rl(fs_info,
668a3ddbaebSQu Wenruo "tree block %llu mirror %u has bad csum, has " CSUM_FMT " want " CSUM_FMT,
669a3ddbaebSQu Wenruo logical, stripe->mirror_num,
670a3ddbaebSQu Wenruo CSUM_FMT_VALUE(fs_info->csum_size, on_disk_csum),
671a3ddbaebSQu Wenruo CSUM_FMT_VALUE(fs_info->csum_size, calculated_csum));
672a3ddbaebSQu Wenruo return;
673a3ddbaebSQu Wenruo }
674a3ddbaebSQu Wenruo if (stripe->sectors[sector_nr].generation !=
675a3ddbaebSQu Wenruo btrfs_stack_header_generation(header)) {
676a3ddbaebSQu Wenruo bitmap_set(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree);
677a3ddbaebSQu Wenruo bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree);
678a3ddbaebSQu Wenruo btrfs_warn_rl(fs_info,
679a3ddbaebSQu Wenruo "tree block %llu mirror %u has bad generation, has %llu want %llu",
680a3ddbaebSQu Wenruo logical, stripe->mirror_num,
681a3ddbaebSQu Wenruo btrfs_stack_header_generation(header),
682a3ddbaebSQu Wenruo stripe->sectors[sector_nr].generation);
683a3ddbaebSQu Wenruo return;
684a3ddbaebSQu Wenruo }
685a3ddbaebSQu Wenruo bitmap_clear(&stripe->error_bitmap, sector_nr, sectors_per_tree);
686a3ddbaebSQu Wenruo bitmap_clear(&stripe->csum_error_bitmap, sector_nr, sectors_per_tree);
687a3ddbaebSQu Wenruo bitmap_clear(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree);
688a3ddbaebSQu Wenruo }
689a3ddbaebSQu Wenruo
scrub_verify_one_sector(struct scrub_stripe * stripe,int sector_nr)69097cf8f37SQu Wenruo static void scrub_verify_one_sector(struct scrub_stripe *stripe, int sector_nr)
69197cf8f37SQu Wenruo {
69297cf8f37SQu Wenruo struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
69397cf8f37SQu Wenruo struct scrub_sector_verification *sector = &stripe->sectors[sector_nr];
69497cf8f37SQu Wenruo const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
69597cf8f37SQu Wenruo struct page *page = scrub_stripe_get_page(stripe, sector_nr);
69697cf8f37SQu Wenruo unsigned int pgoff = scrub_stripe_get_page_offset(stripe, sector_nr);
69797cf8f37SQu Wenruo u8 csum_buf[BTRFS_CSUM_SIZE];
69897cf8f37SQu Wenruo int ret;
69997cf8f37SQu Wenruo
70097cf8f37SQu Wenruo ASSERT(sector_nr >= 0 && sector_nr < stripe->nr_sectors);
70197cf8f37SQu Wenruo
70297cf8f37SQu Wenruo /* Sector not utilized, skip it. */
70397cf8f37SQu Wenruo if (!test_bit(sector_nr, &stripe->extent_sector_bitmap))
70497cf8f37SQu Wenruo return;
70597cf8f37SQu Wenruo
70697cf8f37SQu Wenruo /* IO error, no need to check. */
70797cf8f37SQu Wenruo if (test_bit(sector_nr, &stripe->io_error_bitmap))
70897cf8f37SQu Wenruo return;
70997cf8f37SQu Wenruo
71097cf8f37SQu Wenruo /* Metadata, verify the full tree block. */
71197cf8f37SQu Wenruo if (sector->is_metadata) {
71297cf8f37SQu Wenruo /*
71397cf8f37SQu Wenruo * Check if the tree block crosses the stripe boudary. If
71497cf8f37SQu Wenruo * crossed the boundary, we cannot verify it but only give a
71597cf8f37SQu Wenruo * warning.
71697cf8f37SQu Wenruo *
71797cf8f37SQu Wenruo * This can only happen on a very old filesystem where chunks
71897cf8f37SQu Wenruo * are not ensured to be stripe aligned.
71997cf8f37SQu Wenruo */
72097cf8f37SQu Wenruo if (unlikely(sector_nr + sectors_per_tree > stripe->nr_sectors)) {
72197cf8f37SQu Wenruo btrfs_warn_rl(fs_info,
72297cf8f37SQu Wenruo "tree block at %llu crosses stripe boundary %llu",
72397cf8f37SQu Wenruo stripe->logical +
72497cf8f37SQu Wenruo (sector_nr << fs_info->sectorsize_bits),
72597cf8f37SQu Wenruo stripe->logical);
72697cf8f37SQu Wenruo return;
72797cf8f37SQu Wenruo }
72897cf8f37SQu Wenruo scrub_verify_one_metadata(stripe, sector_nr);
72997cf8f37SQu Wenruo return;
73097cf8f37SQu Wenruo }
73197cf8f37SQu Wenruo
73297cf8f37SQu Wenruo /*
73397cf8f37SQu Wenruo * Data is easier, we just verify the data csum (if we have it). For
73497cf8f37SQu Wenruo * cases without csum, we have no other choice but to trust it.
73597cf8f37SQu Wenruo */
73697cf8f37SQu Wenruo if (!sector->csum) {
73797cf8f37SQu Wenruo clear_bit(sector_nr, &stripe->error_bitmap);
73897cf8f37SQu Wenruo return;
73997cf8f37SQu Wenruo }
74097cf8f37SQu Wenruo
74197cf8f37SQu Wenruo ret = btrfs_check_sector_csum(fs_info, page, pgoff, csum_buf, sector->csum);
74297cf8f37SQu Wenruo if (ret < 0) {
74397cf8f37SQu Wenruo set_bit(sector_nr, &stripe->csum_error_bitmap);
74497cf8f37SQu Wenruo set_bit(sector_nr, &stripe->error_bitmap);
74597cf8f37SQu Wenruo } else {
74697cf8f37SQu Wenruo clear_bit(sector_nr, &stripe->csum_error_bitmap);
74797cf8f37SQu Wenruo clear_bit(sector_nr, &stripe->error_bitmap);
74897cf8f37SQu Wenruo }
74997cf8f37SQu Wenruo }
75097cf8f37SQu Wenruo
75197cf8f37SQu Wenruo /* Verify specified sectors of a stripe. */
scrub_verify_one_stripe(struct scrub_stripe * stripe,unsigned long bitmap)7529ecb5ef5SQu Wenruo static void scrub_verify_one_stripe(struct scrub_stripe *stripe, unsigned long bitmap)
75397cf8f37SQu Wenruo {
75497cf8f37SQu Wenruo struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
75597cf8f37SQu Wenruo const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
75697cf8f37SQu Wenruo int sector_nr;
75797cf8f37SQu Wenruo
75897cf8f37SQu Wenruo for_each_set_bit(sector_nr, &bitmap, stripe->nr_sectors) {
75997cf8f37SQu Wenruo scrub_verify_one_sector(stripe, sector_nr);
76097cf8f37SQu Wenruo if (stripe->sectors[sector_nr].is_metadata)
76197cf8f37SQu Wenruo sector_nr += sectors_per_tree - 1;
76297cf8f37SQu Wenruo }
76397cf8f37SQu Wenruo }
76497cf8f37SQu Wenruo
calc_sector_number(struct scrub_stripe * stripe,struct bio_vec * first_bvec)7659ecb5ef5SQu Wenruo static int calc_sector_number(struct scrub_stripe *stripe, struct bio_vec *first_bvec)
7669ecb5ef5SQu Wenruo {
7679ecb5ef5SQu Wenruo int i;
7689ecb5ef5SQu Wenruo
7699ecb5ef5SQu Wenruo for (i = 0; i < stripe->nr_sectors; i++) {
7709ecb5ef5SQu Wenruo if (scrub_stripe_get_page(stripe, i) == first_bvec->bv_page &&
7719ecb5ef5SQu Wenruo scrub_stripe_get_page_offset(stripe, i) == first_bvec->bv_offset)
7729ecb5ef5SQu Wenruo break;
7739ecb5ef5SQu Wenruo }
7749ecb5ef5SQu Wenruo ASSERT(i < stripe->nr_sectors);
7759ecb5ef5SQu Wenruo return i;
7769ecb5ef5SQu Wenruo }
7779ecb5ef5SQu Wenruo
7789ecb5ef5SQu Wenruo /*
7799ecb5ef5SQu Wenruo * Repair read is different to the regular read:
7809ecb5ef5SQu Wenruo *
7819ecb5ef5SQu Wenruo * - Only reads the failed sectors
7829ecb5ef5SQu Wenruo * - May have extra blocksize limits
7839ecb5ef5SQu Wenruo */
scrub_repair_read_endio(struct btrfs_bio * bbio)7849ecb5ef5SQu Wenruo static void scrub_repair_read_endio(struct btrfs_bio *bbio)
7859ecb5ef5SQu Wenruo {
7869ecb5ef5SQu Wenruo struct scrub_stripe *stripe = bbio->private;
7879ecb5ef5SQu Wenruo struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
7889ecb5ef5SQu Wenruo struct bio_vec *bvec;
7899ecb5ef5SQu Wenruo int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio));
7909ecb5ef5SQu Wenruo u32 bio_size = 0;
7919ecb5ef5SQu Wenruo int i;
7929ecb5ef5SQu Wenruo
7939ecb5ef5SQu Wenruo ASSERT(sector_nr < stripe->nr_sectors);
7949ecb5ef5SQu Wenruo
7959ecb5ef5SQu Wenruo bio_for_each_bvec_all(bvec, &bbio->bio, i)
7969ecb5ef5SQu Wenruo bio_size += bvec->bv_len;
7979ecb5ef5SQu Wenruo
7989ecb5ef5SQu Wenruo if (bbio->bio.bi_status) {
7999ecb5ef5SQu Wenruo bitmap_set(&stripe->io_error_bitmap, sector_nr,
8009ecb5ef5SQu Wenruo bio_size >> fs_info->sectorsize_bits);
8019ecb5ef5SQu Wenruo bitmap_set(&stripe->error_bitmap, sector_nr,
8029ecb5ef5SQu Wenruo bio_size >> fs_info->sectorsize_bits);
8039ecb5ef5SQu Wenruo } else {
8049ecb5ef5SQu Wenruo bitmap_clear(&stripe->io_error_bitmap, sector_nr,
8059ecb5ef5SQu Wenruo bio_size >> fs_info->sectorsize_bits);
8069ecb5ef5SQu Wenruo }
8079ecb5ef5SQu Wenruo bio_put(&bbio->bio);
8089ecb5ef5SQu Wenruo if (atomic_dec_and_test(&stripe->pending_io))
8099ecb5ef5SQu Wenruo wake_up(&stripe->io_wait);
8109ecb5ef5SQu Wenruo }
8119ecb5ef5SQu Wenruo
calc_next_mirror(int mirror,int num_copies)8129ecb5ef5SQu Wenruo static int calc_next_mirror(int mirror, int num_copies)
8139ecb5ef5SQu Wenruo {
8149ecb5ef5SQu Wenruo ASSERT(mirror <= num_copies);
8159ecb5ef5SQu Wenruo return (mirror + 1 > num_copies) ? 1 : mirror + 1;
8169ecb5ef5SQu Wenruo }
8179ecb5ef5SQu Wenruo
scrub_stripe_submit_repair_read(struct scrub_stripe * stripe,int mirror,int blocksize,bool wait)8189ecb5ef5SQu Wenruo static void scrub_stripe_submit_repair_read(struct scrub_stripe *stripe,
8199ecb5ef5SQu Wenruo int mirror, int blocksize, bool wait)
8209ecb5ef5SQu Wenruo {
8219ecb5ef5SQu Wenruo struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
8229ecb5ef5SQu Wenruo struct btrfs_bio *bbio = NULL;
8239ecb5ef5SQu Wenruo const unsigned long old_error_bitmap = stripe->error_bitmap;
8249ecb5ef5SQu Wenruo int i;
8259ecb5ef5SQu Wenruo
8269ecb5ef5SQu Wenruo ASSERT(stripe->mirror_num >= 1);
8279ecb5ef5SQu Wenruo ASSERT(atomic_read(&stripe->pending_io) == 0);
8289ecb5ef5SQu Wenruo
8299ecb5ef5SQu Wenruo for_each_set_bit(i, &old_error_bitmap, stripe->nr_sectors) {
8309ecb5ef5SQu Wenruo struct page *page;
8319ecb5ef5SQu Wenruo int pgoff;
8329ecb5ef5SQu Wenruo int ret;
8339ecb5ef5SQu Wenruo
8349ecb5ef5SQu Wenruo page = scrub_stripe_get_page(stripe, i);
8359ecb5ef5SQu Wenruo pgoff = scrub_stripe_get_page_offset(stripe, i);
8369ecb5ef5SQu Wenruo
8379ecb5ef5SQu Wenruo /* The current sector cannot be merged, submit the bio. */
8389ecb5ef5SQu Wenruo if (bbio && ((i > 0 && !test_bit(i - 1, &stripe->error_bitmap)) ||
8399ecb5ef5SQu Wenruo bbio->bio.bi_iter.bi_size >= blocksize)) {
8409ecb5ef5SQu Wenruo ASSERT(bbio->bio.bi_iter.bi_size);
8419ecb5ef5SQu Wenruo atomic_inc(&stripe->pending_io);
8429ecb5ef5SQu Wenruo btrfs_submit_bio(bbio, mirror);
8439ecb5ef5SQu Wenruo if (wait)
8449ecb5ef5SQu Wenruo wait_scrub_stripe_io(stripe);
8459ecb5ef5SQu Wenruo bbio = NULL;
8469ecb5ef5SQu Wenruo }
8479ecb5ef5SQu Wenruo
8489ecb5ef5SQu Wenruo if (!bbio) {
8499ecb5ef5SQu Wenruo bbio = btrfs_bio_alloc(stripe->nr_sectors, REQ_OP_READ,
8509ecb5ef5SQu Wenruo fs_info, scrub_repair_read_endio, stripe);
8519ecb5ef5SQu Wenruo bbio->bio.bi_iter.bi_sector = (stripe->logical +
8529ecb5ef5SQu Wenruo (i << fs_info->sectorsize_bits)) >> SECTOR_SHIFT;
8539ecb5ef5SQu Wenruo }
8549ecb5ef5SQu Wenruo
8559ecb5ef5SQu Wenruo ret = bio_add_page(&bbio->bio, page, fs_info->sectorsize, pgoff);
8569ecb5ef5SQu Wenruo ASSERT(ret == fs_info->sectorsize);
8579ecb5ef5SQu Wenruo }
8589ecb5ef5SQu Wenruo if (bbio) {
8599ecb5ef5SQu Wenruo ASSERT(bbio->bio.bi_iter.bi_size);
8609ecb5ef5SQu Wenruo atomic_inc(&stripe->pending_io);
8619ecb5ef5SQu Wenruo btrfs_submit_bio(bbio, mirror);
8629ecb5ef5SQu Wenruo if (wait)
8639ecb5ef5SQu Wenruo wait_scrub_stripe_io(stripe);
8649ecb5ef5SQu Wenruo }
8659ecb5ef5SQu Wenruo }
8669ecb5ef5SQu Wenruo
scrub_stripe_report_errors(struct scrub_ctx * sctx,struct scrub_stripe * stripe)86700965807SQu Wenruo static void scrub_stripe_report_errors(struct scrub_ctx *sctx,
86800965807SQu Wenruo struct scrub_stripe *stripe)
86900965807SQu Wenruo {
87000965807SQu Wenruo static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
87100965807SQu Wenruo DEFAULT_RATELIMIT_BURST);
87200965807SQu Wenruo struct btrfs_fs_info *fs_info = sctx->fs_info;
87300965807SQu Wenruo struct btrfs_device *dev = NULL;
87400965807SQu Wenruo u64 physical = 0;
87500965807SQu Wenruo int nr_data_sectors = 0;
87600965807SQu Wenruo int nr_meta_sectors = 0;
87700965807SQu Wenruo int nr_nodatacsum_sectors = 0;
87800965807SQu Wenruo int nr_repaired_sectors = 0;
87900965807SQu Wenruo int sector_nr;
88000965807SQu Wenruo
8811009254bSQu Wenruo if (test_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state))
8821009254bSQu Wenruo return;
8831009254bSQu Wenruo
88400965807SQu Wenruo /*
88500965807SQu Wenruo * Init needed infos for error reporting.
88600965807SQu Wenruo *
88700965807SQu Wenruo * Although our scrub_stripe infrastucture is mostly based on btrfs_submit_bio()
88800965807SQu Wenruo * thus no need for dev/physical, error reporting still needs dev and physical.
88900965807SQu Wenruo */
89000965807SQu Wenruo if (!bitmap_empty(&stripe->init_error_bitmap, stripe->nr_sectors)) {
89100965807SQu Wenruo u64 mapped_len = fs_info->sectorsize;
89200965807SQu Wenruo struct btrfs_io_context *bioc = NULL;
89300965807SQu Wenruo int stripe_index = stripe->mirror_num - 1;
89400965807SQu Wenruo int ret;
89500965807SQu Wenruo
89600965807SQu Wenruo /* For scrub, our mirror_num should always start at 1. */
89700965807SQu Wenruo ASSERT(stripe->mirror_num >= 1);
898723b8bb1SChristoph Hellwig ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
899723b8bb1SChristoph Hellwig stripe->logical, &mapped_len, &bioc,
900723b8bb1SChristoph Hellwig NULL, NULL, 1);
90100965807SQu Wenruo /*
90200965807SQu Wenruo * If we failed, dev will be NULL, and later detailed reports
90300965807SQu Wenruo * will just be skipped.
90400965807SQu Wenruo */
90500965807SQu Wenruo if (ret < 0)
90600965807SQu Wenruo goto skip;
90700965807SQu Wenruo physical = bioc->stripes[stripe_index].physical;
90800965807SQu Wenruo dev = bioc->stripes[stripe_index].dev;
90900965807SQu Wenruo btrfs_put_bioc(bioc);
91000965807SQu Wenruo }
91100965807SQu Wenruo
91200965807SQu Wenruo skip:
91300965807SQu Wenruo for_each_set_bit(sector_nr, &stripe->extent_sector_bitmap, stripe->nr_sectors) {
91400965807SQu Wenruo bool repaired = false;
91500965807SQu Wenruo
91600965807SQu Wenruo if (stripe->sectors[sector_nr].is_metadata) {
91700965807SQu Wenruo nr_meta_sectors++;
91800965807SQu Wenruo } else {
91900965807SQu Wenruo nr_data_sectors++;
92000965807SQu Wenruo if (!stripe->sectors[sector_nr].csum)
92100965807SQu Wenruo nr_nodatacsum_sectors++;
92200965807SQu Wenruo }
92300965807SQu Wenruo
92400965807SQu Wenruo if (test_bit(sector_nr, &stripe->init_error_bitmap) &&
92500965807SQu Wenruo !test_bit(sector_nr, &stripe->error_bitmap)) {
92600965807SQu Wenruo nr_repaired_sectors++;
92700965807SQu Wenruo repaired = true;
92800965807SQu Wenruo }
92900965807SQu Wenruo
93000965807SQu Wenruo /* Good sector from the beginning, nothing need to be done. */
93100965807SQu Wenruo if (!test_bit(sector_nr, &stripe->init_error_bitmap))
93200965807SQu Wenruo continue;
93300965807SQu Wenruo
93400965807SQu Wenruo /*
93500965807SQu Wenruo * Report error for the corrupted sectors. If repaired, just
93600965807SQu Wenruo * output the message of repaired message.
93700965807SQu Wenruo */
93800965807SQu Wenruo if (repaired) {
93900965807SQu Wenruo if (dev) {
94000965807SQu Wenruo btrfs_err_rl_in_rcu(fs_info,
94100965807SQu Wenruo "fixed up error at logical %llu on dev %s physical %llu",
94200965807SQu Wenruo stripe->logical, btrfs_dev_name(dev),
94300965807SQu Wenruo physical);
94400965807SQu Wenruo } else {
94500965807SQu Wenruo btrfs_err_rl_in_rcu(fs_info,
94600965807SQu Wenruo "fixed up error at logical %llu on mirror %u",
94700965807SQu Wenruo stripe->logical, stripe->mirror_num);
94800965807SQu Wenruo }
94900965807SQu Wenruo continue;
95000965807SQu Wenruo }
95100965807SQu Wenruo
95200965807SQu Wenruo /* The remaining are all for unrepaired. */
95300965807SQu Wenruo if (dev) {
95400965807SQu Wenruo btrfs_err_rl_in_rcu(fs_info,
95500965807SQu Wenruo "unable to fixup (regular) error at logical %llu on dev %s physical %llu",
95600965807SQu Wenruo stripe->logical, btrfs_dev_name(dev),
95700965807SQu Wenruo physical);
95800965807SQu Wenruo } else {
95900965807SQu Wenruo btrfs_err_rl_in_rcu(fs_info,
96000965807SQu Wenruo "unable to fixup (regular) error at logical %llu on mirror %u",
96100965807SQu Wenruo stripe->logical, stripe->mirror_num);
96200965807SQu Wenruo }
96300965807SQu Wenruo
96400965807SQu Wenruo if (test_bit(sector_nr, &stripe->io_error_bitmap))
96500965807SQu Wenruo if (__ratelimit(&rs) && dev)
96600965807SQu Wenruo scrub_print_common_warning("i/o error", dev, false,
96700965807SQu Wenruo stripe->logical, physical);
96800965807SQu Wenruo if (test_bit(sector_nr, &stripe->csum_error_bitmap))
96900965807SQu Wenruo if (__ratelimit(&rs) && dev)
97000965807SQu Wenruo scrub_print_common_warning("checksum error", dev, false,
97100965807SQu Wenruo stripe->logical, physical);
97200965807SQu Wenruo if (test_bit(sector_nr, &stripe->meta_error_bitmap))
97300965807SQu Wenruo if (__ratelimit(&rs) && dev)
97400965807SQu Wenruo scrub_print_common_warning("header error", dev, false,
97500965807SQu Wenruo stripe->logical, physical);
97600965807SQu Wenruo }
97700965807SQu Wenruo
97800965807SQu Wenruo spin_lock(&sctx->stat_lock);
97900965807SQu Wenruo sctx->stat.data_extents_scrubbed += stripe->nr_data_extents;
98000965807SQu Wenruo sctx->stat.tree_extents_scrubbed += stripe->nr_meta_extents;
98100965807SQu Wenruo sctx->stat.data_bytes_scrubbed += nr_data_sectors << fs_info->sectorsize_bits;
98200965807SQu Wenruo sctx->stat.tree_bytes_scrubbed += nr_meta_sectors << fs_info->sectorsize_bits;
98300965807SQu Wenruo sctx->stat.no_csum += nr_nodatacsum_sectors;
98479b8ee70SQu Wenruo sctx->stat.read_errors += stripe->init_nr_io_errors;
98579b8ee70SQu Wenruo sctx->stat.csum_errors += stripe->init_nr_csum_errors;
98679b8ee70SQu Wenruo sctx->stat.verify_errors += stripe->init_nr_meta_errors;
98700965807SQu Wenruo sctx->stat.uncorrectable_errors +=
98800965807SQu Wenruo bitmap_weight(&stripe->error_bitmap, stripe->nr_sectors);
98900965807SQu Wenruo sctx->stat.corrected_errors += nr_repaired_sectors;
99000965807SQu Wenruo spin_unlock(&sctx->stat_lock);
99100965807SQu Wenruo }
99200965807SQu Wenruo
9934fe44f9dSQu Wenruo static void scrub_write_sectors(struct scrub_ctx *sctx, struct scrub_stripe *stripe,
9944fe44f9dSQu Wenruo unsigned long write_bitmap, bool dev_replace);
9954fe44f9dSQu Wenruo
9969ecb5ef5SQu Wenruo /*
9979ecb5ef5SQu Wenruo * The main entrance for all read related scrub work, including:
9989ecb5ef5SQu Wenruo *
9999ecb5ef5SQu Wenruo * - Wait for the initial read to finish
10009ecb5ef5SQu Wenruo * - Verify and locate any bad sectors
10019ecb5ef5SQu Wenruo * - Go through the remaining mirrors and try to read as large blocksize as
10029ecb5ef5SQu Wenruo * possible
10039ecb5ef5SQu Wenruo * - Go through all mirrors (including the failed mirror) sector-by-sector
10044fe44f9dSQu Wenruo * - Submit writeback for repaired sectors
10059ecb5ef5SQu Wenruo *
10064fe44f9dSQu Wenruo * Writeback for dev-replace does not happen here, it needs extra
10074fe44f9dSQu Wenruo * synchronization for zoned devices.
10089ecb5ef5SQu Wenruo */
scrub_stripe_read_repair_worker(struct work_struct * work)10099ecb5ef5SQu Wenruo static void scrub_stripe_read_repair_worker(struct work_struct *work)
10109ecb5ef5SQu Wenruo {
10119ecb5ef5SQu Wenruo struct scrub_stripe *stripe = container_of(work, struct scrub_stripe, work);
10124fe44f9dSQu Wenruo struct scrub_ctx *sctx = stripe->sctx;
10134fe44f9dSQu Wenruo struct btrfs_fs_info *fs_info = sctx->fs_info;
10149ecb5ef5SQu Wenruo int num_copies = btrfs_num_copies(fs_info, stripe->bg->start,
10159ecb5ef5SQu Wenruo stripe->bg->length);
1016e498cc00SNaohiro Aota unsigned long repaired;
10179ecb5ef5SQu Wenruo int mirror;
10189ecb5ef5SQu Wenruo int i;
10199ecb5ef5SQu Wenruo
10209ecb5ef5SQu Wenruo ASSERT(stripe->mirror_num > 0);
10219ecb5ef5SQu Wenruo
10229ecb5ef5SQu Wenruo wait_scrub_stripe_io(stripe);
10239ecb5ef5SQu Wenruo scrub_verify_one_stripe(stripe, stripe->extent_sector_bitmap);
10249ecb5ef5SQu Wenruo /* Save the initial failed bitmap for later repair and report usage. */
10259ecb5ef5SQu Wenruo stripe->init_error_bitmap = stripe->error_bitmap;
102679b8ee70SQu Wenruo stripe->init_nr_io_errors = bitmap_weight(&stripe->io_error_bitmap,
102779b8ee70SQu Wenruo stripe->nr_sectors);
102879b8ee70SQu Wenruo stripe->init_nr_csum_errors = bitmap_weight(&stripe->csum_error_bitmap,
102979b8ee70SQu Wenruo stripe->nr_sectors);
103079b8ee70SQu Wenruo stripe->init_nr_meta_errors = bitmap_weight(&stripe->meta_error_bitmap,
103179b8ee70SQu Wenruo stripe->nr_sectors);
10329ecb5ef5SQu Wenruo
10339ecb5ef5SQu Wenruo if (bitmap_empty(&stripe->init_error_bitmap, stripe->nr_sectors))
10349ecb5ef5SQu Wenruo goto out;
10359ecb5ef5SQu Wenruo
10369ecb5ef5SQu Wenruo /*
10379ecb5ef5SQu Wenruo * Try all remaining mirrors.
10389ecb5ef5SQu Wenruo *
10399ecb5ef5SQu Wenruo * Here we still try to read as large block as possible, as this is
10409ecb5ef5SQu Wenruo * faster and we have extra safety nets to rely on.
10419ecb5ef5SQu Wenruo */
10429ecb5ef5SQu Wenruo for (mirror = calc_next_mirror(stripe->mirror_num, num_copies);
10439ecb5ef5SQu Wenruo mirror != stripe->mirror_num;
10449ecb5ef5SQu Wenruo mirror = calc_next_mirror(mirror, num_copies)) {
10459ecb5ef5SQu Wenruo const unsigned long old_error_bitmap = stripe->error_bitmap;
10469ecb5ef5SQu Wenruo
10479ecb5ef5SQu Wenruo scrub_stripe_submit_repair_read(stripe, mirror,
10489ecb5ef5SQu Wenruo BTRFS_STRIPE_LEN, false);
10499ecb5ef5SQu Wenruo wait_scrub_stripe_io(stripe);
10509ecb5ef5SQu Wenruo scrub_verify_one_stripe(stripe, old_error_bitmap);
10519ecb5ef5SQu Wenruo if (bitmap_empty(&stripe->error_bitmap, stripe->nr_sectors))
10529ecb5ef5SQu Wenruo goto out;
10539ecb5ef5SQu Wenruo }
10549ecb5ef5SQu Wenruo
10559ecb5ef5SQu Wenruo /*
10569ecb5ef5SQu Wenruo * Last safety net, try re-checking all mirrors, including the failed
10579ecb5ef5SQu Wenruo * one, sector-by-sector.
10589ecb5ef5SQu Wenruo *
10599ecb5ef5SQu Wenruo * As if one sector failed the drive's internal csum, the whole read
10609ecb5ef5SQu Wenruo * containing the offending sector would be marked as error.
10619ecb5ef5SQu Wenruo * Thus here we do sector-by-sector read.
10629ecb5ef5SQu Wenruo *
10639ecb5ef5SQu Wenruo * This can be slow, thus we only try it as the last resort.
10649ecb5ef5SQu Wenruo */
10659ecb5ef5SQu Wenruo
10669ecb5ef5SQu Wenruo for (i = 0, mirror = stripe->mirror_num;
10679ecb5ef5SQu Wenruo i < num_copies;
10689ecb5ef5SQu Wenruo i++, mirror = calc_next_mirror(mirror, num_copies)) {
10699ecb5ef5SQu Wenruo const unsigned long old_error_bitmap = stripe->error_bitmap;
10709ecb5ef5SQu Wenruo
10719ecb5ef5SQu Wenruo scrub_stripe_submit_repair_read(stripe, mirror,
10729ecb5ef5SQu Wenruo fs_info->sectorsize, true);
10739ecb5ef5SQu Wenruo wait_scrub_stripe_io(stripe);
10749ecb5ef5SQu Wenruo scrub_verify_one_stripe(stripe, old_error_bitmap);
10759ecb5ef5SQu Wenruo if (bitmap_empty(&stripe->error_bitmap, stripe->nr_sectors))
10769ecb5ef5SQu Wenruo goto out;
10779ecb5ef5SQu Wenruo }
10789ecb5ef5SQu Wenruo out:
10794fe44f9dSQu Wenruo /*
10804fe44f9dSQu Wenruo * Submit the repaired sectors. For zoned case, we cannot do repair
10814fe44f9dSQu Wenruo * in-place, but queue the bg to be relocated.
10824fe44f9dSQu Wenruo */
1083e498cc00SNaohiro Aota bitmap_andnot(&repaired, &stripe->init_error_bitmap, &stripe->error_bitmap,
1084e498cc00SNaohiro Aota stripe->nr_sectors);
1085e498cc00SNaohiro Aota if (!sctx->readonly && !bitmap_empty(&repaired, stripe->nr_sectors)) {
10864fe44f9dSQu Wenruo if (btrfs_is_zoned(fs_info)) {
10874fe44f9dSQu Wenruo btrfs_repair_one_zone(fs_info, sctx->stripes[0].bg->start);
1088e498cc00SNaohiro Aota } else {
10894fe44f9dSQu Wenruo scrub_write_sectors(sctx, stripe, repaired, false);
10904fe44f9dSQu Wenruo wait_scrub_stripe_io(stripe);
10914fe44f9dSQu Wenruo }
1092e498cc00SNaohiro Aota }
10934fe44f9dSQu Wenruo
10944fe44f9dSQu Wenruo scrub_stripe_report_errors(sctx, stripe);
10959ecb5ef5SQu Wenruo set_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state);
10969ecb5ef5SQu Wenruo wake_up(&stripe->repair_wait);
10979ecb5ef5SQu Wenruo }
10989ecb5ef5SQu Wenruo
scrub_read_endio(struct btrfs_bio * bbio)109954765392SQu Wenruo static void scrub_read_endio(struct btrfs_bio *bbio)
11009ecb5ef5SQu Wenruo {
11019ecb5ef5SQu Wenruo struct scrub_stripe *stripe = bbio->private;
1102642b9c52SQu Wenruo struct bio_vec *bvec;
1103642b9c52SQu Wenruo int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio));
1104642b9c52SQu Wenruo int num_sectors;
1105642b9c52SQu Wenruo u32 bio_size = 0;
1106642b9c52SQu Wenruo int i;
1107642b9c52SQu Wenruo
1108642b9c52SQu Wenruo ASSERT(sector_nr < stripe->nr_sectors);
1109642b9c52SQu Wenruo bio_for_each_bvec_all(bvec, &bbio->bio, i)
1110642b9c52SQu Wenruo bio_size += bvec->bv_len;
1111642b9c52SQu Wenruo num_sectors = bio_size >> stripe->bg->fs_info->sectorsize_bits;
11129ecb5ef5SQu Wenruo
11139ecb5ef5SQu Wenruo if (bbio->bio.bi_status) {
1114642b9c52SQu Wenruo bitmap_set(&stripe->io_error_bitmap, sector_nr, num_sectors);
1115642b9c52SQu Wenruo bitmap_set(&stripe->error_bitmap, sector_nr, num_sectors);
11169ecb5ef5SQu Wenruo } else {
1117642b9c52SQu Wenruo bitmap_clear(&stripe->io_error_bitmap, sector_nr, num_sectors);
11189ecb5ef5SQu Wenruo }
11199ecb5ef5SQu Wenruo bio_put(&bbio->bio);
11209ecb5ef5SQu Wenruo if (atomic_dec_and_test(&stripe->pending_io)) {
11219ecb5ef5SQu Wenruo wake_up(&stripe->io_wait);
11229ecb5ef5SQu Wenruo INIT_WORK(&stripe->work, scrub_stripe_read_repair_worker);
11239ecb5ef5SQu Wenruo queue_work(stripe->bg->fs_info->scrub_workers, &stripe->work);
11249ecb5ef5SQu Wenruo }
11259ecb5ef5SQu Wenruo }
11269ecb5ef5SQu Wenruo
scrub_write_endio(struct btrfs_bio * bbio)1127058e09e6SQu Wenruo static void scrub_write_endio(struct btrfs_bio *bbio)
1128058e09e6SQu Wenruo {
1129058e09e6SQu Wenruo struct scrub_stripe *stripe = bbio->private;
1130058e09e6SQu Wenruo struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
1131058e09e6SQu Wenruo struct bio_vec *bvec;
1132058e09e6SQu Wenruo int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio));
1133058e09e6SQu Wenruo u32 bio_size = 0;
1134058e09e6SQu Wenruo int i;
1135058e09e6SQu Wenruo
1136058e09e6SQu Wenruo bio_for_each_bvec_all(bvec, &bbio->bio, i)
1137058e09e6SQu Wenruo bio_size += bvec->bv_len;
1138058e09e6SQu Wenruo
1139058e09e6SQu Wenruo if (bbio->bio.bi_status) {
1140058e09e6SQu Wenruo unsigned long flags;
1141058e09e6SQu Wenruo
1142058e09e6SQu Wenruo spin_lock_irqsave(&stripe->write_error_lock, flags);
1143058e09e6SQu Wenruo bitmap_set(&stripe->write_error_bitmap, sector_nr,
1144058e09e6SQu Wenruo bio_size >> fs_info->sectorsize_bits);
1145058e09e6SQu Wenruo spin_unlock_irqrestore(&stripe->write_error_lock, flags);
1146058e09e6SQu Wenruo }
1147058e09e6SQu Wenruo bio_put(&bbio->bio);
1148058e09e6SQu Wenruo
1149058e09e6SQu Wenruo if (atomic_dec_and_test(&stripe->pending_io))
1150058e09e6SQu Wenruo wake_up(&stripe->io_wait);
1151058e09e6SQu Wenruo }
1152058e09e6SQu Wenruo
scrub_submit_write_bio(struct scrub_ctx * sctx,struct scrub_stripe * stripe,struct btrfs_bio * bbio,bool dev_replace)1153b675df02SQu Wenruo static void scrub_submit_write_bio(struct scrub_ctx *sctx,
1154b675df02SQu Wenruo struct scrub_stripe *stripe,
1155b675df02SQu Wenruo struct btrfs_bio *bbio, bool dev_replace)
1156b675df02SQu Wenruo {
1157b675df02SQu Wenruo struct btrfs_fs_info *fs_info = sctx->fs_info;
1158b675df02SQu Wenruo u32 bio_len = bbio->bio.bi_iter.bi_size;
1159b675df02SQu Wenruo u32 bio_off = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT) -
1160b675df02SQu Wenruo stripe->logical;
1161b675df02SQu Wenruo
1162b675df02SQu Wenruo fill_writer_pointer_gap(sctx, stripe->physical + bio_off);
1163b675df02SQu Wenruo atomic_inc(&stripe->pending_io);
1164b675df02SQu Wenruo btrfs_submit_repair_write(bbio, stripe->mirror_num, dev_replace);
1165b675df02SQu Wenruo if (!btrfs_is_zoned(fs_info))
1166b675df02SQu Wenruo return;
1167b675df02SQu Wenruo /*
1168b675df02SQu Wenruo * For zoned writeback, queue depth must be 1, thus we must wait for
1169b675df02SQu Wenruo * the write to finish before the next write.
1170b675df02SQu Wenruo */
1171b675df02SQu Wenruo wait_scrub_stripe_io(stripe);
1172b675df02SQu Wenruo
1173b675df02SQu Wenruo /*
1174b675df02SQu Wenruo * And also need to update the write pointer if write finished
1175b675df02SQu Wenruo * successfully.
1176b675df02SQu Wenruo */
1177b675df02SQu Wenruo if (!test_bit(bio_off >> fs_info->sectorsize_bits,
1178b675df02SQu Wenruo &stripe->write_error_bitmap))
1179b675df02SQu Wenruo sctx->write_pointer += bio_len;
1180b675df02SQu Wenruo }
1181b675df02SQu Wenruo
1182058e09e6SQu Wenruo /*
1183058e09e6SQu Wenruo * Submit the write bio(s) for the sectors specified by @write_bitmap.
1184058e09e6SQu Wenruo *
1185058e09e6SQu Wenruo * Here we utilize btrfs_submit_repair_write(), which has some extra benefits:
1186058e09e6SQu Wenruo *
1187058e09e6SQu Wenruo * - Only needs logical bytenr and mirror_num
1188058e09e6SQu Wenruo * Just like the scrub read path
1189058e09e6SQu Wenruo *
1190058e09e6SQu Wenruo * - Would only result in writes to the specified mirror
1191058e09e6SQu Wenruo * Unlike the regular writeback path, which would write back to all stripes
1192058e09e6SQu Wenruo *
1193058e09e6SQu Wenruo * - Handle dev-replace and read-repair writeback differently
1194058e09e6SQu Wenruo */
scrub_write_sectors(struct scrub_ctx * sctx,struct scrub_stripe * stripe,unsigned long write_bitmap,bool dev_replace)119554765392SQu Wenruo static void scrub_write_sectors(struct scrub_ctx *sctx, struct scrub_stripe *stripe,
1196058e09e6SQu Wenruo unsigned long write_bitmap, bool dev_replace)
1197058e09e6SQu Wenruo {
1198058e09e6SQu Wenruo struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
1199058e09e6SQu Wenruo struct btrfs_bio *bbio = NULL;
1200058e09e6SQu Wenruo int sector_nr;
1201058e09e6SQu Wenruo
1202058e09e6SQu Wenruo for_each_set_bit(sector_nr, &write_bitmap, stripe->nr_sectors) {
1203058e09e6SQu Wenruo struct page *page = scrub_stripe_get_page(stripe, sector_nr);
1204058e09e6SQu Wenruo unsigned int pgoff = scrub_stripe_get_page_offset(stripe, sector_nr);
1205058e09e6SQu Wenruo int ret;
1206058e09e6SQu Wenruo
1207058e09e6SQu Wenruo /* We should only writeback sectors covered by an extent. */
1208058e09e6SQu Wenruo ASSERT(test_bit(sector_nr, &stripe->extent_sector_bitmap));
1209058e09e6SQu Wenruo
1210058e09e6SQu Wenruo /* Cannot merge with previous sector, submit the current one. */
1211058e09e6SQu Wenruo if (bbio && sector_nr && !test_bit(sector_nr - 1, &write_bitmap)) {
1212b675df02SQu Wenruo scrub_submit_write_bio(sctx, stripe, bbio, dev_replace);
1213058e09e6SQu Wenruo bbio = NULL;
1214058e09e6SQu Wenruo }
1215058e09e6SQu Wenruo if (!bbio) {
1216058e09e6SQu Wenruo bbio = btrfs_bio_alloc(stripe->nr_sectors, REQ_OP_WRITE,
1217058e09e6SQu Wenruo fs_info, scrub_write_endio, stripe);
1218058e09e6SQu Wenruo bbio->bio.bi_iter.bi_sector = (stripe->logical +
1219058e09e6SQu Wenruo (sector_nr << fs_info->sectorsize_bits)) >>
1220058e09e6SQu Wenruo SECTOR_SHIFT;
1221058e09e6SQu Wenruo }
1222058e09e6SQu Wenruo ret = bio_add_page(&bbio->bio, page, fs_info->sectorsize, pgoff);
1223058e09e6SQu Wenruo ASSERT(ret == fs_info->sectorsize);
1224058e09e6SQu Wenruo }
1225b675df02SQu Wenruo if (bbio)
1226b675df02SQu Wenruo scrub_submit_write_bio(sctx, stripe, bbio, dev_replace);
1227058e09e6SQu Wenruo }
1228058e09e6SQu Wenruo
122913a62fd9SQu Wenruo /*
123013a62fd9SQu Wenruo * Throttling of IO submission, bandwidth-limit based, the timeslice is 1
123113a62fd9SQu Wenruo * second. Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max.
123213a62fd9SQu Wenruo */
scrub_throttle_dev_io(struct scrub_ctx * sctx,struct btrfs_device * device,unsigned int bio_size)1233e02ee89bSQu Wenruo static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *device,
1234e02ee89bSQu Wenruo unsigned int bio_size)
1235eb3b5053SDavid Sterba {
1236eb3b5053SDavid Sterba const int time_slice = 1000;
1237eb3b5053SDavid Sterba s64 delta;
1238eb3b5053SDavid Sterba ktime_t now;
1239eb3b5053SDavid Sterba u32 div;
1240eb3b5053SDavid Sterba u64 bwlimit;
1241eb3b5053SDavid Sterba
1242eb3b5053SDavid Sterba bwlimit = READ_ONCE(device->scrub_speed_max);
1243eb3b5053SDavid Sterba if (bwlimit == 0)
1244eb3b5053SDavid Sterba return;
1245eb3b5053SDavid Sterba
1246eb3b5053SDavid Sterba /*
1247eb3b5053SDavid Sterba * Slice is divided into intervals when the IO is submitted, adjust by
1248eb3b5053SDavid Sterba * bwlimit and maximum of 64 intervals.
1249eb3b5053SDavid Sterba */
1250eb3b5053SDavid Sterba div = max_t(u32, 1, (u32)(bwlimit / (16 * 1024 * 1024)));
1251eb3b5053SDavid Sterba div = min_t(u32, 64, div);
1252eb3b5053SDavid Sterba
1253eb3b5053SDavid Sterba /* Start new epoch, set deadline */
1254eb3b5053SDavid Sterba now = ktime_get();
1255eb3b5053SDavid Sterba if (sctx->throttle_deadline == 0) {
1256eb3b5053SDavid Sterba sctx->throttle_deadline = ktime_add_ms(now, time_slice / div);
1257eb3b5053SDavid Sterba sctx->throttle_sent = 0;
1258eb3b5053SDavid Sterba }
1259eb3b5053SDavid Sterba
1260eb3b5053SDavid Sterba /* Still in the time to send? */
1261eb3b5053SDavid Sterba if (ktime_before(now, sctx->throttle_deadline)) {
1262eb3b5053SDavid Sterba /* If current bio is within the limit, send it */
1263e02ee89bSQu Wenruo sctx->throttle_sent += bio_size;
1264eb3b5053SDavid Sterba if (sctx->throttle_sent <= div_u64(bwlimit, div))
1265eb3b5053SDavid Sterba return;
1266eb3b5053SDavid Sterba
1267eb3b5053SDavid Sterba /* We're over the limit, sleep until the rest of the slice */
1268eb3b5053SDavid Sterba delta = ktime_ms_delta(sctx->throttle_deadline, now);
1269eb3b5053SDavid Sterba } else {
1270eb3b5053SDavid Sterba /* New request after deadline, start new epoch */
1271eb3b5053SDavid Sterba delta = 0;
1272eb3b5053SDavid Sterba }
1273eb3b5053SDavid Sterba
1274eb3b5053SDavid Sterba if (delta) {
1275eb3b5053SDavid Sterba long timeout;
1276eb3b5053SDavid Sterba
1277eb3b5053SDavid Sterba timeout = div_u64(delta * HZ, 1000);
1278eb3b5053SDavid Sterba schedule_timeout_interruptible(timeout);
1279eb3b5053SDavid Sterba }
1280eb3b5053SDavid Sterba
1281eb3b5053SDavid Sterba /* Next call will start the deadline period */
1282eb3b5053SDavid Sterba sctx->throttle_deadline = 0;
1283eb3b5053SDavid Sterba }
1284eb3b5053SDavid Sterba
1285e02ee89bSQu Wenruo /*
12863b080b25SWang Shilong * Given a physical address, this will calculate it's
12873b080b25SWang Shilong * logical offset. if this is a parity stripe, it will return
12883b080b25SWang Shilong * the most left data stripe's logical offset.
12893b080b25SWang Shilong *
12903b080b25SWang Shilong * return 0 if it is a data stripe, 1 means parity stripe.
12913b080b25SWang Shilong */
get_raid56_logic_offset(u64 physical,int num,struct map_lookup * map,u64 * offset,u64 * stripe_start)12923b080b25SWang Shilong static int get_raid56_logic_offset(u64 physical, int num,
12935a6ac9eaSMiao Xie struct map_lookup *map, u64 *offset,
12945a6ac9eaSMiao Xie u64 *stripe_start)
12953b080b25SWang Shilong {
12963b080b25SWang Shilong int i;
12973b080b25SWang Shilong int j = 0;
12983b080b25SWang Shilong u64 last_offset;
1299cff82672SDavid Sterba const int data_stripes = nr_data_stripes(map);
13003b080b25SWang Shilong
1301cff82672SDavid Sterba last_offset = (physical - map->stripes[num].physical) * data_stripes;
13025a6ac9eaSMiao Xie if (stripe_start)
13035a6ac9eaSMiao Xie *stripe_start = last_offset;
13045a6ac9eaSMiao Xie
13053b080b25SWang Shilong *offset = last_offset;
1306cff82672SDavid Sterba for (i = 0; i < data_stripes; i++) {
13076ded22c1SQu Wenruo u32 stripe_nr;
13086ded22c1SQu Wenruo u32 stripe_index;
13096ded22c1SQu Wenruo u32 rot;
13106ded22c1SQu Wenruo
1311cb091225SQu Wenruo *offset = last_offset + btrfs_stripe_nr_to_offset(i);
13123b080b25SWang Shilong
13136ded22c1SQu Wenruo stripe_nr = (u32)(*offset >> BTRFS_STRIPE_LEN_SHIFT) / data_stripes;
13143b080b25SWang Shilong
13153b080b25SWang Shilong /* Work out the disk rotation on this stripe-set */
13166ded22c1SQu Wenruo rot = stripe_nr % map->num_stripes;
13173b080b25SWang Shilong /* calculate which stripe this data locates */
13183b080b25SWang Shilong rot += i;
1319e4fbaee2SWang Shilong stripe_index = rot % map->num_stripes;
13203b080b25SWang Shilong if (stripe_index == num)
13213b080b25SWang Shilong return 0;
13223b080b25SWang Shilong if (stripe_index < num)
13233b080b25SWang Shilong j++;
13243b080b25SWang Shilong }
1325cb091225SQu Wenruo *offset = last_offset + btrfs_stripe_nr_to_offset(j);
13263b080b25SWang Shilong return 1;
13273b080b25SWang Shilong }
13283b080b25SWang Shilong
1329416bd7e7SQu Wenruo /*
1330416bd7e7SQu Wenruo * Return 0 if the extent item range covers any byte of the range.
1331416bd7e7SQu Wenruo * Return <0 if the extent item is before @search_start.
1332416bd7e7SQu Wenruo * Return >0 if the extent item is after @start_start + @search_len.
1333416bd7e7SQu Wenruo */
compare_extent_item_range(struct btrfs_path * path,u64 search_start,u64 search_len)1334416bd7e7SQu Wenruo static int compare_extent_item_range(struct btrfs_path *path,
1335416bd7e7SQu Wenruo u64 search_start, u64 search_len)
1336416bd7e7SQu Wenruo {
1337416bd7e7SQu Wenruo struct btrfs_fs_info *fs_info = path->nodes[0]->fs_info;
1338416bd7e7SQu Wenruo u64 len;
1339416bd7e7SQu Wenruo struct btrfs_key key;
1340416bd7e7SQu Wenruo
1341416bd7e7SQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1342416bd7e7SQu Wenruo ASSERT(key.type == BTRFS_EXTENT_ITEM_KEY ||
1343416bd7e7SQu Wenruo key.type == BTRFS_METADATA_ITEM_KEY);
1344416bd7e7SQu Wenruo if (key.type == BTRFS_METADATA_ITEM_KEY)
1345416bd7e7SQu Wenruo len = fs_info->nodesize;
1346416bd7e7SQu Wenruo else
1347416bd7e7SQu Wenruo len = key.offset;
1348416bd7e7SQu Wenruo
1349416bd7e7SQu Wenruo if (key.objectid + len <= search_start)
1350416bd7e7SQu Wenruo return -1;
1351416bd7e7SQu Wenruo if (key.objectid >= search_start + search_len)
1352416bd7e7SQu Wenruo return 1;
1353416bd7e7SQu Wenruo return 0;
1354416bd7e7SQu Wenruo }
1355416bd7e7SQu Wenruo
1356416bd7e7SQu Wenruo /*
1357416bd7e7SQu Wenruo * Locate one extent item which covers any byte in range
1358416bd7e7SQu Wenruo * [@search_start, @search_start + @search_length)
1359416bd7e7SQu Wenruo *
1360416bd7e7SQu Wenruo * If the path is not initialized, we will initialize the search by doing
1361416bd7e7SQu Wenruo * a btrfs_search_slot().
1362416bd7e7SQu Wenruo * If the path is already initialized, we will use the path as the initial
1363416bd7e7SQu Wenruo * slot, to avoid duplicated btrfs_search_slot() calls.
1364416bd7e7SQu Wenruo *
1365416bd7e7SQu Wenruo * NOTE: If an extent item starts before @search_start, we will still
1366416bd7e7SQu Wenruo * return the extent item. This is for data extent crossing stripe boundary.
1367416bd7e7SQu Wenruo *
1368416bd7e7SQu Wenruo * Return 0 if we found such extent item, and @path will point to the extent item.
1369416bd7e7SQu Wenruo * Return >0 if no such extent item can be found, and @path will be released.
1370416bd7e7SQu Wenruo * Return <0 if hit fatal error, and @path will be released.
1371416bd7e7SQu Wenruo */
find_first_extent_item(struct btrfs_root * extent_root,struct btrfs_path * path,u64 search_start,u64 search_len)1372416bd7e7SQu Wenruo static int find_first_extent_item(struct btrfs_root *extent_root,
1373416bd7e7SQu Wenruo struct btrfs_path *path,
1374416bd7e7SQu Wenruo u64 search_start, u64 search_len)
1375416bd7e7SQu Wenruo {
1376416bd7e7SQu Wenruo struct btrfs_fs_info *fs_info = extent_root->fs_info;
1377416bd7e7SQu Wenruo struct btrfs_key key;
1378416bd7e7SQu Wenruo int ret;
1379416bd7e7SQu Wenruo
1380416bd7e7SQu Wenruo /* Continue using the existing path */
1381416bd7e7SQu Wenruo if (path->nodes[0])
1382416bd7e7SQu Wenruo goto search_forward;
1383416bd7e7SQu Wenruo
1384416bd7e7SQu Wenruo if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1385416bd7e7SQu Wenruo key.type = BTRFS_METADATA_ITEM_KEY;
1386416bd7e7SQu Wenruo else
1387416bd7e7SQu Wenruo key.type = BTRFS_EXTENT_ITEM_KEY;
1388416bd7e7SQu Wenruo key.objectid = search_start;
1389416bd7e7SQu Wenruo key.offset = (u64)-1;
1390416bd7e7SQu Wenruo
1391416bd7e7SQu Wenruo ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1392416bd7e7SQu Wenruo if (ret < 0)
1393416bd7e7SQu Wenruo return ret;
1394416bd7e7SQu Wenruo
1395416bd7e7SQu Wenruo ASSERT(ret > 0);
1396416bd7e7SQu Wenruo /*
1397416bd7e7SQu Wenruo * Here we intentionally pass 0 as @min_objectid, as there could be
1398416bd7e7SQu Wenruo * an extent item starting before @search_start.
1399416bd7e7SQu Wenruo */
1400416bd7e7SQu Wenruo ret = btrfs_previous_extent_item(extent_root, path, 0);
1401416bd7e7SQu Wenruo if (ret < 0)
1402416bd7e7SQu Wenruo return ret;
1403416bd7e7SQu Wenruo /*
1404416bd7e7SQu Wenruo * No matter whether we have found an extent item, the next loop will
1405416bd7e7SQu Wenruo * properly do every check on the key.
1406416bd7e7SQu Wenruo */
1407416bd7e7SQu Wenruo search_forward:
1408416bd7e7SQu Wenruo while (true) {
1409416bd7e7SQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1410416bd7e7SQu Wenruo if (key.objectid >= search_start + search_len)
1411416bd7e7SQu Wenruo break;
1412416bd7e7SQu Wenruo if (key.type != BTRFS_METADATA_ITEM_KEY &&
1413416bd7e7SQu Wenruo key.type != BTRFS_EXTENT_ITEM_KEY)
1414416bd7e7SQu Wenruo goto next;
1415416bd7e7SQu Wenruo
1416416bd7e7SQu Wenruo ret = compare_extent_item_range(path, search_start, search_len);
1417416bd7e7SQu Wenruo if (ret == 0)
1418416bd7e7SQu Wenruo return ret;
1419416bd7e7SQu Wenruo if (ret > 0)
1420416bd7e7SQu Wenruo break;
1421416bd7e7SQu Wenruo next:
1422416bd7e7SQu Wenruo path->slots[0]++;
1423416bd7e7SQu Wenruo if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
1424416bd7e7SQu Wenruo ret = btrfs_next_leaf(extent_root, path);
1425416bd7e7SQu Wenruo if (ret) {
1426416bd7e7SQu Wenruo /* Either no more item or fatal error */
1427416bd7e7SQu Wenruo btrfs_release_path(path);
1428416bd7e7SQu Wenruo return ret;
1429416bd7e7SQu Wenruo }
1430416bd7e7SQu Wenruo }
1431416bd7e7SQu Wenruo }
1432416bd7e7SQu Wenruo btrfs_release_path(path);
1433416bd7e7SQu Wenruo return 1;
1434416bd7e7SQu Wenruo }
1435416bd7e7SQu Wenruo
get_extent_info(struct btrfs_path * path,u64 * extent_start_ret,u64 * size_ret,u64 * flags_ret,u64 * generation_ret)143609022b14SQu Wenruo static void get_extent_info(struct btrfs_path *path, u64 *extent_start_ret,
143709022b14SQu Wenruo u64 *size_ret, u64 *flags_ret, u64 *generation_ret)
143809022b14SQu Wenruo {
143909022b14SQu Wenruo struct btrfs_key key;
144009022b14SQu Wenruo struct btrfs_extent_item *ei;
144109022b14SQu Wenruo
144209022b14SQu Wenruo btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
144309022b14SQu Wenruo ASSERT(key.type == BTRFS_METADATA_ITEM_KEY ||
144409022b14SQu Wenruo key.type == BTRFS_EXTENT_ITEM_KEY);
144509022b14SQu Wenruo *extent_start_ret = key.objectid;
144609022b14SQu Wenruo if (key.type == BTRFS_METADATA_ITEM_KEY)
144709022b14SQu Wenruo *size_ret = path->nodes[0]->fs_info->nodesize;
144809022b14SQu Wenruo else
144909022b14SQu Wenruo *size_ret = key.offset;
145009022b14SQu Wenruo ei = btrfs_item_ptr(path->nodes[0], path->slots[0], struct btrfs_extent_item);
145109022b14SQu Wenruo *flags_ret = btrfs_extent_flags(path->nodes[0], ei);
145209022b14SQu Wenruo *generation_ret = btrfs_extent_generation(path->nodes[0], ei);
145309022b14SQu Wenruo }
145409022b14SQu Wenruo
sync_write_pointer_for_zoned(struct scrub_ctx * sctx,u64 logical,u64 physical,u64 physical_end)14557db1c5d1SNaohiro Aota static int sync_write_pointer_for_zoned(struct scrub_ctx *sctx, u64 logical,
14567db1c5d1SNaohiro Aota u64 physical, u64 physical_end)
14577db1c5d1SNaohiro Aota {
14587db1c5d1SNaohiro Aota struct btrfs_fs_info *fs_info = sctx->fs_info;
14597db1c5d1SNaohiro Aota int ret = 0;
14607db1c5d1SNaohiro Aota
14617db1c5d1SNaohiro Aota if (!btrfs_is_zoned(fs_info))
14627db1c5d1SNaohiro Aota return 0;
14637db1c5d1SNaohiro Aota
14647db1c5d1SNaohiro Aota mutex_lock(&sctx->wr_lock);
14657db1c5d1SNaohiro Aota if (sctx->write_pointer < physical_end) {
14667db1c5d1SNaohiro Aota ret = btrfs_sync_zone_write_pointer(sctx->wr_tgtdev, logical,
14677db1c5d1SNaohiro Aota physical,
14687db1c5d1SNaohiro Aota sctx->write_pointer);
14697db1c5d1SNaohiro Aota if (ret)
14707db1c5d1SNaohiro Aota btrfs_err(fs_info,
14717db1c5d1SNaohiro Aota "zoned: failed to recover write pointer");
14727db1c5d1SNaohiro Aota }
14737db1c5d1SNaohiro Aota mutex_unlock(&sctx->wr_lock);
14747db1c5d1SNaohiro Aota btrfs_dev_clear_zone_empty(sctx->wr_tgtdev, physical);
14757db1c5d1SNaohiro Aota
14767db1c5d1SNaohiro Aota return ret;
14777db1c5d1SNaohiro Aota }
14787db1c5d1SNaohiro Aota
fill_one_extent_info(struct btrfs_fs_info * fs_info,struct scrub_stripe * stripe,u64 extent_start,u64 extent_len,u64 extent_flags,u64 extent_gen)1479b9795475SQu Wenruo static void fill_one_extent_info(struct btrfs_fs_info *fs_info,
1480b9795475SQu Wenruo struct scrub_stripe *stripe,
1481b9795475SQu Wenruo u64 extent_start, u64 extent_len,
1482b9795475SQu Wenruo u64 extent_flags, u64 extent_gen)
1483b9795475SQu Wenruo {
1484b9795475SQu Wenruo for (u64 cur_logical = max(stripe->logical, extent_start);
1485b9795475SQu Wenruo cur_logical < min(stripe->logical + BTRFS_STRIPE_LEN,
1486b9795475SQu Wenruo extent_start + extent_len);
1487b9795475SQu Wenruo cur_logical += fs_info->sectorsize) {
1488b9795475SQu Wenruo const int nr_sector = (cur_logical - stripe->logical) >>
1489b9795475SQu Wenruo fs_info->sectorsize_bits;
1490b9795475SQu Wenruo struct scrub_sector_verification *sector =
1491b9795475SQu Wenruo &stripe->sectors[nr_sector];
1492b9795475SQu Wenruo
1493b9795475SQu Wenruo set_bit(nr_sector, &stripe->extent_sector_bitmap);
1494b9795475SQu Wenruo if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1495b9795475SQu Wenruo sector->is_metadata = true;
1496b9795475SQu Wenruo sector->generation = extent_gen;
1497b9795475SQu Wenruo }
1498b9795475SQu Wenruo }
1499b9795475SQu Wenruo }
1500b9795475SQu Wenruo
scrub_stripe_reset_bitmaps(struct scrub_stripe * stripe)1501b9795475SQu Wenruo static void scrub_stripe_reset_bitmaps(struct scrub_stripe *stripe)
1502b9795475SQu Wenruo {
1503b9795475SQu Wenruo stripe->extent_sector_bitmap = 0;
1504b9795475SQu Wenruo stripe->init_error_bitmap = 0;
150579b8ee70SQu Wenruo stripe->init_nr_io_errors = 0;
150679b8ee70SQu Wenruo stripe->init_nr_csum_errors = 0;
150779b8ee70SQu Wenruo stripe->init_nr_meta_errors = 0;
1508b9795475SQu Wenruo stripe->error_bitmap = 0;
1509b9795475SQu Wenruo stripe->io_error_bitmap = 0;
1510b9795475SQu Wenruo stripe->csum_error_bitmap = 0;
1511b9795475SQu Wenruo stripe->meta_error_bitmap = 0;
1512b9795475SQu Wenruo }
1513b9795475SQu Wenruo
1514b9795475SQu Wenruo /*
1515b9795475SQu Wenruo * Locate one stripe which has at least one extent in its range.
1516b9795475SQu Wenruo *
1517b9795475SQu Wenruo * Return 0 if found such stripe, and store its info into @stripe.
1518b9795475SQu Wenruo * Return >0 if there is no such stripe in the specified range.
1519b9795475SQu Wenruo * Return <0 for error.
1520b9795475SQu Wenruo */
scrub_find_fill_first_stripe(struct btrfs_block_group * bg,struct btrfs_path * extent_path,struct btrfs_path * csum_path,struct btrfs_device * dev,u64 physical,int mirror_num,u64 logical_start,u32 logical_len,struct scrub_stripe * stripe)152154765392SQu Wenruo static int scrub_find_fill_first_stripe(struct btrfs_block_group *bg,
15221dc4888eSQu Wenruo struct btrfs_path *extent_path,
15233c771c19SQu Wenruo struct btrfs_path *csum_path,
1524b9795475SQu Wenruo struct btrfs_device *dev, u64 physical,
1525b9795475SQu Wenruo int mirror_num, u64 logical_start,
152654765392SQu Wenruo u32 logical_len,
152754765392SQu Wenruo struct scrub_stripe *stripe)
1528b9795475SQu Wenruo {
1529b9795475SQu Wenruo struct btrfs_fs_info *fs_info = bg->fs_info;
1530b9795475SQu Wenruo struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bg->start);
1531b9795475SQu Wenruo struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bg->start);
1532b9795475SQu Wenruo const u64 logical_end = logical_start + logical_len;
1533b9795475SQu Wenruo u64 cur_logical = logical_start;
1534b9795475SQu Wenruo u64 stripe_end;
1535b9795475SQu Wenruo u64 extent_start;
1536b9795475SQu Wenruo u64 extent_len;
1537b9795475SQu Wenruo u64 extent_flags;
1538b9795475SQu Wenruo u64 extent_gen;
1539b9795475SQu Wenruo int ret;
1540b9795475SQu Wenruo
1541*24b85a8bSQu Wenruo if (unlikely(!extent_root)) {
1542*24b85a8bSQu Wenruo btrfs_err(fs_info, "no valid extent root for scrub");
1543*24b85a8bSQu Wenruo return -EUCLEAN;
1544*24b85a8bSQu Wenruo }
1545b9795475SQu Wenruo memset(stripe->sectors, 0, sizeof(struct scrub_sector_verification) *
1546b9795475SQu Wenruo stripe->nr_sectors);
1547b9795475SQu Wenruo scrub_stripe_reset_bitmaps(stripe);
1548b9795475SQu Wenruo
1549b9795475SQu Wenruo /* The range must be inside the bg. */
1550b9795475SQu Wenruo ASSERT(logical_start >= bg->start && logical_end <= bg->start + bg->length);
1551b9795475SQu Wenruo
15521dc4888eSQu Wenruo ret = find_first_extent_item(extent_root, extent_path, logical_start,
15531dc4888eSQu Wenruo logical_len);
1554b9795475SQu Wenruo /* Either error or not found. */
1555b9795475SQu Wenruo if (ret)
1556b9795475SQu Wenruo goto out;
15571dc4888eSQu Wenruo get_extent_info(extent_path, &extent_start, &extent_len, &extent_flags,
15581dc4888eSQu Wenruo &extent_gen);
155900965807SQu Wenruo if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
156000965807SQu Wenruo stripe->nr_meta_extents++;
156100965807SQu Wenruo if (extent_flags & BTRFS_EXTENT_FLAG_DATA)
156200965807SQu Wenruo stripe->nr_data_extents++;
1563b9795475SQu Wenruo cur_logical = max(extent_start, cur_logical);
1564b9795475SQu Wenruo
1565b9795475SQu Wenruo /*
1566b9795475SQu Wenruo * Round down to stripe boundary.
1567b9795475SQu Wenruo *
1568b9795475SQu Wenruo * The extra calculation against bg->start is to handle block groups
1569b9795475SQu Wenruo * whose logical bytenr is not BTRFS_STRIPE_LEN aligned.
1570b9795475SQu Wenruo */
1571b9795475SQu Wenruo stripe->logical = round_down(cur_logical - bg->start, BTRFS_STRIPE_LEN) +
1572b9795475SQu Wenruo bg->start;
1573b9795475SQu Wenruo stripe->physical = physical + stripe->logical - logical_start;
1574b9795475SQu Wenruo stripe->dev = dev;
1575b9795475SQu Wenruo stripe->bg = bg;
1576b9795475SQu Wenruo stripe->mirror_num = mirror_num;
1577b9795475SQu Wenruo stripe_end = stripe->logical + BTRFS_STRIPE_LEN - 1;
1578b9795475SQu Wenruo
1579b9795475SQu Wenruo /* Fill the first extent info into stripe->sectors[] array. */
1580b9795475SQu Wenruo fill_one_extent_info(fs_info, stripe, extent_start, extent_len,
1581b9795475SQu Wenruo extent_flags, extent_gen);
1582b9795475SQu Wenruo cur_logical = extent_start + extent_len;
1583b9795475SQu Wenruo
1584b9795475SQu Wenruo /* Fill the extent info for the remaining sectors. */
1585b9795475SQu Wenruo while (cur_logical <= stripe_end) {
15861dc4888eSQu Wenruo ret = find_first_extent_item(extent_root, extent_path, cur_logical,
1587b9795475SQu Wenruo stripe_end - cur_logical + 1);
1588b9795475SQu Wenruo if (ret < 0)
1589b9795475SQu Wenruo goto out;
1590b9795475SQu Wenruo if (ret > 0) {
1591b9795475SQu Wenruo ret = 0;
1592b9795475SQu Wenruo break;
1593b9795475SQu Wenruo }
15941dc4888eSQu Wenruo get_extent_info(extent_path, &extent_start, &extent_len,
1595b9795475SQu Wenruo &extent_flags, &extent_gen);
159600965807SQu Wenruo if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
159700965807SQu Wenruo stripe->nr_meta_extents++;
159800965807SQu Wenruo if (extent_flags & BTRFS_EXTENT_FLAG_DATA)
159900965807SQu Wenruo stripe->nr_data_extents++;
1600b9795475SQu Wenruo fill_one_extent_info(fs_info, stripe, extent_start, extent_len,
1601b9795475SQu Wenruo extent_flags, extent_gen);
1602b9795475SQu Wenruo cur_logical = extent_start + extent_len;
1603b9795475SQu Wenruo }
1604b9795475SQu Wenruo
1605b9795475SQu Wenruo /* Now fill the data csum. */
1606b9795475SQu Wenruo if (bg->flags & BTRFS_BLOCK_GROUP_DATA) {
1607b9795475SQu Wenruo int sector_nr;
1608b9795475SQu Wenruo unsigned long csum_bitmap = 0;
1609b9795475SQu Wenruo
1610b9795475SQu Wenruo /* Csum space should have already been allocated. */
1611b9795475SQu Wenruo ASSERT(stripe->csums);
1612b9795475SQu Wenruo
1613b9795475SQu Wenruo /*
1614b9795475SQu Wenruo * Our csum bitmap should be large enough, as BTRFS_STRIPE_LEN
1615b9795475SQu Wenruo * should contain at most 16 sectors.
1616b9795475SQu Wenruo */
1617b9795475SQu Wenruo ASSERT(BITS_PER_LONG >= BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
1618b9795475SQu Wenruo
16193c771c19SQu Wenruo ret = btrfs_lookup_csums_bitmap(csum_root, csum_path,
16203c771c19SQu Wenruo stripe->logical, stripe_end,
16213c771c19SQu Wenruo stripe->csums, &csum_bitmap);
1622b9795475SQu Wenruo if (ret < 0)
1623b9795475SQu Wenruo goto out;
1624b9795475SQu Wenruo if (ret > 0)
1625b9795475SQu Wenruo ret = 0;
1626b9795475SQu Wenruo
1627b9795475SQu Wenruo for_each_set_bit(sector_nr, &csum_bitmap, stripe->nr_sectors) {
1628b9795475SQu Wenruo stripe->sectors[sector_nr].csum = stripe->csums +
1629b9795475SQu Wenruo sector_nr * fs_info->csum_size;
1630b9795475SQu Wenruo }
1631b9795475SQu Wenruo }
1632b9795475SQu Wenruo set_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state);
1633b9795475SQu Wenruo out:
1634b9795475SQu Wenruo return ret;
1635b9795475SQu Wenruo }
1636b9795475SQu Wenruo
scrub_reset_stripe(struct scrub_stripe * stripe)163754765392SQu Wenruo static void scrub_reset_stripe(struct scrub_stripe *stripe)
163854765392SQu Wenruo {
163954765392SQu Wenruo scrub_stripe_reset_bitmaps(stripe);
164054765392SQu Wenruo
164154765392SQu Wenruo stripe->nr_meta_extents = 0;
164254765392SQu Wenruo stripe->nr_data_extents = 0;
164354765392SQu Wenruo stripe->state = 0;
164454765392SQu Wenruo
164554765392SQu Wenruo for (int i = 0; i < stripe->nr_sectors; i++) {
164654765392SQu Wenruo stripe->sectors[i].is_metadata = false;
164754765392SQu Wenruo stripe->sectors[i].csum = NULL;
164854765392SQu Wenruo stripe->sectors[i].generation = 0;
164954765392SQu Wenruo }
165054765392SQu Wenruo }
165154765392SQu Wenruo
scrub_submit_initial_read(struct scrub_ctx * sctx,struct scrub_stripe * stripe)165254765392SQu Wenruo static void scrub_submit_initial_read(struct scrub_ctx *sctx,
165354765392SQu Wenruo struct scrub_stripe *stripe)
165454765392SQu Wenruo {
165554765392SQu Wenruo struct btrfs_fs_info *fs_info = sctx->fs_info;
165654765392SQu Wenruo struct btrfs_bio *bbio;
1657642b9c52SQu Wenruo unsigned int nr_sectors = min_t(u64, BTRFS_STRIPE_LEN, stripe->bg->start +
1658642b9c52SQu Wenruo stripe->bg->length - stripe->logical) >>
1659642b9c52SQu Wenruo fs_info->sectorsize_bits;
166054765392SQu Wenruo int mirror = stripe->mirror_num;
166154765392SQu Wenruo
166254765392SQu Wenruo ASSERT(stripe->bg);
166354765392SQu Wenruo ASSERT(stripe->mirror_num > 0);
166454765392SQu Wenruo ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state));
166554765392SQu Wenruo
166654765392SQu Wenruo bbio = btrfs_bio_alloc(SCRUB_STRIPE_PAGES, REQ_OP_READ, fs_info,
166754765392SQu Wenruo scrub_read_endio, stripe);
166854765392SQu Wenruo
166954765392SQu Wenruo bbio->bio.bi_iter.bi_sector = stripe->logical >> SECTOR_SHIFT;
1670642b9c52SQu Wenruo /* Read the whole range inside the chunk boundary. */
1671642b9c52SQu Wenruo for (unsigned int cur = 0; cur < nr_sectors; cur++) {
1672642b9c52SQu Wenruo struct page *page = scrub_stripe_get_page(stripe, cur);
1673642b9c52SQu Wenruo unsigned int pgoff = scrub_stripe_get_page_offset(stripe, cur);
167454765392SQu Wenruo int ret;
167554765392SQu Wenruo
1676642b9c52SQu Wenruo ret = bio_add_page(&bbio->bio, page, fs_info->sectorsize, pgoff);
167754765392SQu Wenruo /* We should have allocated enough bio vectors. */
1678642b9c52SQu Wenruo ASSERT(ret == fs_info->sectorsize);
167954765392SQu Wenruo }
168054765392SQu Wenruo atomic_inc(&stripe->pending_io);
168154765392SQu Wenruo
168254765392SQu Wenruo /*
168354765392SQu Wenruo * For dev-replace, either user asks to avoid the source dev, or
168454765392SQu Wenruo * the device is missing, we try the next mirror instead.
168554765392SQu Wenruo */
168654765392SQu Wenruo if (sctx->is_dev_replace &&
168754765392SQu Wenruo (fs_info->dev_replace.cont_reading_from_srcdev_mode ==
168854765392SQu Wenruo BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID ||
168954765392SQu Wenruo !stripe->dev->bdev)) {
169054765392SQu Wenruo int num_copies = btrfs_num_copies(fs_info, stripe->bg->start,
169154765392SQu Wenruo stripe->bg->length);
169254765392SQu Wenruo
169354765392SQu Wenruo mirror = calc_next_mirror(mirror, num_copies);
169454765392SQu Wenruo }
169554765392SQu Wenruo btrfs_submit_bio(bbio, mirror);
169654765392SQu Wenruo }
169754765392SQu Wenruo
stripe_has_metadata_error(struct scrub_stripe * stripe)16988eb3dd17SQu Wenruo static bool stripe_has_metadata_error(struct scrub_stripe *stripe)
16998eb3dd17SQu Wenruo {
17008eb3dd17SQu Wenruo int i;
17018eb3dd17SQu Wenruo
17028eb3dd17SQu Wenruo for_each_set_bit(i, &stripe->error_bitmap, stripe->nr_sectors) {
17038eb3dd17SQu Wenruo if (stripe->sectors[i].is_metadata) {
17048eb3dd17SQu Wenruo struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
17058eb3dd17SQu Wenruo
17068eb3dd17SQu Wenruo btrfs_err(fs_info,
17078eb3dd17SQu Wenruo "stripe %llu has unrepaired metadata sector at %llu",
17088eb3dd17SQu Wenruo stripe->logical,
17098eb3dd17SQu Wenruo stripe->logical + (i << fs_info->sectorsize_bits));
17108eb3dd17SQu Wenruo return true;
17118eb3dd17SQu Wenruo }
17128eb3dd17SQu Wenruo }
17138eb3dd17SQu Wenruo return false;
17148eb3dd17SQu Wenruo }
17158eb3dd17SQu Wenruo
submit_initial_group_read(struct scrub_ctx * sctx,unsigned int first_slot,unsigned int nr_stripes)1716ae76d8e3SQu Wenruo static void submit_initial_group_read(struct scrub_ctx *sctx,
1717ae76d8e3SQu Wenruo unsigned int first_slot,
1718ae76d8e3SQu Wenruo unsigned int nr_stripes)
1719ae76d8e3SQu Wenruo {
1720ae76d8e3SQu Wenruo struct blk_plug plug;
1721ae76d8e3SQu Wenruo
1722ae76d8e3SQu Wenruo ASSERT(first_slot < SCRUB_TOTAL_STRIPES);
1723ae76d8e3SQu Wenruo ASSERT(first_slot + nr_stripes <= SCRUB_TOTAL_STRIPES);
1724ae76d8e3SQu Wenruo
1725ae76d8e3SQu Wenruo scrub_throttle_dev_io(sctx, sctx->stripes[0].dev,
1726ae76d8e3SQu Wenruo btrfs_stripe_nr_to_offset(nr_stripes));
1727ae76d8e3SQu Wenruo blk_start_plug(&plug);
1728ae76d8e3SQu Wenruo for (int i = 0; i < nr_stripes; i++) {
1729ae76d8e3SQu Wenruo struct scrub_stripe *stripe = &sctx->stripes[first_slot + i];
1730ae76d8e3SQu Wenruo
1731ae76d8e3SQu Wenruo /* Those stripes should be initialized. */
1732ae76d8e3SQu Wenruo ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state));
1733ae76d8e3SQu Wenruo scrub_submit_initial_read(sctx, stripe);
1734ae76d8e3SQu Wenruo }
1735ae76d8e3SQu Wenruo blk_finish_plug(&plug);
1736ae76d8e3SQu Wenruo }
1737ae76d8e3SQu Wenruo
flush_scrub_stripes(struct scrub_ctx * sctx)17388eb3dd17SQu Wenruo static int flush_scrub_stripes(struct scrub_ctx *sctx)
173954765392SQu Wenruo {
174054765392SQu Wenruo struct btrfs_fs_info *fs_info = sctx->fs_info;
174154765392SQu Wenruo struct scrub_stripe *stripe;
174254765392SQu Wenruo const int nr_stripes = sctx->cur_stripe;
17438eb3dd17SQu Wenruo int ret = 0;
174454765392SQu Wenruo
174554765392SQu Wenruo if (!nr_stripes)
17468eb3dd17SQu Wenruo return 0;
174754765392SQu Wenruo
174854765392SQu Wenruo ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &sctx->stripes[0].state));
1749e02ee89bSQu Wenruo
1750ae76d8e3SQu Wenruo /* Submit the stripes which are populated but not submitted. */
1751ae76d8e3SQu Wenruo if (nr_stripes % SCRUB_STRIPES_PER_GROUP) {
1752ae76d8e3SQu Wenruo const int first_slot = round_down(nr_stripes, SCRUB_STRIPES_PER_GROUP);
1753ae76d8e3SQu Wenruo
1754ae76d8e3SQu Wenruo submit_initial_group_read(sctx, first_slot, nr_stripes - first_slot);
175554765392SQu Wenruo }
175654765392SQu Wenruo
175754765392SQu Wenruo for (int i = 0; i < nr_stripes; i++) {
175854765392SQu Wenruo stripe = &sctx->stripes[i];
175954765392SQu Wenruo
176054765392SQu Wenruo wait_event(stripe->repair_wait,
176154765392SQu Wenruo test_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state));
176254765392SQu Wenruo }
176354765392SQu Wenruo
176454765392SQu Wenruo /* Submit for dev-replace. */
176554765392SQu Wenruo if (sctx->is_dev_replace) {
17668eb3dd17SQu Wenruo /*
17678eb3dd17SQu Wenruo * For dev-replace, if we know there is something wrong with
17688eb3dd17SQu Wenruo * metadata, we should immedately abort.
17698eb3dd17SQu Wenruo */
17708eb3dd17SQu Wenruo for (int i = 0; i < nr_stripes; i++) {
17718eb3dd17SQu Wenruo if (stripe_has_metadata_error(&sctx->stripes[i])) {
17728eb3dd17SQu Wenruo ret = -EIO;
17738eb3dd17SQu Wenruo goto out;
17748eb3dd17SQu Wenruo }
17758eb3dd17SQu Wenruo }
177654765392SQu Wenruo for (int i = 0; i < nr_stripes; i++) {
177754765392SQu Wenruo unsigned long good;
177854765392SQu Wenruo
177954765392SQu Wenruo stripe = &sctx->stripes[i];
178054765392SQu Wenruo
178154765392SQu Wenruo ASSERT(stripe->dev == fs_info->dev_replace.srcdev);
178254765392SQu Wenruo
178354765392SQu Wenruo bitmap_andnot(&good, &stripe->extent_sector_bitmap,
178454765392SQu Wenruo &stripe->error_bitmap, stripe->nr_sectors);
178554765392SQu Wenruo scrub_write_sectors(sctx, stripe, good, true);
178654765392SQu Wenruo }
178754765392SQu Wenruo }
178854765392SQu Wenruo
178954765392SQu Wenruo /* Wait for the above writebacks to finish. */
179054765392SQu Wenruo for (int i = 0; i < nr_stripes; i++) {
179154765392SQu Wenruo stripe = &sctx->stripes[i];
179254765392SQu Wenruo
179354765392SQu Wenruo wait_scrub_stripe_io(stripe);
179454765392SQu Wenruo scrub_reset_stripe(stripe);
179554765392SQu Wenruo }
17968eb3dd17SQu Wenruo out:
179754765392SQu Wenruo sctx->cur_stripe = 0;
17988eb3dd17SQu Wenruo return ret;
179954765392SQu Wenruo }
180054765392SQu Wenruo
raid56_scrub_wait_endio(struct bio * bio)18011009254bSQu Wenruo static void raid56_scrub_wait_endio(struct bio *bio)
18021009254bSQu Wenruo {
18031009254bSQu Wenruo complete(bio->bi_private);
18041009254bSQu Wenruo }
18051009254bSQu Wenruo
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,u64 * found_logical_ret)1806e02ee89bSQu Wenruo static int queue_scrub_stripe(struct scrub_ctx *sctx, struct btrfs_block_group *bg,
180754765392SQu Wenruo struct btrfs_device *dev, int mirror_num,
1808ae76d8e3SQu Wenruo u64 logical, u32 length, u64 physical,
1809ae76d8e3SQu Wenruo u64 *found_logical_ret)
181054765392SQu Wenruo {
181154765392SQu Wenruo struct scrub_stripe *stripe;
181254765392SQu Wenruo int ret;
181354765392SQu Wenruo
1814ae76d8e3SQu Wenruo /*
1815ae76d8e3SQu Wenruo * There should always be one slot left, as caller filling the last
1816ae76d8e3SQu Wenruo * slot should flush them all.
1817ae76d8e3SQu Wenruo */
1818ae76d8e3SQu Wenruo ASSERT(sctx->cur_stripe < SCRUB_TOTAL_STRIPES);
181954765392SQu Wenruo
18206927a91cSQu Wenruo /* @found_logical_ret must be specified. */
18216927a91cSQu Wenruo ASSERT(found_logical_ret);
18226927a91cSQu Wenruo
182354765392SQu Wenruo stripe = &sctx->stripes[sctx->cur_stripe];
182454765392SQu Wenruo scrub_reset_stripe(stripe);
18253c771c19SQu Wenruo ret = scrub_find_fill_first_stripe(bg, &sctx->extent_path,
18263c771c19SQu Wenruo &sctx->csum_path, dev, physical,
18273c771c19SQu Wenruo mirror_num, logical, length, stripe);
182854765392SQu Wenruo /* Either >0 as no more extents or <0 for error. */
182954765392SQu Wenruo if (ret)
183054765392SQu Wenruo return ret;
1831ae76d8e3SQu Wenruo *found_logical_ret = stripe->logical;
183254765392SQu Wenruo sctx->cur_stripe++;
1833ae76d8e3SQu Wenruo
1834ae76d8e3SQu Wenruo /* We filled one group, submit it. */
1835ae76d8e3SQu Wenruo if (sctx->cur_stripe % SCRUB_STRIPES_PER_GROUP == 0) {
1836ae76d8e3SQu Wenruo const int first_slot = sctx->cur_stripe - SCRUB_STRIPES_PER_GROUP;
1837ae76d8e3SQu Wenruo
1838ae76d8e3SQu Wenruo submit_initial_group_read(sctx, first_slot, SCRUB_STRIPES_PER_GROUP);
1839ae76d8e3SQu Wenruo }
1840ae76d8e3SQu Wenruo
1841ae76d8e3SQu Wenruo /* Last slot used, flush them all. */
1842ae76d8e3SQu Wenruo if (sctx->cur_stripe == SCRUB_TOTAL_STRIPES)
1843ae76d8e3SQu Wenruo return flush_scrub_stripes(sctx);
184454765392SQu Wenruo return 0;
184554765392SQu Wenruo }
184654765392SQu Wenruo
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)18471009254bSQu Wenruo static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
18481009254bSQu Wenruo struct btrfs_device *scrub_dev,
18491009254bSQu Wenruo struct btrfs_block_group *bg,
18501009254bSQu Wenruo struct map_lookup *map,
18511009254bSQu Wenruo u64 full_stripe_start)
18521009254bSQu Wenruo {
18531009254bSQu Wenruo DECLARE_COMPLETION_ONSTACK(io_done);
18541009254bSQu Wenruo struct btrfs_fs_info *fs_info = sctx->fs_info;
18551009254bSQu Wenruo struct btrfs_raid_bio *rbio;
18561009254bSQu Wenruo struct btrfs_io_context *bioc = NULL;
18571dc4888eSQu Wenruo struct btrfs_path extent_path = { 0 };
18583c771c19SQu Wenruo struct btrfs_path csum_path = { 0 };
18591009254bSQu Wenruo struct bio *bio;
18601009254bSQu Wenruo struct scrub_stripe *stripe;
18611009254bSQu Wenruo bool all_empty = true;
18621009254bSQu Wenruo const int data_stripes = nr_data_stripes(map);
18631009254bSQu Wenruo unsigned long extent_bitmap = 0;
1864cb091225SQu Wenruo u64 length = btrfs_stripe_nr_to_offset(data_stripes);
18651009254bSQu Wenruo int ret;
18661009254bSQu Wenruo
18671009254bSQu Wenruo ASSERT(sctx->raid56_data_stripes);
18681009254bSQu Wenruo
18691dc4888eSQu Wenruo /*
18703c771c19SQu Wenruo * For data stripe search, we cannot re-use the same extent/csum paths,
18713c771c19SQu Wenruo * as the data stripe bytenr may be smaller than previous extent. Thus
18723c771c19SQu Wenruo * we have to use our own extent/csum paths.
18731dc4888eSQu Wenruo */
18741dc4888eSQu Wenruo extent_path.search_commit_root = 1;
18751dc4888eSQu Wenruo extent_path.skip_locking = 1;
18763c771c19SQu Wenruo csum_path.search_commit_root = 1;
18773c771c19SQu Wenruo csum_path.skip_locking = 1;
18781dc4888eSQu Wenruo
18791009254bSQu Wenruo for (int i = 0; i < data_stripes; i++) {
18801009254bSQu Wenruo int stripe_index;
18811009254bSQu Wenruo int rot;
18821009254bSQu Wenruo u64 physical;
18831009254bSQu Wenruo
18841009254bSQu Wenruo stripe = &sctx->raid56_data_stripes[i];
18851009254bSQu Wenruo rot = div_u64(full_stripe_start - bg->start,
18861009254bSQu Wenruo data_stripes) >> BTRFS_STRIPE_LEN_SHIFT;
18871009254bSQu Wenruo stripe_index = (i + rot) % map->num_stripes;
18881009254bSQu Wenruo physical = map->stripes[stripe_index].physical +
1889cb091225SQu Wenruo btrfs_stripe_nr_to_offset(rot);
18901009254bSQu Wenruo
18911009254bSQu Wenruo scrub_reset_stripe(stripe);
18921009254bSQu Wenruo set_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state);
18933c771c19SQu Wenruo ret = scrub_find_fill_first_stripe(bg, &extent_path, &csum_path,
18941009254bSQu Wenruo map->stripes[stripe_index].dev, physical, 1,
1895cb091225SQu Wenruo full_stripe_start + btrfs_stripe_nr_to_offset(i),
18961009254bSQu Wenruo BTRFS_STRIPE_LEN, stripe);
18971009254bSQu Wenruo if (ret < 0)
18981009254bSQu Wenruo goto out;
18991009254bSQu Wenruo /*
19001009254bSQu Wenruo * No extent in this data stripe, need to manually mark them
19011009254bSQu Wenruo * initialized to make later read submission happy.
19021009254bSQu Wenruo */
19031009254bSQu Wenruo if (ret > 0) {
19041009254bSQu Wenruo stripe->logical = full_stripe_start +
1905cb091225SQu Wenruo btrfs_stripe_nr_to_offset(i);
19061009254bSQu Wenruo stripe->dev = map->stripes[stripe_index].dev;
19071009254bSQu Wenruo stripe->mirror_num = 1;
19081009254bSQu Wenruo set_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state);
19091009254bSQu Wenruo }
19101009254bSQu Wenruo }
19111009254bSQu Wenruo
19121009254bSQu Wenruo /* Check if all data stripes are empty. */
19131009254bSQu Wenruo for (int i = 0; i < data_stripes; i++) {
19141009254bSQu Wenruo stripe = &sctx->raid56_data_stripes[i];
19151009254bSQu Wenruo if (!bitmap_empty(&stripe->extent_sector_bitmap, stripe->nr_sectors)) {
19161009254bSQu Wenruo all_empty = false;
19171009254bSQu Wenruo break;
19181009254bSQu Wenruo }
19191009254bSQu Wenruo }
19201009254bSQu Wenruo if (all_empty) {
19211009254bSQu Wenruo ret = 0;
19221009254bSQu Wenruo goto out;
19231009254bSQu Wenruo }
19241009254bSQu Wenruo
19251009254bSQu Wenruo for (int i = 0; i < data_stripes; i++) {
19261009254bSQu Wenruo stripe = &sctx->raid56_data_stripes[i];
19271009254bSQu Wenruo scrub_submit_initial_read(sctx, stripe);
19281009254bSQu Wenruo }
19291009254bSQu Wenruo for (int i = 0; i < data_stripes; i++) {
19301009254bSQu Wenruo stripe = &sctx->raid56_data_stripes[i];
19311009254bSQu Wenruo
19321009254bSQu Wenruo wait_event(stripe->repair_wait,
19331009254bSQu Wenruo test_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state));
19341009254bSQu Wenruo }
19351009254bSQu Wenruo /* For now, no zoned support for RAID56. */
19361009254bSQu Wenruo ASSERT(!btrfs_is_zoned(sctx->fs_info));
19371009254bSQu Wenruo
19381009254bSQu Wenruo /*
19391009254bSQu Wenruo * Now all data stripes are properly verified. Check if we have any
19401009254bSQu Wenruo * unrepaired, if so abort immediately or we could further corrupt the
19411009254bSQu Wenruo * P/Q stripes.
19421009254bSQu Wenruo *
19431009254bSQu Wenruo * During the loop, also populate extent_bitmap.
19441009254bSQu Wenruo */
19451009254bSQu Wenruo for (int i = 0; i < data_stripes; i++) {
19461009254bSQu Wenruo unsigned long error;
19471009254bSQu Wenruo
19481009254bSQu Wenruo stripe = &sctx->raid56_data_stripes[i];
19491009254bSQu Wenruo
19501009254bSQu Wenruo /*
19511009254bSQu Wenruo * We should only check the errors where there is an extent.
19521009254bSQu Wenruo * As we may hit an empty data stripe while it's missing.
19531009254bSQu Wenruo */
19541009254bSQu Wenruo bitmap_and(&error, &stripe->error_bitmap,
19551009254bSQu Wenruo &stripe->extent_sector_bitmap, stripe->nr_sectors);
19561009254bSQu Wenruo if (!bitmap_empty(&error, stripe->nr_sectors)) {
19571009254bSQu Wenruo btrfs_err(fs_info,
19581009254bSQu Wenruo "unrepaired sectors detected, full stripe %llu data stripe %u errors %*pbl",
19591009254bSQu Wenruo full_stripe_start, i, stripe->nr_sectors,
19601009254bSQu Wenruo &error);
19611009254bSQu Wenruo ret = -EIO;
19621009254bSQu Wenruo goto out;
19631009254bSQu Wenruo }
19641009254bSQu Wenruo bitmap_or(&extent_bitmap, &extent_bitmap,
19651009254bSQu Wenruo &stripe->extent_sector_bitmap, stripe->nr_sectors);
19661009254bSQu Wenruo }
19671009254bSQu Wenruo
19681009254bSQu Wenruo /* Now we can check and regenerate the P/Q stripe. */
19691009254bSQu Wenruo bio = bio_alloc(NULL, 1, REQ_OP_READ, GFP_NOFS);
19701009254bSQu Wenruo bio->bi_iter.bi_sector = full_stripe_start >> SECTOR_SHIFT;
19711009254bSQu Wenruo bio->bi_private = &io_done;
19721009254bSQu Wenruo bio->bi_end_io = raid56_scrub_wait_endio;
19731009254bSQu Wenruo
19741009254bSQu Wenruo btrfs_bio_counter_inc_blocked(fs_info);
1975723b8bb1SChristoph Hellwig ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, full_stripe_start,
1976723b8bb1SChristoph Hellwig &length, &bioc, NULL, NULL, 1);
19771009254bSQu Wenruo if (ret < 0) {
19781009254bSQu Wenruo btrfs_put_bioc(bioc);
19791009254bSQu Wenruo btrfs_bio_counter_dec(fs_info);
19801009254bSQu Wenruo goto out;
19811009254bSQu Wenruo }
19821009254bSQu Wenruo rbio = raid56_parity_alloc_scrub_rbio(bio, bioc, scrub_dev, &extent_bitmap,
19831009254bSQu Wenruo BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
19841009254bSQu Wenruo btrfs_put_bioc(bioc);
19851009254bSQu Wenruo if (!rbio) {
19861009254bSQu Wenruo ret = -ENOMEM;
19871009254bSQu Wenruo btrfs_bio_counter_dec(fs_info);
19881009254bSQu Wenruo goto out;
19891009254bSQu Wenruo }
199094ead93eSQu Wenruo /* Use the recovered stripes as cache to avoid read them from disk again. */
199194ead93eSQu Wenruo for (int i = 0; i < data_stripes; i++) {
199294ead93eSQu Wenruo stripe = &sctx->raid56_data_stripes[i];
199394ead93eSQu Wenruo
199494ead93eSQu Wenruo raid56_parity_cache_data_pages(rbio, stripe->pages,
199594ead93eSQu Wenruo full_stripe_start + (i << BTRFS_STRIPE_LEN_SHIFT));
199694ead93eSQu Wenruo }
19971009254bSQu Wenruo raid56_parity_submit_scrub_rbio(rbio);
19981009254bSQu Wenruo wait_for_completion_io(&io_done);
19991009254bSQu Wenruo ret = blk_status_to_errno(bio->bi_status);
20001009254bSQu Wenruo bio_put(bio);
20011009254bSQu Wenruo btrfs_bio_counter_dec(fs_info);
20021009254bSQu Wenruo
20031dc4888eSQu Wenruo btrfs_release_path(&extent_path);
20043c771c19SQu Wenruo btrfs_release_path(&csum_path);
20051009254bSQu Wenruo out:
20061009254bSQu Wenruo return ret;
20071009254bSQu Wenruo }
20081009254bSQu Wenruo
200909022b14SQu Wenruo /*
201009022b14SQu Wenruo * Scrub one range which can only has simple mirror based profile.
201109022b14SQu Wenruo * (Including all range in SINGLE/DUP/RAID1/RAID1C*, and each stripe in
201209022b14SQu Wenruo * RAID0/RAID10).
201309022b14SQu Wenruo *
201409022b14SQu Wenruo * Since we may need to handle a subset of block group, we need @logical_start
201509022b14SQu Wenruo * and @logical_length parameter.
201609022b14SQu Wenruo */
scrub_simple_mirror(struct scrub_ctx * sctx,struct btrfs_block_group * bg,struct map_lookup * map,u64 logical_start,u64 logical_length,struct btrfs_device * device,u64 physical,int mirror_num)201709022b14SQu Wenruo static int scrub_simple_mirror(struct scrub_ctx *sctx,
201809022b14SQu Wenruo struct btrfs_block_group *bg,
201909022b14SQu Wenruo struct map_lookup *map,
202009022b14SQu Wenruo u64 logical_start, u64 logical_length,
202109022b14SQu Wenruo struct btrfs_device *device,
202209022b14SQu Wenruo u64 physical, int mirror_num)
202309022b14SQu Wenruo {
202409022b14SQu Wenruo struct btrfs_fs_info *fs_info = sctx->fs_info;
202509022b14SQu Wenruo const u64 logical_end = logical_start + logical_length;
202609022b14SQu Wenruo u64 cur_logical = logical_start;
2027e130d4fcSLu Yao int ret = 0;
202809022b14SQu Wenruo
202909022b14SQu Wenruo /* The range must be inside the bg */
203009022b14SQu Wenruo ASSERT(logical_start >= bg->start && logical_end <= bg->start + bg->length);
203109022b14SQu Wenruo
203209022b14SQu Wenruo /* Go through each extent items inside the logical range */
203309022b14SQu Wenruo while (cur_logical < logical_end) {
20346927a91cSQu Wenruo u64 found_logical = U64_MAX;
2035e02ee89bSQu Wenruo u64 cur_physical = physical + cur_logical - logical_start;
203609022b14SQu Wenruo
203709022b14SQu Wenruo /* Canceled? */
203809022b14SQu Wenruo if (atomic_read(&fs_info->scrub_cancel_req) ||
203909022b14SQu Wenruo atomic_read(&sctx->cancel_req)) {
204009022b14SQu Wenruo ret = -ECANCELED;
204109022b14SQu Wenruo break;
204209022b14SQu Wenruo }
204309022b14SQu Wenruo /* Paused? */
204409022b14SQu Wenruo if (atomic_read(&fs_info->scrub_pause_req)) {
204509022b14SQu Wenruo /* Push queued extents */
204609022b14SQu Wenruo scrub_blocked_if_needed(fs_info);
204709022b14SQu Wenruo }
204809022b14SQu Wenruo /* Block group removed? */
204909022b14SQu Wenruo spin_lock(&bg->lock);
20503349b57fSJosef Bacik if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) {
205109022b14SQu Wenruo spin_unlock(&bg->lock);
205209022b14SQu Wenruo ret = 0;
205309022b14SQu Wenruo break;
205409022b14SQu Wenruo }
205509022b14SQu Wenruo spin_unlock(&bg->lock);
205609022b14SQu Wenruo
2057e02ee89bSQu Wenruo ret = queue_scrub_stripe(sctx, bg, device, mirror_num,
2058e02ee89bSQu Wenruo cur_logical, logical_end - cur_logical,
2059ae76d8e3SQu Wenruo cur_physical, &found_logical);
206009022b14SQu Wenruo if (ret > 0) {
206109022b14SQu Wenruo /* No more extent, just update the accounting */
206209022b14SQu Wenruo sctx->stat.last_physical = physical + logical_length;
206309022b14SQu Wenruo ret = 0;
206409022b14SQu Wenruo break;
206509022b14SQu Wenruo }
206609022b14SQu Wenruo if (ret < 0)
206709022b14SQu Wenruo break;
206809022b14SQu Wenruo
20696927a91cSQu Wenruo /* queue_scrub_stripe() returned 0, @found_logical must be updated. */
20706927a91cSQu Wenruo ASSERT(found_logical != U64_MAX);
2071ae76d8e3SQu Wenruo cur_logical = found_logical + BTRFS_STRIPE_LEN;
207209022b14SQu Wenruo
207309022b14SQu Wenruo /* Don't hold CPU for too long time */
207409022b14SQu Wenruo cond_resched();
207509022b14SQu Wenruo }
207609022b14SQu Wenruo return ret;
207709022b14SQu Wenruo }
207809022b14SQu Wenruo
20798557635eSQu Wenruo /* Calculate the full stripe length for simple stripe based profiles */
simple_stripe_full_stripe_len(const struct map_lookup * map)20808557635eSQu Wenruo static u64 simple_stripe_full_stripe_len(const struct map_lookup *map)
20818557635eSQu Wenruo {
20828557635eSQu Wenruo ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
20838557635eSQu Wenruo BTRFS_BLOCK_GROUP_RAID10));
20848557635eSQu Wenruo
2085cb091225SQu Wenruo return btrfs_stripe_nr_to_offset(map->num_stripes / map->sub_stripes);
20868557635eSQu Wenruo }
20878557635eSQu Wenruo
20888557635eSQu Wenruo /* Get the logical bytenr for the stripe */
simple_stripe_get_logical(struct map_lookup * map,struct btrfs_block_group * bg,int stripe_index)20898557635eSQu Wenruo static u64 simple_stripe_get_logical(struct map_lookup *map,
20908557635eSQu Wenruo struct btrfs_block_group *bg,
20918557635eSQu Wenruo int stripe_index)
20928557635eSQu Wenruo {
20938557635eSQu Wenruo ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
20948557635eSQu Wenruo BTRFS_BLOCK_GROUP_RAID10));
20958557635eSQu Wenruo ASSERT(stripe_index < map->num_stripes);
20968557635eSQu Wenruo
20978557635eSQu Wenruo /*
20988557635eSQu Wenruo * (stripe_index / sub_stripes) gives how many data stripes we need to
20998557635eSQu Wenruo * skip.
21008557635eSQu Wenruo */
2101cb091225SQu Wenruo return btrfs_stripe_nr_to_offset(stripe_index / map->sub_stripes) +
2102a97699d1SQu Wenruo bg->start;
21038557635eSQu Wenruo }
21048557635eSQu Wenruo
21058557635eSQu Wenruo /* Get the mirror number for the stripe */
simple_stripe_mirror_num(struct map_lookup * map,int stripe_index)21068557635eSQu Wenruo static int simple_stripe_mirror_num(struct map_lookup *map, int stripe_index)
21078557635eSQu Wenruo {
21088557635eSQu Wenruo ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 |
21098557635eSQu Wenruo BTRFS_BLOCK_GROUP_RAID10));
21108557635eSQu Wenruo ASSERT(stripe_index < map->num_stripes);
21118557635eSQu Wenruo
21128557635eSQu Wenruo /* For RAID0, it's fixed to 1, for RAID10 it's 0,1,0,1... */
21138557635eSQu Wenruo return stripe_index % map->sub_stripes + 1;
21148557635eSQu Wenruo }
21158557635eSQu Wenruo
scrub_simple_stripe(struct scrub_ctx * sctx,struct btrfs_block_group * bg,struct map_lookup * map,struct btrfs_device * device,int stripe_index)21168557635eSQu Wenruo static int scrub_simple_stripe(struct scrub_ctx *sctx,
21178557635eSQu Wenruo struct btrfs_block_group *bg,
21188557635eSQu Wenruo struct map_lookup *map,
21198557635eSQu Wenruo struct btrfs_device *device,
21208557635eSQu Wenruo int stripe_index)
21218557635eSQu Wenruo {
21228557635eSQu Wenruo const u64 logical_increment = simple_stripe_full_stripe_len(map);
21238557635eSQu Wenruo const u64 orig_logical = simple_stripe_get_logical(map, bg, stripe_index);
21248557635eSQu Wenruo const u64 orig_physical = map->stripes[stripe_index].physical;
21258557635eSQu Wenruo const int mirror_num = simple_stripe_mirror_num(map, stripe_index);
21268557635eSQu Wenruo u64 cur_logical = orig_logical;
21278557635eSQu Wenruo u64 cur_physical = orig_physical;
21288557635eSQu Wenruo int ret = 0;
21298557635eSQu Wenruo
21308557635eSQu Wenruo while (cur_logical < bg->start + bg->length) {
21318557635eSQu Wenruo /*
21328557635eSQu Wenruo * Inside each stripe, RAID0 is just SINGLE, and RAID10 is
21338557635eSQu Wenruo * just RAID1, so we can reuse scrub_simple_mirror() to scrub
21348557635eSQu Wenruo * this stripe.
21358557635eSQu Wenruo */
21366b4d375aSQu Wenruo ret = scrub_simple_mirror(sctx, bg, map, cur_logical,
21376b4d375aSQu Wenruo BTRFS_STRIPE_LEN, device, cur_physical,
21386b4d375aSQu Wenruo mirror_num);
21398557635eSQu Wenruo if (ret)
21408557635eSQu Wenruo return ret;
21418557635eSQu Wenruo /* Skip to next stripe which belongs to the target device */
21428557635eSQu Wenruo cur_logical += logical_increment;
21438557635eSQu Wenruo /* For physical offset, we just go to next stripe */
2144a97699d1SQu Wenruo cur_physical += BTRFS_STRIPE_LEN;
21458557635eSQu Wenruo }
21468557635eSQu Wenruo return ret;
21478557635eSQu Wenruo }
21488557635eSQu Wenruo
scrub_stripe(struct scrub_ctx * sctx,struct btrfs_block_group * bg,struct extent_map * em,struct btrfs_device * scrub_dev,int stripe_index)2149d9d181c1SStefan Behrens static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
21502ae8ae3dSQu Wenruo struct btrfs_block_group *bg,
2151bc88b486SQu Wenruo struct extent_map *em,
2152a36cf8b8SStefan Behrens struct btrfs_device *scrub_dev,
2153bc88b486SQu Wenruo int stripe_index)
2154a2de733cSArne Jansen {
2155fb456252SJeff Mahoney struct btrfs_fs_info *fs_info = sctx->fs_info;
2156bc88b486SQu Wenruo struct map_lookup *map = em->map_lookup;
215709022b14SQu Wenruo const u64 profile = map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
21582ae8ae3dSQu Wenruo const u64 chunk_logical = bg->start;
2159a2de733cSArne Jansen int ret;
21608eb3dd17SQu Wenruo int ret2;
21611194a824SQu Wenruo u64 physical = map->stripes[stripe_index].physical;
2162bc88b486SQu Wenruo const u64 dev_stripe_len = btrfs_calc_stripe_length(em);
2163bc88b486SQu Wenruo const u64 physical_end = physical + dev_stripe_len;
2164a2de733cSArne Jansen u64 logical;
2165625f1c8dSLiu Bo u64 logic_end;
216618d30ab9SQu Wenruo /* The logical increment after finishing one stripe */
21675c07c53fSJiapeng Chong u64 increment;
216818d30ab9SQu Wenruo /* Offset inside the chunk */
2169a2de733cSArne Jansen u64 offset;
21705a6ac9eaSMiao Xie u64 stripe_logical;
21713b080b25SWang Shilong int stop_loop = 0;
217253b381b3SDavid Woodhouse
21731dc4888eSQu Wenruo /* Extent_path should be released by now. */
21741dc4888eSQu Wenruo ASSERT(sctx->extent_path.nodes[0] == NULL);
21751dc4888eSQu Wenruo
2176cb7ab021SWang Shilong scrub_blocked_if_needed(fs_info);
2177a2de733cSArne Jansen
2178de17addcSNaohiro Aota if (sctx->is_dev_replace &&
2179de17addcSNaohiro Aota btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) {
2180de17addcSNaohiro Aota mutex_lock(&sctx->wr_lock);
2181de17addcSNaohiro Aota sctx->write_pointer = physical;
2182de17addcSNaohiro Aota mutex_unlock(&sctx->wr_lock);
2183de17addcSNaohiro Aota }
2184de17addcSNaohiro Aota
21851009254bSQu Wenruo /* Prepare the extra data stripes used by RAID56. */
21861009254bSQu Wenruo if (profile & BTRFS_BLOCK_GROUP_RAID56_MASK) {
21871009254bSQu Wenruo ASSERT(sctx->raid56_data_stripes == NULL);
21881009254bSQu Wenruo
21891009254bSQu Wenruo sctx->raid56_data_stripes = kcalloc(nr_data_stripes(map),
21901009254bSQu Wenruo sizeof(struct scrub_stripe),
21911009254bSQu Wenruo GFP_KERNEL);
21921009254bSQu Wenruo if (!sctx->raid56_data_stripes) {
21931009254bSQu Wenruo ret = -ENOMEM;
21941009254bSQu Wenruo goto out;
21951009254bSQu Wenruo }
21961009254bSQu Wenruo for (int i = 0; i < nr_data_stripes(map); i++) {
21971009254bSQu Wenruo ret = init_scrub_stripe(fs_info,
21981009254bSQu Wenruo &sctx->raid56_data_stripes[i]);
21991009254bSQu Wenruo if (ret < 0)
22001009254bSQu Wenruo goto out;
22011009254bSQu Wenruo sctx->raid56_data_stripes[i].bg = bg;
22021009254bSQu Wenruo sctx->raid56_data_stripes[i].sctx = sctx;
22031009254bSQu Wenruo }
22041009254bSQu Wenruo }
2205a2de733cSArne Jansen /*
220609022b14SQu Wenruo * There used to be a big double loop to handle all profiles using the
220709022b14SQu Wenruo * same routine, which grows larger and more gross over time.
220809022b14SQu Wenruo *
220909022b14SQu Wenruo * So here we handle each profile differently, so simpler profiles
221009022b14SQu Wenruo * have simpler scrubbing function.
221109022b14SQu Wenruo */
221209022b14SQu Wenruo if (!(profile & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10 |
221309022b14SQu Wenruo BTRFS_BLOCK_GROUP_RAID56_MASK))) {
221409022b14SQu Wenruo /*
221509022b14SQu Wenruo * Above check rules out all complex profile, the remaining
221609022b14SQu Wenruo * profiles are SINGLE|DUP|RAID1|RAID1C*, which is simple
221709022b14SQu Wenruo * mirrored duplication without stripe.
221809022b14SQu Wenruo *
221909022b14SQu Wenruo * Only @physical and @mirror_num needs to calculated using
222009022b14SQu Wenruo * @stripe_index.
222109022b14SQu Wenruo */
22226b4d375aSQu Wenruo ret = scrub_simple_mirror(sctx, bg, map, bg->start, bg->length,
22236b4d375aSQu Wenruo scrub_dev, map->stripes[stripe_index].physical,
222409022b14SQu Wenruo stripe_index + 1);
2225e430c428SQu Wenruo offset = 0;
222609022b14SQu Wenruo goto out;
222709022b14SQu Wenruo }
22288557635eSQu Wenruo if (profile & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
22296b4d375aSQu Wenruo ret = scrub_simple_stripe(sctx, bg, map, scrub_dev, stripe_index);
2230cb091225SQu Wenruo offset = btrfs_stripe_nr_to_offset(stripe_index / map->sub_stripes);
22318557635eSQu Wenruo goto out;
22328557635eSQu Wenruo }
22338557635eSQu Wenruo
22348557635eSQu Wenruo /* Only RAID56 goes through the old code */
22358557635eSQu Wenruo ASSERT(map->type & BTRFS_BLOCK_GROUP_RAID56_MASK);
2236a2de733cSArne Jansen ret = 0;
2237e430c428SQu Wenruo
2238e430c428SQu Wenruo /* Calculate the logical end of the stripe */
2239e430c428SQu Wenruo get_raid56_logic_offset(physical_end, stripe_index,
2240e430c428SQu Wenruo map, &logic_end, NULL);
2241e430c428SQu Wenruo logic_end += chunk_logical;
2242e430c428SQu Wenruo
2243e430c428SQu Wenruo /* Initialize @offset in case we need to go to out: label */
2244e430c428SQu Wenruo get_raid56_logic_offset(physical, stripe_index, map, &offset, NULL);
2245cb091225SQu Wenruo increment = btrfs_stripe_nr_to_offset(nr_data_stripes(map));
2246e430c428SQu Wenruo
224718d30ab9SQu Wenruo /*
224818d30ab9SQu Wenruo * Due to the rotation, for RAID56 it's better to iterate each stripe
224918d30ab9SQu Wenruo * using their physical offset.
225018d30ab9SQu Wenruo */
22513b080b25SWang Shilong while (physical < physical_end) {
225218d30ab9SQu Wenruo ret = get_raid56_logic_offset(physical, stripe_index, map,
225318d30ab9SQu Wenruo &logical, &stripe_logical);
22542ae8ae3dSQu Wenruo logical += chunk_logical;
2255f2f66a2fSZhao Lei if (ret) {
22567955323bSZhao Lei /* it is parity strip */
22572ae8ae3dSQu Wenruo stripe_logical += chunk_logical;
22581009254bSQu Wenruo ret = scrub_raid56_parity_stripe(sctx, scrub_dev, bg,
22591009254bSQu Wenruo map, stripe_logical);
2260f2f66a2fSZhao Lei if (ret)
2261f2f66a2fSZhao Lei goto out;
2262a2de733cSArne Jansen goto next;
2263a2de733cSArne Jansen }
2264a2de733cSArne Jansen
2265a2de733cSArne Jansen /*
226618d30ab9SQu Wenruo * Now we're at a data stripe, scrub each extents in the range.
226718d30ab9SQu Wenruo *
226818d30ab9SQu Wenruo * At this stage, if we ignore the repair part, inside each data
226918d30ab9SQu Wenruo * stripe it is no different than SINGLE profile.
227018d30ab9SQu Wenruo * We can reuse scrub_simple_mirror() here, as the repair part
227118d30ab9SQu Wenruo * is still based on @mirror_num.
2272a2de733cSArne Jansen */
22736b4d375aSQu Wenruo ret = scrub_simple_mirror(sctx, bg, map, logical, BTRFS_STRIPE_LEN,
227418d30ab9SQu Wenruo scrub_dev, physical, 1);
227518d30ab9SQu Wenruo if (ret < 0)
2276a2de733cSArne Jansen goto out;
2277a2de733cSArne Jansen next:
2278a2de733cSArne Jansen logical += increment;
2279a97699d1SQu Wenruo physical += BTRFS_STRIPE_LEN;
2280d9d181c1SStefan Behrens spin_lock(&sctx->stat_lock);
2281625f1c8dSLiu Bo if (stop_loop)
2282bc88b486SQu Wenruo sctx->stat.last_physical =
2283bc88b486SQu Wenruo map->stripes[stripe_index].physical + dev_stripe_len;
2284625f1c8dSLiu Bo else
2285d9d181c1SStefan Behrens sctx->stat.last_physical = physical;
2286d9d181c1SStefan Behrens spin_unlock(&sctx->stat_lock);
2287625f1c8dSLiu Bo if (stop_loop)
2288625f1c8dSLiu Bo break;
2289a2de733cSArne Jansen }
2290ff023aacSStefan Behrens out:
22918eb3dd17SQu Wenruo ret2 = flush_scrub_stripes(sctx);
2292b50f2d04SQu Wenruo if (!ret)
22938eb3dd17SQu Wenruo ret = ret2;
22941dc4888eSQu Wenruo btrfs_release_path(&sctx->extent_path);
22953c771c19SQu Wenruo btrfs_release_path(&sctx->csum_path);
22961dc4888eSQu Wenruo
22971009254bSQu Wenruo if (sctx->raid56_data_stripes) {
22981009254bSQu Wenruo for (int i = 0; i < nr_data_stripes(map); i++)
22991009254bSQu Wenruo release_scrub_stripe(&sctx->raid56_data_stripes[i]);
23001009254bSQu Wenruo kfree(sctx->raid56_data_stripes);
23011009254bSQu Wenruo sctx->raid56_data_stripes = NULL;
23021009254bSQu Wenruo }
23037db1c5d1SNaohiro Aota
23047db1c5d1SNaohiro Aota if (sctx->is_dev_replace && ret >= 0) {
23057db1c5d1SNaohiro Aota int ret2;
23067db1c5d1SNaohiro Aota
23072ae8ae3dSQu Wenruo ret2 = sync_write_pointer_for_zoned(sctx,
23082ae8ae3dSQu Wenruo chunk_logical + offset,
23092ae8ae3dSQu Wenruo map->stripes[stripe_index].physical,
23107db1c5d1SNaohiro Aota physical_end);
23117db1c5d1SNaohiro Aota if (ret2)
23127db1c5d1SNaohiro Aota ret = ret2;
23137db1c5d1SNaohiro Aota }
23147db1c5d1SNaohiro Aota
2315a2de733cSArne Jansen return ret < 0 ? ret : 0;
2316a2de733cSArne Jansen }
2317a2de733cSArne Jansen
scrub_chunk(struct scrub_ctx * sctx,struct btrfs_block_group * bg,struct btrfs_device * scrub_dev,u64 dev_offset,u64 dev_extent_len)2318d9d181c1SStefan Behrens static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
2319d04fbe19SQu Wenruo struct btrfs_block_group *bg,
2320a36cf8b8SStefan Behrens struct btrfs_device *scrub_dev,
2321020d5b73SFilipe Manana u64 dev_offset,
2322d04fbe19SQu Wenruo u64 dev_extent_len)
2323a2de733cSArne Jansen {
2324fb456252SJeff Mahoney struct btrfs_fs_info *fs_info = sctx->fs_info;
2325c8bf1b67SDavid Sterba struct extent_map_tree *map_tree = &fs_info->mapping_tree;
2326a2de733cSArne Jansen struct map_lookup *map;
2327a2de733cSArne Jansen struct extent_map *em;
2328a2de733cSArne Jansen int i;
2329ff023aacSStefan Behrens int ret = 0;
2330a2de733cSArne Jansen
2331c8bf1b67SDavid Sterba read_lock(&map_tree->lock);
2332d04fbe19SQu Wenruo em = lookup_extent_mapping(map_tree, bg->start, bg->length);
2333c8bf1b67SDavid Sterba read_unlock(&map_tree->lock);
2334a2de733cSArne Jansen
2335020d5b73SFilipe Manana if (!em) {
2336020d5b73SFilipe Manana /*
2337020d5b73SFilipe Manana * Might have been an unused block group deleted by the cleaner
2338020d5b73SFilipe Manana * kthread or relocation.
2339020d5b73SFilipe Manana */
2340d04fbe19SQu Wenruo spin_lock(&bg->lock);
23413349b57fSJosef Bacik if (!test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags))
2342020d5b73SFilipe Manana ret = -EINVAL;
2343d04fbe19SQu Wenruo spin_unlock(&bg->lock);
2344020d5b73SFilipe Manana
2345020d5b73SFilipe Manana return ret;
2346020d5b73SFilipe Manana }
2347d04fbe19SQu Wenruo if (em->start != bg->start)
2348d04fbe19SQu Wenruo goto out;
2349d04fbe19SQu Wenruo if (em->len < dev_extent_len)
2350d04fbe19SQu Wenruo goto out;
2351a2de733cSArne Jansen
235295617d69SJeff Mahoney map = em->map_lookup;
2353a2de733cSArne Jansen for (i = 0; i < map->num_stripes; ++i) {
2354a36cf8b8SStefan Behrens if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
2355859acaf1SArne Jansen map->stripes[i].physical == dev_offset) {
2356bc88b486SQu Wenruo ret = scrub_stripe(sctx, bg, em, scrub_dev, i);
2357a2de733cSArne Jansen if (ret)
2358a2de733cSArne Jansen goto out;
2359a2de733cSArne Jansen }
2360a2de733cSArne Jansen }
2361a2de733cSArne Jansen out:
2362a2de733cSArne Jansen free_extent_map(em);
2363a2de733cSArne Jansen
2364a2de733cSArne Jansen return ret;
2365a2de733cSArne Jansen }
2366a2de733cSArne Jansen
finish_extent_writes_for_zoned(struct btrfs_root * root,struct btrfs_block_group * cache)2367de17addcSNaohiro Aota static int finish_extent_writes_for_zoned(struct btrfs_root *root,
2368de17addcSNaohiro Aota struct btrfs_block_group *cache)
2369de17addcSNaohiro Aota {
2370de17addcSNaohiro Aota struct btrfs_fs_info *fs_info = cache->fs_info;
2371de17addcSNaohiro Aota struct btrfs_trans_handle *trans;
2372de17addcSNaohiro Aota
2373de17addcSNaohiro Aota if (!btrfs_is_zoned(fs_info))
2374de17addcSNaohiro Aota return 0;
2375de17addcSNaohiro Aota
2376de17addcSNaohiro Aota btrfs_wait_block_group_reservations(cache);
2377de17addcSNaohiro Aota btrfs_wait_nocow_writers(cache);
2378de17addcSNaohiro Aota btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start, cache->length);
2379de17addcSNaohiro Aota
2380de17addcSNaohiro Aota trans = btrfs_join_transaction(root);
2381de17addcSNaohiro Aota if (IS_ERR(trans))
2382de17addcSNaohiro Aota return PTR_ERR(trans);
2383de17addcSNaohiro Aota return btrfs_commit_transaction(trans);
2384de17addcSNaohiro Aota }
2385de17addcSNaohiro Aota
2386a2de733cSArne Jansen static noinline_for_stack
scrub_enumerate_chunks(struct scrub_ctx * sctx,struct btrfs_device * scrub_dev,u64 start,u64 end)2387a36cf8b8SStefan Behrens int scrub_enumerate_chunks(struct scrub_ctx *sctx,
238832934280SOmar Sandoval struct btrfs_device *scrub_dev, u64 start, u64 end)
2389a2de733cSArne Jansen {
2390a2de733cSArne Jansen struct btrfs_dev_extent *dev_extent = NULL;
2391a2de733cSArne Jansen struct btrfs_path *path;
23920b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->fs_info;
23930b246afaSJeff Mahoney struct btrfs_root *root = fs_info->dev_root;
2394a2de733cSArne Jansen u64 chunk_offset;
239555e3a601SZhaolei int ret = 0;
239676a8efa1SZhaolei int ro_set;
2397a2de733cSArne Jansen int slot;
2398a2de733cSArne Jansen struct extent_buffer *l;
2399a2de733cSArne Jansen struct btrfs_key key;
2400a2de733cSArne Jansen struct btrfs_key found_key;
240132da5386SDavid Sterba struct btrfs_block_group *cache;
2402ff023aacSStefan Behrens struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
2403a2de733cSArne Jansen
2404a2de733cSArne Jansen path = btrfs_alloc_path();
2405a2de733cSArne Jansen if (!path)
2406a2de733cSArne Jansen return -ENOMEM;
2407a2de733cSArne Jansen
2408e4058b54SDavid Sterba path->reada = READA_FORWARD;
2409a2de733cSArne Jansen path->search_commit_root = 1;
2410a2de733cSArne Jansen path->skip_locking = 1;
2411a2de733cSArne Jansen
2412a36cf8b8SStefan Behrens key.objectid = scrub_dev->devid;
2413a2de733cSArne Jansen key.offset = 0ull;
2414a2de733cSArne Jansen key.type = BTRFS_DEV_EXTENT_KEY;
2415a2de733cSArne Jansen
2416a2de733cSArne Jansen while (1) {
2417d04fbe19SQu Wenruo u64 dev_extent_len;
2418d04fbe19SQu Wenruo
2419a2de733cSArne Jansen ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2420a2de733cSArne Jansen if (ret < 0)
24218c51032fSArne Jansen break;
24228c51032fSArne Jansen if (ret > 0) {
24238c51032fSArne Jansen if (path->slots[0] >=
24248c51032fSArne Jansen btrfs_header_nritems(path->nodes[0])) {
24258c51032fSArne Jansen ret = btrfs_next_leaf(root, path);
242655e3a601SZhaolei if (ret < 0)
24278c51032fSArne Jansen break;
242855e3a601SZhaolei if (ret > 0) {
242955e3a601SZhaolei ret = 0;
243055e3a601SZhaolei break;
243155e3a601SZhaolei }
243255e3a601SZhaolei } else {
243355e3a601SZhaolei ret = 0;
24348c51032fSArne Jansen }
24358c51032fSArne Jansen }
2436a2de733cSArne Jansen
2437a2de733cSArne Jansen l = path->nodes[0];
2438a2de733cSArne Jansen slot = path->slots[0];
2439a2de733cSArne Jansen
2440a2de733cSArne Jansen btrfs_item_key_to_cpu(l, &found_key, slot);
2441a2de733cSArne Jansen
2442a36cf8b8SStefan Behrens if (found_key.objectid != scrub_dev->devid)
2443a2de733cSArne Jansen break;
2444a2de733cSArne Jansen
2445962a298fSDavid Sterba if (found_key.type != BTRFS_DEV_EXTENT_KEY)
2446a2de733cSArne Jansen break;
2447a2de733cSArne Jansen
2448a2de733cSArne Jansen if (found_key.offset >= end)
2449a2de733cSArne Jansen break;
2450a2de733cSArne Jansen
2451a2de733cSArne Jansen if (found_key.offset < key.offset)
2452a2de733cSArne Jansen break;
2453a2de733cSArne Jansen
2454a2de733cSArne Jansen dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2455d04fbe19SQu Wenruo dev_extent_len = btrfs_dev_extent_length(l, dev_extent);
2456a2de733cSArne Jansen
2457d04fbe19SQu Wenruo if (found_key.offset + dev_extent_len <= start)
2458ced96edcSQu Wenruo goto skip;
2459a2de733cSArne Jansen
2460a2de733cSArne Jansen chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2461a2de733cSArne Jansen
2462a2de733cSArne Jansen /*
2463a2de733cSArne Jansen * get a reference on the corresponding block group to prevent
2464a2de733cSArne Jansen * the chunk from going away while we scrub it
2465a2de733cSArne Jansen */
2466a2de733cSArne Jansen cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2467ced96edcSQu Wenruo
2468ced96edcSQu Wenruo /* some chunks are removed but not committed to disk yet,
2469ced96edcSQu Wenruo * continue scrubbing */
2470ced96edcSQu Wenruo if (!cache)
2471ced96edcSQu Wenruo goto skip;
2472ced96edcSQu Wenruo
2473a692e13dSFilipe Manana ASSERT(cache->start <= chunk_offset);
2474a692e13dSFilipe Manana /*
2475a692e13dSFilipe Manana * We are using the commit root to search for device extents, so
2476a692e13dSFilipe Manana * that means we could have found a device extent item from a
2477a692e13dSFilipe Manana * block group that was deleted in the current transaction. The
2478a692e13dSFilipe Manana * logical start offset of the deleted block group, stored at
2479a692e13dSFilipe Manana * @chunk_offset, might be part of the logical address range of
2480a692e13dSFilipe Manana * a new block group (which uses different physical extents).
2481a692e13dSFilipe Manana * In this case btrfs_lookup_block_group() has returned the new
2482a692e13dSFilipe Manana * block group, and its start address is less than @chunk_offset.
2483a692e13dSFilipe Manana *
2484a692e13dSFilipe Manana * We skip such new block groups, because it's pointless to
2485a692e13dSFilipe Manana * process them, as we won't find their extents because we search
2486a692e13dSFilipe Manana * for them using the commit root of the extent tree. For a device
2487a692e13dSFilipe Manana * replace it's also fine to skip it, we won't miss copying them
2488a692e13dSFilipe Manana * to the target device because we have the write duplication
2489a692e13dSFilipe Manana * setup through the regular write path (by btrfs_map_block()),
2490a692e13dSFilipe Manana * and we have committed a transaction when we started the device
2491a692e13dSFilipe Manana * replace, right after setting up the device replace state.
2492a692e13dSFilipe Manana */
2493a692e13dSFilipe Manana if (cache->start < chunk_offset) {
2494a692e13dSFilipe Manana btrfs_put_block_group(cache);
2495a692e13dSFilipe Manana goto skip;
2496a692e13dSFilipe Manana }
2497a692e13dSFilipe Manana
249878ce9fc2SNaohiro Aota if (sctx->is_dev_replace && btrfs_is_zoned(fs_info)) {
24993349b57fSJosef Bacik if (!test_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags)) {
25000dc16ef4SFilipe Manana btrfs_put_block_group(cache);
25010dc16ef4SFilipe Manana goto skip;
250278ce9fc2SNaohiro Aota }
250378ce9fc2SNaohiro Aota }
250478ce9fc2SNaohiro Aota
250555e3a601SZhaolei /*
25062473d24fSFilipe Manana * Make sure that while we are scrubbing the corresponding block
25072473d24fSFilipe Manana * group doesn't get its logical address and its device extents
25082473d24fSFilipe Manana * reused for another block group, which can possibly be of a
25092473d24fSFilipe Manana * different type and different profile. We do this to prevent
25102473d24fSFilipe Manana * false error detections and crashes due to bogus attempts to
25112473d24fSFilipe Manana * repair extents.
25122473d24fSFilipe Manana */
25132473d24fSFilipe Manana spin_lock(&cache->lock);
25143349b57fSJosef Bacik if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags)) {
25152473d24fSFilipe Manana spin_unlock(&cache->lock);
25162473d24fSFilipe Manana btrfs_put_block_group(cache);
25172473d24fSFilipe Manana goto skip;
25182473d24fSFilipe Manana }
25196b7304afSFilipe Manana btrfs_freeze_block_group(cache);
25202473d24fSFilipe Manana spin_unlock(&cache->lock);
25212473d24fSFilipe Manana
25222473d24fSFilipe Manana /*
252355e3a601SZhaolei * we need call btrfs_inc_block_group_ro() with scrubs_paused,
252455e3a601SZhaolei * to avoid deadlock caused by:
252555e3a601SZhaolei * btrfs_inc_block_group_ro()
252655e3a601SZhaolei * -> btrfs_wait_for_commit()
252755e3a601SZhaolei * -> btrfs_commit_transaction()
252855e3a601SZhaolei * -> btrfs_scrub_pause()
252955e3a601SZhaolei */
253055e3a601SZhaolei scrub_pause_on(fs_info);
2531b12de528SQu Wenruo
2532b12de528SQu Wenruo /*
2533b12de528SQu Wenruo * Don't do chunk preallocation for scrub.
2534b12de528SQu Wenruo *
2535b12de528SQu Wenruo * This is especially important for SYSTEM bgs, or we can hit
2536b12de528SQu Wenruo * -EFBIG from btrfs_finish_chunk_alloc() like:
2537b12de528SQu Wenruo * 1. The only SYSTEM bg is marked RO.
2538b12de528SQu Wenruo * Since SYSTEM bg is small, that's pretty common.
2539b12de528SQu Wenruo * 2. New SYSTEM bg will be allocated
2540b12de528SQu Wenruo * Due to regular version will allocate new chunk.
2541b12de528SQu Wenruo * 3. New SYSTEM bg is empty and will get cleaned up
2542b12de528SQu Wenruo * Before cleanup really happens, it's marked RO again.
2543b12de528SQu Wenruo * 4. Empty SYSTEM bg get scrubbed
2544b12de528SQu Wenruo * We go back to 2.
2545b12de528SQu Wenruo *
2546b12de528SQu Wenruo * This can easily boost the amount of SYSTEM chunks if cleaner
2547b12de528SQu Wenruo * thread can't be triggered fast enough, and use up all space
2548b12de528SQu Wenruo * of btrfs_super_block::sys_chunk_array
25491bbb97b8SQu Wenruo *
25501bbb97b8SQu Wenruo * While for dev replace, we need to try our best to mark block
25511bbb97b8SQu Wenruo * group RO, to prevent race between:
25521bbb97b8SQu Wenruo * - Write duplication
25531bbb97b8SQu Wenruo * Contains latest data
25541bbb97b8SQu Wenruo * - Scrub copy
25551bbb97b8SQu Wenruo * Contains data from commit tree
25561bbb97b8SQu Wenruo *
25571bbb97b8SQu Wenruo * If target block group is not marked RO, nocow writes can
25581bbb97b8SQu Wenruo * be overwritten by scrub copy, causing data corruption.
25591bbb97b8SQu Wenruo * So for dev-replace, it's not allowed to continue if a block
25601bbb97b8SQu Wenruo * group is not RO.
2561b12de528SQu Wenruo */
25621bbb97b8SQu Wenruo ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace);
2563de17addcSNaohiro Aota if (!ret && sctx->is_dev_replace) {
2564de17addcSNaohiro Aota ret = finish_extent_writes_for_zoned(root, cache);
2565de17addcSNaohiro Aota if (ret) {
2566de17addcSNaohiro Aota btrfs_dec_block_group_ro(cache);
2567de17addcSNaohiro Aota scrub_pause_off(fs_info);
2568de17addcSNaohiro Aota btrfs_put_block_group(cache);
2569de17addcSNaohiro Aota break;
2570de17addcSNaohiro Aota }
2571de17addcSNaohiro Aota }
2572de17addcSNaohiro Aota
257376a8efa1SZhaolei if (ret == 0) {
257476a8efa1SZhaolei ro_set = 1;
25757561551eSQu Wenruo } else if (ret == -ENOSPC && !sctx->is_dev_replace &&
25767561551eSQu Wenruo !(cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK)) {
257776a8efa1SZhaolei /*
257876a8efa1SZhaolei * btrfs_inc_block_group_ro return -ENOSPC when it
257976a8efa1SZhaolei * failed in creating new chunk for metadata.
25801bbb97b8SQu Wenruo * It is not a problem for scrub, because
258176a8efa1SZhaolei * metadata are always cowed, and our scrub paused
258276a8efa1SZhaolei * commit_transactions.
25837561551eSQu Wenruo *
25847561551eSQu Wenruo * For RAID56 chunks, we have to mark them read-only
25857561551eSQu Wenruo * for scrub, as later we would use our own cache
25867561551eSQu Wenruo * out of RAID56 realm.
25877561551eSQu Wenruo * Thus we want the RAID56 bg to be marked RO to
25887561551eSQu Wenruo * prevent RMW from screwing up out cache.
258976a8efa1SZhaolei */
259076a8efa1SZhaolei ro_set = 0;
2591195a49eaSFilipe Manana } else if (ret == -ETXTBSY) {
2592195a49eaSFilipe Manana btrfs_warn(fs_info,
2593195a49eaSFilipe Manana "skipping scrub of block group %llu due to active swapfile",
2594195a49eaSFilipe Manana cache->start);
2595195a49eaSFilipe Manana scrub_pause_off(fs_info);
2596195a49eaSFilipe Manana ret = 0;
2597195a49eaSFilipe Manana goto skip_unfreeze;
259876a8efa1SZhaolei } else {
25995d163e0eSJeff Mahoney btrfs_warn(fs_info,
2600913e1535SDavid Sterba "failed setting block group ro: %d", ret);
26016b7304afSFilipe Manana btrfs_unfreeze_block_group(cache);
260255e3a601SZhaolei btrfs_put_block_group(cache);
26031bbb97b8SQu Wenruo scrub_pause_off(fs_info);
260455e3a601SZhaolei break;
260555e3a601SZhaolei }
260655e3a601SZhaolei
26071bbb97b8SQu Wenruo /*
26081bbb97b8SQu Wenruo * Now the target block is marked RO, wait for nocow writes to
26091bbb97b8SQu Wenruo * finish before dev-replace.
26101bbb97b8SQu Wenruo * COW is fine, as COW never overwrites extents in commit tree.
26111bbb97b8SQu Wenruo */
26121bbb97b8SQu Wenruo if (sctx->is_dev_replace) {
26131bbb97b8SQu Wenruo btrfs_wait_nocow_writers(cache);
26141bbb97b8SQu Wenruo btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start,
26151bbb97b8SQu Wenruo cache->length);
26161bbb97b8SQu Wenruo }
26171bbb97b8SQu Wenruo
26181bbb97b8SQu Wenruo scrub_pause_off(fs_info);
26193ec17a67SDan Carpenter down_write(&dev_replace->rwsem);
2620d04fbe19SQu Wenruo dev_replace->cursor_right = found_key.offset + dev_extent_len;
2621ff023aacSStefan Behrens dev_replace->cursor_left = found_key.offset;
2622ff023aacSStefan Behrens dev_replace->item_needs_writeback = 1;
2623cb5583ddSDavid Sterba up_write(&dev_replace->rwsem);
2624cb5583ddSDavid Sterba
2625d04fbe19SQu Wenruo ret = scrub_chunk(sctx, cache, scrub_dev, found_key.offset,
2626d04fbe19SQu Wenruo dev_extent_len);
262778ce9fc2SNaohiro Aota if (sctx->is_dev_replace &&
262878ce9fc2SNaohiro Aota !btrfs_finish_block_group_to_copy(dev_replace->srcdev,
262978ce9fc2SNaohiro Aota cache, found_key.offset))
263078ce9fc2SNaohiro Aota ro_set = 0;
263178ce9fc2SNaohiro Aota
26323ec17a67SDan Carpenter down_write(&dev_replace->rwsem);
26331a1a8b73SFilipe Manana dev_replace->cursor_left = dev_replace->cursor_right;
26341a1a8b73SFilipe Manana dev_replace->item_needs_writeback = 1;
26353ec17a67SDan Carpenter up_write(&dev_replace->rwsem);
26361a1a8b73SFilipe Manana
263776a8efa1SZhaolei if (ro_set)
26382ff7e61eSJeff Mahoney btrfs_dec_block_group_ro(cache);
2639ff023aacSStefan Behrens
2640758f2dfcSFilipe Manana /*
2641758f2dfcSFilipe Manana * We might have prevented the cleaner kthread from deleting
2642758f2dfcSFilipe Manana * this block group if it was already unused because we raced
2643758f2dfcSFilipe Manana * and set it to RO mode first. So add it back to the unused
2644758f2dfcSFilipe Manana * list, otherwise it might not ever be deleted unless a manual
2645758f2dfcSFilipe Manana * balance is triggered or it becomes used and unused again.
2646758f2dfcSFilipe Manana */
2647758f2dfcSFilipe Manana spin_lock(&cache->lock);
26483349b57fSJosef Bacik if (!test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags) &&
26493349b57fSJosef Bacik !cache->ro && cache->reserved == 0 && cache->used == 0) {
2650758f2dfcSFilipe Manana spin_unlock(&cache->lock);
26516e80d4f8SDennis Zhou if (btrfs_test_opt(fs_info, DISCARD_ASYNC))
26526e80d4f8SDennis Zhou btrfs_discard_queue_work(&fs_info->discard_ctl,
26536e80d4f8SDennis Zhou cache);
26546e80d4f8SDennis Zhou else
2655031f24daSQu Wenruo btrfs_mark_bg_unused(cache);
2656758f2dfcSFilipe Manana } else {
2657758f2dfcSFilipe Manana spin_unlock(&cache->lock);
2658758f2dfcSFilipe Manana }
2659195a49eaSFilipe Manana skip_unfreeze:
26606b7304afSFilipe Manana btrfs_unfreeze_block_group(cache);
2661a2de733cSArne Jansen btrfs_put_block_group(cache);
2662a2de733cSArne Jansen if (ret)
2663a2de733cSArne Jansen break;
266432934280SOmar Sandoval if (sctx->is_dev_replace &&
2665af1be4f8SStefan Behrens atomic64_read(&dev_replace->num_write_errors) > 0) {
2666ff023aacSStefan Behrens ret = -EIO;
2667ff023aacSStefan Behrens break;
2668ff023aacSStefan Behrens }
2669ff023aacSStefan Behrens if (sctx->stat.malloc_errors > 0) {
2670ff023aacSStefan Behrens ret = -ENOMEM;
2671ff023aacSStefan Behrens break;
2672ff023aacSStefan Behrens }
2673ced96edcSQu Wenruo skip:
2674d04fbe19SQu Wenruo key.offset = found_key.offset + dev_extent_len;
267571267333SChris Mason btrfs_release_path(path);
2676a2de733cSArne Jansen }
2677a2de733cSArne Jansen
2678a2de733cSArne Jansen btrfs_free_path(path);
26798c51032fSArne Jansen
268055e3a601SZhaolei return ret;
2681a2de733cSArne Jansen }
2682a2de733cSArne Jansen
scrub_one_super(struct scrub_ctx * sctx,struct btrfs_device * dev,struct page * page,u64 physical,u64 generation)26832a2dc22fSQu Wenruo static int scrub_one_super(struct scrub_ctx *sctx, struct btrfs_device *dev,
26842a2dc22fSQu Wenruo struct page *page, u64 physical, u64 generation)
26852a2dc22fSQu Wenruo {
26862a2dc22fSQu Wenruo struct btrfs_fs_info *fs_info = sctx->fs_info;
26872a2dc22fSQu Wenruo struct bio_vec bvec;
26882a2dc22fSQu Wenruo struct bio bio;
26892a2dc22fSQu Wenruo struct btrfs_super_block *sb = page_address(page);
26902a2dc22fSQu Wenruo int ret;
26912a2dc22fSQu Wenruo
26922a2dc22fSQu Wenruo bio_init(&bio, dev->bdev, &bvec, 1, REQ_OP_READ);
26932a2dc22fSQu Wenruo bio.bi_iter.bi_sector = physical >> SECTOR_SHIFT;
26942a2dc22fSQu Wenruo __bio_add_page(&bio, page, BTRFS_SUPER_INFO_SIZE, 0);
26952a2dc22fSQu Wenruo ret = submit_bio_wait(&bio);
26962a2dc22fSQu Wenruo bio_uninit(&bio);
26972a2dc22fSQu Wenruo
26982a2dc22fSQu Wenruo if (ret < 0)
26992a2dc22fSQu Wenruo return ret;
27002a2dc22fSQu Wenruo ret = btrfs_check_super_csum(fs_info, sb);
27012a2dc22fSQu Wenruo if (ret != 0) {
27022a2dc22fSQu Wenruo btrfs_err_rl(fs_info,
27032a2dc22fSQu Wenruo "super block at physical %llu devid %llu has bad csum",
27042a2dc22fSQu Wenruo physical, dev->devid);
27052a2dc22fSQu Wenruo return -EIO;
27062a2dc22fSQu Wenruo }
27072a2dc22fSQu Wenruo if (btrfs_super_generation(sb) != generation) {
27082a2dc22fSQu Wenruo btrfs_err_rl(fs_info,
27092a2dc22fSQu Wenruo "super block at physical %llu devid %llu has bad generation %llu expect %llu",
27102a2dc22fSQu Wenruo physical, dev->devid,
27112a2dc22fSQu Wenruo btrfs_super_generation(sb), generation);
27122a2dc22fSQu Wenruo return -EUCLEAN;
27132a2dc22fSQu Wenruo }
27142a2dc22fSQu Wenruo
27152a2dc22fSQu Wenruo return btrfs_validate_super(fs_info, sb, -1);
27162a2dc22fSQu Wenruo }
27172a2dc22fSQu Wenruo
scrub_supers(struct scrub_ctx * sctx,struct btrfs_device * scrub_dev)2718a36cf8b8SStefan Behrens static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2719a36cf8b8SStefan Behrens struct btrfs_device *scrub_dev)
2720a2de733cSArne Jansen {
2721a2de733cSArne Jansen int i;
2722a2de733cSArne Jansen u64 bytenr;
2723a2de733cSArne Jansen u64 gen;
27242a2dc22fSQu Wenruo int ret = 0;
27252a2dc22fSQu Wenruo struct page *page;
27260b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = sctx->fs_info;
2727a2de733cSArne Jansen
272884961539SJosef Bacik if (BTRFS_FS_ERROR(fs_info))
2729fbabd4a3SJosef Bacik return -EROFS;
273079787eaaSJeff Mahoney
27312a2dc22fSQu Wenruo page = alloc_page(GFP_KERNEL);
27322a2dc22fSQu Wenruo if (!page) {
27332a2dc22fSQu Wenruo spin_lock(&sctx->stat_lock);
27342a2dc22fSQu Wenruo sctx->stat.malloc_errors++;
27352a2dc22fSQu Wenruo spin_unlock(&sctx->stat_lock);
27362a2dc22fSQu Wenruo return -ENOMEM;
27372a2dc22fSQu Wenruo }
27382a2dc22fSQu Wenruo
27395f546063SMiao Xie /* Seed devices of a new filesystem has their own generation. */
27400b246afaSJeff Mahoney if (scrub_dev->fs_devices != fs_info->fs_devices)
27415f546063SMiao Xie gen = scrub_dev->generation;
27425f546063SMiao Xie else
27430b246afaSJeff Mahoney gen = fs_info->last_trans_committed;
2744a2de733cSArne Jansen
2745a2de733cSArne Jansen for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
27468ca8aac4SJohannes Thumshirn ret = btrfs_sb_log_location(scrub_dev, i, 0, &bytenr);
27478ca8aac4SJohannes Thumshirn if (ret == -ENOENT)
27488ca8aac4SJohannes Thumshirn break;
27498ca8aac4SJohannes Thumshirn
27508ca8aac4SJohannes Thumshirn if (ret) {
27518ca8aac4SJohannes Thumshirn spin_lock(&sctx->stat_lock);
27528ca8aac4SJohannes Thumshirn sctx->stat.super_errors++;
27538ca8aac4SJohannes Thumshirn spin_unlock(&sctx->stat_lock);
27548ca8aac4SJohannes Thumshirn continue;
27558ca8aac4SJohannes Thumshirn }
27568ca8aac4SJohannes Thumshirn
2757935e5cc9SMiao Xie if (bytenr + BTRFS_SUPER_INFO_SIZE >
2758935e5cc9SMiao Xie scrub_dev->commit_total_bytes)
2759a2de733cSArne Jansen break;
276012659251SNaohiro Aota if (!btrfs_check_super_location(scrub_dev, bytenr))
276112659251SNaohiro Aota continue;
2762a2de733cSArne Jansen
27632a2dc22fSQu Wenruo ret = scrub_one_super(sctx, scrub_dev, page, bytenr, gen);
27642a2dc22fSQu Wenruo if (ret) {
27652a2dc22fSQu Wenruo spin_lock(&sctx->stat_lock);
27662a2dc22fSQu Wenruo sctx->stat.super_errors++;
27672a2dc22fSQu Wenruo spin_unlock(&sctx->stat_lock);
2768a2de733cSArne Jansen }
27692a2dc22fSQu Wenruo }
27702a2dc22fSQu Wenruo __free_page(page);
2771a2de733cSArne Jansen return 0;
2772a2de733cSArne Jansen }
2773a2de733cSArne Jansen
scrub_workers_put(struct btrfs_fs_info * fs_info)2774e89c4a9cSJosef Bacik static void scrub_workers_put(struct btrfs_fs_info *fs_info)
2775e89c4a9cSJosef Bacik {
2776e89c4a9cSJosef Bacik if (refcount_dec_and_mutex_lock(&fs_info->scrub_workers_refcnt,
2777e89c4a9cSJosef Bacik &fs_info->scrub_lock)) {
2778be539518SChristoph Hellwig struct workqueue_struct *scrub_workers = fs_info->scrub_workers;
2779e89c4a9cSJosef Bacik
2780e89c4a9cSJosef Bacik fs_info->scrub_workers = NULL;
2781e89c4a9cSJosef Bacik mutex_unlock(&fs_info->scrub_lock);
2782e89c4a9cSJosef Bacik
2783be539518SChristoph Hellwig if (scrub_workers)
2784be539518SChristoph Hellwig destroy_workqueue(scrub_workers);
2785e89c4a9cSJosef Bacik }
2786e89c4a9cSJosef Bacik }
2787e89c4a9cSJosef Bacik
2788a2de733cSArne Jansen /*
2789a2de733cSArne Jansen * get a reference count on fs_info->scrub_workers. start worker if necessary
2790a2de733cSArne Jansen */
scrub_workers_get(struct btrfs_fs_info * fs_info)279139dc7bd9SQu Wenruo static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info)
2792a2de733cSArne Jansen {
2793be539518SChristoph Hellwig struct workqueue_struct *scrub_workers = NULL;
27946f011058SDavid Sterba unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
27950339ef2fSQu Wenruo int max_active = fs_info->thread_pool_size;
2796e89c4a9cSJosef Bacik int ret = -ENOMEM;
2797a2de733cSArne Jansen
2798e89c4a9cSJosef Bacik if (refcount_inc_not_zero(&fs_info->scrub_workers_refcnt))
2799e82afc52SZhao Lei return 0;
2800e82afc52SZhao Lei
280158e814fcSTejun Heo scrub_workers = alloc_workqueue("btrfs-scrub", flags, max_active);
2802e89c4a9cSJosef Bacik if (!scrub_workers)
280381db6ae8SQu Wenruo return -ENOMEM;
2804e89c4a9cSJosef Bacik
2805e89c4a9cSJosef Bacik mutex_lock(&fs_info->scrub_lock);
2806e89c4a9cSJosef Bacik if (refcount_read(&fs_info->scrub_workers_refcnt) == 0) {
280781db6ae8SQu Wenruo ASSERT(fs_info->scrub_workers == NULL);
2808e89c4a9cSJosef Bacik fs_info->scrub_workers = scrub_workers;
2809e89c4a9cSJosef Bacik refcount_set(&fs_info->scrub_workers_refcnt, 1);
2810e89c4a9cSJosef Bacik mutex_unlock(&fs_info->scrub_lock);
2811e89c4a9cSJosef Bacik return 0;
2812e89c4a9cSJosef Bacik }
2813e89c4a9cSJosef Bacik /* Other thread raced in and created the workers for us */
2814e89c4a9cSJosef Bacik refcount_inc(&fs_info->scrub_workers_refcnt);
2815e89c4a9cSJosef Bacik mutex_unlock(&fs_info->scrub_lock);
2816e89c4a9cSJosef Bacik
2817e89c4a9cSJosef Bacik ret = 0;
28185dc96f8dSQu Wenruo
2819be539518SChristoph Hellwig destroy_workqueue(scrub_workers);
2820e89c4a9cSJosef Bacik return ret;
2821a2de733cSArne Jansen }
2822a2de733cSArne Jansen
btrfs_scrub_dev(struct btrfs_fs_info * fs_info,u64 devid,u64 start,u64 end,struct btrfs_scrub_progress * progress,int readonly,int is_dev_replace)2823aa1b8cd4SStefan Behrens int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
2824aa1b8cd4SStefan Behrens u64 end, struct btrfs_scrub_progress *progress,
282563a212abSStefan Behrens int readonly, int is_dev_replace)
2826a2de733cSArne Jansen {
2827562d7b15SJosef Bacik struct btrfs_dev_lookup_args args = { .devid = devid };
2828d9d181c1SStefan Behrens struct scrub_ctx *sctx;
2829a2de733cSArne Jansen int ret;
2830a2de733cSArne Jansen struct btrfs_device *dev;
2831a5fb1142SFilipe Manana unsigned int nofs_flag;
2832f9eab5f0SQu Wenruo bool need_commit = false;
2833a2de733cSArne Jansen
2834aa1b8cd4SStefan Behrens if (btrfs_fs_closing(fs_info))
28356c3abedaSDavid Sterba return -EAGAIN;
2836a2de733cSArne Jansen
2837fc65bb53SQu Wenruo /* At mount time we have ensured nodesize is in the range of [4K, 64K]. */
2838fc65bb53SQu Wenruo ASSERT(fs_info->nodesize <= BTRFS_STRIPE_LEN);
2839b5d67f64SStefan Behrens
28407a9e9987SStefan Behrens /*
2841fc65bb53SQu Wenruo * SCRUB_MAX_SECTORS_PER_BLOCK is calculated using the largest possible
2842fc65bb53SQu Wenruo * value (max nodesize / min sectorsize), thus nodesize should always
2843fc65bb53SQu Wenruo * be fine.
28447a9e9987SStefan Behrens */
2845fc65bb53SQu Wenruo ASSERT(fs_info->nodesize <=
2846fc65bb53SQu Wenruo SCRUB_MAX_SECTORS_PER_BLOCK << fs_info->sectorsize_bits);
28477a9e9987SStefan Behrens
28480e94c4f4SDavid Sterba /* Allocate outside of device_list_mutex */
28490e94c4f4SDavid Sterba sctx = scrub_setup_ctx(fs_info, is_dev_replace);
28500e94c4f4SDavid Sterba if (IS_ERR(sctx))
28510e94c4f4SDavid Sterba return PTR_ERR(sctx);
2852a2de733cSArne Jansen
285339dc7bd9SQu Wenruo ret = scrub_workers_get(fs_info);
2854e89c4a9cSJosef Bacik if (ret)
2855e89c4a9cSJosef Bacik goto out_free_ctx;
2856e89c4a9cSJosef Bacik
2857aa1b8cd4SStefan Behrens mutex_lock(&fs_info->fs_devices->device_list_mutex);
2858562d7b15SJosef Bacik dev = btrfs_find_device(fs_info->fs_devices, &args);
2859e6e674bdSAnand Jain if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
2860e6e674bdSAnand Jain !is_dev_replace)) {
2861aa1b8cd4SStefan Behrens mutex_unlock(&fs_info->fs_devices->device_list_mutex);
28620e94c4f4SDavid Sterba ret = -ENODEV;
2863e89c4a9cSJosef Bacik goto out;
2864a2de733cSArne Jansen }
2865a2de733cSArne Jansen
2866ebbede42SAnand Jain if (!is_dev_replace && !readonly &&
2867ebbede42SAnand Jain !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
28685d68da3bSMiao Xie mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2869a4852cf2SDavid Sterba btrfs_err_in_rcu(fs_info,
2870a4852cf2SDavid Sterba "scrub on devid %llu: filesystem on %s is not writable",
2871cb3e217bSQu Wenruo devid, btrfs_dev_name(dev));
28720e94c4f4SDavid Sterba ret = -EROFS;
2873e89c4a9cSJosef Bacik goto out;
28745d68da3bSMiao Xie }
28755d68da3bSMiao Xie
28763b7a016fSWang Shilong mutex_lock(&fs_info->scrub_lock);
2877e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
2878401e29c1SAnand Jain test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
2879a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
2880aa1b8cd4SStefan Behrens mutex_unlock(&fs_info->fs_devices->device_list_mutex);
28810e94c4f4SDavid Sterba ret = -EIO;
2882e89c4a9cSJosef Bacik goto out;
2883a2de733cSArne Jansen }
2884a2de733cSArne Jansen
2885cb5583ddSDavid Sterba down_read(&fs_info->dev_replace.rwsem);
2886cadbc0a0SAnand Jain if (dev->scrub_ctx ||
28878dabb742SStefan Behrens (!is_dev_replace &&
28888dabb742SStefan Behrens btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
2889cb5583ddSDavid Sterba up_read(&fs_info->dev_replace.rwsem);
2890a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
2891aa1b8cd4SStefan Behrens mutex_unlock(&fs_info->fs_devices->device_list_mutex);
28920e94c4f4SDavid Sterba ret = -EINPROGRESS;
2893e89c4a9cSJosef Bacik goto out;
2894a2de733cSArne Jansen }
2895cb5583ddSDavid Sterba up_read(&fs_info->dev_replace.rwsem);
28963b7a016fSWang Shilong
2897d9d181c1SStefan Behrens sctx->readonly = readonly;
2898cadbc0a0SAnand Jain dev->scrub_ctx = sctx;
28993cb0929aSWang Shilong mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2900a2de733cSArne Jansen
29013cb0929aSWang Shilong /*
29023cb0929aSWang Shilong * checking @scrub_pause_req here, we can avoid
29033cb0929aSWang Shilong * race between committing transaction and scrubbing.
29043cb0929aSWang Shilong */
2905cb7ab021SWang Shilong __scrub_blocked_if_needed(fs_info);
2906a2de733cSArne Jansen atomic_inc(&fs_info->scrubs_running);
2907a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
2908a2de733cSArne Jansen
2909a5fb1142SFilipe Manana /*
2910a5fb1142SFilipe Manana * In order to avoid deadlock with reclaim when there is a transaction
2911a5fb1142SFilipe Manana * trying to pause scrub, make sure we use GFP_NOFS for all the
291246343501SQu Wenruo * allocations done at btrfs_scrub_sectors() and scrub_sectors_for_parity()
2913a5fb1142SFilipe Manana * invoked by our callees. The pausing request is done when the
2914a5fb1142SFilipe Manana * transaction commit starts, and it blocks the transaction until scrub
2915a5fb1142SFilipe Manana * is paused (done at specific points at scrub_stripe() or right above
2916a5fb1142SFilipe Manana * before incrementing fs_info->scrubs_running).
2917a5fb1142SFilipe Manana */
2918a5fb1142SFilipe Manana nofs_flag = memalloc_nofs_save();
2919ff023aacSStefan Behrens if (!is_dev_replace) {
2920f9eab5f0SQu Wenruo u64 old_super_errors;
2921f9eab5f0SQu Wenruo
2922f9eab5f0SQu Wenruo spin_lock(&sctx->stat_lock);
2923f9eab5f0SQu Wenruo old_super_errors = sctx->stat.super_errors;
2924f9eab5f0SQu Wenruo spin_unlock(&sctx->stat_lock);
2925f9eab5f0SQu Wenruo
2926d1e14420SAnand Jain btrfs_info(fs_info, "scrub: started on devid %llu", devid);
29279b011adfSWang Shilong /*
29289b011adfSWang Shilong * by holding device list mutex, we can
29299b011adfSWang Shilong * kick off writing super in log tree sync.
29309b011adfSWang Shilong */
29313cb0929aSWang Shilong mutex_lock(&fs_info->fs_devices->device_list_mutex);
2932a36cf8b8SStefan Behrens ret = scrub_supers(sctx, dev);
29339b011adfSWang Shilong mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2934f9eab5f0SQu Wenruo
2935f9eab5f0SQu Wenruo spin_lock(&sctx->stat_lock);
2936f9eab5f0SQu Wenruo /*
2937f9eab5f0SQu Wenruo * Super block errors found, but we can not commit transaction
2938f9eab5f0SQu Wenruo * at current context, since btrfs_commit_transaction() needs
2939f9eab5f0SQu Wenruo * to pause the current running scrub (hold by ourselves).
2940f9eab5f0SQu Wenruo */
2941f9eab5f0SQu Wenruo if (sctx->stat.super_errors > old_super_errors && !sctx->readonly)
2942f9eab5f0SQu Wenruo need_commit = true;
2943f9eab5f0SQu Wenruo spin_unlock(&sctx->stat_lock);
29443cb0929aSWang Shilong }
2945a2de733cSArne Jansen
2946a2de733cSArne Jansen if (!ret)
294732934280SOmar Sandoval ret = scrub_enumerate_chunks(sctx, dev, start, end);
2948a5fb1142SFilipe Manana memalloc_nofs_restore(nofs_flag);
2949a2de733cSArne Jansen
2950a2de733cSArne Jansen atomic_dec(&fs_info->scrubs_running);
2951a2de733cSArne Jansen wake_up(&fs_info->scrub_pause_wait);
2952a2de733cSArne Jansen
2953a2de733cSArne Jansen if (progress)
2954d9d181c1SStefan Behrens memcpy(progress, &sctx->stat, sizeof(*progress));
2955a2de733cSArne Jansen
2956d1e14420SAnand Jain if (!is_dev_replace)
2957d1e14420SAnand Jain btrfs_info(fs_info, "scrub: %s on devid %llu with status: %d",
2958d1e14420SAnand Jain ret ? "not finished" : "finished", devid, ret);
2959d1e14420SAnand Jain
2960a2de733cSArne Jansen mutex_lock(&fs_info->scrub_lock);
2961cadbc0a0SAnand Jain dev->scrub_ctx = NULL;
2962a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
2963a2de733cSArne Jansen
2964e89c4a9cSJosef Bacik scrub_workers_put(fs_info);
2965f55985f4SFilipe Manana scrub_put_ctx(sctx);
2966a2de733cSArne Jansen
2967f9eab5f0SQu Wenruo /*
2968f9eab5f0SQu Wenruo * We found some super block errors before, now try to force a
2969f9eab5f0SQu Wenruo * transaction commit, as scrub has finished.
2970f9eab5f0SQu Wenruo */
2971f9eab5f0SQu Wenruo if (need_commit) {
2972f9eab5f0SQu Wenruo struct btrfs_trans_handle *trans;
2973f9eab5f0SQu Wenruo
2974f9eab5f0SQu Wenruo trans = btrfs_start_transaction(fs_info->tree_root, 0);
2975f9eab5f0SQu Wenruo if (IS_ERR(trans)) {
2976f9eab5f0SQu Wenruo ret = PTR_ERR(trans);
2977f9eab5f0SQu Wenruo btrfs_err(fs_info,
2978f9eab5f0SQu Wenruo "scrub: failed to start transaction to fix super block errors: %d", ret);
2979f9eab5f0SQu Wenruo return ret;
2980f9eab5f0SQu Wenruo }
2981f9eab5f0SQu Wenruo ret = btrfs_commit_transaction(trans);
2982f9eab5f0SQu Wenruo if (ret < 0)
2983f9eab5f0SQu Wenruo btrfs_err(fs_info,
2984f9eab5f0SQu Wenruo "scrub: failed to commit transaction to fix super block errors: %d", ret);
2985f9eab5f0SQu Wenruo }
2986a2de733cSArne Jansen return ret;
2987e89c4a9cSJosef Bacik out:
2988e89c4a9cSJosef Bacik scrub_workers_put(fs_info);
29890e94c4f4SDavid Sterba out_free_ctx:
29900e94c4f4SDavid Sterba scrub_free_ctx(sctx);
29910e94c4f4SDavid Sterba
29920e94c4f4SDavid Sterba return ret;
2993a2de733cSArne Jansen }
2994a2de733cSArne Jansen
btrfs_scrub_pause(struct btrfs_fs_info * fs_info)29952ff7e61eSJeff Mahoney void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
2996a2de733cSArne Jansen {
2997a2de733cSArne Jansen mutex_lock(&fs_info->scrub_lock);
2998a2de733cSArne Jansen atomic_inc(&fs_info->scrub_pause_req);
2999a2de733cSArne Jansen while (atomic_read(&fs_info->scrubs_paused) !=
3000a2de733cSArne Jansen atomic_read(&fs_info->scrubs_running)) {
3001a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
3002a2de733cSArne Jansen wait_event(fs_info->scrub_pause_wait,
3003a2de733cSArne Jansen atomic_read(&fs_info->scrubs_paused) ==
3004a2de733cSArne Jansen atomic_read(&fs_info->scrubs_running));
3005a2de733cSArne Jansen mutex_lock(&fs_info->scrub_lock);
3006a2de733cSArne Jansen }
3007a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
3008a2de733cSArne Jansen }
3009a2de733cSArne Jansen
btrfs_scrub_continue(struct btrfs_fs_info * fs_info)30102ff7e61eSJeff Mahoney void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
3011a2de733cSArne Jansen {
3012a2de733cSArne Jansen atomic_dec(&fs_info->scrub_pause_req);
3013a2de733cSArne Jansen wake_up(&fs_info->scrub_pause_wait);
3014a2de733cSArne Jansen }
3015a2de733cSArne Jansen
btrfs_scrub_cancel(struct btrfs_fs_info * fs_info)3016aa1b8cd4SStefan Behrens int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
3017a2de733cSArne Jansen {
3018a2de733cSArne Jansen mutex_lock(&fs_info->scrub_lock);
3019a2de733cSArne Jansen if (!atomic_read(&fs_info->scrubs_running)) {
3020a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
3021a2de733cSArne Jansen return -ENOTCONN;
3022a2de733cSArne Jansen }
3023a2de733cSArne Jansen
3024a2de733cSArne Jansen atomic_inc(&fs_info->scrub_cancel_req);
3025a2de733cSArne Jansen while (atomic_read(&fs_info->scrubs_running)) {
3026a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
3027a2de733cSArne Jansen wait_event(fs_info->scrub_pause_wait,
3028a2de733cSArne Jansen atomic_read(&fs_info->scrubs_running) == 0);
3029a2de733cSArne Jansen mutex_lock(&fs_info->scrub_lock);
3030a2de733cSArne Jansen }
3031a2de733cSArne Jansen atomic_dec(&fs_info->scrub_cancel_req);
3032a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
3033a2de733cSArne Jansen
3034a2de733cSArne Jansen return 0;
3035a2de733cSArne Jansen }
3036a2de733cSArne Jansen
btrfs_scrub_cancel_dev(struct btrfs_device * dev)3037163e97eeSDavid Sterba int btrfs_scrub_cancel_dev(struct btrfs_device *dev)
303849b25e05SJeff Mahoney {
3039163e97eeSDavid Sterba struct btrfs_fs_info *fs_info = dev->fs_info;
3040d9d181c1SStefan Behrens struct scrub_ctx *sctx;
3041a2de733cSArne Jansen
3042a2de733cSArne Jansen mutex_lock(&fs_info->scrub_lock);
3043cadbc0a0SAnand Jain sctx = dev->scrub_ctx;
3044d9d181c1SStefan Behrens if (!sctx) {
3045a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
3046a2de733cSArne Jansen return -ENOTCONN;
3047a2de733cSArne Jansen }
3048d9d181c1SStefan Behrens atomic_inc(&sctx->cancel_req);
3049cadbc0a0SAnand Jain while (dev->scrub_ctx) {
3050a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
3051a2de733cSArne Jansen wait_event(fs_info->scrub_pause_wait,
3052cadbc0a0SAnand Jain dev->scrub_ctx == NULL);
3053a2de733cSArne Jansen mutex_lock(&fs_info->scrub_lock);
3054a2de733cSArne Jansen }
3055a2de733cSArne Jansen mutex_unlock(&fs_info->scrub_lock);
3056a2de733cSArne Jansen
3057a2de733cSArne Jansen return 0;
3058a2de733cSArne Jansen }
30591623edebSStefan Behrens
btrfs_scrub_progress(struct btrfs_fs_info * fs_info,u64 devid,struct btrfs_scrub_progress * progress)30602ff7e61eSJeff Mahoney int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
3061a2de733cSArne Jansen struct btrfs_scrub_progress *progress)
3062a2de733cSArne Jansen {
3063562d7b15SJosef Bacik struct btrfs_dev_lookup_args args = { .devid = devid };
3064a2de733cSArne Jansen struct btrfs_device *dev;
3065d9d181c1SStefan Behrens struct scrub_ctx *sctx = NULL;
3066a2de733cSArne Jansen
30670b246afaSJeff Mahoney mutex_lock(&fs_info->fs_devices->device_list_mutex);
3068562d7b15SJosef Bacik dev = btrfs_find_device(fs_info->fs_devices, &args);
3069a2de733cSArne Jansen if (dev)
3070cadbc0a0SAnand Jain sctx = dev->scrub_ctx;
3071d9d181c1SStefan Behrens if (sctx)
3072d9d181c1SStefan Behrens memcpy(progress, &sctx->stat, sizeof(*progress));
30730b246afaSJeff Mahoney mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3074a2de733cSArne Jansen
3075d9d181c1SStefan Behrens return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
3076a2de733cSArne Jansen }
3077