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