blob: 15ac82ae57708b2cd95e5e5ea7a0a7960add5939 [file] [log] [blame]
Arne Jansena2de7332011-03-08 14:14:00 +01001/*
2 * Copyright (C) 2011 STRATO. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Arne Jansena2de7332011-03-08 14:14:00 +010019#include <linux/blkdev.h>
Jan Schmidt558540c2011-06-13 19:59:12 +020020#include <linux/ratelimit.h>
Arne Jansena2de7332011-03-08 14:14:00 +010021#include "ctree.h"
22#include "volumes.h"
23#include "disk-io.h"
24#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020025#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020026#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020027#include "extent_io.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010028#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040029#include "rcu-string.h"
Arne Jansena2de7332011-03-08 14:14:00 +010030
31/*
32 * This is only the first step towards a full-features scrub. It reads all
33 * extent and super block and verifies the checksums. In case a bad checksum
34 * is found or the extent cannot be read, good data will be written back if
35 * any can be found.
36 *
37 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010038 * - In case an unrepairable extent is encountered, track which files are
39 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010040 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010041 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010042 */
43
Stefan Behrensb5d67f62012-03-27 14:21:27 -040044struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010045struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010046
47#define SCRUB_PAGES_PER_BIO 16 /* 64k per bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +010048#define SCRUB_BIOS_PER_CTX 16 /* 1 MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010049
50/*
51 * the following value times PAGE_SIZE needs to be large enough to match the
52 * largest node/leaf/sector size that shall be supported.
53 * Values larger than BTRFS_STRIPE_LEN are not supported.
54 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040055#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010056
57struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040058 struct scrub_block *sblock;
59 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020060 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010061 u64 flags; /* extent flags */
62 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040063 u64 logical;
64 u64 physical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +010065 atomic_t ref_count;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040066 struct {
67 unsigned int mirror_num:8;
68 unsigned int have_csum:1;
69 unsigned int io_error:1;
70 };
Arne Jansena2de7332011-03-08 14:14:00 +010071 u8 csum[BTRFS_CSUM_SIZE];
72};
73
74struct scrub_bio {
75 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010076 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010077 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010078 struct bio *bio;
79 int err;
80 u64 logical;
81 u64 physical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040082 struct scrub_page *pagev[SCRUB_PAGES_PER_BIO];
83 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +010084 int next_free;
85 struct btrfs_work work;
86};
87
Stefan Behrensb5d67f62012-03-27 14:21:27 -040088struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +010089 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -040090 int page_count;
91 atomic_t outstanding_pages;
92 atomic_t ref_count; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +010093 struct scrub_ctx *sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040094 struct {
95 unsigned int header_error:1;
96 unsigned int checksum_error:1;
97 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +020098 unsigned int generation_error:1; /* also sets header_error */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040099 };
100};
101
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100102struct scrub_ctx {
103 struct scrub_bio *bios[SCRUB_BIOS_PER_CTX];
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100104 struct btrfs_root *dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +0100105 int first_free;
106 int curr;
107 atomic_t in_flight;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200108 atomic_t fixup_cnt;
Arne Jansena2de7332011-03-08 14:14:00 +0100109 spinlock_t list_lock;
110 wait_queue_head_t list_wait;
111 u16 csum_size;
112 struct list_head csum_list;
113 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100114 int readonly;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400115 int pages_per_bio; /* <= SCRUB_PAGES_PER_BIO */
116 u32 sectorsize;
117 u32 nodesize;
118 u32 leafsize;
Arne Jansena2de7332011-03-08 14:14:00 +0100119 /*
120 * statistics
121 */
122 struct btrfs_scrub_progress stat;
123 spinlock_t stat_lock;
124};
125
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200126struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100127 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100128 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200129 u64 logical;
130 struct btrfs_root *root;
131 struct btrfs_work work;
132 int mirror_num;
133};
134
Jan Schmidt558540c2011-06-13 19:59:12 +0200135struct scrub_warning {
136 struct btrfs_path *path;
137 u64 extent_item_size;
138 char *scratch_buf;
139 char *msg_buf;
140 const char *errstr;
141 sector_t sector;
142 u64 logical;
143 struct btrfs_device *dev;
144 int msg_bufsize;
145 int scratch_bufsize;
146};
147
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400148
149static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100150static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400151 struct btrfs_mapping_tree *map_tree,
152 u64 length, u64 logical,
153 struct scrub_block *sblock);
154static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
155 struct scrub_block *sblock, int is_metadata,
156 int have_csum, u8 *csum, u64 generation,
157 u16 csum_size);
158static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
159 struct scrub_block *sblock,
160 int is_metadata, int have_csum,
161 const u8 *csum, u64 generation,
162 u16 csum_size);
163static void scrub_complete_bio_end_io(struct bio *bio, int err);
164static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
165 struct scrub_block *sblock_good,
166 int force_write);
167static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
168 struct scrub_block *sblock_good,
169 int page_num, int force_write);
170static int scrub_checksum_data(struct scrub_block *sblock);
171static int scrub_checksum_tree_block(struct scrub_block *sblock);
172static int scrub_checksum_super(struct scrub_block *sblock);
173static void scrub_block_get(struct scrub_block *sblock);
174static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100175static void scrub_page_get(struct scrub_page *spage);
176static void scrub_page_put(struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100177static int scrub_add_page_to_bio(struct scrub_ctx *sctx,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400178 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100179static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100180 u64 physical, struct btrfs_device *dev, u64 flags,
181 u64 gen, int mirror_num, u8 *csum, int force);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400182static void scrub_bio_end_io(struct bio *bio, int err);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400183static void scrub_bio_end_io_worker(struct btrfs_work *work);
184static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400185
186
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100187static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100188{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100189 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100190 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100191 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100192 struct btrfs_ordered_sum, list);
193 list_del(&sum->list);
194 kfree(sum);
195 }
196}
197
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100198static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100199{
200 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100201
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100202 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100203 return;
204
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400205 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100206 if (sctx->curr != -1) {
207 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400208
209 for (i = 0; i < sbio->page_count; i++) {
210 BUG_ON(!sbio->pagev[i]);
211 BUG_ON(!sbio->pagev[i]->page);
212 scrub_block_put(sbio->pagev[i]->sblock);
213 }
214 bio_put(sbio->bio);
215 }
216
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100217 for (i = 0; i < SCRUB_BIOS_PER_CTX; ++i) {
218 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100219
220 if (!sbio)
221 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100222 kfree(sbio);
223 }
224
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100225 scrub_free_csums(sctx);
226 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100227}
228
229static noinline_for_stack
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100230struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev)
Arne Jansena2de7332011-03-08 14:14:00 +0100231{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100232 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100233 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100234 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400235 int pages_per_bio;
Arne Jansena2de7332011-03-08 14:14:00 +0100236
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400237 pages_per_bio = min_t(int, SCRUB_PAGES_PER_BIO,
238 bio_get_nr_vecs(dev->bdev));
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100239 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
240 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100241 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100242 sctx->pages_per_bio = pages_per_bio;
243 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100244 sctx->dev_root = dev->dev_root;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100245 for (i = 0; i < SCRUB_BIOS_PER_CTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100246 struct scrub_bio *sbio;
247
248 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
249 if (!sbio)
250 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100251 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100252
Arne Jansena2de7332011-03-08 14:14:00 +0100253 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100254 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400255 sbio->page_count = 0;
256 sbio->work.func = scrub_bio_end_io_worker;
Arne Jansena2de7332011-03-08 14:14:00 +0100257
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100258 if (i != SCRUB_BIOS_PER_CTX - 1)
259 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200260 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100261 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100262 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100263 sctx->first_free = 0;
264 sctx->nodesize = dev->dev_root->nodesize;
265 sctx->leafsize = dev->dev_root->leafsize;
266 sctx->sectorsize = dev->dev_root->sectorsize;
267 atomic_set(&sctx->in_flight, 0);
268 atomic_set(&sctx->fixup_cnt, 0);
269 atomic_set(&sctx->cancel_req, 0);
270 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
271 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100272
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100273 spin_lock_init(&sctx->list_lock);
274 spin_lock_init(&sctx->stat_lock);
275 init_waitqueue_head(&sctx->list_wait);
276 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100277
278nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100279 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100280 return ERR_PTR(-ENOMEM);
281}
282
Jan Schmidt558540c2011-06-13 19:59:12 +0200283static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, void *ctx)
284{
285 u64 isize;
286 u32 nlink;
287 int ret;
288 int i;
289 struct extent_buffer *eb;
290 struct btrfs_inode_item *inode_item;
291 struct scrub_warning *swarn = ctx;
292 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
293 struct inode_fs_paths *ipath = NULL;
294 struct btrfs_root *local_root;
295 struct btrfs_key root_key;
296
297 root_key.objectid = root;
298 root_key.type = BTRFS_ROOT_ITEM_KEY;
299 root_key.offset = (u64)-1;
300 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
301 if (IS_ERR(local_root)) {
302 ret = PTR_ERR(local_root);
303 goto err;
304 }
305
306 ret = inode_item_info(inum, 0, local_root, swarn->path);
307 if (ret) {
308 btrfs_release_path(swarn->path);
309 goto err;
310 }
311
312 eb = swarn->path->nodes[0];
313 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
314 struct btrfs_inode_item);
315 isize = btrfs_inode_size(eb, inode_item);
316 nlink = btrfs_inode_nlink(eb, inode_item);
317 btrfs_release_path(swarn->path);
318
319 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300320 if (IS_ERR(ipath)) {
321 ret = PTR_ERR(ipath);
322 ipath = NULL;
323 goto err;
324 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200325 ret = paths_from_inode(inum, ipath);
326
327 if (ret < 0)
328 goto err;
329
330 /*
331 * we deliberately ignore the bit ipath might have been too small to
332 * hold all of the paths here
333 */
334 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Josef Bacik606686e2012-06-04 14:03:51 -0400335 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200336 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
337 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400338 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200339 (unsigned long long)swarn->sector, root, inum, offset,
340 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500341 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200342
343 free_ipath(ipath);
344 return 0;
345
346err:
Josef Bacik606686e2012-06-04 14:03:51 -0400347 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200348 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
349 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400350 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200351 (unsigned long long)swarn->sector, root, inum, offset, ret);
352
353 free_ipath(ipath);
354 return 0;
355}
356
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400357static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200358{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100359 struct btrfs_device *dev;
360 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200361 struct btrfs_path *path;
362 struct btrfs_key found_key;
363 struct extent_buffer *eb;
364 struct btrfs_extent_item *ei;
365 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200366 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100367 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600368 u64 flags = 0;
369 u64 ref_root;
370 u32 item_size;
371 u8 ref_level;
372 const int bufsize = 4096;
373 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200374
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100375 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100376 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100377 fs_info = sblock->sctx->dev_root->fs_info;
378
Jan Schmidt558540c2011-06-13 19:59:12 +0200379 path = btrfs_alloc_path();
380
381 swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
382 swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100383 swarn.sector = (sblock->pagev[0]->physical) >> 9;
384 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200385 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100386 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200387 swarn.msg_bufsize = bufsize;
388 swarn.scratch_bufsize = bufsize;
389
390 if (!path || !swarn.scratch_buf || !swarn.msg_buf)
391 goto out;
392
Liu Bo69917e42012-09-07 20:01:28 -0600393 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
394 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200395 if (ret < 0)
396 goto out;
397
Jan Schmidt4692cf52011-12-02 14:56:41 +0100398 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200399 swarn.extent_item_size = found_key.offset;
400
401 eb = path->nodes[0];
402 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
403 item_size = btrfs_item_size_nr(eb, path->slots[0]);
Jan Schmidt4692cf52011-12-02 14:56:41 +0100404 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200405
Liu Bo69917e42012-09-07 20:01:28 -0600406 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200407 do {
408 ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
409 &ref_root, &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400410 printk_in_rcu(KERN_WARNING
Stefan Behrens1623ede2012-03-27 14:21:26 -0400411 "btrfs: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200412 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400413 "%llu\n", errstr, swarn.logical,
414 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200415 (unsigned long long)swarn.sector,
416 ref_level ? "node" : "leaf",
417 ret < 0 ? -1 : ref_level,
418 ret < 0 ? -1 : ref_root);
419 } while (ret != 1);
420 } else {
421 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100422 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100423 iterate_extent_inodes(fs_info, found_key.objectid,
424 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200425 scrub_print_warning_inode, &swarn);
426 }
427
428out:
429 btrfs_free_path(path);
430 kfree(swarn.scratch_buf);
431 kfree(swarn.msg_buf);
432}
433
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200434static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *ctx)
435{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200436 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200437 unsigned long index;
438 struct scrub_fixup_nodatasum *fixup = ctx;
439 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200440 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200441 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200442 struct inode *inode = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200443 u64 end = offset + PAGE_SIZE - 1;
444 struct btrfs_root *local_root;
445
446 key.objectid = root;
447 key.type = BTRFS_ROOT_ITEM_KEY;
448 key.offset = (u64)-1;
449 local_root = btrfs_read_fs_root_no_name(fixup->root->fs_info, &key);
450 if (IS_ERR(local_root))
451 return PTR_ERR(local_root);
452
453 key.type = BTRFS_INODE_ITEM_KEY;
454 key.objectid = inum;
455 key.offset = 0;
456 inode = btrfs_iget(fixup->root->fs_info->sb, &key, local_root, NULL);
457 if (IS_ERR(inode))
458 return PTR_ERR(inode);
459
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200460 index = offset >> PAGE_CACHE_SHIFT;
461
462 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200463 if (!page) {
464 ret = -ENOMEM;
465 goto out;
466 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200467
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200468 if (PageUptodate(page)) {
469 struct btrfs_mapping_tree *map_tree;
470 if (PageDirty(page)) {
471 /*
472 * we need to write the data to the defect sector. the
473 * data that was in that sector is not in memory,
474 * because the page was modified. we must not write the
475 * modified page to that sector.
476 *
477 * TODO: what could be done here: wait for the delalloc
478 * runner to write out that page (might involve
479 * COW) and see whether the sector is still
480 * referenced afterwards.
481 *
482 * For the meantime, we'll treat this error
483 * incorrectable, although there is a chance that a
484 * later scrub will find the bad sector again and that
485 * there's no dirty page in memory, then.
486 */
487 ret = -EIO;
488 goto out;
489 }
490 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
491 ret = repair_io_failure(map_tree, offset, PAGE_SIZE,
492 fixup->logical, page,
493 fixup->mirror_num);
494 unlock_page(page);
495 corrected = !ret;
496 } else {
497 /*
498 * we need to get good data first. the general readpage path
499 * will call repair_io_failure for us, we just have to make
500 * sure we read the bad mirror.
501 */
502 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
503 EXTENT_DAMAGED, GFP_NOFS);
504 if (ret) {
505 /* set_extent_bits should give proper error */
506 WARN_ON(ret > 0);
507 if (ret > 0)
508 ret = -EFAULT;
509 goto out;
510 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200511
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200512 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
513 btrfs_get_extent,
514 fixup->mirror_num);
515 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200516
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200517 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
518 end, EXTENT_DAMAGED, 0, NULL);
519 if (!corrected)
520 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
521 EXTENT_DAMAGED, GFP_NOFS);
522 }
523
524out:
525 if (page)
526 put_page(page);
527 if (inode)
528 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200529
530 if (ret < 0)
531 return ret;
532
533 if (ret == 0 && corrected) {
534 /*
535 * we only need to call readpage for one of the inodes belonging
536 * to this extent. so make iterate_extent_inodes stop
537 */
538 return 1;
539 }
540
541 return -EIO;
542}
543
544static void scrub_fixup_nodatasum(struct btrfs_work *work)
545{
546 int ret;
547 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100548 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200549 struct btrfs_trans_handle *trans = NULL;
550 struct btrfs_fs_info *fs_info;
551 struct btrfs_path *path;
552 int uncorrectable = 0;
553
554 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100555 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200556 fs_info = fixup->root->fs_info;
557
558 path = btrfs_alloc_path();
559 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100560 spin_lock(&sctx->stat_lock);
561 ++sctx->stat.malloc_errors;
562 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200563 uncorrectable = 1;
564 goto out;
565 }
566
567 trans = btrfs_join_transaction(fixup->root);
568 if (IS_ERR(trans)) {
569 uncorrectable = 1;
570 goto out;
571 }
572
573 /*
574 * the idea is to trigger a regular read through the standard path. we
575 * read a page from the (failed) logical address by specifying the
576 * corresponding copynum of the failed sector. thus, that readpage is
577 * expected to fail.
578 * that is the point where on-the-fly error correction will kick in
579 * (once it's finished) and rewrite the failed sector if a good copy
580 * can be found.
581 */
582 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
583 path, scrub_fixup_readpage,
584 fixup);
585 if (ret < 0) {
586 uncorrectable = 1;
587 goto out;
588 }
589 WARN_ON(ret != 1);
590
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100591 spin_lock(&sctx->stat_lock);
592 ++sctx->stat.corrected_errors;
593 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200594
595out:
596 if (trans && !IS_ERR(trans))
597 btrfs_end_transaction(trans, fixup->root);
598 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100599 spin_lock(&sctx->stat_lock);
600 ++sctx->stat.uncorrectable_errors;
601 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400602
603 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400604 "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -0400605 (unsigned long long)fixup->logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100606 rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200607 }
608
609 btrfs_free_path(path);
610 kfree(fixup);
611
612 /* see caller why we're pretending to be paused in the scrub counters */
613 mutex_lock(&fs_info->scrub_lock);
614 atomic_dec(&fs_info->scrubs_running);
615 atomic_dec(&fs_info->scrubs_paused);
616 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100617 atomic_dec(&sctx->fixup_cnt);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200618 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100619 wake_up(&sctx->list_wait);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200620}
621
Arne Jansena2de7332011-03-08 14:14:00 +0100622/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400623 * scrub_handle_errored_block gets called when either verification of the
624 * pages failed or the bio failed to read, e.g. with EIO. In the latter
625 * case, this function handles all pages in the bio, even though only one
626 * may be bad.
627 * The goal of this function is to repair the errored block by using the
628 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100629 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400630static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100631{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100632 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100633 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400634 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100635 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400636 u64 logical;
637 u64 generation;
638 unsigned int failed_mirror_index;
639 unsigned int is_metadata;
640 unsigned int have_csum;
641 u8 *csum;
642 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
643 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100644 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400645 int mirror_index;
646 int page_num;
647 int success;
648 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
649 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100650
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400651 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100652 fs_info = sctx->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400653 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100654 logical = sblock_to_check->pagev[0]->logical;
655 generation = sblock_to_check->pagev[0]->generation;
656 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
657 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
658 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400659 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100660 have_csum = sblock_to_check->pagev[0]->have_csum;
661 csum = sblock_to_check->pagev[0]->csum;
662 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400663
664 /*
665 * read all mirrors one after the other. This includes to
666 * re-read the extent or metadata block that failed (that was
667 * the cause that this fixup code is called) another time,
668 * page by page this time in order to know which pages
669 * caused I/O errors and which ones are good (for all mirrors).
670 * It is the goal to handle the situation when more than one
671 * mirror contains I/O errors, but the errors do not
672 * overlap, i.e. the data can be repaired by selecting the
673 * pages from those mirrors without I/O error on the
674 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
675 * would be that mirror #1 has an I/O error on the first page,
676 * the second page is good, and mirror #2 has an I/O error on
677 * the second page, but the first page is good.
678 * Then the first page of the first mirror can be repaired by
679 * taking the first page of the second mirror, and the
680 * second page of the second mirror can be repaired by
681 * copying the contents of the 2nd page of the 1st mirror.
682 * One more note: if the pages of one mirror contain I/O
683 * errors, the checksum cannot be verified. In order to get
684 * the best data for repairing, the first attempt is to find
685 * a mirror without I/O errors and with a validated checksum.
686 * Only if this is not possible, the pages are picked from
687 * mirrors with I/O errors without considering the checksum.
688 * If the latter is the case, at the end, the checksum of the
689 * repaired area is verified in order to correctly maintain
690 * the statistics.
691 */
692
693 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
694 sizeof(*sblocks_for_recheck),
695 GFP_NOFS);
696 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100697 spin_lock(&sctx->stat_lock);
698 sctx->stat.malloc_errors++;
699 sctx->stat.read_errors++;
700 sctx->stat.uncorrectable_errors++;
701 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100702 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400703 goto out;
704 }
705
706 /* setup the context, map the logical blocks and alloc the pages */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100707 ret = scrub_setup_recheck_block(sctx, &fs_info->mapping_tree, length,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400708 logical, sblocks_for_recheck);
709 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100710 spin_lock(&sctx->stat_lock);
711 sctx->stat.read_errors++;
712 sctx->stat.uncorrectable_errors++;
713 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100714 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400715 goto out;
716 }
717 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
718 sblock_bad = sblocks_for_recheck + failed_mirror_index;
719
720 /* build and submit the bios for the failed mirror, check checksums */
721 ret = scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100722 csum, generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400723 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100724 spin_lock(&sctx->stat_lock);
725 sctx->stat.read_errors++;
726 sctx->stat.uncorrectable_errors++;
727 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100728 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400729 goto out;
730 }
731
732 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
733 sblock_bad->no_io_error_seen) {
734 /*
735 * the error disappeared after reading page by page, or
736 * the area was part of a huge bio and other parts of the
737 * bio caused I/O errors, or the block layer merged several
738 * read requests into one and the error is caused by a
739 * different bio (usually one of the two latter cases is
740 * the cause)
741 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100742 spin_lock(&sctx->stat_lock);
743 sctx->stat.unverified_errors++;
744 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400745
746 goto out;
747 }
748
749 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100750 spin_lock(&sctx->stat_lock);
751 sctx->stat.read_errors++;
752 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400753 if (__ratelimit(&_rs))
754 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100755 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400756 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100757 spin_lock(&sctx->stat_lock);
758 sctx->stat.csum_errors++;
759 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400760 if (__ratelimit(&_rs))
761 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100762 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200763 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400764 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100765 spin_lock(&sctx->stat_lock);
766 sctx->stat.verify_errors++;
767 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400768 if (__ratelimit(&_rs))
769 scrub_print_warning("checksum/header error",
770 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200771 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100772 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200773 BTRFS_DEV_STAT_GENERATION_ERRS);
774 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100775 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200776 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400777 }
778
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100779 if (sctx->readonly)
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400780 goto did_not_correct_error;
781
782 if (!is_metadata && !have_csum) {
783 struct scrub_fixup_nodatasum *fixup_nodatasum;
784
785 /*
786 * !is_metadata and !have_csum, this means that the data
787 * might not be COW'ed, that it might be modified
788 * concurrently. The general strategy to work on the
789 * commit root does not help in the case when COW is not
790 * used.
791 */
792 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
793 if (!fixup_nodatasum)
794 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100795 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100796 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400797 fixup_nodatasum->logical = logical;
798 fixup_nodatasum->root = fs_info->extent_root;
799 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Arne Jansena2de7332011-03-08 14:14:00 +0100800 /*
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200801 * increment scrubs_running to prevent cancel requests from
802 * completing as long as a fixup worker is running. we must also
803 * increment scrubs_paused to prevent deadlocking on pause
804 * requests used for transactions commits (as the worker uses a
805 * transaction context). it is safe to regard the fixup worker
806 * as paused for all matters practical. effectively, we only
807 * avoid cancellation requests from completing.
Arne Jansena2de7332011-03-08 14:14:00 +0100808 */
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200809 mutex_lock(&fs_info->scrub_lock);
810 atomic_inc(&fs_info->scrubs_running);
811 atomic_inc(&fs_info->scrubs_paused);
812 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100813 atomic_inc(&sctx->fixup_cnt);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400814 fixup_nodatasum->work.func = scrub_fixup_nodatasum;
815 btrfs_queue_worker(&fs_info->scrub_workers,
816 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +0100817 goto out;
818 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400819
820 /*
821 * now build and submit the bios for the other mirrors, check
822 * checksums
823 */
824 for (mirror_index = 0;
825 mirror_index < BTRFS_MAX_MIRRORS &&
826 sblocks_for_recheck[mirror_index].page_count > 0;
827 mirror_index++) {
828 if (mirror_index == failed_mirror_index)
829 continue;
830
831 /* build and submit the bios, check checksums */
832 ret = scrub_recheck_block(fs_info,
833 sblocks_for_recheck + mirror_index,
834 is_metadata, have_csum, csum,
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100835 generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400836 if (ret)
837 goto did_not_correct_error;
838 }
839
840 /*
841 * first try to pick the mirror which is completely without I/O
842 * errors and also does not have a checksum error.
843 * If one is found, and if a checksum is present, the full block
844 * that is known to contain an error is rewritten. Afterwards
845 * the block is known to be corrected.
846 * If a mirror is found which is completely correct, and no
847 * checksum is present, only those pages are rewritten that had
848 * an I/O error in the block to be repaired, since it cannot be
849 * determined, which copy of the other pages is better (and it
850 * could happen otherwise that a correct page would be
851 * overwritten by a bad one).
852 */
853 for (mirror_index = 0;
854 mirror_index < BTRFS_MAX_MIRRORS &&
855 sblocks_for_recheck[mirror_index].page_count > 0;
856 mirror_index++) {
857 struct scrub_block *sblock_other = sblocks_for_recheck +
858 mirror_index;
859
860 if (!sblock_other->header_error &&
861 !sblock_other->checksum_error &&
862 sblock_other->no_io_error_seen) {
863 int force_write = is_metadata || have_csum;
864
865 ret = scrub_repair_block_from_good_copy(sblock_bad,
866 sblock_other,
867 force_write);
868 if (0 == ret)
869 goto corrected_error;
Arne Jansena2de7332011-03-08 14:14:00 +0100870 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400871 }
872
873 /*
874 * in case of I/O errors in the area that is supposed to be
875 * repaired, continue by picking good copies of those pages.
876 * Select the good pages from mirrors to rewrite bad pages from
877 * the area to fix. Afterwards verify the checksum of the block
878 * that is supposed to be repaired. This verification step is
879 * only done for the purpose of statistic counting and for the
880 * final scrub report, whether errors remain.
881 * A perfect algorithm could make use of the checksum and try
882 * all possible combinations of pages from the different mirrors
883 * until the checksum verification succeeds. For example, when
884 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
885 * of mirror #2 is readable but the final checksum test fails,
886 * then the 2nd page of mirror #3 could be tried, whether now
887 * the final checksum succeedes. But this would be a rare
888 * exception and is therefore not implemented. At least it is
889 * avoided that the good copy is overwritten.
890 * A more useful improvement would be to pick the sectors
891 * without I/O error based on sector sizes (512 bytes on legacy
892 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
893 * mirror could be repaired by taking 512 byte of a different
894 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
895 * area are unreadable.
896 */
897
898 /* can only fix I/O errors from here on */
899 if (sblock_bad->no_io_error_seen)
900 goto did_not_correct_error;
901
902 success = 1;
903 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100904 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400905
906 if (!page_bad->io_error)
907 continue;
908
909 for (mirror_index = 0;
910 mirror_index < BTRFS_MAX_MIRRORS &&
911 sblocks_for_recheck[mirror_index].page_count > 0;
912 mirror_index++) {
913 struct scrub_block *sblock_other = sblocks_for_recheck +
914 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100915 struct scrub_page *page_other = sblock_other->pagev[
916 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400917
918 if (!page_other->io_error) {
919 ret = scrub_repair_page_from_good_copy(
920 sblock_bad, sblock_other, page_num, 0);
921 if (0 == ret) {
922 page_bad->io_error = 0;
923 break; /* succeeded for this page */
924 }
Jan Schmidt13db62b2011-06-13 19:56:13 +0200925 }
926 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400927
928 if (page_bad->io_error) {
929 /* did not find a mirror to copy the page from */
930 success = 0;
931 }
932 }
933
934 if (success) {
935 if (is_metadata || have_csum) {
936 /*
937 * need to verify the checksum now that all
938 * sectors on disk are repaired (the write
939 * request for data to be repaired is on its way).
940 * Just be lazy and use scrub_recheck_block()
941 * which re-reads the data before the checksum
942 * is verified, but most likely the data comes out
943 * of the page cache.
944 */
945 ret = scrub_recheck_block(fs_info, sblock_bad,
946 is_metadata, have_csum, csum,
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100947 generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400948 if (!ret && !sblock_bad->header_error &&
949 !sblock_bad->checksum_error &&
950 sblock_bad->no_io_error_seen)
951 goto corrected_error;
952 else
953 goto did_not_correct_error;
954 } else {
955corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100956 spin_lock(&sctx->stat_lock);
957 sctx->stat.corrected_errors++;
958 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400959 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400960 "btrfs: fixed up error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -0400961 (unsigned long long)logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100962 rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400963 }
964 } else {
965did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100966 spin_lock(&sctx->stat_lock);
967 sctx->stat.uncorrectable_errors++;
968 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400969 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400970 "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -0400971 (unsigned long long)logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100972 rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +0100973 }
974
975out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400976 if (sblocks_for_recheck) {
977 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
978 mirror_index++) {
979 struct scrub_block *sblock = sblocks_for_recheck +
980 mirror_index;
981 int page_index;
982
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100983 for (page_index = 0; page_index < sblock->page_count;
984 page_index++) {
985 sblock->pagev[page_index]->sblock = NULL;
986 scrub_page_put(sblock->pagev[page_index]);
987 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400988 }
989 kfree(sblocks_for_recheck);
990 }
991
992 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +0100993}
994
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100995static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400996 struct btrfs_mapping_tree *map_tree,
997 u64 length, u64 logical,
998 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +0100999{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001000 int page_index;
1001 int mirror_index;
1002 int ret;
1003
1004 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001005 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001006 * are not used (and not set) in the blocks that are used for
1007 * the recheck procedure
1008 */
1009
1010 page_index = 0;
1011 while (length > 0) {
1012 u64 sublen = min_t(u64, length, PAGE_SIZE);
1013 u64 mapped_length = sublen;
1014 struct btrfs_bio *bbio = NULL;
1015
1016 /*
1017 * with a length of PAGE_SIZE, each returned stripe
1018 * represents one mirror
1019 */
1020 ret = btrfs_map_block(map_tree, WRITE, logical, &mapped_length,
1021 &bbio, 0);
1022 if (ret || !bbio || mapped_length < sublen) {
1023 kfree(bbio);
1024 return -EIO;
1025 }
1026
1027 BUG_ON(page_index >= SCRUB_PAGES_PER_BIO);
1028 for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
1029 mirror_index++) {
1030 struct scrub_block *sblock;
1031 struct scrub_page *page;
1032
1033 if (mirror_index >= BTRFS_MAX_MIRRORS)
1034 continue;
1035
1036 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001037 sblock->sctx = sctx;
1038 page = kzalloc(sizeof(*page), GFP_NOFS);
1039 if (!page) {
1040leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001041 spin_lock(&sctx->stat_lock);
1042 sctx->stat.malloc_errors++;
1043 spin_unlock(&sctx->stat_lock);
Wei Yongjuncf93dcc2012-09-02 07:44:51 -06001044 kfree(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001045 return -ENOMEM;
1046 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001047 scrub_page_get(page);
1048 sblock->pagev[page_index] = page;
1049 page->logical = logical;
1050 page->physical = bbio->stripes[mirror_index].physical;
1051 /* for missing devices, dev->bdev is NULL */
1052 page->dev = bbio->stripes[mirror_index].dev;
1053 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001054 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001055 page->page = alloc_page(GFP_NOFS);
1056 if (!page->page)
1057 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001058 }
1059 kfree(bbio);
1060 length -= sublen;
1061 logical += sublen;
1062 page_index++;
1063 }
1064
1065 return 0;
1066}
1067
1068/*
1069 * this function will check the on disk data for checksum errors, header
1070 * errors and read I/O errors. If any I/O errors happen, the exact pages
1071 * which are errored are marked as being bad. The goal is to enable scrub
1072 * to take those pages that are not errored from all the mirrors so that
1073 * the pages that are errored in the just handled mirror can be repaired.
1074 */
1075static int scrub_recheck_block(struct btrfs_fs_info *fs_info,
1076 struct scrub_block *sblock, int is_metadata,
1077 int have_csum, u8 *csum, u64 generation,
1078 u16 csum_size)
1079{
1080 int page_num;
1081
1082 sblock->no_io_error_seen = 1;
1083 sblock->header_error = 0;
1084 sblock->checksum_error = 0;
1085
1086 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1087 struct bio *bio;
1088 int ret;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001089 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001090 DECLARE_COMPLETION_ONSTACK(complete);
1091
Stefan Behrens442a4f62012-05-25 16:06:08 +02001092 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001093 page->io_error = 1;
1094 sblock->no_io_error_seen = 0;
1095 continue;
1096 }
1097
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001098 WARN_ON(!page->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001099 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001100 if (!bio)
1101 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001102 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001103 bio->bi_sector = page->physical >> 9;
1104 bio->bi_end_io = scrub_complete_bio_end_io;
1105 bio->bi_private = &complete;
1106
1107 ret = bio_add_page(bio, page->page, PAGE_SIZE, 0);
1108 if (PAGE_SIZE != ret) {
1109 bio_put(bio);
1110 return -EIO;
1111 }
1112 btrfsic_submit_bio(READ, bio);
1113
1114 /* this will also unplug the queue */
1115 wait_for_completion(&complete);
1116
1117 page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
1118 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1119 sblock->no_io_error_seen = 0;
1120 bio_put(bio);
1121 }
1122
1123 if (sblock->no_io_error_seen)
1124 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1125 have_csum, csum, generation,
1126 csum_size);
1127
1128 return 0;
1129}
1130
1131static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1132 struct scrub_block *sblock,
1133 int is_metadata, int have_csum,
1134 const u8 *csum, u64 generation,
1135 u16 csum_size)
1136{
1137 int page_num;
1138 u8 calculated_csum[BTRFS_CSUM_SIZE];
1139 u32 crc = ~(u32)0;
1140 struct btrfs_root *root = fs_info->extent_root;
1141 void *mapped_buffer;
1142
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001143 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001144 if (is_metadata) {
1145 struct btrfs_header *h;
1146
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001147 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001148 h = (struct btrfs_header *)mapped_buffer;
1149
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001150 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001151 memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
1152 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001153 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001154 sblock->header_error = 1;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001155 } else if (generation != le64_to_cpu(h->generation)) {
1156 sblock->header_error = 1;
1157 sblock->generation_error = 1;
1158 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001159 csum = h->csum;
1160 } else {
1161 if (!have_csum)
1162 return;
1163
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001164 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001165 }
1166
1167 for (page_num = 0;;) {
1168 if (page_num == 0 && is_metadata)
1169 crc = btrfs_csum_data(root,
1170 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1171 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1172 else
1173 crc = btrfs_csum_data(root, mapped_buffer, crc,
1174 PAGE_SIZE);
1175
Linus Torvalds9613beb2012-03-30 12:44:29 -07001176 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001177 page_num++;
1178 if (page_num >= sblock->page_count)
1179 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001180 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001181
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001182 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001183 }
1184
1185 btrfs_csum_final(crc, calculated_csum);
1186 if (memcmp(calculated_csum, csum, csum_size))
1187 sblock->checksum_error = 1;
1188}
1189
1190static void scrub_complete_bio_end_io(struct bio *bio, int err)
1191{
1192 complete((struct completion *)bio->bi_private);
1193}
1194
1195static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1196 struct scrub_block *sblock_good,
1197 int force_write)
1198{
1199 int page_num;
1200 int ret = 0;
1201
1202 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1203 int ret_sub;
1204
1205 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1206 sblock_good,
1207 page_num,
1208 force_write);
1209 if (ret_sub)
1210 ret = ret_sub;
1211 }
1212
1213 return ret;
1214}
1215
1216static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1217 struct scrub_block *sblock_good,
1218 int page_num, int force_write)
1219{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001220 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1221 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001222
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001223 BUG_ON(page_bad->page == NULL);
1224 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001225 if (force_write || sblock_bad->header_error ||
1226 sblock_bad->checksum_error || page_bad->io_error) {
1227 struct bio *bio;
1228 int ret;
1229 DECLARE_COMPLETION_ONSTACK(complete);
1230
1231 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001232 if (!bio)
1233 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001234 bio->bi_bdev = page_bad->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001235 bio->bi_sector = page_bad->physical >> 9;
1236 bio->bi_end_io = scrub_complete_bio_end_io;
1237 bio->bi_private = &complete;
1238
1239 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1240 if (PAGE_SIZE != ret) {
1241 bio_put(bio);
1242 return -EIO;
1243 }
1244 btrfsic_submit_bio(WRITE, bio);
1245
1246 /* this will also unplug the queue */
1247 wait_for_completion(&complete);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001248 if (!bio_flagged(bio, BIO_UPTODATE)) {
1249 btrfs_dev_stat_inc_and_print(page_bad->dev,
1250 BTRFS_DEV_STAT_WRITE_ERRS);
1251 bio_put(bio);
1252 return -EIO;
1253 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001254 bio_put(bio);
1255 }
1256
1257 return 0;
1258}
1259
1260static void scrub_checksum(struct scrub_block *sblock)
1261{
1262 u64 flags;
1263 int ret;
1264
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001265 WARN_ON(sblock->page_count < 1);
1266 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001267 ret = 0;
1268 if (flags & BTRFS_EXTENT_FLAG_DATA)
1269 ret = scrub_checksum_data(sblock);
1270 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1271 ret = scrub_checksum_tree_block(sblock);
1272 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1273 (void)scrub_checksum_super(sblock);
1274 else
1275 WARN_ON(1);
1276 if (ret)
1277 scrub_handle_errored_block(sblock);
1278}
1279
1280static int scrub_checksum_data(struct scrub_block *sblock)
1281{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001282 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001283 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001284 u8 *on_disk_csum;
1285 struct page *page;
1286 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001287 u32 crc = ~(u32)0;
1288 int fail = 0;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001289 struct btrfs_root *root = sctx->dev_root;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001290 u64 len;
1291 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001292
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001293 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001294 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001295 return 0;
1296
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001297 on_disk_csum = sblock->pagev[0]->csum;
1298 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001299 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001300
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001301 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001302 index = 0;
1303 for (;;) {
1304 u64 l = min_t(u64, len, PAGE_SIZE);
1305
1306 crc = btrfs_csum_data(root, buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001307 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001308 len -= l;
1309 if (len == 0)
1310 break;
1311 index++;
1312 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001313 BUG_ON(!sblock->pagev[index]->page);
1314 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001315 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001316 }
1317
Arne Jansena2de7332011-03-08 14:14:00 +01001318 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001319 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001320 fail = 1;
1321
Arne Jansena2de7332011-03-08 14:14:00 +01001322 return fail;
1323}
1324
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001325static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001326{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001327 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001328 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001329 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001330 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001331 u8 calculated_csum[BTRFS_CSUM_SIZE];
1332 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1333 struct page *page;
1334 void *mapped_buffer;
1335 u64 mapped_size;
1336 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001337 u32 crc = ~(u32)0;
1338 int fail = 0;
1339 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001340 u64 len;
1341 int index;
1342
1343 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001344 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001345 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001346 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001347 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001348
1349 /*
1350 * we don't use the getter functions here, as we
1351 * a) don't have an extent buffer and
1352 * b) the page is already kmapped
1353 */
Arne Jansena2de7332011-03-08 14:14:00 +01001354
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001355 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr))
Arne Jansena2de7332011-03-08 14:14:00 +01001356 ++fail;
1357
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001358 if (sblock->pagev[0]->generation != le64_to_cpu(h->generation))
Arne Jansena2de7332011-03-08 14:14:00 +01001359 ++fail;
1360
1361 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1362 ++fail;
1363
1364 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1365 BTRFS_UUID_SIZE))
1366 ++fail;
1367
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001368 BUG_ON(sctx->nodesize != sctx->leafsize);
1369 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001370 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1371 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1372 index = 0;
1373 for (;;) {
1374 u64 l = min_t(u64, len, mapped_size);
1375
1376 crc = btrfs_csum_data(root, p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001377 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001378 len -= l;
1379 if (len == 0)
1380 break;
1381 index++;
1382 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001383 BUG_ON(!sblock->pagev[index]->page);
1384 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001385 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001386 mapped_size = PAGE_SIZE;
1387 p = mapped_buffer;
1388 }
1389
1390 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001391 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001392 ++crc_fail;
1393
Arne Jansena2de7332011-03-08 14:14:00 +01001394 return fail || crc_fail;
1395}
1396
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001397static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001398{
1399 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001400 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001401 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001402 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001403 u8 calculated_csum[BTRFS_CSUM_SIZE];
1404 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1405 struct page *page;
1406 void *mapped_buffer;
1407 u64 mapped_size;
1408 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001409 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001410 int fail_gen = 0;
1411 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001412 u64 len;
1413 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001414
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001415 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001416 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001417 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001418 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001419 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001420
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001421 if (sblock->pagev[0]->logical != le64_to_cpu(s->bytenr))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001422 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001423
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001424 if (sblock->pagev[0]->generation != le64_to_cpu(s->generation))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001425 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001426
1427 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001428 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001429
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001430 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1431 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1432 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1433 index = 0;
1434 for (;;) {
1435 u64 l = min_t(u64, len, mapped_size);
1436
1437 crc = btrfs_csum_data(root, p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001438 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001439 len -= l;
1440 if (len == 0)
1441 break;
1442 index++;
1443 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001444 BUG_ON(!sblock->pagev[index]->page);
1445 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001446 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001447 mapped_size = PAGE_SIZE;
1448 p = mapped_buffer;
1449 }
1450
1451 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001452 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001453 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001454
Stefan Behrens442a4f62012-05-25 16:06:08 +02001455 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001456 /*
1457 * if we find an error in a super block, we just report it.
1458 * They will get written with the next transaction commit
1459 * anyway
1460 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001461 spin_lock(&sctx->stat_lock);
1462 ++sctx->stat.super_errors;
1463 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001464 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001465 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001466 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1467 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001468 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001469 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001470 }
1471
Stefan Behrens442a4f62012-05-25 16:06:08 +02001472 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001473}
1474
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001475static void scrub_block_get(struct scrub_block *sblock)
1476{
1477 atomic_inc(&sblock->ref_count);
1478}
1479
1480static void scrub_block_put(struct scrub_block *sblock)
1481{
1482 if (atomic_dec_and_test(&sblock->ref_count)) {
1483 int i;
1484
1485 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001486 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001487 kfree(sblock);
1488 }
1489}
1490
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001491static void scrub_page_get(struct scrub_page *spage)
1492{
1493 atomic_inc(&spage->ref_count);
1494}
1495
1496static void scrub_page_put(struct scrub_page *spage)
1497{
1498 if (atomic_dec_and_test(&spage->ref_count)) {
1499 if (spage->page)
1500 __free_page(spage->page);
1501 kfree(spage);
1502 }
1503}
1504
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001505static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01001506{
1507 struct scrub_bio *sbio;
1508
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001509 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04001510 return;
Arne Jansena2de7332011-03-08 14:14:00 +01001511
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001512 sbio = sctx->bios[sctx->curr];
1513 sctx->curr = -1;
1514 atomic_inc(&sctx->in_flight);
Arne Jansena2de7332011-03-08 14:14:00 +01001515
Stefan Behrens21adbd52011-11-09 13:44:05 +01001516 btrfsic_submit_bio(READ, sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01001517}
1518
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001519static int scrub_add_page_to_bio(struct scrub_ctx *sctx,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001520 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01001521{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001522 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01001523 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001524 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01001525
1526again:
1527 /*
1528 * grab a fresh bio or wait for one to become available
1529 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001530 while (sctx->curr == -1) {
1531 spin_lock(&sctx->list_lock);
1532 sctx->curr = sctx->first_free;
1533 if (sctx->curr != -1) {
1534 sctx->first_free = sctx->bios[sctx->curr]->next_free;
1535 sctx->bios[sctx->curr]->next_free = -1;
1536 sctx->bios[sctx->curr]->page_count = 0;
1537 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001538 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001539 spin_unlock(&sctx->list_lock);
1540 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01001541 }
1542 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001543 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001544 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05001545 struct bio *bio;
1546
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001547 sbio->physical = spage->physical;
1548 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001549 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001550 bio = sbio->bio;
1551 if (!bio) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001552 bio = bio_alloc(GFP_NOFS, sctx->pages_per_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001553 if (!bio)
1554 return -ENOMEM;
1555 sbio->bio = bio;
1556 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05001557
1558 bio->bi_private = sbio;
1559 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001560 bio->bi_bdev = sbio->dev->bdev;
1561 bio->bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001562 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001563 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1564 spage->physical ||
1565 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001566 spage->logical ||
1567 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001568 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05001569 goto again;
1570 }
1571
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001572 sbio->pagev[sbio->page_count] = spage;
1573 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1574 if (ret != PAGE_SIZE) {
1575 if (sbio->page_count < 1) {
1576 bio_put(sbio->bio);
1577 sbio->bio = NULL;
1578 return -EIO;
1579 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001580 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001581 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01001582 }
Arne Jansen1bc87792011-05-28 21:57:55 +02001583
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001584 scrub_block_get(sblock); /* one for the added page */
1585 atomic_inc(&sblock->outstanding_pages);
1586 sbio->page_count++;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001587 if (sbio->page_count == sctx->pages_per_bio)
1588 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001589
1590 return 0;
1591}
1592
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001593static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001594 u64 physical, struct btrfs_device *dev, u64 flags,
1595 u64 gen, int mirror_num, u8 *csum, int force)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001596{
1597 struct scrub_block *sblock;
1598 int index;
1599
1600 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
1601 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001602 spin_lock(&sctx->stat_lock);
1603 sctx->stat.malloc_errors++;
1604 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001605 return -ENOMEM;
1606 }
1607
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001608 /* one ref inside this function, plus one for each page added to
1609 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001610 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001611 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001612 sblock->no_io_error_seen = 1;
1613
1614 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001615 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001616 u64 l = min_t(u64, len, PAGE_SIZE);
1617
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001618 spage = kzalloc(sizeof(*spage), GFP_NOFS);
1619 if (!spage) {
1620leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001621 spin_lock(&sctx->stat_lock);
1622 sctx->stat.malloc_errors++;
1623 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001624 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001625 return -ENOMEM;
1626 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001627 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
1628 scrub_page_get(spage);
1629 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001630 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001631 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001632 spage->flags = flags;
1633 spage->generation = gen;
1634 spage->logical = logical;
1635 spage->physical = physical;
1636 spage->mirror_num = mirror_num;
1637 if (csum) {
1638 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001639 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001640 } else {
1641 spage->have_csum = 0;
1642 }
1643 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001644 spage->page = alloc_page(GFP_NOFS);
1645 if (!spage->page)
1646 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001647 len -= l;
1648 logical += l;
1649 physical += l;
1650 }
1651
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001652 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001653 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001654 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001655 int ret;
1656
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001657 ret = scrub_add_page_to_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001658 if (ret) {
1659 scrub_block_put(sblock);
1660 return ret;
1661 }
1662 }
1663
1664 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001665 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001666
1667 /* last one frees, either here or in bio completion for last page */
1668 scrub_block_put(sblock);
1669 return 0;
1670}
1671
1672static void scrub_bio_end_io(struct bio *bio, int err)
1673{
1674 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001675 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001676
1677 sbio->err = err;
1678 sbio->bio = bio;
1679
1680 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
1681}
1682
1683static void scrub_bio_end_io_worker(struct btrfs_work *work)
1684{
1685 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001686 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001687 int i;
1688
1689 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_BIO);
1690 if (sbio->err) {
1691 for (i = 0; i < sbio->page_count; i++) {
1692 struct scrub_page *spage = sbio->pagev[i];
1693
1694 spage->io_error = 1;
1695 spage->sblock->no_io_error_seen = 0;
1696 }
1697 }
1698
1699 /* now complete the scrub_block items that have all pages completed */
1700 for (i = 0; i < sbio->page_count; i++) {
1701 struct scrub_page *spage = sbio->pagev[i];
1702 struct scrub_block *sblock = spage->sblock;
1703
1704 if (atomic_dec_and_test(&sblock->outstanding_pages))
1705 scrub_block_complete(sblock);
1706 scrub_block_put(sblock);
1707 }
1708
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001709 bio_put(sbio->bio);
1710 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001711 spin_lock(&sctx->list_lock);
1712 sbio->next_free = sctx->first_free;
1713 sctx->first_free = sbio->index;
1714 spin_unlock(&sctx->list_lock);
1715 atomic_dec(&sctx->in_flight);
1716 wake_up(&sctx->list_wait);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001717}
1718
1719static void scrub_block_complete(struct scrub_block *sblock)
1720{
1721 if (!sblock->no_io_error_seen)
1722 scrub_handle_errored_block(sblock);
1723 else
1724 scrub_checksum(sblock);
1725}
1726
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001727static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01001728 u8 *csum)
1729{
1730 struct btrfs_ordered_sum *sum = NULL;
1731 int ret = 0;
1732 unsigned long i;
1733 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01001734
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001735 while (!list_empty(&sctx->csum_list)) {
1736 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01001737 struct btrfs_ordered_sum, list);
1738 if (sum->bytenr > logical)
1739 return 0;
1740 if (sum->bytenr + sum->len > logical)
1741 break;
1742
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001743 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01001744 list_del(&sum->list);
1745 kfree(sum);
1746 sum = NULL;
1747 }
1748 if (!sum)
1749 return 0;
1750
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001751 num_sectors = sum->len / sctx->sectorsize;
Arne Jansena2de7332011-03-08 14:14:00 +01001752 for (i = 0; i < num_sectors; ++i) {
1753 if (sum->sums[i].bytenr == logical) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001754 memcpy(csum, &sum->sums[i].sum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001755 ret = 1;
1756 break;
1757 }
1758 }
1759 if (ret && i == num_sectors - 1) {
1760 list_del(&sum->list);
1761 kfree(sum);
1762 }
1763 return ret;
1764}
1765
1766/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001767static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001768 u64 physical, struct btrfs_device *dev, u64 flags,
1769 u64 gen, int mirror_num)
Arne Jansena2de7332011-03-08 14:14:00 +01001770{
1771 int ret;
1772 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001773 u32 blocksize;
1774
1775 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001776 blocksize = sctx->sectorsize;
1777 spin_lock(&sctx->stat_lock);
1778 sctx->stat.data_extents_scrubbed++;
1779 sctx->stat.data_bytes_scrubbed += len;
1780 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001781 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001782 BUG_ON(sctx->nodesize != sctx->leafsize);
1783 blocksize = sctx->nodesize;
1784 spin_lock(&sctx->stat_lock);
1785 sctx->stat.tree_extents_scrubbed++;
1786 sctx->stat.tree_bytes_scrubbed += len;
1787 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001788 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001789 blocksize = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001790 BUG_ON(1);
1791 }
Arne Jansena2de7332011-03-08 14:14:00 +01001792
1793 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001794 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01001795 int have_csum = 0;
1796
1797 if (flags & BTRFS_EXTENT_FLAG_DATA) {
1798 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001799 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01001800 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001801 ++sctx->stat.no_csum;
Arne Jansena2de7332011-03-08 14:14:00 +01001802 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001803 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001804 mirror_num, have_csum ? csum : NULL, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01001805 if (ret)
1806 return ret;
1807 len -= l;
1808 logical += l;
1809 physical += l;
1810 }
1811 return 0;
1812}
1813
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001814static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001815 struct map_lookup *map,
1816 struct btrfs_device *scrub_dev,
1817 int num, u64 base, u64 length)
Arne Jansena2de7332011-03-08 14:14:00 +01001818{
1819 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001820 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01001821 struct btrfs_root *root = fs_info->extent_root;
1822 struct btrfs_root *csum_root = fs_info->csum_root;
1823 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00001824 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01001825 u64 flags;
1826 int ret;
1827 int slot;
1828 int i;
1829 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01001830 struct extent_buffer *l;
1831 struct btrfs_key key;
1832 u64 physical;
1833 u64 logical;
1834 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02001835 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02001836 struct reada_control *reada1;
1837 struct reada_control *reada2;
1838 struct btrfs_key key_start;
1839 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01001840 u64 increment = map->stripe_len;
1841 u64 offset;
1842
1843 nstripes = length;
1844 offset = 0;
1845 do_div(nstripes, map->stripe_len);
1846 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1847 offset = map->stripe_len * num;
1848 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02001849 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001850 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1851 int factor = map->num_stripes / map->sub_stripes;
1852 offset = map->stripe_len * (num / map->sub_stripes);
1853 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02001854 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001855 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1856 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02001857 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001858 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1859 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02001860 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001861 } else {
1862 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02001863 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001864 }
1865
1866 path = btrfs_alloc_path();
1867 if (!path)
1868 return -ENOMEM;
1869
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001870 /*
1871 * work on commit root. The related disk blocks are static as
1872 * long as COW is applied. This means, it is save to rewrite
1873 * them to repair disk errors without any race conditions
1874 */
Arne Jansena2de7332011-03-08 14:14:00 +01001875 path->search_commit_root = 1;
1876 path->skip_locking = 1;
1877
1878 /*
Arne Jansen7a262852011-06-10 12:39:23 +02001879 * trigger the readahead for extent tree csum tree and wait for
1880 * completion. During readahead, the scrub is officially paused
1881 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01001882 */
1883 logical = base + offset;
Arne Jansena2de7332011-03-08 14:14:00 +01001884
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001885 wait_event(sctx->list_wait,
1886 atomic_read(&sctx->in_flight) == 0);
Arne Jansen7a262852011-06-10 12:39:23 +02001887 atomic_inc(&fs_info->scrubs_paused);
1888 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01001889
Arne Jansen7a262852011-06-10 12:39:23 +02001890 /* FIXME it might be better to start readahead at commit root */
1891 key_start.objectid = logical;
1892 key_start.type = BTRFS_EXTENT_ITEM_KEY;
1893 key_start.offset = (u64)0;
1894 key_end.objectid = base + offset + nstripes * increment;
1895 key_end.type = BTRFS_EXTENT_ITEM_KEY;
1896 key_end.offset = (u64)0;
1897 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01001898
Arne Jansen7a262852011-06-10 12:39:23 +02001899 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1900 key_start.type = BTRFS_EXTENT_CSUM_KEY;
1901 key_start.offset = logical;
1902 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1903 key_end.type = BTRFS_EXTENT_CSUM_KEY;
1904 key_end.offset = base + offset + nstripes * increment;
1905 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01001906
Arne Jansen7a262852011-06-10 12:39:23 +02001907 if (!IS_ERR(reada1))
1908 btrfs_reada_wait(reada1);
1909 if (!IS_ERR(reada2))
1910 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01001911
Arne Jansen7a262852011-06-10 12:39:23 +02001912 mutex_lock(&fs_info->scrub_lock);
1913 while (atomic_read(&fs_info->scrub_pause_req)) {
1914 mutex_unlock(&fs_info->scrub_lock);
1915 wait_event(fs_info->scrub_pause_wait,
1916 atomic_read(&fs_info->scrub_pause_req) == 0);
1917 mutex_lock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001918 }
Arne Jansen7a262852011-06-10 12:39:23 +02001919 atomic_dec(&fs_info->scrubs_paused);
1920 mutex_unlock(&fs_info->scrub_lock);
1921 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01001922
1923 /*
1924 * collect all data csums for the stripe to avoid seeking during
1925 * the scrub. This might currently (crc32) end up to be about 1MB
1926 */
Arne Jansene7786c32011-05-28 20:58:38 +00001927 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01001928
Arne Jansena2de7332011-03-08 14:14:00 +01001929 /*
1930 * now find all extents for each stripe and scrub them
1931 */
Arne Jansen7a262852011-06-10 12:39:23 +02001932 logical = base + offset;
1933 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01001934 ret = 0;
Arne Jansen7a262852011-06-10 12:39:23 +02001935 for (i = 0; i < nstripes; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +01001936 /*
1937 * canceled?
1938 */
1939 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001940 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01001941 ret = -ECANCELED;
1942 goto out;
1943 }
1944 /*
1945 * check to see if we have to pause
1946 */
1947 if (atomic_read(&fs_info->scrub_pause_req)) {
1948 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001949 scrub_submit(sctx);
1950 wait_event(sctx->list_wait,
1951 atomic_read(&sctx->in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01001952 atomic_inc(&fs_info->scrubs_paused);
1953 wake_up(&fs_info->scrub_pause_wait);
1954 mutex_lock(&fs_info->scrub_lock);
1955 while (atomic_read(&fs_info->scrub_pause_req)) {
1956 mutex_unlock(&fs_info->scrub_lock);
1957 wait_event(fs_info->scrub_pause_wait,
1958 atomic_read(&fs_info->scrub_pause_req) == 0);
1959 mutex_lock(&fs_info->scrub_lock);
1960 }
1961 atomic_dec(&fs_info->scrubs_paused);
1962 mutex_unlock(&fs_info->scrub_lock);
1963 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01001964 }
1965
Arne Jansen7a262852011-06-10 12:39:23 +02001966 ret = btrfs_lookup_csums_range(csum_root, logical,
1967 logical + map->stripe_len - 1,
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001968 &sctx->csum_list, 1);
Arne Jansen7a262852011-06-10 12:39:23 +02001969 if (ret)
1970 goto out;
1971
Arne Jansena2de7332011-03-08 14:14:00 +01001972 key.objectid = logical;
1973 key.type = BTRFS_EXTENT_ITEM_KEY;
1974 key.offset = (u64)0;
1975
1976 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1977 if (ret < 0)
1978 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02001979 if (ret > 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01001980 ret = btrfs_previous_item(root, path, 0,
1981 BTRFS_EXTENT_ITEM_KEY);
1982 if (ret < 0)
1983 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02001984 if (ret > 0) {
1985 /* there's no smaller item, so stick with the
1986 * larger one */
1987 btrfs_release_path(path);
1988 ret = btrfs_search_slot(NULL, root, &key,
1989 path, 0, 0);
1990 if (ret < 0)
1991 goto out;
1992 }
Arne Jansena2de7332011-03-08 14:14:00 +01001993 }
1994
1995 while (1) {
1996 l = path->nodes[0];
1997 slot = path->slots[0];
1998 if (slot >= btrfs_header_nritems(l)) {
1999 ret = btrfs_next_leaf(root, path);
2000 if (ret == 0)
2001 continue;
2002 if (ret < 0)
2003 goto out;
2004
2005 break;
2006 }
2007 btrfs_item_key_to_cpu(l, &key, slot);
2008
2009 if (key.objectid + key.offset <= logical)
2010 goto next;
2011
2012 if (key.objectid >= logical + map->stripe_len)
2013 break;
2014
2015 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
2016 goto next;
2017
2018 extent = btrfs_item_ptr(l, slot,
2019 struct btrfs_extent_item);
2020 flags = btrfs_extent_flags(l, extent);
2021 generation = btrfs_extent_generation(l, extent);
2022
2023 if (key.objectid < logical &&
2024 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2025 printk(KERN_ERR
2026 "btrfs scrub: tree block %llu spanning "
2027 "stripes, ignored. logical=%llu\n",
2028 (unsigned long long)key.objectid,
2029 (unsigned long long)logical);
2030 goto next;
2031 }
2032
2033 /*
2034 * trim extent to this stripe
2035 */
2036 if (key.objectid < logical) {
2037 key.offset -= logical - key.objectid;
2038 key.objectid = logical;
2039 }
2040 if (key.objectid + key.offset >
2041 logical + map->stripe_len) {
2042 key.offset = logical + map->stripe_len -
2043 key.objectid;
2044 }
2045
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002046 ret = scrub_extent(sctx, key.objectid, key.offset,
Arne Jansena2de7332011-03-08 14:14:00 +01002047 key.objectid - logical + physical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002048 scrub_dev, flags, generation,
2049 mirror_num);
Arne Jansena2de7332011-03-08 14:14:00 +01002050 if (ret)
2051 goto out;
2052
2053next:
2054 path->slots[0]++;
2055 }
Chris Mason71267332011-05-23 06:30:52 -04002056 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002057 logical += increment;
2058 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002059 spin_lock(&sctx->stat_lock);
2060 sctx->stat.last_physical = physical;
2061 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002062 }
2063 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002064 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002065
2066out:
Arne Jansene7786c32011-05-28 20:58:38 +00002067 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002068 btrfs_free_path(path);
2069 return ret < 0 ? ret : 0;
2070}
2071
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002072static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002073 struct btrfs_device *scrub_dev,
2074 u64 chunk_tree, u64 chunk_objectid,
2075 u64 chunk_offset, u64 length,
2076 u64 dev_offset)
Arne Jansena2de7332011-03-08 14:14:00 +01002077{
2078 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002079 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01002080 struct map_lookup *map;
2081 struct extent_map *em;
2082 int i;
2083 int ret = -EINVAL;
2084
2085 read_lock(&map_tree->map_tree.lock);
2086 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2087 read_unlock(&map_tree->map_tree.lock);
2088
2089 if (!em)
2090 return -EINVAL;
2091
2092 map = (struct map_lookup *)em->bdev;
2093 if (em->start != chunk_offset)
2094 goto out;
2095
2096 if (em->len < length)
2097 goto out;
2098
2099 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002100 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01002101 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002102 ret = scrub_stripe(sctx, map, scrub_dev, i,
2103 chunk_offset, length);
Arne Jansena2de7332011-03-08 14:14:00 +01002104 if (ret)
2105 goto out;
2106 }
2107 }
2108out:
2109 free_extent_map(em);
2110
2111 return ret;
2112}
2113
2114static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002115int scrub_enumerate_chunks(struct scrub_ctx *sctx,
2116 struct btrfs_device *scrub_dev, u64 start, u64 end)
Arne Jansena2de7332011-03-08 14:14:00 +01002117{
2118 struct btrfs_dev_extent *dev_extent = NULL;
2119 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002120 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002121 struct btrfs_fs_info *fs_info = root->fs_info;
2122 u64 length;
2123 u64 chunk_tree;
2124 u64 chunk_objectid;
2125 u64 chunk_offset;
2126 int ret;
2127 int slot;
2128 struct extent_buffer *l;
2129 struct btrfs_key key;
2130 struct btrfs_key found_key;
2131 struct btrfs_block_group_cache *cache;
2132
2133 path = btrfs_alloc_path();
2134 if (!path)
2135 return -ENOMEM;
2136
2137 path->reada = 2;
2138 path->search_commit_root = 1;
2139 path->skip_locking = 1;
2140
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002141 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01002142 key.offset = 0ull;
2143 key.type = BTRFS_DEV_EXTENT_KEY;
2144
Arne Jansena2de7332011-03-08 14:14:00 +01002145 while (1) {
2146 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2147 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02002148 break;
2149 if (ret > 0) {
2150 if (path->slots[0] >=
2151 btrfs_header_nritems(path->nodes[0])) {
2152 ret = btrfs_next_leaf(root, path);
2153 if (ret)
2154 break;
2155 }
2156 }
Arne Jansena2de7332011-03-08 14:14:00 +01002157
2158 l = path->nodes[0];
2159 slot = path->slots[0];
2160
2161 btrfs_item_key_to_cpu(l, &found_key, slot);
2162
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002163 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01002164 break;
2165
Arne Jansen8c510322011-06-03 10:09:26 +02002166 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01002167 break;
2168
2169 if (found_key.offset >= end)
2170 break;
2171
2172 if (found_key.offset < key.offset)
2173 break;
2174
2175 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2176 length = btrfs_dev_extent_length(l, dev_extent);
2177
2178 if (found_key.offset + length <= start) {
2179 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002180 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002181 continue;
2182 }
2183
2184 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2185 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2186 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2187
2188 /*
2189 * get a reference on the corresponding block group to prevent
2190 * the chunk from going away while we scrub it
2191 */
2192 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2193 if (!cache) {
2194 ret = -ENOENT;
Arne Jansen8c510322011-06-03 10:09:26 +02002195 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002196 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002197 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Arne Jansen859acaf2012-02-09 15:09:02 +01002198 chunk_offset, length, found_key.offset);
Arne Jansena2de7332011-03-08 14:14:00 +01002199 btrfs_put_block_group(cache);
2200 if (ret)
2201 break;
2202
2203 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002204 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002205 }
2206
Arne Jansena2de7332011-03-08 14:14:00 +01002207 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02002208
2209 /*
2210 * ret can still be 1 from search_slot or next_leaf,
2211 * that's not an error
2212 */
2213 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002214}
2215
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002216static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2217 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01002218{
2219 int i;
2220 u64 bytenr;
2221 u64 gen;
2222 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002223 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002224
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002225 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
2226 return -EIO;
2227
Arne Jansena2de7332011-03-08 14:14:00 +01002228 gen = root->fs_info->last_trans_committed;
2229
2230 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2231 bytenr = btrfs_sb_offset(i);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002232 if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01002233 break;
2234
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002235 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002236 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
2237 NULL, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002238 if (ret)
2239 return ret;
2240 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002241 wait_event(sctx->list_wait, atomic_read(&sctx->in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002242
2243 return 0;
2244}
2245
2246/*
2247 * get a reference count on fs_info->scrub_workers. start worker if necessary
2248 */
2249static noinline_for_stack int scrub_workers_get(struct btrfs_root *root)
2250{
2251 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002252 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002253
2254 mutex_lock(&fs_info->scrub_lock);
Arne Jansen632dd772011-06-10 12:07:07 +02002255 if (fs_info->scrub_workers_refcnt == 0) {
2256 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
2257 fs_info->thread_pool_size, &fs_info->generic_worker);
2258 fs_info->scrub_workers.idle_thresh = 4;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002259 ret = btrfs_start_workers(&fs_info->scrub_workers);
2260 if (ret)
2261 goto out;
Arne Jansen632dd772011-06-10 12:07:07 +02002262 }
Arne Jansena2de7332011-03-08 14:14:00 +01002263 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002264out:
Arne Jansena2de7332011-03-08 14:14:00 +01002265 mutex_unlock(&fs_info->scrub_lock);
2266
Josef Bacik0dc3b842011-11-18 14:37:27 -05002267 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002268}
2269
2270static noinline_for_stack void scrub_workers_put(struct btrfs_root *root)
2271{
2272 struct btrfs_fs_info *fs_info = root->fs_info;
2273
2274 mutex_lock(&fs_info->scrub_lock);
2275 if (--fs_info->scrub_workers_refcnt == 0)
2276 btrfs_stop_workers(&fs_info->scrub_workers);
2277 WARN_ON(fs_info->scrub_workers_refcnt < 0);
2278 mutex_unlock(&fs_info->scrub_lock);
2279}
2280
2281
2282int btrfs_scrub_dev(struct btrfs_root *root, u64 devid, u64 start, u64 end,
Arne Jansen86287642011-03-23 16:34:19 +01002283 struct btrfs_scrub_progress *progress, int readonly)
Arne Jansena2de7332011-03-08 14:14:00 +01002284{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002285 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002286 struct btrfs_fs_info *fs_info = root->fs_info;
2287 int ret;
2288 struct btrfs_device *dev;
2289
David Sterba7841cb22011-05-31 18:07:27 +02002290 if (btrfs_fs_closing(root->fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01002291 return -EINVAL;
2292
2293 /*
2294 * check some assumptions
2295 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002296 if (root->nodesize != root->leafsize) {
2297 printk(KERN_ERR
2298 "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
2299 root->nodesize, root->leafsize);
2300 return -EINVAL;
2301 }
2302
2303 if (root->nodesize > BTRFS_STRIPE_LEN) {
2304 /*
2305 * in this case scrub is unable to calculate the checksum
2306 * the way scrub is implemented. Do not handle this
2307 * situation at all because it won't ever happen.
2308 */
2309 printk(KERN_ERR
2310 "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
2311 root->nodesize, BTRFS_STRIPE_LEN);
2312 return -EINVAL;
2313 }
2314
2315 if (root->sectorsize != PAGE_SIZE) {
2316 /* not supported for data w/o checksums */
2317 printk(KERN_ERR
2318 "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
2319 root->sectorsize, (unsigned long long)PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01002320 return -EINVAL;
2321 }
2322
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002323 if (fs_info->chunk_root->nodesize >
2324 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
2325 fs_info->chunk_root->sectorsize >
2326 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
2327 /*
2328 * would exhaust the array bounds of pagev member in
2329 * struct scrub_block
2330 */
2331 pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
2332 fs_info->chunk_root->nodesize,
2333 SCRUB_MAX_PAGES_PER_BLOCK,
2334 fs_info->chunk_root->sectorsize,
2335 SCRUB_MAX_PAGES_PER_BLOCK);
2336 return -EINVAL;
2337 }
2338
Arne Jansena2de7332011-03-08 14:14:00 +01002339 ret = scrub_workers_get(root);
2340 if (ret)
2341 return ret;
2342
2343 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2344 dev = btrfs_find_device(root, devid, NULL, NULL);
2345 if (!dev || dev->missing) {
2346 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2347 scrub_workers_put(root);
2348 return -ENODEV;
2349 }
2350 mutex_lock(&fs_info->scrub_lock);
2351
2352 if (!dev->in_fs_metadata) {
2353 mutex_unlock(&fs_info->scrub_lock);
2354 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2355 scrub_workers_put(root);
2356 return -ENODEV;
2357 }
2358
2359 if (dev->scrub_device) {
2360 mutex_unlock(&fs_info->scrub_lock);
2361 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2362 scrub_workers_put(root);
2363 return -EINPROGRESS;
2364 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002365 sctx = scrub_setup_ctx(dev);
2366 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002367 mutex_unlock(&fs_info->scrub_lock);
2368 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2369 scrub_workers_put(root);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002370 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002371 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002372 sctx->readonly = readonly;
2373 dev->scrub_device = sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002374
2375 atomic_inc(&fs_info->scrubs_running);
2376 mutex_unlock(&fs_info->scrub_lock);
2377 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2378
2379 down_read(&fs_info->scrub_super_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002380 ret = scrub_supers(sctx, dev);
Arne Jansena2de7332011-03-08 14:14:00 +01002381 up_read(&fs_info->scrub_super_lock);
2382
2383 if (!ret)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002384 ret = scrub_enumerate_chunks(sctx, dev, start, end);
Arne Jansena2de7332011-03-08 14:14:00 +01002385
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002386 wait_event(sctx->list_wait, atomic_read(&sctx->in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002387 atomic_dec(&fs_info->scrubs_running);
2388 wake_up(&fs_info->scrub_pause_wait);
2389
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002390 wait_event(sctx->list_wait, atomic_read(&sctx->fixup_cnt) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02002391
Arne Jansena2de7332011-03-08 14:14:00 +01002392 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002393 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002394
2395 mutex_lock(&fs_info->scrub_lock);
2396 dev->scrub_device = NULL;
2397 mutex_unlock(&fs_info->scrub_lock);
2398
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002399 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002400 scrub_workers_put(root);
2401
2402 return ret;
2403}
2404
Jeff Mahoney143bede2012-03-01 14:56:26 +01002405void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002406{
2407 struct btrfs_fs_info *fs_info = root->fs_info;
2408
2409 mutex_lock(&fs_info->scrub_lock);
2410 atomic_inc(&fs_info->scrub_pause_req);
2411 while (atomic_read(&fs_info->scrubs_paused) !=
2412 atomic_read(&fs_info->scrubs_running)) {
2413 mutex_unlock(&fs_info->scrub_lock);
2414 wait_event(fs_info->scrub_pause_wait,
2415 atomic_read(&fs_info->scrubs_paused) ==
2416 atomic_read(&fs_info->scrubs_running));
2417 mutex_lock(&fs_info->scrub_lock);
2418 }
2419 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002420}
2421
Jeff Mahoney143bede2012-03-01 14:56:26 +01002422void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002423{
2424 struct btrfs_fs_info *fs_info = root->fs_info;
2425
2426 atomic_dec(&fs_info->scrub_pause_req);
2427 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002428}
2429
Jeff Mahoney143bede2012-03-01 14:56:26 +01002430void btrfs_scrub_pause_super(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002431{
2432 down_write(&root->fs_info->scrub_super_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002433}
2434
Jeff Mahoney143bede2012-03-01 14:56:26 +01002435void btrfs_scrub_continue_super(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002436{
2437 up_write(&root->fs_info->scrub_super_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002438}
2439
Jeff Mahoney49b25e02012-03-01 17:24:58 +01002440int __btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002441{
Arne Jansena2de7332011-03-08 14:14:00 +01002442
2443 mutex_lock(&fs_info->scrub_lock);
2444 if (!atomic_read(&fs_info->scrubs_running)) {
2445 mutex_unlock(&fs_info->scrub_lock);
2446 return -ENOTCONN;
2447 }
2448
2449 atomic_inc(&fs_info->scrub_cancel_req);
2450 while (atomic_read(&fs_info->scrubs_running)) {
2451 mutex_unlock(&fs_info->scrub_lock);
2452 wait_event(fs_info->scrub_pause_wait,
2453 atomic_read(&fs_info->scrubs_running) == 0);
2454 mutex_lock(&fs_info->scrub_lock);
2455 }
2456 atomic_dec(&fs_info->scrub_cancel_req);
2457 mutex_unlock(&fs_info->scrub_lock);
2458
2459 return 0;
2460}
2461
Jeff Mahoney49b25e02012-03-01 17:24:58 +01002462int btrfs_scrub_cancel(struct btrfs_root *root)
2463{
2464 return __btrfs_scrub_cancel(root->fs_info);
2465}
2466
Arne Jansena2de7332011-03-08 14:14:00 +01002467int btrfs_scrub_cancel_dev(struct btrfs_root *root, struct btrfs_device *dev)
2468{
2469 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002470 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002471
2472 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002473 sctx = dev->scrub_device;
2474 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01002475 mutex_unlock(&fs_info->scrub_lock);
2476 return -ENOTCONN;
2477 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002478 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01002479 while (dev->scrub_device) {
2480 mutex_unlock(&fs_info->scrub_lock);
2481 wait_event(fs_info->scrub_pause_wait,
2482 dev->scrub_device == NULL);
2483 mutex_lock(&fs_info->scrub_lock);
2484 }
2485 mutex_unlock(&fs_info->scrub_lock);
2486
2487 return 0;
2488}
Stefan Behrens1623ede2012-03-27 14:21:26 -04002489
Arne Jansena2de7332011-03-08 14:14:00 +01002490int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
2491{
2492 struct btrfs_fs_info *fs_info = root->fs_info;
2493 struct btrfs_device *dev;
2494 int ret;
2495
2496 /*
2497 * we have to hold the device_list_mutex here so the device
2498 * does not go away in cancel_dev. FIXME: find a better solution
2499 */
2500 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2501 dev = btrfs_find_device(root, devid, NULL, NULL);
2502 if (!dev) {
2503 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2504 return -ENODEV;
2505 }
2506 ret = btrfs_scrub_cancel_dev(root, dev);
2507 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2508
2509 return ret;
2510}
2511
2512int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
2513 struct btrfs_scrub_progress *progress)
2514{
2515 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002516 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01002517
2518 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2519 dev = btrfs_find_device(root, devid, NULL, NULL);
2520 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002521 sctx = dev->scrub_device;
2522 if (sctx)
2523 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002524 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2525
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002526 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01002527}