btrfs: integrating raid-repair and scrub-fixup-nodatasum
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / btrfs / scrub.c
blobeba42e5fd5fd4119fb01e41b7858dcbc253232ef
1 /*
2 * Copyright (C) 2011 STRATO. All rights reserved.
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.
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.
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.
19 #include <linux/blkdev.h>
20 #include <linux/ratelimit.h>
21 #include "ctree.h"
22 #include "volumes.h"
23 #include "disk-io.h"
24 #include "ordered-data.h"
25 #include "transaction.h"
26 #include "backref.h"
27 #include "extent_io.h"
30 * This is only the first step towards a full-features scrub. It reads all
31 * extent and super block and verifies the checksums. In case a bad checksum
32 * is found or the extent cannot be read, good data will be written back if
33 * any can be found.
35 * Future enhancements:
36 * - To enhance the performance, better read-ahead strategies for the
37 * extent-tree can be employed.
38 * - In case an unrepairable extent is encountered, track which files are
39 * affected and report them
40 * - In case of a read error on files with nodatasum, map the file and read
41 * the extent to trigger a writeback of the good copy
42 * - track and record media errors, throw out bad devices
43 * - add a mode to also read unallocated space
44 * - make the prefetch cancellable
47 struct scrub_bio;
48 struct scrub_page;
49 struct scrub_dev;
50 static void scrub_bio_end_io(struct bio *bio, int err);
51 static void scrub_checksum(struct btrfs_work *work);
52 static int scrub_checksum_data(struct scrub_dev *sdev,
53 struct scrub_page *spag, void *buffer);
54 static int scrub_checksum_tree_block(struct scrub_dev *sdev,
55 struct scrub_page *spag, u64 logical,
56 void *buffer);
57 static int scrub_checksum_super(struct scrub_bio *sbio, void *buffer);
58 static int scrub_fixup_check(struct scrub_bio *sbio, int ix);
59 static void scrub_fixup_end_io(struct bio *bio, int err);
60 static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector,
61 struct page *page);
62 static void scrub_fixup(struct scrub_bio *sbio, int ix);
64 #define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
65 #define SCRUB_BIOS_PER_DEV 16 /* 1 MB per device in flight */
67 struct scrub_page {
68 u64 flags; /* extent flags */
69 u64 generation;
70 int mirror_num;
71 int have_csum;
72 u8 csum[BTRFS_CSUM_SIZE];
75 struct scrub_bio {
76 int index;
77 struct scrub_dev *sdev;
78 struct bio *bio;
79 int err;
80 u64 logical;
81 u64 physical;
82 struct scrub_page spag[SCRUB_PAGES_PER_BIO];
83 u64 count;
84 int next_free;
85 struct btrfs_work work;
88 struct scrub_dev {
89 struct scrub_bio *bios[SCRUB_BIOS_PER_DEV];
90 struct btrfs_device *dev;
91 int first_free;
92 int curr;
93 atomic_t in_flight;
94 atomic_t fixup_cnt;
95 spinlock_t list_lock;
96 wait_queue_head_t list_wait;
97 u16 csum_size;
98 struct list_head csum_list;
99 atomic_t cancel_req;
100 int readonly;
102 * statistics
104 struct btrfs_scrub_progress stat;
105 spinlock_t stat_lock;
108 struct scrub_fixup_nodatasum {
109 struct scrub_dev *sdev;
110 u64 logical;
111 struct btrfs_root *root;
112 struct btrfs_work work;
113 int mirror_num;
116 struct scrub_warning {
117 struct btrfs_path *path;
118 u64 extent_item_size;
119 char *scratch_buf;
120 char *msg_buf;
121 const char *errstr;
122 sector_t sector;
123 u64 logical;
124 struct btrfs_device *dev;
125 int msg_bufsize;
126 int scratch_bufsize;
129 static void scrub_free_csums(struct scrub_dev *sdev)
131 while (!list_empty(&sdev->csum_list)) {
132 struct btrfs_ordered_sum *sum;
133 sum = list_first_entry(&sdev->csum_list,
134 struct btrfs_ordered_sum, list);
135 list_del(&sum->list);
136 kfree(sum);
140 static void scrub_free_bio(struct bio *bio)
142 int i;
143 struct page *last_page = NULL;
145 if (!bio)
146 return;
148 for (i = 0; i < bio->bi_vcnt; ++i) {
149 if (bio->bi_io_vec[i].bv_page == last_page)
150 continue;
151 last_page = bio->bi_io_vec[i].bv_page;
152 __free_page(last_page);
154 bio_put(bio);
157 static noinline_for_stack void scrub_free_dev(struct scrub_dev *sdev)
159 int i;
161 if (!sdev)
162 return;
164 for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
165 struct scrub_bio *sbio = sdev->bios[i];
167 if (!sbio)
168 break;
170 scrub_free_bio(sbio->bio);
171 kfree(sbio);
174 scrub_free_csums(sdev);
175 kfree(sdev);
178 static noinline_for_stack
179 struct scrub_dev *scrub_setup_dev(struct btrfs_device *dev)
181 struct scrub_dev *sdev;
182 int i;
183 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
185 sdev = kzalloc(sizeof(*sdev), GFP_NOFS);
186 if (!sdev)
187 goto nomem;
188 sdev->dev = dev;
189 for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) {
190 struct scrub_bio *sbio;
192 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
193 if (!sbio)
194 goto nomem;
195 sdev->bios[i] = sbio;
197 sbio->index = i;
198 sbio->sdev = sdev;
199 sbio->count = 0;
200 sbio->work.func = scrub_checksum;
202 if (i != SCRUB_BIOS_PER_DEV-1)
203 sdev->bios[i]->next_free = i + 1;
204 else
205 sdev->bios[i]->next_free = -1;
207 sdev->first_free = 0;
208 sdev->curr = -1;
209 atomic_set(&sdev->in_flight, 0);
210 atomic_set(&sdev->fixup_cnt, 0);
211 atomic_set(&sdev->cancel_req, 0);
212 sdev->csum_size = btrfs_super_csum_size(&fs_info->super_copy);
213 INIT_LIST_HEAD(&sdev->csum_list);
215 spin_lock_init(&sdev->list_lock);
216 spin_lock_init(&sdev->stat_lock);
217 init_waitqueue_head(&sdev->list_wait);
218 return sdev;
220 nomem:
221 scrub_free_dev(sdev);
222 return ERR_PTR(-ENOMEM);
225 static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, void *ctx)
227 u64 isize;
228 u32 nlink;
229 int ret;
230 int i;
231 struct extent_buffer *eb;
232 struct btrfs_inode_item *inode_item;
233 struct scrub_warning *swarn = ctx;
234 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
235 struct inode_fs_paths *ipath = NULL;
236 struct btrfs_root *local_root;
237 struct btrfs_key root_key;
239 root_key.objectid = root;
240 root_key.type = BTRFS_ROOT_ITEM_KEY;
241 root_key.offset = (u64)-1;
242 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
243 if (IS_ERR(local_root)) {
244 ret = PTR_ERR(local_root);
245 goto err;
248 ret = inode_item_info(inum, 0, local_root, swarn->path);
249 if (ret) {
250 btrfs_release_path(swarn->path);
251 goto err;
254 eb = swarn->path->nodes[0];
255 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
256 struct btrfs_inode_item);
257 isize = btrfs_inode_size(eb, inode_item);
258 nlink = btrfs_inode_nlink(eb, inode_item);
259 btrfs_release_path(swarn->path);
261 ipath = init_ipath(4096, local_root, swarn->path);
262 ret = paths_from_inode(inum, ipath);
264 if (ret < 0)
265 goto err;
268 * we deliberately ignore the bit ipath might have been too small to
269 * hold all of the paths here
271 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
272 printk(KERN_WARNING "btrfs: %s at logical %llu on dev "
273 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
274 "length %llu, links %u (path: %s)\n", swarn->errstr,
275 swarn->logical, swarn->dev->name,
276 (unsigned long long)swarn->sector, root, inum, offset,
277 min(isize - offset, (u64)PAGE_SIZE), nlink,
278 ipath->fspath->str[i]);
280 free_ipath(ipath);
281 return 0;
283 err:
284 printk(KERN_WARNING "btrfs: %s at logical %llu on dev "
285 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
286 "resolving failed with ret=%d\n", swarn->errstr,
287 swarn->logical, swarn->dev->name,
288 (unsigned long long)swarn->sector, root, inum, offset, ret);
290 free_ipath(ipath);
291 return 0;
294 static void scrub_print_warning(const char *errstr, struct scrub_bio *sbio,
295 int ix)
297 struct btrfs_device *dev = sbio->sdev->dev;
298 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
299 struct btrfs_path *path;
300 struct btrfs_key found_key;
301 struct extent_buffer *eb;
302 struct btrfs_extent_item *ei;
303 struct scrub_warning swarn;
304 u32 item_size;
305 int ret;
306 u64 ref_root;
307 u8 ref_level;
308 unsigned long ptr = 0;
309 const int bufsize = 4096;
310 u64 extent_offset;
312 path = btrfs_alloc_path();
314 swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
315 swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
316 swarn.sector = (sbio->physical + ix * PAGE_SIZE) >> 9;
317 swarn.logical = sbio->logical + ix * PAGE_SIZE;
318 swarn.errstr = errstr;
319 swarn.dev = dev;
320 swarn.msg_bufsize = bufsize;
321 swarn.scratch_bufsize = bufsize;
323 if (!path || !swarn.scratch_buf || !swarn.msg_buf)
324 goto out;
326 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key);
327 if (ret < 0)
328 goto out;
330 extent_offset = swarn.logical - found_key.objectid;
331 swarn.extent_item_size = found_key.offset;
333 eb = path->nodes[0];
334 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
335 item_size = btrfs_item_size_nr(eb, path->slots[0]);
337 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
338 do {
339 ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
340 &ref_root, &ref_level);
341 printk(KERN_WARNING "%s at logical %llu on dev %s, "
342 "sector %llu: metadata %s (level %d) in tree "
343 "%llu\n", errstr, swarn.logical, dev->name,
344 (unsigned long long)swarn.sector,
345 ref_level ? "node" : "leaf",
346 ret < 0 ? -1 : ref_level,
347 ret < 0 ? -1 : ref_root);
348 } while (ret != 1);
349 } else {
350 swarn.path = path;
351 iterate_extent_inodes(fs_info, path, found_key.objectid,
352 extent_offset,
353 scrub_print_warning_inode, &swarn);
356 out:
357 btrfs_free_path(path);
358 kfree(swarn.scratch_buf);
359 kfree(swarn.msg_buf);
362 static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *ctx)
364 struct page *page = NULL;
365 unsigned long index;
366 struct scrub_fixup_nodatasum *fixup = ctx;
367 int ret;
368 int corrected = 0;
369 struct btrfs_key key;
370 struct inode *inode = NULL;
371 u64 end = offset + PAGE_SIZE - 1;
372 struct btrfs_root *local_root;
374 key.objectid = root;
375 key.type = BTRFS_ROOT_ITEM_KEY;
376 key.offset = (u64)-1;
377 local_root = btrfs_read_fs_root_no_name(fixup->root->fs_info, &key);
378 if (IS_ERR(local_root))
379 return PTR_ERR(local_root);
381 key.type = BTRFS_INODE_ITEM_KEY;
382 key.objectid = inum;
383 key.offset = 0;
384 inode = btrfs_iget(fixup->root->fs_info->sb, &key, local_root, NULL);
385 if (IS_ERR(inode))
386 return PTR_ERR(inode);
388 index = offset >> PAGE_CACHE_SHIFT;
390 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
391 if (!page) {
392 ret = -ENOMEM;
393 goto out;
396 if (PageUptodate(page)) {
397 struct btrfs_mapping_tree *map_tree;
398 if (PageDirty(page)) {
400 * we need to write the data to the defect sector. the
401 * data that was in that sector is not in memory,
402 * because the page was modified. we must not write the
403 * modified page to that sector.
405 * TODO: what could be done here: wait for the delalloc
406 * runner to write out that page (might involve
407 * COW) and see whether the sector is still
408 * referenced afterwards.
410 * For the meantime, we'll treat this error
411 * incorrectable, although there is a chance that a
412 * later scrub will find the bad sector again and that
413 * there's no dirty page in memory, then.
415 ret = -EIO;
416 goto out;
418 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
419 ret = repair_io_failure(map_tree, offset, PAGE_SIZE,
420 fixup->logical, page,
421 fixup->mirror_num);
422 unlock_page(page);
423 corrected = !ret;
424 } else {
426 * we need to get good data first. the general readpage path
427 * will call repair_io_failure for us, we just have to make
428 * sure we read the bad mirror.
430 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
431 EXTENT_DAMAGED, GFP_NOFS);
432 if (ret) {
433 /* set_extent_bits should give proper error */
434 WARN_ON(ret > 0);
435 if (ret > 0)
436 ret = -EFAULT;
437 goto out;
440 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
441 btrfs_get_extent,
442 fixup->mirror_num);
443 wait_on_page_locked(page);
445 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
446 end, EXTENT_DAMAGED, 0, NULL);
447 if (!corrected)
448 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
449 EXTENT_DAMAGED, GFP_NOFS);
452 out:
453 if (page)
454 put_page(page);
455 if (inode)
456 iput(inode);
458 if (ret < 0)
459 return ret;
461 if (ret == 0 && corrected) {
463 * we only need to call readpage for one of the inodes belonging
464 * to this extent. so make iterate_extent_inodes stop
466 return 1;
469 return -EIO;
472 static void scrub_fixup_nodatasum(struct btrfs_work *work)
474 int ret;
475 struct scrub_fixup_nodatasum *fixup;
476 struct scrub_dev *sdev;
477 struct btrfs_trans_handle *trans = NULL;
478 struct btrfs_fs_info *fs_info;
479 struct btrfs_path *path;
480 int uncorrectable = 0;
482 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
483 sdev = fixup->sdev;
484 fs_info = fixup->root->fs_info;
486 path = btrfs_alloc_path();
487 if (!path) {
488 spin_lock(&sdev->stat_lock);
489 ++sdev->stat.malloc_errors;
490 spin_unlock(&sdev->stat_lock);
491 uncorrectable = 1;
492 goto out;
495 trans = btrfs_join_transaction(fixup->root);
496 if (IS_ERR(trans)) {
497 uncorrectable = 1;
498 goto out;
502 * the idea is to trigger a regular read through the standard path. we
503 * read a page from the (failed) logical address by specifying the
504 * corresponding copynum of the failed sector. thus, that readpage is
505 * expected to fail.
506 * that is the point where on-the-fly error correction will kick in
507 * (once it's finished) and rewrite the failed sector if a good copy
508 * can be found.
510 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
511 path, scrub_fixup_readpage,
512 fixup);
513 if (ret < 0) {
514 uncorrectable = 1;
515 goto out;
517 WARN_ON(ret != 1);
519 spin_lock(&sdev->stat_lock);
520 ++sdev->stat.corrected_errors;
521 spin_unlock(&sdev->stat_lock);
523 out:
524 if (trans && !IS_ERR(trans))
525 btrfs_end_transaction(trans, fixup->root);
526 if (uncorrectable) {
527 spin_lock(&sdev->stat_lock);
528 ++sdev->stat.uncorrectable_errors;
529 spin_unlock(&sdev->stat_lock);
530 printk_ratelimited(KERN_ERR "btrfs: unable to fixup "
531 "(nodatasum) error at logical %llu\n",
532 fixup->logical);
535 btrfs_free_path(path);
536 kfree(fixup);
538 /* see caller why we're pretending to be paused in the scrub counters */
539 mutex_lock(&fs_info->scrub_lock);
540 atomic_dec(&fs_info->scrubs_running);
541 atomic_dec(&fs_info->scrubs_paused);
542 mutex_unlock(&fs_info->scrub_lock);
543 atomic_dec(&sdev->fixup_cnt);
544 wake_up(&fs_info->scrub_pause_wait);
545 wake_up(&sdev->list_wait);
549 * scrub_recheck_error gets called when either verification of the page
550 * failed or the bio failed to read, e.g. with EIO. In the latter case,
551 * recheck_error gets called for every page in the bio, even though only
552 * one may be bad
554 static int scrub_recheck_error(struct scrub_bio *sbio, int ix)
556 struct scrub_dev *sdev = sbio->sdev;
557 u64 sector = (sbio->physical + ix * PAGE_SIZE) >> 9;
558 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
559 DEFAULT_RATELIMIT_BURST);
561 if (sbio->err) {
562 if (scrub_fixup_io(READ, sbio->sdev->dev->bdev, sector,
563 sbio->bio->bi_io_vec[ix].bv_page) == 0) {
564 if (scrub_fixup_check(sbio, ix) == 0)
565 return 0;
567 if (__ratelimit(&_rs))
568 scrub_print_warning("i/o error", sbio, ix);
569 } else {
570 if (__ratelimit(&_rs))
571 scrub_print_warning("checksum error", sbio, ix);
574 spin_lock(&sdev->stat_lock);
575 ++sdev->stat.read_errors;
576 spin_unlock(&sdev->stat_lock);
578 scrub_fixup(sbio, ix);
579 return 1;
582 static int scrub_fixup_check(struct scrub_bio *sbio, int ix)
584 int ret = 1;
585 struct page *page;
586 void *buffer;
587 u64 flags = sbio->spag[ix].flags;
589 page = sbio->bio->bi_io_vec[ix].bv_page;
590 buffer = kmap_atomic(page, KM_USER0);
591 if (flags & BTRFS_EXTENT_FLAG_DATA) {
592 ret = scrub_checksum_data(sbio->sdev,
593 sbio->spag + ix, buffer);
594 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
595 ret = scrub_checksum_tree_block(sbio->sdev,
596 sbio->spag + ix,
597 sbio->logical + ix * PAGE_SIZE,
598 buffer);
599 } else {
600 WARN_ON(1);
602 kunmap_atomic(buffer, KM_USER0);
604 return ret;
607 static void scrub_fixup_end_io(struct bio *bio, int err)
609 complete((struct completion *)bio->bi_private);
612 static void scrub_fixup(struct scrub_bio *sbio, int ix)
614 struct scrub_dev *sdev = sbio->sdev;
615 struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
616 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
617 struct btrfs_bio *bbio = NULL;
618 struct scrub_fixup_nodatasum *fixup;
619 u64 logical = sbio->logical + ix * PAGE_SIZE;
620 u64 length;
621 int i;
622 int ret;
623 DECLARE_COMPLETION_ONSTACK(complete);
625 if ((sbio->spag[ix].flags & BTRFS_EXTENT_FLAG_DATA) &&
626 (sbio->spag[ix].have_csum == 0)) {
627 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
628 if (!fixup)
629 goto uncorrectable;
630 fixup->sdev = sdev;
631 fixup->logical = logical;
632 fixup->root = fs_info->extent_root;
633 fixup->mirror_num = sbio->spag[ix].mirror_num;
635 * increment scrubs_running to prevent cancel requests from
636 * completing as long as a fixup worker is running. we must also
637 * increment scrubs_paused to prevent deadlocking on pause
638 * requests used for transactions commits (as the worker uses a
639 * transaction context). it is safe to regard the fixup worker
640 * as paused for all matters practical. effectively, we only
641 * avoid cancellation requests from completing.
643 mutex_lock(&fs_info->scrub_lock);
644 atomic_inc(&fs_info->scrubs_running);
645 atomic_inc(&fs_info->scrubs_paused);
646 mutex_unlock(&fs_info->scrub_lock);
647 atomic_inc(&sdev->fixup_cnt);
648 fixup->work.func = scrub_fixup_nodatasum;
649 btrfs_queue_worker(&fs_info->scrub_workers, &fixup->work);
650 return;
653 length = PAGE_SIZE;
654 ret = btrfs_map_block(map_tree, REQ_WRITE, logical, &length,
655 &bbio, 0);
656 if (ret || !bbio || length < PAGE_SIZE) {
657 printk(KERN_ERR
658 "scrub_fixup: btrfs_map_block failed us for %llu\n",
659 (unsigned long long)logical);
660 WARN_ON(1);
661 return;
664 if (bbio->num_stripes == 1)
665 /* there aren't any replicas */
666 goto uncorrectable;
669 * first find a good copy
671 for (i = 0; i < bbio->num_stripes; ++i) {
672 if (i + 1 == sbio->spag[ix].mirror_num)
673 continue;
675 if (scrub_fixup_io(READ, bbio->stripes[i].dev->bdev,
676 bbio->stripes[i].physical >> 9,
677 sbio->bio->bi_io_vec[ix].bv_page)) {
678 /* I/O-error, this is not a good copy */
679 continue;
682 if (scrub_fixup_check(sbio, ix) == 0)
683 break;
685 if (i == bbio->num_stripes)
686 goto uncorrectable;
688 if (!sdev->readonly) {
690 * bi_io_vec[ix].bv_page now contains good data, write it back
692 if (scrub_fixup_io(WRITE, sdev->dev->bdev,
693 (sbio->physical + ix * PAGE_SIZE) >> 9,
694 sbio->bio->bi_io_vec[ix].bv_page)) {
695 /* I/O-error, writeback failed, give up */
696 goto uncorrectable;
700 kfree(bbio);
701 spin_lock(&sdev->stat_lock);
702 ++sdev->stat.corrected_errors;
703 spin_unlock(&sdev->stat_lock);
705 printk_ratelimited(KERN_ERR "btrfs: fixed up error at logical %llu\n",
706 (unsigned long long)logical);
707 return;
709 uncorrectable:
710 kfree(bbio);
711 spin_lock(&sdev->stat_lock);
712 ++sdev->stat.uncorrectable_errors;
713 spin_unlock(&sdev->stat_lock);
715 printk_ratelimited(KERN_ERR "btrfs: unable to fixup (regular) error at "
716 "logical %llu\n", (unsigned long long)logical);
719 static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector,
720 struct page *page)
722 struct bio *bio = NULL;
723 int ret;
724 DECLARE_COMPLETION_ONSTACK(complete);
726 bio = bio_alloc(GFP_NOFS, 1);
727 bio->bi_bdev = bdev;
728 bio->bi_sector = sector;
729 bio_add_page(bio, page, PAGE_SIZE, 0);
730 bio->bi_end_io = scrub_fixup_end_io;
731 bio->bi_private = &complete;
732 submit_bio(rw, bio);
734 /* this will also unplug the queue */
735 wait_for_completion(&complete);
737 ret = !test_bit(BIO_UPTODATE, &bio->bi_flags);
738 bio_put(bio);
739 return ret;
742 static void scrub_bio_end_io(struct bio *bio, int err)
744 struct scrub_bio *sbio = bio->bi_private;
745 struct scrub_dev *sdev = sbio->sdev;
746 struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
748 sbio->err = err;
749 sbio->bio = bio;
751 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
754 static void scrub_checksum(struct btrfs_work *work)
756 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
757 struct scrub_dev *sdev = sbio->sdev;
758 struct page *page;
759 void *buffer;
760 int i;
761 u64 flags;
762 u64 logical;
763 int ret;
765 if (sbio->err) {
766 ret = 0;
767 for (i = 0; i < sbio->count; ++i)
768 ret |= scrub_recheck_error(sbio, i);
769 if (!ret) {
770 spin_lock(&sdev->stat_lock);
771 ++sdev->stat.unverified_errors;
772 spin_unlock(&sdev->stat_lock);
775 sbio->bio->bi_flags &= ~(BIO_POOL_MASK - 1);
776 sbio->bio->bi_flags |= 1 << BIO_UPTODATE;
777 sbio->bio->bi_phys_segments = 0;
778 sbio->bio->bi_idx = 0;
780 for (i = 0; i < sbio->count; i++) {
781 struct bio_vec *bi;
782 bi = &sbio->bio->bi_io_vec[i];
783 bi->bv_offset = 0;
784 bi->bv_len = PAGE_SIZE;
786 goto out;
788 for (i = 0; i < sbio->count; ++i) {
789 page = sbio->bio->bi_io_vec[i].bv_page;
790 buffer = kmap_atomic(page, KM_USER0);
791 flags = sbio->spag[i].flags;
792 logical = sbio->logical + i * PAGE_SIZE;
793 ret = 0;
794 if (flags & BTRFS_EXTENT_FLAG_DATA) {
795 ret = scrub_checksum_data(sdev, sbio->spag + i, buffer);
796 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
797 ret = scrub_checksum_tree_block(sdev, sbio->spag + i,
798 logical, buffer);
799 } else if (flags & BTRFS_EXTENT_FLAG_SUPER) {
800 BUG_ON(i);
801 (void)scrub_checksum_super(sbio, buffer);
802 } else {
803 WARN_ON(1);
805 kunmap_atomic(buffer, KM_USER0);
806 if (ret) {
807 ret = scrub_recheck_error(sbio, i);
808 if (!ret) {
809 spin_lock(&sdev->stat_lock);
810 ++sdev->stat.unverified_errors;
811 spin_unlock(&sdev->stat_lock);
816 out:
817 scrub_free_bio(sbio->bio);
818 sbio->bio = NULL;
819 spin_lock(&sdev->list_lock);
820 sbio->next_free = sdev->first_free;
821 sdev->first_free = sbio->index;
822 spin_unlock(&sdev->list_lock);
823 atomic_dec(&sdev->in_flight);
824 wake_up(&sdev->list_wait);
827 static int scrub_checksum_data(struct scrub_dev *sdev,
828 struct scrub_page *spag, void *buffer)
830 u8 csum[BTRFS_CSUM_SIZE];
831 u32 crc = ~(u32)0;
832 int fail = 0;
833 struct btrfs_root *root = sdev->dev->dev_root;
835 if (!spag->have_csum)
836 return 0;
838 crc = btrfs_csum_data(root, buffer, crc, PAGE_SIZE);
839 btrfs_csum_final(crc, csum);
840 if (memcmp(csum, spag->csum, sdev->csum_size))
841 fail = 1;
843 spin_lock(&sdev->stat_lock);
844 ++sdev->stat.data_extents_scrubbed;
845 sdev->stat.data_bytes_scrubbed += PAGE_SIZE;
846 if (fail)
847 ++sdev->stat.csum_errors;
848 spin_unlock(&sdev->stat_lock);
850 return fail;
853 static int scrub_checksum_tree_block(struct scrub_dev *sdev,
854 struct scrub_page *spag, u64 logical,
855 void *buffer)
857 struct btrfs_header *h;
858 struct btrfs_root *root = sdev->dev->dev_root;
859 struct btrfs_fs_info *fs_info = root->fs_info;
860 u8 csum[BTRFS_CSUM_SIZE];
861 u32 crc = ~(u32)0;
862 int fail = 0;
863 int crc_fail = 0;
866 * we don't use the getter functions here, as we
867 * a) don't have an extent buffer and
868 * b) the page is already kmapped
870 h = (struct btrfs_header *)buffer;
872 if (logical != le64_to_cpu(h->bytenr))
873 ++fail;
875 if (spag->generation != le64_to_cpu(h->generation))
876 ++fail;
878 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
879 ++fail;
881 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
882 BTRFS_UUID_SIZE))
883 ++fail;
885 crc = btrfs_csum_data(root, buffer + BTRFS_CSUM_SIZE, crc,
886 PAGE_SIZE - BTRFS_CSUM_SIZE);
887 btrfs_csum_final(crc, csum);
888 if (memcmp(csum, h->csum, sdev->csum_size))
889 ++crc_fail;
891 spin_lock(&sdev->stat_lock);
892 ++sdev->stat.tree_extents_scrubbed;
893 sdev->stat.tree_bytes_scrubbed += PAGE_SIZE;
894 if (crc_fail)
895 ++sdev->stat.csum_errors;
896 if (fail)
897 ++sdev->stat.verify_errors;
898 spin_unlock(&sdev->stat_lock);
900 return fail || crc_fail;
903 static int scrub_checksum_super(struct scrub_bio *sbio, void *buffer)
905 struct btrfs_super_block *s;
906 u64 logical;
907 struct scrub_dev *sdev = sbio->sdev;
908 struct btrfs_root *root = sdev->dev->dev_root;
909 struct btrfs_fs_info *fs_info = root->fs_info;
910 u8 csum[BTRFS_CSUM_SIZE];
911 u32 crc = ~(u32)0;
912 int fail = 0;
914 s = (struct btrfs_super_block *)buffer;
915 logical = sbio->logical;
917 if (logical != le64_to_cpu(s->bytenr))
918 ++fail;
920 if (sbio->spag[0].generation != le64_to_cpu(s->generation))
921 ++fail;
923 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
924 ++fail;
926 crc = btrfs_csum_data(root, buffer + BTRFS_CSUM_SIZE, crc,
927 PAGE_SIZE - BTRFS_CSUM_SIZE);
928 btrfs_csum_final(crc, csum);
929 if (memcmp(csum, s->csum, sbio->sdev->csum_size))
930 ++fail;
932 if (fail) {
934 * if we find an error in a super block, we just report it.
935 * They will get written with the next transaction commit
936 * anyway
938 spin_lock(&sdev->stat_lock);
939 ++sdev->stat.super_errors;
940 spin_unlock(&sdev->stat_lock);
943 return fail;
946 static int scrub_submit(struct scrub_dev *sdev)
948 struct scrub_bio *sbio;
949 struct bio *bio;
950 int i;
952 if (sdev->curr == -1)
953 return 0;
955 sbio = sdev->bios[sdev->curr];
957 bio = bio_alloc(GFP_NOFS, sbio->count);
958 if (!bio)
959 goto nomem;
961 bio->bi_private = sbio;
962 bio->bi_end_io = scrub_bio_end_io;
963 bio->bi_bdev = sdev->dev->bdev;
964 bio->bi_sector = sbio->physical >> 9;
966 for (i = 0; i < sbio->count; ++i) {
967 struct page *page;
968 int ret;
970 page = alloc_page(GFP_NOFS);
971 if (!page)
972 goto nomem;
974 ret = bio_add_page(bio, page, PAGE_SIZE, 0);
975 if (!ret) {
976 __free_page(page);
977 goto nomem;
981 sbio->err = 0;
982 sdev->curr = -1;
983 atomic_inc(&sdev->in_flight);
985 submit_bio(READ, bio);
987 return 0;
989 nomem:
990 scrub_free_bio(bio);
992 return -ENOMEM;
995 static int scrub_page(struct scrub_dev *sdev, u64 logical, u64 len,
996 u64 physical, u64 flags, u64 gen, int mirror_num,
997 u8 *csum, int force)
999 struct scrub_bio *sbio;
1001 again:
1003 * grab a fresh bio or wait for one to become available
1005 while (sdev->curr == -1) {
1006 spin_lock(&sdev->list_lock);
1007 sdev->curr = sdev->first_free;
1008 if (sdev->curr != -1) {
1009 sdev->first_free = sdev->bios[sdev->curr]->next_free;
1010 sdev->bios[sdev->curr]->next_free = -1;
1011 sdev->bios[sdev->curr]->count = 0;
1012 spin_unlock(&sdev->list_lock);
1013 } else {
1014 spin_unlock(&sdev->list_lock);
1015 wait_event(sdev->list_wait, sdev->first_free != -1);
1018 sbio = sdev->bios[sdev->curr];
1019 if (sbio->count == 0) {
1020 sbio->physical = physical;
1021 sbio->logical = logical;
1022 } else if (sbio->physical + sbio->count * PAGE_SIZE != physical ||
1023 sbio->logical + sbio->count * PAGE_SIZE != logical) {
1024 int ret;
1026 ret = scrub_submit(sdev);
1027 if (ret)
1028 return ret;
1029 goto again;
1031 sbio->spag[sbio->count].flags = flags;
1032 sbio->spag[sbio->count].generation = gen;
1033 sbio->spag[sbio->count].have_csum = 0;
1034 sbio->spag[sbio->count].mirror_num = mirror_num;
1035 if (csum) {
1036 sbio->spag[sbio->count].have_csum = 1;
1037 memcpy(sbio->spag[sbio->count].csum, csum, sdev->csum_size);
1039 ++sbio->count;
1040 if (sbio->count == SCRUB_PAGES_PER_BIO || force) {
1041 int ret;
1043 ret = scrub_submit(sdev);
1044 if (ret)
1045 return ret;
1048 return 0;
1051 static int scrub_find_csum(struct scrub_dev *sdev, u64 logical, u64 len,
1052 u8 *csum)
1054 struct btrfs_ordered_sum *sum = NULL;
1055 int ret = 0;
1056 unsigned long i;
1057 unsigned long num_sectors;
1058 u32 sectorsize = sdev->dev->dev_root->sectorsize;
1060 while (!list_empty(&sdev->csum_list)) {
1061 sum = list_first_entry(&sdev->csum_list,
1062 struct btrfs_ordered_sum, list);
1063 if (sum->bytenr > logical)
1064 return 0;
1065 if (sum->bytenr + sum->len > logical)
1066 break;
1068 ++sdev->stat.csum_discards;
1069 list_del(&sum->list);
1070 kfree(sum);
1071 sum = NULL;
1073 if (!sum)
1074 return 0;
1076 num_sectors = sum->len / sectorsize;
1077 for (i = 0; i < num_sectors; ++i) {
1078 if (sum->sums[i].bytenr == logical) {
1079 memcpy(csum, &sum->sums[i].sum, sdev->csum_size);
1080 ret = 1;
1081 break;
1084 if (ret && i == num_sectors - 1) {
1085 list_del(&sum->list);
1086 kfree(sum);
1088 return ret;
1091 /* scrub extent tries to collect up to 64 kB for each bio */
1092 static int scrub_extent(struct scrub_dev *sdev, u64 logical, u64 len,
1093 u64 physical, u64 flags, u64 gen, int mirror_num)
1095 int ret;
1096 u8 csum[BTRFS_CSUM_SIZE];
1098 while (len) {
1099 u64 l = min_t(u64, len, PAGE_SIZE);
1100 int have_csum = 0;
1102 if (flags & BTRFS_EXTENT_FLAG_DATA) {
1103 /* push csums to sbio */
1104 have_csum = scrub_find_csum(sdev, logical, l, csum);
1105 if (have_csum == 0)
1106 ++sdev->stat.no_csum;
1108 ret = scrub_page(sdev, logical, l, physical, flags, gen,
1109 mirror_num, have_csum ? csum : NULL, 0);
1110 if (ret)
1111 return ret;
1112 len -= l;
1113 logical += l;
1114 physical += l;
1116 return 0;
1119 static noinline_for_stack int scrub_stripe(struct scrub_dev *sdev,
1120 struct map_lookup *map, int num, u64 base, u64 length)
1122 struct btrfs_path *path;
1123 struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info;
1124 struct btrfs_root *root = fs_info->extent_root;
1125 struct btrfs_root *csum_root = fs_info->csum_root;
1126 struct btrfs_extent_item *extent;
1127 struct blk_plug plug;
1128 u64 flags;
1129 int ret;
1130 int slot;
1131 int i;
1132 u64 nstripes;
1133 int start_stripe;
1134 struct extent_buffer *l;
1135 struct btrfs_key key;
1136 u64 physical;
1137 u64 logical;
1138 u64 generation;
1139 int mirror_num;
1141 u64 increment = map->stripe_len;
1142 u64 offset;
1144 nstripes = length;
1145 offset = 0;
1146 do_div(nstripes, map->stripe_len);
1147 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1148 offset = map->stripe_len * num;
1149 increment = map->stripe_len * map->num_stripes;
1150 mirror_num = 1;
1151 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1152 int factor = map->num_stripes / map->sub_stripes;
1153 offset = map->stripe_len * (num / map->sub_stripes);
1154 increment = map->stripe_len * factor;
1155 mirror_num = num % map->sub_stripes + 1;
1156 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1157 increment = map->stripe_len;
1158 mirror_num = num % map->num_stripes + 1;
1159 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1160 increment = map->stripe_len;
1161 mirror_num = num % map->num_stripes + 1;
1162 } else {
1163 increment = map->stripe_len;
1164 mirror_num = 1;
1167 path = btrfs_alloc_path();
1168 if (!path)
1169 return -ENOMEM;
1171 path->reada = 2;
1172 path->search_commit_root = 1;
1173 path->skip_locking = 1;
1176 * find all extents for each stripe and just read them to get
1177 * them into the page cache
1178 * FIXME: we can do better. build a more intelligent prefetching
1180 logical = base + offset;
1181 physical = map->stripes[num].physical;
1182 ret = 0;
1183 for (i = 0; i < nstripes; ++i) {
1184 key.objectid = logical;
1185 key.type = BTRFS_EXTENT_ITEM_KEY;
1186 key.offset = (u64)0;
1188 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1189 if (ret < 0)
1190 goto out_noplug;
1193 * we might miss half an extent here, but that doesn't matter,
1194 * as it's only the prefetch
1196 while (1) {
1197 l = path->nodes[0];
1198 slot = path->slots[0];
1199 if (slot >= btrfs_header_nritems(l)) {
1200 ret = btrfs_next_leaf(root, path);
1201 if (ret == 0)
1202 continue;
1203 if (ret < 0)
1204 goto out_noplug;
1206 break;
1208 btrfs_item_key_to_cpu(l, &key, slot);
1210 if (key.objectid >= logical + map->stripe_len)
1211 break;
1213 path->slots[0]++;
1215 btrfs_release_path(path);
1216 logical += increment;
1217 physical += map->stripe_len;
1218 cond_resched();
1222 * collect all data csums for the stripe to avoid seeking during
1223 * the scrub. This might currently (crc32) end up to be about 1MB
1225 start_stripe = 0;
1226 blk_start_plug(&plug);
1227 again:
1228 logical = base + offset + start_stripe * increment;
1229 for (i = start_stripe; i < nstripes; ++i) {
1230 ret = btrfs_lookup_csums_range(csum_root, logical,
1231 logical + map->stripe_len - 1,
1232 &sdev->csum_list, 1);
1233 if (ret)
1234 goto out;
1236 logical += increment;
1237 cond_resched();
1240 * now find all extents for each stripe and scrub them
1242 logical = base + offset + start_stripe * increment;
1243 physical = map->stripes[num].physical + start_stripe * map->stripe_len;
1244 ret = 0;
1245 for (i = start_stripe; i < nstripes; ++i) {
1247 * canceled?
1249 if (atomic_read(&fs_info->scrub_cancel_req) ||
1250 atomic_read(&sdev->cancel_req)) {
1251 ret = -ECANCELED;
1252 goto out;
1255 * check to see if we have to pause
1257 if (atomic_read(&fs_info->scrub_pause_req)) {
1258 /* push queued extents */
1259 scrub_submit(sdev);
1260 wait_event(sdev->list_wait,
1261 atomic_read(&sdev->in_flight) == 0);
1262 atomic_inc(&fs_info->scrubs_paused);
1263 wake_up(&fs_info->scrub_pause_wait);
1264 mutex_lock(&fs_info->scrub_lock);
1265 while (atomic_read(&fs_info->scrub_pause_req)) {
1266 mutex_unlock(&fs_info->scrub_lock);
1267 wait_event(fs_info->scrub_pause_wait,
1268 atomic_read(&fs_info->scrub_pause_req) == 0);
1269 mutex_lock(&fs_info->scrub_lock);
1271 atomic_dec(&fs_info->scrubs_paused);
1272 mutex_unlock(&fs_info->scrub_lock);
1273 wake_up(&fs_info->scrub_pause_wait);
1274 scrub_free_csums(sdev);
1275 start_stripe = i;
1276 goto again;
1279 key.objectid = logical;
1280 key.type = BTRFS_EXTENT_ITEM_KEY;
1281 key.offset = (u64)0;
1283 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1284 if (ret < 0)
1285 goto out;
1286 if (ret > 0) {
1287 ret = btrfs_previous_item(root, path, 0,
1288 BTRFS_EXTENT_ITEM_KEY);
1289 if (ret < 0)
1290 goto out;
1291 if (ret > 0) {
1292 /* there's no smaller item, so stick with the
1293 * larger one */
1294 btrfs_release_path(path);
1295 ret = btrfs_search_slot(NULL, root, &key,
1296 path, 0, 0);
1297 if (ret < 0)
1298 goto out;
1302 while (1) {
1303 l = path->nodes[0];
1304 slot = path->slots[0];
1305 if (slot >= btrfs_header_nritems(l)) {
1306 ret = btrfs_next_leaf(root, path);
1307 if (ret == 0)
1308 continue;
1309 if (ret < 0)
1310 goto out;
1312 break;
1314 btrfs_item_key_to_cpu(l, &key, slot);
1316 if (key.objectid + key.offset <= logical)
1317 goto next;
1319 if (key.objectid >= logical + map->stripe_len)
1320 break;
1322 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1323 goto next;
1325 extent = btrfs_item_ptr(l, slot,
1326 struct btrfs_extent_item);
1327 flags = btrfs_extent_flags(l, extent);
1328 generation = btrfs_extent_generation(l, extent);
1330 if (key.objectid < logical &&
1331 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
1332 printk(KERN_ERR
1333 "btrfs scrub: tree block %llu spanning "
1334 "stripes, ignored. logical=%llu\n",
1335 (unsigned long long)key.objectid,
1336 (unsigned long long)logical);
1337 goto next;
1341 * trim extent to this stripe
1343 if (key.objectid < logical) {
1344 key.offset -= logical - key.objectid;
1345 key.objectid = logical;
1347 if (key.objectid + key.offset >
1348 logical + map->stripe_len) {
1349 key.offset = logical + map->stripe_len -
1350 key.objectid;
1353 ret = scrub_extent(sdev, key.objectid, key.offset,
1354 key.objectid - logical + physical,
1355 flags, generation, mirror_num);
1356 if (ret)
1357 goto out;
1359 next:
1360 path->slots[0]++;
1362 btrfs_release_path(path);
1363 logical += increment;
1364 physical += map->stripe_len;
1365 spin_lock(&sdev->stat_lock);
1366 sdev->stat.last_physical = physical;
1367 spin_unlock(&sdev->stat_lock);
1369 /* push queued extents */
1370 scrub_submit(sdev);
1372 out:
1373 blk_finish_plug(&plug);
1374 out_noplug:
1375 btrfs_free_path(path);
1376 return ret < 0 ? ret : 0;
1379 static noinline_for_stack int scrub_chunk(struct scrub_dev *sdev,
1380 u64 chunk_tree, u64 chunk_objectid, u64 chunk_offset, u64 length)
1382 struct btrfs_mapping_tree *map_tree =
1383 &sdev->dev->dev_root->fs_info->mapping_tree;
1384 struct map_lookup *map;
1385 struct extent_map *em;
1386 int i;
1387 int ret = -EINVAL;
1389 read_lock(&map_tree->map_tree.lock);
1390 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
1391 read_unlock(&map_tree->map_tree.lock);
1393 if (!em)
1394 return -EINVAL;
1396 map = (struct map_lookup *)em->bdev;
1397 if (em->start != chunk_offset)
1398 goto out;
1400 if (em->len < length)
1401 goto out;
1403 for (i = 0; i < map->num_stripes; ++i) {
1404 if (map->stripes[i].dev == sdev->dev) {
1405 ret = scrub_stripe(sdev, map, i, chunk_offset, length);
1406 if (ret)
1407 goto out;
1410 out:
1411 free_extent_map(em);
1413 return ret;
1416 static noinline_for_stack
1417 int scrub_enumerate_chunks(struct scrub_dev *sdev, u64 start, u64 end)
1419 struct btrfs_dev_extent *dev_extent = NULL;
1420 struct btrfs_path *path;
1421 struct btrfs_root *root = sdev->dev->dev_root;
1422 struct btrfs_fs_info *fs_info = root->fs_info;
1423 u64 length;
1424 u64 chunk_tree;
1425 u64 chunk_objectid;
1426 u64 chunk_offset;
1427 int ret;
1428 int slot;
1429 struct extent_buffer *l;
1430 struct btrfs_key key;
1431 struct btrfs_key found_key;
1432 struct btrfs_block_group_cache *cache;
1434 path = btrfs_alloc_path();
1435 if (!path)
1436 return -ENOMEM;
1438 path->reada = 2;
1439 path->search_commit_root = 1;
1440 path->skip_locking = 1;
1442 key.objectid = sdev->dev->devid;
1443 key.offset = 0ull;
1444 key.type = BTRFS_DEV_EXTENT_KEY;
1447 while (1) {
1448 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1449 if (ret < 0)
1450 break;
1451 if (ret > 0) {
1452 if (path->slots[0] >=
1453 btrfs_header_nritems(path->nodes[0])) {
1454 ret = btrfs_next_leaf(root, path);
1455 if (ret)
1456 break;
1460 l = path->nodes[0];
1461 slot = path->slots[0];
1463 btrfs_item_key_to_cpu(l, &found_key, slot);
1465 if (found_key.objectid != sdev->dev->devid)
1466 break;
1468 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
1469 break;
1471 if (found_key.offset >= end)
1472 break;
1474 if (found_key.offset < key.offset)
1475 break;
1477 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1478 length = btrfs_dev_extent_length(l, dev_extent);
1480 if (found_key.offset + length <= start) {
1481 key.offset = found_key.offset + length;
1482 btrfs_release_path(path);
1483 continue;
1486 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1487 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1488 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1491 * get a reference on the corresponding block group to prevent
1492 * the chunk from going away while we scrub it
1494 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
1495 if (!cache) {
1496 ret = -ENOENT;
1497 break;
1499 ret = scrub_chunk(sdev, chunk_tree, chunk_objectid,
1500 chunk_offset, length);
1501 btrfs_put_block_group(cache);
1502 if (ret)
1503 break;
1505 key.offset = found_key.offset + length;
1506 btrfs_release_path(path);
1509 btrfs_free_path(path);
1512 * ret can still be 1 from search_slot or next_leaf,
1513 * that's not an error
1515 return ret < 0 ? ret : 0;
1518 static noinline_for_stack int scrub_supers(struct scrub_dev *sdev)
1520 int i;
1521 u64 bytenr;
1522 u64 gen;
1523 int ret;
1524 struct btrfs_device *device = sdev->dev;
1525 struct btrfs_root *root = device->dev_root;
1527 gen = root->fs_info->last_trans_committed;
1529 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1530 bytenr = btrfs_sb_offset(i);
1531 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
1532 break;
1534 ret = scrub_page(sdev, bytenr, PAGE_SIZE, bytenr,
1535 BTRFS_EXTENT_FLAG_SUPER, gen, i, NULL, 1);
1536 if (ret)
1537 return ret;
1539 wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
1541 return 0;
1545 * get a reference count on fs_info->scrub_workers. start worker if necessary
1547 static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
1549 struct btrfs_fs_info *fs_info = root->fs_info;
1551 mutex_lock(&fs_info->scrub_lock);
1552 if (fs_info->scrub_workers_refcnt == 0) {
1553 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
1554 fs_info->thread_pool_size, &fs_info->generic_worker);
1555 fs_info->scrub_workers.idle_thresh = 4;
1556 btrfs_start_workers(&fs_info->scrub_workers, 1);
1558 ++fs_info->scrub_workers_refcnt;
1559 mutex_unlock(&fs_info->scrub_lock);
1561 return 0;
1564 static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
1566 struct btrfs_fs_info *fs_info = root->fs_info;
1568 mutex_lock(&fs_info->scrub_lock);
1569 if (--fs_info->scrub_workers_refcnt == 0)
1570 btrfs_stop_workers(&fs_info->scrub_workers);
1571 WARN_ON(fs_info->scrub_workers_refcnt < 0);
1572 mutex_unlock(&fs_info->scrub_lock);
1576 int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
1577 struct btrfs_scrub_progress *progress, int readonly)
1579 struct scrub_dev *sdev;
1580 struct btrfs_fs_info *fs_info = root->fs_info;
1581 int ret;
1582 struct btrfs_device *dev;
1584 if (btrfs_fs_closing(root->fs_info))
1585 return -EINVAL;
1588 * check some assumptions
1590 if (root->sectorsize != PAGE_SIZE ||
1591 root->sectorsize != root->leafsize ||
1592 root->sectorsize != root->nodesize) {
1593 printk(KERN_ERR "btrfs_scrub: size assumptions fail\n");
1594 return -EINVAL;
1597 ret = scrub_workers_get(root);
1598 if (ret)
1599 return ret;
1601 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1602 dev = btrfs_find_device(root, devid, NULL, NULL);
1603 if (!dev || dev->missing) {
1604 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1605 scrub_workers_put(root);
1606 return -ENODEV;
1608 mutex_lock(&fs_info->scrub_lock);
1610 if (!dev->in_fs_metadata) {
1611 mutex_unlock(&fs_info->scrub_lock);
1612 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1613 scrub_workers_put(root);
1614 return -ENODEV;
1617 if (dev->scrub_device) {
1618 mutex_unlock(&fs_info->scrub_lock);
1619 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1620 scrub_workers_put(root);
1621 return -EINPROGRESS;
1623 sdev = scrub_setup_dev(dev);
1624 if (IS_ERR(sdev)) {
1625 mutex_unlock(&fs_info->scrub_lock);
1626 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1627 scrub_workers_put(root);
1628 return PTR_ERR(sdev);
1630 sdev->readonly = readonly;
1631 dev->scrub_device = sdev;
1633 atomic_inc(&fs_info->scrubs_running);
1634 mutex_unlock(&fs_info->scrub_lock);
1635 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1637 down_read(&fs_info->scrub_super_lock);
1638 ret = scrub_supers(sdev);
1639 up_read(&fs_info->scrub_super_lock);
1641 if (!ret)
1642 ret = scrub_enumerate_chunks(sdev, start, end);
1644 wait_event(sdev->list_wait, atomic_read(&sdev->in_flight) == 0);
1645 atomic_dec(&fs_info->scrubs_running);
1646 wake_up(&fs_info->scrub_pause_wait);
1648 wait_event(sdev->list_wait, atomic_read(&sdev->fixup_cnt) == 0);
1650 if (progress)
1651 memcpy(progress, &sdev->stat, sizeof(*progress));
1653 mutex_lock(&fs_info->scrub_lock);
1654 dev->scrub_device = NULL;
1655 mutex_unlock(&fs_info->scrub_lock);
1657 scrub_free_dev(sdev);
1658 scrub_workers_put(root);
1660 return ret;
1663 int btrfs_scrub_pause(struct btrfs_root *root)
1665 struct btrfs_fs_info *fs_info = root->fs_info;
1667 mutex_lock(&fs_info->scrub_lock);
1668 atomic_inc(&fs_info->scrub_pause_req);
1669 while (atomic_read(&fs_info->scrubs_paused) !=
1670 atomic_read(&fs_info->scrubs_running)) {
1671 mutex_unlock(&fs_info->scrub_lock);
1672 wait_event(fs_info->scrub_pause_wait,
1673 atomic_read(&fs_info->scrubs_paused) ==
1674 atomic_read(&fs_info->scrubs_running));
1675 mutex_lock(&fs_info->scrub_lock);
1677 mutex_unlock(&fs_info->scrub_lock);
1679 return 0;
1682 int btrfs_scrub_continue(struct btrfs_root *root)
1684 struct btrfs_fs_info *fs_info = root->fs_info;
1686 atomic_dec(&fs_info->scrub_pause_req);
1687 wake_up(&fs_info->scrub_pause_wait);
1688 return 0;
1691 int btrfs_scrub_pause_super(struct btrfs_root *root)
1693 down_write(&root->fs_info->scrub_super_lock);
1694 return 0;
1697 int btrfs_scrub_continue_super(struct btrfs_root *root)
1699 up_write(&root->fs_info->scrub_super_lock);
1700 return 0;
1703 int btrfs_scrub_cancel(struct btrfs_root *root)
1705 struct btrfs_fs_info *fs_info = root->fs_info;
1707 mutex_lock(&fs_info->scrub_lock);
1708 if (!atomic_read(&fs_info->scrubs_running)) {
1709 mutex_unlock(&fs_info->scrub_lock);
1710 return -ENOTCONN;
1713 atomic_inc(&fs_info->scrub_cancel_req);
1714 while (atomic_read(&fs_info->scrubs_running)) {
1715 mutex_unlock(&fs_info->scrub_lock);
1716 wait_event(fs_info->scrub_pause_wait,
1717 atomic_read(&fs_info->scrubs_running) == 0);
1718 mutex_lock(&fs_info->scrub_lock);
1720 atomic_dec(&fs_info->scrub_cancel_req);
1721 mutex_unlock(&fs_info->scrub_lock);
1723 return 0;
1726 int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
1728 struct btrfs_fs_info *fs_info = root->fs_info;
1729 struct scrub_dev *sdev;
1731 mutex_lock(&fs_info->scrub_lock);
1732 sdev = dev->scrub_device;
1733 if (!sdev) {
1734 mutex_unlock(&fs_info->scrub_lock);
1735 return -ENOTCONN;
1737 atomic_inc(&sdev->cancel_req);
1738 while (dev->scrub_device) {
1739 mutex_unlock(&fs_info->scrub_lock);
1740 wait_event(fs_info->scrub_pause_wait,
1741 dev->scrub_device == NULL);
1742 mutex_lock(&fs_info->scrub_lock);
1744 mutex_unlock(&fs_info->scrub_lock);
1746 return 0;
1748 int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
1750 struct btrfs_fs_info *fs_info = root->fs_info;
1751 struct btrfs_device *dev;
1752 int ret;
1755 * we have to hold the device_list_mutex here so the device
1756 * does not go away in cancel_dev. FIXME: find a better solution
1758 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1759 dev = btrfs_find_device(root, devid, NULL, NULL);
1760 if (!dev) {
1761 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1762 return -ENODEV;
1764 ret = btrfs_scrub_cancel_dev(root, dev);
1765 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1767 return ret;
1770 int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
1771 struct btrfs_scrub_progress *progress)
1773 struct btrfs_device *dev;
1774 struct scrub_dev *sdev = NULL;
1776 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1777 dev = btrfs_find_device(root, devid, NULL, NULL);
1778 if (dev)
1779 sdev = dev->scrub_device;
1780 if (sdev)
1781 memcpy(progress, &sdev->stat, sizeof(*progress));
1782 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1784 return dev ? (sdev ? 0 : -ENOTCONN) : -ENODEV;