Linux 3.12.28
[linux/fpc-iii.git] / fs / btrfs / scrub.c
blob0b23100dd8abc4702e6e48bb66bc79bf6bfd1150
1 /*
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>
21 #include "ctree.h"
22 #include "volumes.h"
23 #include "disk-io.h"
24 #include "ordered-data.h"
25 #include "transaction.h"
26 #include "backref.h"
27 #include "extent_io.h"
28 #include "dev-replace.h"
29 #include "check-integrity.h"
30 #include "rcu-string.h"
31 #include "raid56.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
37 * any can be found.
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
46 struct scrub_block;
47 struct scrub_ctx;
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_page {
67 struct scrub_block *sblock;
68 struct page *page;
69 struct btrfs_device *dev;
70 u64 flags; /* extent flags */
71 u64 generation;
72 u64 logical;
73 u64 physical;
74 u64 physical_for_dev_replace;
75 atomic_t ref_count;
76 struct {
77 unsigned int mirror_num:8;
78 unsigned int have_csum:1;
79 unsigned int io_error:1;
81 u8 csum[BTRFS_CSUM_SIZE];
84 struct scrub_bio {
85 int index;
86 struct scrub_ctx *sctx;
87 struct btrfs_device *dev;
88 struct bio *bio;
89 int err;
90 u64 logical;
91 u64 physical;
92 #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
93 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
94 #else
95 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
96 #endif
97 int page_count;
98 int next_free;
99 struct btrfs_work work;
102 struct scrub_block {
103 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
104 int page_count;
105 atomic_t outstanding_pages;
106 atomic_t ref_count; /* free mem on transition to zero */
107 struct scrub_ctx *sctx;
108 struct {
109 unsigned int header_error:1;
110 unsigned int checksum_error:1;
111 unsigned int no_io_error_seen:1;
112 unsigned int generation_error:1; /* also sets header_error */
116 struct scrub_wr_ctx {
117 struct scrub_bio *wr_curr_bio;
118 struct btrfs_device *tgtdev;
119 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
120 atomic_t flush_all_writes;
121 struct mutex wr_lock;
124 struct scrub_ctx {
125 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
126 struct btrfs_root *dev_root;
127 int first_free;
128 int curr;
129 atomic_t bios_in_flight;
130 atomic_t workers_pending;
131 spinlock_t list_lock;
132 wait_queue_head_t list_wait;
133 u16 csum_size;
134 struct list_head csum_list;
135 atomic_t cancel_req;
136 int readonly;
137 int pages_per_rd_bio;
138 u32 sectorsize;
139 u32 nodesize;
140 u32 leafsize;
142 int is_dev_replace;
143 struct scrub_wr_ctx wr_ctx;
146 * statistics
148 struct btrfs_scrub_progress stat;
149 spinlock_t stat_lock;
152 struct scrub_fixup_nodatasum {
153 struct scrub_ctx *sctx;
154 struct btrfs_device *dev;
155 u64 logical;
156 struct btrfs_root *root;
157 struct btrfs_work work;
158 int mirror_num;
161 struct scrub_nocow_inode {
162 u64 inum;
163 u64 offset;
164 u64 root;
165 struct list_head list;
168 struct scrub_copy_nocow_ctx {
169 struct scrub_ctx *sctx;
170 u64 logical;
171 u64 len;
172 int mirror_num;
173 u64 physical_for_dev_replace;
174 struct list_head inodes;
175 struct btrfs_work work;
178 struct scrub_warning {
179 struct btrfs_path *path;
180 u64 extent_item_size;
181 char *scratch_buf;
182 char *msg_buf;
183 const char *errstr;
184 sector_t sector;
185 u64 logical;
186 struct btrfs_device *dev;
187 int msg_bufsize;
188 int scratch_bufsize;
192 static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
193 static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
194 static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
195 static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
196 static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
197 static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
198 struct btrfs_fs_info *fs_info,
199 struct scrub_block *original_sblock,
200 u64 length, u64 logical,
201 struct scrub_block *sblocks_for_recheck);
202 static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
203 struct scrub_block *sblock, int is_metadata,
204 int have_csum, u8 *csum, u64 generation,
205 u16 csum_size);
206 static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
207 struct scrub_block *sblock,
208 int is_metadata, int have_csum,
209 const u8 *csum, u64 generation,
210 u16 csum_size);
211 static void scrub_complete_bio_end_io(struct bio *bio, int err);
212 static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
213 struct scrub_block *sblock_good,
214 int force_write);
215 static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
216 struct scrub_block *sblock_good,
217 int page_num, int force_write);
218 static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
219 static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
220 int page_num);
221 static int scrub_checksum_data(struct scrub_block *sblock);
222 static int scrub_checksum_tree_block(struct scrub_block *sblock);
223 static int scrub_checksum_super(struct scrub_block *sblock);
224 static void scrub_block_get(struct scrub_block *sblock);
225 static void scrub_block_put(struct scrub_block *sblock);
226 static void scrub_page_get(struct scrub_page *spage);
227 static void scrub_page_put(struct scrub_page *spage);
228 static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
229 struct scrub_page *spage);
230 static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
231 u64 physical, struct btrfs_device *dev, u64 flags,
232 u64 gen, int mirror_num, u8 *csum, int force,
233 u64 physical_for_dev_replace);
234 static void scrub_bio_end_io(struct bio *bio, int err);
235 static void scrub_bio_end_io_worker(struct btrfs_work *work);
236 static void scrub_block_complete(struct scrub_block *sblock);
237 static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
238 u64 extent_logical, u64 extent_len,
239 u64 *extent_physical,
240 struct btrfs_device **extent_dev,
241 int *extent_mirror_num);
242 static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
243 struct scrub_wr_ctx *wr_ctx,
244 struct btrfs_fs_info *fs_info,
245 struct btrfs_device *dev,
246 int is_dev_replace);
247 static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
248 static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
249 struct scrub_page *spage);
250 static void scrub_wr_submit(struct scrub_ctx *sctx);
251 static void scrub_wr_bio_end_io(struct bio *bio, int err);
252 static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
253 static int write_page_nocow(struct scrub_ctx *sctx,
254 u64 physical_for_dev_replace, struct page *page);
255 static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
256 struct scrub_copy_nocow_ctx *ctx);
257 static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
258 int mirror_num, u64 physical_for_dev_replace);
259 static void copy_nocow_pages_worker(struct btrfs_work *work);
262 static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
264 atomic_inc(&sctx->bios_in_flight);
267 static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
269 atomic_dec(&sctx->bios_in_flight);
270 wake_up(&sctx->list_wait);
274 * used for workers that require transaction commits (i.e., for the
275 * NOCOW case)
277 static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
279 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
282 * increment scrubs_running to prevent cancel requests from
283 * completing as long as a worker is running. we must also
284 * increment scrubs_paused to prevent deadlocking on pause
285 * requests used for transactions commits (as the worker uses a
286 * transaction context). it is safe to regard the worker
287 * as paused for all matters practical. effectively, we only
288 * avoid cancellation requests from completing.
290 mutex_lock(&fs_info->scrub_lock);
291 atomic_inc(&fs_info->scrubs_running);
292 atomic_inc(&fs_info->scrubs_paused);
293 mutex_unlock(&fs_info->scrub_lock);
294 atomic_inc(&sctx->workers_pending);
297 /* used for workers that require transaction commits */
298 static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
300 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
303 * see scrub_pending_trans_workers_inc() why we're pretending
304 * to be paused in the scrub counters
306 mutex_lock(&fs_info->scrub_lock);
307 atomic_dec(&fs_info->scrubs_running);
308 atomic_dec(&fs_info->scrubs_paused);
309 mutex_unlock(&fs_info->scrub_lock);
310 atomic_dec(&sctx->workers_pending);
311 wake_up(&fs_info->scrub_pause_wait);
312 wake_up(&sctx->list_wait);
315 static void scrub_free_csums(struct scrub_ctx *sctx)
317 while (!list_empty(&sctx->csum_list)) {
318 struct btrfs_ordered_sum *sum;
319 sum = list_first_entry(&sctx->csum_list,
320 struct btrfs_ordered_sum, list);
321 list_del(&sum->list);
322 kfree(sum);
326 static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
328 int i;
330 if (!sctx)
331 return;
333 scrub_free_wr_ctx(&sctx->wr_ctx);
335 /* this can happen when scrub is cancelled */
336 if (sctx->curr != -1) {
337 struct scrub_bio *sbio = sctx->bios[sctx->curr];
339 for (i = 0; i < sbio->page_count; i++) {
340 WARN_ON(!sbio->pagev[i]->page);
341 scrub_block_put(sbio->pagev[i]->sblock);
343 bio_put(sbio->bio);
346 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
347 struct scrub_bio *sbio = sctx->bios[i];
349 if (!sbio)
350 break;
351 kfree(sbio);
354 scrub_free_csums(sctx);
355 kfree(sctx);
358 static noinline_for_stack
359 struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
361 struct scrub_ctx *sctx;
362 int i;
363 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
364 int pages_per_rd_bio;
365 int ret;
368 * the setting of pages_per_rd_bio is correct for scrub but might
369 * be wrong for the dev_replace code where we might read from
370 * different devices in the initial huge bios. However, that
371 * code is able to correctly handle the case when adding a page
372 * to a bio fails.
374 if (dev->bdev)
375 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
376 bio_get_nr_vecs(dev->bdev));
377 else
378 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
379 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
380 if (!sctx)
381 goto nomem;
382 sctx->is_dev_replace = is_dev_replace;
383 sctx->pages_per_rd_bio = pages_per_rd_bio;
384 sctx->curr = -1;
385 sctx->dev_root = dev->dev_root;
386 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
387 struct scrub_bio *sbio;
389 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
390 if (!sbio)
391 goto nomem;
392 sctx->bios[i] = sbio;
394 sbio->index = i;
395 sbio->sctx = sctx;
396 sbio->page_count = 0;
397 sbio->work.func = scrub_bio_end_io_worker;
399 if (i != SCRUB_BIOS_PER_SCTX - 1)
400 sctx->bios[i]->next_free = i + 1;
401 else
402 sctx->bios[i]->next_free = -1;
404 sctx->first_free = 0;
405 sctx->nodesize = dev->dev_root->nodesize;
406 sctx->leafsize = dev->dev_root->leafsize;
407 sctx->sectorsize = dev->dev_root->sectorsize;
408 atomic_set(&sctx->bios_in_flight, 0);
409 atomic_set(&sctx->workers_pending, 0);
410 atomic_set(&sctx->cancel_req, 0);
411 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
412 INIT_LIST_HEAD(&sctx->csum_list);
414 spin_lock_init(&sctx->list_lock);
415 spin_lock_init(&sctx->stat_lock);
416 init_waitqueue_head(&sctx->list_wait);
418 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
419 fs_info->dev_replace.tgtdev, is_dev_replace);
420 if (ret) {
421 scrub_free_ctx(sctx);
422 return ERR_PTR(ret);
424 return sctx;
426 nomem:
427 scrub_free_ctx(sctx);
428 return ERR_PTR(-ENOMEM);
431 static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
432 void *warn_ctx)
434 u64 isize;
435 u32 nlink;
436 int ret;
437 int i;
438 struct extent_buffer *eb;
439 struct btrfs_inode_item *inode_item;
440 struct scrub_warning *swarn = warn_ctx;
441 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
442 struct inode_fs_paths *ipath = NULL;
443 struct btrfs_root *local_root;
444 struct btrfs_key root_key;
446 root_key.objectid = root;
447 root_key.type = BTRFS_ROOT_ITEM_KEY;
448 root_key.offset = (u64)-1;
449 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
450 if (IS_ERR(local_root)) {
451 ret = PTR_ERR(local_root);
452 goto err;
455 ret = inode_item_info(inum, 0, local_root, swarn->path);
456 if (ret) {
457 btrfs_release_path(swarn->path);
458 goto err;
461 eb = swarn->path->nodes[0];
462 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
463 struct btrfs_inode_item);
464 isize = btrfs_inode_size(eb, inode_item);
465 nlink = btrfs_inode_nlink(eb, inode_item);
466 btrfs_release_path(swarn->path);
468 ipath = init_ipath(4096, local_root, swarn->path);
469 if (IS_ERR(ipath)) {
470 ret = PTR_ERR(ipath);
471 ipath = NULL;
472 goto err;
474 ret = paths_from_inode(inum, ipath);
476 if (ret < 0)
477 goto err;
480 * we deliberately ignore the bit ipath might have been too small to
481 * hold all of the paths here
483 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
484 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
485 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
486 "length %llu, links %u (path: %s)\n", swarn->errstr,
487 swarn->logical, rcu_str_deref(swarn->dev->name),
488 (unsigned long long)swarn->sector, root, inum, offset,
489 min(isize - offset, (u64)PAGE_SIZE), nlink,
490 (char *)(unsigned long)ipath->fspath->val[i]);
492 free_ipath(ipath);
493 return 0;
495 err:
496 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
497 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
498 "resolving failed with ret=%d\n", swarn->errstr,
499 swarn->logical, rcu_str_deref(swarn->dev->name),
500 (unsigned long long)swarn->sector, root, inum, offset, ret);
502 free_ipath(ipath);
503 return 0;
506 static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
508 struct btrfs_device *dev;
509 struct btrfs_fs_info *fs_info;
510 struct btrfs_path *path;
511 struct btrfs_key found_key;
512 struct extent_buffer *eb;
513 struct btrfs_extent_item *ei;
514 struct scrub_warning swarn;
515 unsigned long ptr = 0;
516 u64 extent_item_pos;
517 u64 flags = 0;
518 u64 ref_root;
519 u32 item_size;
520 u8 ref_level;
521 const int bufsize = 4096;
522 int ret;
524 WARN_ON(sblock->page_count < 1);
525 dev = sblock->pagev[0]->dev;
526 fs_info = sblock->sctx->dev_root->fs_info;
528 path = btrfs_alloc_path();
530 swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
531 swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
532 swarn.sector = (sblock->pagev[0]->physical) >> 9;
533 swarn.logical = sblock->pagev[0]->logical;
534 swarn.errstr = errstr;
535 swarn.dev = NULL;
536 swarn.msg_bufsize = bufsize;
537 swarn.scratch_bufsize = bufsize;
539 if (!path || !swarn.scratch_buf || !swarn.msg_buf)
540 goto out;
542 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
543 &flags);
544 if (ret < 0)
545 goto out;
547 extent_item_pos = swarn.logical - found_key.objectid;
548 swarn.extent_item_size = found_key.offset;
550 eb = path->nodes[0];
551 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
552 item_size = btrfs_item_size_nr(eb, path->slots[0]);
554 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
555 do {
556 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
557 item_size, &ref_root,
558 &ref_level);
559 printk_in_rcu(KERN_WARNING
560 "btrfs: %s at logical %llu on dev %s, "
561 "sector %llu: metadata %s (level %d) in tree "
562 "%llu\n", errstr, swarn.logical,
563 rcu_str_deref(dev->name),
564 (unsigned long long)swarn.sector,
565 ref_level ? "node" : "leaf",
566 ret < 0 ? -1 : ref_level,
567 ret < 0 ? -1 : ref_root);
568 } while (ret != 1);
569 btrfs_release_path(path);
570 } else {
571 btrfs_release_path(path);
572 swarn.path = path;
573 swarn.dev = dev;
574 iterate_extent_inodes(fs_info, found_key.objectid,
575 extent_item_pos, 1,
576 scrub_print_warning_inode, &swarn);
579 out:
580 btrfs_free_path(path);
581 kfree(swarn.scratch_buf);
582 kfree(swarn.msg_buf);
585 static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
587 struct page *page = NULL;
588 unsigned long index;
589 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
590 int ret;
591 int corrected = 0;
592 struct btrfs_key key;
593 struct inode *inode = NULL;
594 struct btrfs_fs_info *fs_info;
595 u64 end = offset + PAGE_SIZE - 1;
596 struct btrfs_root *local_root;
597 int srcu_index;
599 key.objectid = root;
600 key.type = BTRFS_ROOT_ITEM_KEY;
601 key.offset = (u64)-1;
603 fs_info = fixup->root->fs_info;
604 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
606 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
607 if (IS_ERR(local_root)) {
608 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
609 return PTR_ERR(local_root);
612 key.type = BTRFS_INODE_ITEM_KEY;
613 key.objectid = inum;
614 key.offset = 0;
615 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
616 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
617 if (IS_ERR(inode))
618 return PTR_ERR(inode);
620 index = offset >> PAGE_CACHE_SHIFT;
622 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
623 if (!page) {
624 ret = -ENOMEM;
625 goto out;
628 if (PageUptodate(page)) {
629 if (PageDirty(page)) {
631 * we need to write the data to the defect sector. the
632 * data that was in that sector is not in memory,
633 * because the page was modified. we must not write the
634 * modified page to that sector.
636 * TODO: what could be done here: wait for the delalloc
637 * runner to write out that page (might involve
638 * COW) and see whether the sector is still
639 * referenced afterwards.
641 * For the meantime, we'll treat this error
642 * incorrectable, although there is a chance that a
643 * later scrub will find the bad sector again and that
644 * there's no dirty page in memory, then.
646 ret = -EIO;
647 goto out;
649 fs_info = BTRFS_I(inode)->root->fs_info;
650 ret = repair_io_failure(fs_info, offset, PAGE_SIZE,
651 fixup->logical, page,
652 fixup->mirror_num);
653 unlock_page(page);
654 corrected = !ret;
655 } else {
657 * we need to get good data first. the general readpage path
658 * will call repair_io_failure for us, we just have to make
659 * sure we read the bad mirror.
661 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
662 EXTENT_DAMAGED, GFP_NOFS);
663 if (ret) {
664 /* set_extent_bits should give proper error */
665 WARN_ON(ret > 0);
666 if (ret > 0)
667 ret = -EFAULT;
668 goto out;
671 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
672 btrfs_get_extent,
673 fixup->mirror_num);
674 wait_on_page_locked(page);
676 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
677 end, EXTENT_DAMAGED, 0, NULL);
678 if (!corrected)
679 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
680 EXTENT_DAMAGED, GFP_NOFS);
683 out:
684 if (page)
685 put_page(page);
686 if (inode)
687 iput(inode);
689 if (ret < 0)
690 return ret;
692 if (ret == 0 && corrected) {
694 * we only need to call readpage for one of the inodes belonging
695 * to this extent. so make iterate_extent_inodes stop
697 return 1;
700 return -EIO;
703 static void scrub_fixup_nodatasum(struct btrfs_work *work)
705 int ret;
706 struct scrub_fixup_nodatasum *fixup;
707 struct scrub_ctx *sctx;
708 struct btrfs_trans_handle *trans = NULL;
709 struct btrfs_fs_info *fs_info;
710 struct btrfs_path *path;
711 int uncorrectable = 0;
713 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
714 sctx = fixup->sctx;
715 fs_info = fixup->root->fs_info;
717 path = btrfs_alloc_path();
718 if (!path) {
719 spin_lock(&sctx->stat_lock);
720 ++sctx->stat.malloc_errors;
721 spin_unlock(&sctx->stat_lock);
722 uncorrectable = 1;
723 goto out;
726 trans = btrfs_join_transaction(fixup->root);
727 if (IS_ERR(trans)) {
728 uncorrectable = 1;
729 goto out;
733 * the idea is to trigger a regular read through the standard path. we
734 * read a page from the (failed) logical address by specifying the
735 * corresponding copynum of the failed sector. thus, that readpage is
736 * expected to fail.
737 * that is the point where on-the-fly error correction will kick in
738 * (once it's finished) and rewrite the failed sector if a good copy
739 * can be found.
741 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
742 path, scrub_fixup_readpage,
743 fixup);
744 if (ret < 0) {
745 uncorrectable = 1;
746 goto out;
748 WARN_ON(ret != 1);
750 spin_lock(&sctx->stat_lock);
751 ++sctx->stat.corrected_errors;
752 spin_unlock(&sctx->stat_lock);
754 out:
755 if (trans && !IS_ERR(trans))
756 btrfs_end_transaction(trans, fixup->root);
757 if (uncorrectable) {
758 spin_lock(&sctx->stat_lock);
759 ++sctx->stat.uncorrectable_errors;
760 spin_unlock(&sctx->stat_lock);
761 btrfs_dev_replace_stats_inc(
762 &sctx->dev_root->fs_info->dev_replace.
763 num_uncorrectable_read_errors);
764 printk_ratelimited_in_rcu(KERN_ERR
765 "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
766 fixup->logical, rcu_str_deref(fixup->dev->name));
769 btrfs_free_path(path);
770 kfree(fixup);
772 scrub_pending_trans_workers_dec(sctx);
776 * scrub_handle_errored_block gets called when either verification of the
777 * pages failed or the bio failed to read, e.g. with EIO. In the latter
778 * case, this function handles all pages in the bio, even though only one
779 * may be bad.
780 * The goal of this function is to repair the errored block by using the
781 * contents of one of the mirrors.
783 static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
785 struct scrub_ctx *sctx = sblock_to_check->sctx;
786 struct btrfs_device *dev;
787 struct btrfs_fs_info *fs_info;
788 u64 length;
789 u64 logical;
790 u64 generation;
791 unsigned int failed_mirror_index;
792 unsigned int is_metadata;
793 unsigned int have_csum;
794 u8 *csum;
795 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
796 struct scrub_block *sblock_bad;
797 int ret;
798 int mirror_index;
799 int page_num;
800 int success;
801 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
802 DEFAULT_RATELIMIT_BURST);
804 BUG_ON(sblock_to_check->page_count < 1);
805 fs_info = sctx->dev_root->fs_info;
806 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
808 * if we find an error in a super block, we just report it.
809 * They will get written with the next transaction commit
810 * anyway
812 spin_lock(&sctx->stat_lock);
813 ++sctx->stat.super_errors;
814 spin_unlock(&sctx->stat_lock);
815 return 0;
817 length = sblock_to_check->page_count * PAGE_SIZE;
818 logical = sblock_to_check->pagev[0]->logical;
819 generation = sblock_to_check->pagev[0]->generation;
820 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
821 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
822 is_metadata = !(sblock_to_check->pagev[0]->flags &
823 BTRFS_EXTENT_FLAG_DATA);
824 have_csum = sblock_to_check->pagev[0]->have_csum;
825 csum = sblock_to_check->pagev[0]->csum;
826 dev = sblock_to_check->pagev[0]->dev;
828 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
829 sblocks_for_recheck = NULL;
830 goto nodatasum_case;
834 * read all mirrors one after the other. This includes to
835 * re-read the extent or metadata block that failed (that was
836 * the cause that this fixup code is called) another time,
837 * page by page this time in order to know which pages
838 * caused I/O errors and which ones are good (for all mirrors).
839 * It is the goal to handle the situation when more than one
840 * mirror contains I/O errors, but the errors do not
841 * overlap, i.e. the data can be repaired by selecting the
842 * pages from those mirrors without I/O error on the
843 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
844 * would be that mirror #1 has an I/O error on the first page,
845 * the second page is good, and mirror #2 has an I/O error on
846 * the second page, but the first page is good.
847 * Then the first page of the first mirror can be repaired by
848 * taking the first page of the second mirror, and the
849 * second page of the second mirror can be repaired by
850 * copying the contents of the 2nd page of the 1st mirror.
851 * One more note: if the pages of one mirror contain I/O
852 * errors, the checksum cannot be verified. In order to get
853 * the best data for repairing, the first attempt is to find
854 * a mirror without I/O errors and with a validated checksum.
855 * Only if this is not possible, the pages are picked from
856 * mirrors with I/O errors without considering the checksum.
857 * If the latter is the case, at the end, the checksum of the
858 * repaired area is verified in order to correctly maintain
859 * the statistics.
862 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
863 sizeof(*sblocks_for_recheck),
864 GFP_NOFS);
865 if (!sblocks_for_recheck) {
866 spin_lock(&sctx->stat_lock);
867 sctx->stat.malloc_errors++;
868 sctx->stat.read_errors++;
869 sctx->stat.uncorrectable_errors++;
870 spin_unlock(&sctx->stat_lock);
871 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
872 goto out;
875 /* setup the context, map the logical blocks and alloc the pages */
876 ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length,
877 logical, sblocks_for_recheck);
878 if (ret) {
879 spin_lock(&sctx->stat_lock);
880 sctx->stat.read_errors++;
881 sctx->stat.uncorrectable_errors++;
882 spin_unlock(&sctx->stat_lock);
883 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
884 goto out;
886 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
887 sblock_bad = sblocks_for_recheck + failed_mirror_index;
889 /* build and submit the bios for the failed mirror, check checksums */
890 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
891 csum, generation, sctx->csum_size);
893 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
894 sblock_bad->no_io_error_seen) {
896 * the error disappeared after reading page by page, or
897 * the area was part of a huge bio and other parts of the
898 * bio caused I/O errors, or the block layer merged several
899 * read requests into one and the error is caused by a
900 * different bio (usually one of the two latter cases is
901 * the cause)
903 spin_lock(&sctx->stat_lock);
904 sctx->stat.unverified_errors++;
905 spin_unlock(&sctx->stat_lock);
907 if (sctx->is_dev_replace)
908 scrub_write_block_to_dev_replace(sblock_bad);
909 goto out;
912 if (!sblock_bad->no_io_error_seen) {
913 spin_lock(&sctx->stat_lock);
914 sctx->stat.read_errors++;
915 spin_unlock(&sctx->stat_lock);
916 if (__ratelimit(&_rs))
917 scrub_print_warning("i/o error", sblock_to_check);
918 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
919 } else if (sblock_bad->checksum_error) {
920 spin_lock(&sctx->stat_lock);
921 sctx->stat.csum_errors++;
922 spin_unlock(&sctx->stat_lock);
923 if (__ratelimit(&_rs))
924 scrub_print_warning("checksum error", sblock_to_check);
925 btrfs_dev_stat_inc_and_print(dev,
926 BTRFS_DEV_STAT_CORRUPTION_ERRS);
927 } else if (sblock_bad->header_error) {
928 spin_lock(&sctx->stat_lock);
929 sctx->stat.verify_errors++;
930 spin_unlock(&sctx->stat_lock);
931 if (__ratelimit(&_rs))
932 scrub_print_warning("checksum/header error",
933 sblock_to_check);
934 if (sblock_bad->generation_error)
935 btrfs_dev_stat_inc_and_print(dev,
936 BTRFS_DEV_STAT_GENERATION_ERRS);
937 else
938 btrfs_dev_stat_inc_and_print(dev,
939 BTRFS_DEV_STAT_CORRUPTION_ERRS);
942 if (sctx->readonly && !sctx->is_dev_replace)
943 goto did_not_correct_error;
945 if (!is_metadata && !have_csum) {
946 struct scrub_fixup_nodatasum *fixup_nodatasum;
948 nodatasum_case:
949 WARN_ON(sctx->is_dev_replace);
952 * !is_metadata and !have_csum, this means that the data
953 * might not be COW'ed, that it might be modified
954 * concurrently. The general strategy to work on the
955 * commit root does not help in the case when COW is not
956 * used.
958 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
959 if (!fixup_nodatasum)
960 goto did_not_correct_error;
961 fixup_nodatasum->sctx = sctx;
962 fixup_nodatasum->dev = dev;
963 fixup_nodatasum->logical = logical;
964 fixup_nodatasum->root = fs_info->extent_root;
965 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
966 scrub_pending_trans_workers_inc(sctx);
967 fixup_nodatasum->work.func = scrub_fixup_nodatasum;
968 btrfs_queue_worker(&fs_info->scrub_workers,
969 &fixup_nodatasum->work);
970 goto out;
974 * now build and submit the bios for the other mirrors, check
975 * checksums.
976 * First try to pick the mirror which is completely without I/O
977 * errors and also does not have a checksum error.
978 * If one is found, and if a checksum is present, the full block
979 * that is known to contain an error is rewritten. Afterwards
980 * the block is known to be corrected.
981 * If a mirror is found which is completely correct, and no
982 * checksum is present, only those pages are rewritten that had
983 * an I/O error in the block to be repaired, since it cannot be
984 * determined, which copy of the other pages is better (and it
985 * could happen otherwise that a correct page would be
986 * overwritten by a bad one).
988 for (mirror_index = 0;
989 mirror_index < BTRFS_MAX_MIRRORS &&
990 sblocks_for_recheck[mirror_index].page_count > 0;
991 mirror_index++) {
992 struct scrub_block *sblock_other;
994 if (mirror_index == failed_mirror_index)
995 continue;
996 sblock_other = sblocks_for_recheck + mirror_index;
998 /* build and submit the bios, check checksums */
999 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1000 have_csum, csum, generation,
1001 sctx->csum_size);
1003 if (!sblock_other->header_error &&
1004 !sblock_other->checksum_error &&
1005 sblock_other->no_io_error_seen) {
1006 if (sctx->is_dev_replace) {
1007 scrub_write_block_to_dev_replace(sblock_other);
1008 } else {
1009 int force_write = is_metadata || have_csum;
1011 ret = scrub_repair_block_from_good_copy(
1012 sblock_bad, sblock_other,
1013 force_write);
1015 if (0 == ret)
1016 goto corrected_error;
1021 * for dev_replace, pick good pages and write to the target device.
1023 if (sctx->is_dev_replace) {
1024 success = 1;
1025 for (page_num = 0; page_num < sblock_bad->page_count;
1026 page_num++) {
1027 int sub_success;
1029 sub_success = 0;
1030 for (mirror_index = 0;
1031 mirror_index < BTRFS_MAX_MIRRORS &&
1032 sblocks_for_recheck[mirror_index].page_count > 0;
1033 mirror_index++) {
1034 struct scrub_block *sblock_other =
1035 sblocks_for_recheck + mirror_index;
1036 struct scrub_page *page_other =
1037 sblock_other->pagev[page_num];
1039 if (!page_other->io_error) {
1040 ret = scrub_write_page_to_dev_replace(
1041 sblock_other, page_num);
1042 if (ret == 0) {
1043 /* succeeded for this page */
1044 sub_success = 1;
1045 break;
1046 } else {
1047 btrfs_dev_replace_stats_inc(
1048 &sctx->dev_root->
1049 fs_info->dev_replace.
1050 num_write_errors);
1055 if (!sub_success) {
1057 * did not find a mirror to fetch the page
1058 * from. scrub_write_page_to_dev_replace()
1059 * handles this case (page->io_error), by
1060 * filling the block with zeros before
1061 * submitting the write request
1063 success = 0;
1064 ret = scrub_write_page_to_dev_replace(
1065 sblock_bad, page_num);
1066 if (ret)
1067 btrfs_dev_replace_stats_inc(
1068 &sctx->dev_root->fs_info->
1069 dev_replace.num_write_errors);
1073 goto out;
1077 * for regular scrub, repair those pages that are errored.
1078 * In case of I/O errors in the area that is supposed to be
1079 * repaired, continue by picking good copies of those pages.
1080 * Select the good pages from mirrors to rewrite bad pages from
1081 * the area to fix. Afterwards verify the checksum of the block
1082 * that is supposed to be repaired. This verification step is
1083 * only done for the purpose of statistic counting and for the
1084 * final scrub report, whether errors remain.
1085 * A perfect algorithm could make use of the checksum and try
1086 * all possible combinations of pages from the different mirrors
1087 * until the checksum verification succeeds. For example, when
1088 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1089 * of mirror #2 is readable but the final checksum test fails,
1090 * then the 2nd page of mirror #3 could be tried, whether now
1091 * the final checksum succeedes. But this would be a rare
1092 * exception and is therefore not implemented. At least it is
1093 * avoided that the good copy is overwritten.
1094 * A more useful improvement would be to pick the sectors
1095 * without I/O error based on sector sizes (512 bytes on legacy
1096 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1097 * mirror could be repaired by taking 512 byte of a different
1098 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1099 * area are unreadable.
1102 /* can only fix I/O errors from here on */
1103 if (sblock_bad->no_io_error_seen)
1104 goto did_not_correct_error;
1106 success = 1;
1107 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1108 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1110 if (!page_bad->io_error)
1111 continue;
1113 for (mirror_index = 0;
1114 mirror_index < BTRFS_MAX_MIRRORS &&
1115 sblocks_for_recheck[mirror_index].page_count > 0;
1116 mirror_index++) {
1117 struct scrub_block *sblock_other = sblocks_for_recheck +
1118 mirror_index;
1119 struct scrub_page *page_other = sblock_other->pagev[
1120 page_num];
1122 if (!page_other->io_error) {
1123 ret = scrub_repair_page_from_good_copy(
1124 sblock_bad, sblock_other, page_num, 0);
1125 if (0 == ret) {
1126 page_bad->io_error = 0;
1127 break; /* succeeded for this page */
1132 if (page_bad->io_error) {
1133 /* did not find a mirror to copy the page from */
1134 success = 0;
1138 if (success) {
1139 if (is_metadata || have_csum) {
1141 * need to verify the checksum now that all
1142 * sectors on disk are repaired (the write
1143 * request for data to be repaired is on its way).
1144 * Just be lazy and use scrub_recheck_block()
1145 * which re-reads the data before the checksum
1146 * is verified, but most likely the data comes out
1147 * of the page cache.
1149 scrub_recheck_block(fs_info, sblock_bad,
1150 is_metadata, have_csum, csum,
1151 generation, sctx->csum_size);
1152 if (!sblock_bad->header_error &&
1153 !sblock_bad->checksum_error &&
1154 sblock_bad->no_io_error_seen)
1155 goto corrected_error;
1156 else
1157 goto did_not_correct_error;
1158 } else {
1159 corrected_error:
1160 spin_lock(&sctx->stat_lock);
1161 sctx->stat.corrected_errors++;
1162 spin_unlock(&sctx->stat_lock);
1163 printk_ratelimited_in_rcu(KERN_ERR
1164 "btrfs: fixed up error at logical %llu on dev %s\n",
1165 logical, rcu_str_deref(dev->name));
1167 } else {
1168 did_not_correct_error:
1169 spin_lock(&sctx->stat_lock);
1170 sctx->stat.uncorrectable_errors++;
1171 spin_unlock(&sctx->stat_lock);
1172 printk_ratelimited_in_rcu(KERN_ERR
1173 "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
1174 logical, rcu_str_deref(dev->name));
1177 out:
1178 if (sblocks_for_recheck) {
1179 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1180 mirror_index++) {
1181 struct scrub_block *sblock = sblocks_for_recheck +
1182 mirror_index;
1183 int page_index;
1185 for (page_index = 0; page_index < sblock->page_count;
1186 page_index++) {
1187 sblock->pagev[page_index]->sblock = NULL;
1188 scrub_page_put(sblock->pagev[page_index]);
1191 kfree(sblocks_for_recheck);
1194 return 0;
1197 static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
1198 struct btrfs_fs_info *fs_info,
1199 struct scrub_block *original_sblock,
1200 u64 length, u64 logical,
1201 struct scrub_block *sblocks_for_recheck)
1203 int page_index;
1204 int mirror_index;
1205 int ret;
1208 * note: the two members ref_count and outstanding_pages
1209 * are not used (and not set) in the blocks that are used for
1210 * the recheck procedure
1213 page_index = 0;
1214 while (length > 0) {
1215 u64 sublen = min_t(u64, length, PAGE_SIZE);
1216 u64 mapped_length = sublen;
1217 struct btrfs_bio *bbio = NULL;
1220 * with a length of PAGE_SIZE, each returned stripe
1221 * represents one mirror
1223 ret = btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, logical,
1224 &mapped_length, &bbio, 0);
1225 if (ret || !bbio || mapped_length < sublen) {
1226 kfree(bbio);
1227 return -EIO;
1230 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
1231 for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
1232 mirror_index++) {
1233 struct scrub_block *sblock;
1234 struct scrub_page *page;
1236 if (mirror_index >= BTRFS_MAX_MIRRORS)
1237 continue;
1239 sblock = sblocks_for_recheck + mirror_index;
1240 sblock->sctx = sctx;
1241 page = kzalloc(sizeof(*page), GFP_NOFS);
1242 if (!page) {
1243 leave_nomem:
1244 spin_lock(&sctx->stat_lock);
1245 sctx->stat.malloc_errors++;
1246 spin_unlock(&sctx->stat_lock);
1247 kfree(bbio);
1248 return -ENOMEM;
1250 scrub_page_get(page);
1251 sblock->pagev[page_index] = page;
1252 page->logical = logical;
1253 page->physical = bbio->stripes[mirror_index].physical;
1254 BUG_ON(page_index >= original_sblock->page_count);
1255 page->physical_for_dev_replace =
1256 original_sblock->pagev[page_index]->
1257 physical_for_dev_replace;
1258 /* for missing devices, dev->bdev is NULL */
1259 page->dev = bbio->stripes[mirror_index].dev;
1260 page->mirror_num = mirror_index + 1;
1261 sblock->page_count++;
1262 page->page = alloc_page(GFP_NOFS);
1263 if (!page->page)
1264 goto leave_nomem;
1266 kfree(bbio);
1267 length -= sublen;
1268 logical += sublen;
1269 page_index++;
1272 return 0;
1276 * this function will check the on disk data for checksum errors, header
1277 * errors and read I/O errors. If any I/O errors happen, the exact pages
1278 * which are errored are marked as being bad. The goal is to enable scrub
1279 * to take those pages that are not errored from all the mirrors so that
1280 * the pages that are errored in the just handled mirror can be repaired.
1282 static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1283 struct scrub_block *sblock, int is_metadata,
1284 int have_csum, u8 *csum, u64 generation,
1285 u16 csum_size)
1287 int page_num;
1289 sblock->no_io_error_seen = 1;
1290 sblock->header_error = 0;
1291 sblock->checksum_error = 0;
1293 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1294 struct bio *bio;
1295 struct scrub_page *page = sblock->pagev[page_num];
1296 DECLARE_COMPLETION_ONSTACK(complete);
1298 if (page->dev->bdev == NULL) {
1299 page->io_error = 1;
1300 sblock->no_io_error_seen = 0;
1301 continue;
1304 WARN_ON(!page->page);
1305 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
1306 if (!bio) {
1307 page->io_error = 1;
1308 sblock->no_io_error_seen = 0;
1309 continue;
1311 bio->bi_bdev = page->dev->bdev;
1312 bio->bi_sector = page->physical >> 9;
1313 bio->bi_end_io = scrub_complete_bio_end_io;
1314 bio->bi_private = &complete;
1316 bio_add_page(bio, page->page, PAGE_SIZE, 0);
1317 btrfsic_submit_bio(READ, bio);
1319 /* this will also unplug the queue */
1320 wait_for_completion(&complete);
1322 page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
1323 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1324 sblock->no_io_error_seen = 0;
1325 bio_put(bio);
1328 if (sblock->no_io_error_seen)
1329 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1330 have_csum, csum, generation,
1331 csum_size);
1333 return;
1336 static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1337 struct scrub_block *sblock,
1338 int is_metadata, int have_csum,
1339 const u8 *csum, u64 generation,
1340 u16 csum_size)
1342 int page_num;
1343 u8 calculated_csum[BTRFS_CSUM_SIZE];
1344 u32 crc = ~(u32)0;
1345 void *mapped_buffer;
1347 WARN_ON(!sblock->pagev[0]->page);
1348 if (is_metadata) {
1349 struct btrfs_header *h;
1351 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
1352 h = (struct btrfs_header *)mapped_buffer;
1354 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
1355 memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
1356 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1357 BTRFS_UUID_SIZE)) {
1358 sblock->header_error = 1;
1359 } else if (generation != btrfs_stack_header_generation(h)) {
1360 sblock->header_error = 1;
1361 sblock->generation_error = 1;
1363 csum = h->csum;
1364 } else {
1365 if (!have_csum)
1366 return;
1368 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
1371 for (page_num = 0;;) {
1372 if (page_num == 0 && is_metadata)
1373 crc = btrfs_csum_data(
1374 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1375 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1376 else
1377 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
1379 kunmap_atomic(mapped_buffer);
1380 page_num++;
1381 if (page_num >= sblock->page_count)
1382 break;
1383 WARN_ON(!sblock->pagev[page_num]->page);
1385 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
1388 btrfs_csum_final(crc, calculated_csum);
1389 if (memcmp(calculated_csum, csum, csum_size))
1390 sblock->checksum_error = 1;
1393 static void scrub_complete_bio_end_io(struct bio *bio, int err)
1395 complete((struct completion *)bio->bi_private);
1398 static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1399 struct scrub_block *sblock_good,
1400 int force_write)
1402 int page_num;
1403 int ret = 0;
1405 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1406 int ret_sub;
1408 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1409 sblock_good,
1410 page_num,
1411 force_write);
1412 if (ret_sub)
1413 ret = ret_sub;
1416 return ret;
1419 static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1420 struct scrub_block *sblock_good,
1421 int page_num, int force_write)
1423 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1424 struct scrub_page *page_good = sblock_good->pagev[page_num];
1426 BUG_ON(page_bad->page == NULL);
1427 BUG_ON(page_good->page == NULL);
1428 if (force_write || sblock_bad->header_error ||
1429 sblock_bad->checksum_error || page_bad->io_error) {
1430 struct bio *bio;
1431 int ret;
1432 DECLARE_COMPLETION_ONSTACK(complete);
1434 if (!page_bad->dev->bdev) {
1435 printk_ratelimited(KERN_WARNING
1436 "btrfs: scrub_repair_page_from_good_copy(bdev == NULL) is unexpected!\n");
1437 return -EIO;
1440 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
1441 if (!bio)
1442 return -EIO;
1443 bio->bi_bdev = page_bad->dev->bdev;
1444 bio->bi_sector = page_bad->physical >> 9;
1445 bio->bi_end_io = scrub_complete_bio_end_io;
1446 bio->bi_private = &complete;
1448 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1449 if (PAGE_SIZE != ret) {
1450 bio_put(bio);
1451 return -EIO;
1453 btrfsic_submit_bio(WRITE, bio);
1455 /* this will also unplug the queue */
1456 wait_for_completion(&complete);
1457 if (!bio_flagged(bio, BIO_UPTODATE)) {
1458 btrfs_dev_stat_inc_and_print(page_bad->dev,
1459 BTRFS_DEV_STAT_WRITE_ERRS);
1460 btrfs_dev_replace_stats_inc(
1461 &sblock_bad->sctx->dev_root->fs_info->
1462 dev_replace.num_write_errors);
1463 bio_put(bio);
1464 return -EIO;
1466 bio_put(bio);
1469 return 0;
1472 static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1474 int page_num;
1476 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1477 int ret;
1479 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1480 if (ret)
1481 btrfs_dev_replace_stats_inc(
1482 &sblock->sctx->dev_root->fs_info->dev_replace.
1483 num_write_errors);
1487 static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1488 int page_num)
1490 struct scrub_page *spage = sblock->pagev[page_num];
1492 BUG_ON(spage->page == NULL);
1493 if (spage->io_error) {
1494 void *mapped_buffer = kmap_atomic(spage->page);
1496 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1497 flush_dcache_page(spage->page);
1498 kunmap_atomic(mapped_buffer);
1500 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1503 static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1504 struct scrub_page *spage)
1506 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1507 struct scrub_bio *sbio;
1508 int ret;
1510 mutex_lock(&wr_ctx->wr_lock);
1511 again:
1512 if (!wr_ctx->wr_curr_bio) {
1513 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1514 GFP_NOFS);
1515 if (!wr_ctx->wr_curr_bio) {
1516 mutex_unlock(&wr_ctx->wr_lock);
1517 return -ENOMEM;
1519 wr_ctx->wr_curr_bio->sctx = sctx;
1520 wr_ctx->wr_curr_bio->page_count = 0;
1522 sbio = wr_ctx->wr_curr_bio;
1523 if (sbio->page_count == 0) {
1524 struct bio *bio;
1526 sbio->physical = spage->physical_for_dev_replace;
1527 sbio->logical = spage->logical;
1528 sbio->dev = wr_ctx->tgtdev;
1529 bio = sbio->bio;
1530 if (!bio) {
1531 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
1532 if (!bio) {
1533 mutex_unlock(&wr_ctx->wr_lock);
1534 return -ENOMEM;
1536 sbio->bio = bio;
1539 bio->bi_private = sbio;
1540 bio->bi_end_io = scrub_wr_bio_end_io;
1541 bio->bi_bdev = sbio->dev->bdev;
1542 bio->bi_sector = sbio->physical >> 9;
1543 sbio->err = 0;
1544 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1545 spage->physical_for_dev_replace ||
1546 sbio->logical + sbio->page_count * PAGE_SIZE !=
1547 spage->logical) {
1548 scrub_wr_submit(sctx);
1549 goto again;
1552 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1553 if (ret != PAGE_SIZE) {
1554 if (sbio->page_count < 1) {
1555 bio_put(sbio->bio);
1556 sbio->bio = NULL;
1557 mutex_unlock(&wr_ctx->wr_lock);
1558 return -EIO;
1560 scrub_wr_submit(sctx);
1561 goto again;
1564 sbio->pagev[sbio->page_count] = spage;
1565 scrub_page_get(spage);
1566 sbio->page_count++;
1567 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1568 scrub_wr_submit(sctx);
1569 mutex_unlock(&wr_ctx->wr_lock);
1571 return 0;
1574 static void scrub_wr_submit(struct scrub_ctx *sctx)
1576 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1577 struct scrub_bio *sbio;
1579 if (!wr_ctx->wr_curr_bio)
1580 return;
1582 sbio = wr_ctx->wr_curr_bio;
1583 wr_ctx->wr_curr_bio = NULL;
1584 WARN_ON(!sbio->bio->bi_bdev);
1585 scrub_pending_bio_inc(sctx);
1586 /* process all writes in a single worker thread. Then the block layer
1587 * orders the requests before sending them to the driver which
1588 * doubled the write performance on spinning disks when measured
1589 * with Linux 3.5 */
1590 btrfsic_submit_bio(WRITE, sbio->bio);
1593 static void scrub_wr_bio_end_io(struct bio *bio, int err)
1595 struct scrub_bio *sbio = bio->bi_private;
1596 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1598 sbio->err = err;
1599 sbio->bio = bio;
1601 sbio->work.func = scrub_wr_bio_end_io_worker;
1602 btrfs_queue_worker(&fs_info->scrub_wr_completion_workers, &sbio->work);
1605 static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1607 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1608 struct scrub_ctx *sctx = sbio->sctx;
1609 int i;
1611 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1612 if (sbio->err) {
1613 struct btrfs_dev_replace *dev_replace =
1614 &sbio->sctx->dev_root->fs_info->dev_replace;
1616 for (i = 0; i < sbio->page_count; i++) {
1617 struct scrub_page *spage = sbio->pagev[i];
1619 spage->io_error = 1;
1620 btrfs_dev_replace_stats_inc(&dev_replace->
1621 num_write_errors);
1625 for (i = 0; i < sbio->page_count; i++)
1626 scrub_page_put(sbio->pagev[i]);
1628 bio_put(sbio->bio);
1629 kfree(sbio);
1630 scrub_pending_bio_dec(sctx);
1633 static int scrub_checksum(struct scrub_block *sblock)
1635 u64 flags;
1636 int ret;
1638 WARN_ON(sblock->page_count < 1);
1639 flags = sblock->pagev[0]->flags;
1640 ret = 0;
1641 if (flags & BTRFS_EXTENT_FLAG_DATA)
1642 ret = scrub_checksum_data(sblock);
1643 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1644 ret = scrub_checksum_tree_block(sblock);
1645 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1646 (void)scrub_checksum_super(sblock);
1647 else
1648 WARN_ON(1);
1649 if (ret)
1650 scrub_handle_errored_block(sblock);
1652 return ret;
1655 static int scrub_checksum_data(struct scrub_block *sblock)
1657 struct scrub_ctx *sctx = sblock->sctx;
1658 u8 csum[BTRFS_CSUM_SIZE];
1659 u8 *on_disk_csum;
1660 struct page *page;
1661 void *buffer;
1662 u32 crc = ~(u32)0;
1663 int fail = 0;
1664 u64 len;
1665 int index;
1667 BUG_ON(sblock->page_count < 1);
1668 if (!sblock->pagev[0]->have_csum)
1669 return 0;
1671 on_disk_csum = sblock->pagev[0]->csum;
1672 page = sblock->pagev[0]->page;
1673 buffer = kmap_atomic(page);
1675 len = sctx->sectorsize;
1676 index = 0;
1677 for (;;) {
1678 u64 l = min_t(u64, len, PAGE_SIZE);
1680 crc = btrfs_csum_data(buffer, crc, l);
1681 kunmap_atomic(buffer);
1682 len -= l;
1683 if (len == 0)
1684 break;
1685 index++;
1686 BUG_ON(index >= sblock->page_count);
1687 BUG_ON(!sblock->pagev[index]->page);
1688 page = sblock->pagev[index]->page;
1689 buffer = kmap_atomic(page);
1692 btrfs_csum_final(crc, csum);
1693 if (memcmp(csum, on_disk_csum, sctx->csum_size))
1694 fail = 1;
1696 return fail;
1699 static int scrub_checksum_tree_block(struct scrub_block *sblock)
1701 struct scrub_ctx *sctx = sblock->sctx;
1702 struct btrfs_header *h;
1703 struct btrfs_root *root = sctx->dev_root;
1704 struct btrfs_fs_info *fs_info = root->fs_info;
1705 u8 calculated_csum[BTRFS_CSUM_SIZE];
1706 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1707 struct page *page;
1708 void *mapped_buffer;
1709 u64 mapped_size;
1710 void *p;
1711 u32 crc = ~(u32)0;
1712 int fail = 0;
1713 int crc_fail = 0;
1714 u64 len;
1715 int index;
1717 BUG_ON(sblock->page_count < 1);
1718 page = sblock->pagev[0]->page;
1719 mapped_buffer = kmap_atomic(page);
1720 h = (struct btrfs_header *)mapped_buffer;
1721 memcpy(on_disk_csum, h->csum, sctx->csum_size);
1724 * we don't use the getter functions here, as we
1725 * a) don't have an extent buffer and
1726 * b) the page is already kmapped
1729 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
1730 ++fail;
1732 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
1733 ++fail;
1735 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1736 ++fail;
1738 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1739 BTRFS_UUID_SIZE))
1740 ++fail;
1742 WARN_ON(sctx->nodesize != sctx->leafsize);
1743 len = sctx->nodesize - BTRFS_CSUM_SIZE;
1744 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1745 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1746 index = 0;
1747 for (;;) {
1748 u64 l = min_t(u64, len, mapped_size);
1750 crc = btrfs_csum_data(p, crc, l);
1751 kunmap_atomic(mapped_buffer);
1752 len -= l;
1753 if (len == 0)
1754 break;
1755 index++;
1756 BUG_ON(index >= sblock->page_count);
1757 BUG_ON(!sblock->pagev[index]->page);
1758 page = sblock->pagev[index]->page;
1759 mapped_buffer = kmap_atomic(page);
1760 mapped_size = PAGE_SIZE;
1761 p = mapped_buffer;
1764 btrfs_csum_final(crc, calculated_csum);
1765 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
1766 ++crc_fail;
1768 return fail || crc_fail;
1771 static int scrub_checksum_super(struct scrub_block *sblock)
1773 struct btrfs_super_block *s;
1774 struct scrub_ctx *sctx = sblock->sctx;
1775 struct btrfs_root *root = sctx->dev_root;
1776 struct btrfs_fs_info *fs_info = root->fs_info;
1777 u8 calculated_csum[BTRFS_CSUM_SIZE];
1778 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1779 struct page *page;
1780 void *mapped_buffer;
1781 u64 mapped_size;
1782 void *p;
1783 u32 crc = ~(u32)0;
1784 int fail_gen = 0;
1785 int fail_cor = 0;
1786 u64 len;
1787 int index;
1789 BUG_ON(sblock->page_count < 1);
1790 page = sblock->pagev[0]->page;
1791 mapped_buffer = kmap_atomic(page);
1792 s = (struct btrfs_super_block *)mapped_buffer;
1793 memcpy(on_disk_csum, s->csum, sctx->csum_size);
1795 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
1796 ++fail_cor;
1798 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
1799 ++fail_gen;
1801 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1802 ++fail_cor;
1804 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1805 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1806 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1807 index = 0;
1808 for (;;) {
1809 u64 l = min_t(u64, len, mapped_size);
1811 crc = btrfs_csum_data(p, crc, l);
1812 kunmap_atomic(mapped_buffer);
1813 len -= l;
1814 if (len == 0)
1815 break;
1816 index++;
1817 BUG_ON(index >= sblock->page_count);
1818 BUG_ON(!sblock->pagev[index]->page);
1819 page = sblock->pagev[index]->page;
1820 mapped_buffer = kmap_atomic(page);
1821 mapped_size = PAGE_SIZE;
1822 p = mapped_buffer;
1825 btrfs_csum_final(crc, calculated_csum);
1826 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
1827 ++fail_cor;
1829 if (fail_cor + fail_gen) {
1831 * if we find an error in a super block, we just report it.
1832 * They will get written with the next transaction commit
1833 * anyway
1835 spin_lock(&sctx->stat_lock);
1836 ++sctx->stat.super_errors;
1837 spin_unlock(&sctx->stat_lock);
1838 if (fail_cor)
1839 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
1840 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1841 else
1842 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
1843 BTRFS_DEV_STAT_GENERATION_ERRS);
1846 return fail_cor + fail_gen;
1849 static void scrub_block_get(struct scrub_block *sblock)
1851 atomic_inc(&sblock->ref_count);
1854 static void scrub_block_put(struct scrub_block *sblock)
1856 if (atomic_dec_and_test(&sblock->ref_count)) {
1857 int i;
1859 for (i = 0; i < sblock->page_count; i++)
1860 scrub_page_put(sblock->pagev[i]);
1861 kfree(sblock);
1865 static void scrub_page_get(struct scrub_page *spage)
1867 atomic_inc(&spage->ref_count);
1870 static void scrub_page_put(struct scrub_page *spage)
1872 if (atomic_dec_and_test(&spage->ref_count)) {
1873 if (spage->page)
1874 __free_page(spage->page);
1875 kfree(spage);
1879 static void scrub_submit(struct scrub_ctx *sctx)
1881 struct scrub_bio *sbio;
1883 if (sctx->curr == -1)
1884 return;
1886 sbio = sctx->bios[sctx->curr];
1887 sctx->curr = -1;
1888 scrub_pending_bio_inc(sctx);
1890 if (!sbio->bio->bi_bdev) {
1892 * this case should not happen. If btrfs_map_block() is
1893 * wrong, it could happen for dev-replace operations on
1894 * missing devices when no mirrors are available, but in
1895 * this case it should already fail the mount.
1896 * This case is handled correctly (but _very_ slowly).
1898 printk_ratelimited(KERN_WARNING
1899 "btrfs: scrub_submit(bio bdev == NULL) is unexpected!\n");
1900 bio_endio(sbio->bio, -EIO);
1901 } else {
1902 btrfsic_submit_bio(READ, sbio->bio);
1906 static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
1907 struct scrub_page *spage)
1909 struct scrub_block *sblock = spage->sblock;
1910 struct scrub_bio *sbio;
1911 int ret;
1913 again:
1915 * grab a fresh bio or wait for one to become available
1917 while (sctx->curr == -1) {
1918 spin_lock(&sctx->list_lock);
1919 sctx->curr = sctx->first_free;
1920 if (sctx->curr != -1) {
1921 sctx->first_free = sctx->bios[sctx->curr]->next_free;
1922 sctx->bios[sctx->curr]->next_free = -1;
1923 sctx->bios[sctx->curr]->page_count = 0;
1924 spin_unlock(&sctx->list_lock);
1925 } else {
1926 spin_unlock(&sctx->list_lock);
1927 wait_event(sctx->list_wait, sctx->first_free != -1);
1930 sbio = sctx->bios[sctx->curr];
1931 if (sbio->page_count == 0) {
1932 struct bio *bio;
1934 sbio->physical = spage->physical;
1935 sbio->logical = spage->logical;
1936 sbio->dev = spage->dev;
1937 bio = sbio->bio;
1938 if (!bio) {
1939 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
1940 if (!bio)
1941 return -ENOMEM;
1942 sbio->bio = bio;
1945 bio->bi_private = sbio;
1946 bio->bi_end_io = scrub_bio_end_io;
1947 bio->bi_bdev = sbio->dev->bdev;
1948 bio->bi_sector = sbio->physical >> 9;
1949 sbio->err = 0;
1950 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1951 spage->physical ||
1952 sbio->logical + sbio->page_count * PAGE_SIZE !=
1953 spage->logical ||
1954 sbio->dev != spage->dev) {
1955 scrub_submit(sctx);
1956 goto again;
1959 sbio->pagev[sbio->page_count] = spage;
1960 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1961 if (ret != PAGE_SIZE) {
1962 if (sbio->page_count < 1) {
1963 bio_put(sbio->bio);
1964 sbio->bio = NULL;
1965 return -EIO;
1967 scrub_submit(sctx);
1968 goto again;
1971 scrub_block_get(sblock); /* one for the page added to the bio */
1972 atomic_inc(&sblock->outstanding_pages);
1973 sbio->page_count++;
1974 if (sbio->page_count == sctx->pages_per_rd_bio)
1975 scrub_submit(sctx);
1977 return 0;
1980 static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
1981 u64 physical, struct btrfs_device *dev, u64 flags,
1982 u64 gen, int mirror_num, u8 *csum, int force,
1983 u64 physical_for_dev_replace)
1985 struct scrub_block *sblock;
1986 int index;
1988 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
1989 if (!sblock) {
1990 spin_lock(&sctx->stat_lock);
1991 sctx->stat.malloc_errors++;
1992 spin_unlock(&sctx->stat_lock);
1993 return -ENOMEM;
1996 /* one ref inside this function, plus one for each page added to
1997 * a bio later on */
1998 atomic_set(&sblock->ref_count, 1);
1999 sblock->sctx = sctx;
2000 sblock->no_io_error_seen = 1;
2002 for (index = 0; len > 0; index++) {
2003 struct scrub_page *spage;
2004 u64 l = min_t(u64, len, PAGE_SIZE);
2006 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2007 if (!spage) {
2008 leave_nomem:
2009 spin_lock(&sctx->stat_lock);
2010 sctx->stat.malloc_errors++;
2011 spin_unlock(&sctx->stat_lock);
2012 scrub_block_put(sblock);
2013 return -ENOMEM;
2015 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2016 scrub_page_get(spage);
2017 sblock->pagev[index] = spage;
2018 spage->sblock = sblock;
2019 spage->dev = dev;
2020 spage->flags = flags;
2021 spage->generation = gen;
2022 spage->logical = logical;
2023 spage->physical = physical;
2024 spage->physical_for_dev_replace = physical_for_dev_replace;
2025 spage->mirror_num = mirror_num;
2026 if (csum) {
2027 spage->have_csum = 1;
2028 memcpy(spage->csum, csum, sctx->csum_size);
2029 } else {
2030 spage->have_csum = 0;
2032 sblock->page_count++;
2033 spage->page = alloc_page(GFP_NOFS);
2034 if (!spage->page)
2035 goto leave_nomem;
2036 len -= l;
2037 logical += l;
2038 physical += l;
2039 physical_for_dev_replace += l;
2042 WARN_ON(sblock->page_count == 0);
2043 for (index = 0; index < sblock->page_count; index++) {
2044 struct scrub_page *spage = sblock->pagev[index];
2045 int ret;
2047 ret = scrub_add_page_to_rd_bio(sctx, spage);
2048 if (ret) {
2049 scrub_block_put(sblock);
2050 return ret;
2054 if (force)
2055 scrub_submit(sctx);
2057 /* last one frees, either here or in bio completion for last page */
2058 scrub_block_put(sblock);
2059 return 0;
2062 static void scrub_bio_end_io(struct bio *bio, int err)
2064 struct scrub_bio *sbio = bio->bi_private;
2065 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
2067 sbio->err = err;
2068 sbio->bio = bio;
2070 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
2073 static void scrub_bio_end_io_worker(struct btrfs_work *work)
2075 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
2076 struct scrub_ctx *sctx = sbio->sctx;
2077 int i;
2079 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
2080 if (sbio->err) {
2081 for (i = 0; i < sbio->page_count; i++) {
2082 struct scrub_page *spage = sbio->pagev[i];
2084 spage->io_error = 1;
2085 spage->sblock->no_io_error_seen = 0;
2089 /* now complete the scrub_block items that have all pages completed */
2090 for (i = 0; i < sbio->page_count; i++) {
2091 struct scrub_page *spage = sbio->pagev[i];
2092 struct scrub_block *sblock = spage->sblock;
2094 if (atomic_dec_and_test(&sblock->outstanding_pages))
2095 scrub_block_complete(sblock);
2096 scrub_block_put(sblock);
2099 bio_put(sbio->bio);
2100 sbio->bio = NULL;
2101 spin_lock(&sctx->list_lock);
2102 sbio->next_free = sctx->first_free;
2103 sctx->first_free = sbio->index;
2104 spin_unlock(&sctx->list_lock);
2106 if (sctx->is_dev_replace &&
2107 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2108 mutex_lock(&sctx->wr_ctx.wr_lock);
2109 scrub_wr_submit(sctx);
2110 mutex_unlock(&sctx->wr_ctx.wr_lock);
2113 scrub_pending_bio_dec(sctx);
2116 static void scrub_block_complete(struct scrub_block *sblock)
2118 if (!sblock->no_io_error_seen) {
2119 scrub_handle_errored_block(sblock);
2120 } else {
2122 * if has checksum error, write via repair mechanism in
2123 * dev replace case, otherwise write here in dev replace
2124 * case.
2126 if (!scrub_checksum(sblock) && sblock->sctx->is_dev_replace)
2127 scrub_write_block_to_dev_replace(sblock);
2131 static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
2132 u8 *csum)
2134 struct btrfs_ordered_sum *sum = NULL;
2135 unsigned long index;
2136 unsigned long num_sectors;
2138 while (!list_empty(&sctx->csum_list)) {
2139 sum = list_first_entry(&sctx->csum_list,
2140 struct btrfs_ordered_sum, list);
2141 if (sum->bytenr > logical)
2142 return 0;
2143 if (sum->bytenr + sum->len > logical)
2144 break;
2146 ++sctx->stat.csum_discards;
2147 list_del(&sum->list);
2148 kfree(sum);
2149 sum = NULL;
2151 if (!sum)
2152 return 0;
2154 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
2155 num_sectors = sum->len / sctx->sectorsize;
2156 memcpy(csum, sum->sums + index, sctx->csum_size);
2157 if (index == num_sectors - 1) {
2158 list_del(&sum->list);
2159 kfree(sum);
2161 return 1;
2164 /* scrub extent tries to collect up to 64 kB for each bio */
2165 static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
2166 u64 physical, struct btrfs_device *dev, u64 flags,
2167 u64 gen, int mirror_num, u64 physical_for_dev_replace)
2169 int ret;
2170 u8 csum[BTRFS_CSUM_SIZE];
2171 u32 blocksize;
2173 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2174 blocksize = sctx->sectorsize;
2175 spin_lock(&sctx->stat_lock);
2176 sctx->stat.data_extents_scrubbed++;
2177 sctx->stat.data_bytes_scrubbed += len;
2178 spin_unlock(&sctx->stat_lock);
2179 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2180 WARN_ON(sctx->nodesize != sctx->leafsize);
2181 blocksize = sctx->nodesize;
2182 spin_lock(&sctx->stat_lock);
2183 sctx->stat.tree_extents_scrubbed++;
2184 sctx->stat.tree_bytes_scrubbed += len;
2185 spin_unlock(&sctx->stat_lock);
2186 } else {
2187 blocksize = sctx->sectorsize;
2188 WARN_ON(1);
2191 while (len) {
2192 u64 l = min_t(u64, len, blocksize);
2193 int have_csum = 0;
2195 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2196 /* push csums to sbio */
2197 have_csum = scrub_find_csum(sctx, logical, l, csum);
2198 if (have_csum == 0)
2199 ++sctx->stat.no_csum;
2200 if (sctx->is_dev_replace && !have_csum) {
2201 ret = copy_nocow_pages(sctx, logical, l,
2202 mirror_num,
2203 physical_for_dev_replace);
2204 goto behind_scrub_pages;
2207 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
2208 mirror_num, have_csum ? csum : NULL, 0,
2209 physical_for_dev_replace);
2210 behind_scrub_pages:
2211 if (ret)
2212 return ret;
2213 len -= l;
2214 logical += l;
2215 physical += l;
2216 physical_for_dev_replace += l;
2218 return 0;
2221 static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
2222 struct map_lookup *map,
2223 struct btrfs_device *scrub_dev,
2224 int num, u64 base, u64 length,
2225 int is_dev_replace)
2227 struct btrfs_path *path;
2228 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2229 struct btrfs_root *root = fs_info->extent_root;
2230 struct btrfs_root *csum_root = fs_info->csum_root;
2231 struct btrfs_extent_item *extent;
2232 struct blk_plug plug;
2233 u64 flags;
2234 int ret;
2235 int slot;
2236 u64 nstripes;
2237 struct extent_buffer *l;
2238 struct btrfs_key key;
2239 u64 physical;
2240 u64 logical;
2241 u64 logic_end;
2242 u64 generation;
2243 int mirror_num;
2244 struct reada_control *reada1;
2245 struct reada_control *reada2;
2246 struct btrfs_key key_start;
2247 struct btrfs_key key_end;
2248 u64 increment = map->stripe_len;
2249 u64 offset;
2250 u64 extent_logical;
2251 u64 extent_physical;
2252 u64 extent_len;
2253 struct btrfs_device *extent_dev;
2254 int extent_mirror_num;
2255 int stop_loop;
2257 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
2258 BTRFS_BLOCK_GROUP_RAID6)) {
2259 if (num >= nr_data_stripes(map)) {
2260 return 0;
2264 nstripes = length;
2265 offset = 0;
2266 do_div(nstripes, map->stripe_len);
2267 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2268 offset = map->stripe_len * num;
2269 increment = map->stripe_len * map->num_stripes;
2270 mirror_num = 1;
2271 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2272 int factor = map->num_stripes / map->sub_stripes;
2273 offset = map->stripe_len * (num / map->sub_stripes);
2274 increment = map->stripe_len * factor;
2275 mirror_num = num % map->sub_stripes + 1;
2276 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2277 increment = map->stripe_len;
2278 mirror_num = num % map->num_stripes + 1;
2279 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2280 increment = map->stripe_len;
2281 mirror_num = num % map->num_stripes + 1;
2282 } else {
2283 increment = map->stripe_len;
2284 mirror_num = 1;
2287 path = btrfs_alloc_path();
2288 if (!path)
2289 return -ENOMEM;
2292 * work on commit root. The related disk blocks are static as
2293 * long as COW is applied. This means, it is save to rewrite
2294 * them to repair disk errors without any race conditions
2296 path->search_commit_root = 1;
2297 path->skip_locking = 1;
2300 * trigger the readahead for extent tree csum tree and wait for
2301 * completion. During readahead, the scrub is officially paused
2302 * to not hold off transaction commits
2304 logical = base + offset;
2306 wait_event(sctx->list_wait,
2307 atomic_read(&sctx->bios_in_flight) == 0);
2308 atomic_inc(&fs_info->scrubs_paused);
2309 wake_up(&fs_info->scrub_pause_wait);
2311 /* FIXME it might be better to start readahead at commit root */
2312 key_start.objectid = logical;
2313 key_start.type = BTRFS_EXTENT_ITEM_KEY;
2314 key_start.offset = (u64)0;
2315 key_end.objectid = base + offset + nstripes * increment;
2316 key_end.type = BTRFS_METADATA_ITEM_KEY;
2317 key_end.offset = (u64)-1;
2318 reada1 = btrfs_reada_add(root, &key_start, &key_end);
2320 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2321 key_start.type = BTRFS_EXTENT_CSUM_KEY;
2322 key_start.offset = logical;
2323 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2324 key_end.type = BTRFS_EXTENT_CSUM_KEY;
2325 key_end.offset = base + offset + nstripes * increment;
2326 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
2328 if (!IS_ERR(reada1))
2329 btrfs_reada_wait(reada1);
2330 if (!IS_ERR(reada2))
2331 btrfs_reada_wait(reada2);
2333 mutex_lock(&fs_info->scrub_lock);
2334 while (atomic_read(&fs_info->scrub_pause_req)) {
2335 mutex_unlock(&fs_info->scrub_lock);
2336 wait_event(fs_info->scrub_pause_wait,
2337 atomic_read(&fs_info->scrub_pause_req) == 0);
2338 mutex_lock(&fs_info->scrub_lock);
2340 atomic_dec(&fs_info->scrubs_paused);
2341 mutex_unlock(&fs_info->scrub_lock);
2342 wake_up(&fs_info->scrub_pause_wait);
2345 * collect all data csums for the stripe to avoid seeking during
2346 * the scrub. This might currently (crc32) end up to be about 1MB
2348 blk_start_plug(&plug);
2351 * now find all extents for each stripe and scrub them
2353 logical = base + offset;
2354 physical = map->stripes[num].physical;
2355 logic_end = logical + increment * nstripes;
2356 ret = 0;
2357 while (logical < logic_end) {
2359 * canceled?
2361 if (atomic_read(&fs_info->scrub_cancel_req) ||
2362 atomic_read(&sctx->cancel_req)) {
2363 ret = -ECANCELED;
2364 goto out;
2367 * check to see if we have to pause
2369 if (atomic_read(&fs_info->scrub_pause_req)) {
2370 /* push queued extents */
2371 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
2372 scrub_submit(sctx);
2373 mutex_lock(&sctx->wr_ctx.wr_lock);
2374 scrub_wr_submit(sctx);
2375 mutex_unlock(&sctx->wr_ctx.wr_lock);
2376 wait_event(sctx->list_wait,
2377 atomic_read(&sctx->bios_in_flight) == 0);
2378 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
2379 atomic_inc(&fs_info->scrubs_paused);
2380 wake_up(&fs_info->scrub_pause_wait);
2381 mutex_lock(&fs_info->scrub_lock);
2382 while (atomic_read(&fs_info->scrub_pause_req)) {
2383 mutex_unlock(&fs_info->scrub_lock);
2384 wait_event(fs_info->scrub_pause_wait,
2385 atomic_read(&fs_info->scrub_pause_req) == 0);
2386 mutex_lock(&fs_info->scrub_lock);
2388 atomic_dec(&fs_info->scrubs_paused);
2389 mutex_unlock(&fs_info->scrub_lock);
2390 wake_up(&fs_info->scrub_pause_wait);
2393 key.objectid = logical;
2394 key.type = BTRFS_EXTENT_ITEM_KEY;
2395 key.offset = (u64)-1;
2397 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2398 if (ret < 0)
2399 goto out;
2401 if (ret > 0) {
2402 ret = btrfs_previous_item(root, path, 0,
2403 BTRFS_EXTENT_ITEM_KEY);
2404 if (ret < 0)
2405 goto out;
2406 if (ret > 0) {
2407 /* there's no smaller item, so stick with the
2408 * larger one */
2409 btrfs_release_path(path);
2410 ret = btrfs_search_slot(NULL, root, &key,
2411 path, 0, 0);
2412 if (ret < 0)
2413 goto out;
2417 stop_loop = 0;
2418 while (1) {
2419 u64 bytes;
2421 l = path->nodes[0];
2422 slot = path->slots[0];
2423 if (slot >= btrfs_header_nritems(l)) {
2424 ret = btrfs_next_leaf(root, path);
2425 if (ret == 0)
2426 continue;
2427 if (ret < 0)
2428 goto out;
2430 stop_loop = 1;
2431 break;
2433 btrfs_item_key_to_cpu(l, &key, slot);
2435 if (key.type == BTRFS_METADATA_ITEM_KEY)
2436 bytes = root->leafsize;
2437 else
2438 bytes = key.offset;
2440 if (key.objectid + bytes <= logical)
2441 goto next;
2443 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2444 key.type != BTRFS_METADATA_ITEM_KEY)
2445 goto next;
2447 if (key.objectid >= logical + map->stripe_len) {
2448 /* out of this device extent */
2449 if (key.objectid >= logic_end)
2450 stop_loop = 1;
2451 break;
2454 extent = btrfs_item_ptr(l, slot,
2455 struct btrfs_extent_item);
2456 flags = btrfs_extent_flags(l, extent);
2457 generation = btrfs_extent_generation(l, extent);
2459 if (key.objectid < logical &&
2460 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2461 printk(KERN_ERR
2462 "btrfs scrub: tree block %llu spanning "
2463 "stripes, ignored. logical=%llu\n",
2464 key.objectid, logical);
2465 goto next;
2468 again:
2469 extent_logical = key.objectid;
2470 extent_len = bytes;
2473 * trim extent to this stripe
2475 if (extent_logical < logical) {
2476 extent_len -= logical - extent_logical;
2477 extent_logical = logical;
2479 if (extent_logical + extent_len >
2480 logical + map->stripe_len) {
2481 extent_len = logical + map->stripe_len -
2482 extent_logical;
2485 extent_physical = extent_logical - logical + physical;
2486 extent_dev = scrub_dev;
2487 extent_mirror_num = mirror_num;
2488 if (is_dev_replace)
2489 scrub_remap_extent(fs_info, extent_logical,
2490 extent_len, &extent_physical,
2491 &extent_dev,
2492 &extent_mirror_num);
2494 ret = btrfs_lookup_csums_range(csum_root, logical,
2495 logical + map->stripe_len - 1,
2496 &sctx->csum_list, 1);
2497 if (ret)
2498 goto out;
2500 ret = scrub_extent(sctx, extent_logical, extent_len,
2501 extent_physical, extent_dev, flags,
2502 generation, extent_mirror_num,
2503 extent_logical - logical + physical);
2504 if (ret)
2505 goto out;
2507 scrub_free_csums(sctx);
2508 if (extent_logical + extent_len <
2509 key.objectid + bytes) {
2510 logical += increment;
2511 physical += map->stripe_len;
2513 if (logical < key.objectid + bytes) {
2514 cond_resched();
2515 goto again;
2518 if (logical >= logic_end) {
2519 stop_loop = 1;
2520 break;
2523 next:
2524 path->slots[0]++;
2526 btrfs_release_path(path);
2527 logical += increment;
2528 physical += map->stripe_len;
2529 spin_lock(&sctx->stat_lock);
2530 if (stop_loop)
2531 sctx->stat.last_physical = map->stripes[num].physical +
2532 length;
2533 else
2534 sctx->stat.last_physical = physical;
2535 spin_unlock(&sctx->stat_lock);
2536 if (stop_loop)
2537 break;
2539 out:
2540 /* push queued extents */
2541 scrub_submit(sctx);
2542 mutex_lock(&sctx->wr_ctx.wr_lock);
2543 scrub_wr_submit(sctx);
2544 mutex_unlock(&sctx->wr_ctx.wr_lock);
2546 blk_finish_plug(&plug);
2547 btrfs_free_path(path);
2548 return ret < 0 ? ret : 0;
2551 static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
2552 struct btrfs_device *scrub_dev,
2553 u64 chunk_tree, u64 chunk_objectid,
2554 u64 chunk_offset, u64 length,
2555 u64 dev_offset, int is_dev_replace)
2557 struct btrfs_mapping_tree *map_tree =
2558 &sctx->dev_root->fs_info->mapping_tree;
2559 struct map_lookup *map;
2560 struct extent_map *em;
2561 int i;
2562 int ret = 0;
2564 read_lock(&map_tree->map_tree.lock);
2565 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2566 read_unlock(&map_tree->map_tree.lock);
2568 if (!em)
2569 return -EINVAL;
2571 map = (struct map_lookup *)em->bdev;
2572 if (em->start != chunk_offset)
2573 goto out;
2575 if (em->len < length)
2576 goto out;
2578 for (i = 0; i < map->num_stripes; ++i) {
2579 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
2580 map->stripes[i].physical == dev_offset) {
2581 ret = scrub_stripe(sctx, map, scrub_dev, i,
2582 chunk_offset, length,
2583 is_dev_replace);
2584 if (ret)
2585 goto out;
2588 out:
2589 free_extent_map(em);
2591 return ret;
2594 static noinline_for_stack
2595 int scrub_enumerate_chunks(struct scrub_ctx *sctx,
2596 struct btrfs_device *scrub_dev, u64 start, u64 end,
2597 int is_dev_replace)
2599 struct btrfs_dev_extent *dev_extent = NULL;
2600 struct btrfs_path *path;
2601 struct btrfs_root *root = sctx->dev_root;
2602 struct btrfs_fs_info *fs_info = root->fs_info;
2603 u64 length;
2604 u64 chunk_tree;
2605 u64 chunk_objectid;
2606 u64 chunk_offset;
2607 int ret;
2608 int slot;
2609 struct extent_buffer *l;
2610 struct btrfs_key key;
2611 struct btrfs_key found_key;
2612 struct btrfs_block_group_cache *cache;
2613 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
2615 path = btrfs_alloc_path();
2616 if (!path)
2617 return -ENOMEM;
2619 path->reada = 2;
2620 path->search_commit_root = 1;
2621 path->skip_locking = 1;
2623 key.objectid = scrub_dev->devid;
2624 key.offset = 0ull;
2625 key.type = BTRFS_DEV_EXTENT_KEY;
2627 while (1) {
2628 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2629 if (ret < 0)
2630 break;
2631 if (ret > 0) {
2632 if (path->slots[0] >=
2633 btrfs_header_nritems(path->nodes[0])) {
2634 ret = btrfs_next_leaf(root, path);
2635 if (ret)
2636 break;
2640 l = path->nodes[0];
2641 slot = path->slots[0];
2643 btrfs_item_key_to_cpu(l, &found_key, slot);
2645 if (found_key.objectid != scrub_dev->devid)
2646 break;
2648 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
2649 break;
2651 if (found_key.offset >= end)
2652 break;
2654 if (found_key.offset < key.offset)
2655 break;
2657 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2658 length = btrfs_dev_extent_length(l, dev_extent);
2660 if (found_key.offset + length <= start) {
2661 key.offset = found_key.offset + length;
2662 btrfs_release_path(path);
2663 continue;
2666 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2667 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2668 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2671 * get a reference on the corresponding block group to prevent
2672 * the chunk from going away while we scrub it
2674 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2675 if (!cache) {
2676 ret = -ENOENT;
2677 break;
2679 dev_replace->cursor_right = found_key.offset + length;
2680 dev_replace->cursor_left = found_key.offset;
2681 dev_replace->item_needs_writeback = 1;
2682 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
2683 chunk_offset, length, found_key.offset,
2684 is_dev_replace);
2687 * flush, submit all pending read and write bios, afterwards
2688 * wait for them.
2689 * Note that in the dev replace case, a read request causes
2690 * write requests that are submitted in the read completion
2691 * worker. Therefore in the current situation, it is required
2692 * that all write requests are flushed, so that all read and
2693 * write requests are really completed when bios_in_flight
2694 * changes to 0.
2696 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
2697 scrub_submit(sctx);
2698 mutex_lock(&sctx->wr_ctx.wr_lock);
2699 scrub_wr_submit(sctx);
2700 mutex_unlock(&sctx->wr_ctx.wr_lock);
2702 wait_event(sctx->list_wait,
2703 atomic_read(&sctx->bios_in_flight) == 0);
2704 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
2705 atomic_inc(&fs_info->scrubs_paused);
2706 wake_up(&fs_info->scrub_pause_wait);
2707 wait_event(sctx->list_wait,
2708 atomic_read(&sctx->workers_pending) == 0);
2710 mutex_lock(&fs_info->scrub_lock);
2711 while (atomic_read(&fs_info->scrub_pause_req)) {
2712 mutex_unlock(&fs_info->scrub_lock);
2713 wait_event(fs_info->scrub_pause_wait,
2714 atomic_read(&fs_info->scrub_pause_req) == 0);
2715 mutex_lock(&fs_info->scrub_lock);
2717 atomic_dec(&fs_info->scrubs_paused);
2718 mutex_unlock(&fs_info->scrub_lock);
2719 wake_up(&fs_info->scrub_pause_wait);
2721 dev_replace->cursor_left = dev_replace->cursor_right;
2722 dev_replace->item_needs_writeback = 1;
2723 btrfs_put_block_group(cache);
2724 if (ret)
2725 break;
2726 if (is_dev_replace &&
2727 atomic64_read(&dev_replace->num_write_errors) > 0) {
2728 ret = -EIO;
2729 break;
2731 if (sctx->stat.malloc_errors > 0) {
2732 ret = -ENOMEM;
2733 break;
2736 key.offset = found_key.offset + length;
2737 btrfs_release_path(path);
2740 btrfs_free_path(path);
2743 * ret can still be 1 from search_slot or next_leaf,
2744 * that's not an error
2746 return ret < 0 ? ret : 0;
2749 static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2750 struct btrfs_device *scrub_dev)
2752 int i;
2753 u64 bytenr;
2754 u64 gen;
2755 int ret;
2756 struct btrfs_root *root = sctx->dev_root;
2758 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
2759 return -EIO;
2761 gen = root->fs_info->last_trans_committed;
2763 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2764 bytenr = btrfs_sb_offset(i);
2765 if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
2766 break;
2768 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
2769 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
2770 NULL, 1, bytenr);
2771 if (ret)
2772 return ret;
2774 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
2776 return 0;
2780 * get a reference count on fs_info->scrub_workers. start worker if necessary
2782 static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
2783 int is_dev_replace)
2785 int ret = 0;
2787 mutex_lock(&fs_info->scrub_lock);
2788 if (fs_info->scrub_workers_refcnt == 0) {
2789 if (is_dev_replace)
2790 btrfs_init_workers(&fs_info->scrub_workers, "scrub", 1,
2791 &fs_info->generic_worker);
2792 else
2793 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
2794 fs_info->thread_pool_size,
2795 &fs_info->generic_worker);
2796 fs_info->scrub_workers.idle_thresh = 4;
2797 ret = btrfs_start_workers(&fs_info->scrub_workers);
2798 if (ret)
2799 goto out;
2800 btrfs_init_workers(&fs_info->scrub_wr_completion_workers,
2801 "scrubwrc",
2802 fs_info->thread_pool_size,
2803 &fs_info->generic_worker);
2804 fs_info->scrub_wr_completion_workers.idle_thresh = 2;
2805 ret = btrfs_start_workers(
2806 &fs_info->scrub_wr_completion_workers);
2807 if (ret)
2808 goto out;
2809 btrfs_init_workers(&fs_info->scrub_nocow_workers, "scrubnc", 1,
2810 &fs_info->generic_worker);
2811 ret = btrfs_start_workers(&fs_info->scrub_nocow_workers);
2812 if (ret)
2813 goto out;
2815 ++fs_info->scrub_workers_refcnt;
2816 out:
2817 mutex_unlock(&fs_info->scrub_lock);
2819 return ret;
2822 static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
2824 mutex_lock(&fs_info->scrub_lock);
2825 if (--fs_info->scrub_workers_refcnt == 0) {
2826 btrfs_stop_workers(&fs_info->scrub_workers);
2827 btrfs_stop_workers(&fs_info->scrub_wr_completion_workers);
2828 btrfs_stop_workers(&fs_info->scrub_nocow_workers);
2830 WARN_ON(fs_info->scrub_workers_refcnt < 0);
2831 mutex_unlock(&fs_info->scrub_lock);
2834 int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
2835 u64 end, struct btrfs_scrub_progress *progress,
2836 int readonly, int is_dev_replace)
2838 struct scrub_ctx *sctx;
2839 int ret;
2840 struct btrfs_device *dev;
2842 if (btrfs_fs_closing(fs_info))
2843 return -EINVAL;
2846 * check some assumptions
2848 if (fs_info->chunk_root->nodesize != fs_info->chunk_root->leafsize) {
2849 printk(KERN_ERR
2850 "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
2851 fs_info->chunk_root->nodesize,
2852 fs_info->chunk_root->leafsize);
2853 return -EINVAL;
2856 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
2858 * in this case scrub is unable to calculate the checksum
2859 * the way scrub is implemented. Do not handle this
2860 * situation at all because it won't ever happen.
2862 printk(KERN_ERR
2863 "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
2864 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
2865 return -EINVAL;
2868 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
2869 /* not supported for data w/o checksums */
2870 printk(KERN_ERR
2871 "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails\n",
2872 fs_info->chunk_root->sectorsize, PAGE_SIZE);
2873 return -EINVAL;
2876 if (fs_info->chunk_root->nodesize >
2877 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
2878 fs_info->chunk_root->sectorsize >
2879 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
2881 * would exhaust the array bounds of pagev member in
2882 * struct scrub_block
2884 pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
2885 fs_info->chunk_root->nodesize,
2886 SCRUB_MAX_PAGES_PER_BLOCK,
2887 fs_info->chunk_root->sectorsize,
2888 SCRUB_MAX_PAGES_PER_BLOCK);
2889 return -EINVAL;
2892 ret = scrub_workers_get(fs_info, is_dev_replace);
2893 if (ret)
2894 return ret;
2896 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2897 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
2898 if (!dev || (dev->missing && !is_dev_replace)) {
2899 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2900 scrub_workers_put(fs_info);
2901 return -ENODEV;
2903 mutex_lock(&fs_info->scrub_lock);
2905 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
2906 mutex_unlock(&fs_info->scrub_lock);
2907 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2908 scrub_workers_put(fs_info);
2909 return -EIO;
2912 btrfs_dev_replace_lock(&fs_info->dev_replace);
2913 if (dev->scrub_device ||
2914 (!is_dev_replace &&
2915 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
2916 btrfs_dev_replace_unlock(&fs_info->dev_replace);
2917 mutex_unlock(&fs_info->scrub_lock);
2918 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2919 scrub_workers_put(fs_info);
2920 return -EINPROGRESS;
2922 btrfs_dev_replace_unlock(&fs_info->dev_replace);
2923 sctx = scrub_setup_ctx(dev, is_dev_replace);
2924 if (IS_ERR(sctx)) {
2925 mutex_unlock(&fs_info->scrub_lock);
2926 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2927 scrub_workers_put(fs_info);
2928 return PTR_ERR(sctx);
2930 sctx->readonly = readonly;
2931 dev->scrub_device = sctx;
2933 atomic_inc(&fs_info->scrubs_running);
2934 mutex_unlock(&fs_info->scrub_lock);
2935 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2937 if (!is_dev_replace) {
2938 down_read(&fs_info->scrub_super_lock);
2939 ret = scrub_supers(sctx, dev);
2940 up_read(&fs_info->scrub_super_lock);
2943 if (!ret)
2944 ret = scrub_enumerate_chunks(sctx, dev, start, end,
2945 is_dev_replace);
2947 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
2948 atomic_dec(&fs_info->scrubs_running);
2949 wake_up(&fs_info->scrub_pause_wait);
2951 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
2953 if (progress)
2954 memcpy(progress, &sctx->stat, sizeof(*progress));
2956 mutex_lock(&fs_info->scrub_lock);
2957 dev->scrub_device = NULL;
2958 mutex_unlock(&fs_info->scrub_lock);
2960 scrub_free_ctx(sctx);
2961 scrub_workers_put(fs_info);
2963 return ret;
2966 void btrfs_scrub_pause(struct btrfs_root *root)
2968 struct btrfs_fs_info *fs_info = root->fs_info;
2970 mutex_lock(&fs_info->scrub_lock);
2971 atomic_inc(&fs_info->scrub_pause_req);
2972 while (atomic_read(&fs_info->scrubs_paused) !=
2973 atomic_read(&fs_info->scrubs_running)) {
2974 mutex_unlock(&fs_info->scrub_lock);
2975 wait_event(fs_info->scrub_pause_wait,
2976 atomic_read(&fs_info->scrubs_paused) ==
2977 atomic_read(&fs_info->scrubs_running));
2978 mutex_lock(&fs_info->scrub_lock);
2980 mutex_unlock(&fs_info->scrub_lock);
2983 void btrfs_scrub_continue(struct btrfs_root *root)
2985 struct btrfs_fs_info *fs_info = root->fs_info;
2987 atomic_dec(&fs_info->scrub_pause_req);
2988 wake_up(&fs_info->scrub_pause_wait);
2991 void btrfs_scrub_pause_super(struct btrfs_root *root)
2993 down_write(&root->fs_info->scrub_super_lock);
2996 void btrfs_scrub_continue_super(struct btrfs_root *root)
2998 up_write(&root->fs_info->scrub_super_lock);
3001 int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
3003 mutex_lock(&fs_info->scrub_lock);
3004 if (!atomic_read(&fs_info->scrubs_running)) {
3005 mutex_unlock(&fs_info->scrub_lock);
3006 return -ENOTCONN;
3009 atomic_inc(&fs_info->scrub_cancel_req);
3010 while (atomic_read(&fs_info->scrubs_running)) {
3011 mutex_unlock(&fs_info->scrub_lock);
3012 wait_event(fs_info->scrub_pause_wait,
3013 atomic_read(&fs_info->scrubs_running) == 0);
3014 mutex_lock(&fs_info->scrub_lock);
3016 atomic_dec(&fs_info->scrub_cancel_req);
3017 mutex_unlock(&fs_info->scrub_lock);
3019 return 0;
3022 int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3023 struct btrfs_device *dev)
3025 struct scrub_ctx *sctx;
3027 mutex_lock(&fs_info->scrub_lock);
3028 sctx = dev->scrub_device;
3029 if (!sctx) {
3030 mutex_unlock(&fs_info->scrub_lock);
3031 return -ENOTCONN;
3033 atomic_inc(&sctx->cancel_req);
3034 while (dev->scrub_device) {
3035 mutex_unlock(&fs_info->scrub_lock);
3036 wait_event(fs_info->scrub_pause_wait,
3037 dev->scrub_device == NULL);
3038 mutex_lock(&fs_info->scrub_lock);
3040 mutex_unlock(&fs_info->scrub_lock);
3042 return 0;
3045 int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3046 struct btrfs_scrub_progress *progress)
3048 struct btrfs_device *dev;
3049 struct scrub_ctx *sctx = NULL;
3051 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
3052 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
3053 if (dev)
3054 sctx = dev->scrub_device;
3055 if (sctx)
3056 memcpy(progress, &sctx->stat, sizeof(*progress));
3057 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3059 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
3062 static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3063 u64 extent_logical, u64 extent_len,
3064 u64 *extent_physical,
3065 struct btrfs_device **extent_dev,
3066 int *extent_mirror_num)
3068 u64 mapped_length;
3069 struct btrfs_bio *bbio = NULL;
3070 int ret;
3072 mapped_length = extent_len;
3073 ret = btrfs_map_block(fs_info, READ, extent_logical,
3074 &mapped_length, &bbio, 0);
3075 if (ret || !bbio || mapped_length < extent_len ||
3076 !bbio->stripes[0].dev->bdev) {
3077 kfree(bbio);
3078 return;
3081 *extent_physical = bbio->stripes[0].physical;
3082 *extent_mirror_num = bbio->mirror_num;
3083 *extent_dev = bbio->stripes[0].dev;
3084 kfree(bbio);
3087 static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3088 struct scrub_wr_ctx *wr_ctx,
3089 struct btrfs_fs_info *fs_info,
3090 struct btrfs_device *dev,
3091 int is_dev_replace)
3093 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3095 mutex_init(&wr_ctx->wr_lock);
3096 wr_ctx->wr_curr_bio = NULL;
3097 if (!is_dev_replace)
3098 return 0;
3100 WARN_ON(!dev->bdev);
3101 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3102 bio_get_nr_vecs(dev->bdev));
3103 wr_ctx->tgtdev = dev;
3104 atomic_set(&wr_ctx->flush_all_writes, 0);
3105 return 0;
3108 static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3110 mutex_lock(&wr_ctx->wr_lock);
3111 kfree(wr_ctx->wr_curr_bio);
3112 wr_ctx->wr_curr_bio = NULL;
3113 mutex_unlock(&wr_ctx->wr_lock);
3116 static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3117 int mirror_num, u64 physical_for_dev_replace)
3119 struct scrub_copy_nocow_ctx *nocow_ctx;
3120 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3122 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3123 if (!nocow_ctx) {
3124 spin_lock(&sctx->stat_lock);
3125 sctx->stat.malloc_errors++;
3126 spin_unlock(&sctx->stat_lock);
3127 return -ENOMEM;
3130 scrub_pending_trans_workers_inc(sctx);
3132 nocow_ctx->sctx = sctx;
3133 nocow_ctx->logical = logical;
3134 nocow_ctx->len = len;
3135 nocow_ctx->mirror_num = mirror_num;
3136 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
3137 nocow_ctx->work.func = copy_nocow_pages_worker;
3138 INIT_LIST_HEAD(&nocow_ctx->inodes);
3139 btrfs_queue_worker(&fs_info->scrub_nocow_workers,
3140 &nocow_ctx->work);
3142 return 0;
3145 static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3147 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3148 struct scrub_nocow_inode *nocow_inode;
3150 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3151 if (!nocow_inode)
3152 return -ENOMEM;
3153 nocow_inode->inum = inum;
3154 nocow_inode->offset = offset;
3155 nocow_inode->root = root;
3156 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3157 return 0;
3160 #define COPY_COMPLETE 1
3162 static void copy_nocow_pages_worker(struct btrfs_work *work)
3164 struct scrub_copy_nocow_ctx *nocow_ctx =
3165 container_of(work, struct scrub_copy_nocow_ctx, work);
3166 struct scrub_ctx *sctx = nocow_ctx->sctx;
3167 u64 logical = nocow_ctx->logical;
3168 u64 len = nocow_ctx->len;
3169 int mirror_num = nocow_ctx->mirror_num;
3170 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3171 int ret;
3172 struct btrfs_trans_handle *trans = NULL;
3173 struct btrfs_fs_info *fs_info;
3174 struct btrfs_path *path;
3175 struct btrfs_root *root;
3176 int not_written = 0;
3178 fs_info = sctx->dev_root->fs_info;
3179 root = fs_info->extent_root;
3181 path = btrfs_alloc_path();
3182 if (!path) {
3183 spin_lock(&sctx->stat_lock);
3184 sctx->stat.malloc_errors++;
3185 spin_unlock(&sctx->stat_lock);
3186 not_written = 1;
3187 goto out;
3190 trans = btrfs_join_transaction(root);
3191 if (IS_ERR(trans)) {
3192 not_written = 1;
3193 goto out;
3196 ret = iterate_inodes_from_logical(logical, fs_info, path,
3197 record_inode_for_nocow, nocow_ctx);
3198 if (ret != 0 && ret != -ENOENT) {
3199 pr_warn("iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d\n",
3200 logical, physical_for_dev_replace, len, mirror_num,
3201 ret);
3202 not_written = 1;
3203 goto out;
3206 btrfs_end_transaction(trans, root);
3207 trans = NULL;
3208 while (!list_empty(&nocow_ctx->inodes)) {
3209 struct scrub_nocow_inode *entry;
3210 entry = list_first_entry(&nocow_ctx->inodes,
3211 struct scrub_nocow_inode,
3212 list);
3213 list_del_init(&entry->list);
3214 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
3215 entry->root, nocow_ctx);
3216 kfree(entry);
3217 if (ret == COPY_COMPLETE) {
3218 ret = 0;
3219 break;
3220 } else if (ret) {
3221 break;
3224 out:
3225 while (!list_empty(&nocow_ctx->inodes)) {
3226 struct scrub_nocow_inode *entry;
3227 entry = list_first_entry(&nocow_ctx->inodes,
3228 struct scrub_nocow_inode,
3229 list);
3230 list_del_init(&entry->list);
3231 kfree(entry);
3233 if (trans && !IS_ERR(trans))
3234 btrfs_end_transaction(trans, root);
3235 if (not_written)
3236 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
3237 num_uncorrectable_read_errors);
3239 btrfs_free_path(path);
3240 kfree(nocow_ctx);
3242 scrub_pending_trans_workers_dec(sctx);
3245 static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
3246 struct scrub_copy_nocow_ctx *nocow_ctx)
3248 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
3249 struct btrfs_key key;
3250 struct inode *inode;
3251 struct page *page;
3252 struct btrfs_root *local_root;
3253 struct btrfs_ordered_extent *ordered;
3254 struct extent_map *em;
3255 struct extent_state *cached_state = NULL;
3256 struct extent_io_tree *io_tree;
3257 u64 physical_for_dev_replace;
3258 u64 len = nocow_ctx->len;
3259 u64 lockstart = offset, lockend = offset + len - 1;
3260 unsigned long index;
3261 int srcu_index;
3262 int ret = 0;
3263 int err = 0;
3265 key.objectid = root;
3266 key.type = BTRFS_ROOT_ITEM_KEY;
3267 key.offset = (u64)-1;
3269 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
3271 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
3272 if (IS_ERR(local_root)) {
3273 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
3274 return PTR_ERR(local_root);
3277 key.type = BTRFS_INODE_ITEM_KEY;
3278 key.objectid = inum;
3279 key.offset = 0;
3280 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
3281 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
3282 if (IS_ERR(inode))
3283 return PTR_ERR(inode);
3285 /* Avoid truncate/dio/punch hole.. */
3286 mutex_lock(&inode->i_mutex);
3287 inode_dio_wait(inode);
3289 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3290 io_tree = &BTRFS_I(inode)->io_tree;
3292 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
3293 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
3294 if (ordered) {
3295 btrfs_put_ordered_extent(ordered);
3296 goto out_unlock;
3299 em = btrfs_get_extent(inode, NULL, 0, lockstart, len, 0);
3300 if (IS_ERR(em)) {
3301 ret = PTR_ERR(em);
3302 goto out_unlock;
3306 * This extent does not actually cover the logical extent anymore,
3307 * move on to the next inode.
3309 if (em->block_start > nocow_ctx->logical ||
3310 em->block_start + em->block_len < nocow_ctx->logical + len) {
3311 free_extent_map(em);
3312 goto out_unlock;
3314 free_extent_map(em);
3316 while (len >= PAGE_CACHE_SIZE) {
3317 index = offset >> PAGE_CACHE_SHIFT;
3318 again:
3319 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3320 if (!page) {
3321 pr_err("find_or_create_page() failed\n");
3322 ret = -ENOMEM;
3323 goto out;
3326 if (PageUptodate(page)) {
3327 if (PageDirty(page))
3328 goto next_page;
3329 } else {
3330 ClearPageError(page);
3331 err = extent_read_full_page_nolock(io_tree, page,
3332 btrfs_get_extent,
3333 nocow_ctx->mirror_num);
3334 if (err) {
3335 ret = err;
3336 goto next_page;
3339 lock_page(page);
3341 * If the page has been remove from the page cache,
3342 * the data on it is meaningless, because it may be
3343 * old one, the new data may be written into the new
3344 * page in the page cache.
3346 if (page->mapping != inode->i_mapping) {
3347 unlock_page(page);
3348 page_cache_release(page);
3349 goto again;
3351 if (!PageUptodate(page)) {
3352 ret = -EIO;
3353 goto next_page;
3356 err = write_page_nocow(nocow_ctx->sctx,
3357 physical_for_dev_replace, page);
3358 if (err)
3359 ret = err;
3360 next_page:
3361 unlock_page(page);
3362 page_cache_release(page);
3364 if (ret)
3365 break;
3367 offset += PAGE_CACHE_SIZE;
3368 physical_for_dev_replace += PAGE_CACHE_SIZE;
3369 len -= PAGE_CACHE_SIZE;
3371 ret = COPY_COMPLETE;
3372 out_unlock:
3373 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
3374 GFP_NOFS);
3375 out:
3376 mutex_unlock(&inode->i_mutex);
3377 iput(inode);
3378 return ret;
3381 static int write_page_nocow(struct scrub_ctx *sctx,
3382 u64 physical_for_dev_replace, struct page *page)
3384 struct bio *bio;
3385 struct btrfs_device *dev;
3386 int ret;
3387 DECLARE_COMPLETION_ONSTACK(compl);
3389 dev = sctx->wr_ctx.tgtdev;
3390 if (!dev)
3391 return -EIO;
3392 if (!dev->bdev) {
3393 printk_ratelimited(KERN_WARNING
3394 "btrfs: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
3395 return -EIO;
3397 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
3398 if (!bio) {
3399 spin_lock(&sctx->stat_lock);
3400 sctx->stat.malloc_errors++;
3401 spin_unlock(&sctx->stat_lock);
3402 return -ENOMEM;
3404 bio->bi_private = &compl;
3405 bio->bi_end_io = scrub_complete_bio_end_io;
3406 bio->bi_size = 0;
3407 bio->bi_sector = physical_for_dev_replace >> 9;
3408 bio->bi_bdev = dev->bdev;
3409 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
3410 if (ret != PAGE_CACHE_SIZE) {
3411 leave_with_eio:
3412 bio_put(bio);
3413 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
3414 return -EIO;
3416 btrfsic_submit_bio(WRITE_SYNC, bio);
3417 wait_for_completion(&compl);
3419 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
3420 goto leave_with_eio;
3422 bio_put(bio);
3423 return 0;