blob: 6acb573e7d6bf7a1e49005b047cd89a4259af389 [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
Josef Bacik652f25a2013-09-12 16:58:28 -0400161struct scrub_nocow_inode {
162 u64 inum;
163 u64 offset;
164 u64 root;
165 struct list_head list;
166};
167
Stefan Behrensff023aa2012-11-06 11:43:11 +0100168struct scrub_copy_nocow_ctx {
169 struct scrub_ctx *sctx;
170 u64 logical;
171 u64 len;
172 int mirror_num;
173 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -0400174 struct list_head inodes;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100175 struct btrfs_work work;
176};
177
Jan Schmidt558540c2011-06-13 19:59:12 +0200178struct scrub_warning {
179 struct btrfs_path *path;
180 u64 extent_item_size;
181 char *scratch_buf;
182 char *msg_buf;
183 const char *errstr;
184 sector_t sector;
185 u64 logical;
186 struct btrfs_device *dev;
187 int msg_bufsize;
188 int scratch_bufsize;
189};
190
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400191
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100192static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
193static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
194static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
195static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400196static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100197static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100198 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100199 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400200 u64 length, u64 logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100201 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100202static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
203 struct scrub_block *sblock, int is_metadata,
204 int have_csum, u8 *csum, u64 generation,
205 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400206static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
207 struct scrub_block *sblock,
208 int is_metadata, int have_csum,
209 const u8 *csum, u64 generation,
210 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400211static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
212 struct scrub_block *sblock_good,
213 int force_write);
214static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
215 struct scrub_block *sblock_good,
216 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100217static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
218static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
219 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400220static int scrub_checksum_data(struct scrub_block *sblock);
221static int scrub_checksum_tree_block(struct scrub_block *sblock);
222static int scrub_checksum_super(struct scrub_block *sblock);
223static void scrub_block_get(struct scrub_block *sblock);
224static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100225static void scrub_page_get(struct scrub_page *spage);
226static void scrub_page_put(struct scrub_page *spage);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100227static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
228 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100229static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100230 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100231 u64 gen, int mirror_num, u8 *csum, int force,
232 u64 physical_for_dev_replace);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400233static void scrub_bio_end_io(struct bio *bio, int err);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400234static void scrub_bio_end_io_worker(struct btrfs_work *work);
235static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100236static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
237 u64 extent_logical, u64 extent_len,
238 u64 *extent_physical,
239 struct btrfs_device **extent_dev,
240 int *extent_mirror_num);
241static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
242 struct scrub_wr_ctx *wr_ctx,
243 struct btrfs_fs_info *fs_info,
244 struct btrfs_device *dev,
245 int is_dev_replace);
246static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
247static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
248 struct scrub_page *spage);
249static void scrub_wr_submit(struct scrub_ctx *sctx);
250static void scrub_wr_bio_end_io(struct bio *bio, int err);
251static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
252static int write_page_nocow(struct scrub_ctx *sctx,
253 u64 physical_for_dev_replace, struct page *page);
254static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400255 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100256static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
257 int mirror_num, u64 physical_for_dev_replace);
258static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilong3cb09292013-12-04 21:15:19 +0800259static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400260
261
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100262static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
263{
264 atomic_inc(&sctx->bios_in_flight);
265}
266
267static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
268{
269 atomic_dec(&sctx->bios_in_flight);
270 wake_up(&sctx->list_wait);
271}
272
Wang Shilong3cb09292013-12-04 21:15:19 +0800273static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
274{
275 while (atomic_read(&fs_info->scrub_pause_req)) {
276 mutex_unlock(&fs_info->scrub_lock);
277 wait_event(fs_info->scrub_pause_wait,
278 atomic_read(&fs_info->scrub_pause_req) == 0);
279 mutex_lock(&fs_info->scrub_lock);
280 }
281}
282
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100283/*
284 * used for workers that require transaction commits (i.e., for the
285 * NOCOW case)
286 */
287static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
288{
289 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
290
291 /*
292 * increment scrubs_running to prevent cancel requests from
293 * completing as long as a worker is running. we must also
294 * increment scrubs_paused to prevent deadlocking on pause
295 * requests used for transactions commits (as the worker uses a
296 * transaction context). it is safe to regard the worker
297 * as paused for all matters practical. effectively, we only
298 * avoid cancellation requests from completing.
299 */
300 mutex_lock(&fs_info->scrub_lock);
301 atomic_inc(&fs_info->scrubs_running);
302 atomic_inc(&fs_info->scrubs_paused);
303 mutex_unlock(&fs_info->scrub_lock);
304 atomic_inc(&sctx->workers_pending);
305}
306
307/* used for workers that require transaction commits */
308static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
309{
310 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
311
312 /*
313 * see scrub_pending_trans_workers_inc() why we're pretending
314 * to be paused in the scrub counters
315 */
316 mutex_lock(&fs_info->scrub_lock);
317 atomic_dec(&fs_info->scrubs_running);
318 atomic_dec(&fs_info->scrubs_paused);
319 mutex_unlock(&fs_info->scrub_lock);
320 atomic_dec(&sctx->workers_pending);
321 wake_up(&fs_info->scrub_pause_wait);
322 wake_up(&sctx->list_wait);
323}
324
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100325static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100326{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100327 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100328 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100329 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100330 struct btrfs_ordered_sum, list);
331 list_del(&sum->list);
332 kfree(sum);
333 }
334}
335
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100336static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100337{
338 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100339
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100340 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100341 return;
342
Stefan Behrensff023aa2012-11-06 11:43:11 +0100343 scrub_free_wr_ctx(&sctx->wr_ctx);
344
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400345 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100346 if (sctx->curr != -1) {
347 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400348
349 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100350 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400351 scrub_block_put(sbio->pagev[i]->sblock);
352 }
353 bio_put(sbio->bio);
354 }
355
Stefan Behrensff023aa2012-11-06 11:43:11 +0100356 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100357 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100358
359 if (!sbio)
360 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100361 kfree(sbio);
362 }
363
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100364 scrub_free_csums(sctx);
365 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100366}
367
368static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100369struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100370{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100371 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100372 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100373 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100374 int pages_per_rd_bio;
375 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100376
Stefan Behrensff023aa2012-11-06 11:43:11 +0100377 /*
378 * the setting of pages_per_rd_bio is correct for scrub but might
379 * be wrong for the dev_replace code where we might read from
380 * different devices in the initial huge bios. However, that
381 * code is able to correctly handle the case when adding a page
382 * to a bio fails.
383 */
384 if (dev->bdev)
385 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
386 bio_get_nr_vecs(dev->bdev));
387 else
388 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100389 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
390 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100391 goto nomem;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100392 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100393 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100394 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100395 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100396 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100397 struct scrub_bio *sbio;
398
399 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
400 if (!sbio)
401 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100402 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100403
Arne Jansena2de7332011-03-08 14:14:00 +0100404 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100405 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400406 sbio->page_count = 0;
407 sbio->work.func = scrub_bio_end_io_worker;
Arne Jansena2de7332011-03-08 14:14:00 +0100408
Stefan Behrensff023aa2012-11-06 11:43:11 +0100409 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100410 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200411 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100412 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100413 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100414 sctx->first_free = 0;
415 sctx->nodesize = dev->dev_root->nodesize;
416 sctx->leafsize = dev->dev_root->leafsize;
417 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100418 atomic_set(&sctx->bios_in_flight, 0);
419 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100420 atomic_set(&sctx->cancel_req, 0);
421 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
422 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100423
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100424 spin_lock_init(&sctx->list_lock);
425 spin_lock_init(&sctx->stat_lock);
426 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100427
428 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
429 fs_info->dev_replace.tgtdev, is_dev_replace);
430 if (ret) {
431 scrub_free_ctx(sctx);
432 return ERR_PTR(ret);
433 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100434 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100435
436nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100437 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100438 return ERR_PTR(-ENOMEM);
439}
440
Stefan Behrensff023aa2012-11-06 11:43:11 +0100441static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
442 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200443{
444 u64 isize;
445 u32 nlink;
446 int ret;
447 int i;
448 struct extent_buffer *eb;
449 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100450 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200451 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
452 struct inode_fs_paths *ipath = NULL;
453 struct btrfs_root *local_root;
454 struct btrfs_key root_key;
455
456 root_key.objectid = root;
457 root_key.type = BTRFS_ROOT_ITEM_KEY;
458 root_key.offset = (u64)-1;
459 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
460 if (IS_ERR(local_root)) {
461 ret = PTR_ERR(local_root);
462 goto err;
463 }
464
465 ret = inode_item_info(inum, 0, local_root, swarn->path);
466 if (ret) {
467 btrfs_release_path(swarn->path);
468 goto err;
469 }
470
471 eb = swarn->path->nodes[0];
472 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
473 struct btrfs_inode_item);
474 isize = btrfs_inode_size(eb, inode_item);
475 nlink = btrfs_inode_nlink(eb, inode_item);
476 btrfs_release_path(swarn->path);
477
478 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300479 if (IS_ERR(ipath)) {
480 ret = PTR_ERR(ipath);
481 ipath = NULL;
482 goto err;
483 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200484 ret = paths_from_inode(inum, ipath);
485
486 if (ret < 0)
487 goto err;
488
489 /*
490 * we deliberately ignore the bit ipath might have been too small to
491 * hold all of the paths here
492 */
493 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Josef Bacik606686e2012-06-04 14:03:51 -0400494 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200495 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
496 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400497 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200498 (unsigned long long)swarn->sector, root, inum, offset,
499 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500500 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200501
502 free_ipath(ipath);
503 return 0;
504
505err:
Josef Bacik606686e2012-06-04 14:03:51 -0400506 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200507 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
508 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400509 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200510 (unsigned long long)swarn->sector, root, inum, offset, ret);
511
512 free_ipath(ipath);
513 return 0;
514}
515
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400516static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200517{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100518 struct btrfs_device *dev;
519 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200520 struct btrfs_path *path;
521 struct btrfs_key found_key;
522 struct extent_buffer *eb;
523 struct btrfs_extent_item *ei;
524 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200525 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100526 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600527 u64 flags = 0;
528 u64 ref_root;
529 u32 item_size;
530 u8 ref_level;
531 const int bufsize = 4096;
532 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200533
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100534 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100535 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100536 fs_info = sblock->sctx->dev_root->fs_info;
537
Jan Schmidt558540c2011-06-13 19:59:12 +0200538 path = btrfs_alloc_path();
539
540 swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
541 swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100542 swarn.sector = (sblock->pagev[0]->physical) >> 9;
543 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200544 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100545 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200546 swarn.msg_bufsize = bufsize;
547 swarn.scratch_bufsize = bufsize;
548
549 if (!path || !swarn.scratch_buf || !swarn.msg_buf)
550 goto out;
551
Liu Bo69917e42012-09-07 20:01:28 -0600552 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
553 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200554 if (ret < 0)
555 goto out;
556
Jan Schmidt4692cf52011-12-02 14:56:41 +0100557 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200558 swarn.extent_item_size = found_key.offset;
559
560 eb = path->nodes[0];
561 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
562 item_size = btrfs_item_size_nr(eb, path->slots[0]);
563
Liu Bo69917e42012-09-07 20:01:28 -0600564 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200565 do {
566 ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
567 &ref_root, &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400568 printk_in_rcu(KERN_WARNING
Stefan Behrens1623ede2012-03-27 14:21:26 -0400569 "btrfs: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200570 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400571 "%llu\n", errstr, swarn.logical,
572 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200573 (unsigned long long)swarn.sector,
574 ref_level ? "node" : "leaf",
575 ret < 0 ? -1 : ref_level,
576 ret < 0 ? -1 : ref_root);
577 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600578 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200579 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600580 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200581 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100582 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100583 iterate_extent_inodes(fs_info, found_key.objectid,
584 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200585 scrub_print_warning_inode, &swarn);
586 }
587
588out:
589 btrfs_free_path(path);
590 kfree(swarn.scratch_buf);
591 kfree(swarn.msg_buf);
592}
593
Stefan Behrensff023aa2012-11-06 11:43:11 +0100594static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200595{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200596 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200597 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100598 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200599 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200600 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200601 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200602 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000603 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200604 u64 end = offset + PAGE_SIZE - 1;
605 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000606 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200607
608 key.objectid = root;
609 key.type = BTRFS_ROOT_ITEM_KEY;
610 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000611
612 fs_info = fixup->root->fs_info;
613 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
614
615 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
616 if (IS_ERR(local_root)) {
617 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200618 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000619 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200620
621 key.type = BTRFS_INODE_ITEM_KEY;
622 key.objectid = inum;
623 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000624 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
625 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200626 if (IS_ERR(inode))
627 return PTR_ERR(inode);
628
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200629 index = offset >> PAGE_CACHE_SHIFT;
630
631 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200632 if (!page) {
633 ret = -ENOMEM;
634 goto out;
635 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200636
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200637 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200638 if (PageDirty(page)) {
639 /*
640 * we need to write the data to the defect sector. the
641 * data that was in that sector is not in memory,
642 * because the page was modified. we must not write the
643 * modified page to that sector.
644 *
645 * TODO: what could be done here: wait for the delalloc
646 * runner to write out that page (might involve
647 * COW) and see whether the sector is still
648 * referenced afterwards.
649 *
650 * For the meantime, we'll treat this error
651 * incorrectable, although there is a chance that a
652 * later scrub will find the bad sector again and that
653 * there's no dirty page in memory, then.
654 */
655 ret = -EIO;
656 goto out;
657 }
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100658 fs_info = BTRFS_I(inode)->root->fs_info;
659 ret = repair_io_failure(fs_info, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200660 fixup->logical, page,
661 fixup->mirror_num);
662 unlock_page(page);
663 corrected = !ret;
664 } else {
665 /*
666 * we need to get good data first. the general readpage path
667 * will call repair_io_failure for us, we just have to make
668 * sure we read the bad mirror.
669 */
670 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
671 EXTENT_DAMAGED, GFP_NOFS);
672 if (ret) {
673 /* set_extent_bits should give proper error */
674 WARN_ON(ret > 0);
675 if (ret > 0)
676 ret = -EFAULT;
677 goto out;
678 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200679
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200680 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
681 btrfs_get_extent,
682 fixup->mirror_num);
683 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200684
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200685 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
686 end, EXTENT_DAMAGED, 0, NULL);
687 if (!corrected)
688 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
689 EXTENT_DAMAGED, GFP_NOFS);
690 }
691
692out:
693 if (page)
694 put_page(page);
695 if (inode)
696 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200697
698 if (ret < 0)
699 return ret;
700
701 if (ret == 0 && corrected) {
702 /*
703 * we only need to call readpage for one of the inodes belonging
704 * to this extent. so make iterate_extent_inodes stop
705 */
706 return 1;
707 }
708
709 return -EIO;
710}
711
712static void scrub_fixup_nodatasum(struct btrfs_work *work)
713{
714 int ret;
715 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100716 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200717 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200718 struct btrfs_path *path;
719 int uncorrectable = 0;
720
721 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100722 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200723
724 path = btrfs_alloc_path();
725 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100726 spin_lock(&sctx->stat_lock);
727 ++sctx->stat.malloc_errors;
728 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200729 uncorrectable = 1;
730 goto out;
731 }
732
733 trans = btrfs_join_transaction(fixup->root);
734 if (IS_ERR(trans)) {
735 uncorrectable = 1;
736 goto out;
737 }
738
739 /*
740 * the idea is to trigger a regular read through the standard path. we
741 * read a page from the (failed) logical address by specifying the
742 * corresponding copynum of the failed sector. thus, that readpage is
743 * expected to fail.
744 * that is the point where on-the-fly error correction will kick in
745 * (once it's finished) and rewrite the failed sector if a good copy
746 * can be found.
747 */
748 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
749 path, scrub_fixup_readpage,
750 fixup);
751 if (ret < 0) {
752 uncorrectable = 1;
753 goto out;
754 }
755 WARN_ON(ret != 1);
756
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100757 spin_lock(&sctx->stat_lock);
758 ++sctx->stat.corrected_errors;
759 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200760
761out:
762 if (trans && !IS_ERR(trans))
763 btrfs_end_transaction(trans, fixup->root);
764 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100765 spin_lock(&sctx->stat_lock);
766 ++sctx->stat.uncorrectable_errors;
767 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100768 btrfs_dev_replace_stats_inc(
769 &sctx->dev_root->fs_info->dev_replace.
770 num_uncorrectable_read_errors);
Josef Bacik606686e2012-06-04 14:03:51 -0400771 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400772 "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200773 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200774 }
775
776 btrfs_free_path(path);
777 kfree(fixup);
778
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100779 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200780}
781
Arne Jansena2de7332011-03-08 14:14:00 +0100782/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400783 * scrub_handle_errored_block gets called when either verification of the
784 * pages failed or the bio failed to read, e.g. with EIO. In the latter
785 * case, this function handles all pages in the bio, even though only one
786 * may be bad.
787 * The goal of this function is to repair the errored block by using the
788 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100789 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400790static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100791{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100792 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100793 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400794 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100795 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400796 u64 logical;
797 u64 generation;
798 unsigned int failed_mirror_index;
799 unsigned int is_metadata;
800 unsigned int have_csum;
801 u8 *csum;
802 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
803 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100804 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400805 int mirror_index;
806 int page_num;
807 int success;
808 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
809 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100810
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400811 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100812 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000813 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
814 /*
815 * if we find an error in a super block, we just report it.
816 * They will get written with the next transaction commit
817 * anyway
818 */
819 spin_lock(&sctx->stat_lock);
820 ++sctx->stat.super_errors;
821 spin_unlock(&sctx->stat_lock);
822 return 0;
823 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400824 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100825 logical = sblock_to_check->pagev[0]->logical;
826 generation = sblock_to_check->pagev[0]->generation;
827 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
828 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
829 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400830 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100831 have_csum = sblock_to_check->pagev[0]->have_csum;
832 csum = sblock_to_check->pagev[0]->csum;
833 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400834
Stefan Behrensff023aa2012-11-06 11:43:11 +0100835 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
836 sblocks_for_recheck = NULL;
837 goto nodatasum_case;
838 }
839
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400840 /*
841 * read all mirrors one after the other. This includes to
842 * re-read the extent or metadata block that failed (that was
843 * the cause that this fixup code is called) another time,
844 * page by page this time in order to know which pages
845 * caused I/O errors and which ones are good (for all mirrors).
846 * It is the goal to handle the situation when more than one
847 * mirror contains I/O errors, but the errors do not
848 * overlap, i.e. the data can be repaired by selecting the
849 * pages from those mirrors without I/O error on the
850 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
851 * would be that mirror #1 has an I/O error on the first page,
852 * the second page is good, and mirror #2 has an I/O error on
853 * the second page, but the first page is good.
854 * Then the first page of the first mirror can be repaired by
855 * taking the first page of the second mirror, and the
856 * second page of the second mirror can be repaired by
857 * copying the contents of the 2nd page of the 1st mirror.
858 * One more note: if the pages of one mirror contain I/O
859 * errors, the checksum cannot be verified. In order to get
860 * the best data for repairing, the first attempt is to find
861 * a mirror without I/O errors and with a validated checksum.
862 * Only if this is not possible, the pages are picked from
863 * mirrors with I/O errors without considering the checksum.
864 * If the latter is the case, at the end, the checksum of the
865 * repaired area is verified in order to correctly maintain
866 * the statistics.
867 */
868
869 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
870 sizeof(*sblocks_for_recheck),
871 GFP_NOFS);
872 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100873 spin_lock(&sctx->stat_lock);
874 sctx->stat.malloc_errors++;
875 sctx->stat.read_errors++;
876 sctx->stat.uncorrectable_errors++;
877 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100878 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400879 goto out;
880 }
881
882 /* setup the context, map the logical blocks and alloc the pages */
Stefan Behrensff023aa2012-11-06 11:43:11 +0100883 ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400884 logical, sblocks_for_recheck);
885 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100886 spin_lock(&sctx->stat_lock);
887 sctx->stat.read_errors++;
888 sctx->stat.uncorrectable_errors++;
889 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100890 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400891 goto out;
892 }
893 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
894 sblock_bad = sblocks_for_recheck + failed_mirror_index;
895
896 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100897 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
898 csum, generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400899
900 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
901 sblock_bad->no_io_error_seen) {
902 /*
903 * the error disappeared after reading page by page, or
904 * the area was part of a huge bio and other parts of the
905 * bio caused I/O errors, or the block layer merged several
906 * read requests into one and the error is caused by a
907 * different bio (usually one of the two latter cases is
908 * the cause)
909 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100910 spin_lock(&sctx->stat_lock);
911 sctx->stat.unverified_errors++;
912 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400913
Stefan Behrensff023aa2012-11-06 11:43:11 +0100914 if (sctx->is_dev_replace)
915 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400916 goto out;
917 }
918
919 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100920 spin_lock(&sctx->stat_lock);
921 sctx->stat.read_errors++;
922 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400923 if (__ratelimit(&_rs))
924 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100925 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400926 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100927 spin_lock(&sctx->stat_lock);
928 sctx->stat.csum_errors++;
929 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400930 if (__ratelimit(&_rs))
931 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100932 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200933 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400934 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100935 spin_lock(&sctx->stat_lock);
936 sctx->stat.verify_errors++;
937 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400938 if (__ratelimit(&_rs))
939 scrub_print_warning("checksum/header error",
940 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200941 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100942 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200943 BTRFS_DEV_STAT_GENERATION_ERRS);
944 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100945 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200946 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400947 }
948
Ilya Dryomov33ef30a2013-11-03 19:06:38 +0200949 if (sctx->readonly) {
950 ASSERT(!sctx->is_dev_replace);
951 goto out;
952 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400953
954 if (!is_metadata && !have_csum) {
955 struct scrub_fixup_nodatasum *fixup_nodatasum;
956
Stefan Behrensff023aa2012-11-06 11:43:11 +0100957nodatasum_case:
958 WARN_ON(sctx->is_dev_replace);
959
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400960 /*
961 * !is_metadata and !have_csum, this means that the data
962 * might not be COW'ed, that it might be modified
963 * concurrently. The general strategy to work on the
964 * commit root does not help in the case when COW is not
965 * used.
966 */
967 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
968 if (!fixup_nodatasum)
969 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100970 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100971 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400972 fixup_nodatasum->logical = logical;
973 fixup_nodatasum->root = fs_info->extent_root;
974 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100975 scrub_pending_trans_workers_inc(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400976 fixup_nodatasum->work.func = scrub_fixup_nodatasum;
977 btrfs_queue_worker(&fs_info->scrub_workers,
978 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +0100979 goto out;
980 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400981
982 /*
983 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100984 * checksums.
985 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400986 * errors and also does not have a checksum error.
987 * If one is found, and if a checksum is present, the full block
988 * that is known to contain an error is rewritten. Afterwards
989 * the block is known to be corrected.
990 * If a mirror is found which is completely correct, and no
991 * checksum is present, only those pages are rewritten that had
992 * an I/O error in the block to be repaired, since it cannot be
993 * determined, which copy of the other pages is better (and it
994 * could happen otherwise that a correct page would be
995 * overwritten by a bad one).
996 */
997 for (mirror_index = 0;
998 mirror_index < BTRFS_MAX_MIRRORS &&
999 sblocks_for_recheck[mirror_index].page_count > 0;
1000 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001001 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001002
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001003 if (mirror_index == failed_mirror_index)
1004 continue;
1005 sblock_other = sblocks_for_recheck + mirror_index;
1006
1007 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001008 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1009 have_csum, csum, generation,
1010 sctx->csum_size);
1011
1012 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001013 !sblock_other->checksum_error &&
1014 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001015 if (sctx->is_dev_replace) {
1016 scrub_write_block_to_dev_replace(sblock_other);
1017 } else {
1018 int force_write = is_metadata || have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001019
Stefan Behrensff023aa2012-11-06 11:43:11 +01001020 ret = scrub_repair_block_from_good_copy(
1021 sblock_bad, sblock_other,
1022 force_write);
1023 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001024 if (0 == ret)
1025 goto corrected_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001026 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001027 }
1028
1029 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001030 * for dev_replace, pick good pages and write to the target device.
1031 */
1032 if (sctx->is_dev_replace) {
1033 success = 1;
1034 for (page_num = 0; page_num < sblock_bad->page_count;
1035 page_num++) {
1036 int sub_success;
1037
1038 sub_success = 0;
1039 for (mirror_index = 0;
1040 mirror_index < BTRFS_MAX_MIRRORS &&
1041 sblocks_for_recheck[mirror_index].page_count > 0;
1042 mirror_index++) {
1043 struct scrub_block *sblock_other =
1044 sblocks_for_recheck + mirror_index;
1045 struct scrub_page *page_other =
1046 sblock_other->pagev[page_num];
1047
1048 if (!page_other->io_error) {
1049 ret = scrub_write_page_to_dev_replace(
1050 sblock_other, page_num);
1051 if (ret == 0) {
1052 /* succeeded for this page */
1053 sub_success = 1;
1054 break;
1055 } else {
1056 btrfs_dev_replace_stats_inc(
1057 &sctx->dev_root->
1058 fs_info->dev_replace.
1059 num_write_errors);
1060 }
1061 }
1062 }
1063
1064 if (!sub_success) {
1065 /*
1066 * did not find a mirror to fetch the page
1067 * from. scrub_write_page_to_dev_replace()
1068 * handles this case (page->io_error), by
1069 * filling the block with zeros before
1070 * submitting the write request
1071 */
1072 success = 0;
1073 ret = scrub_write_page_to_dev_replace(
1074 sblock_bad, page_num);
1075 if (ret)
1076 btrfs_dev_replace_stats_inc(
1077 &sctx->dev_root->fs_info->
1078 dev_replace.num_write_errors);
1079 }
1080 }
1081
1082 goto out;
1083 }
1084
1085 /*
1086 * for regular scrub, repair those pages that are errored.
1087 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001088 * repaired, continue by picking good copies of those pages.
1089 * Select the good pages from mirrors to rewrite bad pages from
1090 * the area to fix. Afterwards verify the checksum of the block
1091 * that is supposed to be repaired. This verification step is
1092 * only done for the purpose of statistic counting and for the
1093 * final scrub report, whether errors remain.
1094 * A perfect algorithm could make use of the checksum and try
1095 * all possible combinations of pages from the different mirrors
1096 * until the checksum verification succeeds. For example, when
1097 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1098 * of mirror #2 is readable but the final checksum test fails,
1099 * then the 2nd page of mirror #3 could be tried, whether now
1100 * the final checksum succeedes. But this would be a rare
1101 * exception and is therefore not implemented. At least it is
1102 * avoided that the good copy is overwritten.
1103 * A more useful improvement would be to pick the sectors
1104 * without I/O error based on sector sizes (512 bytes on legacy
1105 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1106 * mirror could be repaired by taking 512 byte of a different
1107 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1108 * area are unreadable.
1109 */
1110
1111 /* can only fix I/O errors from here on */
1112 if (sblock_bad->no_io_error_seen)
1113 goto did_not_correct_error;
1114
1115 success = 1;
1116 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001117 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001118
1119 if (!page_bad->io_error)
1120 continue;
1121
1122 for (mirror_index = 0;
1123 mirror_index < BTRFS_MAX_MIRRORS &&
1124 sblocks_for_recheck[mirror_index].page_count > 0;
1125 mirror_index++) {
1126 struct scrub_block *sblock_other = sblocks_for_recheck +
1127 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001128 struct scrub_page *page_other = sblock_other->pagev[
1129 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001130
1131 if (!page_other->io_error) {
1132 ret = scrub_repair_page_from_good_copy(
1133 sblock_bad, sblock_other, page_num, 0);
1134 if (0 == ret) {
1135 page_bad->io_error = 0;
1136 break; /* succeeded for this page */
1137 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001138 }
1139 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001140
1141 if (page_bad->io_error) {
1142 /* did not find a mirror to copy the page from */
1143 success = 0;
1144 }
1145 }
1146
1147 if (success) {
1148 if (is_metadata || have_csum) {
1149 /*
1150 * need to verify the checksum now that all
1151 * sectors on disk are repaired (the write
1152 * request for data to be repaired is on its way).
1153 * Just be lazy and use scrub_recheck_block()
1154 * which re-reads the data before the checksum
1155 * is verified, but most likely the data comes out
1156 * of the page cache.
1157 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001158 scrub_recheck_block(fs_info, sblock_bad,
1159 is_metadata, have_csum, csum,
1160 generation, sctx->csum_size);
1161 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001162 !sblock_bad->checksum_error &&
1163 sblock_bad->no_io_error_seen)
1164 goto corrected_error;
1165 else
1166 goto did_not_correct_error;
1167 } else {
1168corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001169 spin_lock(&sctx->stat_lock);
1170 sctx->stat.corrected_errors++;
1171 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001172 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001173 "btrfs: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001174 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001175 }
1176 } else {
1177did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001178 spin_lock(&sctx->stat_lock);
1179 sctx->stat.uncorrectable_errors++;
1180 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001181 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001182 "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001183 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001184 }
1185
1186out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001187 if (sblocks_for_recheck) {
1188 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1189 mirror_index++) {
1190 struct scrub_block *sblock = sblocks_for_recheck +
1191 mirror_index;
1192 int page_index;
1193
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001194 for (page_index = 0; page_index < sblock->page_count;
1195 page_index++) {
1196 sblock->pagev[page_index]->sblock = NULL;
1197 scrub_page_put(sblock->pagev[page_index]);
1198 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001199 }
1200 kfree(sblocks_for_recheck);
1201 }
1202
1203 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001204}
1205
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001206static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001207 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001208 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001209 u64 length, u64 logical,
1210 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001211{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001212 int page_index;
1213 int mirror_index;
1214 int ret;
1215
1216 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001217 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001218 * are not used (and not set) in the blocks that are used for
1219 * the recheck procedure
1220 */
1221
1222 page_index = 0;
1223 while (length > 0) {
1224 u64 sublen = min_t(u64, length, PAGE_SIZE);
1225 u64 mapped_length = sublen;
1226 struct btrfs_bio *bbio = NULL;
1227
1228 /*
1229 * with a length of PAGE_SIZE, each returned stripe
1230 * represents one mirror
1231 */
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01001232 ret = btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, logical,
1233 &mapped_length, &bbio, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001234 if (ret || !bbio || mapped_length < sublen) {
1235 kfree(bbio);
1236 return -EIO;
1237 }
1238
Stefan Behrensff023aa2012-11-06 11:43:11 +01001239 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001240 for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
1241 mirror_index++) {
1242 struct scrub_block *sblock;
1243 struct scrub_page *page;
1244
1245 if (mirror_index >= BTRFS_MAX_MIRRORS)
1246 continue;
1247
1248 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001249 sblock->sctx = sctx;
1250 page = kzalloc(sizeof(*page), GFP_NOFS);
1251 if (!page) {
1252leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001253 spin_lock(&sctx->stat_lock);
1254 sctx->stat.malloc_errors++;
1255 spin_unlock(&sctx->stat_lock);
Wei Yongjuncf93dcc2012-09-02 07:44:51 -06001256 kfree(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001257 return -ENOMEM;
1258 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001259 scrub_page_get(page);
1260 sblock->pagev[page_index] = page;
1261 page->logical = logical;
1262 page->physical = bbio->stripes[mirror_index].physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001263 BUG_ON(page_index >= original_sblock->page_count);
1264 page->physical_for_dev_replace =
1265 original_sblock->pagev[page_index]->
1266 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001267 /* for missing devices, dev->bdev is NULL */
1268 page->dev = bbio->stripes[mirror_index].dev;
1269 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001270 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001271 page->page = alloc_page(GFP_NOFS);
1272 if (!page->page)
1273 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001274 }
1275 kfree(bbio);
1276 length -= sublen;
1277 logical += sublen;
1278 page_index++;
1279 }
1280
1281 return 0;
1282}
1283
1284/*
1285 * this function will check the on disk data for checksum errors, header
1286 * errors and read I/O errors. If any I/O errors happen, the exact pages
1287 * which are errored are marked as being bad. The goal is to enable scrub
1288 * to take those pages that are not errored from all the mirrors so that
1289 * the pages that are errored in the just handled mirror can be repaired.
1290 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001291static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1292 struct scrub_block *sblock, int is_metadata,
1293 int have_csum, u8 *csum, u64 generation,
1294 u16 csum_size)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001295{
1296 int page_num;
1297
1298 sblock->no_io_error_seen = 1;
1299 sblock->header_error = 0;
1300 sblock->checksum_error = 0;
1301
1302 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1303 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001304 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001305
Stefan Behrens442a4f62012-05-25 16:06:08 +02001306 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001307 page->io_error = 1;
1308 sblock->no_io_error_seen = 0;
1309 continue;
1310 }
1311
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001312 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001313 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001314 if (!bio) {
1315 page->io_error = 1;
1316 sblock->no_io_error_seen = 0;
1317 continue;
1318 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001319 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001320 bio->bi_sector = page->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001321
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001322 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -07001323 if (btrfsic_submit_bio_wait(READ, bio))
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001324 sblock->no_io_error_seen = 0;
Kent Overstreetc170bbb2013-11-24 16:32:22 -07001325
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001326 bio_put(bio);
1327 }
1328
1329 if (sblock->no_io_error_seen)
1330 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1331 have_csum, csum, generation,
1332 csum_size);
1333
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001334 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001335}
1336
1337static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1338 struct scrub_block *sblock,
1339 int is_metadata, int have_csum,
1340 const u8 *csum, u64 generation,
1341 u16 csum_size)
1342{
1343 int page_num;
1344 u8 calculated_csum[BTRFS_CSUM_SIZE];
1345 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001346 void *mapped_buffer;
1347
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001348 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001349 if (is_metadata) {
1350 struct btrfs_header *h;
1351
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001352 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001353 h = (struct btrfs_header *)mapped_buffer;
1354
Qu Wenruo3cae2102013-07-16 11:19:18 +08001355 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001356 memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
1357 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001358 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001359 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001360 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001361 sblock->header_error = 1;
1362 sblock->generation_error = 1;
1363 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001364 csum = h->csum;
1365 } else {
1366 if (!have_csum)
1367 return;
1368
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001369 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001370 }
1371
1372 for (page_num = 0;;) {
1373 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001374 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001375 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1376 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1377 else
Liu Bob0496682013-03-14 14:57:45 +00001378 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001379
Linus Torvalds9613beb2012-03-30 12:44:29 -07001380 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001381 page_num++;
1382 if (page_num >= sblock->page_count)
1383 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001384 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001385
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001386 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001387 }
1388
1389 btrfs_csum_final(crc, calculated_csum);
1390 if (memcmp(calculated_csum, csum, csum_size))
1391 sblock->checksum_error = 1;
1392}
1393
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001394static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1395 struct scrub_block *sblock_good,
1396 int force_write)
1397{
1398 int page_num;
1399 int ret = 0;
1400
1401 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1402 int ret_sub;
1403
1404 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1405 sblock_good,
1406 page_num,
1407 force_write);
1408 if (ret_sub)
1409 ret = ret_sub;
1410 }
1411
1412 return ret;
1413}
1414
1415static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1416 struct scrub_block *sblock_good,
1417 int page_num, int force_write)
1418{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001419 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1420 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001421
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001422 BUG_ON(page_bad->page == NULL);
1423 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001424 if (force_write || sblock_bad->header_error ||
1425 sblock_bad->checksum_error || page_bad->io_error) {
1426 struct bio *bio;
1427 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001428
Stefan Behrensff023aa2012-11-06 11:43:11 +01001429 if (!page_bad->dev->bdev) {
1430 printk_ratelimited(KERN_WARNING
1431 "btrfs: scrub_repair_page_from_good_copy(bdev == NULL) is unexpected!\n");
1432 return -EIO;
1433 }
1434
Chris Mason9be33952013-05-17 18:30:14 -04001435 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001436 if (!bio)
1437 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001438 bio->bi_bdev = page_bad->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001439 bio->bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001440
1441 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1442 if (PAGE_SIZE != ret) {
1443 bio_put(bio);
1444 return -EIO;
1445 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001446
Kent Overstreetc170bbb2013-11-24 16:32:22 -07001447 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001448 btrfs_dev_stat_inc_and_print(page_bad->dev,
1449 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001450 btrfs_dev_replace_stats_inc(
1451 &sblock_bad->sctx->dev_root->fs_info->
1452 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001453 bio_put(bio);
1454 return -EIO;
1455 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001456 bio_put(bio);
1457 }
1458
1459 return 0;
1460}
1461
Stefan Behrensff023aa2012-11-06 11:43:11 +01001462static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1463{
1464 int page_num;
1465
1466 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1467 int ret;
1468
1469 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1470 if (ret)
1471 btrfs_dev_replace_stats_inc(
1472 &sblock->sctx->dev_root->fs_info->dev_replace.
1473 num_write_errors);
1474 }
1475}
1476
1477static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1478 int page_num)
1479{
1480 struct scrub_page *spage = sblock->pagev[page_num];
1481
1482 BUG_ON(spage->page == NULL);
1483 if (spage->io_error) {
1484 void *mapped_buffer = kmap_atomic(spage->page);
1485
1486 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1487 flush_dcache_page(spage->page);
1488 kunmap_atomic(mapped_buffer);
1489 }
1490 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1491}
1492
1493static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1494 struct scrub_page *spage)
1495{
1496 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1497 struct scrub_bio *sbio;
1498 int ret;
1499
1500 mutex_lock(&wr_ctx->wr_lock);
1501again:
1502 if (!wr_ctx->wr_curr_bio) {
1503 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1504 GFP_NOFS);
1505 if (!wr_ctx->wr_curr_bio) {
1506 mutex_unlock(&wr_ctx->wr_lock);
1507 return -ENOMEM;
1508 }
1509 wr_ctx->wr_curr_bio->sctx = sctx;
1510 wr_ctx->wr_curr_bio->page_count = 0;
1511 }
1512 sbio = wr_ctx->wr_curr_bio;
1513 if (sbio->page_count == 0) {
1514 struct bio *bio;
1515
1516 sbio->physical = spage->physical_for_dev_replace;
1517 sbio->logical = spage->logical;
1518 sbio->dev = wr_ctx->tgtdev;
1519 bio = sbio->bio;
1520 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001521 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001522 if (!bio) {
1523 mutex_unlock(&wr_ctx->wr_lock);
1524 return -ENOMEM;
1525 }
1526 sbio->bio = bio;
1527 }
1528
1529 bio->bi_private = sbio;
1530 bio->bi_end_io = scrub_wr_bio_end_io;
1531 bio->bi_bdev = sbio->dev->bdev;
1532 bio->bi_sector = sbio->physical >> 9;
1533 sbio->err = 0;
1534 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1535 spage->physical_for_dev_replace ||
1536 sbio->logical + sbio->page_count * PAGE_SIZE !=
1537 spage->logical) {
1538 scrub_wr_submit(sctx);
1539 goto again;
1540 }
1541
1542 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1543 if (ret != PAGE_SIZE) {
1544 if (sbio->page_count < 1) {
1545 bio_put(sbio->bio);
1546 sbio->bio = NULL;
1547 mutex_unlock(&wr_ctx->wr_lock);
1548 return -EIO;
1549 }
1550 scrub_wr_submit(sctx);
1551 goto again;
1552 }
1553
1554 sbio->pagev[sbio->page_count] = spage;
1555 scrub_page_get(spage);
1556 sbio->page_count++;
1557 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1558 scrub_wr_submit(sctx);
1559 mutex_unlock(&wr_ctx->wr_lock);
1560
1561 return 0;
1562}
1563
1564static void scrub_wr_submit(struct scrub_ctx *sctx)
1565{
1566 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1567 struct scrub_bio *sbio;
1568
1569 if (!wr_ctx->wr_curr_bio)
1570 return;
1571
1572 sbio = wr_ctx->wr_curr_bio;
1573 wr_ctx->wr_curr_bio = NULL;
1574 WARN_ON(!sbio->bio->bi_bdev);
1575 scrub_pending_bio_inc(sctx);
1576 /* process all writes in a single worker thread. Then the block layer
1577 * orders the requests before sending them to the driver which
1578 * doubled the write performance on spinning disks when measured
1579 * with Linux 3.5 */
1580 btrfsic_submit_bio(WRITE, sbio->bio);
1581}
1582
1583static void scrub_wr_bio_end_io(struct bio *bio, int err)
1584{
1585 struct scrub_bio *sbio = bio->bi_private;
1586 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1587
1588 sbio->err = err;
1589 sbio->bio = bio;
1590
1591 sbio->work.func = scrub_wr_bio_end_io_worker;
1592 btrfs_queue_worker(&fs_info->scrub_wr_completion_workers, &sbio->work);
1593}
1594
1595static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1596{
1597 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1598 struct scrub_ctx *sctx = sbio->sctx;
1599 int i;
1600
1601 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1602 if (sbio->err) {
1603 struct btrfs_dev_replace *dev_replace =
1604 &sbio->sctx->dev_root->fs_info->dev_replace;
1605
1606 for (i = 0; i < sbio->page_count; i++) {
1607 struct scrub_page *spage = sbio->pagev[i];
1608
1609 spage->io_error = 1;
1610 btrfs_dev_replace_stats_inc(&dev_replace->
1611 num_write_errors);
1612 }
1613 }
1614
1615 for (i = 0; i < sbio->page_count; i++)
1616 scrub_page_put(sbio->pagev[i]);
1617
1618 bio_put(sbio->bio);
1619 kfree(sbio);
1620 scrub_pending_bio_dec(sctx);
1621}
1622
1623static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001624{
1625 u64 flags;
1626 int ret;
1627
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001628 WARN_ON(sblock->page_count < 1);
1629 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001630 ret = 0;
1631 if (flags & BTRFS_EXTENT_FLAG_DATA)
1632 ret = scrub_checksum_data(sblock);
1633 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1634 ret = scrub_checksum_tree_block(sblock);
1635 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1636 (void)scrub_checksum_super(sblock);
1637 else
1638 WARN_ON(1);
1639 if (ret)
1640 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001641
1642 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001643}
1644
1645static int scrub_checksum_data(struct scrub_block *sblock)
1646{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001647 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001648 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001649 u8 *on_disk_csum;
1650 struct page *page;
1651 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001652 u32 crc = ~(u32)0;
1653 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001654 u64 len;
1655 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001656
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001657 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001658 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001659 return 0;
1660
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001661 on_disk_csum = sblock->pagev[0]->csum;
1662 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001663 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001664
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001665 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001666 index = 0;
1667 for (;;) {
1668 u64 l = min_t(u64, len, PAGE_SIZE);
1669
Liu Bob0496682013-03-14 14:57:45 +00001670 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001671 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001672 len -= l;
1673 if (len == 0)
1674 break;
1675 index++;
1676 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001677 BUG_ON(!sblock->pagev[index]->page);
1678 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001679 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001680 }
1681
Arne Jansena2de7332011-03-08 14:14:00 +01001682 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001683 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001684 fail = 1;
1685
Arne Jansena2de7332011-03-08 14:14:00 +01001686 return fail;
1687}
1688
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001689static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001690{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001691 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001692 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001693 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001694 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001695 u8 calculated_csum[BTRFS_CSUM_SIZE];
1696 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1697 struct page *page;
1698 void *mapped_buffer;
1699 u64 mapped_size;
1700 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001701 u32 crc = ~(u32)0;
1702 int fail = 0;
1703 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001704 u64 len;
1705 int index;
1706
1707 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001708 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001709 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001710 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001711 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001712
1713 /*
1714 * we don't use the getter functions here, as we
1715 * a) don't have an extent buffer and
1716 * b) the page is already kmapped
1717 */
Arne Jansena2de7332011-03-08 14:14:00 +01001718
Qu Wenruo3cae2102013-07-16 11:19:18 +08001719 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001720 ++fail;
1721
Qu Wenruo3cae2102013-07-16 11:19:18 +08001722 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001723 ++fail;
1724
1725 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1726 ++fail;
1727
1728 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1729 BTRFS_UUID_SIZE))
1730 ++fail;
1731
Stefan Behrensff023aa2012-11-06 11:43:11 +01001732 WARN_ON(sctx->nodesize != sctx->leafsize);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001733 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001734 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1735 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1736 index = 0;
1737 for (;;) {
1738 u64 l = min_t(u64, len, mapped_size);
1739
Liu Bob0496682013-03-14 14:57:45 +00001740 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001741 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001742 len -= l;
1743 if (len == 0)
1744 break;
1745 index++;
1746 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001747 BUG_ON(!sblock->pagev[index]->page);
1748 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001749 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001750 mapped_size = PAGE_SIZE;
1751 p = mapped_buffer;
1752 }
1753
1754 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001755 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001756 ++crc_fail;
1757
Arne Jansena2de7332011-03-08 14:14:00 +01001758 return fail || crc_fail;
1759}
1760
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001761static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001762{
1763 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001764 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001765 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001766 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001767 u8 calculated_csum[BTRFS_CSUM_SIZE];
1768 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1769 struct page *page;
1770 void *mapped_buffer;
1771 u64 mapped_size;
1772 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001773 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001774 int fail_gen = 0;
1775 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001776 u64 len;
1777 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001778
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001779 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001780 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001781 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001782 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001783 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001784
Qu Wenruo3cae2102013-07-16 11:19:18 +08001785 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001786 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001787
Qu Wenruo3cae2102013-07-16 11:19:18 +08001788 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001789 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001790
1791 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001792 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001793
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001794 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1795 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1796 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1797 index = 0;
1798 for (;;) {
1799 u64 l = min_t(u64, len, mapped_size);
1800
Liu Bob0496682013-03-14 14:57:45 +00001801 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001802 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001803 len -= l;
1804 if (len == 0)
1805 break;
1806 index++;
1807 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001808 BUG_ON(!sblock->pagev[index]->page);
1809 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001810 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001811 mapped_size = PAGE_SIZE;
1812 p = mapped_buffer;
1813 }
1814
1815 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001816 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001817 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001818
Stefan Behrens442a4f62012-05-25 16:06:08 +02001819 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001820 /*
1821 * if we find an error in a super block, we just report it.
1822 * They will get written with the next transaction commit
1823 * anyway
1824 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001825 spin_lock(&sctx->stat_lock);
1826 ++sctx->stat.super_errors;
1827 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001828 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001829 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001830 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1831 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001832 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001833 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001834 }
1835
Stefan Behrens442a4f62012-05-25 16:06:08 +02001836 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001837}
1838
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001839static void scrub_block_get(struct scrub_block *sblock)
1840{
1841 atomic_inc(&sblock->ref_count);
1842}
1843
1844static void scrub_block_put(struct scrub_block *sblock)
1845{
1846 if (atomic_dec_and_test(&sblock->ref_count)) {
1847 int i;
1848
1849 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001850 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001851 kfree(sblock);
1852 }
1853}
1854
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001855static void scrub_page_get(struct scrub_page *spage)
1856{
1857 atomic_inc(&spage->ref_count);
1858}
1859
1860static void scrub_page_put(struct scrub_page *spage)
1861{
1862 if (atomic_dec_and_test(&spage->ref_count)) {
1863 if (spage->page)
1864 __free_page(spage->page);
1865 kfree(spage);
1866 }
1867}
1868
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001869static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01001870{
1871 struct scrub_bio *sbio;
1872
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001873 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04001874 return;
Arne Jansena2de7332011-03-08 14:14:00 +01001875
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001876 sbio = sctx->bios[sctx->curr];
1877 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001878 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001879
Stefan Behrensff023aa2012-11-06 11:43:11 +01001880 if (!sbio->bio->bi_bdev) {
1881 /*
1882 * this case should not happen. If btrfs_map_block() is
1883 * wrong, it could happen for dev-replace operations on
1884 * missing devices when no mirrors are available, but in
1885 * this case it should already fail the mount.
1886 * This case is handled correctly (but _very_ slowly).
1887 */
1888 printk_ratelimited(KERN_WARNING
1889 "btrfs: scrub_submit(bio bdev == NULL) is unexpected!\n");
1890 bio_endio(sbio->bio, -EIO);
1891 } else {
1892 btrfsic_submit_bio(READ, sbio->bio);
1893 }
Arne Jansena2de7332011-03-08 14:14:00 +01001894}
1895
Stefan Behrensff023aa2012-11-06 11:43:11 +01001896static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
1897 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01001898{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001899 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01001900 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001901 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01001902
1903again:
1904 /*
1905 * grab a fresh bio or wait for one to become available
1906 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001907 while (sctx->curr == -1) {
1908 spin_lock(&sctx->list_lock);
1909 sctx->curr = sctx->first_free;
1910 if (sctx->curr != -1) {
1911 sctx->first_free = sctx->bios[sctx->curr]->next_free;
1912 sctx->bios[sctx->curr]->next_free = -1;
1913 sctx->bios[sctx->curr]->page_count = 0;
1914 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001915 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001916 spin_unlock(&sctx->list_lock);
1917 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01001918 }
1919 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001920 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001921 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05001922 struct bio *bio;
1923
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001924 sbio->physical = spage->physical;
1925 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001926 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001927 bio = sbio->bio;
1928 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001929 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001930 if (!bio)
1931 return -ENOMEM;
1932 sbio->bio = bio;
1933 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05001934
1935 bio->bi_private = sbio;
1936 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001937 bio->bi_bdev = sbio->dev->bdev;
1938 bio->bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001939 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001940 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1941 spage->physical ||
1942 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001943 spage->logical ||
1944 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001945 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05001946 goto again;
1947 }
1948
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001949 sbio->pagev[sbio->page_count] = spage;
1950 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1951 if (ret != PAGE_SIZE) {
1952 if (sbio->page_count < 1) {
1953 bio_put(sbio->bio);
1954 sbio->bio = NULL;
1955 return -EIO;
1956 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001957 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001958 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01001959 }
Arne Jansen1bc87792011-05-28 21:57:55 +02001960
Stefan Behrensff023aa2012-11-06 11:43:11 +01001961 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001962 atomic_inc(&sblock->outstanding_pages);
1963 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001964 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001965 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001966
1967 return 0;
1968}
1969
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001970static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001971 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001972 u64 gen, int mirror_num, u8 *csum, int force,
1973 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001974{
1975 struct scrub_block *sblock;
1976 int index;
1977
1978 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
1979 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001980 spin_lock(&sctx->stat_lock);
1981 sctx->stat.malloc_errors++;
1982 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001983 return -ENOMEM;
1984 }
1985
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001986 /* one ref inside this function, plus one for each page added to
1987 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001988 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001989 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001990 sblock->no_io_error_seen = 1;
1991
1992 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001993 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001994 u64 l = min_t(u64, len, PAGE_SIZE);
1995
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001996 spage = kzalloc(sizeof(*spage), GFP_NOFS);
1997 if (!spage) {
1998leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001999 spin_lock(&sctx->stat_lock);
2000 sctx->stat.malloc_errors++;
2001 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002002 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002003 return -ENOMEM;
2004 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002005 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2006 scrub_page_get(spage);
2007 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002008 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002009 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002010 spage->flags = flags;
2011 spage->generation = gen;
2012 spage->logical = logical;
2013 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002014 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002015 spage->mirror_num = mirror_num;
2016 if (csum) {
2017 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002018 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002019 } else {
2020 spage->have_csum = 0;
2021 }
2022 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002023 spage->page = alloc_page(GFP_NOFS);
2024 if (!spage->page)
2025 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002026 len -= l;
2027 logical += l;
2028 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002029 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002030 }
2031
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002032 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002033 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002034 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002035 int ret;
2036
Stefan Behrensff023aa2012-11-06 11:43:11 +01002037 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002038 if (ret) {
2039 scrub_block_put(sblock);
2040 return ret;
2041 }
2042 }
2043
2044 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002045 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002046
2047 /* last one frees, either here or in bio completion for last page */
2048 scrub_block_put(sblock);
2049 return 0;
2050}
2051
2052static void scrub_bio_end_io(struct bio *bio, int err)
2053{
2054 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002055 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002056
2057 sbio->err = err;
2058 sbio->bio = bio;
2059
2060 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
2061}
2062
2063static void scrub_bio_end_io_worker(struct btrfs_work *work)
2064{
2065 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002066 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002067 int i;
2068
Stefan Behrensff023aa2012-11-06 11:43:11 +01002069 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002070 if (sbio->err) {
2071 for (i = 0; i < sbio->page_count; i++) {
2072 struct scrub_page *spage = sbio->pagev[i];
2073
2074 spage->io_error = 1;
2075 spage->sblock->no_io_error_seen = 0;
2076 }
2077 }
2078
2079 /* now complete the scrub_block items that have all pages completed */
2080 for (i = 0; i < sbio->page_count; i++) {
2081 struct scrub_page *spage = sbio->pagev[i];
2082 struct scrub_block *sblock = spage->sblock;
2083
2084 if (atomic_dec_and_test(&sblock->outstanding_pages))
2085 scrub_block_complete(sblock);
2086 scrub_block_put(sblock);
2087 }
2088
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002089 bio_put(sbio->bio);
2090 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002091 spin_lock(&sctx->list_lock);
2092 sbio->next_free = sctx->first_free;
2093 sctx->first_free = sbio->index;
2094 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002095
2096 if (sctx->is_dev_replace &&
2097 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2098 mutex_lock(&sctx->wr_ctx.wr_lock);
2099 scrub_wr_submit(sctx);
2100 mutex_unlock(&sctx->wr_ctx.wr_lock);
2101 }
2102
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002103 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002104}
2105
2106static void scrub_block_complete(struct scrub_block *sblock)
2107{
Stefan Behrensff023aa2012-11-06 11:43:11 +01002108 if (!sblock->no_io_error_seen) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002109 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002110 } else {
2111 /*
2112 * if has checksum error, write via repair mechanism in
2113 * dev replace case, otherwise write here in dev replace
2114 * case.
2115 */
2116 if (!scrub_checksum(sblock) && sblock->sctx->is_dev_replace)
2117 scrub_write_block_to_dev_replace(sblock);
2118 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002119}
2120
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002121static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002122 u8 *csum)
2123{
2124 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002125 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002126 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002127
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002128 while (!list_empty(&sctx->csum_list)) {
2129 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002130 struct btrfs_ordered_sum, list);
2131 if (sum->bytenr > logical)
2132 return 0;
2133 if (sum->bytenr + sum->len > logical)
2134 break;
2135
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002136 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002137 list_del(&sum->list);
2138 kfree(sum);
2139 sum = NULL;
2140 }
2141 if (!sum)
2142 return 0;
2143
Miao Xief51a4a12013-06-19 10:36:09 +08002144 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002145 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002146 memcpy(csum, sum->sums + index, sctx->csum_size);
2147 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002148 list_del(&sum->list);
2149 kfree(sum);
2150 }
Miao Xief51a4a12013-06-19 10:36:09 +08002151 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002152}
2153
2154/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002155static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002156 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002157 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002158{
2159 int ret;
2160 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002161 u32 blocksize;
2162
2163 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002164 blocksize = sctx->sectorsize;
2165 spin_lock(&sctx->stat_lock);
2166 sctx->stat.data_extents_scrubbed++;
2167 sctx->stat.data_bytes_scrubbed += len;
2168 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002169 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002170 WARN_ON(sctx->nodesize != sctx->leafsize);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002171 blocksize = sctx->nodesize;
2172 spin_lock(&sctx->stat_lock);
2173 sctx->stat.tree_extents_scrubbed++;
2174 sctx->stat.tree_bytes_scrubbed += len;
2175 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002176 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002177 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002178 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002179 }
Arne Jansena2de7332011-03-08 14:14:00 +01002180
2181 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002182 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002183 int have_csum = 0;
2184
2185 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2186 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002187 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002188 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002189 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002190 if (sctx->is_dev_replace && !have_csum) {
2191 ret = copy_nocow_pages(sctx, logical, l,
2192 mirror_num,
2193 physical_for_dev_replace);
2194 goto behind_scrub_pages;
2195 }
Arne Jansena2de7332011-03-08 14:14:00 +01002196 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002197 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002198 mirror_num, have_csum ? csum : NULL, 0,
2199 physical_for_dev_replace);
2200behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002201 if (ret)
2202 return ret;
2203 len -= l;
2204 logical += l;
2205 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002206 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002207 }
2208 return 0;
2209}
2210
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002211static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002212 struct map_lookup *map,
2213 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002214 int num, u64 base, u64 length,
2215 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002216{
2217 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002218 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002219 struct btrfs_root *root = fs_info->extent_root;
2220 struct btrfs_root *csum_root = fs_info->csum_root;
2221 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002222 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002223 u64 flags;
2224 int ret;
2225 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002226 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002227 struct extent_buffer *l;
2228 struct btrfs_key key;
2229 u64 physical;
2230 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002231 u64 logic_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002232 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002233 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002234 struct reada_control *reada1;
2235 struct reada_control *reada2;
2236 struct btrfs_key key_start;
2237 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002238 u64 increment = map->stripe_len;
2239 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002240 u64 extent_logical;
2241 u64 extent_physical;
2242 u64 extent_len;
2243 struct btrfs_device *extent_dev;
2244 int extent_mirror_num;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002245 int stop_loop;
Arne Jansena2de7332011-03-08 14:14:00 +01002246
David Woodhouse53b381b2013-01-29 18:40:14 -05002247 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
2248 BTRFS_BLOCK_GROUP_RAID6)) {
2249 if (num >= nr_data_stripes(map)) {
2250 return 0;
2251 }
2252 }
2253
Arne Jansena2de7332011-03-08 14:14:00 +01002254 nstripes = length;
2255 offset = 0;
2256 do_div(nstripes, map->stripe_len);
2257 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2258 offset = map->stripe_len * num;
2259 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02002260 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002261 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2262 int factor = map->num_stripes / map->sub_stripes;
2263 offset = map->stripe_len * (num / map->sub_stripes);
2264 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02002265 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002266 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2267 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002268 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002269 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2270 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002271 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002272 } else {
2273 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002274 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002275 }
2276
2277 path = btrfs_alloc_path();
2278 if (!path)
2279 return -ENOMEM;
2280
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002281 /*
2282 * work on commit root. The related disk blocks are static as
2283 * long as COW is applied. This means, it is save to rewrite
2284 * them to repair disk errors without any race conditions
2285 */
Arne Jansena2de7332011-03-08 14:14:00 +01002286 path->search_commit_root = 1;
2287 path->skip_locking = 1;
2288
2289 /*
Arne Jansen7a262852011-06-10 12:39:23 +02002290 * trigger the readahead for extent tree csum tree and wait for
2291 * completion. During readahead, the scrub is officially paused
2292 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01002293 */
2294 logical = base + offset;
Arne Jansena2de7332011-03-08 14:14:00 +01002295
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002296 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002297 atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansen7a262852011-06-10 12:39:23 +02002298 atomic_inc(&fs_info->scrubs_paused);
2299 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002300
Arne Jansen7a262852011-06-10 12:39:23 +02002301 /* FIXME it might be better to start readahead at commit root */
2302 key_start.objectid = logical;
2303 key_start.type = BTRFS_EXTENT_ITEM_KEY;
2304 key_start.offset = (u64)0;
2305 key_end.objectid = base + offset + nstripes * increment;
Josef Bacik3173a182013-03-07 14:22:04 -05002306 key_end.type = BTRFS_METADATA_ITEM_KEY;
2307 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02002308 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01002309
Arne Jansen7a262852011-06-10 12:39:23 +02002310 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2311 key_start.type = BTRFS_EXTENT_CSUM_KEY;
2312 key_start.offset = logical;
2313 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2314 key_end.type = BTRFS_EXTENT_CSUM_KEY;
2315 key_end.offset = base + offset + nstripes * increment;
2316 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01002317
Arne Jansen7a262852011-06-10 12:39:23 +02002318 if (!IS_ERR(reada1))
2319 btrfs_reada_wait(reada1);
2320 if (!IS_ERR(reada2))
2321 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01002322
Arne Jansen7a262852011-06-10 12:39:23 +02002323 mutex_lock(&fs_info->scrub_lock);
Wang Shilong3cb09292013-12-04 21:15:19 +08002324 scrub_blocked_if_needed(fs_info);
Arne Jansen7a262852011-06-10 12:39:23 +02002325 atomic_dec(&fs_info->scrubs_paused);
2326 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong3cb09292013-12-04 21:15:19 +08002327
Arne Jansen7a262852011-06-10 12:39:23 +02002328 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002329
2330 /*
2331 * collect all data csums for the stripe to avoid seeking during
2332 * the scrub. This might currently (crc32) end up to be about 1MB
2333 */
Arne Jansene7786c32011-05-28 20:58:38 +00002334 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002335
Arne Jansena2de7332011-03-08 14:14:00 +01002336 /*
2337 * now find all extents for each stripe and scrub them
2338 */
Arne Jansen7a262852011-06-10 12:39:23 +02002339 logical = base + offset;
2340 physical = map->stripes[num].physical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002341 logic_end = logical + increment * nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002342 ret = 0;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002343 while (logical < logic_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01002344 /*
2345 * canceled?
2346 */
2347 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002348 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002349 ret = -ECANCELED;
2350 goto out;
2351 }
2352 /*
2353 * check to see if we have to pause
2354 */
2355 if (atomic_read(&fs_info->scrub_pause_req)) {
2356 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002357 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002358 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002359 mutex_lock(&sctx->wr_ctx.wr_lock);
2360 scrub_wr_submit(sctx);
2361 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002362 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002363 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002364 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002365 atomic_inc(&fs_info->scrubs_paused);
2366 wake_up(&fs_info->scrub_pause_wait);
Wang Shilong3cb09292013-12-04 21:15:19 +08002367
Arne Jansena2de7332011-03-08 14:14:00 +01002368 mutex_lock(&fs_info->scrub_lock);
Wang Shilong3cb09292013-12-04 21:15:19 +08002369 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002370 atomic_dec(&fs_info->scrubs_paused);
2371 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong3cb09292013-12-04 21:15:19 +08002372
Arne Jansena2de7332011-03-08 14:14:00 +01002373 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002374 }
2375
2376 key.objectid = logical;
2377 key.type = BTRFS_EXTENT_ITEM_KEY;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002378 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01002379
2380 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2381 if (ret < 0)
2382 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002383
Arne Jansen8c510322011-06-03 10:09:26 +02002384 if (ret > 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01002385 ret = btrfs_previous_item(root, path, 0,
2386 BTRFS_EXTENT_ITEM_KEY);
2387 if (ret < 0)
2388 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02002389 if (ret > 0) {
2390 /* there's no smaller item, so stick with the
2391 * larger one */
2392 btrfs_release_path(path);
2393 ret = btrfs_search_slot(NULL, root, &key,
2394 path, 0, 0);
2395 if (ret < 0)
2396 goto out;
2397 }
Arne Jansena2de7332011-03-08 14:14:00 +01002398 }
2399
Liu Bo625f1c8d2013-04-27 02:56:57 +00002400 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002401 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05002402 u64 bytes;
2403
Arne Jansena2de7332011-03-08 14:14:00 +01002404 l = path->nodes[0];
2405 slot = path->slots[0];
2406 if (slot >= btrfs_header_nritems(l)) {
2407 ret = btrfs_next_leaf(root, path);
2408 if (ret == 0)
2409 continue;
2410 if (ret < 0)
2411 goto out;
2412
Liu Bo625f1c8d2013-04-27 02:56:57 +00002413 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002414 break;
2415 }
2416 btrfs_item_key_to_cpu(l, &key, slot);
2417
Josef Bacik3173a182013-03-07 14:22:04 -05002418 if (key.type == BTRFS_METADATA_ITEM_KEY)
2419 bytes = root->leafsize;
2420 else
2421 bytes = key.offset;
2422
2423 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01002424 goto next;
2425
Liu Bo625f1c8d2013-04-27 02:56:57 +00002426 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2427 key.type != BTRFS_METADATA_ITEM_KEY)
2428 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01002429
Liu Bo625f1c8d2013-04-27 02:56:57 +00002430 if (key.objectid >= logical + map->stripe_len) {
2431 /* out of this device extent */
2432 if (key.objectid >= logic_end)
2433 stop_loop = 1;
2434 break;
2435 }
Arne Jansena2de7332011-03-08 14:14:00 +01002436
2437 extent = btrfs_item_ptr(l, slot,
2438 struct btrfs_extent_item);
2439 flags = btrfs_extent_flags(l, extent);
2440 generation = btrfs_extent_generation(l, extent);
2441
2442 if (key.objectid < logical &&
2443 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2444 printk(KERN_ERR
2445 "btrfs scrub: tree block %llu spanning "
2446 "stripes, ignored. logical=%llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002447 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01002448 goto next;
2449 }
2450
Liu Bo625f1c8d2013-04-27 02:56:57 +00002451again:
2452 extent_logical = key.objectid;
2453 extent_len = bytes;
2454
Arne Jansena2de7332011-03-08 14:14:00 +01002455 /*
2456 * trim extent to this stripe
2457 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00002458 if (extent_logical < logical) {
2459 extent_len -= logical - extent_logical;
2460 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01002461 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00002462 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01002463 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00002464 extent_len = logical + map->stripe_len -
2465 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01002466 }
2467
Liu Bo625f1c8d2013-04-27 02:56:57 +00002468 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002469 extent_dev = scrub_dev;
2470 extent_mirror_num = mirror_num;
2471 if (is_dev_replace)
2472 scrub_remap_extent(fs_info, extent_logical,
2473 extent_len, &extent_physical,
2474 &extent_dev,
2475 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002476
2477 ret = btrfs_lookup_csums_range(csum_root, logical,
2478 logical + map->stripe_len - 1,
2479 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002480 if (ret)
2481 goto out;
2482
Liu Bo625f1c8d2013-04-27 02:56:57 +00002483 ret = scrub_extent(sctx, extent_logical, extent_len,
2484 extent_physical, extent_dev, flags,
2485 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02002486 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002487 if (ret)
2488 goto out;
2489
Josef Bacikd88d46c2013-06-10 12:59:04 +00002490 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002491 if (extent_logical + extent_len <
2492 key.objectid + bytes) {
2493 logical += increment;
2494 physical += map->stripe_len;
2495
2496 if (logical < key.objectid + bytes) {
2497 cond_resched();
2498 goto again;
2499 }
2500
2501 if (logical >= logic_end) {
2502 stop_loop = 1;
2503 break;
2504 }
2505 }
Arne Jansena2de7332011-03-08 14:14:00 +01002506next:
2507 path->slots[0]++;
2508 }
Chris Mason71267332011-05-23 06:30:52 -04002509 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002510 logical += increment;
2511 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002512 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002513 if (stop_loop)
2514 sctx->stat.last_physical = map->stripes[num].physical +
2515 length;
2516 else
2517 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002518 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002519 if (stop_loop)
2520 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002521 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01002522out:
Arne Jansena2de7332011-03-08 14:14:00 +01002523 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002524 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002525 mutex_lock(&sctx->wr_ctx.wr_lock);
2526 scrub_wr_submit(sctx);
2527 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002528
Arne Jansene7786c32011-05-28 20:58:38 +00002529 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002530 btrfs_free_path(path);
2531 return ret < 0 ? ret : 0;
2532}
2533
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002534static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002535 struct btrfs_device *scrub_dev,
2536 u64 chunk_tree, u64 chunk_objectid,
2537 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002538 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002539{
2540 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002541 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01002542 struct map_lookup *map;
2543 struct extent_map *em;
2544 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002545 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002546
2547 read_lock(&map_tree->map_tree.lock);
2548 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2549 read_unlock(&map_tree->map_tree.lock);
2550
2551 if (!em)
2552 return -EINVAL;
2553
2554 map = (struct map_lookup *)em->bdev;
2555 if (em->start != chunk_offset)
2556 goto out;
2557
2558 if (em->len < length)
2559 goto out;
2560
2561 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002562 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01002563 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002564 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002565 chunk_offset, length,
2566 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002567 if (ret)
2568 goto out;
2569 }
2570 }
2571out:
2572 free_extent_map(em);
2573
2574 return ret;
2575}
2576
2577static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002578int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002579 struct btrfs_device *scrub_dev, u64 start, u64 end,
2580 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002581{
2582 struct btrfs_dev_extent *dev_extent = NULL;
2583 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002584 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002585 struct btrfs_fs_info *fs_info = root->fs_info;
2586 u64 length;
2587 u64 chunk_tree;
2588 u64 chunk_objectid;
2589 u64 chunk_offset;
2590 int ret;
2591 int slot;
2592 struct extent_buffer *l;
2593 struct btrfs_key key;
2594 struct btrfs_key found_key;
2595 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002596 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01002597
2598 path = btrfs_alloc_path();
2599 if (!path)
2600 return -ENOMEM;
2601
2602 path->reada = 2;
2603 path->search_commit_root = 1;
2604 path->skip_locking = 1;
2605
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002606 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01002607 key.offset = 0ull;
2608 key.type = BTRFS_DEV_EXTENT_KEY;
2609
Arne Jansena2de7332011-03-08 14:14:00 +01002610 while (1) {
2611 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2612 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02002613 break;
2614 if (ret > 0) {
2615 if (path->slots[0] >=
2616 btrfs_header_nritems(path->nodes[0])) {
2617 ret = btrfs_next_leaf(root, path);
2618 if (ret)
2619 break;
2620 }
2621 }
Arne Jansena2de7332011-03-08 14:14:00 +01002622
2623 l = path->nodes[0];
2624 slot = path->slots[0];
2625
2626 btrfs_item_key_to_cpu(l, &found_key, slot);
2627
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002628 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01002629 break;
2630
Arne Jansen8c510322011-06-03 10:09:26 +02002631 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01002632 break;
2633
2634 if (found_key.offset >= end)
2635 break;
2636
2637 if (found_key.offset < key.offset)
2638 break;
2639
2640 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2641 length = btrfs_dev_extent_length(l, dev_extent);
2642
2643 if (found_key.offset + length <= start) {
2644 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002645 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002646 continue;
2647 }
2648
2649 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2650 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2651 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2652
2653 /*
2654 * get a reference on the corresponding block group to prevent
2655 * the chunk from going away while we scrub it
2656 */
2657 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2658 if (!cache) {
2659 ret = -ENOENT;
Arne Jansen8c510322011-06-03 10:09:26 +02002660 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002661 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01002662 dev_replace->cursor_right = found_key.offset + length;
2663 dev_replace->cursor_left = found_key.offset;
2664 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002665 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002666 chunk_offset, length, found_key.offset,
2667 is_dev_replace);
2668
2669 /*
2670 * flush, submit all pending read and write bios, afterwards
2671 * wait for them.
2672 * Note that in the dev replace case, a read request causes
2673 * write requests that are submitted in the read completion
2674 * worker. Therefore in the current situation, it is required
2675 * that all write requests are flushed, so that all read and
2676 * write requests are really completed when bios_in_flight
2677 * changes to 0.
2678 */
2679 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
2680 scrub_submit(sctx);
2681 mutex_lock(&sctx->wr_ctx.wr_lock);
2682 scrub_wr_submit(sctx);
2683 mutex_unlock(&sctx->wr_ctx.wr_lock);
2684
2685 wait_event(sctx->list_wait,
2686 atomic_read(&sctx->bios_in_flight) == 0);
2687 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
2688 atomic_inc(&fs_info->scrubs_paused);
2689 wake_up(&fs_info->scrub_pause_wait);
2690 wait_event(sctx->list_wait,
2691 atomic_read(&sctx->workers_pending) == 0);
2692
2693 mutex_lock(&fs_info->scrub_lock);
Wang Shilong3cb09292013-12-04 21:15:19 +08002694 scrub_blocked_if_needed(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002695 atomic_dec(&fs_info->scrubs_paused);
2696 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong3cb09292013-12-04 21:15:19 +08002697
Stefan Behrensff023aa2012-11-06 11:43:11 +01002698 wake_up(&fs_info->scrub_pause_wait);
2699
Arne Jansena2de7332011-03-08 14:14:00 +01002700 btrfs_put_block_group(cache);
2701 if (ret)
2702 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00002703 if (is_dev_replace &&
2704 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002705 ret = -EIO;
2706 break;
2707 }
2708 if (sctx->stat.malloc_errors > 0) {
2709 ret = -ENOMEM;
2710 break;
2711 }
Arne Jansena2de7332011-03-08 14:14:00 +01002712
Ilya Dryomov539f3582013-10-07 13:42:57 +03002713 dev_replace->cursor_left = dev_replace->cursor_right;
2714 dev_replace->item_needs_writeback = 1;
2715
Arne Jansena2de7332011-03-08 14:14:00 +01002716 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002717 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002718 }
2719
Arne Jansena2de7332011-03-08 14:14:00 +01002720 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02002721
2722 /*
2723 * ret can still be 1 from search_slot or next_leaf,
2724 * that's not an error
2725 */
2726 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002727}
2728
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002729static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2730 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01002731{
2732 int i;
2733 u64 bytenr;
2734 u64 gen;
2735 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002736 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002737
Miao Xie87533c42013-01-29 10:14:48 +00002738 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002739 return -EIO;
2740
Arne Jansena2de7332011-03-08 14:14:00 +01002741 gen = root->fs_info->last_trans_committed;
2742
2743 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2744 bytenr = btrfs_sb_offset(i);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002745 if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01002746 break;
2747
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002748 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002749 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002750 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01002751 if (ret)
2752 return ret;
2753 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002754 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002755
2756 return 0;
2757}
2758
2759/*
2760 * get a reference count on fs_info->scrub_workers. start worker if necessary
2761 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002762static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
2763 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002764{
Josef Bacik0dc3b842011-11-18 14:37:27 -05002765 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002766
Arne Jansen632dd772011-06-10 12:07:07 +02002767 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002768 if (is_dev_replace)
2769 btrfs_init_workers(&fs_info->scrub_workers, "scrub", 1,
2770 &fs_info->generic_worker);
2771 else
2772 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
2773 fs_info->thread_pool_size,
2774 &fs_info->generic_worker);
Arne Jansen632dd772011-06-10 12:07:07 +02002775 fs_info->scrub_workers.idle_thresh = 4;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002776 ret = btrfs_start_workers(&fs_info->scrub_workers);
2777 if (ret)
2778 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002779 btrfs_init_workers(&fs_info->scrub_wr_completion_workers,
2780 "scrubwrc",
2781 fs_info->thread_pool_size,
2782 &fs_info->generic_worker);
2783 fs_info->scrub_wr_completion_workers.idle_thresh = 2;
2784 ret = btrfs_start_workers(
2785 &fs_info->scrub_wr_completion_workers);
2786 if (ret)
2787 goto out;
2788 btrfs_init_workers(&fs_info->scrub_nocow_workers, "scrubnc", 1,
2789 &fs_info->generic_worker);
2790 ret = btrfs_start_workers(&fs_info->scrub_nocow_workers);
2791 if (ret)
2792 goto out;
Arne Jansen632dd772011-06-10 12:07:07 +02002793 }
Arne Jansena2de7332011-03-08 14:14:00 +01002794 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002795out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05002796 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002797}
2798
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002799static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002800{
Stefan Behrensff023aa2012-11-06 11:43:11 +01002801 if (--fs_info->scrub_workers_refcnt == 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01002802 btrfs_stop_workers(&fs_info->scrub_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002803 btrfs_stop_workers(&fs_info->scrub_wr_completion_workers);
2804 btrfs_stop_workers(&fs_info->scrub_nocow_workers);
2805 }
Arne Jansena2de7332011-03-08 14:14:00 +01002806 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002807}
2808
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002809int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
2810 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01002811 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002812{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002813 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002814 int ret;
2815 struct btrfs_device *dev;
2816
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002817 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01002818 return -EINVAL;
2819
2820 /*
2821 * check some assumptions
2822 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002823 if (fs_info->chunk_root->nodesize != fs_info->chunk_root->leafsize) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002824 printk(KERN_ERR
2825 "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002826 fs_info->chunk_root->nodesize,
2827 fs_info->chunk_root->leafsize);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002828 return -EINVAL;
2829 }
2830
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002831 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002832 /*
2833 * in this case scrub is unable to calculate the checksum
2834 * the way scrub is implemented. Do not handle this
2835 * situation at all because it won't ever happen.
2836 */
2837 printk(KERN_ERR
2838 "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002839 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002840 return -EINVAL;
2841 }
2842
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002843 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002844 /* not supported for data w/o checksums */
2845 printk(KERN_ERR
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02002846 "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails\n",
2847 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01002848 return -EINVAL;
2849 }
2850
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002851 if (fs_info->chunk_root->nodesize >
2852 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
2853 fs_info->chunk_root->sectorsize >
2854 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
2855 /*
2856 * would exhaust the array bounds of pagev member in
2857 * struct scrub_block
2858 */
2859 pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
2860 fs_info->chunk_root->nodesize,
2861 SCRUB_MAX_PAGES_PER_BLOCK,
2862 fs_info->chunk_root->sectorsize,
2863 SCRUB_MAX_PAGES_PER_BLOCK);
2864 return -EINVAL;
2865 }
2866
Arne Jansena2de7332011-03-08 14:14:00 +01002867
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002868 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2869 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002870 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002871 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002872 return -ENODEV;
2873 }
Arne Jansena2de7332011-03-08 14:14:00 +01002874
Wang Shilong3b7a0162013-10-12 02:11:12 +08002875 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002876 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01002877 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002878 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002879 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01002880 }
2881
Stefan Behrens8dabb742012-11-06 13:15:27 +01002882 btrfs_dev_replace_lock(&fs_info->dev_replace);
2883 if (dev->scrub_device ||
2884 (!is_dev_replace &&
2885 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
2886 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002887 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002888 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002889 return -EINPROGRESS;
2890 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01002891 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08002892
2893 ret = scrub_workers_get(fs_info, is_dev_replace);
2894 if (ret) {
2895 mutex_unlock(&fs_info->scrub_lock);
2896 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2897 return ret;
2898 }
2899
Stefan Behrens63a212a2012-11-05 18:29:28 +01002900 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002901 if (IS_ERR(sctx)) {
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);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002905 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002906 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002907 sctx->readonly = readonly;
2908 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08002909 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002910
Wang Shilong3cb09292013-12-04 21:15:19 +08002911 /*
2912 * checking @scrub_pause_req here, we can avoid
2913 * race between committing transaction and scrubbing.
2914 */
2915 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002916 atomic_inc(&fs_info->scrubs_running);
2917 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002918
Stefan Behrensff023aa2012-11-06 11:43:11 +01002919 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08002920 /*
2921 * by holding device list mutex, we can
2922 * kick off writing super in log tree sync.
2923 */
Wang Shilong3cb09292013-12-04 21:15:19 +08002924 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002925 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08002926 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002927 }
Arne Jansena2de7332011-03-08 14:14:00 +01002928
2929 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002930 ret = scrub_enumerate_chunks(sctx, dev, start, end,
2931 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002932
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002933 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002934 atomic_dec(&fs_info->scrubs_running);
2935 wake_up(&fs_info->scrub_pause_wait);
2936
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002937 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02002938
Arne Jansena2de7332011-03-08 14:14:00 +01002939 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002940 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002941
2942 mutex_lock(&fs_info->scrub_lock);
2943 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08002944 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002945 mutex_unlock(&fs_info->scrub_lock);
2946
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002947 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002948
2949 return ret;
2950}
2951
Jeff Mahoney143bede2012-03-01 14:56:26 +01002952void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002953{
2954 struct btrfs_fs_info *fs_info = root->fs_info;
2955
2956 mutex_lock(&fs_info->scrub_lock);
2957 atomic_inc(&fs_info->scrub_pause_req);
2958 while (atomic_read(&fs_info->scrubs_paused) !=
2959 atomic_read(&fs_info->scrubs_running)) {
2960 mutex_unlock(&fs_info->scrub_lock);
2961 wait_event(fs_info->scrub_pause_wait,
2962 atomic_read(&fs_info->scrubs_paused) ==
2963 atomic_read(&fs_info->scrubs_running));
2964 mutex_lock(&fs_info->scrub_lock);
2965 }
2966 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002967}
2968
Jeff Mahoney143bede2012-03-01 14:56:26 +01002969void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002970{
2971 struct btrfs_fs_info *fs_info = root->fs_info;
2972
2973 atomic_dec(&fs_info->scrub_pause_req);
2974 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002975}
2976
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002977int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002978{
Arne Jansena2de7332011-03-08 14:14:00 +01002979 mutex_lock(&fs_info->scrub_lock);
2980 if (!atomic_read(&fs_info->scrubs_running)) {
2981 mutex_unlock(&fs_info->scrub_lock);
2982 return -ENOTCONN;
2983 }
2984
2985 atomic_inc(&fs_info->scrub_cancel_req);
2986 while (atomic_read(&fs_info->scrubs_running)) {
2987 mutex_unlock(&fs_info->scrub_lock);
2988 wait_event(fs_info->scrub_pause_wait,
2989 atomic_read(&fs_info->scrubs_running) == 0);
2990 mutex_lock(&fs_info->scrub_lock);
2991 }
2992 atomic_dec(&fs_info->scrub_cancel_req);
2993 mutex_unlock(&fs_info->scrub_lock);
2994
2995 return 0;
2996}
2997
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002998int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
2999 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003000{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003001 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003002
3003 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003004 sctx = dev->scrub_device;
3005 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003006 mutex_unlock(&fs_info->scrub_lock);
3007 return -ENOTCONN;
3008 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003009 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003010 while (dev->scrub_device) {
3011 mutex_unlock(&fs_info->scrub_lock);
3012 wait_event(fs_info->scrub_pause_wait,
3013 dev->scrub_device == NULL);
3014 mutex_lock(&fs_info->scrub_lock);
3015 }
3016 mutex_unlock(&fs_info->scrub_lock);
3017
3018 return 0;
3019}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003020
Arne Jansena2de7332011-03-08 14:14:00 +01003021int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3022 struct btrfs_scrub_progress *progress)
3023{
3024 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003025 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003026
3027 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003028 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003029 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003030 sctx = dev->scrub_device;
3031 if (sctx)
3032 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003033 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3034
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003035 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003036}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003037
3038static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3039 u64 extent_logical, u64 extent_len,
3040 u64 *extent_physical,
3041 struct btrfs_device **extent_dev,
3042 int *extent_mirror_num)
3043{
3044 u64 mapped_length;
3045 struct btrfs_bio *bbio = NULL;
3046 int ret;
3047
3048 mapped_length = extent_len;
3049 ret = btrfs_map_block(fs_info, READ, extent_logical,
3050 &mapped_length, &bbio, 0);
3051 if (ret || !bbio || mapped_length < extent_len ||
3052 !bbio->stripes[0].dev->bdev) {
3053 kfree(bbio);
3054 return;
3055 }
3056
3057 *extent_physical = bbio->stripes[0].physical;
3058 *extent_mirror_num = bbio->mirror_num;
3059 *extent_dev = bbio->stripes[0].dev;
3060 kfree(bbio);
3061}
3062
3063static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3064 struct scrub_wr_ctx *wr_ctx,
3065 struct btrfs_fs_info *fs_info,
3066 struct btrfs_device *dev,
3067 int is_dev_replace)
3068{
3069 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3070
3071 mutex_init(&wr_ctx->wr_lock);
3072 wr_ctx->wr_curr_bio = NULL;
3073 if (!is_dev_replace)
3074 return 0;
3075
3076 WARN_ON(!dev->bdev);
3077 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3078 bio_get_nr_vecs(dev->bdev));
3079 wr_ctx->tgtdev = dev;
3080 atomic_set(&wr_ctx->flush_all_writes, 0);
3081 return 0;
3082}
3083
3084static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3085{
3086 mutex_lock(&wr_ctx->wr_lock);
3087 kfree(wr_ctx->wr_curr_bio);
3088 wr_ctx->wr_curr_bio = NULL;
3089 mutex_unlock(&wr_ctx->wr_lock);
3090}
3091
3092static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3093 int mirror_num, u64 physical_for_dev_replace)
3094{
3095 struct scrub_copy_nocow_ctx *nocow_ctx;
3096 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3097
3098 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3099 if (!nocow_ctx) {
3100 spin_lock(&sctx->stat_lock);
3101 sctx->stat.malloc_errors++;
3102 spin_unlock(&sctx->stat_lock);
3103 return -ENOMEM;
3104 }
3105
3106 scrub_pending_trans_workers_inc(sctx);
3107
3108 nocow_ctx->sctx = sctx;
3109 nocow_ctx->logical = logical;
3110 nocow_ctx->len = len;
3111 nocow_ctx->mirror_num = mirror_num;
3112 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
3113 nocow_ctx->work.func = copy_nocow_pages_worker;
Josef Bacik652f25a2013-09-12 16:58:28 -04003114 INIT_LIST_HEAD(&nocow_ctx->inodes);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003115 btrfs_queue_worker(&fs_info->scrub_nocow_workers,
3116 &nocow_ctx->work);
3117
3118 return 0;
3119}
3120
Josef Bacik652f25a2013-09-12 16:58:28 -04003121static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3122{
3123 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3124 struct scrub_nocow_inode *nocow_inode;
3125
3126 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3127 if (!nocow_inode)
3128 return -ENOMEM;
3129 nocow_inode->inum = inum;
3130 nocow_inode->offset = offset;
3131 nocow_inode->root = root;
3132 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3133 return 0;
3134}
3135
3136#define COPY_COMPLETE 1
3137
Stefan Behrensff023aa2012-11-06 11:43:11 +01003138static void copy_nocow_pages_worker(struct btrfs_work *work)
3139{
3140 struct scrub_copy_nocow_ctx *nocow_ctx =
3141 container_of(work, struct scrub_copy_nocow_ctx, work);
3142 struct scrub_ctx *sctx = nocow_ctx->sctx;
3143 u64 logical = nocow_ctx->logical;
3144 u64 len = nocow_ctx->len;
3145 int mirror_num = nocow_ctx->mirror_num;
3146 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3147 int ret;
3148 struct btrfs_trans_handle *trans = NULL;
3149 struct btrfs_fs_info *fs_info;
3150 struct btrfs_path *path;
3151 struct btrfs_root *root;
3152 int not_written = 0;
3153
3154 fs_info = sctx->dev_root->fs_info;
3155 root = fs_info->extent_root;
3156
3157 path = btrfs_alloc_path();
3158 if (!path) {
3159 spin_lock(&sctx->stat_lock);
3160 sctx->stat.malloc_errors++;
3161 spin_unlock(&sctx->stat_lock);
3162 not_written = 1;
3163 goto out;
3164 }
3165
3166 trans = btrfs_join_transaction(root);
3167 if (IS_ERR(trans)) {
3168 not_written = 1;
3169 goto out;
3170 }
3171
3172 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04003173 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003174 if (ret != 0 && ret != -ENOENT) {
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02003175 pr_warn("iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d\n",
3176 logical, physical_for_dev_replace, len, mirror_num,
3177 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003178 not_written = 1;
3179 goto out;
3180 }
3181
Josef Bacik652f25a2013-09-12 16:58:28 -04003182 btrfs_end_transaction(trans, root);
3183 trans = NULL;
3184 while (!list_empty(&nocow_ctx->inodes)) {
3185 struct scrub_nocow_inode *entry;
3186 entry = list_first_entry(&nocow_ctx->inodes,
3187 struct scrub_nocow_inode,
3188 list);
3189 list_del_init(&entry->list);
3190 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
3191 entry->root, nocow_ctx);
3192 kfree(entry);
3193 if (ret == COPY_COMPLETE) {
3194 ret = 0;
3195 break;
3196 } else if (ret) {
3197 break;
3198 }
3199 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003200out:
Josef Bacik652f25a2013-09-12 16:58:28 -04003201 while (!list_empty(&nocow_ctx->inodes)) {
3202 struct scrub_nocow_inode *entry;
3203 entry = list_first_entry(&nocow_ctx->inodes,
3204 struct scrub_nocow_inode,
3205 list);
3206 list_del_init(&entry->list);
3207 kfree(entry);
3208 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003209 if (trans && !IS_ERR(trans))
3210 btrfs_end_transaction(trans, root);
3211 if (not_written)
3212 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
3213 num_uncorrectable_read_errors);
3214
3215 btrfs_free_path(path);
3216 kfree(nocow_ctx);
3217
3218 scrub_pending_trans_workers_dec(sctx);
3219}
3220
Josef Bacik652f25a2013-09-12 16:58:28 -04003221static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
3222 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003223{
Miao Xie826aa0a2013-06-27 18:50:59 +08003224 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003225 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08003226 struct inode *inode;
3227 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003228 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04003229 struct btrfs_ordered_extent *ordered;
3230 struct extent_map *em;
3231 struct extent_state *cached_state = NULL;
3232 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003233 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04003234 u64 len = nocow_ctx->len;
3235 u64 lockstart = offset, lockend = offset + len - 1;
Miao Xie826aa0a2013-06-27 18:50:59 +08003236 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00003237 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04003238 int ret = 0;
3239 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003240
3241 key.objectid = root;
3242 key.type = BTRFS_ROOT_ITEM_KEY;
3243 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00003244
3245 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
3246
Stefan Behrensff023aa2012-11-06 11:43:11 +01003247 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00003248 if (IS_ERR(local_root)) {
3249 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003250 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00003251 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003252
3253 key.type = BTRFS_INODE_ITEM_KEY;
3254 key.objectid = inum;
3255 key.offset = 0;
3256 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00003257 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003258 if (IS_ERR(inode))
3259 return PTR_ERR(inode);
3260
Miao Xieedd14002013-06-27 18:51:00 +08003261 /* Avoid truncate/dio/punch hole.. */
3262 mutex_lock(&inode->i_mutex);
3263 inode_dio_wait(inode);
3264
Stefan Behrensff023aa2012-11-06 11:43:11 +01003265 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04003266 io_tree = &BTRFS_I(inode)->io_tree;
3267
3268 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
3269 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
3270 if (ordered) {
3271 btrfs_put_ordered_extent(ordered);
3272 goto out_unlock;
3273 }
3274
3275 em = btrfs_get_extent(inode, NULL, 0, lockstart, len, 0);
3276 if (IS_ERR(em)) {
3277 ret = PTR_ERR(em);
3278 goto out_unlock;
3279 }
3280
3281 /*
3282 * This extent does not actually cover the logical extent anymore,
3283 * move on to the next inode.
3284 */
3285 if (em->block_start > nocow_ctx->logical ||
3286 em->block_start + em->block_len < nocow_ctx->logical + len) {
3287 free_extent_map(em);
3288 goto out_unlock;
3289 }
3290 free_extent_map(em);
3291
Stefan Behrensff023aa2012-11-06 11:43:11 +01003292 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003293 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08003294again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01003295 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3296 if (!page) {
3297 pr_err("find_or_create_page() failed\n");
3298 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08003299 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003300 }
3301
3302 if (PageUptodate(page)) {
3303 if (PageDirty(page))
3304 goto next_page;
3305 } else {
3306 ClearPageError(page);
Josef Bacik652f25a2013-09-12 16:58:28 -04003307 err = extent_read_full_page_nolock(io_tree, page,
3308 btrfs_get_extent,
3309 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08003310 if (err) {
3311 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003312 goto next_page;
3313 }
Miao Xieedd14002013-06-27 18:51:00 +08003314
Miao Xie26b258912013-06-27 18:50:58 +08003315 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08003316 /*
3317 * If the page has been remove from the page cache,
3318 * the data on it is meaningless, because it may be
3319 * old one, the new data may be written into the new
3320 * page in the page cache.
3321 */
3322 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04003323 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08003324 page_cache_release(page);
3325 goto again;
3326 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003327 if (!PageUptodate(page)) {
3328 ret = -EIO;
3329 goto next_page;
3330 }
3331 }
Miao Xie826aa0a2013-06-27 18:50:59 +08003332 err = write_page_nocow(nocow_ctx->sctx,
3333 physical_for_dev_replace, page);
3334 if (err)
3335 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003336next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08003337 unlock_page(page);
3338 page_cache_release(page);
3339
3340 if (ret)
3341 break;
3342
Stefan Behrensff023aa2012-11-06 11:43:11 +01003343 offset += PAGE_CACHE_SIZE;
3344 physical_for_dev_replace += PAGE_CACHE_SIZE;
3345 len -= PAGE_CACHE_SIZE;
3346 }
Josef Bacik652f25a2013-09-12 16:58:28 -04003347 ret = COPY_COMPLETE;
3348out_unlock:
3349 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
3350 GFP_NOFS);
Miao Xie826aa0a2013-06-27 18:50:59 +08003351out:
Miao Xieedd14002013-06-27 18:51:00 +08003352 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08003353 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003354 return ret;
3355}
3356
3357static int write_page_nocow(struct scrub_ctx *sctx,
3358 u64 physical_for_dev_replace, struct page *page)
3359{
3360 struct bio *bio;
3361 struct btrfs_device *dev;
3362 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003363
3364 dev = sctx->wr_ctx.tgtdev;
3365 if (!dev)
3366 return -EIO;
3367 if (!dev->bdev) {
3368 printk_ratelimited(KERN_WARNING
3369 "btrfs: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
3370 return -EIO;
3371 }
Chris Mason9be33952013-05-17 18:30:14 -04003372 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003373 if (!bio) {
3374 spin_lock(&sctx->stat_lock);
3375 sctx->stat.malloc_errors++;
3376 spin_unlock(&sctx->stat_lock);
3377 return -ENOMEM;
3378 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003379 bio->bi_size = 0;
3380 bio->bi_sector = physical_for_dev_replace >> 9;
3381 bio->bi_bdev = dev->bdev;
3382 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
3383 if (ret != PAGE_CACHE_SIZE) {
3384leave_with_eio:
3385 bio_put(bio);
3386 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
3387 return -EIO;
3388 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003389
Kent Overstreetc170bbb2013-11-24 16:32:22 -07003390 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01003391 goto leave_with_eio;
3392
3393 bio_put(bio);
3394 return 0;
3395}