btrfs: open code btrfs_dev_replace_clear_lock_blocking
[linux-2.6-block.git] / fs / btrfs / scrub.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
a2de733c 2/*
b6bfebc1 3 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
a2de733c
AJ
4 */
5
a2de733c 6#include <linux/blkdev.h>
558540c1 7#include <linux/ratelimit.h>
de2491fd 8#include <linux/sched/mm.h>
a2de733c
AJ
9#include "ctree.h"
10#include "volumes.h"
11#include "disk-io.h"
12#include "ordered-data.h"
0ef8e451 13#include "transaction.h"
558540c1 14#include "backref.h"
5da6fcbc 15#include "extent_io.h"
ff023aac 16#include "dev-replace.h"
21adbd5c 17#include "check-integrity.h"
606686ee 18#include "rcu-string.h"
53b381b3 19#include "raid56.h"
a2de733c
AJ
20
21/*
22 * This is only the first step towards a full-features scrub. It reads all
23 * extent and super block and verifies the checksums. In case a bad checksum
24 * is found or the extent cannot be read, good data will be written back if
25 * any can be found.
26 *
27 * Future enhancements:
a2de733c
AJ
28 * - In case an unrepairable extent is encountered, track which files are
29 * affected and report them
a2de733c 30 * - track and record media errors, throw out bad devices
a2de733c 31 * - add a mode to also read unallocated space
a2de733c
AJ
32 */
33
b5d67f64 34struct scrub_block;
d9d181c1 35struct scrub_ctx;
a2de733c 36
ff023aac
SB
37/*
38 * the following three values only influence the performance.
39 * The last one configures the number of parallel and outstanding I/O
40 * operations. The first two values configure an upper limit for the number
41 * of (dynamically allocated) pages that are added to a bio.
42 */
43#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
44#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
45#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
7a9e9987
SB
46
47/*
48 * the following value times PAGE_SIZE needs to be large enough to match the
49 * largest node/leaf/sector size that shall be supported.
50 * Values larger than BTRFS_STRIPE_LEN are not supported.
51 */
b5d67f64 52#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
a2de733c 53
af8e2d1d 54struct scrub_recover {
6f615018 55 refcount_t refs;
af8e2d1d 56 struct btrfs_bio *bbio;
af8e2d1d
MX
57 u64 map_length;
58};
59
a2de733c 60struct scrub_page {
b5d67f64
SB
61 struct scrub_block *sblock;
62 struct page *page;
442a4f63 63 struct btrfs_device *dev;
5a6ac9ea 64 struct list_head list;
a2de733c
AJ
65 u64 flags; /* extent flags */
66 u64 generation;
b5d67f64
SB
67 u64 logical;
68 u64 physical;
ff023aac 69 u64 physical_for_dev_replace;
57019345 70 atomic_t refs;
b5d67f64
SB
71 struct {
72 unsigned int mirror_num:8;
73 unsigned int have_csum:1;
74 unsigned int io_error:1;
75 };
a2de733c 76 u8 csum[BTRFS_CSUM_SIZE];
af8e2d1d
MX
77
78 struct scrub_recover *recover;
a2de733c
AJ
79};
80
81struct scrub_bio {
82 int index;
d9d181c1 83 struct scrub_ctx *sctx;
a36cf8b8 84 struct btrfs_device *dev;
a2de733c 85 struct bio *bio;
4e4cbee9 86 blk_status_t status;
a2de733c
AJ
87 u64 logical;
88 u64 physical;
ff023aac
SB
89#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
90 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
91#else
92 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
93#endif
b5d67f64 94 int page_count;
a2de733c
AJ
95 int next_free;
96 struct btrfs_work work;
97};
98
b5d67f64 99struct scrub_block {
7a9e9987 100 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
b5d67f64
SB
101 int page_count;
102 atomic_t outstanding_pages;
186debd6 103 refcount_t refs; /* free mem on transition to zero */
d9d181c1 104 struct scrub_ctx *sctx;
5a6ac9ea 105 struct scrub_parity *sparity;
b5d67f64
SB
106 struct {
107 unsigned int header_error:1;
108 unsigned int checksum_error:1;
109 unsigned int no_io_error_seen:1;
442a4f63 110 unsigned int generation_error:1; /* also sets header_error */
5a6ac9ea
MX
111
112 /* The following is for the data used to check parity */
113 /* It is for the data with checksum */
114 unsigned int data_corrected:1;
b5d67f64 115 };
73ff61db 116 struct btrfs_work work;
b5d67f64
SB
117};
118
5a6ac9ea
MX
119/* Used for the chunks with parity stripe such RAID5/6 */
120struct scrub_parity {
121 struct scrub_ctx *sctx;
122
123 struct btrfs_device *scrub_dev;
124
125 u64 logic_start;
126
127 u64 logic_end;
128
129 int nsectors;
130
972d7219 131 u64 stripe_len;
5a6ac9ea 132
78a76450 133 refcount_t refs;
5a6ac9ea
MX
134
135 struct list_head spages;
136
137 /* Work of parity check and repair */
138 struct btrfs_work work;
139
140 /* Mark the parity blocks which have data */
141 unsigned long *dbitmap;
142
143 /*
144 * Mark the parity blocks which have data, but errors happen when
145 * read data or check data
146 */
147 unsigned long *ebitmap;
148
149 unsigned long bitmap[0];
150};
151
d9d181c1 152struct scrub_ctx {
ff023aac 153 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
fb456252 154 struct btrfs_fs_info *fs_info;
a2de733c
AJ
155 int first_free;
156 int curr;
b6bfebc1
SB
157 atomic_t bios_in_flight;
158 atomic_t workers_pending;
a2de733c
AJ
159 spinlock_t list_lock;
160 wait_queue_head_t list_wait;
161 u16 csum_size;
162 struct list_head csum_list;
163 atomic_t cancel_req;
8628764e 164 int readonly;
ff023aac 165 int pages_per_rd_bio;
63a212ab
SB
166
167 int is_dev_replace;
3fb99303
DS
168
169 struct scrub_bio *wr_curr_bio;
170 struct mutex wr_lock;
171 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
3fb99303 172 struct btrfs_device *wr_tgtdev;
2073c4c2 173 bool flush_all_writes;
63a212ab 174
a2de733c
AJ
175 /*
176 * statistics
177 */
178 struct btrfs_scrub_progress stat;
179 spinlock_t stat_lock;
f55985f4
FM
180
181 /*
182 * Use a ref counter to avoid use-after-free issues. Scrub workers
183 * decrement bios_in_flight and workers_pending and then do a wakeup
184 * on the list_wait wait queue. We must ensure the main scrub task
185 * doesn't free the scrub context before or while the workers are
186 * doing the wakeup() call.
187 */
99f4cdb1 188 refcount_t refs;
a2de733c
AJ
189};
190
558540c1
JS
191struct scrub_warning {
192 struct btrfs_path *path;
193 u64 extent_item_size;
558540c1 194 const char *errstr;
6aa21263 195 u64 physical;
558540c1
JS
196 u64 logical;
197 struct btrfs_device *dev;
558540c1
JS
198};
199
0966a7b1
QW
200struct full_stripe_lock {
201 struct rb_node node;
202 u64 logical;
203 u64 refs;
204 struct mutex mutex;
205};
206
b6bfebc1
SB
207static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
208static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
b5d67f64 209static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
be50a8dd 210static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
ff023aac 211 struct scrub_block *sblocks_for_recheck);
34f5c8e9 212static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
affe4a5a
ZL
213 struct scrub_block *sblock,
214 int retry_failed_mirror);
ba7cf988 215static void scrub_recheck_block_checksum(struct scrub_block *sblock);
b5d67f64 216static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
114ab50d 217 struct scrub_block *sblock_good);
b5d67f64
SB
218static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
219 struct scrub_block *sblock_good,
220 int page_num, int force_write);
ff023aac
SB
221static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
222static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
223 int page_num);
b5d67f64
SB
224static int scrub_checksum_data(struct scrub_block *sblock);
225static int scrub_checksum_tree_block(struct scrub_block *sblock);
226static int scrub_checksum_super(struct scrub_block *sblock);
227static void scrub_block_get(struct scrub_block *sblock);
228static void scrub_block_put(struct scrub_block *sblock);
7a9e9987
SB
229static void scrub_page_get(struct scrub_page *spage);
230static void scrub_page_put(struct scrub_page *spage);
5a6ac9ea
MX
231static void scrub_parity_get(struct scrub_parity *sparity);
232static void scrub_parity_put(struct scrub_parity *sparity);
ff023aac
SB
233static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
234 struct scrub_page *spage);
d9d181c1 235static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
a36cf8b8 236 u64 physical, struct btrfs_device *dev, u64 flags,
ff023aac
SB
237 u64 gen, int mirror_num, u8 *csum, int force,
238 u64 physical_for_dev_replace);
4246a0b6 239static void scrub_bio_end_io(struct bio *bio);
b5d67f64
SB
240static void scrub_bio_end_io_worker(struct btrfs_work *work);
241static void scrub_block_complete(struct scrub_block *sblock);
ff023aac
SB
242static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
243 u64 extent_logical, u64 extent_len,
244 u64 *extent_physical,
245 struct btrfs_device **extent_dev,
246 int *extent_mirror_num);
ff023aac
SB
247static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
248 struct scrub_page *spage);
249static void scrub_wr_submit(struct scrub_ctx *sctx);
4246a0b6 250static void scrub_wr_bio_end_io(struct bio *bio);
ff023aac 251static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
cb7ab021 252static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
3cb0929a 253static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
f55985f4 254static void scrub_put_ctx(struct scrub_ctx *sctx);
1623edeb 255
762221f0
LB
256static inline int scrub_is_page_on_raid56(struct scrub_page *page)
257{
258 return page->recover &&
259 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
260}
1623edeb 261
b6bfebc1
SB
262static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
263{
99f4cdb1 264 refcount_inc(&sctx->refs);
b6bfebc1
SB
265 atomic_inc(&sctx->bios_in_flight);
266}
267
268static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
269{
270 atomic_dec(&sctx->bios_in_flight);
271 wake_up(&sctx->list_wait);
f55985f4 272 scrub_put_ctx(sctx);
b6bfebc1
SB
273}
274
cb7ab021 275static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
3cb0929a
WS
276{
277 while (atomic_read(&fs_info->scrub_pause_req)) {
278 mutex_unlock(&fs_info->scrub_lock);
279 wait_event(fs_info->scrub_pause_wait,
280 atomic_read(&fs_info->scrub_pause_req) == 0);
281 mutex_lock(&fs_info->scrub_lock);
282 }
283}
284
0e22be89 285static void scrub_pause_on(struct btrfs_fs_info *fs_info)
cb7ab021
WS
286{
287 atomic_inc(&fs_info->scrubs_paused);
288 wake_up(&fs_info->scrub_pause_wait);
0e22be89 289}
cb7ab021 290
0e22be89
Z
291static void scrub_pause_off(struct btrfs_fs_info *fs_info)
292{
cb7ab021
WS
293 mutex_lock(&fs_info->scrub_lock);
294 __scrub_blocked_if_needed(fs_info);
295 atomic_dec(&fs_info->scrubs_paused);
296 mutex_unlock(&fs_info->scrub_lock);
297
298 wake_up(&fs_info->scrub_pause_wait);
299}
300
0e22be89
Z
301static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
302{
303 scrub_pause_on(fs_info);
304 scrub_pause_off(fs_info);
305}
306
0966a7b1
QW
307/*
308 * Insert new full stripe lock into full stripe locks tree
309 *
310 * Return pointer to existing or newly inserted full_stripe_lock structure if
311 * everything works well.
312 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
313 *
314 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
315 * function
316 */
317static struct full_stripe_lock *insert_full_stripe_lock(
318 struct btrfs_full_stripe_locks_tree *locks_root,
319 u64 fstripe_logical)
320{
321 struct rb_node **p;
322 struct rb_node *parent = NULL;
323 struct full_stripe_lock *entry;
324 struct full_stripe_lock *ret;
325
a32bf9a3 326 lockdep_assert_held(&locks_root->lock);
0966a7b1
QW
327
328 p = &locks_root->root.rb_node;
329 while (*p) {
330 parent = *p;
331 entry = rb_entry(parent, struct full_stripe_lock, node);
332 if (fstripe_logical < entry->logical) {
333 p = &(*p)->rb_left;
334 } else if (fstripe_logical > entry->logical) {
335 p = &(*p)->rb_right;
336 } else {
337 entry->refs++;
338 return entry;
339 }
340 }
341
342 /* Insert new lock */
343 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
344 if (!ret)
345 return ERR_PTR(-ENOMEM);
346 ret->logical = fstripe_logical;
347 ret->refs = 1;
348 mutex_init(&ret->mutex);
349
350 rb_link_node(&ret->node, parent, p);
351 rb_insert_color(&ret->node, &locks_root->root);
352 return ret;
353}
354
355/*
356 * Search for a full stripe lock of a block group
357 *
358 * Return pointer to existing full stripe lock if found
359 * Return NULL if not found
360 */
361static struct full_stripe_lock *search_full_stripe_lock(
362 struct btrfs_full_stripe_locks_tree *locks_root,
363 u64 fstripe_logical)
364{
365 struct rb_node *node;
366 struct full_stripe_lock *entry;
367
a32bf9a3 368 lockdep_assert_held(&locks_root->lock);
0966a7b1
QW
369
370 node = locks_root->root.rb_node;
371 while (node) {
372 entry = rb_entry(node, struct full_stripe_lock, node);
373 if (fstripe_logical < entry->logical)
374 node = node->rb_left;
375 else if (fstripe_logical > entry->logical)
376 node = node->rb_right;
377 else
378 return entry;
379 }
380 return NULL;
381}
382
383/*
384 * Helper to get full stripe logical from a normal bytenr.
385 *
386 * Caller must ensure @cache is a RAID56 block group.
387 */
388static u64 get_full_stripe_logical(struct btrfs_block_group_cache *cache,
389 u64 bytenr)
390{
391 u64 ret;
392
393 /*
394 * Due to chunk item size limit, full stripe length should not be
395 * larger than U32_MAX. Just a sanity check here.
396 */
397 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
398
399 /*
400 * round_down() can only handle power of 2, while RAID56 full
401 * stripe length can be 64KiB * n, so we need to manually round down.
402 */
403 ret = div64_u64(bytenr - cache->key.objectid, cache->full_stripe_len) *
404 cache->full_stripe_len + cache->key.objectid;
405 return ret;
406}
407
408/*
409 * Lock a full stripe to avoid concurrency of recovery and read
410 *
411 * It's only used for profiles with parities (RAID5/6), for other profiles it
412 * does nothing.
413 *
414 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
415 * So caller must call unlock_full_stripe() at the same context.
416 *
417 * Return <0 if encounters error.
418 */
419static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
420 bool *locked_ret)
421{
422 struct btrfs_block_group_cache *bg_cache;
423 struct btrfs_full_stripe_locks_tree *locks_root;
424 struct full_stripe_lock *existing;
425 u64 fstripe_start;
426 int ret = 0;
427
428 *locked_ret = false;
429 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
430 if (!bg_cache) {
431 ASSERT(0);
432 return -ENOENT;
433 }
434
435 /* Profiles not based on parity don't need full stripe lock */
436 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
437 goto out;
438 locks_root = &bg_cache->full_stripe_locks_root;
439
440 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
441
442 /* Now insert the full stripe lock */
443 mutex_lock(&locks_root->lock);
444 existing = insert_full_stripe_lock(locks_root, fstripe_start);
445 mutex_unlock(&locks_root->lock);
446 if (IS_ERR(existing)) {
447 ret = PTR_ERR(existing);
448 goto out;
449 }
450 mutex_lock(&existing->mutex);
451 *locked_ret = true;
452out:
453 btrfs_put_block_group(bg_cache);
454 return ret;
455}
456
457/*
458 * Unlock a full stripe.
459 *
460 * NOTE: Caller must ensure it's the same context calling corresponding
461 * lock_full_stripe().
462 *
463 * Return 0 if we unlock full stripe without problem.
464 * Return <0 for error
465 */
466static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
467 bool locked)
468{
469 struct btrfs_block_group_cache *bg_cache;
470 struct btrfs_full_stripe_locks_tree *locks_root;
471 struct full_stripe_lock *fstripe_lock;
472 u64 fstripe_start;
473 bool freeit = false;
474 int ret = 0;
475
476 /* If we didn't acquire full stripe lock, no need to continue */
477 if (!locked)
478 return 0;
479
480 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
481 if (!bg_cache) {
482 ASSERT(0);
483 return -ENOENT;
484 }
485 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
486 goto out;
487
488 locks_root = &bg_cache->full_stripe_locks_root;
489 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
490
491 mutex_lock(&locks_root->lock);
492 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
493 /* Unpaired unlock_full_stripe() detected */
494 if (!fstripe_lock) {
495 WARN_ON(1);
496 ret = -ENOENT;
497 mutex_unlock(&locks_root->lock);
498 goto out;
499 }
500
501 if (fstripe_lock->refs == 0) {
502 WARN_ON(1);
503 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
504 fstripe_lock->logical);
505 } else {
506 fstripe_lock->refs--;
507 }
508
509 if (fstripe_lock->refs == 0) {
510 rb_erase(&fstripe_lock->node, &locks_root->root);
511 freeit = true;
512 }
513 mutex_unlock(&locks_root->lock);
514
515 mutex_unlock(&fstripe_lock->mutex);
516 if (freeit)
517 kfree(fstripe_lock);
518out:
519 btrfs_put_block_group(bg_cache);
520 return ret;
521}
522
d9d181c1 523static void scrub_free_csums(struct scrub_ctx *sctx)
a2de733c 524{
d9d181c1 525 while (!list_empty(&sctx->csum_list)) {
a2de733c 526 struct btrfs_ordered_sum *sum;
d9d181c1 527 sum = list_first_entry(&sctx->csum_list,
a2de733c
AJ
528 struct btrfs_ordered_sum, list);
529 list_del(&sum->list);
530 kfree(sum);
531 }
532}
533
d9d181c1 534static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
a2de733c
AJ
535{
536 int i;
a2de733c 537
d9d181c1 538 if (!sctx)
a2de733c
AJ
539 return;
540
b5d67f64 541 /* this can happen when scrub is cancelled */
d9d181c1
SB
542 if (sctx->curr != -1) {
543 struct scrub_bio *sbio = sctx->bios[sctx->curr];
b5d67f64
SB
544
545 for (i = 0; i < sbio->page_count; i++) {
ff023aac 546 WARN_ON(!sbio->pagev[i]->page);
b5d67f64
SB
547 scrub_block_put(sbio->pagev[i]->sblock);
548 }
549 bio_put(sbio->bio);
550 }
551
ff023aac 552 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
d9d181c1 553 struct scrub_bio *sbio = sctx->bios[i];
a2de733c
AJ
554
555 if (!sbio)
556 break;
a2de733c
AJ
557 kfree(sbio);
558 }
559
3fb99303 560 kfree(sctx->wr_curr_bio);
d9d181c1
SB
561 scrub_free_csums(sctx);
562 kfree(sctx);
a2de733c
AJ
563}
564
f55985f4
FM
565static void scrub_put_ctx(struct scrub_ctx *sctx)
566{
99f4cdb1 567 if (refcount_dec_and_test(&sctx->refs))
f55985f4
FM
568 scrub_free_ctx(sctx);
569}
570
a2de733c 571static noinline_for_stack
63a212ab 572struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
a2de733c 573{
d9d181c1 574 struct scrub_ctx *sctx;
a2de733c 575 int i;
fb456252 576 struct btrfs_fs_info *fs_info = dev->fs_info;
a2de733c 577
58c4e173 578 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
d9d181c1 579 if (!sctx)
a2de733c 580 goto nomem;
99f4cdb1 581 refcount_set(&sctx->refs, 1);
63a212ab 582 sctx->is_dev_replace = is_dev_replace;
b54ffb73 583 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
d9d181c1 584 sctx->curr = -1;
fb456252 585 sctx->fs_info = dev->fs_info;
ff023aac 586 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
a2de733c
AJ
587 struct scrub_bio *sbio;
588
58c4e173 589 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
a2de733c
AJ
590 if (!sbio)
591 goto nomem;
d9d181c1 592 sctx->bios[i] = sbio;
a2de733c 593
a2de733c 594 sbio->index = i;
d9d181c1 595 sbio->sctx = sctx;
b5d67f64 596 sbio->page_count = 0;
9e0af237
LB
597 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
598 scrub_bio_end_io_worker, NULL, NULL);
a2de733c 599
ff023aac 600 if (i != SCRUB_BIOS_PER_SCTX - 1)
d9d181c1 601 sctx->bios[i]->next_free = i + 1;
0ef8e451 602 else
d9d181c1
SB
603 sctx->bios[i]->next_free = -1;
604 }
605 sctx->first_free = 0;
b6bfebc1
SB
606 atomic_set(&sctx->bios_in_flight, 0);
607 atomic_set(&sctx->workers_pending, 0);
d9d181c1
SB
608 atomic_set(&sctx->cancel_req, 0);
609 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
610 INIT_LIST_HEAD(&sctx->csum_list);
611
612 spin_lock_init(&sctx->list_lock);
613 spin_lock_init(&sctx->stat_lock);
614 init_waitqueue_head(&sctx->list_wait);
ff023aac 615
3fb99303
DS
616 WARN_ON(sctx->wr_curr_bio != NULL);
617 mutex_init(&sctx->wr_lock);
618 sctx->wr_curr_bio = NULL;
8fcdac3f 619 if (is_dev_replace) {
ded56184 620 WARN_ON(!fs_info->dev_replace.tgtdev);
3fb99303 621 sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
ded56184 622 sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
2073c4c2 623 sctx->flush_all_writes = false;
ff023aac 624 }
8fcdac3f 625
d9d181c1 626 return sctx;
a2de733c
AJ
627
628nomem:
d9d181c1 629 scrub_free_ctx(sctx);
a2de733c
AJ
630 return ERR_PTR(-ENOMEM);
631}
632
ff023aac
SB
633static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
634 void *warn_ctx)
558540c1
JS
635{
636 u64 isize;
637 u32 nlink;
638 int ret;
639 int i;
de2491fd 640 unsigned nofs_flag;
558540c1
JS
641 struct extent_buffer *eb;
642 struct btrfs_inode_item *inode_item;
ff023aac 643 struct scrub_warning *swarn = warn_ctx;
fb456252 644 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
558540c1
JS
645 struct inode_fs_paths *ipath = NULL;
646 struct btrfs_root *local_root;
647 struct btrfs_key root_key;
1d4c08e0 648 struct btrfs_key key;
558540c1
JS
649
650 root_key.objectid = root;
651 root_key.type = BTRFS_ROOT_ITEM_KEY;
652 root_key.offset = (u64)-1;
653 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
654 if (IS_ERR(local_root)) {
655 ret = PTR_ERR(local_root);
656 goto err;
657 }
658
14692cc1
DS
659 /*
660 * this makes the path point to (inum INODE_ITEM ioff)
661 */
1d4c08e0
DS
662 key.objectid = inum;
663 key.type = BTRFS_INODE_ITEM_KEY;
664 key.offset = 0;
665
666 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
558540c1
JS
667 if (ret) {
668 btrfs_release_path(swarn->path);
669 goto err;
670 }
671
672 eb = swarn->path->nodes[0];
673 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
674 struct btrfs_inode_item);
675 isize = btrfs_inode_size(eb, inode_item);
676 nlink = btrfs_inode_nlink(eb, inode_item);
677 btrfs_release_path(swarn->path);
678
de2491fd
DS
679 /*
680 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
681 * uses GFP_NOFS in this context, so we keep it consistent but it does
682 * not seem to be strictly necessary.
683 */
684 nofs_flag = memalloc_nofs_save();
558540c1 685 ipath = init_ipath(4096, local_root, swarn->path);
de2491fd 686 memalloc_nofs_restore(nofs_flag);
26bdef54
DC
687 if (IS_ERR(ipath)) {
688 ret = PTR_ERR(ipath);
689 ipath = NULL;
690 goto err;
691 }
558540c1
JS
692 ret = paths_from_inode(inum, ipath);
693
694 if (ret < 0)
695 goto err;
696
697 /*
698 * we deliberately ignore the bit ipath might have been too small to
699 * hold all of the paths here
700 */
701 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
5d163e0e 702 btrfs_warn_in_rcu(fs_info,
6aa21263 703"%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
5d163e0e
JM
704 swarn->errstr, swarn->logical,
705 rcu_str_deref(swarn->dev->name),
6aa21263 706 swarn->physical,
5d163e0e
JM
707 root, inum, offset,
708 min(isize - offset, (u64)PAGE_SIZE), nlink,
709 (char *)(unsigned long)ipath->fspath->val[i]);
558540c1
JS
710
711 free_ipath(ipath);
712 return 0;
713
714err:
5d163e0e 715 btrfs_warn_in_rcu(fs_info,
6aa21263 716 "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
5d163e0e
JM
717 swarn->errstr, swarn->logical,
718 rcu_str_deref(swarn->dev->name),
6aa21263 719 swarn->physical,
5d163e0e 720 root, inum, offset, ret);
558540c1
JS
721
722 free_ipath(ipath);
723 return 0;
724}
725
b5d67f64 726static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
558540c1 727{
a36cf8b8
SB
728 struct btrfs_device *dev;
729 struct btrfs_fs_info *fs_info;
558540c1
JS
730 struct btrfs_path *path;
731 struct btrfs_key found_key;
732 struct extent_buffer *eb;
733 struct btrfs_extent_item *ei;
734 struct scrub_warning swarn;
69917e43
LB
735 unsigned long ptr = 0;
736 u64 extent_item_pos;
737 u64 flags = 0;
558540c1 738 u64 ref_root;
69917e43 739 u32 item_size;
07c9a8e0 740 u8 ref_level = 0;
69917e43 741 int ret;
558540c1 742
a36cf8b8 743 WARN_ON(sblock->page_count < 1);
7a9e9987 744 dev = sblock->pagev[0]->dev;
fb456252 745 fs_info = sblock->sctx->fs_info;
a36cf8b8 746
558540c1 747 path = btrfs_alloc_path();
8b9456da
DS
748 if (!path)
749 return;
558540c1 750
6aa21263 751 swarn.physical = sblock->pagev[0]->physical;
7a9e9987 752 swarn.logical = sblock->pagev[0]->logical;
558540c1 753 swarn.errstr = errstr;
a36cf8b8 754 swarn.dev = NULL;
558540c1 755
69917e43
LB
756 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
757 &flags);
558540c1
JS
758 if (ret < 0)
759 goto out;
760
4692cf58 761 extent_item_pos = swarn.logical - found_key.objectid;
558540c1
JS
762 swarn.extent_item_size = found_key.offset;
763
764 eb = path->nodes[0];
765 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
766 item_size = btrfs_item_size_nr(eb, path->slots[0]);
767
69917e43 768 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
558540c1 769 do {
6eda71d0
LB
770 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
771 item_size, &ref_root,
772 &ref_level);
ecaeb14b 773 btrfs_warn_in_rcu(fs_info,
6aa21263 774"%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
5d163e0e 775 errstr, swarn.logical,
606686ee 776 rcu_str_deref(dev->name),
6aa21263 777 swarn.physical,
558540c1
JS
778 ref_level ? "node" : "leaf",
779 ret < 0 ? -1 : ref_level,
780 ret < 0 ? -1 : ref_root);
781 } while (ret != 1);
d8fe29e9 782 btrfs_release_path(path);
558540c1 783 } else {
d8fe29e9 784 btrfs_release_path(path);
558540c1 785 swarn.path = path;
a36cf8b8 786 swarn.dev = dev;
7a3ae2f8
JS
787 iterate_extent_inodes(fs_info, found_key.objectid,
788 extent_item_pos, 1,
c995ab3c 789 scrub_print_warning_inode, &swarn, false);
558540c1
JS
790 }
791
792out:
793 btrfs_free_path(path);
558540c1
JS
794}
795
af8e2d1d
MX
796static inline void scrub_get_recover(struct scrub_recover *recover)
797{
6f615018 798 refcount_inc(&recover->refs);
af8e2d1d
MX
799}
800
e501bfe3
QW
801static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
802 struct scrub_recover *recover)
af8e2d1d 803{
6f615018 804 if (refcount_dec_and_test(&recover->refs)) {
e501bfe3 805 btrfs_bio_counter_dec(fs_info);
6e9606d2 806 btrfs_put_bbio(recover->bbio);
af8e2d1d
MX
807 kfree(recover);
808 }
809}
810
a2de733c 811/*
b5d67f64
SB
812 * scrub_handle_errored_block gets called when either verification of the
813 * pages failed or the bio failed to read, e.g. with EIO. In the latter
814 * case, this function handles all pages in the bio, even though only one
815 * may be bad.
816 * The goal of this function is to repair the errored block by using the
817 * contents of one of the mirrors.
a2de733c 818 */
b5d67f64 819static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
a2de733c 820{
d9d181c1 821 struct scrub_ctx *sctx = sblock_to_check->sctx;
a36cf8b8 822 struct btrfs_device *dev;
b5d67f64 823 struct btrfs_fs_info *fs_info;
b5d67f64 824 u64 logical;
b5d67f64
SB
825 unsigned int failed_mirror_index;
826 unsigned int is_metadata;
827 unsigned int have_csum;
b5d67f64
SB
828 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
829 struct scrub_block *sblock_bad;
830 int ret;
831 int mirror_index;
832 int page_num;
833 int success;
28d70e23 834 bool full_stripe_locked;
558540c1 835 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
b5d67f64
SB
836 DEFAULT_RATELIMIT_BURST);
837
838 BUG_ON(sblock_to_check->page_count < 1);
fb456252 839 fs_info = sctx->fs_info;
4ded4f63
SB
840 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
841 /*
842 * if we find an error in a super block, we just report it.
843 * They will get written with the next transaction commit
844 * anyway
845 */
846 spin_lock(&sctx->stat_lock);
847 ++sctx->stat.super_errors;
848 spin_unlock(&sctx->stat_lock);
849 return 0;
850 }
7a9e9987 851 logical = sblock_to_check->pagev[0]->logical;
7a9e9987
SB
852 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
853 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
854 is_metadata = !(sblock_to_check->pagev[0]->flags &
b5d67f64 855 BTRFS_EXTENT_FLAG_DATA);
7a9e9987 856 have_csum = sblock_to_check->pagev[0]->have_csum;
7a9e9987 857 dev = sblock_to_check->pagev[0]->dev;
13db62b7 858
28d70e23
QW
859 /*
860 * For RAID5/6, race can happen for a different device scrub thread.
861 * For data corruption, Parity and Data threads will both try
862 * to recovery the data.
863 * Race can lead to doubly added csum error, or even unrecoverable
864 * error.
865 */
866 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
867 if (ret < 0) {
868 spin_lock(&sctx->stat_lock);
869 if (ret == -ENOMEM)
870 sctx->stat.malloc_errors++;
871 sctx->stat.read_errors++;
872 sctx->stat.uncorrectable_errors++;
873 spin_unlock(&sctx->stat_lock);
874 return ret;
875 }
876
b5d67f64
SB
877 /*
878 * read all mirrors one after the other. This includes to
879 * re-read the extent or metadata block that failed (that was
880 * the cause that this fixup code is called) another time,
881 * page by page this time in order to know which pages
882 * caused I/O errors and which ones are good (for all mirrors).
883 * It is the goal to handle the situation when more than one
884 * mirror contains I/O errors, but the errors do not
885 * overlap, i.e. the data can be repaired by selecting the
886 * pages from those mirrors without I/O error on the
887 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
888 * would be that mirror #1 has an I/O error on the first page,
889 * the second page is good, and mirror #2 has an I/O error on
890 * the second page, but the first page is good.
891 * Then the first page of the first mirror can be repaired by
892 * taking the first page of the second mirror, and the
893 * second page of the second mirror can be repaired by
894 * copying the contents of the 2nd page of the 1st mirror.
895 * One more note: if the pages of one mirror contain I/O
896 * errors, the checksum cannot be verified. In order to get
897 * the best data for repairing, the first attempt is to find
898 * a mirror without I/O errors and with a validated checksum.
899 * Only if this is not possible, the pages are picked from
900 * mirrors with I/O errors without considering the checksum.
901 * If the latter is the case, at the end, the checksum of the
902 * repaired area is verified in order to correctly maintain
903 * the statistics.
904 */
905
31e818fe
DS
906 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
907 sizeof(*sblocks_for_recheck), GFP_NOFS);
b5d67f64 908 if (!sblocks_for_recheck) {
d9d181c1
SB
909 spin_lock(&sctx->stat_lock);
910 sctx->stat.malloc_errors++;
911 sctx->stat.read_errors++;
912 sctx->stat.uncorrectable_errors++;
913 spin_unlock(&sctx->stat_lock);
a36cf8b8 914 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
b5d67f64 915 goto out;
a2de733c
AJ
916 }
917
b5d67f64 918 /* setup the context, map the logical blocks and alloc the pages */
be50a8dd 919 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
b5d67f64 920 if (ret) {
d9d181c1
SB
921 spin_lock(&sctx->stat_lock);
922 sctx->stat.read_errors++;
923 sctx->stat.uncorrectable_errors++;
924 spin_unlock(&sctx->stat_lock);
a36cf8b8 925 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
b5d67f64
SB
926 goto out;
927 }
928 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
929 sblock_bad = sblocks_for_recheck + failed_mirror_index;
13db62b7 930
b5d67f64 931 /* build and submit the bios for the failed mirror, check checksums */
affe4a5a 932 scrub_recheck_block(fs_info, sblock_bad, 1);
a2de733c 933
b5d67f64
SB
934 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
935 sblock_bad->no_io_error_seen) {
936 /*
937 * the error disappeared after reading page by page, or
938 * the area was part of a huge bio and other parts of the
939 * bio caused I/O errors, or the block layer merged several
940 * read requests into one and the error is caused by a
941 * different bio (usually one of the two latter cases is
942 * the cause)
943 */
d9d181c1
SB
944 spin_lock(&sctx->stat_lock);
945 sctx->stat.unverified_errors++;
5a6ac9ea 946 sblock_to_check->data_corrected = 1;
d9d181c1 947 spin_unlock(&sctx->stat_lock);
a2de733c 948
ff023aac
SB
949 if (sctx->is_dev_replace)
950 scrub_write_block_to_dev_replace(sblock_bad);
b5d67f64 951 goto out;
a2de733c 952 }
a2de733c 953
b5d67f64 954 if (!sblock_bad->no_io_error_seen) {
d9d181c1
SB
955 spin_lock(&sctx->stat_lock);
956 sctx->stat.read_errors++;
957 spin_unlock(&sctx->stat_lock);
b5d67f64
SB
958 if (__ratelimit(&_rs))
959 scrub_print_warning("i/o error", sblock_to_check);
a36cf8b8 960 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
b5d67f64 961 } else if (sblock_bad->checksum_error) {
d9d181c1
SB
962 spin_lock(&sctx->stat_lock);
963 sctx->stat.csum_errors++;
964 spin_unlock(&sctx->stat_lock);
b5d67f64
SB
965 if (__ratelimit(&_rs))
966 scrub_print_warning("checksum error", sblock_to_check);
a36cf8b8 967 btrfs_dev_stat_inc_and_print(dev,
442a4f63 968 BTRFS_DEV_STAT_CORRUPTION_ERRS);
b5d67f64 969 } else if (sblock_bad->header_error) {
d9d181c1
SB
970 spin_lock(&sctx->stat_lock);
971 sctx->stat.verify_errors++;
972 spin_unlock(&sctx->stat_lock);
b5d67f64
SB
973 if (__ratelimit(&_rs))
974 scrub_print_warning("checksum/header error",
975 sblock_to_check);
442a4f63 976 if (sblock_bad->generation_error)
a36cf8b8 977 btrfs_dev_stat_inc_and_print(dev,
442a4f63
SB
978 BTRFS_DEV_STAT_GENERATION_ERRS);
979 else
a36cf8b8 980 btrfs_dev_stat_inc_and_print(dev,
442a4f63 981 BTRFS_DEV_STAT_CORRUPTION_ERRS);
b5d67f64 982 }
a2de733c 983
33ef30ad
ID
984 if (sctx->readonly) {
985 ASSERT(!sctx->is_dev_replace);
986 goto out;
987 }
a2de733c 988
b5d67f64
SB
989 /*
990 * now build and submit the bios for the other mirrors, check
cb2ced73
SB
991 * checksums.
992 * First try to pick the mirror which is completely without I/O
b5d67f64
SB
993 * errors and also does not have a checksum error.
994 * If one is found, and if a checksum is present, the full block
995 * that is known to contain an error is rewritten. Afterwards
996 * the block is known to be corrected.
997 * If a mirror is found which is completely correct, and no
998 * checksum is present, only those pages are rewritten that had
999 * an I/O error in the block to be repaired, since it cannot be
1000 * determined, which copy of the other pages is better (and it
1001 * could happen otherwise that a correct page would be
1002 * overwritten by a bad one).
1003 */
762221f0 1004 for (mirror_index = 0; ;mirror_index++) {
cb2ced73 1005 struct scrub_block *sblock_other;
b5d67f64 1006
cb2ced73
SB
1007 if (mirror_index == failed_mirror_index)
1008 continue;
762221f0
LB
1009
1010 /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
1011 if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1012 if (mirror_index >= BTRFS_MAX_MIRRORS)
1013 break;
1014 if (!sblocks_for_recheck[mirror_index].page_count)
1015 break;
1016
1017 sblock_other = sblocks_for_recheck + mirror_index;
1018 } else {
1019 struct scrub_recover *r = sblock_bad->pagev[0]->recover;
1020 int max_allowed = r->bbio->num_stripes -
1021 r->bbio->num_tgtdevs;
1022
1023 if (mirror_index >= max_allowed)
1024 break;
1025 if (!sblocks_for_recheck[1].page_count)
1026 break;
1027
1028 ASSERT(failed_mirror_index == 0);
1029 sblock_other = sblocks_for_recheck + 1;
1030 sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
1031 }
cb2ced73
SB
1032
1033 /* build and submit the bios, check checksums */
affe4a5a 1034 scrub_recheck_block(fs_info, sblock_other, 0);
34f5c8e9
SB
1035
1036 if (!sblock_other->header_error &&
b5d67f64
SB
1037 !sblock_other->checksum_error &&
1038 sblock_other->no_io_error_seen) {
ff023aac
SB
1039 if (sctx->is_dev_replace) {
1040 scrub_write_block_to_dev_replace(sblock_other);
114ab50d 1041 goto corrected_error;
ff023aac 1042 } else {
ff023aac 1043 ret = scrub_repair_block_from_good_copy(
114ab50d
ZL
1044 sblock_bad, sblock_other);
1045 if (!ret)
1046 goto corrected_error;
ff023aac 1047 }
b5d67f64
SB
1048 }
1049 }
a2de733c 1050
b968fed1
ZL
1051 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1052 goto did_not_correct_error;
ff023aac
SB
1053
1054 /*
ff023aac 1055 * In case of I/O errors in the area that is supposed to be
b5d67f64
SB
1056 * repaired, continue by picking good copies of those pages.
1057 * Select the good pages from mirrors to rewrite bad pages from
1058 * the area to fix. Afterwards verify the checksum of the block
1059 * that is supposed to be repaired. This verification step is
1060 * only done for the purpose of statistic counting and for the
1061 * final scrub report, whether errors remain.
1062 * A perfect algorithm could make use of the checksum and try
1063 * all possible combinations of pages from the different mirrors
1064 * until the checksum verification succeeds. For example, when
1065 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1066 * of mirror #2 is readable but the final checksum test fails,
1067 * then the 2nd page of mirror #3 could be tried, whether now
01327610 1068 * the final checksum succeeds. But this would be a rare
b5d67f64
SB
1069 * exception and is therefore not implemented. At least it is
1070 * avoided that the good copy is overwritten.
1071 * A more useful improvement would be to pick the sectors
1072 * without I/O error based on sector sizes (512 bytes on legacy
1073 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1074 * mirror could be repaired by taking 512 byte of a different
1075 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1076 * area are unreadable.
a2de733c 1077 */
b5d67f64 1078 success = 1;
b968fed1
ZL
1079 for (page_num = 0; page_num < sblock_bad->page_count;
1080 page_num++) {
7a9e9987 1081 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
b968fed1 1082 struct scrub_block *sblock_other = NULL;
b5d67f64 1083
b968fed1
ZL
1084 /* skip no-io-error page in scrub */
1085 if (!page_bad->io_error && !sctx->is_dev_replace)
a2de733c 1086 continue;
b5d67f64 1087
4759700a
LB
1088 if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1089 /*
1090 * In case of dev replace, if raid56 rebuild process
1091 * didn't work out correct data, then copy the content
1092 * in sblock_bad to make sure target device is identical
1093 * to source device, instead of writing garbage data in
1094 * sblock_for_recheck array to target device.
1095 */
1096 sblock_other = NULL;
1097 } else if (page_bad->io_error) {
1098 /* try to find no-io-error page in mirrors */
b968fed1
ZL
1099 for (mirror_index = 0;
1100 mirror_index < BTRFS_MAX_MIRRORS &&
1101 sblocks_for_recheck[mirror_index].page_count > 0;
1102 mirror_index++) {
1103 if (!sblocks_for_recheck[mirror_index].
1104 pagev[page_num]->io_error) {
1105 sblock_other = sblocks_for_recheck +
1106 mirror_index;
1107 break;
b5d67f64
SB
1108 }
1109 }
b968fed1
ZL
1110 if (!sblock_other)
1111 success = 0;
96e36920 1112 }
a2de733c 1113
b968fed1
ZL
1114 if (sctx->is_dev_replace) {
1115 /*
1116 * did not find a mirror to fetch the page
1117 * from. scrub_write_page_to_dev_replace()
1118 * handles this case (page->io_error), by
1119 * filling the block with zeros before
1120 * submitting the write request
1121 */
1122 if (!sblock_other)
1123 sblock_other = sblock_bad;
1124
1125 if (scrub_write_page_to_dev_replace(sblock_other,
1126 page_num) != 0) {
1127 btrfs_dev_replace_stats_inc(
0b246afa 1128 &fs_info->dev_replace.num_write_errors);
b968fed1
ZL
1129 success = 0;
1130 }
1131 } else if (sblock_other) {
1132 ret = scrub_repair_page_from_good_copy(sblock_bad,
1133 sblock_other,
1134 page_num, 0);
1135 if (0 == ret)
1136 page_bad->io_error = 0;
1137 else
1138 success = 0;
b5d67f64 1139 }
a2de733c 1140 }
a2de733c 1141
b968fed1 1142 if (success && !sctx->is_dev_replace) {
b5d67f64
SB
1143 if (is_metadata || have_csum) {
1144 /*
1145 * need to verify the checksum now that all
1146 * sectors on disk are repaired (the write
1147 * request for data to be repaired is on its way).
1148 * Just be lazy and use scrub_recheck_block()
1149 * which re-reads the data before the checksum
1150 * is verified, but most likely the data comes out
1151 * of the page cache.
1152 */
affe4a5a 1153 scrub_recheck_block(fs_info, sblock_bad, 1);
34f5c8e9 1154 if (!sblock_bad->header_error &&
b5d67f64
SB
1155 !sblock_bad->checksum_error &&
1156 sblock_bad->no_io_error_seen)
1157 goto corrected_error;
1158 else
1159 goto did_not_correct_error;
1160 } else {
1161corrected_error:
d9d181c1
SB
1162 spin_lock(&sctx->stat_lock);
1163 sctx->stat.corrected_errors++;
5a6ac9ea 1164 sblock_to_check->data_corrected = 1;
d9d181c1 1165 spin_unlock(&sctx->stat_lock);
b14af3b4
DS
1166 btrfs_err_rl_in_rcu(fs_info,
1167 "fixed up error at logical %llu on dev %s",
c1c9ff7c 1168 logical, rcu_str_deref(dev->name));
8628764e 1169 }
b5d67f64
SB
1170 } else {
1171did_not_correct_error:
d9d181c1
SB
1172 spin_lock(&sctx->stat_lock);
1173 sctx->stat.uncorrectable_errors++;
1174 spin_unlock(&sctx->stat_lock);
b14af3b4
DS
1175 btrfs_err_rl_in_rcu(fs_info,
1176 "unable to fixup (regular) error at logical %llu on dev %s",
c1c9ff7c 1177 logical, rcu_str_deref(dev->name));
96e36920 1178 }
a2de733c 1179
b5d67f64
SB
1180out:
1181 if (sblocks_for_recheck) {
1182 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1183 mirror_index++) {
1184 struct scrub_block *sblock = sblocks_for_recheck +
1185 mirror_index;
af8e2d1d 1186 struct scrub_recover *recover;
b5d67f64
SB
1187 int page_index;
1188
7a9e9987
SB
1189 for (page_index = 0; page_index < sblock->page_count;
1190 page_index++) {
1191 sblock->pagev[page_index]->sblock = NULL;
af8e2d1d
MX
1192 recover = sblock->pagev[page_index]->recover;
1193 if (recover) {
e501bfe3 1194 scrub_put_recover(fs_info, recover);
af8e2d1d
MX
1195 sblock->pagev[page_index]->recover =
1196 NULL;
1197 }
7a9e9987
SB
1198 scrub_page_put(sblock->pagev[page_index]);
1199 }
b5d67f64
SB
1200 }
1201 kfree(sblocks_for_recheck);
1202 }
a2de733c 1203
28d70e23
QW
1204 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
1205 if (ret < 0)
1206 return ret;
b5d67f64
SB
1207 return 0;
1208}
a2de733c 1209
8e5cfb55 1210static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
af8e2d1d 1211{
10f11900
ZL
1212 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1213 return 2;
1214 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1215 return 3;
1216 else
af8e2d1d 1217 return (int)bbio->num_stripes;
af8e2d1d
MX
1218}
1219
10f11900
ZL
1220static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1221 u64 *raid_map,
af8e2d1d
MX
1222 u64 mapped_length,
1223 int nstripes, int mirror,
1224 int *stripe_index,
1225 u64 *stripe_offset)
1226{
1227 int i;
1228
ffe2d203 1229 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
af8e2d1d
MX
1230 /* RAID5/6 */
1231 for (i = 0; i < nstripes; i++) {
1232 if (raid_map[i] == RAID6_Q_STRIPE ||
1233 raid_map[i] == RAID5_P_STRIPE)
1234 continue;
1235
1236 if (logical >= raid_map[i] &&
1237 logical < raid_map[i] + mapped_length)
1238 break;
1239 }
1240
1241 *stripe_index = i;
1242 *stripe_offset = logical - raid_map[i];
1243 } else {
1244 /* The other RAID type */
1245 *stripe_index = mirror;
1246 *stripe_offset = 0;
1247 }
1248}
1249
be50a8dd 1250static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
b5d67f64
SB
1251 struct scrub_block *sblocks_for_recheck)
1252{
be50a8dd 1253 struct scrub_ctx *sctx = original_sblock->sctx;
fb456252 1254 struct btrfs_fs_info *fs_info = sctx->fs_info;
be50a8dd
ZL
1255 u64 length = original_sblock->page_count * PAGE_SIZE;
1256 u64 logical = original_sblock->pagev[0]->logical;
4734b7ed
ZL
1257 u64 generation = original_sblock->pagev[0]->generation;
1258 u64 flags = original_sblock->pagev[0]->flags;
1259 u64 have_csum = original_sblock->pagev[0]->have_csum;
af8e2d1d
MX
1260 struct scrub_recover *recover;
1261 struct btrfs_bio *bbio;
af8e2d1d
MX
1262 u64 sublen;
1263 u64 mapped_length;
1264 u64 stripe_offset;
1265 int stripe_index;
be50a8dd 1266 int page_index = 0;
b5d67f64 1267 int mirror_index;
af8e2d1d 1268 int nmirrors;
b5d67f64
SB
1269 int ret;
1270
1271 /*
57019345 1272 * note: the two members refs and outstanding_pages
b5d67f64
SB
1273 * are not used (and not set) in the blocks that are used for
1274 * the recheck procedure
1275 */
1276
b5d67f64 1277 while (length > 0) {
af8e2d1d
MX
1278 sublen = min_t(u64, length, PAGE_SIZE);
1279 mapped_length = sublen;
1280 bbio = NULL;
a2de733c 1281
b5d67f64
SB
1282 /*
1283 * with a length of PAGE_SIZE, each returned stripe
1284 * represents one mirror
1285 */
e501bfe3 1286 btrfs_bio_counter_inc_blocked(fs_info);
cf8cddd3 1287 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
825ad4c9 1288 logical, &mapped_length, &bbio);
b5d67f64 1289 if (ret || !bbio || mapped_length < sublen) {
6e9606d2 1290 btrfs_put_bbio(bbio);
e501bfe3 1291 btrfs_bio_counter_dec(fs_info);
b5d67f64
SB
1292 return -EIO;
1293 }
a2de733c 1294
af8e2d1d
MX
1295 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1296 if (!recover) {
6e9606d2 1297 btrfs_put_bbio(bbio);
e501bfe3 1298 btrfs_bio_counter_dec(fs_info);
af8e2d1d
MX
1299 return -ENOMEM;
1300 }
1301
6f615018 1302 refcount_set(&recover->refs, 1);
af8e2d1d 1303 recover->bbio = bbio;
af8e2d1d
MX
1304 recover->map_length = mapped_length;
1305
24731149 1306 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
af8e2d1d 1307
be50a8dd 1308 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
10f11900 1309
af8e2d1d 1310 for (mirror_index = 0; mirror_index < nmirrors;
b5d67f64
SB
1311 mirror_index++) {
1312 struct scrub_block *sblock;
1313 struct scrub_page *page;
1314
b5d67f64 1315 sblock = sblocks_for_recheck + mirror_index;
7a9e9987 1316 sblock->sctx = sctx;
4734b7ed 1317
7a9e9987
SB
1318 page = kzalloc(sizeof(*page), GFP_NOFS);
1319 if (!page) {
1320leave_nomem:
d9d181c1
SB
1321 spin_lock(&sctx->stat_lock);
1322 sctx->stat.malloc_errors++;
1323 spin_unlock(&sctx->stat_lock);
e501bfe3 1324 scrub_put_recover(fs_info, recover);
b5d67f64
SB
1325 return -ENOMEM;
1326 }
7a9e9987
SB
1327 scrub_page_get(page);
1328 sblock->pagev[page_index] = page;
4734b7ed
ZL
1329 page->sblock = sblock;
1330 page->flags = flags;
1331 page->generation = generation;
7a9e9987 1332 page->logical = logical;
4734b7ed
ZL
1333 page->have_csum = have_csum;
1334 if (have_csum)
1335 memcpy(page->csum,
1336 original_sblock->pagev[0]->csum,
1337 sctx->csum_size);
af8e2d1d 1338
10f11900
ZL
1339 scrub_stripe_index_and_offset(logical,
1340 bbio->map_type,
1341 bbio->raid_map,
af8e2d1d 1342 mapped_length,
e34c330d
ZL
1343 bbio->num_stripes -
1344 bbio->num_tgtdevs,
af8e2d1d
MX
1345 mirror_index,
1346 &stripe_index,
1347 &stripe_offset);
1348 page->physical = bbio->stripes[stripe_index].physical +
1349 stripe_offset;
1350 page->dev = bbio->stripes[stripe_index].dev;
1351
ff023aac
SB
1352 BUG_ON(page_index >= original_sblock->page_count);
1353 page->physical_for_dev_replace =
1354 original_sblock->pagev[page_index]->
1355 physical_for_dev_replace;
7a9e9987 1356 /* for missing devices, dev->bdev is NULL */
7a9e9987 1357 page->mirror_num = mirror_index + 1;
b5d67f64 1358 sblock->page_count++;
7a9e9987
SB
1359 page->page = alloc_page(GFP_NOFS);
1360 if (!page->page)
1361 goto leave_nomem;
af8e2d1d
MX
1362
1363 scrub_get_recover(recover);
1364 page->recover = recover;
b5d67f64 1365 }
e501bfe3 1366 scrub_put_recover(fs_info, recover);
b5d67f64
SB
1367 length -= sublen;
1368 logical += sublen;
1369 page_index++;
1370 }
1371
1372 return 0;
96e36920
ID
1373}
1374
4246a0b6 1375static void scrub_bio_wait_endio(struct bio *bio)
af8e2d1d 1376{
b4ff5ad7 1377 complete(bio->bi_private);
af8e2d1d
MX
1378}
1379
af8e2d1d
MX
1380static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1381 struct bio *bio,
1382 struct scrub_page *page)
1383{
b4ff5ad7 1384 DECLARE_COMPLETION_ONSTACK(done);
af8e2d1d 1385 int ret;
762221f0 1386 int mirror_num;
af8e2d1d 1387
af8e2d1d
MX
1388 bio->bi_iter.bi_sector = page->logical >> 9;
1389 bio->bi_private = &done;
1390 bio->bi_end_io = scrub_bio_wait_endio;
1391
762221f0 1392 mirror_num = page->sblock->pagev[0]->mirror_num;
2ff7e61e 1393 ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
af8e2d1d 1394 page->recover->map_length,
762221f0 1395 mirror_num, 0);
af8e2d1d
MX
1396 if (ret)
1397 return ret;
1398
b4ff5ad7
LB
1399 wait_for_completion_io(&done);
1400 return blk_status_to_errno(bio->bi_status);
af8e2d1d
MX
1401}
1402
6ca1765b
LB
1403static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
1404 struct scrub_block *sblock)
1405{
1406 struct scrub_page *first_page = sblock->pagev[0];
1407 struct bio *bio;
1408 int page_num;
1409
1410 /* All pages in sblock belong to the same stripe on the same device. */
1411 ASSERT(first_page->dev);
1412 if (!first_page->dev->bdev)
1413 goto out;
1414
1415 bio = btrfs_io_bio_alloc(BIO_MAX_PAGES);
1416 bio_set_dev(bio, first_page->dev->bdev);
1417
1418 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1419 struct scrub_page *page = sblock->pagev[page_num];
1420
1421 WARN_ON(!page->page);
1422 bio_add_page(bio, page->page, PAGE_SIZE, 0);
1423 }
1424
1425 if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) {
1426 bio_put(bio);
1427 goto out;
1428 }
1429
1430 bio_put(bio);
1431
1432 scrub_recheck_block_checksum(sblock);
1433
1434 return;
1435out:
1436 for (page_num = 0; page_num < sblock->page_count; page_num++)
1437 sblock->pagev[page_num]->io_error = 1;
1438
1439 sblock->no_io_error_seen = 0;
1440}
1441
b5d67f64
SB
1442/*
1443 * this function will check the on disk data for checksum errors, header
1444 * errors and read I/O errors. If any I/O errors happen, the exact pages
1445 * which are errored are marked as being bad. The goal is to enable scrub
1446 * to take those pages that are not errored from all the mirrors so that
1447 * the pages that are errored in the just handled mirror can be repaired.
1448 */
34f5c8e9 1449static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
affe4a5a
ZL
1450 struct scrub_block *sblock,
1451 int retry_failed_mirror)
96e36920 1452{
b5d67f64 1453 int page_num;
96e36920 1454
b5d67f64 1455 sblock->no_io_error_seen = 1;
96e36920 1456
6ca1765b
LB
1457 /* short cut for raid56 */
1458 if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0]))
1459 return scrub_recheck_block_on_raid56(fs_info, sblock);
1460
b5d67f64
SB
1461 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1462 struct bio *bio;
7a9e9987 1463 struct scrub_page *page = sblock->pagev[page_num];
b5d67f64 1464
442a4f63 1465 if (page->dev->bdev == NULL) {
ea9947b4
SB
1466 page->io_error = 1;
1467 sblock->no_io_error_seen = 0;
1468 continue;
1469 }
1470
7a9e9987 1471 WARN_ON(!page->page);
c5e4c3d7 1472 bio = btrfs_io_bio_alloc(1);
74d46992 1473 bio_set_dev(bio, page->dev->bdev);
b5d67f64 1474
34f5c8e9 1475 bio_add_page(bio, page->page, PAGE_SIZE, 0);
6ca1765b
LB
1476 bio->bi_iter.bi_sector = page->physical >> 9;
1477 bio->bi_opf = REQ_OP_READ;
af8e2d1d 1478
6ca1765b
LB
1479 if (btrfsic_submit_bio_wait(bio)) {
1480 page->io_error = 1;
1481 sblock->no_io_error_seen = 0;
af8e2d1d 1482 }
33879d45 1483
b5d67f64
SB
1484 bio_put(bio);
1485 }
96e36920 1486
b5d67f64 1487 if (sblock->no_io_error_seen)
ba7cf988 1488 scrub_recheck_block_checksum(sblock);
a2de733c
AJ
1489}
1490
17a9be2f
MX
1491static inline int scrub_check_fsid(u8 fsid[],
1492 struct scrub_page *spage)
1493{
1494 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1495 int ret;
1496
44880fdc 1497 ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
17a9be2f
MX
1498 return !ret;
1499}
1500
ba7cf988 1501static void scrub_recheck_block_checksum(struct scrub_block *sblock)
a2de733c 1502{
ba7cf988
ZL
1503 sblock->header_error = 0;
1504 sblock->checksum_error = 0;
1505 sblock->generation_error = 0;
b5d67f64 1506
ba7cf988
ZL
1507 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1508 scrub_checksum_data(sblock);
1509 else
1510 scrub_checksum_tree_block(sblock);
a2de733c
AJ
1511}
1512
b5d67f64 1513static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
114ab50d 1514 struct scrub_block *sblock_good)
b5d67f64
SB
1515{
1516 int page_num;
1517 int ret = 0;
96e36920 1518
b5d67f64
SB
1519 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1520 int ret_sub;
96e36920 1521
b5d67f64
SB
1522 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1523 sblock_good,
114ab50d 1524 page_num, 1);
b5d67f64
SB
1525 if (ret_sub)
1526 ret = ret_sub;
a2de733c 1527 }
b5d67f64
SB
1528
1529 return ret;
1530}
1531
1532static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1533 struct scrub_block *sblock_good,
1534 int page_num, int force_write)
1535{
7a9e9987
SB
1536 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1537 struct scrub_page *page_good = sblock_good->pagev[page_num];
0b246afa 1538 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
b5d67f64 1539
7a9e9987
SB
1540 BUG_ON(page_bad->page == NULL);
1541 BUG_ON(page_good->page == NULL);
b5d67f64
SB
1542 if (force_write || sblock_bad->header_error ||
1543 sblock_bad->checksum_error || page_bad->io_error) {
1544 struct bio *bio;
1545 int ret;
b5d67f64 1546
ff023aac 1547 if (!page_bad->dev->bdev) {
0b246afa 1548 btrfs_warn_rl(fs_info,
5d163e0e 1549 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
ff023aac
SB
1550 return -EIO;
1551 }
1552
c5e4c3d7 1553 bio = btrfs_io_bio_alloc(1);
74d46992 1554 bio_set_dev(bio, page_bad->dev->bdev);
4f024f37 1555 bio->bi_iter.bi_sector = page_bad->physical >> 9;
ebcc3263 1556 bio->bi_opf = REQ_OP_WRITE;
b5d67f64
SB
1557
1558 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1559 if (PAGE_SIZE != ret) {
1560 bio_put(bio);
1561 return -EIO;
13db62b7 1562 }
b5d67f64 1563
4e49ea4a 1564 if (btrfsic_submit_bio_wait(bio)) {
442a4f63
SB
1565 btrfs_dev_stat_inc_and_print(page_bad->dev,
1566 BTRFS_DEV_STAT_WRITE_ERRS);
ff023aac 1567 btrfs_dev_replace_stats_inc(
0b246afa 1568 &fs_info->dev_replace.num_write_errors);
442a4f63
SB
1569 bio_put(bio);
1570 return -EIO;
1571 }
b5d67f64 1572 bio_put(bio);
a2de733c
AJ
1573 }
1574
b5d67f64
SB
1575 return 0;
1576}
1577
ff023aac
SB
1578static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1579{
0b246afa 1580 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
ff023aac
SB
1581 int page_num;
1582
5a6ac9ea
MX
1583 /*
1584 * This block is used for the check of the parity on the source device,
1585 * so the data needn't be written into the destination device.
1586 */
1587 if (sblock->sparity)
1588 return;
1589
ff023aac
SB
1590 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1591 int ret;
1592
1593 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1594 if (ret)
1595 btrfs_dev_replace_stats_inc(
0b246afa 1596 &fs_info->dev_replace.num_write_errors);
ff023aac
SB
1597 }
1598}
1599
1600static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1601 int page_num)
1602{
1603 struct scrub_page *spage = sblock->pagev[page_num];
1604
1605 BUG_ON(spage->page == NULL);
1606 if (spage->io_error) {
1607 void *mapped_buffer = kmap_atomic(spage->page);
1608
619a9742 1609 clear_page(mapped_buffer);
ff023aac
SB
1610 flush_dcache_page(spage->page);
1611 kunmap_atomic(mapped_buffer);
1612 }
1613 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1614}
1615
1616static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1617 struct scrub_page *spage)
1618{
ff023aac
SB
1619 struct scrub_bio *sbio;
1620 int ret;
1621
3fb99303 1622 mutex_lock(&sctx->wr_lock);
ff023aac 1623again:
3fb99303
DS
1624 if (!sctx->wr_curr_bio) {
1625 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
58c4e173 1626 GFP_KERNEL);
3fb99303
DS
1627 if (!sctx->wr_curr_bio) {
1628 mutex_unlock(&sctx->wr_lock);
ff023aac
SB
1629 return -ENOMEM;
1630 }
3fb99303
DS
1631 sctx->wr_curr_bio->sctx = sctx;
1632 sctx->wr_curr_bio->page_count = 0;
ff023aac 1633 }
3fb99303 1634 sbio = sctx->wr_curr_bio;
ff023aac
SB
1635 if (sbio->page_count == 0) {
1636 struct bio *bio;
1637
1638 sbio->physical = spage->physical_for_dev_replace;
1639 sbio->logical = spage->logical;
3fb99303 1640 sbio->dev = sctx->wr_tgtdev;
ff023aac
SB
1641 bio = sbio->bio;
1642 if (!bio) {
c5e4c3d7 1643 bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
ff023aac
SB
1644 sbio->bio = bio;
1645 }
1646
1647 bio->bi_private = sbio;
1648 bio->bi_end_io = scrub_wr_bio_end_io;
74d46992 1649 bio_set_dev(bio, sbio->dev->bdev);
4f024f37 1650 bio->bi_iter.bi_sector = sbio->physical >> 9;
ebcc3263 1651 bio->bi_opf = REQ_OP_WRITE;
4e4cbee9 1652 sbio->status = 0;
ff023aac
SB
1653 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1654 spage->physical_for_dev_replace ||
1655 sbio->logical + sbio->page_count * PAGE_SIZE !=
1656 spage->logical) {
1657 scrub_wr_submit(sctx);
1658 goto again;
1659 }
1660
1661 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1662 if (ret != PAGE_SIZE) {
1663 if (sbio->page_count < 1) {
1664 bio_put(sbio->bio);
1665 sbio->bio = NULL;
3fb99303 1666 mutex_unlock(&sctx->wr_lock);
ff023aac
SB
1667 return -EIO;
1668 }
1669 scrub_wr_submit(sctx);
1670 goto again;
1671 }
1672
1673 sbio->pagev[sbio->page_count] = spage;
1674 scrub_page_get(spage);
1675 sbio->page_count++;
3fb99303 1676 if (sbio->page_count == sctx->pages_per_wr_bio)
ff023aac 1677 scrub_wr_submit(sctx);
3fb99303 1678 mutex_unlock(&sctx->wr_lock);
ff023aac
SB
1679
1680 return 0;
1681}
1682
1683static void scrub_wr_submit(struct scrub_ctx *sctx)
1684{
ff023aac
SB
1685 struct scrub_bio *sbio;
1686
3fb99303 1687 if (!sctx->wr_curr_bio)
ff023aac
SB
1688 return;
1689
3fb99303
DS
1690 sbio = sctx->wr_curr_bio;
1691 sctx->wr_curr_bio = NULL;
74d46992 1692 WARN_ON(!sbio->bio->bi_disk);
ff023aac
SB
1693 scrub_pending_bio_inc(sctx);
1694 /* process all writes in a single worker thread. Then the block layer
1695 * orders the requests before sending them to the driver which
1696 * doubled the write performance on spinning disks when measured
1697 * with Linux 3.5 */
4e49ea4a 1698 btrfsic_submit_bio(sbio->bio);
ff023aac
SB
1699}
1700
4246a0b6 1701static void scrub_wr_bio_end_io(struct bio *bio)
ff023aac
SB
1702{
1703 struct scrub_bio *sbio = bio->bi_private;
fb456252 1704 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
ff023aac 1705
4e4cbee9 1706 sbio->status = bio->bi_status;
ff023aac
SB
1707 sbio->bio = bio;
1708
9e0af237
LB
1709 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1710 scrub_wr_bio_end_io_worker, NULL, NULL);
0339ef2f 1711 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
ff023aac
SB
1712}
1713
1714static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1715{
1716 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1717 struct scrub_ctx *sctx = sbio->sctx;
1718 int i;
1719
1720 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
4e4cbee9 1721 if (sbio->status) {
ff023aac 1722 struct btrfs_dev_replace *dev_replace =
fb456252 1723 &sbio->sctx->fs_info->dev_replace;
ff023aac
SB
1724
1725 for (i = 0; i < sbio->page_count; i++) {
1726 struct scrub_page *spage = sbio->pagev[i];
1727
1728 spage->io_error = 1;
1729 btrfs_dev_replace_stats_inc(&dev_replace->
1730 num_write_errors);
1731 }
1732 }
1733
1734 for (i = 0; i < sbio->page_count; i++)
1735 scrub_page_put(sbio->pagev[i]);
1736
1737 bio_put(sbio->bio);
1738 kfree(sbio);
1739 scrub_pending_bio_dec(sctx);
1740}
1741
1742static int scrub_checksum(struct scrub_block *sblock)
b5d67f64
SB
1743{
1744 u64 flags;
1745 int ret;
1746
ba7cf988
ZL
1747 /*
1748 * No need to initialize these stats currently,
1749 * because this function only use return value
1750 * instead of these stats value.
1751 *
1752 * Todo:
1753 * always use stats
1754 */
1755 sblock->header_error = 0;
1756 sblock->generation_error = 0;
1757 sblock->checksum_error = 0;
1758
7a9e9987
SB
1759 WARN_ON(sblock->page_count < 1);
1760 flags = sblock->pagev[0]->flags;
b5d67f64
SB
1761 ret = 0;
1762 if (flags & BTRFS_EXTENT_FLAG_DATA)
1763 ret = scrub_checksum_data(sblock);
1764 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1765 ret = scrub_checksum_tree_block(sblock);
1766 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1767 (void)scrub_checksum_super(sblock);
1768 else
1769 WARN_ON(1);
1770 if (ret)
1771 scrub_handle_errored_block(sblock);
ff023aac
SB
1772
1773 return ret;
a2de733c
AJ
1774}
1775
b5d67f64 1776static int scrub_checksum_data(struct scrub_block *sblock)
a2de733c 1777{
d9d181c1 1778 struct scrub_ctx *sctx = sblock->sctx;
a2de733c 1779 u8 csum[BTRFS_CSUM_SIZE];
b5d67f64
SB
1780 u8 *on_disk_csum;
1781 struct page *page;
1782 void *buffer;
a2de733c 1783 u32 crc = ~(u32)0;
b5d67f64
SB
1784 u64 len;
1785 int index;
a2de733c 1786
b5d67f64 1787 BUG_ON(sblock->page_count < 1);
7a9e9987 1788 if (!sblock->pagev[0]->have_csum)
a2de733c
AJ
1789 return 0;
1790
7a9e9987
SB
1791 on_disk_csum = sblock->pagev[0]->csum;
1792 page = sblock->pagev[0]->page;
9613bebb 1793 buffer = kmap_atomic(page);
b5d67f64 1794
25cc1226 1795 len = sctx->fs_info->sectorsize;
b5d67f64
SB
1796 index = 0;
1797 for (;;) {
1798 u64 l = min_t(u64, len, PAGE_SIZE);
1799
b0496686 1800 crc = btrfs_csum_data(buffer, crc, l);
9613bebb 1801 kunmap_atomic(buffer);
b5d67f64
SB
1802 len -= l;
1803 if (len == 0)
1804 break;
1805 index++;
1806 BUG_ON(index >= sblock->page_count);
7a9e9987
SB
1807 BUG_ON(!sblock->pagev[index]->page);
1808 page = sblock->pagev[index]->page;
9613bebb 1809 buffer = kmap_atomic(page);
b5d67f64
SB
1810 }
1811
a2de733c 1812 btrfs_csum_final(crc, csum);
d9d181c1 1813 if (memcmp(csum, on_disk_csum, sctx->csum_size))
ba7cf988 1814 sblock->checksum_error = 1;
a2de733c 1815
ba7cf988 1816 return sblock->checksum_error;
a2de733c
AJ
1817}
1818
b5d67f64 1819static int scrub_checksum_tree_block(struct scrub_block *sblock)
a2de733c 1820{
d9d181c1 1821 struct scrub_ctx *sctx = sblock->sctx;
a2de733c 1822 struct btrfs_header *h;
0b246afa 1823 struct btrfs_fs_info *fs_info = sctx->fs_info;
b5d67f64
SB
1824 u8 calculated_csum[BTRFS_CSUM_SIZE];
1825 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1826 struct page *page;
1827 void *mapped_buffer;
1828 u64 mapped_size;
1829 void *p;
a2de733c 1830 u32 crc = ~(u32)0;
b5d67f64
SB
1831 u64 len;
1832 int index;
1833
1834 BUG_ON(sblock->page_count < 1);
7a9e9987 1835 page = sblock->pagev[0]->page;
9613bebb 1836 mapped_buffer = kmap_atomic(page);
b5d67f64 1837 h = (struct btrfs_header *)mapped_buffer;
d9d181c1 1838 memcpy(on_disk_csum, h->csum, sctx->csum_size);
a2de733c
AJ
1839
1840 /*
1841 * we don't use the getter functions here, as we
1842 * a) don't have an extent buffer and
1843 * b) the page is already kmapped
1844 */
3cae210f 1845 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
ba7cf988 1846 sblock->header_error = 1;
a2de733c 1847
ba7cf988
ZL
1848 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
1849 sblock->header_error = 1;
1850 sblock->generation_error = 1;
1851 }
a2de733c 1852
17a9be2f 1853 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
ba7cf988 1854 sblock->header_error = 1;
a2de733c
AJ
1855
1856 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1857 BTRFS_UUID_SIZE))
ba7cf988 1858 sblock->header_error = 1;
a2de733c 1859
25cc1226 1860 len = sctx->fs_info->nodesize - BTRFS_CSUM_SIZE;
b5d67f64
SB
1861 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1862 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1863 index = 0;
1864 for (;;) {
1865 u64 l = min_t(u64, len, mapped_size);
1866
b0496686 1867 crc = btrfs_csum_data(p, crc, l);
9613bebb 1868 kunmap_atomic(mapped_buffer);
b5d67f64
SB
1869 len -= l;
1870 if (len == 0)
1871 break;
1872 index++;
1873 BUG_ON(index >= sblock->page_count);
7a9e9987
SB
1874 BUG_ON(!sblock->pagev[index]->page);
1875 page = sblock->pagev[index]->page;
9613bebb 1876 mapped_buffer = kmap_atomic(page);
b5d67f64
SB
1877 mapped_size = PAGE_SIZE;
1878 p = mapped_buffer;
1879 }
1880
1881 btrfs_csum_final(crc, calculated_csum);
d9d181c1 1882 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
ba7cf988 1883 sblock->checksum_error = 1;
a2de733c 1884
ba7cf988 1885 return sblock->header_error || sblock->checksum_error;
a2de733c
AJ
1886}
1887
b5d67f64 1888static int scrub_checksum_super(struct scrub_block *sblock)
a2de733c
AJ
1889{
1890 struct btrfs_super_block *s;
d9d181c1 1891 struct scrub_ctx *sctx = sblock->sctx;
b5d67f64
SB
1892 u8 calculated_csum[BTRFS_CSUM_SIZE];
1893 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1894 struct page *page;
1895 void *mapped_buffer;
1896 u64 mapped_size;
1897 void *p;
a2de733c 1898 u32 crc = ~(u32)0;
442a4f63
SB
1899 int fail_gen = 0;
1900 int fail_cor = 0;
b5d67f64
SB
1901 u64 len;
1902 int index;
a2de733c 1903
b5d67f64 1904 BUG_ON(sblock->page_count < 1);
7a9e9987 1905 page = sblock->pagev[0]->page;
9613bebb 1906 mapped_buffer = kmap_atomic(page);
b5d67f64 1907 s = (struct btrfs_super_block *)mapped_buffer;
d9d181c1 1908 memcpy(on_disk_csum, s->csum, sctx->csum_size);
a2de733c 1909
3cae210f 1910 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
442a4f63 1911 ++fail_cor;
a2de733c 1912
3cae210f 1913 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
442a4f63 1914 ++fail_gen;
a2de733c 1915
17a9be2f 1916 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
442a4f63 1917 ++fail_cor;
a2de733c 1918
b5d67f64
SB
1919 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1920 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1921 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1922 index = 0;
1923 for (;;) {
1924 u64 l = min_t(u64, len, mapped_size);
1925
b0496686 1926 crc = btrfs_csum_data(p, crc, l);
9613bebb 1927 kunmap_atomic(mapped_buffer);
b5d67f64
SB
1928 len -= l;
1929 if (len == 0)
1930 break;
1931 index++;
1932 BUG_ON(index >= sblock->page_count);
7a9e9987
SB
1933 BUG_ON(!sblock->pagev[index]->page);
1934 page = sblock->pagev[index]->page;
9613bebb 1935 mapped_buffer = kmap_atomic(page);
b5d67f64
SB
1936 mapped_size = PAGE_SIZE;
1937 p = mapped_buffer;
1938 }
1939
1940 btrfs_csum_final(crc, calculated_csum);
d9d181c1 1941 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
442a4f63 1942 ++fail_cor;
a2de733c 1943
442a4f63 1944 if (fail_cor + fail_gen) {
a2de733c
AJ
1945 /*
1946 * if we find an error in a super block, we just report it.
1947 * They will get written with the next transaction commit
1948 * anyway
1949 */
d9d181c1
SB
1950 spin_lock(&sctx->stat_lock);
1951 ++sctx->stat.super_errors;
1952 spin_unlock(&sctx->stat_lock);
442a4f63 1953 if (fail_cor)
7a9e9987 1954 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
442a4f63
SB
1955 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1956 else
7a9e9987 1957 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
442a4f63 1958 BTRFS_DEV_STAT_GENERATION_ERRS);
a2de733c
AJ
1959 }
1960
442a4f63 1961 return fail_cor + fail_gen;
a2de733c
AJ
1962}
1963
b5d67f64
SB
1964static void scrub_block_get(struct scrub_block *sblock)
1965{
186debd6 1966 refcount_inc(&sblock->refs);
b5d67f64
SB
1967}
1968
1969static void scrub_block_put(struct scrub_block *sblock)
1970{
186debd6 1971 if (refcount_dec_and_test(&sblock->refs)) {
b5d67f64
SB
1972 int i;
1973
5a6ac9ea
MX
1974 if (sblock->sparity)
1975 scrub_parity_put(sblock->sparity);
1976
b5d67f64 1977 for (i = 0; i < sblock->page_count; i++)
7a9e9987 1978 scrub_page_put(sblock->pagev[i]);
b5d67f64
SB
1979 kfree(sblock);
1980 }
1981}
1982
7a9e9987
SB
1983static void scrub_page_get(struct scrub_page *spage)
1984{
57019345 1985 atomic_inc(&spage->refs);
7a9e9987
SB
1986}
1987
1988static void scrub_page_put(struct scrub_page *spage)
1989{
57019345 1990 if (atomic_dec_and_test(&spage->refs)) {
7a9e9987
SB
1991 if (spage->page)
1992 __free_page(spage->page);
1993 kfree(spage);
1994 }
1995}
1996
d9d181c1 1997static void scrub_submit(struct scrub_ctx *sctx)
a2de733c
AJ
1998{
1999 struct scrub_bio *sbio;
2000
d9d181c1 2001 if (sctx->curr == -1)
1623edeb 2002 return;
a2de733c 2003
d9d181c1
SB
2004 sbio = sctx->bios[sctx->curr];
2005 sctx->curr = -1;
b6bfebc1 2006 scrub_pending_bio_inc(sctx);
4e49ea4a 2007 btrfsic_submit_bio(sbio->bio);
a2de733c
AJ
2008}
2009
ff023aac
SB
2010static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2011 struct scrub_page *spage)
a2de733c 2012{
b5d67f64 2013 struct scrub_block *sblock = spage->sblock;
a2de733c 2014 struct scrub_bio *sbio;
69f4cb52 2015 int ret;
a2de733c
AJ
2016
2017again:
2018 /*
2019 * grab a fresh bio or wait for one to become available
2020 */
d9d181c1
SB
2021 while (sctx->curr == -1) {
2022 spin_lock(&sctx->list_lock);
2023 sctx->curr = sctx->first_free;
2024 if (sctx->curr != -1) {
2025 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2026 sctx->bios[sctx->curr]->next_free = -1;
2027 sctx->bios[sctx->curr]->page_count = 0;
2028 spin_unlock(&sctx->list_lock);
a2de733c 2029 } else {
d9d181c1
SB
2030 spin_unlock(&sctx->list_lock);
2031 wait_event(sctx->list_wait, sctx->first_free != -1);
a2de733c
AJ
2032 }
2033 }
d9d181c1 2034 sbio = sctx->bios[sctx->curr];
b5d67f64 2035 if (sbio->page_count == 0) {
69f4cb52
AJ
2036 struct bio *bio;
2037
b5d67f64
SB
2038 sbio->physical = spage->physical;
2039 sbio->logical = spage->logical;
a36cf8b8 2040 sbio->dev = spage->dev;
b5d67f64
SB
2041 bio = sbio->bio;
2042 if (!bio) {
c5e4c3d7 2043 bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
b5d67f64
SB
2044 sbio->bio = bio;
2045 }
69f4cb52
AJ
2046
2047 bio->bi_private = sbio;
2048 bio->bi_end_io = scrub_bio_end_io;
74d46992 2049 bio_set_dev(bio, sbio->dev->bdev);
4f024f37 2050 bio->bi_iter.bi_sector = sbio->physical >> 9;
ebcc3263 2051 bio->bi_opf = REQ_OP_READ;
4e4cbee9 2052 sbio->status = 0;
b5d67f64
SB
2053 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2054 spage->physical ||
2055 sbio->logical + sbio->page_count * PAGE_SIZE !=
a36cf8b8
SB
2056 spage->logical ||
2057 sbio->dev != spage->dev) {
d9d181c1 2058 scrub_submit(sctx);
a2de733c
AJ
2059 goto again;
2060 }
69f4cb52 2061
b5d67f64
SB
2062 sbio->pagev[sbio->page_count] = spage;
2063 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2064 if (ret != PAGE_SIZE) {
2065 if (sbio->page_count < 1) {
2066 bio_put(sbio->bio);
2067 sbio->bio = NULL;
2068 return -EIO;
2069 }
d9d181c1 2070 scrub_submit(sctx);
69f4cb52
AJ
2071 goto again;
2072 }
2073
ff023aac 2074 scrub_block_get(sblock); /* one for the page added to the bio */
b5d67f64
SB
2075 atomic_inc(&sblock->outstanding_pages);
2076 sbio->page_count++;
ff023aac 2077 if (sbio->page_count == sctx->pages_per_rd_bio)
d9d181c1 2078 scrub_submit(sctx);
b5d67f64
SB
2079
2080 return 0;
2081}
2082
22365979 2083static void scrub_missing_raid56_end_io(struct bio *bio)
73ff61db
OS
2084{
2085 struct scrub_block *sblock = bio->bi_private;
fb456252 2086 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
73ff61db 2087
4e4cbee9 2088 if (bio->bi_status)
73ff61db
OS
2089 sblock->no_io_error_seen = 0;
2090
4673272f
ST
2091 bio_put(bio);
2092
73ff61db
OS
2093 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2094}
2095
2096static void scrub_missing_raid56_worker(struct btrfs_work *work)
2097{
2098 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2099 struct scrub_ctx *sctx = sblock->sctx;
0b246afa 2100 struct btrfs_fs_info *fs_info = sctx->fs_info;
73ff61db
OS
2101 u64 logical;
2102 struct btrfs_device *dev;
2103
73ff61db
OS
2104 logical = sblock->pagev[0]->logical;
2105 dev = sblock->pagev[0]->dev;
2106
affe4a5a 2107 if (sblock->no_io_error_seen)
ba7cf988 2108 scrub_recheck_block_checksum(sblock);
73ff61db
OS
2109
2110 if (!sblock->no_io_error_seen) {
2111 spin_lock(&sctx->stat_lock);
2112 sctx->stat.read_errors++;
2113 spin_unlock(&sctx->stat_lock);
0b246afa 2114 btrfs_err_rl_in_rcu(fs_info,
b14af3b4 2115 "IO error rebuilding logical %llu for dev %s",
73ff61db
OS
2116 logical, rcu_str_deref(dev->name));
2117 } else if (sblock->header_error || sblock->checksum_error) {
2118 spin_lock(&sctx->stat_lock);
2119 sctx->stat.uncorrectable_errors++;
2120 spin_unlock(&sctx->stat_lock);
0b246afa 2121 btrfs_err_rl_in_rcu(fs_info,
b14af3b4 2122 "failed to rebuild valid logical %llu for dev %s",
73ff61db
OS
2123 logical, rcu_str_deref(dev->name));
2124 } else {
2125 scrub_write_block_to_dev_replace(sblock);
2126 }
2127
2128 scrub_block_put(sblock);
2129
2073c4c2 2130 if (sctx->is_dev_replace && sctx->flush_all_writes) {
3fb99303 2131 mutex_lock(&sctx->wr_lock);
73ff61db 2132 scrub_wr_submit(sctx);
3fb99303 2133 mutex_unlock(&sctx->wr_lock);
73ff61db
OS
2134 }
2135
2136 scrub_pending_bio_dec(sctx);
2137}
2138
2139static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2140{
2141 struct scrub_ctx *sctx = sblock->sctx;
fb456252 2142 struct btrfs_fs_info *fs_info = sctx->fs_info;
73ff61db
OS
2143 u64 length = sblock->page_count * PAGE_SIZE;
2144 u64 logical = sblock->pagev[0]->logical;
f1fee653 2145 struct btrfs_bio *bbio = NULL;
73ff61db
OS
2146 struct bio *bio;
2147 struct btrfs_raid_bio *rbio;
2148 int ret;
2149 int i;
2150
ae6529c3 2151 btrfs_bio_counter_inc_blocked(fs_info);
cf8cddd3 2152 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
825ad4c9 2153 &length, &bbio);
73ff61db
OS
2154 if (ret || !bbio || !bbio->raid_map)
2155 goto bbio_out;
2156
2157 if (WARN_ON(!sctx->is_dev_replace ||
2158 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2159 /*
2160 * We shouldn't be scrubbing a missing device. Even for dev
2161 * replace, we should only get here for RAID 5/6. We either
2162 * managed to mount something with no mirrors remaining or
2163 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2164 */
2165 goto bbio_out;
2166 }
2167
c5e4c3d7 2168 bio = btrfs_io_bio_alloc(0);
73ff61db
OS
2169 bio->bi_iter.bi_sector = logical >> 9;
2170 bio->bi_private = sblock;
2171 bio->bi_end_io = scrub_missing_raid56_end_io;
2172
2ff7e61e 2173 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
73ff61db
OS
2174 if (!rbio)
2175 goto rbio_out;
2176
2177 for (i = 0; i < sblock->page_count; i++) {
2178 struct scrub_page *spage = sblock->pagev[i];
2179
2180 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2181 }
2182
2183 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2184 scrub_missing_raid56_worker, NULL, NULL);
2185 scrub_block_get(sblock);
2186 scrub_pending_bio_inc(sctx);
2187 raid56_submit_missing_rbio(rbio);
2188 return;
2189
2190rbio_out:
2191 bio_put(bio);
2192bbio_out:
ae6529c3 2193 btrfs_bio_counter_dec(fs_info);
73ff61db
OS
2194 btrfs_put_bbio(bbio);
2195 spin_lock(&sctx->stat_lock);
2196 sctx->stat.malloc_errors++;
2197 spin_unlock(&sctx->stat_lock);
2198}
2199
d9d181c1 2200static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
a36cf8b8 2201 u64 physical, struct btrfs_device *dev, u64 flags,
ff023aac
SB
2202 u64 gen, int mirror_num, u8 *csum, int force,
2203 u64 physical_for_dev_replace)
b5d67f64
SB
2204{
2205 struct scrub_block *sblock;
2206 int index;
2207
58c4e173 2208 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
b5d67f64 2209 if (!sblock) {
d9d181c1
SB
2210 spin_lock(&sctx->stat_lock);
2211 sctx->stat.malloc_errors++;
2212 spin_unlock(&sctx->stat_lock);
b5d67f64 2213 return -ENOMEM;
a2de733c 2214 }
b5d67f64 2215
7a9e9987
SB
2216 /* one ref inside this function, plus one for each page added to
2217 * a bio later on */
186debd6 2218 refcount_set(&sblock->refs, 1);
d9d181c1 2219 sblock->sctx = sctx;
b5d67f64
SB
2220 sblock->no_io_error_seen = 1;
2221
2222 for (index = 0; len > 0; index++) {
7a9e9987 2223 struct scrub_page *spage;
b5d67f64
SB
2224 u64 l = min_t(u64, len, PAGE_SIZE);
2225
58c4e173 2226 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
7a9e9987
SB
2227 if (!spage) {
2228leave_nomem:
d9d181c1
SB
2229 spin_lock(&sctx->stat_lock);
2230 sctx->stat.malloc_errors++;
2231 spin_unlock(&sctx->stat_lock);
7a9e9987 2232 scrub_block_put(sblock);
b5d67f64
SB
2233 return -ENOMEM;
2234 }
7a9e9987
SB
2235 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2236 scrub_page_get(spage);
2237 sblock->pagev[index] = spage;
b5d67f64 2238 spage->sblock = sblock;
a36cf8b8 2239 spage->dev = dev;
b5d67f64
SB
2240 spage->flags = flags;
2241 spage->generation = gen;
2242 spage->logical = logical;
2243 spage->physical = physical;
ff023aac 2244 spage->physical_for_dev_replace = physical_for_dev_replace;
b5d67f64
SB
2245 spage->mirror_num = mirror_num;
2246 if (csum) {
2247 spage->have_csum = 1;
d9d181c1 2248 memcpy(spage->csum, csum, sctx->csum_size);
b5d67f64
SB
2249 } else {
2250 spage->have_csum = 0;
2251 }
2252 sblock->page_count++;
58c4e173 2253 spage->page = alloc_page(GFP_KERNEL);
7a9e9987
SB
2254 if (!spage->page)
2255 goto leave_nomem;
b5d67f64
SB
2256 len -= l;
2257 logical += l;
2258 physical += l;
ff023aac 2259 physical_for_dev_replace += l;
b5d67f64
SB
2260 }
2261
7a9e9987 2262 WARN_ON(sblock->page_count == 0);
e6e674bd 2263 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
73ff61db
OS
2264 /*
2265 * This case should only be hit for RAID 5/6 device replace. See
2266 * the comment in scrub_missing_raid56_pages() for details.
2267 */
2268 scrub_missing_raid56_pages(sblock);
2269 } else {
2270 for (index = 0; index < sblock->page_count; index++) {
2271 struct scrub_page *spage = sblock->pagev[index];
2272 int ret;
1bc87793 2273
73ff61db
OS
2274 ret = scrub_add_page_to_rd_bio(sctx, spage);
2275 if (ret) {
2276 scrub_block_put(sblock);
2277 return ret;
2278 }
b5d67f64 2279 }
a2de733c 2280
73ff61db
OS
2281 if (force)
2282 scrub_submit(sctx);
2283 }
a2de733c 2284
b5d67f64
SB
2285 /* last one frees, either here or in bio completion for last page */
2286 scrub_block_put(sblock);
a2de733c
AJ
2287 return 0;
2288}
2289
4246a0b6 2290static void scrub_bio_end_io(struct bio *bio)
b5d67f64
SB
2291{
2292 struct scrub_bio *sbio = bio->bi_private;
fb456252 2293 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
b5d67f64 2294
4e4cbee9 2295 sbio->status = bio->bi_status;
b5d67f64
SB
2296 sbio->bio = bio;
2297
0339ef2f 2298 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
b5d67f64
SB
2299}
2300
2301static void scrub_bio_end_io_worker(struct btrfs_work *work)
2302{
2303 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
d9d181c1 2304 struct scrub_ctx *sctx = sbio->sctx;
b5d67f64
SB
2305 int i;
2306
ff023aac 2307 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
4e4cbee9 2308 if (sbio->status) {
b5d67f64
SB
2309 for (i = 0; i < sbio->page_count; i++) {
2310 struct scrub_page *spage = sbio->pagev[i];
2311
2312 spage->io_error = 1;
2313 spage->sblock->no_io_error_seen = 0;
2314 }
2315 }
2316
2317 /* now complete the scrub_block items that have all pages completed */
2318 for (i = 0; i < sbio->page_count; i++) {
2319 struct scrub_page *spage = sbio->pagev[i];
2320 struct scrub_block *sblock = spage->sblock;
2321
2322 if (atomic_dec_and_test(&sblock->outstanding_pages))
2323 scrub_block_complete(sblock);
2324 scrub_block_put(sblock);
2325 }
2326
b5d67f64
SB
2327 bio_put(sbio->bio);
2328 sbio->bio = NULL;
d9d181c1
SB
2329 spin_lock(&sctx->list_lock);
2330 sbio->next_free = sctx->first_free;
2331 sctx->first_free = sbio->index;
2332 spin_unlock(&sctx->list_lock);
ff023aac 2333
2073c4c2 2334 if (sctx->is_dev_replace && sctx->flush_all_writes) {
3fb99303 2335 mutex_lock(&sctx->wr_lock);
ff023aac 2336 scrub_wr_submit(sctx);
3fb99303 2337 mutex_unlock(&sctx->wr_lock);
ff023aac
SB
2338 }
2339
b6bfebc1 2340 scrub_pending_bio_dec(sctx);
b5d67f64
SB
2341}
2342
5a6ac9ea
MX
2343static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2344 unsigned long *bitmap,
2345 u64 start, u64 len)
2346{
972d7219 2347 u64 offset;
7736b0a4
DS
2348 u64 nsectors64;
2349 u32 nsectors;
da17066c 2350 int sectorsize = sparity->sctx->fs_info->sectorsize;
5a6ac9ea
MX
2351
2352 if (len >= sparity->stripe_len) {
2353 bitmap_set(bitmap, 0, sparity->nsectors);
2354 return;
2355 }
2356
2357 start -= sparity->logic_start;
972d7219
LB
2358 start = div64_u64_rem(start, sparity->stripe_len, &offset);
2359 offset = div_u64(offset, sectorsize);
7736b0a4
DS
2360 nsectors64 = div_u64(len, sectorsize);
2361
2362 ASSERT(nsectors64 < UINT_MAX);
2363 nsectors = (u32)nsectors64;
5a6ac9ea
MX
2364
2365 if (offset + nsectors <= sparity->nsectors) {
2366 bitmap_set(bitmap, offset, nsectors);
2367 return;
2368 }
2369
2370 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2371 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2372}
2373
2374static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2375 u64 start, u64 len)
2376{
2377 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2378}
2379
2380static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2381 u64 start, u64 len)
2382{
2383 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2384}
2385
b5d67f64
SB
2386static void scrub_block_complete(struct scrub_block *sblock)
2387{
5a6ac9ea
MX
2388 int corrupted = 0;
2389
ff023aac 2390 if (!sblock->no_io_error_seen) {
5a6ac9ea 2391 corrupted = 1;
b5d67f64 2392 scrub_handle_errored_block(sblock);
ff023aac
SB
2393 } else {
2394 /*
2395 * if has checksum error, write via repair mechanism in
2396 * dev replace case, otherwise write here in dev replace
2397 * case.
2398 */
5a6ac9ea
MX
2399 corrupted = scrub_checksum(sblock);
2400 if (!corrupted && sblock->sctx->is_dev_replace)
ff023aac
SB
2401 scrub_write_block_to_dev_replace(sblock);
2402 }
5a6ac9ea
MX
2403
2404 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2405 u64 start = sblock->pagev[0]->logical;
2406 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2407 PAGE_SIZE;
2408
2409 scrub_parity_mark_sectors_error(sblock->sparity,
2410 start, end - start);
2411 }
b5d67f64
SB
2412}
2413
3b5753ec 2414static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
a2de733c
AJ
2415{
2416 struct btrfs_ordered_sum *sum = NULL;
f51a4a18 2417 unsigned long index;
a2de733c 2418 unsigned long num_sectors;
a2de733c 2419
d9d181c1
SB
2420 while (!list_empty(&sctx->csum_list)) {
2421 sum = list_first_entry(&sctx->csum_list,
a2de733c
AJ
2422 struct btrfs_ordered_sum, list);
2423 if (sum->bytenr > logical)
2424 return 0;
2425 if (sum->bytenr + sum->len > logical)
2426 break;
2427
d9d181c1 2428 ++sctx->stat.csum_discards;
a2de733c
AJ
2429 list_del(&sum->list);
2430 kfree(sum);
2431 sum = NULL;
2432 }
2433 if (!sum)
2434 return 0;
2435
1d1bf92d
DS
2436 index = div_u64(logical - sum->bytenr, sctx->fs_info->sectorsize);
2437 ASSERT(index < UINT_MAX);
2438
25cc1226 2439 num_sectors = sum->len / sctx->fs_info->sectorsize;
f51a4a18
MX
2440 memcpy(csum, sum->sums + index, sctx->csum_size);
2441 if (index == num_sectors - 1) {
a2de733c
AJ
2442 list_del(&sum->list);
2443 kfree(sum);
2444 }
f51a4a18 2445 return 1;
a2de733c
AJ
2446}
2447
2448/* scrub extent tries to collect up to 64 kB for each bio */
6ca1765b
LB
2449static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map,
2450 u64 logical, u64 len,
a36cf8b8 2451 u64 physical, struct btrfs_device *dev, u64 flags,
ff023aac 2452 u64 gen, int mirror_num, u64 physical_for_dev_replace)
a2de733c
AJ
2453{
2454 int ret;
2455 u8 csum[BTRFS_CSUM_SIZE];
b5d67f64
SB
2456 u32 blocksize;
2457
2458 if (flags & BTRFS_EXTENT_FLAG_DATA) {
6ca1765b
LB
2459 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2460 blocksize = map->stripe_len;
2461 else
2462 blocksize = sctx->fs_info->sectorsize;
d9d181c1
SB
2463 spin_lock(&sctx->stat_lock);
2464 sctx->stat.data_extents_scrubbed++;
2465 sctx->stat.data_bytes_scrubbed += len;
2466 spin_unlock(&sctx->stat_lock);
b5d67f64 2467 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
6ca1765b
LB
2468 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2469 blocksize = map->stripe_len;
2470 else
2471 blocksize = sctx->fs_info->nodesize;
d9d181c1
SB
2472 spin_lock(&sctx->stat_lock);
2473 sctx->stat.tree_extents_scrubbed++;
2474 sctx->stat.tree_bytes_scrubbed += len;
2475 spin_unlock(&sctx->stat_lock);
b5d67f64 2476 } else {
25cc1226 2477 blocksize = sctx->fs_info->sectorsize;
ff023aac 2478 WARN_ON(1);
b5d67f64 2479 }
a2de733c
AJ
2480
2481 while (len) {
b5d67f64 2482 u64 l = min_t(u64, len, blocksize);
a2de733c
AJ
2483 int have_csum = 0;
2484
2485 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2486 /* push csums to sbio */
3b5753ec 2487 have_csum = scrub_find_csum(sctx, logical, csum);
a2de733c 2488 if (have_csum == 0)
d9d181c1 2489 ++sctx->stat.no_csum;
a2de733c 2490 }
a36cf8b8 2491 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
ff023aac
SB
2492 mirror_num, have_csum ? csum : NULL, 0,
2493 physical_for_dev_replace);
a2de733c
AJ
2494 if (ret)
2495 return ret;
2496 len -= l;
2497 logical += l;
2498 physical += l;
ff023aac 2499 physical_for_dev_replace += l;
a2de733c
AJ
2500 }
2501 return 0;
2502}
2503
5a6ac9ea
MX
2504static int scrub_pages_for_parity(struct scrub_parity *sparity,
2505 u64 logical, u64 len,
2506 u64 physical, struct btrfs_device *dev,
2507 u64 flags, u64 gen, int mirror_num, u8 *csum)
2508{
2509 struct scrub_ctx *sctx = sparity->sctx;
2510 struct scrub_block *sblock;
2511 int index;
2512
58c4e173 2513 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
5a6ac9ea
MX
2514 if (!sblock) {
2515 spin_lock(&sctx->stat_lock);
2516 sctx->stat.malloc_errors++;
2517 spin_unlock(&sctx->stat_lock);
2518 return -ENOMEM;
2519 }
2520
2521 /* one ref inside this function, plus one for each page added to
2522 * a bio later on */
186debd6 2523 refcount_set(&sblock->refs, 1);
5a6ac9ea
MX
2524 sblock->sctx = sctx;
2525 sblock->no_io_error_seen = 1;
2526 sblock->sparity = sparity;
2527 scrub_parity_get(sparity);
2528
2529 for (index = 0; len > 0; index++) {
2530 struct scrub_page *spage;
2531 u64 l = min_t(u64, len, PAGE_SIZE);
2532
58c4e173 2533 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
5a6ac9ea
MX
2534 if (!spage) {
2535leave_nomem:
2536 spin_lock(&sctx->stat_lock);
2537 sctx->stat.malloc_errors++;
2538 spin_unlock(&sctx->stat_lock);
2539 scrub_block_put(sblock);
2540 return -ENOMEM;
2541 }
2542 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2543 /* For scrub block */
2544 scrub_page_get(spage);
2545 sblock->pagev[index] = spage;
2546 /* For scrub parity */
2547 scrub_page_get(spage);
2548 list_add_tail(&spage->list, &sparity->spages);
2549 spage->sblock = sblock;
2550 spage->dev = dev;
2551 spage->flags = flags;
2552 spage->generation = gen;
2553 spage->logical = logical;
2554 spage->physical = physical;
2555 spage->mirror_num = mirror_num;
2556 if (csum) {
2557 spage->have_csum = 1;
2558 memcpy(spage->csum, csum, sctx->csum_size);
2559 } else {
2560 spage->have_csum = 0;
2561 }
2562 sblock->page_count++;
58c4e173 2563 spage->page = alloc_page(GFP_KERNEL);
5a6ac9ea
MX
2564 if (!spage->page)
2565 goto leave_nomem;
2566 len -= l;
2567 logical += l;
2568 physical += l;
2569 }
2570
2571 WARN_ON(sblock->page_count == 0);
2572 for (index = 0; index < sblock->page_count; index++) {
2573 struct scrub_page *spage = sblock->pagev[index];
2574 int ret;
2575
2576 ret = scrub_add_page_to_rd_bio(sctx, spage);
2577 if (ret) {
2578 scrub_block_put(sblock);
2579 return ret;
2580 }
2581 }
2582
2583 /* last one frees, either here or in bio completion for last page */
2584 scrub_block_put(sblock);
2585 return 0;
2586}
2587
2588static int scrub_extent_for_parity(struct scrub_parity *sparity,
2589 u64 logical, u64 len,
2590 u64 physical, struct btrfs_device *dev,
2591 u64 flags, u64 gen, int mirror_num)
2592{
2593 struct scrub_ctx *sctx = sparity->sctx;
2594 int ret;
2595 u8 csum[BTRFS_CSUM_SIZE];
2596 u32 blocksize;
2597
e6e674bd 2598 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
4a770891
OS
2599 scrub_parity_mark_sectors_error(sparity, logical, len);
2600 return 0;
2601 }
2602
5a6ac9ea 2603 if (flags & BTRFS_EXTENT_FLAG_DATA) {
6ca1765b 2604 blocksize = sparity->stripe_len;
5a6ac9ea 2605 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
6ca1765b 2606 blocksize = sparity->stripe_len;
5a6ac9ea 2607 } else {
25cc1226 2608 blocksize = sctx->fs_info->sectorsize;
5a6ac9ea
MX
2609 WARN_ON(1);
2610 }
2611
2612 while (len) {
2613 u64 l = min_t(u64, len, blocksize);
2614 int have_csum = 0;
2615
2616 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2617 /* push csums to sbio */
3b5753ec 2618 have_csum = scrub_find_csum(sctx, logical, csum);
5a6ac9ea
MX
2619 if (have_csum == 0)
2620 goto skip;
2621 }
2622 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2623 flags, gen, mirror_num,
2624 have_csum ? csum : NULL);
5a6ac9ea
MX
2625 if (ret)
2626 return ret;
6b6d24b3 2627skip:
5a6ac9ea
MX
2628 len -= l;
2629 logical += l;
2630 physical += l;
2631 }
2632 return 0;
2633}
2634
3b080b25
WS
2635/*
2636 * Given a physical address, this will calculate it's
2637 * logical offset. if this is a parity stripe, it will return
2638 * the most left data stripe's logical offset.
2639 *
2640 * return 0 if it is a data stripe, 1 means parity stripe.
2641 */
2642static int get_raid56_logic_offset(u64 physical, int num,
5a6ac9ea
MX
2643 struct map_lookup *map, u64 *offset,
2644 u64 *stripe_start)
3b080b25
WS
2645{
2646 int i;
2647 int j = 0;
2648 u64 stripe_nr;
2649 u64 last_offset;
9d644a62
DS
2650 u32 stripe_index;
2651 u32 rot;
3b080b25
WS
2652
2653 last_offset = (physical - map->stripes[num].physical) *
2654 nr_data_stripes(map);
5a6ac9ea
MX
2655 if (stripe_start)
2656 *stripe_start = last_offset;
2657
3b080b25
WS
2658 *offset = last_offset;
2659 for (i = 0; i < nr_data_stripes(map); i++) {
2660 *offset = last_offset + i * map->stripe_len;
2661
42c61ab6 2662 stripe_nr = div64_u64(*offset, map->stripe_len);
b8b93add 2663 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
3b080b25
WS
2664
2665 /* Work out the disk rotation on this stripe-set */
47c5713f 2666 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
3b080b25
WS
2667 /* calculate which stripe this data locates */
2668 rot += i;
e4fbaee2 2669 stripe_index = rot % map->num_stripes;
3b080b25
WS
2670 if (stripe_index == num)
2671 return 0;
2672 if (stripe_index < num)
2673 j++;
2674 }
2675 *offset = last_offset + j * map->stripe_len;
2676 return 1;
2677}
2678
5a6ac9ea
MX
2679static void scrub_free_parity(struct scrub_parity *sparity)
2680{
2681 struct scrub_ctx *sctx = sparity->sctx;
2682 struct scrub_page *curr, *next;
2683 int nbits;
2684
2685 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2686 if (nbits) {
2687 spin_lock(&sctx->stat_lock);
2688 sctx->stat.read_errors += nbits;
2689 sctx->stat.uncorrectable_errors += nbits;
2690 spin_unlock(&sctx->stat_lock);
2691 }
2692
2693 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2694 list_del_init(&curr->list);
2695 scrub_page_put(curr);
2696 }
2697
2698 kfree(sparity);
2699}
2700
20b2e302
ZL
2701static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2702{
2703 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2704 work);
2705 struct scrub_ctx *sctx = sparity->sctx;
2706
2707 scrub_free_parity(sparity);
2708 scrub_pending_bio_dec(sctx);
2709}
2710
4246a0b6 2711static void scrub_parity_bio_endio(struct bio *bio)
5a6ac9ea
MX
2712{
2713 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
0b246afa 2714 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
5a6ac9ea 2715
4e4cbee9 2716 if (bio->bi_status)
5a6ac9ea
MX
2717 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2718 sparity->nsectors);
2719
5a6ac9ea 2720 bio_put(bio);
20b2e302
ZL
2721
2722 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2723 scrub_parity_bio_endio_worker, NULL, NULL);
0b246afa 2724 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
5a6ac9ea
MX
2725}
2726
2727static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2728{
2729 struct scrub_ctx *sctx = sparity->sctx;
0b246afa 2730 struct btrfs_fs_info *fs_info = sctx->fs_info;
5a6ac9ea
MX
2731 struct bio *bio;
2732 struct btrfs_raid_bio *rbio;
5a6ac9ea 2733 struct btrfs_bio *bbio = NULL;
5a6ac9ea
MX
2734 u64 length;
2735 int ret;
2736
2737 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2738 sparity->nsectors))
2739 goto out;
2740
a0dd59de 2741 length = sparity->logic_end - sparity->logic_start;
ae6529c3
QW
2742
2743 btrfs_bio_counter_inc_blocked(fs_info);
0b246afa 2744 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
825ad4c9 2745 &length, &bbio);
8e5cfb55 2746 if (ret || !bbio || !bbio->raid_map)
5a6ac9ea
MX
2747 goto bbio_out;
2748
c5e4c3d7 2749 bio = btrfs_io_bio_alloc(0);
5a6ac9ea
MX
2750 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2751 bio->bi_private = sparity;
2752 bio->bi_end_io = scrub_parity_bio_endio;
2753
2ff7e61e 2754 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
8e5cfb55 2755 length, sparity->scrub_dev,
5a6ac9ea
MX
2756 sparity->dbitmap,
2757 sparity->nsectors);
2758 if (!rbio)
2759 goto rbio_out;
2760
5a6ac9ea
MX
2761 scrub_pending_bio_inc(sctx);
2762 raid56_parity_submit_scrub_rbio(rbio);
2763 return;
2764
2765rbio_out:
2766 bio_put(bio);
2767bbio_out:
ae6529c3 2768 btrfs_bio_counter_dec(fs_info);
6e9606d2 2769 btrfs_put_bbio(bbio);
5a6ac9ea
MX
2770 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2771 sparity->nsectors);
2772 spin_lock(&sctx->stat_lock);
2773 sctx->stat.malloc_errors++;
2774 spin_unlock(&sctx->stat_lock);
2775out:
2776 scrub_free_parity(sparity);
2777}
2778
2779static inline int scrub_calc_parity_bitmap_len(int nsectors)
2780{
bfca9a6d 2781 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
5a6ac9ea
MX
2782}
2783
2784static void scrub_parity_get(struct scrub_parity *sparity)
2785{
78a76450 2786 refcount_inc(&sparity->refs);
5a6ac9ea
MX
2787}
2788
2789static void scrub_parity_put(struct scrub_parity *sparity)
2790{
78a76450 2791 if (!refcount_dec_and_test(&sparity->refs))
5a6ac9ea
MX
2792 return;
2793
2794 scrub_parity_check_and_repair(sparity);
2795}
2796
2797static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2798 struct map_lookup *map,
2799 struct btrfs_device *sdev,
2800 struct btrfs_path *path,
2801 u64 logic_start,
2802 u64 logic_end)
2803{
fb456252 2804 struct btrfs_fs_info *fs_info = sctx->fs_info;
5a6ac9ea
MX
2805 struct btrfs_root *root = fs_info->extent_root;
2806 struct btrfs_root *csum_root = fs_info->csum_root;
2807 struct btrfs_extent_item *extent;
4a770891 2808 struct btrfs_bio *bbio = NULL;
5a6ac9ea
MX
2809 u64 flags;
2810 int ret;
2811 int slot;
2812 struct extent_buffer *l;
2813 struct btrfs_key key;
2814 u64 generation;
2815 u64 extent_logical;
2816 u64 extent_physical;
2817 u64 extent_len;
4a770891 2818 u64 mapped_length;
5a6ac9ea
MX
2819 struct btrfs_device *extent_dev;
2820 struct scrub_parity *sparity;
2821 int nsectors;
2822 int bitmap_len;
2823 int extent_mirror_num;
2824 int stop_loop = 0;
2825
0b246afa 2826 nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
5a6ac9ea
MX
2827 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2828 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2829 GFP_NOFS);
2830 if (!sparity) {
2831 spin_lock(&sctx->stat_lock);
2832 sctx->stat.malloc_errors++;
2833 spin_unlock(&sctx->stat_lock);
2834 return -ENOMEM;
2835 }
2836
2837 sparity->stripe_len = map->stripe_len;
2838 sparity->nsectors = nsectors;
2839 sparity->sctx = sctx;
2840 sparity->scrub_dev = sdev;
2841 sparity->logic_start = logic_start;
2842 sparity->logic_end = logic_end;
78a76450 2843 refcount_set(&sparity->refs, 1);
5a6ac9ea
MX
2844 INIT_LIST_HEAD(&sparity->spages);
2845 sparity->dbitmap = sparity->bitmap;
2846 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2847
2848 ret = 0;
2849 while (logic_start < logic_end) {
2850 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2851 key.type = BTRFS_METADATA_ITEM_KEY;
2852 else
2853 key.type = BTRFS_EXTENT_ITEM_KEY;
2854 key.objectid = logic_start;
2855 key.offset = (u64)-1;
2856
2857 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2858 if (ret < 0)
2859 goto out;
2860
2861 if (ret > 0) {
2862 ret = btrfs_previous_extent_item(root, path, 0);
2863 if (ret < 0)
2864 goto out;
2865 if (ret > 0) {
2866 btrfs_release_path(path);
2867 ret = btrfs_search_slot(NULL, root, &key,
2868 path, 0, 0);
2869 if (ret < 0)
2870 goto out;
2871 }
2872 }
2873
2874 stop_loop = 0;
2875 while (1) {
2876 u64 bytes;
2877
2878 l = path->nodes[0];
2879 slot = path->slots[0];
2880 if (slot >= btrfs_header_nritems(l)) {
2881 ret = btrfs_next_leaf(root, path);
2882 if (ret == 0)
2883 continue;
2884 if (ret < 0)
2885 goto out;
2886
2887 stop_loop = 1;
2888 break;
2889 }
2890 btrfs_item_key_to_cpu(l, &key, slot);
2891
d7cad238
ZL
2892 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2893 key.type != BTRFS_METADATA_ITEM_KEY)
2894 goto next;
2895
5a6ac9ea 2896 if (key.type == BTRFS_METADATA_ITEM_KEY)
0b246afa 2897 bytes = fs_info->nodesize;
5a6ac9ea
MX
2898 else
2899 bytes = key.offset;
2900
2901 if (key.objectid + bytes <= logic_start)
2902 goto next;
2903
a0dd59de 2904 if (key.objectid >= logic_end) {
5a6ac9ea
MX
2905 stop_loop = 1;
2906 break;
2907 }
2908
2909 while (key.objectid >= logic_start + map->stripe_len)
2910 logic_start += map->stripe_len;
2911
2912 extent = btrfs_item_ptr(l, slot,
2913 struct btrfs_extent_item);
2914 flags = btrfs_extent_flags(l, extent);
2915 generation = btrfs_extent_generation(l, extent);
2916
a323e813
ZL
2917 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
2918 (key.objectid < logic_start ||
2919 key.objectid + bytes >
2920 logic_start + map->stripe_len)) {
5d163e0e
JM
2921 btrfs_err(fs_info,
2922 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
a323e813 2923 key.objectid, logic_start);
9799d2c3
ZL
2924 spin_lock(&sctx->stat_lock);
2925 sctx->stat.uncorrectable_errors++;
2926 spin_unlock(&sctx->stat_lock);
5a6ac9ea
MX
2927 goto next;
2928 }
2929again:
2930 extent_logical = key.objectid;
2931 extent_len = bytes;
2932
2933 if (extent_logical < logic_start) {
2934 extent_len -= logic_start - extent_logical;
2935 extent_logical = logic_start;
2936 }
2937
2938 if (extent_logical + extent_len >
2939 logic_start + map->stripe_len)
2940 extent_len = logic_start + map->stripe_len -
2941 extent_logical;
2942
2943 scrub_parity_mark_sectors_data(sparity, extent_logical,
2944 extent_len);
2945
4a770891 2946 mapped_length = extent_len;
f1fee653 2947 bbio = NULL;
cf8cddd3
CH
2948 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
2949 extent_logical, &mapped_length, &bbio,
2950 0);
4a770891
OS
2951 if (!ret) {
2952 if (!bbio || mapped_length < extent_len)
2953 ret = -EIO;
2954 }
2955 if (ret) {
2956 btrfs_put_bbio(bbio);
2957 goto out;
2958 }
2959 extent_physical = bbio->stripes[0].physical;
2960 extent_mirror_num = bbio->mirror_num;
2961 extent_dev = bbio->stripes[0].dev;
2962 btrfs_put_bbio(bbio);
5a6ac9ea
MX
2963
2964 ret = btrfs_lookup_csums_range(csum_root,
2965 extent_logical,
2966 extent_logical + extent_len - 1,
2967 &sctx->csum_list, 1);
2968 if (ret)
2969 goto out;
2970
2971 ret = scrub_extent_for_parity(sparity, extent_logical,
2972 extent_len,
2973 extent_physical,
2974 extent_dev, flags,
2975 generation,
2976 extent_mirror_num);
6fa96d72
ZL
2977
2978 scrub_free_csums(sctx);
2979
5a6ac9ea
MX
2980 if (ret)
2981 goto out;
2982
5a6ac9ea
MX
2983 if (extent_logical + extent_len <
2984 key.objectid + bytes) {
2985 logic_start += map->stripe_len;
2986
2987 if (logic_start >= logic_end) {
2988 stop_loop = 1;
2989 break;
2990 }
2991
2992 if (logic_start < key.objectid + bytes) {
2993 cond_resched();
2994 goto again;
2995 }
2996 }
2997next:
2998 path->slots[0]++;
2999 }
3000
3001 btrfs_release_path(path);
3002
3003 if (stop_loop)
3004 break;
3005
3006 logic_start += map->stripe_len;
3007 }
3008out:
3009 if (ret < 0)
3010 scrub_parity_mark_sectors_error(sparity, logic_start,
a0dd59de 3011 logic_end - logic_start);
5a6ac9ea
MX
3012 scrub_parity_put(sparity);
3013 scrub_submit(sctx);
3fb99303 3014 mutex_lock(&sctx->wr_lock);
5a6ac9ea 3015 scrub_wr_submit(sctx);
3fb99303 3016 mutex_unlock(&sctx->wr_lock);
5a6ac9ea
MX
3017
3018 btrfs_release_path(path);
3019 return ret < 0 ? ret : 0;
3020}
3021
d9d181c1 3022static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
a36cf8b8
SB
3023 struct map_lookup *map,
3024 struct btrfs_device *scrub_dev,
32934280 3025 int num, u64 base, u64 length)
a2de733c 3026{
5a6ac9ea 3027 struct btrfs_path *path, *ppath;
fb456252 3028 struct btrfs_fs_info *fs_info = sctx->fs_info;
a2de733c
AJ
3029 struct btrfs_root *root = fs_info->extent_root;
3030 struct btrfs_root *csum_root = fs_info->csum_root;
3031 struct btrfs_extent_item *extent;
e7786c3a 3032 struct blk_plug plug;
a2de733c
AJ
3033 u64 flags;
3034 int ret;
3035 int slot;
a2de733c 3036 u64 nstripes;
a2de733c 3037 struct extent_buffer *l;
a2de733c
AJ
3038 u64 physical;
3039 u64 logical;
625f1c8d 3040 u64 logic_end;
3b080b25 3041 u64 physical_end;
a2de733c 3042 u64 generation;
e12fa9cd 3043 int mirror_num;
7a26285e
AJ
3044 struct reada_control *reada1;
3045 struct reada_control *reada2;
e6c11f9a 3046 struct btrfs_key key;
7a26285e 3047 struct btrfs_key key_end;
a2de733c
AJ
3048 u64 increment = map->stripe_len;
3049 u64 offset;
ff023aac
SB
3050 u64 extent_logical;
3051 u64 extent_physical;
3052 u64 extent_len;
5a6ac9ea
MX
3053 u64 stripe_logical;
3054 u64 stripe_end;
ff023aac
SB
3055 struct btrfs_device *extent_dev;
3056 int extent_mirror_num;
3b080b25 3057 int stop_loop = 0;
53b381b3 3058
3b080b25 3059 physical = map->stripes[num].physical;
a2de733c 3060 offset = 0;
42c61ab6 3061 nstripes = div64_u64(length, map->stripe_len);
a2de733c
AJ
3062 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3063 offset = map->stripe_len * num;
3064 increment = map->stripe_len * map->num_stripes;
193ea74b 3065 mirror_num = 1;
a2de733c
AJ
3066 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3067 int factor = map->num_stripes / map->sub_stripes;
3068 offset = map->stripe_len * (num / map->sub_stripes);
3069 increment = map->stripe_len * factor;
193ea74b 3070 mirror_num = num % map->sub_stripes + 1;
a2de733c
AJ
3071 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3072 increment = map->stripe_len;
193ea74b 3073 mirror_num = num % map->num_stripes + 1;
a2de733c
AJ
3074 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3075 increment = map->stripe_len;
193ea74b 3076 mirror_num = num % map->num_stripes + 1;
ffe2d203 3077 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5a6ac9ea 3078 get_raid56_logic_offset(physical, num, map, &offset, NULL);
3b080b25
WS
3079 increment = map->stripe_len * nr_data_stripes(map);
3080 mirror_num = 1;
a2de733c
AJ
3081 } else {
3082 increment = map->stripe_len;
193ea74b 3083 mirror_num = 1;
a2de733c
AJ
3084 }
3085
3086 path = btrfs_alloc_path();
3087 if (!path)
3088 return -ENOMEM;
3089
5a6ac9ea
MX
3090 ppath = btrfs_alloc_path();
3091 if (!ppath) {
379d6854 3092 btrfs_free_path(path);
5a6ac9ea
MX
3093 return -ENOMEM;
3094 }
3095
b5d67f64
SB
3096 /*
3097 * work on commit root. The related disk blocks are static as
3098 * long as COW is applied. This means, it is save to rewrite
3099 * them to repair disk errors without any race conditions
3100 */
a2de733c
AJ
3101 path->search_commit_root = 1;
3102 path->skip_locking = 1;
3103
063c54dc
GH
3104 ppath->search_commit_root = 1;
3105 ppath->skip_locking = 1;
a2de733c 3106 /*
7a26285e
AJ
3107 * trigger the readahead for extent tree csum tree and wait for
3108 * completion. During readahead, the scrub is officially paused
3109 * to not hold off transaction commits
a2de733c
AJ
3110 */
3111 logical = base + offset;
3b080b25 3112 physical_end = physical + nstripes * map->stripe_len;
ffe2d203 3113 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3b080b25 3114 get_raid56_logic_offset(physical_end, num,
5a6ac9ea 3115 map, &logic_end, NULL);
3b080b25
WS
3116 logic_end += base;
3117 } else {
3118 logic_end = logical + increment * nstripes;
3119 }
d9d181c1 3120 wait_event(sctx->list_wait,
b6bfebc1 3121 atomic_read(&sctx->bios_in_flight) == 0);
cb7ab021 3122 scrub_blocked_if_needed(fs_info);
7a26285e
AJ
3123
3124 /* FIXME it might be better to start readahead at commit root */
e6c11f9a
DS
3125 key.objectid = logical;
3126 key.type = BTRFS_EXTENT_ITEM_KEY;
3127 key.offset = (u64)0;
3b080b25 3128 key_end.objectid = logic_end;
3173a18f
JB
3129 key_end.type = BTRFS_METADATA_ITEM_KEY;
3130 key_end.offset = (u64)-1;
e6c11f9a 3131 reada1 = btrfs_reada_add(root, &key, &key_end);
7a26285e 3132
e6c11f9a
DS
3133 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3134 key.type = BTRFS_EXTENT_CSUM_KEY;
3135 key.offset = logical;
7a26285e
AJ
3136 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3137 key_end.type = BTRFS_EXTENT_CSUM_KEY;
3b080b25 3138 key_end.offset = logic_end;
e6c11f9a 3139 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
7a26285e
AJ
3140
3141 if (!IS_ERR(reada1))
3142 btrfs_reada_wait(reada1);
3143 if (!IS_ERR(reada2))
3144 btrfs_reada_wait(reada2);
3145
a2de733c
AJ
3146
3147 /*
3148 * collect all data csums for the stripe to avoid seeking during
3149 * the scrub. This might currently (crc32) end up to be about 1MB
3150 */
e7786c3a 3151 blk_start_plug(&plug);
a2de733c 3152
a2de733c
AJ
3153 /*
3154 * now find all extents for each stripe and scrub them
3155 */
a2de733c 3156 ret = 0;
3b080b25 3157 while (physical < physical_end) {
a2de733c
AJ
3158 /*
3159 * canceled?
3160 */
3161 if (atomic_read(&fs_info->scrub_cancel_req) ||
d9d181c1 3162 atomic_read(&sctx->cancel_req)) {
a2de733c
AJ
3163 ret = -ECANCELED;
3164 goto out;
3165 }
3166 /*
3167 * check to see if we have to pause
3168 */
3169 if (atomic_read(&fs_info->scrub_pause_req)) {
3170 /* push queued extents */
2073c4c2 3171 sctx->flush_all_writes = true;
d9d181c1 3172 scrub_submit(sctx);
3fb99303 3173 mutex_lock(&sctx->wr_lock);
ff023aac 3174 scrub_wr_submit(sctx);
3fb99303 3175 mutex_unlock(&sctx->wr_lock);
d9d181c1 3176 wait_event(sctx->list_wait,
b6bfebc1 3177 atomic_read(&sctx->bios_in_flight) == 0);
2073c4c2 3178 sctx->flush_all_writes = false;
3cb0929a 3179 scrub_blocked_if_needed(fs_info);
a2de733c
AJ
3180 }
3181
f2f66a2f
ZL
3182 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3183 ret = get_raid56_logic_offset(physical, num, map,
3184 &logical,
3185 &stripe_logical);
3186 logical += base;
3187 if (ret) {
7955323b 3188 /* it is parity strip */
f2f66a2f 3189 stripe_logical += base;
a0dd59de 3190 stripe_end = stripe_logical + increment;
f2f66a2f
ZL
3191 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3192 ppath, stripe_logical,
3193 stripe_end);
3194 if (ret)
3195 goto out;
3196 goto skip;
3197 }
3198 }
3199
7c76edb7
WS
3200 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3201 key.type = BTRFS_METADATA_ITEM_KEY;
3202 else
3203 key.type = BTRFS_EXTENT_ITEM_KEY;
a2de733c 3204 key.objectid = logical;
625f1c8d 3205 key.offset = (u64)-1;
a2de733c
AJ
3206
3207 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3208 if (ret < 0)
3209 goto out;
3173a18f 3210
8c51032f 3211 if (ret > 0) {
ade2e0b3 3212 ret = btrfs_previous_extent_item(root, path, 0);
a2de733c
AJ
3213 if (ret < 0)
3214 goto out;
8c51032f
AJ
3215 if (ret > 0) {
3216 /* there's no smaller item, so stick with the
3217 * larger one */
3218 btrfs_release_path(path);
3219 ret = btrfs_search_slot(NULL, root, &key,
3220 path, 0, 0);
3221 if (ret < 0)
3222 goto out;
3223 }
a2de733c
AJ
3224 }
3225
625f1c8d 3226 stop_loop = 0;
a2de733c 3227 while (1) {
3173a18f
JB
3228 u64 bytes;
3229
a2de733c
AJ
3230 l = path->nodes[0];
3231 slot = path->slots[0];
3232 if (slot >= btrfs_header_nritems(l)) {
3233 ret = btrfs_next_leaf(root, path);
3234 if (ret == 0)
3235 continue;
3236 if (ret < 0)
3237 goto out;
3238
625f1c8d 3239 stop_loop = 1;
a2de733c
AJ
3240 break;
3241 }
3242 btrfs_item_key_to_cpu(l, &key, slot);
3243
d7cad238
ZL
3244 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3245 key.type != BTRFS_METADATA_ITEM_KEY)
3246 goto next;
3247
3173a18f 3248 if (key.type == BTRFS_METADATA_ITEM_KEY)
0b246afa 3249 bytes = fs_info->nodesize;
3173a18f
JB
3250 else
3251 bytes = key.offset;
3252
3253 if (key.objectid + bytes <= logical)
a2de733c
AJ
3254 goto next;
3255
625f1c8d
LB
3256 if (key.objectid >= logical + map->stripe_len) {
3257 /* out of this device extent */
3258 if (key.objectid >= logic_end)
3259 stop_loop = 1;
3260 break;
3261 }
a2de733c
AJ
3262
3263 extent = btrfs_item_ptr(l, slot,
3264 struct btrfs_extent_item);
3265 flags = btrfs_extent_flags(l, extent);
3266 generation = btrfs_extent_generation(l, extent);
3267
a323e813
ZL
3268 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3269 (key.objectid < logical ||
3270 key.objectid + bytes >
3271 logical + map->stripe_len)) {
efe120a0 3272 btrfs_err(fs_info,
5d163e0e 3273 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
c1c9ff7c 3274 key.objectid, logical);
9799d2c3
ZL
3275 spin_lock(&sctx->stat_lock);
3276 sctx->stat.uncorrectable_errors++;
3277 spin_unlock(&sctx->stat_lock);
a2de733c
AJ
3278 goto next;
3279 }
3280
625f1c8d
LB
3281again:
3282 extent_logical = key.objectid;
3283 extent_len = bytes;
3284
a2de733c
AJ
3285 /*
3286 * trim extent to this stripe
3287 */
625f1c8d
LB
3288 if (extent_logical < logical) {
3289 extent_len -= logical - extent_logical;
3290 extent_logical = logical;
a2de733c 3291 }
625f1c8d 3292 if (extent_logical + extent_len >
a2de733c 3293 logical + map->stripe_len) {
625f1c8d
LB
3294 extent_len = logical + map->stripe_len -
3295 extent_logical;
a2de733c
AJ
3296 }
3297
625f1c8d 3298 extent_physical = extent_logical - logical + physical;
ff023aac
SB
3299 extent_dev = scrub_dev;
3300 extent_mirror_num = mirror_num;
32934280 3301 if (sctx->is_dev_replace)
ff023aac
SB
3302 scrub_remap_extent(fs_info, extent_logical,
3303 extent_len, &extent_physical,
3304 &extent_dev,
3305 &extent_mirror_num);
625f1c8d 3306
fe8cf654
ZL
3307 ret = btrfs_lookup_csums_range(csum_root,
3308 extent_logical,
3309 extent_logical +
3310 extent_len - 1,
3311 &sctx->csum_list, 1);
625f1c8d
LB
3312 if (ret)
3313 goto out;
3314
6ca1765b 3315 ret = scrub_extent(sctx, map, extent_logical, extent_len,
ff023aac
SB
3316 extent_physical, extent_dev, flags,
3317 generation, extent_mirror_num,
115930cb 3318 extent_logical - logical + physical);
6fa96d72
ZL
3319
3320 scrub_free_csums(sctx);
3321
a2de733c
AJ
3322 if (ret)
3323 goto out;
3324
625f1c8d
LB
3325 if (extent_logical + extent_len <
3326 key.objectid + bytes) {
ffe2d203 3327 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3b080b25
WS
3328 /*
3329 * loop until we find next data stripe
3330 * or we have finished all stripes.
3331 */
5a6ac9ea
MX
3332loop:
3333 physical += map->stripe_len;
3334 ret = get_raid56_logic_offset(physical,
3335 num, map, &logical,
3336 &stripe_logical);
3337 logical += base;
3338
3339 if (ret && physical < physical_end) {
3340 stripe_logical += base;
3341 stripe_end = stripe_logical +
a0dd59de 3342 increment;
5a6ac9ea
MX
3343 ret = scrub_raid56_parity(sctx,
3344 map, scrub_dev, ppath,
3345 stripe_logical,
3346 stripe_end);
3347 if (ret)
3348 goto out;
3349 goto loop;
3350 }
3b080b25
WS
3351 } else {
3352 physical += map->stripe_len;
3353 logical += increment;
3354 }
625f1c8d
LB
3355 if (logical < key.objectid + bytes) {
3356 cond_resched();
3357 goto again;
3358 }
3359
3b080b25 3360 if (physical >= physical_end) {
625f1c8d
LB
3361 stop_loop = 1;
3362 break;
3363 }
3364 }
a2de733c
AJ
3365next:
3366 path->slots[0]++;
3367 }
71267333 3368 btrfs_release_path(path);
3b080b25 3369skip:
a2de733c
AJ
3370 logical += increment;
3371 physical += map->stripe_len;
d9d181c1 3372 spin_lock(&sctx->stat_lock);
625f1c8d
LB
3373 if (stop_loop)
3374 sctx->stat.last_physical = map->stripes[num].physical +
3375 length;
3376 else
3377 sctx->stat.last_physical = physical;
d9d181c1 3378 spin_unlock(&sctx->stat_lock);
625f1c8d
LB
3379 if (stop_loop)
3380 break;
a2de733c 3381 }
ff023aac 3382out:
a2de733c 3383 /* push queued extents */
d9d181c1 3384 scrub_submit(sctx);
3fb99303 3385 mutex_lock(&sctx->wr_lock);
ff023aac 3386 scrub_wr_submit(sctx);
3fb99303 3387 mutex_unlock(&sctx->wr_lock);
a2de733c 3388
e7786c3a 3389 blk_finish_plug(&plug);
a2de733c 3390 btrfs_free_path(path);
5a6ac9ea 3391 btrfs_free_path(ppath);
a2de733c
AJ
3392 return ret < 0 ? ret : 0;
3393}
3394
d9d181c1 3395static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
a36cf8b8 3396 struct btrfs_device *scrub_dev,
a36cf8b8 3397 u64 chunk_offset, u64 length,
020d5b73 3398 u64 dev_offset,
32934280 3399 struct btrfs_block_group_cache *cache)
a2de733c 3400{
fb456252
JM
3401 struct btrfs_fs_info *fs_info = sctx->fs_info;
3402 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
a2de733c
AJ
3403 struct map_lookup *map;
3404 struct extent_map *em;
3405 int i;
ff023aac 3406 int ret = 0;
a2de733c
AJ
3407
3408 read_lock(&map_tree->map_tree.lock);
3409 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3410 read_unlock(&map_tree->map_tree.lock);
3411
020d5b73
FM
3412 if (!em) {
3413 /*
3414 * Might have been an unused block group deleted by the cleaner
3415 * kthread or relocation.
3416 */
3417 spin_lock(&cache->lock);
3418 if (!cache->removed)
3419 ret = -EINVAL;
3420 spin_unlock(&cache->lock);
3421
3422 return ret;
3423 }
a2de733c 3424
95617d69 3425 map = em->map_lookup;
a2de733c
AJ
3426 if (em->start != chunk_offset)
3427 goto out;
3428
3429 if (em->len < length)
3430 goto out;
3431
3432 for (i = 0; i < map->num_stripes; ++i) {
a36cf8b8 3433 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
859acaf1 3434 map->stripes[i].physical == dev_offset) {
a36cf8b8 3435 ret = scrub_stripe(sctx, map, scrub_dev, i,
32934280 3436 chunk_offset, length);
a2de733c
AJ
3437 if (ret)
3438 goto out;
3439 }
3440 }
3441out:
3442 free_extent_map(em);
3443
3444 return ret;
3445}
3446
3447static noinline_for_stack
a36cf8b8 3448int scrub_enumerate_chunks(struct scrub_ctx *sctx,
32934280 3449 struct btrfs_device *scrub_dev, u64 start, u64 end)
a2de733c
AJ
3450{
3451 struct btrfs_dev_extent *dev_extent = NULL;
3452 struct btrfs_path *path;
0b246afa
JM
3453 struct btrfs_fs_info *fs_info = sctx->fs_info;
3454 struct btrfs_root *root = fs_info->dev_root;
a2de733c 3455 u64 length;
a2de733c 3456 u64 chunk_offset;
55e3a601 3457 int ret = 0;
76a8efa1 3458 int ro_set;
a2de733c
AJ
3459 int slot;
3460 struct extent_buffer *l;
3461 struct btrfs_key key;
3462 struct btrfs_key found_key;
3463 struct btrfs_block_group_cache *cache;
ff023aac 3464 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
a2de733c
AJ
3465
3466 path = btrfs_alloc_path();
3467 if (!path)
3468 return -ENOMEM;
3469
e4058b54 3470 path->reada = READA_FORWARD;
a2de733c
AJ
3471 path->search_commit_root = 1;
3472 path->skip_locking = 1;
3473
a36cf8b8 3474 key.objectid = scrub_dev->devid;
a2de733c
AJ
3475 key.offset = 0ull;
3476 key.type = BTRFS_DEV_EXTENT_KEY;
3477
a2de733c
AJ
3478 while (1) {
3479 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3480 if (ret < 0)
8c51032f
AJ
3481 break;
3482 if (ret > 0) {
3483 if (path->slots[0] >=
3484 btrfs_header_nritems(path->nodes[0])) {
3485 ret = btrfs_next_leaf(root, path);
55e3a601
Z
3486 if (ret < 0)
3487 break;
3488 if (ret > 0) {
3489 ret = 0;
8c51032f 3490 break;
55e3a601
Z
3491 }
3492 } else {
3493 ret = 0;
8c51032f
AJ
3494 }
3495 }
a2de733c
AJ
3496
3497 l = path->nodes[0];
3498 slot = path->slots[0];
3499
3500 btrfs_item_key_to_cpu(l, &found_key, slot);
3501
a36cf8b8 3502 if (found_key.objectid != scrub_dev->devid)
a2de733c
AJ
3503 break;
3504
962a298f 3505 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
a2de733c
AJ
3506 break;
3507
3508 if (found_key.offset >= end)
3509 break;
3510
3511 if (found_key.offset < key.offset)
3512 break;
3513
3514 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3515 length = btrfs_dev_extent_length(l, dev_extent);
3516
ced96edc
QW
3517 if (found_key.offset + length <= start)
3518 goto skip;
a2de733c 3519
a2de733c
AJ
3520 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3521
3522 /*
3523 * get a reference on the corresponding block group to prevent
3524 * the chunk from going away while we scrub it
3525 */
3526 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
ced96edc
QW
3527
3528 /* some chunks are removed but not committed to disk yet,
3529 * continue scrubbing */
3530 if (!cache)
3531 goto skip;
3532
55e3a601
Z
3533 /*
3534 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3535 * to avoid deadlock caused by:
3536 * btrfs_inc_block_group_ro()
3537 * -> btrfs_wait_for_commit()
3538 * -> btrfs_commit_transaction()
3539 * -> btrfs_scrub_pause()
3540 */
3541 scrub_pause_on(fs_info);
c83488af 3542 ret = btrfs_inc_block_group_ro(cache);
32934280 3543 if (!ret && sctx->is_dev_replace) {
f0e9b7d6
FM
3544 /*
3545 * If we are doing a device replace wait for any tasks
3546 * that started dellaloc right before we set the block
3547 * group to RO mode, as they might have just allocated
3548 * an extent from it or decided they could do a nocow
3549 * write. And if any such tasks did that, wait for their
3550 * ordered extents to complete and then commit the
3551 * current transaction, so that we can later see the new
3552 * extent items in the extent tree - the ordered extents
3553 * create delayed data references (for cow writes) when
3554 * they complete, which will be run and insert the
3555 * corresponding extent items into the extent tree when
3556 * we commit the transaction they used when running
3557 * inode.c:btrfs_finish_ordered_io(). We later use
3558 * the commit root of the extent tree to find extents
3559 * to copy from the srcdev into the tgtdev, and we don't
3560 * want to miss any new extents.
3561 */
3562 btrfs_wait_block_group_reservations(cache);
3563 btrfs_wait_nocow_writers(cache);
6374e57a 3564 ret = btrfs_wait_ordered_roots(fs_info, U64_MAX,
f0e9b7d6
FM
3565 cache->key.objectid,
3566 cache->key.offset);
3567 if (ret > 0) {
3568 struct btrfs_trans_handle *trans;
3569
3570 trans = btrfs_join_transaction(root);
3571 if (IS_ERR(trans))
3572 ret = PTR_ERR(trans);
3573 else
3a45bb20 3574 ret = btrfs_commit_transaction(trans);
f0e9b7d6
FM
3575 if (ret) {
3576 scrub_pause_off(fs_info);
3577 btrfs_put_block_group(cache);
3578 break;
3579 }
3580 }
3581 }
55e3a601 3582 scrub_pause_off(fs_info);
76a8efa1
Z
3583
3584 if (ret == 0) {
3585 ro_set = 1;
3586 } else if (ret == -ENOSPC) {
3587 /*
3588 * btrfs_inc_block_group_ro return -ENOSPC when it
3589 * failed in creating new chunk for metadata.
3590 * It is not a problem for scrub/replace, because
3591 * metadata are always cowed, and our scrub paused
3592 * commit_transactions.
3593 */
3594 ro_set = 0;
3595 } else {
5d163e0e 3596 btrfs_warn(fs_info,
913e1535 3597 "failed setting block group ro: %d", ret);
55e3a601
Z
3598 btrfs_put_block_group(cache);
3599 break;
3600 }
3601
7e79cb86 3602 btrfs_dev_replace_write_lock(&fs_info->dev_replace);
ff023aac
SB
3603 dev_replace->cursor_right = found_key.offset + length;
3604 dev_replace->cursor_left = found_key.offset;
3605 dev_replace->item_needs_writeback = 1;
7e79cb86 3606 btrfs_dev_replace_write_unlock(&fs_info->dev_replace);
8c204c96 3607 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
32934280 3608 found_key.offset, cache);
ff023aac
SB
3609
3610 /*
3611 * flush, submit all pending read and write bios, afterwards
3612 * wait for them.
3613 * Note that in the dev replace case, a read request causes
3614 * write requests that are submitted in the read completion
3615 * worker. Therefore in the current situation, it is required
3616 * that all write requests are flushed, so that all read and
3617 * write requests are really completed when bios_in_flight
3618 * changes to 0.
3619 */
2073c4c2 3620 sctx->flush_all_writes = true;
ff023aac 3621 scrub_submit(sctx);
3fb99303 3622 mutex_lock(&sctx->wr_lock);
ff023aac 3623 scrub_wr_submit(sctx);
3fb99303 3624 mutex_unlock(&sctx->wr_lock);
ff023aac
SB
3625
3626 wait_event(sctx->list_wait,
3627 atomic_read(&sctx->bios_in_flight) == 0);
b708ce96
Z
3628
3629 scrub_pause_on(fs_info);
12cf9372
WS
3630
3631 /*
3632 * must be called before we decrease @scrub_paused.
3633 * make sure we don't block transaction commit while
3634 * we are waiting pending workers finished.
3635 */
ff023aac
SB
3636 wait_event(sctx->list_wait,
3637 atomic_read(&sctx->workers_pending) == 0);
2073c4c2 3638 sctx->flush_all_writes = false;
12cf9372 3639
b708ce96 3640 scrub_pause_off(fs_info);
ff023aac 3641
7e79cb86 3642 btrfs_dev_replace_write_lock(&fs_info->dev_replace);
1a1a8b73
FM
3643 dev_replace->cursor_left = dev_replace->cursor_right;
3644 dev_replace->item_needs_writeback = 1;
7e79cb86 3645 btrfs_dev_replace_write_unlock(&fs_info->dev_replace);
1a1a8b73 3646
76a8efa1 3647 if (ro_set)
2ff7e61e 3648 btrfs_dec_block_group_ro(cache);
ff023aac 3649
758f2dfc
FM
3650 /*
3651 * We might have prevented the cleaner kthread from deleting
3652 * this block group if it was already unused because we raced
3653 * and set it to RO mode first. So add it back to the unused
3654 * list, otherwise it might not ever be deleted unless a manual
3655 * balance is triggered or it becomes used and unused again.
3656 */
3657 spin_lock(&cache->lock);
3658 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3659 btrfs_block_group_used(&cache->item) == 0) {
3660 spin_unlock(&cache->lock);
031f24da 3661 btrfs_mark_bg_unused(cache);
758f2dfc
FM
3662 } else {
3663 spin_unlock(&cache->lock);
3664 }
3665
a2de733c
AJ
3666 btrfs_put_block_group(cache);
3667 if (ret)
3668 break;
32934280 3669 if (sctx->is_dev_replace &&
af1be4f8 3670 atomic64_read(&dev_replace->num_write_errors) > 0) {
ff023aac
SB
3671 ret = -EIO;
3672 break;
3673 }
3674 if (sctx->stat.malloc_errors > 0) {
3675 ret = -ENOMEM;
3676 break;
3677 }
ced96edc 3678skip:
a2de733c 3679 key.offset = found_key.offset + length;
71267333 3680 btrfs_release_path(path);
a2de733c
AJ
3681 }
3682
a2de733c 3683 btrfs_free_path(path);
8c51032f 3684
55e3a601 3685 return ret;
a2de733c
AJ
3686}
3687
a36cf8b8
SB
3688static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3689 struct btrfs_device *scrub_dev)
a2de733c
AJ
3690{
3691 int i;
3692 u64 bytenr;
3693 u64 gen;
3694 int ret;
0b246afa 3695 struct btrfs_fs_info *fs_info = sctx->fs_info;
a2de733c 3696
0b246afa 3697 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
79787eaa
JM
3698 return -EIO;
3699
5f546063 3700 /* Seed devices of a new filesystem has their own generation. */
0b246afa 3701 if (scrub_dev->fs_devices != fs_info->fs_devices)
5f546063
MX
3702 gen = scrub_dev->generation;
3703 else
0b246afa 3704 gen = fs_info->last_trans_committed;
a2de733c
AJ
3705
3706 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3707 bytenr = btrfs_sb_offset(i);
935e5cc9
MX
3708 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3709 scrub_dev->commit_total_bytes)
a2de733c
AJ
3710 break;
3711
d9d181c1 3712 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
a36cf8b8 3713 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
ff023aac 3714 NULL, 1, bytenr);
a2de733c
AJ
3715 if (ret)
3716 return ret;
3717 }
b6bfebc1 3718 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
a2de733c
AJ
3719
3720 return 0;
3721}
3722
3723/*
3724 * get a reference count on fs_info->scrub_workers. start worker if necessary
3725 */
ff023aac
SB
3726static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3727 int is_dev_replace)
a2de733c 3728{
6f011058 3729 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
0339ef2f 3730 int max_active = fs_info->thread_pool_size;
a2de733c 3731
632dd772 3732 if (fs_info->scrub_workers_refcnt == 0) {
af1cbe0a
DS
3733 fs_info->scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub",
3734 flags, is_dev_replace ? 1 : max_active, 4);
e82afc52
ZL
3735 if (!fs_info->scrub_workers)
3736 goto fail_scrub_workers;
3737
0339ef2f 3738 fs_info->scrub_wr_completion_workers =
cb001095 3739 btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
0339ef2f 3740 max_active, 2);
e82afc52
ZL
3741 if (!fs_info->scrub_wr_completion_workers)
3742 goto fail_scrub_wr_completion_workers;
3743
20b2e302 3744 fs_info->scrub_parity_workers =
cb001095 3745 btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
20b2e302 3746 max_active, 2);
e82afc52
ZL
3747 if (!fs_info->scrub_parity_workers)
3748 goto fail_scrub_parity_workers;
632dd772 3749 }
a2de733c 3750 ++fs_info->scrub_workers_refcnt;
e82afc52
ZL
3751 return 0;
3752
3753fail_scrub_parity_workers:
e82afc52
ZL
3754 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3755fail_scrub_wr_completion_workers:
3756 btrfs_destroy_workqueue(fs_info->scrub_workers);
3757fail_scrub_workers:
3758 return -ENOMEM;
a2de733c
AJ
3759}
3760
aa1b8cd4 3761static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
a2de733c 3762{
ff023aac 3763 if (--fs_info->scrub_workers_refcnt == 0) {
0339ef2f
QW
3764 btrfs_destroy_workqueue(fs_info->scrub_workers);
3765 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
20b2e302 3766 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
ff023aac 3767 }
a2de733c 3768 WARN_ON(fs_info->scrub_workers_refcnt < 0);
a2de733c
AJ
3769}
3770
aa1b8cd4
SB
3771int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3772 u64 end, struct btrfs_scrub_progress *progress,
63a212ab 3773 int readonly, int is_dev_replace)
a2de733c 3774{
d9d181c1 3775 struct scrub_ctx *sctx;
a2de733c
AJ
3776 int ret;
3777 struct btrfs_device *dev;
3778
aa1b8cd4 3779 if (btrfs_fs_closing(fs_info))
a2de733c
AJ
3780 return -EINVAL;
3781
da17066c 3782 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
b5d67f64
SB
3783 /*
3784 * in this case scrub is unable to calculate the checksum
3785 * the way scrub is implemented. Do not handle this
3786 * situation at all because it won't ever happen.
3787 */
efe120a0
FH
3788 btrfs_err(fs_info,
3789 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
da17066c
JM
3790 fs_info->nodesize,
3791 BTRFS_STRIPE_LEN);
b5d67f64
SB
3792 return -EINVAL;
3793 }
3794
da17066c 3795 if (fs_info->sectorsize != PAGE_SIZE) {
b5d67f64 3796 /* not supported for data w/o checksums */
751bebbe 3797 btrfs_err_rl(fs_info,
5d163e0e 3798 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
da17066c 3799 fs_info->sectorsize, PAGE_SIZE);
a2de733c
AJ
3800 return -EINVAL;
3801 }
3802
da17066c 3803 if (fs_info->nodesize >
7a9e9987 3804 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
da17066c 3805 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
7a9e9987
SB
3806 /*
3807 * would exhaust the array bounds of pagev member in
3808 * struct scrub_block
3809 */
5d163e0e
JM
3810 btrfs_err(fs_info,
3811 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
da17066c 3812 fs_info->nodesize,
7a9e9987 3813 SCRUB_MAX_PAGES_PER_BLOCK,
da17066c 3814 fs_info->sectorsize,
7a9e9987
SB
3815 SCRUB_MAX_PAGES_PER_BLOCK);
3816 return -EINVAL;
3817 }
3818
a2de733c 3819
aa1b8cd4
SB
3820 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3821 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
e6e674bd
AJ
3822 if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
3823 !is_dev_replace)) {
aa1b8cd4 3824 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
a2de733c
AJ
3825 return -ENODEV;
3826 }
a2de733c 3827
ebbede42
AJ
3828 if (!is_dev_replace && !readonly &&
3829 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
5d68da3b 3830 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
672d5990
MT
3831 btrfs_err_in_rcu(fs_info, "scrub: device %s is not writable",
3832 rcu_str_deref(dev->name));
5d68da3b
MX
3833 return -EROFS;
3834 }
3835
3b7a016f 3836 mutex_lock(&fs_info->scrub_lock);
e12c9621 3837 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
401e29c1 3838 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
a2de733c 3839 mutex_unlock(&fs_info->scrub_lock);
aa1b8cd4 3840 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
aa1b8cd4 3841 return -EIO;
a2de733c
AJ
3842 }
3843
7e79cb86 3844 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
cadbc0a0 3845 if (dev->scrub_ctx ||
8dabb742
SB
3846 (!is_dev_replace &&
3847 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
7e79cb86 3848 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
a2de733c 3849 mutex_unlock(&fs_info->scrub_lock);
aa1b8cd4 3850 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
a2de733c
AJ
3851 return -EINPROGRESS;
3852 }
7e79cb86 3853 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
3b7a016f
WS
3854
3855 ret = scrub_workers_get(fs_info, is_dev_replace);
3856 if (ret) {
3857 mutex_unlock(&fs_info->scrub_lock);
3858 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3859 return ret;
3860 }
3861
63a212ab 3862 sctx = scrub_setup_ctx(dev, is_dev_replace);
d9d181c1 3863 if (IS_ERR(sctx)) {
a2de733c 3864 mutex_unlock(&fs_info->scrub_lock);
aa1b8cd4
SB
3865 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3866 scrub_workers_put(fs_info);
d9d181c1 3867 return PTR_ERR(sctx);
a2de733c 3868 }
d9d181c1 3869 sctx->readonly = readonly;
cadbc0a0 3870 dev->scrub_ctx = sctx;
3cb0929a 3871 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
a2de733c 3872
3cb0929a
WS
3873 /*
3874 * checking @scrub_pause_req here, we can avoid
3875 * race between committing transaction and scrubbing.
3876 */
cb7ab021 3877 __scrub_blocked_if_needed(fs_info);
a2de733c
AJ
3878 atomic_inc(&fs_info->scrubs_running);
3879 mutex_unlock(&fs_info->scrub_lock);
a2de733c 3880
ff023aac 3881 if (!is_dev_replace) {
9b011adf
WS
3882 /*
3883 * by holding device list mutex, we can
3884 * kick off writing super in log tree sync.
3885 */
3cb0929a 3886 mutex_lock(&fs_info->fs_devices->device_list_mutex);
ff023aac 3887 ret = scrub_supers(sctx, dev);
3cb0929a 3888 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
ff023aac 3889 }
a2de733c
AJ
3890
3891 if (!ret)
32934280 3892 ret = scrub_enumerate_chunks(sctx, dev, start, end);
a2de733c 3893
b6bfebc1 3894 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
a2de733c
AJ
3895 atomic_dec(&fs_info->scrubs_running);
3896 wake_up(&fs_info->scrub_pause_wait);
3897
b6bfebc1 3898 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
0ef8e451 3899
a2de733c 3900 if (progress)
d9d181c1 3901 memcpy(progress, &sctx->stat, sizeof(*progress));
a2de733c
AJ
3902
3903 mutex_lock(&fs_info->scrub_lock);
cadbc0a0 3904 dev->scrub_ctx = NULL;
3b7a016f 3905 scrub_workers_put(fs_info);
a2de733c
AJ
3906 mutex_unlock(&fs_info->scrub_lock);
3907
f55985f4 3908 scrub_put_ctx(sctx);
a2de733c
AJ
3909
3910 return ret;
3911}
3912
2ff7e61e 3913void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
a2de733c 3914{
a2de733c
AJ
3915 mutex_lock(&fs_info->scrub_lock);
3916 atomic_inc(&fs_info->scrub_pause_req);
3917 while (atomic_read(&fs_info->scrubs_paused) !=
3918 atomic_read(&fs_info->scrubs_running)) {
3919 mutex_unlock(&fs_info->scrub_lock);
3920 wait_event(fs_info->scrub_pause_wait,
3921 atomic_read(&fs_info->scrubs_paused) ==
3922 atomic_read(&fs_info->scrubs_running));
3923 mutex_lock(&fs_info->scrub_lock);
3924 }
3925 mutex_unlock(&fs_info->scrub_lock);
a2de733c
AJ
3926}
3927
2ff7e61e 3928void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
a2de733c 3929{
a2de733c
AJ
3930 atomic_dec(&fs_info->scrub_pause_req);
3931 wake_up(&fs_info->scrub_pause_wait);
a2de733c
AJ
3932}
3933
aa1b8cd4 3934int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
a2de733c 3935{
a2de733c
AJ
3936 mutex_lock(&fs_info->scrub_lock);
3937 if (!atomic_read(&fs_info->scrubs_running)) {
3938 mutex_unlock(&fs_info->scrub_lock);
3939 return -ENOTCONN;
3940 }
3941
3942 atomic_inc(&fs_info->scrub_cancel_req);
3943 while (atomic_read(&fs_info->scrubs_running)) {
3944 mutex_unlock(&fs_info->scrub_lock);
3945 wait_event(fs_info->scrub_pause_wait,
3946 atomic_read(&fs_info->scrubs_running) == 0);
3947 mutex_lock(&fs_info->scrub_lock);
3948 }
3949 atomic_dec(&fs_info->scrub_cancel_req);
3950 mutex_unlock(&fs_info->scrub_lock);
3951
3952 return 0;
3953}
3954
aa1b8cd4
SB
3955int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3956 struct btrfs_device *dev)
49b25e05 3957{
d9d181c1 3958 struct scrub_ctx *sctx;
a2de733c
AJ
3959
3960 mutex_lock(&fs_info->scrub_lock);
cadbc0a0 3961 sctx = dev->scrub_ctx;
d9d181c1 3962 if (!sctx) {
a2de733c
AJ
3963 mutex_unlock(&fs_info->scrub_lock);
3964 return -ENOTCONN;
3965 }
d9d181c1 3966 atomic_inc(&sctx->cancel_req);
cadbc0a0 3967 while (dev->scrub_ctx) {
a2de733c
AJ
3968 mutex_unlock(&fs_info->scrub_lock);
3969 wait_event(fs_info->scrub_pause_wait,
cadbc0a0 3970 dev->scrub_ctx == NULL);
a2de733c
AJ
3971 mutex_lock(&fs_info->scrub_lock);
3972 }
3973 mutex_unlock(&fs_info->scrub_lock);
3974
3975 return 0;
3976}
1623edeb 3977
2ff7e61e 3978int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
a2de733c
AJ
3979 struct btrfs_scrub_progress *progress)
3980{
3981 struct btrfs_device *dev;
d9d181c1 3982 struct scrub_ctx *sctx = NULL;
a2de733c 3983
0b246afa
JM
3984 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3985 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
a2de733c 3986 if (dev)
cadbc0a0 3987 sctx = dev->scrub_ctx;
d9d181c1
SB
3988 if (sctx)
3989 memcpy(progress, &sctx->stat, sizeof(*progress));
0b246afa 3990 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
a2de733c 3991
d9d181c1 3992 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
a2de733c 3993}
ff023aac
SB
3994
3995static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3996 u64 extent_logical, u64 extent_len,
3997 u64 *extent_physical,
3998 struct btrfs_device **extent_dev,
3999 int *extent_mirror_num)
4000{
4001 u64 mapped_length;
4002 struct btrfs_bio *bbio = NULL;
4003 int ret;
4004
4005 mapped_length = extent_len;
cf8cddd3 4006 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
ff023aac
SB
4007 &mapped_length, &bbio, 0);
4008 if (ret || !bbio || mapped_length < extent_len ||
4009 !bbio->stripes[0].dev->bdev) {
6e9606d2 4010 btrfs_put_bbio(bbio);
ff023aac
SB
4011 return;
4012 }
4013
4014 *extent_physical = bbio->stripes[0].physical;
4015 *extent_mirror_num = bbio->mirror_num;
4016 *extent_dev = bbio->stripes[0].dev;
6e9606d2 4017 btrfs_put_bbio(bbio);
ff023aac 4018}