blob: 9c146d8307b51266c643be0e3fdab7f6a45a765a [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
Miao Xieaf8e2d12014-10-23 14:42:50 +080066struct scrub_recover {
67 atomic_t refs;
68 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +080069 u64 map_length;
70};
71
Arne Jansena2de7332011-03-08 14:14:00 +010072struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040073 struct scrub_block *sblock;
74 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020075 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080076 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010077 u64 flags; /* extent flags */
78 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040079 u64 logical;
80 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010081 u64 physical_for_dev_replace;
Zhao Lei57019342015-01-20 15:11:45 +080082 atomic_t refs;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040083 struct {
84 unsigned int mirror_num:8;
85 unsigned int have_csum:1;
86 unsigned int io_error:1;
87 };
Arne Jansena2de7332011-03-08 14:14:00 +010088 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080089
90 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010091};
92
93struct scrub_bio {
94 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010095 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010096 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010097 struct bio *bio;
98 int err;
99 u64 logical;
100 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100101#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
102 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
103#else
104 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
105#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400106 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +0100107 int next_free;
108 struct btrfs_work work;
109};
110
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400111struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100112 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400113 int page_count;
114 atomic_t outstanding_pages;
Zhao Lei57019342015-01-20 15:11:45 +0800115 atomic_t refs; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100116 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800117 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400118 struct {
119 unsigned int header_error:1;
120 unsigned int checksum_error:1;
121 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200122 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800123
124 /* The following is for the data used to check parity */
125 /* It is for the data with checksum */
126 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400127 };
128};
129
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800130/* Used for the chunks with parity stripe such RAID5/6 */
131struct scrub_parity {
132 struct scrub_ctx *sctx;
133
134 struct btrfs_device *scrub_dev;
135
136 u64 logic_start;
137
138 u64 logic_end;
139
140 int nsectors;
141
142 int stripe_len;
143
Zhao Lei57019342015-01-20 15:11:45 +0800144 atomic_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800145
146 struct list_head spages;
147
148 /* Work of parity check and repair */
149 struct btrfs_work work;
150
151 /* Mark the parity blocks which have data */
152 unsigned long *dbitmap;
153
154 /*
155 * Mark the parity blocks which have data, but errors happen when
156 * read data or check data
157 */
158 unsigned long *ebitmap;
159
160 unsigned long bitmap[0];
161};
162
Stefan Behrensff023aa2012-11-06 11:43:11 +0100163struct scrub_wr_ctx {
164 struct scrub_bio *wr_curr_bio;
165 struct btrfs_device *tgtdev;
166 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
167 atomic_t flush_all_writes;
168 struct mutex wr_lock;
169};
170
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100171struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100172 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100173 struct btrfs_root *dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +0100174 int first_free;
175 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100176 atomic_t bios_in_flight;
177 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100178 spinlock_t list_lock;
179 wait_queue_head_t list_wait;
180 u16 csum_size;
181 struct list_head csum_list;
182 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100183 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100184 int pages_per_rd_bio;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400185 u32 sectorsize;
186 u32 nodesize;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100187
188 int is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100189 struct scrub_wr_ctx wr_ctx;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100190
Arne Jansena2de7332011-03-08 14:14:00 +0100191 /*
192 * statistics
193 */
194 struct btrfs_scrub_progress stat;
195 spinlock_t stat_lock;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000196
197 /*
198 * Use a ref counter to avoid use-after-free issues. Scrub workers
199 * decrement bios_in_flight and workers_pending and then do a wakeup
200 * on the list_wait wait queue. We must ensure the main scrub task
201 * doesn't free the scrub context before or while the workers are
202 * doing the wakeup() call.
203 */
204 atomic_t refs;
Arne Jansena2de7332011-03-08 14:14:00 +0100205};
206
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200207struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100208 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100209 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200210 u64 logical;
211 struct btrfs_root *root;
212 struct btrfs_work work;
213 int mirror_num;
214};
215
Josef Bacik652f25a2013-09-12 16:58:28 -0400216struct scrub_nocow_inode {
217 u64 inum;
218 u64 offset;
219 u64 root;
220 struct list_head list;
221};
222
Stefan Behrensff023aa2012-11-06 11:43:11 +0100223struct scrub_copy_nocow_ctx {
224 struct scrub_ctx *sctx;
225 u64 logical;
226 u64 len;
227 int mirror_num;
228 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -0400229 struct list_head inodes;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100230 struct btrfs_work work;
231};
232
Jan Schmidt558540c2011-06-13 19:59:12 +0200233struct scrub_warning {
234 struct btrfs_path *path;
235 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200236 const char *errstr;
237 sector_t sector;
238 u64 logical;
239 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200240};
241
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100242static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
243static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
244static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
245static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400246static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Zhao Leibe50a8d2015-01-20 15:11:42 +0800247static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100248 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100249static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
250 struct scrub_block *sblock, int is_metadata,
251 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800252 u16 csum_size, int retry_failed_mirror);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400253static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
254 struct scrub_block *sblock,
255 int is_metadata, int have_csum,
256 const u8 *csum, u64 generation,
257 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400258static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800259 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400260static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
261 struct scrub_block *sblock_good,
262 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100263static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
264static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
265 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400266static int scrub_checksum_data(struct scrub_block *sblock);
267static int scrub_checksum_tree_block(struct scrub_block *sblock);
268static int scrub_checksum_super(struct scrub_block *sblock);
269static void scrub_block_get(struct scrub_block *sblock);
270static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100271static void scrub_page_get(struct scrub_page *spage);
272static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800273static void scrub_parity_get(struct scrub_parity *sparity);
274static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100275static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
276 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100277static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100278 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100279 u64 gen, int mirror_num, u8 *csum, int force,
280 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200281static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400282static void scrub_bio_end_io_worker(struct btrfs_work *work);
283static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100284static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
285 u64 extent_logical, u64 extent_len,
286 u64 *extent_physical,
287 struct btrfs_device **extent_dev,
288 int *extent_mirror_num);
289static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
290 struct scrub_wr_ctx *wr_ctx,
291 struct btrfs_fs_info *fs_info,
292 struct btrfs_device *dev,
293 int is_dev_replace);
294static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
295static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
296 struct scrub_page *spage);
297static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200298static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100299static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
300static int write_page_nocow(struct scrub_ctx *sctx,
301 u64 physical_for_dev_replace, struct page *page);
302static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400303 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100304static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
305 int mirror_num, u64 physical_for_dev_replace);
306static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800307static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800308static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000309static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400310
311
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100312static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
313{
Filipe Mananaf55985f2015-02-09 21:14:24 +0000314 atomic_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100315 atomic_inc(&sctx->bios_in_flight);
316}
317
318static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
319{
320 atomic_dec(&sctx->bios_in_flight);
321 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000322 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100323}
324
Wang Shilongcb7ab022013-12-04 21:16:53 +0800325static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800326{
327 while (atomic_read(&fs_info->scrub_pause_req)) {
328 mutex_unlock(&fs_info->scrub_lock);
329 wait_event(fs_info->scrub_pause_wait,
330 atomic_read(&fs_info->scrub_pause_req) == 0);
331 mutex_lock(&fs_info->scrub_lock);
332 }
333}
334
Wang Shilongcb7ab022013-12-04 21:16:53 +0800335static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
336{
337 atomic_inc(&fs_info->scrubs_paused);
338 wake_up(&fs_info->scrub_pause_wait);
339
340 mutex_lock(&fs_info->scrub_lock);
341 __scrub_blocked_if_needed(fs_info);
342 atomic_dec(&fs_info->scrubs_paused);
343 mutex_unlock(&fs_info->scrub_lock);
344
345 wake_up(&fs_info->scrub_pause_wait);
346}
347
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100348/*
349 * used for workers that require transaction commits (i.e., for the
350 * NOCOW case)
351 */
352static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
353{
354 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
355
Filipe Mananaf55985f2015-02-09 21:14:24 +0000356 atomic_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100357 /*
358 * increment scrubs_running to prevent cancel requests from
359 * completing as long as a worker is running. we must also
360 * increment scrubs_paused to prevent deadlocking on pause
361 * requests used for transactions commits (as the worker uses a
362 * transaction context). it is safe to regard the worker
363 * as paused for all matters practical. effectively, we only
364 * avoid cancellation requests from completing.
365 */
366 mutex_lock(&fs_info->scrub_lock);
367 atomic_inc(&fs_info->scrubs_running);
368 atomic_inc(&fs_info->scrubs_paused);
369 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800370
371 /*
372 * check if @scrubs_running=@scrubs_paused condition
373 * inside wait_event() is not an atomic operation.
374 * which means we may inc/dec @scrub_running/paused
375 * at any time. Let's wake up @scrub_pause_wait as
376 * much as we can to let commit transaction blocked less.
377 */
378 wake_up(&fs_info->scrub_pause_wait);
379
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100380 atomic_inc(&sctx->workers_pending);
381}
382
383/* used for workers that require transaction commits */
384static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
385{
386 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
387
388 /*
389 * see scrub_pending_trans_workers_inc() why we're pretending
390 * to be paused in the scrub counters
391 */
392 mutex_lock(&fs_info->scrub_lock);
393 atomic_dec(&fs_info->scrubs_running);
394 atomic_dec(&fs_info->scrubs_paused);
395 mutex_unlock(&fs_info->scrub_lock);
396 atomic_dec(&sctx->workers_pending);
397 wake_up(&fs_info->scrub_pause_wait);
398 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000399 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100400}
401
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100402static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100403{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100404 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100405 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100406 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100407 struct btrfs_ordered_sum, list);
408 list_del(&sum->list);
409 kfree(sum);
410 }
411}
412
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100413static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100414{
415 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100416
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100417 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100418 return;
419
Stefan Behrensff023aa2012-11-06 11:43:11 +0100420 scrub_free_wr_ctx(&sctx->wr_ctx);
421
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400422 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100423 if (sctx->curr != -1) {
424 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400425
426 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100427 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400428 scrub_block_put(sbio->pagev[i]->sblock);
429 }
430 bio_put(sbio->bio);
431 }
432
Stefan Behrensff023aa2012-11-06 11:43:11 +0100433 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100434 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100435
436 if (!sbio)
437 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100438 kfree(sbio);
439 }
440
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100441 scrub_free_csums(sctx);
442 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100443}
444
Filipe Mananaf55985f2015-02-09 21:14:24 +0000445static void scrub_put_ctx(struct scrub_ctx *sctx)
446{
447 if (atomic_dec_and_test(&sctx->refs))
448 scrub_free_ctx(sctx);
449}
450
Arne Jansena2de7332011-03-08 14:14:00 +0100451static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100452struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100453{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100454 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100455 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100456 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100457 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100458
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100459 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
460 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100461 goto nomem;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000462 atomic_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100463 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200464 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100465 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100466 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100467 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100468 struct scrub_bio *sbio;
469
470 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
471 if (!sbio)
472 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100473 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100474
Arne Jansena2de7332011-03-08 14:14:00 +0100475 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100476 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400477 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800478 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
479 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100480
Stefan Behrensff023aa2012-11-06 11:43:11 +0100481 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100482 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200483 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100484 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100485 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100486 sctx->first_free = 0;
487 sctx->nodesize = dev->dev_root->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100488 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100489 atomic_set(&sctx->bios_in_flight, 0);
490 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100491 atomic_set(&sctx->cancel_req, 0);
492 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
493 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100494
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100495 spin_lock_init(&sctx->list_lock);
496 spin_lock_init(&sctx->stat_lock);
497 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100498
499 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
500 fs_info->dev_replace.tgtdev, is_dev_replace);
501 if (ret) {
502 scrub_free_ctx(sctx);
503 return ERR_PTR(ret);
504 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100505 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100506
507nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100508 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100509 return ERR_PTR(-ENOMEM);
510}
511
Stefan Behrensff023aa2012-11-06 11:43:11 +0100512static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
513 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200514{
515 u64 isize;
516 u32 nlink;
517 int ret;
518 int i;
519 struct extent_buffer *eb;
520 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100521 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200522 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
523 struct inode_fs_paths *ipath = NULL;
524 struct btrfs_root *local_root;
525 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100526 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200527
528 root_key.objectid = root;
529 root_key.type = BTRFS_ROOT_ITEM_KEY;
530 root_key.offset = (u64)-1;
531 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
532 if (IS_ERR(local_root)) {
533 ret = PTR_ERR(local_root);
534 goto err;
535 }
536
David Sterba14692cc2015-01-02 18:55:46 +0100537 /*
538 * this makes the path point to (inum INODE_ITEM ioff)
539 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100540 key.objectid = inum;
541 key.type = BTRFS_INODE_ITEM_KEY;
542 key.offset = 0;
543
544 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200545 if (ret) {
546 btrfs_release_path(swarn->path);
547 goto err;
548 }
549
550 eb = swarn->path->nodes[0];
551 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
552 struct btrfs_inode_item);
553 isize = btrfs_inode_size(eb, inode_item);
554 nlink = btrfs_inode_nlink(eb, inode_item);
555 btrfs_release_path(swarn->path);
556
557 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300558 if (IS_ERR(ipath)) {
559 ret = PTR_ERR(ipath);
560 ipath = NULL;
561 goto err;
562 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200563 ret = paths_from_inode(inum, ipath);
564
565 if (ret < 0)
566 goto err;
567
568 /*
569 * we deliberately ignore the bit ipath might have been too small to
570 * hold all of the paths here
571 */
572 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Frank Holtonefe120a2013-12-20 11:37:06 -0500573 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200574 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
575 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400576 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200577 (unsigned long long)swarn->sector, root, inum, offset,
578 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500579 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200580
581 free_ipath(ipath);
582 return 0;
583
584err:
Frank Holtonefe120a2013-12-20 11:37:06 -0500585 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200586 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
587 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400588 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200589 (unsigned long long)swarn->sector, root, inum, offset, ret);
590
591 free_ipath(ipath);
592 return 0;
593}
594
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400595static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200596{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100597 struct btrfs_device *dev;
598 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200599 struct btrfs_path *path;
600 struct btrfs_key found_key;
601 struct extent_buffer *eb;
602 struct btrfs_extent_item *ei;
603 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200604 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100605 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600606 u64 flags = 0;
607 u64 ref_root;
608 u32 item_size;
609 u8 ref_level;
Liu Bo69917e42012-09-07 20:01:28 -0600610 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200611
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100612 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100613 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100614 fs_info = sblock->sctx->dev_root->fs_info;
615
Jan Schmidt558540c2011-06-13 19:59:12 +0200616 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200617 if (!path)
618 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200619
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100620 swarn.sector = (sblock->pagev[0]->physical) >> 9;
621 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200622 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100623 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200624
Liu Bo69917e42012-09-07 20:01:28 -0600625 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
626 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200627 if (ret < 0)
628 goto out;
629
Jan Schmidt4692cf52011-12-02 14:56:41 +0100630 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200631 swarn.extent_item_size = found_key.offset;
632
633 eb = path->nodes[0];
634 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
635 item_size = btrfs_item_size_nr(eb, path->slots[0]);
636
Liu Bo69917e42012-09-07 20:01:28 -0600637 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200638 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800639 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
640 item_size, &ref_root,
641 &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400642 printk_in_rcu(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -0500643 "BTRFS: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200644 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400645 "%llu\n", errstr, swarn.logical,
646 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200647 (unsigned long long)swarn.sector,
648 ref_level ? "node" : "leaf",
649 ret < 0 ? -1 : ref_level,
650 ret < 0 ? -1 : ref_root);
651 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600652 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200653 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600654 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200655 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100656 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100657 iterate_extent_inodes(fs_info, found_key.objectid,
658 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200659 scrub_print_warning_inode, &swarn);
660 }
661
662out:
663 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200664}
665
Stefan Behrensff023aa2012-11-06 11:43:11 +0100666static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200667{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200668 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200669 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100670 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200671 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200672 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200673 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200674 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000675 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200676 u64 end = offset + PAGE_SIZE - 1;
677 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000678 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200679
680 key.objectid = root;
681 key.type = BTRFS_ROOT_ITEM_KEY;
682 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000683
684 fs_info = fixup->root->fs_info;
685 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
686
687 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
688 if (IS_ERR(local_root)) {
689 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200690 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000691 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200692
693 key.type = BTRFS_INODE_ITEM_KEY;
694 key.objectid = inum;
695 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000696 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
697 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200698 if (IS_ERR(inode))
699 return PTR_ERR(inode);
700
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200701 index = offset >> PAGE_CACHE_SHIFT;
702
703 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200704 if (!page) {
705 ret = -ENOMEM;
706 goto out;
707 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200708
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200709 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200710 if (PageDirty(page)) {
711 /*
712 * we need to write the data to the defect sector. the
713 * data that was in that sector is not in memory,
714 * because the page was modified. we must not write the
715 * modified page to that sector.
716 *
717 * TODO: what could be done here: wait for the delalloc
718 * runner to write out that page (might involve
719 * COW) and see whether the sector is still
720 * referenced afterwards.
721 *
722 * For the meantime, we'll treat this error
723 * incorrectable, although there is a chance that a
724 * later scrub will find the bad sector again and that
725 * there's no dirty page in memory, then.
726 */
727 ret = -EIO;
728 goto out;
729 }
Miao Xie1203b682014-09-12 18:44:01 +0800730 ret = repair_io_failure(inode, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200731 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800732 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200733 fixup->mirror_num);
734 unlock_page(page);
735 corrected = !ret;
736 } else {
737 /*
738 * we need to get good data first. the general readpage path
739 * will call repair_io_failure for us, we just have to make
740 * sure we read the bad mirror.
741 */
742 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
743 EXTENT_DAMAGED, GFP_NOFS);
744 if (ret) {
745 /* set_extent_bits should give proper error */
746 WARN_ON(ret > 0);
747 if (ret > 0)
748 ret = -EFAULT;
749 goto out;
750 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200751
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200752 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
753 btrfs_get_extent,
754 fixup->mirror_num);
755 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200756
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200757 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
758 end, EXTENT_DAMAGED, 0, NULL);
759 if (!corrected)
760 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
761 EXTENT_DAMAGED, GFP_NOFS);
762 }
763
764out:
765 if (page)
766 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200767
768 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200769
770 if (ret < 0)
771 return ret;
772
773 if (ret == 0 && corrected) {
774 /*
775 * we only need to call readpage for one of the inodes belonging
776 * to this extent. so make iterate_extent_inodes stop
777 */
778 return 1;
779 }
780
781 return -EIO;
782}
783
784static void scrub_fixup_nodatasum(struct btrfs_work *work)
785{
786 int ret;
787 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100788 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200789 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200790 struct btrfs_path *path;
791 int uncorrectable = 0;
792
793 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100794 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200795
796 path = btrfs_alloc_path();
797 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100798 spin_lock(&sctx->stat_lock);
799 ++sctx->stat.malloc_errors;
800 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200801 uncorrectable = 1;
802 goto out;
803 }
804
805 trans = btrfs_join_transaction(fixup->root);
806 if (IS_ERR(trans)) {
807 uncorrectable = 1;
808 goto out;
809 }
810
811 /*
812 * the idea is to trigger a regular read through the standard path. we
813 * read a page from the (failed) logical address by specifying the
814 * corresponding copynum of the failed sector. thus, that readpage is
815 * expected to fail.
816 * that is the point where on-the-fly error correction will kick in
817 * (once it's finished) and rewrite the failed sector if a good copy
818 * can be found.
819 */
820 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
821 path, scrub_fixup_readpage,
822 fixup);
823 if (ret < 0) {
824 uncorrectable = 1;
825 goto out;
826 }
827 WARN_ON(ret != 1);
828
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100829 spin_lock(&sctx->stat_lock);
830 ++sctx->stat.corrected_errors;
831 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200832
833out:
834 if (trans && !IS_ERR(trans))
835 btrfs_end_transaction(trans, fixup->root);
836 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100837 spin_lock(&sctx->stat_lock);
838 ++sctx->stat.uncorrectable_errors;
839 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100840 btrfs_dev_replace_stats_inc(
841 &sctx->dev_root->fs_info->dev_replace.
842 num_uncorrectable_read_errors);
Frank Holtonefe120a2013-12-20 11:37:06 -0500843 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
844 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200845 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200846 }
847
848 btrfs_free_path(path);
849 kfree(fixup);
850
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100851 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200852}
853
Miao Xieaf8e2d12014-10-23 14:42:50 +0800854static inline void scrub_get_recover(struct scrub_recover *recover)
855{
856 atomic_inc(&recover->refs);
857}
858
859static inline void scrub_put_recover(struct scrub_recover *recover)
860{
861 if (atomic_dec_and_test(&recover->refs)) {
Zhao Lei6e9606d2015-01-20 15:11:34 +0800862 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800863 kfree(recover);
864 }
865}
866
Arne Jansena2de7332011-03-08 14:14:00 +0100867/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400868 * scrub_handle_errored_block gets called when either verification of the
869 * pages failed or the bio failed to read, e.g. with EIO. In the latter
870 * case, this function handles all pages in the bio, even though only one
871 * may be bad.
872 * The goal of this function is to repair the errored block by using the
873 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100874 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400875static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100876{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100877 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100878 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400879 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100880 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400881 u64 logical;
882 u64 generation;
883 unsigned int failed_mirror_index;
884 unsigned int is_metadata;
885 unsigned int have_csum;
886 u8 *csum;
887 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
888 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100889 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400890 int mirror_index;
891 int page_num;
892 int success;
893 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
894 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100895
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400896 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100897 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000898 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
899 /*
900 * if we find an error in a super block, we just report it.
901 * They will get written with the next transaction commit
902 * anyway
903 */
904 spin_lock(&sctx->stat_lock);
905 ++sctx->stat.super_errors;
906 spin_unlock(&sctx->stat_lock);
907 return 0;
908 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400909 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100910 logical = sblock_to_check->pagev[0]->logical;
911 generation = sblock_to_check->pagev[0]->generation;
912 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
913 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
914 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400915 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100916 have_csum = sblock_to_check->pagev[0]->have_csum;
917 csum = sblock_to_check->pagev[0]->csum;
918 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400919
Stefan Behrensff023aa2012-11-06 11:43:11 +0100920 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
921 sblocks_for_recheck = NULL;
922 goto nodatasum_case;
923 }
924
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400925 /*
926 * read all mirrors one after the other. This includes to
927 * re-read the extent or metadata block that failed (that was
928 * the cause that this fixup code is called) another time,
929 * page by page this time in order to know which pages
930 * caused I/O errors and which ones are good (for all mirrors).
931 * It is the goal to handle the situation when more than one
932 * mirror contains I/O errors, but the errors do not
933 * overlap, i.e. the data can be repaired by selecting the
934 * pages from those mirrors without I/O error on the
935 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
936 * would be that mirror #1 has an I/O error on the first page,
937 * the second page is good, and mirror #2 has an I/O error on
938 * the second page, but the first page is good.
939 * Then the first page of the first mirror can be repaired by
940 * taking the first page of the second mirror, and the
941 * second page of the second mirror can be repaired by
942 * copying the contents of the 2nd page of the 1st mirror.
943 * One more note: if the pages of one mirror contain I/O
944 * errors, the checksum cannot be verified. In order to get
945 * the best data for repairing, the first attempt is to find
946 * a mirror without I/O errors and with a validated checksum.
947 * Only if this is not possible, the pages are picked from
948 * mirrors with I/O errors without considering the checksum.
949 * If the latter is the case, at the end, the checksum of the
950 * repaired area is verified in order to correctly maintain
951 * the statistics.
952 */
953
David Sterba31e818f2015-02-20 18:00:26 +0100954 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
955 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400956 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100957 spin_lock(&sctx->stat_lock);
958 sctx->stat.malloc_errors++;
959 sctx->stat.read_errors++;
960 sctx->stat.uncorrectable_errors++;
961 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100962 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400963 goto out;
964 }
965
966 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800967 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400968 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100969 spin_lock(&sctx->stat_lock);
970 sctx->stat.read_errors++;
971 sctx->stat.uncorrectable_errors++;
972 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100973 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400974 goto out;
975 }
976 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
977 sblock_bad = sblocks_for_recheck + failed_mirror_index;
978
979 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100980 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800981 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400982
983 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
984 sblock_bad->no_io_error_seen) {
985 /*
986 * the error disappeared after reading page by page, or
987 * the area was part of a huge bio and other parts of the
988 * bio caused I/O errors, or the block layer merged several
989 * read requests into one and the error is caused by a
990 * different bio (usually one of the two latter cases is
991 * the cause)
992 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100993 spin_lock(&sctx->stat_lock);
994 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800995 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100996 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400997
Stefan Behrensff023aa2012-11-06 11:43:11 +0100998 if (sctx->is_dev_replace)
999 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001000 goto out;
1001 }
1002
1003 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001004 spin_lock(&sctx->stat_lock);
1005 sctx->stat.read_errors++;
1006 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001007 if (__ratelimit(&_rs))
1008 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001009 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001010 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001011 spin_lock(&sctx->stat_lock);
1012 sctx->stat.csum_errors++;
1013 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001014 if (__ratelimit(&_rs))
1015 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001016 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001017 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001018 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001019 spin_lock(&sctx->stat_lock);
1020 sctx->stat.verify_errors++;
1021 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001022 if (__ratelimit(&_rs))
1023 scrub_print_warning("checksum/header error",
1024 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001025 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001026 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001027 BTRFS_DEV_STAT_GENERATION_ERRS);
1028 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001029 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001030 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001031 }
1032
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001033 if (sctx->readonly) {
1034 ASSERT(!sctx->is_dev_replace);
1035 goto out;
1036 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001037
1038 if (!is_metadata && !have_csum) {
1039 struct scrub_fixup_nodatasum *fixup_nodatasum;
1040
Stefan Behrensff023aa2012-11-06 11:43:11 +01001041 WARN_ON(sctx->is_dev_replace);
1042
Zhao Leib25c94c2015-01-20 15:11:35 +08001043nodatasum_case:
1044
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001045 /*
1046 * !is_metadata and !have_csum, this means that the data
1047 * might not be COW'ed, that it might be modified
1048 * concurrently. The general strategy to work on the
1049 * commit root does not help in the case when COW is not
1050 * used.
1051 */
1052 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1053 if (!fixup_nodatasum)
1054 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001055 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001056 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001057 fixup_nodatasum->logical = logical;
1058 fixup_nodatasum->root = fs_info->extent_root;
1059 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001060 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001061 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1062 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001063 btrfs_queue_work(fs_info->scrub_workers,
1064 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001065 goto out;
1066 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001067
1068 /*
1069 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001070 * checksums.
1071 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001072 * errors and also does not have a checksum error.
1073 * If one is found, and if a checksum is present, the full block
1074 * that is known to contain an error is rewritten. Afterwards
1075 * the block is known to be corrected.
1076 * If a mirror is found which is completely correct, and no
1077 * checksum is present, only those pages are rewritten that had
1078 * an I/O error in the block to be repaired, since it cannot be
1079 * determined, which copy of the other pages is better (and it
1080 * could happen otherwise that a correct page would be
1081 * overwritten by a bad one).
1082 */
1083 for (mirror_index = 0;
1084 mirror_index < BTRFS_MAX_MIRRORS &&
1085 sblocks_for_recheck[mirror_index].page_count > 0;
1086 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001087 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001088
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001089 if (mirror_index == failed_mirror_index)
1090 continue;
1091 sblock_other = sblocks_for_recheck + mirror_index;
1092
1093 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001094 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1095 have_csum, csum, generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001096 sctx->csum_size, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001097
1098 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001099 !sblock_other->checksum_error &&
1100 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001101 if (sctx->is_dev_replace) {
1102 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001103 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001104 } else {
1105 ret = scrub_repair_block_from_good_copy(
1106 sblock_bad, sblock_other);
1107 if (!ret)
1108 goto corrected_error;
1109 }
Arne Jansena2de7332011-03-08 14:14:00 +01001110 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001111 }
1112
Zhao Leib968fed2015-01-20 15:11:41 +08001113 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1114 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001115
1116 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001117 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001118 * repaired, continue by picking good copies of those pages.
1119 * Select the good pages from mirrors to rewrite bad pages from
1120 * the area to fix. Afterwards verify the checksum of the block
1121 * that is supposed to be repaired. This verification step is
1122 * only done for the purpose of statistic counting and for the
1123 * final scrub report, whether errors remain.
1124 * A perfect algorithm could make use of the checksum and try
1125 * all possible combinations of pages from the different mirrors
1126 * until the checksum verification succeeds. For example, when
1127 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1128 * of mirror #2 is readable but the final checksum test fails,
1129 * then the 2nd page of mirror #3 could be tried, whether now
1130 * the final checksum succeedes. But this would be a rare
1131 * exception and is therefore not implemented. At least it is
1132 * avoided that the good copy is overwritten.
1133 * A more useful improvement would be to pick the sectors
1134 * without I/O error based on sector sizes (512 bytes on legacy
1135 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1136 * mirror could be repaired by taking 512 byte of a different
1137 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1138 * area are unreadable.
1139 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001140 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001141 for (page_num = 0; page_num < sblock_bad->page_count;
1142 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001143 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001144 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001145
Zhao Leib968fed2015-01-20 15:11:41 +08001146 /* skip no-io-error page in scrub */
1147 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001148 continue;
1149
Zhao Leib968fed2015-01-20 15:11:41 +08001150 /* try to find no-io-error page in mirrors */
1151 if (page_bad->io_error) {
1152 for (mirror_index = 0;
1153 mirror_index < BTRFS_MAX_MIRRORS &&
1154 sblocks_for_recheck[mirror_index].page_count > 0;
1155 mirror_index++) {
1156 if (!sblocks_for_recheck[mirror_index].
1157 pagev[page_num]->io_error) {
1158 sblock_other = sblocks_for_recheck +
1159 mirror_index;
1160 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001161 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001162 }
Zhao Leib968fed2015-01-20 15:11:41 +08001163 if (!sblock_other)
1164 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001165 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001166
Zhao Leib968fed2015-01-20 15:11:41 +08001167 if (sctx->is_dev_replace) {
1168 /*
1169 * did not find a mirror to fetch the page
1170 * from. scrub_write_page_to_dev_replace()
1171 * handles this case (page->io_error), by
1172 * filling the block with zeros before
1173 * submitting the write request
1174 */
1175 if (!sblock_other)
1176 sblock_other = sblock_bad;
1177
1178 if (scrub_write_page_to_dev_replace(sblock_other,
1179 page_num) != 0) {
1180 btrfs_dev_replace_stats_inc(
1181 &sctx->dev_root->
1182 fs_info->dev_replace.
1183 num_write_errors);
1184 success = 0;
1185 }
1186 } else if (sblock_other) {
1187 ret = scrub_repair_page_from_good_copy(sblock_bad,
1188 sblock_other,
1189 page_num, 0);
1190 if (0 == ret)
1191 page_bad->io_error = 0;
1192 else
1193 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001194 }
1195 }
1196
Zhao Leib968fed2015-01-20 15:11:41 +08001197 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001198 if (is_metadata || have_csum) {
1199 /*
1200 * need to verify the checksum now that all
1201 * sectors on disk are repaired (the write
1202 * request for data to be repaired is on its way).
1203 * Just be lazy and use scrub_recheck_block()
1204 * which re-reads the data before the checksum
1205 * is verified, but most likely the data comes out
1206 * of the page cache.
1207 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001208 scrub_recheck_block(fs_info, sblock_bad,
1209 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001210 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001211 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001212 !sblock_bad->checksum_error &&
1213 sblock_bad->no_io_error_seen)
1214 goto corrected_error;
1215 else
1216 goto did_not_correct_error;
1217 } else {
1218corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001219 spin_lock(&sctx->stat_lock);
1220 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001221 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001222 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001223 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001224 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001225 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001226 }
1227 } else {
1228did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001229 spin_lock(&sctx->stat_lock);
1230 sctx->stat.uncorrectable_errors++;
1231 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001232 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001233 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001234 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001235 }
1236
1237out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001238 if (sblocks_for_recheck) {
1239 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1240 mirror_index++) {
1241 struct scrub_block *sblock = sblocks_for_recheck +
1242 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001243 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001244 int page_index;
1245
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001246 for (page_index = 0; page_index < sblock->page_count;
1247 page_index++) {
1248 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001249 recover = sblock->pagev[page_index]->recover;
1250 if (recover) {
1251 scrub_put_recover(recover);
1252 sblock->pagev[page_index]->recover =
1253 NULL;
1254 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001255 scrub_page_put(sblock->pagev[page_index]);
1256 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001257 }
1258 kfree(sblocks_for_recheck);
1259 }
1260
1261 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001262}
1263
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001264static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001265{
Zhao Lei10f11902015-01-20 15:11:43 +08001266 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1267 return 2;
1268 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1269 return 3;
1270 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001271 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001272}
1273
Zhao Lei10f11902015-01-20 15:11:43 +08001274static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1275 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001276 u64 mapped_length,
1277 int nstripes, int mirror,
1278 int *stripe_index,
1279 u64 *stripe_offset)
1280{
1281 int i;
1282
Zhao Leiffe2d202015-01-20 15:11:44 +08001283 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001284 /* RAID5/6 */
1285 for (i = 0; i < nstripes; i++) {
1286 if (raid_map[i] == RAID6_Q_STRIPE ||
1287 raid_map[i] == RAID5_P_STRIPE)
1288 continue;
1289
1290 if (logical >= raid_map[i] &&
1291 logical < raid_map[i] + mapped_length)
1292 break;
1293 }
1294
1295 *stripe_index = i;
1296 *stripe_offset = logical - raid_map[i];
1297 } else {
1298 /* The other RAID type */
1299 *stripe_index = mirror;
1300 *stripe_offset = 0;
1301 }
1302}
1303
Zhao Leibe50a8d2015-01-20 15:11:42 +08001304static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001305 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001306{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001307 struct scrub_ctx *sctx = original_sblock->sctx;
1308 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1309 u64 length = original_sblock->page_count * PAGE_SIZE;
1310 u64 logical = original_sblock->pagev[0]->logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001311 struct scrub_recover *recover;
1312 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001313 u64 sublen;
1314 u64 mapped_length;
1315 u64 stripe_offset;
1316 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001317 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001318 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001319 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001320 int ret;
1321
1322 /*
Zhao Lei57019342015-01-20 15:11:45 +08001323 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001324 * are not used (and not set) in the blocks that are used for
1325 * the recheck procedure
1326 */
1327
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001328 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001329 sublen = min_t(u64, length, PAGE_SIZE);
1330 mapped_length = sublen;
1331 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001332
1333 /*
1334 * with a length of PAGE_SIZE, each returned stripe
1335 * represents one mirror
1336 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001337 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001338 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001339 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001340 btrfs_put_bbio(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001341 return -EIO;
1342 }
1343
Miao Xieaf8e2d12014-10-23 14:42:50 +08001344 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1345 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001346 btrfs_put_bbio(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001347 return -ENOMEM;
1348 }
1349
1350 atomic_set(&recover->refs, 1);
1351 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001352 recover->map_length = mapped_length;
1353
Stefan Behrensff023aa2012-11-06 11:43:11 +01001354 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001355
Zhao Leibe50a8d2015-01-20 15:11:42 +08001356 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001357
Miao Xieaf8e2d12014-10-23 14:42:50 +08001358 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001359 mirror_index++) {
1360 struct scrub_block *sblock;
1361 struct scrub_page *page;
1362
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001363 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001364 sblock->sctx = sctx;
1365 page = kzalloc(sizeof(*page), GFP_NOFS);
1366 if (!page) {
1367leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001368 spin_lock(&sctx->stat_lock);
1369 sctx->stat.malloc_errors++;
1370 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001371 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001372 return -ENOMEM;
1373 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001374 scrub_page_get(page);
1375 sblock->pagev[page_index] = page;
1376 page->logical = logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001377
Zhao Lei10f11902015-01-20 15:11:43 +08001378 scrub_stripe_index_and_offset(logical,
1379 bbio->map_type,
1380 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001381 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001382 bbio->num_stripes -
1383 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001384 mirror_index,
1385 &stripe_index,
1386 &stripe_offset);
1387 page->physical = bbio->stripes[stripe_index].physical +
1388 stripe_offset;
1389 page->dev = bbio->stripes[stripe_index].dev;
1390
Stefan Behrensff023aa2012-11-06 11:43:11 +01001391 BUG_ON(page_index >= original_sblock->page_count);
1392 page->physical_for_dev_replace =
1393 original_sblock->pagev[page_index]->
1394 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001395 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001396 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001397 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001398 page->page = alloc_page(GFP_NOFS);
1399 if (!page->page)
1400 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001401
1402 scrub_get_recover(recover);
1403 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001404 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001405 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001406 length -= sublen;
1407 logical += sublen;
1408 page_index++;
1409 }
1410
1411 return 0;
1412}
1413
Miao Xieaf8e2d12014-10-23 14:42:50 +08001414struct scrub_bio_ret {
1415 struct completion event;
1416 int error;
1417};
1418
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001419static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001420{
1421 struct scrub_bio_ret *ret = bio->bi_private;
1422
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001423 ret->error = bio->bi_error;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001424 complete(&ret->event);
1425}
1426
1427static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1428{
Zhao Lei10f11902015-01-20 15:11:43 +08001429 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001430 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001431}
1432
1433static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1434 struct bio *bio,
1435 struct scrub_page *page)
1436{
1437 struct scrub_bio_ret done;
1438 int ret;
1439
1440 init_completion(&done.event);
1441 done.error = 0;
1442 bio->bi_iter.bi_sector = page->logical >> 9;
1443 bio->bi_private = &done;
1444 bio->bi_end_io = scrub_bio_wait_endio;
1445
1446 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001447 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001448 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001449 if (ret)
1450 return ret;
1451
1452 wait_for_completion(&done.event);
1453 if (done.error)
1454 return -EIO;
1455
1456 return 0;
1457}
1458
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001459/*
1460 * this function will check the on disk data for checksum errors, header
1461 * errors and read I/O errors. If any I/O errors happen, the exact pages
1462 * which are errored are marked as being bad. The goal is to enable scrub
1463 * to take those pages that are not errored from all the mirrors so that
1464 * the pages that are errored in the just handled mirror can be repaired.
1465 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001466static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1467 struct scrub_block *sblock, int is_metadata,
1468 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001469 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001470{
1471 int page_num;
1472
1473 sblock->no_io_error_seen = 1;
1474 sblock->header_error = 0;
1475 sblock->checksum_error = 0;
1476
1477 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1478 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001479 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001480
Stefan Behrens442a4f62012-05-25 16:06:08 +02001481 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001482 page->io_error = 1;
1483 sblock->no_io_error_seen = 0;
1484 continue;
1485 }
1486
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001487 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001488 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001489 if (!bio) {
1490 page->io_error = 1;
1491 sblock->no_io_error_seen = 0;
1492 continue;
1493 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001494 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001495
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001496 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001497 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1498 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1499 sblock->no_io_error_seen = 0;
1500 } else {
1501 bio->bi_iter.bi_sector = page->physical >> 9;
1502
1503 if (btrfsic_submit_bio_wait(READ, bio))
1504 sblock->no_io_error_seen = 0;
1505 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001506
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001507 bio_put(bio);
1508 }
1509
1510 if (sblock->no_io_error_seen)
1511 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1512 have_csum, csum, generation,
1513 csum_size);
1514
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001515 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001516}
1517
Miao Xie17a9be22014-07-24 11:37:08 +08001518static inline int scrub_check_fsid(u8 fsid[],
1519 struct scrub_page *spage)
1520{
1521 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1522 int ret;
1523
1524 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1525 return !ret;
1526}
1527
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001528static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1529 struct scrub_block *sblock,
1530 int is_metadata, int have_csum,
1531 const u8 *csum, u64 generation,
1532 u16 csum_size)
1533{
1534 int page_num;
1535 u8 calculated_csum[BTRFS_CSUM_SIZE];
1536 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001537 void *mapped_buffer;
1538
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001539 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001540 if (is_metadata) {
1541 struct btrfs_header *h;
1542
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001543 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001544 h = (struct btrfs_header *)mapped_buffer;
1545
Qu Wenruo3cae2102013-07-16 11:19:18 +08001546 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001547 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001548 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001549 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001550 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001551 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001552 sblock->header_error = 1;
1553 sblock->generation_error = 1;
1554 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001555 csum = h->csum;
1556 } else {
1557 if (!have_csum)
1558 return;
1559
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001560 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001561 }
1562
1563 for (page_num = 0;;) {
1564 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001565 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001566 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1567 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1568 else
Liu Bob0496682013-03-14 14:57:45 +00001569 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001570
Linus Torvalds9613beb2012-03-30 12:44:29 -07001571 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001572 page_num++;
1573 if (page_num >= sblock->page_count)
1574 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001575 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001576
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001577 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001578 }
1579
1580 btrfs_csum_final(crc, calculated_csum);
1581 if (memcmp(calculated_csum, csum, csum_size))
1582 sblock->checksum_error = 1;
1583}
1584
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001585static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001586 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001587{
1588 int page_num;
1589 int ret = 0;
1590
1591 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1592 int ret_sub;
1593
1594 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1595 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001596 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001597 if (ret_sub)
1598 ret = ret_sub;
1599 }
1600
1601 return ret;
1602}
1603
1604static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1605 struct scrub_block *sblock_good,
1606 int page_num, int force_write)
1607{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001608 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1609 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001610
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001611 BUG_ON(page_bad->page == NULL);
1612 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001613 if (force_write || sblock_bad->header_error ||
1614 sblock_bad->checksum_error || page_bad->io_error) {
1615 struct bio *bio;
1616 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001617
Stefan Behrensff023aa2012-11-06 11:43:11 +01001618 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001619 printk_ratelimited(KERN_WARNING "BTRFS: "
1620 "scrub_repair_page_from_good_copy(bdev == NULL) "
1621 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001622 return -EIO;
1623 }
1624
Chris Mason9be33952013-05-17 18:30:14 -04001625 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001626 if (!bio)
1627 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001628 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001629 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001630
1631 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1632 if (PAGE_SIZE != ret) {
1633 bio_put(bio);
1634 return -EIO;
1635 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001636
Kent Overstreet33879d42013-11-23 22:33:32 -08001637 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001638 btrfs_dev_stat_inc_and_print(page_bad->dev,
1639 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001640 btrfs_dev_replace_stats_inc(
1641 &sblock_bad->sctx->dev_root->fs_info->
1642 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001643 bio_put(bio);
1644 return -EIO;
1645 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001646 bio_put(bio);
1647 }
1648
1649 return 0;
1650}
1651
Stefan Behrensff023aa2012-11-06 11:43:11 +01001652static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1653{
1654 int page_num;
1655
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001656 /*
1657 * This block is used for the check of the parity on the source device,
1658 * so the data needn't be written into the destination device.
1659 */
1660 if (sblock->sparity)
1661 return;
1662
Stefan Behrensff023aa2012-11-06 11:43:11 +01001663 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1664 int ret;
1665
1666 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1667 if (ret)
1668 btrfs_dev_replace_stats_inc(
1669 &sblock->sctx->dev_root->fs_info->dev_replace.
1670 num_write_errors);
1671 }
1672}
1673
1674static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1675 int page_num)
1676{
1677 struct scrub_page *spage = sblock->pagev[page_num];
1678
1679 BUG_ON(spage->page == NULL);
1680 if (spage->io_error) {
1681 void *mapped_buffer = kmap_atomic(spage->page);
1682
1683 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1684 flush_dcache_page(spage->page);
1685 kunmap_atomic(mapped_buffer);
1686 }
1687 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1688}
1689
1690static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1691 struct scrub_page *spage)
1692{
1693 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1694 struct scrub_bio *sbio;
1695 int ret;
1696
1697 mutex_lock(&wr_ctx->wr_lock);
1698again:
1699 if (!wr_ctx->wr_curr_bio) {
1700 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1701 GFP_NOFS);
1702 if (!wr_ctx->wr_curr_bio) {
1703 mutex_unlock(&wr_ctx->wr_lock);
1704 return -ENOMEM;
1705 }
1706 wr_ctx->wr_curr_bio->sctx = sctx;
1707 wr_ctx->wr_curr_bio->page_count = 0;
1708 }
1709 sbio = wr_ctx->wr_curr_bio;
1710 if (sbio->page_count == 0) {
1711 struct bio *bio;
1712
1713 sbio->physical = spage->physical_for_dev_replace;
1714 sbio->logical = spage->logical;
1715 sbio->dev = wr_ctx->tgtdev;
1716 bio = sbio->bio;
1717 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001718 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001719 if (!bio) {
1720 mutex_unlock(&wr_ctx->wr_lock);
1721 return -ENOMEM;
1722 }
1723 sbio->bio = bio;
1724 }
1725
1726 bio->bi_private = sbio;
1727 bio->bi_end_io = scrub_wr_bio_end_io;
1728 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001729 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001730 sbio->err = 0;
1731 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1732 spage->physical_for_dev_replace ||
1733 sbio->logical + sbio->page_count * PAGE_SIZE !=
1734 spage->logical) {
1735 scrub_wr_submit(sctx);
1736 goto again;
1737 }
1738
1739 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1740 if (ret != PAGE_SIZE) {
1741 if (sbio->page_count < 1) {
1742 bio_put(sbio->bio);
1743 sbio->bio = NULL;
1744 mutex_unlock(&wr_ctx->wr_lock);
1745 return -EIO;
1746 }
1747 scrub_wr_submit(sctx);
1748 goto again;
1749 }
1750
1751 sbio->pagev[sbio->page_count] = spage;
1752 scrub_page_get(spage);
1753 sbio->page_count++;
1754 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1755 scrub_wr_submit(sctx);
1756 mutex_unlock(&wr_ctx->wr_lock);
1757
1758 return 0;
1759}
1760
1761static void scrub_wr_submit(struct scrub_ctx *sctx)
1762{
1763 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1764 struct scrub_bio *sbio;
1765
1766 if (!wr_ctx->wr_curr_bio)
1767 return;
1768
1769 sbio = wr_ctx->wr_curr_bio;
1770 wr_ctx->wr_curr_bio = NULL;
1771 WARN_ON(!sbio->bio->bi_bdev);
1772 scrub_pending_bio_inc(sctx);
1773 /* process all writes in a single worker thread. Then the block layer
1774 * orders the requests before sending them to the driver which
1775 * doubled the write performance on spinning disks when measured
1776 * with Linux 3.5 */
1777 btrfsic_submit_bio(WRITE, sbio->bio);
1778}
1779
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001780static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001781{
1782 struct scrub_bio *sbio = bio->bi_private;
1783 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1784
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001785 sbio->err = bio->bi_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001786 sbio->bio = bio;
1787
Liu Bo9e0af232014-08-15 23:36:53 +08001788 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1789 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001790 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001791}
1792
1793static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1794{
1795 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1796 struct scrub_ctx *sctx = sbio->sctx;
1797 int i;
1798
1799 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1800 if (sbio->err) {
1801 struct btrfs_dev_replace *dev_replace =
1802 &sbio->sctx->dev_root->fs_info->dev_replace;
1803
1804 for (i = 0; i < sbio->page_count; i++) {
1805 struct scrub_page *spage = sbio->pagev[i];
1806
1807 spage->io_error = 1;
1808 btrfs_dev_replace_stats_inc(&dev_replace->
1809 num_write_errors);
1810 }
1811 }
1812
1813 for (i = 0; i < sbio->page_count; i++)
1814 scrub_page_put(sbio->pagev[i]);
1815
1816 bio_put(sbio->bio);
1817 kfree(sbio);
1818 scrub_pending_bio_dec(sctx);
1819}
1820
1821static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001822{
1823 u64 flags;
1824 int ret;
1825
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001826 WARN_ON(sblock->page_count < 1);
1827 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001828 ret = 0;
1829 if (flags & BTRFS_EXTENT_FLAG_DATA)
1830 ret = scrub_checksum_data(sblock);
1831 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1832 ret = scrub_checksum_tree_block(sblock);
1833 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1834 (void)scrub_checksum_super(sblock);
1835 else
1836 WARN_ON(1);
1837 if (ret)
1838 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001839
1840 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001841}
1842
1843static int scrub_checksum_data(struct scrub_block *sblock)
1844{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001845 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001846 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001847 u8 *on_disk_csum;
1848 struct page *page;
1849 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001850 u32 crc = ~(u32)0;
1851 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001852 u64 len;
1853 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001854
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001855 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001856 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001857 return 0;
1858
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001859 on_disk_csum = sblock->pagev[0]->csum;
1860 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001861 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001862
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001863 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001864 index = 0;
1865 for (;;) {
1866 u64 l = min_t(u64, len, PAGE_SIZE);
1867
Liu Bob0496682013-03-14 14:57:45 +00001868 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001869 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001870 len -= l;
1871 if (len == 0)
1872 break;
1873 index++;
1874 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001875 BUG_ON(!sblock->pagev[index]->page);
1876 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001877 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001878 }
1879
Arne Jansena2de7332011-03-08 14:14:00 +01001880 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001881 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001882 fail = 1;
1883
Arne Jansena2de7332011-03-08 14:14:00 +01001884 return fail;
1885}
1886
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001887static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001888{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001889 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001890 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001891 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001892 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001893 u8 calculated_csum[BTRFS_CSUM_SIZE];
1894 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1895 struct page *page;
1896 void *mapped_buffer;
1897 u64 mapped_size;
1898 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001899 u32 crc = ~(u32)0;
1900 int fail = 0;
1901 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001902 u64 len;
1903 int index;
1904
1905 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001906 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001907 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001908 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001909 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001910
1911 /*
1912 * we don't use the getter functions here, as we
1913 * a) don't have an extent buffer and
1914 * b) the page is already kmapped
1915 */
Arne Jansena2de7332011-03-08 14:14:00 +01001916
Qu Wenruo3cae2102013-07-16 11:19:18 +08001917 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001918 ++fail;
1919
Qu Wenruo3cae2102013-07-16 11:19:18 +08001920 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001921 ++fail;
1922
Miao Xie17a9be22014-07-24 11:37:08 +08001923 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001924 ++fail;
1925
1926 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1927 BTRFS_UUID_SIZE))
1928 ++fail;
1929
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001930 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001931 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1932 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1933 index = 0;
1934 for (;;) {
1935 u64 l = min_t(u64, len, mapped_size);
1936
Liu Bob0496682013-03-14 14:57:45 +00001937 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001938 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001939 len -= l;
1940 if (len == 0)
1941 break;
1942 index++;
1943 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001944 BUG_ON(!sblock->pagev[index]->page);
1945 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001946 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001947 mapped_size = PAGE_SIZE;
1948 p = mapped_buffer;
1949 }
1950
1951 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001952 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001953 ++crc_fail;
1954
Arne Jansena2de7332011-03-08 14:14:00 +01001955 return fail || crc_fail;
1956}
1957
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001958static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001959{
1960 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001961 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001962 u8 calculated_csum[BTRFS_CSUM_SIZE];
1963 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1964 struct page *page;
1965 void *mapped_buffer;
1966 u64 mapped_size;
1967 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001968 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001969 int fail_gen = 0;
1970 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001971 u64 len;
1972 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001973
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001974 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001975 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001976 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001977 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001978 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001979
Qu Wenruo3cae2102013-07-16 11:19:18 +08001980 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001981 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001982
Qu Wenruo3cae2102013-07-16 11:19:18 +08001983 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001984 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001985
Miao Xie17a9be22014-07-24 11:37:08 +08001986 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001987 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001988
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001989 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1990 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1991 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1992 index = 0;
1993 for (;;) {
1994 u64 l = min_t(u64, len, mapped_size);
1995
Liu Bob0496682013-03-14 14:57:45 +00001996 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001997 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001998 len -= l;
1999 if (len == 0)
2000 break;
2001 index++;
2002 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002003 BUG_ON(!sblock->pagev[index]->page);
2004 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002005 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002006 mapped_size = PAGE_SIZE;
2007 p = mapped_buffer;
2008 }
2009
2010 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002011 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002012 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002013
Stefan Behrens442a4f62012-05-25 16:06:08 +02002014 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002015 /*
2016 * if we find an error in a super block, we just report it.
2017 * They will get written with the next transaction commit
2018 * anyway
2019 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002020 spin_lock(&sctx->stat_lock);
2021 ++sctx->stat.super_errors;
2022 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002023 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002024 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002025 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2026 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002027 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002028 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002029 }
2030
Stefan Behrens442a4f62012-05-25 16:06:08 +02002031 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002032}
2033
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002034static void scrub_block_get(struct scrub_block *sblock)
2035{
Zhao Lei57019342015-01-20 15:11:45 +08002036 atomic_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002037}
2038
2039static void scrub_block_put(struct scrub_block *sblock)
2040{
Zhao Lei57019342015-01-20 15:11:45 +08002041 if (atomic_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002042 int i;
2043
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002044 if (sblock->sparity)
2045 scrub_parity_put(sblock->sparity);
2046
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002047 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002048 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002049 kfree(sblock);
2050 }
2051}
2052
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002053static void scrub_page_get(struct scrub_page *spage)
2054{
Zhao Lei57019342015-01-20 15:11:45 +08002055 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002056}
2057
2058static void scrub_page_put(struct scrub_page *spage)
2059{
Zhao Lei57019342015-01-20 15:11:45 +08002060 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002061 if (spage->page)
2062 __free_page(spage->page);
2063 kfree(spage);
2064 }
2065}
2066
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002067static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002068{
2069 struct scrub_bio *sbio;
2070
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002071 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002072 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002073
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002074 sbio = sctx->bios[sctx->curr];
2075 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002076 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002077
Stefan Behrensff023aa2012-11-06 11:43:11 +01002078 if (!sbio->bio->bi_bdev) {
2079 /*
2080 * this case should not happen. If btrfs_map_block() is
2081 * wrong, it could happen for dev-replace operations on
2082 * missing devices when no mirrors are available, but in
2083 * this case it should already fail the mount.
2084 * This case is handled correctly (but _very_ slowly).
2085 */
2086 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05002087 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002088 bio_io_error(sbio->bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002089 } else {
2090 btrfsic_submit_bio(READ, sbio->bio);
2091 }
Arne Jansena2de7332011-03-08 14:14:00 +01002092}
2093
Stefan Behrensff023aa2012-11-06 11:43:11 +01002094static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2095 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002096{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002097 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002098 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002099 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002100
2101again:
2102 /*
2103 * grab a fresh bio or wait for one to become available
2104 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002105 while (sctx->curr == -1) {
2106 spin_lock(&sctx->list_lock);
2107 sctx->curr = sctx->first_free;
2108 if (sctx->curr != -1) {
2109 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2110 sctx->bios[sctx->curr]->next_free = -1;
2111 sctx->bios[sctx->curr]->page_count = 0;
2112 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002113 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002114 spin_unlock(&sctx->list_lock);
2115 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002116 }
2117 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002118 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002119 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002120 struct bio *bio;
2121
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002122 sbio->physical = spage->physical;
2123 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002124 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002125 bio = sbio->bio;
2126 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002127 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002128 if (!bio)
2129 return -ENOMEM;
2130 sbio->bio = bio;
2131 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002132
2133 bio->bi_private = sbio;
2134 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002135 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002136 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002137 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002138 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2139 spage->physical ||
2140 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002141 spage->logical ||
2142 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002143 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002144 goto again;
2145 }
2146
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002147 sbio->pagev[sbio->page_count] = spage;
2148 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2149 if (ret != PAGE_SIZE) {
2150 if (sbio->page_count < 1) {
2151 bio_put(sbio->bio);
2152 sbio->bio = NULL;
2153 return -EIO;
2154 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002155 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002156 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002157 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002158
Stefan Behrensff023aa2012-11-06 11:43:11 +01002159 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002160 atomic_inc(&sblock->outstanding_pages);
2161 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002162 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002163 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002164
2165 return 0;
2166}
2167
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002168static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002169 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002170 u64 gen, int mirror_num, u8 *csum, int force,
2171 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002172{
2173 struct scrub_block *sblock;
2174 int index;
2175
2176 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2177 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002178 spin_lock(&sctx->stat_lock);
2179 sctx->stat.malloc_errors++;
2180 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002181 return -ENOMEM;
2182 }
2183
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002184 /* one ref inside this function, plus one for each page added to
2185 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002186 atomic_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002187 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002188 sblock->no_io_error_seen = 1;
2189
2190 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002191 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002192 u64 l = min_t(u64, len, PAGE_SIZE);
2193
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002194 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2195 if (!spage) {
2196leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002197 spin_lock(&sctx->stat_lock);
2198 sctx->stat.malloc_errors++;
2199 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002200 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002201 return -ENOMEM;
2202 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002203 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2204 scrub_page_get(spage);
2205 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002206 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002207 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002208 spage->flags = flags;
2209 spage->generation = gen;
2210 spage->logical = logical;
2211 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002212 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002213 spage->mirror_num = mirror_num;
2214 if (csum) {
2215 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002216 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002217 } else {
2218 spage->have_csum = 0;
2219 }
2220 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002221 spage->page = alloc_page(GFP_NOFS);
2222 if (!spage->page)
2223 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002224 len -= l;
2225 logical += l;
2226 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002227 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002228 }
2229
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002230 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002231 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002232 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002233 int ret;
2234
Stefan Behrensff023aa2012-11-06 11:43:11 +01002235 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002236 if (ret) {
2237 scrub_block_put(sblock);
2238 return ret;
2239 }
2240 }
2241
2242 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002243 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002244
2245 /* last one frees, either here or in bio completion for last page */
2246 scrub_block_put(sblock);
2247 return 0;
2248}
2249
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002250static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002251{
2252 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002253 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002254
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002255 sbio->err = bio->bi_error;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002256 sbio->bio = bio;
2257
Qu Wenruo0339ef22014-02-28 10:46:17 +08002258 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002259}
2260
2261static void scrub_bio_end_io_worker(struct btrfs_work *work)
2262{
2263 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002264 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002265 int i;
2266
Stefan Behrensff023aa2012-11-06 11:43:11 +01002267 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002268 if (sbio->err) {
2269 for (i = 0; i < sbio->page_count; i++) {
2270 struct scrub_page *spage = sbio->pagev[i];
2271
2272 spage->io_error = 1;
2273 spage->sblock->no_io_error_seen = 0;
2274 }
2275 }
2276
2277 /* now complete the scrub_block items that have all pages completed */
2278 for (i = 0; i < sbio->page_count; i++) {
2279 struct scrub_page *spage = sbio->pagev[i];
2280 struct scrub_block *sblock = spage->sblock;
2281
2282 if (atomic_dec_and_test(&sblock->outstanding_pages))
2283 scrub_block_complete(sblock);
2284 scrub_block_put(sblock);
2285 }
2286
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002287 bio_put(sbio->bio);
2288 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002289 spin_lock(&sctx->list_lock);
2290 sbio->next_free = sctx->first_free;
2291 sctx->first_free = sbio->index;
2292 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002293
2294 if (sctx->is_dev_replace &&
2295 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2296 mutex_lock(&sctx->wr_ctx.wr_lock);
2297 scrub_wr_submit(sctx);
2298 mutex_unlock(&sctx->wr_ctx.wr_lock);
2299 }
2300
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002301 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002302}
2303
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002304static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2305 unsigned long *bitmap,
2306 u64 start, u64 len)
2307{
David Sterba9d644a62015-02-20 18:42:11 +01002308 u32 offset;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002309 int nsectors;
2310 int sectorsize = sparity->sctx->dev_root->sectorsize;
2311
2312 if (len >= sparity->stripe_len) {
2313 bitmap_set(bitmap, 0, sparity->nsectors);
2314 return;
2315 }
2316
2317 start -= sparity->logic_start;
David Sterba47c57132015-02-20 18:43:47 +01002318 start = div_u64_rem(start, sparity->stripe_len, &offset);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002319 offset /= sectorsize;
2320 nsectors = (int)len / sectorsize;
2321
2322 if (offset + nsectors <= sparity->nsectors) {
2323 bitmap_set(bitmap, offset, nsectors);
2324 return;
2325 }
2326
2327 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2328 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2329}
2330
2331static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2332 u64 start, u64 len)
2333{
2334 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2335}
2336
2337static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2338 u64 start, u64 len)
2339{
2340 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2341}
2342
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002343static void scrub_block_complete(struct scrub_block *sblock)
2344{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002345 int corrupted = 0;
2346
Stefan Behrensff023aa2012-11-06 11:43:11 +01002347 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002348 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002349 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002350 } else {
2351 /*
2352 * if has checksum error, write via repair mechanism in
2353 * dev replace case, otherwise write here in dev replace
2354 * case.
2355 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002356 corrupted = scrub_checksum(sblock);
2357 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002358 scrub_write_block_to_dev_replace(sblock);
2359 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002360
2361 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2362 u64 start = sblock->pagev[0]->logical;
2363 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2364 PAGE_SIZE;
2365
2366 scrub_parity_mark_sectors_error(sblock->sparity,
2367 start, end - start);
2368 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002369}
2370
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002371static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002372 u8 *csum)
2373{
2374 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002375 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002376 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002377
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002378 while (!list_empty(&sctx->csum_list)) {
2379 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002380 struct btrfs_ordered_sum, list);
2381 if (sum->bytenr > logical)
2382 return 0;
2383 if (sum->bytenr + sum->len > logical)
2384 break;
2385
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002386 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002387 list_del(&sum->list);
2388 kfree(sum);
2389 sum = NULL;
2390 }
2391 if (!sum)
2392 return 0;
2393
Miao Xief51a4a12013-06-19 10:36:09 +08002394 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002395 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002396 memcpy(csum, sum->sums + index, sctx->csum_size);
2397 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002398 list_del(&sum->list);
2399 kfree(sum);
2400 }
Miao Xief51a4a12013-06-19 10:36:09 +08002401 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002402}
2403
2404/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002405static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002406 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002407 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002408{
2409 int ret;
2410 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002411 u32 blocksize;
2412
2413 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002414 blocksize = sctx->sectorsize;
2415 spin_lock(&sctx->stat_lock);
2416 sctx->stat.data_extents_scrubbed++;
2417 sctx->stat.data_bytes_scrubbed += len;
2418 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002419 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002420 blocksize = sctx->nodesize;
2421 spin_lock(&sctx->stat_lock);
2422 sctx->stat.tree_extents_scrubbed++;
2423 sctx->stat.tree_bytes_scrubbed += len;
2424 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002425 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002426 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002427 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002428 }
Arne Jansena2de7332011-03-08 14:14:00 +01002429
2430 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002431 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002432 int have_csum = 0;
2433
2434 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2435 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002436 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002437 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002438 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002439 if (sctx->is_dev_replace && !have_csum) {
2440 ret = copy_nocow_pages(sctx, logical, l,
2441 mirror_num,
2442 physical_for_dev_replace);
2443 goto behind_scrub_pages;
2444 }
Arne Jansena2de7332011-03-08 14:14:00 +01002445 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002446 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002447 mirror_num, have_csum ? csum : NULL, 0,
2448 physical_for_dev_replace);
2449behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002450 if (ret)
2451 return ret;
2452 len -= l;
2453 logical += l;
2454 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002455 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002456 }
2457 return 0;
2458}
2459
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002460static int scrub_pages_for_parity(struct scrub_parity *sparity,
2461 u64 logical, u64 len,
2462 u64 physical, struct btrfs_device *dev,
2463 u64 flags, u64 gen, int mirror_num, u8 *csum)
2464{
2465 struct scrub_ctx *sctx = sparity->sctx;
2466 struct scrub_block *sblock;
2467 int index;
2468
2469 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2470 if (!sblock) {
2471 spin_lock(&sctx->stat_lock);
2472 sctx->stat.malloc_errors++;
2473 spin_unlock(&sctx->stat_lock);
2474 return -ENOMEM;
2475 }
2476
2477 /* one ref inside this function, plus one for each page added to
2478 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002479 atomic_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002480 sblock->sctx = sctx;
2481 sblock->no_io_error_seen = 1;
2482 sblock->sparity = sparity;
2483 scrub_parity_get(sparity);
2484
2485 for (index = 0; len > 0; index++) {
2486 struct scrub_page *spage;
2487 u64 l = min_t(u64, len, PAGE_SIZE);
2488
2489 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2490 if (!spage) {
2491leave_nomem:
2492 spin_lock(&sctx->stat_lock);
2493 sctx->stat.malloc_errors++;
2494 spin_unlock(&sctx->stat_lock);
2495 scrub_block_put(sblock);
2496 return -ENOMEM;
2497 }
2498 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2499 /* For scrub block */
2500 scrub_page_get(spage);
2501 sblock->pagev[index] = spage;
2502 /* For scrub parity */
2503 scrub_page_get(spage);
2504 list_add_tail(&spage->list, &sparity->spages);
2505 spage->sblock = sblock;
2506 spage->dev = dev;
2507 spage->flags = flags;
2508 spage->generation = gen;
2509 spage->logical = logical;
2510 spage->physical = physical;
2511 spage->mirror_num = mirror_num;
2512 if (csum) {
2513 spage->have_csum = 1;
2514 memcpy(spage->csum, csum, sctx->csum_size);
2515 } else {
2516 spage->have_csum = 0;
2517 }
2518 sblock->page_count++;
2519 spage->page = alloc_page(GFP_NOFS);
2520 if (!spage->page)
2521 goto leave_nomem;
2522 len -= l;
2523 logical += l;
2524 physical += l;
2525 }
2526
2527 WARN_ON(sblock->page_count == 0);
2528 for (index = 0; index < sblock->page_count; index++) {
2529 struct scrub_page *spage = sblock->pagev[index];
2530 int ret;
2531
2532 ret = scrub_add_page_to_rd_bio(sctx, spage);
2533 if (ret) {
2534 scrub_block_put(sblock);
2535 return ret;
2536 }
2537 }
2538
2539 /* last one frees, either here or in bio completion for last page */
2540 scrub_block_put(sblock);
2541 return 0;
2542}
2543
2544static int scrub_extent_for_parity(struct scrub_parity *sparity,
2545 u64 logical, u64 len,
2546 u64 physical, struct btrfs_device *dev,
2547 u64 flags, u64 gen, int mirror_num)
2548{
2549 struct scrub_ctx *sctx = sparity->sctx;
2550 int ret;
2551 u8 csum[BTRFS_CSUM_SIZE];
2552 u32 blocksize;
2553
2554 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2555 blocksize = sctx->sectorsize;
2556 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2557 blocksize = sctx->nodesize;
2558 } else {
2559 blocksize = sctx->sectorsize;
2560 WARN_ON(1);
2561 }
2562
2563 while (len) {
2564 u64 l = min_t(u64, len, blocksize);
2565 int have_csum = 0;
2566
2567 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2568 /* push csums to sbio */
2569 have_csum = scrub_find_csum(sctx, logical, l, csum);
2570 if (have_csum == 0)
2571 goto skip;
2572 }
2573 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2574 flags, gen, mirror_num,
2575 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002576 if (ret)
2577 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002578skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002579 len -= l;
2580 logical += l;
2581 physical += l;
2582 }
2583 return 0;
2584}
2585
Wang Shilong3b080b22014-04-01 18:01:43 +08002586/*
2587 * Given a physical address, this will calculate it's
2588 * logical offset. if this is a parity stripe, it will return
2589 * the most left data stripe's logical offset.
2590 *
2591 * return 0 if it is a data stripe, 1 means parity stripe.
2592 */
2593static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002594 struct map_lookup *map, u64 *offset,
2595 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002596{
2597 int i;
2598 int j = 0;
2599 u64 stripe_nr;
2600 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002601 u32 stripe_index;
2602 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002603
2604 last_offset = (physical - map->stripes[num].physical) *
2605 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002606 if (stripe_start)
2607 *stripe_start = last_offset;
2608
Wang Shilong3b080b22014-04-01 18:01:43 +08002609 *offset = last_offset;
2610 for (i = 0; i < nr_data_stripes(map); i++) {
2611 *offset = last_offset + i * map->stripe_len;
2612
David Sterbab8b93ad2015-01-16 17:26:13 +01002613 stripe_nr = div_u64(*offset, map->stripe_len);
2614 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002615
2616 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002617 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002618 /* calculate which stripe this data locates */
2619 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002620 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002621 if (stripe_index == num)
2622 return 0;
2623 if (stripe_index < num)
2624 j++;
2625 }
2626 *offset = last_offset + j * map->stripe_len;
2627 return 1;
2628}
2629
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002630static void scrub_free_parity(struct scrub_parity *sparity)
2631{
2632 struct scrub_ctx *sctx = sparity->sctx;
2633 struct scrub_page *curr, *next;
2634 int nbits;
2635
2636 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2637 if (nbits) {
2638 spin_lock(&sctx->stat_lock);
2639 sctx->stat.read_errors += nbits;
2640 sctx->stat.uncorrectable_errors += nbits;
2641 spin_unlock(&sctx->stat_lock);
2642 }
2643
2644 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2645 list_del_init(&curr->list);
2646 scrub_page_put(curr);
2647 }
2648
2649 kfree(sparity);
2650}
2651
Zhao Lei20b2e302015-06-04 20:09:15 +08002652static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2653{
2654 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2655 work);
2656 struct scrub_ctx *sctx = sparity->sctx;
2657
2658 scrub_free_parity(sparity);
2659 scrub_pending_bio_dec(sctx);
2660}
2661
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002662static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002663{
2664 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002665
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002666 if (bio->bi_error)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002667 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2668 sparity->nsectors);
2669
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002670 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002671
2672 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2673 scrub_parity_bio_endio_worker, NULL, NULL);
2674 btrfs_queue_work(sparity->sctx->dev_root->fs_info->scrub_parity_workers,
2675 &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002676}
2677
2678static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2679{
2680 struct scrub_ctx *sctx = sparity->sctx;
2681 struct bio *bio;
2682 struct btrfs_raid_bio *rbio;
2683 struct scrub_page *spage;
2684 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002685 u64 length;
2686 int ret;
2687
2688 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2689 sparity->nsectors))
2690 goto out;
2691
2692 length = sparity->logic_end - sparity->logic_start + 1;
Miao Xie76035972014-11-14 17:45:42 +08002693 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002694 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002695 &length, &bbio, 0, 1);
2696 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002697 goto bbio_out;
2698
2699 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2700 if (!bio)
2701 goto bbio_out;
2702
2703 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2704 bio->bi_private = sparity;
2705 bio->bi_end_io = scrub_parity_bio_endio;
2706
2707 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002708 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002709 sparity->dbitmap,
2710 sparity->nsectors);
2711 if (!rbio)
2712 goto rbio_out;
2713
2714 list_for_each_entry(spage, &sparity->spages, list)
2715 raid56_parity_add_scrub_pages(rbio, spage->page,
2716 spage->logical);
2717
2718 scrub_pending_bio_inc(sctx);
2719 raid56_parity_submit_scrub_rbio(rbio);
2720 return;
2721
2722rbio_out:
2723 bio_put(bio);
2724bbio_out:
Zhao Lei6e9606d2015-01-20 15:11:34 +08002725 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002726 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2727 sparity->nsectors);
2728 spin_lock(&sctx->stat_lock);
2729 sctx->stat.malloc_errors++;
2730 spin_unlock(&sctx->stat_lock);
2731out:
2732 scrub_free_parity(sparity);
2733}
2734
2735static inline int scrub_calc_parity_bitmap_len(int nsectors)
2736{
2737 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2738}
2739
2740static void scrub_parity_get(struct scrub_parity *sparity)
2741{
Zhao Lei57019342015-01-20 15:11:45 +08002742 atomic_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002743}
2744
2745static void scrub_parity_put(struct scrub_parity *sparity)
2746{
Zhao Lei57019342015-01-20 15:11:45 +08002747 if (!atomic_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002748 return;
2749
2750 scrub_parity_check_and_repair(sparity);
2751}
2752
2753static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2754 struct map_lookup *map,
2755 struct btrfs_device *sdev,
2756 struct btrfs_path *path,
2757 u64 logic_start,
2758 u64 logic_end)
2759{
2760 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2761 struct btrfs_root *root = fs_info->extent_root;
2762 struct btrfs_root *csum_root = fs_info->csum_root;
2763 struct btrfs_extent_item *extent;
2764 u64 flags;
2765 int ret;
2766 int slot;
2767 struct extent_buffer *l;
2768 struct btrfs_key key;
2769 u64 generation;
2770 u64 extent_logical;
2771 u64 extent_physical;
2772 u64 extent_len;
2773 struct btrfs_device *extent_dev;
2774 struct scrub_parity *sparity;
2775 int nsectors;
2776 int bitmap_len;
2777 int extent_mirror_num;
2778 int stop_loop = 0;
2779
2780 nsectors = map->stripe_len / root->sectorsize;
2781 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2782 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2783 GFP_NOFS);
2784 if (!sparity) {
2785 spin_lock(&sctx->stat_lock);
2786 sctx->stat.malloc_errors++;
2787 spin_unlock(&sctx->stat_lock);
2788 return -ENOMEM;
2789 }
2790
2791 sparity->stripe_len = map->stripe_len;
2792 sparity->nsectors = nsectors;
2793 sparity->sctx = sctx;
2794 sparity->scrub_dev = sdev;
2795 sparity->logic_start = logic_start;
2796 sparity->logic_end = logic_end;
Zhao Lei57019342015-01-20 15:11:45 +08002797 atomic_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002798 INIT_LIST_HEAD(&sparity->spages);
2799 sparity->dbitmap = sparity->bitmap;
2800 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2801
2802 ret = 0;
2803 while (logic_start < logic_end) {
2804 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2805 key.type = BTRFS_METADATA_ITEM_KEY;
2806 else
2807 key.type = BTRFS_EXTENT_ITEM_KEY;
2808 key.objectid = logic_start;
2809 key.offset = (u64)-1;
2810
2811 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2812 if (ret < 0)
2813 goto out;
2814
2815 if (ret > 0) {
2816 ret = btrfs_previous_extent_item(root, path, 0);
2817 if (ret < 0)
2818 goto out;
2819 if (ret > 0) {
2820 btrfs_release_path(path);
2821 ret = btrfs_search_slot(NULL, root, &key,
2822 path, 0, 0);
2823 if (ret < 0)
2824 goto out;
2825 }
2826 }
2827
2828 stop_loop = 0;
2829 while (1) {
2830 u64 bytes;
2831
2832 l = path->nodes[0];
2833 slot = path->slots[0];
2834 if (slot >= btrfs_header_nritems(l)) {
2835 ret = btrfs_next_leaf(root, path);
2836 if (ret == 0)
2837 continue;
2838 if (ret < 0)
2839 goto out;
2840
2841 stop_loop = 1;
2842 break;
2843 }
2844 btrfs_item_key_to_cpu(l, &key, slot);
2845
2846 if (key.type == BTRFS_METADATA_ITEM_KEY)
2847 bytes = root->nodesize;
2848 else
2849 bytes = key.offset;
2850
2851 if (key.objectid + bytes <= logic_start)
2852 goto next;
2853
2854 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2855 key.type != BTRFS_METADATA_ITEM_KEY)
2856 goto next;
2857
2858 if (key.objectid > logic_end) {
2859 stop_loop = 1;
2860 break;
2861 }
2862
2863 while (key.objectid >= logic_start + map->stripe_len)
2864 logic_start += map->stripe_len;
2865
2866 extent = btrfs_item_ptr(l, slot,
2867 struct btrfs_extent_item);
2868 flags = btrfs_extent_flags(l, extent);
2869 generation = btrfs_extent_generation(l, extent);
2870
2871 if (key.objectid < logic_start &&
2872 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2873 btrfs_err(fs_info,
2874 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2875 key.objectid, logic_start);
2876 goto next;
2877 }
2878again:
2879 extent_logical = key.objectid;
2880 extent_len = bytes;
2881
2882 if (extent_logical < logic_start) {
2883 extent_len -= logic_start - extent_logical;
2884 extent_logical = logic_start;
2885 }
2886
2887 if (extent_logical + extent_len >
2888 logic_start + map->stripe_len)
2889 extent_len = logic_start + map->stripe_len -
2890 extent_logical;
2891
2892 scrub_parity_mark_sectors_data(sparity, extent_logical,
2893 extent_len);
2894
2895 scrub_remap_extent(fs_info, extent_logical,
2896 extent_len, &extent_physical,
2897 &extent_dev,
2898 &extent_mirror_num);
2899
2900 ret = btrfs_lookup_csums_range(csum_root,
2901 extent_logical,
2902 extent_logical + extent_len - 1,
2903 &sctx->csum_list, 1);
2904 if (ret)
2905 goto out;
2906
2907 ret = scrub_extent_for_parity(sparity, extent_logical,
2908 extent_len,
2909 extent_physical,
2910 extent_dev, flags,
2911 generation,
2912 extent_mirror_num);
2913 if (ret)
2914 goto out;
2915
2916 scrub_free_csums(sctx);
2917 if (extent_logical + extent_len <
2918 key.objectid + bytes) {
2919 logic_start += map->stripe_len;
2920
2921 if (logic_start >= logic_end) {
2922 stop_loop = 1;
2923 break;
2924 }
2925
2926 if (logic_start < key.objectid + bytes) {
2927 cond_resched();
2928 goto again;
2929 }
2930 }
2931next:
2932 path->slots[0]++;
2933 }
2934
2935 btrfs_release_path(path);
2936
2937 if (stop_loop)
2938 break;
2939
2940 logic_start += map->stripe_len;
2941 }
2942out:
2943 if (ret < 0)
2944 scrub_parity_mark_sectors_error(sparity, logic_start,
2945 logic_end - logic_start + 1);
2946 scrub_parity_put(sparity);
2947 scrub_submit(sctx);
2948 mutex_lock(&sctx->wr_ctx.wr_lock);
2949 scrub_wr_submit(sctx);
2950 mutex_unlock(&sctx->wr_ctx.wr_lock);
2951
2952 btrfs_release_path(path);
2953 return ret < 0 ? ret : 0;
2954}
2955
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002956static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002957 struct map_lookup *map,
2958 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002959 int num, u64 base, u64 length,
2960 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002961{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002962 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002963 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002964 struct btrfs_root *root = fs_info->extent_root;
2965 struct btrfs_root *csum_root = fs_info->csum_root;
2966 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002967 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002968 u64 flags;
2969 int ret;
2970 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002971 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002972 struct extent_buffer *l;
2973 struct btrfs_key key;
2974 u64 physical;
2975 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002976 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08002977 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002978 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002979 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002980 struct reada_control *reada1;
2981 struct reada_control *reada2;
2982 struct btrfs_key key_start;
2983 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002984 u64 increment = map->stripe_len;
2985 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002986 u64 extent_logical;
2987 u64 extent_physical;
2988 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002989 u64 stripe_logical;
2990 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002991 struct btrfs_device *extent_dev;
2992 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08002993 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002994
Wang Shilong3b080b22014-04-01 18:01:43 +08002995 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01002996 offset = 0;
David Sterbab8b93ad2015-01-16 17:26:13 +01002997 nstripes = div_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01002998 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2999 offset = map->stripe_len * num;
3000 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003001 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003002 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3003 int factor = map->num_stripes / map->sub_stripes;
3004 offset = map->stripe_len * (num / map->sub_stripes);
3005 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003006 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003007 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3008 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003009 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003010 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3011 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003012 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003013 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003014 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003015 increment = map->stripe_len * nr_data_stripes(map);
3016 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003017 } else {
3018 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003019 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003020 }
3021
3022 path = btrfs_alloc_path();
3023 if (!path)
3024 return -ENOMEM;
3025
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003026 ppath = btrfs_alloc_path();
3027 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003028 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003029 return -ENOMEM;
3030 }
3031
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003032 /*
3033 * work on commit root. The related disk blocks are static as
3034 * long as COW is applied. This means, it is save to rewrite
3035 * them to repair disk errors without any race conditions
3036 */
Arne Jansena2de7332011-03-08 14:14:00 +01003037 path->search_commit_root = 1;
3038 path->skip_locking = 1;
3039
Gui Hecheng063c54d2015-01-09 09:39:40 +08003040 ppath->search_commit_root = 1;
3041 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003042 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003043 * trigger the readahead for extent tree csum tree and wait for
3044 * completion. During readahead, the scrub is officially paused
3045 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003046 */
3047 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003048 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003049 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003050 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003051 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003052 logic_end += base;
3053 } else {
3054 logic_end = logical + increment * nstripes;
3055 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003056 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003057 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003058 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003059
Arne Jansen7a262852011-06-10 12:39:23 +02003060 /* FIXME it might be better to start readahead at commit root */
3061 key_start.objectid = logical;
3062 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3063 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003064 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003065 key_end.type = BTRFS_METADATA_ITEM_KEY;
3066 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003067 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003068
Arne Jansen7a262852011-06-10 12:39:23 +02003069 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3070 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3071 key_start.offset = logical;
3072 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3073 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003074 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003075 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003076
Arne Jansen7a262852011-06-10 12:39:23 +02003077 if (!IS_ERR(reada1))
3078 btrfs_reada_wait(reada1);
3079 if (!IS_ERR(reada2))
3080 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003081
Arne Jansena2de7332011-03-08 14:14:00 +01003082
3083 /*
3084 * collect all data csums for the stripe to avoid seeking during
3085 * the scrub. This might currently (crc32) end up to be about 1MB
3086 */
Arne Jansene7786c32011-05-28 20:58:38 +00003087 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003088
Arne Jansena2de7332011-03-08 14:14:00 +01003089 /*
3090 * now find all extents for each stripe and scrub them
3091 */
Arne Jansena2de7332011-03-08 14:14:00 +01003092 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003093 while (physical < physical_end) {
3094 /* for raid56, we skip parity stripe */
Zhao Leiffe2d202015-01-20 15:11:44 +08003095 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003096 ret = get_raid56_logic_offset(physical, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003097 map, &logical, &stripe_logical);
Wang Shilong3b080b22014-04-01 18:01:43 +08003098 logical += base;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003099 if (ret) {
3100 stripe_logical += base;
3101 stripe_end = stripe_logical + increment - 1;
3102 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3103 ppath, stripe_logical,
3104 stripe_end);
3105 if (ret)
3106 goto out;
Wang Shilong3b080b22014-04-01 18:01:43 +08003107 goto skip;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003108 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003109 }
Arne Jansena2de7332011-03-08 14:14:00 +01003110 /*
3111 * canceled?
3112 */
3113 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003114 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003115 ret = -ECANCELED;
3116 goto out;
3117 }
3118 /*
3119 * check to see if we have to pause
3120 */
3121 if (atomic_read(&fs_info->scrub_pause_req)) {
3122 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003123 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003124 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003125 mutex_lock(&sctx->wr_ctx.wr_lock);
3126 scrub_wr_submit(sctx);
3127 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003128 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003129 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003130 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003131 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003132 }
3133
Wang Shilong7c76edb2014-01-12 21:38:32 +08003134 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3135 key.type = BTRFS_METADATA_ITEM_KEY;
3136 else
3137 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003138 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003139 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003140
3141 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3142 if (ret < 0)
3143 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003144
Arne Jansen8c510322011-06-03 10:09:26 +02003145 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003146 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003147 if (ret < 0)
3148 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003149 if (ret > 0) {
3150 /* there's no smaller item, so stick with the
3151 * larger one */
3152 btrfs_release_path(path);
3153 ret = btrfs_search_slot(NULL, root, &key,
3154 path, 0, 0);
3155 if (ret < 0)
3156 goto out;
3157 }
Arne Jansena2de7332011-03-08 14:14:00 +01003158 }
3159
Liu Bo625f1c8d2013-04-27 02:56:57 +00003160 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003161 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003162 u64 bytes;
3163
Arne Jansena2de7332011-03-08 14:14:00 +01003164 l = path->nodes[0];
3165 slot = path->slots[0];
3166 if (slot >= btrfs_header_nritems(l)) {
3167 ret = btrfs_next_leaf(root, path);
3168 if (ret == 0)
3169 continue;
3170 if (ret < 0)
3171 goto out;
3172
Liu Bo625f1c8d2013-04-27 02:56:57 +00003173 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003174 break;
3175 }
3176 btrfs_item_key_to_cpu(l, &key, slot);
3177
Josef Bacik3173a182013-03-07 14:22:04 -05003178 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003179 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003180 else
3181 bytes = key.offset;
3182
3183 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003184 goto next;
3185
Liu Bo625f1c8d2013-04-27 02:56:57 +00003186 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3187 key.type != BTRFS_METADATA_ITEM_KEY)
3188 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01003189
Liu Bo625f1c8d2013-04-27 02:56:57 +00003190 if (key.objectid >= logical + map->stripe_len) {
3191 /* out of this device extent */
3192 if (key.objectid >= logic_end)
3193 stop_loop = 1;
3194 break;
3195 }
Arne Jansena2de7332011-03-08 14:14:00 +01003196
3197 extent = btrfs_item_ptr(l, slot,
3198 struct btrfs_extent_item);
3199 flags = btrfs_extent_flags(l, extent);
3200 generation = btrfs_extent_generation(l, extent);
3201
3202 if (key.objectid < logical &&
3203 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003204 btrfs_err(fs_info,
3205 "scrub: tree block %llu spanning "
3206 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003207 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01003208 goto next;
3209 }
3210
Liu Bo625f1c8d2013-04-27 02:56:57 +00003211again:
3212 extent_logical = key.objectid;
3213 extent_len = bytes;
3214
Arne Jansena2de7332011-03-08 14:14:00 +01003215 /*
3216 * trim extent to this stripe
3217 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003218 if (extent_logical < logical) {
3219 extent_len -= logical - extent_logical;
3220 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003221 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003222 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003223 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003224 extent_len = logical + map->stripe_len -
3225 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003226 }
3227
Liu Bo625f1c8d2013-04-27 02:56:57 +00003228 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003229 extent_dev = scrub_dev;
3230 extent_mirror_num = mirror_num;
3231 if (is_dev_replace)
3232 scrub_remap_extent(fs_info, extent_logical,
3233 extent_len, &extent_physical,
3234 &extent_dev,
3235 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003236
3237 ret = btrfs_lookup_csums_range(csum_root, logical,
3238 logical + map->stripe_len - 1,
3239 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003240 if (ret)
3241 goto out;
3242
Liu Bo625f1c8d2013-04-27 02:56:57 +00003243 ret = scrub_extent(sctx, extent_logical, extent_len,
3244 extent_physical, extent_dev, flags,
3245 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003246 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003247 if (ret)
3248 goto out;
3249
Josef Bacikd88d46c2013-06-10 12:59:04 +00003250 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003251 if (extent_logical + extent_len <
3252 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003253 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003254 /*
3255 * loop until we find next data stripe
3256 * or we have finished all stripes.
3257 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003258loop:
3259 physical += map->stripe_len;
3260 ret = get_raid56_logic_offset(physical,
3261 num, map, &logical,
3262 &stripe_logical);
3263 logical += base;
3264
3265 if (ret && physical < physical_end) {
3266 stripe_logical += base;
3267 stripe_end = stripe_logical +
3268 increment - 1;
3269 ret = scrub_raid56_parity(sctx,
3270 map, scrub_dev, ppath,
3271 stripe_logical,
3272 stripe_end);
3273 if (ret)
3274 goto out;
3275 goto loop;
3276 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003277 } else {
3278 physical += map->stripe_len;
3279 logical += increment;
3280 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003281 if (logical < key.objectid + bytes) {
3282 cond_resched();
3283 goto again;
3284 }
3285
Wang Shilong3b080b22014-04-01 18:01:43 +08003286 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003287 stop_loop = 1;
3288 break;
3289 }
3290 }
Arne Jansena2de7332011-03-08 14:14:00 +01003291next:
3292 path->slots[0]++;
3293 }
Chris Mason71267332011-05-23 06:30:52 -04003294 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003295skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003296 logical += increment;
3297 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003298 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003299 if (stop_loop)
3300 sctx->stat.last_physical = map->stripes[num].physical +
3301 length;
3302 else
3303 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003304 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003305 if (stop_loop)
3306 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003307 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003308out:
Arne Jansena2de7332011-03-08 14:14:00 +01003309 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003310 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003311 mutex_lock(&sctx->wr_ctx.wr_lock);
3312 scrub_wr_submit(sctx);
3313 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003314
Arne Jansene7786c32011-05-28 20:58:38 +00003315 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003316 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003317 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003318 return ret < 0 ? ret : 0;
3319}
3320
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003321static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003322 struct btrfs_device *scrub_dev,
3323 u64 chunk_tree, u64 chunk_objectid,
3324 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003325 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003326{
3327 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003328 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003329 struct map_lookup *map;
3330 struct extent_map *em;
3331 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003332 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003333
3334 read_lock(&map_tree->map_tree.lock);
3335 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3336 read_unlock(&map_tree->map_tree.lock);
3337
3338 if (!em)
3339 return -EINVAL;
3340
3341 map = (struct map_lookup *)em->bdev;
3342 if (em->start != chunk_offset)
3343 goto out;
3344
3345 if (em->len < length)
3346 goto out;
3347
3348 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003349 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003350 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003351 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003352 chunk_offset, length,
3353 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003354 if (ret)
3355 goto out;
3356 }
3357 }
3358out:
3359 free_extent_map(em);
3360
3361 return ret;
3362}
3363
3364static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003365int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003366 struct btrfs_device *scrub_dev, u64 start, u64 end,
3367 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003368{
3369 struct btrfs_dev_extent *dev_extent = NULL;
3370 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003371 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003372 struct btrfs_fs_info *fs_info = root->fs_info;
3373 u64 length;
3374 u64 chunk_tree;
3375 u64 chunk_objectid;
3376 u64 chunk_offset;
3377 int ret;
3378 int slot;
3379 struct extent_buffer *l;
3380 struct btrfs_key key;
3381 struct btrfs_key found_key;
3382 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003383 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003384
3385 path = btrfs_alloc_path();
3386 if (!path)
3387 return -ENOMEM;
3388
3389 path->reada = 2;
3390 path->search_commit_root = 1;
3391 path->skip_locking = 1;
3392
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003393 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003394 key.offset = 0ull;
3395 key.type = BTRFS_DEV_EXTENT_KEY;
3396
Arne Jansena2de7332011-03-08 14:14:00 +01003397 while (1) {
3398 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3399 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003400 break;
3401 if (ret > 0) {
3402 if (path->slots[0] >=
3403 btrfs_header_nritems(path->nodes[0])) {
3404 ret = btrfs_next_leaf(root, path);
3405 if (ret)
3406 break;
3407 }
3408 }
Arne Jansena2de7332011-03-08 14:14:00 +01003409
3410 l = path->nodes[0];
3411 slot = path->slots[0];
3412
3413 btrfs_item_key_to_cpu(l, &found_key, slot);
3414
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003415 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003416 break;
3417
David Sterba962a2982014-06-04 18:41:45 +02003418 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003419 break;
3420
3421 if (found_key.offset >= end)
3422 break;
3423
3424 if (found_key.offset < key.offset)
3425 break;
3426
3427 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3428 length = btrfs_dev_extent_length(l, dev_extent);
3429
Qu Wenruoced96ed2014-06-19 10:42:51 +08003430 if (found_key.offset + length <= start)
3431 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003432
3433 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3434 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3435 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3436
3437 /*
3438 * get a reference on the corresponding block group to prevent
3439 * the chunk from going away while we scrub it
3440 */
3441 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003442
3443 /* some chunks are removed but not committed to disk yet,
3444 * continue scrubbing */
3445 if (!cache)
3446 goto skip;
3447
Stefan Behrensff023aa2012-11-06 11:43:11 +01003448 dev_replace->cursor_right = found_key.offset + length;
3449 dev_replace->cursor_left = found_key.offset;
3450 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003451 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003452 chunk_offset, length, found_key.offset,
3453 is_dev_replace);
3454
3455 /*
3456 * flush, submit all pending read and write bios, afterwards
3457 * wait for them.
3458 * Note that in the dev replace case, a read request causes
3459 * write requests that are submitted in the read completion
3460 * worker. Therefore in the current situation, it is required
3461 * that all write requests are flushed, so that all read and
3462 * write requests are really completed when bios_in_flight
3463 * changes to 0.
3464 */
3465 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3466 scrub_submit(sctx);
3467 mutex_lock(&sctx->wr_ctx.wr_lock);
3468 scrub_wr_submit(sctx);
3469 mutex_unlock(&sctx->wr_ctx.wr_lock);
3470
3471 wait_event(sctx->list_wait,
3472 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003473 atomic_inc(&fs_info->scrubs_paused);
3474 wake_up(&fs_info->scrub_pause_wait);
3475
3476 /*
3477 * must be called before we decrease @scrub_paused.
3478 * make sure we don't block transaction commit while
3479 * we are waiting pending workers finished.
3480 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003481 wait_event(sctx->list_wait,
3482 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003483 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3484
3485 mutex_lock(&fs_info->scrub_lock);
3486 __scrub_blocked_if_needed(fs_info);
3487 atomic_dec(&fs_info->scrubs_paused);
3488 mutex_unlock(&fs_info->scrub_lock);
3489 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003490
Arne Jansena2de7332011-03-08 14:14:00 +01003491 btrfs_put_block_group(cache);
3492 if (ret)
3493 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003494 if (is_dev_replace &&
3495 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003496 ret = -EIO;
3497 break;
3498 }
3499 if (sctx->stat.malloc_errors > 0) {
3500 ret = -ENOMEM;
3501 break;
3502 }
Arne Jansena2de7332011-03-08 14:14:00 +01003503
Ilya Dryomov539f3582013-10-07 13:42:57 +03003504 dev_replace->cursor_left = dev_replace->cursor_right;
3505 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003506skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003507 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003508 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003509 }
3510
Arne Jansena2de7332011-03-08 14:14:00 +01003511 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003512
3513 /*
3514 * ret can still be 1 from search_slot or next_leaf,
3515 * that's not an error
3516 */
3517 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003518}
3519
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003520static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3521 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003522{
3523 int i;
3524 u64 bytenr;
3525 u64 gen;
3526 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003527 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003528
Miao Xie87533c42013-01-29 10:14:48 +00003529 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003530 return -EIO;
3531
Miao Xie5f546062014-07-24 11:37:09 +08003532 /* Seed devices of a new filesystem has their own generation. */
3533 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3534 gen = scrub_dev->generation;
3535 else
3536 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003537
3538 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3539 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003540 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3541 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003542 break;
3543
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003544 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003545 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003546 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003547 if (ret)
3548 return ret;
3549 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003550 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003551
3552 return 0;
3553}
3554
3555/*
3556 * get a reference count on fs_info->scrub_workers. start worker if necessary
3557 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003558static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3559 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003560{
David Sterba6f011052015-02-16 18:34:01 +01003561 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003562 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003563
Arne Jansen632dd772011-06-10 12:07:07 +02003564 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003565 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003566 fs_info->scrub_workers =
3567 btrfs_alloc_workqueue("btrfs-scrub", flags,
3568 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003569 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003570 fs_info->scrub_workers =
3571 btrfs_alloc_workqueue("btrfs-scrub", flags,
3572 max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08003573 if (!fs_info->scrub_workers)
3574 goto fail_scrub_workers;
3575
Qu Wenruo0339ef22014-02-28 10:46:17 +08003576 fs_info->scrub_wr_completion_workers =
3577 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3578 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003579 if (!fs_info->scrub_wr_completion_workers)
3580 goto fail_scrub_wr_completion_workers;
3581
Qu Wenruo0339ef22014-02-28 10:46:17 +08003582 fs_info->scrub_nocow_workers =
3583 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
Zhao Leie82afc52015-06-12 20:36:58 +08003584 if (!fs_info->scrub_nocow_workers)
3585 goto fail_scrub_nocow_workers;
Zhao Lei20b2e302015-06-04 20:09:15 +08003586 fs_info->scrub_parity_workers =
3587 btrfs_alloc_workqueue("btrfs-scrubparity", flags,
3588 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003589 if (!fs_info->scrub_parity_workers)
3590 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02003591 }
Arne Jansena2de7332011-03-08 14:14:00 +01003592 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08003593 return 0;
3594
3595fail_scrub_parity_workers:
3596 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
3597fail_scrub_nocow_workers:
3598 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3599fail_scrub_wr_completion_workers:
3600 btrfs_destroy_workqueue(fs_info->scrub_workers);
3601fail_scrub_workers:
3602 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01003603}
3604
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003605static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003606{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003607 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003608 btrfs_destroy_workqueue(fs_info->scrub_workers);
3609 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3610 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08003611 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003612 }
Arne Jansena2de7332011-03-08 14:14:00 +01003613 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003614}
3615
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003616int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3617 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003618 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003619{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003620 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003621 int ret;
3622 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003623 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003624
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003625 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003626 return -EINVAL;
3627
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003628 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003629 /*
3630 * in this case scrub is unable to calculate the checksum
3631 * the way scrub is implemented. Do not handle this
3632 * situation at all because it won't ever happen.
3633 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003634 btrfs_err(fs_info,
3635 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003636 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003637 return -EINVAL;
3638 }
3639
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003640 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003641 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003642 btrfs_err(fs_info,
3643 "scrub: size assumption sectorsize != PAGE_SIZE "
3644 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003645 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003646 return -EINVAL;
3647 }
3648
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003649 if (fs_info->chunk_root->nodesize >
3650 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3651 fs_info->chunk_root->sectorsize >
3652 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3653 /*
3654 * would exhaust the array bounds of pagev member in
3655 * struct scrub_block
3656 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003657 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3658 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003659 fs_info->chunk_root->nodesize,
3660 SCRUB_MAX_PAGES_PER_BLOCK,
3661 fs_info->chunk_root->sectorsize,
3662 SCRUB_MAX_PAGES_PER_BLOCK);
3663 return -EINVAL;
3664 }
3665
Arne Jansena2de7332011-03-08 14:14:00 +01003666
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003667 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3668 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003669 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003670 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003671 return -ENODEV;
3672 }
Arne Jansena2de7332011-03-08 14:14:00 +01003673
Miao Xie5d68da32014-07-24 11:37:07 +08003674 if (!is_dev_replace && !readonly && !dev->writeable) {
3675 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3676 rcu_read_lock();
3677 name = rcu_dereference(dev->name);
3678 btrfs_err(fs_info, "scrub: device %s is not writable",
3679 name->str);
3680 rcu_read_unlock();
3681 return -EROFS;
3682 }
3683
Wang Shilong3b7a0162013-10-12 02:11:12 +08003684 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003685 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003686 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003687 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003688 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003689 }
3690
Stefan Behrens8dabb742012-11-06 13:15:27 +01003691 btrfs_dev_replace_lock(&fs_info->dev_replace);
3692 if (dev->scrub_device ||
3693 (!is_dev_replace &&
3694 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3695 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003696 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003697 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003698 return -EINPROGRESS;
3699 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003700 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003701
3702 ret = scrub_workers_get(fs_info, is_dev_replace);
3703 if (ret) {
3704 mutex_unlock(&fs_info->scrub_lock);
3705 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3706 return ret;
3707 }
3708
Stefan Behrens63a212a2012-11-05 18:29:28 +01003709 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003710 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003711 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003712 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3713 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003714 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003715 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003716 sctx->readonly = readonly;
3717 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003718 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003719
Wang Shilong3cb09292013-12-04 21:15:19 +08003720 /*
3721 * checking @scrub_pause_req here, we can avoid
3722 * race between committing transaction and scrubbing.
3723 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003724 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003725 atomic_inc(&fs_info->scrubs_running);
3726 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003727
Stefan Behrensff023aa2012-11-06 11:43:11 +01003728 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003729 /*
3730 * by holding device list mutex, we can
3731 * kick off writing super in log tree sync.
3732 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003733 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003734 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003735 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003736 }
Arne Jansena2de7332011-03-08 14:14:00 +01003737
3738 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003739 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3740 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003741
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003742 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003743 atomic_dec(&fs_info->scrubs_running);
3744 wake_up(&fs_info->scrub_pause_wait);
3745
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003746 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003747
Arne Jansena2de7332011-03-08 14:14:00 +01003748 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003749 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003750
3751 mutex_lock(&fs_info->scrub_lock);
3752 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003753 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003754 mutex_unlock(&fs_info->scrub_lock);
3755
Filipe Mananaf55985f2015-02-09 21:14:24 +00003756 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003757
3758 return ret;
3759}
3760
Jeff Mahoney143bede2012-03-01 14:56:26 +01003761void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003762{
3763 struct btrfs_fs_info *fs_info = root->fs_info;
3764
3765 mutex_lock(&fs_info->scrub_lock);
3766 atomic_inc(&fs_info->scrub_pause_req);
3767 while (atomic_read(&fs_info->scrubs_paused) !=
3768 atomic_read(&fs_info->scrubs_running)) {
3769 mutex_unlock(&fs_info->scrub_lock);
3770 wait_event(fs_info->scrub_pause_wait,
3771 atomic_read(&fs_info->scrubs_paused) ==
3772 atomic_read(&fs_info->scrubs_running));
3773 mutex_lock(&fs_info->scrub_lock);
3774 }
3775 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003776}
3777
Jeff Mahoney143bede2012-03-01 14:56:26 +01003778void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003779{
3780 struct btrfs_fs_info *fs_info = root->fs_info;
3781
3782 atomic_dec(&fs_info->scrub_pause_req);
3783 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003784}
3785
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003786int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003787{
Arne Jansena2de7332011-03-08 14:14:00 +01003788 mutex_lock(&fs_info->scrub_lock);
3789 if (!atomic_read(&fs_info->scrubs_running)) {
3790 mutex_unlock(&fs_info->scrub_lock);
3791 return -ENOTCONN;
3792 }
3793
3794 atomic_inc(&fs_info->scrub_cancel_req);
3795 while (atomic_read(&fs_info->scrubs_running)) {
3796 mutex_unlock(&fs_info->scrub_lock);
3797 wait_event(fs_info->scrub_pause_wait,
3798 atomic_read(&fs_info->scrubs_running) == 0);
3799 mutex_lock(&fs_info->scrub_lock);
3800 }
3801 atomic_dec(&fs_info->scrub_cancel_req);
3802 mutex_unlock(&fs_info->scrub_lock);
3803
3804 return 0;
3805}
3806
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003807int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3808 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003809{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003810 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003811
3812 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003813 sctx = dev->scrub_device;
3814 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003815 mutex_unlock(&fs_info->scrub_lock);
3816 return -ENOTCONN;
3817 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003818 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003819 while (dev->scrub_device) {
3820 mutex_unlock(&fs_info->scrub_lock);
3821 wait_event(fs_info->scrub_pause_wait,
3822 dev->scrub_device == NULL);
3823 mutex_lock(&fs_info->scrub_lock);
3824 }
3825 mutex_unlock(&fs_info->scrub_lock);
3826
3827 return 0;
3828}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003829
Arne Jansena2de7332011-03-08 14:14:00 +01003830int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3831 struct btrfs_scrub_progress *progress)
3832{
3833 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003834 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003835
3836 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003837 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003838 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003839 sctx = dev->scrub_device;
3840 if (sctx)
3841 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003842 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3843
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003844 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003845}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003846
3847static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3848 u64 extent_logical, u64 extent_len,
3849 u64 *extent_physical,
3850 struct btrfs_device **extent_dev,
3851 int *extent_mirror_num)
3852{
3853 u64 mapped_length;
3854 struct btrfs_bio *bbio = NULL;
3855 int ret;
3856
3857 mapped_length = extent_len;
3858 ret = btrfs_map_block(fs_info, READ, extent_logical,
3859 &mapped_length, &bbio, 0);
3860 if (ret || !bbio || mapped_length < extent_len ||
3861 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08003862 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003863 return;
3864 }
3865
3866 *extent_physical = bbio->stripes[0].physical;
3867 *extent_mirror_num = bbio->mirror_num;
3868 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08003869 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003870}
3871
3872static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3873 struct scrub_wr_ctx *wr_ctx,
3874 struct btrfs_fs_info *fs_info,
3875 struct btrfs_device *dev,
3876 int is_dev_replace)
3877{
3878 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3879
3880 mutex_init(&wr_ctx->wr_lock);
3881 wr_ctx->wr_curr_bio = NULL;
3882 if (!is_dev_replace)
3883 return 0;
3884
3885 WARN_ON(!dev->bdev);
Kent Overstreetb54ffb72015-05-19 14:31:01 +02003886 wr_ctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003887 wr_ctx->tgtdev = dev;
3888 atomic_set(&wr_ctx->flush_all_writes, 0);
3889 return 0;
3890}
3891
3892static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3893{
3894 mutex_lock(&wr_ctx->wr_lock);
3895 kfree(wr_ctx->wr_curr_bio);
3896 wr_ctx->wr_curr_bio = NULL;
3897 mutex_unlock(&wr_ctx->wr_lock);
3898}
3899
3900static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3901 int mirror_num, u64 physical_for_dev_replace)
3902{
3903 struct scrub_copy_nocow_ctx *nocow_ctx;
3904 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3905
3906 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3907 if (!nocow_ctx) {
3908 spin_lock(&sctx->stat_lock);
3909 sctx->stat.malloc_errors++;
3910 spin_unlock(&sctx->stat_lock);
3911 return -ENOMEM;
3912 }
3913
3914 scrub_pending_trans_workers_inc(sctx);
3915
3916 nocow_ctx->sctx = sctx;
3917 nocow_ctx->logical = logical;
3918 nocow_ctx->len = len;
3919 nocow_ctx->mirror_num = mirror_num;
3920 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08003921 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3922 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003923 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003924 btrfs_queue_work(fs_info->scrub_nocow_workers,
3925 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003926
3927 return 0;
3928}
3929
Josef Bacik652f25a2013-09-12 16:58:28 -04003930static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3931{
3932 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3933 struct scrub_nocow_inode *nocow_inode;
3934
3935 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3936 if (!nocow_inode)
3937 return -ENOMEM;
3938 nocow_inode->inum = inum;
3939 nocow_inode->offset = offset;
3940 nocow_inode->root = root;
3941 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3942 return 0;
3943}
3944
3945#define COPY_COMPLETE 1
3946
Stefan Behrensff023aa2012-11-06 11:43:11 +01003947static void copy_nocow_pages_worker(struct btrfs_work *work)
3948{
3949 struct scrub_copy_nocow_ctx *nocow_ctx =
3950 container_of(work, struct scrub_copy_nocow_ctx, work);
3951 struct scrub_ctx *sctx = nocow_ctx->sctx;
3952 u64 logical = nocow_ctx->logical;
3953 u64 len = nocow_ctx->len;
3954 int mirror_num = nocow_ctx->mirror_num;
3955 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3956 int ret;
3957 struct btrfs_trans_handle *trans = NULL;
3958 struct btrfs_fs_info *fs_info;
3959 struct btrfs_path *path;
3960 struct btrfs_root *root;
3961 int not_written = 0;
3962
3963 fs_info = sctx->dev_root->fs_info;
3964 root = fs_info->extent_root;
3965
3966 path = btrfs_alloc_path();
3967 if (!path) {
3968 spin_lock(&sctx->stat_lock);
3969 sctx->stat.malloc_errors++;
3970 spin_unlock(&sctx->stat_lock);
3971 not_written = 1;
3972 goto out;
3973 }
3974
3975 trans = btrfs_join_transaction(root);
3976 if (IS_ERR(trans)) {
3977 not_written = 1;
3978 goto out;
3979 }
3980
3981 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04003982 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003983 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003984 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
3985 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02003986 logical, physical_for_dev_replace, len, mirror_num,
3987 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003988 not_written = 1;
3989 goto out;
3990 }
3991
Josef Bacik652f25a2013-09-12 16:58:28 -04003992 btrfs_end_transaction(trans, root);
3993 trans = NULL;
3994 while (!list_empty(&nocow_ctx->inodes)) {
3995 struct scrub_nocow_inode *entry;
3996 entry = list_first_entry(&nocow_ctx->inodes,
3997 struct scrub_nocow_inode,
3998 list);
3999 list_del_init(&entry->list);
4000 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4001 entry->root, nocow_ctx);
4002 kfree(entry);
4003 if (ret == COPY_COMPLETE) {
4004 ret = 0;
4005 break;
4006 } else if (ret) {
4007 break;
4008 }
4009 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004010out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004011 while (!list_empty(&nocow_ctx->inodes)) {
4012 struct scrub_nocow_inode *entry;
4013 entry = list_first_entry(&nocow_ctx->inodes,
4014 struct scrub_nocow_inode,
4015 list);
4016 list_del_init(&entry->list);
4017 kfree(entry);
4018 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004019 if (trans && !IS_ERR(trans))
4020 btrfs_end_transaction(trans, root);
4021 if (not_written)
4022 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4023 num_uncorrectable_read_errors);
4024
4025 btrfs_free_path(path);
4026 kfree(nocow_ctx);
4027
4028 scrub_pending_trans_workers_dec(sctx);
4029}
4030
Gui Hecheng32159242014-11-10 15:36:08 +08004031static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4032 u64 logical)
4033{
4034 struct extent_state *cached_state = NULL;
4035 struct btrfs_ordered_extent *ordered;
4036 struct extent_io_tree *io_tree;
4037 struct extent_map *em;
4038 u64 lockstart = start, lockend = start + len - 1;
4039 int ret = 0;
4040
4041 io_tree = &BTRFS_I(inode)->io_tree;
4042
4043 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4044 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4045 if (ordered) {
4046 btrfs_put_ordered_extent(ordered);
4047 ret = 1;
4048 goto out_unlock;
4049 }
4050
4051 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4052 if (IS_ERR(em)) {
4053 ret = PTR_ERR(em);
4054 goto out_unlock;
4055 }
4056
4057 /*
4058 * This extent does not actually cover the logical extent anymore,
4059 * move on to the next inode.
4060 */
4061 if (em->block_start > logical ||
4062 em->block_start + em->block_len < logical + len) {
4063 free_extent_map(em);
4064 ret = 1;
4065 goto out_unlock;
4066 }
4067 free_extent_map(em);
4068
4069out_unlock:
4070 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4071 GFP_NOFS);
4072 return ret;
4073}
4074
Josef Bacik652f25a2013-09-12 16:58:28 -04004075static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4076 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004077{
Miao Xie826aa0a2013-06-27 18:50:59 +08004078 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004079 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004080 struct inode *inode;
4081 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004082 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004083 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004084 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004085 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004086 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004087 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004088 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004089 int ret = 0;
4090 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004091
4092 key.objectid = root;
4093 key.type = BTRFS_ROOT_ITEM_KEY;
4094 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004095
4096 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4097
Stefan Behrensff023aa2012-11-06 11:43:11 +01004098 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004099 if (IS_ERR(local_root)) {
4100 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004101 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004102 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004103
4104 key.type = BTRFS_INODE_ITEM_KEY;
4105 key.objectid = inum;
4106 key.offset = 0;
4107 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004108 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004109 if (IS_ERR(inode))
4110 return PTR_ERR(inode);
4111
Miao Xieedd14002013-06-27 18:51:00 +08004112 /* Avoid truncate/dio/punch hole.. */
4113 mutex_lock(&inode->i_mutex);
4114 inode_dio_wait(inode);
4115
Stefan Behrensff023aa2012-11-06 11:43:11 +01004116 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004117 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004118 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004119
Gui Hecheng32159242014-11-10 15:36:08 +08004120 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4121 if (ret) {
4122 ret = ret > 0 ? 0 : ret;
4123 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004124 }
4125
Stefan Behrensff023aa2012-11-06 11:43:11 +01004126 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004127 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004128again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004129 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4130 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004131 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004132 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004133 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004134 }
4135
4136 if (PageUptodate(page)) {
4137 if (PageDirty(page))
4138 goto next_page;
4139 } else {
4140 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004141 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004142 btrfs_get_extent,
4143 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004144 if (err) {
4145 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004146 goto next_page;
4147 }
Miao Xieedd14002013-06-27 18:51:00 +08004148
Miao Xie26b258912013-06-27 18:50:58 +08004149 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004150 /*
4151 * If the page has been remove from the page cache,
4152 * the data on it is meaningless, because it may be
4153 * old one, the new data may be written into the new
4154 * page in the page cache.
4155 */
4156 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004157 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004158 page_cache_release(page);
4159 goto again;
4160 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004161 if (!PageUptodate(page)) {
4162 ret = -EIO;
4163 goto next_page;
4164 }
4165 }
Gui Hecheng32159242014-11-10 15:36:08 +08004166
4167 ret = check_extent_to_block(inode, offset, len,
4168 nocow_ctx_logical);
4169 if (ret) {
4170 ret = ret > 0 ? 0 : ret;
4171 goto next_page;
4172 }
4173
Miao Xie826aa0a2013-06-27 18:50:59 +08004174 err = write_page_nocow(nocow_ctx->sctx,
4175 physical_for_dev_replace, page);
4176 if (err)
4177 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004178next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004179 unlock_page(page);
4180 page_cache_release(page);
4181
4182 if (ret)
4183 break;
4184
Stefan Behrensff023aa2012-11-06 11:43:11 +01004185 offset += PAGE_CACHE_SIZE;
4186 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004187 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004188 len -= PAGE_CACHE_SIZE;
4189 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004190 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004191out:
Miao Xieedd14002013-06-27 18:51:00 +08004192 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004193 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004194 return ret;
4195}
4196
4197static int write_page_nocow(struct scrub_ctx *sctx,
4198 u64 physical_for_dev_replace, struct page *page)
4199{
4200 struct bio *bio;
4201 struct btrfs_device *dev;
4202 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004203
4204 dev = sctx->wr_ctx.tgtdev;
4205 if (!dev)
4206 return -EIO;
4207 if (!dev->bdev) {
4208 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05004209 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004210 return -EIO;
4211 }
Chris Mason9be33952013-05-17 18:30:14 -04004212 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004213 if (!bio) {
4214 spin_lock(&sctx->stat_lock);
4215 sctx->stat.malloc_errors++;
4216 spin_unlock(&sctx->stat_lock);
4217 return -ENOMEM;
4218 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004219 bio->bi_iter.bi_size = 0;
4220 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004221 bio->bi_bdev = dev->bdev;
4222 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4223 if (ret != PAGE_CACHE_SIZE) {
4224leave_with_eio:
4225 bio_put(bio);
4226 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4227 return -EIO;
4228 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004229
Kent Overstreet33879d42013-11-23 22:33:32 -08004230 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004231 goto leave_with_eio;
4232
4233 bio_put(bio);
4234 return 0;
4235}