2 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/blkdev.h>
20 #include <linux/ratelimit.h>
24 #include "ordered-data.h"
25 #include "transaction.h"
27 #include "extent_io.h"
28 #include "dev-replace.h"
29 #include "check-integrity.h"
30 #include "rcu-string.h"
34 * This is only the first step towards a full-features scrub. It reads all
35 * extent and super block and verifies the checksums. In case a bad checksum
36 * is found or the extent cannot be read, good data will be written back if
39 * Future enhancements:
40 * - In case an unrepairable extent is encountered, track which files are
41 * affected and report them
42 * - track and record media errors, throw out bad devices
43 * - add a mode to also read unallocated space
50 * the following three values only influence the performance.
51 * The last one configures the number of parallel and outstanding I/O
52 * operations. The first two values configure an upper limit for the number
53 * of (dynamically allocated) pages that are added to a bio.
55 #define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
56 #define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
57 #define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
60 * the following value times PAGE_SIZE needs to be large enough to match the
61 * largest node/leaf/sector size that shall be supported.
62 * Values larger than BTRFS_STRIPE_LEN are not supported.
64 #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
66 struct scrub_recover {
68 struct btrfs_bio *bbio;
73 struct scrub_block *sblock;
75 struct btrfs_device *dev;
76 struct list_head list;
77 u64 flags; /* extent flags */
81 u64 physical_for_dev_replace;
84 unsigned int mirror_num:8;
85 unsigned int have_csum:1;
86 unsigned int io_error:1;
88 u8 csum[BTRFS_CSUM_SIZE];
90 struct scrub_recover *recover;
95 struct scrub_ctx *sctx;
96 struct btrfs_device *dev;
101 #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
102 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
104 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
108 struct btrfs_work work;
112 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
114 atomic_t outstanding_pages;
115 atomic_t refs; /* free mem on transition to zero */
116 struct scrub_ctx *sctx;
117 struct scrub_parity *sparity;
119 unsigned int header_error:1;
120 unsigned int checksum_error:1;
121 unsigned int no_io_error_seen:1;
122 unsigned int generation_error:1; /* also sets header_error */
124 /* The following is for the data used to check parity */
125 /* It is for the data with checksum */
126 unsigned int data_corrected:1;
130 /* Used for the chunks with parity stripe such RAID5/6 */
131 struct scrub_parity {
132 struct scrub_ctx *sctx;
134 struct btrfs_device *scrub_dev;
146 struct list_head spages;
148 /* Work of parity check and repair */
149 struct btrfs_work work;
151 /* Mark the parity blocks which have data */
152 unsigned long *dbitmap;
155 * Mark the parity blocks which have data, but errors happen when
156 * read data or check data
158 unsigned long *ebitmap;
160 unsigned long bitmap[0];
163 struct scrub_wr_ctx {
164 struct scrub_bio *wr_curr_bio;
165 struct btrfs_device *tgtdev;
166 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
167 atomic_t flush_all_writes;
168 struct mutex wr_lock;
172 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
173 struct btrfs_root *dev_root;
176 atomic_t bios_in_flight;
177 atomic_t workers_pending;
178 spinlock_t list_lock;
179 wait_queue_head_t list_wait;
181 struct list_head csum_list;
184 int pages_per_rd_bio;
189 struct scrub_wr_ctx wr_ctx;
194 struct btrfs_scrub_progress stat;
195 spinlock_t stat_lock;
198 * Use a ref counter to avoid use-after-free issues. Scrub workers
199 * decrement bios_in_flight and workers_pending and then do a wakeup
200 * on the list_wait wait queue. We must ensure the main scrub task
201 * doesn't free the scrub context before or while the workers are
202 * doing the wakeup() call.
207 struct scrub_fixup_nodatasum {
208 struct scrub_ctx *sctx;
209 struct btrfs_device *dev;
211 struct btrfs_root *root;
212 struct btrfs_work work;
216 struct scrub_nocow_inode {
220 struct list_head list;
223 struct scrub_copy_nocow_ctx {
224 struct scrub_ctx *sctx;
228 u64 physical_for_dev_replace;
229 struct list_head inodes;
230 struct btrfs_work work;
233 struct scrub_warning {
234 struct btrfs_path *path;
235 u64 extent_item_size;
239 struct btrfs_device *dev;
242 static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
243 static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
244 static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
245 static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
246 static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
247 static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
248 struct scrub_block *sblocks_for_recheck);
249 static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
250 struct scrub_block *sblock, int is_metadata,
251 int have_csum, u8 *csum, u64 generation,
252 u16 csum_size, int retry_failed_mirror);
253 static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
254 struct scrub_block *sblock,
255 int is_metadata, int have_csum,
256 const u8 *csum, u64 generation,
258 static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
259 struct scrub_block *sblock_good);
260 static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
261 struct scrub_block *sblock_good,
262 int page_num, int force_write);
263 static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
264 static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
266 static int scrub_checksum_data(struct scrub_block *sblock);
267 static int scrub_checksum_tree_block(struct scrub_block *sblock);
268 static int scrub_checksum_super(struct scrub_block *sblock);
269 static void scrub_block_get(struct scrub_block *sblock);
270 static void scrub_block_put(struct scrub_block *sblock);
271 static void scrub_page_get(struct scrub_page *spage);
272 static void scrub_page_put(struct scrub_page *spage);
273 static void scrub_parity_get(struct scrub_parity *sparity);
274 static void scrub_parity_put(struct scrub_parity *sparity);
275 static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
276 struct scrub_page *spage);
277 static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
278 u64 physical, struct btrfs_device *dev, u64 flags,
279 u64 gen, int mirror_num, u8 *csum, int force,
280 u64 physical_for_dev_replace);
281 static void scrub_bio_end_io(struct bio *bio);
282 static void scrub_bio_end_io_worker(struct btrfs_work *work);
283 static void scrub_block_complete(struct scrub_block *sblock);
284 static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
285 u64 extent_logical, u64 extent_len,
286 u64 *extent_physical,
287 struct btrfs_device **extent_dev,
288 int *extent_mirror_num);
289 static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
290 struct scrub_wr_ctx *wr_ctx,
291 struct btrfs_fs_info *fs_info,
292 struct btrfs_device *dev,
294 static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
295 static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
296 struct scrub_page *spage);
297 static void scrub_wr_submit(struct scrub_ctx *sctx);
298 static void scrub_wr_bio_end_io(struct bio *bio);
299 static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
300 static int write_page_nocow(struct scrub_ctx *sctx,
301 u64 physical_for_dev_replace, struct page *page);
302 static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
303 struct scrub_copy_nocow_ctx *ctx);
304 static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
305 int mirror_num, u64 physical_for_dev_replace);
306 static void copy_nocow_pages_worker(struct btrfs_work *work);
307 static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
308 static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
309 static void scrub_put_ctx(struct scrub_ctx *sctx);
312 static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
314 atomic_inc(&sctx->refs);
315 atomic_inc(&sctx->bios_in_flight);
318 static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
320 atomic_dec(&sctx->bios_in_flight);
321 wake_up(&sctx->list_wait);
325 static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
327 while (atomic_read(&fs_info->scrub_pause_req)) {
328 mutex_unlock(&fs_info->scrub_lock);
329 wait_event(fs_info->scrub_pause_wait,
330 atomic_read(&fs_info->scrub_pause_req) == 0);
331 mutex_lock(&fs_info->scrub_lock);
335 static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
337 atomic_inc(&fs_info->scrubs_paused);
338 wake_up(&fs_info->scrub_pause_wait);
340 mutex_lock(&fs_info->scrub_lock);
341 __scrub_blocked_if_needed(fs_info);
342 atomic_dec(&fs_info->scrubs_paused);
343 mutex_unlock(&fs_info->scrub_lock);
345 wake_up(&fs_info->scrub_pause_wait);
349 * used for workers that require transaction commits (i.e., for the
352 static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
354 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
356 atomic_inc(&sctx->refs);
358 * increment scrubs_running to prevent cancel requests from
359 * completing as long as a worker is running. we must also
360 * increment scrubs_paused to prevent deadlocking on pause
361 * requests used for transactions commits (as the worker uses a
362 * transaction context). it is safe to regard the worker
363 * as paused for all matters practical. effectively, we only
364 * avoid cancellation requests from completing.
366 mutex_lock(&fs_info->scrub_lock);
367 atomic_inc(&fs_info->scrubs_running);
368 atomic_inc(&fs_info->scrubs_paused);
369 mutex_unlock(&fs_info->scrub_lock);
372 * check if @scrubs_running=@scrubs_paused condition
373 * inside wait_event() is not an atomic operation.
374 * which means we may inc/dec @scrub_running/paused
375 * at any time. Let's wake up @scrub_pause_wait as
376 * much as we can to let commit transaction blocked less.
378 wake_up(&fs_info->scrub_pause_wait);
380 atomic_inc(&sctx->workers_pending);
383 /* used for workers that require transaction commits */
384 static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
386 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
389 * see scrub_pending_trans_workers_inc() why we're pretending
390 * to be paused in the scrub counters
392 mutex_lock(&fs_info->scrub_lock);
393 atomic_dec(&fs_info->scrubs_running);
394 atomic_dec(&fs_info->scrubs_paused);
395 mutex_unlock(&fs_info->scrub_lock);
396 atomic_dec(&sctx->workers_pending);
397 wake_up(&fs_info->scrub_pause_wait);
398 wake_up(&sctx->list_wait);
402 static void scrub_free_csums(struct scrub_ctx *sctx)
404 while (!list_empty(&sctx->csum_list)) {
405 struct btrfs_ordered_sum *sum;
406 sum = list_first_entry(&sctx->csum_list,
407 struct btrfs_ordered_sum, list);
408 list_del(&sum->list);
413 static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
420 scrub_free_wr_ctx(&sctx->wr_ctx);
422 /* this can happen when scrub is cancelled */
423 if (sctx->curr != -1) {
424 struct scrub_bio *sbio = sctx->bios[sctx->curr];
426 for (i = 0; i < sbio->page_count; i++) {
427 WARN_ON(!sbio->pagev[i]->page);
428 scrub_block_put(sbio->pagev[i]->sblock);
433 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
434 struct scrub_bio *sbio = sctx->bios[i];
441 scrub_free_csums(sctx);
445 static void scrub_put_ctx(struct scrub_ctx *sctx)
447 if (atomic_dec_and_test(&sctx->refs))
448 scrub_free_ctx(sctx);
451 static noinline_for_stack
452 struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
454 struct scrub_ctx *sctx;
456 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
459 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
462 atomic_set(&sctx->refs, 1);
463 sctx->is_dev_replace = is_dev_replace;
464 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
466 sctx->dev_root = dev->dev_root;
467 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
468 struct scrub_bio *sbio;
470 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
473 sctx->bios[i] = sbio;
477 sbio->page_count = 0;
478 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
479 scrub_bio_end_io_worker, NULL, NULL);
481 if (i != SCRUB_BIOS_PER_SCTX - 1)
482 sctx->bios[i]->next_free = i + 1;
484 sctx->bios[i]->next_free = -1;
486 sctx->first_free = 0;
487 sctx->nodesize = dev->dev_root->nodesize;
488 sctx->sectorsize = dev->dev_root->sectorsize;
489 atomic_set(&sctx->bios_in_flight, 0);
490 atomic_set(&sctx->workers_pending, 0);
491 atomic_set(&sctx->cancel_req, 0);
492 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
493 INIT_LIST_HEAD(&sctx->csum_list);
495 spin_lock_init(&sctx->list_lock);
496 spin_lock_init(&sctx->stat_lock);
497 init_waitqueue_head(&sctx->list_wait);
499 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
500 fs_info->dev_replace.tgtdev, is_dev_replace);
502 scrub_free_ctx(sctx);
508 scrub_free_ctx(sctx);
509 return ERR_PTR(-ENOMEM);
512 static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
519 struct extent_buffer *eb;
520 struct btrfs_inode_item *inode_item;
521 struct scrub_warning *swarn = warn_ctx;
522 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
523 struct inode_fs_paths *ipath = NULL;
524 struct btrfs_root *local_root;
525 struct btrfs_key root_key;
526 struct btrfs_key key;
528 root_key.objectid = root;
529 root_key.type = BTRFS_ROOT_ITEM_KEY;
530 root_key.offset = (u64)-1;
531 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
532 if (IS_ERR(local_root)) {
533 ret = PTR_ERR(local_root);
538 * this makes the path point to (inum INODE_ITEM ioff)
541 key.type = BTRFS_INODE_ITEM_KEY;
544 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
546 btrfs_release_path(swarn->path);
550 eb = swarn->path->nodes[0];
551 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
552 struct btrfs_inode_item);
553 isize = btrfs_inode_size(eb, inode_item);
554 nlink = btrfs_inode_nlink(eb, inode_item);
555 btrfs_release_path(swarn->path);
557 ipath = init_ipath(4096, local_root, swarn->path);
559 ret = PTR_ERR(ipath);
563 ret = paths_from_inode(inum, ipath);
569 * we deliberately ignore the bit ipath might have been too small to
570 * hold all of the paths here
572 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
573 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
574 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
575 "length %llu, links %u (path: %s)\n", swarn->errstr,
576 swarn->logical, rcu_str_deref(swarn->dev->name),
577 (unsigned long long)swarn->sector, root, inum, offset,
578 min(isize - offset, (u64)PAGE_SIZE), nlink,
579 (char *)(unsigned long)ipath->fspath->val[i]);
585 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
586 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
587 "resolving failed with ret=%d\n", swarn->errstr,
588 swarn->logical, rcu_str_deref(swarn->dev->name),
589 (unsigned long long)swarn->sector, root, inum, offset, ret);
595 static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
597 struct btrfs_device *dev;
598 struct btrfs_fs_info *fs_info;
599 struct btrfs_path *path;
600 struct btrfs_key found_key;
601 struct extent_buffer *eb;
602 struct btrfs_extent_item *ei;
603 struct scrub_warning swarn;
604 unsigned long ptr = 0;
612 WARN_ON(sblock->page_count < 1);
613 dev = sblock->pagev[0]->dev;
614 fs_info = sblock->sctx->dev_root->fs_info;
616 path = btrfs_alloc_path();
620 swarn.sector = (sblock->pagev[0]->physical) >> 9;
621 swarn.logical = sblock->pagev[0]->logical;
622 swarn.errstr = errstr;
625 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
630 extent_item_pos = swarn.logical - found_key.objectid;
631 swarn.extent_item_size = found_key.offset;
634 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
635 item_size = btrfs_item_size_nr(eb, path->slots[0]);
637 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
639 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
640 item_size, &ref_root,
642 printk_in_rcu(KERN_WARNING
643 "BTRFS: %s at logical %llu on dev %s, "
644 "sector %llu: metadata %s (level %d) in tree "
645 "%llu\n", errstr, swarn.logical,
646 rcu_str_deref(dev->name),
647 (unsigned long long)swarn.sector,
648 ref_level ? "node" : "leaf",
649 ret < 0 ? -1 : ref_level,
650 ret < 0 ? -1 : ref_root);
652 btrfs_release_path(path);
654 btrfs_release_path(path);
657 iterate_extent_inodes(fs_info, found_key.objectid,
659 scrub_print_warning_inode, &swarn);
663 btrfs_free_path(path);
666 static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
668 struct page *page = NULL;
670 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
673 struct btrfs_key key;
674 struct inode *inode = NULL;
675 struct btrfs_fs_info *fs_info;
676 u64 end = offset + PAGE_SIZE - 1;
677 struct btrfs_root *local_root;
681 key.type = BTRFS_ROOT_ITEM_KEY;
682 key.offset = (u64)-1;
684 fs_info = fixup->root->fs_info;
685 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
687 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
688 if (IS_ERR(local_root)) {
689 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
690 return PTR_ERR(local_root);
693 key.type = BTRFS_INODE_ITEM_KEY;
696 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
697 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
699 return PTR_ERR(inode);
701 index = offset >> PAGE_CACHE_SHIFT;
703 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
709 if (PageUptodate(page)) {
710 if (PageDirty(page)) {
712 * we need to write the data to the defect sector. the
713 * data that was in that sector is not in memory,
714 * because the page was modified. we must not write the
715 * modified page to that sector.
717 * TODO: what could be done here: wait for the delalloc
718 * runner to write out that page (might involve
719 * COW) and see whether the sector is still
720 * referenced afterwards.
722 * For the meantime, we'll treat this error
723 * incorrectable, although there is a chance that a
724 * later scrub will find the bad sector again and that
725 * there's no dirty page in memory, then.
730 ret = repair_io_failure(inode, offset, PAGE_SIZE,
731 fixup->logical, page,
732 offset - page_offset(page),
738 * we need to get good data first. the general readpage path
739 * will call repair_io_failure for us, we just have to make
740 * sure we read the bad mirror.
742 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
743 EXTENT_DAMAGED, GFP_NOFS);
745 /* set_extent_bits should give proper error */
752 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
755 wait_on_page_locked(page);
757 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
758 end, EXTENT_DAMAGED, 0, NULL);
760 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
761 EXTENT_DAMAGED, GFP_NOFS);
773 if (ret == 0 && corrected) {
775 * we only need to call readpage for one of the inodes belonging
776 * to this extent. so make iterate_extent_inodes stop
784 static void scrub_fixup_nodatasum(struct btrfs_work *work)
787 struct scrub_fixup_nodatasum *fixup;
788 struct scrub_ctx *sctx;
789 struct btrfs_trans_handle *trans = NULL;
790 struct btrfs_path *path;
791 int uncorrectable = 0;
793 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
796 path = btrfs_alloc_path();
798 spin_lock(&sctx->stat_lock);
799 ++sctx->stat.malloc_errors;
800 spin_unlock(&sctx->stat_lock);
805 trans = btrfs_join_transaction(fixup->root);
812 * the idea is to trigger a regular read through the standard path. we
813 * read a page from the (failed) logical address by specifying the
814 * corresponding copynum of the failed sector. thus, that readpage is
816 * that is the point where on-the-fly error correction will kick in
817 * (once it's finished) and rewrite the failed sector if a good copy
820 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
821 path, scrub_fixup_readpage,
829 spin_lock(&sctx->stat_lock);
830 ++sctx->stat.corrected_errors;
831 spin_unlock(&sctx->stat_lock);
834 if (trans && !IS_ERR(trans))
835 btrfs_end_transaction(trans, fixup->root);
837 spin_lock(&sctx->stat_lock);
838 ++sctx->stat.uncorrectable_errors;
839 spin_unlock(&sctx->stat_lock);
840 btrfs_dev_replace_stats_inc(
841 &sctx->dev_root->fs_info->dev_replace.
842 num_uncorrectable_read_errors);
843 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
844 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
845 fixup->logical, rcu_str_deref(fixup->dev->name));
848 btrfs_free_path(path);
851 scrub_pending_trans_workers_dec(sctx);
854 static inline void scrub_get_recover(struct scrub_recover *recover)
856 atomic_inc(&recover->refs);
859 static inline void scrub_put_recover(struct scrub_recover *recover)
861 if (atomic_dec_and_test(&recover->refs)) {
862 btrfs_put_bbio(recover->bbio);
868 * scrub_handle_errored_block gets called when either verification of the
869 * pages failed or the bio failed to read, e.g. with EIO. In the latter
870 * case, this function handles all pages in the bio, even though only one
872 * The goal of this function is to repair the errored block by using the
873 * contents of one of the mirrors.
875 static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
877 struct scrub_ctx *sctx = sblock_to_check->sctx;
878 struct btrfs_device *dev;
879 struct btrfs_fs_info *fs_info;
883 unsigned int failed_mirror_index;
884 unsigned int is_metadata;
885 unsigned int have_csum;
887 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
888 struct scrub_block *sblock_bad;
893 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
894 DEFAULT_RATELIMIT_BURST);
896 BUG_ON(sblock_to_check->page_count < 1);
897 fs_info = sctx->dev_root->fs_info;
898 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
900 * if we find an error in a super block, we just report it.
901 * They will get written with the next transaction commit
904 spin_lock(&sctx->stat_lock);
905 ++sctx->stat.super_errors;
906 spin_unlock(&sctx->stat_lock);
909 length = sblock_to_check->page_count * PAGE_SIZE;
910 logical = sblock_to_check->pagev[0]->logical;
911 generation = sblock_to_check->pagev[0]->generation;
912 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
913 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
914 is_metadata = !(sblock_to_check->pagev[0]->flags &
915 BTRFS_EXTENT_FLAG_DATA);
916 have_csum = sblock_to_check->pagev[0]->have_csum;
917 csum = sblock_to_check->pagev[0]->csum;
918 dev = sblock_to_check->pagev[0]->dev;
920 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
921 sblocks_for_recheck = NULL;
926 * read all mirrors one after the other. This includes to
927 * re-read the extent or metadata block that failed (that was
928 * the cause that this fixup code is called) another time,
929 * page by page this time in order to know which pages
930 * caused I/O errors and which ones are good (for all mirrors).
931 * It is the goal to handle the situation when more than one
932 * mirror contains I/O errors, but the errors do not
933 * overlap, i.e. the data can be repaired by selecting the
934 * pages from those mirrors without I/O error on the
935 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
936 * would be that mirror #1 has an I/O error on the first page,
937 * the second page is good, and mirror #2 has an I/O error on
938 * the second page, but the first page is good.
939 * Then the first page of the first mirror can be repaired by
940 * taking the first page of the second mirror, and the
941 * second page of the second mirror can be repaired by
942 * copying the contents of the 2nd page of the 1st mirror.
943 * One more note: if the pages of one mirror contain I/O
944 * errors, the checksum cannot be verified. In order to get
945 * the best data for repairing, the first attempt is to find
946 * a mirror without I/O errors and with a validated checksum.
947 * Only if this is not possible, the pages are picked from
948 * mirrors with I/O errors without considering the checksum.
949 * If the latter is the case, at the end, the checksum of the
950 * repaired area is verified in order to correctly maintain
954 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
955 sizeof(*sblocks_for_recheck), GFP_NOFS);
956 if (!sblocks_for_recheck) {
957 spin_lock(&sctx->stat_lock);
958 sctx->stat.malloc_errors++;
959 sctx->stat.read_errors++;
960 sctx->stat.uncorrectable_errors++;
961 spin_unlock(&sctx->stat_lock);
962 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
966 /* setup the context, map the logical blocks and alloc the pages */
967 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
969 spin_lock(&sctx->stat_lock);
970 sctx->stat.read_errors++;
971 sctx->stat.uncorrectable_errors++;
972 spin_unlock(&sctx->stat_lock);
973 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
976 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
977 sblock_bad = sblocks_for_recheck + failed_mirror_index;
979 /* build and submit the bios for the failed mirror, check checksums */
980 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
981 csum, generation, sctx->csum_size, 1);
983 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
984 sblock_bad->no_io_error_seen) {
986 * the error disappeared after reading page by page, or
987 * the area was part of a huge bio and other parts of the
988 * bio caused I/O errors, or the block layer merged several
989 * read requests into one and the error is caused by a
990 * different bio (usually one of the two latter cases is
993 spin_lock(&sctx->stat_lock);
994 sctx->stat.unverified_errors++;
995 sblock_to_check->data_corrected = 1;
996 spin_unlock(&sctx->stat_lock);
998 if (sctx->is_dev_replace)
999 scrub_write_block_to_dev_replace(sblock_bad);
1003 if (!sblock_bad->no_io_error_seen) {
1004 spin_lock(&sctx->stat_lock);
1005 sctx->stat.read_errors++;
1006 spin_unlock(&sctx->stat_lock);
1007 if (__ratelimit(&_rs))
1008 scrub_print_warning("i/o error", sblock_to_check);
1009 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
1010 } else if (sblock_bad->checksum_error) {
1011 spin_lock(&sctx->stat_lock);
1012 sctx->stat.csum_errors++;
1013 spin_unlock(&sctx->stat_lock);
1014 if (__ratelimit(&_rs))
1015 scrub_print_warning("checksum error", sblock_to_check);
1016 btrfs_dev_stat_inc_and_print(dev,
1017 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1018 } else if (sblock_bad->header_error) {
1019 spin_lock(&sctx->stat_lock);
1020 sctx->stat.verify_errors++;
1021 spin_unlock(&sctx->stat_lock);
1022 if (__ratelimit(&_rs))
1023 scrub_print_warning("checksum/header error",
1025 if (sblock_bad->generation_error)
1026 btrfs_dev_stat_inc_and_print(dev,
1027 BTRFS_DEV_STAT_GENERATION_ERRS);
1029 btrfs_dev_stat_inc_and_print(dev,
1030 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1033 if (sctx->readonly) {
1034 ASSERT(!sctx->is_dev_replace);
1038 if (!is_metadata && !have_csum) {
1039 struct scrub_fixup_nodatasum *fixup_nodatasum;
1041 WARN_ON(sctx->is_dev_replace);
1046 * !is_metadata and !have_csum, this means that the data
1047 * might not be COW'ed, that it might be modified
1048 * concurrently. The general strategy to work on the
1049 * commit root does not help in the case when COW is not
1052 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1053 if (!fixup_nodatasum)
1054 goto did_not_correct_error;
1055 fixup_nodatasum->sctx = sctx;
1056 fixup_nodatasum->dev = dev;
1057 fixup_nodatasum->logical = logical;
1058 fixup_nodatasum->root = fs_info->extent_root;
1059 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
1060 scrub_pending_trans_workers_inc(sctx);
1061 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1062 scrub_fixup_nodatasum, NULL, NULL);
1063 btrfs_queue_work(fs_info->scrub_workers,
1064 &fixup_nodatasum->work);
1069 * now build and submit the bios for the other mirrors, check
1071 * First try to pick the mirror which is completely without I/O
1072 * errors and also does not have a checksum error.
1073 * If one is found, and if a checksum is present, the full block
1074 * that is known to contain an error is rewritten. Afterwards
1075 * the block is known to be corrected.
1076 * If a mirror is found which is completely correct, and no
1077 * checksum is present, only those pages are rewritten that had
1078 * an I/O error in the block to be repaired, since it cannot be
1079 * determined, which copy of the other pages is better (and it
1080 * could happen otherwise that a correct page would be
1081 * overwritten by a bad one).
1083 for (mirror_index = 0;
1084 mirror_index < BTRFS_MAX_MIRRORS &&
1085 sblocks_for_recheck[mirror_index].page_count > 0;
1087 struct scrub_block *sblock_other;
1089 if (mirror_index == failed_mirror_index)
1091 sblock_other = sblocks_for_recheck + mirror_index;
1093 /* build and submit the bios, check checksums */
1094 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1095 have_csum, csum, generation,
1096 sctx->csum_size, 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 succeedes. 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,
1209 is_metadata, have_csum, csum,
1210 generation, sctx->csum_size, 1);
1211 if (!sblock_bad->header_error &&
1212 !sblock_bad->checksum_error &&
1213 sblock_bad->no_io_error_seen)
1214 goto corrected_error;
1216 goto did_not_correct_error;
1219 spin_lock(&sctx->stat_lock);
1220 sctx->stat.corrected_errors++;
1221 sblock_to_check->data_corrected = 1;
1222 spin_unlock(&sctx->stat_lock);
1223 printk_ratelimited_in_rcu(KERN_ERR
1224 "BTRFS: fixed up error at logical %llu on dev %s\n",
1225 logical, rcu_str_deref(dev->name));
1228 did_not_correct_error:
1229 spin_lock(&sctx->stat_lock);
1230 sctx->stat.uncorrectable_errors++;
1231 spin_unlock(&sctx->stat_lock);
1232 printk_ratelimited_in_rcu(KERN_ERR
1233 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
1234 logical, rcu_str_deref(dev->name));
1238 if (sblocks_for_recheck) {
1239 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1241 struct scrub_block *sblock = sblocks_for_recheck +
1243 struct scrub_recover *recover;
1246 for (page_index = 0; page_index < sblock->page_count;
1248 sblock->pagev[page_index]->sblock = NULL;
1249 recover = sblock->pagev[page_index]->recover;
1251 scrub_put_recover(recover);
1252 sblock->pagev[page_index]->recover =
1255 scrub_page_put(sblock->pagev[page_index]);
1258 kfree(sblocks_for_recheck);
1264 static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
1266 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1268 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1271 return (int)bbio->num_stripes;
1274 static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1277 int nstripes, int mirror,
1283 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
1285 for (i = 0; i < nstripes; i++) {
1286 if (raid_map[i] == RAID6_Q_STRIPE ||
1287 raid_map[i] == RAID5_P_STRIPE)
1290 if (logical >= raid_map[i] &&
1291 logical < raid_map[i] + mapped_length)
1296 *stripe_offset = logical - raid_map[i];
1298 /* The other RAID type */
1299 *stripe_index = mirror;
1304 static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
1305 struct scrub_block *sblocks_for_recheck)
1307 struct scrub_ctx *sctx = original_sblock->sctx;
1308 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1309 u64 length = original_sblock->page_count * PAGE_SIZE;
1310 u64 logical = original_sblock->pagev[0]->logical;
1311 struct scrub_recover *recover;
1312 struct btrfs_bio *bbio;
1323 * note: the two members refs and outstanding_pages
1324 * are not used (and not set) in the blocks that are used for
1325 * the recheck procedure
1328 while (length > 0) {
1329 sublen = min_t(u64, length, PAGE_SIZE);
1330 mapped_length = sublen;
1334 * with a length of PAGE_SIZE, each returned stripe
1335 * represents one mirror
1337 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
1338 &mapped_length, &bbio, 0, 1);
1339 if (ret || !bbio || mapped_length < sublen) {
1340 btrfs_put_bbio(bbio);
1344 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1346 btrfs_put_bbio(bbio);
1350 atomic_set(&recover->refs, 1);
1351 recover->bbio = bbio;
1352 recover->map_length = mapped_length;
1354 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
1356 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
1358 for (mirror_index = 0; mirror_index < nmirrors;
1360 struct scrub_block *sblock;
1361 struct scrub_page *page;
1363 sblock = sblocks_for_recheck + mirror_index;
1364 sblock->sctx = sctx;
1365 page = kzalloc(sizeof(*page), GFP_NOFS);
1368 spin_lock(&sctx->stat_lock);
1369 sctx->stat.malloc_errors++;
1370 spin_unlock(&sctx->stat_lock);
1371 scrub_put_recover(recover);
1374 scrub_page_get(page);
1375 sblock->pagev[page_index] = page;
1376 page->logical = logical;
1378 scrub_stripe_index_and_offset(logical,
1387 page->physical = bbio->stripes[stripe_index].physical +
1389 page->dev = bbio->stripes[stripe_index].dev;
1391 BUG_ON(page_index >= original_sblock->page_count);
1392 page->physical_for_dev_replace =
1393 original_sblock->pagev[page_index]->
1394 physical_for_dev_replace;
1395 /* for missing devices, dev->bdev is NULL */
1396 page->mirror_num = mirror_index + 1;
1397 sblock->page_count++;
1398 page->page = alloc_page(GFP_NOFS);
1402 scrub_get_recover(recover);
1403 page->recover = recover;
1405 scrub_put_recover(recover);
1414 struct scrub_bio_ret {
1415 struct completion event;
1419 static void scrub_bio_wait_endio(struct bio *bio)
1421 struct scrub_bio_ret *ret = bio->bi_private;
1423 ret->error = bio->bi_error;
1424 complete(&ret->event);
1427 static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1429 return page->recover &&
1430 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
1433 static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1435 struct scrub_page *page)
1437 struct scrub_bio_ret done;
1440 init_completion(&done.event);
1442 bio->bi_iter.bi_sector = page->logical >> 9;
1443 bio->bi_private = &done;
1444 bio->bi_end_io = scrub_bio_wait_endio;
1446 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
1447 page->recover->map_length,
1448 page->mirror_num, 0);
1452 wait_for_completion(&done.event);
1460 * this function will check the on disk data for checksum errors, header
1461 * errors and read I/O errors. If any I/O errors happen, the exact pages
1462 * which are errored are marked as being bad. The goal is to enable scrub
1463 * to take those pages that are not errored from all the mirrors so that
1464 * the pages that are errored in the just handled mirror can be repaired.
1466 static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1467 struct scrub_block *sblock, int is_metadata,
1468 int have_csum, u8 *csum, u64 generation,
1469 u16 csum_size, int retry_failed_mirror)
1473 sblock->no_io_error_seen = 1;
1474 sblock->header_error = 0;
1475 sblock->checksum_error = 0;
1477 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1479 struct scrub_page *page = sblock->pagev[page_num];
1481 if (page->dev->bdev == NULL) {
1483 sblock->no_io_error_seen = 0;
1487 WARN_ON(!page->page);
1488 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
1491 sblock->no_io_error_seen = 0;
1494 bio->bi_bdev = page->dev->bdev;
1496 bio_add_page(bio, page->page, PAGE_SIZE, 0);
1497 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1498 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1499 sblock->no_io_error_seen = 0;
1501 bio->bi_iter.bi_sector = page->physical >> 9;
1503 if (btrfsic_submit_bio_wait(READ, bio))
1504 sblock->no_io_error_seen = 0;
1510 if (sblock->no_io_error_seen)
1511 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1512 have_csum, csum, generation,
1518 static inline int scrub_check_fsid(u8 fsid[],
1519 struct scrub_page *spage)
1521 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1524 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1528 static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1529 struct scrub_block *sblock,
1530 int is_metadata, int have_csum,
1531 const u8 *csum, u64 generation,
1535 u8 calculated_csum[BTRFS_CSUM_SIZE];
1537 void *mapped_buffer;
1539 WARN_ON(!sblock->pagev[0]->page);
1541 struct btrfs_header *h;
1543 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
1544 h = (struct btrfs_header *)mapped_buffer;
1546 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
1547 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
1548 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1550 sblock->header_error = 1;
1551 } else if (generation != btrfs_stack_header_generation(h)) {
1552 sblock->header_error = 1;
1553 sblock->generation_error = 1;
1560 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
1563 for (page_num = 0;;) {
1564 if (page_num == 0 && is_metadata)
1565 crc = btrfs_csum_data(
1566 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1567 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1569 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
1571 kunmap_atomic(mapped_buffer);
1573 if (page_num >= sblock->page_count)
1575 WARN_ON(!sblock->pagev[page_num]->page);
1577 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
1580 btrfs_csum_final(crc, calculated_csum);
1581 if (memcmp(calculated_csum, csum, csum_size))
1582 sblock->checksum_error = 1;
1585 static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1586 struct scrub_block *sblock_good)
1591 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1594 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1604 static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1605 struct scrub_block *sblock_good,
1606 int page_num, int force_write)
1608 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1609 struct scrub_page *page_good = sblock_good->pagev[page_num];
1611 BUG_ON(page_bad->page == NULL);
1612 BUG_ON(page_good->page == NULL);
1613 if (force_write || sblock_bad->header_error ||
1614 sblock_bad->checksum_error || page_bad->io_error) {
1618 if (!page_bad->dev->bdev) {
1619 printk_ratelimited(KERN_WARNING "BTRFS: "
1620 "scrub_repair_page_from_good_copy(bdev == NULL) "
1621 "is unexpected!\n");
1625 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
1628 bio->bi_bdev = page_bad->dev->bdev;
1629 bio->bi_iter.bi_sector = page_bad->physical >> 9;
1631 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1632 if (PAGE_SIZE != ret) {
1637 if (btrfsic_submit_bio_wait(WRITE, bio)) {
1638 btrfs_dev_stat_inc_and_print(page_bad->dev,
1639 BTRFS_DEV_STAT_WRITE_ERRS);
1640 btrfs_dev_replace_stats_inc(
1641 &sblock_bad->sctx->dev_root->fs_info->
1642 dev_replace.num_write_errors);
1652 static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1657 * This block is used for the check of the parity on the source device,
1658 * so the data needn't be written into the destination device.
1660 if (sblock->sparity)
1663 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1666 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1668 btrfs_dev_replace_stats_inc(
1669 &sblock->sctx->dev_root->fs_info->dev_replace.
1674 static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1677 struct scrub_page *spage = sblock->pagev[page_num];
1679 BUG_ON(spage->page == NULL);
1680 if (spage->io_error) {
1681 void *mapped_buffer = kmap_atomic(spage->page);
1683 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1684 flush_dcache_page(spage->page);
1685 kunmap_atomic(mapped_buffer);
1687 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1690 static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1691 struct scrub_page *spage)
1693 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1694 struct scrub_bio *sbio;
1697 mutex_lock(&wr_ctx->wr_lock);
1699 if (!wr_ctx->wr_curr_bio) {
1700 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1702 if (!wr_ctx->wr_curr_bio) {
1703 mutex_unlock(&wr_ctx->wr_lock);
1706 wr_ctx->wr_curr_bio->sctx = sctx;
1707 wr_ctx->wr_curr_bio->page_count = 0;
1709 sbio = wr_ctx->wr_curr_bio;
1710 if (sbio->page_count == 0) {
1713 sbio->physical = spage->physical_for_dev_replace;
1714 sbio->logical = spage->logical;
1715 sbio->dev = wr_ctx->tgtdev;
1718 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
1720 mutex_unlock(&wr_ctx->wr_lock);
1726 bio->bi_private = sbio;
1727 bio->bi_end_io = scrub_wr_bio_end_io;
1728 bio->bi_bdev = sbio->dev->bdev;
1729 bio->bi_iter.bi_sector = sbio->physical >> 9;
1731 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1732 spage->physical_for_dev_replace ||
1733 sbio->logical + sbio->page_count * PAGE_SIZE !=
1735 scrub_wr_submit(sctx);
1739 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1740 if (ret != PAGE_SIZE) {
1741 if (sbio->page_count < 1) {
1744 mutex_unlock(&wr_ctx->wr_lock);
1747 scrub_wr_submit(sctx);
1751 sbio->pagev[sbio->page_count] = spage;
1752 scrub_page_get(spage);
1754 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1755 scrub_wr_submit(sctx);
1756 mutex_unlock(&wr_ctx->wr_lock);
1761 static void scrub_wr_submit(struct scrub_ctx *sctx)
1763 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1764 struct scrub_bio *sbio;
1766 if (!wr_ctx->wr_curr_bio)
1769 sbio = wr_ctx->wr_curr_bio;
1770 wr_ctx->wr_curr_bio = NULL;
1771 WARN_ON(!sbio->bio->bi_bdev);
1772 scrub_pending_bio_inc(sctx);
1773 /* process all writes in a single worker thread. Then the block layer
1774 * orders the requests before sending them to the driver which
1775 * doubled the write performance on spinning disks when measured
1777 btrfsic_submit_bio(WRITE, sbio->bio);
1780 static void scrub_wr_bio_end_io(struct bio *bio)
1782 struct scrub_bio *sbio = bio->bi_private;
1783 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1785 sbio->err = bio->bi_error;
1788 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1789 scrub_wr_bio_end_io_worker, NULL, NULL);
1790 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
1793 static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1795 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1796 struct scrub_ctx *sctx = sbio->sctx;
1799 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1801 struct btrfs_dev_replace *dev_replace =
1802 &sbio->sctx->dev_root->fs_info->dev_replace;
1804 for (i = 0; i < sbio->page_count; i++) {
1805 struct scrub_page *spage = sbio->pagev[i];
1807 spage->io_error = 1;
1808 btrfs_dev_replace_stats_inc(&dev_replace->
1813 for (i = 0; i < sbio->page_count; i++)
1814 scrub_page_put(sbio->pagev[i]);
1818 scrub_pending_bio_dec(sctx);
1821 static int scrub_checksum(struct scrub_block *sblock)
1826 WARN_ON(sblock->page_count < 1);
1827 flags = sblock->pagev[0]->flags;
1829 if (flags & BTRFS_EXTENT_FLAG_DATA)
1830 ret = scrub_checksum_data(sblock);
1831 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1832 ret = scrub_checksum_tree_block(sblock);
1833 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1834 (void)scrub_checksum_super(sblock);
1838 scrub_handle_errored_block(sblock);
1843 static int scrub_checksum_data(struct scrub_block *sblock)
1845 struct scrub_ctx *sctx = sblock->sctx;
1846 u8 csum[BTRFS_CSUM_SIZE];
1855 BUG_ON(sblock->page_count < 1);
1856 if (!sblock->pagev[0]->have_csum)
1859 on_disk_csum = sblock->pagev[0]->csum;
1860 page = sblock->pagev[0]->page;
1861 buffer = kmap_atomic(page);
1863 len = sctx->sectorsize;
1866 u64 l = min_t(u64, len, PAGE_SIZE);
1868 crc = btrfs_csum_data(buffer, crc, l);
1869 kunmap_atomic(buffer);
1874 BUG_ON(index >= sblock->page_count);
1875 BUG_ON(!sblock->pagev[index]->page);
1876 page = sblock->pagev[index]->page;
1877 buffer = kmap_atomic(page);
1880 btrfs_csum_final(crc, csum);
1881 if (memcmp(csum, on_disk_csum, sctx->csum_size))
1887 static int scrub_checksum_tree_block(struct scrub_block *sblock)
1889 struct scrub_ctx *sctx = sblock->sctx;
1890 struct btrfs_header *h;
1891 struct btrfs_root *root = sctx->dev_root;
1892 struct btrfs_fs_info *fs_info = root->fs_info;
1893 u8 calculated_csum[BTRFS_CSUM_SIZE];
1894 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1896 void *mapped_buffer;
1905 BUG_ON(sblock->page_count < 1);
1906 page = sblock->pagev[0]->page;
1907 mapped_buffer = kmap_atomic(page);
1908 h = (struct btrfs_header *)mapped_buffer;
1909 memcpy(on_disk_csum, h->csum, sctx->csum_size);
1912 * we don't use the getter functions here, as we
1913 * a) don't have an extent buffer and
1914 * b) the page is already kmapped
1917 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
1920 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
1923 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
1926 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1930 len = sctx->nodesize - BTRFS_CSUM_SIZE;
1931 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1932 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1935 u64 l = min_t(u64, len, mapped_size);
1937 crc = btrfs_csum_data(p, crc, l);
1938 kunmap_atomic(mapped_buffer);
1943 BUG_ON(index >= sblock->page_count);
1944 BUG_ON(!sblock->pagev[index]->page);
1945 page = sblock->pagev[index]->page;
1946 mapped_buffer = kmap_atomic(page);
1947 mapped_size = PAGE_SIZE;
1951 btrfs_csum_final(crc, calculated_csum);
1952 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
1955 return fail || crc_fail;
1958 static int scrub_checksum_super(struct scrub_block *sblock)
1960 struct btrfs_super_block *s;
1961 struct scrub_ctx *sctx = sblock->sctx;
1962 u8 calculated_csum[BTRFS_CSUM_SIZE];
1963 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1965 void *mapped_buffer;
1974 BUG_ON(sblock->page_count < 1);
1975 page = sblock->pagev[0]->page;
1976 mapped_buffer = kmap_atomic(page);
1977 s = (struct btrfs_super_block *)mapped_buffer;
1978 memcpy(on_disk_csum, s->csum, sctx->csum_size);
1980 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
1983 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
1986 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
1989 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1990 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1991 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1994 u64 l = min_t(u64, len, mapped_size);
1996 crc = btrfs_csum_data(p, crc, l);
1997 kunmap_atomic(mapped_buffer);
2002 BUG_ON(index >= sblock->page_count);
2003 BUG_ON(!sblock->pagev[index]->page);
2004 page = sblock->pagev[index]->page;
2005 mapped_buffer = kmap_atomic(page);
2006 mapped_size = PAGE_SIZE;
2010 btrfs_csum_final(crc, calculated_csum);
2011 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
2014 if (fail_cor + fail_gen) {
2016 * if we find an error in a super block, we just report it.
2017 * They will get written with the next transaction commit
2020 spin_lock(&sctx->stat_lock);
2021 ++sctx->stat.super_errors;
2022 spin_unlock(&sctx->stat_lock);
2024 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
2025 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2027 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
2028 BTRFS_DEV_STAT_GENERATION_ERRS);
2031 return fail_cor + fail_gen;
2034 static void scrub_block_get(struct scrub_block *sblock)
2036 atomic_inc(&sblock->refs);
2039 static void scrub_block_put(struct scrub_block *sblock)
2041 if (atomic_dec_and_test(&sblock->refs)) {
2044 if (sblock->sparity)
2045 scrub_parity_put(sblock->sparity);
2047 for (i = 0; i < sblock->page_count; i++)
2048 scrub_page_put(sblock->pagev[i]);
2053 static void scrub_page_get(struct scrub_page *spage)
2055 atomic_inc(&spage->refs);
2058 static void scrub_page_put(struct scrub_page *spage)
2060 if (atomic_dec_and_test(&spage->refs)) {
2062 __free_page(spage->page);
2067 static void scrub_submit(struct scrub_ctx *sctx)
2069 struct scrub_bio *sbio;
2071 if (sctx->curr == -1)
2074 sbio = sctx->bios[sctx->curr];
2076 scrub_pending_bio_inc(sctx);
2078 if (!sbio->bio->bi_bdev) {
2080 * this case should not happen. If btrfs_map_block() is
2081 * wrong, it could happen for dev-replace operations on
2082 * missing devices when no mirrors are available, but in
2083 * this case it should already fail the mount.
2084 * This case is handled correctly (but _very_ slowly).
2086 printk_ratelimited(KERN_WARNING
2087 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
2088 bio_io_error(sbio->bio);
2090 btrfsic_submit_bio(READ, sbio->bio);
2094 static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2095 struct scrub_page *spage)
2097 struct scrub_block *sblock = spage->sblock;
2098 struct scrub_bio *sbio;
2103 * grab a fresh bio or wait for one to become available
2105 while (sctx->curr == -1) {
2106 spin_lock(&sctx->list_lock);
2107 sctx->curr = sctx->first_free;
2108 if (sctx->curr != -1) {
2109 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2110 sctx->bios[sctx->curr]->next_free = -1;
2111 sctx->bios[sctx->curr]->page_count = 0;
2112 spin_unlock(&sctx->list_lock);
2114 spin_unlock(&sctx->list_lock);
2115 wait_event(sctx->list_wait, sctx->first_free != -1);
2118 sbio = sctx->bios[sctx->curr];
2119 if (sbio->page_count == 0) {
2122 sbio->physical = spage->physical;
2123 sbio->logical = spage->logical;
2124 sbio->dev = spage->dev;
2127 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
2133 bio->bi_private = sbio;
2134 bio->bi_end_io = scrub_bio_end_io;
2135 bio->bi_bdev = sbio->dev->bdev;
2136 bio->bi_iter.bi_sector = sbio->physical >> 9;
2138 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2140 sbio->logical + sbio->page_count * PAGE_SIZE !=
2142 sbio->dev != spage->dev) {
2147 sbio->pagev[sbio->page_count] = spage;
2148 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2149 if (ret != PAGE_SIZE) {
2150 if (sbio->page_count < 1) {
2159 scrub_block_get(sblock); /* one for the page added to the bio */
2160 atomic_inc(&sblock->outstanding_pages);
2162 if (sbio->page_count == sctx->pages_per_rd_bio)
2168 static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
2169 u64 physical, struct btrfs_device *dev, u64 flags,
2170 u64 gen, int mirror_num, u8 *csum, int force,
2171 u64 physical_for_dev_replace)
2173 struct scrub_block *sblock;
2176 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2178 spin_lock(&sctx->stat_lock);
2179 sctx->stat.malloc_errors++;
2180 spin_unlock(&sctx->stat_lock);
2184 /* one ref inside this function, plus one for each page added to
2186 atomic_set(&sblock->refs, 1);
2187 sblock->sctx = sctx;
2188 sblock->no_io_error_seen = 1;
2190 for (index = 0; len > 0; index++) {
2191 struct scrub_page *spage;
2192 u64 l = min_t(u64, len, PAGE_SIZE);
2194 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2197 spin_lock(&sctx->stat_lock);
2198 sctx->stat.malloc_errors++;
2199 spin_unlock(&sctx->stat_lock);
2200 scrub_block_put(sblock);
2203 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2204 scrub_page_get(spage);
2205 sblock->pagev[index] = spage;
2206 spage->sblock = sblock;
2208 spage->flags = flags;
2209 spage->generation = gen;
2210 spage->logical = logical;
2211 spage->physical = physical;
2212 spage->physical_for_dev_replace = physical_for_dev_replace;
2213 spage->mirror_num = mirror_num;
2215 spage->have_csum = 1;
2216 memcpy(spage->csum, csum, sctx->csum_size);
2218 spage->have_csum = 0;
2220 sblock->page_count++;
2221 spage->page = alloc_page(GFP_NOFS);
2227 physical_for_dev_replace += l;
2230 WARN_ON(sblock->page_count == 0);
2231 for (index = 0; index < sblock->page_count; index++) {
2232 struct scrub_page *spage = sblock->pagev[index];
2235 ret = scrub_add_page_to_rd_bio(sctx, spage);
2237 scrub_block_put(sblock);
2245 /* last one frees, either here or in bio completion for last page */
2246 scrub_block_put(sblock);
2250 static void scrub_bio_end_io(struct bio *bio)
2252 struct scrub_bio *sbio = bio->bi_private;
2253 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
2255 sbio->err = bio->bi_error;
2258 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
2261 static void scrub_bio_end_io_worker(struct btrfs_work *work)
2263 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
2264 struct scrub_ctx *sctx = sbio->sctx;
2267 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
2269 for (i = 0; i < sbio->page_count; i++) {
2270 struct scrub_page *spage = sbio->pagev[i];
2272 spage->io_error = 1;
2273 spage->sblock->no_io_error_seen = 0;
2277 /* now complete the scrub_block items that have all pages completed */
2278 for (i = 0; i < sbio->page_count; i++) {
2279 struct scrub_page *spage = sbio->pagev[i];
2280 struct scrub_block *sblock = spage->sblock;
2282 if (atomic_dec_and_test(&sblock->outstanding_pages))
2283 scrub_block_complete(sblock);
2284 scrub_block_put(sblock);
2289 spin_lock(&sctx->list_lock);
2290 sbio->next_free = sctx->first_free;
2291 sctx->first_free = sbio->index;
2292 spin_unlock(&sctx->list_lock);
2294 if (sctx->is_dev_replace &&
2295 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2296 mutex_lock(&sctx->wr_ctx.wr_lock);
2297 scrub_wr_submit(sctx);
2298 mutex_unlock(&sctx->wr_ctx.wr_lock);
2301 scrub_pending_bio_dec(sctx);
2304 static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2305 unsigned long *bitmap,
2310 int sectorsize = sparity->sctx->dev_root->sectorsize;
2312 if (len >= sparity->stripe_len) {
2313 bitmap_set(bitmap, 0, sparity->nsectors);
2317 start -= sparity->logic_start;
2318 start = div_u64_rem(start, sparity->stripe_len, &offset);
2319 offset /= sectorsize;
2320 nsectors = (int)len / sectorsize;
2322 if (offset + nsectors <= sparity->nsectors) {
2323 bitmap_set(bitmap, offset, nsectors);
2327 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2328 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2331 static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2334 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2337 static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2340 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2343 static void scrub_block_complete(struct scrub_block *sblock)
2347 if (!sblock->no_io_error_seen) {
2349 scrub_handle_errored_block(sblock);
2352 * if has checksum error, write via repair mechanism in
2353 * dev replace case, otherwise write here in dev replace
2356 corrupted = scrub_checksum(sblock);
2357 if (!corrupted && sblock->sctx->is_dev_replace)
2358 scrub_write_block_to_dev_replace(sblock);
2361 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2362 u64 start = sblock->pagev[0]->logical;
2363 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2366 scrub_parity_mark_sectors_error(sblock->sparity,
2367 start, end - start);
2371 static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
2374 struct btrfs_ordered_sum *sum = NULL;
2375 unsigned long index;
2376 unsigned long num_sectors;
2378 while (!list_empty(&sctx->csum_list)) {
2379 sum = list_first_entry(&sctx->csum_list,
2380 struct btrfs_ordered_sum, list);
2381 if (sum->bytenr > logical)
2383 if (sum->bytenr + sum->len > logical)
2386 ++sctx->stat.csum_discards;
2387 list_del(&sum->list);
2394 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
2395 num_sectors = sum->len / sctx->sectorsize;
2396 memcpy(csum, sum->sums + index, sctx->csum_size);
2397 if (index == num_sectors - 1) {
2398 list_del(&sum->list);
2404 /* scrub extent tries to collect up to 64 kB for each bio */
2405 static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
2406 u64 physical, struct btrfs_device *dev, u64 flags,
2407 u64 gen, int mirror_num, u64 physical_for_dev_replace)
2410 u8 csum[BTRFS_CSUM_SIZE];
2413 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2414 blocksize = sctx->sectorsize;
2415 spin_lock(&sctx->stat_lock);
2416 sctx->stat.data_extents_scrubbed++;
2417 sctx->stat.data_bytes_scrubbed += len;
2418 spin_unlock(&sctx->stat_lock);
2419 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2420 blocksize = sctx->nodesize;
2421 spin_lock(&sctx->stat_lock);
2422 sctx->stat.tree_extents_scrubbed++;
2423 sctx->stat.tree_bytes_scrubbed += len;
2424 spin_unlock(&sctx->stat_lock);
2426 blocksize = sctx->sectorsize;
2431 u64 l = min_t(u64, len, blocksize);
2434 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2435 /* push csums to sbio */
2436 have_csum = scrub_find_csum(sctx, logical, l, csum);
2438 ++sctx->stat.no_csum;
2439 if (sctx->is_dev_replace && !have_csum) {
2440 ret = copy_nocow_pages(sctx, logical, l,
2442 physical_for_dev_replace);
2443 goto behind_scrub_pages;
2446 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
2447 mirror_num, have_csum ? csum : NULL, 0,
2448 physical_for_dev_replace);
2455 physical_for_dev_replace += l;
2460 static int scrub_pages_for_parity(struct scrub_parity *sparity,
2461 u64 logical, u64 len,
2462 u64 physical, struct btrfs_device *dev,
2463 u64 flags, u64 gen, int mirror_num, u8 *csum)
2465 struct scrub_ctx *sctx = sparity->sctx;
2466 struct scrub_block *sblock;
2469 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2471 spin_lock(&sctx->stat_lock);
2472 sctx->stat.malloc_errors++;
2473 spin_unlock(&sctx->stat_lock);
2477 /* one ref inside this function, plus one for each page added to
2479 atomic_set(&sblock->refs, 1);
2480 sblock->sctx = sctx;
2481 sblock->no_io_error_seen = 1;
2482 sblock->sparity = sparity;
2483 scrub_parity_get(sparity);
2485 for (index = 0; len > 0; index++) {
2486 struct scrub_page *spage;
2487 u64 l = min_t(u64, len, PAGE_SIZE);
2489 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2492 spin_lock(&sctx->stat_lock);
2493 sctx->stat.malloc_errors++;
2494 spin_unlock(&sctx->stat_lock);
2495 scrub_block_put(sblock);
2498 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2499 /* For scrub block */
2500 scrub_page_get(spage);
2501 sblock->pagev[index] = spage;
2502 /* For scrub parity */
2503 scrub_page_get(spage);
2504 list_add_tail(&spage->list, &sparity->spages);
2505 spage->sblock = sblock;
2507 spage->flags = flags;
2508 spage->generation = gen;
2509 spage->logical = logical;
2510 spage->physical = physical;
2511 spage->mirror_num = mirror_num;
2513 spage->have_csum = 1;
2514 memcpy(spage->csum, csum, sctx->csum_size);
2516 spage->have_csum = 0;
2518 sblock->page_count++;
2519 spage->page = alloc_page(GFP_NOFS);
2527 WARN_ON(sblock->page_count == 0);
2528 for (index = 0; index < sblock->page_count; index++) {
2529 struct scrub_page *spage = sblock->pagev[index];
2532 ret = scrub_add_page_to_rd_bio(sctx, spage);
2534 scrub_block_put(sblock);
2539 /* last one frees, either here or in bio completion for last page */
2540 scrub_block_put(sblock);
2544 static int scrub_extent_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)
2549 struct scrub_ctx *sctx = sparity->sctx;
2551 u8 csum[BTRFS_CSUM_SIZE];
2554 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2555 blocksize = sctx->sectorsize;
2556 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2557 blocksize = sctx->nodesize;
2559 blocksize = sctx->sectorsize;
2564 u64 l = min_t(u64, len, blocksize);
2567 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2568 /* push csums to sbio */
2569 have_csum = scrub_find_csum(sctx, logical, l, csum);
2573 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2574 flags, gen, mirror_num,
2575 have_csum ? csum : NULL);
2587 * Given a physical address, this will calculate it's
2588 * logical offset. if this is a parity stripe, it will return
2589 * the most left data stripe's logical offset.
2591 * return 0 if it is a data stripe, 1 means parity stripe.
2593 static int get_raid56_logic_offset(u64 physical, int num,
2594 struct map_lookup *map, u64 *offset,
2604 last_offset = (physical - map->stripes[num].physical) *
2605 nr_data_stripes(map);
2607 *stripe_start = last_offset;
2609 *offset = last_offset;
2610 for (i = 0; i < nr_data_stripes(map); i++) {
2611 *offset = last_offset + i * map->stripe_len;
2613 stripe_nr = div_u64(*offset, map->stripe_len);
2614 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
2616 /* Work out the disk rotation on this stripe-set */
2617 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
2618 /* calculate which stripe this data locates */
2620 stripe_index = rot % map->num_stripes;
2621 if (stripe_index == num)
2623 if (stripe_index < num)
2626 *offset = last_offset + j * map->stripe_len;
2630 static void scrub_free_parity(struct scrub_parity *sparity)
2632 struct scrub_ctx *sctx = sparity->sctx;
2633 struct scrub_page *curr, *next;
2636 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2638 spin_lock(&sctx->stat_lock);
2639 sctx->stat.read_errors += nbits;
2640 sctx->stat.uncorrectable_errors += nbits;
2641 spin_unlock(&sctx->stat_lock);
2644 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2645 list_del_init(&curr->list);
2646 scrub_page_put(curr);
2652 static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2654 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2656 struct scrub_ctx *sctx = sparity->sctx;
2658 scrub_free_parity(sparity);
2659 scrub_pending_bio_dec(sctx);
2662 static void scrub_parity_bio_endio(struct bio *bio)
2664 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2667 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2672 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2673 scrub_parity_bio_endio_worker, NULL, NULL);
2674 btrfs_queue_work(sparity->sctx->dev_root->fs_info->scrub_parity_workers,
2678 static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2680 struct scrub_ctx *sctx = sparity->sctx;
2682 struct btrfs_raid_bio *rbio;
2683 struct scrub_page *spage;
2684 struct btrfs_bio *bbio = NULL;
2688 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2692 length = sparity->logic_end - sparity->logic_start + 1;
2693 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
2694 sparity->logic_start,
2695 &length, &bbio, 0, 1);
2696 if (ret || !bbio || !bbio->raid_map)
2699 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2703 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2704 bio->bi_private = sparity;
2705 bio->bi_end_io = scrub_parity_bio_endio;
2707 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
2708 length, sparity->scrub_dev,
2714 list_for_each_entry(spage, &sparity->spages, list)
2715 raid56_parity_add_scrub_pages(rbio, spage->page,
2718 scrub_pending_bio_inc(sctx);
2719 raid56_parity_submit_scrub_rbio(rbio);
2725 btrfs_put_bbio(bbio);
2726 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2728 spin_lock(&sctx->stat_lock);
2729 sctx->stat.malloc_errors++;
2730 spin_unlock(&sctx->stat_lock);
2732 scrub_free_parity(sparity);
2735 static inline int scrub_calc_parity_bitmap_len(int nsectors)
2737 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2740 static void scrub_parity_get(struct scrub_parity *sparity)
2742 atomic_inc(&sparity->refs);
2745 static void scrub_parity_put(struct scrub_parity *sparity)
2747 if (!atomic_dec_and_test(&sparity->refs))
2750 scrub_parity_check_and_repair(sparity);
2753 static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2754 struct map_lookup *map,
2755 struct btrfs_device *sdev,
2756 struct btrfs_path *path,
2760 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2761 struct btrfs_root *root = fs_info->extent_root;
2762 struct btrfs_root *csum_root = fs_info->csum_root;
2763 struct btrfs_extent_item *extent;
2767 struct extent_buffer *l;
2768 struct btrfs_key key;
2771 u64 extent_physical;
2773 struct btrfs_device *extent_dev;
2774 struct scrub_parity *sparity;
2777 int extent_mirror_num;
2780 nsectors = map->stripe_len / root->sectorsize;
2781 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2782 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2785 spin_lock(&sctx->stat_lock);
2786 sctx->stat.malloc_errors++;
2787 spin_unlock(&sctx->stat_lock);
2791 sparity->stripe_len = map->stripe_len;
2792 sparity->nsectors = nsectors;
2793 sparity->sctx = sctx;
2794 sparity->scrub_dev = sdev;
2795 sparity->logic_start = logic_start;
2796 sparity->logic_end = logic_end;
2797 atomic_set(&sparity->refs, 1);
2798 INIT_LIST_HEAD(&sparity->spages);
2799 sparity->dbitmap = sparity->bitmap;
2800 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2803 while (logic_start < logic_end) {
2804 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2805 key.type = BTRFS_METADATA_ITEM_KEY;
2807 key.type = BTRFS_EXTENT_ITEM_KEY;
2808 key.objectid = logic_start;
2809 key.offset = (u64)-1;
2811 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2816 ret = btrfs_previous_extent_item(root, path, 0);
2820 btrfs_release_path(path);
2821 ret = btrfs_search_slot(NULL, root, &key,
2833 slot = path->slots[0];
2834 if (slot >= btrfs_header_nritems(l)) {
2835 ret = btrfs_next_leaf(root, path);
2844 btrfs_item_key_to_cpu(l, &key, slot);
2846 if (key.type == BTRFS_METADATA_ITEM_KEY)
2847 bytes = root->nodesize;
2851 if (key.objectid + bytes <= logic_start)
2854 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2855 key.type != BTRFS_METADATA_ITEM_KEY)
2858 if (key.objectid > logic_end) {
2863 while (key.objectid >= logic_start + map->stripe_len)
2864 logic_start += map->stripe_len;
2866 extent = btrfs_item_ptr(l, slot,
2867 struct btrfs_extent_item);
2868 flags = btrfs_extent_flags(l, extent);
2869 generation = btrfs_extent_generation(l, extent);
2871 if (key.objectid < logic_start &&
2872 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2874 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2875 key.objectid, logic_start);
2879 extent_logical = key.objectid;
2882 if (extent_logical < logic_start) {
2883 extent_len -= logic_start - extent_logical;
2884 extent_logical = logic_start;
2887 if (extent_logical + extent_len >
2888 logic_start + map->stripe_len)
2889 extent_len = logic_start + map->stripe_len -
2892 scrub_parity_mark_sectors_data(sparity, extent_logical,
2895 scrub_remap_extent(fs_info, extent_logical,
2896 extent_len, &extent_physical,
2898 &extent_mirror_num);
2900 ret = btrfs_lookup_csums_range(csum_root,
2902 extent_logical + extent_len - 1,
2903 &sctx->csum_list, 1);
2907 ret = scrub_extent_for_parity(sparity, extent_logical,
2916 scrub_free_csums(sctx);
2917 if (extent_logical + extent_len <
2918 key.objectid + bytes) {
2919 logic_start += map->stripe_len;
2921 if (logic_start >= logic_end) {
2926 if (logic_start < key.objectid + bytes) {
2935 btrfs_release_path(path);
2940 logic_start += map->stripe_len;
2944 scrub_parity_mark_sectors_error(sparity, logic_start,
2945 logic_end - logic_start + 1);
2946 scrub_parity_put(sparity);
2948 mutex_lock(&sctx->wr_ctx.wr_lock);
2949 scrub_wr_submit(sctx);
2950 mutex_unlock(&sctx->wr_ctx.wr_lock);
2952 btrfs_release_path(path);
2953 return ret < 0 ? ret : 0;
2956 static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
2957 struct map_lookup *map,
2958 struct btrfs_device *scrub_dev,
2959 int num, u64 base, u64 length,
2962 struct btrfs_path *path, *ppath;
2963 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2964 struct btrfs_root *root = fs_info->extent_root;
2965 struct btrfs_root *csum_root = fs_info->csum_root;
2966 struct btrfs_extent_item *extent;
2967 struct blk_plug plug;
2972 struct extent_buffer *l;
2973 struct btrfs_key key;
2980 struct reada_control *reada1;
2981 struct reada_control *reada2;
2982 struct btrfs_key key_start;
2983 struct btrfs_key key_end;
2984 u64 increment = map->stripe_len;
2987 u64 extent_physical;
2991 struct btrfs_device *extent_dev;
2992 int extent_mirror_num;
2995 physical = map->stripes[num].physical;
2997 nstripes = div_u64(length, map->stripe_len);
2998 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2999 offset = map->stripe_len * num;
3000 increment = map->stripe_len * map->num_stripes;
3002 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3003 int factor = map->num_stripes / map->sub_stripes;
3004 offset = map->stripe_len * (num / map->sub_stripes);
3005 increment = map->stripe_len * factor;
3006 mirror_num = num % map->sub_stripes + 1;
3007 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3008 increment = map->stripe_len;
3009 mirror_num = num % map->num_stripes + 1;
3010 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3011 increment = map->stripe_len;
3012 mirror_num = num % map->num_stripes + 1;
3013 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3014 get_raid56_logic_offset(physical, num, map, &offset, NULL);
3015 increment = map->stripe_len * nr_data_stripes(map);
3018 increment = map->stripe_len;
3022 path = btrfs_alloc_path();
3026 ppath = btrfs_alloc_path();
3028 btrfs_free_path(path);
3033 * work on commit root. The related disk blocks are static as
3034 * long as COW is applied. This means, it is save to rewrite
3035 * them to repair disk errors without any race conditions
3037 path->search_commit_root = 1;
3038 path->skip_locking = 1;
3040 ppath->search_commit_root = 1;
3041 ppath->skip_locking = 1;
3043 * trigger the readahead for extent tree csum tree and wait for
3044 * completion. During readahead, the scrub is officially paused
3045 * to not hold off transaction commits
3047 logical = base + offset;
3048 physical_end = physical + nstripes * map->stripe_len;
3049 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3050 get_raid56_logic_offset(physical_end, num,
3051 map, &logic_end, NULL);
3054 logic_end = logical + increment * nstripes;
3056 wait_event(sctx->list_wait,
3057 atomic_read(&sctx->bios_in_flight) == 0);
3058 scrub_blocked_if_needed(fs_info);
3060 /* FIXME it might be better to start readahead at commit root */
3061 key_start.objectid = logical;
3062 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3063 key_start.offset = (u64)0;
3064 key_end.objectid = logic_end;
3065 key_end.type = BTRFS_METADATA_ITEM_KEY;
3066 key_end.offset = (u64)-1;
3067 reada1 = btrfs_reada_add(root, &key_start, &key_end);
3069 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3070 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3071 key_start.offset = logical;
3072 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3073 key_end.type = BTRFS_EXTENT_CSUM_KEY;
3074 key_end.offset = logic_end;
3075 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
3077 if (!IS_ERR(reada1))
3078 btrfs_reada_wait(reada1);
3079 if (!IS_ERR(reada2))
3080 btrfs_reada_wait(reada2);
3084 * collect all data csums for the stripe to avoid seeking during
3085 * the scrub. This might currently (crc32) end up to be about 1MB
3087 blk_start_plug(&plug);
3090 * now find all extents for each stripe and scrub them
3093 while (physical < physical_end) {
3094 /* for raid56, we skip parity stripe */
3095 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3096 ret = get_raid56_logic_offset(physical, num,
3097 map, &logical, &stripe_logical);
3100 stripe_logical += base;
3101 stripe_end = stripe_logical + increment - 1;
3102 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3103 ppath, stripe_logical,
3113 if (atomic_read(&fs_info->scrub_cancel_req) ||
3114 atomic_read(&sctx->cancel_req)) {
3119 * check to see if we have to pause
3121 if (atomic_read(&fs_info->scrub_pause_req)) {
3122 /* push queued extents */
3123 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3125 mutex_lock(&sctx->wr_ctx.wr_lock);
3126 scrub_wr_submit(sctx);
3127 mutex_unlock(&sctx->wr_ctx.wr_lock);
3128 wait_event(sctx->list_wait,
3129 atomic_read(&sctx->bios_in_flight) == 0);
3130 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3131 scrub_blocked_if_needed(fs_info);
3134 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3135 key.type = BTRFS_METADATA_ITEM_KEY;
3137 key.type = BTRFS_EXTENT_ITEM_KEY;
3138 key.objectid = logical;
3139 key.offset = (u64)-1;
3141 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3146 ret = btrfs_previous_extent_item(root, path, 0);
3150 /* there's no smaller item, so stick with the
3152 btrfs_release_path(path);
3153 ret = btrfs_search_slot(NULL, root, &key,
3165 slot = path->slots[0];
3166 if (slot >= btrfs_header_nritems(l)) {
3167 ret = btrfs_next_leaf(root, path);
3176 btrfs_item_key_to_cpu(l, &key, slot);
3178 if (key.type == BTRFS_METADATA_ITEM_KEY)
3179 bytes = root->nodesize;
3183 if (key.objectid + bytes <= logical)
3186 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3187 key.type != BTRFS_METADATA_ITEM_KEY)
3190 if (key.objectid >= logical + map->stripe_len) {
3191 /* out of this device extent */
3192 if (key.objectid >= logic_end)
3197 extent = btrfs_item_ptr(l, slot,
3198 struct btrfs_extent_item);
3199 flags = btrfs_extent_flags(l, extent);
3200 generation = btrfs_extent_generation(l, extent);
3202 if (key.objectid < logical &&
3203 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
3205 "scrub: tree block %llu spanning "
3206 "stripes, ignored. logical=%llu",
3207 key.objectid, logical);
3212 extent_logical = key.objectid;
3216 * trim extent to this stripe
3218 if (extent_logical < logical) {
3219 extent_len -= logical - extent_logical;
3220 extent_logical = logical;
3222 if (extent_logical + extent_len >
3223 logical + map->stripe_len) {
3224 extent_len = logical + map->stripe_len -
3228 extent_physical = extent_logical - logical + physical;
3229 extent_dev = scrub_dev;
3230 extent_mirror_num = mirror_num;
3232 scrub_remap_extent(fs_info, extent_logical,
3233 extent_len, &extent_physical,
3235 &extent_mirror_num);
3237 ret = btrfs_lookup_csums_range(csum_root, logical,
3238 logical + map->stripe_len - 1,
3239 &sctx->csum_list, 1);
3243 ret = scrub_extent(sctx, extent_logical, extent_len,
3244 extent_physical, extent_dev, flags,
3245 generation, extent_mirror_num,
3246 extent_logical - logical + physical);
3250 scrub_free_csums(sctx);
3251 if (extent_logical + extent_len <
3252 key.objectid + bytes) {
3253 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3255 * loop until we find next data stripe
3256 * or we have finished all stripes.
3259 physical += map->stripe_len;
3260 ret = get_raid56_logic_offset(physical,
3265 if (ret && physical < physical_end) {
3266 stripe_logical += base;
3267 stripe_end = stripe_logical +
3269 ret = scrub_raid56_parity(sctx,
3270 map, scrub_dev, ppath,
3278 physical += map->stripe_len;
3279 logical += increment;
3281 if (logical < key.objectid + bytes) {
3286 if (physical >= physical_end) {
3294 btrfs_release_path(path);
3296 logical += increment;
3297 physical += map->stripe_len;
3298 spin_lock(&sctx->stat_lock);
3300 sctx->stat.last_physical = map->stripes[num].physical +
3303 sctx->stat.last_physical = physical;
3304 spin_unlock(&sctx->stat_lock);
3309 /* push queued extents */
3311 mutex_lock(&sctx->wr_ctx.wr_lock);
3312 scrub_wr_submit(sctx);
3313 mutex_unlock(&sctx->wr_ctx.wr_lock);
3315 blk_finish_plug(&plug);
3316 btrfs_free_path(path);
3317 btrfs_free_path(ppath);
3318 return ret < 0 ? ret : 0;
3321 static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
3322 struct btrfs_device *scrub_dev,
3323 u64 chunk_tree, u64 chunk_objectid,
3324 u64 chunk_offset, u64 length,
3325 u64 dev_offset, int is_dev_replace)
3327 struct btrfs_mapping_tree *map_tree =
3328 &sctx->dev_root->fs_info->mapping_tree;
3329 struct map_lookup *map;
3330 struct extent_map *em;
3334 read_lock(&map_tree->map_tree.lock);
3335 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3336 read_unlock(&map_tree->map_tree.lock);
3341 map = (struct map_lookup *)em->bdev;
3342 if (em->start != chunk_offset)
3345 if (em->len < length)
3348 for (i = 0; i < map->num_stripes; ++i) {
3349 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
3350 map->stripes[i].physical == dev_offset) {
3351 ret = scrub_stripe(sctx, map, scrub_dev, i,
3352 chunk_offset, length,
3359 free_extent_map(em);
3364 static noinline_for_stack
3365 int scrub_enumerate_chunks(struct scrub_ctx *sctx,
3366 struct btrfs_device *scrub_dev, u64 start, u64 end,
3369 struct btrfs_dev_extent *dev_extent = NULL;
3370 struct btrfs_path *path;
3371 struct btrfs_root *root = sctx->dev_root;
3372 struct btrfs_fs_info *fs_info = root->fs_info;
3379 struct extent_buffer *l;
3380 struct btrfs_key key;
3381 struct btrfs_key found_key;
3382 struct btrfs_block_group_cache *cache;
3383 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
3385 path = btrfs_alloc_path();
3390 path->search_commit_root = 1;
3391 path->skip_locking = 1;
3393 key.objectid = scrub_dev->devid;
3395 key.type = BTRFS_DEV_EXTENT_KEY;
3398 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3402 if (path->slots[0] >=
3403 btrfs_header_nritems(path->nodes[0])) {
3404 ret = btrfs_next_leaf(root, path);
3411 slot = path->slots[0];
3413 btrfs_item_key_to_cpu(l, &found_key, slot);
3415 if (found_key.objectid != scrub_dev->devid)
3418 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
3421 if (found_key.offset >= end)
3424 if (found_key.offset < key.offset)
3427 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3428 length = btrfs_dev_extent_length(l, dev_extent);
3430 if (found_key.offset + length <= start)
3433 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3434 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3435 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3438 * get a reference on the corresponding block group to prevent
3439 * the chunk from going away while we scrub it
3441 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3443 /* some chunks are removed but not committed to disk yet,
3444 * continue scrubbing */
3448 dev_replace->cursor_right = found_key.offset + length;
3449 dev_replace->cursor_left = found_key.offset;
3450 dev_replace->item_needs_writeback = 1;
3451 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
3452 chunk_offset, length, found_key.offset,
3456 * flush, submit all pending read and write bios, afterwards
3458 * Note that in the dev replace case, a read request causes
3459 * write requests that are submitted in the read completion
3460 * worker. Therefore in the current situation, it is required
3461 * that all write requests are flushed, so that all read and
3462 * write requests are really completed when bios_in_flight
3465 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3467 mutex_lock(&sctx->wr_ctx.wr_lock);
3468 scrub_wr_submit(sctx);
3469 mutex_unlock(&sctx->wr_ctx.wr_lock);
3471 wait_event(sctx->list_wait,
3472 atomic_read(&sctx->bios_in_flight) == 0);
3473 atomic_inc(&fs_info->scrubs_paused);
3474 wake_up(&fs_info->scrub_pause_wait);
3477 * must be called before we decrease @scrub_paused.
3478 * make sure we don't block transaction commit while
3479 * we are waiting pending workers finished.
3481 wait_event(sctx->list_wait,
3482 atomic_read(&sctx->workers_pending) == 0);
3483 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3485 mutex_lock(&fs_info->scrub_lock);
3486 __scrub_blocked_if_needed(fs_info);
3487 atomic_dec(&fs_info->scrubs_paused);
3488 mutex_unlock(&fs_info->scrub_lock);
3489 wake_up(&fs_info->scrub_pause_wait);
3491 btrfs_put_block_group(cache);
3494 if (is_dev_replace &&
3495 atomic64_read(&dev_replace->num_write_errors) > 0) {
3499 if (sctx->stat.malloc_errors > 0) {
3504 dev_replace->cursor_left = dev_replace->cursor_right;
3505 dev_replace->item_needs_writeback = 1;
3507 key.offset = found_key.offset + length;
3508 btrfs_release_path(path);
3511 btrfs_free_path(path);
3514 * ret can still be 1 from search_slot or next_leaf,
3515 * that's not an error
3517 return ret < 0 ? ret : 0;
3520 static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3521 struct btrfs_device *scrub_dev)
3527 struct btrfs_root *root = sctx->dev_root;
3529 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
3532 /* Seed devices of a new filesystem has their own generation. */
3533 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3534 gen = scrub_dev->generation;
3536 gen = root->fs_info->last_trans_committed;
3538 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3539 bytenr = btrfs_sb_offset(i);
3540 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3541 scrub_dev->commit_total_bytes)
3544 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
3545 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
3550 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3556 * get a reference count on fs_info->scrub_workers. start worker if necessary
3558 static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3561 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
3562 int max_active = fs_info->thread_pool_size;
3564 if (fs_info->scrub_workers_refcnt == 0) {
3566 fs_info->scrub_workers =
3567 btrfs_alloc_workqueue("btrfs-scrub", flags,
3570 fs_info->scrub_workers =
3571 btrfs_alloc_workqueue("btrfs-scrub", flags,
3573 if (!fs_info->scrub_workers)
3574 goto fail_scrub_workers;
3576 fs_info->scrub_wr_completion_workers =
3577 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3579 if (!fs_info->scrub_wr_completion_workers)
3580 goto fail_scrub_wr_completion_workers;
3582 fs_info->scrub_nocow_workers =
3583 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
3584 if (!fs_info->scrub_nocow_workers)
3585 goto fail_scrub_nocow_workers;
3586 fs_info->scrub_parity_workers =
3587 btrfs_alloc_workqueue("btrfs-scrubparity", flags,
3589 if (!fs_info->scrub_parity_workers)
3590 goto fail_scrub_parity_workers;
3592 ++fs_info->scrub_workers_refcnt;
3595 fail_scrub_parity_workers:
3596 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
3597 fail_scrub_nocow_workers:
3598 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3599 fail_scrub_wr_completion_workers:
3600 btrfs_destroy_workqueue(fs_info->scrub_workers);
3605 static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
3607 if (--fs_info->scrub_workers_refcnt == 0) {
3608 btrfs_destroy_workqueue(fs_info->scrub_workers);
3609 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3610 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
3611 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
3613 WARN_ON(fs_info->scrub_workers_refcnt < 0);
3616 int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3617 u64 end, struct btrfs_scrub_progress *progress,
3618 int readonly, int is_dev_replace)
3620 struct scrub_ctx *sctx;
3622 struct btrfs_device *dev;
3623 struct rcu_string *name;
3625 if (btrfs_fs_closing(fs_info))
3628 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
3630 * in this case scrub is unable to calculate the checksum
3631 * the way scrub is implemented. Do not handle this
3632 * situation at all because it won't ever happen.
3635 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
3636 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
3640 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
3641 /* not supported for data w/o checksums */
3643 "scrub: size assumption sectorsize != PAGE_SIZE "
3644 "(%d != %lu) fails",
3645 fs_info->chunk_root->sectorsize, PAGE_SIZE);
3649 if (fs_info->chunk_root->nodesize >
3650 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3651 fs_info->chunk_root->sectorsize >
3652 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3654 * would exhaust the array bounds of pagev member in
3655 * struct scrub_block
3657 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3658 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
3659 fs_info->chunk_root->nodesize,
3660 SCRUB_MAX_PAGES_PER_BLOCK,
3661 fs_info->chunk_root->sectorsize,
3662 SCRUB_MAX_PAGES_PER_BLOCK);
3667 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3668 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
3669 if (!dev || (dev->missing && !is_dev_replace)) {
3670 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3674 if (!is_dev_replace && !readonly && !dev->writeable) {
3675 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3677 name = rcu_dereference(dev->name);
3678 btrfs_err(fs_info, "scrub: device %s is not writable",
3684 mutex_lock(&fs_info->scrub_lock);
3685 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
3686 mutex_unlock(&fs_info->scrub_lock);
3687 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3691 btrfs_dev_replace_lock(&fs_info->dev_replace);
3692 if (dev->scrub_device ||
3694 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3695 btrfs_dev_replace_unlock(&fs_info->dev_replace);
3696 mutex_unlock(&fs_info->scrub_lock);
3697 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3698 return -EINPROGRESS;
3700 btrfs_dev_replace_unlock(&fs_info->dev_replace);
3702 ret = scrub_workers_get(fs_info, is_dev_replace);
3704 mutex_unlock(&fs_info->scrub_lock);
3705 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3709 sctx = scrub_setup_ctx(dev, is_dev_replace);
3711 mutex_unlock(&fs_info->scrub_lock);
3712 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3713 scrub_workers_put(fs_info);
3714 return PTR_ERR(sctx);
3716 sctx->readonly = readonly;
3717 dev->scrub_device = sctx;
3718 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3721 * checking @scrub_pause_req here, we can avoid
3722 * race between committing transaction and scrubbing.
3724 __scrub_blocked_if_needed(fs_info);
3725 atomic_inc(&fs_info->scrubs_running);
3726 mutex_unlock(&fs_info->scrub_lock);
3728 if (!is_dev_replace) {
3730 * by holding device list mutex, we can
3731 * kick off writing super in log tree sync.
3733 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3734 ret = scrub_supers(sctx, dev);
3735 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3739 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3742 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3743 atomic_dec(&fs_info->scrubs_running);
3744 wake_up(&fs_info->scrub_pause_wait);
3746 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
3749 memcpy(progress, &sctx->stat, sizeof(*progress));
3751 mutex_lock(&fs_info->scrub_lock);
3752 dev->scrub_device = NULL;
3753 scrub_workers_put(fs_info);
3754 mutex_unlock(&fs_info->scrub_lock);
3756 scrub_put_ctx(sctx);
3761 void btrfs_scrub_pause(struct btrfs_root *root)
3763 struct btrfs_fs_info *fs_info = root->fs_info;
3765 mutex_lock(&fs_info->scrub_lock);
3766 atomic_inc(&fs_info->scrub_pause_req);
3767 while (atomic_read(&fs_info->scrubs_paused) !=
3768 atomic_read(&fs_info->scrubs_running)) {
3769 mutex_unlock(&fs_info->scrub_lock);
3770 wait_event(fs_info->scrub_pause_wait,
3771 atomic_read(&fs_info->scrubs_paused) ==
3772 atomic_read(&fs_info->scrubs_running));
3773 mutex_lock(&fs_info->scrub_lock);
3775 mutex_unlock(&fs_info->scrub_lock);
3778 void btrfs_scrub_continue(struct btrfs_root *root)
3780 struct btrfs_fs_info *fs_info = root->fs_info;
3782 atomic_dec(&fs_info->scrub_pause_req);
3783 wake_up(&fs_info->scrub_pause_wait);
3786 int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
3788 mutex_lock(&fs_info->scrub_lock);
3789 if (!atomic_read(&fs_info->scrubs_running)) {
3790 mutex_unlock(&fs_info->scrub_lock);
3794 atomic_inc(&fs_info->scrub_cancel_req);
3795 while (atomic_read(&fs_info->scrubs_running)) {
3796 mutex_unlock(&fs_info->scrub_lock);
3797 wait_event(fs_info->scrub_pause_wait,
3798 atomic_read(&fs_info->scrubs_running) == 0);
3799 mutex_lock(&fs_info->scrub_lock);
3801 atomic_dec(&fs_info->scrub_cancel_req);
3802 mutex_unlock(&fs_info->scrub_lock);
3807 int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3808 struct btrfs_device *dev)
3810 struct scrub_ctx *sctx;
3812 mutex_lock(&fs_info->scrub_lock);
3813 sctx = dev->scrub_device;
3815 mutex_unlock(&fs_info->scrub_lock);
3818 atomic_inc(&sctx->cancel_req);
3819 while (dev->scrub_device) {
3820 mutex_unlock(&fs_info->scrub_lock);
3821 wait_event(fs_info->scrub_pause_wait,
3822 dev->scrub_device == NULL);
3823 mutex_lock(&fs_info->scrub_lock);
3825 mutex_unlock(&fs_info->scrub_lock);
3830 int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3831 struct btrfs_scrub_progress *progress)
3833 struct btrfs_device *dev;
3834 struct scrub_ctx *sctx = NULL;
3836 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
3837 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
3839 sctx = dev->scrub_device;
3841 memcpy(progress, &sctx->stat, sizeof(*progress));
3842 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3844 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
3847 static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3848 u64 extent_logical, u64 extent_len,
3849 u64 *extent_physical,
3850 struct btrfs_device **extent_dev,
3851 int *extent_mirror_num)
3854 struct btrfs_bio *bbio = NULL;
3857 mapped_length = extent_len;
3858 ret = btrfs_map_block(fs_info, READ, extent_logical,
3859 &mapped_length, &bbio, 0);
3860 if (ret || !bbio || mapped_length < extent_len ||
3861 !bbio->stripes[0].dev->bdev) {
3862 btrfs_put_bbio(bbio);
3866 *extent_physical = bbio->stripes[0].physical;
3867 *extent_mirror_num = bbio->mirror_num;
3868 *extent_dev = bbio->stripes[0].dev;
3869 btrfs_put_bbio(bbio);
3872 static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3873 struct scrub_wr_ctx *wr_ctx,
3874 struct btrfs_fs_info *fs_info,
3875 struct btrfs_device *dev,
3878 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3880 mutex_init(&wr_ctx->wr_lock);
3881 wr_ctx->wr_curr_bio = NULL;
3882 if (!is_dev_replace)
3885 WARN_ON(!dev->bdev);
3886 wr_ctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
3887 wr_ctx->tgtdev = dev;
3888 atomic_set(&wr_ctx->flush_all_writes, 0);
3892 static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3894 mutex_lock(&wr_ctx->wr_lock);
3895 kfree(wr_ctx->wr_curr_bio);
3896 wr_ctx->wr_curr_bio = NULL;
3897 mutex_unlock(&wr_ctx->wr_lock);
3900 static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3901 int mirror_num, u64 physical_for_dev_replace)
3903 struct scrub_copy_nocow_ctx *nocow_ctx;
3904 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3906 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3908 spin_lock(&sctx->stat_lock);
3909 sctx->stat.malloc_errors++;
3910 spin_unlock(&sctx->stat_lock);
3914 scrub_pending_trans_workers_inc(sctx);
3916 nocow_ctx->sctx = sctx;
3917 nocow_ctx->logical = logical;
3918 nocow_ctx->len = len;
3919 nocow_ctx->mirror_num = mirror_num;
3920 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
3921 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3922 copy_nocow_pages_worker, NULL, NULL);
3923 INIT_LIST_HEAD(&nocow_ctx->inodes);
3924 btrfs_queue_work(fs_info->scrub_nocow_workers,
3930 static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3932 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3933 struct scrub_nocow_inode *nocow_inode;
3935 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3938 nocow_inode->inum = inum;
3939 nocow_inode->offset = offset;
3940 nocow_inode->root = root;
3941 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3945 #define COPY_COMPLETE 1
3947 static void copy_nocow_pages_worker(struct btrfs_work *work)
3949 struct scrub_copy_nocow_ctx *nocow_ctx =
3950 container_of(work, struct scrub_copy_nocow_ctx, work);
3951 struct scrub_ctx *sctx = nocow_ctx->sctx;
3952 u64 logical = nocow_ctx->logical;
3953 u64 len = nocow_ctx->len;
3954 int mirror_num = nocow_ctx->mirror_num;
3955 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3957 struct btrfs_trans_handle *trans = NULL;
3958 struct btrfs_fs_info *fs_info;
3959 struct btrfs_path *path;
3960 struct btrfs_root *root;
3961 int not_written = 0;
3963 fs_info = sctx->dev_root->fs_info;
3964 root = fs_info->extent_root;
3966 path = btrfs_alloc_path();
3968 spin_lock(&sctx->stat_lock);
3969 sctx->stat.malloc_errors++;
3970 spin_unlock(&sctx->stat_lock);
3975 trans = btrfs_join_transaction(root);
3976 if (IS_ERR(trans)) {
3981 ret = iterate_inodes_from_logical(logical, fs_info, path,
3982 record_inode_for_nocow, nocow_ctx);
3983 if (ret != 0 && ret != -ENOENT) {
3984 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
3985 "phys %llu, len %llu, mir %u, ret %d",
3986 logical, physical_for_dev_replace, len, mirror_num,
3992 btrfs_end_transaction(trans, root);
3994 while (!list_empty(&nocow_ctx->inodes)) {
3995 struct scrub_nocow_inode *entry;
3996 entry = list_first_entry(&nocow_ctx->inodes,
3997 struct scrub_nocow_inode,
3999 list_del_init(&entry->list);
4000 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4001 entry->root, nocow_ctx);
4003 if (ret == COPY_COMPLETE) {
4011 while (!list_empty(&nocow_ctx->inodes)) {
4012 struct scrub_nocow_inode *entry;
4013 entry = list_first_entry(&nocow_ctx->inodes,
4014 struct scrub_nocow_inode,
4016 list_del_init(&entry->list);
4019 if (trans && !IS_ERR(trans))
4020 btrfs_end_transaction(trans, root);
4022 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4023 num_uncorrectable_read_errors);
4025 btrfs_free_path(path);
4028 scrub_pending_trans_workers_dec(sctx);
4031 static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4034 struct extent_state *cached_state = NULL;
4035 struct btrfs_ordered_extent *ordered;
4036 struct extent_io_tree *io_tree;
4037 struct extent_map *em;
4038 u64 lockstart = start, lockend = start + len - 1;
4041 io_tree = &BTRFS_I(inode)->io_tree;
4043 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4044 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4046 btrfs_put_ordered_extent(ordered);
4051 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4058 * This extent does not actually cover the logical extent anymore,
4059 * move on to the next inode.
4061 if (em->block_start > logical ||
4062 em->block_start + em->block_len < logical + len) {
4063 free_extent_map(em);
4067 free_extent_map(em);
4070 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4075 static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4076 struct scrub_copy_nocow_ctx *nocow_ctx)
4078 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
4079 struct btrfs_key key;
4080 struct inode *inode;
4082 struct btrfs_root *local_root;
4083 struct extent_io_tree *io_tree;
4084 u64 physical_for_dev_replace;
4085 u64 nocow_ctx_logical;
4086 u64 len = nocow_ctx->len;
4087 unsigned long index;
4092 key.objectid = root;
4093 key.type = BTRFS_ROOT_ITEM_KEY;
4094 key.offset = (u64)-1;
4096 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4098 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
4099 if (IS_ERR(local_root)) {
4100 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
4101 return PTR_ERR(local_root);
4104 key.type = BTRFS_INODE_ITEM_KEY;
4105 key.objectid = inum;
4107 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
4108 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
4110 return PTR_ERR(inode);
4112 /* Avoid truncate/dio/punch hole.. */
4113 mutex_lock(&inode->i_mutex);
4114 inode_dio_wait(inode);
4116 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4117 io_tree = &BTRFS_I(inode)->io_tree;
4118 nocow_ctx_logical = nocow_ctx->logical;
4120 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4122 ret = ret > 0 ? 0 : ret;
4126 while (len >= PAGE_CACHE_SIZE) {
4127 index = offset >> PAGE_CACHE_SHIFT;
4129 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4131 btrfs_err(fs_info, "find_or_create_page() failed");
4136 if (PageUptodate(page)) {
4137 if (PageDirty(page))
4140 ClearPageError(page);
4141 err = extent_read_full_page(io_tree, page,
4143 nocow_ctx->mirror_num);
4151 * If the page has been remove from the page cache,
4152 * the data on it is meaningless, because it may be
4153 * old one, the new data may be written into the new
4154 * page in the page cache.
4156 if (page->mapping != inode->i_mapping) {
4158 page_cache_release(page);
4161 if (!PageUptodate(page)) {
4167 ret = check_extent_to_block(inode, offset, len,
4170 ret = ret > 0 ? 0 : ret;
4174 err = write_page_nocow(nocow_ctx->sctx,
4175 physical_for_dev_replace, page);
4180 page_cache_release(page);
4185 offset += PAGE_CACHE_SIZE;
4186 physical_for_dev_replace += PAGE_CACHE_SIZE;
4187 nocow_ctx_logical += PAGE_CACHE_SIZE;
4188 len -= PAGE_CACHE_SIZE;
4190 ret = COPY_COMPLETE;
4192 mutex_unlock(&inode->i_mutex);
4197 static int write_page_nocow(struct scrub_ctx *sctx,
4198 u64 physical_for_dev_replace, struct page *page)
4201 struct btrfs_device *dev;
4204 dev = sctx->wr_ctx.tgtdev;
4208 printk_ratelimited(KERN_WARNING
4209 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
4212 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
4214 spin_lock(&sctx->stat_lock);
4215 sctx->stat.malloc_errors++;
4216 spin_unlock(&sctx->stat_lock);
4219 bio->bi_iter.bi_size = 0;
4220 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
4221 bio->bi_bdev = dev->bdev;
4222 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4223 if (ret != PAGE_CACHE_SIZE) {
4226 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4230 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
4231 goto leave_with_eio;