blob: 4ba2a69a60ad4ffc327697ec4a8db3d04159d0a8 [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]);
545
Liu Bo69917e42012-09-07 20:01:28 -0600546 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200547 do {
548 ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
549 &ref_root, &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400550 printk_in_rcu(KERN_WARNING
Stefan Behrens1623ede2012-03-27 14:21:26 -0400551 "btrfs: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200552 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400553 "%llu\n", errstr, swarn.logical,
554 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200555 (unsigned long long)swarn.sector,
556 ref_level ? "node" : "leaf",
557 ret < 0 ? -1 : ref_level,
558 ret < 0 ? -1 : ref_root);
559 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600560 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200561 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600562 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200563 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100564 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100565 iterate_extent_inodes(fs_info, found_key.objectid,
566 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200567 scrub_print_warning_inode, &swarn);
568 }
569
570out:
571 btrfs_free_path(path);
572 kfree(swarn.scratch_buf);
573 kfree(swarn.msg_buf);
574}
575
Stefan Behrensff023aa2012-11-06 11:43:11 +0100576static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200577{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200578 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200579 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100580 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200581 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200582 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200583 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200584 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000585 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200586 u64 end = offset + PAGE_SIZE - 1;
587 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000588 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200589
590 key.objectid = root;
591 key.type = BTRFS_ROOT_ITEM_KEY;
592 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000593
594 fs_info = fixup->root->fs_info;
595 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
596
597 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
598 if (IS_ERR(local_root)) {
599 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200600 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000601 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200602
603 key.type = BTRFS_INODE_ITEM_KEY;
604 key.objectid = inum;
605 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000606 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
607 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200608 if (IS_ERR(inode))
609 return PTR_ERR(inode);
610
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200611 index = offset >> PAGE_CACHE_SHIFT;
612
613 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200614 if (!page) {
615 ret = -ENOMEM;
616 goto out;
617 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200618
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200619 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200620 if (PageDirty(page)) {
621 /*
622 * we need to write the data to the defect sector. the
623 * data that was in that sector is not in memory,
624 * because the page was modified. we must not write the
625 * modified page to that sector.
626 *
627 * TODO: what could be done here: wait for the delalloc
628 * runner to write out that page (might involve
629 * COW) and see whether the sector is still
630 * referenced afterwards.
631 *
632 * For the meantime, we'll treat this error
633 * incorrectable, although there is a chance that a
634 * later scrub will find the bad sector again and that
635 * there's no dirty page in memory, then.
636 */
637 ret = -EIO;
638 goto out;
639 }
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100640 fs_info = BTRFS_I(inode)->root->fs_info;
641 ret = repair_io_failure(fs_info, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200642 fixup->logical, page,
643 fixup->mirror_num);
644 unlock_page(page);
645 corrected = !ret;
646 } else {
647 /*
648 * we need to get good data first. the general readpage path
649 * will call repair_io_failure for us, we just have to make
650 * sure we read the bad mirror.
651 */
652 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
653 EXTENT_DAMAGED, GFP_NOFS);
654 if (ret) {
655 /* set_extent_bits should give proper error */
656 WARN_ON(ret > 0);
657 if (ret > 0)
658 ret = -EFAULT;
659 goto out;
660 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200661
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200662 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
663 btrfs_get_extent,
664 fixup->mirror_num);
665 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200666
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200667 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
668 end, EXTENT_DAMAGED, 0, NULL);
669 if (!corrected)
670 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
671 EXTENT_DAMAGED, GFP_NOFS);
672 }
673
674out:
675 if (page)
676 put_page(page);
677 if (inode)
678 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200679
680 if (ret < 0)
681 return ret;
682
683 if (ret == 0 && corrected) {
684 /*
685 * we only need to call readpage for one of the inodes belonging
686 * to this extent. so make iterate_extent_inodes stop
687 */
688 return 1;
689 }
690
691 return -EIO;
692}
693
694static void scrub_fixup_nodatasum(struct btrfs_work *work)
695{
696 int ret;
697 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100698 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200699 struct btrfs_trans_handle *trans = NULL;
700 struct btrfs_fs_info *fs_info;
701 struct btrfs_path *path;
702 int uncorrectable = 0;
703
704 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100705 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200706 fs_info = fixup->root->fs_info;
707
708 path = btrfs_alloc_path();
709 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100710 spin_lock(&sctx->stat_lock);
711 ++sctx->stat.malloc_errors;
712 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200713 uncorrectable = 1;
714 goto out;
715 }
716
717 trans = btrfs_join_transaction(fixup->root);
718 if (IS_ERR(trans)) {
719 uncorrectable = 1;
720 goto out;
721 }
722
723 /*
724 * the idea is to trigger a regular read through the standard path. we
725 * read a page from the (failed) logical address by specifying the
726 * corresponding copynum of the failed sector. thus, that readpage is
727 * expected to fail.
728 * that is the point where on-the-fly error correction will kick in
729 * (once it's finished) and rewrite the failed sector if a good copy
730 * can be found.
731 */
732 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
733 path, scrub_fixup_readpage,
734 fixup);
735 if (ret < 0) {
736 uncorrectable = 1;
737 goto out;
738 }
739 WARN_ON(ret != 1);
740
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100741 spin_lock(&sctx->stat_lock);
742 ++sctx->stat.corrected_errors;
743 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200744
745out:
746 if (trans && !IS_ERR(trans))
747 btrfs_end_transaction(trans, fixup->root);
748 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100749 spin_lock(&sctx->stat_lock);
750 ++sctx->stat.uncorrectable_errors;
751 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100752 btrfs_dev_replace_stats_inc(
753 &sctx->dev_root->fs_info->dev_replace.
754 num_uncorrectable_read_errors);
Josef Bacik606686e2012-06-04 14:03:51 -0400755 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400756 "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -0400757 (unsigned long long)fixup->logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100758 rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200759 }
760
761 btrfs_free_path(path);
762 kfree(fixup);
763
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100764 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200765}
766
Arne Jansena2de7332011-03-08 14:14:00 +0100767/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400768 * scrub_handle_errored_block gets called when either verification of the
769 * pages failed or the bio failed to read, e.g. with EIO. In the latter
770 * case, this function handles all pages in the bio, even though only one
771 * may be bad.
772 * The goal of this function is to repair the errored block by using the
773 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100774 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400775static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100776{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100777 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100778 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400779 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100780 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400781 u64 logical;
782 u64 generation;
783 unsigned int failed_mirror_index;
784 unsigned int is_metadata;
785 unsigned int have_csum;
786 u8 *csum;
787 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
788 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100789 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400790 int mirror_index;
791 int page_num;
792 int success;
793 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
794 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100795
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400796 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100797 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000798 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
799 /*
800 * if we find an error in a super block, we just report it.
801 * They will get written with the next transaction commit
802 * anyway
803 */
804 spin_lock(&sctx->stat_lock);
805 ++sctx->stat.super_errors;
806 spin_unlock(&sctx->stat_lock);
807 return 0;
808 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400809 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100810 logical = sblock_to_check->pagev[0]->logical;
811 generation = sblock_to_check->pagev[0]->generation;
812 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
813 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
814 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400815 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100816 have_csum = sblock_to_check->pagev[0]->have_csum;
817 csum = sblock_to_check->pagev[0]->csum;
818 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400819
Stefan Behrensff023aa2012-11-06 11:43:11 +0100820 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
821 sblocks_for_recheck = NULL;
822 goto nodatasum_case;
823 }
824
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400825 /*
826 * read all mirrors one after the other. This includes to
827 * re-read the extent or metadata block that failed (that was
828 * the cause that this fixup code is called) another time,
829 * page by page this time in order to know which pages
830 * caused I/O errors and which ones are good (for all mirrors).
831 * It is the goal to handle the situation when more than one
832 * mirror contains I/O errors, but the errors do not
833 * overlap, i.e. the data can be repaired by selecting the
834 * pages from those mirrors without I/O error on the
835 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
836 * would be that mirror #1 has an I/O error on the first page,
837 * the second page is good, and mirror #2 has an I/O error on
838 * the second page, but the first page is good.
839 * Then the first page of the first mirror can be repaired by
840 * taking the first page of the second mirror, and the
841 * second page of the second mirror can be repaired by
842 * copying the contents of the 2nd page of the 1st mirror.
843 * One more note: if the pages of one mirror contain I/O
844 * errors, the checksum cannot be verified. In order to get
845 * the best data for repairing, the first attempt is to find
846 * a mirror without I/O errors and with a validated checksum.
847 * Only if this is not possible, the pages are picked from
848 * mirrors with I/O errors without considering the checksum.
849 * If the latter is the case, at the end, the checksum of the
850 * repaired area is verified in order to correctly maintain
851 * the statistics.
852 */
853
854 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
855 sizeof(*sblocks_for_recheck),
856 GFP_NOFS);
857 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100858 spin_lock(&sctx->stat_lock);
859 sctx->stat.malloc_errors++;
860 sctx->stat.read_errors++;
861 sctx->stat.uncorrectable_errors++;
862 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100863 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400864 goto out;
865 }
866
867 /* setup the context, map the logical blocks and alloc the pages */
Stefan Behrensff023aa2012-11-06 11:43:11 +0100868 ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400869 logical, sblocks_for_recheck);
870 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100871 spin_lock(&sctx->stat_lock);
872 sctx->stat.read_errors++;
873 sctx->stat.uncorrectable_errors++;
874 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100875 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400876 goto out;
877 }
878 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
879 sblock_bad = sblocks_for_recheck + failed_mirror_index;
880
881 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100882 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
883 csum, generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400884
885 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
886 sblock_bad->no_io_error_seen) {
887 /*
888 * the error disappeared after reading page by page, or
889 * the area was part of a huge bio and other parts of the
890 * bio caused I/O errors, or the block layer merged several
891 * read requests into one and the error is caused by a
892 * different bio (usually one of the two latter cases is
893 * the cause)
894 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100895 spin_lock(&sctx->stat_lock);
896 sctx->stat.unverified_errors++;
897 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400898
Stefan Behrensff023aa2012-11-06 11:43:11 +0100899 if (sctx->is_dev_replace)
900 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400901 goto out;
902 }
903
904 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100905 spin_lock(&sctx->stat_lock);
906 sctx->stat.read_errors++;
907 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400908 if (__ratelimit(&_rs))
909 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100910 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400911 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100912 spin_lock(&sctx->stat_lock);
913 sctx->stat.csum_errors++;
914 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400915 if (__ratelimit(&_rs))
916 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100917 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200918 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400919 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100920 spin_lock(&sctx->stat_lock);
921 sctx->stat.verify_errors++;
922 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400923 if (__ratelimit(&_rs))
924 scrub_print_warning("checksum/header error",
925 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200926 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100927 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200928 BTRFS_DEV_STAT_GENERATION_ERRS);
929 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100930 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200931 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400932 }
933
Stefan Behrensff023aa2012-11-06 11:43:11 +0100934 if (sctx->readonly && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400935 goto did_not_correct_error;
936
937 if (!is_metadata && !have_csum) {
938 struct scrub_fixup_nodatasum *fixup_nodatasum;
939
Stefan Behrensff023aa2012-11-06 11:43:11 +0100940nodatasum_case:
941 WARN_ON(sctx->is_dev_replace);
942
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400943 /*
944 * !is_metadata and !have_csum, this means that the data
945 * might not be COW'ed, that it might be modified
946 * concurrently. The general strategy to work on the
947 * commit root does not help in the case when COW is not
948 * used.
949 */
950 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
951 if (!fixup_nodatasum)
952 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100953 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100954 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400955 fixup_nodatasum->logical = logical;
956 fixup_nodatasum->root = fs_info->extent_root;
957 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100958 scrub_pending_trans_workers_inc(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400959 fixup_nodatasum->work.func = scrub_fixup_nodatasum;
960 btrfs_queue_worker(&fs_info->scrub_workers,
961 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +0100962 goto out;
963 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400964
965 /*
966 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100967 * checksums.
968 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400969 * errors and also does not have a checksum error.
970 * If one is found, and if a checksum is present, the full block
971 * that is known to contain an error is rewritten. Afterwards
972 * the block is known to be corrected.
973 * If a mirror is found which is completely correct, and no
974 * checksum is present, only those pages are rewritten that had
975 * an I/O error in the block to be repaired, since it cannot be
976 * determined, which copy of the other pages is better (and it
977 * could happen otherwise that a correct page would be
978 * overwritten by a bad one).
979 */
980 for (mirror_index = 0;
981 mirror_index < BTRFS_MAX_MIRRORS &&
982 sblocks_for_recheck[mirror_index].page_count > 0;
983 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100984 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400985
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100986 if (mirror_index == failed_mirror_index)
987 continue;
988 sblock_other = sblocks_for_recheck + mirror_index;
989
990 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100991 scrub_recheck_block(fs_info, sblock_other, is_metadata,
992 have_csum, csum, generation,
993 sctx->csum_size);
994
995 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400996 !sblock_other->checksum_error &&
997 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100998 if (sctx->is_dev_replace) {
999 scrub_write_block_to_dev_replace(sblock_other);
1000 } else {
1001 int force_write = is_metadata || have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001002
Stefan Behrensff023aa2012-11-06 11:43:11 +01001003 ret = scrub_repair_block_from_good_copy(
1004 sblock_bad, sblock_other,
1005 force_write);
1006 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001007 if (0 == ret)
1008 goto corrected_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001009 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001010 }
1011
1012 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001013 * for dev_replace, pick good pages and write to the target device.
1014 */
1015 if (sctx->is_dev_replace) {
1016 success = 1;
1017 for (page_num = 0; page_num < sblock_bad->page_count;
1018 page_num++) {
1019 int sub_success;
1020
1021 sub_success = 0;
1022 for (mirror_index = 0;
1023 mirror_index < BTRFS_MAX_MIRRORS &&
1024 sblocks_for_recheck[mirror_index].page_count > 0;
1025 mirror_index++) {
1026 struct scrub_block *sblock_other =
1027 sblocks_for_recheck + mirror_index;
1028 struct scrub_page *page_other =
1029 sblock_other->pagev[page_num];
1030
1031 if (!page_other->io_error) {
1032 ret = scrub_write_page_to_dev_replace(
1033 sblock_other, page_num);
1034 if (ret == 0) {
1035 /* succeeded for this page */
1036 sub_success = 1;
1037 break;
1038 } else {
1039 btrfs_dev_replace_stats_inc(
1040 &sctx->dev_root->
1041 fs_info->dev_replace.
1042 num_write_errors);
1043 }
1044 }
1045 }
1046
1047 if (!sub_success) {
1048 /*
1049 * did not find a mirror to fetch the page
1050 * from. scrub_write_page_to_dev_replace()
1051 * handles this case (page->io_error), by
1052 * filling the block with zeros before
1053 * submitting the write request
1054 */
1055 success = 0;
1056 ret = scrub_write_page_to_dev_replace(
1057 sblock_bad, page_num);
1058 if (ret)
1059 btrfs_dev_replace_stats_inc(
1060 &sctx->dev_root->fs_info->
1061 dev_replace.num_write_errors);
1062 }
1063 }
1064
1065 goto out;
1066 }
1067
1068 /*
1069 * for regular scrub, repair those pages that are errored.
1070 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001071 * repaired, continue by picking good copies of those pages.
1072 * Select the good pages from mirrors to rewrite bad pages from
1073 * the area to fix. Afterwards verify the checksum of the block
1074 * that is supposed to be repaired. This verification step is
1075 * only done for the purpose of statistic counting and for the
1076 * final scrub report, whether errors remain.
1077 * A perfect algorithm could make use of the checksum and try
1078 * all possible combinations of pages from the different mirrors
1079 * until the checksum verification succeeds. For example, when
1080 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1081 * of mirror #2 is readable but the final checksum test fails,
1082 * then the 2nd page of mirror #3 could be tried, whether now
1083 * the final checksum succeedes. But this would be a rare
1084 * exception and is therefore not implemented. At least it is
1085 * avoided that the good copy is overwritten.
1086 * A more useful improvement would be to pick the sectors
1087 * without I/O error based on sector sizes (512 bytes on legacy
1088 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1089 * mirror could be repaired by taking 512 byte of a different
1090 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1091 * area are unreadable.
1092 */
1093
1094 /* can only fix I/O errors from here on */
1095 if (sblock_bad->no_io_error_seen)
1096 goto did_not_correct_error;
1097
1098 success = 1;
1099 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001100 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001101
1102 if (!page_bad->io_error)
1103 continue;
1104
1105 for (mirror_index = 0;
1106 mirror_index < BTRFS_MAX_MIRRORS &&
1107 sblocks_for_recheck[mirror_index].page_count > 0;
1108 mirror_index++) {
1109 struct scrub_block *sblock_other = sblocks_for_recheck +
1110 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001111 struct scrub_page *page_other = sblock_other->pagev[
1112 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001113
1114 if (!page_other->io_error) {
1115 ret = scrub_repair_page_from_good_copy(
1116 sblock_bad, sblock_other, page_num, 0);
1117 if (0 == ret) {
1118 page_bad->io_error = 0;
1119 break; /* succeeded for this page */
1120 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001121 }
1122 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001123
1124 if (page_bad->io_error) {
1125 /* did not find a mirror to copy the page from */
1126 success = 0;
1127 }
1128 }
1129
1130 if (success) {
1131 if (is_metadata || have_csum) {
1132 /*
1133 * need to verify the checksum now that all
1134 * sectors on disk are repaired (the write
1135 * request for data to be repaired is on its way).
1136 * Just be lazy and use scrub_recheck_block()
1137 * which re-reads the data before the checksum
1138 * is verified, but most likely the data comes out
1139 * of the page cache.
1140 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001141 scrub_recheck_block(fs_info, sblock_bad,
1142 is_metadata, have_csum, csum,
1143 generation, sctx->csum_size);
1144 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001145 !sblock_bad->checksum_error &&
1146 sblock_bad->no_io_error_seen)
1147 goto corrected_error;
1148 else
1149 goto did_not_correct_error;
1150 } else {
1151corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001152 spin_lock(&sctx->stat_lock);
1153 sctx->stat.corrected_errors++;
1154 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001155 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001156 "btrfs: fixed up error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -04001157 (unsigned long long)logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001158 rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001159 }
1160 } else {
1161did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001162 spin_lock(&sctx->stat_lock);
1163 sctx->stat.uncorrectable_errors++;
1164 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001165 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001166 "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
Josef Bacik606686e2012-06-04 14:03:51 -04001167 (unsigned long long)logical,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001168 rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001169 }
1170
1171out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001172 if (sblocks_for_recheck) {
1173 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1174 mirror_index++) {
1175 struct scrub_block *sblock = sblocks_for_recheck +
1176 mirror_index;
1177 int page_index;
1178
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001179 for (page_index = 0; page_index < sblock->page_count;
1180 page_index++) {
1181 sblock->pagev[page_index]->sblock = NULL;
1182 scrub_page_put(sblock->pagev[page_index]);
1183 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001184 }
1185 kfree(sblocks_for_recheck);
1186 }
1187
1188 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001189}
1190
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001191static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001192 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001193 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001194 u64 length, u64 logical,
1195 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001196{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001197 int page_index;
1198 int mirror_index;
1199 int ret;
1200
1201 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001202 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001203 * are not used (and not set) in the blocks that are used for
1204 * the recheck procedure
1205 */
1206
1207 page_index = 0;
1208 while (length > 0) {
1209 u64 sublen = min_t(u64, length, PAGE_SIZE);
1210 u64 mapped_length = sublen;
1211 struct btrfs_bio *bbio = NULL;
1212
1213 /*
1214 * with a length of PAGE_SIZE, each returned stripe
1215 * represents one mirror
1216 */
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01001217 ret = btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, logical,
1218 &mapped_length, &bbio, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001219 if (ret || !bbio || mapped_length < sublen) {
1220 kfree(bbio);
1221 return -EIO;
1222 }
1223
Stefan Behrensff023aa2012-11-06 11:43:11 +01001224 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001225 for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
1226 mirror_index++) {
1227 struct scrub_block *sblock;
1228 struct scrub_page *page;
1229
1230 if (mirror_index >= BTRFS_MAX_MIRRORS)
1231 continue;
1232
1233 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001234 sblock->sctx = sctx;
1235 page = kzalloc(sizeof(*page), GFP_NOFS);
1236 if (!page) {
1237leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001238 spin_lock(&sctx->stat_lock);
1239 sctx->stat.malloc_errors++;
1240 spin_unlock(&sctx->stat_lock);
Wei Yongjuncf93dcc2012-09-02 07:44:51 -06001241 kfree(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001242 return -ENOMEM;
1243 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001244 scrub_page_get(page);
1245 sblock->pagev[page_index] = page;
1246 page->logical = logical;
1247 page->physical = bbio->stripes[mirror_index].physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001248 BUG_ON(page_index >= original_sblock->page_count);
1249 page->physical_for_dev_replace =
1250 original_sblock->pagev[page_index]->
1251 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001252 /* for missing devices, dev->bdev is NULL */
1253 page->dev = bbio->stripes[mirror_index].dev;
1254 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001255 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001256 page->page = alloc_page(GFP_NOFS);
1257 if (!page->page)
1258 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001259 }
1260 kfree(bbio);
1261 length -= sublen;
1262 logical += sublen;
1263 page_index++;
1264 }
1265
1266 return 0;
1267}
1268
1269/*
1270 * this function will check the on disk data for checksum errors, header
1271 * errors and read I/O errors. If any I/O errors happen, the exact pages
1272 * which are errored are marked as being bad. The goal is to enable scrub
1273 * to take those pages that are not errored from all the mirrors so that
1274 * the pages that are errored in the just handled mirror can be repaired.
1275 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001276static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1277 struct scrub_block *sblock, int is_metadata,
1278 int have_csum, u8 *csum, u64 generation,
1279 u16 csum_size)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001280{
1281 int page_num;
1282
1283 sblock->no_io_error_seen = 1;
1284 sblock->header_error = 0;
1285 sblock->checksum_error = 0;
1286
1287 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1288 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001289 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001290 DECLARE_COMPLETION_ONSTACK(complete);
1291
Stefan Behrens442a4f62012-05-25 16:06:08 +02001292 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001293 page->io_error = 1;
1294 sblock->no_io_error_seen = 0;
1295 continue;
1296 }
1297
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001298 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001299 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001300 if (!bio) {
1301 page->io_error = 1;
1302 sblock->no_io_error_seen = 0;
1303 continue;
1304 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001305 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001306 bio->bi_sector = page->physical >> 9;
1307 bio->bi_end_io = scrub_complete_bio_end_io;
1308 bio->bi_private = &complete;
1309
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001310 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001311 btrfsic_submit_bio(READ, bio);
1312
1313 /* this will also unplug the queue */
1314 wait_for_completion(&complete);
1315
1316 page->io_error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
1317 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1318 sblock->no_io_error_seen = 0;
1319 bio_put(bio);
1320 }
1321
1322 if (sblock->no_io_error_seen)
1323 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1324 have_csum, csum, generation,
1325 csum_size);
1326
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001327 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001328}
1329
1330static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1331 struct scrub_block *sblock,
1332 int is_metadata, int have_csum,
1333 const u8 *csum, u64 generation,
1334 u16 csum_size)
1335{
1336 int page_num;
1337 u8 calculated_csum[BTRFS_CSUM_SIZE];
1338 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001339 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)
Liu Bob0496682013-03-14 14:57:45 +00001367 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001368 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1369 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1370 else
Liu Bob0496682013-03-14 14:57:45 +00001371 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001372
Linus Torvalds9613beb2012-03-30 12:44:29 -07001373 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001374 page_num++;
1375 if (page_num >= sblock->page_count)
1376 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001377 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001378
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001379 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001380 }
1381
1382 btrfs_csum_final(crc, calculated_csum);
1383 if (memcmp(calculated_csum, csum, csum_size))
1384 sblock->checksum_error = 1;
1385}
1386
1387static void scrub_complete_bio_end_io(struct bio *bio, int err)
1388{
1389 complete((struct completion *)bio->bi_private);
1390}
1391
1392static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1393 struct scrub_block *sblock_good,
1394 int force_write)
1395{
1396 int page_num;
1397 int ret = 0;
1398
1399 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1400 int ret_sub;
1401
1402 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1403 sblock_good,
1404 page_num,
1405 force_write);
1406 if (ret_sub)
1407 ret = ret_sub;
1408 }
1409
1410 return ret;
1411}
1412
1413static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1414 struct scrub_block *sblock_good,
1415 int page_num, int force_write)
1416{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001417 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1418 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001419
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001420 BUG_ON(page_bad->page == NULL);
1421 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001422 if (force_write || sblock_bad->header_error ||
1423 sblock_bad->checksum_error || page_bad->io_error) {
1424 struct bio *bio;
1425 int ret;
1426 DECLARE_COMPLETION_ONSTACK(complete);
1427
Stefan Behrensff023aa2012-11-06 11:43:11 +01001428 if (!page_bad->dev->bdev) {
1429 printk_ratelimited(KERN_WARNING
1430 "btrfs: scrub_repair_page_from_good_copy(bdev == NULL) is unexpected!\n");
1431 return -EIO;
1432 }
1433
Chris Mason9be33952013-05-17 18:30:14 -04001434 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001435 if (!bio)
1436 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001437 bio->bi_bdev = page_bad->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001438 bio->bi_sector = page_bad->physical >> 9;
1439 bio->bi_end_io = scrub_complete_bio_end_io;
1440 bio->bi_private = &complete;
1441
1442 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1443 if (PAGE_SIZE != ret) {
1444 bio_put(bio);
1445 return -EIO;
1446 }
1447 btrfsic_submit_bio(WRITE, bio);
1448
1449 /* this will also unplug the queue */
1450 wait_for_completion(&complete);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001451 if (!bio_flagged(bio, BIO_UPTODATE)) {
1452 btrfs_dev_stat_inc_and_print(page_bad->dev,
1453 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001454 btrfs_dev_replace_stats_inc(
1455 &sblock_bad->sctx->dev_root->fs_info->
1456 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001457 bio_put(bio);
1458 return -EIO;
1459 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001460 bio_put(bio);
1461 }
1462
1463 return 0;
1464}
1465
Stefan Behrensff023aa2012-11-06 11:43:11 +01001466static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1467{
1468 int page_num;
1469
1470 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1471 int ret;
1472
1473 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1474 if (ret)
1475 btrfs_dev_replace_stats_inc(
1476 &sblock->sctx->dev_root->fs_info->dev_replace.
1477 num_write_errors);
1478 }
1479}
1480
1481static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1482 int page_num)
1483{
1484 struct scrub_page *spage = sblock->pagev[page_num];
1485
1486 BUG_ON(spage->page == NULL);
1487 if (spage->io_error) {
1488 void *mapped_buffer = kmap_atomic(spage->page);
1489
1490 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1491 flush_dcache_page(spage->page);
1492 kunmap_atomic(mapped_buffer);
1493 }
1494 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1495}
1496
1497static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1498 struct scrub_page *spage)
1499{
1500 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1501 struct scrub_bio *sbio;
1502 int ret;
1503
1504 mutex_lock(&wr_ctx->wr_lock);
1505again:
1506 if (!wr_ctx->wr_curr_bio) {
1507 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1508 GFP_NOFS);
1509 if (!wr_ctx->wr_curr_bio) {
1510 mutex_unlock(&wr_ctx->wr_lock);
1511 return -ENOMEM;
1512 }
1513 wr_ctx->wr_curr_bio->sctx = sctx;
1514 wr_ctx->wr_curr_bio->page_count = 0;
1515 }
1516 sbio = wr_ctx->wr_curr_bio;
1517 if (sbio->page_count == 0) {
1518 struct bio *bio;
1519
1520 sbio->physical = spage->physical_for_dev_replace;
1521 sbio->logical = spage->logical;
1522 sbio->dev = wr_ctx->tgtdev;
1523 bio = sbio->bio;
1524 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001525 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001526 if (!bio) {
1527 mutex_unlock(&wr_ctx->wr_lock);
1528 return -ENOMEM;
1529 }
1530 sbio->bio = bio;
1531 }
1532
1533 bio->bi_private = sbio;
1534 bio->bi_end_io = scrub_wr_bio_end_io;
1535 bio->bi_bdev = sbio->dev->bdev;
1536 bio->bi_sector = sbio->physical >> 9;
1537 sbio->err = 0;
1538 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1539 spage->physical_for_dev_replace ||
1540 sbio->logical + sbio->page_count * PAGE_SIZE !=
1541 spage->logical) {
1542 scrub_wr_submit(sctx);
1543 goto again;
1544 }
1545
1546 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1547 if (ret != PAGE_SIZE) {
1548 if (sbio->page_count < 1) {
1549 bio_put(sbio->bio);
1550 sbio->bio = NULL;
1551 mutex_unlock(&wr_ctx->wr_lock);
1552 return -EIO;
1553 }
1554 scrub_wr_submit(sctx);
1555 goto again;
1556 }
1557
1558 sbio->pagev[sbio->page_count] = spage;
1559 scrub_page_get(spage);
1560 sbio->page_count++;
1561 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1562 scrub_wr_submit(sctx);
1563 mutex_unlock(&wr_ctx->wr_lock);
1564
1565 return 0;
1566}
1567
1568static void scrub_wr_submit(struct scrub_ctx *sctx)
1569{
1570 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1571 struct scrub_bio *sbio;
1572
1573 if (!wr_ctx->wr_curr_bio)
1574 return;
1575
1576 sbio = wr_ctx->wr_curr_bio;
1577 wr_ctx->wr_curr_bio = NULL;
1578 WARN_ON(!sbio->bio->bi_bdev);
1579 scrub_pending_bio_inc(sctx);
1580 /* process all writes in a single worker thread. Then the block layer
1581 * orders the requests before sending them to the driver which
1582 * doubled the write performance on spinning disks when measured
1583 * with Linux 3.5 */
1584 btrfsic_submit_bio(WRITE, sbio->bio);
1585}
1586
1587static void scrub_wr_bio_end_io(struct bio *bio, int err)
1588{
1589 struct scrub_bio *sbio = bio->bi_private;
1590 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1591
1592 sbio->err = err;
1593 sbio->bio = bio;
1594
1595 sbio->work.func = scrub_wr_bio_end_io_worker;
1596 btrfs_queue_worker(&fs_info->scrub_wr_completion_workers, &sbio->work);
1597}
1598
1599static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1600{
1601 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1602 struct scrub_ctx *sctx = sbio->sctx;
1603 int i;
1604
1605 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1606 if (sbio->err) {
1607 struct btrfs_dev_replace *dev_replace =
1608 &sbio->sctx->dev_root->fs_info->dev_replace;
1609
1610 for (i = 0; i < sbio->page_count; i++) {
1611 struct scrub_page *spage = sbio->pagev[i];
1612
1613 spage->io_error = 1;
1614 btrfs_dev_replace_stats_inc(&dev_replace->
1615 num_write_errors);
1616 }
1617 }
1618
1619 for (i = 0; i < sbio->page_count; i++)
1620 scrub_page_put(sbio->pagev[i]);
1621
1622 bio_put(sbio->bio);
1623 kfree(sbio);
1624 scrub_pending_bio_dec(sctx);
1625}
1626
1627static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001628{
1629 u64 flags;
1630 int ret;
1631
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001632 WARN_ON(sblock->page_count < 1);
1633 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001634 ret = 0;
1635 if (flags & BTRFS_EXTENT_FLAG_DATA)
1636 ret = scrub_checksum_data(sblock);
1637 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1638 ret = scrub_checksum_tree_block(sblock);
1639 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1640 (void)scrub_checksum_super(sblock);
1641 else
1642 WARN_ON(1);
1643 if (ret)
1644 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001645
1646 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001647}
1648
1649static int scrub_checksum_data(struct scrub_block *sblock)
1650{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001651 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001652 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001653 u8 *on_disk_csum;
1654 struct page *page;
1655 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001656 u32 crc = ~(u32)0;
1657 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001658 u64 len;
1659 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001660
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001661 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001662 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001663 return 0;
1664
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001665 on_disk_csum = sblock->pagev[0]->csum;
1666 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001667 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001668
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001669 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001670 index = 0;
1671 for (;;) {
1672 u64 l = min_t(u64, len, PAGE_SIZE);
1673
Liu Bob0496682013-03-14 14:57:45 +00001674 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001675 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001676 len -= l;
1677 if (len == 0)
1678 break;
1679 index++;
1680 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001681 BUG_ON(!sblock->pagev[index]->page);
1682 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001683 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001684 }
1685
Arne Jansena2de7332011-03-08 14:14:00 +01001686 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001687 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001688 fail = 1;
1689
Arne Jansena2de7332011-03-08 14:14:00 +01001690 return fail;
1691}
1692
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001693static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001694{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001695 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001696 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001697 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001698 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001699 u8 calculated_csum[BTRFS_CSUM_SIZE];
1700 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1701 struct page *page;
1702 void *mapped_buffer;
1703 u64 mapped_size;
1704 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001705 u32 crc = ~(u32)0;
1706 int fail = 0;
1707 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001708 u64 len;
1709 int index;
1710
1711 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001712 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001713 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001714 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001715 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001716
1717 /*
1718 * we don't use the getter functions here, as we
1719 * a) don't have an extent buffer and
1720 * b) the page is already kmapped
1721 */
Arne Jansena2de7332011-03-08 14:14:00 +01001722
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001723 if (sblock->pagev[0]->logical != le64_to_cpu(h->bytenr))
Arne Jansena2de7332011-03-08 14:14:00 +01001724 ++fail;
1725
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001726 if (sblock->pagev[0]->generation != le64_to_cpu(h->generation))
Arne Jansena2de7332011-03-08 14:14:00 +01001727 ++fail;
1728
1729 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1730 ++fail;
1731
1732 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1733 BTRFS_UUID_SIZE))
1734 ++fail;
1735
Stefan Behrensff023aa2012-11-06 11:43:11 +01001736 WARN_ON(sctx->nodesize != sctx->leafsize);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001737 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001738 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1739 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1740 index = 0;
1741 for (;;) {
1742 u64 l = min_t(u64, len, mapped_size);
1743
Liu Bob0496682013-03-14 14:57:45 +00001744 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001745 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001746 len -= l;
1747 if (len == 0)
1748 break;
1749 index++;
1750 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001751 BUG_ON(!sblock->pagev[index]->page);
1752 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001753 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001754 mapped_size = PAGE_SIZE;
1755 p = mapped_buffer;
1756 }
1757
1758 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001759 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001760 ++crc_fail;
1761
Arne Jansena2de7332011-03-08 14:14:00 +01001762 return fail || crc_fail;
1763}
1764
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001765static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001766{
1767 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001768 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001769 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001770 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001771 u8 calculated_csum[BTRFS_CSUM_SIZE];
1772 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1773 struct page *page;
1774 void *mapped_buffer;
1775 u64 mapped_size;
1776 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001777 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001778 int fail_gen = 0;
1779 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001780 u64 len;
1781 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001782
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001783 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001784 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001785 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001786 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001787 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001788
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001789 if (sblock->pagev[0]->logical != le64_to_cpu(s->bytenr))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001790 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001791
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001792 if (sblock->pagev[0]->generation != le64_to_cpu(s->generation))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001793 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001794
1795 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001796 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001797
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001798 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1799 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1800 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1801 index = 0;
1802 for (;;) {
1803 u64 l = min_t(u64, len, mapped_size);
1804
Liu Bob0496682013-03-14 14:57:45 +00001805 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001806 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001807 len -= l;
1808 if (len == 0)
1809 break;
1810 index++;
1811 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001812 BUG_ON(!sblock->pagev[index]->page);
1813 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001814 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001815 mapped_size = PAGE_SIZE;
1816 p = mapped_buffer;
1817 }
1818
1819 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001820 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001821 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001822
Stefan Behrens442a4f62012-05-25 16:06:08 +02001823 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001824 /*
1825 * if we find an error in a super block, we just report it.
1826 * They will get written with the next transaction commit
1827 * anyway
1828 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001829 spin_lock(&sctx->stat_lock);
1830 ++sctx->stat.super_errors;
1831 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001832 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001833 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001834 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1835 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001836 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001837 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001838 }
1839
Stefan Behrens442a4f62012-05-25 16:06:08 +02001840 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001841}
1842
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001843static void scrub_block_get(struct scrub_block *sblock)
1844{
1845 atomic_inc(&sblock->ref_count);
1846}
1847
1848static void scrub_block_put(struct scrub_block *sblock)
1849{
1850 if (atomic_dec_and_test(&sblock->ref_count)) {
1851 int i;
1852
1853 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001854 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001855 kfree(sblock);
1856 }
1857}
1858
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001859static void scrub_page_get(struct scrub_page *spage)
1860{
1861 atomic_inc(&spage->ref_count);
1862}
1863
1864static void scrub_page_put(struct scrub_page *spage)
1865{
1866 if (atomic_dec_and_test(&spage->ref_count)) {
1867 if (spage->page)
1868 __free_page(spage->page);
1869 kfree(spage);
1870 }
1871}
1872
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001873static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01001874{
1875 struct scrub_bio *sbio;
1876
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001877 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04001878 return;
Arne Jansena2de7332011-03-08 14:14:00 +01001879
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001880 sbio = sctx->bios[sctx->curr];
1881 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001882 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001883
Stefan Behrensff023aa2012-11-06 11:43:11 +01001884 if (!sbio->bio->bi_bdev) {
1885 /*
1886 * this case should not happen. If btrfs_map_block() is
1887 * wrong, it could happen for dev-replace operations on
1888 * missing devices when no mirrors are available, but in
1889 * this case it should already fail the mount.
1890 * This case is handled correctly (but _very_ slowly).
1891 */
1892 printk_ratelimited(KERN_WARNING
1893 "btrfs: scrub_submit(bio bdev == NULL) is unexpected!\n");
1894 bio_endio(sbio->bio, -EIO);
1895 } else {
1896 btrfsic_submit_bio(READ, sbio->bio);
1897 }
Arne Jansena2de7332011-03-08 14:14:00 +01001898}
1899
Stefan Behrensff023aa2012-11-06 11:43:11 +01001900static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
1901 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01001902{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001903 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01001904 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001905 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01001906
1907again:
1908 /*
1909 * grab a fresh bio or wait for one to become available
1910 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001911 while (sctx->curr == -1) {
1912 spin_lock(&sctx->list_lock);
1913 sctx->curr = sctx->first_free;
1914 if (sctx->curr != -1) {
1915 sctx->first_free = sctx->bios[sctx->curr]->next_free;
1916 sctx->bios[sctx->curr]->next_free = -1;
1917 sctx->bios[sctx->curr]->page_count = 0;
1918 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001919 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001920 spin_unlock(&sctx->list_lock);
1921 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01001922 }
1923 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001924 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001925 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05001926 struct bio *bio;
1927
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001928 sbio->physical = spage->physical;
1929 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001930 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001931 bio = sbio->bio;
1932 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001933 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001934 if (!bio)
1935 return -ENOMEM;
1936 sbio->bio = bio;
1937 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05001938
1939 bio->bi_private = sbio;
1940 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001941 bio->bi_bdev = sbio->dev->bdev;
1942 bio->bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001943 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001944 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1945 spage->physical ||
1946 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001947 spage->logical ||
1948 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001949 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05001950 goto again;
1951 }
1952
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001953 sbio->pagev[sbio->page_count] = spage;
1954 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1955 if (ret != PAGE_SIZE) {
1956 if (sbio->page_count < 1) {
1957 bio_put(sbio->bio);
1958 sbio->bio = NULL;
1959 return -EIO;
1960 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001961 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001962 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01001963 }
Arne Jansen1bc87792011-05-28 21:57:55 +02001964
Stefan Behrensff023aa2012-11-06 11:43:11 +01001965 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001966 atomic_inc(&sblock->outstanding_pages);
1967 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001968 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001969 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001970
1971 return 0;
1972}
1973
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001974static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001975 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001976 u64 gen, int mirror_num, u8 *csum, int force,
1977 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001978{
1979 struct scrub_block *sblock;
1980 int index;
1981
1982 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
1983 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001984 spin_lock(&sctx->stat_lock);
1985 sctx->stat.malloc_errors++;
1986 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001987 return -ENOMEM;
1988 }
1989
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001990 /* one ref inside this function, plus one for each page added to
1991 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001992 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001993 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001994 sblock->no_io_error_seen = 1;
1995
1996 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001997 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001998 u64 l = min_t(u64, len, PAGE_SIZE);
1999
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002000 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2001 if (!spage) {
2002leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002003 spin_lock(&sctx->stat_lock);
2004 sctx->stat.malloc_errors++;
2005 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002006 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002007 return -ENOMEM;
2008 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002009 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2010 scrub_page_get(spage);
2011 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002012 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002013 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002014 spage->flags = flags;
2015 spage->generation = gen;
2016 spage->logical = logical;
2017 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002018 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002019 spage->mirror_num = mirror_num;
2020 if (csum) {
2021 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002022 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002023 } else {
2024 spage->have_csum = 0;
2025 }
2026 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002027 spage->page = alloc_page(GFP_NOFS);
2028 if (!spage->page)
2029 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002030 len -= l;
2031 logical += l;
2032 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002033 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002034 }
2035
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002036 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002037 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002038 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002039 int ret;
2040
Stefan Behrensff023aa2012-11-06 11:43:11 +01002041 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002042 if (ret) {
2043 scrub_block_put(sblock);
2044 return ret;
2045 }
2046 }
2047
2048 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002049 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002050
2051 /* last one frees, either here or in bio completion for last page */
2052 scrub_block_put(sblock);
2053 return 0;
2054}
2055
2056static void scrub_bio_end_io(struct bio *bio, int err)
2057{
2058 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002059 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002060
2061 sbio->err = err;
2062 sbio->bio = bio;
2063
2064 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
2065}
2066
2067static void scrub_bio_end_io_worker(struct btrfs_work *work)
2068{
2069 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002070 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002071 int i;
2072
Stefan Behrensff023aa2012-11-06 11:43:11 +01002073 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002074 if (sbio->err) {
2075 for (i = 0; i < sbio->page_count; i++) {
2076 struct scrub_page *spage = sbio->pagev[i];
2077
2078 spage->io_error = 1;
2079 spage->sblock->no_io_error_seen = 0;
2080 }
2081 }
2082
2083 /* now complete the scrub_block items that have all pages completed */
2084 for (i = 0; i < sbio->page_count; i++) {
2085 struct scrub_page *spage = sbio->pagev[i];
2086 struct scrub_block *sblock = spage->sblock;
2087
2088 if (atomic_dec_and_test(&sblock->outstanding_pages))
2089 scrub_block_complete(sblock);
2090 scrub_block_put(sblock);
2091 }
2092
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002093 bio_put(sbio->bio);
2094 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002095 spin_lock(&sctx->list_lock);
2096 sbio->next_free = sctx->first_free;
2097 sctx->first_free = sbio->index;
2098 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002099
2100 if (sctx->is_dev_replace &&
2101 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2102 mutex_lock(&sctx->wr_ctx.wr_lock);
2103 scrub_wr_submit(sctx);
2104 mutex_unlock(&sctx->wr_ctx.wr_lock);
2105 }
2106
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002107 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002108}
2109
2110static void scrub_block_complete(struct scrub_block *sblock)
2111{
Stefan Behrensff023aa2012-11-06 11:43:11 +01002112 if (!sblock->no_io_error_seen) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002113 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002114 } else {
2115 /*
2116 * if has checksum error, write via repair mechanism in
2117 * dev replace case, otherwise write here in dev replace
2118 * case.
2119 */
2120 if (!scrub_checksum(sblock) && sblock->sctx->is_dev_replace)
2121 scrub_write_block_to_dev_replace(sblock);
2122 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002123}
2124
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002125static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002126 u8 *csum)
2127{
2128 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002129 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002130 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002131
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002132 while (!list_empty(&sctx->csum_list)) {
2133 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002134 struct btrfs_ordered_sum, list);
2135 if (sum->bytenr > logical)
2136 return 0;
2137 if (sum->bytenr + sum->len > logical)
2138 break;
2139
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002140 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002141 list_del(&sum->list);
2142 kfree(sum);
2143 sum = NULL;
2144 }
2145 if (!sum)
2146 return 0;
2147
Miao Xief51a4a12013-06-19 10:36:09 +08002148 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002149 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002150 memcpy(csum, sum->sums + index, sctx->csum_size);
2151 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002152 list_del(&sum->list);
2153 kfree(sum);
2154 }
Miao Xief51a4a12013-06-19 10:36:09 +08002155 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002156}
2157
2158/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002159static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002160 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002161 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002162{
2163 int ret;
2164 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002165 u32 blocksize;
2166
2167 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002168 blocksize = sctx->sectorsize;
2169 spin_lock(&sctx->stat_lock);
2170 sctx->stat.data_extents_scrubbed++;
2171 sctx->stat.data_bytes_scrubbed += len;
2172 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002173 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002174 WARN_ON(sctx->nodesize != sctx->leafsize);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002175 blocksize = sctx->nodesize;
2176 spin_lock(&sctx->stat_lock);
2177 sctx->stat.tree_extents_scrubbed++;
2178 sctx->stat.tree_bytes_scrubbed += len;
2179 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002180 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002181 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002182 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002183 }
Arne Jansena2de7332011-03-08 14:14:00 +01002184
2185 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002186 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002187 int have_csum = 0;
2188
2189 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2190 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002191 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002192 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002193 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002194 if (sctx->is_dev_replace && !have_csum) {
2195 ret = copy_nocow_pages(sctx, logical, l,
2196 mirror_num,
2197 physical_for_dev_replace);
2198 goto behind_scrub_pages;
2199 }
Arne Jansena2de7332011-03-08 14:14:00 +01002200 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002201 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002202 mirror_num, have_csum ? csum : NULL, 0,
2203 physical_for_dev_replace);
2204behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002205 if (ret)
2206 return ret;
2207 len -= l;
2208 logical += l;
2209 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002210 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002211 }
2212 return 0;
2213}
2214
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002215static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002216 struct map_lookup *map,
2217 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002218 int num, u64 base, u64 length,
2219 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002220{
2221 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002222 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002223 struct btrfs_root *root = fs_info->extent_root;
2224 struct btrfs_root *csum_root = fs_info->csum_root;
2225 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002226 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002227 u64 flags;
2228 int ret;
2229 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002230 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002231 struct extent_buffer *l;
2232 struct btrfs_key key;
2233 u64 physical;
2234 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002235 u64 logic_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002236 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002237 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002238 struct reada_control *reada1;
2239 struct reada_control *reada2;
2240 struct btrfs_key key_start;
2241 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002242 u64 increment = map->stripe_len;
2243 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002244 u64 extent_logical;
2245 u64 extent_physical;
2246 u64 extent_len;
2247 struct btrfs_device *extent_dev;
2248 int extent_mirror_num;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002249 int stop_loop;
Arne Jansena2de7332011-03-08 14:14:00 +01002250
David Woodhouse53b381b2013-01-29 18:40:14 -05002251 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
2252 BTRFS_BLOCK_GROUP_RAID6)) {
2253 if (num >= nr_data_stripes(map)) {
2254 return 0;
2255 }
2256 }
2257
Arne Jansena2de7332011-03-08 14:14:00 +01002258 nstripes = length;
2259 offset = 0;
2260 do_div(nstripes, map->stripe_len);
2261 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2262 offset = map->stripe_len * num;
2263 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02002264 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002265 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2266 int factor = map->num_stripes / map->sub_stripes;
2267 offset = map->stripe_len * (num / map->sub_stripes);
2268 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02002269 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002270 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2271 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002272 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002273 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2274 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002275 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002276 } else {
2277 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002278 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002279 }
2280
2281 path = btrfs_alloc_path();
2282 if (!path)
2283 return -ENOMEM;
2284
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002285 /*
2286 * work on commit root. The related disk blocks are static as
2287 * long as COW is applied. This means, it is save to rewrite
2288 * them to repair disk errors without any race conditions
2289 */
Arne Jansena2de7332011-03-08 14:14:00 +01002290 path->search_commit_root = 1;
2291 path->skip_locking = 1;
2292
2293 /*
Arne Jansen7a262852011-06-10 12:39:23 +02002294 * trigger the readahead for extent tree csum tree and wait for
2295 * completion. During readahead, the scrub is officially paused
2296 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01002297 */
2298 logical = base + offset;
Arne Jansena2de7332011-03-08 14:14:00 +01002299
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002300 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002301 atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansen7a262852011-06-10 12:39:23 +02002302 atomic_inc(&fs_info->scrubs_paused);
2303 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002304
Arne Jansen7a262852011-06-10 12:39:23 +02002305 /* FIXME it might be better to start readahead at commit root */
2306 key_start.objectid = logical;
2307 key_start.type = BTRFS_EXTENT_ITEM_KEY;
2308 key_start.offset = (u64)0;
2309 key_end.objectid = base + offset + nstripes * increment;
Josef Bacik3173a182013-03-07 14:22:04 -05002310 key_end.type = BTRFS_METADATA_ITEM_KEY;
2311 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02002312 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01002313
Arne Jansen7a262852011-06-10 12:39:23 +02002314 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2315 key_start.type = BTRFS_EXTENT_CSUM_KEY;
2316 key_start.offset = logical;
2317 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2318 key_end.type = BTRFS_EXTENT_CSUM_KEY;
2319 key_end.offset = base + offset + nstripes * increment;
2320 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01002321
Arne Jansen7a262852011-06-10 12:39:23 +02002322 if (!IS_ERR(reada1))
2323 btrfs_reada_wait(reada1);
2324 if (!IS_ERR(reada2))
2325 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01002326
Arne Jansen7a262852011-06-10 12:39:23 +02002327 mutex_lock(&fs_info->scrub_lock);
2328 while (atomic_read(&fs_info->scrub_pause_req)) {
2329 mutex_unlock(&fs_info->scrub_lock);
2330 wait_event(fs_info->scrub_pause_wait,
2331 atomic_read(&fs_info->scrub_pause_req) == 0);
2332 mutex_lock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002333 }
Arne Jansen7a262852011-06-10 12:39:23 +02002334 atomic_dec(&fs_info->scrubs_paused);
2335 mutex_unlock(&fs_info->scrub_lock);
2336 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002337
2338 /*
2339 * collect all data csums for the stripe to avoid seeking during
2340 * the scrub. This might currently (crc32) end up to be about 1MB
2341 */
Arne Jansene7786c32011-05-28 20:58:38 +00002342 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002343
Arne Jansena2de7332011-03-08 14:14:00 +01002344 /*
2345 * now find all extents for each stripe and scrub them
2346 */
Arne Jansen7a262852011-06-10 12:39:23 +02002347 logical = base + offset;
2348 physical = map->stripes[num].physical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002349 logic_end = logical + increment * nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002350 ret = 0;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002351 while (logical < logic_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01002352 /*
2353 * canceled?
2354 */
2355 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002356 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002357 ret = -ECANCELED;
2358 goto out;
2359 }
2360 /*
2361 * check to see if we have to pause
2362 */
2363 if (atomic_read(&fs_info->scrub_pause_req)) {
2364 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002365 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002366 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002367 mutex_lock(&sctx->wr_ctx.wr_lock);
2368 scrub_wr_submit(sctx);
2369 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002370 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002371 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002372 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002373 atomic_inc(&fs_info->scrubs_paused);
2374 wake_up(&fs_info->scrub_pause_wait);
2375 mutex_lock(&fs_info->scrub_lock);
2376 while (atomic_read(&fs_info->scrub_pause_req)) {
2377 mutex_unlock(&fs_info->scrub_lock);
2378 wait_event(fs_info->scrub_pause_wait,
2379 atomic_read(&fs_info->scrub_pause_req) == 0);
2380 mutex_lock(&fs_info->scrub_lock);
2381 }
2382 atomic_dec(&fs_info->scrubs_paused);
2383 mutex_unlock(&fs_info->scrub_lock);
2384 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002385 }
2386
2387 key.objectid = logical;
2388 key.type = BTRFS_EXTENT_ITEM_KEY;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002389 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01002390
2391 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2392 if (ret < 0)
2393 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002394
Arne Jansen8c510322011-06-03 10:09:26 +02002395 if (ret > 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01002396 ret = btrfs_previous_item(root, path, 0,
2397 BTRFS_EXTENT_ITEM_KEY);
2398 if (ret < 0)
2399 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02002400 if (ret > 0) {
2401 /* there's no smaller item, so stick with the
2402 * larger one */
2403 btrfs_release_path(path);
2404 ret = btrfs_search_slot(NULL, root, &key,
2405 path, 0, 0);
2406 if (ret < 0)
2407 goto out;
2408 }
Arne Jansena2de7332011-03-08 14:14:00 +01002409 }
2410
Liu Bo625f1c8d2013-04-27 02:56:57 +00002411 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002412 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05002413 u64 bytes;
2414
Arne Jansena2de7332011-03-08 14:14:00 +01002415 l = path->nodes[0];
2416 slot = path->slots[0];
2417 if (slot >= btrfs_header_nritems(l)) {
2418 ret = btrfs_next_leaf(root, path);
2419 if (ret == 0)
2420 continue;
2421 if (ret < 0)
2422 goto out;
2423
Liu Bo625f1c8d2013-04-27 02:56:57 +00002424 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002425 break;
2426 }
2427 btrfs_item_key_to_cpu(l, &key, slot);
2428
Josef Bacik3173a182013-03-07 14:22:04 -05002429 if (key.type == BTRFS_METADATA_ITEM_KEY)
2430 bytes = root->leafsize;
2431 else
2432 bytes = key.offset;
2433
2434 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01002435 goto next;
2436
Liu Bo625f1c8d2013-04-27 02:56:57 +00002437 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2438 key.type != BTRFS_METADATA_ITEM_KEY)
2439 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01002440
Liu Bo625f1c8d2013-04-27 02:56:57 +00002441 if (key.objectid >= logical + map->stripe_len) {
2442 /* out of this device extent */
2443 if (key.objectid >= logic_end)
2444 stop_loop = 1;
2445 break;
2446 }
Arne Jansena2de7332011-03-08 14:14:00 +01002447
2448 extent = btrfs_item_ptr(l, slot,
2449 struct btrfs_extent_item);
2450 flags = btrfs_extent_flags(l, extent);
2451 generation = btrfs_extent_generation(l, extent);
2452
2453 if (key.objectid < logical &&
2454 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2455 printk(KERN_ERR
2456 "btrfs scrub: tree block %llu spanning "
2457 "stripes, ignored. logical=%llu\n",
2458 (unsigned long long)key.objectid,
2459 (unsigned long long)logical);
2460 goto next;
2461 }
2462
Liu Bo625f1c8d2013-04-27 02:56:57 +00002463again:
2464 extent_logical = key.objectid;
2465 extent_len = bytes;
2466
Arne Jansena2de7332011-03-08 14:14:00 +01002467 /*
2468 * trim extent to this stripe
2469 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00002470 if (extent_logical < logical) {
2471 extent_len -= logical - extent_logical;
2472 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01002473 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00002474 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01002475 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00002476 extent_len = logical + map->stripe_len -
2477 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01002478 }
2479
Liu Bo625f1c8d2013-04-27 02:56:57 +00002480 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002481 extent_dev = scrub_dev;
2482 extent_mirror_num = mirror_num;
2483 if (is_dev_replace)
2484 scrub_remap_extent(fs_info, extent_logical,
2485 extent_len, &extent_physical,
2486 &extent_dev,
2487 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002488
2489 ret = btrfs_lookup_csums_range(csum_root, logical,
2490 logical + map->stripe_len - 1,
2491 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002492 if (ret)
2493 goto out;
2494
Liu Bo625f1c8d2013-04-27 02:56:57 +00002495 ret = scrub_extent(sctx, extent_logical, extent_len,
2496 extent_physical, extent_dev, flags,
2497 generation, extent_mirror_num,
2498 extent_physical);
2499 if (ret)
2500 goto out;
2501
Josef Bacikd88d46c2013-06-10 12:59:04 +00002502 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002503 if (extent_logical + extent_len <
2504 key.objectid + bytes) {
2505 logical += increment;
2506 physical += map->stripe_len;
2507
2508 if (logical < key.objectid + bytes) {
2509 cond_resched();
2510 goto again;
2511 }
2512
2513 if (logical >= logic_end) {
2514 stop_loop = 1;
2515 break;
2516 }
2517 }
Arne Jansena2de7332011-03-08 14:14:00 +01002518next:
2519 path->slots[0]++;
2520 }
Chris Mason71267332011-05-23 06:30:52 -04002521 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002522 logical += increment;
2523 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002524 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002525 if (stop_loop)
2526 sctx->stat.last_physical = map->stripes[num].physical +
2527 length;
2528 else
2529 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002530 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002531 if (stop_loop)
2532 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002533 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01002534out:
Arne Jansena2de7332011-03-08 14:14:00 +01002535 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002536 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002537 mutex_lock(&sctx->wr_ctx.wr_lock);
2538 scrub_wr_submit(sctx);
2539 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002540
Arne Jansene7786c32011-05-28 20:58:38 +00002541 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002542 btrfs_free_path(path);
2543 return ret < 0 ? ret : 0;
2544}
2545
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002546static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002547 struct btrfs_device *scrub_dev,
2548 u64 chunk_tree, u64 chunk_objectid,
2549 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002550 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002551{
2552 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002553 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01002554 struct map_lookup *map;
2555 struct extent_map *em;
2556 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002557 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002558
2559 read_lock(&map_tree->map_tree.lock);
2560 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2561 read_unlock(&map_tree->map_tree.lock);
2562
2563 if (!em)
2564 return -EINVAL;
2565
2566 map = (struct map_lookup *)em->bdev;
2567 if (em->start != chunk_offset)
2568 goto out;
2569
2570 if (em->len < length)
2571 goto out;
2572
2573 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002574 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01002575 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002576 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002577 chunk_offset, length,
2578 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002579 if (ret)
2580 goto out;
2581 }
2582 }
2583out:
2584 free_extent_map(em);
2585
2586 return ret;
2587}
2588
2589static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002590int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002591 struct btrfs_device *scrub_dev, u64 start, u64 end,
2592 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002593{
2594 struct btrfs_dev_extent *dev_extent = NULL;
2595 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002596 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002597 struct btrfs_fs_info *fs_info = root->fs_info;
2598 u64 length;
2599 u64 chunk_tree;
2600 u64 chunk_objectid;
2601 u64 chunk_offset;
2602 int ret;
2603 int slot;
2604 struct extent_buffer *l;
2605 struct btrfs_key key;
2606 struct btrfs_key found_key;
2607 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002608 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01002609
2610 path = btrfs_alloc_path();
2611 if (!path)
2612 return -ENOMEM;
2613
2614 path->reada = 2;
2615 path->search_commit_root = 1;
2616 path->skip_locking = 1;
2617
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002618 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01002619 key.offset = 0ull;
2620 key.type = BTRFS_DEV_EXTENT_KEY;
2621
Arne Jansena2de7332011-03-08 14:14:00 +01002622 while (1) {
2623 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2624 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02002625 break;
2626 if (ret > 0) {
2627 if (path->slots[0] >=
2628 btrfs_header_nritems(path->nodes[0])) {
2629 ret = btrfs_next_leaf(root, path);
2630 if (ret)
2631 break;
2632 }
2633 }
Arne Jansena2de7332011-03-08 14:14:00 +01002634
2635 l = path->nodes[0];
2636 slot = path->slots[0];
2637
2638 btrfs_item_key_to_cpu(l, &found_key, slot);
2639
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002640 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01002641 break;
2642
Arne Jansen8c510322011-06-03 10:09:26 +02002643 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01002644 break;
2645
2646 if (found_key.offset >= end)
2647 break;
2648
2649 if (found_key.offset < key.offset)
2650 break;
2651
2652 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2653 length = btrfs_dev_extent_length(l, dev_extent);
2654
2655 if (found_key.offset + length <= start) {
2656 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002657 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002658 continue;
2659 }
2660
2661 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2662 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2663 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2664
2665 /*
2666 * get a reference on the corresponding block group to prevent
2667 * the chunk from going away while we scrub it
2668 */
2669 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2670 if (!cache) {
2671 ret = -ENOENT;
Arne Jansen8c510322011-06-03 10:09:26 +02002672 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002673 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01002674 dev_replace->cursor_right = found_key.offset + length;
2675 dev_replace->cursor_left = found_key.offset;
2676 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002677 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002678 chunk_offset, length, found_key.offset,
2679 is_dev_replace);
2680
2681 /*
2682 * flush, submit all pending read and write bios, afterwards
2683 * wait for them.
2684 * Note that in the dev replace case, a read request causes
2685 * write requests that are submitted in the read completion
2686 * worker. Therefore in the current situation, it is required
2687 * that all write requests are flushed, so that all read and
2688 * write requests are really completed when bios_in_flight
2689 * changes to 0.
2690 */
2691 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
2692 scrub_submit(sctx);
2693 mutex_lock(&sctx->wr_ctx.wr_lock);
2694 scrub_wr_submit(sctx);
2695 mutex_unlock(&sctx->wr_ctx.wr_lock);
2696
2697 wait_event(sctx->list_wait,
2698 atomic_read(&sctx->bios_in_flight) == 0);
2699 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
2700 atomic_inc(&fs_info->scrubs_paused);
2701 wake_up(&fs_info->scrub_pause_wait);
2702 wait_event(sctx->list_wait,
2703 atomic_read(&sctx->workers_pending) == 0);
2704
2705 mutex_lock(&fs_info->scrub_lock);
2706 while (atomic_read(&fs_info->scrub_pause_req)) {
2707 mutex_unlock(&fs_info->scrub_lock);
2708 wait_event(fs_info->scrub_pause_wait,
2709 atomic_read(&fs_info->scrub_pause_req) == 0);
2710 mutex_lock(&fs_info->scrub_lock);
2711 }
2712 atomic_dec(&fs_info->scrubs_paused);
2713 mutex_unlock(&fs_info->scrub_lock);
2714 wake_up(&fs_info->scrub_pause_wait);
2715
2716 dev_replace->cursor_left = dev_replace->cursor_right;
2717 dev_replace->item_needs_writeback = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002718 btrfs_put_block_group(cache);
2719 if (ret)
2720 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00002721 if (is_dev_replace &&
2722 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002723 ret = -EIO;
2724 break;
2725 }
2726 if (sctx->stat.malloc_errors > 0) {
2727 ret = -ENOMEM;
2728 break;
2729 }
Arne Jansena2de7332011-03-08 14:14:00 +01002730
2731 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002732 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002733 }
2734
Arne Jansena2de7332011-03-08 14:14:00 +01002735 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02002736
2737 /*
2738 * ret can still be 1 from search_slot or next_leaf,
2739 * that's not an error
2740 */
2741 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002742}
2743
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002744static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2745 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01002746{
2747 int i;
2748 u64 bytenr;
2749 u64 gen;
2750 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002751 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002752
Miao Xie87533c42013-01-29 10:14:48 +00002753 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002754 return -EIO;
2755
Arne Jansena2de7332011-03-08 14:14:00 +01002756 gen = root->fs_info->last_trans_committed;
2757
2758 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2759 bytenr = btrfs_sb_offset(i);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002760 if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01002761 break;
2762
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002763 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002764 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002765 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01002766 if (ret)
2767 return ret;
2768 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002769 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002770
2771 return 0;
2772}
2773
2774/*
2775 * get a reference count on fs_info->scrub_workers. start worker if necessary
2776 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002777static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
2778 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002779{
Josef Bacik0dc3b842011-11-18 14:37:27 -05002780 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002781
2782 mutex_lock(&fs_info->scrub_lock);
Arne Jansen632dd772011-06-10 12:07:07 +02002783 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002784 if (is_dev_replace)
2785 btrfs_init_workers(&fs_info->scrub_workers, "scrub", 1,
2786 &fs_info->generic_worker);
2787 else
2788 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
2789 fs_info->thread_pool_size,
2790 &fs_info->generic_worker);
Arne Jansen632dd772011-06-10 12:07:07 +02002791 fs_info->scrub_workers.idle_thresh = 4;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002792 ret = btrfs_start_workers(&fs_info->scrub_workers);
2793 if (ret)
2794 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002795 btrfs_init_workers(&fs_info->scrub_wr_completion_workers,
2796 "scrubwrc",
2797 fs_info->thread_pool_size,
2798 &fs_info->generic_worker);
2799 fs_info->scrub_wr_completion_workers.idle_thresh = 2;
2800 ret = btrfs_start_workers(
2801 &fs_info->scrub_wr_completion_workers);
2802 if (ret)
2803 goto out;
2804 btrfs_init_workers(&fs_info->scrub_nocow_workers, "scrubnc", 1,
2805 &fs_info->generic_worker);
2806 ret = btrfs_start_workers(&fs_info->scrub_nocow_workers);
2807 if (ret)
2808 goto out;
Arne Jansen632dd772011-06-10 12:07:07 +02002809 }
Arne Jansena2de7332011-03-08 14:14:00 +01002810 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002811out:
Arne Jansena2de7332011-03-08 14:14:00 +01002812 mutex_unlock(&fs_info->scrub_lock);
2813
Josef Bacik0dc3b842011-11-18 14:37:27 -05002814 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002815}
2816
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002817static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002818{
Arne Jansena2de7332011-03-08 14:14:00 +01002819 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002820 if (--fs_info->scrub_workers_refcnt == 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01002821 btrfs_stop_workers(&fs_info->scrub_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002822 btrfs_stop_workers(&fs_info->scrub_wr_completion_workers);
2823 btrfs_stop_workers(&fs_info->scrub_nocow_workers);
2824 }
Arne Jansena2de7332011-03-08 14:14:00 +01002825 WARN_ON(fs_info->scrub_workers_refcnt < 0);
2826 mutex_unlock(&fs_info->scrub_lock);
2827}
2828
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002829int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
2830 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01002831 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002832{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002833 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002834 int ret;
2835 struct btrfs_device *dev;
2836
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002837 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01002838 return -EINVAL;
2839
2840 /*
2841 * check some assumptions
2842 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002843 if (fs_info->chunk_root->nodesize != fs_info->chunk_root->leafsize) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002844 printk(KERN_ERR
2845 "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002846 fs_info->chunk_root->nodesize,
2847 fs_info->chunk_root->leafsize);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002848 return -EINVAL;
2849 }
2850
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002851 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002852 /*
2853 * in this case scrub is unable to calculate the checksum
2854 * the way scrub is implemented. Do not handle this
2855 * situation at all because it won't ever happen.
2856 */
2857 printk(KERN_ERR
2858 "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002859 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002860 return -EINVAL;
2861 }
2862
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002863 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002864 /* not supported for data w/o checksums */
2865 printk(KERN_ERR
2866 "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lld) fails\n",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002867 fs_info->chunk_root->sectorsize,
2868 (unsigned long long)PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01002869 return -EINVAL;
2870 }
2871
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002872 if (fs_info->chunk_root->nodesize >
2873 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
2874 fs_info->chunk_root->sectorsize >
2875 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
2876 /*
2877 * would exhaust the array bounds of pagev member in
2878 * struct scrub_block
2879 */
2880 pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
2881 fs_info->chunk_root->nodesize,
2882 SCRUB_MAX_PAGES_PER_BLOCK,
2883 fs_info->chunk_root->sectorsize,
2884 SCRUB_MAX_PAGES_PER_BLOCK);
2885 return -EINVAL;
2886 }
2887
Stefan Behrensff023aa2012-11-06 11:43:11 +01002888 ret = scrub_workers_get(fs_info, is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002889 if (ret)
2890 return ret;
2891
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002892 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2893 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002894 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002895 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2896 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002897 return -ENODEV;
2898 }
2899 mutex_lock(&fs_info->scrub_lock);
2900
Stefan Behrens63a212a2012-11-05 18:29:28 +01002901 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01002902 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002903 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2904 scrub_workers_put(fs_info);
2905 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01002906 }
2907
Stefan Behrens8dabb742012-11-06 13:15:27 +01002908 btrfs_dev_replace_lock(&fs_info->dev_replace);
2909 if (dev->scrub_device ||
2910 (!is_dev_replace &&
2911 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
2912 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002913 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002914 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2915 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002916 return -EINPROGRESS;
2917 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01002918 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002919 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002920 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002921 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002922 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2923 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002924 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002925 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002926 sctx->readonly = readonly;
2927 dev->scrub_device = sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002928
2929 atomic_inc(&fs_info->scrubs_running);
2930 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002931 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002932
Stefan Behrensff023aa2012-11-06 11:43:11 +01002933 if (!is_dev_replace) {
2934 down_read(&fs_info->scrub_super_lock);
2935 ret = scrub_supers(sctx, dev);
2936 up_read(&fs_info->scrub_super_lock);
2937 }
Arne Jansena2de7332011-03-08 14:14:00 +01002938
2939 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002940 ret = scrub_enumerate_chunks(sctx, dev, start, end,
2941 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002942
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002943 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002944 atomic_dec(&fs_info->scrubs_running);
2945 wake_up(&fs_info->scrub_pause_wait);
2946
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002947 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02002948
Arne Jansena2de7332011-03-08 14:14:00 +01002949 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002950 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002951
2952 mutex_lock(&fs_info->scrub_lock);
2953 dev->scrub_device = NULL;
2954 mutex_unlock(&fs_info->scrub_lock);
2955
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002956 scrub_free_ctx(sctx);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002957 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002958
2959 return ret;
2960}
2961
Jeff Mahoney143bede2012-03-01 14:56:26 +01002962void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002963{
2964 struct btrfs_fs_info *fs_info = root->fs_info;
2965
2966 mutex_lock(&fs_info->scrub_lock);
2967 atomic_inc(&fs_info->scrub_pause_req);
2968 while (atomic_read(&fs_info->scrubs_paused) !=
2969 atomic_read(&fs_info->scrubs_running)) {
2970 mutex_unlock(&fs_info->scrub_lock);
2971 wait_event(fs_info->scrub_pause_wait,
2972 atomic_read(&fs_info->scrubs_paused) ==
2973 atomic_read(&fs_info->scrubs_running));
2974 mutex_lock(&fs_info->scrub_lock);
2975 }
2976 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002977}
2978
Jeff Mahoney143bede2012-03-01 14:56:26 +01002979void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002980{
2981 struct btrfs_fs_info *fs_info = root->fs_info;
2982
2983 atomic_dec(&fs_info->scrub_pause_req);
2984 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002985}
2986
Jeff Mahoney143bede2012-03-01 14:56:26 +01002987void btrfs_scrub_pause_super(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002988{
2989 down_write(&root->fs_info->scrub_super_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002990}
2991
Jeff Mahoney143bede2012-03-01 14:56:26 +01002992void btrfs_scrub_continue_super(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002993{
2994 up_write(&root->fs_info->scrub_super_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002995}
2996
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002997int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002998{
Arne Jansena2de7332011-03-08 14:14:00 +01002999 mutex_lock(&fs_info->scrub_lock);
3000 if (!atomic_read(&fs_info->scrubs_running)) {
3001 mutex_unlock(&fs_info->scrub_lock);
3002 return -ENOTCONN;
3003 }
3004
3005 atomic_inc(&fs_info->scrub_cancel_req);
3006 while (atomic_read(&fs_info->scrubs_running)) {
3007 mutex_unlock(&fs_info->scrub_lock);
3008 wait_event(fs_info->scrub_pause_wait,
3009 atomic_read(&fs_info->scrubs_running) == 0);
3010 mutex_lock(&fs_info->scrub_lock);
3011 }
3012 atomic_dec(&fs_info->scrub_cancel_req);
3013 mutex_unlock(&fs_info->scrub_lock);
3014
3015 return 0;
3016}
3017
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003018int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3019 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003020{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003021 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003022
3023 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003024 sctx = dev->scrub_device;
3025 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003026 mutex_unlock(&fs_info->scrub_lock);
3027 return -ENOTCONN;
3028 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003029 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003030 while (dev->scrub_device) {
3031 mutex_unlock(&fs_info->scrub_lock);
3032 wait_event(fs_info->scrub_pause_wait,
3033 dev->scrub_device == NULL);
3034 mutex_lock(&fs_info->scrub_lock);
3035 }
3036 mutex_unlock(&fs_info->scrub_lock);
3037
3038 return 0;
3039}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003040
Arne Jansena2de7332011-03-08 14:14:00 +01003041int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3042 struct btrfs_scrub_progress *progress)
3043{
3044 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003045 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003046
3047 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003048 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003049 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003050 sctx = dev->scrub_device;
3051 if (sctx)
3052 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003053 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3054
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003055 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003056}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003057
3058static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3059 u64 extent_logical, u64 extent_len,
3060 u64 *extent_physical,
3061 struct btrfs_device **extent_dev,
3062 int *extent_mirror_num)
3063{
3064 u64 mapped_length;
3065 struct btrfs_bio *bbio = NULL;
3066 int ret;
3067
3068 mapped_length = extent_len;
3069 ret = btrfs_map_block(fs_info, READ, extent_logical,
3070 &mapped_length, &bbio, 0);
3071 if (ret || !bbio || mapped_length < extent_len ||
3072 !bbio->stripes[0].dev->bdev) {
3073 kfree(bbio);
3074 return;
3075 }
3076
3077 *extent_physical = bbio->stripes[0].physical;
3078 *extent_mirror_num = bbio->mirror_num;
3079 *extent_dev = bbio->stripes[0].dev;
3080 kfree(bbio);
3081}
3082
3083static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3084 struct scrub_wr_ctx *wr_ctx,
3085 struct btrfs_fs_info *fs_info,
3086 struct btrfs_device *dev,
3087 int is_dev_replace)
3088{
3089 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3090
3091 mutex_init(&wr_ctx->wr_lock);
3092 wr_ctx->wr_curr_bio = NULL;
3093 if (!is_dev_replace)
3094 return 0;
3095
3096 WARN_ON(!dev->bdev);
3097 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3098 bio_get_nr_vecs(dev->bdev));
3099 wr_ctx->tgtdev = dev;
3100 atomic_set(&wr_ctx->flush_all_writes, 0);
3101 return 0;
3102}
3103
3104static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3105{
3106 mutex_lock(&wr_ctx->wr_lock);
3107 kfree(wr_ctx->wr_curr_bio);
3108 wr_ctx->wr_curr_bio = NULL;
3109 mutex_unlock(&wr_ctx->wr_lock);
3110}
3111
3112static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3113 int mirror_num, u64 physical_for_dev_replace)
3114{
3115 struct scrub_copy_nocow_ctx *nocow_ctx;
3116 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3117
3118 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3119 if (!nocow_ctx) {
3120 spin_lock(&sctx->stat_lock);
3121 sctx->stat.malloc_errors++;
3122 spin_unlock(&sctx->stat_lock);
3123 return -ENOMEM;
3124 }
3125
3126 scrub_pending_trans_workers_inc(sctx);
3127
3128 nocow_ctx->sctx = sctx;
3129 nocow_ctx->logical = logical;
3130 nocow_ctx->len = len;
3131 nocow_ctx->mirror_num = mirror_num;
3132 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
3133 nocow_ctx->work.func = copy_nocow_pages_worker;
3134 btrfs_queue_worker(&fs_info->scrub_nocow_workers,
3135 &nocow_ctx->work);
3136
3137 return 0;
3138}
3139
3140static void copy_nocow_pages_worker(struct btrfs_work *work)
3141{
3142 struct scrub_copy_nocow_ctx *nocow_ctx =
3143 container_of(work, struct scrub_copy_nocow_ctx, work);
3144 struct scrub_ctx *sctx = nocow_ctx->sctx;
3145 u64 logical = nocow_ctx->logical;
3146 u64 len = nocow_ctx->len;
3147 int mirror_num = nocow_ctx->mirror_num;
3148 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3149 int ret;
3150 struct btrfs_trans_handle *trans = NULL;
3151 struct btrfs_fs_info *fs_info;
3152 struct btrfs_path *path;
3153 struct btrfs_root *root;
3154 int not_written = 0;
3155
3156 fs_info = sctx->dev_root->fs_info;
3157 root = fs_info->extent_root;
3158
3159 path = btrfs_alloc_path();
3160 if (!path) {
3161 spin_lock(&sctx->stat_lock);
3162 sctx->stat.malloc_errors++;
3163 spin_unlock(&sctx->stat_lock);
3164 not_written = 1;
3165 goto out;
3166 }
3167
3168 trans = btrfs_join_transaction(root);
3169 if (IS_ERR(trans)) {
3170 not_written = 1;
3171 goto out;
3172 }
3173
3174 ret = iterate_inodes_from_logical(logical, fs_info, path,
3175 copy_nocow_pages_for_inode,
3176 nocow_ctx);
3177 if (ret != 0 && ret != -ENOENT) {
3178 pr_warn("iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %llu, ret %d\n",
3179 (unsigned long long)logical,
3180 (unsigned long long)physical_for_dev_replace,
3181 (unsigned long long)len,
3182 (unsigned long long)mirror_num, ret);
3183 not_written = 1;
3184 goto out;
3185 }
3186
3187out:
3188 if (trans && !IS_ERR(trans))
3189 btrfs_end_transaction(trans, root);
3190 if (not_written)
3191 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
3192 num_uncorrectable_read_errors);
3193
3194 btrfs_free_path(path);
3195 kfree(nocow_ctx);
3196
3197 scrub_pending_trans_workers_dec(sctx);
3198}
3199
3200static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root, void *ctx)
3201{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003202 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
Miao Xie826aa0a2013-06-27 18:50:59 +08003203 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003204 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08003205 struct inode *inode;
3206 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003207 struct btrfs_root *local_root;
3208 u64 physical_for_dev_replace;
3209 u64 len;
Miao Xie826aa0a2013-06-27 18:50:59 +08003210 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00003211 int srcu_index;
Miao Xie826aa0a2013-06-27 18:50:59 +08003212 int ret;
3213 int err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003214
3215 key.objectid = root;
3216 key.type = BTRFS_ROOT_ITEM_KEY;
3217 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00003218
3219 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
3220
Stefan Behrensff023aa2012-11-06 11:43:11 +01003221 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00003222 if (IS_ERR(local_root)) {
3223 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003224 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00003225 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003226
Miao Xieedd14002013-06-27 18:51:00 +08003227 if (btrfs_root_refs(&local_root->root_item) == 0) {
3228 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
3229 return -ENOENT;
3230 }
3231
Stefan Behrensff023aa2012-11-06 11:43:11 +01003232 key.type = BTRFS_INODE_ITEM_KEY;
3233 key.objectid = inum;
3234 key.offset = 0;
3235 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00003236 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003237 if (IS_ERR(inode))
3238 return PTR_ERR(inode);
3239
Miao Xieedd14002013-06-27 18:51:00 +08003240 /* Avoid truncate/dio/punch hole.. */
3241 mutex_lock(&inode->i_mutex);
3242 inode_dio_wait(inode);
3243
Miao Xie826aa0a2013-06-27 18:50:59 +08003244 ret = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003245 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3246 len = nocow_ctx->len;
3247 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003248 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08003249again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01003250 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3251 if (!page) {
3252 pr_err("find_or_create_page() failed\n");
3253 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08003254 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003255 }
3256
3257 if (PageUptodate(page)) {
3258 if (PageDirty(page))
3259 goto next_page;
3260 } else {
3261 ClearPageError(page);
Miao Xie826aa0a2013-06-27 18:50:59 +08003262 err = extent_read_full_page(&BTRFS_I(inode)->
Stefan Behrensff023aa2012-11-06 11:43:11 +01003263 io_tree,
3264 page, btrfs_get_extent,
3265 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08003266 if (err) {
3267 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003268 goto next_page;
3269 }
Miao Xieedd14002013-06-27 18:51:00 +08003270
Miao Xie26b258912013-06-27 18:50:58 +08003271 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08003272 /*
3273 * If the page has been remove from the page cache,
3274 * the data on it is meaningless, because it may be
3275 * old one, the new data may be written into the new
3276 * page in the page cache.
3277 */
3278 if (page->mapping != inode->i_mapping) {
3279 page_cache_release(page);
3280 goto again;
3281 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003282 if (!PageUptodate(page)) {
3283 ret = -EIO;
3284 goto next_page;
3285 }
3286 }
Miao Xie826aa0a2013-06-27 18:50:59 +08003287 err = write_page_nocow(nocow_ctx->sctx,
3288 physical_for_dev_replace, page);
3289 if (err)
3290 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003291next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08003292 unlock_page(page);
3293 page_cache_release(page);
3294
3295 if (ret)
3296 break;
3297
Stefan Behrensff023aa2012-11-06 11:43:11 +01003298 offset += PAGE_CACHE_SIZE;
3299 physical_for_dev_replace += PAGE_CACHE_SIZE;
3300 len -= PAGE_CACHE_SIZE;
3301 }
Miao Xie826aa0a2013-06-27 18:50:59 +08003302out:
Miao Xieedd14002013-06-27 18:51:00 +08003303 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08003304 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003305 return ret;
3306}
3307
3308static int write_page_nocow(struct scrub_ctx *sctx,
3309 u64 physical_for_dev_replace, struct page *page)
3310{
3311 struct bio *bio;
3312 struct btrfs_device *dev;
3313 int ret;
3314 DECLARE_COMPLETION_ONSTACK(compl);
3315
3316 dev = sctx->wr_ctx.tgtdev;
3317 if (!dev)
3318 return -EIO;
3319 if (!dev->bdev) {
3320 printk_ratelimited(KERN_WARNING
3321 "btrfs: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
3322 return -EIO;
3323 }
Chris Mason9be33952013-05-17 18:30:14 -04003324 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003325 if (!bio) {
3326 spin_lock(&sctx->stat_lock);
3327 sctx->stat.malloc_errors++;
3328 spin_unlock(&sctx->stat_lock);
3329 return -ENOMEM;
3330 }
3331 bio->bi_private = &compl;
3332 bio->bi_end_io = scrub_complete_bio_end_io;
3333 bio->bi_size = 0;
3334 bio->bi_sector = physical_for_dev_replace >> 9;
3335 bio->bi_bdev = dev->bdev;
3336 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
3337 if (ret != PAGE_CACHE_SIZE) {
3338leave_with_eio:
3339 bio_put(bio);
3340 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
3341 return -EIO;
3342 }
3343 btrfsic_submit_bio(WRITE_SYNC, bio);
3344 wait_for_completion(&compl);
3345
3346 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
3347 goto leave_with_eio;
3348
3349 bio_put(bio);
3350 return 0;
3351}