2 * Copyright (C) 2011, 2012 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>
24 #include "ordered-data.h"
25 #include "transaction.h"
27 #include "extent_io.h"
28 #include "dev-replace.h"
29 #include "check-integrity.h"
30 #include "rcu-string.h"
34 * This is only the first step towards a full-features scrub. It reads all
35 * extent and super block and verifies the checksums. In case a bad checksum
36 * is found or the extent cannot be read, good data will be written back if
39 * Future enhancements:
40 * - In case an unrepairable extent is encountered, track which files are
41 * affected and report them
42 * - track and record media errors, throw out bad devices
43 * - add a mode to also read unallocated space
50 * the following three values only influence the performance.
51 * The last one configures the number of parallel and outstanding I/O
52 * operations. The first two values configure an upper limit for the number
53 * of (dynamically allocated) pages that are added to a bio.
55 #define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
56 #define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
57 #define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
60 * the following value times PAGE_SIZE needs to be large enough to match the
61 * largest node/leaf/sector size that shall be supported.
62 * Values larger than BTRFS_STRIPE_LEN are not supported.
64 #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
66 struct scrub_recover {
68 struct btrfs_bio *bbio;
73 struct scrub_block *sblock;
75 struct btrfs_device *dev;
76 struct list_head list;
77 u64 flags; /* extent flags */
81 u64 physical_for_dev_replace;
84 unsigned int mirror_num:8;
85 unsigned int have_csum:1;
86 unsigned int io_error:1;
88 u8 csum[BTRFS_CSUM_SIZE];
90 struct scrub_recover *recover;
95 struct scrub_ctx *sctx;
96 struct btrfs_device *dev;
101 #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
102 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
104 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
108 struct btrfs_work work;
112 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
114 atomic_t outstanding_pages;
115 atomic_t refs; /* free mem on transition to zero */
116 struct scrub_ctx *sctx;
117 struct scrub_parity *sparity;
119 unsigned int header_error:1;
120 unsigned int checksum_error:1;
121 unsigned int no_io_error_seen:1;
122 unsigned int generation_error:1; /* also sets header_error */
124 /* The following is for the data used to check parity */
125 /* It is for the data with checksum */
126 unsigned int data_corrected:1;
130 /* Used for the chunks with parity stripe such RAID5/6 */
131 struct scrub_parity {
132 struct scrub_ctx *sctx;
134 struct btrfs_device *scrub_dev;
146 struct list_head spages;
148 /* Work of parity check and repair */
149 struct btrfs_work work;
151 /* Mark the parity blocks which have data */
152 unsigned long *dbitmap;
155 * Mark the parity blocks which have data, but errors happen when
156 * read data or check data
158 unsigned long *ebitmap;
160 unsigned long bitmap[0];
163 struct scrub_wr_ctx {
164 struct scrub_bio *wr_curr_bio;
165 struct btrfs_device *tgtdev;
166 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
167 atomic_t flush_all_writes;
168 struct mutex wr_lock;
172 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
173 struct btrfs_root *dev_root;
176 atomic_t bios_in_flight;
177 atomic_t workers_pending;
178 spinlock_t list_lock;
179 wait_queue_head_t list_wait;
181 struct list_head csum_list;
184 int pages_per_rd_bio;
189 struct scrub_wr_ctx wr_ctx;
194 struct btrfs_scrub_progress stat;
195 spinlock_t stat_lock;
198 struct scrub_fixup_nodatasum {
199 struct scrub_ctx *sctx;
200 struct btrfs_device *dev;
202 struct btrfs_root *root;
203 struct btrfs_work work;
207 struct scrub_nocow_inode {
211 struct list_head list;
214 struct scrub_copy_nocow_ctx {
215 struct scrub_ctx *sctx;
219 u64 physical_for_dev_replace;
220 struct list_head inodes;
221 struct btrfs_work work;
224 struct scrub_warning {
225 struct btrfs_path *path;
226 u64 extent_item_size;
230 struct btrfs_device *dev;
233 static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
234 static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
235 static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
236 static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
237 static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
238 static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
239 struct scrub_block *sblocks_for_recheck);
240 static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
241 struct scrub_block *sblock, int is_metadata,
242 int have_csum, u8 *csum, u64 generation,
243 u16 csum_size, int retry_failed_mirror);
244 static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
245 struct scrub_block *sblock,
246 int is_metadata, int have_csum,
247 const u8 *csum, u64 generation,
249 static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
250 struct scrub_block *sblock_good);
251 static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
252 struct scrub_block *sblock_good,
253 int page_num, int force_write);
254 static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
255 static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
257 static int scrub_checksum_data(struct scrub_block *sblock);
258 static int scrub_checksum_tree_block(struct scrub_block *sblock);
259 static int scrub_checksum_super(struct scrub_block *sblock);
260 static void scrub_block_get(struct scrub_block *sblock);
261 static void scrub_block_put(struct scrub_block *sblock);
262 static void scrub_page_get(struct scrub_page *spage);
263 static void scrub_page_put(struct scrub_page *spage);
264 static void scrub_parity_get(struct scrub_parity *sparity);
265 static void scrub_parity_put(struct scrub_parity *sparity);
266 static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
267 struct scrub_page *spage);
268 static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
269 u64 physical, struct btrfs_device *dev, u64 flags,
270 u64 gen, int mirror_num, u8 *csum, int force,
271 u64 physical_for_dev_replace);
272 static void scrub_bio_end_io(struct bio *bio, int err);
273 static void scrub_bio_end_io_worker(struct btrfs_work *work);
274 static void scrub_block_complete(struct scrub_block *sblock);
275 static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
276 u64 extent_logical, u64 extent_len,
277 u64 *extent_physical,
278 struct btrfs_device **extent_dev,
279 int *extent_mirror_num);
280 static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
281 struct scrub_wr_ctx *wr_ctx,
282 struct btrfs_fs_info *fs_info,
283 struct btrfs_device *dev,
285 static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
286 static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
287 struct scrub_page *spage);
288 static void scrub_wr_submit(struct scrub_ctx *sctx);
289 static void scrub_wr_bio_end_io(struct bio *bio, int err);
290 static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
291 static int write_page_nocow(struct scrub_ctx *sctx,
292 u64 physical_for_dev_replace, struct page *page);
293 static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
294 struct scrub_copy_nocow_ctx *ctx);
295 static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
296 int mirror_num, u64 physical_for_dev_replace);
297 static void copy_nocow_pages_worker(struct btrfs_work *work);
298 static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
299 static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
302 static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
304 atomic_inc(&sctx->bios_in_flight);
307 static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
309 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
312 * Hold the scrub_lock while doing the wakeup to ensure the
313 * sctx (and its wait queue list_wait) isn't destroyed/freed
316 mutex_lock(&fs_info->scrub_lock);
317 atomic_dec(&sctx->bios_in_flight);
318 wake_up(&sctx->list_wait);
319 mutex_unlock(&fs_info->scrub_lock);
322 static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
324 while (atomic_read(&fs_info->scrub_pause_req)) {
325 mutex_unlock(&fs_info->scrub_lock);
326 wait_event(fs_info->scrub_pause_wait,
327 atomic_read(&fs_info->scrub_pause_req) == 0);
328 mutex_lock(&fs_info->scrub_lock);
332 static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
334 atomic_inc(&fs_info->scrubs_paused);
335 wake_up(&fs_info->scrub_pause_wait);
337 mutex_lock(&fs_info->scrub_lock);
338 __scrub_blocked_if_needed(fs_info);
339 atomic_dec(&fs_info->scrubs_paused);
340 mutex_unlock(&fs_info->scrub_lock);
342 wake_up(&fs_info->scrub_pause_wait);
346 * used for workers that require transaction commits (i.e., for the
349 static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
351 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
354 * increment scrubs_running to prevent cancel requests from
355 * completing as long as a worker is running. we must also
356 * increment scrubs_paused to prevent deadlocking on pause
357 * requests used for transactions commits (as the worker uses a
358 * transaction context). it is safe to regard the worker
359 * as paused for all matters practical. effectively, we only
360 * avoid cancellation requests from completing.
362 mutex_lock(&fs_info->scrub_lock);
363 atomic_inc(&fs_info->scrubs_running);
364 atomic_inc(&fs_info->scrubs_paused);
365 mutex_unlock(&fs_info->scrub_lock);
368 * check if @scrubs_running=@scrubs_paused condition
369 * inside wait_event() is not an atomic operation.
370 * which means we may inc/dec @scrub_running/paused
371 * at any time. Let's wake up @scrub_pause_wait as
372 * much as we can to let commit transaction blocked less.
374 wake_up(&fs_info->scrub_pause_wait);
376 atomic_inc(&sctx->workers_pending);
379 /* used for workers that require transaction commits */
380 static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
382 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
385 * see scrub_pending_trans_workers_inc() why we're pretending
386 * to be paused in the scrub counters
388 mutex_lock(&fs_info->scrub_lock);
389 atomic_dec(&fs_info->scrubs_running);
390 atomic_dec(&fs_info->scrubs_paused);
391 atomic_dec(&sctx->workers_pending);
392 wake_up(&fs_info->scrub_pause_wait);
394 * Hold the scrub_lock while doing the wakeup to ensure the
395 * sctx (and its wait queue list_wait) isn't destroyed/freed
398 wake_up(&sctx->list_wait);
399 mutex_unlock(&fs_info->scrub_lock);
402 static void scrub_free_csums(struct scrub_ctx *sctx)
404 while (!list_empty(&sctx->csum_list)) {
405 struct btrfs_ordered_sum *sum;
406 sum = list_first_entry(&sctx->csum_list,
407 struct btrfs_ordered_sum, list);
408 list_del(&sum->list);
413 static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
420 scrub_free_wr_ctx(&sctx->wr_ctx);
422 /* this can happen when scrub is cancelled */
423 if (sctx->curr != -1) {
424 struct scrub_bio *sbio = sctx->bios[sctx->curr];
426 for (i = 0; i < sbio->page_count; i++) {
427 WARN_ON(!sbio->pagev[i]->page);
428 scrub_block_put(sbio->pagev[i]->sblock);
433 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
434 struct scrub_bio *sbio = sctx->bios[i];
441 scrub_free_csums(sctx);
445 static noinline_for_stack
446 struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
448 struct scrub_ctx *sctx;
450 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
451 int pages_per_rd_bio;
455 * the setting of pages_per_rd_bio is correct for scrub but might
456 * be wrong for the dev_replace code where we might read from
457 * different devices in the initial huge bios. However, that
458 * code is able to correctly handle the case when adding a page
462 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
463 bio_get_nr_vecs(dev->bdev));
465 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
466 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
469 sctx->is_dev_replace = is_dev_replace;
470 sctx->pages_per_rd_bio = pages_per_rd_bio;
472 sctx->dev_root = dev->dev_root;
473 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
474 struct scrub_bio *sbio;
476 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
479 sctx->bios[i] = sbio;
483 sbio->page_count = 0;
484 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
485 scrub_bio_end_io_worker, NULL, NULL);
487 if (i != SCRUB_BIOS_PER_SCTX - 1)
488 sctx->bios[i]->next_free = i + 1;
490 sctx->bios[i]->next_free = -1;
492 sctx->first_free = 0;
493 sctx->nodesize = dev->dev_root->nodesize;
494 sctx->sectorsize = dev->dev_root->sectorsize;
495 atomic_set(&sctx->bios_in_flight, 0);
496 atomic_set(&sctx->workers_pending, 0);
497 atomic_set(&sctx->cancel_req, 0);
498 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
499 INIT_LIST_HEAD(&sctx->csum_list);
501 spin_lock_init(&sctx->list_lock);
502 spin_lock_init(&sctx->stat_lock);
503 init_waitqueue_head(&sctx->list_wait);
505 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
506 fs_info->dev_replace.tgtdev, is_dev_replace);
508 scrub_free_ctx(sctx);
514 scrub_free_ctx(sctx);
515 return ERR_PTR(-ENOMEM);
518 static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
525 struct extent_buffer *eb;
526 struct btrfs_inode_item *inode_item;
527 struct scrub_warning *swarn = warn_ctx;
528 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
529 struct inode_fs_paths *ipath = NULL;
530 struct btrfs_root *local_root;
531 struct btrfs_key root_key;
532 struct btrfs_key key;
534 root_key.objectid = root;
535 root_key.type = BTRFS_ROOT_ITEM_KEY;
536 root_key.offset = (u64)-1;
537 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
538 if (IS_ERR(local_root)) {
539 ret = PTR_ERR(local_root);
544 * this makes the path point to (inum INODE_ITEM ioff)
547 key.type = BTRFS_INODE_ITEM_KEY;
550 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
552 btrfs_release_path(swarn->path);
556 eb = swarn->path->nodes[0];
557 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
558 struct btrfs_inode_item);
559 isize = btrfs_inode_size(eb, inode_item);
560 nlink = btrfs_inode_nlink(eb, inode_item);
561 btrfs_release_path(swarn->path);
563 ipath = init_ipath(4096, local_root, swarn->path);
565 ret = PTR_ERR(ipath);
569 ret = paths_from_inode(inum, ipath);
575 * we deliberately ignore the bit ipath might have been too small to
576 * hold all of the paths here
578 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
579 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
580 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
581 "length %llu, links %u (path: %s)\n", swarn->errstr,
582 swarn->logical, rcu_str_deref(swarn->dev->name),
583 (unsigned long long)swarn->sector, root, inum, offset,
584 min(isize - offset, (u64)PAGE_SIZE), nlink,
585 (char *)(unsigned long)ipath->fspath->val[i]);
591 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
592 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
593 "resolving failed with ret=%d\n", swarn->errstr,
594 swarn->logical, rcu_str_deref(swarn->dev->name),
595 (unsigned long long)swarn->sector, root, inum, offset, ret);
601 static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
603 struct btrfs_device *dev;
604 struct btrfs_fs_info *fs_info;
605 struct btrfs_path *path;
606 struct btrfs_key found_key;
607 struct extent_buffer *eb;
608 struct btrfs_extent_item *ei;
609 struct scrub_warning swarn;
610 unsigned long ptr = 0;
618 WARN_ON(sblock->page_count < 1);
619 dev = sblock->pagev[0]->dev;
620 fs_info = sblock->sctx->dev_root->fs_info;
622 path = btrfs_alloc_path();
626 swarn.sector = (sblock->pagev[0]->physical) >> 9;
627 swarn.logical = sblock->pagev[0]->logical;
628 swarn.errstr = errstr;
631 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
636 extent_item_pos = swarn.logical - found_key.objectid;
637 swarn.extent_item_size = found_key.offset;
640 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
641 item_size = btrfs_item_size_nr(eb, path->slots[0]);
643 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
645 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
646 item_size, &ref_root,
648 printk_in_rcu(KERN_WARNING
649 "BTRFS: %s at logical %llu on dev %s, "
650 "sector %llu: metadata %s (level %d) in tree "
651 "%llu\n", errstr, swarn.logical,
652 rcu_str_deref(dev->name),
653 (unsigned long long)swarn.sector,
654 ref_level ? "node" : "leaf",
655 ret < 0 ? -1 : ref_level,
656 ret < 0 ? -1 : ref_root);
658 btrfs_release_path(path);
660 btrfs_release_path(path);
663 iterate_extent_inodes(fs_info, found_key.objectid,
665 scrub_print_warning_inode, &swarn);
669 btrfs_free_path(path);
672 static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
674 struct page *page = NULL;
676 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
679 struct btrfs_key key;
680 struct inode *inode = NULL;
681 struct btrfs_fs_info *fs_info;
682 u64 end = offset + PAGE_SIZE - 1;
683 struct btrfs_root *local_root;
687 key.type = BTRFS_ROOT_ITEM_KEY;
688 key.offset = (u64)-1;
690 fs_info = fixup->root->fs_info;
691 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
693 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
694 if (IS_ERR(local_root)) {
695 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
696 return PTR_ERR(local_root);
699 key.type = BTRFS_INODE_ITEM_KEY;
702 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
703 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
705 return PTR_ERR(inode);
707 index = offset >> PAGE_CACHE_SHIFT;
709 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
715 if (PageUptodate(page)) {
716 if (PageDirty(page)) {
718 * we need to write the data to the defect sector. the
719 * data that was in that sector is not in memory,
720 * because the page was modified. we must not write the
721 * modified page to that sector.
723 * TODO: what could be done here: wait for the delalloc
724 * runner to write out that page (might involve
725 * COW) and see whether the sector is still
726 * referenced afterwards.
728 * For the meantime, we'll treat this error
729 * incorrectable, although there is a chance that a
730 * later scrub will find the bad sector again and that
731 * there's no dirty page in memory, then.
736 ret = repair_io_failure(inode, offset, PAGE_SIZE,
737 fixup->logical, page,
738 offset - page_offset(page),
744 * we need to get good data first. the general readpage path
745 * will call repair_io_failure for us, we just have to make
746 * sure we read the bad mirror.
748 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
749 EXTENT_DAMAGED, GFP_NOFS);
751 /* set_extent_bits should give proper error */
758 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
761 wait_on_page_locked(page);
763 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
764 end, EXTENT_DAMAGED, 0, NULL);
766 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
767 EXTENT_DAMAGED, GFP_NOFS);
779 if (ret == 0 && corrected) {
781 * we only need to call readpage for one of the inodes belonging
782 * to this extent. so make iterate_extent_inodes stop
790 static void scrub_fixup_nodatasum(struct btrfs_work *work)
793 struct scrub_fixup_nodatasum *fixup;
794 struct scrub_ctx *sctx;
795 struct btrfs_trans_handle *trans = NULL;
796 struct btrfs_path *path;
797 int uncorrectable = 0;
799 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
802 path = btrfs_alloc_path();
804 spin_lock(&sctx->stat_lock);
805 ++sctx->stat.malloc_errors;
806 spin_unlock(&sctx->stat_lock);
811 trans = btrfs_join_transaction(fixup->root);
818 * the idea is to trigger a regular read through the standard path. we
819 * read a page from the (failed) logical address by specifying the
820 * corresponding copynum of the failed sector. thus, that readpage is
822 * that is the point where on-the-fly error correction will kick in
823 * (once it's finished) and rewrite the failed sector if a good copy
826 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
827 path, scrub_fixup_readpage,
835 spin_lock(&sctx->stat_lock);
836 ++sctx->stat.corrected_errors;
837 spin_unlock(&sctx->stat_lock);
840 if (trans && !IS_ERR(trans))
841 btrfs_end_transaction(trans, fixup->root);
843 spin_lock(&sctx->stat_lock);
844 ++sctx->stat.uncorrectable_errors;
845 spin_unlock(&sctx->stat_lock);
846 btrfs_dev_replace_stats_inc(
847 &sctx->dev_root->fs_info->dev_replace.
848 num_uncorrectable_read_errors);
849 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
850 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
851 fixup->logical, rcu_str_deref(fixup->dev->name));
854 btrfs_free_path(path);
857 scrub_pending_trans_workers_dec(sctx);
860 static inline void scrub_get_recover(struct scrub_recover *recover)
862 atomic_inc(&recover->refs);
865 static inline void scrub_put_recover(struct scrub_recover *recover)
867 if (atomic_dec_and_test(&recover->refs)) {
868 btrfs_put_bbio(recover->bbio);
874 * scrub_handle_errored_block gets called when either verification of the
875 * pages failed or the bio failed to read, e.g. with EIO. In the latter
876 * case, this function handles all pages in the bio, even though only one
878 * The goal of this function is to repair the errored block by using the
879 * contents of one of the mirrors.
881 static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
883 struct scrub_ctx *sctx = sblock_to_check->sctx;
884 struct btrfs_device *dev;
885 struct btrfs_fs_info *fs_info;
889 unsigned int failed_mirror_index;
890 unsigned int is_metadata;
891 unsigned int have_csum;
893 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
894 struct scrub_block *sblock_bad;
899 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
900 DEFAULT_RATELIMIT_BURST);
902 BUG_ON(sblock_to_check->page_count < 1);
903 fs_info = sctx->dev_root->fs_info;
904 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
906 * if we find an error in a super block, we just report it.
907 * They will get written with the next transaction commit
910 spin_lock(&sctx->stat_lock);
911 ++sctx->stat.super_errors;
912 spin_unlock(&sctx->stat_lock);
915 length = sblock_to_check->page_count * PAGE_SIZE;
916 logical = sblock_to_check->pagev[0]->logical;
917 generation = sblock_to_check->pagev[0]->generation;
918 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
919 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
920 is_metadata = !(sblock_to_check->pagev[0]->flags &
921 BTRFS_EXTENT_FLAG_DATA);
922 have_csum = sblock_to_check->pagev[0]->have_csum;
923 csum = sblock_to_check->pagev[0]->csum;
924 dev = sblock_to_check->pagev[0]->dev;
926 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
927 sblocks_for_recheck = NULL;
932 * read all mirrors one after the other. This includes to
933 * re-read the extent or metadata block that failed (that was
934 * the cause that this fixup code is called) another time,
935 * page by page this time in order to know which pages
936 * caused I/O errors and which ones are good (for all mirrors).
937 * It is the goal to handle the situation when more than one
938 * mirror contains I/O errors, but the errors do not
939 * overlap, i.e. the data can be repaired by selecting the
940 * pages from those mirrors without I/O error on the
941 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
942 * would be that mirror #1 has an I/O error on the first page,
943 * the second page is good, and mirror #2 has an I/O error on
944 * the second page, but the first page is good.
945 * Then the first page of the first mirror can be repaired by
946 * taking the first page of the second mirror, and the
947 * second page of the second mirror can be repaired by
948 * copying the contents of the 2nd page of the 1st mirror.
949 * One more note: if the pages of one mirror contain I/O
950 * errors, the checksum cannot be verified. In order to get
951 * the best data for repairing, the first attempt is to find
952 * a mirror without I/O errors and with a validated checksum.
953 * Only if this is not possible, the pages are picked from
954 * mirrors with I/O errors without considering the checksum.
955 * If the latter is the case, at the end, the checksum of the
956 * repaired area is verified in order to correctly maintain
960 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
961 sizeof(*sblocks_for_recheck),
963 if (!sblocks_for_recheck) {
964 spin_lock(&sctx->stat_lock);
965 sctx->stat.malloc_errors++;
966 sctx->stat.read_errors++;
967 sctx->stat.uncorrectable_errors++;
968 spin_unlock(&sctx->stat_lock);
969 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
973 /* setup the context, map the logical blocks and alloc the pages */
974 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
976 spin_lock(&sctx->stat_lock);
977 sctx->stat.read_errors++;
978 sctx->stat.uncorrectable_errors++;
979 spin_unlock(&sctx->stat_lock);
980 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
983 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
984 sblock_bad = sblocks_for_recheck + failed_mirror_index;
986 /* build and submit the bios for the failed mirror, check checksums */
987 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
988 csum, generation, sctx->csum_size, 1);
990 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
991 sblock_bad->no_io_error_seen) {
993 * the error disappeared after reading page by page, or
994 * the area was part of a huge bio and other parts of the
995 * bio caused I/O errors, or the block layer merged several
996 * read requests into one and the error is caused by a
997 * different bio (usually one of the two latter cases is
1000 spin_lock(&sctx->stat_lock);
1001 sctx->stat.unverified_errors++;
1002 sblock_to_check->data_corrected = 1;
1003 spin_unlock(&sctx->stat_lock);
1005 if (sctx->is_dev_replace)
1006 scrub_write_block_to_dev_replace(sblock_bad);
1010 if (!sblock_bad->no_io_error_seen) {
1011 spin_lock(&sctx->stat_lock);
1012 sctx->stat.read_errors++;
1013 spin_unlock(&sctx->stat_lock);
1014 if (__ratelimit(&_rs))
1015 scrub_print_warning("i/o error", sblock_to_check);
1016 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
1017 } else if (sblock_bad->checksum_error) {
1018 spin_lock(&sctx->stat_lock);
1019 sctx->stat.csum_errors++;
1020 spin_unlock(&sctx->stat_lock);
1021 if (__ratelimit(&_rs))
1022 scrub_print_warning("checksum error", sblock_to_check);
1023 btrfs_dev_stat_inc_and_print(dev,
1024 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1025 } else if (sblock_bad->header_error) {
1026 spin_lock(&sctx->stat_lock);
1027 sctx->stat.verify_errors++;
1028 spin_unlock(&sctx->stat_lock);
1029 if (__ratelimit(&_rs))
1030 scrub_print_warning("checksum/header error",
1032 if (sblock_bad->generation_error)
1033 btrfs_dev_stat_inc_and_print(dev,
1034 BTRFS_DEV_STAT_GENERATION_ERRS);
1036 btrfs_dev_stat_inc_and_print(dev,
1037 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1040 if (sctx->readonly) {
1041 ASSERT(!sctx->is_dev_replace);
1045 if (!is_metadata && !have_csum) {
1046 struct scrub_fixup_nodatasum *fixup_nodatasum;
1048 WARN_ON(sctx->is_dev_replace);
1053 * !is_metadata and !have_csum, this means that the data
1054 * might not be COW'ed, that it might be modified
1055 * concurrently. The general strategy to work on the
1056 * commit root does not help in the case when COW is not
1059 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1060 if (!fixup_nodatasum)
1061 goto did_not_correct_error;
1062 fixup_nodatasum->sctx = sctx;
1063 fixup_nodatasum->dev = dev;
1064 fixup_nodatasum->logical = logical;
1065 fixup_nodatasum->root = fs_info->extent_root;
1066 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
1067 scrub_pending_trans_workers_inc(sctx);
1068 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1069 scrub_fixup_nodatasum, NULL, NULL);
1070 btrfs_queue_work(fs_info->scrub_workers,
1071 &fixup_nodatasum->work);
1076 * now build and submit the bios for the other mirrors, check
1078 * First try to pick the mirror which is completely without I/O
1079 * errors and also does not have a checksum error.
1080 * If one is found, and if a checksum is present, the full block
1081 * that is known to contain an error is rewritten. Afterwards
1082 * the block is known to be corrected.
1083 * If a mirror is found which is completely correct, and no
1084 * checksum is present, only those pages are rewritten that had
1085 * an I/O error in the block to be repaired, since it cannot be
1086 * determined, which copy of the other pages is better (and it
1087 * could happen otherwise that a correct page would be
1088 * overwritten by a bad one).
1090 for (mirror_index = 0;
1091 mirror_index < BTRFS_MAX_MIRRORS &&
1092 sblocks_for_recheck[mirror_index].page_count > 0;
1094 struct scrub_block *sblock_other;
1096 if (mirror_index == failed_mirror_index)
1098 sblock_other = sblocks_for_recheck + mirror_index;
1100 /* build and submit the bios, check checksums */
1101 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1102 have_csum, csum, generation,
1103 sctx->csum_size, 0);
1105 if (!sblock_other->header_error &&
1106 !sblock_other->checksum_error &&
1107 sblock_other->no_io_error_seen) {
1108 if (sctx->is_dev_replace) {
1109 scrub_write_block_to_dev_replace(sblock_other);
1110 goto corrected_error;
1112 ret = scrub_repair_block_from_good_copy(
1113 sblock_bad, sblock_other);
1115 goto corrected_error;
1120 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1121 goto did_not_correct_error;
1124 * In case of I/O errors in the area that is supposed to be
1125 * repaired, continue by picking good copies of those pages.
1126 * Select the good pages from mirrors to rewrite bad pages from
1127 * the area to fix. Afterwards verify the checksum of the block
1128 * that is supposed to be repaired. This verification step is
1129 * only done for the purpose of statistic counting and for the
1130 * final scrub report, whether errors remain.
1131 * A perfect algorithm could make use of the checksum and try
1132 * all possible combinations of pages from the different mirrors
1133 * until the checksum verification succeeds. For example, when
1134 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1135 * of mirror #2 is readable but the final checksum test fails,
1136 * then the 2nd page of mirror #3 could be tried, whether now
1137 * the final checksum succeedes. But this would be a rare
1138 * exception and is therefore not implemented. At least it is
1139 * avoided that the good copy is overwritten.
1140 * A more useful improvement would be to pick the sectors
1141 * without I/O error based on sector sizes (512 bytes on legacy
1142 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1143 * mirror could be repaired by taking 512 byte of a different
1144 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1145 * area are unreadable.
1148 for (page_num = 0; page_num < sblock_bad->page_count;
1150 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1151 struct scrub_block *sblock_other = NULL;
1153 /* skip no-io-error page in scrub */
1154 if (!page_bad->io_error && !sctx->is_dev_replace)
1157 /* try to find no-io-error page in mirrors */
1158 if (page_bad->io_error) {
1159 for (mirror_index = 0;
1160 mirror_index < BTRFS_MAX_MIRRORS &&
1161 sblocks_for_recheck[mirror_index].page_count > 0;
1163 if (!sblocks_for_recheck[mirror_index].
1164 pagev[page_num]->io_error) {
1165 sblock_other = sblocks_for_recheck +
1174 if (sctx->is_dev_replace) {
1176 * did not find a mirror to fetch the page
1177 * from. scrub_write_page_to_dev_replace()
1178 * handles this case (page->io_error), by
1179 * filling the block with zeros before
1180 * submitting the write request
1183 sblock_other = sblock_bad;
1185 if (scrub_write_page_to_dev_replace(sblock_other,
1187 btrfs_dev_replace_stats_inc(
1189 fs_info->dev_replace.
1193 } else if (sblock_other) {
1194 ret = scrub_repair_page_from_good_copy(sblock_bad,
1198 page_bad->io_error = 0;
1204 if (success && !sctx->is_dev_replace) {
1205 if (is_metadata || have_csum) {
1207 * need to verify the checksum now that all
1208 * sectors on disk are repaired (the write
1209 * request for data to be repaired is on its way).
1210 * Just be lazy and use scrub_recheck_block()
1211 * which re-reads the data before the checksum
1212 * is verified, but most likely the data comes out
1213 * of the page cache.
1215 scrub_recheck_block(fs_info, sblock_bad,
1216 is_metadata, have_csum, csum,
1217 generation, sctx->csum_size, 1);
1218 if (!sblock_bad->header_error &&
1219 !sblock_bad->checksum_error &&
1220 sblock_bad->no_io_error_seen)
1221 goto corrected_error;
1223 goto did_not_correct_error;
1226 spin_lock(&sctx->stat_lock);
1227 sctx->stat.corrected_errors++;
1228 sblock_to_check->data_corrected = 1;
1229 spin_unlock(&sctx->stat_lock);
1230 printk_ratelimited_in_rcu(KERN_ERR
1231 "BTRFS: fixed up error at logical %llu on dev %s\n",
1232 logical, rcu_str_deref(dev->name));
1235 did_not_correct_error:
1236 spin_lock(&sctx->stat_lock);
1237 sctx->stat.uncorrectable_errors++;
1238 spin_unlock(&sctx->stat_lock);
1239 printk_ratelimited_in_rcu(KERN_ERR
1240 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
1241 logical, rcu_str_deref(dev->name));
1245 if (sblocks_for_recheck) {
1246 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1248 struct scrub_block *sblock = sblocks_for_recheck +
1250 struct scrub_recover *recover;
1253 for (page_index = 0; page_index < sblock->page_count;
1255 sblock->pagev[page_index]->sblock = NULL;
1256 recover = sblock->pagev[page_index]->recover;
1258 scrub_put_recover(recover);
1259 sblock->pagev[page_index]->recover =
1262 scrub_page_put(sblock->pagev[page_index]);
1265 kfree(sblocks_for_recheck);
1271 static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
1273 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1275 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1278 return (int)bbio->num_stripes;
1281 static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1284 int nstripes, int mirror,
1290 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1292 for (i = 0; i < nstripes; i++) {
1293 if (raid_map[i] == RAID6_Q_STRIPE ||
1294 raid_map[i] == RAID5_P_STRIPE)
1297 if (logical >= raid_map[i] &&
1298 logical < raid_map[i] + mapped_length)
1303 *stripe_offset = logical - raid_map[i];
1305 /* The other RAID type */
1306 *stripe_index = mirror;
1311 static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
1312 struct scrub_block *sblocks_for_recheck)
1314 struct scrub_ctx *sctx = original_sblock->sctx;
1315 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1316 u64 length = original_sblock->page_count * PAGE_SIZE;
1317 u64 logical = original_sblock->pagev[0]->logical;
1318 struct scrub_recover *recover;
1319 struct btrfs_bio *bbio;
1330 * note: the two members refs and outstanding_pages
1331 * are not used (and not set) in the blocks that are used for
1332 * the recheck procedure
1335 while (length > 0) {
1336 sublen = min_t(u64, length, PAGE_SIZE);
1337 mapped_length = sublen;
1341 * with a length of PAGE_SIZE, each returned stripe
1342 * represents one mirror
1344 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
1345 &mapped_length, &bbio, 0, 1);
1346 if (ret || !bbio || mapped_length < sublen) {
1347 btrfs_put_bbio(bbio);
1351 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1353 btrfs_put_bbio(bbio);
1357 atomic_set(&recover->refs, 1);
1358 recover->bbio = bbio;
1359 recover->map_length = mapped_length;
1361 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
1363 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
1365 for (mirror_index = 0; mirror_index < nmirrors;
1367 struct scrub_block *sblock;
1368 struct scrub_page *page;
1370 sblock = sblocks_for_recheck + mirror_index;
1371 sblock->sctx = sctx;
1372 page = kzalloc(sizeof(*page), GFP_NOFS);
1375 spin_lock(&sctx->stat_lock);
1376 sctx->stat.malloc_errors++;
1377 spin_unlock(&sctx->stat_lock);
1378 scrub_put_recover(recover);
1381 scrub_page_get(page);
1382 sblock->pagev[page_index] = page;
1383 page->logical = logical;
1385 scrub_stripe_index_and_offset(logical,
1394 page->physical = bbio->stripes[stripe_index].physical +
1396 page->dev = bbio->stripes[stripe_index].dev;
1398 BUG_ON(page_index >= original_sblock->page_count);
1399 page->physical_for_dev_replace =
1400 original_sblock->pagev[page_index]->
1401 physical_for_dev_replace;
1402 /* for missing devices, dev->bdev is NULL */
1403 page->mirror_num = mirror_index + 1;
1404 sblock->page_count++;
1405 page->page = alloc_page(GFP_NOFS);
1409 scrub_get_recover(recover);
1410 page->recover = recover;
1412 scrub_put_recover(recover);
1421 struct scrub_bio_ret {
1422 struct completion event;
1426 static void scrub_bio_wait_endio(struct bio *bio, int error)
1428 struct scrub_bio_ret *ret = bio->bi_private;
1431 complete(&ret->event);
1434 static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1436 return page->recover &&
1437 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
1440 static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1442 struct scrub_page *page)
1444 struct scrub_bio_ret done;
1447 init_completion(&done.event);
1449 bio->bi_iter.bi_sector = page->logical >> 9;
1450 bio->bi_private = &done;
1451 bio->bi_end_io = scrub_bio_wait_endio;
1453 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
1454 page->recover->map_length,
1455 page->mirror_num, 0);
1459 wait_for_completion(&done.event);
1467 * this function will check the on disk data for checksum errors, header
1468 * errors and read I/O errors. If any I/O errors happen, the exact pages
1469 * which are errored are marked as being bad. The goal is to enable scrub
1470 * to take those pages that are not errored from all the mirrors so that
1471 * the pages that are errored in the just handled mirror can be repaired.
1473 static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1474 struct scrub_block *sblock, int is_metadata,
1475 int have_csum, u8 *csum, u64 generation,
1476 u16 csum_size, int retry_failed_mirror)
1480 sblock->no_io_error_seen = 1;
1481 sblock->header_error = 0;
1482 sblock->checksum_error = 0;
1484 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1486 struct scrub_page *page = sblock->pagev[page_num];
1488 if (page->dev->bdev == NULL) {
1490 sblock->no_io_error_seen = 0;
1494 WARN_ON(!page->page);
1495 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
1498 sblock->no_io_error_seen = 0;
1501 bio->bi_bdev = page->dev->bdev;
1503 bio_add_page(bio, page->page, PAGE_SIZE, 0);
1504 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1505 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1506 sblock->no_io_error_seen = 0;
1508 bio->bi_iter.bi_sector = page->physical >> 9;
1510 if (btrfsic_submit_bio_wait(READ, bio))
1511 sblock->no_io_error_seen = 0;
1517 if (sblock->no_io_error_seen)
1518 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1519 have_csum, csum, generation,
1525 static inline int scrub_check_fsid(u8 fsid[],
1526 struct scrub_page *spage)
1528 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1531 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1535 static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1536 struct scrub_block *sblock,
1537 int is_metadata, int have_csum,
1538 const u8 *csum, u64 generation,
1542 u8 calculated_csum[BTRFS_CSUM_SIZE];
1544 void *mapped_buffer;
1546 WARN_ON(!sblock->pagev[0]->page);
1548 struct btrfs_header *h;
1550 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
1551 h = (struct btrfs_header *)mapped_buffer;
1553 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
1554 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
1555 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1557 sblock->header_error = 1;
1558 } else if (generation != btrfs_stack_header_generation(h)) {
1559 sblock->header_error = 1;
1560 sblock->generation_error = 1;
1567 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
1570 for (page_num = 0;;) {
1571 if (page_num == 0 && is_metadata)
1572 crc = btrfs_csum_data(
1573 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1574 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1576 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
1578 kunmap_atomic(mapped_buffer);
1580 if (page_num >= sblock->page_count)
1582 WARN_ON(!sblock->pagev[page_num]->page);
1584 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
1587 btrfs_csum_final(crc, calculated_csum);
1588 if (memcmp(calculated_csum, csum, csum_size))
1589 sblock->checksum_error = 1;
1592 static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1593 struct scrub_block *sblock_good)
1598 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1601 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1611 static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1612 struct scrub_block *sblock_good,
1613 int page_num, int force_write)
1615 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1616 struct scrub_page *page_good = sblock_good->pagev[page_num];
1618 BUG_ON(page_bad->page == NULL);
1619 BUG_ON(page_good->page == NULL);
1620 if (force_write || sblock_bad->header_error ||
1621 sblock_bad->checksum_error || page_bad->io_error) {
1625 if (!page_bad->dev->bdev) {
1626 printk_ratelimited(KERN_WARNING "BTRFS: "
1627 "scrub_repair_page_from_good_copy(bdev == NULL) "
1628 "is unexpected!\n");
1632 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
1635 bio->bi_bdev = page_bad->dev->bdev;
1636 bio->bi_iter.bi_sector = page_bad->physical >> 9;
1638 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1639 if (PAGE_SIZE != ret) {
1644 if (btrfsic_submit_bio_wait(WRITE, bio)) {
1645 btrfs_dev_stat_inc_and_print(page_bad->dev,
1646 BTRFS_DEV_STAT_WRITE_ERRS);
1647 btrfs_dev_replace_stats_inc(
1648 &sblock_bad->sctx->dev_root->fs_info->
1649 dev_replace.num_write_errors);
1659 static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1664 * This block is used for the check of the parity on the source device,
1665 * so the data needn't be written into the destination device.
1667 if (sblock->sparity)
1670 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1673 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1675 btrfs_dev_replace_stats_inc(
1676 &sblock->sctx->dev_root->fs_info->dev_replace.
1681 static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1684 struct scrub_page *spage = sblock->pagev[page_num];
1686 BUG_ON(spage->page == NULL);
1687 if (spage->io_error) {
1688 void *mapped_buffer = kmap_atomic(spage->page);
1690 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1691 flush_dcache_page(spage->page);
1692 kunmap_atomic(mapped_buffer);
1694 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1697 static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1698 struct scrub_page *spage)
1700 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1701 struct scrub_bio *sbio;
1704 mutex_lock(&wr_ctx->wr_lock);
1706 if (!wr_ctx->wr_curr_bio) {
1707 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1709 if (!wr_ctx->wr_curr_bio) {
1710 mutex_unlock(&wr_ctx->wr_lock);
1713 wr_ctx->wr_curr_bio->sctx = sctx;
1714 wr_ctx->wr_curr_bio->page_count = 0;
1716 sbio = wr_ctx->wr_curr_bio;
1717 if (sbio->page_count == 0) {
1720 sbio->physical = spage->physical_for_dev_replace;
1721 sbio->logical = spage->logical;
1722 sbio->dev = wr_ctx->tgtdev;
1725 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
1727 mutex_unlock(&wr_ctx->wr_lock);
1733 bio->bi_private = sbio;
1734 bio->bi_end_io = scrub_wr_bio_end_io;
1735 bio->bi_bdev = sbio->dev->bdev;
1736 bio->bi_iter.bi_sector = sbio->physical >> 9;
1738 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1739 spage->physical_for_dev_replace ||
1740 sbio->logical + sbio->page_count * PAGE_SIZE !=
1742 scrub_wr_submit(sctx);
1746 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1747 if (ret != PAGE_SIZE) {
1748 if (sbio->page_count < 1) {
1751 mutex_unlock(&wr_ctx->wr_lock);
1754 scrub_wr_submit(sctx);
1758 sbio->pagev[sbio->page_count] = spage;
1759 scrub_page_get(spage);
1761 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1762 scrub_wr_submit(sctx);
1763 mutex_unlock(&wr_ctx->wr_lock);
1768 static void scrub_wr_submit(struct scrub_ctx *sctx)
1770 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1771 struct scrub_bio *sbio;
1773 if (!wr_ctx->wr_curr_bio)
1776 sbio = wr_ctx->wr_curr_bio;
1777 wr_ctx->wr_curr_bio = NULL;
1778 WARN_ON(!sbio->bio->bi_bdev);
1779 scrub_pending_bio_inc(sctx);
1780 /* process all writes in a single worker thread. Then the block layer
1781 * orders the requests before sending them to the driver which
1782 * doubled the write performance on spinning disks when measured
1784 btrfsic_submit_bio(WRITE, sbio->bio);
1787 static void scrub_wr_bio_end_io(struct bio *bio, int err)
1789 struct scrub_bio *sbio = bio->bi_private;
1790 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1795 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1796 scrub_wr_bio_end_io_worker, NULL, NULL);
1797 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
1800 static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1802 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1803 struct scrub_ctx *sctx = sbio->sctx;
1806 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1808 struct btrfs_dev_replace *dev_replace =
1809 &sbio->sctx->dev_root->fs_info->dev_replace;
1811 for (i = 0; i < sbio->page_count; i++) {
1812 struct scrub_page *spage = sbio->pagev[i];
1814 spage->io_error = 1;
1815 btrfs_dev_replace_stats_inc(&dev_replace->
1820 for (i = 0; i < sbio->page_count; i++)
1821 scrub_page_put(sbio->pagev[i]);
1825 scrub_pending_bio_dec(sctx);
1828 static int scrub_checksum(struct scrub_block *sblock)
1833 WARN_ON(sblock->page_count < 1);
1834 flags = sblock->pagev[0]->flags;
1836 if (flags & BTRFS_EXTENT_FLAG_DATA)
1837 ret = scrub_checksum_data(sblock);
1838 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1839 ret = scrub_checksum_tree_block(sblock);
1840 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1841 (void)scrub_checksum_super(sblock);
1845 scrub_handle_errored_block(sblock);
1850 static int scrub_checksum_data(struct scrub_block *sblock)
1852 struct scrub_ctx *sctx = sblock->sctx;
1853 u8 csum[BTRFS_CSUM_SIZE];
1862 BUG_ON(sblock->page_count < 1);
1863 if (!sblock->pagev[0]->have_csum)
1866 on_disk_csum = sblock->pagev[0]->csum;
1867 page = sblock->pagev[0]->page;
1868 buffer = kmap_atomic(page);
1870 len = sctx->sectorsize;
1873 u64 l = min_t(u64, len, PAGE_SIZE);
1875 crc = btrfs_csum_data(buffer, crc, l);
1876 kunmap_atomic(buffer);
1881 BUG_ON(index >= sblock->page_count);
1882 BUG_ON(!sblock->pagev[index]->page);
1883 page = sblock->pagev[index]->page;
1884 buffer = kmap_atomic(page);
1887 btrfs_csum_final(crc, csum);
1888 if (memcmp(csum, on_disk_csum, sctx->csum_size))
1894 static int scrub_checksum_tree_block(struct scrub_block *sblock)
1896 struct scrub_ctx *sctx = sblock->sctx;
1897 struct btrfs_header *h;
1898 struct btrfs_root *root = sctx->dev_root;
1899 struct btrfs_fs_info *fs_info = root->fs_info;
1900 u8 calculated_csum[BTRFS_CSUM_SIZE];
1901 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1903 void *mapped_buffer;
1912 BUG_ON(sblock->page_count < 1);
1913 page = sblock->pagev[0]->page;
1914 mapped_buffer = kmap_atomic(page);
1915 h = (struct btrfs_header *)mapped_buffer;
1916 memcpy(on_disk_csum, h->csum, sctx->csum_size);
1919 * we don't use the getter functions here, as we
1920 * a) don't have an extent buffer and
1921 * b) the page is already kmapped
1924 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
1927 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
1930 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
1933 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1937 len = sctx->nodesize - BTRFS_CSUM_SIZE;
1938 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1939 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1942 u64 l = min_t(u64, len, mapped_size);
1944 crc = btrfs_csum_data(p, crc, l);
1945 kunmap_atomic(mapped_buffer);
1950 BUG_ON(index >= sblock->page_count);
1951 BUG_ON(!sblock->pagev[index]->page);
1952 page = sblock->pagev[index]->page;
1953 mapped_buffer = kmap_atomic(page);
1954 mapped_size = PAGE_SIZE;
1958 btrfs_csum_final(crc, calculated_csum);
1959 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
1962 return fail || crc_fail;
1965 static int scrub_checksum_super(struct scrub_block *sblock)
1967 struct btrfs_super_block *s;
1968 struct scrub_ctx *sctx = sblock->sctx;
1969 u8 calculated_csum[BTRFS_CSUM_SIZE];
1970 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1972 void *mapped_buffer;
1981 BUG_ON(sblock->page_count < 1);
1982 page = sblock->pagev[0]->page;
1983 mapped_buffer = kmap_atomic(page);
1984 s = (struct btrfs_super_block *)mapped_buffer;
1985 memcpy(on_disk_csum, s->csum, sctx->csum_size);
1987 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
1990 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
1993 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
1996 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1997 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1998 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2001 u64 l = min_t(u64, len, mapped_size);
2003 crc = btrfs_csum_data(p, crc, l);
2004 kunmap_atomic(mapped_buffer);
2009 BUG_ON(index >= sblock->page_count);
2010 BUG_ON(!sblock->pagev[index]->page);
2011 page = sblock->pagev[index]->page;
2012 mapped_buffer = kmap_atomic(page);
2013 mapped_size = PAGE_SIZE;
2017 btrfs_csum_final(crc, calculated_csum);
2018 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
2021 if (fail_cor + fail_gen) {
2023 * if we find an error in a super block, we just report it.
2024 * They will get written with the next transaction commit
2027 spin_lock(&sctx->stat_lock);
2028 ++sctx->stat.super_errors;
2029 spin_unlock(&sctx->stat_lock);
2031 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
2032 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2034 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
2035 BTRFS_DEV_STAT_GENERATION_ERRS);
2038 return fail_cor + fail_gen;
2041 static void scrub_block_get(struct scrub_block *sblock)
2043 atomic_inc(&sblock->refs);
2046 static void scrub_block_put(struct scrub_block *sblock)
2048 if (atomic_dec_and_test(&sblock->refs)) {
2051 if (sblock->sparity)
2052 scrub_parity_put(sblock->sparity);
2054 for (i = 0; i < sblock->page_count; i++)
2055 scrub_page_put(sblock->pagev[i]);
2060 static void scrub_page_get(struct scrub_page *spage)
2062 atomic_inc(&spage->refs);
2065 static void scrub_page_put(struct scrub_page *spage)
2067 if (atomic_dec_and_test(&spage->refs)) {
2069 __free_page(spage->page);
2074 static void scrub_submit(struct scrub_ctx *sctx)
2076 struct scrub_bio *sbio;
2078 if (sctx->curr == -1)
2081 sbio = sctx->bios[sctx->curr];
2083 scrub_pending_bio_inc(sctx);
2085 if (!sbio->bio->bi_bdev) {
2087 * this case should not happen. If btrfs_map_block() is
2088 * wrong, it could happen for dev-replace operations on
2089 * missing devices when no mirrors are available, but in
2090 * this case it should already fail the mount.
2091 * This case is handled correctly (but _very_ slowly).
2093 printk_ratelimited(KERN_WARNING
2094 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
2095 bio_endio(sbio->bio, -EIO);
2097 btrfsic_submit_bio(READ, sbio->bio);
2101 static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2102 struct scrub_page *spage)
2104 struct scrub_block *sblock = spage->sblock;
2105 struct scrub_bio *sbio;
2110 * grab a fresh bio or wait for one to become available
2112 while (sctx->curr == -1) {
2113 spin_lock(&sctx->list_lock);
2114 sctx->curr = sctx->first_free;
2115 if (sctx->curr != -1) {
2116 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2117 sctx->bios[sctx->curr]->next_free = -1;
2118 sctx->bios[sctx->curr]->page_count = 0;
2119 spin_unlock(&sctx->list_lock);
2121 spin_unlock(&sctx->list_lock);
2122 wait_event(sctx->list_wait, sctx->first_free != -1);
2125 sbio = sctx->bios[sctx->curr];
2126 if (sbio->page_count == 0) {
2129 sbio->physical = spage->physical;
2130 sbio->logical = spage->logical;
2131 sbio->dev = spage->dev;
2134 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
2140 bio->bi_private = sbio;
2141 bio->bi_end_io = scrub_bio_end_io;
2142 bio->bi_bdev = sbio->dev->bdev;
2143 bio->bi_iter.bi_sector = sbio->physical >> 9;
2145 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2147 sbio->logical + sbio->page_count * PAGE_SIZE !=
2149 sbio->dev != spage->dev) {
2154 sbio->pagev[sbio->page_count] = spage;
2155 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2156 if (ret != PAGE_SIZE) {
2157 if (sbio->page_count < 1) {
2166 scrub_block_get(sblock); /* one for the page added to the bio */
2167 atomic_inc(&sblock->outstanding_pages);
2169 if (sbio->page_count == sctx->pages_per_rd_bio)
2175 static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
2176 u64 physical, struct btrfs_device *dev, u64 flags,
2177 u64 gen, int mirror_num, u8 *csum, int force,
2178 u64 physical_for_dev_replace)
2180 struct scrub_block *sblock;
2183 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2185 spin_lock(&sctx->stat_lock);
2186 sctx->stat.malloc_errors++;
2187 spin_unlock(&sctx->stat_lock);
2191 /* one ref inside this function, plus one for each page added to
2193 atomic_set(&sblock->refs, 1);
2194 sblock->sctx = sctx;
2195 sblock->no_io_error_seen = 1;
2197 for (index = 0; len > 0; index++) {
2198 struct scrub_page *spage;
2199 u64 l = min_t(u64, len, PAGE_SIZE);
2201 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2204 spin_lock(&sctx->stat_lock);
2205 sctx->stat.malloc_errors++;
2206 spin_unlock(&sctx->stat_lock);
2207 scrub_block_put(sblock);
2210 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2211 scrub_page_get(spage);
2212 sblock->pagev[index] = spage;
2213 spage->sblock = sblock;
2215 spage->flags = flags;
2216 spage->generation = gen;
2217 spage->logical = logical;
2218 spage->physical = physical;
2219 spage->physical_for_dev_replace = physical_for_dev_replace;
2220 spage->mirror_num = mirror_num;
2222 spage->have_csum = 1;
2223 memcpy(spage->csum, csum, sctx->csum_size);
2225 spage->have_csum = 0;
2227 sblock->page_count++;
2228 spage->page = alloc_page(GFP_NOFS);
2234 physical_for_dev_replace += l;
2237 WARN_ON(sblock->page_count == 0);
2238 for (index = 0; index < sblock->page_count; index++) {
2239 struct scrub_page *spage = sblock->pagev[index];
2242 ret = scrub_add_page_to_rd_bio(sctx, spage);
2244 scrub_block_put(sblock);
2252 /* last one frees, either here or in bio completion for last page */
2253 scrub_block_put(sblock);
2257 static void scrub_bio_end_io(struct bio *bio, int err)
2259 struct scrub_bio *sbio = bio->bi_private;
2260 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
2265 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
2268 static void scrub_bio_end_io_worker(struct btrfs_work *work)
2270 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
2271 struct scrub_ctx *sctx = sbio->sctx;
2274 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
2276 for (i = 0; i < sbio->page_count; i++) {
2277 struct scrub_page *spage = sbio->pagev[i];
2279 spage->io_error = 1;
2280 spage->sblock->no_io_error_seen = 0;
2284 /* now complete the scrub_block items that have all pages completed */
2285 for (i = 0; i < sbio->page_count; i++) {
2286 struct scrub_page *spage = sbio->pagev[i];
2287 struct scrub_block *sblock = spage->sblock;
2289 if (atomic_dec_and_test(&sblock->outstanding_pages))
2290 scrub_block_complete(sblock);
2291 scrub_block_put(sblock);
2296 spin_lock(&sctx->list_lock);
2297 sbio->next_free = sctx->first_free;
2298 sctx->first_free = sbio->index;
2299 spin_unlock(&sctx->list_lock);
2301 if (sctx->is_dev_replace &&
2302 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2303 mutex_lock(&sctx->wr_ctx.wr_lock);
2304 scrub_wr_submit(sctx);
2305 mutex_unlock(&sctx->wr_ctx.wr_lock);
2308 scrub_pending_bio_dec(sctx);
2311 static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2312 unsigned long *bitmap,
2317 int sectorsize = sparity->sctx->dev_root->sectorsize;
2319 if (len >= sparity->stripe_len) {
2320 bitmap_set(bitmap, 0, sparity->nsectors);
2324 start -= sparity->logic_start;
2325 offset = (int)do_div(start, sparity->stripe_len);
2326 offset /= sectorsize;
2327 nsectors = (int)len / sectorsize;
2329 if (offset + nsectors <= sparity->nsectors) {
2330 bitmap_set(bitmap, offset, nsectors);
2334 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2335 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2338 static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2341 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2344 static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2347 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2350 static void scrub_block_complete(struct scrub_block *sblock)
2354 if (!sblock->no_io_error_seen) {
2356 scrub_handle_errored_block(sblock);
2359 * if has checksum error, write via repair mechanism in
2360 * dev replace case, otherwise write here in dev replace
2363 corrupted = scrub_checksum(sblock);
2364 if (!corrupted && sblock->sctx->is_dev_replace)
2365 scrub_write_block_to_dev_replace(sblock);
2368 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2369 u64 start = sblock->pagev[0]->logical;
2370 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2373 scrub_parity_mark_sectors_error(sblock->sparity,
2374 start, end - start);
2378 static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
2381 struct btrfs_ordered_sum *sum = NULL;
2382 unsigned long index;
2383 unsigned long num_sectors;
2385 while (!list_empty(&sctx->csum_list)) {
2386 sum = list_first_entry(&sctx->csum_list,
2387 struct btrfs_ordered_sum, list);
2388 if (sum->bytenr > logical)
2390 if (sum->bytenr + sum->len > logical)
2393 ++sctx->stat.csum_discards;
2394 list_del(&sum->list);
2401 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
2402 num_sectors = sum->len / sctx->sectorsize;
2403 memcpy(csum, sum->sums + index, sctx->csum_size);
2404 if (index == num_sectors - 1) {
2405 list_del(&sum->list);
2411 /* scrub extent tries to collect up to 64 kB for each bio */
2412 static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
2413 u64 physical, struct btrfs_device *dev, u64 flags,
2414 u64 gen, int mirror_num, u64 physical_for_dev_replace)
2417 u8 csum[BTRFS_CSUM_SIZE];
2420 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2421 blocksize = sctx->sectorsize;
2422 spin_lock(&sctx->stat_lock);
2423 sctx->stat.data_extents_scrubbed++;
2424 sctx->stat.data_bytes_scrubbed += len;
2425 spin_unlock(&sctx->stat_lock);
2426 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2427 blocksize = sctx->nodesize;
2428 spin_lock(&sctx->stat_lock);
2429 sctx->stat.tree_extents_scrubbed++;
2430 sctx->stat.tree_bytes_scrubbed += len;
2431 spin_unlock(&sctx->stat_lock);
2433 blocksize = sctx->sectorsize;
2438 u64 l = min_t(u64, len, blocksize);
2441 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2442 /* push csums to sbio */
2443 have_csum = scrub_find_csum(sctx, logical, l, csum);
2445 ++sctx->stat.no_csum;
2446 if (sctx->is_dev_replace && !have_csum) {
2447 ret = copy_nocow_pages(sctx, logical, l,
2449 physical_for_dev_replace);
2450 goto behind_scrub_pages;
2453 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
2454 mirror_num, have_csum ? csum : NULL, 0,
2455 physical_for_dev_replace);
2462 physical_for_dev_replace += l;
2467 static int scrub_pages_for_parity(struct scrub_parity *sparity,
2468 u64 logical, u64 len,
2469 u64 physical, struct btrfs_device *dev,
2470 u64 flags, u64 gen, int mirror_num, u8 *csum)
2472 struct scrub_ctx *sctx = sparity->sctx;
2473 struct scrub_block *sblock;
2476 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2478 spin_lock(&sctx->stat_lock);
2479 sctx->stat.malloc_errors++;
2480 spin_unlock(&sctx->stat_lock);
2484 /* one ref inside this function, plus one for each page added to
2486 atomic_set(&sblock->refs, 1);
2487 sblock->sctx = sctx;
2488 sblock->no_io_error_seen = 1;
2489 sblock->sparity = sparity;
2490 scrub_parity_get(sparity);
2492 for (index = 0; len > 0; index++) {
2493 struct scrub_page *spage;
2494 u64 l = min_t(u64, len, PAGE_SIZE);
2496 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2499 spin_lock(&sctx->stat_lock);
2500 sctx->stat.malloc_errors++;
2501 spin_unlock(&sctx->stat_lock);
2502 scrub_block_put(sblock);
2505 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2506 /* For scrub block */
2507 scrub_page_get(spage);
2508 sblock->pagev[index] = spage;
2509 /* For scrub parity */
2510 scrub_page_get(spage);
2511 list_add_tail(&spage->list, &sparity->spages);
2512 spage->sblock = sblock;
2514 spage->flags = flags;
2515 spage->generation = gen;
2516 spage->logical = logical;
2517 spage->physical = physical;
2518 spage->mirror_num = mirror_num;
2520 spage->have_csum = 1;
2521 memcpy(spage->csum, csum, sctx->csum_size);
2523 spage->have_csum = 0;
2525 sblock->page_count++;
2526 spage->page = alloc_page(GFP_NOFS);
2534 WARN_ON(sblock->page_count == 0);
2535 for (index = 0; index < sblock->page_count; index++) {
2536 struct scrub_page *spage = sblock->pagev[index];
2539 ret = scrub_add_page_to_rd_bio(sctx, spage);
2541 scrub_block_put(sblock);
2546 /* last one frees, either here or in bio completion for last page */
2547 scrub_block_put(sblock);
2551 static int scrub_extent_for_parity(struct scrub_parity *sparity,
2552 u64 logical, u64 len,
2553 u64 physical, struct btrfs_device *dev,
2554 u64 flags, u64 gen, int mirror_num)
2556 struct scrub_ctx *sctx = sparity->sctx;
2558 u8 csum[BTRFS_CSUM_SIZE];
2561 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2562 blocksize = sctx->sectorsize;
2563 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2564 blocksize = sctx->nodesize;
2566 blocksize = sctx->sectorsize;
2571 u64 l = min_t(u64, len, blocksize);
2574 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2575 /* push csums to sbio */
2576 have_csum = scrub_find_csum(sctx, logical, l, csum);
2580 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2581 flags, gen, mirror_num,
2582 have_csum ? csum : NULL);
2594 * Given a physical address, this will calculate it's
2595 * logical offset. if this is a parity stripe, it will return
2596 * the most left data stripe's logical offset.
2598 * return 0 if it is a data stripe, 1 means parity stripe.
2600 static int get_raid56_logic_offset(u64 physical, int num,
2601 struct map_lookup *map, u64 *offset,
2611 last_offset = (physical - map->stripes[num].physical) *
2612 nr_data_stripes(map);
2614 *stripe_start = last_offset;
2616 *offset = last_offset;
2617 for (i = 0; i < nr_data_stripes(map); i++) {
2618 *offset = last_offset + i * map->stripe_len;
2620 stripe_nr = *offset;
2621 do_div(stripe_nr, map->stripe_len);
2622 do_div(stripe_nr, nr_data_stripes(map));
2624 /* Work out the disk rotation on this stripe-set */
2625 rot = do_div(stripe_nr, map->num_stripes);
2626 /* calculate which stripe this data locates */
2628 stripe_index = rot % map->num_stripes;
2629 if (stripe_index == num)
2631 if (stripe_index < num)
2634 *offset = last_offset + j * map->stripe_len;
2638 static void scrub_free_parity(struct scrub_parity *sparity)
2640 struct scrub_ctx *sctx = sparity->sctx;
2641 struct scrub_page *curr, *next;
2644 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2646 spin_lock(&sctx->stat_lock);
2647 sctx->stat.read_errors += nbits;
2648 sctx->stat.uncorrectable_errors += nbits;
2649 spin_unlock(&sctx->stat_lock);
2652 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2653 list_del_init(&curr->list);
2654 scrub_page_put(curr);
2660 static void scrub_parity_bio_endio(struct bio *bio, int error)
2662 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2663 struct scrub_ctx *sctx = sparity->sctx;
2666 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2669 scrub_free_parity(sparity);
2670 scrub_pending_bio_dec(sctx);
2674 static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2676 struct scrub_ctx *sctx = sparity->sctx;
2678 struct btrfs_raid_bio *rbio;
2679 struct scrub_page *spage;
2680 struct btrfs_bio *bbio = NULL;
2684 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2688 length = sparity->logic_end - sparity->logic_start + 1;
2689 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
2690 sparity->logic_start,
2691 &length, &bbio, 0, 1);
2692 if (ret || !bbio || !bbio->raid_map)
2695 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2699 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2700 bio->bi_private = sparity;
2701 bio->bi_end_io = scrub_parity_bio_endio;
2703 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
2704 length, sparity->scrub_dev,
2710 list_for_each_entry(spage, &sparity->spages, list)
2711 raid56_parity_add_scrub_pages(rbio, spage->page,
2714 scrub_pending_bio_inc(sctx);
2715 raid56_parity_submit_scrub_rbio(rbio);
2721 btrfs_put_bbio(bbio);
2722 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2724 spin_lock(&sctx->stat_lock);
2725 sctx->stat.malloc_errors++;
2726 spin_unlock(&sctx->stat_lock);
2728 scrub_free_parity(sparity);
2731 static inline int scrub_calc_parity_bitmap_len(int nsectors)
2733 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2736 static void scrub_parity_get(struct scrub_parity *sparity)
2738 atomic_inc(&sparity->refs);
2741 static void scrub_parity_put(struct scrub_parity *sparity)
2743 if (!atomic_dec_and_test(&sparity->refs))
2746 scrub_parity_check_and_repair(sparity);
2749 static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2750 struct map_lookup *map,
2751 struct btrfs_device *sdev,
2752 struct btrfs_path *path,
2756 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2757 struct btrfs_root *root = fs_info->extent_root;
2758 struct btrfs_root *csum_root = fs_info->csum_root;
2759 struct btrfs_extent_item *extent;
2763 struct extent_buffer *l;
2764 struct btrfs_key key;
2767 u64 extent_physical;
2769 struct btrfs_device *extent_dev;
2770 struct scrub_parity *sparity;
2773 int extent_mirror_num;
2776 nsectors = map->stripe_len / root->sectorsize;
2777 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2778 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2781 spin_lock(&sctx->stat_lock);
2782 sctx->stat.malloc_errors++;
2783 spin_unlock(&sctx->stat_lock);
2787 sparity->stripe_len = map->stripe_len;
2788 sparity->nsectors = nsectors;
2789 sparity->sctx = sctx;
2790 sparity->scrub_dev = sdev;
2791 sparity->logic_start = logic_start;
2792 sparity->logic_end = logic_end;
2793 atomic_set(&sparity->refs, 1);
2794 INIT_LIST_HEAD(&sparity->spages);
2795 sparity->dbitmap = sparity->bitmap;
2796 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2799 while (logic_start < logic_end) {
2800 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2801 key.type = BTRFS_METADATA_ITEM_KEY;
2803 key.type = BTRFS_EXTENT_ITEM_KEY;
2804 key.objectid = logic_start;
2805 key.offset = (u64)-1;
2807 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2812 ret = btrfs_previous_extent_item(root, path, 0);
2816 btrfs_release_path(path);
2817 ret = btrfs_search_slot(NULL, root, &key,
2829 slot = path->slots[0];
2830 if (slot >= btrfs_header_nritems(l)) {
2831 ret = btrfs_next_leaf(root, path);
2840 btrfs_item_key_to_cpu(l, &key, slot);
2842 if (key.type == BTRFS_METADATA_ITEM_KEY)
2843 bytes = root->nodesize;
2847 if (key.objectid + bytes <= logic_start)
2850 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2851 key.type != BTRFS_METADATA_ITEM_KEY)
2854 if (key.objectid > logic_end) {
2859 while (key.objectid >= logic_start + map->stripe_len)
2860 logic_start += map->stripe_len;
2862 extent = btrfs_item_ptr(l, slot,
2863 struct btrfs_extent_item);
2864 flags = btrfs_extent_flags(l, extent);
2865 generation = btrfs_extent_generation(l, extent);
2867 if (key.objectid < logic_start &&
2868 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2870 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2871 key.objectid, logic_start);
2875 extent_logical = key.objectid;
2878 if (extent_logical < logic_start) {
2879 extent_len -= logic_start - extent_logical;
2880 extent_logical = logic_start;
2883 if (extent_logical + extent_len >
2884 logic_start + map->stripe_len)
2885 extent_len = logic_start + map->stripe_len -
2888 scrub_parity_mark_sectors_data(sparity, extent_logical,
2891 scrub_remap_extent(fs_info, extent_logical,
2892 extent_len, &extent_physical,
2894 &extent_mirror_num);
2896 ret = btrfs_lookup_csums_range(csum_root,
2898 extent_logical + extent_len - 1,
2899 &sctx->csum_list, 1);
2903 ret = scrub_extent_for_parity(sparity, extent_logical,
2912 scrub_free_csums(sctx);
2913 if (extent_logical + extent_len <
2914 key.objectid + bytes) {
2915 logic_start += map->stripe_len;
2917 if (logic_start >= logic_end) {
2922 if (logic_start < key.objectid + bytes) {
2931 btrfs_release_path(path);
2936 logic_start += map->stripe_len;
2940 scrub_parity_mark_sectors_error(sparity, logic_start,
2941 logic_end - logic_start + 1);
2942 scrub_parity_put(sparity);
2944 mutex_lock(&sctx->wr_ctx.wr_lock);
2945 scrub_wr_submit(sctx);
2946 mutex_unlock(&sctx->wr_ctx.wr_lock);
2948 btrfs_release_path(path);
2949 return ret < 0 ? ret : 0;
2952 static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
2953 struct map_lookup *map,
2954 struct btrfs_device *scrub_dev,
2955 int num, u64 base, u64 length,
2958 struct btrfs_path *path, *ppath;
2959 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2960 struct btrfs_root *root = fs_info->extent_root;
2961 struct btrfs_root *csum_root = fs_info->csum_root;
2962 struct btrfs_extent_item *extent;
2963 struct blk_plug plug;
2968 struct extent_buffer *l;
2969 struct btrfs_key key;
2976 struct reada_control *reada1;
2977 struct reada_control *reada2;
2978 struct btrfs_key key_start;
2979 struct btrfs_key key_end;
2980 u64 increment = map->stripe_len;
2983 u64 extent_physical;
2987 struct btrfs_device *extent_dev;
2988 int extent_mirror_num;
2992 physical = map->stripes[num].physical;
2994 do_div(nstripes, map->stripe_len);
2995 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2996 offset = map->stripe_len * num;
2997 increment = map->stripe_len * map->num_stripes;
2999 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3000 int factor = map->num_stripes / map->sub_stripes;
3001 offset = map->stripe_len * (num / map->sub_stripes);
3002 increment = map->stripe_len * factor;
3003 mirror_num = num % map->sub_stripes + 1;
3004 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3005 increment = map->stripe_len;
3006 mirror_num = num % map->num_stripes + 1;
3007 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3008 increment = map->stripe_len;
3009 mirror_num = num % map->num_stripes + 1;
3010 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3011 get_raid56_logic_offset(physical, num, map, &offset, NULL);
3012 increment = map->stripe_len * nr_data_stripes(map);
3015 increment = map->stripe_len;
3019 path = btrfs_alloc_path();
3023 ppath = btrfs_alloc_path();
3025 btrfs_free_path(ppath);
3030 * work on commit root. The related disk blocks are static as
3031 * long as COW is applied. This means, it is save to rewrite
3032 * them to repair disk errors without any race conditions
3034 path->search_commit_root = 1;
3035 path->skip_locking = 1;
3038 * trigger the readahead for extent tree csum tree and wait for
3039 * completion. During readahead, the scrub is officially paused
3040 * to not hold off transaction commits
3042 logical = base + offset;
3043 physical_end = physical + nstripes * map->stripe_len;
3044 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3045 get_raid56_logic_offset(physical_end, num,
3046 map, &logic_end, NULL);
3049 logic_end = logical + increment * nstripes;
3051 wait_event(sctx->list_wait,
3052 atomic_read(&sctx->bios_in_flight) == 0);
3053 scrub_blocked_if_needed(fs_info);
3055 /* FIXME it might be better to start readahead at commit root */
3056 key_start.objectid = logical;
3057 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3058 key_start.offset = (u64)0;
3059 key_end.objectid = logic_end;
3060 key_end.type = BTRFS_METADATA_ITEM_KEY;
3061 key_end.offset = (u64)-1;
3062 reada1 = btrfs_reada_add(root, &key_start, &key_end);
3064 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3065 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3066 key_start.offset = logical;
3067 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3068 key_end.type = BTRFS_EXTENT_CSUM_KEY;
3069 key_end.offset = logic_end;
3070 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
3072 if (!IS_ERR(reada1))
3073 btrfs_reada_wait(reada1);
3074 if (!IS_ERR(reada2))
3075 btrfs_reada_wait(reada2);
3079 * collect all data csums for the stripe to avoid seeking during
3080 * the scrub. This might currently (crc32) end up to be about 1MB
3082 blk_start_plug(&plug);
3085 * now find all extents for each stripe and scrub them
3088 while (physical < physical_end) {
3089 /* for raid56, we skip parity stripe */
3090 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3091 ret = get_raid56_logic_offset(physical, num,
3092 map, &logical, &stripe_logical);
3095 stripe_logical += base;
3096 stripe_end = stripe_logical + increment - 1;
3097 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3098 ppath, stripe_logical,
3108 if (atomic_read(&fs_info->scrub_cancel_req) ||
3109 atomic_read(&sctx->cancel_req)) {
3114 * check to see if we have to pause
3116 if (atomic_read(&fs_info->scrub_pause_req)) {
3117 /* push queued extents */
3118 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3120 mutex_lock(&sctx->wr_ctx.wr_lock);
3121 scrub_wr_submit(sctx);
3122 mutex_unlock(&sctx->wr_ctx.wr_lock);
3123 wait_event(sctx->list_wait,
3124 atomic_read(&sctx->bios_in_flight) == 0);
3125 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3126 scrub_blocked_if_needed(fs_info);
3129 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3130 key.type = BTRFS_METADATA_ITEM_KEY;
3132 key.type = BTRFS_EXTENT_ITEM_KEY;
3133 key.objectid = logical;
3134 key.offset = (u64)-1;
3136 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3141 ret = btrfs_previous_extent_item(root, path, 0);
3145 /* there's no smaller item, so stick with the
3147 btrfs_release_path(path);
3148 ret = btrfs_search_slot(NULL, root, &key,
3160 slot = path->slots[0];
3161 if (slot >= btrfs_header_nritems(l)) {
3162 ret = btrfs_next_leaf(root, path);
3171 btrfs_item_key_to_cpu(l, &key, slot);
3173 if (key.type == BTRFS_METADATA_ITEM_KEY)
3174 bytes = root->nodesize;
3178 if (key.objectid + bytes <= logical)
3181 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3182 key.type != BTRFS_METADATA_ITEM_KEY)
3185 if (key.objectid >= logical + map->stripe_len) {
3186 /* out of this device extent */
3187 if (key.objectid >= logic_end)
3192 extent = btrfs_item_ptr(l, slot,
3193 struct btrfs_extent_item);
3194 flags = btrfs_extent_flags(l, extent);
3195 generation = btrfs_extent_generation(l, extent);
3197 if (key.objectid < logical &&
3198 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
3200 "scrub: tree block %llu spanning "
3201 "stripes, ignored. logical=%llu",
3202 key.objectid, logical);
3207 extent_logical = key.objectid;
3211 * trim extent to this stripe
3213 if (extent_logical < logical) {
3214 extent_len -= logical - extent_logical;
3215 extent_logical = logical;
3217 if (extent_logical + extent_len >
3218 logical + map->stripe_len) {
3219 extent_len = logical + map->stripe_len -
3223 extent_physical = extent_logical - logical + physical;
3224 extent_dev = scrub_dev;
3225 extent_mirror_num = mirror_num;
3227 scrub_remap_extent(fs_info, extent_logical,
3228 extent_len, &extent_physical,
3230 &extent_mirror_num);
3232 ret = btrfs_lookup_csums_range(csum_root, logical,
3233 logical + map->stripe_len - 1,
3234 &sctx->csum_list, 1);
3238 ret = scrub_extent(sctx, extent_logical, extent_len,
3239 extent_physical, extent_dev, flags,
3240 generation, extent_mirror_num,
3241 extent_logical - logical + physical);
3245 scrub_free_csums(sctx);
3246 if (extent_logical + extent_len <
3247 key.objectid + bytes) {
3248 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3250 * loop until we find next data stripe
3251 * or we have finished all stripes.
3254 physical += map->stripe_len;