blob: 7d38f40732432607399a94d871a9539a144ab1ad [file] [log] [blame]
Arne Jansena2de7332011-03-08 14:14:00 +01001/*
2 * Copyright (C) 2011 STRATO. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
Arne Jansena2de7332011-03-08 14:14:00 +010019#include <linux/blkdev.h>
Jan Schmidt558540c2011-06-13 19:59:12 +020020#include <linux/ratelimit.h>
Arne Jansena2de7332011-03-08 14:14:00 +010021#include "ctree.h"
22#include "volumes.h"
23#include "disk-io.h"
24#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020025#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020026#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020027#include "extent_io.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010028#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040029#include "rcu-string.h"
Arne Jansena2de7332011-03-08 14:14:00 +010030
31/*
32 * This is only the first step towards a full-features scrub. It reads all
33 * extent and super block and verifies the checksums. In case a bad checksum
34 * is found or the extent cannot be read, good data will be written back if
35 * any can be found.
36 *
37 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010038 * - In case an unrepairable extent is encountered, track which files are
39 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010040 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010041 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010042 */
43
Stefan Behrensb5d67f62012-03-27 14:21:27 -040044struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010045struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010046
47#define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +010048#define SCRUB_BIOS_PER_CTX 16 /* 1 MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010049
50/*
51 * the following value times PAGE_SIZE needs to be large enough to match the
52 * largest node/leaf/sector size that shall be supported.
53 * Values larger than BTRFS_STRIPE_LEN are not supported.
54 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040055#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010056
57struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040058 struct scrub_block *sblock;
59 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020060 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010061 u64 flags; /* extent flags */
62 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040063 u64 logical;
64 u64 physical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +010065 atomic_t ref_count;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040066 struct {
67 unsigned int mirror_num:8;
68 unsigned int have_csum:1;
69 unsigned int io_error:1;
70 };
Arne Jansena2de7332011-03-08 14:14:00 +010071 u8 csum[BTRFS_CSUM_SIZE];
72};
73
74struct scrub_bio {
75 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010076 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010077 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010078 struct bio *bio;
79 int err;
80 u64 logical;
81 u64 physical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040082 struct scrub_page *pagev[SCRUB_PAGES_PER_BIO];
83 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +010084 int next_free;
85 struct btrfs_work work;
86};
87
Stefan Behrensb5d67f62012-03-27 14:21:27 -040088struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +010089 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -040090 int page_count;
91 atomic_t outstanding_pages;
92 atomic_t ref_count; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +010093 struct scrub_ctx *sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040094 struct {
95 unsigned int header_error:1;
96 unsigned int checksum_error:1;
97 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +020098 unsigned int generation_error:1; /* also sets header_error */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040099 };
100};
101
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100102struct scrub_ctx {
103 struct scrub_bio *bios[SCRUB_BIOS_PER_CTX];
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100104 struct btrfs_root *dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +0100105 int first_free;
106 int curr;
107 atomic_t in_flight;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200108 atomic_t fixup_cnt;
Arne Jansena2de7332011-03-08 14:14:00 +0100109 spinlock_t list_lock;
110 wait_queue_head_t list_wait;
111 u16 csum_size;
112 struct list_head csum_list;
113 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100114 int readonly;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400115 int pages_per_bio; /* <= SCRUB_PAGES_PER_BIO */
116 u32 sectorsize;
117 u32 nodesize;
118 u32 leafsize;
Arne Jansena2de7332011-03-08 14:14:00 +0100119 /*
120 * statistics
121 */
122 struct btrfs_scrub_progress stat;
123 spinlock_t stat_lock;
124};
125
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200126struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100127 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100128 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200129 u64 logical;
130 struct btrfs_root *root;
131 struct btrfs_work work;
132 int mirror_num;
133};
134
Jan Schmidt558540c2011-06-13 19:59:12 +0200135struct scrub_warning {
136 struct btrfs_path *path;
137 u64 extent_item_size;
138 char *scratch_buf;
139 char *msg_buf;
140 const char *errstr;
141 sector_t sector;
142 u64 logical;
143 struct btrfs_device *dev;
144 int msg_bufsize;
145 int scratch_bufsize;
146};
147
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400148
149static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100150static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400151 struct btrfs_mapping_tree *map_tree,
152 u64 length, u64 logical,
153 struct scrub_block *sblock);
154static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
155 struct scrub_block *sblock, int is_metadata,
156 int have_csum, u8 *csum, u64 generation,
157 u16 csum_size);
158static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
159 struct scrub_block *sblock,
160 int is_metadata, int have_csum,
161 const u8 *csum, u64 generation,
162 u16 csum_size);
163static void scrub_complete_bio_end_io(struct bio *bio, int err);
164static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
165 struct scrub_block *sblock_good,
166 int force_write);
167static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
168 struct scrub_block *sblock_good,
169 int page_num, int force_write);
170static int scrub_checksum_data(struct scrub_block *sblock);
171static int scrub_checksum_tree_block(struct scrub_block *sblock);
172static int scrub_checksum_super(struct scrub_block *sblock);
173static void scrub_block_get(struct scrub_block *sblock);
174static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100175static void scrub_page_get(struct scrub_page *spage);
176static void scrub_page_put(struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100177static int scrub_add_page_to_bio(struct scrub_ctx *sctx,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400178 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100179static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100180 u64 physical, struct btrfs_device *dev, u64 flags,
181 u64 gen, int mirror_num, u8 *csum, int force);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400182static void scrub_bio_end_io(struct bio *bio, int err);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400183static void scrub_bio_end_io_worker(struct btrfs_work *work);
184static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400185
186
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100187static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100188{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100189 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100190 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100191 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100192 struct btrfs_ordered_sum, list);
193 list_del(&sum->list);
194 kfree(sum);
195 }
196}
197
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100198static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100199{
200 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100201
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100202 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100203 return;
204
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400205 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100206 if (sctx->curr != -1) {
207 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400208
209 for (i = 0; i < sbio->page_count; i++) {
210 BUG_ON(!sbio->pagev[i]);
211 BUG_ON(!sbio->pagev[i]->page);
212 scrub_block_put(sbio->pagev[i]->sblock);
213 }
214 bio_put(sbio->bio);
215 }
216
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100217 for (i = 0; i < SCRUB_BIOS_PER_CTX; ++i) {
218 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100219
220 if (!sbio)
221 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100222 kfree(sbio);
223 }
224
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100225 scrub_free_csums(sctx);
226 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100227}
228
229static noinline_for_stack
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100230struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev)
Arne Jansena2de7332011-03-08 14:14:00 +0100231{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100232 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100233 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100234 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400235 int pages_per_bio;
Arne Jansena2de7332011-03-08 14:14:00 +0100236
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400237 pages_per_bio = min_t(int, SCRUB_PAGES_PER_BIO,
238 bio_get_nr_vecs(dev->bdev));
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100239 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
240 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100241 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100242 sctx->pages_per_bio = pages_per_bio;
243 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100244 sctx->dev_root = dev->dev_root;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100245 for (i = 0; i < SCRUB_BIOS_PER_CTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100246 struct scrub_bio *sbio;
247
248 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
249 if (!sbio)
250 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100251 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100252
Arne Jansena2de7332011-03-08 14:14:00 +0100253 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100254 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400255 sbio->page_count = 0;
256 sbio->work.func = scrub_bio_end_io_worker;
Arne Jansena2de7332011-03-08 14:14:00 +0100257
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100258 if (i != SCRUB_BIOS_PER_CTX - 1)
259 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200260 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100261 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100262 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100263 sctx->first_free = 0;
264 sctx->nodesize = dev->dev_root->nodesize;
265 sctx->leafsize = dev->dev_root->leafsize;
266 sctx->sectorsize = dev->dev_root->sectorsize;
267 atomic_set(&sctx->in_flight, 0);
268 atomic_set(&sctx->fixup_cnt, 0);
269 atomic_set(&sctx->cancel_req, 0);
270 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
271 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100272
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100273 spin_lock_init(&sctx->list_lock);
274 spin_lock_init(&sctx->stat_lock);
275 init_waitqueue_head(&sctx->list_wait);
276 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100277
278nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100279 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100280 return ERR_PTR(-ENOMEM);
281}
282
Jan Schmidt558540c2011-06-13 19:59:12 +0200283static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, void *ctx)
284{
285 u64 isize;
286 u32 nlink;
287 int ret;
288 int i;
289 struct extent_buffer *eb;
290 struct btrfs_inode_item *inode_item;
291 struct scrub_warning *swarn = ctx;
292 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
293 struct inode_fs_paths *ipath = NULL;
294 struct btrfs_root *local_root;
295 struct btrfs_key root_key;
296
297 root_key.objectid = root;
298 root_key.type = BTRFS_ROOT_ITEM_KEY;
299 root_key.offset = (u64)-1;
300 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
301 if (IS_ERR(local_root)) {
302 ret = PTR_ERR(local_root);
303 goto err;
304 }
305
306 ret = inode_item_info(inum, 0, local_root, swarn->path);
307 if (ret) {
308 btrfs_release_path(swarn->path);
309 goto err;
310 }
311
312 eb = swarn->path->nodes[0];
313 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
314 struct btrfs_inode_item);
315 isize = btrfs_inode_size(eb, inode_item);
316 nlink = btrfs_inode_nlink(eb, inode_item);
317 btrfs_release_path(swarn->path);
318
319 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300320 if (IS_ERR(ipath)) {
321 ret = PTR_ERR(ipath);
322 ipath = NULL;
323 goto err;
324 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200325 ret = paths_from_inode(inum, ipath);
326
327 if (ret < 0)
328 goto err;
329
330 /*
331 * we deliberately ignore the bit ipath might have been too small to
332 * hold all of the paths here
333 */
334 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Josef Bacik606686e2012-06-04 14:03:51 -0400335 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200336 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
337 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400338 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200339 (unsigned long long)swarn->sector, root, inum, offset,
340 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500341 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200342
343 free_ipath(ipath);
344 return 0;
345
346err:
Josef Bacik606686e2012-06-04 14:03:51 -0400347 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200348 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
349 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400350 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200351 (unsigned long long)swarn->sector, root, inum, offset, ret);
352
353 free_ipath(ipath);
354 return 0;
355}
356
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400357static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200358{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100359 struct btrfs_device *dev;
360 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200361 struct btrfs_path *path;
362 struct btrfs_key found_key;
363 struct extent_buffer *eb;
364 struct btrfs_extent_item *ei;
365 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200366 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100367 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600368 u64 flags = 0;
369 u64 ref_root;
370 u32 item_size;
371 u8 ref_level;
372 const int bufsize = 4096;
373 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200374
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100375 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100376 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100377 fs_info = sblock->sctx->dev_root->fs_info;
378
Jan Schmidt558540c2011-06-13 19:59:12 +0200379 path = btrfs_alloc_path();
380
381 swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
382 swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100383 swarn.sector = (sblock->pagev[0]->physical) >> 9;
384 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200385 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100386 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200387 swarn.msg_bufsize = bufsize;
388 swarn.scratch_bufsize = bufsize;
389
390 if (!path || !swarn.scratch_buf || !swarn.msg_buf)
391 goto out;
392
Liu Bo69917e42012-09-07 20:01:28 -0600393 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
394 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200395 if (ret < 0)
396 goto out;
397
Jan Schmidt4692cf52011-12-02 14:56:41 +0100398 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200399 swarn.extent_item_size = found_key.offset;
400
401 eb = path->nodes[0];
402 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
403 item_size = btrfs_item_size_nr(eb, path->slots[0]);
Jan Schmidt4692cf52011-12-02 14:56:41 +0100404 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200405
Liu Bo69917e42012-09-07 20:01:28 -0600406 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200407 do {
408 ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
409 &ref_root, &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400410 printk_in_rcu(KERN_WARNING
Stefan Behrens1623ede2012-03-27 14:21:26 -0400411 "btrfs: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200412 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400413 "%llu\n", errstr, swarn.logical,
414 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200415 (unsigned long long)swarn.sector,
416 ref_level ? "node" : "leaf",
417 ret < 0 ? -1 : ref_level,
418 ret < 0 ? -1 : ref_root);
419 } while (ret != 1);
420 } else {
421 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100422 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100423 iterate_extent_inodes(fs_info, found_key.objectid,
424 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200425 scrub_print_warning_inode, &swarn);
426 }
427
428out:
429 btrfs_free_path(path);
430 kfree(swarn.scratch_buf);
431 kfree(swarn.msg_buf);
432}
433
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200434static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *ctx)
435{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200436 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200437 unsigned long index;
438 struct scrub_fixup_nodatasum *fixup = ctx;
439 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200440 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200441 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200442 struct inode *inode = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200443 u64 end = offset + PAGE_SIZE - 1;
444 struct btrfs_root *local_root;
445
446 key.objectid = root;
447 key.type = BTRFS_ROOT_ITEM_KEY;
448 key.offset = (u64)-1;
449 local_root = btrfs_read_fs_root_no_name(fixup->root->fs_info, &key);
450 if (IS_ERR(local_root))
451 return PTR_ERR(local_root);
452
453 key.type = BTRFS_INODE_ITEM_KEY;
454 key.objectid = inum;
455 key.offset = 0;
456 inode = btrfs_iget(fixup->root->fs_info->sb, &key, local_root, NULL);
457 if (IS_ERR(inode))
458 return PTR_ERR(inode);
459
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200460 index = offset >> PAGE_CACHE_SHIFT;
461
462 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200463 if (!page) {
464 ret = -ENOMEM;
465 goto out;
466 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200467
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200468 if (PageUptodate(page)) {
469 struct btrfs_mapping_tree *map_tree;
470 if (PageDirty(page)) {
471 /*
472 * we need to write the data to the defect sector. the
473 * data that was in that sector is not in memory,
474 * because the page was modified. we must not write the
475 * modified page to that sector.
476 *
477 * TODO: what could be done here: wait for the delalloc
478 * runner to write out that page (might involve
479 * COW) and see whether the sector is still
480 * referenced afterwards.
481 *
482 * For the meantime, we'll treat this error
483 * incorrectable, although there is a chance that a
484 * later scrub will find the bad sector again and that
485 * there's no dirty page in memory, then.
486 */
487 ret = -EIO;
488 goto out;
489 }
490 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
491 ret = repair_io_failure(map_tree, offset, PAGE_SIZE,
492 fixup->logical, page,
493 fixup->mirror_num);
494 unlock_page(page);
495 corrected = !ret;
496 } else {
497 /*
498 * we need to get good data first. the general readpage path
499 * will call repair_io_failure for us, we just have to make
500 * sure we read the bad mirror.
501 */
502 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
503 EXTENT_DAMAGED, GFP_NOFS);
504 if (ret) {
505 /* set_extent_bits should give proper error */
506 WARN_ON(ret > 0);
507 if (ret > 0)
508 ret = -EFAULT;
509 goto out;
510 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200511
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200512 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
513 btrfs_get_extent,
514 fixup->mirror_num);
515 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200516
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200517 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
518 end, EXTENT_DAMAGED, 0, NULL);
519 if (!corrected)
520 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
521 EXTENT_DAMAGED, GFP_NOFS);
522 }
523
524out:
525 if (page)
526 put_page(page);
527 if (inode)
528 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200529
530 if (ret < 0)
531 return ret;
532
533 if (ret == 0 && corrected) {
534 /*
535 * we only need to call readpage for one of the inodes belonging
536 * to this extent. so make iterate_extent_inodes stop
537 */
538 return 1;
539 }
540
541 return -EIO;
542}
543
544static void scrub_fixup_nodatasum(struct btrfs_work *work)
545{
546 int ret;
547 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100548 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200549 struct btrfs_trans_handle *trans = NULL;
550 struct btrfs_fs_info *fs_info;
551 struct btrfs_path *path;
552 int uncorrectable = 0;
553
554 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100555 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200556 fs_info = fixup->root->fs_info;
557
558 path = btrfs_alloc_path();
559 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100560 spin_lock(&sctx->stat_lock);
561 ++sctx->stat.malloc_errors;
562 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200563 uncorrectable = 1;
564 goto out;
565 }
566
567 trans = btrfs_join_transaction(fixup->root);
568 if (IS_ERR(trans)) {
569 uncorrectable = 1;
570 goto out;
571 }
572
573 /*
574 * the idea is to trigger a regular read through the standard path. we
575 * read a page from the (failed) logical address by specifying the
576 * corresponding copynum of the failed sector. thus, that readpage is
577 * expected to fail.
578 * that is the point where on-the-fly error correction will kick in
579 * (once it's finished) and rewrite the failed sector if a good copy
580 * can be found.
581 */
582 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
583 path, scrub_fixup_readpage,
584 fixup);
585 if (ret < 0) {
586 uncorrectable = 1;
587 goto out;
588 }
589 WARN_ON(ret != 1);
590
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100591 spin_lock(&sctx->stat_lock);
592 ++sctx->stat.corrected_errors;
593 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200594
595out:
596 if (trans && !IS_ERR(trans))
597 btrfs_end_transaction(trans, fixup->root);
598 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100599 spin_lock(&sctx->stat_lock);
600 ++sctx->stat.uncorrectable_errors;
601 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400602
603 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400604 "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -0400605 (unsigned long long)fixup->logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100606 rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200607 }
608
609 btrfs_free_path(path);
610 kfree(fixup);
611
612 /* see caller why we're pretending to be paused in the scrub counters */
613 mutex_lock(&fs_info->scrub_lock);
614 atomic_dec(&fs_info->scrubs_running);
615 atomic_dec(&fs_info->scrubs_paused);
616 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100617 atomic_dec(&sctx->fixup_cnt);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200618 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100619 wake_up(&sctx->list_wait);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200620}
621
Arne Jansena2de7332011-03-08 14:14:00 +0100622/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400623 * scrub_handle_errored_block gets called when either verification of the
624 * pages failed or the bio failed to read, e.g. with EIO. In the latter
625 * case, this function handles all pages in the bio, even though only one
626 * may be bad.
627 * The goal of this function is to repair the errored block by using the
628 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100629 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400630static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100631{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100632 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100633 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400634 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100635 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400636 u64 logical;
637 u64 generation;
638 unsigned int failed_mirror_index;
639 unsigned int is_metadata;
640 unsigned int have_csum;
641 u8 *csum;
642 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
643 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100644 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400645 int mirror_index;
646 int page_num;
647 int success;
648 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
649 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100650
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400651 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100652 fs_info = sctx->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400653 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100654 logical = sblock_to_check->pagev[0]->logical;
655 generation = sblock_to_check->pagev[0]->generation;
656 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
657 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
658 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400659 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100660 have_csum = sblock_to_check->pagev[0]->have_csum;
661 csum = sblock_to_check->pagev[0]->csum;
662 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400663
664 /*
665 * read all mirrors one after the other. This includes to
666 * re-read the extent or metadata block that failed (that was
667 * the cause that this fixup code is called) another time,
668 * page by page this time in order to know which pages
669 * caused I/O errors and which ones are good (for all mirrors).
670 * It is the goal to handle the situation when more than one
671 * mirror contains I/O errors, but the errors do not
672 * overlap, i.e. the data can be repaired by selecting the
673 * pages from those mirrors without I/O error on the
674 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
675 * would be that mirror #1 has an I/O error on the first page,
676 * the second page is good, and mirror #2 has an I/O error on
677 * the second page, but the first page is good.
678 * Then the first page of the first mirror can be repaired by
679 * taking the first page of the second mirror, and the
680 * second page of the second mirror can be repaired by
681 * copying the contents of the 2nd page of the 1st mirror.
682 * One more note: if the pages of one mirror contain I/O
683 * errors, the checksum cannot be verified. In order to get
684 * the best data for repairing, the first attempt is to find
685 * a mirror without I/O errors and with a validated checksum.
686 * Only if this is not possible, the pages are picked from
687 * mirrors with I/O errors without considering the checksum.
688 * If the latter is the case, at the end, the checksum of the
689 * repaired area is verified in order to correctly maintain
690 * the statistics.
691 */
692
693 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
694 sizeof(*sblocks_for_recheck),
695 GFP_NOFS);
696 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100697 spin_lock(&sctx->stat_lock);
698 sctx->stat.malloc_errors++;
699 sctx->stat.read_errors++;
700 sctx->stat.uncorrectable_errors++;
701 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100702 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400703 goto out;
704 }
705
706 /* setup the context, map the logical blocks and alloc the pages */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100707 ret = scrub_setup_recheck_block(sctx, &fs_info->mapping_tree, length,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400708 logical, sblocks_for_recheck);
709 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100710 spin_lock(&sctx->stat_lock);
711 sctx->stat.read_errors++;
712 sctx->stat.uncorrectable_errors++;
713 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100714 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400715 goto out;
716 }
717 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
718 sblock_bad = sblocks_for_recheck + failed_mirror_index;
719
720 /* build and submit the bios for the failed mirror, check checksums */
721 ret = scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100722 csum, generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400723 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100724 spin_lock(&sctx->stat_lock);
725 sctx->stat.read_errors++;
726 sctx->stat.uncorrectable_errors++;
727 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100728 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400729 goto out;
730 }
731
732 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
733 sblock_bad->no_io_error_seen) {
734 /*
735 * the error disappeared after reading page by page, or
736 * the area was part of a huge bio and other parts of the
737 * bio caused I/O errors, or the block layer merged several
738 * read requests into one and the error is caused by a
739 * different bio (usually one of the two latter cases is
740 * the cause)
741 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100742 spin_lock(&sctx->stat_lock);
743 sctx->stat.unverified_errors++;
744 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400745
746 goto out;
747 }
748
749 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100750 spin_lock(&sctx->stat_lock);
751 sctx->stat.read_errors++;
752 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400753 if (__ratelimit(&_rs))
754 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100755 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400756 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100757 spin_lock(&sctx->stat_lock);
758 sctx->stat.csum_errors++;
759 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400760 if (__ratelimit(&_rs))
761 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100762 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200763 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400764 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100765 spin_lock(&sctx->stat_lock);
766 sctx->stat.verify_errors++;
767 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400768 if (__ratelimit(&_rs))
769 scrub_print_warning("checksum/header error",
770 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200771 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100772 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200773 BTRFS_DEV_STAT_GENERATION_ERRS);
774 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100775 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200776 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400777 }
778
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100779 if (sctx->readonly)
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400780 goto did_not_correct_error;
781
782 if (!is_metadata && !have_csum) {
783 struct scrub_fixup_nodatasum *fixup_nodatasum;
784
785 /*
786 * !is_metadata and !have_csum, this means that the data
787 * might not be COW'ed, that it might be modified
788 * concurrently. The general strategy to work on the
789 * commit root does not help in the case when COW is not
790 * used.
791 */
792 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
793 if (!fixup_nodatasum)
794 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100795 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100796 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400797 fixup_nodatasum->logical = logical;
798 fixup_nodatasum->root = fs_info->extent_root;
799 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Arne Jansena2de7332011-03-08 14:14:00 +0100800 /*
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200801 * increment scrubs_running to prevent cancel requests from
802 * completing as long as a fixup worker is running. we must also
803 * increment scrubs_paused to prevent deadlocking on pause
804 * requests used for transactions commits (as the worker uses a
805 * transaction context). it is safe to regard the fixup worker
806 * as paused for all matters practical. effectively, we only
807 * avoid cancellation requests from completing.
Arne Jansena2de7332011-03-08 14:14:00 +0100808 */
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200809 mutex_lock(&fs_info->scrub_lock);
810 atomic_inc(&fs_info->scrubs_running);
811 atomic_inc(&fs_info->scrubs_paused);
812 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100813 atomic_inc(&sctx->fixup_cnt);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400814 fixup_nodatasum->work.func = scrub_fixup_nodatasum;
815 btrfs_queue_worker(&fs_info->scrub_workers,
816 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +0100817 goto out;
818 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400819
820 /*
821 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100822 * checksums.
823 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400824 * errors and also does not have a checksum error.
825 * If one is found, and if a checksum is present, the full block
826 * that is known to contain an error is rewritten. Afterwards
827 * the block is known to be corrected.
828 * If a mirror is found which is completely correct, and no
829 * checksum is present, only those pages are rewritten that had
830 * an I/O error in the block to be repaired, since it cannot be
831 * determined, which copy of the other pages is better (and it
832 * could happen otherwise that a correct page would be
833 * overwritten by a bad one).
834 */
835 for (mirror_index = 0;
836 mirror_index < BTRFS_MAX_MIRRORS &&
837 sblocks_for_recheck[mirror_index].page_count > 0;
838 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100839 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400840
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100841 if (mirror_index == failed_mirror_index)
842 continue;
843 sblock_other = sblocks_for_recheck + mirror_index;
844
845 /* build and submit the bios, check checksums */
846 ret = scrub_recheck_block(fs_info, sblock_other, is_metadata,
847 have_csum, csum, generation,
848 sctx->csum_size);
849 if (!ret && !sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400850 !sblock_other->checksum_error &&
851 sblock_other->no_io_error_seen) {
852 int force_write = is_metadata || have_csum;
853
854 ret = scrub_repair_block_from_good_copy(sblock_bad,
855 sblock_other,
856 force_write);
857 if (0 == ret)
858 goto corrected_error;
Arne Jansena2de7332011-03-08 14:14:00 +0100859 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400860 }
861
862 /*
863 * in case of I/O errors in the area that is supposed to be
864 * repaired, continue by picking good copies of those pages.
865 * Select the good pages from mirrors to rewrite bad pages from
866 * the area to fix. Afterwards verify the checksum of the block
867 * that is supposed to be repaired. This verification step is
868 * only done for the purpose of statistic counting and for the
869 * final scrub report, whether errors remain.
870 * A perfect algorithm could make use of the checksum and try
871 * all possible combinations of pages from the different mirrors
872 * until the checksum verification succeeds. For example, when
873 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
874 * of mirror #2 is readable but the final checksum test fails,
875 * then the 2nd page of mirror #3 could be tried, whether now
876 * the final checksum succeedes. But this would be a rare
877 * exception and is therefore not implemented. At least it is
878 * avoided that the good copy is overwritten.
879 * A more useful improvement would be to pick the sectors
880 * without I/O error based on sector sizes (512 bytes on legacy
881 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
882 * mirror could be repaired by taking 512 byte of a different
883 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
884 * area are unreadable.
885 */
886
887 /* can only fix I/O errors from here on */
888 if (sblock_bad->no_io_error_seen)
889 goto did_not_correct_error;
890
891 success = 1;
892 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100893 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400894
895 if (!page_bad->io_error)
896 continue;
897
898 for (mirror_index = 0;
899 mirror_index < BTRFS_MAX_MIRRORS &&
900 sblocks_for_recheck[mirror_index].page_count > 0;
901 mirror_index++) {
902 struct scrub_block *sblock_other = sblocks_for_recheck +
903 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100904 struct scrub_page *page_other = sblock_other->pagev[
905 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400906
907 if (!page_other->io_error) {
908 ret = scrub_repair_page_from_good_copy(
909 sblock_bad, sblock_other, page_num, 0);
910 if (0 == ret) {
911 page_bad->io_error = 0;
912 break; /* succeeded for this page */
913 }
Jan Schmidt13db62b2011-06-13 19:56:13 +0200914 }
915 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400916
917 if (page_bad->io_error) {
918 /* did not find a mirror to copy the page from */
919 success = 0;
920 }
921 }
922
923 if (success) {
924 if (is_metadata || have_csum) {
925 /*
926 * need to verify the checksum now that all
927 * sectors on disk are repaired (the write
928 * request for data to be repaired is on its way).
929 * Just be lazy and use scrub_recheck_block()
930 * which re-reads the data before the checksum
931 * is verified, but most likely the data comes out
932 * of the page cache.
933 */
934 ret = scrub_recheck_block(fs_info, sblock_bad,
935 is_metadata, have_csum, csum,
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100936 generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400937 if (!ret && !sblock_bad->header_error &&
938 !sblock_bad->checksum_error &&
939 sblock_bad->no_io_error_seen)
940 goto corrected_error;
941 else
942 goto did_not_correct_error;
943 } else {
944corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100945 spin_lock(&sctx->stat_lock);
946 sctx->stat.corrected_errors++;
947 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400948 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400949 "btrfs: fixed up error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -0400950 (unsigned long long)logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100951 rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400952 }
953 } else {
954did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100955 spin_lock(&sctx->stat_lock);
956 sctx->stat.uncorrectable_errors++;
957 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400958 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400959 "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -0400960 (unsigned long long)logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100961 rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +0100962 }
963
964out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400965 if (sblocks_for_recheck) {
966 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
967 mirror_index++) {
968 struct scrub_block *sblock = sblocks_for_recheck +
969 mirror_index;
970 int page_index;
971
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100972 for (page_index = 0; page_index < sblock->page_count;
973 page_index++) {
974 sblock->pagev[page_index]->sblock = NULL;
975 scrub_page_put(sblock->pagev[page_index]);
976 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400977 }
978 kfree(sblocks_for_recheck);
979 }
980
981 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +0100982}
983
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100984static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400985 struct btrfs_mapping_tree *map_tree,
986 u64 length, u64 logical,
987 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +0100988{
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400989 int page_index;
990 int mirror_index;
991 int ret;
992
993 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100994 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400995 * are not used (and not set) in the blocks that are used for
996 * the recheck procedure
997 */
998
999 page_index = 0;
1000 while (length > 0) {
1001 u64 sublen = min_t(u64, length, PAGE_SIZE);
1002 u64 mapped_length = sublen;
1003 struct btrfs_bio *bbio = NULL;
1004
1005 /*
1006 * with a length of PAGE_SIZE, each returned stripe
1007 * represents one mirror
1008 */
1009 ret = btrfs_map_block(map_tree, WRITE, logical, &mapped_length,
1010 &bbio, 0);
1011 if (ret || !bbio || mapped_length < sublen) {
1012 kfree(bbio);
1013 return -EIO;
1014 }
1015
1016 BUG_ON(page_index >= SCRUB_PAGES_PER_BIO);
1017 for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
1018 mirror_index++) {
1019 struct scrub_block *sblock;
1020 struct scrub_page *page;
1021
1022 if (mirror_index >= BTRFS_MAX_MIRRORS)
1023 continue;
1024
1025 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001026 sblock->sctx = sctx;
1027 page = kzalloc(sizeof(*page), GFP_NOFS);
1028 if (!page) {
1029leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001030 spin_lock(&sctx->stat_lock);
1031 sctx->stat.malloc_errors++;
1032 spin_unlock(&sctx->stat_lock);
Wei Yongjuncf93dcc2012-09-02 07:44:51 -06001033 kfree(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001034 return -ENOMEM;
1035 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001036 scrub_page_get(page);
1037 sblock->pagev[page_index] = page;
1038 page->logical = logical;
1039 page->physical = bbio->stripes[mirror_index].physical;
1040 /* for missing devices, dev->bdev is NULL */
1041 page->dev = bbio->stripes[mirror_index].dev;
1042 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001043 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001044 page->page = alloc_page(GFP_NOFS);
1045 if (!page->page)
1046 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001047 }
1048 kfree(bbio);
1049 length -= sublen;
1050 logical += sublen;
1051 page_index++;
1052 }
1053
1054 return 0;
1055}
1056
1057/*
1058 * this function will check the on disk data for checksum errors, header
1059 * errors and read I/O errors. If any I/O errors happen, the exact pages
1060 * which are errored are marked as being bad. The goal is to enable scrub
1061 * to take those pages that are not errored from all the mirrors so that
1062 * the pages that are errored in the just handled mirror can be repaired.
1063 */
1064static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
1065 struct scrub_block *sblock, int is_metadata,
1066 int have_csum, u8 *csum, u64 generation,
1067 u16 csum_size)
1068{
1069 int page_num;
1070
1071 sblock->no_io_error_seen = 1;
1072 sblock->header_error = 0;
1073 sblock->checksum_error = 0;
1074
1075 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1076 struct bio *bio;
1077 int ret;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001078 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001079 DECLARE_COMPLETION_ONSTACK(complete);
1080
Stefan Behrens442a4f62012-05-25 16:06:08 +02001081 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001082 page->io_error = 1;
1083 sblock->no_io_error_seen = 0;
1084 continue;
1085 }
1086
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001087 WARN_ON(!page->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001088 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001089 if (!bio)
1090 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001091 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001092 bio->bi_sector = page->physical >> 9;
1093 bio->bi_end_io = scrub_complete_bio_end_io;
1094 bio->bi_private = &complete;
1095
1096 ret = bio_add_page(bio, page->page, PAGE_SIZE, 0);
1097 if (PAGE_SIZE != ret) {
1098 bio_put(bio);
1099 return -EIO;
1100 }
1101 btrfsic_submit_bio(READ, bio);
1102
1103 /* this will also unplug the queue */
1104 wait_for_completion(&complete);
1105
1106 page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
1107 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1108 sblock->no_io_error_seen = 0;
1109 bio_put(bio);
1110 }
1111
1112 if (sblock->no_io_error_seen)
1113 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1114 have_csum, csum, generation,
1115 csum_size);
1116
1117 return 0;
1118}
1119
1120static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1121 struct scrub_block *sblock,
1122 int is_metadata, int have_csum,
1123 const u8 *csum, u64 generation,
1124 u16 csum_size)
1125{
1126 int page_num;
1127 u8 calculated_csum[BTRFS_CSUM_SIZE];
1128 u32 crc = ~(u32)0;
1129 struct btrfs_root *root = fs_info->extent_root;
1130 void *mapped_buffer;
1131
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001132 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001133 if (is_metadata) {
1134 struct btrfs_header *h;
1135
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001136 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001137 h = (struct btrfs_header *)mapped_buffer;
1138
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001139 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001140 memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
1141 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001142 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001143 sblock->header_error = 1;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001144 } else if (generation != le64_to_cpu(h->generation)) {
1145 sblock->header_error = 1;
1146 sblock->generation_error = 1;
1147 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001148 csum = h->csum;
1149 } else {
1150 if (!have_csum)
1151 return;
1152
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001153 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001154 }
1155
1156 for (page_num = 0;;) {
1157 if (page_num == 0 && is_metadata)
1158 crc = btrfs_csum_data(root,
1159 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1160 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1161 else
1162 crc = btrfs_csum_data(root, mapped_buffer, crc,
1163 PAGE_SIZE);
1164
Linus Torvalds9613beb2012-03-30 12:44:29 -07001165 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001166 page_num++;
1167 if (page_num >= sblock->page_count)
1168 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001169 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001170
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001171 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001172 }
1173
1174 btrfs_csum_final(crc, calculated_csum);
1175 if (memcmp(calculated_csum, csum, csum_size))
1176 sblock->checksum_error = 1;
1177}
1178
1179static void scrub_complete_bio_end_io(struct bio *bio, int err)
1180{
1181 complete((struct completion *)bio->bi_private);
1182}
1183
1184static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1185 struct scrub_block *sblock_good,
1186 int force_write)
1187{
1188 int page_num;
1189 int ret = 0;
1190
1191 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1192 int ret_sub;
1193
1194 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1195 sblock_good,
1196 page_num,
1197 force_write);
1198 if (ret_sub)
1199 ret = ret_sub;
1200 }
1201
1202 return ret;
1203}
1204
1205static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1206 struct scrub_block *sblock_good,
1207 int page_num, int force_write)
1208{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001209 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1210 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001211
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001212 BUG_ON(page_bad->page == NULL);
1213 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001214 if (force_write || sblock_bad->header_error ||
1215 sblock_bad->checksum_error || page_bad->io_error) {
1216 struct bio *bio;
1217 int ret;
1218 DECLARE_COMPLETION_ONSTACK(complete);
1219
1220 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001221 if (!bio)
1222 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001223 bio->bi_bdev = page_bad->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001224 bio->bi_sector = page_bad->physical >> 9;
1225 bio->bi_end_io = scrub_complete_bio_end_io;
1226 bio->bi_private = &complete;
1227
1228 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1229 if (PAGE_SIZE != ret) {
1230 bio_put(bio);
1231 return -EIO;
1232 }
1233 btrfsic_submit_bio(WRITE, bio);
1234
1235 /* this will also unplug the queue */
1236 wait_for_completion(&complete);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001237 if (!bio_flagged(bio, BIO_UPTODATE)) {
1238 btrfs_dev_stat_inc_and_print(page_bad->dev,
1239 BTRFS_DEV_STAT_WRITE_ERRS);
1240 bio_put(bio);
1241 return -EIO;
1242 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001243 bio_put(bio);
1244 }
1245
1246 return 0;
1247}
1248
1249static void scrub_checksum(struct scrub_block *sblock)
1250{
1251 u64 flags;
1252 int ret;
1253
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001254 WARN_ON(sblock->page_count < 1);
1255 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001256 ret = 0;
1257 if (flags & BTRFS_EXTENT_FLAG_DATA)
1258 ret = scrub_checksum_data(sblock);
1259 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1260 ret = scrub_checksum_tree_block(sblock);
1261 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1262 (void)scrub_checksum_super(sblock);
1263 else
1264 WARN_ON(1);
1265 if (ret)
1266 scrub_handle_errored_block(sblock);
1267}
1268
1269static int scrub_checksum_data(struct scrub_block *sblock)
1270{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001271 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001272 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001273 u8 *on_disk_csum;
1274 struct page *page;
1275 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001276 u32 crc = ~(u32)0;
1277 int fail = 0;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001278 struct btrfs_root *root = sctx->dev_root;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001279 u64 len;
1280 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001281
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001282 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001283 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001284 return 0;
1285
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001286 on_disk_csum = sblock->pagev[0]->csum;
1287 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001288 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001289
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001290 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001291 index = 0;
1292 for (;;) {
1293 u64 l = min_t(u64, len, PAGE_SIZE);
1294
1295 crc = btrfs_csum_data(root, buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001296 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001297 len -= l;
1298 if (len == 0)
1299 break;
1300 index++;
1301 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001302 BUG_ON(!sblock->pagev[index]->page);
1303 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001304 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001305 }
1306
Arne Jansena2de7332011-03-08 14:14:00 +01001307 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001308 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001309 fail = 1;
1310
Arne Jansena2de7332011-03-08 14:14:00 +01001311 return fail;
1312}
1313
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001314static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001315{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001316 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001317 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001318 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001319 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001320 u8 calculated_csum[BTRFS_CSUM_SIZE];
1321 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1322 struct page *page;
1323 void *mapped_buffer;
1324 u64 mapped_size;
1325 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001326 u32 crc = ~(u32)0;
1327 int fail = 0;
1328 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001329 u64 len;
1330 int index;
1331
1332 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001333 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001334 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001335 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001336 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001337
1338 /*
1339 * we don't use the getter functions here, as we
1340 * a) don't have an extent buffer and
1341 * b) the page is already kmapped
1342 */
Arne Jansena2de7332011-03-08 14:14:00 +01001343
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001344 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr))
Arne Jansena2de7332011-03-08 14:14:00 +01001345 ++fail;
1346
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001347 if (sblock->pagev[0]->generation != le64_to_cpu(h->generation))
Arne Jansena2de7332011-03-08 14:14:00 +01001348 ++fail;
1349
1350 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1351 ++fail;
1352
1353 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1354 BTRFS_UUID_SIZE))
1355 ++fail;
1356
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001357 BUG_ON(sctx->nodesize != sctx->leafsize);
1358 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001359 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1360 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1361 index = 0;
1362 for (;;) {
1363 u64 l = min_t(u64, len, mapped_size);
1364
1365 crc = btrfs_csum_data(root, p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001366 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001367 len -= l;
1368 if (len == 0)
1369 break;
1370 index++;
1371 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001372 BUG_ON(!sblock->pagev[index]->page);
1373 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001374 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001375 mapped_size = PAGE_SIZE;
1376 p = mapped_buffer;
1377 }
1378
1379 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001380 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001381 ++crc_fail;
1382
Arne Jansena2de7332011-03-08 14:14:00 +01001383 return fail || crc_fail;
1384}
1385
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001386static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001387{
1388 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001389 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001390 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001391 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001392 u8 calculated_csum[BTRFS_CSUM_SIZE];
1393 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1394 struct page *page;
1395 void *mapped_buffer;
1396 u64 mapped_size;
1397 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001398 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001399 int fail_gen = 0;
1400 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001401 u64 len;
1402 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001403
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001404 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001405 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001406 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001407 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001408 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001409
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001410 if (sblock->pagev[0]->logical != le64_to_cpu(s->bytenr))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001411 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001412
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001413 if (sblock->pagev[0]->generation != le64_to_cpu(s->generation))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001414 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001415
1416 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001417 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001418
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001419 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1420 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1421 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1422 index = 0;
1423 for (;;) {
1424 u64 l = min_t(u64, len, mapped_size);
1425
1426 crc = btrfs_csum_data(root, p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001427 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001428 len -= l;
1429 if (len == 0)
1430 break;
1431 index++;
1432 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001433 BUG_ON(!sblock->pagev[index]->page);
1434 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001435 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001436 mapped_size = PAGE_SIZE;
1437 p = mapped_buffer;
1438 }
1439
1440 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001441 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001442 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001443
Stefan Behrens442a4f62012-05-25 16:06:08 +02001444 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001445 /*
1446 * if we find an error in a super block, we just report it.
1447 * They will get written with the next transaction commit
1448 * anyway
1449 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001450 spin_lock(&sctx->stat_lock);
1451 ++sctx->stat.super_errors;
1452 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001453 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001454 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001455 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1456 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001457 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001458 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001459 }
1460
Stefan Behrens442a4f62012-05-25 16:06:08 +02001461 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001462}
1463
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001464static void scrub_block_get(struct scrub_block *sblock)
1465{
1466 atomic_inc(&sblock->ref_count);
1467}
1468
1469static void scrub_block_put(struct scrub_block *sblock)
1470{
1471 if (atomic_dec_and_test(&sblock->ref_count)) {
1472 int i;
1473
1474 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001475 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001476 kfree(sblock);
1477 }
1478}
1479
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001480static void scrub_page_get(struct scrub_page *spage)
1481{
1482 atomic_inc(&spage->ref_count);
1483}
1484
1485static void scrub_page_put(struct scrub_page *spage)
1486{
1487 if (atomic_dec_and_test(&spage->ref_count)) {
1488 if (spage->page)
1489 __free_page(spage->page);
1490 kfree(spage);
1491 }
1492}
1493
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001494static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01001495{
1496 struct scrub_bio *sbio;
1497
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001498 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04001499 return;
Arne Jansena2de7332011-03-08 14:14:00 +01001500
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001501 sbio = sctx->bios[sctx->curr];
1502 sctx->curr = -1;
1503 atomic_inc(&sctx->in_flight);
Arne Jansena2de7332011-03-08 14:14:00 +01001504
Stefan Behrens21adbd52011-11-09 13:44:05 +01001505 btrfsic_submit_bio(READ, sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01001506}
1507
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001508static int scrub_add_page_to_bio(struct scrub_ctx *sctx,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001509 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01001510{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001511 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01001512 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001513 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01001514
1515again:
1516 /*
1517 * grab a fresh bio or wait for one to become available
1518 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001519 while (sctx->curr == -1) {
1520 spin_lock(&sctx->list_lock);
1521 sctx->curr = sctx->first_free;
1522 if (sctx->curr != -1) {
1523 sctx->first_free = sctx->bios[sctx->curr]->next_free;
1524 sctx->bios[sctx->curr]->next_free = -1;
1525 sctx->bios[sctx->curr]->page_count = 0;
1526 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001527 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001528 spin_unlock(&sctx->list_lock);
1529 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01001530 }
1531 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001532 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001533 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05001534 struct bio *bio;
1535
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001536 sbio->physical = spage->physical;
1537 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001538 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001539 bio = sbio->bio;
1540 if (!bio) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001541 bio = bio_alloc(GFP_NOFS, sctx->pages_per_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001542 if (!bio)
1543 return -ENOMEM;
1544 sbio->bio = bio;
1545 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05001546
1547 bio->bi_private = sbio;
1548 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001549 bio->bi_bdev = sbio->dev->bdev;
1550 bio->bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001551 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001552 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1553 spage->physical ||
1554 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001555 spage->logical ||
1556 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001557 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05001558 goto again;
1559 }
1560
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001561 sbio->pagev[sbio->page_count] = spage;
1562 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1563 if (ret != PAGE_SIZE) {
1564 if (sbio->page_count < 1) {
1565 bio_put(sbio->bio);
1566 sbio->bio = NULL;
1567 return -EIO;
1568 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001569 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001570 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01001571 }
Arne Jansen1bc87792011-05-28 21:57:55 +02001572
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001573 scrub_block_get(sblock); /* one for the added page */
1574 atomic_inc(&sblock->outstanding_pages);
1575 sbio->page_count++;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001576 if (sbio->page_count == sctx->pages_per_bio)
1577 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001578
1579 return 0;
1580}
1581
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001582static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001583 u64 physical, struct btrfs_device *dev, u64 flags,
1584 u64 gen, int mirror_num, u8 *csum, int force)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001585{
1586 struct scrub_block *sblock;
1587 int index;
1588
1589 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
1590 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001591 spin_lock(&sctx->stat_lock);
1592 sctx->stat.malloc_errors++;
1593 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001594 return -ENOMEM;
1595 }
1596
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001597 /* one ref inside this function, plus one for each page added to
1598 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001599 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001600 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001601 sblock->no_io_error_seen = 1;
1602
1603 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001604 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001605 u64 l = min_t(u64, len, PAGE_SIZE);
1606
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001607 spage = kzalloc(sizeof(*spage), GFP_NOFS);
1608 if (!spage) {
1609leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001610 spin_lock(&sctx->stat_lock);
1611 sctx->stat.malloc_errors++;
1612 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001613 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001614 return -ENOMEM;
1615 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001616 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
1617 scrub_page_get(spage);
1618 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001619 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001620 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001621 spage->flags = flags;
1622 spage->generation = gen;
1623 spage->logical = logical;
1624 spage->physical = physical;
1625 spage->mirror_num = mirror_num;
1626 if (csum) {
1627 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001628 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001629 } else {
1630 spage->have_csum = 0;
1631 }
1632 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001633 spage->page = alloc_page(GFP_NOFS);
1634 if (!spage->page)
1635 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001636 len -= l;
1637 logical += l;
1638 physical += l;
1639 }
1640
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001641 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001642 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001643 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001644 int ret;
1645
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001646 ret = scrub_add_page_to_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001647 if (ret) {
1648 scrub_block_put(sblock);
1649 return ret;
1650 }
1651 }
1652
1653 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001654 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001655
1656 /* last one frees, either here or in bio completion for last page */
1657 scrub_block_put(sblock);
1658 return 0;
1659}
1660
1661static void scrub_bio_end_io(struct bio *bio, int err)
1662{
1663 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001664 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001665
1666 sbio->err = err;
1667 sbio->bio = bio;
1668
1669 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
1670}
1671
1672static void scrub_bio_end_io_worker(struct btrfs_work *work)
1673{
1674 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001675 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001676 int i;
1677
1678 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_BIO);
1679 if (sbio->err) {
1680 for (i = 0; i < sbio->page_count; i++) {
1681 struct scrub_page *spage = sbio->pagev[i];
1682
1683 spage->io_error = 1;
1684 spage->sblock->no_io_error_seen = 0;
1685 }
1686 }
1687
1688 /* now complete the scrub_block items that have all pages completed */
1689 for (i = 0; i < sbio->page_count; i++) {
1690 struct scrub_page *spage = sbio->pagev[i];
1691 struct scrub_block *sblock = spage->sblock;
1692
1693 if (atomic_dec_and_test(&sblock->outstanding_pages))
1694 scrub_block_complete(sblock);
1695 scrub_block_put(sblock);
1696 }
1697
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001698 bio_put(sbio->bio);
1699 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001700 spin_lock(&sctx->list_lock);
1701 sbio->next_free = sctx->first_free;
1702 sctx->first_free = sbio->index;
1703 spin_unlock(&sctx->list_lock);
1704 atomic_dec(&sctx->in_flight);
1705 wake_up(&sctx->list_wait);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001706}
1707
1708static void scrub_block_complete(struct scrub_block *sblock)
1709{
1710 if (!sblock->no_io_error_seen)
1711 scrub_handle_errored_block(sblock);
1712 else
1713 scrub_checksum(sblock);
1714}
1715
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001716static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01001717 u8 *csum)
1718{
1719 struct btrfs_ordered_sum *sum = NULL;
1720 int ret = 0;
1721 unsigned long i;
1722 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01001723
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001724 while (!list_empty(&sctx->csum_list)) {
1725 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01001726 struct btrfs_ordered_sum, list);
1727 if (sum->bytenr > logical)
1728 return 0;
1729 if (sum->bytenr + sum->len > logical)
1730 break;
1731
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001732 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01001733 list_del(&sum->list);
1734 kfree(sum);
1735 sum = NULL;
1736 }
1737 if (!sum)
1738 return 0;
1739
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001740 num_sectors = sum->len / sctx->sectorsize;
Arne Jansena2de7332011-03-08 14:14:00 +01001741 for (i = 0; i < num_sectors; ++i) {
1742 if (sum->sums[i].bytenr == logical) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001743 memcpy(csum, &sum->sums[i].sum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001744 ret = 1;
1745 break;
1746 }
1747 }
1748 if (ret && i == num_sectors - 1) {
1749 list_del(&sum->list);
1750 kfree(sum);
1751 }
1752 return ret;
1753}
1754
1755/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001756static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001757 u64 physical, struct btrfs_device *dev, u64 flags,
1758 u64 gen, int mirror_num)
Arne Jansena2de7332011-03-08 14:14:00 +01001759{
1760 int ret;
1761 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001762 u32 blocksize;
1763
1764 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001765 blocksize = sctx->sectorsize;
1766 spin_lock(&sctx->stat_lock);
1767 sctx->stat.data_extents_scrubbed++;
1768 sctx->stat.data_bytes_scrubbed += len;
1769 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001770 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001771 BUG_ON(sctx->nodesize != sctx->leafsize);
1772 blocksize = sctx->nodesize;
1773 spin_lock(&sctx->stat_lock);
1774 sctx->stat.tree_extents_scrubbed++;
1775 sctx->stat.tree_bytes_scrubbed += len;
1776 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001777 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001778 blocksize = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001779 BUG_ON(1);
1780 }
Arne Jansena2de7332011-03-08 14:14:00 +01001781
1782 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001783 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01001784 int have_csum = 0;
1785
1786 if (flags & BTRFS_EXTENT_FLAG_DATA) {
1787 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001788 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01001789 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001790 ++sctx->stat.no_csum;
Arne Jansena2de7332011-03-08 14:14:00 +01001791 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001792 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001793 mirror_num, have_csum ? csum : NULL, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01001794 if (ret)
1795 return ret;
1796 len -= l;
1797 logical += l;
1798 physical += l;
1799 }
1800 return 0;
1801}
1802
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001803static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001804 struct map_lookup *map,
1805 struct btrfs_device *scrub_dev,
1806 int num, u64 base, u64 length)
Arne Jansena2de7332011-03-08 14:14:00 +01001807{
1808 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001809 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01001810 struct btrfs_root *root = fs_info->extent_root;
1811 struct btrfs_root *csum_root = fs_info->csum_root;
1812 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00001813 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01001814 u64 flags;
1815 int ret;
1816 int slot;
1817 int i;
1818 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01001819 struct extent_buffer *l;
1820 struct btrfs_key key;
1821 u64 physical;
1822 u64 logical;
1823 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02001824 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02001825 struct reada_control *reada1;
1826 struct reada_control *reada2;
1827 struct btrfs_key key_start;
1828 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01001829 u64 increment = map->stripe_len;
1830 u64 offset;
1831
1832 nstripes = length;
1833 offset = 0;
1834 do_div(nstripes, map->stripe_len);
1835 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1836 offset = map->stripe_len * num;
1837 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02001838 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001839 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1840 int factor = map->num_stripes / map->sub_stripes;
1841 offset = map->stripe_len * (num / map->sub_stripes);
1842 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02001843 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001844 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1845 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02001846 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001847 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1848 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02001849 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001850 } else {
1851 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02001852 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001853 }
1854
1855 path = btrfs_alloc_path();
1856 if (!path)
1857 return -ENOMEM;
1858
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001859 /*
1860 * work on commit root. The related disk blocks are static as
1861 * long as COW is applied. This means, it is save to rewrite
1862 * them to repair disk errors without any race conditions
1863 */
Arne Jansena2de7332011-03-08 14:14:00 +01001864 path->search_commit_root = 1;
1865 path->skip_locking = 1;
1866
1867 /*
Arne Jansen7a262852011-06-10 12:39:23 +02001868 * trigger the readahead for extent tree csum tree and wait for
1869 * completion. During readahead, the scrub is officially paused
1870 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01001871 */
1872 logical = base + offset;
Arne Jansena2de7332011-03-08 14:14:00 +01001873
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001874 wait_event(sctx->list_wait,
1875 atomic_read(&sctx->in_flight) == 0);
Arne Jansen7a262852011-06-10 12:39:23 +02001876 atomic_inc(&fs_info->scrubs_paused);
1877 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01001878
Arne Jansen7a262852011-06-10 12:39:23 +02001879 /* FIXME it might be better to start readahead at commit root */
1880 key_start.objectid = logical;
1881 key_start.type = BTRFS_EXTENT_ITEM_KEY;
1882 key_start.offset = (u64)0;
1883 key_end.objectid = base + offset + nstripes * increment;
1884 key_end.type = BTRFS_EXTENT_ITEM_KEY;
1885 key_end.offset = (u64)0;
1886 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01001887
Arne Jansen7a262852011-06-10 12:39:23 +02001888 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1889 key_start.type = BTRFS_EXTENT_CSUM_KEY;
1890 key_start.offset = logical;
1891 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1892 key_end.type = BTRFS_EXTENT_CSUM_KEY;
1893 key_end.offset = base + offset + nstripes * increment;
1894 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01001895
Arne Jansen7a262852011-06-10 12:39:23 +02001896 if (!IS_ERR(reada1))
1897 btrfs_reada_wait(reada1);
1898 if (!IS_ERR(reada2))
1899 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01001900
Arne Jansen7a262852011-06-10 12:39:23 +02001901 mutex_lock(&fs_info->scrub_lock);
1902 while (atomic_read(&fs_info->scrub_pause_req)) {
1903 mutex_unlock(&fs_info->scrub_lock);
1904 wait_event(fs_info->scrub_pause_wait,
1905 atomic_read(&fs_info->scrub_pause_req) == 0);
1906 mutex_lock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001907 }
Arne Jansen7a262852011-06-10 12:39:23 +02001908 atomic_dec(&fs_info->scrubs_paused);
1909 mutex_unlock(&fs_info->scrub_lock);
1910 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01001911
1912 /*
1913 * collect all data csums for the stripe to avoid seeking during
1914 * the scrub. This might currently (crc32) end up to be about 1MB
1915 */
Arne Jansene7786c32011-05-28 20:58:38 +00001916 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01001917
Arne Jansena2de7332011-03-08 14:14:00 +01001918 /*
1919 * now find all extents for each stripe and scrub them
1920 */
Arne Jansen7a262852011-06-10 12:39:23 +02001921 logical = base + offset;
1922 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01001923 ret = 0;
Arne Jansen7a262852011-06-10 12:39:23 +02001924 for (i = 0; i < nstripes; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +01001925 /*
1926 * canceled?
1927 */
1928 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001929 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01001930 ret = -ECANCELED;
1931 goto out;
1932 }
1933 /*
1934 * check to see if we have to pause
1935 */
1936 if (atomic_read(&fs_info->scrub_pause_req)) {
1937 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001938 scrub_submit(sctx);
1939 wait_event(sctx->list_wait,
1940 atomic_read(&sctx->in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01001941 atomic_inc(&fs_info->scrubs_paused);
1942 wake_up(&fs_info->scrub_pause_wait);
1943 mutex_lock(&fs_info->scrub_lock);
1944 while (atomic_read(&fs_info->scrub_pause_req)) {
1945 mutex_unlock(&fs_info->scrub_lock);
1946 wait_event(fs_info->scrub_pause_wait,
1947 atomic_read(&fs_info->scrub_pause_req) == 0);
1948 mutex_lock(&fs_info->scrub_lock);
1949 }
1950 atomic_dec(&fs_info->scrubs_paused);
1951 mutex_unlock(&fs_info->scrub_lock);
1952 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01001953 }
1954
Arne Jansen7a262852011-06-10 12:39:23 +02001955 ret = btrfs_lookup_csums_range(csum_root, logical,
1956 logical + map->stripe_len - 1,
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001957 &sctx->csum_list, 1);
Arne Jansen7a262852011-06-10 12:39:23 +02001958 if (ret)
1959 goto out;
1960
Arne Jansena2de7332011-03-08 14:14:00 +01001961 key.objectid = logical;
1962 key.type = BTRFS_EXTENT_ITEM_KEY;
1963 key.offset = (u64)0;
1964
1965 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1966 if (ret < 0)
1967 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02001968 if (ret > 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01001969 ret = btrfs_previous_item(root, path, 0,
1970 BTRFS_EXTENT_ITEM_KEY);
1971 if (ret < 0)
1972 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02001973 if (ret > 0) {
1974 /* there's no smaller item, so stick with the
1975 * larger one */
1976 btrfs_release_path(path);
1977 ret = btrfs_search_slot(NULL, root, &key,
1978 path, 0, 0);
1979 if (ret < 0)
1980 goto out;
1981 }
Arne Jansena2de7332011-03-08 14:14:00 +01001982 }
1983
1984 while (1) {
1985 l = path->nodes[0];
1986 slot = path->slots[0];
1987 if (slot >= btrfs_header_nritems(l)) {
1988 ret = btrfs_next_leaf(root, path);
1989 if (ret == 0)
1990 continue;
1991 if (ret < 0)
1992 goto out;
1993
1994 break;
1995 }
1996 btrfs_item_key_to_cpu(l, &key, slot);
1997
1998 if (key.objectid + key.offset <= logical)
1999 goto next;
2000
2001 if (key.objectid >= logical + map->stripe_len)
2002 break;
2003
2004 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
2005 goto next;
2006
2007 extent = btrfs_item_ptr(l, slot,
2008 struct btrfs_extent_item);
2009 flags = btrfs_extent_flags(l, extent);
2010 generation = btrfs_extent_generation(l, extent);
2011
2012 if (key.objectid < logical &&
2013 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2014 printk(KERN_ERR
2015 "btrfs scrub: tree block %llu spanning "
2016 "stripes, ignored. logical=%llu\n",
2017 (unsigned long long)key.objectid,
2018 (unsigned long long)logical);
2019 goto next;
2020 }
2021
2022 /*
2023 * trim extent to this stripe
2024 */
2025 if (key.objectid < logical) {
2026 key.offset -= logical - key.objectid;
2027 key.objectid = logical;
2028 }
2029 if (key.objectid + key.offset >
2030 logical + map->stripe_len) {
2031 key.offset = logical + map->stripe_len -
2032 key.objectid;
2033 }
2034
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002035 ret = scrub_extent(sctx, key.objectid, key.offset,
Arne Jansena2de7332011-03-08 14:14:00 +01002036 key.objectid - logical + physical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002037 scrub_dev, flags, generation,
2038 mirror_num);
Arne Jansena2de7332011-03-08 14:14:00 +01002039 if (ret)
2040 goto out;
2041
2042next:
2043 path->slots[0]++;
2044 }
Chris Mason71267332011-05-23 06:30:52 -04002045 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002046 logical += increment;
2047 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002048 spin_lock(&sctx->stat_lock);
2049 sctx->stat.last_physical = physical;
2050 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002051 }
2052 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002053 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002054
2055out:
Arne Jansene7786c32011-05-28 20:58:38 +00002056 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002057 btrfs_free_path(path);
2058 return ret < 0 ? ret : 0;
2059}
2060
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002061static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002062 struct btrfs_device *scrub_dev,
2063 u64 chunk_tree, u64 chunk_objectid,
2064 u64 chunk_offset, u64 length,
2065 u64 dev_offset)
Arne Jansena2de7332011-03-08 14:14:00 +01002066{
2067 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002068 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01002069 struct map_lookup *map;
2070 struct extent_map *em;
2071 int i;
2072 int ret = -EINVAL;
2073
2074 read_lock(&map_tree->map_tree.lock);
2075 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2076 read_unlock(&map_tree->map_tree.lock);
2077
2078 if (!em)
2079 return -EINVAL;
2080
2081 map = (struct map_lookup *)em->bdev;
2082 if (em->start != chunk_offset)
2083 goto out;
2084
2085 if (em->len < length)
2086 goto out;
2087
2088 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002089 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01002090 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002091 ret = scrub_stripe(sctx, map, scrub_dev, i,
2092 chunk_offset, length);
Arne Jansena2de7332011-03-08 14:14:00 +01002093 if (ret)
2094 goto out;
2095 }
2096 }
2097out:
2098 free_extent_map(em);
2099
2100 return ret;
2101}
2102
2103static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002104int scrub_enumerate_chunks(struct scrub_ctx *sctx,
2105 struct btrfs_device *scrub_dev, u64 start, u64 end)
Arne Jansena2de7332011-03-08 14:14:00 +01002106{
2107 struct btrfs_dev_extent *dev_extent = NULL;
2108 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002109 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002110 struct btrfs_fs_info *fs_info = root->fs_info;
2111 u64 length;
2112 u64 chunk_tree;
2113 u64 chunk_objectid;
2114 u64 chunk_offset;
2115 int ret;
2116 int slot;
2117 struct extent_buffer *l;
2118 struct btrfs_key key;
2119 struct btrfs_key found_key;
2120 struct btrfs_block_group_cache *cache;
2121
2122 path = btrfs_alloc_path();
2123 if (!path)
2124 return -ENOMEM;
2125
2126 path->reada = 2;
2127 path->search_commit_root = 1;
2128 path->skip_locking = 1;
2129
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002130 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01002131 key.offset = 0ull;
2132 key.type = BTRFS_DEV_EXTENT_KEY;
2133
Arne Jansena2de7332011-03-08 14:14:00 +01002134 while (1) {
2135 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2136 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02002137 break;
2138 if (ret > 0) {
2139 if (path->slots[0] >=
2140 btrfs_header_nritems(path->nodes[0])) {
2141 ret = btrfs_next_leaf(root, path);
2142 if (ret)
2143 break;
2144 }
2145 }
Arne Jansena2de7332011-03-08 14:14:00 +01002146
2147 l = path->nodes[0];
2148 slot = path->slots[0];
2149
2150 btrfs_item_key_to_cpu(l, &found_key, slot);
2151
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002152 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01002153 break;
2154
Arne Jansen8c510322011-06-03 10:09:26 +02002155 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01002156 break;
2157
2158 if (found_key.offset >= end)
2159 break;
2160
2161 if (found_key.offset < key.offset)
2162 break;
2163
2164 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2165 length = btrfs_dev_extent_length(l, dev_extent);
2166
2167 if (found_key.offset + length <= start) {
2168 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002169 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002170 continue;
2171 }
2172
2173 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2174 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2175 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2176
2177 /*
2178 * get a reference on the corresponding block group to prevent
2179 * the chunk from going away while we scrub it
2180 */
2181 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2182 if (!cache) {
2183 ret = -ENOENT;
Arne Jansen8c510322011-06-03 10:09:26 +02002184 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002185 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002186 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Arne Jansen859acaf2012-02-09 15:09:02 +01002187 chunk_offset, length, found_key.offset);
Arne Jansena2de7332011-03-08 14:14:00 +01002188 btrfs_put_block_group(cache);
2189 if (ret)
2190 break;
2191
2192 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002193 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002194 }
2195
Arne Jansena2de7332011-03-08 14:14:00 +01002196 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02002197
2198 /*
2199 * ret can still be 1 from search_slot or next_leaf,
2200 * that's not an error
2201 */
2202 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002203}
2204
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002205static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2206 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01002207{
2208 int i;
2209 u64 bytenr;
2210 u64 gen;
2211 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002212 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002213
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002214 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
2215 return -EIO;
2216
Arne Jansena2de7332011-03-08 14:14:00 +01002217 gen = root->fs_info->last_trans_committed;
2218
2219 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2220 bytenr = btrfs_sb_offset(i);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002221 if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01002222 break;
2223
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002224 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002225 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
2226 NULL, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002227 if (ret)
2228 return ret;
2229 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002230 wait_event(sctx->list_wait, atomic_read(&sctx->in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002231
2232 return 0;
2233}
2234
2235/*
2236 * get a reference count on fs_info->scrub_workers. start worker if necessary
2237 */
2238static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
2239{
2240 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002241 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002242
2243 mutex_lock(&fs_info->scrub_lock);
Arne Jansen632dd772011-06-10 12:07:07 +02002244 if (fs_info->scrub_workers_refcnt == 0) {
2245 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
2246 fs_info->thread_pool_size, &fs_info->generic_worker);
2247 fs_info->scrub_workers.idle_thresh = 4;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002248 ret = btrfs_start_workers(&fs_info->scrub_workers);
2249 if (ret)
2250 goto out;
Arne Jansen632dd772011-06-10 12:07:07 +02002251 }
Arne Jansena2de7332011-03-08 14:14:00 +01002252 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002253out:
Arne Jansena2de7332011-03-08 14:14:00 +01002254 mutex_unlock(&fs_info->scrub_lock);
2255
Josef Bacik0dc3b842011-11-18 14:37:27 -05002256 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002257}
2258
2259static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
2260{
2261 struct btrfs_fs_info *fs_info = root->fs_info;
2262
2263 mutex_lock(&fs_info->scrub_lock);
2264 if (--fs_info->scrub_workers_refcnt == 0)
2265 btrfs_stop_workers(&fs_info->scrub_workers);
2266 WARN_ON(fs_info->scrub_workers_refcnt < 0);
2267 mutex_unlock(&fs_info->scrub_lock);
2268}
2269
2270
2271int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
Arne Jansen86287642011-03-23 16:34:19 +01002272 struct btrfs_scrub_progress *progress, int readonly)
Arne Jansena2de7332011-03-08 14:14:00 +01002273{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002274 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002275 struct btrfs_fs_info *fs_info = root->fs_info;
2276 int ret;
2277 struct btrfs_device *dev;
2278
David Sterba7841cb22011-05-31 18:07:27 +02002279 if (btrfs_fs_closing(root->fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01002280 return -EINVAL;
2281
2282 /*
2283 * check some assumptions
2284 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002285 if (root->nodesize != root->leafsize) {
2286 printk(KERN_ERR
2287 "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
2288 root->nodesize, root->leafsize);
2289 return -EINVAL;
2290 }
2291
2292 if (root->nodesize > BTRFS_STRIPE_LEN) {
2293 /*
2294 * in this case scrub is unable to calculate the checksum
2295 * the way scrub is implemented. Do not handle this
2296 * situation at all because it won't ever happen.
2297 */
2298 printk(KERN_ERR
2299 "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
2300 root->nodesize, BTRFS_STRIPE_LEN);
2301 return -EINVAL;
2302 }
2303
2304 if (root->sectorsize != PAGE_SIZE) {
2305 /* not supported for data w/o checksums */
2306 printk(KERN_ERR
2307 "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
2308 root->sectorsize, (unsigned long long)PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01002309 return -EINVAL;
2310 }
2311
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002312 if (fs_info->chunk_root->nodesize >
2313 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
2314 fs_info->chunk_root->sectorsize >
2315 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
2316 /*
2317 * would exhaust the array bounds of pagev member in
2318 * struct scrub_block
2319 */
2320 pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
2321 fs_info->chunk_root->nodesize,
2322 SCRUB_MAX_PAGES_PER_BLOCK,
2323 fs_info->chunk_root->sectorsize,
2324 SCRUB_MAX_PAGES_PER_BLOCK);
2325 return -EINVAL;
2326 }
2327
Arne Jansena2de7332011-03-08 14:14:00 +01002328 ret = scrub_workers_get(root);
2329 if (ret)
2330 return ret;
2331
2332 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2333 dev = btrfs_find_device(root, devid, NULL, NULL);
2334 if (!dev || dev->missing) {
2335 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2336 scrub_workers_put(root);
2337 return -ENODEV;
2338 }
2339 mutex_lock(&fs_info->scrub_lock);
2340
2341 if (!dev->in_fs_metadata) {
2342 mutex_unlock(&fs_info->scrub_lock);
2343 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2344 scrub_workers_put(root);
2345 return -ENODEV;
2346 }
2347
2348 if (dev->scrub_device) {
2349 mutex_unlock(&fs_info->scrub_lock);
2350 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2351 scrub_workers_put(root);
2352 return -EINPROGRESS;
2353 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002354 sctx = scrub_setup_ctx(dev);
2355 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002356 mutex_unlock(&fs_info->scrub_lock);
2357 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2358 scrub_workers_put(root);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002359 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002360 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002361 sctx->readonly = readonly;
2362 dev->scrub_device = sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002363
2364 atomic_inc(&fs_info->scrubs_running);
2365 mutex_unlock(&fs_info->scrub_lock);
2366 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2367
2368 down_read(&fs_info->scrub_super_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002369 ret = scrub_supers(sctx, dev);
Arne Jansena2de7332011-03-08 14:14:00 +01002370 up_read(&fs_info->scrub_super_lock);
2371
2372 if (!ret)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002373 ret = scrub_enumerate_chunks(sctx, dev, start, end);
Arne Jansena2de7332011-03-08 14:14:00 +01002374
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002375 wait_event(sctx->list_wait, atomic_read(&sctx->in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002376 atomic_dec(&fs_info->scrubs_running);
2377 wake_up(&fs_info->scrub_pause_wait);
2378
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002379 wait_event(sctx->list_wait, atomic_read(&sctx->fixup_cnt) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02002380
Arne Jansena2de7332011-03-08 14:14:00 +01002381 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002382 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002383
2384 mutex_lock(&fs_info->scrub_lock);
2385 dev->scrub_device = NULL;
2386 mutex_unlock(&fs_info->scrub_lock);
2387
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002388 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002389 scrub_workers_put(root);
2390
2391 return ret;
2392}
2393
Jeff Mahoney143bede2012-03-01 14:56:26 +01002394void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002395{
2396 struct btrfs_fs_info *fs_info = root->fs_info;
2397
2398 mutex_lock(&fs_info->scrub_lock);
2399 atomic_inc(&fs_info->scrub_pause_req);
2400 while (atomic_read(&fs_info->scrubs_paused) !=
2401 atomic_read(&fs_info->scrubs_running)) {
2402 mutex_unlock(&fs_info->scrub_lock);
2403 wait_event(fs_info->scrub_pause_wait,
2404 atomic_read(&fs_info->scrubs_paused) ==
2405 atomic_read(&fs_info->scrubs_running));
2406 mutex_lock(&fs_info->scrub_lock);
2407 }
2408 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002409}
2410
Jeff Mahoney143bede2012-03-01 14:56:26 +01002411void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002412{
2413 struct btrfs_fs_info *fs_info = root->fs_info;
2414
2415 atomic_dec(&fs_info->scrub_pause_req);
2416 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002417}
2418
Jeff Mahoney143bede2012-03-01 14:56:26 +01002419void btrfs_scrub_pause_super(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002420{
2421 down_write(&root->fs_info->scrub_super_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002422}
2423
Jeff Mahoney143bede2012-03-01 14:56:26 +01002424void btrfs_scrub_continue_super(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002425{
2426 up_write(&root->fs_info->scrub_super_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002427}
2428
Jeff Mahoney49b25e02012-03-01 17:24:58 +01002429int __btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002430{
Arne Jansena2de7332011-03-08 14:14:00 +01002431
2432 mutex_lock(&fs_info->scrub_lock);
2433 if (!atomic_read(&fs_info->scrubs_running)) {
2434 mutex_unlock(&fs_info->scrub_lock);
2435 return -ENOTCONN;
2436 }
2437
2438 atomic_inc(&fs_info->scrub_cancel_req);
2439 while (atomic_read(&fs_info->scrubs_running)) {
2440 mutex_unlock(&fs_info->scrub_lock);
2441 wait_event(fs_info->scrub_pause_wait,
2442 atomic_read(&fs_info->scrubs_running) == 0);
2443 mutex_lock(&fs_info->scrub_lock);
2444 }
2445 atomic_dec(&fs_info->scrub_cancel_req);
2446 mutex_unlock(&fs_info->scrub_lock);
2447
2448 return 0;
2449}
2450
Jeff Mahoney49b25e02012-03-01 17:24:58 +01002451int btrfs_scrub_cancel(struct btrfs_root *root)
2452{
2453 return __btrfs_scrub_cancel(root->fs_info);
2454}
2455
Arne Jansena2de7332011-03-08 14:14:00 +01002456int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
2457{
2458 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002459 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002460
2461 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002462 sctx = dev->scrub_device;
2463 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01002464 mutex_unlock(&fs_info->scrub_lock);
2465 return -ENOTCONN;
2466 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002467 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01002468 while (dev->scrub_device) {
2469 mutex_unlock(&fs_info->scrub_lock);
2470 wait_event(fs_info->scrub_pause_wait,
2471 dev->scrub_device == NULL);
2472 mutex_lock(&fs_info->scrub_lock);
2473 }
2474 mutex_unlock(&fs_info->scrub_lock);
2475
2476 return 0;
2477}
Stefan Behrens1623ede2012-03-27 14:21:26 -04002478
Arne Jansena2de7332011-03-08 14:14:00 +01002479int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
2480{
2481 struct btrfs_fs_info *fs_info = root->fs_info;
2482 struct btrfs_device *dev;
2483 int ret;
2484
2485 /*
2486 * we have to hold the device_list_mutex here so the device
2487 * does not go away in cancel_dev. FIXME: find a better solution
2488 */
2489 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2490 dev = btrfs_find_device(root, devid, NULL, NULL);
2491 if (!dev) {
2492 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2493 return -ENODEV;
2494 }
2495 ret = btrfs_scrub_cancel_dev(root, dev);
2496 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2497
2498 return ret;
2499}
2500
2501int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
2502 struct btrfs_scrub_progress *progress)
2503{
2504 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002505 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01002506
2507 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2508 dev = btrfs_find_device(root, devid, NULL, NULL);
2509 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002510 sctx = dev->scrub_device;
2511 if (sctx)
2512 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002513 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2514
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002515 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01002516}