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