]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/btrfs/scrub.c
Btrfs: make the scrub page array dynamically allocated
[mirror_ubuntu-zesty-kernel.git] / fs / btrfs / scrub.c
CommitLineData
a2de733c
AJ
1/*
2 * Copyright (C) 2011 STRATO. All rights reserved.
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"
21adbd5c 28#include "check-integrity.h"
606686ee 29#include "rcu-string.h"
a2de733c
AJ
30
31/*
32 * This is only the first step towards a full-features scrub. It reads all
33 * extent and super block and verifies the checksums. In case a bad checksum
34 * is found or the extent cannot be read, good data will be written back if
35 * any can be found.
36 *
37 * Future enhancements:
a2de733c
AJ
38 * - In case an unrepairable extent is encountered, track which files are
39 * affected and report them
a2de733c 40 * - track and record media errors, throw out bad devices
a2de733c 41 * - add a mode to also read unallocated space
a2de733c
AJ
42 */
43
b5d67f64 44struct scrub_block;
d9d181c1 45struct scrub_ctx;
a2de733c
AJ
46
47#define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
d9d181c1 48#define SCRUB_BIOS_PER_CTX 16 /* 1 MB per device in flight */
7a9e9987
SB
49
50/*
51 * the following value times PAGE_SIZE needs to be large enough to match the
52 * largest node/leaf/sector size that shall be supported.
53 * Values larger than BTRFS_STRIPE_LEN are not supported.
54 */
b5d67f64 55#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
a2de733c
AJ
56
57struct scrub_page {
b5d67f64
SB
58 struct scrub_block *sblock;
59 struct page *page;
442a4f63 60 struct btrfs_device *dev;
a2de733c
AJ
61 u64 flags; /* extent flags */
62 u64 generation;
b5d67f64
SB
63 u64 logical;
64 u64 physical;
7a9e9987 65 atomic_t ref_count;
b5d67f64
SB
66 struct {
67 unsigned int mirror_num:8;
68 unsigned int have_csum:1;
69 unsigned int io_error:1;
70 };
a2de733c
AJ
71 u8 csum[BTRFS_CSUM_SIZE];
72};
73
74struct scrub_bio {
75 int index;
d9d181c1 76 struct scrub_ctx *sctx;
a36cf8b8 77 struct btrfs_device *dev;
a2de733c
AJ
78 struct bio *bio;
79 int err;
80 u64 logical;
81 u64 physical;
b5d67f64
SB
82 struct scrub_page *pagev[SCRUB_PAGES_PER_BIO];
83 int page_count;
a2de733c
AJ
84 int next_free;
85 struct btrfs_work work;
86};
87
b5d67f64 88struct scrub_block {
7a9e9987 89 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
b5d67f64
SB
90 int page_count;
91 atomic_t outstanding_pages;
92 atomic_t ref_count; /* free mem on transition to zero */
d9d181c1 93 struct scrub_ctx *sctx;
b5d67f64
SB
94 struct {
95 unsigned int header_error:1;
96 unsigned int checksum_error:1;
97 unsigned int no_io_error_seen:1;
442a4f63 98 unsigned int generation_error:1; /* also sets header_error */
b5d67f64
SB
99 };
100};
101
d9d181c1
SB
102struct scrub_ctx {
103 struct scrub_bio *bios[SCRUB_BIOS_PER_CTX];
a36cf8b8 104 struct btrfs_root *dev_root;
a2de733c
AJ
105 int first_free;
106 int curr;
107 atomic_t in_flight;
0ef8e451 108 atomic_t fixup_cnt;
a2de733c
AJ
109 spinlock_t list_lock;
110 wait_queue_head_t list_wait;
111 u16 csum_size;
112 struct list_head csum_list;
113 atomic_t cancel_req;
8628764e 114 int readonly;
b5d67f64
SB
115 int pages_per_bio; /* <= SCRUB_PAGES_PER_BIO */
116 u32 sectorsize;
117 u32 nodesize;
118 u32 leafsize;
a2de733c
AJ
119 /*
120 * statistics
121 */
122 struct btrfs_scrub_progress stat;
123 spinlock_t stat_lock;
124};
125
0ef8e451 126struct scrub_fixup_nodatasum {
d9d181c1 127 struct scrub_ctx *sctx;
a36cf8b8 128 struct btrfs_device *dev;
0ef8e451
JS
129 u64 logical;
130 struct btrfs_root *root;
131 struct btrfs_work work;
132 int mirror_num;
133};
134
558540c1
JS
135struct scrub_warning {
136 struct btrfs_path *path;
137 u64 extent_item_size;
138 char *scratch_buf;
139 char *msg_buf;
140 const char *errstr;
141 sector_t sector;
142 u64 logical;
143 struct btrfs_device *dev;
144 int msg_bufsize;
145 int scratch_bufsize;
146};
147
b5d67f64
SB
148
149static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
d9d181c1 150static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
b5d67f64
SB
151 struct btrfs_mapping_tree *map_tree,
152 u64 length, u64 logical,
153 struct scrub_block *sblock);
154static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
155 struct scrub_block *sblock, int is_metadata,
156 int have_csum, u8 *csum, u64 generation,
157 u16 csum_size);
158static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
159 struct scrub_block *sblock,
160 int is_metadata, int have_csum,
161 const u8 *csum, u64 generation,
162 u16 csum_size);
163static void scrub_complete_bio_end_io(struct bio *bio, int err);
164static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
165 struct scrub_block *sblock_good,
166 int force_write);
167static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
168 struct scrub_block *sblock_good,
169 int page_num, int force_write);
170static int scrub_checksum_data(struct scrub_block *sblock);
171static int scrub_checksum_tree_block(struct scrub_block *sblock);
172static int scrub_checksum_super(struct scrub_block *sblock);
173static void scrub_block_get(struct scrub_block *sblock);
174static void scrub_block_put(struct scrub_block *sblock);
7a9e9987
SB
175static void scrub_page_get(struct scrub_page *spage);
176static void scrub_page_put(struct scrub_page *spage);
d9d181c1 177static int scrub_add_page_to_bio(struct scrub_ctx *sctx,
b5d67f64 178 struct scrub_page *spage);
d9d181c1 179static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
a36cf8b8
SB
180 u64 physical, struct btrfs_device *dev, u64 flags,
181 u64 gen, int mirror_num, u8 *csum, int force);
1623edeb 182static void scrub_bio_end_io(struct bio *bio, int err);
b5d67f64
SB
183static void scrub_bio_end_io_worker(struct btrfs_work *work);
184static void scrub_block_complete(struct scrub_block *sblock);
1623edeb
SB
185
186
d9d181c1 187static void scrub_free_csums(struct scrub_ctx *sctx)
a2de733c 188{
d9d181c1 189 while (!list_empty(&sctx->csum_list)) {
a2de733c 190 struct btrfs_ordered_sum *sum;
d9d181c1 191 sum = list_first_entry(&sctx->csum_list,
a2de733c
AJ
192 struct btrfs_ordered_sum, list);
193 list_del(&sum->list);
194 kfree(sum);
195 }
196}
197
d9d181c1 198static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
a2de733c
AJ
199{
200 int i;
a2de733c 201
d9d181c1 202 if (!sctx)
a2de733c
AJ
203 return;
204
b5d67f64 205 /* this can happen when scrub is cancelled */
d9d181c1
SB
206 if (sctx->curr != -1) {
207 struct scrub_bio *sbio = sctx->bios[sctx->curr];
b5d67f64
SB
208
209 for (i = 0; i < sbio->page_count; i++) {
210 BUG_ON(!sbio->pagev[i]);
211 BUG_ON(!sbio->pagev[i]->page);
212 scrub_block_put(sbio->pagev[i]->sblock);
213 }
214 bio_put(sbio->bio);
215 }
216
d9d181c1
SB
217 for (i = 0; i < SCRUB_BIOS_PER_CTX; ++i) {
218 struct scrub_bio *sbio = sctx->bios[i];
a2de733c
AJ
219
220 if (!sbio)
221 break;
a2de733c
AJ
222 kfree(sbio);
223 }
224
d9d181c1
SB
225 scrub_free_csums(sctx);
226 kfree(sctx);
a2de733c
AJ
227}
228
229static noinline_for_stack
d9d181c1 230struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev)
a2de733c 231{
d9d181c1 232 struct scrub_ctx *sctx;
a2de733c 233 int i;
a2de733c 234 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
b5d67f64 235 int pages_per_bio;
a2de733c 236
b5d67f64
SB
237 pages_per_bio = min_t(int, SCRUB_PAGES_PER_BIO,
238 bio_get_nr_vecs(dev->bdev));
d9d181c1
SB
239 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
240 if (!sctx)
a2de733c 241 goto nomem;
d9d181c1
SB
242 sctx->pages_per_bio = pages_per_bio;
243 sctx->curr = -1;
a36cf8b8 244 sctx->dev_root = dev->dev_root;
d9d181c1 245 for (i = 0; i < SCRUB_BIOS_PER_CTX; ++i) {
a2de733c
AJ
246 struct scrub_bio *sbio;
247
248 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
249 if (!sbio)
250 goto nomem;
d9d181c1 251 sctx->bios[i] = sbio;
a2de733c 252
a2de733c 253 sbio->index = i;
d9d181c1 254 sbio->sctx = sctx;
b5d67f64
SB
255 sbio->page_count = 0;
256 sbio->work.func = scrub_bio_end_io_worker;
a2de733c 257
d9d181c1
SB
258 if (i != SCRUB_BIOS_PER_CTX - 1)
259 sctx->bios[i]->next_free = i + 1;
0ef8e451 260 else
d9d181c1
SB
261 sctx->bios[i]->next_free = -1;
262 }
263 sctx->first_free = 0;
264 sctx->nodesize = dev->dev_root->nodesize;
265 sctx->leafsize = dev->dev_root->leafsize;
266 sctx->sectorsize = dev->dev_root->sectorsize;
267 atomic_set(&sctx->in_flight, 0);
268 atomic_set(&sctx->fixup_cnt, 0);
269 atomic_set(&sctx->cancel_req, 0);
270 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
271 INIT_LIST_HEAD(&sctx->csum_list);
272
273 spin_lock_init(&sctx->list_lock);
274 spin_lock_init(&sctx->stat_lock);
275 init_waitqueue_head(&sctx->list_wait);
276 return sctx;
a2de733c
AJ
277
278nomem:
d9d181c1 279 scrub_free_ctx(sctx);
a2de733c
AJ
280 return ERR_PTR(-ENOMEM);
281}
282
558540c1
JS
283static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, void *ctx)
284{
285 u64 isize;
286 u32 nlink;
287 int ret;
288 int i;
289 struct extent_buffer *eb;
290 struct btrfs_inode_item *inode_item;
291 struct scrub_warning *swarn = ctx;
292 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
293 struct inode_fs_paths *ipath = NULL;
294 struct btrfs_root *local_root;
295 struct btrfs_key root_key;
296
297 root_key.objectid = root;
298 root_key.type = BTRFS_ROOT_ITEM_KEY;
299 root_key.offset = (u64)-1;
300 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
301 if (IS_ERR(local_root)) {
302 ret = PTR_ERR(local_root);
303 goto err;
304 }
305
306 ret = inode_item_info(inum, 0, local_root, swarn->path);
307 if (ret) {
308 btrfs_release_path(swarn->path);
309 goto err;
310 }
311
312 eb = swarn->path->nodes[0];
313 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
314 struct btrfs_inode_item);
315 isize = btrfs_inode_size(eb, inode_item);
316 nlink = btrfs_inode_nlink(eb, inode_item);
317 btrfs_release_path(swarn->path);
318
319 ipath = init_ipath(4096, local_root, swarn->path);
26bdef54
DC
320 if (IS_ERR(ipath)) {
321 ret = PTR_ERR(ipath);
322 ipath = NULL;
323 goto err;
324 }
558540c1
JS
325 ret = paths_from_inode(inum, ipath);
326
327 if (ret < 0)
328 goto err;
329
330 /*
331 * we deliberately ignore the bit ipath might have been too small to
332 * hold all of the paths here
333 */
334 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
606686ee 335 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
558540c1
JS
336 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
337 "length %llu, links %u (path: %s)\n", swarn->errstr,
606686ee 338 swarn->logical, rcu_str_deref(swarn->dev->name),
558540c1
JS
339 (unsigned long long)swarn->sector, root, inum, offset,
340 min(isize - offset, (u64)PAGE_SIZE), nlink,
745c4d8e 341 (char *)(unsigned long)ipath->fspath->val[i]);
558540c1
JS
342
343 free_ipath(ipath);
344 return 0;
345
346err:
606686ee 347 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
558540c1
JS
348 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
349 "resolving failed with ret=%d\n", swarn->errstr,
606686ee 350 swarn->logical, rcu_str_deref(swarn->dev->name),
558540c1
JS
351 (unsigned long long)swarn->sector, root, inum, offset, ret);
352
353 free_ipath(ipath);
354 return 0;
355}
356
b5d67f64 357static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
558540c1 358{
a36cf8b8
SB
359 struct btrfs_device *dev;
360 struct btrfs_fs_info *fs_info;
558540c1
JS
361 struct btrfs_path *path;
362 struct btrfs_key found_key;
363 struct extent_buffer *eb;
364 struct btrfs_extent_item *ei;
365 struct scrub_warning swarn;
69917e43
LB
366 unsigned long ptr = 0;
367 u64 extent_item_pos;
368 u64 flags = 0;
558540c1 369 u64 ref_root;
69917e43 370 u32 item_size;
558540c1 371 u8 ref_level;
558540c1 372 const int bufsize = 4096;
69917e43 373 int ret;
558540c1 374
a36cf8b8 375 WARN_ON(sblock->page_count < 1);
7a9e9987 376 dev = sblock->pagev[0]->dev;
a36cf8b8
SB
377 fs_info = sblock->sctx->dev_root->fs_info;
378
558540c1
JS
379 path = btrfs_alloc_path();
380
381 swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
382 swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
7a9e9987
SB
383 swarn.sector = (sblock->pagev[0]->physical) >> 9;
384 swarn.logical = sblock->pagev[0]->logical;
558540c1 385 swarn.errstr = errstr;
a36cf8b8 386 swarn.dev = NULL;
558540c1
JS
387 swarn.msg_bufsize = bufsize;
388 swarn.scratch_bufsize = bufsize;
389
390 if (!path || !swarn.scratch_buf || !swarn.msg_buf)
391 goto out;
392
69917e43
LB
393 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
394 &flags);
558540c1
JS
395 if (ret < 0)
396 goto out;
397
4692cf58 398 extent_item_pos = swarn.logical - found_key.objectid;
558540c1
JS
399 swarn.extent_item_size = found_key.offset;
400
401 eb = path->nodes[0];
402 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
403 item_size = btrfs_item_size_nr(eb, path->slots[0]);
4692cf58 404 btrfs_release_path(path);
558540c1 405
69917e43 406 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
558540c1
JS
407 do {
408 ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
409 &ref_root, &ref_level);
606686ee 410 printk_in_rcu(KERN_WARNING
1623edeb 411 "btrfs: %s at logical %llu on dev %s, "
558540c1 412 "sector %llu: metadata %s (level %d) in tree "
606686ee
JB
413 "%llu\n", errstr, swarn.logical,
414 rcu_str_deref(dev->name),
558540c1
JS
415 (unsigned long long)swarn.sector,
416 ref_level ? "node" : "leaf",
417 ret < 0 ? -1 : ref_level,
418 ret < 0 ? -1 : ref_root);
419 } while (ret != 1);
420 } else {
421 swarn.path = path;
a36cf8b8 422 swarn.dev = dev;
7a3ae2f8
JS
423 iterate_extent_inodes(fs_info, found_key.objectid,
424 extent_item_pos, 1,
558540c1
JS
425 scrub_print_warning_inode, &swarn);
426 }
427
428out:
429 btrfs_free_path(path);
430 kfree(swarn.scratch_buf);
431 kfree(swarn.msg_buf);
432}
433
0ef8e451
JS
434static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *ctx)
435{
5da6fcbc 436 struct page *page = NULL;
0ef8e451
JS
437 unsigned long index;
438 struct scrub_fixup_nodatasum *fixup = ctx;
439 int ret;
5da6fcbc 440 int corrected = 0;
0ef8e451 441 struct btrfs_key key;
5da6fcbc 442 struct inode *inode = NULL;
0ef8e451
JS
443 u64 end = offset + PAGE_SIZE - 1;
444 struct btrfs_root *local_root;
445
446 key.objectid = root;
447 key.type = BTRFS_ROOT_ITEM_KEY;
448 key.offset = (u64)-1;
449 local_root = btrfs_read_fs_root_no_name(fixup->root->fs_info, &key);
450 if (IS_ERR(local_root))
451 return PTR_ERR(local_root);
452
453 key.type = BTRFS_INODE_ITEM_KEY;
454 key.objectid = inum;
455 key.offset = 0;
456 inode = btrfs_iget(fixup->root->fs_info->sb, &key, local_root, NULL);
457 if (IS_ERR(inode))
458 return PTR_ERR(inode);
459
0ef8e451
JS
460 index = offset >> PAGE_CACHE_SHIFT;
461
462 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
5da6fcbc
JS
463 if (!page) {
464 ret = -ENOMEM;
465 goto out;
466 }
467
468 if (PageUptodate(page)) {
469 struct btrfs_mapping_tree *map_tree;
470 if (PageDirty(page)) {
471 /*
472 * we need to write the data to the defect sector. the
473 * data that was in that sector is not in memory,
474 * because the page was modified. we must not write the
475 * modified page to that sector.
476 *
477 * TODO: what could be done here: wait for the delalloc
478 * runner to write out that page (might involve
479 * COW) and see whether the sector is still
480 * referenced afterwards.
481 *
482 * For the meantime, we'll treat this error
483 * incorrectable, although there is a chance that a
484 * later scrub will find the bad sector again and that
485 * there's no dirty page in memory, then.
486 */
487 ret = -EIO;
488 goto out;
489 }
490 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
491 ret = repair_io_failure(map_tree, offset, PAGE_SIZE,
492 fixup->logical, page,
493 fixup->mirror_num);
494 unlock_page(page);
495 corrected = !ret;
496 } else {
497 /*
498 * we need to get good data first. the general readpage path
499 * will call repair_io_failure for us, we just have to make
500 * sure we read the bad mirror.
501 */
502 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
503 EXTENT_DAMAGED, GFP_NOFS);
504 if (ret) {
505 /* set_extent_bits should give proper error */
506 WARN_ON(ret > 0);
507 if (ret > 0)
508 ret = -EFAULT;
509 goto out;
510 }
511
512 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
513 btrfs_get_extent,
514 fixup->mirror_num);
515 wait_on_page_locked(page);
516
517 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
518 end, EXTENT_DAMAGED, 0, NULL);
519 if (!corrected)
520 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
521 EXTENT_DAMAGED, GFP_NOFS);
522 }
523
524out:
525 if (page)
526 put_page(page);
527 if (inode)
528 iput(inode);
0ef8e451
JS
529
530 if (ret < 0)
531 return ret;
532
533 if (ret == 0 && corrected) {
534 /*
535 * we only need to call readpage for one of the inodes belonging
536 * to this extent. so make iterate_extent_inodes stop
537 */
538 return 1;
539 }
540
541 return -EIO;
542}
543
544static void scrub_fixup_nodatasum(struct btrfs_work *work)
545{
546 int ret;
547 struct scrub_fixup_nodatasum *fixup;
d9d181c1 548 struct scrub_ctx *sctx;
0ef8e451
JS
549 struct btrfs_trans_handle *trans = NULL;
550 struct btrfs_fs_info *fs_info;
551 struct btrfs_path *path;
552 int uncorrectable = 0;
553
554 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
d9d181c1 555 sctx = fixup->sctx;
0ef8e451
JS
556 fs_info = fixup->root->fs_info;
557
558 path = btrfs_alloc_path();
559 if (!path) {
d9d181c1
SB
560 spin_lock(&sctx->stat_lock);
561 ++sctx->stat.malloc_errors;
562 spin_unlock(&sctx->stat_lock);
0ef8e451
JS
563 uncorrectable = 1;
564 goto out;
565 }
566
567 trans = btrfs_join_transaction(fixup->root);
568 if (IS_ERR(trans)) {
569 uncorrectable = 1;
570 goto out;
571 }
572
573 /*
574 * the idea is to trigger a regular read through the standard path. we
575 * read a page from the (failed) logical address by specifying the
576 * corresponding copynum of the failed sector. thus, that readpage is
577 * expected to fail.
578 * that is the point where on-the-fly error correction will kick in
579 * (once it's finished) and rewrite the failed sector if a good copy
580 * can be found.
581 */
582 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
583 path, scrub_fixup_readpage,
584 fixup);
585 if (ret < 0) {
586 uncorrectable = 1;
587 goto out;
588 }
589 WARN_ON(ret != 1);
590
d9d181c1
SB
591 spin_lock(&sctx->stat_lock);
592 ++sctx->stat.corrected_errors;
593 spin_unlock(&sctx->stat_lock);
0ef8e451
JS
594
595out:
596 if (trans && !IS_ERR(trans))
597 btrfs_end_transaction(trans, fixup->root);
598 if (uncorrectable) {
d9d181c1
SB
599 spin_lock(&sctx->stat_lock);
600 ++sctx->stat.uncorrectable_errors;
601 spin_unlock(&sctx->stat_lock);
606686ee
JB
602
603 printk_ratelimited_in_rcu(KERN_ERR
b5d67f64 604 "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
606686ee 605 (unsigned long long)fixup->logical,
a36cf8b8 606 rcu_str_deref(fixup->dev->name));
0ef8e451
JS
607 }
608
609 btrfs_free_path(path);
610 kfree(fixup);
611
612 /* see caller why we're pretending to be paused in the scrub counters */
613 mutex_lock(&fs_info->scrub_lock);
614 atomic_dec(&fs_info->scrubs_running);
615 atomic_dec(&fs_info->scrubs_paused);
616 mutex_unlock(&fs_info->scrub_lock);
d9d181c1 617 atomic_dec(&sctx->fixup_cnt);
0ef8e451 618 wake_up(&fs_info->scrub_pause_wait);
d9d181c1 619 wake_up(&sctx->list_wait);
0ef8e451
JS
620}
621
a2de733c 622/*
b5d67f64
SB
623 * scrub_handle_errored_block gets called when either verification of the
624 * pages failed or the bio failed to read, e.g. with EIO. In the latter
625 * case, this function handles all pages in the bio, even though only one
626 * may be bad.
627 * The goal of this function is to repair the errored block by using the
628 * contents of one of the mirrors.
a2de733c 629 */
b5d67f64 630static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
a2de733c 631{
d9d181c1 632 struct scrub_ctx *sctx = sblock_to_check->sctx;
a36cf8b8 633 struct btrfs_device *dev;
b5d67f64
SB
634 struct btrfs_fs_info *fs_info;
635 u64 length;
636 u64 logical;
637 u64 generation;
638 unsigned int failed_mirror_index;
639 unsigned int is_metadata;
640 unsigned int have_csum;
641 u8 *csum;
642 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
643 struct scrub_block *sblock_bad;
644 int ret;
645 int mirror_index;
646 int page_num;
647 int success;
558540c1 648 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
b5d67f64
SB
649 DEFAULT_RATELIMIT_BURST);
650
651 BUG_ON(sblock_to_check->page_count < 1);
a36cf8b8 652 fs_info = sctx->dev_root->fs_info;
b5d67f64 653 length = sblock_to_check->page_count * PAGE_SIZE;
7a9e9987
SB
654 logical = sblock_to_check->pagev[0]->logical;
655 generation = sblock_to_check->pagev[0]->generation;
656 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
657 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
658 is_metadata = !(sblock_to_check->pagev[0]->flags &
b5d67f64 659 BTRFS_EXTENT_FLAG_DATA);
7a9e9987
SB
660 have_csum = sblock_to_check->pagev[0]->have_csum;
661 csum = sblock_to_check->pagev[0]->csum;
662 dev = sblock_to_check->pagev[0]->dev;
13db62b7 663
b5d67f64
SB
664 /*
665 * read all mirrors one after the other. This includes to
666 * re-read the extent or metadata block that failed (that was
667 * the cause that this fixup code is called) another time,
668 * page by page this time in order to know which pages
669 * caused I/O errors and which ones are good (for all mirrors).
670 * It is the goal to handle the situation when more than one
671 * mirror contains I/O errors, but the errors do not
672 * overlap, i.e. the data can be repaired by selecting the
673 * pages from those mirrors without I/O error on the
674 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
675 * would be that mirror #1 has an I/O error on the first page,
676 * the second page is good, and mirror #2 has an I/O error on
677 * the second page, but the first page is good.
678 * Then the first page of the first mirror can be repaired by
679 * taking the first page of the second mirror, and the
680 * second page of the second mirror can be repaired by
681 * copying the contents of the 2nd page of the 1st mirror.
682 * One more note: if the pages of one mirror contain I/O
683 * errors, the checksum cannot be verified. In order to get
684 * the best data for repairing, the first attempt is to find
685 * a mirror without I/O errors and with a validated checksum.
686 * Only if this is not possible, the pages are picked from
687 * mirrors with I/O errors without considering the checksum.
688 * If the latter is the case, at the end, the checksum of the
689 * repaired area is verified in order to correctly maintain
690 * the statistics.
691 */
692
693 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
694 sizeof(*sblocks_for_recheck),
695 GFP_NOFS);
696 if (!sblocks_for_recheck) {
d9d181c1
SB
697 spin_lock(&sctx->stat_lock);
698 sctx->stat.malloc_errors++;
699 sctx->stat.read_errors++;
700 sctx->stat.uncorrectable_errors++;
701 spin_unlock(&sctx->stat_lock);
a36cf8b8 702 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
b5d67f64 703 goto out;
a2de733c
AJ
704 }
705
b5d67f64 706 /* setup the context, map the logical blocks and alloc the pages */
d9d181c1 707 ret = scrub_setup_recheck_block(sctx, &fs_info->mapping_tree, length,
b5d67f64
SB
708 logical, sblocks_for_recheck);
709 if (ret) {
d9d181c1
SB
710 spin_lock(&sctx->stat_lock);
711 sctx->stat.read_errors++;
712 sctx->stat.uncorrectable_errors++;
713 spin_unlock(&sctx->stat_lock);
a36cf8b8 714 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
b5d67f64
SB
715 goto out;
716 }
717 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
718 sblock_bad = sblocks_for_recheck + failed_mirror_index;
13db62b7 719
b5d67f64
SB
720 /* build and submit the bios for the failed mirror, check checksums */
721 ret = scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
d9d181c1 722 csum, generation, sctx->csum_size);
b5d67f64 723 if (ret) {
d9d181c1
SB
724 spin_lock(&sctx->stat_lock);
725 sctx->stat.read_errors++;
726 sctx->stat.uncorrectable_errors++;
727 spin_unlock(&sctx->stat_lock);
a36cf8b8 728 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
b5d67f64
SB
729 goto out;
730 }
a2de733c 731
b5d67f64
SB
732 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
733 sblock_bad->no_io_error_seen) {
734 /*
735 * the error disappeared after reading page by page, or
736 * the area was part of a huge bio and other parts of the
737 * bio caused I/O errors, or the block layer merged several
738 * read requests into one and the error is caused by a
739 * different bio (usually one of the two latter cases is
740 * the cause)
741 */
d9d181c1
SB
742 spin_lock(&sctx->stat_lock);
743 sctx->stat.unverified_errors++;
744 spin_unlock(&sctx->stat_lock);
a2de733c 745
b5d67f64 746 goto out;
a2de733c 747 }
a2de733c 748
b5d67f64 749 if (!sblock_bad->no_io_error_seen) {
d9d181c1
SB
750 spin_lock(&sctx->stat_lock);
751 sctx->stat.read_errors++;
752 spin_unlock(&sctx->stat_lock);
b5d67f64
SB
753 if (__ratelimit(&_rs))
754 scrub_print_warning("i/o error", sblock_to_check);
a36cf8b8 755 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
b5d67f64 756 } else if (sblock_bad->checksum_error) {
d9d181c1
SB
757 spin_lock(&sctx->stat_lock);
758 sctx->stat.csum_errors++;
759 spin_unlock(&sctx->stat_lock);
b5d67f64
SB
760 if (__ratelimit(&_rs))
761 scrub_print_warning("checksum error", sblock_to_check);
a36cf8b8 762 btrfs_dev_stat_inc_and_print(dev,
442a4f63 763 BTRFS_DEV_STAT_CORRUPTION_ERRS);
b5d67f64 764 } else if (sblock_bad->header_error) {
d9d181c1
SB
765 spin_lock(&sctx->stat_lock);
766 sctx->stat.verify_errors++;
767 spin_unlock(&sctx->stat_lock);
b5d67f64
SB
768 if (__ratelimit(&_rs))
769 scrub_print_warning("checksum/header error",
770 sblock_to_check);
442a4f63 771 if (sblock_bad->generation_error)
a36cf8b8 772 btrfs_dev_stat_inc_and_print(dev,
442a4f63
SB
773 BTRFS_DEV_STAT_GENERATION_ERRS);
774 else
a36cf8b8 775 btrfs_dev_stat_inc_and_print(dev,
442a4f63 776 BTRFS_DEV_STAT_CORRUPTION_ERRS);
b5d67f64 777 }
a2de733c 778
d9d181c1 779 if (sctx->readonly)
b5d67f64 780 goto did_not_correct_error;
a2de733c 781
b5d67f64
SB
782 if (!is_metadata && !have_csum) {
783 struct scrub_fixup_nodatasum *fixup_nodatasum;
a2de733c 784
b5d67f64
SB
785 /*
786 * !is_metadata and !have_csum, this means that the data
787 * might not be COW'ed, that it might be modified
788 * concurrently. The general strategy to work on the
789 * commit root does not help in the case when COW is not
790 * used.
791 */
792 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
793 if (!fixup_nodatasum)
794 goto did_not_correct_error;
d9d181c1 795 fixup_nodatasum->sctx = sctx;
a36cf8b8 796 fixup_nodatasum->dev = dev;
b5d67f64
SB
797 fixup_nodatasum->logical = logical;
798 fixup_nodatasum->root = fs_info->extent_root;
799 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
a2de733c 800 /*
0ef8e451
JS
801 * increment scrubs_running to prevent cancel requests from
802 * completing as long as a fixup worker is running. we must also
803 * increment scrubs_paused to prevent deadlocking on pause
804 * requests used for transactions commits (as the worker uses a
805 * transaction context). it is safe to regard the fixup worker
806 * as paused for all matters practical. effectively, we only
807 * avoid cancellation requests from completing.
a2de733c 808 */
0ef8e451
JS
809 mutex_lock(&fs_info->scrub_lock);
810 atomic_inc(&fs_info->scrubs_running);
811 atomic_inc(&fs_info->scrubs_paused);
812 mutex_unlock(&fs_info->scrub_lock);
d9d181c1 813 atomic_inc(&sctx->fixup_cnt);
b5d67f64
SB
814 fixup_nodatasum->work.func = scrub_fixup_nodatasum;
815 btrfs_queue_worker(&fs_info->scrub_workers,
816 &fixup_nodatasum->work);
817 goto out;
a2de733c
AJ
818 }
819
b5d67f64
SB
820 /*
821 * now build and submit the bios for the other mirrors, check
822 * checksums
823 */
824 for (mirror_index = 0;
825 mirror_index < BTRFS_MAX_MIRRORS &&
826 sblocks_for_recheck[mirror_index].page_count > 0;
827 mirror_index++) {
828 if (mirror_index == failed_mirror_index)
829 continue;
830
831 /* build and submit the bios, check checksums */
832 ret = scrub_recheck_block(fs_info,
833 sblocks_for_recheck + mirror_index,
834 is_metadata, have_csum, csum,
d9d181c1 835 generation, sctx->csum_size);
b5d67f64
SB
836 if (ret)
837 goto did_not_correct_error;
a2de733c
AJ
838 }
839
b5d67f64
SB
840 /*
841 * first try to pick the mirror which is completely without I/O
842 * errors and also does not have a checksum error.
843 * If one is found, and if a checksum is present, the full block
844 * that is known to contain an error is rewritten. Afterwards
845 * the block is known to be corrected.
846 * If a mirror is found which is completely correct, and no
847 * checksum is present, only those pages are rewritten that had
848 * an I/O error in the block to be repaired, since it cannot be
849 * determined, which copy of the other pages is better (and it
850 * could happen otherwise that a correct page would be
851 * overwritten by a bad one).
852 */
853 for (mirror_index = 0;
854 mirror_index < BTRFS_MAX_MIRRORS &&
855 sblocks_for_recheck[mirror_index].page_count > 0;
856 mirror_index++) {
857 struct scrub_block *sblock_other = sblocks_for_recheck +
858 mirror_index;
859
860 if (!sblock_other->header_error &&
861 !sblock_other->checksum_error &&
862 sblock_other->no_io_error_seen) {
863 int force_write = is_metadata || have_csum;
864
865 ret = scrub_repair_block_from_good_copy(sblock_bad,
866 sblock_other,
867 force_write);
868 if (0 == ret)
869 goto corrected_error;
870 }
871 }
a2de733c
AJ
872
873 /*
b5d67f64
SB
874 * in case of I/O errors in the area that is supposed to be
875 * repaired, continue by picking good copies of those pages.
876 * Select the good pages from mirrors to rewrite bad pages from
877 * the area to fix. Afterwards verify the checksum of the block
878 * that is supposed to be repaired. This verification step is
879 * only done for the purpose of statistic counting and for the
880 * final scrub report, whether errors remain.
881 * A perfect algorithm could make use of the checksum and try
882 * all possible combinations of pages from the different mirrors
883 * until the checksum verification succeeds. For example, when
884 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
885 * of mirror #2 is readable but the final checksum test fails,
886 * then the 2nd page of mirror #3 could be tried, whether now
887 * the final checksum succeedes. But this would be a rare
888 * exception and is therefore not implemented. At least it is
889 * avoided that the good copy is overwritten.
890 * A more useful improvement would be to pick the sectors
891 * without I/O error based on sector sizes (512 bytes on legacy
892 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
893 * mirror could be repaired by taking 512 byte of a different
894 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
895 * area are unreadable.
a2de733c 896 */
a2de733c 897
b5d67f64
SB
898 /* can only fix I/O errors from here on */
899 if (sblock_bad->no_io_error_seen)
900 goto did_not_correct_error;
901
902 success = 1;
903 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
7a9e9987 904 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
b5d67f64
SB
905
906 if (!page_bad->io_error)
a2de733c 907 continue;
b5d67f64
SB
908
909 for (mirror_index = 0;
910 mirror_index < BTRFS_MAX_MIRRORS &&
911 sblocks_for_recheck[mirror_index].page_count > 0;
912 mirror_index++) {
913 struct scrub_block *sblock_other = sblocks_for_recheck +
914 mirror_index;
7a9e9987
SB
915 struct scrub_page *page_other = sblock_other->pagev[
916 page_num];
b5d67f64
SB
917
918 if (!page_other->io_error) {
919 ret = scrub_repair_page_from_good_copy(
920 sblock_bad, sblock_other, page_num, 0);
921 if (0 == ret) {
922 page_bad->io_error = 0;
923 break; /* succeeded for this page */
924 }
925 }
96e36920 926 }
a2de733c 927
b5d67f64
SB
928 if (page_bad->io_error) {
929 /* did not find a mirror to copy the page from */
930 success = 0;
931 }
a2de733c 932 }
a2de733c 933
b5d67f64
SB
934 if (success) {
935 if (is_metadata || have_csum) {
936 /*
937 * need to verify the checksum now that all
938 * sectors on disk are repaired (the write
939 * request for data to be repaired is on its way).
940 * Just be lazy and use scrub_recheck_block()
941 * which re-reads the data before the checksum
942 * is verified, but most likely the data comes out
943 * of the page cache.
944 */
945 ret = scrub_recheck_block(fs_info, sblock_bad,
946 is_metadata, have_csum, csum,
d9d181c1 947 generation, sctx->csum_size);
b5d67f64
SB
948 if (!ret && !sblock_bad->header_error &&
949 !sblock_bad->checksum_error &&
950 sblock_bad->no_io_error_seen)
951 goto corrected_error;
952 else
953 goto did_not_correct_error;
954 } else {
955corrected_error:
d9d181c1
SB
956 spin_lock(&sctx->stat_lock);
957 sctx->stat.corrected_errors++;
958 spin_unlock(&sctx->stat_lock);
606686ee 959 printk_ratelimited_in_rcu(KERN_ERR
b5d67f64 960 "btrfs: fixed up error at logical %llu on dev %s\n",
606686ee 961 (unsigned long long)logical,
a36cf8b8 962 rcu_str_deref(dev->name));
8628764e 963 }
b5d67f64
SB
964 } else {
965did_not_correct_error:
d9d181c1
SB
966 spin_lock(&sctx->stat_lock);
967 sctx->stat.uncorrectable_errors++;
968 spin_unlock(&sctx->stat_lock);
606686ee 969 printk_ratelimited_in_rcu(KERN_ERR
b5d67f64 970 "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
606686ee 971 (unsigned long long)logical,
a36cf8b8 972 rcu_str_deref(dev->name));
96e36920 973 }
a2de733c 974
b5d67f64
SB
975out:
976 if (sblocks_for_recheck) {
977 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
978 mirror_index++) {
979 struct scrub_block *sblock = sblocks_for_recheck +
980 mirror_index;
981 int page_index;
982
7a9e9987
SB
983 for (page_index = 0; page_index < sblock->page_count;
984 page_index++) {
985 sblock->pagev[page_index]->sblock = NULL;
986 scrub_page_put(sblock->pagev[page_index]);
987 }
b5d67f64
SB
988 }
989 kfree(sblocks_for_recheck);
990 }
a2de733c 991
b5d67f64
SB
992 return 0;
993}
a2de733c 994
d9d181c1 995static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
b5d67f64
SB
996 struct btrfs_mapping_tree *map_tree,
997 u64 length, u64 logical,
998 struct scrub_block *sblocks_for_recheck)
999{
1000 int page_index;
1001 int mirror_index;
1002 int ret;
1003
1004 /*
7a9e9987 1005 * note: the two members ref_count and outstanding_pages
b5d67f64
SB
1006 * are not used (and not set) in the blocks that are used for
1007 * the recheck procedure
1008 */
1009
1010 page_index = 0;
1011 while (length > 0) {
1012 u64 sublen = min_t(u64, length, PAGE_SIZE);
1013 u64 mapped_length = sublen;
1014 struct btrfs_bio *bbio = NULL;
a2de733c 1015
b5d67f64
SB
1016 /*
1017 * with a length of PAGE_SIZE, each returned stripe
1018 * represents one mirror
1019 */
1020 ret = btrfs_map_block(map_tree, WRITE, logical, &mapped_length,
1021 &bbio, 0);
1022 if (ret || !bbio || mapped_length < sublen) {
1023 kfree(bbio);
1024 return -EIO;
1025 }
a2de733c 1026
b5d67f64
SB
1027 BUG_ON(page_index >= SCRUB_PAGES_PER_BIO);
1028 for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
1029 mirror_index++) {
1030 struct scrub_block *sblock;
1031 struct scrub_page *page;
1032
1033 if (mirror_index >= BTRFS_MAX_MIRRORS)
1034 continue;
1035
1036 sblock = sblocks_for_recheck + mirror_index;
7a9e9987
SB
1037 sblock->sctx = sctx;
1038 page = kzalloc(sizeof(*page), GFP_NOFS);
1039 if (!page) {
1040leave_nomem:
d9d181c1
SB
1041 spin_lock(&sctx->stat_lock);
1042 sctx->stat.malloc_errors++;
1043 spin_unlock(&sctx->stat_lock);
cf93dcce 1044 kfree(bbio);
b5d67f64
SB
1045 return -ENOMEM;
1046 }
7a9e9987
SB
1047 scrub_page_get(page);
1048 sblock->pagev[page_index] = page;
1049 page->logical = logical;
1050 page->physical = bbio->stripes[mirror_index].physical;
1051 /* for missing devices, dev->bdev is NULL */
1052 page->dev = bbio->stripes[mirror_index].dev;
1053 page->mirror_num = mirror_index + 1;
b5d67f64 1054 sblock->page_count++;
7a9e9987
SB
1055 page->page = alloc_page(GFP_NOFS);
1056 if (!page->page)
1057 goto leave_nomem;
b5d67f64
SB
1058 }
1059 kfree(bbio);
1060 length -= sublen;
1061 logical += sublen;
1062 page_index++;
1063 }
1064
1065 return 0;
96e36920
ID
1066}
1067
b5d67f64
SB
1068/*
1069 * this function will check the on disk data for checksum errors, header
1070 * errors and read I/O errors. If any I/O errors happen, the exact pages
1071 * which are errored are marked as being bad. The goal is to enable scrub
1072 * to take those pages that are not errored from all the mirrors so that
1073 * the pages that are errored in the just handled mirror can be repaired.
1074 */
1075static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
1076 struct scrub_block *sblock, int is_metadata,
1077 int have_csum, u8 *csum, u64 generation,
1078 u16 csum_size)
96e36920 1079{
b5d67f64 1080 int page_num;
96e36920 1081
b5d67f64
SB
1082 sblock->no_io_error_seen = 1;
1083 sblock->header_error = 0;
1084 sblock->checksum_error = 0;
96e36920 1085
b5d67f64
SB
1086 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1087 struct bio *bio;
1088 int ret;
7a9e9987 1089 struct scrub_page *page = sblock->pagev[page_num];
b5d67f64
SB
1090 DECLARE_COMPLETION_ONSTACK(complete);
1091
442a4f63 1092 if (page->dev->bdev == NULL) {
ea9947b4
SB
1093 page->io_error = 1;
1094 sblock->no_io_error_seen = 0;
1095 continue;
1096 }
1097
7a9e9987 1098 WARN_ON(!page->page);
b5d67f64 1099 bio = bio_alloc(GFP_NOFS, 1);
e627ee7b
TI
1100 if (!bio)
1101 return -EIO;
442a4f63 1102 bio->bi_bdev = page->dev->bdev;
b5d67f64
SB
1103 bio->bi_sector = page->physical >> 9;
1104 bio->bi_end_io = scrub_complete_bio_end_io;
1105 bio->bi_private = &complete;
1106
1107 ret = bio_add_page(bio, page->page, PAGE_SIZE, 0);
1108 if (PAGE_SIZE != ret) {
1109 bio_put(bio);
1110 return -EIO;
1111 }
1112 btrfsic_submit_bio(READ, bio);
96e36920 1113
b5d67f64
SB
1114 /* this will also unplug the queue */
1115 wait_for_completion(&complete);
96e36920 1116
b5d67f64
SB
1117 page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
1118 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1119 sblock->no_io_error_seen = 0;
1120 bio_put(bio);
1121 }
96e36920 1122
b5d67f64
SB
1123 if (sblock->no_io_error_seen)
1124 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1125 have_csum, csum, generation,
1126 csum_size);
1127
1128 return 0;
a2de733c
AJ
1129}
1130
b5d67f64
SB
1131static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1132 struct scrub_block *sblock,
1133 int is_metadata, int have_csum,
1134 const u8 *csum, u64 generation,
1135 u16 csum_size)
a2de733c 1136{
b5d67f64
SB
1137 int page_num;
1138 u8 calculated_csum[BTRFS_CSUM_SIZE];
1139 u32 crc = ~(u32)0;
1140 struct btrfs_root *root = fs_info->extent_root;
1141 void *mapped_buffer;
1142
7a9e9987 1143 WARN_ON(!sblock->pagev[0]->page);
b5d67f64
SB
1144 if (is_metadata) {
1145 struct btrfs_header *h;
1146
7a9e9987 1147 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
b5d67f64
SB
1148 h = (struct btrfs_header *)mapped_buffer;
1149
7a9e9987 1150 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr) ||
b5d67f64
SB
1151 memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
1152 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
442a4f63 1153 BTRFS_UUID_SIZE)) {
b5d67f64 1154 sblock->header_error = 1;
442a4f63
SB
1155 } else if (generation != le64_to_cpu(h->generation)) {
1156 sblock->header_error = 1;
1157 sblock->generation_error = 1;
1158 }
b5d67f64
SB
1159 csum = h->csum;
1160 } else {
1161 if (!have_csum)
1162 return;
a2de733c 1163
7a9e9987 1164 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
b5d67f64 1165 }
a2de733c 1166
b5d67f64
SB
1167 for (page_num = 0;;) {
1168 if (page_num == 0 && is_metadata)
1169 crc = btrfs_csum_data(root,
1170 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1171 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1172 else
1173 crc = btrfs_csum_data(root, mapped_buffer, crc,
1174 PAGE_SIZE);
1175
9613bebb 1176 kunmap_atomic(mapped_buffer);
b5d67f64
SB
1177 page_num++;
1178 if (page_num >= sblock->page_count)
1179 break;
7a9e9987 1180 WARN_ON(!sblock->pagev[page_num]->page);
b5d67f64 1181
7a9e9987 1182 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
b5d67f64
SB
1183 }
1184
1185 btrfs_csum_final(crc, calculated_csum);
1186 if (memcmp(calculated_csum, csum, csum_size))
1187 sblock->checksum_error = 1;
a2de733c
AJ
1188}
1189
b5d67f64 1190static void scrub_complete_bio_end_io(struct bio *bio, int err)
a2de733c 1191{
b5d67f64
SB
1192 complete((struct completion *)bio->bi_private);
1193}
a2de733c 1194
b5d67f64
SB
1195static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1196 struct scrub_block *sblock_good,
1197 int force_write)
1198{
1199 int page_num;
1200 int ret = 0;
96e36920 1201
b5d67f64
SB
1202 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1203 int ret_sub;
96e36920 1204
b5d67f64
SB
1205 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1206 sblock_good,
1207 page_num,
1208 force_write);
1209 if (ret_sub)
1210 ret = ret_sub;
a2de733c 1211 }
b5d67f64
SB
1212
1213 return ret;
1214}
1215
1216static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1217 struct scrub_block *sblock_good,
1218 int page_num, int force_write)
1219{
7a9e9987
SB
1220 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1221 struct scrub_page *page_good = sblock_good->pagev[page_num];
b5d67f64 1222
7a9e9987
SB
1223 BUG_ON(page_bad->page == NULL);
1224 BUG_ON(page_good->page == NULL);
b5d67f64
SB
1225 if (force_write || sblock_bad->header_error ||
1226 sblock_bad->checksum_error || page_bad->io_error) {
1227 struct bio *bio;
1228 int ret;
1229 DECLARE_COMPLETION_ONSTACK(complete);
1230
1231 bio = bio_alloc(GFP_NOFS, 1);
e627ee7b
TI
1232 if (!bio)
1233 return -EIO;
442a4f63 1234 bio->bi_bdev = page_bad->dev->bdev;
b5d67f64
SB
1235 bio->bi_sector = page_bad->physical >> 9;
1236 bio->bi_end_io = scrub_complete_bio_end_io;
1237 bio->bi_private = &complete;
1238
1239 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1240 if (PAGE_SIZE != ret) {
1241 bio_put(bio);
1242 return -EIO;
13db62b7 1243 }
b5d67f64
SB
1244 btrfsic_submit_bio(WRITE, bio);
1245
1246 /* this will also unplug the queue */
1247 wait_for_completion(&complete);
442a4f63
SB
1248 if (!bio_flagged(bio, BIO_UPTODATE)) {
1249 btrfs_dev_stat_inc_and_print(page_bad->dev,
1250 BTRFS_DEV_STAT_WRITE_ERRS);
1251 bio_put(bio);
1252 return -EIO;
1253 }
b5d67f64 1254 bio_put(bio);
a2de733c
AJ
1255 }
1256
b5d67f64
SB
1257 return 0;
1258}
1259
1260static void scrub_checksum(struct scrub_block *sblock)
1261{
1262 u64 flags;
1263 int ret;
1264
7a9e9987
SB
1265 WARN_ON(sblock->page_count < 1);
1266 flags = sblock->pagev[0]->flags;
b5d67f64
SB
1267 ret = 0;
1268 if (flags & BTRFS_EXTENT_FLAG_DATA)
1269 ret = scrub_checksum_data(sblock);
1270 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1271 ret = scrub_checksum_tree_block(sblock);
1272 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1273 (void)scrub_checksum_super(sblock);
1274 else
1275 WARN_ON(1);
1276 if (ret)
1277 scrub_handle_errored_block(sblock);
a2de733c
AJ
1278}
1279
b5d67f64 1280static int scrub_checksum_data(struct scrub_block *sblock)
a2de733c 1281{
d9d181c1 1282 struct scrub_ctx *sctx = sblock->sctx;
a2de733c 1283 u8 csum[BTRFS_CSUM_SIZE];
b5d67f64
SB
1284 u8 *on_disk_csum;
1285 struct page *page;
1286 void *buffer;
a2de733c
AJ
1287 u32 crc = ~(u32)0;
1288 int fail = 0;
a36cf8b8 1289 struct btrfs_root *root = sctx->dev_root;
b5d67f64
SB
1290 u64 len;
1291 int index;
a2de733c 1292
b5d67f64 1293 BUG_ON(sblock->page_count < 1);
7a9e9987 1294 if (!sblock->pagev[0]->have_csum)
a2de733c
AJ
1295 return 0;
1296
7a9e9987
SB
1297 on_disk_csum = sblock->pagev[0]->csum;
1298 page = sblock->pagev[0]->page;
9613bebb 1299 buffer = kmap_atomic(page);
b5d67f64 1300
d9d181c1 1301 len = sctx->sectorsize;
b5d67f64
SB
1302 index = 0;
1303 for (;;) {
1304 u64 l = min_t(u64, len, PAGE_SIZE);
1305
1306 crc = btrfs_csum_data(root, buffer, crc, l);
9613bebb 1307 kunmap_atomic(buffer);
b5d67f64
SB
1308 len -= l;
1309 if (len == 0)
1310 break;
1311 index++;
1312 BUG_ON(index >= sblock->page_count);
7a9e9987
SB
1313 BUG_ON(!sblock->pagev[index]->page);
1314 page = sblock->pagev[index]->page;
9613bebb 1315 buffer = kmap_atomic(page);
b5d67f64
SB
1316 }
1317
a2de733c 1318 btrfs_csum_final(crc, csum);
d9d181c1 1319 if (memcmp(csum, on_disk_csum, sctx->csum_size))
a2de733c
AJ
1320 fail = 1;
1321
a2de733c
AJ
1322 return fail;
1323}
1324
b5d67f64 1325static int scrub_checksum_tree_block(struct scrub_block *sblock)
a2de733c 1326{
d9d181c1 1327 struct scrub_ctx *sctx = sblock->sctx;
a2de733c 1328 struct btrfs_header *h;
a36cf8b8 1329 struct btrfs_root *root = sctx->dev_root;
a2de733c 1330 struct btrfs_fs_info *fs_info = root->fs_info;
b5d67f64
SB
1331 u8 calculated_csum[BTRFS_CSUM_SIZE];
1332 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1333 struct page *page;
1334 void *mapped_buffer;
1335 u64 mapped_size;
1336 void *p;
a2de733c
AJ
1337 u32 crc = ~(u32)0;
1338 int fail = 0;
1339 int crc_fail = 0;
b5d67f64
SB
1340 u64 len;
1341 int index;
1342
1343 BUG_ON(sblock->page_count < 1);
7a9e9987 1344 page = sblock->pagev[0]->page;
9613bebb 1345 mapped_buffer = kmap_atomic(page);
b5d67f64 1346 h = (struct btrfs_header *)mapped_buffer;
d9d181c1 1347 memcpy(on_disk_csum, h->csum, sctx->csum_size);
a2de733c
AJ
1348
1349 /*
1350 * we don't use the getter functions here, as we
1351 * a) don't have an extent buffer and
1352 * b) the page is already kmapped
1353 */
a2de733c 1354
7a9e9987 1355 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr))
a2de733c
AJ
1356 ++fail;
1357
7a9e9987 1358 if (sblock->pagev[0]->generation != le64_to_cpu(h->generation))
a2de733c
AJ
1359 ++fail;
1360
1361 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1362 ++fail;
1363
1364 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1365 BTRFS_UUID_SIZE))
1366 ++fail;
1367
d9d181c1
SB
1368 BUG_ON(sctx->nodesize != sctx->leafsize);
1369 len = sctx->nodesize - BTRFS_CSUM_SIZE;
b5d67f64
SB
1370 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1371 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1372 index = 0;
1373 for (;;) {
1374 u64 l = min_t(u64, len, mapped_size);
1375
1376 crc = btrfs_csum_data(root, p, crc, l);
9613bebb 1377 kunmap_atomic(mapped_buffer);
b5d67f64
SB
1378 len -= l;
1379 if (len == 0)
1380 break;
1381 index++;
1382 BUG_ON(index >= sblock->page_count);
7a9e9987
SB
1383 BUG_ON(!sblock->pagev[index]->page);
1384 page = sblock->pagev[index]->page;
9613bebb 1385 mapped_buffer = kmap_atomic(page);
b5d67f64
SB
1386 mapped_size = PAGE_SIZE;
1387 p = mapped_buffer;
1388 }
1389
1390 btrfs_csum_final(crc, calculated_csum);
d9d181c1 1391 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
a2de733c
AJ
1392 ++crc_fail;
1393
a2de733c
AJ
1394 return fail || crc_fail;
1395}
1396
b5d67f64 1397static int scrub_checksum_super(struct scrub_block *sblock)
a2de733c
AJ
1398{
1399 struct btrfs_super_block *s;
d9d181c1 1400 struct scrub_ctx *sctx = sblock->sctx;
a36cf8b8 1401 struct btrfs_root *root = sctx->dev_root;
a2de733c 1402 struct btrfs_fs_info *fs_info = root->fs_info;
b5d67f64
SB
1403 u8 calculated_csum[BTRFS_CSUM_SIZE];
1404 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1405 struct page *page;
1406 void *mapped_buffer;
1407 u64 mapped_size;
1408 void *p;
a2de733c 1409 u32 crc = ~(u32)0;
442a4f63
SB
1410 int fail_gen = 0;
1411 int fail_cor = 0;
b5d67f64
SB
1412 u64 len;
1413 int index;
a2de733c 1414
b5d67f64 1415 BUG_ON(sblock->page_count < 1);
7a9e9987 1416 page = sblock->pagev[0]->page;
9613bebb 1417 mapped_buffer = kmap_atomic(page);
b5d67f64 1418 s = (struct btrfs_super_block *)mapped_buffer;
d9d181c1 1419 memcpy(on_disk_csum, s->csum, sctx->csum_size);
a2de733c 1420
7a9e9987 1421 if (sblock->pagev[0]->logical != le64_to_cpu(s->bytenr))
442a4f63 1422 ++fail_cor;
a2de733c 1423
7a9e9987 1424 if (sblock->pagev[0]->generation != le64_to_cpu(s->generation))
442a4f63 1425 ++fail_gen;
a2de733c
AJ
1426
1427 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
442a4f63 1428 ++fail_cor;
a2de733c 1429
b5d67f64
SB
1430 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1431 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1432 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1433 index = 0;
1434 for (;;) {
1435 u64 l = min_t(u64, len, mapped_size);
1436
1437 crc = btrfs_csum_data(root, p, crc, l);
9613bebb 1438 kunmap_atomic(mapped_buffer);
b5d67f64
SB
1439 len -= l;
1440 if (len == 0)
1441 break;
1442 index++;
1443 BUG_ON(index >= sblock->page_count);
7a9e9987
SB
1444 BUG_ON(!sblock->pagev[index]->page);
1445 page = sblock->pagev[index]->page;
9613bebb 1446 mapped_buffer = kmap_atomic(page);
b5d67f64
SB
1447 mapped_size = PAGE_SIZE;
1448 p = mapped_buffer;
1449 }
1450
1451 btrfs_csum_final(crc, calculated_csum);
d9d181c1 1452 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
442a4f63 1453 ++fail_cor;
a2de733c 1454
442a4f63 1455 if (fail_cor + fail_gen) {
a2de733c
AJ
1456 /*
1457 * if we find an error in a super block, we just report it.
1458 * They will get written with the next transaction commit
1459 * anyway
1460 */
d9d181c1
SB
1461 spin_lock(&sctx->stat_lock);
1462 ++sctx->stat.super_errors;
1463 spin_unlock(&sctx->stat_lock);
442a4f63 1464 if (fail_cor)
7a9e9987 1465 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
442a4f63
SB
1466 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1467 else
7a9e9987 1468 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
442a4f63 1469 BTRFS_DEV_STAT_GENERATION_ERRS);
a2de733c
AJ
1470 }
1471
442a4f63 1472 return fail_cor + fail_gen;
a2de733c
AJ
1473}
1474
b5d67f64
SB
1475static void scrub_block_get(struct scrub_block *sblock)
1476{
1477 atomic_inc(&sblock->ref_count);
1478}
1479
1480static void scrub_block_put(struct scrub_block *sblock)
1481{
1482 if (atomic_dec_and_test(&sblock->ref_count)) {
1483 int i;
1484
1485 for (i = 0; i < sblock->page_count; i++)
7a9e9987 1486 scrub_page_put(sblock->pagev[i]);
b5d67f64
SB
1487 kfree(sblock);
1488 }
1489}
1490
7a9e9987
SB
1491static void scrub_page_get(struct scrub_page *spage)
1492{
1493 atomic_inc(&spage->ref_count);
1494}
1495
1496static void scrub_page_put(struct scrub_page *spage)
1497{
1498 if (atomic_dec_and_test(&spage->ref_count)) {
1499 if (spage->page)
1500 __free_page(spage->page);
1501 kfree(spage);
1502 }
1503}
1504
d9d181c1 1505static void scrub_submit(struct scrub_ctx *sctx)
a2de733c
AJ
1506{
1507 struct scrub_bio *sbio;
1508
d9d181c1 1509 if (sctx->curr == -1)
1623edeb 1510 return;
a2de733c 1511
d9d181c1
SB
1512 sbio = sctx->bios[sctx->curr];
1513 sctx->curr = -1;
1514 atomic_inc(&sctx->in_flight);
a2de733c 1515
21adbd5c 1516 btrfsic_submit_bio(READ, sbio->bio);
a2de733c
AJ
1517}
1518
d9d181c1 1519static int scrub_add_page_to_bio(struct scrub_ctx *sctx,
b5d67f64 1520 struct scrub_page *spage)
a2de733c 1521{
b5d67f64 1522 struct scrub_block *sblock = spage->sblock;
a2de733c 1523 struct scrub_bio *sbio;
69f4cb52 1524 int ret;
a2de733c
AJ
1525
1526again:
1527 /*
1528 * grab a fresh bio or wait for one to become available
1529 */
d9d181c1
SB
1530 while (sctx->curr == -1) {
1531 spin_lock(&sctx->list_lock);
1532 sctx->curr = sctx->first_free;
1533 if (sctx->curr != -1) {
1534 sctx->first_free = sctx->bios[sctx->curr]->next_free;
1535 sctx->bios[sctx->curr]->next_free = -1;
1536 sctx->bios[sctx->curr]->page_count = 0;
1537 spin_unlock(&sctx->list_lock);
a2de733c 1538 } else {
d9d181c1
SB
1539 spin_unlock(&sctx->list_lock);
1540 wait_event(sctx->list_wait, sctx->first_free != -1);
a2de733c
AJ
1541 }
1542 }
d9d181c1 1543 sbio = sctx->bios[sctx->curr];
b5d67f64 1544 if (sbio->page_count == 0) {
69f4cb52
AJ
1545 struct bio *bio;
1546
b5d67f64
SB
1547 sbio->physical = spage->physical;
1548 sbio->logical = spage->logical;
a36cf8b8 1549 sbio->dev = spage->dev;
b5d67f64
SB
1550 bio = sbio->bio;
1551 if (!bio) {
d9d181c1 1552 bio = bio_alloc(GFP_NOFS, sctx->pages_per_bio);
b5d67f64
SB
1553 if (!bio)
1554 return -ENOMEM;
1555 sbio->bio = bio;
1556 }
69f4cb52
AJ
1557
1558 bio->bi_private = sbio;
1559 bio->bi_end_io = scrub_bio_end_io;
a36cf8b8
SB
1560 bio->bi_bdev = sbio->dev->bdev;
1561 bio->bi_sector = sbio->physical >> 9;
69f4cb52 1562 sbio->err = 0;
b5d67f64
SB
1563 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1564 spage->physical ||
1565 sbio->logical + sbio->page_count * PAGE_SIZE !=
a36cf8b8
SB
1566 spage->logical ||
1567 sbio->dev != spage->dev) {
d9d181c1 1568 scrub_submit(sctx);
a2de733c
AJ
1569 goto again;
1570 }
69f4cb52 1571
b5d67f64
SB
1572 sbio->pagev[sbio->page_count] = spage;
1573 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1574 if (ret != PAGE_SIZE) {
1575 if (sbio->page_count < 1) {
1576 bio_put(sbio->bio);
1577 sbio->bio = NULL;
1578 return -EIO;
1579 }
d9d181c1 1580 scrub_submit(sctx);
69f4cb52
AJ
1581 goto again;
1582 }
1583
b5d67f64
SB
1584 scrub_block_get(sblock); /* one for the added page */
1585 atomic_inc(&sblock->outstanding_pages);
1586 sbio->page_count++;
d9d181c1
SB
1587 if (sbio->page_count == sctx->pages_per_bio)
1588 scrub_submit(sctx);
b5d67f64
SB
1589
1590 return 0;
1591}
1592
d9d181c1 1593static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
a36cf8b8
SB
1594 u64 physical, struct btrfs_device *dev, u64 flags,
1595 u64 gen, int mirror_num, u8 *csum, int force)
b5d67f64
SB
1596{
1597 struct scrub_block *sblock;
1598 int index;
1599
1600 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
1601 if (!sblock) {
d9d181c1
SB
1602 spin_lock(&sctx->stat_lock);
1603 sctx->stat.malloc_errors++;
1604 spin_unlock(&sctx->stat_lock);
b5d67f64 1605 return -ENOMEM;
a2de733c 1606 }
b5d67f64 1607
7a9e9987
SB
1608 /* one ref inside this function, plus one for each page added to
1609 * a bio later on */
b5d67f64 1610 atomic_set(&sblock->ref_count, 1);
d9d181c1 1611 sblock->sctx = sctx;
b5d67f64
SB
1612 sblock->no_io_error_seen = 1;
1613
1614 for (index = 0; len > 0; index++) {
7a9e9987 1615 struct scrub_page *spage;
b5d67f64
SB
1616 u64 l = min_t(u64, len, PAGE_SIZE);
1617
7a9e9987
SB
1618 spage = kzalloc(sizeof(*spage), GFP_NOFS);
1619 if (!spage) {
1620leave_nomem:
d9d181c1
SB
1621 spin_lock(&sctx->stat_lock);
1622 sctx->stat.malloc_errors++;
1623 spin_unlock(&sctx->stat_lock);
7a9e9987 1624 scrub_block_put(sblock);
b5d67f64
SB
1625 return -ENOMEM;
1626 }
7a9e9987
SB
1627 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
1628 scrub_page_get(spage);
1629 sblock->pagev[index] = spage;
b5d67f64 1630 spage->sblock = sblock;
a36cf8b8 1631 spage->dev = dev;
b5d67f64
SB
1632 spage->flags = flags;
1633 spage->generation = gen;
1634 spage->logical = logical;
1635 spage->physical = physical;
1636 spage->mirror_num = mirror_num;
1637 if (csum) {
1638 spage->have_csum = 1;
d9d181c1 1639 memcpy(spage->csum, csum, sctx->csum_size);
b5d67f64
SB
1640 } else {
1641 spage->have_csum = 0;
1642 }
1643 sblock->page_count++;
7a9e9987
SB
1644 spage->page = alloc_page(GFP_NOFS);
1645 if (!spage->page)
1646 goto leave_nomem;
b5d67f64
SB
1647 len -= l;
1648 logical += l;
1649 physical += l;
1650 }
1651
7a9e9987 1652 WARN_ON(sblock->page_count == 0);
b5d67f64 1653 for (index = 0; index < sblock->page_count; index++) {
7a9e9987 1654 struct scrub_page *spage = sblock->pagev[index];
1bc87793
AJ
1655 int ret;
1656
d9d181c1 1657 ret = scrub_add_page_to_bio(sctx, spage);
b5d67f64
SB
1658 if (ret) {
1659 scrub_block_put(sblock);
1bc87793 1660 return ret;
b5d67f64 1661 }
1bc87793 1662 }
a2de733c 1663
b5d67f64 1664 if (force)
d9d181c1 1665 scrub_submit(sctx);
a2de733c 1666
b5d67f64
SB
1667 /* last one frees, either here or in bio completion for last page */
1668 scrub_block_put(sblock);
a2de733c
AJ
1669 return 0;
1670}
1671
b5d67f64
SB
1672static void scrub_bio_end_io(struct bio *bio, int err)
1673{
1674 struct scrub_bio *sbio = bio->bi_private;
a36cf8b8 1675 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
b5d67f64
SB
1676
1677 sbio->err = err;
1678 sbio->bio = bio;
1679
1680 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
1681}
1682
1683static void scrub_bio_end_io_worker(struct btrfs_work *work)
1684{
1685 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
d9d181c1 1686 struct scrub_ctx *sctx = sbio->sctx;
b5d67f64
SB
1687 int i;
1688
1689 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_BIO);
1690 if (sbio->err) {
1691 for (i = 0; i < sbio->page_count; i++) {
1692 struct scrub_page *spage = sbio->pagev[i];
1693
1694 spage->io_error = 1;
1695 spage->sblock->no_io_error_seen = 0;
1696 }
1697 }
1698
1699 /* now complete the scrub_block items that have all pages completed */
1700 for (i = 0; i < sbio->page_count; i++) {
1701 struct scrub_page *spage = sbio->pagev[i];
1702 struct scrub_block *sblock = spage->sblock;
1703
1704 if (atomic_dec_and_test(&sblock->outstanding_pages))
1705 scrub_block_complete(sblock);
1706 scrub_block_put(sblock);
1707 }
1708
b5d67f64
SB
1709 bio_put(sbio->bio);
1710 sbio->bio = NULL;
d9d181c1
SB
1711 spin_lock(&sctx->list_lock);
1712 sbio->next_free = sctx->first_free;
1713 sctx->first_free = sbio->index;
1714 spin_unlock(&sctx->list_lock);
1715 atomic_dec(&sctx->in_flight);
1716 wake_up(&sctx->list_wait);
b5d67f64
SB
1717}
1718
1719static void scrub_block_complete(struct scrub_block *sblock)
1720{
1721 if (!sblock->no_io_error_seen)
1722 scrub_handle_errored_block(sblock);
1723 else
1724 scrub_checksum(sblock);
1725}
1726
d9d181c1 1727static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
a2de733c
AJ
1728 u8 *csum)
1729{
1730 struct btrfs_ordered_sum *sum = NULL;
1731 int ret = 0;
1732 unsigned long i;
1733 unsigned long num_sectors;
a2de733c 1734
d9d181c1
SB
1735 while (!list_empty(&sctx->csum_list)) {
1736 sum = list_first_entry(&sctx->csum_list,
a2de733c
AJ
1737 struct btrfs_ordered_sum, list);
1738 if (sum->bytenr > logical)
1739 return 0;
1740 if (sum->bytenr + sum->len > logical)
1741 break;
1742
d9d181c1 1743 ++sctx->stat.csum_discards;
a2de733c
AJ
1744 list_del(&sum->list);
1745 kfree(sum);
1746 sum = NULL;
1747 }
1748 if (!sum)
1749 return 0;
1750
d9d181c1 1751 num_sectors = sum->len / sctx->sectorsize;
a2de733c
AJ
1752 for (i = 0; i < num_sectors; ++i) {
1753 if (sum->sums[i].bytenr == logical) {
d9d181c1 1754 memcpy(csum, &sum->sums[i].sum, sctx->csum_size);
a2de733c
AJ
1755 ret = 1;
1756 break;
1757 }
1758 }
1759 if (ret && i == num_sectors - 1) {
1760 list_del(&sum->list);
1761 kfree(sum);
1762 }
1763 return ret;
1764}
1765
1766/* scrub extent tries to collect up to 64 kB for each bio */
d9d181c1 1767static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
a36cf8b8
SB
1768 u64 physical, struct btrfs_device *dev, u64 flags,
1769 u64 gen, int mirror_num)
a2de733c
AJ
1770{
1771 int ret;
1772 u8 csum[BTRFS_CSUM_SIZE];
b5d67f64
SB
1773 u32 blocksize;
1774
1775 if (flags & BTRFS_EXTENT_FLAG_DATA) {
d9d181c1
SB
1776 blocksize = sctx->sectorsize;
1777 spin_lock(&sctx->stat_lock);
1778 sctx->stat.data_extents_scrubbed++;
1779 sctx->stat.data_bytes_scrubbed += len;
1780 spin_unlock(&sctx->stat_lock);
b5d67f64 1781 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
d9d181c1
SB
1782 BUG_ON(sctx->nodesize != sctx->leafsize);
1783 blocksize = sctx->nodesize;
1784 spin_lock(&sctx->stat_lock);
1785 sctx->stat.tree_extents_scrubbed++;
1786 sctx->stat.tree_bytes_scrubbed += len;
1787 spin_unlock(&sctx->stat_lock);
b5d67f64 1788 } else {
d9d181c1 1789 blocksize = sctx->sectorsize;
b5d67f64
SB
1790 BUG_ON(1);
1791 }
a2de733c
AJ
1792
1793 while (len) {
b5d67f64 1794 u64 l = min_t(u64, len, blocksize);
a2de733c
AJ
1795 int have_csum = 0;
1796
1797 if (flags & BTRFS_EXTENT_FLAG_DATA) {
1798 /* push csums to sbio */
d9d181c1 1799 have_csum = scrub_find_csum(sctx, logical, l, csum);
a2de733c 1800 if (have_csum == 0)
d9d181c1 1801 ++sctx->stat.no_csum;
a2de733c 1802 }
a36cf8b8 1803 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
b5d67f64 1804 mirror_num, have_csum ? csum : NULL, 0);
a2de733c
AJ
1805 if (ret)
1806 return ret;
1807 len -= l;
1808 logical += l;
1809 physical += l;
1810 }
1811 return 0;
1812}
1813
d9d181c1 1814static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
a36cf8b8
SB
1815 struct map_lookup *map,
1816 struct btrfs_device *scrub_dev,
1817 int num, u64 base, u64 length)
a2de733c
AJ
1818{
1819 struct btrfs_path *path;
a36cf8b8 1820 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
a2de733c
AJ
1821 struct btrfs_root *root = fs_info->extent_root;
1822 struct btrfs_root *csum_root = fs_info->csum_root;
1823 struct btrfs_extent_item *extent;
e7786c3a 1824 struct blk_plug plug;
a2de733c
AJ
1825 u64 flags;
1826 int ret;
1827 int slot;
1828 int i;
1829 u64 nstripes;
a2de733c
AJ
1830 struct extent_buffer *l;
1831 struct btrfs_key key;
1832 u64 physical;
1833 u64 logical;
1834 u64 generation;
e12fa9cd 1835 int mirror_num;
7a26285e
AJ
1836 struct reada_control *reada1;
1837 struct reada_control *reada2;
1838 struct btrfs_key key_start;
1839 struct btrfs_key key_end;
a2de733c
AJ
1840 u64 increment = map->stripe_len;
1841 u64 offset;
1842
1843 nstripes = length;
1844 offset = 0;
1845 do_div(nstripes, map->stripe_len);
1846 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1847 offset = map->stripe_len * num;
1848 increment = map->stripe_len * map->num_stripes;
193ea74b 1849 mirror_num = 1;
a2de733c
AJ
1850 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1851 int factor = map->num_stripes / map->sub_stripes;
1852 offset = map->stripe_len * (num / map->sub_stripes);
1853 increment = map->stripe_len * factor;
193ea74b 1854 mirror_num = num % map->sub_stripes + 1;
a2de733c
AJ
1855 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1856 increment = map->stripe_len;
193ea74b 1857 mirror_num = num % map->num_stripes + 1;
a2de733c
AJ
1858 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1859 increment = map->stripe_len;
193ea74b 1860 mirror_num = num % map->num_stripes + 1;
a2de733c
AJ
1861 } else {
1862 increment = map->stripe_len;
193ea74b 1863 mirror_num = 1;
a2de733c
AJ
1864 }
1865
1866 path = btrfs_alloc_path();
1867 if (!path)
1868 return -ENOMEM;
1869
b5d67f64
SB
1870 /*
1871 * work on commit root. The related disk blocks are static as
1872 * long as COW is applied. This means, it is save to rewrite
1873 * them to repair disk errors without any race conditions
1874 */
a2de733c
AJ
1875 path->search_commit_root = 1;
1876 path->skip_locking = 1;
1877
1878 /*
7a26285e
AJ
1879 * trigger the readahead for extent tree csum tree and wait for
1880 * completion. During readahead, the scrub is officially paused
1881 * to not hold off transaction commits
a2de733c
AJ
1882 */
1883 logical = base + offset;
a2de733c 1884
d9d181c1
SB
1885 wait_event(sctx->list_wait,
1886 atomic_read(&sctx->in_flight) == 0);
7a26285e
AJ
1887 atomic_inc(&fs_info->scrubs_paused);
1888 wake_up(&fs_info->scrub_pause_wait);
1889
1890 /* FIXME it might be better to start readahead at commit root */
1891 key_start.objectid = logical;
1892 key_start.type = BTRFS_EXTENT_ITEM_KEY;
1893 key_start.offset = (u64)0;
1894 key_end.objectid = base + offset + nstripes * increment;
1895 key_end.type = BTRFS_EXTENT_ITEM_KEY;
1896 key_end.offset = (u64)0;
1897 reada1 = btrfs_reada_add(root, &key_start, &key_end);
1898
1899 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1900 key_start.type = BTRFS_EXTENT_CSUM_KEY;
1901 key_start.offset = logical;
1902 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1903 key_end.type = BTRFS_EXTENT_CSUM_KEY;
1904 key_end.offset = base + offset + nstripes * increment;
1905 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
1906
1907 if (!IS_ERR(reada1))
1908 btrfs_reada_wait(reada1);
1909 if (!IS_ERR(reada2))
1910 btrfs_reada_wait(reada2);
1911
1912 mutex_lock(&fs_info->scrub_lock);
1913 while (atomic_read(&fs_info->scrub_pause_req)) {
1914 mutex_unlock(&fs_info->scrub_lock);
1915 wait_event(fs_info->scrub_pause_wait,
1916 atomic_read(&fs_info->scrub_pause_req) == 0);
1917 mutex_lock(&fs_info->scrub_lock);
a2de733c 1918 }
7a26285e
AJ
1919 atomic_dec(&fs_info->scrubs_paused);
1920 mutex_unlock(&fs_info->scrub_lock);
1921 wake_up(&fs_info->scrub_pause_wait);
a2de733c
AJ
1922
1923 /*
1924 * collect all data csums for the stripe to avoid seeking during
1925 * the scrub. This might currently (crc32) end up to be about 1MB
1926 */
e7786c3a 1927 blk_start_plug(&plug);
a2de733c 1928
a2de733c
AJ
1929 /*
1930 * now find all extents for each stripe and scrub them
1931 */
7a26285e
AJ
1932 logical = base + offset;
1933 physical = map->stripes[num].physical;
a2de733c 1934 ret = 0;
7a26285e 1935 for (i = 0; i < nstripes; ++i) {
a2de733c
AJ
1936 /*
1937 * canceled?
1938 */
1939 if (atomic_read(&fs_info->scrub_cancel_req) ||
d9d181c1 1940 atomic_read(&sctx->cancel_req)) {
a2de733c
AJ
1941 ret = -ECANCELED;
1942 goto out;
1943 }
1944 /*
1945 * check to see if we have to pause
1946 */
1947 if (atomic_read(&fs_info->scrub_pause_req)) {
1948 /* push queued extents */
d9d181c1
SB
1949 scrub_submit(sctx);
1950 wait_event(sctx->list_wait,
1951 atomic_read(&sctx->in_flight) == 0);
a2de733c
AJ
1952 atomic_inc(&fs_info->scrubs_paused);
1953 wake_up(&fs_info->scrub_pause_wait);
1954 mutex_lock(&fs_info->scrub_lock);
1955 while (atomic_read(&fs_info->scrub_pause_req)) {
1956 mutex_unlock(&fs_info->scrub_lock);
1957 wait_event(fs_info->scrub_pause_wait,
1958 atomic_read(&fs_info->scrub_pause_req) == 0);
1959 mutex_lock(&fs_info->scrub_lock);
1960 }
1961 atomic_dec(&fs_info->scrubs_paused);
1962 mutex_unlock(&fs_info->scrub_lock);
1963 wake_up(&fs_info->scrub_pause_wait);
a2de733c
AJ
1964 }
1965
7a26285e
AJ
1966 ret = btrfs_lookup_csums_range(csum_root, logical,
1967 logical + map->stripe_len - 1,
d9d181c1 1968 &sctx->csum_list, 1);
7a26285e
AJ
1969 if (ret)
1970 goto out;
1971
a2de733c
AJ
1972 key.objectid = logical;
1973 key.type = BTRFS_EXTENT_ITEM_KEY;
1974 key.offset = (u64)0;
1975
1976 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1977 if (ret < 0)
1978 goto out;
8c51032f 1979 if (ret > 0) {
a2de733c
AJ
1980 ret = btrfs_previous_item(root, path, 0,
1981 BTRFS_EXTENT_ITEM_KEY);
1982 if (ret < 0)
1983 goto out;
8c51032f
AJ
1984 if (ret > 0) {
1985 /* there's no smaller item, so stick with the
1986 * larger one */
1987 btrfs_release_path(path);
1988 ret = btrfs_search_slot(NULL, root, &key,
1989 path, 0, 0);
1990 if (ret < 0)
1991 goto out;
1992 }
a2de733c
AJ
1993 }
1994
1995 while (1) {
1996 l = path->nodes[0];
1997 slot = path->slots[0];
1998 if (slot >= btrfs_header_nritems(l)) {
1999 ret = btrfs_next_leaf(root, path);
2000 if (ret == 0)
2001 continue;
2002 if (ret < 0)
2003 goto out;
2004
2005 break;
2006 }
2007 btrfs_item_key_to_cpu(l, &key, slot);
2008
2009 if (key.objectid + key.offset <= logical)
2010 goto next;
2011
2012 if (key.objectid >= logical + map->stripe_len)
2013 break;
2014
2015 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
2016 goto next;
2017
2018 extent = btrfs_item_ptr(l, slot,
2019 struct btrfs_extent_item);
2020 flags = btrfs_extent_flags(l, extent);
2021 generation = btrfs_extent_generation(l, extent);
2022
2023 if (key.objectid < logical &&
2024 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2025 printk(KERN_ERR
2026 "btrfs scrub: tree block %llu spanning "
2027 "stripes, ignored. logical=%llu\n",
2028 (unsigned long long)key.objectid,
2029 (unsigned long long)logical);
2030 goto next;
2031 }
2032
2033 /*
2034 * trim extent to this stripe
2035 */
2036 if (key.objectid < logical) {
2037 key.offset -= logical - key.objectid;
2038 key.objectid = logical;
2039 }
2040 if (key.objectid + key.offset >
2041 logical + map->stripe_len) {
2042 key.offset = logical + map->stripe_len -
2043 key.objectid;
2044 }
2045
d9d181c1 2046 ret = scrub_extent(sctx, key.objectid, key.offset,
a2de733c 2047 key.objectid - logical + physical,
a36cf8b8
SB
2048 scrub_dev, flags, generation,
2049 mirror_num);
a2de733c
AJ
2050 if (ret)
2051 goto out;
2052
2053next:
2054 path->slots[0]++;
2055 }
71267333 2056 btrfs_release_path(path);
a2de733c
AJ
2057 logical += increment;
2058 physical += map->stripe_len;
d9d181c1
SB
2059 spin_lock(&sctx->stat_lock);
2060 sctx->stat.last_physical = physical;
2061 spin_unlock(&sctx->stat_lock);
a2de733c
AJ
2062 }
2063 /* push queued extents */
d9d181c1 2064 scrub_submit(sctx);
a2de733c
AJ
2065
2066out:
e7786c3a 2067 blk_finish_plug(&plug);
a2de733c
AJ
2068 btrfs_free_path(path);
2069 return ret < 0 ? ret : 0;
2070}
2071
d9d181c1 2072static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
a36cf8b8
SB
2073 struct btrfs_device *scrub_dev,
2074 u64 chunk_tree, u64 chunk_objectid,
2075 u64 chunk_offset, u64 length,
2076 u64 dev_offset)
a2de733c
AJ
2077{
2078 struct btrfs_mapping_tree *map_tree =
a36cf8b8 2079 &sctx->dev_root->fs_info->mapping_tree;
a2de733c
AJ
2080 struct map_lookup *map;
2081 struct extent_map *em;
2082 int i;
2083 int ret = -EINVAL;
2084
2085 read_lock(&map_tree->map_tree.lock);
2086 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2087 read_unlock(&map_tree->map_tree.lock);
2088
2089 if (!em)
2090 return -EINVAL;
2091
2092 map = (struct map_lookup *)em->bdev;
2093 if (em->start != chunk_offset)
2094 goto out;
2095
2096 if (em->len < length)
2097 goto out;
2098
2099 for (i = 0; i < map->num_stripes; ++i) {
a36cf8b8 2100 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
859acaf1 2101 map->stripes[i].physical == dev_offset) {
a36cf8b8
SB
2102 ret = scrub_stripe(sctx, map, scrub_dev, i,
2103 chunk_offset, length);
a2de733c
AJ
2104 if (ret)
2105 goto out;
2106 }
2107 }
2108out:
2109 free_extent_map(em);
2110
2111 return ret;
2112}
2113
2114static noinline_for_stack
a36cf8b8
SB
2115int scrub_enumerate_chunks(struct scrub_ctx *sctx,
2116 struct btrfs_device *scrub_dev, u64 start, u64 end)
a2de733c
AJ
2117{
2118 struct btrfs_dev_extent *dev_extent = NULL;
2119 struct btrfs_path *path;
a36cf8b8 2120 struct btrfs_root *root = sctx->dev_root;
a2de733c
AJ
2121 struct btrfs_fs_info *fs_info = root->fs_info;
2122 u64 length;
2123 u64 chunk_tree;
2124 u64 chunk_objectid;
2125 u64 chunk_offset;
2126 int ret;
2127 int slot;
2128 struct extent_buffer *l;
2129 struct btrfs_key key;
2130 struct btrfs_key found_key;
2131 struct btrfs_block_group_cache *cache;
2132
2133 path = btrfs_alloc_path();
2134 if (!path)
2135 return -ENOMEM;
2136
2137 path->reada = 2;
2138 path->search_commit_root = 1;
2139 path->skip_locking = 1;
2140
a36cf8b8 2141 key.objectid = scrub_dev->devid;
a2de733c
AJ
2142 key.offset = 0ull;
2143 key.type = BTRFS_DEV_EXTENT_KEY;
2144
a2de733c
AJ
2145 while (1) {
2146 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2147 if (ret < 0)
8c51032f
AJ
2148 break;
2149 if (ret > 0) {
2150 if (path->slots[0] >=
2151 btrfs_header_nritems(path->nodes[0])) {
2152 ret = btrfs_next_leaf(root, path);
2153 if (ret)
2154 break;
2155 }
2156 }
a2de733c
AJ
2157
2158 l = path->nodes[0];
2159 slot = path->slots[0];
2160
2161 btrfs_item_key_to_cpu(l, &found_key, slot);
2162
a36cf8b8 2163 if (found_key.objectid != scrub_dev->devid)
a2de733c
AJ
2164 break;
2165
8c51032f 2166 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
a2de733c
AJ
2167 break;
2168
2169 if (found_key.offset >= end)
2170 break;
2171
2172 if (found_key.offset < key.offset)
2173 break;
2174
2175 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2176 length = btrfs_dev_extent_length(l, dev_extent);
2177
2178 if (found_key.offset + length <= start) {
2179 key.offset = found_key.offset + length;
71267333 2180 btrfs_release_path(path);
a2de733c
AJ
2181 continue;
2182 }
2183
2184 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2185 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2186 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2187
2188 /*
2189 * get a reference on the corresponding block group to prevent
2190 * the chunk from going away while we scrub it
2191 */
2192 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2193 if (!cache) {
2194 ret = -ENOENT;
8c51032f 2195 break;
a2de733c 2196 }
a36cf8b8 2197 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
859acaf1 2198 chunk_offset, length, found_key.offset);
a2de733c
AJ
2199 btrfs_put_block_group(cache);
2200 if (ret)
2201 break;
2202
2203 key.offset = found_key.offset + length;
71267333 2204 btrfs_release_path(path);
a2de733c
AJ
2205 }
2206
a2de733c 2207 btrfs_free_path(path);
8c51032f
AJ
2208
2209 /*
2210 * ret can still be 1 from search_slot or next_leaf,
2211 * that's not an error
2212 */
2213 return ret < 0 ? ret : 0;
a2de733c
AJ
2214}
2215
a36cf8b8
SB
2216static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2217 struct btrfs_device *scrub_dev)
a2de733c
AJ
2218{
2219 int i;
2220 u64 bytenr;
2221 u64 gen;
2222 int ret;
a36cf8b8 2223 struct btrfs_root *root = sctx->dev_root;
a2de733c 2224
79787eaa
JM
2225 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
2226 return -EIO;
2227
a2de733c
AJ
2228 gen = root->fs_info->last_trans_committed;
2229
2230 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2231 bytenr = btrfs_sb_offset(i);
a36cf8b8 2232 if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
a2de733c
AJ
2233 break;
2234
d9d181c1 2235 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
a36cf8b8
SB
2236 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
2237 NULL, 1);
a2de733c
AJ
2238 if (ret)
2239 return ret;
2240 }
d9d181c1 2241 wait_event(sctx->list_wait, atomic_read(&sctx->in_flight) == 0);
a2de733c
AJ
2242
2243 return 0;
2244}
2245
2246/*
2247 * get a reference count on fs_info->scrub_workers. start worker if necessary
2248 */
2249static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
2250{
2251 struct btrfs_fs_info *fs_info = root->fs_info;
0dc3b84a 2252 int ret = 0;
a2de733c
AJ
2253
2254 mutex_lock(&fs_info->scrub_lock);
632dd772
AJ
2255 if (fs_info->scrub_workers_refcnt == 0) {
2256 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
2257 fs_info->thread_pool_size, &fs_info->generic_worker);
2258 fs_info->scrub_workers.idle_thresh = 4;
0dc3b84a
JB
2259 ret = btrfs_start_workers(&fs_info->scrub_workers);
2260 if (ret)
2261 goto out;
632dd772 2262 }
a2de733c 2263 ++fs_info->scrub_workers_refcnt;
0dc3b84a 2264out:
a2de733c
AJ
2265 mutex_unlock(&fs_info->scrub_lock);
2266
0dc3b84a 2267 return ret;
a2de733c
AJ
2268}
2269
2270static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
2271{
2272 struct btrfs_fs_info *fs_info = root->fs_info;
2273
2274 mutex_lock(&fs_info->scrub_lock);
2275 if (--fs_info->scrub_workers_refcnt == 0)
2276 btrfs_stop_workers(&fs_info->scrub_workers);
2277 WARN_ON(fs_info->scrub_workers_refcnt < 0);
2278 mutex_unlock(&fs_info->scrub_lock);
2279}
2280
2281
2282int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
8628764e 2283 struct btrfs_scrub_progress *progress, int readonly)
a2de733c 2284{
d9d181c1 2285 struct scrub_ctx *sctx;
a2de733c
AJ
2286 struct btrfs_fs_info *fs_info = root->fs_info;
2287 int ret;
2288 struct btrfs_device *dev;
2289
7841cb28 2290 if (btrfs_fs_closing(root->fs_info))
a2de733c
AJ
2291 return -EINVAL;
2292
2293 /*
2294 * check some assumptions
2295 */
b5d67f64
SB
2296 if (root->nodesize != root->leafsize) {
2297 printk(KERN_ERR
2298 "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
2299 root->nodesize, root->leafsize);
2300 return -EINVAL;
2301 }
2302
2303 if (root->nodesize > BTRFS_STRIPE_LEN) {
2304 /*
2305 * in this case scrub is unable to calculate the checksum
2306 * the way scrub is implemented. Do not handle this
2307 * situation at all because it won't ever happen.
2308 */
2309 printk(KERN_ERR
2310 "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
2311 root->nodesize, BTRFS_STRIPE_LEN);
2312 return -EINVAL;
2313 }
2314
2315 if (root->sectorsize != PAGE_SIZE) {
2316 /* not supported for data w/o checksums */
2317 printk(KERN_ERR
2318 "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
2319 root->sectorsize, (unsigned long long)PAGE_SIZE);
a2de733c
AJ
2320 return -EINVAL;
2321 }
2322
7a9e9987
SB
2323 if (fs_info->chunk_root->nodesize >
2324 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
2325 fs_info->chunk_root->sectorsize >
2326 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
2327 /*
2328 * would exhaust the array bounds of pagev member in
2329 * struct scrub_block
2330 */
2331 pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
2332 fs_info->chunk_root->nodesize,
2333 SCRUB_MAX_PAGES_PER_BLOCK,
2334 fs_info->chunk_root->sectorsize,
2335 SCRUB_MAX_PAGES_PER_BLOCK);
2336 return -EINVAL;
2337 }
2338
a2de733c
AJ
2339 ret = scrub_workers_get(root);
2340 if (ret)
2341 return ret;
2342
2343 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2344 dev = btrfs_find_device(root, devid, NULL, NULL);
2345 if (!dev || dev->missing) {
2346 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2347 scrub_workers_put(root);
2348 return -ENODEV;
2349 }
2350 mutex_lock(&fs_info->scrub_lock);
2351
2352 if (!dev->in_fs_metadata) {
2353 mutex_unlock(&fs_info->scrub_lock);
2354 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2355 scrub_workers_put(root);
2356 return -ENODEV;
2357 }
2358
2359 if (dev->scrub_device) {
2360 mutex_unlock(&fs_info->scrub_lock);
2361 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2362 scrub_workers_put(root);
2363 return -EINPROGRESS;
2364 }
d9d181c1
SB
2365 sctx = scrub_setup_ctx(dev);
2366 if (IS_ERR(sctx)) {
a2de733c
AJ
2367 mutex_unlock(&fs_info->scrub_lock);
2368 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2369 scrub_workers_put(root);
d9d181c1 2370 return PTR_ERR(sctx);
a2de733c 2371 }
d9d181c1
SB
2372 sctx->readonly = readonly;
2373 dev->scrub_device = sctx;
a2de733c
AJ
2374
2375 atomic_inc(&fs_info->scrubs_running);
2376 mutex_unlock(&fs_info->scrub_lock);
2377 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2378
2379 down_read(&fs_info->scrub_super_lock);
a36cf8b8 2380 ret = scrub_supers(sctx, dev);
a2de733c
AJ
2381 up_read(&fs_info->scrub_super_lock);
2382
2383 if (!ret)
a36cf8b8 2384 ret = scrub_enumerate_chunks(sctx, dev, start, end);
a2de733c 2385
d9d181c1 2386 wait_event(sctx->list_wait, atomic_read(&sctx->in_flight) == 0);
a2de733c
AJ
2387 atomic_dec(&fs_info->scrubs_running);
2388 wake_up(&fs_info->scrub_pause_wait);
2389
d9d181c1 2390 wait_event(sctx->list_wait, atomic_read(&sctx->fixup_cnt) == 0);
0ef8e451 2391
a2de733c 2392 if (progress)
d9d181c1 2393 memcpy(progress, &sctx->stat, sizeof(*progress));
a2de733c
AJ
2394
2395 mutex_lock(&fs_info->scrub_lock);
2396 dev->scrub_device = NULL;
2397 mutex_unlock(&fs_info->scrub_lock);
2398
d9d181c1 2399 scrub_free_ctx(sctx);
a2de733c
AJ
2400 scrub_workers_put(root);
2401
2402 return ret;
2403}
2404
143bede5 2405void btrfs_scrub_pause(struct btrfs_root *root)
a2de733c
AJ
2406{
2407 struct btrfs_fs_info *fs_info = root->fs_info;
2408
2409 mutex_lock(&fs_info->scrub_lock);
2410 atomic_inc(&fs_info->scrub_pause_req);
2411 while (atomic_read(&fs_info->scrubs_paused) !=
2412 atomic_read(&fs_info->scrubs_running)) {
2413 mutex_unlock(&fs_info->scrub_lock);
2414 wait_event(fs_info->scrub_pause_wait,
2415 atomic_read(&fs_info->scrubs_paused) ==
2416 atomic_read(&fs_info->scrubs_running));
2417 mutex_lock(&fs_info->scrub_lock);
2418 }
2419 mutex_unlock(&fs_info->scrub_lock);
a2de733c
AJ
2420}
2421
143bede5 2422void btrfs_scrub_continue(struct btrfs_root *root)
a2de733c
AJ
2423{
2424 struct btrfs_fs_info *fs_info = root->fs_info;
2425
2426 atomic_dec(&fs_info->scrub_pause_req);
2427 wake_up(&fs_info->scrub_pause_wait);
a2de733c
AJ
2428}
2429
143bede5 2430void btrfs_scrub_pause_super(struct btrfs_root *root)
a2de733c
AJ
2431{
2432 down_write(&root->fs_info->scrub_super_lock);
a2de733c
AJ
2433}
2434
143bede5 2435void btrfs_scrub_continue_super(struct btrfs_root *root)
a2de733c
AJ
2436{
2437 up_write(&root->fs_info->scrub_super_lock);
a2de733c
AJ
2438}
2439
49b25e05 2440int __btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
a2de733c 2441{
a2de733c
AJ
2442
2443 mutex_lock(&fs_info->scrub_lock);
2444 if (!atomic_read(&fs_info->scrubs_running)) {
2445 mutex_unlock(&fs_info->scrub_lock);
2446 return -ENOTCONN;
2447 }
2448
2449 atomic_inc(&fs_info->scrub_cancel_req);
2450 while (atomic_read(&fs_info->scrubs_running)) {
2451 mutex_unlock(&fs_info->scrub_lock);
2452 wait_event(fs_info->scrub_pause_wait,
2453 atomic_read(&fs_info->scrubs_running) == 0);
2454 mutex_lock(&fs_info->scrub_lock);
2455 }
2456 atomic_dec(&fs_info->scrub_cancel_req);
2457 mutex_unlock(&fs_info->scrub_lock);
2458
2459 return 0;
2460}
2461
49b25e05
JM
2462int btrfs_scrub_cancel(struct btrfs_root *root)
2463{
2464 return __btrfs_scrub_cancel(root->fs_info);
2465}
2466
a2de733c
AJ
2467int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
2468{
2469 struct btrfs_fs_info *fs_info = root->fs_info;
d9d181c1 2470 struct scrub_ctx *sctx;
a2de733c
AJ
2471
2472 mutex_lock(&fs_info->scrub_lock);
d9d181c1
SB
2473 sctx = dev->scrub_device;
2474 if (!sctx) {
a2de733c
AJ
2475 mutex_unlock(&fs_info->scrub_lock);
2476 return -ENOTCONN;
2477 }
d9d181c1 2478 atomic_inc(&sctx->cancel_req);
a2de733c
AJ
2479 while (dev->scrub_device) {
2480 mutex_unlock(&fs_info->scrub_lock);
2481 wait_event(fs_info->scrub_pause_wait,
2482 dev->scrub_device == NULL);
2483 mutex_lock(&fs_info->scrub_lock);
2484 }
2485 mutex_unlock(&fs_info->scrub_lock);
2486
2487 return 0;
2488}
1623edeb 2489
a2de733c
AJ
2490int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
2491{
2492 struct btrfs_fs_info *fs_info = root->fs_info;
2493 struct btrfs_device *dev;
2494 int ret;
2495
2496 /*
2497 * we have to hold the device_list_mutex here so the device
2498 * does not go away in cancel_dev. FIXME: find a better solution
2499 */
2500 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2501 dev = btrfs_find_device(root, devid, NULL, NULL);
2502 if (!dev) {
2503 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2504 return -ENODEV;
2505 }
2506 ret = btrfs_scrub_cancel_dev(root, dev);
2507 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2508
2509 return ret;
2510}
2511
2512int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
2513 struct btrfs_scrub_progress *progress)
2514{
2515 struct btrfs_device *dev;
d9d181c1 2516 struct scrub_ctx *sctx = NULL;
a2de733c
AJ
2517
2518 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2519 dev = btrfs_find_device(root, devid, NULL, NULL);
2520 if (dev)
d9d181c1
SB
2521 sctx = dev->scrub_device;
2522 if (sctx)
2523 memcpy(progress, &sctx->stat, sizeof(*progress));
a2de733c
AJ
2524 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2525
d9d181c1 2526 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
a2de733c 2527}