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;
128 struct btrfs_work work;
131 /* Used for the chunks with parity stripe such RAID5/6 */
132 struct scrub_parity {
133 struct scrub_ctx *sctx;
135 struct btrfs_device *scrub_dev;
147 struct list_head spages;
149 /* Work of parity check and repair */
150 struct btrfs_work work;
152 /* Mark the parity blocks which have data */
153 unsigned long *dbitmap;
156 * Mark the parity blocks which have data, but errors happen when
157 * read data or check data
159 unsigned long *ebitmap;
161 unsigned long bitmap[0];
164 struct scrub_wr_ctx {
165 struct scrub_bio *wr_curr_bio;
166 struct btrfs_device *tgtdev;
167 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
168 atomic_t flush_all_writes;
169 struct mutex wr_lock;
173 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
174 struct btrfs_root *dev_root;
177 atomic_t bios_in_flight;
178 atomic_t workers_pending;
179 spinlock_t list_lock;
180 wait_queue_head_t list_wait;
182 struct list_head csum_list;
185 int pages_per_rd_bio;
190 struct scrub_wr_ctx wr_ctx;
195 struct btrfs_scrub_progress stat;
196 spinlock_t stat_lock;
199 * Use a ref counter to avoid use-after-free issues. Scrub workers
200 * decrement bios_in_flight and workers_pending and then do a wakeup
201 * on the list_wait wait queue. We must ensure the main scrub task
202 * doesn't free the scrub context before or while the workers are
203 * doing the wakeup() call.
208 struct scrub_fixup_nodatasum {
209 struct scrub_ctx *sctx;
210 struct btrfs_device *dev;
212 struct btrfs_root *root;
213 struct btrfs_work work;
217 struct scrub_nocow_inode {
221 struct list_head list;
224 struct scrub_copy_nocow_ctx {
225 struct scrub_ctx *sctx;
229 u64 physical_for_dev_replace;
230 struct list_head inodes;
231 struct btrfs_work work;
234 struct scrub_warning {
235 struct btrfs_path *path;
236 u64 extent_item_size;
240 struct btrfs_device *dev;
243 static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
244 static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
245 static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
246 static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
247 static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
248 static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
249 struct scrub_block *sblocks_for_recheck);
250 static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
251 struct scrub_block *sblock,
252 int retry_failed_mirror);
253 static void scrub_recheck_block_checksum(struct scrub_block *sblock);
254 static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
255 struct scrub_block *sblock_good);
256 static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
257 struct scrub_block *sblock_good,
258 int page_num, int force_write);
259 static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
260 static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
262 static int scrub_checksum_data(struct scrub_block *sblock);
263 static int scrub_checksum_tree_block(struct scrub_block *sblock);
264 static int scrub_checksum_super(struct scrub_block *sblock);
265 static void scrub_block_get(struct scrub_block *sblock);
266 static void scrub_block_put(struct scrub_block *sblock);
267 static void scrub_page_get(struct scrub_page *spage);
268 static void scrub_page_put(struct scrub_page *spage);
269 static void scrub_parity_get(struct scrub_parity *sparity);
270 static void scrub_parity_put(struct scrub_parity *sparity);
271 static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
272 struct scrub_page *spage);
273 static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
274 u64 physical, struct btrfs_device *dev, u64 flags,
275 u64 gen, int mirror_num, u8 *csum, int force,
276 u64 physical_for_dev_replace);
277 static void scrub_bio_end_io(struct bio *bio);
278 static void scrub_bio_end_io_worker(struct btrfs_work *work);
279 static void scrub_block_complete(struct scrub_block *sblock);
280 static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
281 u64 extent_logical, u64 extent_len,
282 u64 *extent_physical,
283 struct btrfs_device **extent_dev,
284 int *extent_mirror_num);
285 static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
286 struct scrub_wr_ctx *wr_ctx,
287 struct btrfs_fs_info *fs_info,
288 struct btrfs_device *dev,
290 static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
291 static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
292 struct scrub_page *spage);
293 static void scrub_wr_submit(struct scrub_ctx *sctx);
294 static void scrub_wr_bio_end_io(struct bio *bio);
295 static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
296 static int write_page_nocow(struct scrub_ctx *sctx,
297 u64 physical_for_dev_replace, struct page *page);
298 static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
299 struct scrub_copy_nocow_ctx *ctx);
300 static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
301 int mirror_num, u64 physical_for_dev_replace);
302 static void copy_nocow_pages_worker(struct btrfs_work *work);
303 static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
304 static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
305 static void scrub_put_ctx(struct scrub_ctx *sctx);
308 static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
310 atomic_inc(&sctx->refs);
311 atomic_inc(&sctx->bios_in_flight);
314 static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
316 atomic_dec(&sctx->bios_in_flight);
317 wake_up(&sctx->list_wait);
321 static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
323 while (atomic_read(&fs_info->scrub_pause_req)) {
324 mutex_unlock(&fs_info->scrub_lock);
325 wait_event(fs_info->scrub_pause_wait,
326 atomic_read(&fs_info->scrub_pause_req) == 0);
327 mutex_lock(&fs_info->scrub_lock);
331 static void scrub_pause_on(struct btrfs_fs_info *fs_info)
333 atomic_inc(&fs_info->scrubs_paused);
334 wake_up(&fs_info->scrub_pause_wait);
337 static void scrub_pause_off(struct btrfs_fs_info *fs_info)
339 mutex_lock(&fs_info->scrub_lock);
340 __scrub_blocked_if_needed(fs_info);
341 atomic_dec(&fs_info->scrubs_paused);
342 mutex_unlock(&fs_info->scrub_lock);
344 wake_up(&fs_info->scrub_pause_wait);
347 static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
349 scrub_pause_on(fs_info);
350 scrub_pause_off(fs_info);
354 * used for workers that require transaction commits (i.e., for the
357 static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
359 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
361 atomic_inc(&sctx->refs);
363 * increment scrubs_running to prevent cancel requests from
364 * completing as long as a worker is running. we must also
365 * increment scrubs_paused to prevent deadlocking on pause
366 * requests used for transactions commits (as the worker uses a
367 * transaction context). it is safe to regard the worker
368 * as paused for all matters practical. effectively, we only
369 * avoid cancellation requests from completing.
371 mutex_lock(&fs_info->scrub_lock);
372 atomic_inc(&fs_info->scrubs_running);
373 atomic_inc(&fs_info->scrubs_paused);
374 mutex_unlock(&fs_info->scrub_lock);
377 * check if @scrubs_running=@scrubs_paused condition
378 * inside wait_event() is not an atomic operation.
379 * which means we may inc/dec @scrub_running/paused
380 * at any time. Let's wake up @scrub_pause_wait as
381 * much as we can to let commit transaction blocked less.
383 wake_up(&fs_info->scrub_pause_wait);
385 atomic_inc(&sctx->workers_pending);
388 /* used for workers that require transaction commits */
389 static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
391 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
394 * see scrub_pending_trans_workers_inc() why we're pretending
395 * to be paused in the scrub counters
397 mutex_lock(&fs_info->scrub_lock);
398 atomic_dec(&fs_info->scrubs_running);
399 atomic_dec(&fs_info->scrubs_paused);
400 mutex_unlock(&fs_info->scrub_lock);
401 atomic_dec(&sctx->workers_pending);
402 wake_up(&fs_info->scrub_pause_wait);
403 wake_up(&sctx->list_wait);
407 static void scrub_free_csums(struct scrub_ctx *sctx)
409 while (!list_empty(&sctx->csum_list)) {
410 struct btrfs_ordered_sum *sum;
411 sum = list_first_entry(&sctx->csum_list,
412 struct btrfs_ordered_sum, list);
413 list_del(&sum->list);
418 static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
425 scrub_free_wr_ctx(&sctx->wr_ctx);
427 /* this can happen when scrub is cancelled */
428 if (sctx->curr != -1) {
429 struct scrub_bio *sbio = sctx->bios[sctx->curr];
431 for (i = 0; i < sbio->page_count; i++) {
432 WARN_ON(!sbio->pagev[i]->page);
433 scrub_block_put(sbio->pagev[i]->sblock);
438 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
439 struct scrub_bio *sbio = sctx->bios[i];
446 scrub_free_csums(sctx);
450 static void scrub_put_ctx(struct scrub_ctx *sctx)
452 if (atomic_dec_and_test(&sctx->refs))
453 scrub_free_ctx(sctx);
456 static noinline_for_stack
457 struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
459 struct scrub_ctx *sctx;
461 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
464 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
467 atomic_set(&sctx->refs, 1);
468 sctx->is_dev_replace = is_dev_replace;
469 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
471 sctx->dev_root = dev->dev_root;
472 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
473 struct scrub_bio *sbio;
475 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
478 sctx->bios[i] = sbio;
482 sbio->page_count = 0;
483 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
484 scrub_bio_end_io_worker, NULL, NULL);
486 if (i != SCRUB_BIOS_PER_SCTX - 1)
487 sctx->bios[i]->next_free = i + 1;
489 sctx->bios[i]->next_free = -1;
491 sctx->first_free = 0;
492 sctx->nodesize = dev->dev_root->nodesize;
493 sctx->sectorsize = dev->dev_root->sectorsize;
494 atomic_set(&sctx->bios_in_flight, 0);
495 atomic_set(&sctx->workers_pending, 0);
496 atomic_set(&sctx->cancel_req, 0);
497 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
498 INIT_LIST_HEAD(&sctx->csum_list);
500 spin_lock_init(&sctx->list_lock);
501 spin_lock_init(&sctx->stat_lock);
502 init_waitqueue_head(&sctx->list_wait);
504 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
505 fs_info->dev_replace.tgtdev, is_dev_replace);
507 scrub_free_ctx(sctx);
513 scrub_free_ctx(sctx);
514 return ERR_PTR(-ENOMEM);
517 static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
524 struct extent_buffer *eb;
525 struct btrfs_inode_item *inode_item;
526 struct scrub_warning *swarn = warn_ctx;
527 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
528 struct inode_fs_paths *ipath = NULL;
529 struct btrfs_root *local_root;
530 struct btrfs_key root_key;
531 struct btrfs_key key;
533 root_key.objectid = root;
534 root_key.type = BTRFS_ROOT_ITEM_KEY;
535 root_key.offset = (u64)-1;
536 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
537 if (IS_ERR(local_root)) {
538 ret = PTR_ERR(local_root);
543 * this makes the path point to (inum INODE_ITEM ioff)
546 key.type = BTRFS_INODE_ITEM_KEY;
549 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
551 btrfs_release_path(swarn->path);
555 eb = swarn->path->nodes[0];
556 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
557 struct btrfs_inode_item);
558 isize = btrfs_inode_size(eb, inode_item);
559 nlink = btrfs_inode_nlink(eb, inode_item);
560 btrfs_release_path(swarn->path);
562 ipath = init_ipath(4096, local_root, swarn->path);
564 ret = PTR_ERR(ipath);
568 ret = paths_from_inode(inum, ipath);
574 * we deliberately ignore the bit ipath might have been too small to
575 * hold all of the paths here
577 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
578 btrfs_warn_in_rcu(fs_info,
579 "%s at logical %llu on dev %s, sector %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
580 swarn->errstr, swarn->logical,
581 rcu_str_deref(swarn->dev->name),
582 (unsigned long long)swarn->sector,
584 min(isize - offset, (u64)PAGE_SIZE), nlink,
585 (char *)(unsigned long)ipath->fspath->val[i]);
591 btrfs_warn_in_rcu(fs_info,
592 "%s at logical %llu on dev %s, sector %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
593 swarn->errstr, swarn->logical,
594 rcu_str_deref(swarn->dev->name),
595 (unsigned long long)swarn->sector,
596 root, inum, offset, ret);
602 static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
604 struct btrfs_device *dev;
605 struct btrfs_fs_info *fs_info;
606 struct btrfs_path *path;
607 struct btrfs_key found_key;
608 struct extent_buffer *eb;
609 struct btrfs_extent_item *ei;
610 struct scrub_warning swarn;
611 unsigned long ptr = 0;
619 WARN_ON(sblock->page_count < 1);
620 dev = sblock->pagev[0]->dev;
621 fs_info = sblock->sctx->dev_root->fs_info;
623 path = btrfs_alloc_path();
627 swarn.sector = (sblock->pagev[0]->physical) >> 9;
628 swarn.logical = sblock->pagev[0]->logical;
629 swarn.errstr = errstr;
632 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
637 extent_item_pos = swarn.logical - found_key.objectid;
638 swarn.extent_item_size = found_key.offset;
641 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
642 item_size = btrfs_item_size_nr(eb, path->slots[0]);
644 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
646 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
647 item_size, &ref_root,
649 btrfs_warn_in_rcu(fs_info,
650 "%s at logical %llu on dev %s, sector %llu: metadata %s (level %d) in tree %llu",
651 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_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,
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,
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 btrfs_err_rl_in_rcu(sctx->dev_root->fs_info,
850 "unable to fixup (nodatasum) error at logical %llu on dev %s",
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;
888 unsigned int failed_mirror_index;
889 unsigned int is_metadata;
890 unsigned int have_csum;
891 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
892 struct scrub_block *sblock_bad;
897 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
898 DEFAULT_RATELIMIT_BURST);
900 BUG_ON(sblock_to_check->page_count < 1);
901 fs_info = sctx->dev_root->fs_info;
902 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
904 * if we find an error in a super block, we just report it.
905 * They will get written with the next transaction commit
908 spin_lock(&sctx->stat_lock);
909 ++sctx->stat.super_errors;
910 spin_unlock(&sctx->stat_lock);
913 length = sblock_to_check->page_count * PAGE_SIZE;
914 logical = sblock_to_check->pagev[0]->logical;
915 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
916 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
917 is_metadata = !(sblock_to_check->pagev[0]->flags &
918 BTRFS_EXTENT_FLAG_DATA);
919 have_csum = sblock_to_check->pagev[0]->have_csum;
920 dev = sblock_to_check->pagev[0]->dev;
923 * read all mirrors one after the other. This includes to
924 * re-read the extent or metadata block that failed (that was
925 * the cause that this fixup code is called) another time,
926 * page by page this time in order to know which pages
927 * caused I/O errors and which ones are good (for all mirrors).
928 * It is the goal to handle the situation when more than one
929 * mirror contains I/O errors, but the errors do not
930 * overlap, i.e. the data can be repaired by selecting the
931 * pages from those mirrors without I/O error on the
932 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
933 * would be that mirror #1 has an I/O error on the first page,
934 * the second page is good, and mirror #2 has an I/O error on
935 * the second page, but the first page is good.
936 * Then the first page of the first mirror can be repaired by
937 * taking the first page of the second mirror, and the
938 * second page of the second mirror can be repaired by
939 * copying the contents of the 2nd page of the 1st mirror.
940 * One more note: if the pages of one mirror contain I/O
941 * errors, the checksum cannot be verified. In order to get
942 * the best data for repairing, the first attempt is to find
943 * a mirror without I/O errors and with a validated checksum.
944 * Only if this is not possible, the pages are picked from
945 * mirrors with I/O errors without considering the checksum.
946 * If the latter is the case, at the end, the checksum of the
947 * repaired area is verified in order to correctly maintain
951 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
952 sizeof(*sblocks_for_recheck), GFP_NOFS);
953 if (!sblocks_for_recheck) {
954 spin_lock(&sctx->stat_lock);
955 sctx->stat.malloc_errors++;
956 sctx->stat.read_errors++;
957 sctx->stat.uncorrectable_errors++;
958 spin_unlock(&sctx->stat_lock);
959 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
963 /* setup the context, map the logical blocks and alloc the pages */
964 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
966 spin_lock(&sctx->stat_lock);
967 sctx->stat.read_errors++;
968 sctx->stat.uncorrectable_errors++;
969 spin_unlock(&sctx->stat_lock);
970 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
973 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
974 sblock_bad = sblocks_for_recheck + failed_mirror_index;
976 /* build and submit the bios for the failed mirror, check checksums */
977 scrub_recheck_block(fs_info, sblock_bad, 1);
979 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
980 sblock_bad->no_io_error_seen) {
982 * the error disappeared after reading page by page, or
983 * the area was part of a huge bio and other parts of the
984 * bio caused I/O errors, or the block layer merged several
985 * read requests into one and the error is caused by a
986 * different bio (usually one of the two latter cases is
989 spin_lock(&sctx->stat_lock);
990 sctx->stat.unverified_errors++;
991 sblock_to_check->data_corrected = 1;
992 spin_unlock(&sctx->stat_lock);
994 if (sctx->is_dev_replace)
995 scrub_write_block_to_dev_replace(sblock_bad);
999 if (!sblock_bad->no_io_error_seen) {
1000 spin_lock(&sctx->stat_lock);
1001 sctx->stat.read_errors++;
1002 spin_unlock(&sctx->stat_lock);
1003 if (__ratelimit(&_rs))
1004 scrub_print_warning("i/o error", sblock_to_check);
1005 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
1006 } else if (sblock_bad->checksum_error) {
1007 spin_lock(&sctx->stat_lock);
1008 sctx->stat.csum_errors++;
1009 spin_unlock(&sctx->stat_lock);
1010 if (__ratelimit(&_rs))
1011 scrub_print_warning("checksum error", sblock_to_check);
1012 btrfs_dev_stat_inc_and_print(dev,
1013 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1014 } else if (sblock_bad->header_error) {
1015 spin_lock(&sctx->stat_lock);
1016 sctx->stat.verify_errors++;
1017 spin_unlock(&sctx->stat_lock);
1018 if (__ratelimit(&_rs))
1019 scrub_print_warning("checksum/header error",
1021 if (sblock_bad->generation_error)
1022 btrfs_dev_stat_inc_and_print(dev,
1023 BTRFS_DEV_STAT_GENERATION_ERRS);
1025 btrfs_dev_stat_inc_and_print(dev,
1026 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1029 if (sctx->readonly) {
1030 ASSERT(!sctx->is_dev_replace);
1035 * NOTE: Even for nodatasum case, it's still possible that it's a
1036 * compressed data extent, thus scrub_fixup_nodatasum(), which write
1037 * inode page cache onto disk, could cause serious data corruption.
1039 * So here we could only read from disk, and hope our recovery could
1040 * reach disk before the newer write.
1042 if (0 && !is_metadata && !have_csum) {
1043 struct scrub_fixup_nodatasum *fixup_nodatasum;
1045 WARN_ON(sctx->is_dev_replace);
1048 * !is_metadata and !have_csum, this means that the data
1049 * might not be COWed, that it might be modified
1050 * concurrently. The general strategy to work on the
1051 * commit root does not help in the case when COW is not
1054 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1055 if (!fixup_nodatasum)
1056 goto did_not_correct_error;
1057 fixup_nodatasum->sctx = sctx;
1058 fixup_nodatasum->dev = dev;
1059 fixup_nodatasum->logical = logical;
1060 fixup_nodatasum->root = fs_info->extent_root;
1061 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
1062 scrub_pending_trans_workers_inc(sctx);
1063 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1064 scrub_fixup_nodatasum, NULL, NULL);
1065 btrfs_queue_work(fs_info->scrub_workers,
1066 &fixup_nodatasum->work);
1071 * now build and submit the bios for the other mirrors, check
1073 * First try to pick the mirror which is completely without I/O
1074 * errors and also does not have a checksum error.
1075 * If one is found, and if a checksum is present, the full block
1076 * that is known to contain an error is rewritten. Afterwards
1077 * the block is known to be corrected.
1078 * If a mirror is found which is completely correct, and no
1079 * checksum is present, only those pages are rewritten that had
1080 * an I/O error in the block to be repaired, since it cannot be
1081 * determined, which copy of the other pages is better (and it
1082 * could happen otherwise that a correct page would be
1083 * overwritten by a bad one).
1085 for (mirror_index = 0;
1086 mirror_index < BTRFS_MAX_MIRRORS &&
1087 sblocks_for_recheck[mirror_index].page_count > 0;
1089 struct scrub_block *sblock_other;
1091 if (mirror_index == failed_mirror_index)
1093 sblock_other = sblocks_for_recheck + mirror_index;
1095 /* build and submit the bios, check checksums */
1096 scrub_recheck_block(fs_info, sblock_other, 0);
1098 if (!sblock_other->header_error &&
1099 !sblock_other->checksum_error &&
1100 sblock_other->no_io_error_seen) {
1101 if (sctx->is_dev_replace) {
1102 scrub_write_block_to_dev_replace(sblock_other);
1103 goto corrected_error;
1105 ret = scrub_repair_block_from_good_copy(
1106 sblock_bad, sblock_other);
1108 goto corrected_error;
1113 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1114 goto did_not_correct_error;
1117 * In case of I/O errors in the area that is supposed to be
1118 * repaired, continue by picking good copies of those pages.
1119 * Select the good pages from mirrors to rewrite bad pages from
1120 * the area to fix. Afterwards verify the checksum of the block
1121 * that is supposed to be repaired. This verification step is
1122 * only done for the purpose of statistic counting and for the
1123 * final scrub report, whether errors remain.
1124 * A perfect algorithm could make use of the checksum and try
1125 * all possible combinations of pages from the different mirrors
1126 * until the checksum verification succeeds. For example, when
1127 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1128 * of mirror #2 is readable but the final checksum test fails,
1129 * then the 2nd page of mirror #3 could be tried, whether now
1130 * the final checksum succeeds. But this would be a rare
1131 * exception and is therefore not implemented. At least it is
1132 * avoided that the good copy is overwritten.
1133 * A more useful improvement would be to pick the sectors
1134 * without I/O error based on sector sizes (512 bytes on legacy
1135 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1136 * mirror could be repaired by taking 512 byte of a different
1137 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1138 * area are unreadable.
1141 for (page_num = 0; page_num < sblock_bad->page_count;
1143 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1144 struct scrub_block *sblock_other = NULL;
1146 /* skip no-io-error page in scrub */
1147 if (!page_bad->io_error && !sctx->is_dev_replace)
1150 /* try to find no-io-error page in mirrors */
1151 if (page_bad->io_error) {
1152 for (mirror_index = 0;
1153 mirror_index < BTRFS_MAX_MIRRORS &&
1154 sblocks_for_recheck[mirror_index].page_count > 0;
1156 if (!sblocks_for_recheck[mirror_index].
1157 pagev[page_num]->io_error) {
1158 sblock_other = sblocks_for_recheck +
1167 if (sctx->is_dev_replace) {
1169 * did not find a mirror to fetch the page
1170 * from. scrub_write_page_to_dev_replace()
1171 * handles this case (page->io_error), by
1172 * filling the block with zeros before
1173 * submitting the write request
1176 sblock_other = sblock_bad;
1178 if (scrub_write_page_to_dev_replace(sblock_other,
1180 btrfs_dev_replace_stats_inc(
1182 fs_info->dev_replace.
1186 } else if (sblock_other) {
1187 ret = scrub_repair_page_from_good_copy(sblock_bad,
1191 page_bad->io_error = 0;
1197 if (success && !sctx->is_dev_replace) {
1198 if (is_metadata || have_csum) {
1200 * need to verify the checksum now that all
1201 * sectors on disk are repaired (the write
1202 * request for data to be repaired is on its way).
1203 * Just be lazy and use scrub_recheck_block()
1204 * which re-reads the data before the checksum
1205 * is verified, but most likely the data comes out
1206 * of the page cache.
1208 scrub_recheck_block(fs_info, sblock_bad, 1);
1209 if (!sblock_bad->header_error &&
1210 !sblock_bad->checksum_error &&
1211 sblock_bad->no_io_error_seen)
1212 goto corrected_error;
1214 goto did_not_correct_error;
1217 spin_lock(&sctx->stat_lock);
1218 sctx->stat.corrected_errors++;
1219 sblock_to_check->data_corrected = 1;
1220 spin_unlock(&sctx->stat_lock);
1221 btrfs_err_rl_in_rcu(fs_info,
1222 "fixed up error at logical %llu on dev %s",
1223 logical, rcu_str_deref(dev->name));
1226 did_not_correct_error:
1227 spin_lock(&sctx->stat_lock);
1228 sctx->stat.uncorrectable_errors++;
1229 spin_unlock(&sctx->stat_lock);
1230 btrfs_err_rl_in_rcu(fs_info,
1231 "unable to fixup (regular) error at logical %llu on dev %s",
1232 logical, rcu_str_deref(dev->name));
1236 if (sblocks_for_recheck) {
1237 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1239 struct scrub_block *sblock = sblocks_for_recheck +
1241 struct scrub_recover *recover;
1244 for (page_index = 0; page_index < sblock->page_count;
1246 sblock->pagev[page_index]->sblock = NULL;
1247 recover = sblock->pagev[page_index]->recover;
1249 scrub_put_recover(recover);
1250 sblock->pagev[page_index]->recover =
1253 scrub_page_put(sblock->pagev[page_index]);
1256 kfree(sblocks_for_recheck);
1262 static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
1264 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1266 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1269 return (int)bbio->num_stripes;
1272 static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1275 int nstripes, int mirror,
1281 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1283 for (i = 0; i < nstripes; i++) {
1284 if (raid_map[i] == RAID6_Q_STRIPE ||
1285 raid_map[i] == RAID5_P_STRIPE)
1288 if (logical >= raid_map[i] &&
1289 logical < raid_map[i] + mapped_length)
1294 *stripe_offset = logical - raid_map[i];
1296 /* The other RAID type */
1297 *stripe_index = mirror;
1302 static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
1303 struct scrub_block *sblocks_for_recheck)
1305 struct scrub_ctx *sctx = original_sblock->sctx;
1306 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1307 u64 length = original_sblock->page_count * PAGE_SIZE;
1308 u64 logical = original_sblock->pagev[0]->logical;
1309 u64 generation = original_sblock->pagev[0]->generation;
1310 u64 flags = original_sblock->pagev[0]->flags;
1311 u64 have_csum = original_sblock->pagev[0]->have_csum;
1312 struct scrub_recover *recover;
1313 struct btrfs_bio *bbio;
1324 * note: the two members refs and outstanding_pages
1325 * are not used (and not set) in the blocks that are used for
1326 * the recheck procedure
1329 while (length > 0) {
1330 sublen = min_t(u64, length, PAGE_SIZE);
1331 mapped_length = sublen;
1335 * with a length of PAGE_SIZE, each returned stripe
1336 * represents one mirror
1338 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
1339 &mapped_length, &bbio, 0, 1);
1340 if (ret || !bbio || mapped_length < sublen) {
1341 btrfs_put_bbio(bbio);
1345 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1347 btrfs_put_bbio(bbio);
1351 atomic_set(&recover->refs, 1);
1352 recover->bbio = bbio;
1353 recover->map_length = mapped_length;
1355 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
1357 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
1359 for (mirror_index = 0; mirror_index < nmirrors;
1361 struct scrub_block *sblock;
1362 struct scrub_page *page;
1364 sblock = sblocks_for_recheck + mirror_index;
1365 sblock->sctx = sctx;
1367 page = kzalloc(sizeof(*page), GFP_NOFS);
1370 spin_lock(&sctx->stat_lock);
1371 sctx->stat.malloc_errors++;
1372 spin_unlock(&sctx->stat_lock);
1373 scrub_put_recover(recover);
1376 scrub_page_get(page);
1377 sblock->pagev[page_index] = page;
1378 page->sblock = sblock;
1379 page->flags = flags;
1380 page->generation = generation;
1381 page->logical = logical;
1382 page->have_csum = have_csum;
1385 original_sblock->pagev[0]->csum,
1388 scrub_stripe_index_and_offset(logical,
1397 page->physical = bbio->stripes[stripe_index].physical +
1399 page->dev = bbio->stripes[stripe_index].dev;
1401 BUG_ON(page_index >= original_sblock->page_count);
1402 page->physical_for_dev_replace =
1403 original_sblock->pagev[page_index]->
1404 physical_for_dev_replace;
1405 /* for missing devices, dev->bdev is NULL */
1406 page->mirror_num = mirror_index + 1;
1407 sblock->page_count++;
1408 page->page = alloc_page(GFP_NOFS);
1412 scrub_get_recover(recover);
1413 page->recover = recover;
1415 scrub_put_recover(recover);
1424 struct scrub_bio_ret {
1425 struct completion event;
1429 static void scrub_bio_wait_endio(struct bio *bio)
1431 struct scrub_bio_ret *ret = bio->bi_private;
1433 ret->error = bio->bi_error;
1434 complete(&ret->event);
1437 static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1439 return page->recover &&
1440 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
1443 static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1445 struct scrub_page *page)
1447 struct scrub_bio_ret done;
1450 init_completion(&done.event);
1452 bio->bi_iter.bi_sector = page->logical >> 9;
1453 bio->bi_private = &done;
1454 bio->bi_end_io = scrub_bio_wait_endio;
1456 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
1457 page->recover->map_length,
1458 page->mirror_num, 0);
1462 wait_for_completion(&done.event);
1470 * this function will check the on disk data for checksum errors, header
1471 * errors and read I/O errors. If any I/O errors happen, the exact pages
1472 * which are errored are marked as being bad. The goal is to enable scrub
1473 * to take those pages that are not errored from all the mirrors so that
1474 * the pages that are errored in the just handled mirror can be repaired.
1476 static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1477 struct scrub_block *sblock,
1478 int retry_failed_mirror)
1482 sblock->no_io_error_seen = 1;
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;
1509 bio_set_op_attrs(bio, REQ_OP_READ, 0);
1511 if (btrfsic_submit_bio_wait(bio))
1512 sblock->no_io_error_seen = 0;
1518 if (sblock->no_io_error_seen)
1519 scrub_recheck_block_checksum(sblock);
1522 static inline int scrub_check_fsid(u8 fsid[],
1523 struct scrub_page *spage)
1525 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1528 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1532 static void scrub_recheck_block_checksum(struct scrub_block *sblock)
1534 sblock->header_error = 0;
1535 sblock->checksum_error = 0;
1536 sblock->generation_error = 0;
1538 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1539 scrub_checksum_data(sblock);
1541 scrub_checksum_tree_block(sblock);
1544 static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1545 struct scrub_block *sblock_good)
1550 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1553 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1563 static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1564 struct scrub_block *sblock_good,
1565 int page_num, int force_write)
1567 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1568 struct scrub_page *page_good = sblock_good->pagev[page_num];
1570 BUG_ON(page_bad->page == NULL);
1571 BUG_ON(page_good->page == NULL);
1572 if (force_write || sblock_bad->header_error ||
1573 sblock_bad->checksum_error || page_bad->io_error) {
1577 if (!page_bad->dev->bdev) {
1578 btrfs_warn_rl(sblock_bad->sctx->dev_root->fs_info,
1579 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
1583 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
1586 bio->bi_bdev = page_bad->dev->bdev;
1587 bio->bi_iter.bi_sector = page_bad->physical >> 9;
1588 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
1590 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1591 if (PAGE_SIZE != ret) {
1596 if (btrfsic_submit_bio_wait(bio)) {
1597 btrfs_dev_stat_inc_and_print(page_bad->dev,
1598 BTRFS_DEV_STAT_WRITE_ERRS);
1599 btrfs_dev_replace_stats_inc(
1600 &sblock_bad->sctx->dev_root->fs_info->
1601 dev_replace.num_write_errors);
1611 static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1616 * This block is used for the check of the parity on the source device,
1617 * so the data needn't be written into the destination device.
1619 if (sblock->sparity)
1622 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1625 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1627 btrfs_dev_replace_stats_inc(
1628 &sblock->sctx->dev_root->fs_info->dev_replace.
1633 static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1636 struct scrub_page *spage = sblock->pagev[page_num];
1638 BUG_ON(spage->page == NULL);
1639 if (spage->io_error) {
1640 void *mapped_buffer = kmap_atomic(spage->page);
1642 memset(mapped_buffer, 0, PAGE_SIZE);
1643 flush_dcache_page(spage->page);
1644 kunmap_atomic(mapped_buffer);
1646 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1649 static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1650 struct scrub_page *spage)
1652 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1653 struct scrub_bio *sbio;
1656 mutex_lock(&wr_ctx->wr_lock);
1658 if (!wr_ctx->wr_curr_bio) {
1659 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1661 if (!wr_ctx->wr_curr_bio) {
1662 mutex_unlock(&wr_ctx->wr_lock);
1665 wr_ctx->wr_curr_bio->sctx = sctx;
1666 wr_ctx->wr_curr_bio->page_count = 0;
1668 sbio = wr_ctx->wr_curr_bio;
1669 if (sbio->page_count == 0) {
1672 sbio->physical = spage->physical_for_dev_replace;
1673 sbio->logical = spage->logical;
1674 sbio->dev = wr_ctx->tgtdev;
1677 bio = btrfs_io_bio_alloc(GFP_KERNEL,
1678 wr_ctx->pages_per_wr_bio);
1680 mutex_unlock(&wr_ctx->wr_lock);
1686 bio->bi_private = sbio;
1687 bio->bi_end_io = scrub_wr_bio_end_io;
1688 bio->bi_bdev = sbio->dev->bdev;
1689 bio->bi_iter.bi_sector = sbio->physical >> 9;
1690 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
1692 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1693 spage->physical_for_dev_replace ||
1694 sbio->logical + sbio->page_count * PAGE_SIZE !=
1696 scrub_wr_submit(sctx);
1700 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1701 if (ret != PAGE_SIZE) {
1702 if (sbio->page_count < 1) {
1705 mutex_unlock(&wr_ctx->wr_lock);
1708 scrub_wr_submit(sctx);
1712 sbio->pagev[sbio->page_count] = spage;
1713 scrub_page_get(spage);
1715 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1716 scrub_wr_submit(sctx);
1717 mutex_unlock(&wr_ctx->wr_lock);
1722 static void scrub_wr_submit(struct scrub_ctx *sctx)
1724 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1725 struct scrub_bio *sbio;
1727 if (!wr_ctx->wr_curr_bio)
1730 sbio = wr_ctx->wr_curr_bio;
1731 wr_ctx->wr_curr_bio = NULL;
1732 WARN_ON(!sbio->bio->bi_bdev);
1733 scrub_pending_bio_inc(sctx);
1734 /* process all writes in a single worker thread. Then the block layer
1735 * orders the requests before sending them to the driver which
1736 * doubled the write performance on spinning disks when measured
1738 btrfsic_submit_bio(sbio->bio);
1741 static void scrub_wr_bio_end_io(struct bio *bio)
1743 struct scrub_bio *sbio = bio->bi_private;
1744 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1746 sbio->err = bio->bi_error;
1749 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1750 scrub_wr_bio_end_io_worker, NULL, NULL);
1751 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
1754 static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1756 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1757 struct scrub_ctx *sctx = sbio->sctx;
1760 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1762 struct btrfs_dev_replace *dev_replace =
1763 &sbio->sctx->dev_root->fs_info->dev_replace;
1765 for (i = 0; i < sbio->page_count; i++) {
1766 struct scrub_page *spage = sbio->pagev[i];
1768 spage->io_error = 1;
1769 btrfs_dev_replace_stats_inc(&dev_replace->
1774 for (i = 0; i < sbio->page_count; i++)
1775 scrub_page_put(sbio->pagev[i]);
1779 scrub_pending_bio_dec(sctx);
1782 static int scrub_checksum(struct scrub_block *sblock)
1788 * No need to initialize these stats currently,
1789 * because this function only use return value
1790 * instead of these stats value.
1795 sblock->header_error = 0;
1796 sblock->generation_error = 0;
1797 sblock->checksum_error = 0;
1799 WARN_ON(sblock->page_count < 1);
1800 flags = sblock->pagev[0]->flags;
1802 if (flags & BTRFS_EXTENT_FLAG_DATA)
1803 ret = scrub_checksum_data(sblock);
1804 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1805 ret = scrub_checksum_tree_block(sblock);
1806 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1807 (void)scrub_checksum_super(sblock);
1811 scrub_handle_errored_block(sblock);
1816 static int scrub_checksum_data(struct scrub_block *sblock)
1818 struct scrub_ctx *sctx = sblock->sctx;
1819 u8 csum[BTRFS_CSUM_SIZE];
1827 BUG_ON(sblock->page_count < 1);
1828 if (!sblock->pagev[0]->have_csum)
1831 on_disk_csum = sblock->pagev[0]->csum;
1832 page = sblock->pagev[0]->page;
1833 buffer = kmap_atomic(page);
1835 len = sctx->sectorsize;
1838 u64 l = min_t(u64, len, PAGE_SIZE);
1840 crc = btrfs_csum_data(buffer, crc, l);
1841 kunmap_atomic(buffer);
1846 BUG_ON(index >= sblock->page_count);
1847 BUG_ON(!sblock->pagev[index]->page);
1848 page = sblock->pagev[index]->page;
1849 buffer = kmap_atomic(page);
1852 btrfs_csum_final(crc, csum);
1853 if (memcmp(csum, on_disk_csum, sctx->csum_size))
1854 sblock->checksum_error = 1;
1856 return sblock->checksum_error;
1859 static int scrub_checksum_tree_block(struct scrub_block *sblock)
1861 struct scrub_ctx *sctx = sblock->sctx;
1862 struct btrfs_header *h;
1863 struct btrfs_root *root = sctx->dev_root;
1864 struct btrfs_fs_info *fs_info = root->fs_info;
1865 u8 calculated_csum[BTRFS_CSUM_SIZE];
1866 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1868 void *mapped_buffer;
1875 BUG_ON(sblock->page_count < 1);
1876 page = sblock->pagev[0]->page;
1877 mapped_buffer = kmap_atomic(page);
1878 h = (struct btrfs_header *)mapped_buffer;
1879 memcpy(on_disk_csum, h->csum, sctx->csum_size);
1882 * we don't use the getter functions here, as we
1883 * a) don't have an extent buffer and
1884 * b) the page is already kmapped
1886 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
1887 sblock->header_error = 1;
1889 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
1890 sblock->header_error = 1;
1891 sblock->generation_error = 1;
1894 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
1895 sblock->header_error = 1;
1897 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1899 sblock->header_error = 1;
1901 len = sctx->nodesize - BTRFS_CSUM_SIZE;
1902 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1903 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1906 u64 l = min_t(u64, len, mapped_size);
1908 crc = btrfs_csum_data(p, crc, l);
1909 kunmap_atomic(mapped_buffer);
1914 BUG_ON(index >= sblock->page_count);
1915 BUG_ON(!sblock->pagev[index]->page);
1916 page = sblock->pagev[index]->page;
1917 mapped_buffer = kmap_atomic(page);
1918 mapped_size = PAGE_SIZE;
1922 btrfs_csum_final(crc, calculated_csum);
1923 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
1924 sblock->checksum_error = 1;
1926 return sblock->header_error || sblock->checksum_error;
1929 static int scrub_checksum_super(struct scrub_block *sblock)
1931 struct btrfs_super_block *s;
1932 struct scrub_ctx *sctx = sblock->sctx;
1933 u8 calculated_csum[BTRFS_CSUM_SIZE];
1934 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1936 void *mapped_buffer;
1945 BUG_ON(sblock->page_count < 1);
1946 page = sblock->pagev[0]->page;
1947 mapped_buffer = kmap_atomic(page);
1948 s = (struct btrfs_super_block *)mapped_buffer;
1949 memcpy(on_disk_csum, s->csum, sctx->csum_size);
1951 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
1954 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
1957 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
1960 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1961 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1962 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1965 u64 l = min_t(u64, len, mapped_size);
1967 crc = btrfs_csum_data(p, crc, l);
1968 kunmap_atomic(mapped_buffer);
1973 BUG_ON(index >= sblock->page_count);
1974 BUG_ON(!sblock->pagev[index]->page);
1975 page = sblock->pagev[index]->page;
1976 mapped_buffer = kmap_atomic(page);
1977 mapped_size = PAGE_SIZE;
1981 btrfs_csum_final(crc, calculated_csum);
1982 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
1985 if (fail_cor + fail_gen) {
1987 * if we find an error in a super block, we just report it.
1988 * They will get written with the next transaction commit
1991 spin_lock(&sctx->stat_lock);
1992 ++sctx->stat.super_errors;
1993 spin_unlock(&sctx->stat_lock);
1995 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
1996 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1998 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
1999 BTRFS_DEV_STAT_GENERATION_ERRS);
2002 return fail_cor + fail_gen;
2005 static void scrub_block_get(struct scrub_block *sblock)
2007 atomic_inc(&sblock->refs);
2010 static void scrub_block_put(struct scrub_block *sblock)
2012 if (atomic_dec_and_test(&sblock->refs)) {
2015 if (sblock->sparity)
2016 scrub_parity_put(sblock->sparity);
2018 for (i = 0; i < sblock->page_count; i++)
2019 scrub_page_put(sblock->pagev[i]);
2024 static void scrub_page_get(struct scrub_page *spage)
2026 atomic_inc(&spage->refs);
2029 static void scrub_page_put(struct scrub_page *spage)
2031 if (atomic_dec_and_test(&spage->refs)) {
2033 __free_page(spage->page);
2038 static void scrub_submit(struct scrub_ctx *sctx)
2040 struct scrub_bio *sbio;
2042 if (sctx->curr == -1)
2045 sbio = sctx->bios[sctx->curr];
2047 scrub_pending_bio_inc(sctx);
2048 btrfsic_submit_bio(sbio->bio);
2051 static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2052 struct scrub_page *spage)
2054 struct scrub_block *sblock = spage->sblock;
2055 struct scrub_bio *sbio;
2060 * grab a fresh bio or wait for one to become available
2062 while (sctx->curr == -1) {
2063 spin_lock(&sctx->list_lock);
2064 sctx->curr = sctx->first_free;
2065 if (sctx->curr != -1) {
2066 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2067 sctx->bios[sctx->curr]->next_free = -1;
2068 sctx->bios[sctx->curr]->page_count = 0;
2069 spin_unlock(&sctx->list_lock);
2071 spin_unlock(&sctx->list_lock);
2072 wait_event(sctx->list_wait, sctx->first_free != -1);
2075 sbio = sctx->bios[sctx->curr];
2076 if (sbio->page_count == 0) {
2079 sbio->physical = spage->physical;
2080 sbio->logical = spage->logical;
2081 sbio->dev = spage->dev;
2084 bio = btrfs_io_bio_alloc(GFP_KERNEL,
2085 sctx->pages_per_rd_bio);
2091 bio->bi_private = sbio;
2092 bio->bi_end_io = scrub_bio_end_io;
2093 bio->bi_bdev = sbio->dev->bdev;
2094 bio->bi_iter.bi_sector = sbio->physical >> 9;
2095 bio_set_op_attrs(bio, REQ_OP_READ, 0);
2097 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2099 sbio->logical + sbio->page_count * PAGE_SIZE !=
2101 sbio->dev != spage->dev) {
2106 sbio->pagev[sbio->page_count] = spage;
2107 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2108 if (ret != PAGE_SIZE) {
2109 if (sbio->page_count < 1) {
2118 scrub_block_get(sblock); /* one for the page added to the bio */
2119 atomic_inc(&sblock->outstanding_pages);
2121 if (sbio->page_count == sctx->pages_per_rd_bio)
2127 static void scrub_missing_raid56_end_io(struct bio *bio)
2129 struct scrub_block *sblock = bio->bi_private;
2130 struct btrfs_fs_info *fs_info = sblock->sctx->dev_root->fs_info;
2133 sblock->no_io_error_seen = 0;
2137 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2140 static void scrub_missing_raid56_worker(struct btrfs_work *work)
2142 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2143 struct scrub_ctx *sctx = sblock->sctx;
2145 struct btrfs_device *dev;
2147 logical = sblock->pagev[0]->logical;
2148 dev = sblock->pagev[0]->dev;
2150 if (sblock->no_io_error_seen)
2151 scrub_recheck_block_checksum(sblock);
2153 if (!sblock->no_io_error_seen) {
2154 spin_lock(&sctx->stat_lock);
2155 sctx->stat.read_errors++;
2156 spin_unlock(&sctx->stat_lock);
2157 btrfs_err_rl_in_rcu(sctx->dev_root->fs_info,
2158 "IO error rebuilding logical %llu for dev %s",
2159 logical, rcu_str_deref(dev->name));
2160 } else if (sblock->header_error || sblock->checksum_error) {
2161 spin_lock(&sctx->stat_lock);
2162 sctx->stat.uncorrectable_errors++;
2163 spin_unlock(&sctx->stat_lock);
2164 btrfs_err_rl_in_rcu(sctx->dev_root->fs_info,
2165 "failed to rebuild valid logical %llu for dev %s",
2166 logical, rcu_str_deref(dev->name));
2168 scrub_write_block_to_dev_replace(sblock);
2171 scrub_block_put(sblock);
2173 if (sctx->is_dev_replace &&
2174 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2175 mutex_lock(&sctx->wr_ctx.wr_lock);
2176 scrub_wr_submit(sctx);
2177 mutex_unlock(&sctx->wr_ctx.wr_lock);
2180 scrub_pending_bio_dec(sctx);
2183 static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2185 struct scrub_ctx *sctx = sblock->sctx;
2186 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2187 u64 length = sblock->page_count * PAGE_SIZE;
2188 u64 logical = sblock->pagev[0]->logical;
2189 struct btrfs_bio *bbio = NULL;
2191 struct btrfs_raid_bio *rbio;
2195 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical, &length,
2197 if (ret || !bbio || !bbio->raid_map)
2200 if (WARN_ON(!sctx->is_dev_replace ||
2201 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2203 * We shouldn't be scrubbing a missing device. Even for dev
2204 * replace, we should only get here for RAID 5/6. We either
2205 * managed to mount something with no mirrors remaining or
2206 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2211 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2215 bio->bi_iter.bi_sector = logical >> 9;
2216 bio->bi_private = sblock;
2217 bio->bi_end_io = scrub_missing_raid56_end_io;
2219 rbio = raid56_alloc_missing_rbio(sctx->dev_root, bio, bbio, length);
2223 for (i = 0; i < sblock->page_count; i++) {
2224 struct scrub_page *spage = sblock->pagev[i];
2226 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2229 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2230 scrub_missing_raid56_worker, NULL, NULL);
2231 scrub_block_get(sblock);
2232 scrub_pending_bio_inc(sctx);
2233 raid56_submit_missing_rbio(rbio);
2239 btrfs_put_bbio(bbio);
2240 spin_lock(&sctx->stat_lock);
2241 sctx->stat.malloc_errors++;
2242 spin_unlock(&sctx->stat_lock);
2245 static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
2246 u64 physical, struct btrfs_device *dev, u64 flags,
2247 u64 gen, int mirror_num, u8 *csum, int force,
2248 u64 physical_for_dev_replace)
2250 struct scrub_block *sblock;
2253 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
2255 spin_lock(&sctx->stat_lock);
2256 sctx->stat.malloc_errors++;
2257 spin_unlock(&sctx->stat_lock);
2261 /* one ref inside this function, plus one for each page added to
2263 atomic_set(&sblock->refs, 1);
2264 sblock->sctx = sctx;
2265 sblock->no_io_error_seen = 1;
2267 for (index = 0; len > 0; index++) {
2268 struct scrub_page *spage;
2269 u64 l = min_t(u64, len, PAGE_SIZE);
2271 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
2274 spin_lock(&sctx->stat_lock);
2275 sctx->stat.malloc_errors++;
2276 spin_unlock(&sctx->stat_lock);
2277 scrub_block_put(sblock);
2280 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2281 scrub_page_get(spage);
2282 sblock->pagev[index] = spage;
2283 spage->sblock = sblock;
2285 spage->flags = flags;
2286 spage->generation = gen;
2287 spage->logical = logical;
2288 spage->physical = physical;
2289 spage->physical_for_dev_replace = physical_for_dev_replace;
2290 spage->mirror_num = mirror_num;
2292 spage->have_csum = 1;
2293 memcpy(spage->csum, csum, sctx->csum_size);
2295 spage->have_csum = 0;
2297 sblock->page_count++;
2298 spage->page = alloc_page(GFP_KERNEL);
2304 physical_for_dev_replace += l;
2307 WARN_ON(sblock->page_count == 0);
2310 * This case should only be hit for RAID 5/6 device replace. See
2311 * the comment in scrub_missing_raid56_pages() for details.
2313 scrub_missing_raid56_pages(sblock);
2315 for (index = 0; index < sblock->page_count; index++) {
2316 struct scrub_page *spage = sblock->pagev[index];
2319 ret = scrub_add_page_to_rd_bio(sctx, spage);
2321 scrub_block_put(sblock);
2330 /* last one frees, either here or in bio completion for last page */
2331 scrub_block_put(sblock);
2335 static void scrub_bio_end_io(struct bio *bio)
2337 struct scrub_bio *sbio = bio->bi_private;
2338 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
2340 sbio->err = bio->bi_error;
2343 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
2346 static void scrub_bio_end_io_worker(struct btrfs_work *work)
2348 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
2349 struct scrub_ctx *sctx = sbio->sctx;
2352 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
2354 for (i = 0; i < sbio->page_count; i++) {
2355 struct scrub_page *spage = sbio->pagev[i];
2357 spage->io_error = 1;
2358 spage->sblock->no_io_error_seen = 0;
2362 /* now complete the scrub_block items that have all pages completed */
2363 for (i = 0; i < sbio->page_count; i++) {
2364 struct scrub_page *spage = sbio->pagev[i];
2365 struct scrub_block *sblock = spage->sblock;
2367 if (atomic_dec_and_test(&sblock->outstanding_pages))
2368 scrub_block_complete(sblock);
2369 scrub_block_put(sblock);
2374 spin_lock(&sctx->list_lock);
2375 sbio->next_free = sctx->first_free;
2376 sctx->first_free = sbio->index;
2377 spin_unlock(&sctx->list_lock);
2379 if (sctx->is_dev_replace &&
2380 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2381 mutex_lock(&sctx->wr_ctx.wr_lock);
2382 scrub_wr_submit(sctx);
2383 mutex_unlock(&sctx->wr_ctx.wr_lock);
2386 scrub_pending_bio_dec(sctx);
2389 static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2390 unsigned long *bitmap,
2395 int sectorsize = sparity->sctx->dev_root->sectorsize;
2397 if (len >= sparity->stripe_len) {
2398 bitmap_set(bitmap, 0, sparity->nsectors);
2402 start -= sparity->logic_start;
2403 start = div_u64_rem(start, sparity->stripe_len, &offset);
2404 offset /= sectorsize;
2405 nsectors = (int)len / sectorsize;
2407 if (offset + nsectors <= sparity->nsectors) {
2408 bitmap_set(bitmap, offset, nsectors);
2412 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2413 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2416 static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2419 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2422 static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2425 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2428 static void scrub_block_complete(struct scrub_block *sblock)
2432 if (!sblock->no_io_error_seen) {
2434 scrub_handle_errored_block(sblock);
2437 * if has checksum error, write via repair mechanism in
2438 * dev replace case, otherwise write here in dev replace
2441 corrupted = scrub_checksum(sblock);
2442 if (!corrupted && sblock->sctx->is_dev_replace)
2443 scrub_write_block_to_dev_replace(sblock);
2446 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2447 u64 start = sblock->pagev[0]->logical;
2448 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2451 scrub_parity_mark_sectors_error(sblock->sparity,
2452 start, end - start);
2456 static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
2458 struct btrfs_ordered_sum *sum = NULL;
2459 unsigned long index;
2460 unsigned long num_sectors;
2462 while (!list_empty(&sctx->csum_list)) {
2463 sum = list_first_entry(&sctx->csum_list,
2464 struct btrfs_ordered_sum, list);
2465 if (sum->bytenr > logical)
2467 if (sum->bytenr + sum->len > logical)
2470 ++sctx->stat.csum_discards;
2471 list_del(&sum->list);
2478 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
2479 num_sectors = sum->len / sctx->sectorsize;
2480 memcpy(csum, sum->sums + index, sctx->csum_size);
2481 if (index == num_sectors - 1) {
2482 list_del(&sum->list);
2488 /* scrub extent tries to collect up to 64 kB for each bio */
2489 static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
2490 u64 physical, struct btrfs_device *dev, u64 flags,
2491 u64 gen, int mirror_num, u64 physical_for_dev_replace)
2494 u8 csum[BTRFS_CSUM_SIZE];
2497 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2498 blocksize = sctx->sectorsize;
2499 spin_lock(&sctx->stat_lock);
2500 sctx->stat.data_extents_scrubbed++;
2501 sctx->stat.data_bytes_scrubbed += len;
2502 spin_unlock(&sctx->stat_lock);
2503 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2504 blocksize = sctx->nodesize;
2505 spin_lock(&sctx->stat_lock);
2506 sctx->stat.tree_extents_scrubbed++;
2507 sctx->stat.tree_bytes_scrubbed += len;
2508 spin_unlock(&sctx->stat_lock);
2510 blocksize = sctx->sectorsize;
2515 u64 l = min_t(u64, len, blocksize);
2518 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2519 /* push csums to sbio */
2520 have_csum = scrub_find_csum(sctx, logical, csum);
2522 ++sctx->stat.no_csum;
2523 if (0 && sctx->is_dev_replace && !have_csum) {
2524 ret = copy_nocow_pages(sctx, logical, l,
2526 physical_for_dev_replace);
2527 goto behind_scrub_pages;
2530 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
2531 mirror_num, have_csum ? csum : NULL, 0,
2532 physical_for_dev_replace);
2539 physical_for_dev_replace += l;
2544 static int scrub_pages_for_parity(struct scrub_parity *sparity,
2545 u64 logical, u64 len,
2546 u64 physical, struct btrfs_device *dev,
2547 u64 flags, u64 gen, int mirror_num, u8 *csum)
2549 struct scrub_ctx *sctx = sparity->sctx;
2550 struct scrub_block *sblock;
2553 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
2555 spin_lock(&sctx->stat_lock);
2556 sctx->stat.malloc_errors++;
2557 spin_unlock(&sctx->stat_lock);
2561 /* one ref inside this function, plus one for each page added to
2563 atomic_set(&sblock->refs, 1);
2564 sblock->sctx = sctx;
2565 sblock->no_io_error_seen = 1;
2566 sblock->sparity = sparity;
2567 scrub_parity_get(sparity);
2569 for (index = 0; len > 0; index++) {
2570 struct scrub_page *spage;
2571 u64 l = min_t(u64, len, PAGE_SIZE);
2573 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
2576 spin_lock(&sctx->stat_lock);
2577 sctx->stat.malloc_errors++;
2578 spin_unlock(&sctx->stat_lock);
2579 scrub_block_put(sblock);
2582 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2583 /* For scrub block */
2584 scrub_page_get(spage);
2585 sblock->pagev[index] = spage;
2586 /* For scrub parity */
2587 scrub_page_get(spage);
2588 list_add_tail(&spage->list, &sparity->spages);
2589 spage->sblock = sblock;
2591 spage->flags = flags;
2592 spage->generation = gen;
2593 spage->logical = logical;
2594 spage->physical = physical;
2595 spage->mirror_num = mirror_num;
2597 spage->have_csum = 1;
2598 memcpy(spage->csum, csum, sctx->csum_size);
2600 spage->have_csum = 0;
2602 sblock->page_count++;
2603 spage->page = alloc_page(GFP_KERNEL);
2611 WARN_ON(sblock->page_count == 0);
2612 for (index = 0; index < sblock->page_count; index++) {
2613 struct scrub_page *spage = sblock->pagev[index];
2616 ret = scrub_add_page_to_rd_bio(sctx, spage);
2618 scrub_block_put(sblock);
2623 /* last one frees, either here or in bio completion for last page */
2624 scrub_block_put(sblock);
2628 static int scrub_extent_for_parity(struct scrub_parity *sparity,
2629 u64 logical, u64 len,
2630 u64 physical, struct btrfs_device *dev,
2631 u64 flags, u64 gen, int mirror_num)
2633 struct scrub_ctx *sctx = sparity->sctx;
2635 u8 csum[BTRFS_CSUM_SIZE];
2639 scrub_parity_mark_sectors_error(sparity, logical, len);
2643 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2644 blocksize = sctx->sectorsize;
2645 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2646 blocksize = sctx->nodesize;
2648 blocksize = sctx->sectorsize;
2653 u64 l = min_t(u64, len, blocksize);
2656 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2657 /* push csums to sbio */
2658 have_csum = scrub_find_csum(sctx, logical, csum);
2662 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2663 flags, gen, mirror_num,
2664 have_csum ? csum : NULL);
2676 * Given a physical address, this will calculate it's
2677 * logical offset. if this is a parity stripe, it will return
2678 * the most left data stripe's logical offset.
2680 * return 0 if it is a data stripe, 1 means parity stripe.
2682 static int get_raid56_logic_offset(u64 physical, int num,
2683 struct map_lookup *map, u64 *offset,
2693 last_offset = (physical - map->stripes[num].physical) *
2694 nr_data_stripes(map);
2696 *stripe_start = last_offset;
2698 *offset = last_offset;
2699 for (i = 0; i < nr_data_stripes(map); i++) {
2700 *offset = last_offset + i * map->stripe_len;
2702 stripe_nr = div_u64(*offset, map->stripe_len);
2703 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
2705 /* Work out the disk rotation on this stripe-set */
2706 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
2707 /* calculate which stripe this data locates */
2709 stripe_index = rot % map->num_stripes;
2710 if (stripe_index == num)
2712 if (stripe_index < num)
2715 *offset = last_offset + j * map->stripe_len;
2719 static void scrub_free_parity(struct scrub_parity *sparity)
2721 struct scrub_ctx *sctx = sparity->sctx;
2722 struct scrub_page *curr, *next;
2725 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2727 spin_lock(&sctx->stat_lock);
2728 sctx->stat.read_errors += nbits;
2729 sctx->stat.uncorrectable_errors += nbits;
2730 spin_unlock(&sctx->stat_lock);
2733 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2734 list_del_init(&curr->list);
2735 scrub_page_put(curr);
2741 static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2743 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2745 struct scrub_ctx *sctx = sparity->sctx;
2747 scrub_free_parity(sparity);
2748 scrub_pending_bio_dec(sctx);
2751 static void scrub_parity_bio_endio(struct bio *bio)
2753 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2756 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2761 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2762 scrub_parity_bio_endio_worker, NULL, NULL);
2763 btrfs_queue_work(sparity->sctx->dev_root->fs_info->scrub_parity_workers,
2767 static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2769 struct scrub_ctx *sctx = sparity->sctx;
2771 struct btrfs_raid_bio *rbio;
2772 struct scrub_page *spage;
2773 struct btrfs_bio *bbio = NULL;
2777 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2781 length = sparity->logic_end - sparity->logic_start;
2782 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
2783 sparity->logic_start,
2784 &length, &bbio, 0, 1);
2785 if (ret || !bbio || !bbio->raid_map)
2788 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2792 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2793 bio->bi_private = sparity;
2794 bio->bi_end_io = scrub_parity_bio_endio;
2796 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
2797 length, sparity->scrub_dev,
2803 list_for_each_entry(spage, &sparity->spages, list)
2804 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2806 scrub_pending_bio_inc(sctx);
2807 raid56_parity_submit_scrub_rbio(rbio);
2813 btrfs_put_bbio(bbio);
2814 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2816 spin_lock(&sctx->stat_lock);
2817 sctx->stat.malloc_errors++;
2818 spin_unlock(&sctx->stat_lock);
2820 scrub_free_parity(sparity);
2823 static inline int scrub_calc_parity_bitmap_len(int nsectors)
2825 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
2828 static void scrub_parity_get(struct scrub_parity *sparity)
2830 atomic_inc(&sparity->refs);
2833 static void scrub_parity_put(struct scrub_parity *sparity)
2835 if (!atomic_dec_and_test(&sparity->refs))
2838 scrub_parity_check_and_repair(sparity);
2841 static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2842 struct map_lookup *map,
2843 struct btrfs_device *sdev,
2844 struct btrfs_path *path,
2848 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2849 struct btrfs_root *root = fs_info->extent_root;
2850 struct btrfs_root *csum_root = fs_info->csum_root;
2851 struct btrfs_extent_item *extent;
2852 struct btrfs_bio *bbio = NULL;
2856 struct extent_buffer *l;
2857 struct btrfs_key key;
2860 u64 extent_physical;
2863 struct btrfs_device *extent_dev;
2864 struct scrub_parity *sparity;
2867 int extent_mirror_num;
2870 nsectors = div_u64(map->stripe_len, root->sectorsize);
2871 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2872 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2875 spin_lock(&sctx->stat_lock);
2876 sctx->stat.malloc_errors++;
2877 spin_unlock(&sctx->stat_lock);
2881 sparity->stripe_len = map->stripe_len;
2882 sparity->nsectors = nsectors;
2883 sparity->sctx = sctx;
2884 sparity->scrub_dev = sdev;
2885 sparity->logic_start = logic_start;
2886 sparity->logic_end = logic_end;
2887 atomic_set(&sparity->refs, 1);
2888 INIT_LIST_HEAD(&sparity->spages);
2889 sparity->dbitmap = sparity->bitmap;
2890 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2893 while (logic_start < logic_end) {
2894 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2895 key.type = BTRFS_METADATA_ITEM_KEY;
2897 key.type = BTRFS_EXTENT_ITEM_KEY;
2898 key.objectid = logic_start;
2899 key.offset = (u64)-1;
2901 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2906 ret = btrfs_previous_extent_item(root, path, 0);
2910 btrfs_release_path(path);
2911 ret = btrfs_search_slot(NULL, root, &key,
2923 slot = path->slots[0];
2924 if (slot >= btrfs_header_nritems(l)) {
2925 ret = btrfs_next_leaf(root, path);
2934 btrfs_item_key_to_cpu(l, &key, slot);
2936 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2937 key.type != BTRFS_METADATA_ITEM_KEY)
2940 if (key.type == BTRFS_METADATA_ITEM_KEY)
2941 bytes = root->nodesize;
2945 if (key.objectid + bytes <= logic_start)
2948 if (key.objectid >= logic_end) {
2953 while (key.objectid >= logic_start + map->stripe_len)
2954 logic_start += map->stripe_len;
2956 extent = btrfs_item_ptr(l, slot,
2957 struct btrfs_extent_item);
2958 flags = btrfs_extent_flags(l, extent);
2959 generation = btrfs_extent_generation(l, extent);
2961 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
2962 (key.objectid < logic_start ||
2963 key.objectid + bytes >
2964 logic_start + map->stripe_len)) {
2966 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2967 key.objectid, logic_start);
2968 spin_lock(&sctx->stat_lock);
2969 sctx->stat.uncorrectable_errors++;
2970 spin_unlock(&sctx->stat_lock);
2974 extent_logical = key.objectid;
2977 if (extent_logical < logic_start) {
2978 extent_len -= logic_start - extent_logical;
2979 extent_logical = logic_start;
2982 if (extent_logical + extent_len >
2983 logic_start + map->stripe_len)
2984 extent_len = logic_start + map->stripe_len -
2987 scrub_parity_mark_sectors_data(sparity, extent_logical,
2990 mapped_length = extent_len;
2992 ret = btrfs_map_block(fs_info, READ, extent_logical,
2993 &mapped_length, &bbio, 0);
2995 if (!bbio || mapped_length < extent_len)
2999 btrfs_put_bbio(bbio);
3002 extent_physical = bbio->stripes[0].physical;
3003 extent_mirror_num = bbio->mirror_num;
3004 extent_dev = bbio->stripes[0].dev;
3005 btrfs_put_bbio(bbio);
3007 ret = btrfs_lookup_csums_range(csum_root,
3009 extent_logical + extent_len - 1,
3010 &sctx->csum_list, 1);
3014 ret = scrub_extent_for_parity(sparity, extent_logical,
3021 scrub_free_csums(sctx);
3026 if (extent_logical + extent_len <
3027 key.objectid + bytes) {
3028 logic_start += map->stripe_len;
3030 if (logic_start >= logic_end) {
3035 if (logic_start < key.objectid + bytes) {
3044 btrfs_release_path(path);
3049 logic_start += map->stripe_len;
3053 scrub_parity_mark_sectors_error(sparity, logic_start,
3054 logic_end - logic_start);
3055 scrub_parity_put(sparity);
3057 mutex_lock(&sctx->wr_ctx.wr_lock);
3058 scrub_wr_submit(sctx);
3059 mutex_unlock(&sctx->wr_ctx.wr_lock);
3061 btrfs_release_path(path);
3062 return ret < 0 ? ret : 0;
3065 static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
3066 struct map_lookup *map,
3067 struct btrfs_device *scrub_dev,
3068 int num, u64 base, u64 length,
3071 struct btrfs_path *path, *ppath;
3072 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3073 struct btrfs_root *root = fs_info->extent_root;
3074 struct btrfs_root *csum_root = fs_info->csum_root;
3075 struct btrfs_extent_item *extent;
3076 struct blk_plug plug;
3081 struct extent_buffer *l;
3088 struct reada_control *reada1;
3089 struct reada_control *reada2;
3090 struct btrfs_key key;
3091 struct btrfs_key key_end;
3092 u64 increment = map->stripe_len;
3095 u64 extent_physical;
3099 struct btrfs_device *extent_dev;
3100 int extent_mirror_num;
3103 physical = map->stripes[num].physical;
3105 nstripes = div_u64(length, map->stripe_len);
3106 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3107 offset = map->stripe_len * num;
3108 increment = map->stripe_len * map->num_stripes;
3110 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3111 int factor = map->num_stripes / map->sub_stripes;
3112 offset = map->stripe_len * (num / map->sub_stripes);
3113 increment = map->stripe_len * factor;
3114 mirror_num = num % map->sub_stripes + 1;
3115 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3116 increment = map->stripe_len;
3117 mirror_num = num % map->num_stripes + 1;
3118 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3119 increment = map->stripe_len;
3120 mirror_num = num % map->num_stripes + 1;
3121 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3122 get_raid56_logic_offset(physical, num, map, &offset, NULL);
3123 increment = map->stripe_len * nr_data_stripes(map);
3126 increment = map->stripe_len;
3130 path = btrfs_alloc_path();
3134 ppath = btrfs_alloc_path();
3136 btrfs_free_path(path);
3141 * work on commit root. The related disk blocks are static as
3142 * long as COW is applied. This means, it is save to rewrite
3143 * them to repair disk errors without any race conditions
3145 path->search_commit_root = 1;
3146 path->skip_locking = 1;
3148 ppath->search_commit_root = 1;
3149 ppath->skip_locking = 1;
3151 * trigger the readahead for extent tree csum tree and wait for
3152 * completion. During readahead, the scrub is officially paused
3153 * to not hold off transaction commits
3155 logical = base + offset;
3156 physical_end = physical + nstripes * map->stripe_len;
3157 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3158 get_raid56_logic_offset(physical_end, num,
3159 map, &logic_end, NULL);
3162 logic_end = logical + increment * nstripes;
3164 wait_event(sctx->list_wait,
3165 atomic_read(&sctx->bios_in_flight) == 0);
3166 scrub_blocked_if_needed(fs_info);
3168 /* FIXME it might be better to start readahead at commit root */
3169 key.objectid = logical;
3170 key.type = BTRFS_EXTENT_ITEM_KEY;
3171 key.offset = (u64)0;
3172 key_end.objectid = logic_end;
3173 key_end.type = BTRFS_METADATA_ITEM_KEY;
3174 key_end.offset = (u64)-1;
3175 reada1 = btrfs_reada_add(root, &key, &key_end);
3177 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3178 key.type = BTRFS_EXTENT_CSUM_KEY;
3179 key.offset = logical;
3180 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3181 key_end.type = BTRFS_EXTENT_CSUM_KEY;
3182 key_end.offset = logic_end;
3183 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
3185 if (!IS_ERR(reada1))
3186 btrfs_reada_wait(reada1);
3187 if (!IS_ERR(reada2))
3188 btrfs_reada_wait(reada2);
3192 * collect all data csums for the stripe to avoid seeking during
3193 * the scrub. This might currently (crc32) end up to be about 1MB
3195 blk_start_plug(&plug);
3198 * now find all extents for each stripe and scrub them
3201 while (physical < physical_end) {
3205 if (atomic_read(&fs_info->scrub_cancel_req) ||
3206 atomic_read(&sctx->cancel_req)) {
3211 * check to see if we have to pause
3213 if (atomic_read(&fs_info->scrub_pause_req)) {
3214 /* push queued extents */
3215 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3217 mutex_lock(&sctx->wr_ctx.wr_lock);
3218 scrub_wr_submit(sctx);
3219 mutex_unlock(&sctx->wr_ctx.wr_lock);
3220 wait_event(sctx->list_wait,
3221 atomic_read(&sctx->bios_in_flight) == 0);
3222 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3223 scrub_blocked_if_needed(fs_info);
3226 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3227 ret = get_raid56_logic_offset(physical, num, map,
3232 /* it is parity strip */
3233 stripe_logical += base;
3234 stripe_end = stripe_logical + increment;
3235 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3236 ppath, stripe_logical,
3244 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3245 key.type = BTRFS_METADATA_ITEM_KEY;
3247 key.type = BTRFS_EXTENT_ITEM_KEY;
3248 key.objectid = logical;
3249 key.offset = (u64)-1;
3251 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3256 ret = btrfs_previous_extent_item(root, path, 0);
3260 /* there's no smaller item, so stick with the
3262 btrfs_release_path(path);
3263 ret = btrfs_search_slot(NULL, root, &key,
3275 slot = path->slots[0];
3276 if (slot >= btrfs_header_nritems(l)) {
3277 ret = btrfs_next_leaf(root, path);
3286 btrfs_item_key_to_cpu(l, &key, slot);
3288 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3289 key.type != BTRFS_METADATA_ITEM_KEY)
3292 if (key.type == BTRFS_METADATA_ITEM_KEY)
3293 bytes = root->nodesize;
3297 if (key.objectid + bytes <= logical)
3300 if (key.objectid >= logical + map->stripe_len) {
3301 /* out of this device extent */
3302 if (key.objectid >= logic_end)
3307 extent = btrfs_item_ptr(l, slot,
3308 struct btrfs_extent_item);
3309 flags = btrfs_extent_flags(l, extent);
3310 generation = btrfs_extent_generation(l, extent);
3312 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3313 (key.objectid < logical ||
3314 key.objectid + bytes >
3315 logical + map->stripe_len)) {
3317 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
3318 key.objectid, logical);
3319 spin_lock(&sctx->stat_lock);
3320 sctx->stat.uncorrectable_errors++;
3321 spin_unlock(&sctx->stat_lock);
3326 extent_logical = key.objectid;
3330 * trim extent to this stripe
3332 if (extent_logical < logical) {
3333 extent_len -= logical - extent_logical;
3334 extent_logical = logical;
3336 if (extent_logical + extent_len >
3337 logical + map->stripe_len) {
3338 extent_len = logical + map->stripe_len -
3342 extent_physical = extent_logical - logical + physical;
3343 extent_dev = scrub_dev;
3344 extent_mirror_num = mirror_num;
3346 scrub_remap_extent(fs_info, extent_logical,
3347 extent_len, &extent_physical,
3349 &extent_mirror_num);
3351 ret = btrfs_lookup_csums_range(csum_root,
3355 &sctx->csum_list, 1);
3359 ret = scrub_extent(sctx, extent_logical, extent_len,
3360 extent_physical, extent_dev, flags,
3361 generation, extent_mirror_num,
3362 extent_logical - logical + physical);
3364 scrub_free_csums(sctx);
3369 if (extent_logical + extent_len <
3370 key.objectid + bytes) {
3371 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3373 * loop until we find next data stripe
3374 * or we have finished all stripes.
3377 physical += map->stripe_len;
3378 ret = get_raid56_logic_offset(physical,
3383 if (ret && physical < physical_end) {
3384 stripe_logical += base;
3385 stripe_end = stripe_logical +
3387 ret = scrub_raid56_parity(sctx,
3388 map, scrub_dev, ppath,
3396 physical += map->stripe_len;
3397 logical += increment;
3399 if (logical < key.objectid + bytes) {
3404 if (physical >= physical_end) {
3412 btrfs_release_path(path);
3414 logical += increment;
3415 physical += map->stripe_len;
3416 spin_lock(&sctx->stat_lock);
3418 sctx->stat.last_physical = map->stripes[num].physical +
3421 sctx->stat.last_physical = physical;
3422 spin_unlock(&sctx->stat_lock);
3427 /* push queued extents */
3429 mutex_lock(&sctx->wr_ctx.wr_lock);
3430 scrub_wr_submit(sctx);
3431 mutex_unlock(&sctx->wr_ctx.wr_lock);
3433 blk_finish_plug(&plug);
3434 btrfs_free_path(path);
3435 btrfs_free_path(ppath);
3436 return ret < 0 ? ret : 0;
3439 static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
3440 struct btrfs_device *scrub_dev,
3441 u64 chunk_offset, u64 length,
3443 struct btrfs_block_group_cache *cache,
3446 struct btrfs_mapping_tree *map_tree =
3447 &sctx->dev_root->fs_info->mapping_tree;
3448 struct map_lookup *map;
3449 struct extent_map *em;
3453 read_lock(&map_tree->map_tree.lock);
3454 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3455 read_unlock(&map_tree->map_tree.lock);
3459 * Might have been an unused block group deleted by the cleaner
3460 * kthread or relocation.
3462 spin_lock(&cache->lock);
3463 if (!cache->removed)
3465 spin_unlock(&cache->lock);
3470 map = em->map_lookup;
3471 if (em->start != chunk_offset)
3474 if (em->len < length)
3477 for (i = 0; i < map->num_stripes; ++i) {
3478 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
3479 map->stripes[i].physical == dev_offset) {
3480 ret = scrub_stripe(sctx, map, scrub_dev, i,
3481 chunk_offset, length,
3488 free_extent_map(em);
3493 static noinline_for_stack
3494 int scrub_enumerate_chunks(struct scrub_ctx *sctx,
3495 struct btrfs_device *scrub_dev, u64 start, u64 end,
3498 struct btrfs_dev_extent *dev_extent = NULL;
3499 struct btrfs_path *path;
3500 struct btrfs_root *root = sctx->dev_root;
3501 struct btrfs_fs_info *fs_info = root->fs_info;
3507 struct extent_buffer *l;
3508 struct btrfs_key key;
3509 struct btrfs_key found_key;
3510 struct btrfs_block_group_cache *cache;
3511 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
3513 path = btrfs_alloc_path();
3517 path->reada = READA_FORWARD;
3518 path->search_commit_root = 1;
3519 path->skip_locking = 1;
3521 key.objectid = scrub_dev->devid;
3523 key.type = BTRFS_DEV_EXTENT_KEY;
3526 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3530 if (path->slots[0] >=
3531 btrfs_header_nritems(path->nodes[0])) {
3532 ret = btrfs_next_leaf(root, path);
3545 slot = path->slots[0];
3547 btrfs_item_key_to_cpu(l, &found_key, slot);
3549 if (found_key.objectid != scrub_dev->devid)
3552 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
3555 if (found_key.offset >= end)
3558 if (found_key.offset < key.offset)
3561 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3562 length = btrfs_dev_extent_length(l, dev_extent);
3564 if (found_key.offset + length <= start)
3567 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3570 * get a reference on the corresponding block group to prevent
3571 * the chunk from going away while we scrub it
3573 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3575 /* some chunks are removed but not committed to disk yet,
3576 * continue scrubbing */
3581 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3582 * to avoid deadlock caused by:
3583 * btrfs_inc_block_group_ro()
3584 * -> btrfs_wait_for_commit()
3585 * -> btrfs_commit_transaction()
3586 * -> btrfs_scrub_pause()
3588 scrub_pause_on(fs_info);
3589 ret = btrfs_inc_block_group_ro(root, cache);
3590 if (!ret && is_dev_replace) {
3592 * If we are doing a device replace wait for any tasks
3593 * that started dellaloc right before we set the block
3594 * group to RO mode, as they might have just allocated
3595 * an extent from it or decided they could do a nocow
3596 * write. And if any such tasks did that, wait for their
3597 * ordered extents to complete and then commit the
3598 * current transaction, so that we can later see the new
3599 * extent items in the extent tree - the ordered extents
3600 * create delayed data references (for cow writes) when
3601 * they complete, which will be run and insert the
3602 * corresponding extent items into the extent tree when
3603 * we commit the transaction they used when running
3604 * inode.c:btrfs_finish_ordered_io(). We later use
3605 * the commit root of the extent tree to find extents
3606 * to copy from the srcdev into the tgtdev, and we don't
3607 * want to miss any new extents.
3609 btrfs_wait_block_group_reservations(cache);
3610 btrfs_wait_nocow_writers(cache);
3611 ret = btrfs_wait_ordered_roots(fs_info, -1,
3612 cache->key.objectid,
3615 struct btrfs_trans_handle *trans;
3617 trans = btrfs_join_transaction(root);
3619 ret = PTR_ERR(trans);
3621 ret = btrfs_commit_transaction(trans,
3624 scrub_pause_off(fs_info);
3625 btrfs_put_block_group(cache);
3630 scrub_pause_off(fs_info);
3634 } else if (ret == -ENOSPC) {
3636 * btrfs_inc_block_group_ro return -ENOSPC when it
3637 * failed in creating new chunk for metadata.
3638 * It is not a problem for scrub/replace, because
3639 * metadata are always cowed, and our scrub paused
3640 * commit_transactions.
3645 "failed setting block group ro, ret=%d\n",
3647 btrfs_put_block_group(cache);
3651 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
3652 dev_replace->cursor_right = found_key.offset + length;
3653 dev_replace->cursor_left = found_key.offset;
3654 dev_replace->item_needs_writeback = 1;
3655 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
3656 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
3657 found_key.offset, cache, is_dev_replace);
3660 * flush, submit all pending read and write bios, afterwards
3662 * Note that in the dev replace case, a read request causes
3663 * write requests that are submitted in the read completion
3664 * worker. Therefore in the current situation, it is required
3665 * that all write requests are flushed, so that all read and
3666 * write requests are really completed when bios_in_flight
3669 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3671 mutex_lock(&sctx->wr_ctx.wr_lock);
3672 scrub_wr_submit(sctx);
3673 mutex_unlock(&sctx->wr_ctx.wr_lock);
3675 wait_event(sctx->list_wait,
3676 atomic_read(&sctx->bios_in_flight) == 0);
3678 scrub_pause_on(fs_info);
3681 * must be called before we decrease @scrub_paused.
3682 * make sure we don't block transaction commit while
3683 * we are waiting pending workers finished.
3685 wait_event(sctx->list_wait,
3686 atomic_read(&sctx->workers_pending) == 0);
3687 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3689 scrub_pause_off(fs_info);
3691 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
3692 dev_replace->cursor_left = dev_replace->cursor_right;
3693 dev_replace->item_needs_writeback = 1;
3694 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
3697 btrfs_dec_block_group_ro(root, cache);
3700 * We might have prevented the cleaner kthread from deleting
3701 * this block group if it was already unused because we raced
3702 * and set it to RO mode first. So add it back to the unused
3703 * list, otherwise it might not ever be deleted unless a manual
3704 * balance is triggered or it becomes used and unused again.
3706 spin_lock(&cache->lock);
3707 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3708 btrfs_block_group_used(&cache->item) == 0) {
3709 spin_unlock(&cache->lock);
3710 spin_lock(&fs_info->unused_bgs_lock);
3711 if (list_empty(&cache->bg_list)) {
3712 btrfs_get_block_group(cache);
3713 list_add_tail(&cache->bg_list,
3714 &fs_info->unused_bgs);
3716 spin_unlock(&fs_info->unused_bgs_lock);
3718 spin_unlock(&cache->lock);
3721 btrfs_put_block_group(cache);
3724 if (is_dev_replace &&
3725 atomic64_read(&dev_replace->num_write_errors) > 0) {
3729 if (sctx->stat.malloc_errors > 0) {
3734 key.offset = found_key.offset + length;
3735 btrfs_release_path(path);
3738 btrfs_free_path(path);
3743 static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3744 struct btrfs_device *scrub_dev)
3750 struct btrfs_root *root = sctx->dev_root;
3752 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
3755 /* Seed devices of a new filesystem has their own generation. */
3756 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3757 gen = scrub_dev->generation;
3759 gen = root->fs_info->last_trans_committed;
3761 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3762 bytenr = btrfs_sb_offset(i);
3763 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3764 scrub_dev->commit_total_bytes)
3767 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
3768 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
3773 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3779 * get a reference count on fs_info->scrub_workers. start worker if necessary
3781 static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3784 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
3785 int max_active = fs_info->thread_pool_size;
3787 if (fs_info->scrub_workers_refcnt == 0) {
3789 fs_info->scrub_workers =
3790 btrfs_alloc_workqueue(fs_info, "scrub", flags,
3793 fs_info->scrub_workers =
3794 btrfs_alloc_workqueue(fs_info, "scrub", flags,
3796 if (!fs_info->scrub_workers)
3797 goto fail_scrub_workers;
3799 fs_info->scrub_wr_completion_workers =
3800 btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
3802 if (!fs_info->scrub_wr_completion_workers)
3803 goto fail_scrub_wr_completion_workers;
3805 fs_info->scrub_nocow_workers =
3806 btrfs_alloc_workqueue(fs_info, "scrubnc", flags, 1, 0);
3807 if (!fs_info->scrub_nocow_workers)
3808 goto fail_scrub_nocow_workers;
3809 fs_info->scrub_parity_workers =
3810 btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
3812 if (!fs_info->scrub_parity_workers)
3813 goto fail_scrub_parity_workers;
3815 ++fs_info->scrub_workers_refcnt;
3818 fail_scrub_parity_workers:
3819 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
3820 fail_scrub_nocow_workers:
3821 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3822 fail_scrub_wr_completion_workers:
3823 btrfs_destroy_workqueue(fs_info->scrub_workers);
3828 static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
3830 if (--fs_info->scrub_workers_refcnt == 0) {
3831 btrfs_destroy_workqueue(fs_info->scrub_workers);
3832 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3833 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
3834 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
3836 WARN_ON(fs_info->scrub_workers_refcnt < 0);
3839 int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3840 u64 end, struct btrfs_scrub_progress *progress,
3841 int readonly, int is_dev_replace)
3843 struct scrub_ctx *sctx;
3845 struct btrfs_device *dev;
3846 struct rcu_string *name;
3848 if (btrfs_fs_closing(fs_info))
3851 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
3853 * in this case scrub is unable to calculate the checksum
3854 * the way scrub is implemented. Do not handle this
3855 * situation at all because it won't ever happen.
3858 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
3859 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
3863 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
3864 /* not supported for data w/o checksums */
3865 btrfs_err_rl(fs_info,
3866 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
3867 fs_info->chunk_root->sectorsize, PAGE_SIZE);
3871 if (fs_info->chunk_root->nodesize >
3872 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3873 fs_info->chunk_root->sectorsize >
3874 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3876 * would exhaust the array bounds of pagev member in
3877 * struct scrub_block
3880 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
3881 fs_info->chunk_root->nodesize,
3882 SCRUB_MAX_PAGES_PER_BLOCK,
3883 fs_info->chunk_root->sectorsize,
3884 SCRUB_MAX_PAGES_PER_BLOCK);
3889 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3890 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
3891 if (!dev || (dev->missing && !is_dev_replace)) {
3892 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3896 if (!is_dev_replace && !readonly && !dev->writeable) {
3897 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3899 name = rcu_dereference(dev->name);
3900 btrfs_err(fs_info, "scrub: device %s is not writable",
3906 mutex_lock(&fs_info->scrub_lock);
3907 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
3908 mutex_unlock(&fs_info->scrub_lock);
3909 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3913 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
3914 if (dev->scrub_device ||
3916 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3917 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
3918 mutex_unlock(&fs_info->scrub_lock);
3919 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3920 return -EINPROGRESS;
3922 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
3924 ret = scrub_workers_get(fs_info, is_dev_replace);
3926 mutex_unlock(&fs_info->scrub_lock);
3927 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3931 sctx = scrub_setup_ctx(dev, is_dev_replace);
3933 mutex_unlock(&fs_info->scrub_lock);
3934 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3935 scrub_workers_put(fs_info);
3936 return PTR_ERR(sctx);
3938 sctx->readonly = readonly;
3939 dev->scrub_device = sctx;
3940 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3943 * checking @scrub_pause_req here, we can avoid
3944 * race between committing transaction and scrubbing.
3946 __scrub_blocked_if_needed(fs_info);
3947 atomic_inc(&fs_info->scrubs_running);
3948 mutex_unlock(&fs_info->scrub_lock);
3950 if (!is_dev_replace) {
3952 * by holding device list mutex, we can
3953 * kick off writing super in log tree sync.
3955 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3956 ret = scrub_supers(sctx, dev);
3957 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3961 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3964 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3965 atomic_dec(&fs_info->scrubs_running);
3966 wake_up(&fs_info->scrub_pause_wait);
3968 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
3971 memcpy(progress, &sctx->stat, sizeof(*progress));
3973 mutex_lock(&fs_info->scrub_lock);
3974 dev->scrub_device = NULL;
3975 scrub_workers_put(fs_info);
3976 mutex_unlock(&fs_info->scrub_lock);
3978 scrub_put_ctx(sctx);
3983 void btrfs_scrub_pause(struct btrfs_root *root)
3985 struct btrfs_fs_info *fs_info = root->fs_info;
3987 mutex_lock(&fs_info->scrub_lock);
3988 atomic_inc(&fs_info->scrub_pause_req);
3989 while (atomic_read(&fs_info->scrubs_paused) !=
3990 atomic_read(&fs_info->scrubs_running)) {
3991 mutex_unlock(&fs_info->scrub_lock);
3992 wait_event(fs_info->scrub_pause_wait,
3993 atomic_read(&fs_info->scrubs_paused) ==
3994 atomic_read(&fs_info->scrubs_running));
3995 mutex_lock(&fs_info->scrub_lock);
3997 mutex_unlock(&fs_info->scrub_lock);
4000 void btrfs_scrub_continue(struct btrfs_root *root)
4002 struct btrfs_fs_info *fs_info = root->fs_info;
4004 atomic_dec(&fs_info->scrub_pause_req);
4005 wake_up(&fs_info->scrub_pause_wait);
4008 int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
4010 mutex_lock(&fs_info->scrub_lock);
4011 if (!atomic_read(&fs_info->scrubs_running)) {
4012 mutex_unlock(&fs_info->scrub_lock);
4016 atomic_inc(&fs_info->scrub_cancel_req);
4017 while (atomic_read(&fs_info->scrubs_running)) {
4018 mutex_unlock(&fs_info->scrub_lock);
4019 wait_event(fs_info->scrub_pause_wait,
4020 atomic_read(&fs_info->scrubs_running) == 0);
4021 mutex_lock(&fs_info->scrub_lock);
4023 atomic_dec(&fs_info->scrub_cancel_req);
4024 mutex_unlock(&fs_info->scrub_lock);
4029 int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
4030 struct btrfs_device *dev)
4032 struct scrub_ctx *sctx;
4034 mutex_lock(&fs_info->scrub_lock);
4035 sctx = dev->scrub_device;
4037 mutex_unlock(&fs_info->scrub_lock);
4040 atomic_inc(&sctx->cancel_req);
4041 while (dev->scrub_device) {
4042 mutex_unlock(&fs_info->scrub_lock);
4043 wait_event(fs_info->scrub_pause_wait,
4044 dev->scrub_device == NULL);
4045 mutex_lock(&fs_info->scrub_lock);
4047 mutex_unlock(&fs_info->scrub_lock);
4052 int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
4053 struct btrfs_scrub_progress *progress)
4055 struct btrfs_device *dev;
4056 struct scrub_ctx *sctx = NULL;
4058 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
4059 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
4061 sctx = dev->scrub_device;
4063 memcpy(progress, &sctx->stat, sizeof(*progress));
4064 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
4066 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
4069 static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4070 u64 extent_logical, u64 extent_len,
4071 u64 *extent_physical,
4072 struct btrfs_device **extent_dev,
4073 int *extent_mirror_num)
4076 struct btrfs_bio *bbio = NULL;
4079 mapped_length = extent_len;
4080 ret = btrfs_map_block(fs_info, READ, extent_logical,
4081 &mapped_length, &bbio, 0);
4082 if (ret || !bbio || mapped_length < extent_len ||
4083 !bbio->stripes[0].dev->bdev) {
4084 btrfs_put_bbio(bbio);
4088 *extent_physical = bbio->stripes[0].physical;
4089 *extent_mirror_num = bbio->mirror_num;
4090 *extent_dev = bbio->stripes[0].dev;
4091 btrfs_put_bbio(bbio);
4094 static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
4095 struct scrub_wr_ctx *wr_ctx,
4096 struct btrfs_fs_info *fs_info,
4097 struct btrfs_device *dev,
4100 WARN_ON(wr_ctx->wr_curr_bio != NULL);
4102 mutex_init(&wr_ctx->wr_lock);
4103 wr_ctx->wr_curr_bio = NULL;
4104 if (!is_dev_replace)
4107 WARN_ON(!dev->bdev);
4108 wr_ctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
4109 wr_ctx->tgtdev = dev;
4110 atomic_set(&wr_ctx->flush_all_writes, 0);
4114 static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
4116 mutex_lock(&wr_ctx->wr_lock);
4117 kfree(wr_ctx->wr_curr_bio);
4118 wr_ctx->wr_curr_bio = NULL;
4119 mutex_unlock(&wr_ctx->wr_lock);
4122 static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
4123 int mirror_num, u64 physical_for_dev_replace)
4125 struct scrub_copy_nocow_ctx *nocow_ctx;
4126 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
4128 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
4130 spin_lock(&sctx->stat_lock);
4131 sctx->stat.malloc_errors++;
4132 spin_unlock(&sctx->stat_lock);
4136 scrub_pending_trans_workers_inc(sctx);
4138 nocow_ctx->sctx = sctx;
4139 nocow_ctx->logical = logical;
4140 nocow_ctx->len = len;
4141 nocow_ctx->mirror_num = mirror_num;
4142 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
4143 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
4144 copy_nocow_pages_worker, NULL, NULL);
4145 INIT_LIST_HEAD(&nocow_ctx->inodes);
4146 btrfs_queue_work(fs_info->scrub_nocow_workers,
4152 static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
4154 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
4155 struct scrub_nocow_inode *nocow_inode;
4157 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
4160 nocow_inode->inum = inum;
4161 nocow_inode->offset = offset;
4162 nocow_inode->root = root;
4163 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
4167 #define COPY_COMPLETE 1
4169 static void copy_nocow_pages_worker(struct btrfs_work *work)
4171 struct scrub_copy_nocow_ctx *nocow_ctx =
4172 container_of(work, struct scrub_copy_nocow_ctx, work);
4173 struct scrub_ctx *sctx = nocow_ctx->sctx;
4174 u64 logical = nocow_ctx->logical;
4175 u64 len = nocow_ctx->len;
4176 int mirror_num = nocow_ctx->mirror_num;
4177 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4179 struct btrfs_trans_handle *trans = NULL;
4180 struct btrfs_fs_info *fs_info;
4181 struct btrfs_path *path;
4182 struct btrfs_root *root;
4183 int not_written = 0;
4185 fs_info = sctx->dev_root->fs_info;
4186 root = fs_info->extent_root;
4188 path = btrfs_alloc_path();
4190 spin_lock(&sctx->stat_lock);
4191 sctx->stat.malloc_errors++;
4192 spin_unlock(&sctx->stat_lock);
4197 trans = btrfs_join_transaction(root);
4198 if (IS_ERR(trans)) {
4203 ret = iterate_inodes_from_logical(logical, fs_info, path,
4204 record_inode_for_nocow, nocow_ctx);
4205 if (ret != 0 && ret != -ENOENT) {
4207 "iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d",
4208 logical, physical_for_dev_replace, len, mirror_num,
4214 btrfs_end_transaction(trans, root);
4216 while (!list_empty(&nocow_ctx->inodes)) {
4217 struct scrub_nocow_inode *entry;
4218 entry = list_first_entry(&nocow_ctx->inodes,
4219 struct scrub_nocow_inode,
4221 list_del_init(&entry->list);
4222 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4223 entry->root, nocow_ctx);
4225 if (ret == COPY_COMPLETE) {
4233 while (!list_empty(&nocow_ctx->inodes)) {
4234 struct scrub_nocow_inode *entry;
4235 entry = list_first_entry(&nocow_ctx->inodes,
4236 struct scrub_nocow_inode,
4238 list_del_init(&entry->list);
4241 if (trans && !IS_ERR(trans))
4242 btrfs_end_transaction(trans, root);
4244 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4245 num_uncorrectable_read_errors);
4247 btrfs_free_path(path);
4250 scrub_pending_trans_workers_dec(sctx);
4253 static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4256 struct extent_state *cached_state = NULL;
4257 struct btrfs_ordered_extent *ordered;
4258 struct extent_io_tree *io_tree;
4259 struct extent_map *em;
4260 u64 lockstart = start, lockend = start + len - 1;
4263 io_tree = &BTRFS_I(inode)->io_tree;
4265 lock_extent_bits(io_tree, lockstart, lockend, &cached_state);
4266 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4268 btrfs_put_ordered_extent(ordered);
4273 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4280 * This extent does not actually cover the logical extent anymore,
4281 * move on to the next inode.
4283 if (em->block_start > logical ||
4284 em->block_start + em->block_len < logical + len) {
4285 free_extent_map(em);
4289 free_extent_map(em);
4292 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4297 static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4298 struct scrub_copy_nocow_ctx *nocow_ctx)
4300 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
4301 struct btrfs_key key;
4302 struct inode *inode;
4304 struct btrfs_root *local_root;
4305 struct extent_io_tree *io_tree;
4306 u64 physical_for_dev_replace;
4307 u64 nocow_ctx_logical;
4308 u64 len = nocow_ctx->len;
4309 unsigned long index;
4314 key.objectid = root;
4315 key.type = BTRFS_ROOT_ITEM_KEY;
4316 key.offset = (u64)-1;
4318 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4320 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
4321 if (IS_ERR(local_root)) {
4322 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
4323 return PTR_ERR(local_root);
4326 key.type = BTRFS_INODE_ITEM_KEY;
4327 key.objectid = inum;
4329 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
4330 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
4332 return PTR_ERR(inode);
4334 /* Avoid truncate/dio/punch hole.. */
4336 inode_dio_wait(inode);
4338 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4339 io_tree = &BTRFS_I(inode)->io_tree;
4340 nocow_ctx_logical = nocow_ctx->logical;
4342 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4344 ret = ret > 0 ? 0 : ret;
4348 while (len >= PAGE_SIZE) {
4349 index = offset >> PAGE_SHIFT;
4351 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4353 btrfs_err(fs_info, "find_or_create_page() failed");
4358 if (PageUptodate(page)) {
4359 if (PageDirty(page))
4362 ClearPageError(page);
4363 err = extent_read_full_page(io_tree, page,
4365 nocow_ctx->mirror_num);
4373 * If the page has been remove from the page cache,
4374 * the data on it is meaningless, because it may be
4375 * old one, the new data may be written into the new
4376 * page in the page cache.
4378 if (page->mapping != inode->i_mapping) {
4383 if (!PageUptodate(page)) {
4389 ret = check_extent_to_block(inode, offset, len,
4392 ret = ret > 0 ? 0 : ret;
4396 err = write_page_nocow(nocow_ctx->sctx,
4397 physical_for_dev_replace, page);
4407 offset += PAGE_SIZE;
4408 physical_for_dev_replace += PAGE_SIZE;
4409 nocow_ctx_logical += PAGE_SIZE;
4412 ret = COPY_COMPLETE;
4414 inode_unlock(inode);
4419 static int write_page_nocow(struct scrub_ctx *sctx,
4420 u64 physical_for_dev_replace, struct page *page)
4423 struct btrfs_device *dev;
4426 dev = sctx->wr_ctx.tgtdev;
4430 btrfs_warn_rl(dev->dev_root->fs_info,
4431 "scrub write_page_nocow(bdev == NULL) is unexpected");
4434 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
4436 spin_lock(&sctx->stat_lock);
4437 sctx->stat.malloc_errors++;
4438 spin_unlock(&sctx->stat_lock);
4441 bio->bi_iter.bi_size = 0;
4442 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
4443 bio->bi_bdev = dev->bdev;
4444 bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_SYNC);
4445 ret = bio_add_page(bio, page, PAGE_SIZE, 0);
4446 if (ret != PAGE_SIZE) {
4449 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4453 if (btrfsic_submit_bio_wait(bio))
4454 goto leave_with_eio;