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