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