blob: adebe12e497e4f6529542cf2d9302cbea9373a16 [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 Shilongcb7ab022013-12-04 21:16:53 +0800259static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800260static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400261
262
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100263static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
264{
265 atomic_inc(&sctx->bios_in_flight);
266}
267
268static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
269{
270 atomic_dec(&sctx->bios_in_flight);
271 wake_up(&sctx->list_wait);
272}
273
Wang Shilongcb7ab022013-12-04 21:16:53 +0800274static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800275{
276 while (atomic_read(&fs_info->scrub_pause_req)) {
277 mutex_unlock(&fs_info->scrub_lock);
278 wait_event(fs_info->scrub_pause_wait,
279 atomic_read(&fs_info->scrub_pause_req) == 0);
280 mutex_lock(&fs_info->scrub_lock);
281 }
282}
283
Wang Shilongcb7ab022013-12-04 21:16:53 +0800284static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
285{
286 atomic_inc(&fs_info->scrubs_paused);
287 wake_up(&fs_info->scrub_pause_wait);
288
289 mutex_lock(&fs_info->scrub_lock);
290 __scrub_blocked_if_needed(fs_info);
291 atomic_dec(&fs_info->scrubs_paused);
292 mutex_unlock(&fs_info->scrub_lock);
293
294 wake_up(&fs_info->scrub_pause_wait);
295}
296
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100297/*
298 * used for workers that require transaction commits (i.e., for the
299 * NOCOW case)
300 */
301static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
302{
303 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
304
305 /*
306 * increment scrubs_running to prevent cancel requests from
307 * completing as long as a worker is running. we must also
308 * increment scrubs_paused to prevent deadlocking on pause
309 * requests used for transactions commits (as the worker uses a
310 * transaction context). it is safe to regard the worker
311 * as paused for all matters practical. effectively, we only
312 * avoid cancellation requests from completing.
313 */
314 mutex_lock(&fs_info->scrub_lock);
315 atomic_inc(&fs_info->scrubs_running);
316 atomic_inc(&fs_info->scrubs_paused);
317 mutex_unlock(&fs_info->scrub_lock);
318 atomic_inc(&sctx->workers_pending);
319}
320
321/* used for workers that require transaction commits */
322static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
323{
324 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
325
326 /*
327 * see scrub_pending_trans_workers_inc() why we're pretending
328 * to be paused in the scrub counters
329 */
330 mutex_lock(&fs_info->scrub_lock);
331 atomic_dec(&fs_info->scrubs_running);
332 atomic_dec(&fs_info->scrubs_paused);
333 mutex_unlock(&fs_info->scrub_lock);
334 atomic_dec(&sctx->workers_pending);
335 wake_up(&fs_info->scrub_pause_wait);
336 wake_up(&sctx->list_wait);
337}
338
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100339static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100340{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100341 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100342 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100343 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100344 struct btrfs_ordered_sum, list);
345 list_del(&sum->list);
346 kfree(sum);
347 }
348}
349
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100350static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100351{
352 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100353
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100354 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100355 return;
356
Stefan Behrensff023aa2012-11-06 11:43:11 +0100357 scrub_free_wr_ctx(&sctx->wr_ctx);
358
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400359 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100360 if (sctx->curr != -1) {
361 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400362
363 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100364 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400365 scrub_block_put(sbio->pagev[i]->sblock);
366 }
367 bio_put(sbio->bio);
368 }
369
Stefan Behrensff023aa2012-11-06 11:43:11 +0100370 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100371 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100372
373 if (!sbio)
374 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100375 kfree(sbio);
376 }
377
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100378 scrub_free_csums(sctx);
379 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100380}
381
382static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100383struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100384{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100385 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100386 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100387 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100388 int pages_per_rd_bio;
389 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100390
Stefan Behrensff023aa2012-11-06 11:43:11 +0100391 /*
392 * the setting of pages_per_rd_bio is correct for scrub but might
393 * be wrong for the dev_replace code where we might read from
394 * different devices in the initial huge bios. However, that
395 * code is able to correctly handle the case when adding a page
396 * to a bio fails.
397 */
398 if (dev->bdev)
399 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
400 bio_get_nr_vecs(dev->bdev));
401 else
402 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100403 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
404 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100405 goto nomem;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100406 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100407 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100408 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100409 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100410 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100411 struct scrub_bio *sbio;
412
413 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
414 if (!sbio)
415 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100416 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100417
Arne Jansena2de7332011-03-08 14:14:00 +0100418 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100419 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400420 sbio->page_count = 0;
421 sbio->work.func = scrub_bio_end_io_worker;
Arne Jansena2de7332011-03-08 14:14:00 +0100422
Stefan Behrensff023aa2012-11-06 11:43:11 +0100423 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100424 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200425 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100426 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100427 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100428 sctx->first_free = 0;
429 sctx->nodesize = dev->dev_root->nodesize;
430 sctx->leafsize = dev->dev_root->leafsize;
431 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100432 atomic_set(&sctx->bios_in_flight, 0);
433 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100434 atomic_set(&sctx->cancel_req, 0);
435 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
436 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100437
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100438 spin_lock_init(&sctx->list_lock);
439 spin_lock_init(&sctx->stat_lock);
440 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100441
442 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
443 fs_info->dev_replace.tgtdev, is_dev_replace);
444 if (ret) {
445 scrub_free_ctx(sctx);
446 return ERR_PTR(ret);
447 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100448 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100449
450nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100451 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100452 return ERR_PTR(-ENOMEM);
453}
454
Stefan Behrensff023aa2012-11-06 11:43:11 +0100455static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
456 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200457{
458 u64 isize;
459 u32 nlink;
460 int ret;
461 int i;
462 struct extent_buffer *eb;
463 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100464 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200465 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
466 struct inode_fs_paths *ipath = NULL;
467 struct btrfs_root *local_root;
468 struct btrfs_key root_key;
469
470 root_key.objectid = root;
471 root_key.type = BTRFS_ROOT_ITEM_KEY;
472 root_key.offset = (u64)-1;
473 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
474 if (IS_ERR(local_root)) {
475 ret = PTR_ERR(local_root);
476 goto err;
477 }
478
479 ret = inode_item_info(inum, 0, local_root, swarn->path);
480 if (ret) {
481 btrfs_release_path(swarn->path);
482 goto err;
483 }
484
485 eb = swarn->path->nodes[0];
486 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
487 struct btrfs_inode_item);
488 isize = btrfs_inode_size(eb, inode_item);
489 nlink = btrfs_inode_nlink(eb, inode_item);
490 btrfs_release_path(swarn->path);
491
492 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300493 if (IS_ERR(ipath)) {
494 ret = PTR_ERR(ipath);
495 ipath = NULL;
496 goto err;
497 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200498 ret = paths_from_inode(inum, ipath);
499
500 if (ret < 0)
501 goto err;
502
503 /*
504 * we deliberately ignore the bit ipath might have been too small to
505 * hold all of the paths here
506 */
507 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Josef Bacik606686e2012-06-04 14:03:51 -0400508 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200509 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
510 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400511 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200512 (unsigned long long)swarn->sector, root, inum, offset,
513 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500514 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200515
516 free_ipath(ipath);
517 return 0;
518
519err:
Josef Bacik606686e2012-06-04 14:03:51 -0400520 printk_in_rcu(KERN_WARNING "btrfs: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200521 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
522 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400523 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200524 (unsigned long long)swarn->sector, root, inum, offset, ret);
525
526 free_ipath(ipath);
527 return 0;
528}
529
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400530static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200531{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100532 struct btrfs_device *dev;
533 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200534 struct btrfs_path *path;
535 struct btrfs_key found_key;
536 struct extent_buffer *eb;
537 struct btrfs_extent_item *ei;
538 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200539 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100540 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600541 u64 flags = 0;
542 u64 ref_root;
543 u32 item_size;
544 u8 ref_level;
545 const int bufsize = 4096;
546 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200547
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100548 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100549 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100550 fs_info = sblock->sctx->dev_root->fs_info;
551
Jan Schmidt558540c2011-06-13 19:59:12 +0200552 path = btrfs_alloc_path();
553
554 swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
555 swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100556 swarn.sector = (sblock->pagev[0]->physical) >> 9;
557 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200558 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100559 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200560 swarn.msg_bufsize = bufsize;
561 swarn.scratch_bufsize = bufsize;
562
563 if (!path || !swarn.scratch_buf || !swarn.msg_buf)
564 goto out;
565
Liu Bo69917e42012-09-07 20:01:28 -0600566 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
567 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200568 if (ret < 0)
569 goto out;
570
Jan Schmidt4692cf52011-12-02 14:56:41 +0100571 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200572 swarn.extent_item_size = found_key.offset;
573
574 eb = path->nodes[0];
575 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
576 item_size = btrfs_item_size_nr(eb, path->slots[0]);
577
Liu Bo69917e42012-09-07 20:01:28 -0600578 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200579 do {
580 ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
581 &ref_root, &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400582 printk_in_rcu(KERN_WARNING
Stefan Behrens1623ede2012-03-27 14:21:26 -0400583 "btrfs: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200584 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400585 "%llu\n", errstr, swarn.logical,
586 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200587 (unsigned long long)swarn.sector,
588 ref_level ? "node" : "leaf",
589 ret < 0 ? -1 : ref_level,
590 ret < 0 ? -1 : ref_root);
591 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600592 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200593 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600594 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200595 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100596 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100597 iterate_extent_inodes(fs_info, found_key.objectid,
598 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200599 scrub_print_warning_inode, &swarn);
600 }
601
602out:
603 btrfs_free_path(path);
604 kfree(swarn.scratch_buf);
605 kfree(swarn.msg_buf);
606}
607
Stefan Behrensff023aa2012-11-06 11:43:11 +0100608static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200609{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200610 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200611 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100612 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200613 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200614 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200615 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200616 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000617 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200618 u64 end = offset + PAGE_SIZE - 1;
619 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000620 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200621
622 key.objectid = root;
623 key.type = BTRFS_ROOT_ITEM_KEY;
624 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000625
626 fs_info = fixup->root->fs_info;
627 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
628
629 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
630 if (IS_ERR(local_root)) {
631 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200632 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000633 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200634
635 key.type = BTRFS_INODE_ITEM_KEY;
636 key.objectid = inum;
637 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000638 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
639 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200640 if (IS_ERR(inode))
641 return PTR_ERR(inode);
642
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200643 index = offset >> PAGE_CACHE_SHIFT;
644
645 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200646 if (!page) {
647 ret = -ENOMEM;
648 goto out;
649 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200650
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200651 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200652 if (PageDirty(page)) {
653 /*
654 * we need to write the data to the defect sector. the
655 * data that was in that sector is not in memory,
656 * because the page was modified. we must not write the
657 * modified page to that sector.
658 *
659 * TODO: what could be done here: wait for the delalloc
660 * runner to write out that page (might involve
661 * COW) and see whether the sector is still
662 * referenced afterwards.
663 *
664 * For the meantime, we'll treat this error
665 * incorrectable, although there is a chance that a
666 * later scrub will find the bad sector again and that
667 * there's no dirty page in memory, then.
668 */
669 ret = -EIO;
670 goto out;
671 }
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100672 fs_info = BTRFS_I(inode)->root->fs_info;
673 ret = repair_io_failure(fs_info, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200674 fixup->logical, page,
675 fixup->mirror_num);
676 unlock_page(page);
677 corrected = !ret;
678 } else {
679 /*
680 * we need to get good data first. the general readpage path
681 * will call repair_io_failure for us, we just have to make
682 * sure we read the bad mirror.
683 */
684 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
685 EXTENT_DAMAGED, GFP_NOFS);
686 if (ret) {
687 /* set_extent_bits should give proper error */
688 WARN_ON(ret > 0);
689 if (ret > 0)
690 ret = -EFAULT;
691 goto out;
692 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200693
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200694 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
695 btrfs_get_extent,
696 fixup->mirror_num);
697 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200698
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200699 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
700 end, EXTENT_DAMAGED, 0, NULL);
701 if (!corrected)
702 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
703 EXTENT_DAMAGED, GFP_NOFS);
704 }
705
706out:
707 if (page)
708 put_page(page);
709 if (inode)
710 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200711
712 if (ret < 0)
713 return ret;
714
715 if (ret == 0 && corrected) {
716 /*
717 * we only need to call readpage for one of the inodes belonging
718 * to this extent. so make iterate_extent_inodes stop
719 */
720 return 1;
721 }
722
723 return -EIO;
724}
725
726static void scrub_fixup_nodatasum(struct btrfs_work *work)
727{
728 int ret;
729 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100730 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200731 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200732 struct btrfs_path *path;
733 int uncorrectable = 0;
734
735 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100736 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200737
738 path = btrfs_alloc_path();
739 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100740 spin_lock(&sctx->stat_lock);
741 ++sctx->stat.malloc_errors;
742 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200743 uncorrectable = 1;
744 goto out;
745 }
746
747 trans = btrfs_join_transaction(fixup->root);
748 if (IS_ERR(trans)) {
749 uncorrectable = 1;
750 goto out;
751 }
752
753 /*
754 * the idea is to trigger a regular read through the standard path. we
755 * read a page from the (failed) logical address by specifying the
756 * corresponding copynum of the failed sector. thus, that readpage is
757 * expected to fail.
758 * that is the point where on-the-fly error correction will kick in
759 * (once it's finished) and rewrite the failed sector if a good copy
760 * can be found.
761 */
762 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
763 path, scrub_fixup_readpage,
764 fixup);
765 if (ret < 0) {
766 uncorrectable = 1;
767 goto out;
768 }
769 WARN_ON(ret != 1);
770
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100771 spin_lock(&sctx->stat_lock);
772 ++sctx->stat.corrected_errors;
773 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200774
775out:
776 if (trans && !IS_ERR(trans))
777 btrfs_end_transaction(trans, fixup->root);
778 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100779 spin_lock(&sctx->stat_lock);
780 ++sctx->stat.uncorrectable_errors;
781 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100782 btrfs_dev_replace_stats_inc(
783 &sctx->dev_root->fs_info->dev_replace.
784 num_uncorrectable_read_errors);
Josef Bacik606686e2012-06-04 14:03:51 -0400785 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400786 "btrfs: unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200787 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200788 }
789
790 btrfs_free_path(path);
791 kfree(fixup);
792
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100793 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200794}
795
Arne Jansena2de7332011-03-08 14:14:00 +0100796/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400797 * scrub_handle_errored_block gets called when either verification of the
798 * pages failed or the bio failed to read, e.g. with EIO. In the latter
799 * case, this function handles all pages in the bio, even though only one
800 * may be bad.
801 * The goal of this function is to repair the errored block by using the
802 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100803 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400804static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100805{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100806 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100807 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400808 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100809 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400810 u64 logical;
811 u64 generation;
812 unsigned int failed_mirror_index;
813 unsigned int is_metadata;
814 unsigned int have_csum;
815 u8 *csum;
816 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
817 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100818 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400819 int mirror_index;
820 int page_num;
821 int success;
822 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
823 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100824
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400825 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100826 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000827 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
828 /*
829 * if we find an error in a super block, we just report it.
830 * They will get written with the next transaction commit
831 * anyway
832 */
833 spin_lock(&sctx->stat_lock);
834 ++sctx->stat.super_errors;
835 spin_unlock(&sctx->stat_lock);
836 return 0;
837 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400838 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100839 logical = sblock_to_check->pagev[0]->logical;
840 generation = sblock_to_check->pagev[0]->generation;
841 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
842 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
843 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400844 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100845 have_csum = sblock_to_check->pagev[0]->have_csum;
846 csum = sblock_to_check->pagev[0]->csum;
847 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400848
Stefan Behrensff023aa2012-11-06 11:43:11 +0100849 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
850 sblocks_for_recheck = NULL;
851 goto nodatasum_case;
852 }
853
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400854 /*
855 * read all mirrors one after the other. This includes to
856 * re-read the extent or metadata block that failed (that was
857 * the cause that this fixup code is called) another time,
858 * page by page this time in order to know which pages
859 * caused I/O errors and which ones are good (for all mirrors).
860 * It is the goal to handle the situation when more than one
861 * mirror contains I/O errors, but the errors do not
862 * overlap, i.e. the data can be repaired by selecting the
863 * pages from those mirrors without I/O error on the
864 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
865 * would be that mirror #1 has an I/O error on the first page,
866 * the second page is good, and mirror #2 has an I/O error on
867 * the second page, but the first page is good.
868 * Then the first page of the first mirror can be repaired by
869 * taking the first page of the second mirror, and the
870 * second page of the second mirror can be repaired by
871 * copying the contents of the 2nd page of the 1st mirror.
872 * One more note: if the pages of one mirror contain I/O
873 * errors, the checksum cannot be verified. In order to get
874 * the best data for repairing, the first attempt is to find
875 * a mirror without I/O errors and with a validated checksum.
876 * Only if this is not possible, the pages are picked from
877 * mirrors with I/O errors without considering the checksum.
878 * If the latter is the case, at the end, the checksum of the
879 * repaired area is verified in order to correctly maintain
880 * the statistics.
881 */
882
883 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
884 sizeof(*sblocks_for_recheck),
885 GFP_NOFS);
886 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100887 spin_lock(&sctx->stat_lock);
888 sctx->stat.malloc_errors++;
889 sctx->stat.read_errors++;
890 sctx->stat.uncorrectable_errors++;
891 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100892 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400893 goto out;
894 }
895
896 /* setup the context, map the logical blocks and alloc the pages */
Stefan Behrensff023aa2012-11-06 11:43:11 +0100897 ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400898 logical, sblocks_for_recheck);
899 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100900 spin_lock(&sctx->stat_lock);
901 sctx->stat.read_errors++;
902 sctx->stat.uncorrectable_errors++;
903 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100904 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400905 goto out;
906 }
907 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
908 sblock_bad = sblocks_for_recheck + failed_mirror_index;
909
910 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100911 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
912 csum, generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400913
914 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
915 sblock_bad->no_io_error_seen) {
916 /*
917 * the error disappeared after reading page by page, or
918 * the area was part of a huge bio and other parts of the
919 * bio caused I/O errors, or the block layer merged several
920 * read requests into one and the error is caused by a
921 * different bio (usually one of the two latter cases is
922 * the cause)
923 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100924 spin_lock(&sctx->stat_lock);
925 sctx->stat.unverified_errors++;
926 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400927
Stefan Behrensff023aa2012-11-06 11:43:11 +0100928 if (sctx->is_dev_replace)
929 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400930 goto out;
931 }
932
933 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100934 spin_lock(&sctx->stat_lock);
935 sctx->stat.read_errors++;
936 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400937 if (__ratelimit(&_rs))
938 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100939 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400940 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100941 spin_lock(&sctx->stat_lock);
942 sctx->stat.csum_errors++;
943 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400944 if (__ratelimit(&_rs))
945 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100946 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200947 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400948 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100949 spin_lock(&sctx->stat_lock);
950 sctx->stat.verify_errors++;
951 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400952 if (__ratelimit(&_rs))
953 scrub_print_warning("checksum/header error",
954 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200955 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100956 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200957 BTRFS_DEV_STAT_GENERATION_ERRS);
958 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100959 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200960 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400961 }
962
Ilya Dryomov33ef30a2013-11-03 19:06:38 +0200963 if (sctx->readonly) {
964 ASSERT(!sctx->is_dev_replace);
965 goto out;
966 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400967
968 if (!is_metadata && !have_csum) {
969 struct scrub_fixup_nodatasum *fixup_nodatasum;
970
Stefan Behrensff023aa2012-11-06 11:43:11 +0100971nodatasum_case:
972 WARN_ON(sctx->is_dev_replace);
973
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400974 /*
975 * !is_metadata and !have_csum, this means that the data
976 * might not be COW'ed, that it might be modified
977 * concurrently. The general strategy to work on the
978 * commit root does not help in the case when COW is not
979 * used.
980 */
981 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
982 if (!fixup_nodatasum)
983 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100984 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100985 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400986 fixup_nodatasum->logical = logical;
987 fixup_nodatasum->root = fs_info->extent_root;
988 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100989 scrub_pending_trans_workers_inc(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400990 fixup_nodatasum->work.func = scrub_fixup_nodatasum;
991 btrfs_queue_worker(&fs_info->scrub_workers,
992 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +0100993 goto out;
994 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400995
996 /*
997 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100998 * checksums.
999 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001000 * errors and also does not have a checksum error.
1001 * If one is found, and if a checksum is present, the full block
1002 * that is known to contain an error is rewritten. Afterwards
1003 * the block is known to be corrected.
1004 * If a mirror is found which is completely correct, and no
1005 * checksum is present, only those pages are rewritten that had
1006 * an I/O error in the block to be repaired, since it cannot be
1007 * determined, which copy of the other pages is better (and it
1008 * could happen otherwise that a correct page would be
1009 * overwritten by a bad one).
1010 */
1011 for (mirror_index = 0;
1012 mirror_index < BTRFS_MAX_MIRRORS &&
1013 sblocks_for_recheck[mirror_index].page_count > 0;
1014 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001015 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001016
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001017 if (mirror_index == failed_mirror_index)
1018 continue;
1019 sblock_other = sblocks_for_recheck + mirror_index;
1020
1021 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001022 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1023 have_csum, csum, generation,
1024 sctx->csum_size);
1025
1026 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001027 !sblock_other->checksum_error &&
1028 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001029 if (sctx->is_dev_replace) {
1030 scrub_write_block_to_dev_replace(sblock_other);
1031 } else {
1032 int force_write = is_metadata || have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001033
Stefan Behrensff023aa2012-11-06 11:43:11 +01001034 ret = scrub_repair_block_from_good_copy(
1035 sblock_bad, sblock_other,
1036 force_write);
1037 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001038 if (0 == ret)
1039 goto corrected_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001040 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001041 }
1042
1043 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001044 * for dev_replace, pick good pages and write to the target device.
1045 */
1046 if (sctx->is_dev_replace) {
1047 success = 1;
1048 for (page_num = 0; page_num < sblock_bad->page_count;
1049 page_num++) {
1050 int sub_success;
1051
1052 sub_success = 0;
1053 for (mirror_index = 0;
1054 mirror_index < BTRFS_MAX_MIRRORS &&
1055 sblocks_for_recheck[mirror_index].page_count > 0;
1056 mirror_index++) {
1057 struct scrub_block *sblock_other =
1058 sblocks_for_recheck + mirror_index;
1059 struct scrub_page *page_other =
1060 sblock_other->pagev[page_num];
1061
1062 if (!page_other->io_error) {
1063 ret = scrub_write_page_to_dev_replace(
1064 sblock_other, page_num);
1065 if (ret == 0) {
1066 /* succeeded for this page */
1067 sub_success = 1;
1068 break;
1069 } else {
1070 btrfs_dev_replace_stats_inc(
1071 &sctx->dev_root->
1072 fs_info->dev_replace.
1073 num_write_errors);
1074 }
1075 }
1076 }
1077
1078 if (!sub_success) {
1079 /*
1080 * did not find a mirror to fetch the page
1081 * from. scrub_write_page_to_dev_replace()
1082 * handles this case (page->io_error), by
1083 * filling the block with zeros before
1084 * submitting the write request
1085 */
1086 success = 0;
1087 ret = scrub_write_page_to_dev_replace(
1088 sblock_bad, page_num);
1089 if (ret)
1090 btrfs_dev_replace_stats_inc(
1091 &sctx->dev_root->fs_info->
1092 dev_replace.num_write_errors);
1093 }
1094 }
1095
1096 goto out;
1097 }
1098
1099 /*
1100 * for regular scrub, repair those pages that are errored.
1101 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001102 * repaired, continue by picking good copies of those pages.
1103 * Select the good pages from mirrors to rewrite bad pages from
1104 * the area to fix. Afterwards verify the checksum of the block
1105 * that is supposed to be repaired. This verification step is
1106 * only done for the purpose of statistic counting and for the
1107 * final scrub report, whether errors remain.
1108 * A perfect algorithm could make use of the checksum and try
1109 * all possible combinations of pages from the different mirrors
1110 * until the checksum verification succeeds. For example, when
1111 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1112 * of mirror #2 is readable but the final checksum test fails,
1113 * then the 2nd page of mirror #3 could be tried, whether now
1114 * the final checksum succeedes. But this would be a rare
1115 * exception and is therefore not implemented. At least it is
1116 * avoided that the good copy is overwritten.
1117 * A more useful improvement would be to pick the sectors
1118 * without I/O error based on sector sizes (512 bytes on legacy
1119 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1120 * mirror could be repaired by taking 512 byte of a different
1121 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1122 * area are unreadable.
1123 */
1124
1125 /* can only fix I/O errors from here on */
1126 if (sblock_bad->no_io_error_seen)
1127 goto did_not_correct_error;
1128
1129 success = 1;
1130 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001131 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001132
1133 if (!page_bad->io_error)
1134 continue;
1135
1136 for (mirror_index = 0;
1137 mirror_index < BTRFS_MAX_MIRRORS &&
1138 sblocks_for_recheck[mirror_index].page_count > 0;
1139 mirror_index++) {
1140 struct scrub_block *sblock_other = sblocks_for_recheck +
1141 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001142 struct scrub_page *page_other = sblock_other->pagev[
1143 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001144
1145 if (!page_other->io_error) {
1146 ret = scrub_repair_page_from_good_copy(
1147 sblock_bad, sblock_other, page_num, 0);
1148 if (0 == ret) {
1149 page_bad->io_error = 0;
1150 break; /* succeeded for this page */
1151 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001152 }
1153 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001154
1155 if (page_bad->io_error) {
1156 /* did not find a mirror to copy the page from */
1157 success = 0;
1158 }
1159 }
1160
1161 if (success) {
1162 if (is_metadata || have_csum) {
1163 /*
1164 * need to verify the checksum now that all
1165 * sectors on disk are repaired (the write
1166 * request for data to be repaired is on its way).
1167 * Just be lazy and use scrub_recheck_block()
1168 * which re-reads the data before the checksum
1169 * is verified, but most likely the data comes out
1170 * of the page cache.
1171 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001172 scrub_recheck_block(fs_info, sblock_bad,
1173 is_metadata, have_csum, csum,
1174 generation, sctx->csum_size);
1175 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001176 !sblock_bad->checksum_error &&
1177 sblock_bad->no_io_error_seen)
1178 goto corrected_error;
1179 else
1180 goto did_not_correct_error;
1181 } else {
1182corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001183 spin_lock(&sctx->stat_lock);
1184 sctx->stat.corrected_errors++;
1185 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001186 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001187 "btrfs: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001188 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001189 }
1190 } else {
1191did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001192 spin_lock(&sctx->stat_lock);
1193 sctx->stat.uncorrectable_errors++;
1194 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001195 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001196 "btrfs: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001197 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001198 }
1199
1200out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001201 if (sblocks_for_recheck) {
1202 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1203 mirror_index++) {
1204 struct scrub_block *sblock = sblocks_for_recheck +
1205 mirror_index;
1206 int page_index;
1207
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001208 for (page_index = 0; page_index < sblock->page_count;
1209 page_index++) {
1210 sblock->pagev[page_index]->sblock = NULL;
1211 scrub_page_put(sblock->pagev[page_index]);
1212 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001213 }
1214 kfree(sblocks_for_recheck);
1215 }
1216
1217 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001218}
1219
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001220static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001221 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001222 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001223 u64 length, u64 logical,
1224 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001225{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001226 int page_index;
1227 int mirror_index;
1228 int ret;
1229
1230 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001231 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001232 * are not used (and not set) in the blocks that are used for
1233 * the recheck procedure
1234 */
1235
1236 page_index = 0;
1237 while (length > 0) {
1238 u64 sublen = min_t(u64, length, PAGE_SIZE);
1239 u64 mapped_length = sublen;
1240 struct btrfs_bio *bbio = NULL;
1241
1242 /*
1243 * with a length of PAGE_SIZE, each returned stripe
1244 * represents one mirror
1245 */
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01001246 ret = btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, logical,
1247 &mapped_length, &bbio, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001248 if (ret || !bbio || mapped_length < sublen) {
1249 kfree(bbio);
1250 return -EIO;
1251 }
1252
Stefan Behrensff023aa2012-11-06 11:43:11 +01001253 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001254 for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
1255 mirror_index++) {
1256 struct scrub_block *sblock;
1257 struct scrub_page *page;
1258
1259 if (mirror_index >= BTRFS_MAX_MIRRORS)
1260 continue;
1261
1262 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001263 sblock->sctx = sctx;
1264 page = kzalloc(sizeof(*page), GFP_NOFS);
1265 if (!page) {
1266leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001267 spin_lock(&sctx->stat_lock);
1268 sctx->stat.malloc_errors++;
1269 spin_unlock(&sctx->stat_lock);
Wei Yongjuncf93dcc2012-09-02 07:44:51 -06001270 kfree(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001271 return -ENOMEM;
1272 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001273 scrub_page_get(page);
1274 sblock->pagev[page_index] = page;
1275 page->logical = logical;
1276 page->physical = bbio->stripes[mirror_index].physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001277 BUG_ON(page_index >= original_sblock->page_count);
1278 page->physical_for_dev_replace =
1279 original_sblock->pagev[page_index]->
1280 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001281 /* for missing devices, dev->bdev is NULL */
1282 page->dev = bbio->stripes[mirror_index].dev;
1283 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001284 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001285 page->page = alloc_page(GFP_NOFS);
1286 if (!page->page)
1287 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001288 }
1289 kfree(bbio);
1290 length -= sublen;
1291 logical += sublen;
1292 page_index++;
1293 }
1294
1295 return 0;
1296}
1297
1298/*
1299 * this function will check the on disk data for checksum errors, header
1300 * errors and read I/O errors. If any I/O errors happen, the exact pages
1301 * which are errored are marked as being bad. The goal is to enable scrub
1302 * to take those pages that are not errored from all the mirrors so that
1303 * the pages that are errored in the just handled mirror can be repaired.
1304 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001305static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1306 struct scrub_block *sblock, int is_metadata,
1307 int have_csum, u8 *csum, u64 generation,
1308 u16 csum_size)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001309{
1310 int page_num;
1311
1312 sblock->no_io_error_seen = 1;
1313 sblock->header_error = 0;
1314 sblock->checksum_error = 0;
1315
1316 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1317 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001318 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001319
Stefan Behrens442a4f62012-05-25 16:06:08 +02001320 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001321 page->io_error = 1;
1322 sblock->no_io_error_seen = 0;
1323 continue;
1324 }
1325
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001326 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001327 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001328 if (!bio) {
1329 page->io_error = 1;
1330 sblock->no_io_error_seen = 0;
1331 continue;
1332 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001333 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001334 bio->bi_sector = page->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001335
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001336 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -07001337 if (btrfsic_submit_bio_wait(READ, bio))
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001338 sblock->no_io_error_seen = 0;
Kent Overstreetc170bbb2013-11-24 16:32:22 -07001339
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001340 bio_put(bio);
1341 }
1342
1343 if (sblock->no_io_error_seen)
1344 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1345 have_csum, csum, generation,
1346 csum_size);
1347
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001348 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001349}
1350
1351static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1352 struct scrub_block *sblock,
1353 int is_metadata, int have_csum,
1354 const u8 *csum, u64 generation,
1355 u16 csum_size)
1356{
1357 int page_num;
1358 u8 calculated_csum[BTRFS_CSUM_SIZE];
1359 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001360 void *mapped_buffer;
1361
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001362 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001363 if (is_metadata) {
1364 struct btrfs_header *h;
1365
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001366 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001367 h = (struct btrfs_header *)mapped_buffer;
1368
Qu Wenruo3cae2102013-07-16 11:19:18 +08001369 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001370 memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
1371 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001372 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001373 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001374 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001375 sblock->header_error = 1;
1376 sblock->generation_error = 1;
1377 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001378 csum = h->csum;
1379 } else {
1380 if (!have_csum)
1381 return;
1382
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001383 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001384 }
1385
1386 for (page_num = 0;;) {
1387 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001388 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001389 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1390 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1391 else
Liu Bob0496682013-03-14 14:57:45 +00001392 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001393
Linus Torvalds9613beb2012-03-30 12:44:29 -07001394 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001395 page_num++;
1396 if (page_num >= sblock->page_count)
1397 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001398 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001399
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001400 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001401 }
1402
1403 btrfs_csum_final(crc, calculated_csum);
1404 if (memcmp(calculated_csum, csum, csum_size))
1405 sblock->checksum_error = 1;
1406}
1407
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001408static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1409 struct scrub_block *sblock_good,
1410 int force_write)
1411{
1412 int page_num;
1413 int ret = 0;
1414
1415 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1416 int ret_sub;
1417
1418 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1419 sblock_good,
1420 page_num,
1421 force_write);
1422 if (ret_sub)
1423 ret = ret_sub;
1424 }
1425
1426 return ret;
1427}
1428
1429static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1430 struct scrub_block *sblock_good,
1431 int page_num, int force_write)
1432{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001433 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1434 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001435
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001436 BUG_ON(page_bad->page == NULL);
1437 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001438 if (force_write || sblock_bad->header_error ||
1439 sblock_bad->checksum_error || page_bad->io_error) {
1440 struct bio *bio;
1441 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001442
Stefan Behrensff023aa2012-11-06 11:43:11 +01001443 if (!page_bad->dev->bdev) {
1444 printk_ratelimited(KERN_WARNING
1445 "btrfs: scrub_repair_page_from_good_copy(bdev == NULL) is unexpected!\n");
1446 return -EIO;
1447 }
1448
Chris Mason9be33952013-05-17 18:30:14 -04001449 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001450 if (!bio)
1451 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001452 bio->bi_bdev = page_bad->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001453 bio->bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001454
1455 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1456 if (PAGE_SIZE != ret) {
1457 bio_put(bio);
1458 return -EIO;
1459 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001460
Kent Overstreetc170bbb2013-11-24 16:32:22 -07001461 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001462 btrfs_dev_stat_inc_and_print(page_bad->dev,
1463 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001464 btrfs_dev_replace_stats_inc(
1465 &sblock_bad->sctx->dev_root->fs_info->
1466 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001467 bio_put(bio);
1468 return -EIO;
1469 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001470 bio_put(bio);
1471 }
1472
1473 return 0;
1474}
1475
Stefan Behrensff023aa2012-11-06 11:43:11 +01001476static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1477{
1478 int page_num;
1479
1480 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1481 int ret;
1482
1483 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1484 if (ret)
1485 btrfs_dev_replace_stats_inc(
1486 &sblock->sctx->dev_root->fs_info->dev_replace.
1487 num_write_errors);
1488 }
1489}
1490
1491static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1492 int page_num)
1493{
1494 struct scrub_page *spage = sblock->pagev[page_num];
1495
1496 BUG_ON(spage->page == NULL);
1497 if (spage->io_error) {
1498 void *mapped_buffer = kmap_atomic(spage->page);
1499
1500 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1501 flush_dcache_page(spage->page);
1502 kunmap_atomic(mapped_buffer);
1503 }
1504 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1505}
1506
1507static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1508 struct scrub_page *spage)
1509{
1510 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1511 struct scrub_bio *sbio;
1512 int ret;
1513
1514 mutex_lock(&wr_ctx->wr_lock);
1515again:
1516 if (!wr_ctx->wr_curr_bio) {
1517 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1518 GFP_NOFS);
1519 if (!wr_ctx->wr_curr_bio) {
1520 mutex_unlock(&wr_ctx->wr_lock);
1521 return -ENOMEM;
1522 }
1523 wr_ctx->wr_curr_bio->sctx = sctx;
1524 wr_ctx->wr_curr_bio->page_count = 0;
1525 }
1526 sbio = wr_ctx->wr_curr_bio;
1527 if (sbio->page_count == 0) {
1528 struct bio *bio;
1529
1530 sbio->physical = spage->physical_for_dev_replace;
1531 sbio->logical = spage->logical;
1532 sbio->dev = wr_ctx->tgtdev;
1533 bio = sbio->bio;
1534 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001535 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001536 if (!bio) {
1537 mutex_unlock(&wr_ctx->wr_lock);
1538 return -ENOMEM;
1539 }
1540 sbio->bio = bio;
1541 }
1542
1543 bio->bi_private = sbio;
1544 bio->bi_end_io = scrub_wr_bio_end_io;
1545 bio->bi_bdev = sbio->dev->bdev;
1546 bio->bi_sector = sbio->physical >> 9;
1547 sbio->err = 0;
1548 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1549 spage->physical_for_dev_replace ||
1550 sbio->logical + sbio->page_count * PAGE_SIZE !=
1551 spage->logical) {
1552 scrub_wr_submit(sctx);
1553 goto again;
1554 }
1555
1556 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1557 if (ret != PAGE_SIZE) {
1558 if (sbio->page_count < 1) {
1559 bio_put(sbio->bio);
1560 sbio->bio = NULL;
1561 mutex_unlock(&wr_ctx->wr_lock);
1562 return -EIO;
1563 }
1564 scrub_wr_submit(sctx);
1565 goto again;
1566 }
1567
1568 sbio->pagev[sbio->page_count] = spage;
1569 scrub_page_get(spage);
1570 sbio->page_count++;
1571 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1572 scrub_wr_submit(sctx);
1573 mutex_unlock(&wr_ctx->wr_lock);
1574
1575 return 0;
1576}
1577
1578static void scrub_wr_submit(struct scrub_ctx *sctx)
1579{
1580 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1581 struct scrub_bio *sbio;
1582
1583 if (!wr_ctx->wr_curr_bio)
1584 return;
1585
1586 sbio = wr_ctx->wr_curr_bio;
1587 wr_ctx->wr_curr_bio = NULL;
1588 WARN_ON(!sbio->bio->bi_bdev);
1589 scrub_pending_bio_inc(sctx);
1590 /* process all writes in a single worker thread. Then the block layer
1591 * orders the requests before sending them to the driver which
1592 * doubled the write performance on spinning disks when measured
1593 * with Linux 3.5 */
1594 btrfsic_submit_bio(WRITE, sbio->bio);
1595}
1596
1597static void scrub_wr_bio_end_io(struct bio *bio, int err)
1598{
1599 struct scrub_bio *sbio = bio->bi_private;
1600 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1601
1602 sbio->err = err;
1603 sbio->bio = bio;
1604
1605 sbio->work.func = scrub_wr_bio_end_io_worker;
1606 btrfs_queue_worker(&fs_info->scrub_wr_completion_workers, &sbio->work);
1607}
1608
1609static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1610{
1611 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1612 struct scrub_ctx *sctx = sbio->sctx;
1613 int i;
1614
1615 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1616 if (sbio->err) {
1617 struct btrfs_dev_replace *dev_replace =
1618 &sbio->sctx->dev_root->fs_info->dev_replace;
1619
1620 for (i = 0; i < sbio->page_count; i++) {
1621 struct scrub_page *spage = sbio->pagev[i];
1622
1623 spage->io_error = 1;
1624 btrfs_dev_replace_stats_inc(&dev_replace->
1625 num_write_errors);
1626 }
1627 }
1628
1629 for (i = 0; i < sbio->page_count; i++)
1630 scrub_page_put(sbio->pagev[i]);
1631
1632 bio_put(sbio->bio);
1633 kfree(sbio);
1634 scrub_pending_bio_dec(sctx);
1635}
1636
1637static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001638{
1639 u64 flags;
1640 int ret;
1641
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001642 WARN_ON(sblock->page_count < 1);
1643 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001644 ret = 0;
1645 if (flags & BTRFS_EXTENT_FLAG_DATA)
1646 ret = scrub_checksum_data(sblock);
1647 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1648 ret = scrub_checksum_tree_block(sblock);
1649 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1650 (void)scrub_checksum_super(sblock);
1651 else
1652 WARN_ON(1);
1653 if (ret)
1654 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001655
1656 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001657}
1658
1659static int scrub_checksum_data(struct scrub_block *sblock)
1660{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001661 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001662 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001663 u8 *on_disk_csum;
1664 struct page *page;
1665 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001666 u32 crc = ~(u32)0;
1667 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001668 u64 len;
1669 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001670
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001671 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001672 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001673 return 0;
1674
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001675 on_disk_csum = sblock->pagev[0]->csum;
1676 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001677 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001678
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001679 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001680 index = 0;
1681 for (;;) {
1682 u64 l = min_t(u64, len, PAGE_SIZE);
1683
Liu Bob0496682013-03-14 14:57:45 +00001684 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001685 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001686 len -= l;
1687 if (len == 0)
1688 break;
1689 index++;
1690 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001691 BUG_ON(!sblock->pagev[index]->page);
1692 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001693 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001694 }
1695
Arne Jansena2de7332011-03-08 14:14:00 +01001696 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001697 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001698 fail = 1;
1699
Arne Jansena2de7332011-03-08 14:14:00 +01001700 return fail;
1701}
1702
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001703static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001704{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001705 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001706 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001707 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001708 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001709 u8 calculated_csum[BTRFS_CSUM_SIZE];
1710 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1711 struct page *page;
1712 void *mapped_buffer;
1713 u64 mapped_size;
1714 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001715 u32 crc = ~(u32)0;
1716 int fail = 0;
1717 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001718 u64 len;
1719 int index;
1720
1721 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001722 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001723 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001724 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001725 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001726
1727 /*
1728 * we don't use the getter functions here, as we
1729 * a) don't have an extent buffer and
1730 * b) the page is already kmapped
1731 */
Arne Jansena2de7332011-03-08 14:14:00 +01001732
Qu Wenruo3cae2102013-07-16 11:19:18 +08001733 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001734 ++fail;
1735
Qu Wenruo3cae2102013-07-16 11:19:18 +08001736 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001737 ++fail;
1738
1739 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1740 ++fail;
1741
1742 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1743 BTRFS_UUID_SIZE))
1744 ++fail;
1745
Stefan Behrensff023aa2012-11-06 11:43:11 +01001746 WARN_ON(sctx->nodesize != sctx->leafsize);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001747 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001748 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1749 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1750 index = 0;
1751 for (;;) {
1752 u64 l = min_t(u64, len, mapped_size);
1753
Liu Bob0496682013-03-14 14:57:45 +00001754 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001755 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001756 len -= l;
1757 if (len == 0)
1758 break;
1759 index++;
1760 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001761 BUG_ON(!sblock->pagev[index]->page);
1762 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001763 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001764 mapped_size = PAGE_SIZE;
1765 p = mapped_buffer;
1766 }
1767
1768 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001769 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001770 ++crc_fail;
1771
Arne Jansena2de7332011-03-08 14:14:00 +01001772 return fail || crc_fail;
1773}
1774
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001775static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001776{
1777 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001778 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001779 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001780 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001781 u8 calculated_csum[BTRFS_CSUM_SIZE];
1782 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1783 struct page *page;
1784 void *mapped_buffer;
1785 u64 mapped_size;
1786 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001787 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001788 int fail_gen = 0;
1789 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001790 u64 len;
1791 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001792
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001793 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001794 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001795 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001796 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001797 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001798
Qu Wenruo3cae2102013-07-16 11:19:18 +08001799 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001800 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001801
Qu Wenruo3cae2102013-07-16 11:19:18 +08001802 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001803 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001804
1805 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001806 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001807
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001808 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1809 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1810 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1811 index = 0;
1812 for (;;) {
1813 u64 l = min_t(u64, len, mapped_size);
1814
Liu Bob0496682013-03-14 14:57:45 +00001815 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001816 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001817 len -= l;
1818 if (len == 0)
1819 break;
1820 index++;
1821 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001822 BUG_ON(!sblock->pagev[index]->page);
1823 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001824 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001825 mapped_size = PAGE_SIZE;
1826 p = mapped_buffer;
1827 }
1828
1829 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001830 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001831 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001832
Stefan Behrens442a4f62012-05-25 16:06:08 +02001833 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001834 /*
1835 * if we find an error in a super block, we just report it.
1836 * They will get written with the next transaction commit
1837 * anyway
1838 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001839 spin_lock(&sctx->stat_lock);
1840 ++sctx->stat.super_errors;
1841 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001842 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001843 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001844 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1845 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001846 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001847 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001848 }
1849
Stefan Behrens442a4f62012-05-25 16:06:08 +02001850 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001851}
1852
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001853static void scrub_block_get(struct scrub_block *sblock)
1854{
1855 atomic_inc(&sblock->ref_count);
1856}
1857
1858static void scrub_block_put(struct scrub_block *sblock)
1859{
1860 if (atomic_dec_and_test(&sblock->ref_count)) {
1861 int i;
1862
1863 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001864 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001865 kfree(sblock);
1866 }
1867}
1868
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001869static void scrub_page_get(struct scrub_page *spage)
1870{
1871 atomic_inc(&spage->ref_count);
1872}
1873
1874static void scrub_page_put(struct scrub_page *spage)
1875{
1876 if (atomic_dec_and_test(&spage->ref_count)) {
1877 if (spage->page)
1878 __free_page(spage->page);
1879 kfree(spage);
1880 }
1881}
1882
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001883static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01001884{
1885 struct scrub_bio *sbio;
1886
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001887 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04001888 return;
Arne Jansena2de7332011-03-08 14:14:00 +01001889
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001890 sbio = sctx->bios[sctx->curr];
1891 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001892 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001893
Stefan Behrensff023aa2012-11-06 11:43:11 +01001894 if (!sbio->bio->bi_bdev) {
1895 /*
1896 * this case should not happen. If btrfs_map_block() is
1897 * wrong, it could happen for dev-replace operations on
1898 * missing devices when no mirrors are available, but in
1899 * this case it should already fail the mount.
1900 * This case is handled correctly (but _very_ slowly).
1901 */
1902 printk_ratelimited(KERN_WARNING
1903 "btrfs: scrub_submit(bio bdev == NULL) is unexpected!\n");
1904 bio_endio(sbio->bio, -EIO);
1905 } else {
1906 btrfsic_submit_bio(READ, sbio->bio);
1907 }
Arne Jansena2de7332011-03-08 14:14:00 +01001908}
1909
Stefan Behrensff023aa2012-11-06 11:43:11 +01001910static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
1911 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01001912{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001913 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01001914 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001915 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01001916
1917again:
1918 /*
1919 * grab a fresh bio or wait for one to become available
1920 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001921 while (sctx->curr == -1) {
1922 spin_lock(&sctx->list_lock);
1923 sctx->curr = sctx->first_free;
1924 if (sctx->curr != -1) {
1925 sctx->first_free = sctx->bios[sctx->curr]->next_free;
1926 sctx->bios[sctx->curr]->next_free = -1;
1927 sctx->bios[sctx->curr]->page_count = 0;
1928 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001929 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001930 spin_unlock(&sctx->list_lock);
1931 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01001932 }
1933 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001934 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001935 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05001936 struct bio *bio;
1937
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001938 sbio->physical = spage->physical;
1939 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001940 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001941 bio = sbio->bio;
1942 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001943 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001944 if (!bio)
1945 return -ENOMEM;
1946 sbio->bio = bio;
1947 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05001948
1949 bio->bi_private = sbio;
1950 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001951 bio->bi_bdev = sbio->dev->bdev;
1952 bio->bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001953 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001954 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1955 spage->physical ||
1956 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001957 spage->logical ||
1958 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001959 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05001960 goto again;
1961 }
1962
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001963 sbio->pagev[sbio->page_count] = spage;
1964 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1965 if (ret != PAGE_SIZE) {
1966 if (sbio->page_count < 1) {
1967 bio_put(sbio->bio);
1968 sbio->bio = NULL;
1969 return -EIO;
1970 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001971 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001972 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01001973 }
Arne Jansen1bc87792011-05-28 21:57:55 +02001974
Stefan Behrensff023aa2012-11-06 11:43:11 +01001975 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001976 atomic_inc(&sblock->outstanding_pages);
1977 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001978 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001979 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001980
1981 return 0;
1982}
1983
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001984static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001985 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001986 u64 gen, int mirror_num, u8 *csum, int force,
1987 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001988{
1989 struct scrub_block *sblock;
1990 int index;
1991
1992 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
1993 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001994 spin_lock(&sctx->stat_lock);
1995 sctx->stat.malloc_errors++;
1996 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001997 return -ENOMEM;
1998 }
1999
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002000 /* one ref inside this function, plus one for each page added to
2001 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002002 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002003 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002004 sblock->no_io_error_seen = 1;
2005
2006 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002007 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002008 u64 l = min_t(u64, len, PAGE_SIZE);
2009
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002010 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2011 if (!spage) {
2012leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002013 spin_lock(&sctx->stat_lock);
2014 sctx->stat.malloc_errors++;
2015 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002016 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002017 return -ENOMEM;
2018 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002019 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2020 scrub_page_get(spage);
2021 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002022 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002023 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002024 spage->flags = flags;
2025 spage->generation = gen;
2026 spage->logical = logical;
2027 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002028 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002029 spage->mirror_num = mirror_num;
2030 if (csum) {
2031 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002032 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002033 } else {
2034 spage->have_csum = 0;
2035 }
2036 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002037 spage->page = alloc_page(GFP_NOFS);
2038 if (!spage->page)
2039 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002040 len -= l;
2041 logical += l;
2042 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002043 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002044 }
2045
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002046 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002047 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002048 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002049 int ret;
2050
Stefan Behrensff023aa2012-11-06 11:43:11 +01002051 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002052 if (ret) {
2053 scrub_block_put(sblock);
2054 return ret;
2055 }
2056 }
2057
2058 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002059 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002060
2061 /* last one frees, either here or in bio completion for last page */
2062 scrub_block_put(sblock);
2063 return 0;
2064}
2065
2066static void scrub_bio_end_io(struct bio *bio, int err)
2067{
2068 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002069 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002070
2071 sbio->err = err;
2072 sbio->bio = bio;
2073
2074 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work);
2075}
2076
2077static void scrub_bio_end_io_worker(struct btrfs_work *work)
2078{
2079 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002080 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002081 int i;
2082
Stefan Behrensff023aa2012-11-06 11:43:11 +01002083 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002084 if (sbio->err) {
2085 for (i = 0; i < sbio->page_count; i++) {
2086 struct scrub_page *spage = sbio->pagev[i];
2087
2088 spage->io_error = 1;
2089 spage->sblock->no_io_error_seen = 0;
2090 }
2091 }
2092
2093 /* now complete the scrub_block items that have all pages completed */
2094 for (i = 0; i < sbio->page_count; i++) {
2095 struct scrub_page *spage = sbio->pagev[i];
2096 struct scrub_block *sblock = spage->sblock;
2097
2098 if (atomic_dec_and_test(&sblock->outstanding_pages))
2099 scrub_block_complete(sblock);
2100 scrub_block_put(sblock);
2101 }
2102
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002103 bio_put(sbio->bio);
2104 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002105 spin_lock(&sctx->list_lock);
2106 sbio->next_free = sctx->first_free;
2107 sctx->first_free = sbio->index;
2108 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002109
2110 if (sctx->is_dev_replace &&
2111 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2112 mutex_lock(&sctx->wr_ctx.wr_lock);
2113 scrub_wr_submit(sctx);
2114 mutex_unlock(&sctx->wr_ctx.wr_lock);
2115 }
2116
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002117 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002118}
2119
2120static void scrub_block_complete(struct scrub_block *sblock)
2121{
Stefan Behrensff023aa2012-11-06 11:43:11 +01002122 if (!sblock->no_io_error_seen) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002123 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002124 } else {
2125 /*
2126 * if has checksum error, write via repair mechanism in
2127 * dev replace case, otherwise write here in dev replace
2128 * case.
2129 */
2130 if (!scrub_checksum(sblock) && sblock->sctx->is_dev_replace)
2131 scrub_write_block_to_dev_replace(sblock);
2132 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002133}
2134
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002135static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002136 u8 *csum)
2137{
2138 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002139 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002140 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002141
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002142 while (!list_empty(&sctx->csum_list)) {
2143 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002144 struct btrfs_ordered_sum, list);
2145 if (sum->bytenr > logical)
2146 return 0;
2147 if (sum->bytenr + sum->len > logical)
2148 break;
2149
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002150 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002151 list_del(&sum->list);
2152 kfree(sum);
2153 sum = NULL;
2154 }
2155 if (!sum)
2156 return 0;
2157
Miao Xief51a4a12013-06-19 10:36:09 +08002158 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002159 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002160 memcpy(csum, sum->sums + index, sctx->csum_size);
2161 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002162 list_del(&sum->list);
2163 kfree(sum);
2164 }
Miao Xief51a4a12013-06-19 10:36:09 +08002165 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002166}
2167
2168/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002169static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002170 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002171 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002172{
2173 int ret;
2174 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002175 u32 blocksize;
2176
2177 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002178 blocksize = sctx->sectorsize;
2179 spin_lock(&sctx->stat_lock);
2180 sctx->stat.data_extents_scrubbed++;
2181 sctx->stat.data_bytes_scrubbed += len;
2182 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002183 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002184 WARN_ON(sctx->nodesize != sctx->leafsize);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002185 blocksize = sctx->nodesize;
2186 spin_lock(&sctx->stat_lock);
2187 sctx->stat.tree_extents_scrubbed++;
2188 sctx->stat.tree_bytes_scrubbed += len;
2189 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002190 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002191 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002192 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002193 }
Arne Jansena2de7332011-03-08 14:14:00 +01002194
2195 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002196 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002197 int have_csum = 0;
2198
2199 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2200 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002201 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002202 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002203 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002204 if (sctx->is_dev_replace && !have_csum) {
2205 ret = copy_nocow_pages(sctx, logical, l,
2206 mirror_num,
2207 physical_for_dev_replace);
2208 goto behind_scrub_pages;
2209 }
Arne Jansena2de7332011-03-08 14:14:00 +01002210 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002211 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002212 mirror_num, have_csum ? csum : NULL, 0,
2213 physical_for_dev_replace);
2214behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002215 if (ret)
2216 return ret;
2217 len -= l;
2218 logical += l;
2219 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002220 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002221 }
2222 return 0;
2223}
2224
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002225static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002226 struct map_lookup *map,
2227 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002228 int num, u64 base, u64 length,
2229 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002230{
2231 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002232 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002233 struct btrfs_root *root = fs_info->extent_root;
2234 struct btrfs_root *csum_root = fs_info->csum_root;
2235 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002236 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002237 u64 flags;
2238 int ret;
2239 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002240 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002241 struct extent_buffer *l;
2242 struct btrfs_key key;
2243 u64 physical;
2244 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002245 u64 logic_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002246 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002247 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002248 struct reada_control *reada1;
2249 struct reada_control *reada2;
2250 struct btrfs_key key_start;
2251 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002252 u64 increment = map->stripe_len;
2253 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002254 u64 extent_logical;
2255 u64 extent_physical;
2256 u64 extent_len;
2257 struct btrfs_device *extent_dev;
2258 int extent_mirror_num;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002259 int stop_loop;
Arne Jansena2de7332011-03-08 14:14:00 +01002260
David Woodhouse53b381b2013-01-29 18:40:14 -05002261 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
2262 BTRFS_BLOCK_GROUP_RAID6)) {
2263 if (num >= nr_data_stripes(map)) {
2264 return 0;
2265 }
2266 }
2267
Arne Jansena2de7332011-03-08 14:14:00 +01002268 nstripes = length;
2269 offset = 0;
2270 do_div(nstripes, map->stripe_len);
2271 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2272 offset = map->stripe_len * num;
2273 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02002274 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002275 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2276 int factor = map->num_stripes / map->sub_stripes;
2277 offset = map->stripe_len * (num / map->sub_stripes);
2278 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02002279 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002280 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2281 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002282 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002283 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2284 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002285 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002286 } else {
2287 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002288 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002289 }
2290
2291 path = btrfs_alloc_path();
2292 if (!path)
2293 return -ENOMEM;
2294
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002295 /*
2296 * work on commit root. The related disk blocks are static as
2297 * long as COW is applied. This means, it is save to rewrite
2298 * them to repair disk errors without any race conditions
2299 */
Arne Jansena2de7332011-03-08 14:14:00 +01002300 path->search_commit_root = 1;
2301 path->skip_locking = 1;
2302
2303 /*
Arne Jansen7a262852011-06-10 12:39:23 +02002304 * trigger the readahead for extent tree csum tree and wait for
2305 * completion. During readahead, the scrub is officially paused
2306 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01002307 */
2308 logical = base + offset;
Arne Jansena2de7332011-03-08 14:14:00 +01002309
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002310 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002311 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08002312 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002313
Arne Jansen7a262852011-06-10 12:39:23 +02002314 /* FIXME it might be better to start readahead at commit root */
2315 key_start.objectid = logical;
2316 key_start.type = BTRFS_EXTENT_ITEM_KEY;
2317 key_start.offset = (u64)0;
2318 key_end.objectid = base + offset + nstripes * increment;
Josef Bacik3173a182013-03-07 14:22:04 -05002319 key_end.type = BTRFS_METADATA_ITEM_KEY;
2320 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02002321 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01002322
Arne Jansen7a262852011-06-10 12:39:23 +02002323 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2324 key_start.type = BTRFS_EXTENT_CSUM_KEY;
2325 key_start.offset = logical;
2326 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2327 key_end.type = BTRFS_EXTENT_CSUM_KEY;
2328 key_end.offset = base + offset + nstripes * increment;
2329 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01002330
Arne Jansen7a262852011-06-10 12:39:23 +02002331 if (!IS_ERR(reada1))
2332 btrfs_reada_wait(reada1);
2333 if (!IS_ERR(reada2))
2334 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01002335
Arne Jansena2de7332011-03-08 14:14:00 +01002336
2337 /*
2338 * collect all data csums for the stripe to avoid seeking during
2339 * the scrub. This might currently (crc32) end up to be about 1MB
2340 */
Arne Jansene7786c32011-05-28 20:58:38 +00002341 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002342
Arne Jansena2de7332011-03-08 14:14:00 +01002343 /*
2344 * now find all extents for each stripe and scrub them
2345 */
Arne Jansen7a262852011-06-10 12:39:23 +02002346 logical = base + offset;
2347 physical = map->stripes[num].physical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002348 logic_end = logical + increment * nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002349 ret = 0;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002350 while (logical < logic_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01002351 /*
2352 * canceled?
2353 */
2354 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002355 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002356 ret = -ECANCELED;
2357 goto out;
2358 }
2359 /*
2360 * check to see if we have to pause
2361 */
2362 if (atomic_read(&fs_info->scrub_pause_req)) {
2363 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002364 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002365 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002366 mutex_lock(&sctx->wr_ctx.wr_lock);
2367 scrub_wr_submit(sctx);
2368 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002369 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002370 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002371 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08002372 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002373 }
2374
2375 key.objectid = logical;
2376 key.type = BTRFS_EXTENT_ITEM_KEY;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002377 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01002378
2379 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2380 if (ret < 0)
2381 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002382
Arne Jansen8c510322011-06-03 10:09:26 +02002383 if (ret > 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01002384 ret = btrfs_previous_item(root, path, 0,
2385 BTRFS_EXTENT_ITEM_KEY);
2386 if (ret < 0)
2387 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02002388 if (ret > 0) {
2389 /* there's no smaller item, so stick with the
2390 * larger one */
2391 btrfs_release_path(path);
2392 ret = btrfs_search_slot(NULL, root, &key,
2393 path, 0, 0);
2394 if (ret < 0)
2395 goto out;
2396 }
Arne Jansena2de7332011-03-08 14:14:00 +01002397 }
2398
Liu Bo625f1c8d2013-04-27 02:56:57 +00002399 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002400 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05002401 u64 bytes;
2402
Arne Jansena2de7332011-03-08 14:14:00 +01002403 l = path->nodes[0];
2404 slot = path->slots[0];
2405 if (slot >= btrfs_header_nritems(l)) {
2406 ret = btrfs_next_leaf(root, path);
2407 if (ret == 0)
2408 continue;
2409 if (ret < 0)
2410 goto out;
2411
Liu Bo625f1c8d2013-04-27 02:56:57 +00002412 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002413 break;
2414 }
2415 btrfs_item_key_to_cpu(l, &key, slot);
2416
Josef Bacik3173a182013-03-07 14:22:04 -05002417 if (key.type == BTRFS_METADATA_ITEM_KEY)
2418 bytes = root->leafsize;
2419 else
2420 bytes = key.offset;
2421
2422 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01002423 goto next;
2424
Liu Bo625f1c8d2013-04-27 02:56:57 +00002425 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2426 key.type != BTRFS_METADATA_ITEM_KEY)
2427 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01002428
Liu Bo625f1c8d2013-04-27 02:56:57 +00002429 if (key.objectid >= logical + map->stripe_len) {
2430 /* out of this device extent */
2431 if (key.objectid >= logic_end)
2432 stop_loop = 1;
2433 break;
2434 }
Arne Jansena2de7332011-03-08 14:14:00 +01002435
2436 extent = btrfs_item_ptr(l, slot,
2437 struct btrfs_extent_item);
2438 flags = btrfs_extent_flags(l, extent);
2439 generation = btrfs_extent_generation(l, extent);
2440
2441 if (key.objectid < logical &&
2442 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2443 printk(KERN_ERR
2444 "btrfs scrub: tree block %llu spanning "
2445 "stripes, ignored. logical=%llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002446 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01002447 goto next;
2448 }
2449
Liu Bo625f1c8d2013-04-27 02:56:57 +00002450again:
2451 extent_logical = key.objectid;
2452 extent_len = bytes;
2453
Arne Jansena2de7332011-03-08 14:14:00 +01002454 /*
2455 * trim extent to this stripe
2456 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00002457 if (extent_logical < logical) {
2458 extent_len -= logical - extent_logical;
2459 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01002460 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00002461 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01002462 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00002463 extent_len = logical + map->stripe_len -
2464 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01002465 }
2466
Liu Bo625f1c8d2013-04-27 02:56:57 +00002467 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002468 extent_dev = scrub_dev;
2469 extent_mirror_num = mirror_num;
2470 if (is_dev_replace)
2471 scrub_remap_extent(fs_info, extent_logical,
2472 extent_len, &extent_physical,
2473 &extent_dev,
2474 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002475
2476 ret = btrfs_lookup_csums_range(csum_root, logical,
2477 logical + map->stripe_len - 1,
2478 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002479 if (ret)
2480 goto out;
2481
Liu Bo625f1c8d2013-04-27 02:56:57 +00002482 ret = scrub_extent(sctx, extent_logical, extent_len,
2483 extent_physical, extent_dev, flags,
2484 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02002485 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002486 if (ret)
2487 goto out;
2488
Josef Bacikd88d46c2013-06-10 12:59:04 +00002489 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002490 if (extent_logical + extent_len <
2491 key.objectid + bytes) {
2492 logical += increment;
2493 physical += map->stripe_len;
2494
2495 if (logical < key.objectid + bytes) {
2496 cond_resched();
2497 goto again;
2498 }
2499
2500 if (logical >= logic_end) {
2501 stop_loop = 1;
2502 break;
2503 }
2504 }
Arne Jansena2de7332011-03-08 14:14:00 +01002505next:
2506 path->slots[0]++;
2507 }
Chris Mason71267332011-05-23 06:30:52 -04002508 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002509 logical += increment;
2510 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002511 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002512 if (stop_loop)
2513 sctx->stat.last_physical = map->stripes[num].physical +
2514 length;
2515 else
2516 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002517 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002518 if (stop_loop)
2519 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002520 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01002521out:
Arne Jansena2de7332011-03-08 14:14:00 +01002522 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002523 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002524 mutex_lock(&sctx->wr_ctx.wr_lock);
2525 scrub_wr_submit(sctx);
2526 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002527
Arne Jansene7786c32011-05-28 20:58:38 +00002528 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002529 btrfs_free_path(path);
2530 return ret < 0 ? ret : 0;
2531}
2532
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002533static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002534 struct btrfs_device *scrub_dev,
2535 u64 chunk_tree, u64 chunk_objectid,
2536 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002537 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002538{
2539 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002540 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01002541 struct map_lookup *map;
2542 struct extent_map *em;
2543 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002544 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002545
2546 read_lock(&map_tree->map_tree.lock);
2547 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2548 read_unlock(&map_tree->map_tree.lock);
2549
2550 if (!em)
2551 return -EINVAL;
2552
2553 map = (struct map_lookup *)em->bdev;
2554 if (em->start != chunk_offset)
2555 goto out;
2556
2557 if (em->len < length)
2558 goto out;
2559
2560 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002561 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01002562 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002563 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002564 chunk_offset, length,
2565 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002566 if (ret)
2567 goto out;
2568 }
2569 }
2570out:
2571 free_extent_map(em);
2572
2573 return ret;
2574}
2575
2576static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002577int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002578 struct btrfs_device *scrub_dev, u64 start, u64 end,
2579 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002580{
2581 struct btrfs_dev_extent *dev_extent = NULL;
2582 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002583 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002584 struct btrfs_fs_info *fs_info = root->fs_info;
2585 u64 length;
2586 u64 chunk_tree;
2587 u64 chunk_objectid;
2588 u64 chunk_offset;
2589 int ret;
2590 int slot;
2591 struct extent_buffer *l;
2592 struct btrfs_key key;
2593 struct btrfs_key found_key;
2594 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002595 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01002596
2597 path = btrfs_alloc_path();
2598 if (!path)
2599 return -ENOMEM;
2600
2601 path->reada = 2;
2602 path->search_commit_root = 1;
2603 path->skip_locking = 1;
2604
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002605 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01002606 key.offset = 0ull;
2607 key.type = BTRFS_DEV_EXTENT_KEY;
2608
Arne Jansena2de7332011-03-08 14:14:00 +01002609 while (1) {
2610 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2611 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02002612 break;
2613 if (ret > 0) {
2614 if (path->slots[0] >=
2615 btrfs_header_nritems(path->nodes[0])) {
2616 ret = btrfs_next_leaf(root, path);
2617 if (ret)
2618 break;
2619 }
2620 }
Arne Jansena2de7332011-03-08 14:14:00 +01002621
2622 l = path->nodes[0];
2623 slot = path->slots[0];
2624
2625 btrfs_item_key_to_cpu(l, &found_key, slot);
2626
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002627 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01002628 break;
2629
Arne Jansen8c510322011-06-03 10:09:26 +02002630 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01002631 break;
2632
2633 if (found_key.offset >= end)
2634 break;
2635
2636 if (found_key.offset < key.offset)
2637 break;
2638
2639 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2640 length = btrfs_dev_extent_length(l, dev_extent);
2641
2642 if (found_key.offset + length <= start) {
2643 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002644 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002645 continue;
2646 }
2647
2648 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2649 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2650 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2651
2652 /*
2653 * get a reference on the corresponding block group to prevent
2654 * the chunk from going away while we scrub it
2655 */
2656 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2657 if (!cache) {
2658 ret = -ENOENT;
Arne Jansen8c510322011-06-03 10:09:26 +02002659 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002660 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01002661 dev_replace->cursor_right = found_key.offset + length;
2662 dev_replace->cursor_left = found_key.offset;
2663 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002664 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002665 chunk_offset, length, found_key.offset,
2666 is_dev_replace);
2667
2668 /*
2669 * flush, submit all pending read and write bios, afterwards
2670 * wait for them.
2671 * Note that in the dev replace case, a read request causes
2672 * write requests that are submitted in the read completion
2673 * worker. Therefore in the current situation, it is required
2674 * that all write requests are flushed, so that all read and
2675 * write requests are really completed when bios_in_flight
2676 * changes to 0.
2677 */
2678 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
2679 scrub_submit(sctx);
2680 mutex_lock(&sctx->wr_ctx.wr_lock);
2681 scrub_wr_submit(sctx);
2682 mutex_unlock(&sctx->wr_ctx.wr_lock);
2683
2684 wait_event(sctx->list_wait,
2685 atomic_read(&sctx->bios_in_flight) == 0);
2686 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002687 wait_event(sctx->list_wait,
2688 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08002689 scrub_blocked_if_needed(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002690
Arne Jansena2de7332011-03-08 14:14:00 +01002691 btrfs_put_block_group(cache);
2692 if (ret)
2693 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00002694 if (is_dev_replace &&
2695 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002696 ret = -EIO;
2697 break;
2698 }
2699 if (sctx->stat.malloc_errors > 0) {
2700 ret = -ENOMEM;
2701 break;
2702 }
Arne Jansena2de7332011-03-08 14:14:00 +01002703
Ilya Dryomov539f3582013-10-07 13:42:57 +03002704 dev_replace->cursor_left = dev_replace->cursor_right;
2705 dev_replace->item_needs_writeback = 1;
2706
Arne Jansena2de7332011-03-08 14:14:00 +01002707 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002708 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002709 }
2710
Arne Jansena2de7332011-03-08 14:14:00 +01002711 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02002712
2713 /*
2714 * ret can still be 1 from search_slot or next_leaf,
2715 * that's not an error
2716 */
2717 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002718}
2719
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002720static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2721 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01002722{
2723 int i;
2724 u64 bytenr;
2725 u64 gen;
2726 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002727 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002728
Miao Xie87533c42013-01-29 10:14:48 +00002729 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002730 return -EIO;
2731
Arne Jansena2de7332011-03-08 14:14:00 +01002732 gen = root->fs_info->last_trans_committed;
2733
2734 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2735 bytenr = btrfs_sb_offset(i);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002736 if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01002737 break;
2738
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002739 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002740 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002741 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01002742 if (ret)
2743 return ret;
2744 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002745 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002746
2747 return 0;
2748}
2749
2750/*
2751 * get a reference count on fs_info->scrub_workers. start worker if necessary
2752 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002753static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
2754 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002755{
Josef Bacik0dc3b842011-11-18 14:37:27 -05002756 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002757
Arne Jansen632dd772011-06-10 12:07:07 +02002758 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002759 if (is_dev_replace)
2760 btrfs_init_workers(&fs_info->scrub_workers, "scrub", 1,
2761 &fs_info->generic_worker);
2762 else
2763 btrfs_init_workers(&fs_info->scrub_workers, "scrub",
2764 fs_info->thread_pool_size,
2765 &fs_info->generic_worker);
Arne Jansen632dd772011-06-10 12:07:07 +02002766 fs_info->scrub_workers.idle_thresh = 4;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002767 ret = btrfs_start_workers(&fs_info->scrub_workers);
2768 if (ret)
2769 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002770 btrfs_init_workers(&fs_info->scrub_wr_completion_workers,
2771 "scrubwrc",
2772 fs_info->thread_pool_size,
2773 &fs_info->generic_worker);
2774 fs_info->scrub_wr_completion_workers.idle_thresh = 2;
2775 ret = btrfs_start_workers(
2776 &fs_info->scrub_wr_completion_workers);
2777 if (ret)
2778 goto out;
2779 btrfs_init_workers(&fs_info->scrub_nocow_workers, "scrubnc", 1,
2780 &fs_info->generic_worker);
2781 ret = btrfs_start_workers(&fs_info->scrub_nocow_workers);
2782 if (ret)
2783 goto out;
Arne Jansen632dd772011-06-10 12:07:07 +02002784 }
Arne Jansena2de7332011-03-08 14:14:00 +01002785 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002786out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05002787 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002788}
2789
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002790static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002791{
Stefan Behrensff023aa2012-11-06 11:43:11 +01002792 if (--fs_info->scrub_workers_refcnt == 0) {
Arne Jansena2de7332011-03-08 14:14:00 +01002793 btrfs_stop_workers(&fs_info->scrub_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002794 btrfs_stop_workers(&fs_info->scrub_wr_completion_workers);
2795 btrfs_stop_workers(&fs_info->scrub_nocow_workers);
2796 }
Arne Jansena2de7332011-03-08 14:14:00 +01002797 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002798}
2799
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002800int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
2801 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01002802 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002803{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002804 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002805 int ret;
2806 struct btrfs_device *dev;
2807
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002808 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01002809 return -EINVAL;
2810
2811 /*
2812 * check some assumptions
2813 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002814 if (fs_info->chunk_root->nodesize != fs_info->chunk_root->leafsize) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002815 printk(KERN_ERR
2816 "btrfs_scrub: size assumption nodesize == leafsize (%d == %d) fails\n",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002817 fs_info->chunk_root->nodesize,
2818 fs_info->chunk_root->leafsize);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002819 return -EINVAL;
2820 }
2821
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002822 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002823 /*
2824 * in this case scrub is unable to calculate the checksum
2825 * the way scrub is implemented. Do not handle this
2826 * situation at all because it won't ever happen.
2827 */
2828 printk(KERN_ERR
2829 "btrfs_scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails\n",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002830 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002831 return -EINVAL;
2832 }
2833
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002834 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002835 /* not supported for data w/o checksums */
2836 printk(KERN_ERR
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02002837 "btrfs_scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails\n",
2838 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01002839 return -EINVAL;
2840 }
2841
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002842 if (fs_info->chunk_root->nodesize >
2843 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
2844 fs_info->chunk_root->sectorsize >
2845 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
2846 /*
2847 * would exhaust the array bounds of pagev member in
2848 * struct scrub_block
2849 */
2850 pr_err("btrfs_scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails\n",
2851 fs_info->chunk_root->nodesize,
2852 SCRUB_MAX_PAGES_PER_BLOCK,
2853 fs_info->chunk_root->sectorsize,
2854 SCRUB_MAX_PAGES_PER_BLOCK);
2855 return -EINVAL;
2856 }
2857
Arne Jansena2de7332011-03-08 14:14:00 +01002858
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002859 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2860 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002861 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002862 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002863 return -ENODEV;
2864 }
Arne Jansena2de7332011-03-08 14:14:00 +01002865
Wang Shilong3b7a0162013-10-12 02:11:12 +08002866 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002867 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01002868 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002869 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002870 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01002871 }
2872
Stefan Behrens8dabb742012-11-06 13:15:27 +01002873 btrfs_dev_replace_lock(&fs_info->dev_replace);
2874 if (dev->scrub_device ||
2875 (!is_dev_replace &&
2876 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
2877 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002878 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002879 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002880 return -EINPROGRESS;
2881 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01002882 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08002883
2884 ret = scrub_workers_get(fs_info, is_dev_replace);
2885 if (ret) {
2886 mutex_unlock(&fs_info->scrub_lock);
2887 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2888 return ret;
2889 }
2890
Stefan Behrens63a212a2012-11-05 18:29:28 +01002891 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002892 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002893 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002894 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2895 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002896 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002897 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002898 sctx->readonly = readonly;
2899 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08002900 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002901
Wang Shilong3cb09292013-12-04 21:15:19 +08002902 /*
2903 * checking @scrub_pause_req here, we can avoid
2904 * race between committing transaction and scrubbing.
2905 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08002906 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002907 atomic_inc(&fs_info->scrubs_running);
2908 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002909
Stefan Behrensff023aa2012-11-06 11:43:11 +01002910 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08002911 /*
2912 * by holding device list mutex, we can
2913 * kick off writing super in log tree sync.
2914 */
Wang Shilong3cb09292013-12-04 21:15:19 +08002915 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002916 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08002917 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002918 }
Arne Jansena2de7332011-03-08 14:14:00 +01002919
2920 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002921 ret = scrub_enumerate_chunks(sctx, dev, start, end,
2922 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002923
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002924 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002925 atomic_dec(&fs_info->scrubs_running);
2926 wake_up(&fs_info->scrub_pause_wait);
2927
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002928 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02002929
Arne Jansena2de7332011-03-08 14:14:00 +01002930 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002931 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002932
2933 mutex_lock(&fs_info->scrub_lock);
2934 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08002935 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002936 mutex_unlock(&fs_info->scrub_lock);
2937
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002938 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002939
2940 return ret;
2941}
2942
Jeff Mahoney143bede2012-03-01 14:56:26 +01002943void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002944{
2945 struct btrfs_fs_info *fs_info = root->fs_info;
2946
2947 mutex_lock(&fs_info->scrub_lock);
2948 atomic_inc(&fs_info->scrub_pause_req);
2949 while (atomic_read(&fs_info->scrubs_paused) !=
2950 atomic_read(&fs_info->scrubs_running)) {
2951 mutex_unlock(&fs_info->scrub_lock);
2952 wait_event(fs_info->scrub_pause_wait,
2953 atomic_read(&fs_info->scrubs_paused) ==
2954 atomic_read(&fs_info->scrubs_running));
2955 mutex_lock(&fs_info->scrub_lock);
2956 }
2957 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002958}
2959
Jeff Mahoney143bede2012-03-01 14:56:26 +01002960void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002961{
2962 struct btrfs_fs_info *fs_info = root->fs_info;
2963
2964 atomic_dec(&fs_info->scrub_pause_req);
2965 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01002966}
2967
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002968int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002969{
Arne Jansena2de7332011-03-08 14:14:00 +01002970 mutex_lock(&fs_info->scrub_lock);
2971 if (!atomic_read(&fs_info->scrubs_running)) {
2972 mutex_unlock(&fs_info->scrub_lock);
2973 return -ENOTCONN;
2974 }
2975
2976 atomic_inc(&fs_info->scrub_cancel_req);
2977 while (atomic_read(&fs_info->scrubs_running)) {
2978 mutex_unlock(&fs_info->scrub_lock);
2979 wait_event(fs_info->scrub_pause_wait,
2980 atomic_read(&fs_info->scrubs_running) == 0);
2981 mutex_lock(&fs_info->scrub_lock);
2982 }
2983 atomic_dec(&fs_info->scrub_cancel_req);
2984 mutex_unlock(&fs_info->scrub_lock);
2985
2986 return 0;
2987}
2988
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002989int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
2990 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01002991{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002992 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002993
2994 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002995 sctx = dev->scrub_device;
2996 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01002997 mutex_unlock(&fs_info->scrub_lock);
2998 return -ENOTCONN;
2999 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003000 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003001 while (dev->scrub_device) {
3002 mutex_unlock(&fs_info->scrub_lock);
3003 wait_event(fs_info->scrub_pause_wait,
3004 dev->scrub_device == NULL);
3005 mutex_lock(&fs_info->scrub_lock);
3006 }
3007 mutex_unlock(&fs_info->scrub_lock);
3008
3009 return 0;
3010}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003011
Arne Jansena2de7332011-03-08 14:14:00 +01003012int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3013 struct btrfs_scrub_progress *progress)
3014{
3015 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003016 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003017
3018 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003019 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003020 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003021 sctx = dev->scrub_device;
3022 if (sctx)
3023 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003024 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3025
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003026 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003027}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003028
3029static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3030 u64 extent_logical, u64 extent_len,
3031 u64 *extent_physical,
3032 struct btrfs_device **extent_dev,
3033 int *extent_mirror_num)
3034{
3035 u64 mapped_length;
3036 struct btrfs_bio *bbio = NULL;
3037 int ret;
3038
3039 mapped_length = extent_len;
3040 ret = btrfs_map_block(fs_info, READ, extent_logical,
3041 &mapped_length, &bbio, 0);
3042 if (ret || !bbio || mapped_length < extent_len ||
3043 !bbio->stripes[0].dev->bdev) {
3044 kfree(bbio);
3045 return;
3046 }
3047
3048 *extent_physical = bbio->stripes[0].physical;
3049 *extent_mirror_num = bbio->mirror_num;
3050 *extent_dev = bbio->stripes[0].dev;
3051 kfree(bbio);
3052}
3053
3054static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3055 struct scrub_wr_ctx *wr_ctx,
3056 struct btrfs_fs_info *fs_info,
3057 struct btrfs_device *dev,
3058 int is_dev_replace)
3059{
3060 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3061
3062 mutex_init(&wr_ctx->wr_lock);
3063 wr_ctx->wr_curr_bio = NULL;
3064 if (!is_dev_replace)
3065 return 0;
3066
3067 WARN_ON(!dev->bdev);
3068 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3069 bio_get_nr_vecs(dev->bdev));
3070 wr_ctx->tgtdev = dev;
3071 atomic_set(&wr_ctx->flush_all_writes, 0);
3072 return 0;
3073}
3074
3075static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3076{
3077 mutex_lock(&wr_ctx->wr_lock);
3078 kfree(wr_ctx->wr_curr_bio);
3079 wr_ctx->wr_curr_bio = NULL;
3080 mutex_unlock(&wr_ctx->wr_lock);
3081}
3082
3083static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3084 int mirror_num, u64 physical_for_dev_replace)
3085{
3086 struct scrub_copy_nocow_ctx *nocow_ctx;
3087 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3088
3089 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3090 if (!nocow_ctx) {
3091 spin_lock(&sctx->stat_lock);
3092 sctx->stat.malloc_errors++;
3093 spin_unlock(&sctx->stat_lock);
3094 return -ENOMEM;
3095 }
3096
3097 scrub_pending_trans_workers_inc(sctx);
3098
3099 nocow_ctx->sctx = sctx;
3100 nocow_ctx->logical = logical;
3101 nocow_ctx->len = len;
3102 nocow_ctx->mirror_num = mirror_num;
3103 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
3104 nocow_ctx->work.func = copy_nocow_pages_worker;
Josef Bacik652f25a2013-09-12 16:58:28 -04003105 INIT_LIST_HEAD(&nocow_ctx->inodes);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003106 btrfs_queue_worker(&fs_info->scrub_nocow_workers,
3107 &nocow_ctx->work);
3108
3109 return 0;
3110}
3111
Josef Bacik652f25a2013-09-12 16:58:28 -04003112static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3113{
3114 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3115 struct scrub_nocow_inode *nocow_inode;
3116
3117 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3118 if (!nocow_inode)
3119 return -ENOMEM;
3120 nocow_inode->inum = inum;
3121 nocow_inode->offset = offset;
3122 nocow_inode->root = root;
3123 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3124 return 0;
3125}
3126
3127#define COPY_COMPLETE 1
3128
Stefan Behrensff023aa2012-11-06 11:43:11 +01003129static void copy_nocow_pages_worker(struct btrfs_work *work)
3130{
3131 struct scrub_copy_nocow_ctx *nocow_ctx =
3132 container_of(work, struct scrub_copy_nocow_ctx, work);
3133 struct scrub_ctx *sctx = nocow_ctx->sctx;
3134 u64 logical = nocow_ctx->logical;
3135 u64 len = nocow_ctx->len;
3136 int mirror_num = nocow_ctx->mirror_num;
3137 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3138 int ret;
3139 struct btrfs_trans_handle *trans = NULL;
3140 struct btrfs_fs_info *fs_info;
3141 struct btrfs_path *path;
3142 struct btrfs_root *root;
3143 int not_written = 0;
3144
3145 fs_info = sctx->dev_root->fs_info;
3146 root = fs_info->extent_root;
3147
3148 path = btrfs_alloc_path();
3149 if (!path) {
3150 spin_lock(&sctx->stat_lock);
3151 sctx->stat.malloc_errors++;
3152 spin_unlock(&sctx->stat_lock);
3153 not_written = 1;
3154 goto out;
3155 }
3156
3157 trans = btrfs_join_transaction(root);
3158 if (IS_ERR(trans)) {
3159 not_written = 1;
3160 goto out;
3161 }
3162
3163 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04003164 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003165 if (ret != 0 && ret != -ENOENT) {
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02003166 pr_warn("iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d\n",
3167 logical, physical_for_dev_replace, len, mirror_num,
3168 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003169 not_written = 1;
3170 goto out;
3171 }
3172
Josef Bacik652f25a2013-09-12 16:58:28 -04003173 btrfs_end_transaction(trans, root);
3174 trans = NULL;
3175 while (!list_empty(&nocow_ctx->inodes)) {
3176 struct scrub_nocow_inode *entry;
3177 entry = list_first_entry(&nocow_ctx->inodes,
3178 struct scrub_nocow_inode,
3179 list);
3180 list_del_init(&entry->list);
3181 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
3182 entry->root, nocow_ctx);
3183 kfree(entry);
3184 if (ret == COPY_COMPLETE) {
3185 ret = 0;
3186 break;
3187 } else if (ret) {
3188 break;
3189 }
3190 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003191out:
Josef Bacik652f25a2013-09-12 16:58:28 -04003192 while (!list_empty(&nocow_ctx->inodes)) {
3193 struct scrub_nocow_inode *entry;
3194 entry = list_first_entry(&nocow_ctx->inodes,
3195 struct scrub_nocow_inode,
3196 list);
3197 list_del_init(&entry->list);
3198 kfree(entry);
3199 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003200 if (trans && !IS_ERR(trans))
3201 btrfs_end_transaction(trans, root);
3202 if (not_written)
3203 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
3204 num_uncorrectable_read_errors);
3205
3206 btrfs_free_path(path);
3207 kfree(nocow_ctx);
3208
3209 scrub_pending_trans_workers_dec(sctx);
3210}
3211
Josef Bacik652f25a2013-09-12 16:58:28 -04003212static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
3213 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003214{
Miao Xie826aa0a2013-06-27 18:50:59 +08003215 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003216 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08003217 struct inode *inode;
3218 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003219 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04003220 struct btrfs_ordered_extent *ordered;
3221 struct extent_map *em;
3222 struct extent_state *cached_state = NULL;
3223 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003224 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04003225 u64 len = nocow_ctx->len;
3226 u64 lockstart = offset, lockend = offset + len - 1;
Miao Xie826aa0a2013-06-27 18:50:59 +08003227 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00003228 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04003229 int ret = 0;
3230 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003231
3232 key.objectid = root;
3233 key.type = BTRFS_ROOT_ITEM_KEY;
3234 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00003235
3236 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
3237
Stefan Behrensff023aa2012-11-06 11:43:11 +01003238 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00003239 if (IS_ERR(local_root)) {
3240 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003241 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00003242 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003243
3244 key.type = BTRFS_INODE_ITEM_KEY;
3245 key.objectid = inum;
3246 key.offset = 0;
3247 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00003248 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003249 if (IS_ERR(inode))
3250 return PTR_ERR(inode);
3251
Miao Xieedd14002013-06-27 18:51:00 +08003252 /* Avoid truncate/dio/punch hole.. */
3253 mutex_lock(&inode->i_mutex);
3254 inode_dio_wait(inode);
3255
Stefan Behrensff023aa2012-11-06 11:43:11 +01003256 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04003257 io_tree = &BTRFS_I(inode)->io_tree;
3258
3259 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
3260 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
3261 if (ordered) {
3262 btrfs_put_ordered_extent(ordered);
3263 goto out_unlock;
3264 }
3265
3266 em = btrfs_get_extent(inode, NULL, 0, lockstart, len, 0);
3267 if (IS_ERR(em)) {
3268 ret = PTR_ERR(em);
3269 goto out_unlock;
3270 }
3271
3272 /*
3273 * This extent does not actually cover the logical extent anymore,
3274 * move on to the next inode.
3275 */
3276 if (em->block_start > nocow_ctx->logical ||
3277 em->block_start + em->block_len < nocow_ctx->logical + len) {
3278 free_extent_map(em);
3279 goto out_unlock;
3280 }
3281 free_extent_map(em);
3282
Stefan Behrensff023aa2012-11-06 11:43:11 +01003283 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003284 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08003285again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01003286 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3287 if (!page) {
3288 pr_err("find_or_create_page() failed\n");
3289 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08003290 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003291 }
3292
3293 if (PageUptodate(page)) {
3294 if (PageDirty(page))
3295 goto next_page;
3296 } else {
3297 ClearPageError(page);
Josef Bacik652f25a2013-09-12 16:58:28 -04003298 err = extent_read_full_page_nolock(io_tree, page,
3299 btrfs_get_extent,
3300 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08003301 if (err) {
3302 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003303 goto next_page;
3304 }
Miao Xieedd14002013-06-27 18:51:00 +08003305
Miao Xie26b258912013-06-27 18:50:58 +08003306 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08003307 /*
3308 * If the page has been remove from the page cache,
3309 * the data on it is meaningless, because it may be
3310 * old one, the new data may be written into the new
3311 * page in the page cache.
3312 */
3313 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04003314 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08003315 page_cache_release(page);
3316 goto again;
3317 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003318 if (!PageUptodate(page)) {
3319 ret = -EIO;
3320 goto next_page;
3321 }
3322 }
Miao Xie826aa0a2013-06-27 18:50:59 +08003323 err = write_page_nocow(nocow_ctx->sctx,
3324 physical_for_dev_replace, page);
3325 if (err)
3326 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003327next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08003328 unlock_page(page);
3329 page_cache_release(page);
3330
3331 if (ret)
3332 break;
3333
Stefan Behrensff023aa2012-11-06 11:43:11 +01003334 offset += PAGE_CACHE_SIZE;
3335 physical_for_dev_replace += PAGE_CACHE_SIZE;
3336 len -= PAGE_CACHE_SIZE;
3337 }
Josef Bacik652f25a2013-09-12 16:58:28 -04003338 ret = COPY_COMPLETE;
3339out_unlock:
3340 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
3341 GFP_NOFS);
Miao Xie826aa0a2013-06-27 18:50:59 +08003342out:
Miao Xieedd14002013-06-27 18:51:00 +08003343 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08003344 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003345 return ret;
3346}
3347
3348static int write_page_nocow(struct scrub_ctx *sctx,
3349 u64 physical_for_dev_replace, struct page *page)
3350{
3351 struct bio *bio;
3352 struct btrfs_device *dev;
3353 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003354
3355 dev = sctx->wr_ctx.tgtdev;
3356 if (!dev)
3357 return -EIO;
3358 if (!dev->bdev) {
3359 printk_ratelimited(KERN_WARNING
3360 "btrfs: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
3361 return -EIO;
3362 }
Chris Mason9be33952013-05-17 18:30:14 -04003363 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003364 if (!bio) {
3365 spin_lock(&sctx->stat_lock);
3366 sctx->stat.malloc_errors++;
3367 spin_unlock(&sctx->stat_lock);
3368 return -ENOMEM;
3369 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003370 bio->bi_size = 0;
3371 bio->bi_sector = physical_for_dev_replace >> 9;
3372 bio->bi_bdev = dev->bdev;
3373 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
3374 if (ret != PAGE_CACHE_SIZE) {
3375leave_with_eio:
3376 bio_put(bio);
3377 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
3378 return -EIO;
3379 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003380
Kent Overstreetc170bbb2013-11-24 16:32:22 -07003381 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01003382 goto leave_with_eio;
3383
3384 bio_put(bio);
3385 return 0;
3386}