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