blob: 53c3501fa4ca348f13cb17617b363643a59b1f6f [file] [log] [blame]
Arne Jansena2de7332011-03-08 14:14:00 +01001/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
Arne Jansena2de7332011-03-08 14:14:00 +01003 *
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 Behrensff023aa2012-11-06 11:43:11 +010028#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010029#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040030#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050031#include "raid56.h"
Arne Jansena2de7332011-03-08 14:14:00 +010032
33/*
34 * This is only the first step towards a full-features scrub. It reads all
35 * extent and super block and verifies the checksums. In case a bad checksum
36 * is found or the extent cannot be read, good data will be written back if
37 * any can be found.
38 *
39 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010040 * - In case an unrepairable extent is encountered, track which files are
41 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010042 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010043 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010044 */
45
Stefan Behrensb5d67f62012-03-27 14:21:27 -040046struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010047struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010048
Stefan Behrensff023aa2012-11-06 11:43:11 +010049/*
50 * the following three values only influence the performance.
51 * The last one configures the number of parallel and outstanding I/O
52 * operations. The first two values configure an upper limit for the number
53 * of (dynamically allocated) pages that are added to a bio.
54 */
55#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
56#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
57#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010058
59/*
60 * the following value times PAGE_SIZE needs to be large enough to match the
61 * largest node/leaf/sector size that shall be supported.
62 * Values larger than BTRFS_STRIPE_LEN are not supported.
63 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040064#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010065
66struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040067 struct scrub_block *sblock;
68 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020069 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010070 u64 flags; /* extent flags */
71 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040072 u64 logical;
73 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010074 u64 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +010075 atomic_t ref_count;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040076 struct {
77 unsigned int mirror_num:8;
78 unsigned int have_csum:1;
79 unsigned int io_error:1;
80 };
Arne Jansena2de7332011-03-08 14:14:00 +010081 u8 csum[BTRFS_CSUM_SIZE];
82};
83
84struct scrub_bio {
85 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010086 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010087 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010088 struct bio *bio;
89 int err;
90 u64 logical;
91 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010092#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
93 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
94#else
95 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
96#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -040097 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +010098 int next_free;
99 struct btrfs_work work;
100};
101
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400102struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100103 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400104 int page_count;
105 atomic_t outstanding_pages;
106 atomic_t ref_count; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100107 struct scrub_ctx *sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400108 struct {
109 unsigned int header_error:1;
110 unsigned int checksum_error:1;
111 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200112 unsigned int generation_error:1; /* also sets header_error */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400113 };
114};
115
Stefan Behrensff023aa2012-11-06 11:43:11 +0100116struct scrub_wr_ctx {
117 struct scrub_bio *wr_curr_bio;
118 struct btrfs_device *tgtdev;
119 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
120 atomic_t flush_all_writes;
121 struct mutex wr_lock;
122};
123
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100124struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100125 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100126 struct btrfs_root *dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +0100127 int first_free;
128 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100129 atomic_t bios_in_flight;
130 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100131 spinlock_t list_lock;
132 wait_queue_head_t list_wait;
133 u16 csum_size;
134 struct list_head csum_list;
135 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100136 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100137 int pages_per_rd_bio;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400138 u32 sectorsize;
139 u32 nodesize;
140 u32 leafsize;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100141
142 int is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100143 struct scrub_wr_ctx wr_ctx;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100144
Arne Jansena2de7332011-03-08 14:14:00 +0100145 /*
146 * statistics
147 */
148 struct btrfs_scrub_progress stat;
149 spinlock_t stat_lock;
150};
151
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200152struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100153 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100154 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200155 u64 logical;
156 struct btrfs_root *root;
157 struct btrfs_work work;
158 int mirror_num;
159};
160
Stefan Behrensff023aa2012-11-06 11:43:11 +0100161struct scrub_copy_nocow_ctx {
162 struct scrub_ctx *sctx;
163 u64 logical;
164 u64 len;
165 int mirror_num;
166 u64 physical_for_dev_replace;
167 struct btrfs_work work;
168};
169
Jan Schmidt558540c2011-06-13 19:59:12 +0200170struct scrub_warning {
171 struct btrfs_path *path;
172 u64 extent_item_size;
173 char *scratch_buf;
174 char *msg_buf;
175 const char *errstr;
176 sector_t sector;
177 u64 logical;
178 struct btrfs_device *dev;
179 int msg_bufsize;
180 int scratch_bufsize;
181};
182
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400183
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100184static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
185static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
186static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
187static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400188static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100189static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100190 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100191 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400192 u64 length, u64 logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100193 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100194static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
195 struct scrub_block *sblock, int is_metadata,
196 int have_csum, u8 *csum, u64 generation,
197 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400198static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
199 struct scrub_block *sblock,
200 int is_metadata, int have_csum,
201 const u8 *csum, u64 generation,
202 u16 csum_size);
203static void scrub_complete_bio_end_io(struct bio *bio, int err);
204static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
205 struct scrub_block *sblock_good,
206 int force_write);
207static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
208 struct scrub_block *sblock_good,
209 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100210static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
211static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
212 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400213static int scrub_checksum_data(struct scrub_block *sblock);
214static int scrub_checksum_tree_block(struct scrub_block *sblock);
215static int scrub_checksum_super(struct scrub_block *sblock);
216static void scrub_block_get(struct scrub_block *sblock);
217static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100218static void scrub_page_get(struct scrub_page *spage);
219static void scrub_page_put(struct scrub_page *spage);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100220static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
221 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100222static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100223 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100224 u64 gen, int mirror_num, u8 *csum, int force,
225 u64 physical_for_dev_replace);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400226static void scrub_bio_end_io(struct bio *bio, int err);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400227static void scrub_bio_end_io_worker(struct btrfs_work *work);
228static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100229static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
230 u64 extent_logical, u64 extent_len,
231 u64 *extent_physical,
232 struct btrfs_device **extent_dev,
233 int *extent_mirror_num);
234static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
235 struct scrub_wr_ctx *wr_ctx,
236 struct btrfs_fs_info *fs_info,
237 struct btrfs_device *dev,
238 int is_dev_replace);
239static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
240static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
241 struct scrub_page *spage);
242static void scrub_wr_submit(struct scrub_ctx *sctx);
243static void scrub_wr_bio_end_io(struct bio *bio, int err);
244static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
245static int write_page_nocow(struct scrub_ctx *sctx,
246 u64 physical_for_dev_replace, struct page *page);
247static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
248 void *ctx);
249static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
250 int mirror_num, u64 physical_for_dev_replace);
251static void copy_nocow_pages_worker(struct btrfs_work *work);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400252
253
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100254static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
255{
256 atomic_inc(&sctx->bios_in_flight);
257}
258
259static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
260{
261 atomic_dec(&sctx->bios_in_flight);
262 wake_up(&sctx->list_wait);
263}
264
265/*
266 * used for workers that require transaction commits (i.e., for the
267 * NOCOW case)
268 */
269static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
270{
271 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
272
273 /*
274 * increment scrubs_running to prevent cancel requests from
275 * completing as long as a worker is running. we must also
276 * increment scrubs_paused to prevent deadlocking on pause
277 * requests used for transactions commits (as the worker uses a
278 * transaction context). it is safe to regard the worker
279 * as paused for all matters practical. effectively, we only
280 * avoid cancellation requests from completing.
281 */
282 mutex_lock(&fs_info->scrub_lock);
283 atomic_inc(&fs_info->scrubs_running);
284 atomic_inc(&fs_info->scrubs_paused);
285 mutex_unlock(&fs_info->scrub_lock);
286 atomic_inc(&sctx->workers_pending);
287}
288
289/* used for workers that require transaction commits */
290static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
291{
292 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
293
294 /*
295 * see scrub_pending_trans_workers_inc() why we're pretending
296 * to be paused in the scrub counters
297 */
298 mutex_lock(&fs_info->scrub_lock);
299 atomic_dec(&fs_info->scrubs_running);
300 atomic_dec(&fs_info->scrubs_paused);
301 mutex_unlock(&fs_info->scrub_lock);
302 atomic_dec(&sctx->workers_pending);
303 wake_up(&fs_info->scrub_pause_wait);
304 wake_up(&sctx->list_wait);
305}
306
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100307static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100308{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100309 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100310 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100311 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100312 struct btrfs_ordered_sum, list);
313 list_del(&sum->list);
314 kfree(sum);
315 }
316}
317
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100318static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100319{
320 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100321
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100322 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100323 return;
324
Stefan Behrensff023aa2012-11-06 11:43:11 +0100325 scrub_free_wr_ctx(&sctx->wr_ctx);
326
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400327 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100328 if (sctx->curr != -1) {
329 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400330
331 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100332 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400333 scrub_block_put(sbio->pagev[i]->sblock);
334 }
335 bio_put(sbio->bio);
336 }
337
Stefan Behrensff023aa2012-11-06 11:43:11 +0100338 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100339 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100340
341 if (!sbio)
342 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100343 kfree(sbio);
344 }
345
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100346 scrub_free_csums(sctx);
347 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100348}
349
350static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100351struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100352{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100353 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100354 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100355 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100356 int pages_per_rd_bio;
357 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100358
Stefan Behrensff023aa2012-11-06 11:43:11 +0100359 /*
360 * the setting of pages_per_rd_bio is correct for scrub but might
361 * be wrong for the dev_replace code where we might read from
362 * different devices in the initial huge bios. However, that
363 * code is able to correctly handle the case when adding a page
364 * to a bio fails.
365 */
366 if (dev->bdev)
367 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
368 bio_get_nr_vecs(dev->bdev));
369 else
370 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100371 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
372 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100373 goto nomem;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100374 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100375 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100376 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100377 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100378 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100379 struct scrub_bio *sbio;
380
381 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
382 if (!sbio)
383 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100384 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100385
Arne Jansena2de7332011-03-08 14:14:00 +0100386 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100387 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400388 sbio->page_count = 0;
389 sbio->work.func = scrub_bio_end_io_worker;
Arne Jansena2de7332011-03-08 14:14:00 +0100390
Stefan Behrensff023aa2012-11-06 11:43:11 +0100391 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100392 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200393 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100394 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100395 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100396 sctx->first_free = 0;
397 sctx->nodesize = dev->dev_root->nodesize;
398 sctx->leafsize = dev->dev_root->leafsize;
399 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100400 atomic_set(&sctx->bios_in_flight, 0);
401 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100402 atomic_set(&sctx->cancel_req, 0);
403 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
404 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100405
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100406 spin_lock_init(&sctx->list_lock);
407 spin_lock_init(&sctx->stat_lock);
408 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100409
410 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
411 fs_info->dev_replace.tgtdev, is_dev_replace);
412 if (ret) {
413 scrub_free_ctx(sctx);
414 return ERR_PTR(ret);
415 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100416 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100417
418nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100419 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100420 return ERR_PTR(-ENOMEM);
421}
422
Stefan Behrensff023aa2012-11-06 11:43:11 +0100423static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
424 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200425{
426 u64 isize;
427 u32 nlink;
428 int ret;
429 int i;
430 struct extent_buffer *eb;
431 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100432 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200433 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
434 struct inode_fs_paths *ipath = NULL;
435 struct btrfs_root *local_root;
436 struct btrfs_key root_key;
437
438 root_key.objectid = root;
439 root_key.type = BTRFS_ROOT_ITEM_KEY;
440 root_key.offset = (u64)-1;
441 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
442 if (IS_ERR(local_root)) {
443 ret = PTR_ERR(local_root);
444 goto err;
445 }
446
447 ret = inode_item_info(inum, 0, local_root, swarn->path);
448 if (ret) {
449 btrfs_release_path(swarn->path);
450 goto err;
451 }
452
453 eb = swarn->path->nodes[0];
454 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
455 struct btrfs_inode_item);
456 isize = btrfs_inode_size(eb, inode_item);
457 nlink = btrfs_inode_nlink(eb, inode_item);
458 btrfs_release_path(swarn->path);
459
460 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300461 if (IS_ERR(ipath)) {
462 ret = PTR_ERR(ipath);
463 ipath = NULL;
464 goto err;
465 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200466 ret = paths_from_inode(inum, ipath);
467
468 if (ret < 0)
469 goto err;
470
471 /*
472 * we deliberately ignore the bit ipath might have been too small to
473 * hold all of the paths here
474 */
475 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Josef Bacik606686e2012-06-04 14:03:51 -0400476 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200477 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
478 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400479 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200480 (unsigned long long)swarn->sector, root, inum, offset,
481 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500482 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200483
484 free_ipath(ipath);
485 return 0;
486
487err:
Josef Bacik606686e2012-06-04 14:03:51 -0400488 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200489 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
490 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400491 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200492 (unsigned long long)swarn->sector, root, inum, offset, ret);
493
494 free_ipath(ipath);
495 return 0;
496}
497
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400498static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200499{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100500 struct btrfs_device *dev;
501 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200502 struct btrfs_path *path;
503 struct btrfs_key found_key;
504 struct extent_buffer *eb;
505 struct btrfs_extent_item *ei;
506 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200507 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100508 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600509 u64 flags = 0;
510 u64 ref_root;
511 u32 item_size;
512 u8 ref_level;
513 const int bufsize = 4096;
514 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200515
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100516 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100517 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100518 fs_info = sblock->sctx->dev_root->fs_info;
519
Jan Schmidt558540c2011-06-13 19:59:12 +0200520 path = btrfs_alloc_path();
521
522 swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
523 swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100524 swarn.sector = (sblock->pagev[0]->physical) >> 9;
525 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200526 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100527 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200528 swarn.msg_bufsize = bufsize;
529 swarn.scratch_bufsize = bufsize;
530
531 if (!path || !swarn.scratch_buf || !swarn.msg_buf)
532 goto out;
533
Liu Bo69917e42012-09-07 20:01:28 -0600534 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
535 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200536 if (ret < 0)
537 goto out;
538
Jan Schmidt4692cf52011-12-02 14:56:41 +0100539 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200540 swarn.extent_item_size = found_key.offset;
541
542 eb = path->nodes[0];
543 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
544 item_size = btrfs_item_size_nr(eb, path->slots[0]);
Jan Schmidt4692cf52011-12-02 14:56:41 +0100545 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200546
Liu Bo69917e42012-09-07 20:01:28 -0600547 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200548 do {
549 ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
550 &ref_root, &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400551 printk_in_rcu(KERN_WARNING
Stefan Behrens1623ede2012-03-27 14:21:26 -0400552 "btrfs: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200553 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400554 "%llu\n", errstr, swarn.logical,
555 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200556 (unsigned long long)swarn.sector,
557 ref_level ? "node" : "leaf",
558 ret < 0 ? -1 : ref_level,
559 ret < 0 ? -1 : ref_root);
560 } while (ret != 1);
561 } else {
562 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100563 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100564 iterate_extent_inodes(fs_info, found_key.objectid,
565 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200566 scrub_print_warning_inode, &swarn);
567 }
568
569out:
570 btrfs_free_path(path);
571 kfree(swarn.scratch_buf);
572 kfree(swarn.msg_buf);
573}
574
Stefan Behrensff023aa2012-11-06 11:43:11 +0100575static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200576{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200577 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200578 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100579 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200580 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200581 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200582 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200583 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000584 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200585 u64 end = offset + PAGE_SIZE - 1;
586 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000587 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200588
589 key.objectid = root;
590 key.type = BTRFS_ROOT_ITEM_KEY;
591 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000592
593 fs_info = fixup->root->fs_info;
594 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
595
596 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
597 if (IS_ERR(local_root)) {
598 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200599 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000600 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200601
602 key.type = BTRFS_INODE_ITEM_KEY;
603 key.objectid = inum;
604 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000605 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
606 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200607 if (IS_ERR(inode))
608 return PTR_ERR(inode);
609
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200610 index = offset >> PAGE_CACHE_SHIFT;
611
612 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200613 if (!page) {
614 ret = -ENOMEM;
615 goto out;
616 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200617
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200618 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200619 if (PageDirty(page)) {
620 /*
621 * we need to write the data to the defect sector. the
622 * data that was in that sector is not in memory,
623 * because the page was modified. we must not write the
624 * modified page to that sector.
625 *
626 * TODO: what could be done here: wait for the delalloc
627 * runner to write out that page (might involve
628 * COW) and see whether the sector is still
629 * referenced afterwards.
630 *
631 * For the meantime, we'll treat this error
632 * incorrectable, although there is a chance that a
633 * later scrub will find the bad sector again and that
634 * there's no dirty page in memory, then.
635 */
636 ret = -EIO;
637 goto out;
638 }
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100639 fs_info = BTRFS_I(inode)->root->fs_info;
640 ret = repair_io_failure(fs_info, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200641 fixup->logical, page,
642 fixup->mirror_num);
643 unlock_page(page);
644 corrected = !ret;
645 } else {
646 /*
647 * we need to get good data first. the general readpage path
648 * will call repair_io_failure for us, we just have to make
649 * sure we read the bad mirror.
650 */
651 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
652 EXTENT_DAMAGED, GFP_NOFS);
653 if (ret) {
654 /* set_extent_bits should give proper error */
655 WARN_ON(ret > 0);
656 if (ret > 0)
657 ret = -EFAULT;
658 goto out;
659 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200660
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200661 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
662 btrfs_get_extent,
663 fixup->mirror_num);
664 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200665
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200666 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
667 end, EXTENT_DAMAGED, 0, NULL);
668 if (!corrected)
669 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
670 EXTENT_DAMAGED, GFP_NOFS);
671 }
672
673out:
674 if (page)
675 put_page(page);
676 if (inode)
677 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200678
679 if (ret < 0)
680 return ret;
681
682 if (ret == 0 && corrected) {
683 /*
684 * we only need to call readpage for one of the inodes belonging
685 * to this extent. so make iterate_extent_inodes stop
686 */
687 return 1;
688 }
689
690 return -EIO;
691}
692
693static void scrub_fixup_nodatasum(struct btrfs_work *work)
694{
695 int ret;
696 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100697 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200698 struct btrfs_trans_handle *trans = NULL;
699 struct btrfs_fs_info *fs_info;
700 struct btrfs_path *path;
701 int uncorrectable = 0;
702
703 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100704 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200705 fs_info = fixup->root->fs_info;
706
707 path = btrfs_alloc_path();
708 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100709 spin_lock(&sctx->stat_lock);
710 ++sctx->stat.malloc_errors;
711 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200712 uncorrectable = 1;
713 goto out;
714 }
715
716 trans = btrfs_join_transaction(fixup->root);
717 if (IS_ERR(trans)) {
718 uncorrectable = 1;
719 goto out;
720 }
721
722 /*
723 * the idea is to trigger a regular read through the standard path. we
724 * read a page from the (failed) logical address by specifying the
725 * corresponding copynum of the failed sector. thus, that readpage is
726 * expected to fail.
727 * that is the point where on-the-fly error correction will kick in
728 * (once it's finished) and rewrite the failed sector if a good copy
729 * can be found.
730 */
731 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
732 path, scrub_fixup_readpage,
733 fixup);
734 if (ret < 0) {
735 uncorrectable = 1;
736 goto out;
737 }
738 WARN_ON(ret != 1);
739
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100740 spin_lock(&sctx->stat_lock);
741 ++sctx->stat.corrected_errors;
742 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200743
744out:
745 if (trans && !IS_ERR(trans))
746 btrfs_end_transaction(trans, fixup->root);
747 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100748 spin_lock(&sctx->stat_lock);
749 ++sctx->stat.uncorrectable_errors;
750 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100751 btrfs_dev_replace_stats_inc(
752 &sctx->dev_root->fs_info->dev_replace.
753 num_uncorrectable_read_errors);
Josef Bacik606686e2012-06-04 14:03:51 -0400754 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400755 "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -0400756 (unsigned long long)fixup->logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100757 rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200758 }
759
760 btrfs_free_path(path);
761 kfree(fixup);
762
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100763 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200764}
765
Arne Jansena2de7332011-03-08 14:14:00 +0100766/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400767 * scrub_handle_errored_block gets called when either verification of the
768 * pages failed or the bio failed to read, e.g. with EIO. In the latter
769 * case, this function handles all pages in the bio, even though only one
770 * may be bad.
771 * The goal of this function is to repair the errored block by using the
772 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100773 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400774static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100775{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100776 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100777 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400778 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100779 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400780 u64 logical;
781 u64 generation;
782 unsigned int failed_mirror_index;
783 unsigned int is_metadata;
784 unsigned int have_csum;
785 u8 *csum;
786 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
787 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100788 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400789 int mirror_index;
790 int page_num;
791 int success;
792 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
793 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100794
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400795 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100796 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000797 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
798 /*
799 * if we find an error in a super block, we just report it.
800 * They will get written with the next transaction commit
801 * anyway
802 */
803 spin_lock(&sctx->stat_lock);
804 ++sctx->stat.super_errors;
805 spin_unlock(&sctx->stat_lock);
806 return 0;
807 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400808 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100809 logical = sblock_to_check->pagev[0]->logical;
810 generation = sblock_to_check->pagev[0]->generation;
811 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
812 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
813 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400814 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100815 have_csum = sblock_to_check->pagev[0]->have_csum;
816 csum = sblock_to_check->pagev[0]->csum;
817 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400818
Stefan Behrensff023aa2012-11-06 11:43:11 +0100819 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
820 sblocks_for_recheck = NULL;
821 goto nodatasum_case;
822 }
823
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400824 /*
825 * read all mirrors one after the other. This includes to
826 * re-read the extent or metadata block that failed (that was
827 * the cause that this fixup code is called) another time,
828 * page by page this time in order to know which pages
829 * caused I/O errors and which ones are good (for all mirrors).
830 * It is the goal to handle the situation when more than one
831 * mirror contains I/O errors, but the errors do not
832 * overlap, i.e. the data can be repaired by selecting the
833 * pages from those mirrors without I/O error on the
834 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
835 * would be that mirror #1 has an I/O error on the first page,
836 * the second page is good, and mirror #2 has an I/O error on
837 * the second page, but the first page is good.
838 * Then the first page of the first mirror can be repaired by
839 * taking the first page of the second mirror, and the
840 * second page of the second mirror can be repaired by
841 * copying the contents of the 2nd page of the 1st mirror.
842 * One more note: if the pages of one mirror contain I/O
843 * errors, the checksum cannot be verified. In order to get
844 * the best data for repairing, the first attempt is to find
845 * a mirror without I/O errors and with a validated checksum.
846 * Only if this is not possible, the pages are picked from
847 * mirrors with I/O errors without considering the checksum.
848 * If the latter is the case, at the end, the checksum of the
849 * repaired area is verified in order to correctly maintain
850 * the statistics.
851 */
852
853 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
854 sizeof(*sblocks_for_recheck),
855 GFP_NOFS);
856 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100857 spin_lock(&sctx->stat_lock);
858 sctx->stat.malloc_errors++;
859 sctx->stat.read_errors++;
860 sctx->stat.uncorrectable_errors++;
861 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100862 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400863 goto out;
864 }
865
866 /* setup the context, map the logical blocks and alloc the pages */
Stefan Behrensff023aa2012-11-06 11:43:11 +0100867 ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400868 logical, sblocks_for_recheck);
869 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100870 spin_lock(&sctx->stat_lock);
871 sctx->stat.read_errors++;
872 sctx->stat.uncorrectable_errors++;
873 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100874 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400875 goto out;
876 }
877 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
878 sblock_bad = sblocks_for_recheck + failed_mirror_index;
879
880 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100881 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
882 csum, generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400883
884 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
885 sblock_bad->no_io_error_seen) {
886 /*
887 * the error disappeared after reading page by page, or
888 * the area was part of a huge bio and other parts of the
889 * bio caused I/O errors, or the block layer merged several
890 * read requests into one and the error is caused by a
891 * different bio (usually one of the two latter cases is
892 * the cause)
893 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100894 spin_lock(&sctx->stat_lock);
895 sctx->stat.unverified_errors++;
896 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400897
Stefan Behrensff023aa2012-11-06 11:43:11 +0100898 if (sctx->is_dev_replace)
899 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400900 goto out;
901 }
902
903 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100904 spin_lock(&sctx->stat_lock);
905 sctx->stat.read_errors++;
906 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400907 if (__ratelimit(&_rs))
908 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100909 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400910 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100911 spin_lock(&sctx->stat_lock);
912 sctx->stat.csum_errors++;
913 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400914 if (__ratelimit(&_rs))
915 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100916 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200917 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400918 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100919 spin_lock(&sctx->stat_lock);
920 sctx->stat.verify_errors++;
921 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400922 if (__ratelimit(&_rs))
923 scrub_print_warning("checksum/header error",
924 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200925 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100926 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200927 BTRFS_DEV_STAT_GENERATION_ERRS);
928 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100929 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200930 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400931 }
932
Stefan Behrensff023aa2012-11-06 11:43:11 +0100933 if (sctx->readonly && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400934 goto did_not_correct_error;
935
936 if (!is_metadata && !have_csum) {
937 struct scrub_fixup_nodatasum *fixup_nodatasum;
938
Stefan Behrensff023aa2012-11-06 11:43:11 +0100939nodatasum_case:
940 WARN_ON(sctx->is_dev_replace);
941
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400942 /*
943 * !is_metadata and !have_csum, this means that the data
944 * might not be COW'ed, that it might be modified
945 * concurrently. The general strategy to work on the
946 * commit root does not help in the case when COW is not
947 * used.
948 */
949 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
950 if (!fixup_nodatasum)
951 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100952 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100953 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400954 fixup_nodatasum->logical = logical;
955 fixup_nodatasum->root = fs_info->extent_root;
956 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100957 scrub_pending_trans_workers_inc(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400958 fixup_nodatasum->work.func = scrub_fixup_nodatasum;
959 btrfs_queue_worker(&fs_info->scrub_workers,
960 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +0100961 goto out;
962 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400963
964 /*
965 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100966 * checksums.
967 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400968 * errors and also does not have a checksum error.
969 * If one is found, and if a checksum is present, the full block
970 * that is known to contain an error is rewritten. Afterwards
971 * the block is known to be corrected.
972 * If a mirror is found which is completely correct, and no
973 * checksum is present, only those pages are rewritten that had
974 * an I/O error in the block to be repaired, since it cannot be
975 * determined, which copy of the other pages is better (and it
976 * could happen otherwise that a correct page would be
977 * overwritten by a bad one).
978 */
979 for (mirror_index = 0;
980 mirror_index < BTRFS_MAX_MIRRORS &&
981 sblocks_for_recheck[mirror_index].page_count > 0;
982 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100983 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400984
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100985 if (mirror_index == failed_mirror_index)
986 continue;
987 sblock_other = sblocks_for_recheck + mirror_index;
988
989 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100990 scrub_recheck_block(fs_info, sblock_other, is_metadata,
991 have_csum, csum, generation,
992 sctx->csum_size);
993
994 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400995 !sblock_other->checksum_error &&
996 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100997 if (sctx->is_dev_replace) {
998 scrub_write_block_to_dev_replace(sblock_other);
999 } else {
1000 int force_write = is_metadata || have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001001
Stefan Behrensff023aa2012-11-06 11:43:11 +01001002 ret = scrub_repair_block_from_good_copy(
1003 sblock_bad, sblock_other,
1004 force_write);
1005 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001006 if (0 == ret)
1007 goto corrected_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001008 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001009 }
1010
1011 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001012 * for dev_replace, pick good pages and write to the target device.
1013 */
1014 if (sctx->is_dev_replace) {
1015 success = 1;
1016 for (page_num = 0; page_num < sblock_bad->page_count;
1017 page_num++) {
1018 int sub_success;
1019
1020 sub_success = 0;
1021 for (mirror_index = 0;
1022 mirror_index < BTRFS_MAX_MIRRORS &&
1023 sblocks_for_recheck[mirror_index].page_count > 0;
1024 mirror_index++) {
1025 struct scrub_block *sblock_other =
1026 sblocks_for_recheck + mirror_index;
1027 struct scrub_page *page_other =
1028 sblock_other->pagev[page_num];
1029
1030 if (!page_other->io_error) {
1031 ret = scrub_write_page_to_dev_replace(
1032 sblock_other, page_num);
1033 if (ret == 0) {
1034 /* succeeded for this page */
1035 sub_success = 1;
1036 break;
1037 } else {
1038 btrfs_dev_replace_stats_inc(
1039 &sctx->dev_root->
1040 fs_info->dev_replace.
1041 num_write_errors);
1042 }
1043 }
1044 }
1045
1046 if (!sub_success) {
1047 /*
1048 * did not find a mirror to fetch the page
1049 * from. scrub_write_page_to_dev_replace()
1050 * handles this case (page->io_error), by
1051 * filling the block with zeros before
1052 * submitting the write request
1053 */
1054 success = 0;
1055 ret = scrub_write_page_to_dev_replace(
1056 sblock_bad, page_num);
1057 if (ret)
1058 btrfs_dev_replace_stats_inc(
1059 &sctx->dev_root->fs_info->
1060 dev_replace.num_write_errors);
1061 }
1062 }
1063
1064 goto out;
1065 }
1066
1067 /*
1068 * for regular scrub, repair those pages that are errored.
1069 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001070 * repaired, continue by picking good copies of those pages.
1071 * Select the good pages from mirrors to rewrite bad pages from
1072 * the area to fix. Afterwards verify the checksum of the block
1073 * that is supposed to be repaired. This verification step is
1074 * only done for the purpose of statistic counting and for the
1075 * final scrub report, whether errors remain.
1076 * A perfect algorithm could make use of the checksum and try
1077 * all possible combinations of pages from the different mirrors
1078 * until the checksum verification succeeds. For example, when
1079 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1080 * of mirror #2 is readable but the final checksum test fails,
1081 * then the 2nd page of mirror #3 could be tried, whether now
1082 * the final checksum succeedes. But this would be a rare
1083 * exception and is therefore not implemented. At least it is
1084 * avoided that the good copy is overwritten.
1085 * A more useful improvement would be to pick the sectors
1086 * without I/O error based on sector sizes (512 bytes on legacy
1087 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1088 * mirror could be repaired by taking 512 byte of a different
1089 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1090 * area are unreadable.
1091 */
1092
1093 /* can only fix I/O errors from here on */
1094 if (sblock_bad->no_io_error_seen)
1095 goto did_not_correct_error;
1096
1097 success = 1;
1098 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001099 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001100
1101 if (!page_bad->io_error)
1102 continue;
1103
1104 for (mirror_index = 0;
1105 mirror_index < BTRFS_MAX_MIRRORS &&
1106 sblocks_for_recheck[mirror_index].page_count > 0;
1107 mirror_index++) {
1108 struct scrub_block *sblock_other = sblocks_for_recheck +
1109 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001110 struct scrub_page *page_other = sblock_other->pagev[
1111 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001112
1113 if (!page_other->io_error) {
1114 ret = scrub_repair_page_from_good_copy(
1115 sblock_bad, sblock_other, page_num, 0);
1116 if (0 == ret) {
1117 page_bad->io_error = 0;
1118 break; /* succeeded for this page */
1119 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001120 }
1121 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001122
1123 if (page_bad->io_error) {
1124 /* did not find a mirror to copy the page from */
1125 success = 0;
1126 }
1127 }
1128
1129 if (success) {
1130 if (is_metadata || have_csum) {
1131 /*
1132 * need to verify the checksum now that all
1133 * sectors on disk are repaired (the write
1134 * request for data to be repaired is on its way).
1135 * Just be lazy and use scrub_recheck_block()
1136 * which re-reads the data before the checksum
1137 * is verified, but most likely the data comes out
1138 * of the page cache.
1139 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001140 scrub_recheck_block(fs_info, sblock_bad,
1141 is_metadata, have_csum, csum,
1142 generation, sctx->csum_size);
1143 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001144 !sblock_bad->checksum_error &&
1145 sblock_bad->no_io_error_seen)
1146 goto corrected_error;
1147 else
1148 goto did_not_correct_error;
1149 } else {
1150corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001151 spin_lock(&sctx->stat_lock);
1152 sctx->stat.corrected_errors++;
1153 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001154 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001155 "btrfs: fixed up error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -04001156 (unsigned long long)logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001157 rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001158 }
1159 } else {
1160did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001161 spin_lock(&sctx->stat_lock);
1162 sctx->stat.uncorrectable_errors++;
1163 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001164 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001165 "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -04001166 (unsigned long long)logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001167 rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001168 }
1169
1170out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001171 if (sblocks_for_recheck) {
1172 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1173 mirror_index++) {
1174 struct scrub_block *sblock = sblocks_for_recheck +
1175 mirror_index;
1176 int page_index;
1177
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001178 for (page_index = 0; page_index < sblock->page_count;
1179 page_index++) {
1180 sblock->pagev[page_index]->sblock = NULL;
1181 scrub_page_put(sblock->pagev[page_index]);
1182 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001183 }
1184 kfree(sblocks_for_recheck);
1185 }
1186
1187 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001188}
1189
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001190static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001191 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001192 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001193 u64 length, u64 logical,
1194 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001195{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001196 int page_index;
1197 int mirror_index;
1198 int ret;
1199
1200 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001201 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001202 * are not used (and not set) in the blocks that are used for
1203 * the recheck procedure
1204 */
1205
1206 page_index = 0;
1207 while (length > 0) {
1208 u64 sublen = min_t(u64, length, PAGE_SIZE);
1209 u64 mapped_length = sublen;
1210 struct btrfs_bio *bbio = NULL;
1211
1212 /*
1213 * with a length of PAGE_SIZE, each returned stripe
1214 * represents one mirror
1215 */
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01001216 ret = btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, logical,
1217 &mapped_length, &bbio, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001218 if (ret || !bbio || mapped_length < sublen) {
1219 kfree(bbio);
1220 return -EIO;
1221 }
1222
Stefan Behrensff023aa2012-11-06 11:43:11 +01001223 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001224 for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
1225 mirror_index++) {
1226 struct scrub_block *sblock;
1227 struct scrub_page *page;
1228
1229 if (mirror_index >= BTRFS_MAX_MIRRORS)
1230 continue;
1231
1232 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001233 sblock->sctx = sctx;
1234 page = kzalloc(sizeof(*page), GFP_NOFS);
1235 if (!page) {
1236leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001237 spin_lock(&sctx->stat_lock);
1238 sctx->stat.malloc_errors++;
1239 spin_unlock(&sctx->stat_lock);
Wei Yongjuncf93dcc2012-09-02 07:44:51 -06001240 kfree(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001241 return -ENOMEM;
1242 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001243 scrub_page_get(page);
1244 sblock->pagev[page_index] = page;
1245 page->logical = logical;
1246 page->physical = bbio->stripes[mirror_index].physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001247 BUG_ON(page_index >= original_sblock->page_count);
1248 page->physical_for_dev_replace =
1249 original_sblock->pagev[page_index]->
1250 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001251 /* for missing devices, dev->bdev is NULL */
1252 page->dev = bbio->stripes[mirror_index].dev;
1253 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001254 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001255 page->page = alloc_page(GFP_NOFS);
1256 if (!page->page)
1257 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001258 }
1259 kfree(bbio);
1260 length -= sublen;
1261 logical += sublen;
1262 page_index++;
1263 }
1264
1265 return 0;
1266}
1267
1268/*
1269 * this function will check the on disk data for checksum errors, header
1270 * errors and read I/O errors. If any I/O errors happen, the exact pages
1271 * which are errored are marked as being bad. The goal is to enable scrub
1272 * to take those pages that are not errored from all the mirrors so that
1273 * the pages that are errored in the just handled mirror can be repaired.
1274 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001275static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1276 struct scrub_block *sblock, int is_metadata,
1277 int have_csum, u8 *csum, u64 generation,
1278 u16 csum_size)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001279{
1280 int page_num;
1281
1282 sblock->no_io_error_seen = 1;
1283 sblock->header_error = 0;
1284 sblock->checksum_error = 0;
1285
1286 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1287 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001288 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001289 DECLARE_COMPLETION_ONSTACK(complete);
1290
Stefan Behrens442a4f62012-05-25 16:06:08 +02001291 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001292 page->io_error = 1;
1293 sblock->no_io_error_seen = 0;
1294 continue;
1295 }
1296
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001297 WARN_ON(!page->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001298 bio = bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001299 if (!bio) {
1300 page->io_error = 1;
1301 sblock->no_io_error_seen = 0;
1302 continue;
1303 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001304 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001305 bio->bi_sector = page->physical >> 9;
1306 bio->bi_end_io = scrub_complete_bio_end_io;
1307 bio->bi_private = &complete;
1308
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001309 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001310 btrfsic_submit_bio(READ, bio);
1311
1312 /* this will also unplug the queue */
1313 wait_for_completion(&complete);
1314
1315 page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
1316 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1317 sblock->no_io_error_seen = 0;
1318 bio_put(bio);
1319 }
1320
1321 if (sblock->no_io_error_seen)
1322 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1323 have_csum, csum, generation,
1324 csum_size);
1325
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001326 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001327}
1328
1329static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1330 struct scrub_block *sblock,
1331 int is_metadata, int have_csum,
1332 const u8 *csum, u64 generation,
1333 u16 csum_size)
1334{
1335 int page_num;
1336 u8 calculated_csum[BTRFS_CSUM_SIZE];
1337 u32 crc = ~(u32)0;
1338 struct btrfs_root *root = fs_info->extent_root;
1339 void *mapped_buffer;
1340
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001341 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001342 if (is_metadata) {
1343 struct btrfs_header *h;
1344
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001345 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001346 h = (struct btrfs_header *)mapped_buffer;
1347
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001348 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001349 memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
1350 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001351 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001352 sblock->header_error = 1;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001353 } else if (generation != le64_to_cpu(h->generation)) {
1354 sblock->header_error = 1;
1355 sblock->generation_error = 1;
1356 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001357 csum = h->csum;
1358 } else {
1359 if (!have_csum)
1360 return;
1361
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001362 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001363 }
1364
1365 for (page_num = 0;;) {
1366 if (page_num == 0 && is_metadata)
1367 crc = btrfs_csum_data(root,
1368 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1369 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1370 else
1371 crc = btrfs_csum_data(root, mapped_buffer, crc,
1372 PAGE_SIZE);
1373
Linus Torvalds9613beb2012-03-30 12:44:29 -07001374 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001375 page_num++;
1376 if (page_num >= sblock->page_count)
1377 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001378 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001379
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001380 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001381 }
1382
1383 btrfs_csum_final(crc, calculated_csum);
1384 if (memcmp(calculated_csum, csum, csum_size))
1385 sblock->checksum_error = 1;
1386}
1387
1388static void scrub_complete_bio_end_io(struct bio *bio, int err)
1389{
1390 complete((struct completion *)bio->bi_private);
1391}
1392
1393static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1394 struct scrub_block *sblock_good,
1395 int force_write)
1396{
1397 int page_num;
1398 int ret = 0;
1399
1400 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1401 int ret_sub;
1402
1403 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1404 sblock_good,
1405 page_num,
1406 force_write);
1407 if (ret_sub)
1408 ret = ret_sub;
1409 }
1410
1411 return ret;
1412}
1413
1414static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1415 struct scrub_block *sblock_good,
1416 int page_num, int force_write)
1417{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001418 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1419 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001420
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001421 BUG_ON(page_bad->page == NULL);
1422 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001423 if (force_write || sblock_bad->header_error ||
1424 sblock_bad->checksum_error || page_bad->io_error) {
1425 struct bio *bio;
1426 int ret;
1427 DECLARE_COMPLETION_ONSTACK(complete);
1428
Stefan Behrensff023aa2012-11-06 11:43:11 +01001429 if (!page_bad->dev->bdev) {
1430 printk_ratelimited(KERN_WARNING
1431 "btrfs: scrub_repair_page_from_good_copy(bdev == NULL) is unexpected!\n");
1432 return -EIO;
1433 }
1434
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001435 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001436 if (!bio)
1437 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001438 bio->bi_bdev = page_bad->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001439 bio->bi_sector = page_bad->physical >> 9;
1440 bio->bi_end_io = scrub_complete_bio_end_io;
1441 bio->bi_private = &complete;
1442
1443 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1444 if (PAGE_SIZE != ret) {
1445 bio_put(bio);
1446 return -EIO;
1447 }
1448 btrfsic_submit_bio(WRITE, bio);
1449
1450 /* this will also unplug the queue */
1451 wait_for_completion(&complete);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001452 if (!bio_flagged(bio, BIO_UPTODATE)) {
1453 btrfs_dev_stat_inc_and_print(page_bad->dev,
1454 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001455 btrfs_dev_replace_stats_inc(
1456 &sblock_bad->sctx->dev_root->fs_info->
1457 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001458 bio_put(bio);
1459 return -EIO;
1460 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001461 bio_put(bio);
1462 }
1463
1464 return 0;
1465}
1466
Stefan Behrensff023aa2012-11-06 11:43:11 +01001467static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1468{
1469 int page_num;
1470
1471 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1472 int ret;
1473
1474 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1475 if (ret)
1476 btrfs_dev_replace_stats_inc(
1477 &sblock->sctx->dev_root->fs_info->dev_replace.
1478 num_write_errors);
1479 }
1480}
1481
1482static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1483 int page_num)
1484{
1485 struct scrub_page *spage = sblock->pagev[page_num];
1486
1487 BUG_ON(spage->page == NULL);
1488 if (spage->io_error) {
1489 void *mapped_buffer = kmap_atomic(spage->page);
1490
1491 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1492 flush_dcache_page(spage->page);
1493 kunmap_atomic(mapped_buffer);
1494 }
1495 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1496}
1497
1498static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1499 struct scrub_page *spage)
1500{
1501 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1502 struct scrub_bio *sbio;
1503 int ret;
1504
1505 mutex_lock(&wr_ctx->wr_lock);
1506again:
1507 if (!wr_ctx->wr_curr_bio) {
1508 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1509 GFP_NOFS);
1510 if (!wr_ctx->wr_curr_bio) {
1511 mutex_unlock(&wr_ctx->wr_lock);
1512 return -ENOMEM;
1513 }
1514 wr_ctx->wr_curr_bio->sctx = sctx;
1515 wr_ctx->wr_curr_bio->page_count = 0;
1516 }
1517 sbio = wr_ctx->wr_curr_bio;
1518 if (sbio->page_count == 0) {
1519 struct bio *bio;
1520
1521 sbio->physical = spage->physical_for_dev_replace;
1522 sbio->logical = spage->logical;
1523 sbio->dev = wr_ctx->tgtdev;
1524 bio = sbio->bio;
1525 if (!bio) {
1526 bio = bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
1527 if (!bio) {
1528 mutex_unlock(&wr_ctx->wr_lock);
1529 return -ENOMEM;
1530 }
1531 sbio->bio = bio;
1532 }
1533
1534 bio->bi_private = sbio;
1535 bio->bi_end_io = scrub_wr_bio_end_io;
1536 bio->bi_bdev = sbio->dev->bdev;
1537 bio->bi_sector = sbio->physical >> 9;
1538 sbio->err = 0;
1539 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1540 spage->physical_for_dev_replace ||
1541 sbio->logical + sbio->page_count * PAGE_SIZE !=
1542 spage->logical) {
1543 scrub_wr_submit(sctx);
1544 goto again;
1545 }
1546
1547 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1548 if (ret != PAGE_SIZE) {
1549 if (sbio->page_count < 1) {
1550 bio_put(sbio->bio);
1551 sbio->bio = NULL;
1552 mutex_unlock(&wr_ctx->wr_lock);
1553 return -EIO;
1554 }
1555 scrub_wr_submit(sctx);
1556 goto again;
1557 }
1558
1559 sbio->pagev[sbio->page_count] = spage;
1560 scrub_page_get(spage);
1561 sbio->page_count++;
1562 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1563 scrub_wr_submit(sctx);
1564 mutex_unlock(&wr_ctx->wr_lock);
1565
1566 return 0;
1567}
1568
1569static void scrub_wr_submit(struct scrub_ctx *sctx)
1570{
1571 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1572 struct scrub_bio *sbio;
1573
1574 if (!wr_ctx->wr_curr_bio)
1575 return;
1576
1577 sbio = wr_ctx->wr_curr_bio;
1578 wr_ctx->wr_curr_bio = NULL;
1579 WARN_ON(!sbio->bio->bi_bdev);
1580 scrub_pending_bio_inc(sctx);
1581 /* process all writes in a single worker thread. Then the block layer
1582 * orders the requests before sending them to the driver which
1583 * doubled the write performance on spinning disks when measured
1584 * with Linux 3.5 */
1585 btrfsic_submit_bio(WRITE, sbio->bio);
1586}
1587
1588static void scrub_wr_bio_end_io(struct bio *bio, int err)
1589{
1590 struct scrub_bio *sbio = bio->bi_private;
1591 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1592
1593 sbio->err = err;
1594 sbio->bio = bio;
1595
1596 sbio->work.func = scrub_wr_bio_end_io_worker;
1597 btrfs_queue_worker(&fs_info->scrub_wr_completion_workers, &sbio->work);
1598}
1599
1600static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1601{
1602 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1603 struct scrub_ctx *sctx = sbio->sctx;
1604 int i;
1605
1606 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1607 if (sbio->err) {
1608 struct btrfs_dev_replace *dev_replace =
1609 &sbio->sctx->dev_root->fs_info->dev_replace;
1610
1611 for (i = 0; i < sbio->page_count; i++) {
1612 struct scrub_page *spage = sbio->pagev[i];
1613
1614 spage->io_error = 1;
1615 btrfs_dev_replace_stats_inc(&dev_replace->
1616 num_write_errors);
1617 }
1618 }
1619
1620 for (i = 0; i < sbio->page_count; i++)
1621 scrub_page_put(sbio->pagev[i]);
1622
1623 bio_put(sbio->bio);
1624 kfree(sbio);
1625 scrub_pending_bio_dec(sctx);
1626}
1627
1628static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001629{
1630 u64 flags;
1631 int ret;
1632
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001633 WARN_ON(sblock->page_count < 1);
1634 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001635 ret = 0;
1636 if (flags & BTRFS_EXTENT_FLAG_DATA)
1637 ret = scrub_checksum_data(sblock);
1638 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1639 ret = scrub_checksum_tree_block(sblock);
1640 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1641 (void)scrub_checksum_super(sblock);
1642 else
1643 WARN_ON(1);
1644 if (ret)
1645 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001646
1647 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001648}
1649
1650static int scrub_checksum_data(struct scrub_block *sblock)
1651{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001652 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001653 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001654 u8 *on_disk_csum;
1655 struct page *page;
1656 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001657 u32 crc = ~(u32)0;
1658 int fail = 0;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001659 struct btrfs_root *root = sctx->dev_root;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001660 u64 len;
1661 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001662
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001663 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001664 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001665 return 0;
1666
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001667 on_disk_csum = sblock->pagev[0]->csum;
1668 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001669 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001670
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001671 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001672 index = 0;
1673 for (;;) {
1674 u64 l = min_t(u64, len, PAGE_SIZE);
1675
1676 crc = btrfs_csum_data(root, buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001677 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001678 len -= l;
1679 if (len == 0)
1680 break;
1681 index++;
1682 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001683 BUG_ON(!sblock->pagev[index]->page);
1684 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001685 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001686 }
1687
Arne Jansena2de7332011-03-08 14:14:00 +01001688 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001689 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001690 fail = 1;
1691
Arne Jansena2de7332011-03-08 14:14:00 +01001692 return fail;
1693}
1694
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001695static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001696{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001697 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001698 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001699 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001700 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001701 u8 calculated_csum[BTRFS_CSUM_SIZE];
1702 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1703 struct page *page;
1704 void *mapped_buffer;
1705 u64 mapped_size;
1706 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001707 u32 crc = ~(u32)0;
1708 int fail = 0;
1709 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001710 u64 len;
1711 int index;
1712
1713 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001714 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001715 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001716 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001717 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001718
1719 /*
1720 * we don't use the getter functions here, as we
1721 * a) don't have an extent buffer and
1722 * b) the page is already kmapped
1723 */
Arne Jansena2de7332011-03-08 14:14:00 +01001724
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001725 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr))
Arne Jansena2de7332011-03-08 14:14:00 +01001726 ++fail;
1727
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001728 if (sblock->pagev[0]->generation != le64_to_cpu(h->generation))
Arne Jansena2de7332011-03-08 14:14:00 +01001729 ++fail;
1730
1731 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1732 ++fail;
1733
1734 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1735 BTRFS_UUID_SIZE))
1736 ++fail;
1737
Stefan Behrensff023aa2012-11-06 11:43:11 +01001738 WARN_ON(sctx->nodesize != sctx->leafsize);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001739 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001740 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1741 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1742 index = 0;
1743 for (;;) {
1744 u64 l = min_t(u64, len, mapped_size);
1745
1746 crc = btrfs_csum_data(root, p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001747 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001748 len -= l;
1749 if (len == 0)
1750 break;
1751 index++;
1752 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001753 BUG_ON(!sblock->pagev[index]->page);
1754 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001755 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001756 mapped_size = PAGE_SIZE;
1757 p = mapped_buffer;
1758 }
1759
1760 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001761 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001762 ++crc_fail;
1763
Arne Jansena2de7332011-03-08 14:14:00 +01001764 return fail || crc_fail;
1765}
1766
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001767static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001768{
1769 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001770 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001771 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001772 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001773 u8 calculated_csum[BTRFS_CSUM_SIZE];
1774 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1775 struct page *page;
1776 void *mapped_buffer;
1777 u64 mapped_size;
1778 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001779 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001780 int fail_gen = 0;
1781 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001782 u64 len;
1783 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001784
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001785 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001786 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001787 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001788 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001789 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001790
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001791 if (sblock->pagev[0]->logical != le64_to_cpu(s->bytenr))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001792 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001793
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001794 if (sblock->pagev[0]->generation != le64_to_cpu(s->generation))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001795 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001796
1797 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001798 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001799
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001800 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1801 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1802 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1803 index = 0;
1804 for (;;) {
1805 u64 l = min_t(u64, len, mapped_size);
1806
1807 crc = btrfs_csum_data(root, p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001808 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001809 len -= l;
1810 if (len == 0)
1811 break;
1812 index++;
1813 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001814 BUG_ON(!sblock->pagev[index]->page);
1815 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001816 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001817 mapped_size = PAGE_SIZE;
1818 p = mapped_buffer;
1819 }
1820
1821 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001822 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001823 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001824
Stefan Behrens442a4f62012-05-25 16:06:08 +02001825 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001826 /*
1827 * if we find an error in a super block, we just report it.
1828 * They will get written with the next transaction commit
1829 * anyway
1830 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001831 spin_lock(&sctx->stat_lock);
1832 ++sctx->stat.super_errors;
1833 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001834 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001835 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001836 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1837 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001838 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001839 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001840 }
1841
Stefan Behrens442a4f62012-05-25 16:06:08 +02001842 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001843}
1844
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001845static void scrub_block_get(struct scrub_block *sblock)
1846{
1847 atomic_inc(&sblock->ref_count);
1848}
1849
1850static void scrub_block_put(struct scrub_block *sblock)
1851{
1852 if (atomic_dec_and_test(&sblock->ref_count)) {
1853 int i;
1854
1855 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001856 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001857 kfree(sblock);
1858 }
1859}
1860
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001861static void scrub_page_get(struct scrub_page *spage)
1862{
1863 atomic_inc(&spage->ref_count);
1864}
1865
1866static void scrub_page_put(struct scrub_page *spage)
1867{
1868 if (atomic_dec_and_test(&spage->ref_count)) {
1869 if (spage->page)
1870 __free_page(spage->page);
1871 kfree(spage);
1872 }
1873}
1874
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001875static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01001876{
1877 struct scrub_bio *sbio;
1878
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001879 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04001880 return;
Arne Jansena2de7332011-03-08 14:14:00 +01001881
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001882 sbio = sctx->bios[sctx->curr];
1883 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001884 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001885
Stefan Behrensff023aa2012-11-06 11:43:11 +01001886 if (!sbio->bio->bi_bdev) {
1887 /*
1888 * this case should not happen. If btrfs_map_block() is
1889 * wrong, it could happen for dev-replace operations on
1890 * missing devices when no mirrors are available, but in
1891 * this case it should already fail the mount.
1892 * This case is handled correctly (but _very_ slowly).
1893 */
1894 printk_ratelimited(KERN_WARNING
1895 "btrfs: scrub_submit(bio bdev == NULL) is unexpected!\n");
1896 bio_endio(sbio->bio, -EIO);
1897 } else {
1898 btrfsic_submit_bio(READ, sbio->bio);
1899 }
Arne Jansena2de7332011-03-08 14:14:00 +01001900}
1901
Stefan Behrensff023aa2012-11-06 11:43:11 +01001902static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
1903 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01001904{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001905 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01001906 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001907 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01001908
1909again:
1910 /*
1911 * grab a fresh bio or wait for one to become available
1912 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001913 while (sctx->curr == -1) {
1914 spin_lock(&sctx->list_lock);
1915 sctx->curr = sctx->first_free;
1916 if (sctx->curr != -1) {
1917 sctx->first_free = sctx->bios[sctx->curr]->next_free;
1918 sctx->bios[sctx->curr]->next_free = -1;
1919 sctx->bios[sctx->curr]->page_count = 0;
1920 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001921 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001922 spin_unlock(&sctx->list_lock);
1923 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01001924 }
1925 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001926 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001927 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05001928 struct bio *bio;
1929
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001930 sbio->physical = spage->physical;
1931 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001932 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001933 bio = sbio->bio;
1934 if (!bio) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001935 bio = bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001936 if (!bio)
1937 return -ENOMEM;
1938 sbio->bio = bio;
1939 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05001940
1941 bio->bi_private = sbio;
1942 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001943 bio->bi_bdev = sbio->dev->bdev;
1944 bio->bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001945 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001946 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1947 spage->physical ||
1948 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001949 spage->logical ||
1950 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001951 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05001952 goto again;
1953 }
1954
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001955 sbio->pagev[sbio->page_count] = spage;
1956 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1957 if (ret != PAGE_SIZE) {
1958 if (sbio->page_count < 1) {
1959 bio_put(sbio->bio);
1960 sbio->bio = NULL;
1961 return -EIO;
1962 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001963 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001964 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01001965 }
Arne Jansen1bc87792011-05-28 21:57:55 +02001966
Stefan Behrensff023aa2012-11-06 11:43:11 +01001967 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001968 atomic_inc(&sblock->outstanding_pages);
1969 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001970 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001971 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001972
1973 return 0;
1974}
1975
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001976static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001977 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001978 u64 gen, int mirror_num, u8 *csum, int force,
1979 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001980{
1981 struct scrub_block *sblock;
1982 int index;
1983
1984 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
1985 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001986 spin_lock(&sctx->stat_lock);
1987 sctx->stat.malloc_errors++;
1988 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001989 return -ENOMEM;
1990 }
1991
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001992 /* one ref inside this function, plus one for each page added to
1993 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001994 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001995 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001996 sblock->no_io_error_seen = 1;
1997
1998 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001999 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002000 u64 l = min_t(u64, len, PAGE_SIZE);
2001
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002002 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2003 if (!spage) {
2004leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002005 spin_lock(&sctx->stat_lock);
2006 sctx->stat.malloc_errors++;
2007 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002008 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002009 return -ENOMEM;
2010 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002011 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2012 scrub_page_get(spage);
2013 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002014 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002015 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002016 spage->flags = flags;
2017 spage->generation = gen;
2018 spage->logical = logical;
2019 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002020 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002021 spage->mirror_num = mirror_num;
2022 if (csum) {
2023 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002024 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002025 } else {
2026 spage->have_csum = 0;
2027 }
2028 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002029 spage->page = alloc_page(GFP_NOFS);
2030 if (!spage->page)
2031 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002032 len -= l;
2033 logical += l;
2034 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002035 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002036 }
2037
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002038 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002039 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002040 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002041 int ret;
2042
Stefan Behrensff023aa2012-11-06 11:43:11 +01002043 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002044 if (ret) {
2045 scrub_block_put(sblock);
2046 return ret;
2047 }
2048 }
2049
2050 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002051 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002052
2053 /* last one frees, either here or in bio completion for last page */
2054 scrub_block_put(sblock);
2055 return 0;
2056}
2057
2058static void scrub_bio_end_io(struct bio *bio, int err)
2059{
2060 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002061 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002062
2063 sbio->err = err;
2064 sbio->bio = bio;
2065
2066 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
2067}
2068
2069static void scrub_bio_end_io_worker(struct btrfs_work *work)
2070{
2071 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002072 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002073 int i;
2074
Stefan Behrensff023aa2012-11-06 11:43:11 +01002075 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002076 if (sbio->err) {
2077 for (i = 0; i < sbio->page_count; i++) {
2078 struct scrub_page *spage = sbio->pagev[i];
2079
2080 spage->io_error = 1;
2081 spage->sblock->no_io_error_seen = 0;
2082 }
2083 }
2084
2085 /* now complete the scrub_block items that have all pages completed */
2086 for (i = 0; i < sbio->page_count; i++) {
2087 struct scrub_page *spage = sbio->pagev[i];
2088 struct scrub_block *sblock = spage->sblock;
2089
2090 if (atomic_dec_and_test(&sblock->outstanding_pages))
2091 scrub_block_complete(sblock);
2092 scrub_block_put(sblock);
2093 }
2094
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002095 bio_put(sbio->bio);
2096 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002097 spin_lock(&sctx->list_lock);
2098 sbio->next_free = sctx->first_free;
2099 sctx->first_free = sbio->index;
2100 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002101
2102 if (sctx->is_dev_replace &&
2103 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2104 mutex_lock(&sctx->wr_ctx.wr_lock);
2105 scrub_wr_submit(sctx);
2106 mutex_unlock(&sctx->wr_ctx.wr_lock);
2107 }
2108
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002109 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002110}
2111
2112static void scrub_block_complete(struct scrub_block *sblock)
2113{
Stefan Behrensff023aa2012-11-06 11:43:11 +01002114 if (!sblock->no_io_error_seen) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002115 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002116 } else {
2117 /*
2118 * if has checksum error, write via repair mechanism in
2119 * dev replace case, otherwise write here in dev replace
2120 * case.
2121 */
2122 if (!scrub_checksum(sblock) && sblock->sctx->is_dev_replace)
2123 scrub_write_block_to_dev_replace(sblock);
2124 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002125}
2126
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002127static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002128 u8 *csum)
2129{
2130 struct btrfs_ordered_sum *sum = NULL;
2131 int ret = 0;
2132 unsigned long i;
2133 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002134
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002135 while (!list_empty(&sctx->csum_list)) {
2136 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002137 struct btrfs_ordered_sum, list);
2138 if (sum->bytenr > logical)
2139 return 0;
2140 if (sum->bytenr + sum->len > logical)
2141 break;
2142
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002143 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002144 list_del(&sum->list);
2145 kfree(sum);
2146 sum = NULL;
2147 }
2148 if (!sum)
2149 return 0;
2150
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002151 num_sectors = sum->len / sctx->sectorsize;
Arne Jansena2de7332011-03-08 14:14:00 +01002152 for (i = 0; i < num_sectors; ++i) {
2153 if (sum->sums[i].bytenr == logical) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002154 memcpy(csum, &sum->sums[i].sum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002155 ret = 1;
2156 break;
2157 }
2158 }
2159 if (ret && i == num_sectors - 1) {
2160 list_del(&sum->list);
2161 kfree(sum);
2162 }
2163 return ret;
2164}
2165
2166/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002167static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002168 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002169 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002170{
2171 int ret;
2172 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002173 u32 blocksize;
2174
2175 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002176 blocksize = sctx->sectorsize;
2177 spin_lock(&sctx->stat_lock);
2178 sctx->stat.data_extents_scrubbed++;
2179 sctx->stat.data_bytes_scrubbed += len;
2180 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002181 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002182 WARN_ON(sctx->nodesize != sctx->leafsize);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002183 blocksize = sctx->nodesize;
2184 spin_lock(&sctx->stat_lock);
2185 sctx->stat.tree_extents_scrubbed++;
2186 sctx->stat.tree_bytes_scrubbed += len;
2187 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002188 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002189 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002190 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002191 }
Arne Jansena2de7332011-03-08 14:14:00 +01002192
2193 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002194 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002195 int have_csum = 0;
2196
2197 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2198 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002199 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002200 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002201 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002202 if (sctx->is_dev_replace && !have_csum) {
2203 ret = copy_nocow_pages(sctx, logical, l,
2204 mirror_num,
2205 physical_for_dev_replace);
2206 goto behind_scrub_pages;
2207 }
Arne Jansena2de7332011-03-08 14:14:00 +01002208 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002209 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002210 mirror_num, have_csum ? csum : NULL, 0,
2211 physical_for_dev_replace);
2212behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002213 if (ret)
2214 return ret;
2215 len -= l;
2216 logical += l;
2217 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002218 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002219 }
2220 return 0;
2221}
2222
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002223static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002224 struct map_lookup *map,
2225 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002226 int num, u64 base, u64 length,
2227 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002228{
2229 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002230 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002231 struct btrfs_root *root = fs_info->extent_root;
2232 struct btrfs_root *csum_root = fs_info->csum_root;
2233 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002234 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002235 u64 flags;
2236 int ret;
2237 int slot;
2238 int i;
2239 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002240 struct extent_buffer *l;
2241 struct btrfs_key key;
2242 u64 physical;
2243 u64 logical;
2244 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002245 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002246 struct reada_control *reada1;
2247 struct reada_control *reada2;
2248 struct btrfs_key key_start;
2249 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002250 u64 increment = map->stripe_len;
2251 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002252 u64 extent_logical;
2253 u64 extent_physical;
2254 u64 extent_len;
2255 struct btrfs_device *extent_dev;
2256 int extent_mirror_num;
Arne Jansena2de7332011-03-08 14:14:00 +01002257
David Woodhouse53b381b2013-01-29 18:40:14 -05002258 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
2259 BTRFS_BLOCK_GROUP_RAID6)) {
2260 if (num >= nr_data_stripes(map)) {
2261 return 0;
2262 }
2263 }
2264
Arne Jansena2de7332011-03-08 14:14:00 +01002265 nstripes = length;
2266 offset = 0;
2267 do_div(nstripes, map->stripe_len);
2268 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2269 offset = map->stripe_len * num;
2270 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02002271 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002272 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2273 int factor = map->num_stripes / map->sub_stripes;
2274 offset = map->stripe_len * (num / map->sub_stripes);
2275 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02002276 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002277 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2278 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002279 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002280 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2281 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002282 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002283 } else {
2284 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002285 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002286 }
2287
2288 path = btrfs_alloc_path();
2289 if (!path)
2290 return -ENOMEM;
2291
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002292 /*
2293 * work on commit root. The related disk blocks are static as
2294 * long as COW is applied. This means, it is save to rewrite
2295 * them to repair disk errors without any race conditions
2296 */
Arne Jansena2de7332011-03-08 14:14:00 +01002297 path->search_commit_root = 1;
2298 path->skip_locking = 1;
2299
2300 /*
Arne Jansen7a262852011-06-10 12:39:23 +02002301 * trigger the readahead for extent tree csum tree and wait for
2302 * completion. During readahead, the scrub is officially paused
2303 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01002304 */
2305 logical = base + offset;
Arne Jansena2de7332011-03-08 14:14:00 +01002306
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002307 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002308 atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansen7a262852011-06-10 12:39:23 +02002309 atomic_inc(&fs_info->scrubs_paused);
2310 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002311
Arne Jansen7a262852011-06-10 12:39:23 +02002312 /* FIXME it might be better to start readahead at commit root */
2313 key_start.objectid = logical;
2314 key_start.type = BTRFS_EXTENT_ITEM_KEY;
2315 key_start.offset = (u64)0;
2316 key_end.objectid = base + offset + nstripes * increment;
2317 key_end.type = BTRFS_EXTENT_ITEM_KEY;
2318 key_end.offset = (u64)0;
2319 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01002320
Arne Jansen7a262852011-06-10 12:39:23 +02002321 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2322 key_start.type = BTRFS_EXTENT_CSUM_KEY;
2323 key_start.offset = logical;
2324 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2325 key_end.type = BTRFS_EXTENT_CSUM_KEY;
2326 key_end.offset = base + offset + nstripes * increment;
2327 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01002328
Arne Jansen7a262852011-06-10 12:39:23 +02002329 if (!IS_ERR(reada1))
2330 btrfs_reada_wait(reada1);
2331 if (!IS_ERR(reada2))
2332 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01002333
Arne Jansen7a262852011-06-10 12:39:23 +02002334 mutex_lock(&fs_info->scrub_lock);
2335 while (atomic_read(&fs_info->scrub_pause_req)) {
2336 mutex_unlock(&fs_info->scrub_lock);
2337 wait_event(fs_info->scrub_pause_wait,
2338 atomic_read(&fs_info->scrub_pause_req) == 0);
2339 mutex_lock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002340 }
Arne Jansen7a262852011-06-10 12:39:23 +02002341 atomic_dec(&fs_info->scrubs_paused);
2342 mutex_unlock(&fs_info->scrub_lock);
2343 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002344
2345 /*
2346 * collect all data csums for the stripe to avoid seeking during
2347 * the scrub. This might currently (crc32) end up to be about 1MB
2348 */
Arne Jansene7786c32011-05-28 20:58:38 +00002349 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002350
Arne Jansena2de7332011-03-08 14:14:00 +01002351 /*
2352 * now find all extents for each stripe and scrub them
2353 */
Arne Jansen7a262852011-06-10 12:39:23 +02002354 logical = base + offset;
2355 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01002356 ret = 0;
Arne Jansen7a262852011-06-10 12:39:23 +02002357 for (i = 0; i < nstripes; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +01002358 /*
2359 * canceled?
2360 */
2361 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002362 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002363 ret = -ECANCELED;
2364 goto out;
2365 }
2366 /*
2367 * check to see if we have to pause
2368 */
2369 if (atomic_read(&fs_info->scrub_pause_req)) {
2370 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002371 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002372 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002373 mutex_lock(&sctx->wr_ctx.wr_lock);
2374 scrub_wr_submit(sctx);
2375 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002376 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002377 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002378 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002379 atomic_inc(&fs_info->scrubs_paused);
2380 wake_up(&fs_info->scrub_pause_wait);
2381 mutex_lock(&fs_info->scrub_lock);
2382 while (atomic_read(&fs_info->scrub_pause_req)) {
2383 mutex_unlock(&fs_info->scrub_lock);
2384 wait_event(fs_info->scrub_pause_wait,
2385 atomic_read(&fs_info->scrub_pause_req) == 0);
2386 mutex_lock(&fs_info->scrub_lock);
2387 }
2388 atomic_dec(&fs_info->scrubs_paused);
2389 mutex_unlock(&fs_info->scrub_lock);
2390 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002391 }
2392
Arne Jansen7a262852011-06-10 12:39:23 +02002393 ret = btrfs_lookup_csums_range(csum_root, logical,
2394 logical + map->stripe_len - 1,
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002395 &sctx->csum_list, 1);
Arne Jansen7a262852011-06-10 12:39:23 +02002396 if (ret)
2397 goto out;
2398
Arne Jansena2de7332011-03-08 14:14:00 +01002399 key.objectid = logical;
2400 key.type = BTRFS_EXTENT_ITEM_KEY;
2401 key.offset = (u64)0;
2402
2403 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2404 if (ret < 0)
2405 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02002406 if (ret > 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01002407 ret = btrfs_previous_item(root, path, 0,
2408 BTRFS_EXTENT_ITEM_KEY);
2409 if (ret < 0)
2410 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02002411 if (ret > 0) {
2412 /* there's no smaller item, so stick with the
2413 * larger one */
2414 btrfs_release_path(path);
2415 ret = btrfs_search_slot(NULL, root, &key,
2416 path, 0, 0);
2417 if (ret < 0)
2418 goto out;
2419 }
Arne Jansena2de7332011-03-08 14:14:00 +01002420 }
2421
2422 while (1) {
2423 l = path->nodes[0];
2424 slot = path->slots[0];
2425 if (slot >= btrfs_header_nritems(l)) {
2426 ret = btrfs_next_leaf(root, path);
2427 if (ret == 0)
2428 continue;
2429 if (ret < 0)
2430 goto out;
2431
2432 break;
2433 }
2434 btrfs_item_key_to_cpu(l, &key, slot);
2435
2436 if (key.objectid + key.offset <= logical)
2437 goto next;
2438
2439 if (key.objectid >= logical + map->stripe_len)
2440 break;
2441
2442 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
2443 goto next;
2444
2445 extent = btrfs_item_ptr(l, slot,
2446 struct btrfs_extent_item);
2447 flags = btrfs_extent_flags(l, extent);
2448 generation = btrfs_extent_generation(l, extent);
2449
2450 if (key.objectid < logical &&
2451 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2452 printk(KERN_ERR
2453 "btrfs scrub: tree block %llu spanning "
2454 "stripes, ignored. logical=%llu\n",
2455 (unsigned long long)key.objectid,
2456 (unsigned long long)logical);
2457 goto next;
2458 }
2459
2460 /*
2461 * trim extent to this stripe
2462 */
2463 if (key.objectid < logical) {
2464 key.offset -= logical - key.objectid;
2465 key.objectid = logical;
2466 }
2467 if (key.objectid + key.offset >
2468 logical + map->stripe_len) {
2469 key.offset = logical + map->stripe_len -
2470 key.objectid;
2471 }
2472
Stefan Behrensff023aa2012-11-06 11:43:11 +01002473 extent_logical = key.objectid;
2474 extent_physical = key.objectid - logical + physical;
2475 extent_len = key.offset;
2476 extent_dev = scrub_dev;
2477 extent_mirror_num = mirror_num;
2478 if (is_dev_replace)
2479 scrub_remap_extent(fs_info, extent_logical,
2480 extent_len, &extent_physical,
2481 &extent_dev,
2482 &extent_mirror_num);
2483 ret = scrub_extent(sctx, extent_logical, extent_len,
2484 extent_physical, extent_dev, flags,
2485 generation, extent_mirror_num,
2486 key.objectid - logical + physical);
Arne Jansena2de7332011-03-08 14:14:00 +01002487 if (ret)
2488 goto out;
2489
2490next:
2491 path->slots[0]++;
2492 }
Chris Mason71267332011-05-23 06:30:52 -04002493 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002494 logical += increment;
2495 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002496 spin_lock(&sctx->stat_lock);
2497 sctx->stat.last_physical = physical;
2498 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002499 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01002500out:
Arne Jansena2de7332011-03-08 14:14:00 +01002501 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002502 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002503 mutex_lock(&sctx->wr_ctx.wr_lock);
2504 scrub_wr_submit(sctx);
2505 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002506
Arne Jansene7786c32011-05-28 20:58:38 +00002507 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002508 btrfs_free_path(path);
2509 return ret < 0 ? ret : 0;
2510}
2511
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002512static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002513 struct btrfs_device *scrub_dev,
2514 u64 chunk_tree, u64 chunk_objectid,
2515 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002516 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002517{
2518 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002519 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01002520 struct map_lookup *map;
2521 struct extent_map *em;
2522 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002523 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002524
2525 read_lock(&map_tree->map_tree.lock);
2526 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2527 read_unlock(&map_tree->map_tree.lock);
2528
2529 if (!em)
2530 return -EINVAL;
2531
2532 map = (struct map_lookup *)em->bdev;
2533 if (em->start != chunk_offset)
2534 goto out;
2535
2536 if (em->len < length)
2537 goto out;
2538
2539 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002540 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01002541 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002542 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002543 chunk_offset, length,
2544 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002545 if (ret)
2546 goto out;
2547 }
2548 }
2549out:
2550 free_extent_map(em);
2551
2552 return ret;
2553}
2554
2555static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002556int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002557 struct btrfs_device *scrub_dev, u64 start, u64 end,
2558 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002559{
2560 struct btrfs_dev_extent *dev_extent = NULL;
2561 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002562 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002563 struct btrfs_fs_info *fs_info = root->fs_info;
2564 u64 length;
2565 u64 chunk_tree;
2566 u64 chunk_objectid;
2567 u64 chunk_offset;
2568 int ret;
2569 int slot;
2570 struct extent_buffer *l;
2571 struct btrfs_key key;
2572 struct btrfs_key found_key;
2573 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002574 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01002575
2576 path = btrfs_alloc_path();
2577 if (!path)
2578 return -ENOMEM;
2579
2580 path->reada = 2;
2581 path->search_commit_root = 1;
2582 path->skip_locking = 1;
2583
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002584 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01002585 key.offset = 0ull;
2586 key.type = BTRFS_DEV_EXTENT_KEY;
2587
Arne Jansena2de7332011-03-08 14:14:00 +01002588 while (1) {
2589 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2590 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02002591 break;
2592 if (ret > 0) {
2593 if (path->slots[0] >=
2594 btrfs_header_nritems(path->nodes[0])) {
2595 ret = btrfs_next_leaf(root, path);
2596 if (ret)
2597 break;
2598 }
2599 }
Arne Jansena2de7332011-03-08 14:14:00 +01002600
2601 l = path->nodes[0];
2602 slot = path->slots[0];
2603
2604 btrfs_item_key_to_cpu(l, &found_key, slot);
2605
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002606 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01002607 break;
2608
Arne Jansen8c510322011-06-03 10:09:26 +02002609 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01002610 break;
2611
2612 if (found_key.offset >= end)
2613 break;
2614
2615 if (found_key.offset < key.offset)
2616 break;
2617
2618 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2619 length = btrfs_dev_extent_length(l, dev_extent);
2620
2621 if (found_key.offset + length <= start) {
2622 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002623 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002624 continue;
2625 }
2626
2627 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2628 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2629 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2630
2631 /*
2632 * get a reference on the corresponding block group to prevent
2633 * the chunk from going away while we scrub it
2634 */
2635 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2636 if (!cache) {
2637 ret = -ENOENT;
Arne Jansen8c510322011-06-03 10:09:26 +02002638 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002639 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01002640 dev_replace->cursor_right = found_key.offset + length;
2641 dev_replace->cursor_left = found_key.offset;
2642 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002643 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002644 chunk_offset, length, found_key.offset,
2645 is_dev_replace);
2646
2647 /*
2648 * flush, submit all pending read and write bios, afterwards
2649 * wait for them.
2650 * Note that in the dev replace case, a read request causes
2651 * write requests that are submitted in the read completion
2652 * worker. Therefore in the current situation, it is required
2653 * that all write requests are flushed, so that all read and
2654 * write requests are really completed when bios_in_flight
2655 * changes to 0.
2656 */
2657 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
2658 scrub_submit(sctx);
2659 mutex_lock(&sctx->wr_ctx.wr_lock);
2660 scrub_wr_submit(sctx);
2661 mutex_unlock(&sctx->wr_ctx.wr_lock);
2662
2663 wait_event(sctx->list_wait,
2664 atomic_read(&sctx->bios_in_flight) == 0);
2665 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
2666 atomic_inc(&fs_info->scrubs_paused);
2667 wake_up(&fs_info->scrub_pause_wait);
2668 wait_event(sctx->list_wait,
2669 atomic_read(&sctx->workers_pending) == 0);
2670
2671 mutex_lock(&fs_info->scrub_lock);
2672 while (atomic_read(&fs_info->scrub_pause_req)) {
2673 mutex_unlock(&fs_info->scrub_lock);
2674 wait_event(fs_info->scrub_pause_wait,
2675 atomic_read(&fs_info->scrub_pause_req) == 0);
2676 mutex_lock(&fs_info->scrub_lock);
2677 }
2678 atomic_dec(&fs_info->scrubs_paused);
2679 mutex_unlock(&fs_info->scrub_lock);
2680 wake_up(&fs_info->scrub_pause_wait);
2681
2682 dev_replace->cursor_left = dev_replace->cursor_right;
2683 dev_replace->item_needs_writeback = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002684 btrfs_put_block_group(cache);
2685 if (ret)
2686 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00002687 if (is_dev_replace &&
2688 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002689 ret = -EIO;
2690 break;
2691 }
2692 if (sctx->stat.malloc_errors > 0) {
2693 ret = -ENOMEM;
2694 break;
2695 }
Arne Jansena2de7332011-03-08 14:14:00 +01002696
2697 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002698 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002699 }
2700
Arne Jansena2de7332011-03-08 14:14:00 +01002701 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02002702
2703 /*
2704 * ret can still be 1 from search_slot or next_leaf,
2705 * that's not an error
2706 */
2707 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002708}
2709
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002710static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2711 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01002712{
2713 int i;
2714 u64 bytenr;
2715 u64 gen;
2716 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002717 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002718
Miao Xie87533c42013-01-29 10:14:48 +00002719 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002720 return -EIO;
2721
Arne Jansena2de7332011-03-08 14:14:00 +01002722 gen = root->fs_info->last_trans_committed;
2723
2724 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2725 bytenr = btrfs_sb_offset(i);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002726 if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01002727 break;
2728
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002729 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002730 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002731 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01002732 if (ret)
2733 return ret;
2734 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002735 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002736
2737 return 0;
2738}
2739
2740/*
2741 * get a reference count on fs_info->scrub_workers. start worker if necessary
2742 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002743static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
2744 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002745{
Josef Bacik0dc3b842011-11-18 14:37:27 -05002746 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002747
2748 mutex_lock(&fs_info->scrub_lock);
Arne Jansen632dd772011-06-10 12:07:07 +02002749 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002750 if (is_dev_replace)
2751 btrfs_init_workers(&fs_info->scrub_workers, "scrub", 1,
2752 &fs_info->generic_worker);
2753 else
2754 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
2755 fs_info->thread_pool_size,
2756 &fs_info->generic_worker);
Arne Jansen632dd772011-06-10 12:07:07 +02002757 fs_info->scrub_workers.idle_thresh = 4;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002758 ret = btrfs_start_workers(&fs_info->scrub_workers);
2759 if (ret)
2760 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002761 btrfs_init_workers(&fs_info->scrub_wr_completion_workers,
2762 "scrubwrc",
2763 fs_info->thread_pool_size,
2764 &fs_info->generic_worker);
2765 fs_info->scrub_wr_completion_workers.idle_thresh = 2;
2766 ret = btrfs_start_workers(
2767 &fs_info->scrub_wr_completion_workers);
2768 if (ret)
2769 goto out;
2770 btrfs_init_workers(&fs_info->scrub_nocow_workers, "scrubnc", 1,
2771 &fs_info->generic_worker);
2772 ret = btrfs_start_workers(&fs_info->scrub_nocow_workers);
2773 if (ret)
2774 goto out;
Arne Jansen632dd772011-06-10 12:07:07 +02002775 }
Arne Jansena2de7332011-03-08 14:14:00 +01002776 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002777out:
Arne Jansena2de7332011-03-08 14:14:00 +01002778 mutex_unlock(&fs_info->scrub_lock);
2779
Josef Bacik0dc3b842011-11-18 14:37:27 -05002780 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002781}
2782
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002783static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002784{
Arne Jansena2de7332011-03-08 14:14:00 +01002785 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002786 if (--fs_info->scrub_workers_refcnt == 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01002787 btrfs_stop_workers(&fs_info->scrub_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002788 btrfs_stop_workers(&fs_info->scrub_wr_completion_workers);
2789 btrfs_stop_workers(&fs_info->scrub_nocow_workers);
2790 }
Arne Jansena2de7332011-03-08 14:14:00 +01002791 WARN_ON(fs_info->scrub_workers_refcnt < 0);
2792 mutex_unlock(&fs_info->scrub_lock);
2793}
2794
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002795int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
2796 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01002797 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002798{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002799 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002800 int ret;
2801 struct btrfs_device *dev;
2802
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002803 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01002804 return -EINVAL;
2805
2806 /*
2807 * check some assumptions
2808 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002809 if (fs_info->chunk_root->nodesize != fs_info->chunk_root->leafsize) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002810 printk(KERN_ERR
2811 "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002812 fs_info->chunk_root->nodesize,
2813 fs_info->chunk_root->leafsize);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002814 return -EINVAL;
2815 }
2816
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002817 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002818 /*
2819 * in this case scrub is unable to calculate the checksum
2820 * the way scrub is implemented. Do not handle this
2821 * situation at all because it won't ever happen.
2822 */
2823 printk(KERN_ERR
2824 "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002825 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002826 return -EINVAL;
2827 }
2828
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002829 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002830 /* not supported for data w/o checksums */
2831 printk(KERN_ERR
2832 "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002833 fs_info->chunk_root->sectorsize,
2834 (unsigned long long)PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01002835 return -EINVAL;
2836 }
2837
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002838 if (fs_info->chunk_root->nodesize >
2839 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
2840 fs_info->chunk_root->sectorsize >
2841 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
2842 /*
2843 * would exhaust the array bounds of pagev member in
2844 * struct scrub_block
2845 */
2846 pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
2847 fs_info->chunk_root->nodesize,
2848 SCRUB_MAX_PAGES_PER_BLOCK,
2849 fs_info->chunk_root->sectorsize,
2850 SCRUB_MAX_PAGES_PER_BLOCK);
2851 return -EINVAL;
2852 }
2853
Stefan Behrensff023aa2012-11-06 11:43:11 +01002854 ret = scrub_workers_get(fs_info, is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002855 if (ret)
2856 return ret;
2857
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002858 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2859 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002860 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002861 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2862 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002863 return -ENODEV;
2864 }
2865 mutex_lock(&fs_info->scrub_lock);
2866
Stefan Behrens63a212a2012-11-05 18:29:28 +01002867 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01002868 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002869 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2870 scrub_workers_put(fs_info);
2871 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01002872 }
2873
Stefan Behrens8dabb742012-11-06 13:15:27 +01002874 btrfs_dev_replace_lock(&fs_info->dev_replace);
2875 if (dev->scrub_device ||
2876 (!is_dev_replace &&
2877 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
2878 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002879 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002880 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2881 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002882 return -EINPROGRESS;
2883 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01002884 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002885 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002886 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002887 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002888 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2889 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002890 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002891 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002892 sctx->readonly = readonly;
2893 dev->scrub_device = sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002894
2895 atomic_inc(&fs_info->scrubs_running);
2896 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002897 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002898
Stefan Behrensff023aa2012-11-06 11:43:11 +01002899 if (!is_dev_replace) {
2900 down_read(&fs_info->scrub_super_lock);
2901 ret = scrub_supers(sctx, dev);
2902 up_read(&fs_info->scrub_super_lock);
2903 }
Arne Jansena2de7332011-03-08 14:14:00 +01002904
2905 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002906 ret = scrub_enumerate_chunks(sctx, dev, start, end,
2907 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002908
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002909 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002910 atomic_dec(&fs_info->scrubs_running);
2911 wake_up(&fs_info->scrub_pause_wait);
2912
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002913 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02002914
Arne Jansena2de7332011-03-08 14:14:00 +01002915 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002916 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002917
2918 mutex_lock(&fs_info->scrub_lock);
2919 dev->scrub_device = NULL;
2920 mutex_unlock(&fs_info->scrub_lock);
2921
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002922 scrub_free_ctx(sctx);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002923 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002924
2925 return ret;
2926}
2927
Jeff Mahoney143bede2012-03-01 14:56:26 +01002928void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002929{
2930 struct btrfs_fs_info *fs_info = root->fs_info;
2931
2932 mutex_lock(&fs_info->scrub_lock);
2933 atomic_inc(&fs_info->scrub_pause_req);
2934 while (atomic_read(&fs_info->scrubs_paused) !=
2935 atomic_read(&fs_info->scrubs_running)) {
2936 mutex_unlock(&fs_info->scrub_lock);
2937 wait_event(fs_info->scrub_pause_wait,
2938 atomic_read(&fs_info->scrubs_paused) ==
2939 atomic_read(&fs_info->scrubs_running));
2940 mutex_lock(&fs_info->scrub_lock);
2941 }
2942 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002943}
2944
Jeff Mahoney143bede2012-03-01 14:56:26 +01002945void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002946{
2947 struct btrfs_fs_info *fs_info = root->fs_info;
2948
2949 atomic_dec(&fs_info->scrub_pause_req);
2950 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002951}
2952
Jeff Mahoney143bede2012-03-01 14:56:26 +01002953void btrfs_scrub_pause_super(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002954{
2955 down_write(&root->fs_info->scrub_super_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002956}
2957
Jeff Mahoney143bede2012-03-01 14:56:26 +01002958void btrfs_scrub_continue_super(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002959{
2960 up_write(&root->fs_info->scrub_super_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002961}
2962
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002963int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002964{
Arne Jansena2de7332011-03-08 14:14:00 +01002965 mutex_lock(&fs_info->scrub_lock);
2966 if (!atomic_read(&fs_info->scrubs_running)) {
2967 mutex_unlock(&fs_info->scrub_lock);
2968 return -ENOTCONN;
2969 }
2970
2971 atomic_inc(&fs_info->scrub_cancel_req);
2972 while (atomic_read(&fs_info->scrubs_running)) {
2973 mutex_unlock(&fs_info->scrub_lock);
2974 wait_event(fs_info->scrub_pause_wait,
2975 atomic_read(&fs_info->scrubs_running) == 0);
2976 mutex_lock(&fs_info->scrub_lock);
2977 }
2978 atomic_dec(&fs_info->scrub_cancel_req);
2979 mutex_unlock(&fs_info->scrub_lock);
2980
2981 return 0;
2982}
2983
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002984int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
2985 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01002986{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002987 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002988
2989 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002990 sctx = dev->scrub_device;
2991 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01002992 mutex_unlock(&fs_info->scrub_lock);
2993 return -ENOTCONN;
2994 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002995 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01002996 while (dev->scrub_device) {
2997 mutex_unlock(&fs_info->scrub_lock);
2998 wait_event(fs_info->scrub_pause_wait,
2999 dev->scrub_device == NULL);
3000 mutex_lock(&fs_info->scrub_lock);
3001 }
3002 mutex_unlock(&fs_info->scrub_lock);
3003
3004 return 0;
3005}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003006
Arne Jansena2de7332011-03-08 14:14:00 +01003007int btrfs_scrub_cancel_devid(struct btrfs_root *root, u64 devid)
3008{
3009 struct btrfs_fs_info *fs_info = root->fs_info;
3010 struct btrfs_device *dev;
3011 int ret;
3012
3013 /*
3014 * we have to hold the device_list_mutex here so the device
3015 * does not go away in cancel_dev. FIXME: find a better solution
3016 */
3017 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003018 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003019 if (!dev) {
3020 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3021 return -ENODEV;
3022 }
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003023 ret = btrfs_scrub_cancel_dev(fs_info, dev);
Arne Jansena2de7332011-03-08 14:14:00 +01003024 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3025
3026 return ret;
3027}
3028
3029int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3030 struct btrfs_scrub_progress *progress)
3031{
3032 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003033 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003034
3035 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003036 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003037 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003038 sctx = dev->scrub_device;
3039 if (sctx)
3040 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003041 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3042
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003043 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003044}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003045
3046static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3047 u64 extent_logical, u64 extent_len,
3048 u64 *extent_physical,
3049 struct btrfs_device **extent_dev,
3050 int *extent_mirror_num)
3051{
3052 u64 mapped_length;
3053 struct btrfs_bio *bbio = NULL;
3054 int ret;
3055
3056 mapped_length = extent_len;
3057 ret = btrfs_map_block(fs_info, READ, extent_logical,
3058 &mapped_length, &bbio, 0);
3059 if (ret || !bbio || mapped_length < extent_len ||
3060 !bbio->stripes[0].dev->bdev) {
3061 kfree(bbio);
3062 return;
3063 }
3064
3065 *extent_physical = bbio->stripes[0].physical;
3066 *extent_mirror_num = bbio->mirror_num;
3067 *extent_dev = bbio->stripes[0].dev;
3068 kfree(bbio);
3069}
3070
3071static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3072 struct scrub_wr_ctx *wr_ctx,
3073 struct btrfs_fs_info *fs_info,
3074 struct btrfs_device *dev,
3075 int is_dev_replace)
3076{
3077 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3078
3079 mutex_init(&wr_ctx->wr_lock);
3080 wr_ctx->wr_curr_bio = NULL;
3081 if (!is_dev_replace)
3082 return 0;
3083
3084 WARN_ON(!dev->bdev);
3085 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3086 bio_get_nr_vecs(dev->bdev));
3087 wr_ctx->tgtdev = dev;
3088 atomic_set(&wr_ctx->flush_all_writes, 0);
3089 return 0;
3090}
3091
3092static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3093{
3094 mutex_lock(&wr_ctx->wr_lock);
3095 kfree(wr_ctx->wr_curr_bio);
3096 wr_ctx->wr_curr_bio = NULL;
3097 mutex_unlock(&wr_ctx->wr_lock);
3098}
3099
3100static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3101 int mirror_num, u64 physical_for_dev_replace)
3102{
3103 struct scrub_copy_nocow_ctx *nocow_ctx;
3104 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3105
3106 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3107 if (!nocow_ctx) {
3108 spin_lock(&sctx->stat_lock);
3109 sctx->stat.malloc_errors++;
3110 spin_unlock(&sctx->stat_lock);
3111 return -ENOMEM;
3112 }
3113
3114 scrub_pending_trans_workers_inc(sctx);
3115
3116 nocow_ctx->sctx = sctx;
3117 nocow_ctx->logical = logical;
3118 nocow_ctx->len = len;
3119 nocow_ctx->mirror_num = mirror_num;
3120 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
3121 nocow_ctx->work.func = copy_nocow_pages_worker;
3122 btrfs_queue_worker(&fs_info->scrub_nocow_workers,
3123 &nocow_ctx->work);
3124
3125 return 0;
3126}
3127
3128static void copy_nocow_pages_worker(struct btrfs_work *work)
3129{
3130 struct scrub_copy_nocow_ctx *nocow_ctx =
3131 container_of(work, struct scrub_copy_nocow_ctx, work);
3132 struct scrub_ctx *sctx = nocow_ctx->sctx;
3133 u64 logical = nocow_ctx->logical;
3134 u64 len = nocow_ctx->len;
3135 int mirror_num = nocow_ctx->mirror_num;
3136 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3137 int ret;
3138 struct btrfs_trans_handle *trans = NULL;
3139 struct btrfs_fs_info *fs_info;
3140 struct btrfs_path *path;
3141 struct btrfs_root *root;
3142 int not_written = 0;
3143
3144 fs_info = sctx->dev_root->fs_info;
3145 root = fs_info->extent_root;
3146
3147 path = btrfs_alloc_path();
3148 if (!path) {
3149 spin_lock(&sctx->stat_lock);
3150 sctx->stat.malloc_errors++;
3151 spin_unlock(&sctx->stat_lock);
3152 not_written = 1;
3153 goto out;
3154 }
3155
3156 trans = btrfs_join_transaction(root);
3157 if (IS_ERR(trans)) {
3158 not_written = 1;
3159 goto out;
3160 }
3161
3162 ret = iterate_inodes_from_logical(logical, fs_info, path,
3163 copy_nocow_pages_for_inode,
3164 nocow_ctx);
3165 if (ret != 0 && ret != -ENOENT) {
3166 pr_warn("iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %llu, ret %d\n",
3167 (unsigned long long)logical,
3168 (unsigned long long)physical_for_dev_replace,
3169 (unsigned long long)len,
3170 (unsigned long long)mirror_num, ret);
3171 not_written = 1;
3172 goto out;
3173 }
3174
3175out:
3176 if (trans && !IS_ERR(trans))
3177 btrfs_end_transaction(trans, root);
3178 if (not_written)
3179 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
3180 num_uncorrectable_read_errors);
3181
3182 btrfs_free_path(path);
3183 kfree(nocow_ctx);
3184
3185 scrub_pending_trans_workers_dec(sctx);
3186}
3187
3188static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root, void *ctx)
3189{
3190 unsigned long index;
3191 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3192 int ret = 0;
3193 struct btrfs_key key;
3194 struct inode *inode = NULL;
3195 struct btrfs_root *local_root;
3196 u64 physical_for_dev_replace;
3197 u64 len;
3198 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Liu Bo6f1c3602013-01-29 03:22:10 +00003199 int srcu_index;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003200
3201 key.objectid = root;
3202 key.type = BTRFS_ROOT_ITEM_KEY;
3203 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00003204
3205 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
3206
Stefan Behrensff023aa2012-11-06 11:43:11 +01003207 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00003208 if (IS_ERR(local_root)) {
3209 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003210 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00003211 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003212
3213 key.type = BTRFS_INODE_ITEM_KEY;
3214 key.objectid = inum;
3215 key.offset = 0;
3216 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00003217 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003218 if (IS_ERR(inode))
3219 return PTR_ERR(inode);
3220
3221 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3222 len = nocow_ctx->len;
3223 while (len >= PAGE_CACHE_SIZE) {
3224 struct page *page = NULL;
3225 int ret_sub;
3226
3227 index = offset >> PAGE_CACHE_SHIFT;
3228
3229 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3230 if (!page) {
3231 pr_err("find_or_create_page() failed\n");
3232 ret = -ENOMEM;
3233 goto next_page;
3234 }
3235
3236 if (PageUptodate(page)) {
3237 if (PageDirty(page))
3238 goto next_page;
3239 } else {
3240 ClearPageError(page);
3241 ret_sub = extent_read_full_page(&BTRFS_I(inode)->
3242 io_tree,
3243 page, btrfs_get_extent,
3244 nocow_ctx->mirror_num);
3245 if (ret_sub) {
3246 ret = ret_sub;
3247 goto next_page;
3248 }
3249 wait_on_page_locked(page);
3250 if (!PageUptodate(page)) {
3251 ret = -EIO;
3252 goto next_page;
3253 }
3254 }
3255 ret_sub = write_page_nocow(nocow_ctx->sctx,
3256 physical_for_dev_replace, page);
3257 if (ret_sub) {
3258 ret = ret_sub;
3259 goto next_page;
3260 }
3261
3262next_page:
3263 if (page) {
3264 unlock_page(page);
3265 put_page(page);
3266 }
3267 offset += PAGE_CACHE_SIZE;
3268 physical_for_dev_replace += PAGE_CACHE_SIZE;
3269 len -= PAGE_CACHE_SIZE;
3270 }
3271
3272 if (inode)
3273 iput(inode);
3274 return ret;
3275}
3276
3277static int write_page_nocow(struct scrub_ctx *sctx,
3278 u64 physical_for_dev_replace, struct page *page)
3279{
3280 struct bio *bio;
3281 struct btrfs_device *dev;
3282 int ret;
3283 DECLARE_COMPLETION_ONSTACK(compl);
3284
3285 dev = sctx->wr_ctx.tgtdev;
3286 if (!dev)
3287 return -EIO;
3288 if (!dev->bdev) {
3289 printk_ratelimited(KERN_WARNING
3290 "btrfs: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
3291 return -EIO;
3292 }
3293 bio = bio_alloc(GFP_NOFS, 1);
3294 if (!bio) {
3295 spin_lock(&sctx->stat_lock);
3296 sctx->stat.malloc_errors++;
3297 spin_unlock(&sctx->stat_lock);
3298 return -ENOMEM;
3299 }
3300 bio->bi_private = &compl;
3301 bio->bi_end_io = scrub_complete_bio_end_io;
3302 bio->bi_size = 0;
3303 bio->bi_sector = physical_for_dev_replace >> 9;
3304 bio->bi_bdev = dev->bdev;
3305 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
3306 if (ret != PAGE_CACHE_SIZE) {
3307leave_with_eio:
3308 bio_put(bio);
3309 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
3310 return -EIO;
3311 }
3312 btrfsic_submit_bio(WRITE_SYNC, bio);
3313 wait_for_completion(&compl);
3314
3315 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
3316 goto leave_with_eio;
3317
3318 bio_put(bio);
3319 return 0;
3320}