blob: fcd5bccaa4edd6b9f575485330d3a257ed6e024f [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);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100154static void 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);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400158static 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 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100721 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
722 csum, generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400723
724 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
725 sblock_bad->no_io_error_seen) {
726 /*
727 * the error disappeared after reading page by page, or
728 * the area was part of a huge bio and other parts of the
729 * bio caused I/O errors, or the block layer merged several
730 * read requests into one and the error is caused by a
731 * different bio (usually one of the two latter cases is
732 * the cause)
733 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100734 spin_lock(&sctx->stat_lock);
735 sctx->stat.unverified_errors++;
736 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400737
738 goto out;
739 }
740
741 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100742 spin_lock(&sctx->stat_lock);
743 sctx->stat.read_errors++;
744 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400745 if (__ratelimit(&_rs))
746 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100747 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400748 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100749 spin_lock(&sctx->stat_lock);
750 sctx->stat.csum_errors++;
751 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400752 if (__ratelimit(&_rs))
753 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100754 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200755 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400756 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100757 spin_lock(&sctx->stat_lock);
758 sctx->stat.verify_errors++;
759 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400760 if (__ratelimit(&_rs))
761 scrub_print_warning("checksum/header error",
762 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200763 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100764 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200765 BTRFS_DEV_STAT_GENERATION_ERRS);
766 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100767 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200768 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400769 }
770
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100771 if (sctx->readonly)
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400772 goto did_not_correct_error;
773
774 if (!is_metadata && !have_csum) {
775 struct scrub_fixup_nodatasum *fixup_nodatasum;
776
777 /*
778 * !is_metadata and !have_csum, this means that the data
779 * might not be COW'ed, that it might be modified
780 * concurrently. The general strategy to work on the
781 * commit root does not help in the case when COW is not
782 * used.
783 */
784 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
785 if (!fixup_nodatasum)
786 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100787 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100788 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400789 fixup_nodatasum->logical = logical;
790 fixup_nodatasum->root = fs_info->extent_root;
791 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Arne Jansena2de7332011-03-08 14:14:00 +0100792 /*
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200793 * increment scrubs_running to prevent cancel requests from
794 * completing as long as a fixup worker is running. we must also
795 * increment scrubs_paused to prevent deadlocking on pause
796 * requests used for transactions commits (as the worker uses a
797 * transaction context). it is safe to regard the fixup worker
798 * as paused for all matters practical. effectively, we only
799 * avoid cancellation requests from completing.
Arne Jansena2de7332011-03-08 14:14:00 +0100800 */
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200801 mutex_lock(&fs_info->scrub_lock);
802 atomic_inc(&fs_info->scrubs_running);
803 atomic_inc(&fs_info->scrubs_paused);
804 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100805 atomic_inc(&sctx->fixup_cnt);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400806 fixup_nodatasum->work.func = scrub_fixup_nodatasum;
807 btrfs_queue_worker(&fs_info->scrub_workers,
808 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +0100809 goto out;
810 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400811
812 /*
813 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100814 * checksums.
815 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400816 * errors and also does not have a checksum error.
817 * If one is found, and if a checksum is present, the full block
818 * that is known to contain an error is rewritten. Afterwards
819 * the block is known to be corrected.
820 * If a mirror is found which is completely correct, and no
821 * checksum is present, only those pages are rewritten that had
822 * an I/O error in the block to be repaired, since it cannot be
823 * determined, which copy of the other pages is better (and it
824 * could happen otherwise that a correct page would be
825 * overwritten by a bad one).
826 */
827 for (mirror_index = 0;
828 mirror_index < BTRFS_MAX_MIRRORS &&
829 sblocks_for_recheck[mirror_index].page_count > 0;
830 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100831 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400832
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100833 if (mirror_index == failed_mirror_index)
834 continue;
835 sblock_other = sblocks_for_recheck + mirror_index;
836
837 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100838 scrub_recheck_block(fs_info, sblock_other, is_metadata,
839 have_csum, csum, generation,
840 sctx->csum_size);
841
842 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400843 !sblock_other->checksum_error &&
844 sblock_other->no_io_error_seen) {
845 int force_write = is_metadata || have_csum;
846
847 ret = scrub_repair_block_from_good_copy(sblock_bad,
848 sblock_other,
849 force_write);
850 if (0 == ret)
851 goto corrected_error;
Arne Jansena2de7332011-03-08 14:14:00 +0100852 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400853 }
854
855 /*
856 * in case of I/O errors in the area that is supposed to be
857 * repaired, continue by picking good copies of those pages.
858 * Select the good pages from mirrors to rewrite bad pages from
859 * the area to fix. Afterwards verify the checksum of the block
860 * that is supposed to be repaired. This verification step is
861 * only done for the purpose of statistic counting and for the
862 * final scrub report, whether errors remain.
863 * A perfect algorithm could make use of the checksum and try
864 * all possible combinations of pages from the different mirrors
865 * until the checksum verification succeeds. For example, when
866 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
867 * of mirror #2 is readable but the final checksum test fails,
868 * then the 2nd page of mirror #3 could be tried, whether now
869 * the final checksum succeedes. But this would be a rare
870 * exception and is therefore not implemented. At least it is
871 * avoided that the good copy is overwritten.
872 * A more useful improvement would be to pick the sectors
873 * without I/O error based on sector sizes (512 bytes on legacy
874 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
875 * mirror could be repaired by taking 512 byte of a different
876 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
877 * area are unreadable.
878 */
879
880 /* can only fix I/O errors from here on */
881 if (sblock_bad->no_io_error_seen)
882 goto did_not_correct_error;
883
884 success = 1;
885 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100886 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400887
888 if (!page_bad->io_error)
889 continue;
890
891 for (mirror_index = 0;
892 mirror_index < BTRFS_MAX_MIRRORS &&
893 sblocks_for_recheck[mirror_index].page_count > 0;
894 mirror_index++) {
895 struct scrub_block *sblock_other = sblocks_for_recheck +
896 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100897 struct scrub_page *page_other = sblock_other->pagev[
898 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400899
900 if (!page_other->io_error) {
901 ret = scrub_repair_page_from_good_copy(
902 sblock_bad, sblock_other, page_num, 0);
903 if (0 == ret) {
904 page_bad->io_error = 0;
905 break; /* succeeded for this page */
906 }
Jan Schmidt13db62b2011-06-13 19:56:13 +0200907 }
908 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400909
910 if (page_bad->io_error) {
911 /* did not find a mirror to copy the page from */
912 success = 0;
913 }
914 }
915
916 if (success) {
917 if (is_metadata || have_csum) {
918 /*
919 * need to verify the checksum now that all
920 * sectors on disk are repaired (the write
921 * request for data to be repaired is on its way).
922 * Just be lazy and use scrub_recheck_block()
923 * which re-reads the data before the checksum
924 * is verified, but most likely the data comes out
925 * of the page cache.
926 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100927 scrub_recheck_block(fs_info, sblock_bad,
928 is_metadata, have_csum, csum,
929 generation, sctx->csum_size);
930 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400931 !sblock_bad->checksum_error &&
932 sblock_bad->no_io_error_seen)
933 goto corrected_error;
934 else
935 goto did_not_correct_error;
936 } else {
937corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100938 spin_lock(&sctx->stat_lock);
939 sctx->stat.corrected_errors++;
940 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400941 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400942 "btrfs: fixed up error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -0400943 (unsigned long long)logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100944 rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400945 }
946 } else {
947did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100948 spin_lock(&sctx->stat_lock);
949 sctx->stat.uncorrectable_errors++;
950 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400951 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400952 "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -0400953 (unsigned long long)logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100954 rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +0100955 }
956
957out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400958 if (sblocks_for_recheck) {
959 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
960 mirror_index++) {
961 struct scrub_block *sblock = sblocks_for_recheck +
962 mirror_index;
963 int page_index;
964
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100965 for (page_index = 0; page_index < sblock->page_count;
966 page_index++) {
967 sblock->pagev[page_index]->sblock = NULL;
968 scrub_page_put(sblock->pagev[page_index]);
969 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400970 }
971 kfree(sblocks_for_recheck);
972 }
973
974 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +0100975}
976
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100977static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400978 struct btrfs_mapping_tree *map_tree,
979 u64 length, u64 logical,
980 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +0100981{
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400982 int page_index;
983 int mirror_index;
984 int ret;
985
986 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100987 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400988 * are not used (and not set) in the blocks that are used for
989 * the recheck procedure
990 */
991
992 page_index = 0;
993 while (length > 0) {
994 u64 sublen = min_t(u64, length, PAGE_SIZE);
995 u64 mapped_length = sublen;
996 struct btrfs_bio *bbio = NULL;
997
998 /*
999 * with a length of PAGE_SIZE, each returned stripe
1000 * represents one mirror
1001 */
1002 ret = btrfs_map_block(map_tree, WRITE, logical, &mapped_length,
1003 &bbio, 0);
1004 if (ret || !bbio || mapped_length < sublen) {
1005 kfree(bbio);
1006 return -EIO;
1007 }
1008
1009 BUG_ON(page_index >= SCRUB_PAGES_PER_BIO);
1010 for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
1011 mirror_index++) {
1012 struct scrub_block *sblock;
1013 struct scrub_page *page;
1014
1015 if (mirror_index >= BTRFS_MAX_MIRRORS)
1016 continue;
1017
1018 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001019 sblock->sctx = sctx;
1020 page = kzalloc(sizeof(*page), GFP_NOFS);
1021 if (!page) {
1022leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001023 spin_lock(&sctx->stat_lock);
1024 sctx->stat.malloc_errors++;
1025 spin_unlock(&sctx->stat_lock);
Wei Yongjuncf93dcc2012-09-02 07:44:51 -06001026 kfree(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001027 return -ENOMEM;
1028 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001029 scrub_page_get(page);
1030 sblock->pagev[page_index] = page;
1031 page->logical = logical;
1032 page->physical = bbio->stripes[mirror_index].physical;
1033 /* for missing devices, dev->bdev is NULL */
1034 page->dev = bbio->stripes[mirror_index].dev;
1035 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001036 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001037 page->page = alloc_page(GFP_NOFS);
1038 if (!page->page)
1039 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001040 }
1041 kfree(bbio);
1042 length -= sublen;
1043 logical += sublen;
1044 page_index++;
1045 }
1046
1047 return 0;
1048}
1049
1050/*
1051 * this function will check the on disk data for checksum errors, header
1052 * errors and read I/O errors. If any I/O errors happen, the exact pages
1053 * which are errored are marked as being bad. The goal is to enable scrub
1054 * to take those pages that are not errored from all the mirrors so that
1055 * the pages that are errored in the just handled mirror can be repaired.
1056 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001057static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1058 struct scrub_block *sblock, int is_metadata,
1059 int have_csum, u8 *csum, u64 generation,
1060 u16 csum_size)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001061{
1062 int page_num;
1063
1064 sblock->no_io_error_seen = 1;
1065 sblock->header_error = 0;
1066 sblock->checksum_error = 0;
1067
1068 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1069 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001070 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001071 DECLARE_COMPLETION_ONSTACK(complete);
1072
Stefan Behrens442a4f62012-05-25 16:06:08 +02001073 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001074 page->io_error = 1;
1075 sblock->no_io_error_seen = 0;
1076 continue;
1077 }
1078
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001079 WARN_ON(!page->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001080 bio = bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001081 if (!bio) {
1082 page->io_error = 1;
1083 sblock->no_io_error_seen = 0;
1084 continue;
1085 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001086 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001087 bio->bi_sector = page->physical >> 9;
1088 bio->bi_end_io = scrub_complete_bio_end_io;
1089 bio->bi_private = &complete;
1090
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001091 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001092 btrfsic_submit_bio(READ, bio);
1093
1094 /* this will also unplug the queue */
1095 wait_for_completion(&complete);
1096
1097 page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
1098 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1099 sblock->no_io_error_seen = 0;
1100 bio_put(bio);
1101 }
1102
1103 if (sblock->no_io_error_seen)
1104 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1105 have_csum, csum, generation,
1106 csum_size);
1107
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001108 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001109}
1110
1111static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1112 struct scrub_block *sblock,
1113 int is_metadata, int have_csum,
1114 const u8 *csum, u64 generation,
1115 u16 csum_size)
1116{
1117 int page_num;
1118 u8 calculated_csum[BTRFS_CSUM_SIZE];
1119 u32 crc = ~(u32)0;
1120 struct btrfs_root *root = fs_info->extent_root;
1121 void *mapped_buffer;
1122
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001123 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001124 if (is_metadata) {
1125 struct btrfs_header *h;
1126
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001127 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001128 h = (struct btrfs_header *)mapped_buffer;
1129
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001130 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001131 memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
1132 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001133 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001134 sblock->header_error = 1;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001135 } else if (generation != le64_to_cpu(h->generation)) {
1136 sblock->header_error = 1;
1137 sblock->generation_error = 1;
1138 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001139 csum = h->csum;
1140 } else {
1141 if (!have_csum)
1142 return;
1143
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001144 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001145 }
1146
1147 for (page_num = 0;;) {
1148 if (page_num == 0 && is_metadata)
1149 crc = btrfs_csum_data(root,
1150 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1151 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1152 else
1153 crc = btrfs_csum_data(root, mapped_buffer, crc,
1154 PAGE_SIZE);
1155
Linus Torvalds9613beb2012-03-30 12:44:29 -07001156 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001157 page_num++;
1158 if (page_num >= sblock->page_count)
1159 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001160 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001161
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001162 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001163 }
1164
1165 btrfs_csum_final(crc, calculated_csum);
1166 if (memcmp(calculated_csum, csum, csum_size))
1167 sblock->checksum_error = 1;
1168}
1169
1170static void scrub_complete_bio_end_io(struct bio *bio, int err)
1171{
1172 complete((struct completion *)bio->bi_private);
1173}
1174
1175static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1176 struct scrub_block *sblock_good,
1177 int force_write)
1178{
1179 int page_num;
1180 int ret = 0;
1181
1182 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1183 int ret_sub;
1184
1185 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1186 sblock_good,
1187 page_num,
1188 force_write);
1189 if (ret_sub)
1190 ret = ret_sub;
1191 }
1192
1193 return ret;
1194}
1195
1196static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1197 struct scrub_block *sblock_good,
1198 int page_num, int force_write)
1199{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001200 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1201 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001202
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001203 BUG_ON(page_bad->page == NULL);
1204 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001205 if (force_write || sblock_bad->header_error ||
1206 sblock_bad->checksum_error || page_bad->io_error) {
1207 struct bio *bio;
1208 int ret;
1209 DECLARE_COMPLETION_ONSTACK(complete);
1210
1211 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001212 if (!bio)
1213 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001214 bio->bi_bdev = page_bad->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001215 bio->bi_sector = page_bad->physical >> 9;
1216 bio->bi_end_io = scrub_complete_bio_end_io;
1217 bio->bi_private = &complete;
1218
1219 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1220 if (PAGE_SIZE != ret) {
1221 bio_put(bio);
1222 return -EIO;
1223 }
1224 btrfsic_submit_bio(WRITE, bio);
1225
1226 /* this will also unplug the queue */
1227 wait_for_completion(&complete);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001228 if (!bio_flagged(bio, BIO_UPTODATE)) {
1229 btrfs_dev_stat_inc_and_print(page_bad->dev,
1230 BTRFS_DEV_STAT_WRITE_ERRS);
1231 bio_put(bio);
1232 return -EIO;
1233 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001234 bio_put(bio);
1235 }
1236
1237 return 0;
1238}
1239
1240static void scrub_checksum(struct scrub_block *sblock)
1241{
1242 u64 flags;
1243 int ret;
1244
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001245 WARN_ON(sblock->page_count < 1);
1246 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001247 ret = 0;
1248 if (flags & BTRFS_EXTENT_FLAG_DATA)
1249 ret = scrub_checksum_data(sblock);
1250 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1251 ret = scrub_checksum_tree_block(sblock);
1252 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1253 (void)scrub_checksum_super(sblock);
1254 else
1255 WARN_ON(1);
1256 if (ret)
1257 scrub_handle_errored_block(sblock);
1258}
1259
1260static int scrub_checksum_data(struct scrub_block *sblock)
1261{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001262 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001263 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001264 u8 *on_disk_csum;
1265 struct page *page;
1266 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001267 u32 crc = ~(u32)0;
1268 int fail = 0;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001269 struct btrfs_root *root = sctx->dev_root;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001270 u64 len;
1271 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001272
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001273 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001274 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001275 return 0;
1276
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001277 on_disk_csum = sblock->pagev[0]->csum;
1278 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001279 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001280
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001281 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001282 index = 0;
1283 for (;;) {
1284 u64 l = min_t(u64, len, PAGE_SIZE);
1285
1286 crc = btrfs_csum_data(root, buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001287 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001288 len -= l;
1289 if (len == 0)
1290 break;
1291 index++;
1292 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001293 BUG_ON(!sblock->pagev[index]->page);
1294 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001295 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001296 }
1297
Arne Jansena2de7332011-03-08 14:14:00 +01001298 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001299 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001300 fail = 1;
1301
Arne Jansena2de7332011-03-08 14:14:00 +01001302 return fail;
1303}
1304
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001305static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001306{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001307 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001308 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001309 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001310 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001311 u8 calculated_csum[BTRFS_CSUM_SIZE];
1312 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1313 struct page *page;
1314 void *mapped_buffer;
1315 u64 mapped_size;
1316 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001317 u32 crc = ~(u32)0;
1318 int fail = 0;
1319 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001320 u64 len;
1321 int index;
1322
1323 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001324 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001325 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001326 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001327 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001328
1329 /*
1330 * we don't use the getter functions here, as we
1331 * a) don't have an extent buffer and
1332 * b) the page is already kmapped
1333 */
Arne Jansena2de7332011-03-08 14:14:00 +01001334
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001335 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr))
Arne Jansena2de7332011-03-08 14:14:00 +01001336 ++fail;
1337
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001338 if (sblock->pagev[0]->generation != le64_to_cpu(h->generation))
Arne Jansena2de7332011-03-08 14:14:00 +01001339 ++fail;
1340
1341 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1342 ++fail;
1343
1344 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1345 BTRFS_UUID_SIZE))
1346 ++fail;
1347
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001348 BUG_ON(sctx->nodesize != sctx->leafsize);
1349 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001350 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1351 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1352 index = 0;
1353 for (;;) {
1354 u64 l = min_t(u64, len, mapped_size);
1355
1356 crc = btrfs_csum_data(root, p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001357 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001358 len -= l;
1359 if (len == 0)
1360 break;
1361 index++;
1362 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001363 BUG_ON(!sblock->pagev[index]->page);
1364 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001365 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001366 mapped_size = PAGE_SIZE;
1367 p = mapped_buffer;
1368 }
1369
1370 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001371 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001372 ++crc_fail;
1373
Arne Jansena2de7332011-03-08 14:14:00 +01001374 return fail || crc_fail;
1375}
1376
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001377static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001378{
1379 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001380 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001381 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001382 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001383 u8 calculated_csum[BTRFS_CSUM_SIZE];
1384 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1385 struct page *page;
1386 void *mapped_buffer;
1387 u64 mapped_size;
1388 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001389 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001390 int fail_gen = 0;
1391 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001392 u64 len;
1393 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001394
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001395 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001396 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001397 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001398 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001399 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001400
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001401 if (sblock->pagev[0]->logical != le64_to_cpu(s->bytenr))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001402 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001403
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001404 if (sblock->pagev[0]->generation != le64_to_cpu(s->generation))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001405 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001406
1407 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001408 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001409
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001410 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1411 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1412 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1413 index = 0;
1414 for (;;) {
1415 u64 l = min_t(u64, len, mapped_size);
1416
1417 crc = btrfs_csum_data(root, p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001418 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001419 len -= l;
1420 if (len == 0)
1421 break;
1422 index++;
1423 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001424 BUG_ON(!sblock->pagev[index]->page);
1425 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001426 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001427 mapped_size = PAGE_SIZE;
1428 p = mapped_buffer;
1429 }
1430
1431 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001432 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001433 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001434
Stefan Behrens442a4f62012-05-25 16:06:08 +02001435 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001436 /*
1437 * if we find an error in a super block, we just report it.
1438 * They will get written with the next transaction commit
1439 * anyway
1440 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001441 spin_lock(&sctx->stat_lock);
1442 ++sctx->stat.super_errors;
1443 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001444 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001445 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001446 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1447 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001448 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001449 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001450 }
1451
Stefan Behrens442a4f62012-05-25 16:06:08 +02001452 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001453}
1454
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001455static void scrub_block_get(struct scrub_block *sblock)
1456{
1457 atomic_inc(&sblock->ref_count);
1458}
1459
1460static void scrub_block_put(struct scrub_block *sblock)
1461{
1462 if (atomic_dec_and_test(&sblock->ref_count)) {
1463 int i;
1464
1465 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001466 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001467 kfree(sblock);
1468 }
1469}
1470
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001471static void scrub_page_get(struct scrub_page *spage)
1472{
1473 atomic_inc(&spage->ref_count);
1474}
1475
1476static void scrub_page_put(struct scrub_page *spage)
1477{
1478 if (atomic_dec_and_test(&spage->ref_count)) {
1479 if (spage->page)
1480 __free_page(spage->page);
1481 kfree(spage);
1482 }
1483}
1484
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001485static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01001486{
1487 struct scrub_bio *sbio;
1488
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001489 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04001490 return;
Arne Jansena2de7332011-03-08 14:14:00 +01001491
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001492 sbio = sctx->bios[sctx->curr];
1493 sctx->curr = -1;
1494 atomic_inc(&sctx->in_flight);
Arne Jansena2de7332011-03-08 14:14:00 +01001495
Stefan Behrens21adbd52011-11-09 13:44:05 +01001496 btrfsic_submit_bio(READ, sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01001497}
1498
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001499static int scrub_add_page_to_bio(struct scrub_ctx *sctx,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001500 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01001501{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001502 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01001503 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001504 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01001505
1506again:
1507 /*
1508 * grab a fresh bio or wait for one to become available
1509 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001510 while (sctx->curr == -1) {
1511 spin_lock(&sctx->list_lock);
1512 sctx->curr = sctx->first_free;
1513 if (sctx->curr != -1) {
1514 sctx->first_free = sctx->bios[sctx->curr]->next_free;
1515 sctx->bios[sctx->curr]->next_free = -1;
1516 sctx->bios[sctx->curr]->page_count = 0;
1517 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001518 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001519 spin_unlock(&sctx->list_lock);
1520 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01001521 }
1522 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001523 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001524 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05001525 struct bio *bio;
1526
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001527 sbio->physical = spage->physical;
1528 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001529 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001530 bio = sbio->bio;
1531 if (!bio) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001532 bio = bio_alloc(GFP_NOFS, sctx->pages_per_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001533 if (!bio)
1534 return -ENOMEM;
1535 sbio->bio = bio;
1536 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05001537
1538 bio->bi_private = sbio;
1539 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001540 bio->bi_bdev = sbio->dev->bdev;
1541 bio->bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001542 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001543 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1544 spage->physical ||
1545 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001546 spage->logical ||
1547 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001548 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05001549 goto again;
1550 }
1551
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001552 sbio->pagev[sbio->page_count] = spage;
1553 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1554 if (ret != PAGE_SIZE) {
1555 if (sbio->page_count < 1) {
1556 bio_put(sbio->bio);
1557 sbio->bio = NULL;
1558 return -EIO;
1559 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001560 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001561 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01001562 }
Arne Jansen1bc87792011-05-28 21:57:55 +02001563
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001564 scrub_block_get(sblock); /* one for the added page */
1565 atomic_inc(&sblock->outstanding_pages);
1566 sbio->page_count++;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001567 if (sbio->page_count == sctx->pages_per_bio)
1568 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001569
1570 return 0;
1571}
1572
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001573static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001574 u64 physical, struct btrfs_device *dev, u64 flags,
1575 u64 gen, int mirror_num, u8 *csum, int force)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001576{
1577 struct scrub_block *sblock;
1578 int index;
1579
1580 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
1581 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001582 spin_lock(&sctx->stat_lock);
1583 sctx->stat.malloc_errors++;
1584 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001585 return -ENOMEM;
1586 }
1587
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001588 /* one ref inside this function, plus one for each page added to
1589 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001590 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001591 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001592 sblock->no_io_error_seen = 1;
1593
1594 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001595 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001596 u64 l = min_t(u64, len, PAGE_SIZE);
1597
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001598 spage = kzalloc(sizeof(*spage), GFP_NOFS);
1599 if (!spage) {
1600leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001601 spin_lock(&sctx->stat_lock);
1602 sctx->stat.malloc_errors++;
1603 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001604 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001605 return -ENOMEM;
1606 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001607 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
1608 scrub_page_get(spage);
1609 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001610 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001611 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001612 spage->flags = flags;
1613 spage->generation = gen;
1614 spage->logical = logical;
1615 spage->physical = physical;
1616 spage->mirror_num = mirror_num;
1617 if (csum) {
1618 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001619 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001620 } else {
1621 spage->have_csum = 0;
1622 }
1623 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001624 spage->page = alloc_page(GFP_NOFS);
1625 if (!spage->page)
1626 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001627 len -= l;
1628 logical += l;
1629 physical += l;
1630 }
1631
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001632 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001633 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001634 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001635 int ret;
1636
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001637 ret = scrub_add_page_to_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001638 if (ret) {
1639 scrub_block_put(sblock);
1640 return ret;
1641 }
1642 }
1643
1644 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001645 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001646
1647 /* last one frees, either here or in bio completion for last page */
1648 scrub_block_put(sblock);
1649 return 0;
1650}
1651
1652static void scrub_bio_end_io(struct bio *bio, int err)
1653{
1654 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001655 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001656
1657 sbio->err = err;
1658 sbio->bio = bio;
1659
1660 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
1661}
1662
1663static void scrub_bio_end_io_worker(struct btrfs_work *work)
1664{
1665 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001666 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001667 int i;
1668
1669 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_BIO);
1670 if (sbio->err) {
1671 for (i = 0; i < sbio->page_count; i++) {
1672 struct scrub_page *spage = sbio->pagev[i];
1673
1674 spage->io_error = 1;
1675 spage->sblock->no_io_error_seen = 0;
1676 }
1677 }
1678
1679 /* now complete the scrub_block items that have all pages completed */
1680 for (i = 0; i < sbio->page_count; i++) {
1681 struct scrub_page *spage = sbio->pagev[i];
1682 struct scrub_block *sblock = spage->sblock;
1683
1684 if (atomic_dec_and_test(&sblock->outstanding_pages))
1685 scrub_block_complete(sblock);
1686 scrub_block_put(sblock);
1687 }
1688
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001689 bio_put(sbio->bio);
1690 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001691 spin_lock(&sctx->list_lock);
1692 sbio->next_free = sctx->first_free;
1693 sctx->first_free = sbio->index;
1694 spin_unlock(&sctx->list_lock);
1695 atomic_dec(&sctx->in_flight);
1696 wake_up(&sctx->list_wait);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001697}
1698
1699static void scrub_block_complete(struct scrub_block *sblock)
1700{
1701 if (!sblock->no_io_error_seen)
1702 scrub_handle_errored_block(sblock);
1703 else
1704 scrub_checksum(sblock);
1705}
1706
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001707static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01001708 u8 *csum)
1709{
1710 struct btrfs_ordered_sum *sum = NULL;
1711 int ret = 0;
1712 unsigned long i;
1713 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01001714
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001715 while (!list_empty(&sctx->csum_list)) {
1716 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01001717 struct btrfs_ordered_sum, list);
1718 if (sum->bytenr > logical)
1719 return 0;
1720 if (sum->bytenr + sum->len > logical)
1721 break;
1722
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001723 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01001724 list_del(&sum->list);
1725 kfree(sum);
1726 sum = NULL;
1727 }
1728 if (!sum)
1729 return 0;
1730
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001731 num_sectors = sum->len / sctx->sectorsize;
Arne Jansena2de7332011-03-08 14:14:00 +01001732 for (i = 0; i < num_sectors; ++i) {
1733 if (sum->sums[i].bytenr == logical) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001734 memcpy(csum, &sum->sums[i].sum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001735 ret = 1;
1736 break;
1737 }
1738 }
1739 if (ret && i == num_sectors - 1) {
1740 list_del(&sum->list);
1741 kfree(sum);
1742 }
1743 return ret;
1744}
1745
1746/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001747static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001748 u64 physical, struct btrfs_device *dev, u64 flags,
1749 u64 gen, int mirror_num)
Arne Jansena2de7332011-03-08 14:14:00 +01001750{
1751 int ret;
1752 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001753 u32 blocksize;
1754
1755 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001756 blocksize = sctx->sectorsize;
1757 spin_lock(&sctx->stat_lock);
1758 sctx->stat.data_extents_scrubbed++;
1759 sctx->stat.data_bytes_scrubbed += len;
1760 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001761 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001762 BUG_ON(sctx->nodesize != sctx->leafsize);
1763 blocksize = sctx->nodesize;
1764 spin_lock(&sctx->stat_lock);
1765 sctx->stat.tree_extents_scrubbed++;
1766 sctx->stat.tree_bytes_scrubbed += len;
1767 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001768 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001769 blocksize = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001770 BUG_ON(1);
1771 }
Arne Jansena2de7332011-03-08 14:14:00 +01001772
1773 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001774 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01001775 int have_csum = 0;
1776
1777 if (flags & BTRFS_EXTENT_FLAG_DATA) {
1778 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001779 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01001780 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001781 ++sctx->stat.no_csum;
Arne Jansena2de7332011-03-08 14:14:00 +01001782 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001783 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001784 mirror_num, have_csum ? csum : NULL, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01001785 if (ret)
1786 return ret;
1787 len -= l;
1788 logical += l;
1789 physical += l;
1790 }
1791 return 0;
1792}
1793
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001794static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001795 struct map_lookup *map,
1796 struct btrfs_device *scrub_dev,
1797 int num, u64 base, u64 length)
Arne Jansena2de7332011-03-08 14:14:00 +01001798{
1799 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001800 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01001801 struct btrfs_root *root = fs_info->extent_root;
1802 struct btrfs_root *csum_root = fs_info->csum_root;
1803 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00001804 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01001805 u64 flags;
1806 int ret;
1807 int slot;
1808 int i;
1809 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01001810 struct extent_buffer *l;
1811 struct btrfs_key key;
1812 u64 physical;
1813 u64 logical;
1814 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02001815 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02001816 struct reada_control *reada1;
1817 struct reada_control *reada2;
1818 struct btrfs_key key_start;
1819 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01001820 u64 increment = map->stripe_len;
1821 u64 offset;
1822
1823 nstripes = length;
1824 offset = 0;
1825 do_div(nstripes, map->stripe_len);
1826 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1827 offset = map->stripe_len * num;
1828 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02001829 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001830 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1831 int factor = map->num_stripes / map->sub_stripes;
1832 offset = map->stripe_len * (num / map->sub_stripes);
1833 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02001834 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001835 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1836 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02001837 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001838 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1839 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02001840 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001841 } else {
1842 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02001843 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001844 }
1845
1846 path = btrfs_alloc_path();
1847 if (!path)
1848 return -ENOMEM;
1849
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001850 /*
1851 * work on commit root. The related disk blocks are static as
1852 * long as COW is applied. This means, it is save to rewrite
1853 * them to repair disk errors without any race conditions
1854 */
Arne Jansena2de7332011-03-08 14:14:00 +01001855 path->search_commit_root = 1;
1856 path->skip_locking = 1;
1857
1858 /*
Arne Jansen7a262852011-06-10 12:39:23 +02001859 * trigger the readahead for extent tree csum tree and wait for
1860 * completion. During readahead, the scrub is officially paused
1861 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01001862 */
1863 logical = base + offset;
Arne Jansena2de7332011-03-08 14:14:00 +01001864
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001865 wait_event(sctx->list_wait,
1866 atomic_read(&sctx->in_flight) == 0);
Arne Jansen7a262852011-06-10 12:39:23 +02001867 atomic_inc(&fs_info->scrubs_paused);
1868 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01001869
Arne Jansen7a262852011-06-10 12:39:23 +02001870 /* FIXME it might be better to start readahead at commit root */
1871 key_start.objectid = logical;
1872 key_start.type = BTRFS_EXTENT_ITEM_KEY;
1873 key_start.offset = (u64)0;
1874 key_end.objectid = base + offset + nstripes * increment;
1875 key_end.type = BTRFS_EXTENT_ITEM_KEY;
1876 key_end.offset = (u64)0;
1877 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01001878
Arne Jansen7a262852011-06-10 12:39:23 +02001879 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1880 key_start.type = BTRFS_EXTENT_CSUM_KEY;
1881 key_start.offset = logical;
1882 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1883 key_end.type = BTRFS_EXTENT_CSUM_KEY;
1884 key_end.offset = base + offset + nstripes * increment;
1885 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01001886
Arne Jansen7a262852011-06-10 12:39:23 +02001887 if (!IS_ERR(reada1))
1888 btrfs_reada_wait(reada1);
1889 if (!IS_ERR(reada2))
1890 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01001891
Arne Jansen7a262852011-06-10 12:39:23 +02001892 mutex_lock(&fs_info->scrub_lock);
1893 while (atomic_read(&fs_info->scrub_pause_req)) {
1894 mutex_unlock(&fs_info->scrub_lock);
1895 wait_event(fs_info->scrub_pause_wait,
1896 atomic_read(&fs_info->scrub_pause_req) == 0);
1897 mutex_lock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001898 }
Arne Jansen7a262852011-06-10 12:39:23 +02001899 atomic_dec(&fs_info->scrubs_paused);
1900 mutex_unlock(&fs_info->scrub_lock);
1901 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01001902
1903 /*
1904 * collect all data csums for the stripe to avoid seeking during
1905 * the scrub. This might currently (crc32) end up to be about 1MB
1906 */
Arne Jansene7786c32011-05-28 20:58:38 +00001907 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01001908
Arne Jansena2de7332011-03-08 14:14:00 +01001909 /*
1910 * now find all extents for each stripe and scrub them
1911 */
Arne Jansen7a262852011-06-10 12:39:23 +02001912 logical = base + offset;
1913 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01001914 ret = 0;
Arne Jansen7a262852011-06-10 12:39:23 +02001915 for (i = 0; i < nstripes; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +01001916 /*
1917 * canceled?
1918 */
1919 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001920 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01001921 ret = -ECANCELED;
1922 goto out;
1923 }
1924 /*
1925 * check to see if we have to pause
1926 */
1927 if (atomic_read(&fs_info->scrub_pause_req)) {
1928 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001929 scrub_submit(sctx);
1930 wait_event(sctx->list_wait,
1931 atomic_read(&sctx->in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01001932 atomic_inc(&fs_info->scrubs_paused);
1933 wake_up(&fs_info->scrub_pause_wait);
1934 mutex_lock(&fs_info->scrub_lock);
1935 while (atomic_read(&fs_info->scrub_pause_req)) {
1936 mutex_unlock(&fs_info->scrub_lock);
1937 wait_event(fs_info->scrub_pause_wait,
1938 atomic_read(&fs_info->scrub_pause_req) == 0);
1939 mutex_lock(&fs_info->scrub_lock);
1940 }
1941 atomic_dec(&fs_info->scrubs_paused);
1942 mutex_unlock(&fs_info->scrub_lock);
1943 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01001944 }
1945
Arne Jansen7a262852011-06-10 12:39:23 +02001946 ret = btrfs_lookup_csums_range(csum_root, logical,
1947 logical + map->stripe_len - 1,
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001948 &sctx->csum_list, 1);
Arne Jansen7a262852011-06-10 12:39:23 +02001949 if (ret)
1950 goto out;
1951
Arne Jansena2de7332011-03-08 14:14:00 +01001952 key.objectid = logical;
1953 key.type = BTRFS_EXTENT_ITEM_KEY;
1954 key.offset = (u64)0;
1955
1956 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1957 if (ret < 0)
1958 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02001959 if (ret > 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01001960 ret = btrfs_previous_item(root, path, 0,
1961 BTRFS_EXTENT_ITEM_KEY);
1962 if (ret < 0)
1963 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02001964 if (ret > 0) {
1965 /* there's no smaller item, so stick with the
1966 * larger one */
1967 btrfs_release_path(path);
1968 ret = btrfs_search_slot(NULL, root, &key,
1969 path, 0, 0);
1970 if (ret < 0)
1971 goto out;
1972 }
Arne Jansena2de7332011-03-08 14:14:00 +01001973 }
1974
1975 while (1) {
1976 l = path->nodes[0];
1977 slot = path->slots[0];
1978 if (slot >= btrfs_header_nritems(l)) {
1979 ret = btrfs_next_leaf(root, path);
1980 if (ret == 0)
1981 continue;
1982 if (ret < 0)
1983 goto out;
1984
1985 break;
1986 }
1987 btrfs_item_key_to_cpu(l, &key, slot);
1988
1989 if (key.objectid + key.offset <= logical)
1990 goto next;
1991
1992 if (key.objectid >= logical + map->stripe_len)
1993 break;
1994
1995 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1996 goto next;
1997
1998 extent = btrfs_item_ptr(l, slot,
1999 struct btrfs_extent_item);
2000 flags = btrfs_extent_flags(l, extent);
2001 generation = btrfs_extent_generation(l, extent);
2002
2003 if (key.objectid < logical &&
2004 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2005 printk(KERN_ERR
2006 "btrfs scrub: tree block %llu spanning "
2007 "stripes, ignored. logical=%llu\n",
2008 (unsigned long long)key.objectid,
2009 (unsigned long long)logical);
2010 goto next;
2011 }
2012
2013 /*
2014 * trim extent to this stripe
2015 */
2016 if (key.objectid < logical) {
2017 key.offset -= logical - key.objectid;
2018 key.objectid = logical;
2019 }
2020 if (key.objectid + key.offset >
2021 logical + map->stripe_len) {
2022 key.offset = logical + map->stripe_len -
2023 key.objectid;
2024 }
2025
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002026 ret = scrub_extent(sctx, key.objectid, key.offset,
Arne Jansena2de7332011-03-08 14:14:00 +01002027 key.objectid - logical + physical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002028 scrub_dev, flags, generation,
2029 mirror_num);
Arne Jansena2de7332011-03-08 14:14:00 +01002030 if (ret)
2031 goto out;
2032
2033next:
2034 path->slots[0]++;
2035 }
Chris Mason71267332011-05-23 06:30:52 -04002036 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002037 logical += increment;
2038 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002039 spin_lock(&sctx->stat_lock);
2040 sctx->stat.last_physical = physical;
2041 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002042 }
2043 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002044 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002045
2046out:
Arne Jansene7786c32011-05-28 20:58:38 +00002047 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002048 btrfs_free_path(path);
2049 return ret < 0 ? ret : 0;
2050}
2051
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002052static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002053 struct btrfs_device *scrub_dev,
2054 u64 chunk_tree, u64 chunk_objectid,
2055 u64 chunk_offset, u64 length,
2056 u64 dev_offset)
Arne Jansena2de7332011-03-08 14:14:00 +01002057{
2058 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002059 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01002060 struct map_lookup *map;
2061 struct extent_map *em;
2062 int i;
2063 int ret = -EINVAL;
2064
2065 read_lock(&map_tree->map_tree.lock);
2066 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2067 read_unlock(&map_tree->map_tree.lock);
2068
2069 if (!em)
2070 return -EINVAL;
2071
2072 map = (struct map_lookup *)em->bdev;
2073 if (em->start != chunk_offset)
2074 goto out;
2075
2076 if (em->len < length)
2077 goto out;
2078
2079 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002080 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01002081 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002082 ret = scrub_stripe(sctx, map, scrub_dev, i,
2083 chunk_offset, length);
Arne Jansena2de7332011-03-08 14:14:00 +01002084 if (ret)
2085 goto out;
2086 }
2087 }
2088out:
2089 free_extent_map(em);
2090
2091 return ret;
2092}
2093
2094static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002095int scrub_enumerate_chunks(struct scrub_ctx *sctx,
2096 struct btrfs_device *scrub_dev, u64 start, u64 end)
Arne Jansena2de7332011-03-08 14:14:00 +01002097{
2098 struct btrfs_dev_extent *dev_extent = NULL;
2099 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002100 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002101 struct btrfs_fs_info *fs_info = root->fs_info;
2102 u64 length;
2103 u64 chunk_tree;
2104 u64 chunk_objectid;
2105 u64 chunk_offset;
2106 int ret;
2107 int slot;
2108 struct extent_buffer *l;
2109 struct btrfs_key key;
2110 struct btrfs_key found_key;
2111 struct btrfs_block_group_cache *cache;
2112
2113 path = btrfs_alloc_path();
2114 if (!path)
2115 return -ENOMEM;
2116
2117 path->reada = 2;
2118 path->search_commit_root = 1;
2119 path->skip_locking = 1;
2120
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002121 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01002122 key.offset = 0ull;
2123 key.type = BTRFS_DEV_EXTENT_KEY;
2124
Arne Jansena2de7332011-03-08 14:14:00 +01002125 while (1) {
2126 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2127 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02002128 break;
2129 if (ret > 0) {
2130 if (path->slots[0] >=
2131 btrfs_header_nritems(path->nodes[0])) {
2132 ret = btrfs_next_leaf(root, path);
2133 if (ret)
2134 break;
2135 }
2136 }
Arne Jansena2de7332011-03-08 14:14:00 +01002137
2138 l = path->nodes[0];
2139 slot = path->slots[0];
2140
2141 btrfs_item_key_to_cpu(l, &found_key, slot);
2142
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002143 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01002144 break;
2145
Arne Jansen8c510322011-06-03 10:09:26 +02002146 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01002147 break;
2148
2149 if (found_key.offset >= end)
2150 break;
2151
2152 if (found_key.offset < key.offset)
2153 break;
2154
2155 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2156 length = btrfs_dev_extent_length(l, dev_extent);
2157
2158 if (found_key.offset + length <= start) {
2159 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002160 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002161 continue;
2162 }
2163
2164 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2165 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2166 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2167
2168 /*
2169 * get a reference on the corresponding block group to prevent
2170 * the chunk from going away while we scrub it
2171 */
2172 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2173 if (!cache) {
2174 ret = -ENOENT;
Arne Jansen8c510322011-06-03 10:09:26 +02002175 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002176 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002177 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Arne Jansen859acaf2012-02-09 15:09:02 +01002178 chunk_offset, length, found_key.offset);
Arne Jansena2de7332011-03-08 14:14:00 +01002179 btrfs_put_block_group(cache);
2180 if (ret)
2181 break;
2182
2183 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002184 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002185 }
2186
Arne Jansena2de7332011-03-08 14:14:00 +01002187 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02002188
2189 /*
2190 * ret can still be 1 from search_slot or next_leaf,
2191 * that's not an error
2192 */
2193 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002194}
2195
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002196static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2197 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01002198{
2199 int i;
2200 u64 bytenr;
2201 u64 gen;
2202 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002203 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002204
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002205 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
2206 return -EIO;
2207
Arne Jansena2de7332011-03-08 14:14:00 +01002208 gen = root->fs_info->last_trans_committed;
2209
2210 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2211 bytenr = btrfs_sb_offset(i);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002212 if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01002213 break;
2214
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002215 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002216 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
2217 NULL, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002218 if (ret)
2219 return ret;
2220 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002221 wait_event(sctx->list_wait, atomic_read(&sctx->in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002222
2223 return 0;
2224}
2225
2226/*
2227 * get a reference count on fs_info->scrub_workers. start worker if necessary
2228 */
2229static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
2230{
2231 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002232 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002233
2234 mutex_lock(&fs_info->scrub_lock);
Arne Jansen632dd772011-06-10 12:07:07 +02002235 if (fs_info->scrub_workers_refcnt == 0) {
2236 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
2237 fs_info->thread_pool_size, &fs_info->generic_worker);
2238 fs_info->scrub_workers.idle_thresh = 4;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002239 ret = btrfs_start_workers(&fs_info->scrub_workers);
2240 if (ret)
2241 goto out;
Arne Jansen632dd772011-06-10 12:07:07 +02002242 }
Arne Jansena2de7332011-03-08 14:14:00 +01002243 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002244out:
Arne Jansena2de7332011-03-08 14:14:00 +01002245 mutex_unlock(&fs_info->scrub_lock);
2246
Josef Bacik0dc3b842011-11-18 14:37:27 -05002247 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002248}
2249
2250static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
2251{
2252 struct btrfs_fs_info *fs_info = root->fs_info;
2253
2254 mutex_lock(&fs_info->scrub_lock);
2255 if (--fs_info->scrub_workers_refcnt == 0)
2256 btrfs_stop_workers(&fs_info->scrub_workers);
2257 WARN_ON(fs_info->scrub_workers_refcnt < 0);
2258 mutex_unlock(&fs_info->scrub_lock);
2259}
2260
2261
2262int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
Arne Jansen86287642011-03-23 16:34:19 +01002263 struct btrfs_scrub_progress *progress, int readonly)
Arne Jansena2de7332011-03-08 14:14:00 +01002264{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002265 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002266 struct btrfs_fs_info *fs_info = root->fs_info;
2267 int ret;
2268 struct btrfs_device *dev;
2269
David Sterba7841cb22011-05-31 18:07:27 +02002270 if (btrfs_fs_closing(root->fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01002271 return -EINVAL;
2272
2273 /*
2274 * check some assumptions
2275 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002276 if (root->nodesize != root->leafsize) {
2277 printk(KERN_ERR
2278 "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
2279 root->nodesize, root->leafsize);
2280 return -EINVAL;
2281 }
2282
2283 if (root->nodesize > BTRFS_STRIPE_LEN) {
2284 /*
2285 * in this case scrub is unable to calculate the checksum
2286 * the way scrub is implemented. Do not handle this
2287 * situation at all because it won't ever happen.
2288 */
2289 printk(KERN_ERR
2290 "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
2291 root->nodesize, BTRFS_STRIPE_LEN);
2292 return -EINVAL;
2293 }
2294
2295 if (root->sectorsize != PAGE_SIZE) {
2296 /* not supported for data w/o checksums */
2297 printk(KERN_ERR
2298 "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
2299 root->sectorsize, (unsigned long long)PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01002300 return -EINVAL;
2301 }
2302
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002303 if (fs_info->chunk_root->nodesize >
2304 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
2305 fs_info->chunk_root->sectorsize >
2306 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
2307 /*
2308 * would exhaust the array bounds of pagev member in
2309 * struct scrub_block
2310 */
2311 pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
2312 fs_info->chunk_root->nodesize,
2313 SCRUB_MAX_PAGES_PER_BLOCK,
2314 fs_info->chunk_root->sectorsize,
2315 SCRUB_MAX_PAGES_PER_BLOCK);
2316 return -EINVAL;
2317 }
2318
Arne Jansena2de7332011-03-08 14:14:00 +01002319 ret = scrub_workers_get(root);
2320 if (ret)
2321 return ret;
2322
2323 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2324 dev = btrfs_find_device(root, devid, NULL, NULL);
2325 if (!dev || dev->missing) {
2326 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2327 scrub_workers_put(root);
2328 return -ENODEV;
2329 }
2330 mutex_lock(&fs_info->scrub_lock);
2331
2332 if (!dev->in_fs_metadata) {
2333 mutex_unlock(&fs_info->scrub_lock);
2334 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2335 scrub_workers_put(root);
2336 return -ENODEV;
2337 }
2338
2339 if (dev->scrub_device) {
2340 mutex_unlock(&fs_info->scrub_lock);
2341 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2342 scrub_workers_put(root);
2343 return -EINPROGRESS;
2344 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002345 sctx = scrub_setup_ctx(dev);
2346 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002347 mutex_unlock(&fs_info->scrub_lock);
2348 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2349 scrub_workers_put(root);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002350 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002351 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002352 sctx->readonly = readonly;
2353 dev->scrub_device = sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002354
2355 atomic_inc(&fs_info->scrubs_running);
2356 mutex_unlock(&fs_info->scrub_lock);
2357 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2358
2359 down_read(&fs_info->scrub_super_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002360 ret = scrub_supers(sctx, dev);
Arne Jansena2de7332011-03-08 14:14:00 +01002361 up_read(&fs_info->scrub_super_lock);
2362
2363 if (!ret)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002364 ret = scrub_enumerate_chunks(sctx, dev, start, end);
Arne Jansena2de7332011-03-08 14:14:00 +01002365
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002366 wait_event(sctx->list_wait, atomic_read(&sctx->in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002367 atomic_dec(&fs_info->scrubs_running);
2368 wake_up(&fs_info->scrub_pause_wait);
2369
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002370 wait_event(sctx->list_wait, atomic_read(&sctx->fixup_cnt) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02002371
Arne Jansena2de7332011-03-08 14:14:00 +01002372 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002373 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002374
2375 mutex_lock(&fs_info->scrub_lock);
2376 dev->scrub_device = NULL;
2377 mutex_unlock(&fs_info->scrub_lock);
2378
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002379 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002380 scrub_workers_put(root);
2381
2382 return ret;
2383}
2384
Jeff Mahoney143bede2012-03-01 14:56:26 +01002385void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002386{
2387 struct btrfs_fs_info *fs_info = root->fs_info;
2388
2389 mutex_lock(&fs_info->scrub_lock);
2390 atomic_inc(&fs_info->scrub_pause_req);
2391 while (atomic_read(&fs_info->scrubs_paused) !=
2392 atomic_read(&fs_info->scrubs_running)) {
2393 mutex_unlock(&fs_info->scrub_lock);
2394 wait_event(fs_info->scrub_pause_wait,
2395 atomic_read(&fs_info->scrubs_paused) ==
2396 atomic_read(&fs_info->scrubs_running));
2397 mutex_lock(&fs_info->scrub_lock);
2398 }
2399 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002400}
2401
Jeff Mahoney143bede2012-03-01 14:56:26 +01002402void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002403{
2404 struct btrfs_fs_info *fs_info = root->fs_info;
2405
2406 atomic_dec(&fs_info->scrub_pause_req);
2407 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002408}
2409
Jeff Mahoney143bede2012-03-01 14:56:26 +01002410void btrfs_scrub_pause_super(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002411{
2412 down_write(&root->fs_info->scrub_super_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002413}
2414
Jeff Mahoney143bede2012-03-01 14:56:26 +01002415void btrfs_scrub_continue_super(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002416{
2417 up_write(&root->fs_info->scrub_super_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002418}
2419
Jeff Mahoney49b25e02012-03-01 17:24:58 +01002420int __btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002421{
Arne Jansena2de7332011-03-08 14:14:00 +01002422
2423 mutex_lock(&fs_info->scrub_lock);
2424 if (!atomic_read(&fs_info->scrubs_running)) {
2425 mutex_unlock(&fs_info->scrub_lock);
2426 return -ENOTCONN;
2427 }
2428
2429 atomic_inc(&fs_info->scrub_cancel_req);
2430 while (atomic_read(&fs_info->scrubs_running)) {
2431 mutex_unlock(&fs_info->scrub_lock);
2432 wait_event(fs_info->scrub_pause_wait,
2433 atomic_read(&fs_info->scrubs_running) == 0);
2434 mutex_lock(&fs_info->scrub_lock);
2435 }
2436 atomic_dec(&fs_info->scrub_cancel_req);
2437 mutex_unlock(&fs_info->scrub_lock);
2438
2439 return 0;
2440}
2441
Jeff Mahoney49b25e02012-03-01 17:24:58 +01002442int btrfs_scrub_cancel(struct btrfs_root *root)
2443{
2444 return __btrfs_scrub_cancel(root->fs_info);
2445}
2446
Arne Jansena2de7332011-03-08 14:14:00 +01002447int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
2448{
2449 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002450 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002451
2452 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002453 sctx = dev->scrub_device;
2454 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01002455 mutex_unlock(&fs_info->scrub_lock);
2456 return -ENOTCONN;
2457 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002458 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01002459 while (dev->scrub_device) {
2460 mutex_unlock(&fs_info->scrub_lock);
2461 wait_event(fs_info->scrub_pause_wait,
2462 dev->scrub_device == NULL);
2463 mutex_lock(&fs_info->scrub_lock);
2464 }
2465 mutex_unlock(&fs_info->scrub_lock);
2466
2467 return 0;
2468}
Stefan Behrens1623ede2012-03-27 14:21:26 -04002469
Arne Jansena2de7332011-03-08 14:14:00 +01002470int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
2471{
2472 struct btrfs_fs_info *fs_info = root->fs_info;
2473 struct btrfs_device *dev;
2474 int ret;
2475
2476 /*
2477 * we have to hold the device_list_mutex here so the device
2478 * does not go away in cancel_dev. FIXME: find a better solution
2479 */
2480 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2481 dev = btrfs_find_device(root, devid, NULL, NULL);
2482 if (!dev) {
2483 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2484 return -ENODEV;
2485 }
2486 ret = btrfs_scrub_cancel_dev(root, dev);
2487 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2488
2489 return ret;
2490}
2491
2492int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
2493 struct btrfs_scrub_progress *progress)
2494{
2495 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002496 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01002497
2498 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2499 dev = btrfs_find_device(root, devid, NULL, NULL);
2500 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002501 sctx = dev->scrub_device;
2502 if (sctx)
2503 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002504 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2505
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002506 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01002507}