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