blob: ab5811545a988edf685ef4acce607cbff1ac7b81 [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);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400281static void scrub_bio_end_io(struct bio *bio, int err);
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);
298static void scrub_wr_bio_end_io(struct bio *bio, int err);
299static 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 pages_per_rd_bio;
458 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100459
Stefan Behrensff023aa2012-11-06 11:43:11 +0100460 /*
461 * the setting of pages_per_rd_bio is correct for scrub but might
462 * be wrong for the dev_replace code where we might read from
463 * different devices in the initial huge bios. However, that
464 * code is able to correctly handle the case when adding a page
465 * to a bio fails.
466 */
467 if (dev->bdev)
468 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
469 bio_get_nr_vecs(dev->bdev));
470 else
471 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100472 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
473 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100474 goto nomem;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000475 atomic_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100476 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100477 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100478 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100479 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100480 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100481 struct scrub_bio *sbio;
482
483 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
484 if (!sbio)
485 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100486 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100487
Arne Jansena2de7332011-03-08 14:14:00 +0100488 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100489 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400490 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800491 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
492 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100493
Stefan Behrensff023aa2012-11-06 11:43:11 +0100494 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100495 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200496 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100497 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100498 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100499 sctx->first_free = 0;
500 sctx->nodesize = dev->dev_root->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100501 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100502 atomic_set(&sctx->bios_in_flight, 0);
503 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100504 atomic_set(&sctx->cancel_req, 0);
505 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
506 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100507
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100508 spin_lock_init(&sctx->list_lock);
509 spin_lock_init(&sctx->stat_lock);
510 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100511
512 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
513 fs_info->dev_replace.tgtdev, is_dev_replace);
514 if (ret) {
515 scrub_free_ctx(sctx);
516 return ERR_PTR(ret);
517 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100518 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100519
520nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100521 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100522 return ERR_PTR(-ENOMEM);
523}
524
Stefan Behrensff023aa2012-11-06 11:43:11 +0100525static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
526 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200527{
528 u64 isize;
529 u32 nlink;
530 int ret;
531 int i;
532 struct extent_buffer *eb;
533 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100534 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200535 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
536 struct inode_fs_paths *ipath = NULL;
537 struct btrfs_root *local_root;
538 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100539 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200540
541 root_key.objectid = root;
542 root_key.type = BTRFS_ROOT_ITEM_KEY;
543 root_key.offset = (u64)-1;
544 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
545 if (IS_ERR(local_root)) {
546 ret = PTR_ERR(local_root);
547 goto err;
548 }
549
David Sterba14692cc2015-01-02 18:55:46 +0100550 /*
551 * this makes the path point to (inum INODE_ITEM ioff)
552 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100553 key.objectid = inum;
554 key.type = BTRFS_INODE_ITEM_KEY;
555 key.offset = 0;
556
557 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200558 if (ret) {
559 btrfs_release_path(swarn->path);
560 goto err;
561 }
562
563 eb = swarn->path->nodes[0];
564 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
565 struct btrfs_inode_item);
566 isize = btrfs_inode_size(eb, inode_item);
567 nlink = btrfs_inode_nlink(eb, inode_item);
568 btrfs_release_path(swarn->path);
569
570 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300571 if (IS_ERR(ipath)) {
572 ret = PTR_ERR(ipath);
573 ipath = NULL;
574 goto err;
575 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200576 ret = paths_from_inode(inum, ipath);
577
578 if (ret < 0)
579 goto err;
580
581 /*
582 * we deliberately ignore the bit ipath might have been too small to
583 * hold all of the paths here
584 */
585 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Frank Holtonefe120a2013-12-20 11:37:06 -0500586 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200587 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
588 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400589 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200590 (unsigned long long)swarn->sector, root, inum, offset,
591 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500592 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200593
594 free_ipath(ipath);
595 return 0;
596
597err:
Frank Holtonefe120a2013-12-20 11:37:06 -0500598 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200599 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
600 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400601 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200602 (unsigned long long)swarn->sector, root, inum, offset, ret);
603
604 free_ipath(ipath);
605 return 0;
606}
607
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400608static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200609{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100610 struct btrfs_device *dev;
611 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200612 struct btrfs_path *path;
613 struct btrfs_key found_key;
614 struct extent_buffer *eb;
615 struct btrfs_extent_item *ei;
616 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200617 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100618 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600619 u64 flags = 0;
620 u64 ref_root;
621 u32 item_size;
622 u8 ref_level;
Liu Bo69917e42012-09-07 20:01:28 -0600623 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200624
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100625 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100626 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100627 fs_info = sblock->sctx->dev_root->fs_info;
628
Jan Schmidt558540c2011-06-13 19:59:12 +0200629 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200630 if (!path)
631 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200632
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100633 swarn.sector = (sblock->pagev[0]->physical) >> 9;
634 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200635 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100636 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200637
Liu Bo69917e42012-09-07 20:01:28 -0600638 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
639 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200640 if (ret < 0)
641 goto out;
642
Jan Schmidt4692cf52011-12-02 14:56:41 +0100643 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200644 swarn.extent_item_size = found_key.offset;
645
646 eb = path->nodes[0];
647 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
648 item_size = btrfs_item_size_nr(eb, path->slots[0]);
649
Liu Bo69917e42012-09-07 20:01:28 -0600650 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200651 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800652 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
653 item_size, &ref_root,
654 &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400655 printk_in_rcu(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -0500656 "BTRFS: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200657 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400658 "%llu\n", errstr, swarn.logical,
659 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200660 (unsigned long long)swarn.sector,
661 ref_level ? "node" : "leaf",
662 ret < 0 ? -1 : ref_level,
663 ret < 0 ? -1 : ref_root);
664 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600665 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200666 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600667 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200668 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100669 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100670 iterate_extent_inodes(fs_info, found_key.objectid,
671 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200672 scrub_print_warning_inode, &swarn);
673 }
674
675out:
676 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200677}
678
Stefan Behrensff023aa2012-11-06 11:43:11 +0100679static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200680{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200681 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200682 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100683 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200684 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200685 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200686 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200687 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000688 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200689 u64 end = offset + PAGE_SIZE - 1;
690 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000691 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200692
693 key.objectid = root;
694 key.type = BTRFS_ROOT_ITEM_KEY;
695 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000696
697 fs_info = fixup->root->fs_info;
698 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
699
700 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
701 if (IS_ERR(local_root)) {
702 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200703 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000704 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200705
706 key.type = BTRFS_INODE_ITEM_KEY;
707 key.objectid = inum;
708 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000709 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
710 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200711 if (IS_ERR(inode))
712 return PTR_ERR(inode);
713
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200714 index = offset >> PAGE_CACHE_SHIFT;
715
716 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200717 if (!page) {
718 ret = -ENOMEM;
719 goto out;
720 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200721
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200722 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200723 if (PageDirty(page)) {
724 /*
725 * we need to write the data to the defect sector. the
726 * data that was in that sector is not in memory,
727 * because the page was modified. we must not write the
728 * modified page to that sector.
729 *
730 * TODO: what could be done here: wait for the delalloc
731 * runner to write out that page (might involve
732 * COW) and see whether the sector is still
733 * referenced afterwards.
734 *
735 * For the meantime, we'll treat this error
736 * incorrectable, although there is a chance that a
737 * later scrub will find the bad sector again and that
738 * there's no dirty page in memory, then.
739 */
740 ret = -EIO;
741 goto out;
742 }
Miao Xie1203b682014-09-12 18:44:01 +0800743 ret = repair_io_failure(inode, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200744 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800745 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200746 fixup->mirror_num);
747 unlock_page(page);
748 corrected = !ret;
749 } else {
750 /*
751 * we need to get good data first. the general readpage path
752 * will call repair_io_failure for us, we just have to make
753 * sure we read the bad mirror.
754 */
755 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
756 EXTENT_DAMAGED, GFP_NOFS);
757 if (ret) {
758 /* set_extent_bits should give proper error */
759 WARN_ON(ret > 0);
760 if (ret > 0)
761 ret = -EFAULT;
762 goto out;
763 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200764
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200765 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
766 btrfs_get_extent,
767 fixup->mirror_num);
768 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200769
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200770 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
771 end, EXTENT_DAMAGED, 0, NULL);
772 if (!corrected)
773 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
774 EXTENT_DAMAGED, GFP_NOFS);
775 }
776
777out:
778 if (page)
779 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200780
781 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200782
783 if (ret < 0)
784 return ret;
785
786 if (ret == 0 && corrected) {
787 /*
788 * we only need to call readpage for one of the inodes belonging
789 * to this extent. so make iterate_extent_inodes stop
790 */
791 return 1;
792 }
793
794 return -EIO;
795}
796
797static void scrub_fixup_nodatasum(struct btrfs_work *work)
798{
799 int ret;
800 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100801 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200802 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200803 struct btrfs_path *path;
804 int uncorrectable = 0;
805
806 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100807 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200808
809 path = btrfs_alloc_path();
810 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100811 spin_lock(&sctx->stat_lock);
812 ++sctx->stat.malloc_errors;
813 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200814 uncorrectable = 1;
815 goto out;
816 }
817
818 trans = btrfs_join_transaction(fixup->root);
819 if (IS_ERR(trans)) {
820 uncorrectable = 1;
821 goto out;
822 }
823
824 /*
825 * the idea is to trigger a regular read through the standard path. we
826 * read a page from the (failed) logical address by specifying the
827 * corresponding copynum of the failed sector. thus, that readpage is
828 * expected to fail.
829 * that is the point where on-the-fly error correction will kick in
830 * (once it's finished) and rewrite the failed sector if a good copy
831 * can be found.
832 */
833 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
834 path, scrub_fixup_readpage,
835 fixup);
836 if (ret < 0) {
837 uncorrectable = 1;
838 goto out;
839 }
840 WARN_ON(ret != 1);
841
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100842 spin_lock(&sctx->stat_lock);
843 ++sctx->stat.corrected_errors;
844 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200845
846out:
847 if (trans && !IS_ERR(trans))
848 btrfs_end_transaction(trans, fixup->root);
849 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100850 spin_lock(&sctx->stat_lock);
851 ++sctx->stat.uncorrectable_errors;
852 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100853 btrfs_dev_replace_stats_inc(
854 &sctx->dev_root->fs_info->dev_replace.
855 num_uncorrectable_read_errors);
Frank Holtonefe120a2013-12-20 11:37:06 -0500856 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
857 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200858 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200859 }
860
861 btrfs_free_path(path);
862 kfree(fixup);
863
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100864 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200865}
866
Miao Xieaf8e2d12014-10-23 14:42:50 +0800867static inline void scrub_get_recover(struct scrub_recover *recover)
868{
869 atomic_inc(&recover->refs);
870}
871
872static inline void scrub_put_recover(struct scrub_recover *recover)
873{
874 if (atomic_dec_and_test(&recover->refs)) {
Zhao Lei6e9606d2015-01-20 15:11:34 +0800875 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800876 kfree(recover);
877 }
878}
879
Arne Jansena2de7332011-03-08 14:14:00 +0100880/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400881 * scrub_handle_errored_block gets called when either verification of the
882 * pages failed or the bio failed to read, e.g. with EIO. In the latter
883 * case, this function handles all pages in the bio, even though only one
884 * may be bad.
885 * The goal of this function is to repair the errored block by using the
886 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100887 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400888static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100889{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100890 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100891 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400892 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100893 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400894 u64 logical;
895 u64 generation;
896 unsigned int failed_mirror_index;
897 unsigned int is_metadata;
898 unsigned int have_csum;
899 u8 *csum;
900 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
901 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100902 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400903 int mirror_index;
904 int page_num;
905 int success;
906 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
907 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100908
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400909 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100910 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000911 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
912 /*
913 * if we find an error in a super block, we just report it.
914 * They will get written with the next transaction commit
915 * anyway
916 */
917 spin_lock(&sctx->stat_lock);
918 ++sctx->stat.super_errors;
919 spin_unlock(&sctx->stat_lock);
920 return 0;
921 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400922 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100923 logical = sblock_to_check->pagev[0]->logical;
924 generation = sblock_to_check->pagev[0]->generation;
925 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
926 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
927 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400928 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100929 have_csum = sblock_to_check->pagev[0]->have_csum;
930 csum = sblock_to_check->pagev[0]->csum;
931 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400932
Stefan Behrensff023aa2012-11-06 11:43:11 +0100933 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
934 sblocks_for_recheck = NULL;
935 goto nodatasum_case;
936 }
937
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400938 /*
939 * read all mirrors one after the other. This includes to
940 * re-read the extent or metadata block that failed (that was
941 * the cause that this fixup code is called) another time,
942 * page by page this time in order to know which pages
943 * caused I/O errors and which ones are good (for all mirrors).
944 * It is the goal to handle the situation when more than one
945 * mirror contains I/O errors, but the errors do not
946 * overlap, i.e. the data can be repaired by selecting the
947 * pages from those mirrors without I/O error on the
948 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
949 * would be that mirror #1 has an I/O error on the first page,
950 * the second page is good, and mirror #2 has an I/O error on
951 * the second page, but the first page is good.
952 * Then the first page of the first mirror can be repaired by
953 * taking the first page of the second mirror, and the
954 * second page of the second mirror can be repaired by
955 * copying the contents of the 2nd page of the 1st mirror.
956 * One more note: if the pages of one mirror contain I/O
957 * errors, the checksum cannot be verified. In order to get
958 * the best data for repairing, the first attempt is to find
959 * a mirror without I/O errors and with a validated checksum.
960 * Only if this is not possible, the pages are picked from
961 * mirrors with I/O errors without considering the checksum.
962 * If the latter is the case, at the end, the checksum of the
963 * repaired area is verified in order to correctly maintain
964 * the statistics.
965 */
966
David Sterba31e818f2015-02-20 18:00:26 +0100967 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
968 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400969 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100970 spin_lock(&sctx->stat_lock);
971 sctx->stat.malloc_errors++;
972 sctx->stat.read_errors++;
973 sctx->stat.uncorrectable_errors++;
974 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100975 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400976 goto out;
977 }
978
979 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800980 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400981 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100982 spin_lock(&sctx->stat_lock);
983 sctx->stat.read_errors++;
984 sctx->stat.uncorrectable_errors++;
985 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100986 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400987 goto out;
988 }
989 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
990 sblock_bad = sblocks_for_recheck + failed_mirror_index;
991
992 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100993 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800994 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400995
996 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
997 sblock_bad->no_io_error_seen) {
998 /*
999 * the error disappeared after reading page by page, or
1000 * the area was part of a huge bio and other parts of the
1001 * bio caused I/O errors, or the block layer merged several
1002 * read requests into one and the error is caused by a
1003 * different bio (usually one of the two latter cases is
1004 * the cause)
1005 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001006 spin_lock(&sctx->stat_lock);
1007 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001008 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001009 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001010
Stefan Behrensff023aa2012-11-06 11:43:11 +01001011 if (sctx->is_dev_replace)
1012 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001013 goto out;
1014 }
1015
1016 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001017 spin_lock(&sctx->stat_lock);
1018 sctx->stat.read_errors++;
1019 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001020 if (__ratelimit(&_rs))
1021 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001022 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001023 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001024 spin_lock(&sctx->stat_lock);
1025 sctx->stat.csum_errors++;
1026 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001027 if (__ratelimit(&_rs))
1028 scrub_print_warning("checksum error", sblock_to_check);
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 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001032 spin_lock(&sctx->stat_lock);
1033 sctx->stat.verify_errors++;
1034 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001035 if (__ratelimit(&_rs))
1036 scrub_print_warning("checksum/header error",
1037 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001038 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001039 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001040 BTRFS_DEV_STAT_GENERATION_ERRS);
1041 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001042 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001043 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001044 }
1045
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001046 if (sctx->readonly) {
1047 ASSERT(!sctx->is_dev_replace);
1048 goto out;
1049 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001050
1051 if (!is_metadata && !have_csum) {
1052 struct scrub_fixup_nodatasum *fixup_nodatasum;
1053
Stefan Behrensff023aa2012-11-06 11:43:11 +01001054 WARN_ON(sctx->is_dev_replace);
1055
Zhao Leib25c94c2015-01-20 15:11:35 +08001056nodatasum_case:
1057
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001058 /*
1059 * !is_metadata and !have_csum, this means that the data
1060 * might not be COW'ed, that it might be modified
1061 * concurrently. The general strategy to work on the
1062 * commit root does not help in the case when COW is not
1063 * used.
1064 */
1065 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1066 if (!fixup_nodatasum)
1067 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001068 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001069 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001070 fixup_nodatasum->logical = logical;
1071 fixup_nodatasum->root = fs_info->extent_root;
1072 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001073 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001074 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1075 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001076 btrfs_queue_work(fs_info->scrub_workers,
1077 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001078 goto out;
1079 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001080
1081 /*
1082 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001083 * checksums.
1084 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001085 * errors and also does not have a checksum error.
1086 * If one is found, and if a checksum is present, the full block
1087 * that is known to contain an error is rewritten. Afterwards
1088 * the block is known to be corrected.
1089 * If a mirror is found which is completely correct, and no
1090 * checksum is present, only those pages are rewritten that had
1091 * an I/O error in the block to be repaired, since it cannot be
1092 * determined, which copy of the other pages is better (and it
1093 * could happen otherwise that a correct page would be
1094 * overwritten by a bad one).
1095 */
1096 for (mirror_index = 0;
1097 mirror_index < BTRFS_MAX_MIRRORS &&
1098 sblocks_for_recheck[mirror_index].page_count > 0;
1099 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001100 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001101
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001102 if (mirror_index == failed_mirror_index)
1103 continue;
1104 sblock_other = sblocks_for_recheck + mirror_index;
1105
1106 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001107 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1108 have_csum, csum, generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001109 sctx->csum_size, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001110
1111 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001112 !sblock_other->checksum_error &&
1113 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001114 if (sctx->is_dev_replace) {
1115 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001116 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001117 } else {
1118 ret = scrub_repair_block_from_good_copy(
1119 sblock_bad, sblock_other);
1120 if (!ret)
1121 goto corrected_error;
1122 }
Arne Jansena2de7332011-03-08 14:14:00 +01001123 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001124 }
1125
Zhao Leib968fed2015-01-20 15:11:41 +08001126 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1127 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001128
1129 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001130 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001131 * repaired, continue by picking good copies of those pages.
1132 * Select the good pages from mirrors to rewrite bad pages from
1133 * the area to fix. Afterwards verify the checksum of the block
1134 * that is supposed to be repaired. This verification step is
1135 * only done for the purpose of statistic counting and for the
1136 * final scrub report, whether errors remain.
1137 * A perfect algorithm could make use of the checksum and try
1138 * all possible combinations of pages from the different mirrors
1139 * until the checksum verification succeeds. For example, when
1140 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1141 * of mirror #2 is readable but the final checksum test fails,
1142 * then the 2nd page of mirror #3 could be tried, whether now
1143 * the final checksum succeedes. But this would be a rare
1144 * exception and is therefore not implemented. At least it is
1145 * avoided that the good copy is overwritten.
1146 * A more useful improvement would be to pick the sectors
1147 * without I/O error based on sector sizes (512 bytes on legacy
1148 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1149 * mirror could be repaired by taking 512 byte of a different
1150 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1151 * area are unreadable.
1152 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001153 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001154 for (page_num = 0; page_num < sblock_bad->page_count;
1155 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001156 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001157 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001158
Zhao Leib968fed2015-01-20 15:11:41 +08001159 /* skip no-io-error page in scrub */
1160 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001161 continue;
1162
Zhao Leib968fed2015-01-20 15:11:41 +08001163 /* try to find no-io-error page in mirrors */
1164 if (page_bad->io_error) {
1165 for (mirror_index = 0;
1166 mirror_index < BTRFS_MAX_MIRRORS &&
1167 sblocks_for_recheck[mirror_index].page_count > 0;
1168 mirror_index++) {
1169 if (!sblocks_for_recheck[mirror_index].
1170 pagev[page_num]->io_error) {
1171 sblock_other = sblocks_for_recheck +
1172 mirror_index;
1173 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001174 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001175 }
Zhao Leib968fed2015-01-20 15:11:41 +08001176 if (!sblock_other)
1177 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001178 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001179
Zhao Leib968fed2015-01-20 15:11:41 +08001180 if (sctx->is_dev_replace) {
1181 /*
1182 * did not find a mirror to fetch the page
1183 * from. scrub_write_page_to_dev_replace()
1184 * handles this case (page->io_error), by
1185 * filling the block with zeros before
1186 * submitting the write request
1187 */
1188 if (!sblock_other)
1189 sblock_other = sblock_bad;
1190
1191 if (scrub_write_page_to_dev_replace(sblock_other,
1192 page_num) != 0) {
1193 btrfs_dev_replace_stats_inc(
1194 &sctx->dev_root->
1195 fs_info->dev_replace.
1196 num_write_errors);
1197 success = 0;
1198 }
1199 } else if (sblock_other) {
1200 ret = scrub_repair_page_from_good_copy(sblock_bad,
1201 sblock_other,
1202 page_num, 0);
1203 if (0 == ret)
1204 page_bad->io_error = 0;
1205 else
1206 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001207 }
1208 }
1209
Zhao Leib968fed2015-01-20 15:11:41 +08001210 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001211 if (is_metadata || have_csum) {
1212 /*
1213 * need to verify the checksum now that all
1214 * sectors on disk are repaired (the write
1215 * request for data to be repaired is on its way).
1216 * Just be lazy and use scrub_recheck_block()
1217 * which re-reads the data before the checksum
1218 * is verified, but most likely the data comes out
1219 * of the page cache.
1220 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001221 scrub_recheck_block(fs_info, sblock_bad,
1222 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001223 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001224 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001225 !sblock_bad->checksum_error &&
1226 sblock_bad->no_io_error_seen)
1227 goto corrected_error;
1228 else
1229 goto did_not_correct_error;
1230 } else {
1231corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001232 spin_lock(&sctx->stat_lock);
1233 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001234 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001235 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001236 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001237 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001238 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001239 }
1240 } else {
1241did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001242 spin_lock(&sctx->stat_lock);
1243 sctx->stat.uncorrectable_errors++;
1244 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001245 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001246 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001247 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001248 }
1249
1250out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001251 if (sblocks_for_recheck) {
1252 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1253 mirror_index++) {
1254 struct scrub_block *sblock = sblocks_for_recheck +
1255 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001256 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001257 int page_index;
1258
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001259 for (page_index = 0; page_index < sblock->page_count;
1260 page_index++) {
1261 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001262 recover = sblock->pagev[page_index]->recover;
1263 if (recover) {
1264 scrub_put_recover(recover);
1265 sblock->pagev[page_index]->recover =
1266 NULL;
1267 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001268 scrub_page_put(sblock->pagev[page_index]);
1269 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001270 }
1271 kfree(sblocks_for_recheck);
1272 }
1273
1274 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001275}
1276
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001277static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001278{
Zhao Lei10f11902015-01-20 15:11:43 +08001279 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1280 return 2;
1281 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1282 return 3;
1283 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001284 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001285}
1286
Zhao Lei10f11902015-01-20 15:11:43 +08001287static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1288 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001289 u64 mapped_length,
1290 int nstripes, int mirror,
1291 int *stripe_index,
1292 u64 *stripe_offset)
1293{
1294 int i;
1295
Zhao Leiffe2d202015-01-20 15:11:44 +08001296 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001297 /* RAID5/6 */
1298 for (i = 0; i < nstripes; i++) {
1299 if (raid_map[i] == RAID6_Q_STRIPE ||
1300 raid_map[i] == RAID5_P_STRIPE)
1301 continue;
1302
1303 if (logical >= raid_map[i] &&
1304 logical < raid_map[i] + mapped_length)
1305 break;
1306 }
1307
1308 *stripe_index = i;
1309 *stripe_offset = logical - raid_map[i];
1310 } else {
1311 /* The other RAID type */
1312 *stripe_index = mirror;
1313 *stripe_offset = 0;
1314 }
1315}
1316
Zhao Leibe50a8d2015-01-20 15:11:42 +08001317static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001318 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001319{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001320 struct scrub_ctx *sctx = original_sblock->sctx;
1321 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1322 u64 length = original_sblock->page_count * PAGE_SIZE;
1323 u64 logical = original_sblock->pagev[0]->logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001324 struct scrub_recover *recover;
1325 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001326 u64 sublen;
1327 u64 mapped_length;
1328 u64 stripe_offset;
1329 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001330 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001331 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001332 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001333 int ret;
1334
1335 /*
Zhao Lei57019342015-01-20 15:11:45 +08001336 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001337 * are not used (and not set) in the blocks that are used for
1338 * the recheck procedure
1339 */
1340
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001341 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001342 sublen = min_t(u64, length, PAGE_SIZE);
1343 mapped_length = sublen;
1344 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001345
1346 /*
1347 * with a length of PAGE_SIZE, each returned stripe
1348 * represents one mirror
1349 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001350 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001351 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001352 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001353 btrfs_put_bbio(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001354 return -EIO;
1355 }
1356
Miao Xieaf8e2d12014-10-23 14:42:50 +08001357 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1358 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001359 btrfs_put_bbio(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001360 return -ENOMEM;
1361 }
1362
1363 atomic_set(&recover->refs, 1);
1364 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001365 recover->map_length = mapped_length;
1366
Stefan Behrensff023aa2012-11-06 11:43:11 +01001367 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001368
Zhao Leibe50a8d2015-01-20 15:11:42 +08001369 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001370
Miao Xieaf8e2d12014-10-23 14:42:50 +08001371 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001372 mirror_index++) {
1373 struct scrub_block *sblock;
1374 struct scrub_page *page;
1375
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001376 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001377 sblock->sctx = sctx;
1378 page = kzalloc(sizeof(*page), GFP_NOFS);
1379 if (!page) {
1380leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001381 spin_lock(&sctx->stat_lock);
1382 sctx->stat.malloc_errors++;
1383 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001384 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001385 return -ENOMEM;
1386 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001387 scrub_page_get(page);
1388 sblock->pagev[page_index] = page;
1389 page->logical = logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001390
Zhao Lei10f11902015-01-20 15:11:43 +08001391 scrub_stripe_index_and_offset(logical,
1392 bbio->map_type,
1393 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001394 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001395 bbio->num_stripes -
1396 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001397 mirror_index,
1398 &stripe_index,
1399 &stripe_offset);
1400 page->physical = bbio->stripes[stripe_index].physical +
1401 stripe_offset;
1402 page->dev = bbio->stripes[stripe_index].dev;
1403
Stefan Behrensff023aa2012-11-06 11:43:11 +01001404 BUG_ON(page_index >= original_sblock->page_count);
1405 page->physical_for_dev_replace =
1406 original_sblock->pagev[page_index]->
1407 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001408 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001409 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001410 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001411 page->page = alloc_page(GFP_NOFS);
1412 if (!page->page)
1413 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001414
1415 scrub_get_recover(recover);
1416 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001417 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001418 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001419 length -= sublen;
1420 logical += sublen;
1421 page_index++;
1422 }
1423
1424 return 0;
1425}
1426
Miao Xieaf8e2d12014-10-23 14:42:50 +08001427struct scrub_bio_ret {
1428 struct completion event;
1429 int error;
1430};
1431
1432static void scrub_bio_wait_endio(struct bio *bio, int error)
1433{
1434 struct scrub_bio_ret *ret = bio->bi_private;
1435
1436 ret->error = error;
1437 complete(&ret->event);
1438}
1439
1440static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1441{
Zhao Lei10f11902015-01-20 15:11:43 +08001442 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001443 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001444}
1445
1446static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1447 struct bio *bio,
1448 struct scrub_page *page)
1449{
1450 struct scrub_bio_ret done;
1451 int ret;
1452
1453 init_completion(&done.event);
1454 done.error = 0;
1455 bio->bi_iter.bi_sector = page->logical >> 9;
1456 bio->bi_private = &done;
1457 bio->bi_end_io = scrub_bio_wait_endio;
1458
1459 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001460 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001461 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001462 if (ret)
1463 return ret;
1464
1465 wait_for_completion(&done.event);
1466 if (done.error)
1467 return -EIO;
1468
1469 return 0;
1470}
1471
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001472/*
1473 * this function will check the on disk data for checksum errors, header
1474 * errors and read I/O errors. If any I/O errors happen, the exact pages
1475 * which are errored are marked as being bad. The goal is to enable scrub
1476 * to take those pages that are not errored from all the mirrors so that
1477 * the pages that are errored in the just handled mirror can be repaired.
1478 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001479static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1480 struct scrub_block *sblock, int is_metadata,
1481 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001482 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001483{
1484 int page_num;
1485
1486 sblock->no_io_error_seen = 1;
1487 sblock->header_error = 0;
1488 sblock->checksum_error = 0;
1489
1490 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1491 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001492 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001493
Stefan Behrens442a4f62012-05-25 16:06:08 +02001494 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001495 page->io_error = 1;
1496 sblock->no_io_error_seen = 0;
1497 continue;
1498 }
1499
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001500 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001501 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001502 if (!bio) {
1503 page->io_error = 1;
1504 sblock->no_io_error_seen = 0;
1505 continue;
1506 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001507 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001508
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001509 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001510 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1511 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1512 sblock->no_io_error_seen = 0;
1513 } else {
1514 bio->bi_iter.bi_sector = page->physical >> 9;
1515
1516 if (btrfsic_submit_bio_wait(READ, bio))
1517 sblock->no_io_error_seen = 0;
1518 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001519
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001520 bio_put(bio);
1521 }
1522
1523 if (sblock->no_io_error_seen)
1524 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1525 have_csum, csum, generation,
1526 csum_size);
1527
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001528 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001529}
1530
Miao Xie17a9be22014-07-24 11:37:08 +08001531static inline int scrub_check_fsid(u8 fsid[],
1532 struct scrub_page *spage)
1533{
1534 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1535 int ret;
1536
1537 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1538 return !ret;
1539}
1540
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001541static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1542 struct scrub_block *sblock,
1543 int is_metadata, int have_csum,
1544 const u8 *csum, u64 generation,
1545 u16 csum_size)
1546{
1547 int page_num;
1548 u8 calculated_csum[BTRFS_CSUM_SIZE];
1549 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001550 void *mapped_buffer;
1551
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001552 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001553 if (is_metadata) {
1554 struct btrfs_header *h;
1555
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001556 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001557 h = (struct btrfs_header *)mapped_buffer;
1558
Qu Wenruo3cae2102013-07-16 11:19:18 +08001559 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001560 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001561 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001562 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001563 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001564 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001565 sblock->header_error = 1;
1566 sblock->generation_error = 1;
1567 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001568 csum = h->csum;
1569 } else {
1570 if (!have_csum)
1571 return;
1572
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001573 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001574 }
1575
1576 for (page_num = 0;;) {
1577 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001578 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001579 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1580 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1581 else
Liu Bob0496682013-03-14 14:57:45 +00001582 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001583
Linus Torvalds9613beb2012-03-30 12:44:29 -07001584 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001585 page_num++;
1586 if (page_num >= sblock->page_count)
1587 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001588 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001589
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001590 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001591 }
1592
1593 btrfs_csum_final(crc, calculated_csum);
1594 if (memcmp(calculated_csum, csum, csum_size))
1595 sblock->checksum_error = 1;
1596}
1597
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001598static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001599 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001600{
1601 int page_num;
1602 int ret = 0;
1603
1604 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1605 int ret_sub;
1606
1607 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1608 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001609 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001610 if (ret_sub)
1611 ret = ret_sub;
1612 }
1613
1614 return ret;
1615}
1616
1617static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1618 struct scrub_block *sblock_good,
1619 int page_num, int force_write)
1620{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001621 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1622 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001623
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001624 BUG_ON(page_bad->page == NULL);
1625 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001626 if (force_write || sblock_bad->header_error ||
1627 sblock_bad->checksum_error || page_bad->io_error) {
1628 struct bio *bio;
1629 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001630
Stefan Behrensff023aa2012-11-06 11:43:11 +01001631 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001632 printk_ratelimited(KERN_WARNING "BTRFS: "
1633 "scrub_repair_page_from_good_copy(bdev == NULL) "
1634 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001635 return -EIO;
1636 }
1637
Chris Mason9be33952013-05-17 18:30:14 -04001638 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001639 if (!bio)
1640 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001641 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001642 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001643
1644 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1645 if (PAGE_SIZE != ret) {
1646 bio_put(bio);
1647 return -EIO;
1648 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001649
Kent Overstreet33879d42013-11-23 22:33:32 -08001650 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001651 btrfs_dev_stat_inc_and_print(page_bad->dev,
1652 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001653 btrfs_dev_replace_stats_inc(
1654 &sblock_bad->sctx->dev_root->fs_info->
1655 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001656 bio_put(bio);
1657 return -EIO;
1658 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001659 bio_put(bio);
1660 }
1661
1662 return 0;
1663}
1664
Stefan Behrensff023aa2012-11-06 11:43:11 +01001665static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1666{
1667 int page_num;
1668
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001669 /*
1670 * This block is used for the check of the parity on the source device,
1671 * so the data needn't be written into the destination device.
1672 */
1673 if (sblock->sparity)
1674 return;
1675
Stefan Behrensff023aa2012-11-06 11:43:11 +01001676 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1677 int ret;
1678
1679 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1680 if (ret)
1681 btrfs_dev_replace_stats_inc(
1682 &sblock->sctx->dev_root->fs_info->dev_replace.
1683 num_write_errors);
1684 }
1685}
1686
1687static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1688 int page_num)
1689{
1690 struct scrub_page *spage = sblock->pagev[page_num];
1691
1692 BUG_ON(spage->page == NULL);
1693 if (spage->io_error) {
1694 void *mapped_buffer = kmap_atomic(spage->page);
1695
1696 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1697 flush_dcache_page(spage->page);
1698 kunmap_atomic(mapped_buffer);
1699 }
1700 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1701}
1702
1703static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1704 struct scrub_page *spage)
1705{
1706 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1707 struct scrub_bio *sbio;
1708 int ret;
1709
1710 mutex_lock(&wr_ctx->wr_lock);
1711again:
1712 if (!wr_ctx->wr_curr_bio) {
1713 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1714 GFP_NOFS);
1715 if (!wr_ctx->wr_curr_bio) {
1716 mutex_unlock(&wr_ctx->wr_lock);
1717 return -ENOMEM;
1718 }
1719 wr_ctx->wr_curr_bio->sctx = sctx;
1720 wr_ctx->wr_curr_bio->page_count = 0;
1721 }
1722 sbio = wr_ctx->wr_curr_bio;
1723 if (sbio->page_count == 0) {
1724 struct bio *bio;
1725
1726 sbio->physical = spage->physical_for_dev_replace;
1727 sbio->logical = spage->logical;
1728 sbio->dev = wr_ctx->tgtdev;
1729 bio = sbio->bio;
1730 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001731 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001732 if (!bio) {
1733 mutex_unlock(&wr_ctx->wr_lock);
1734 return -ENOMEM;
1735 }
1736 sbio->bio = bio;
1737 }
1738
1739 bio->bi_private = sbio;
1740 bio->bi_end_io = scrub_wr_bio_end_io;
1741 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001742 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001743 sbio->err = 0;
1744 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1745 spage->physical_for_dev_replace ||
1746 sbio->logical + sbio->page_count * PAGE_SIZE !=
1747 spage->logical) {
1748 scrub_wr_submit(sctx);
1749 goto again;
1750 }
1751
1752 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1753 if (ret != PAGE_SIZE) {
1754 if (sbio->page_count < 1) {
1755 bio_put(sbio->bio);
1756 sbio->bio = NULL;
1757 mutex_unlock(&wr_ctx->wr_lock);
1758 return -EIO;
1759 }
1760 scrub_wr_submit(sctx);
1761 goto again;
1762 }
1763
1764 sbio->pagev[sbio->page_count] = spage;
1765 scrub_page_get(spage);
1766 sbio->page_count++;
1767 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1768 scrub_wr_submit(sctx);
1769 mutex_unlock(&wr_ctx->wr_lock);
1770
1771 return 0;
1772}
1773
1774static void scrub_wr_submit(struct scrub_ctx *sctx)
1775{
1776 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1777 struct scrub_bio *sbio;
1778
1779 if (!wr_ctx->wr_curr_bio)
1780 return;
1781
1782 sbio = wr_ctx->wr_curr_bio;
1783 wr_ctx->wr_curr_bio = NULL;
1784 WARN_ON(!sbio->bio->bi_bdev);
1785 scrub_pending_bio_inc(sctx);
1786 /* process all writes in a single worker thread. Then the block layer
1787 * orders the requests before sending them to the driver which
1788 * doubled the write performance on spinning disks when measured
1789 * with Linux 3.5 */
1790 btrfsic_submit_bio(WRITE, sbio->bio);
1791}
1792
1793static void scrub_wr_bio_end_io(struct bio *bio, int err)
1794{
1795 struct scrub_bio *sbio = bio->bi_private;
1796 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1797
1798 sbio->err = err;
1799 sbio->bio = bio;
1800
Liu Bo9e0af232014-08-15 23:36:53 +08001801 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1802 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001803 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001804}
1805
1806static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1807{
1808 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1809 struct scrub_ctx *sctx = sbio->sctx;
1810 int i;
1811
1812 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1813 if (sbio->err) {
1814 struct btrfs_dev_replace *dev_replace =
1815 &sbio->sctx->dev_root->fs_info->dev_replace;
1816
1817 for (i = 0; i < sbio->page_count; i++) {
1818 struct scrub_page *spage = sbio->pagev[i];
1819
1820 spage->io_error = 1;
1821 btrfs_dev_replace_stats_inc(&dev_replace->
1822 num_write_errors);
1823 }
1824 }
1825
1826 for (i = 0; i < sbio->page_count; i++)
1827 scrub_page_put(sbio->pagev[i]);
1828
1829 bio_put(sbio->bio);
1830 kfree(sbio);
1831 scrub_pending_bio_dec(sctx);
1832}
1833
1834static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001835{
1836 u64 flags;
1837 int ret;
1838
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001839 WARN_ON(sblock->page_count < 1);
1840 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001841 ret = 0;
1842 if (flags & BTRFS_EXTENT_FLAG_DATA)
1843 ret = scrub_checksum_data(sblock);
1844 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1845 ret = scrub_checksum_tree_block(sblock);
1846 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1847 (void)scrub_checksum_super(sblock);
1848 else
1849 WARN_ON(1);
1850 if (ret)
1851 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001852
1853 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001854}
1855
1856static int scrub_checksum_data(struct scrub_block *sblock)
1857{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001858 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001859 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001860 u8 *on_disk_csum;
1861 struct page *page;
1862 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001863 u32 crc = ~(u32)0;
1864 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001865 u64 len;
1866 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001867
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001868 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001869 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001870 return 0;
1871
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001872 on_disk_csum = sblock->pagev[0]->csum;
1873 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001874 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001875
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001876 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001877 index = 0;
1878 for (;;) {
1879 u64 l = min_t(u64, len, PAGE_SIZE);
1880
Liu Bob0496682013-03-14 14:57:45 +00001881 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001882 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001883 len -= l;
1884 if (len == 0)
1885 break;
1886 index++;
1887 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001888 BUG_ON(!sblock->pagev[index]->page);
1889 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001890 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001891 }
1892
Arne Jansena2de7332011-03-08 14:14:00 +01001893 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001894 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001895 fail = 1;
1896
Arne Jansena2de7332011-03-08 14:14:00 +01001897 return fail;
1898}
1899
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001900static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001901{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001902 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001903 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001904 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001905 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001906 u8 calculated_csum[BTRFS_CSUM_SIZE];
1907 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1908 struct page *page;
1909 void *mapped_buffer;
1910 u64 mapped_size;
1911 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001912 u32 crc = ~(u32)0;
1913 int fail = 0;
1914 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001915 u64 len;
1916 int index;
1917
1918 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001919 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001920 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001921 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001922 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001923
1924 /*
1925 * we don't use the getter functions here, as we
1926 * a) don't have an extent buffer and
1927 * b) the page is already kmapped
1928 */
Arne Jansena2de7332011-03-08 14:14:00 +01001929
Qu Wenruo3cae2102013-07-16 11:19:18 +08001930 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001931 ++fail;
1932
Qu Wenruo3cae2102013-07-16 11:19:18 +08001933 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001934 ++fail;
1935
Miao Xie17a9be22014-07-24 11:37:08 +08001936 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001937 ++fail;
1938
1939 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1940 BTRFS_UUID_SIZE))
1941 ++fail;
1942
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001943 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001944 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1945 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1946 index = 0;
1947 for (;;) {
1948 u64 l = min_t(u64, len, mapped_size);
1949
Liu Bob0496682013-03-14 14:57:45 +00001950 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001951 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001952 len -= l;
1953 if (len == 0)
1954 break;
1955 index++;
1956 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001957 BUG_ON(!sblock->pagev[index]->page);
1958 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001959 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001960 mapped_size = PAGE_SIZE;
1961 p = mapped_buffer;
1962 }
1963
1964 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001965 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001966 ++crc_fail;
1967
Arne Jansena2de7332011-03-08 14:14:00 +01001968 return fail || crc_fail;
1969}
1970
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001971static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001972{
1973 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001974 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001975 u8 calculated_csum[BTRFS_CSUM_SIZE];
1976 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1977 struct page *page;
1978 void *mapped_buffer;
1979 u64 mapped_size;
1980 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001981 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001982 int fail_gen = 0;
1983 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001984 u64 len;
1985 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001986
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001987 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001988 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001989 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001990 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001991 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001992
Qu Wenruo3cae2102013-07-16 11:19:18 +08001993 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001994 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001995
Qu Wenruo3cae2102013-07-16 11:19:18 +08001996 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001997 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001998
Miao Xie17a9be22014-07-24 11:37:08 +08001999 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002000 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002001
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002002 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2003 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2004 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2005 index = 0;
2006 for (;;) {
2007 u64 l = min_t(u64, len, mapped_size);
2008
Liu Bob0496682013-03-14 14:57:45 +00002009 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002010 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002011 len -= l;
2012 if (len == 0)
2013 break;
2014 index++;
2015 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002016 BUG_ON(!sblock->pagev[index]->page);
2017 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002018 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002019 mapped_size = PAGE_SIZE;
2020 p = mapped_buffer;
2021 }
2022
2023 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002024 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002025 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002026
Stefan Behrens442a4f62012-05-25 16:06:08 +02002027 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002028 /*
2029 * if we find an error in a super block, we just report it.
2030 * They will get written with the next transaction commit
2031 * anyway
2032 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002033 spin_lock(&sctx->stat_lock);
2034 ++sctx->stat.super_errors;
2035 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002036 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002037 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002038 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2039 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002040 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002041 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002042 }
2043
Stefan Behrens442a4f62012-05-25 16:06:08 +02002044 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002045}
2046
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002047static void scrub_block_get(struct scrub_block *sblock)
2048{
Zhao Lei57019342015-01-20 15:11:45 +08002049 atomic_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002050}
2051
2052static void scrub_block_put(struct scrub_block *sblock)
2053{
Zhao Lei57019342015-01-20 15:11:45 +08002054 if (atomic_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002055 int i;
2056
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002057 if (sblock->sparity)
2058 scrub_parity_put(sblock->sparity);
2059
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002060 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002061 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002062 kfree(sblock);
2063 }
2064}
2065
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002066static void scrub_page_get(struct scrub_page *spage)
2067{
Zhao Lei57019342015-01-20 15:11:45 +08002068 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002069}
2070
2071static void scrub_page_put(struct scrub_page *spage)
2072{
Zhao Lei57019342015-01-20 15:11:45 +08002073 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002074 if (spage->page)
2075 __free_page(spage->page);
2076 kfree(spage);
2077 }
2078}
2079
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002080static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002081{
2082 struct scrub_bio *sbio;
2083
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002084 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002085 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002086
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002087 sbio = sctx->bios[sctx->curr];
2088 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002089 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002090
Stefan Behrensff023aa2012-11-06 11:43:11 +01002091 if (!sbio->bio->bi_bdev) {
2092 /*
2093 * this case should not happen. If btrfs_map_block() is
2094 * wrong, it could happen for dev-replace operations on
2095 * missing devices when no mirrors are available, but in
2096 * this case it should already fail the mount.
2097 * This case is handled correctly (but _very_ slowly).
2098 */
2099 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05002100 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01002101 bio_endio(sbio->bio, -EIO);
2102 } else {
2103 btrfsic_submit_bio(READ, sbio->bio);
2104 }
Arne Jansena2de7332011-03-08 14:14:00 +01002105}
2106
Stefan Behrensff023aa2012-11-06 11:43:11 +01002107static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2108 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002109{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002110 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002111 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002112 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002113
2114again:
2115 /*
2116 * grab a fresh bio or wait for one to become available
2117 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002118 while (sctx->curr == -1) {
2119 spin_lock(&sctx->list_lock);
2120 sctx->curr = sctx->first_free;
2121 if (sctx->curr != -1) {
2122 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2123 sctx->bios[sctx->curr]->next_free = -1;
2124 sctx->bios[sctx->curr]->page_count = 0;
2125 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002126 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002127 spin_unlock(&sctx->list_lock);
2128 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002129 }
2130 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002131 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002132 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002133 struct bio *bio;
2134
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002135 sbio->physical = spage->physical;
2136 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002137 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002138 bio = sbio->bio;
2139 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002140 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002141 if (!bio)
2142 return -ENOMEM;
2143 sbio->bio = bio;
2144 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002145
2146 bio->bi_private = sbio;
2147 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002148 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002149 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002150 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002151 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2152 spage->physical ||
2153 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002154 spage->logical ||
2155 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002156 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002157 goto again;
2158 }
2159
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002160 sbio->pagev[sbio->page_count] = spage;
2161 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2162 if (ret != PAGE_SIZE) {
2163 if (sbio->page_count < 1) {
2164 bio_put(sbio->bio);
2165 sbio->bio = NULL;
2166 return -EIO;
2167 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002168 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002169 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002170 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002171
Stefan Behrensff023aa2012-11-06 11:43:11 +01002172 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002173 atomic_inc(&sblock->outstanding_pages);
2174 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002175 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002176 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002177
2178 return 0;
2179}
2180
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002181static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002182 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002183 u64 gen, int mirror_num, u8 *csum, int force,
2184 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002185{
2186 struct scrub_block *sblock;
2187 int index;
2188
2189 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2190 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002191 spin_lock(&sctx->stat_lock);
2192 sctx->stat.malloc_errors++;
2193 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002194 return -ENOMEM;
2195 }
2196
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002197 /* one ref inside this function, plus one for each page added to
2198 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002199 atomic_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002200 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002201 sblock->no_io_error_seen = 1;
2202
2203 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002204 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002205 u64 l = min_t(u64, len, PAGE_SIZE);
2206
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002207 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2208 if (!spage) {
2209leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002210 spin_lock(&sctx->stat_lock);
2211 sctx->stat.malloc_errors++;
2212 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002213 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002214 return -ENOMEM;
2215 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002216 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2217 scrub_page_get(spage);
2218 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002219 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002220 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002221 spage->flags = flags;
2222 spage->generation = gen;
2223 spage->logical = logical;
2224 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002225 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002226 spage->mirror_num = mirror_num;
2227 if (csum) {
2228 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002229 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002230 } else {
2231 spage->have_csum = 0;
2232 }
2233 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002234 spage->page = alloc_page(GFP_NOFS);
2235 if (!spage->page)
2236 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002237 len -= l;
2238 logical += l;
2239 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002240 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002241 }
2242
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002243 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002244 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002245 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002246 int ret;
2247
Stefan Behrensff023aa2012-11-06 11:43:11 +01002248 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002249 if (ret) {
2250 scrub_block_put(sblock);
2251 return ret;
2252 }
2253 }
2254
2255 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002256 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002257
2258 /* last one frees, either here or in bio completion for last page */
2259 scrub_block_put(sblock);
2260 return 0;
2261}
2262
2263static void scrub_bio_end_io(struct bio *bio, int err)
2264{
2265 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002266 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002267
2268 sbio->err = err;
2269 sbio->bio = bio;
2270
Qu Wenruo0339ef22014-02-28 10:46:17 +08002271 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002272}
2273
2274static void scrub_bio_end_io_worker(struct btrfs_work *work)
2275{
2276 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002277 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002278 int i;
2279
Stefan Behrensff023aa2012-11-06 11:43:11 +01002280 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002281 if (sbio->err) {
2282 for (i = 0; i < sbio->page_count; i++) {
2283 struct scrub_page *spage = sbio->pagev[i];
2284
2285 spage->io_error = 1;
2286 spage->sblock->no_io_error_seen = 0;
2287 }
2288 }
2289
2290 /* now complete the scrub_block items that have all pages completed */
2291 for (i = 0; i < sbio->page_count; i++) {
2292 struct scrub_page *spage = sbio->pagev[i];
2293 struct scrub_block *sblock = spage->sblock;
2294
2295 if (atomic_dec_and_test(&sblock->outstanding_pages))
2296 scrub_block_complete(sblock);
2297 scrub_block_put(sblock);
2298 }
2299
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002300 bio_put(sbio->bio);
2301 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002302 spin_lock(&sctx->list_lock);
2303 sbio->next_free = sctx->first_free;
2304 sctx->first_free = sbio->index;
2305 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002306
2307 if (sctx->is_dev_replace &&
2308 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2309 mutex_lock(&sctx->wr_ctx.wr_lock);
2310 scrub_wr_submit(sctx);
2311 mutex_unlock(&sctx->wr_ctx.wr_lock);
2312 }
2313
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002314 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002315}
2316
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002317static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2318 unsigned long *bitmap,
2319 u64 start, u64 len)
2320{
David Sterba9d644a62015-02-20 18:42:11 +01002321 u32 offset;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002322 int nsectors;
2323 int sectorsize = sparity->sctx->dev_root->sectorsize;
2324
2325 if (len >= sparity->stripe_len) {
2326 bitmap_set(bitmap, 0, sparity->nsectors);
2327 return;
2328 }
2329
2330 start -= sparity->logic_start;
David Sterba47c57132015-02-20 18:43:47 +01002331 start = div_u64_rem(start, sparity->stripe_len, &offset);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002332 offset /= sectorsize;
2333 nsectors = (int)len / sectorsize;
2334
2335 if (offset + nsectors <= sparity->nsectors) {
2336 bitmap_set(bitmap, offset, nsectors);
2337 return;
2338 }
2339
2340 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2341 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2342}
2343
2344static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2345 u64 start, u64 len)
2346{
2347 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2348}
2349
2350static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2351 u64 start, u64 len)
2352{
2353 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2354}
2355
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002356static void scrub_block_complete(struct scrub_block *sblock)
2357{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002358 int corrupted = 0;
2359
Stefan Behrensff023aa2012-11-06 11:43:11 +01002360 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002361 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002362 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002363 } else {
2364 /*
2365 * if has checksum error, write via repair mechanism in
2366 * dev replace case, otherwise write here in dev replace
2367 * case.
2368 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002369 corrupted = scrub_checksum(sblock);
2370 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002371 scrub_write_block_to_dev_replace(sblock);
2372 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002373
2374 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2375 u64 start = sblock->pagev[0]->logical;
2376 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2377 PAGE_SIZE;
2378
2379 scrub_parity_mark_sectors_error(sblock->sparity,
2380 start, end - start);
2381 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002382}
2383
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002384static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002385 u8 *csum)
2386{
2387 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002388 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002389 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002390
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002391 while (!list_empty(&sctx->csum_list)) {
2392 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002393 struct btrfs_ordered_sum, list);
2394 if (sum->bytenr > logical)
2395 return 0;
2396 if (sum->bytenr + sum->len > logical)
2397 break;
2398
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002399 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002400 list_del(&sum->list);
2401 kfree(sum);
2402 sum = NULL;
2403 }
2404 if (!sum)
2405 return 0;
2406
Miao Xief51a4a12013-06-19 10:36:09 +08002407 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002408 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002409 memcpy(csum, sum->sums + index, sctx->csum_size);
2410 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002411 list_del(&sum->list);
2412 kfree(sum);
2413 }
Miao Xief51a4a12013-06-19 10:36:09 +08002414 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002415}
2416
2417/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002418static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002419 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002420 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002421{
2422 int ret;
2423 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002424 u32 blocksize;
2425
2426 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002427 blocksize = sctx->sectorsize;
2428 spin_lock(&sctx->stat_lock);
2429 sctx->stat.data_extents_scrubbed++;
2430 sctx->stat.data_bytes_scrubbed += len;
2431 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002432 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002433 blocksize = sctx->nodesize;
2434 spin_lock(&sctx->stat_lock);
2435 sctx->stat.tree_extents_scrubbed++;
2436 sctx->stat.tree_bytes_scrubbed += len;
2437 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002438 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002439 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002440 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002441 }
Arne Jansena2de7332011-03-08 14:14:00 +01002442
2443 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002444 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002445 int have_csum = 0;
2446
2447 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2448 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002449 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002450 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002451 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002452 if (sctx->is_dev_replace && !have_csum) {
2453 ret = copy_nocow_pages(sctx, logical, l,
2454 mirror_num,
2455 physical_for_dev_replace);
2456 goto behind_scrub_pages;
2457 }
Arne Jansena2de7332011-03-08 14:14:00 +01002458 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002459 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002460 mirror_num, have_csum ? csum : NULL, 0,
2461 physical_for_dev_replace);
2462behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002463 if (ret)
2464 return ret;
2465 len -= l;
2466 logical += l;
2467 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002468 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002469 }
2470 return 0;
2471}
2472
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002473static int scrub_pages_for_parity(struct scrub_parity *sparity,
2474 u64 logical, u64 len,
2475 u64 physical, struct btrfs_device *dev,
2476 u64 flags, u64 gen, int mirror_num, u8 *csum)
2477{
2478 struct scrub_ctx *sctx = sparity->sctx;
2479 struct scrub_block *sblock;
2480 int index;
2481
2482 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2483 if (!sblock) {
2484 spin_lock(&sctx->stat_lock);
2485 sctx->stat.malloc_errors++;
2486 spin_unlock(&sctx->stat_lock);
2487 return -ENOMEM;
2488 }
2489
2490 /* one ref inside this function, plus one for each page added to
2491 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002492 atomic_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002493 sblock->sctx = sctx;
2494 sblock->no_io_error_seen = 1;
2495 sblock->sparity = sparity;
2496 scrub_parity_get(sparity);
2497
2498 for (index = 0; len > 0; index++) {
2499 struct scrub_page *spage;
2500 u64 l = min_t(u64, len, PAGE_SIZE);
2501
2502 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2503 if (!spage) {
2504leave_nomem:
2505 spin_lock(&sctx->stat_lock);
2506 sctx->stat.malloc_errors++;
2507 spin_unlock(&sctx->stat_lock);
2508 scrub_block_put(sblock);
2509 return -ENOMEM;
2510 }
2511 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2512 /* For scrub block */
2513 scrub_page_get(spage);
2514 sblock->pagev[index] = spage;
2515 /* For scrub parity */
2516 scrub_page_get(spage);
2517 list_add_tail(&spage->list, &sparity->spages);
2518 spage->sblock = sblock;
2519 spage->dev = dev;
2520 spage->flags = flags;
2521 spage->generation = gen;
2522 spage->logical = logical;
2523 spage->physical = physical;
2524 spage->mirror_num = mirror_num;
2525 if (csum) {
2526 spage->have_csum = 1;
2527 memcpy(spage->csum, csum, sctx->csum_size);
2528 } else {
2529 spage->have_csum = 0;
2530 }
2531 sblock->page_count++;
2532 spage->page = alloc_page(GFP_NOFS);
2533 if (!spage->page)
2534 goto leave_nomem;
2535 len -= l;
2536 logical += l;
2537 physical += l;
2538 }
2539
2540 WARN_ON(sblock->page_count == 0);
2541 for (index = 0; index < sblock->page_count; index++) {
2542 struct scrub_page *spage = sblock->pagev[index];
2543 int ret;
2544
2545 ret = scrub_add_page_to_rd_bio(sctx, spage);
2546 if (ret) {
2547 scrub_block_put(sblock);
2548 return ret;
2549 }
2550 }
2551
2552 /* last one frees, either here or in bio completion for last page */
2553 scrub_block_put(sblock);
2554 return 0;
2555}
2556
2557static int scrub_extent_for_parity(struct scrub_parity *sparity,
2558 u64 logical, u64 len,
2559 u64 physical, struct btrfs_device *dev,
2560 u64 flags, u64 gen, int mirror_num)
2561{
2562 struct scrub_ctx *sctx = sparity->sctx;
2563 int ret;
2564 u8 csum[BTRFS_CSUM_SIZE];
2565 u32 blocksize;
2566
2567 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2568 blocksize = sctx->sectorsize;
2569 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2570 blocksize = sctx->nodesize;
2571 } else {
2572 blocksize = sctx->sectorsize;
2573 WARN_ON(1);
2574 }
2575
2576 while (len) {
2577 u64 l = min_t(u64, len, blocksize);
2578 int have_csum = 0;
2579
2580 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2581 /* push csums to sbio */
2582 have_csum = scrub_find_csum(sctx, logical, l, csum);
2583 if (have_csum == 0)
2584 goto skip;
2585 }
2586 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2587 flags, gen, mirror_num,
2588 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002589 if (ret)
2590 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002591skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002592 len -= l;
2593 logical += l;
2594 physical += l;
2595 }
2596 return 0;
2597}
2598
Wang Shilong3b080b22014-04-01 18:01:43 +08002599/*
2600 * Given a physical address, this will calculate it's
2601 * logical offset. if this is a parity stripe, it will return
2602 * the most left data stripe's logical offset.
2603 *
2604 * return 0 if it is a data stripe, 1 means parity stripe.
2605 */
2606static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002607 struct map_lookup *map, u64 *offset,
2608 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002609{
2610 int i;
2611 int j = 0;
2612 u64 stripe_nr;
2613 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002614 u32 stripe_index;
2615 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002616
2617 last_offset = (physical - map->stripes[num].physical) *
2618 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002619 if (stripe_start)
2620 *stripe_start = last_offset;
2621
Wang Shilong3b080b22014-04-01 18:01:43 +08002622 *offset = last_offset;
2623 for (i = 0; i < nr_data_stripes(map); i++) {
2624 *offset = last_offset + i * map->stripe_len;
2625
David Sterbab8b93ad2015-01-16 17:26:13 +01002626 stripe_nr = div_u64(*offset, map->stripe_len);
2627 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002628
2629 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002630 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002631 /* calculate which stripe this data locates */
2632 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002633 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002634 if (stripe_index == num)
2635 return 0;
2636 if (stripe_index < num)
2637 j++;
2638 }
2639 *offset = last_offset + j * map->stripe_len;
2640 return 1;
2641}
2642
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002643static void scrub_free_parity(struct scrub_parity *sparity)
2644{
2645 struct scrub_ctx *sctx = sparity->sctx;
2646 struct scrub_page *curr, *next;
2647 int nbits;
2648
2649 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2650 if (nbits) {
2651 spin_lock(&sctx->stat_lock);
2652 sctx->stat.read_errors += nbits;
2653 sctx->stat.uncorrectable_errors += nbits;
2654 spin_unlock(&sctx->stat_lock);
2655 }
2656
2657 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2658 list_del_init(&curr->list);
2659 scrub_page_put(curr);
2660 }
2661
2662 kfree(sparity);
2663}
2664
2665static void scrub_parity_bio_endio(struct bio *bio, int error)
2666{
2667 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2668 struct scrub_ctx *sctx = sparity->sctx;
2669
2670 if (error)
2671 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2672 sparity->nsectors);
2673
2674 scrub_free_parity(sparity);
2675 scrub_pending_bio_dec(sctx);
2676 bio_put(bio);
2677}
2678
2679static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2680{
2681 struct scrub_ctx *sctx = sparity->sctx;
2682 struct bio *bio;
2683 struct btrfs_raid_bio *rbio;
2684 struct scrub_page *spage;
2685 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002686 u64 length;
2687 int ret;
2688
2689 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2690 sparity->nsectors))
2691 goto out;
2692
2693 length = sparity->logic_end - sparity->logic_start + 1;
Miao Xie76035972014-11-14 17:45:42 +08002694 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002695 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002696 &length, &bbio, 0, 1);
2697 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002698 goto bbio_out;
2699
2700 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2701 if (!bio)
2702 goto bbio_out;
2703
2704 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2705 bio->bi_private = sparity;
2706 bio->bi_end_io = scrub_parity_bio_endio;
2707
2708 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002709 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002710 sparity->dbitmap,
2711 sparity->nsectors);
2712 if (!rbio)
2713 goto rbio_out;
2714
2715 list_for_each_entry(spage, &sparity->spages, list)
2716 raid56_parity_add_scrub_pages(rbio, spage->page,
2717 spage->logical);
2718
2719 scrub_pending_bio_inc(sctx);
2720 raid56_parity_submit_scrub_rbio(rbio);
2721 return;
2722
2723rbio_out:
2724 bio_put(bio);
2725bbio_out:
Zhao Lei6e9606d2015-01-20 15:11:34 +08002726 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002727 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2728 sparity->nsectors);
2729 spin_lock(&sctx->stat_lock);
2730 sctx->stat.malloc_errors++;
2731 spin_unlock(&sctx->stat_lock);
2732out:
2733 scrub_free_parity(sparity);
2734}
2735
2736static inline int scrub_calc_parity_bitmap_len(int nsectors)
2737{
2738 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2739}
2740
2741static void scrub_parity_get(struct scrub_parity *sparity)
2742{
Zhao Lei57019342015-01-20 15:11:45 +08002743 atomic_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002744}
2745
2746static void scrub_parity_put(struct scrub_parity *sparity)
2747{
Zhao Lei57019342015-01-20 15:11:45 +08002748 if (!atomic_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002749 return;
2750
2751 scrub_parity_check_and_repair(sparity);
2752}
2753
2754static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2755 struct map_lookup *map,
2756 struct btrfs_device *sdev,
2757 struct btrfs_path *path,
2758 u64 logic_start,
2759 u64 logic_end)
2760{
2761 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2762 struct btrfs_root *root = fs_info->extent_root;
2763 struct btrfs_root *csum_root = fs_info->csum_root;
2764 struct btrfs_extent_item *extent;
2765 u64 flags;
2766 int ret;
2767 int slot;
2768 struct extent_buffer *l;
2769 struct btrfs_key key;
2770 u64 generation;
2771 u64 extent_logical;
2772 u64 extent_physical;
2773 u64 extent_len;
2774 struct btrfs_device *extent_dev;
2775 struct scrub_parity *sparity;
2776 int nsectors;
2777 int bitmap_len;
2778 int extent_mirror_num;
2779 int stop_loop = 0;
2780
2781 nsectors = map->stripe_len / root->sectorsize;
2782 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2783 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2784 GFP_NOFS);
2785 if (!sparity) {
2786 spin_lock(&sctx->stat_lock);
2787 sctx->stat.malloc_errors++;
2788 spin_unlock(&sctx->stat_lock);
2789 return -ENOMEM;
2790 }
2791
2792 sparity->stripe_len = map->stripe_len;
2793 sparity->nsectors = nsectors;
2794 sparity->sctx = sctx;
2795 sparity->scrub_dev = sdev;
2796 sparity->logic_start = logic_start;
2797 sparity->logic_end = logic_end;
Zhao Lei57019342015-01-20 15:11:45 +08002798 atomic_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002799 INIT_LIST_HEAD(&sparity->spages);
2800 sparity->dbitmap = sparity->bitmap;
2801 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2802
2803 ret = 0;
2804 while (logic_start < logic_end) {
2805 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2806 key.type = BTRFS_METADATA_ITEM_KEY;
2807 else
2808 key.type = BTRFS_EXTENT_ITEM_KEY;
2809 key.objectid = logic_start;
2810 key.offset = (u64)-1;
2811
2812 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2813 if (ret < 0)
2814 goto out;
2815
2816 if (ret > 0) {
2817 ret = btrfs_previous_extent_item(root, path, 0);
2818 if (ret < 0)
2819 goto out;
2820 if (ret > 0) {
2821 btrfs_release_path(path);
2822 ret = btrfs_search_slot(NULL, root, &key,
2823 path, 0, 0);
2824 if (ret < 0)
2825 goto out;
2826 }
2827 }
2828
2829 stop_loop = 0;
2830 while (1) {
2831 u64 bytes;
2832
2833 l = path->nodes[0];
2834 slot = path->slots[0];
2835 if (slot >= btrfs_header_nritems(l)) {
2836 ret = btrfs_next_leaf(root, path);
2837 if (ret == 0)
2838 continue;
2839 if (ret < 0)
2840 goto out;
2841
2842 stop_loop = 1;
2843 break;
2844 }
2845 btrfs_item_key_to_cpu(l, &key, slot);
2846
2847 if (key.type == BTRFS_METADATA_ITEM_KEY)
2848 bytes = root->nodesize;
2849 else
2850 bytes = key.offset;
2851
2852 if (key.objectid + bytes <= logic_start)
2853 goto next;
2854
2855 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2856 key.type != BTRFS_METADATA_ITEM_KEY)
2857 goto next;
2858
2859 if (key.objectid > logic_end) {
2860 stop_loop = 1;
2861 break;
2862 }
2863
2864 while (key.objectid >= logic_start + map->stripe_len)
2865 logic_start += map->stripe_len;
2866
2867 extent = btrfs_item_ptr(l, slot,
2868 struct btrfs_extent_item);
2869 flags = btrfs_extent_flags(l, extent);
2870 generation = btrfs_extent_generation(l, extent);
2871
2872 if (key.objectid < logic_start &&
2873 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2874 btrfs_err(fs_info,
2875 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2876 key.objectid, logic_start);
2877 goto next;
2878 }
2879again:
2880 extent_logical = key.objectid;
2881 extent_len = bytes;
2882
2883 if (extent_logical < logic_start) {
2884 extent_len -= logic_start - extent_logical;
2885 extent_logical = logic_start;
2886 }
2887
2888 if (extent_logical + extent_len >
2889 logic_start + map->stripe_len)
2890 extent_len = logic_start + map->stripe_len -
2891 extent_logical;
2892
2893 scrub_parity_mark_sectors_data(sparity, extent_logical,
2894 extent_len);
2895
2896 scrub_remap_extent(fs_info, extent_logical,
2897 extent_len, &extent_physical,
2898 &extent_dev,
2899 &extent_mirror_num);
2900
2901 ret = btrfs_lookup_csums_range(csum_root,
2902 extent_logical,
2903 extent_logical + extent_len - 1,
2904 &sctx->csum_list, 1);
2905 if (ret)
2906 goto out;
2907
2908 ret = scrub_extent_for_parity(sparity, extent_logical,
2909 extent_len,
2910 extent_physical,
2911 extent_dev, flags,
2912 generation,
2913 extent_mirror_num);
2914 if (ret)
2915 goto out;
2916
2917 scrub_free_csums(sctx);
2918 if (extent_logical + extent_len <
2919 key.objectid + bytes) {
2920 logic_start += map->stripe_len;
2921
2922 if (logic_start >= logic_end) {
2923 stop_loop = 1;
2924 break;
2925 }
2926
2927 if (logic_start < key.objectid + bytes) {
2928 cond_resched();
2929 goto again;
2930 }
2931 }
2932next:
2933 path->slots[0]++;
2934 }
2935
2936 btrfs_release_path(path);
2937
2938 if (stop_loop)
2939 break;
2940
2941 logic_start += map->stripe_len;
2942 }
2943out:
2944 if (ret < 0)
2945 scrub_parity_mark_sectors_error(sparity, logic_start,
2946 logic_end - logic_start + 1);
2947 scrub_parity_put(sparity);
2948 scrub_submit(sctx);
2949 mutex_lock(&sctx->wr_ctx.wr_lock);
2950 scrub_wr_submit(sctx);
2951 mutex_unlock(&sctx->wr_ctx.wr_lock);
2952
2953 btrfs_release_path(path);
2954 return ret < 0 ? ret : 0;
2955}
2956
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002957static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002958 struct map_lookup *map,
2959 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002960 int num, u64 base, u64 length,
2961 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002962{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002963 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002964 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002965 struct btrfs_root *root = fs_info->extent_root;
2966 struct btrfs_root *csum_root = fs_info->csum_root;
2967 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002968 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002969 u64 flags;
2970 int ret;
2971 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002972 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002973 struct extent_buffer *l;
2974 struct btrfs_key key;
2975 u64 physical;
2976 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002977 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08002978 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002979 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002980 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002981 struct reada_control *reada1;
2982 struct reada_control *reada2;
2983 struct btrfs_key key_start;
2984 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002985 u64 increment = map->stripe_len;
2986 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002987 u64 extent_logical;
2988 u64 extent_physical;
2989 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002990 u64 stripe_logical;
2991 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002992 struct btrfs_device *extent_dev;
2993 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08002994 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002995
Wang Shilong3b080b22014-04-01 18:01:43 +08002996 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01002997 offset = 0;
David Sterbab8b93ad2015-01-16 17:26:13 +01002998 nstripes = div_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01002999 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3000 offset = map->stripe_len * num;
3001 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003002 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003003 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3004 int factor = map->num_stripes / map->sub_stripes;
3005 offset = map->stripe_len * (num / map->sub_stripes);
3006 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003007 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003008 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3009 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003010 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003011 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3012 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003013 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003014 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003015 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003016 increment = map->stripe_len * nr_data_stripes(map);
3017 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003018 } else {
3019 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003020 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003021 }
3022
3023 path = btrfs_alloc_path();
3024 if (!path)
3025 return -ENOMEM;
3026
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003027 ppath = btrfs_alloc_path();
3028 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003029 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003030 return -ENOMEM;
3031 }
3032
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003033 /*
3034 * work on commit root. The related disk blocks are static as
3035 * long as COW is applied. This means, it is save to rewrite
3036 * them to repair disk errors without any race conditions
3037 */
Arne Jansena2de7332011-03-08 14:14:00 +01003038 path->search_commit_root = 1;
3039 path->skip_locking = 1;
3040
Gui Hecheng063c54d2015-01-09 09:39:40 +08003041 ppath->search_commit_root = 1;
3042 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003043 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003044 * trigger the readahead for extent tree csum tree and wait for
3045 * completion. During readahead, the scrub is officially paused
3046 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003047 */
3048 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003049 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003050 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003051 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003052 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003053 logic_end += base;
3054 } else {
3055 logic_end = logical + increment * nstripes;
3056 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003057 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003058 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003059 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003060
Arne Jansen7a262852011-06-10 12:39:23 +02003061 /* FIXME it might be better to start readahead at commit root */
3062 key_start.objectid = logical;
3063 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3064 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003065 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003066 key_end.type = BTRFS_METADATA_ITEM_KEY;
3067 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003068 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003069
Arne Jansen7a262852011-06-10 12:39:23 +02003070 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3071 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3072 key_start.offset = logical;
3073 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3074 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003075 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003076 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003077
Arne Jansen7a262852011-06-10 12:39:23 +02003078 if (!IS_ERR(reada1))
3079 btrfs_reada_wait(reada1);
3080 if (!IS_ERR(reada2))
3081 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003082
Arne Jansena2de7332011-03-08 14:14:00 +01003083
3084 /*
3085 * collect all data csums for the stripe to avoid seeking during
3086 * the scrub. This might currently (crc32) end up to be about 1MB
3087 */
Arne Jansene7786c32011-05-28 20:58:38 +00003088 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003089
Arne Jansena2de7332011-03-08 14:14:00 +01003090 /*
3091 * now find all extents for each stripe and scrub them
3092 */
Arne Jansena2de7332011-03-08 14:14:00 +01003093 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003094 while (physical < physical_end) {
3095 /* for raid56, we skip parity stripe */
Zhao Leiffe2d202015-01-20 15:11:44 +08003096 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003097 ret = get_raid56_logic_offset(physical, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003098 map, &logical, &stripe_logical);
Wang Shilong3b080b22014-04-01 18:01:43 +08003099 logical += base;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003100 if (ret) {
3101 stripe_logical += base;
3102 stripe_end = stripe_logical + increment - 1;
3103 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3104 ppath, stripe_logical,
3105 stripe_end);
3106 if (ret)
3107 goto out;
Wang Shilong3b080b22014-04-01 18:01:43 +08003108 goto skip;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003109 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003110 }
Arne Jansena2de7332011-03-08 14:14:00 +01003111 /*
3112 * canceled?
3113 */
3114 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003115 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003116 ret = -ECANCELED;
3117 goto out;
3118 }
3119 /*
3120 * check to see if we have to pause
3121 */
3122 if (atomic_read(&fs_info->scrub_pause_req)) {
3123 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003124 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003125 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003126 mutex_lock(&sctx->wr_ctx.wr_lock);
3127 scrub_wr_submit(sctx);
3128 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003129 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003130 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003131 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003132 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003133 }
3134
Wang Shilong7c76edb2014-01-12 21:38:32 +08003135 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3136 key.type = BTRFS_METADATA_ITEM_KEY;
3137 else
3138 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003139 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003140 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003141
3142 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3143 if (ret < 0)
3144 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003145
Arne Jansen8c510322011-06-03 10:09:26 +02003146 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003147 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003148 if (ret < 0)
3149 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003150 if (ret > 0) {
3151 /* there's no smaller item, so stick with the
3152 * larger one */
3153 btrfs_release_path(path);
3154 ret = btrfs_search_slot(NULL, root, &key,
3155 path, 0, 0);
3156 if (ret < 0)
3157 goto out;
3158 }
Arne Jansena2de7332011-03-08 14:14:00 +01003159 }
3160
Liu Bo625f1c8d2013-04-27 02:56:57 +00003161 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003162 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003163 u64 bytes;
3164
Arne Jansena2de7332011-03-08 14:14:00 +01003165 l = path->nodes[0];
3166 slot = path->slots[0];
3167 if (slot >= btrfs_header_nritems(l)) {
3168 ret = btrfs_next_leaf(root, path);
3169 if (ret == 0)
3170 continue;
3171 if (ret < 0)
3172 goto out;
3173
Liu Bo625f1c8d2013-04-27 02:56:57 +00003174 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003175 break;
3176 }
3177 btrfs_item_key_to_cpu(l, &key, slot);
3178
Josef Bacik3173a182013-03-07 14:22:04 -05003179 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003180 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003181 else
3182 bytes = key.offset;
3183
3184 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003185 goto next;
3186
Liu Bo625f1c8d2013-04-27 02:56:57 +00003187 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3188 key.type != BTRFS_METADATA_ITEM_KEY)
3189 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01003190
Liu Bo625f1c8d2013-04-27 02:56:57 +00003191 if (key.objectid >= logical + map->stripe_len) {
3192 /* out of this device extent */
3193 if (key.objectid >= logic_end)
3194 stop_loop = 1;
3195 break;
3196 }
Arne Jansena2de7332011-03-08 14:14:00 +01003197
3198 extent = btrfs_item_ptr(l, slot,
3199 struct btrfs_extent_item);
3200 flags = btrfs_extent_flags(l, extent);
3201 generation = btrfs_extent_generation(l, extent);
3202
3203 if (key.objectid < logical &&
3204 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003205 btrfs_err(fs_info,
3206 "scrub: tree block %llu spanning "
3207 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003208 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01003209 goto next;
3210 }
3211
Liu Bo625f1c8d2013-04-27 02:56:57 +00003212again:
3213 extent_logical = key.objectid;
3214 extent_len = bytes;
3215
Arne Jansena2de7332011-03-08 14:14:00 +01003216 /*
3217 * trim extent to this stripe
3218 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003219 if (extent_logical < logical) {
3220 extent_len -= logical - extent_logical;
3221 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003222 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003223 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003224 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003225 extent_len = logical + map->stripe_len -
3226 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003227 }
3228
Liu Bo625f1c8d2013-04-27 02:56:57 +00003229 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003230 extent_dev = scrub_dev;
3231 extent_mirror_num = mirror_num;
3232 if (is_dev_replace)
3233 scrub_remap_extent(fs_info, extent_logical,
3234 extent_len, &extent_physical,
3235 &extent_dev,
3236 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003237
3238 ret = btrfs_lookup_csums_range(csum_root, logical,
3239 logical + map->stripe_len - 1,
3240 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003241 if (ret)
3242 goto out;
3243
Liu Bo625f1c8d2013-04-27 02:56:57 +00003244 ret = scrub_extent(sctx, extent_logical, extent_len,
3245 extent_physical, extent_dev, flags,
3246 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003247 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003248 if (ret)
3249 goto out;
3250
Josef Bacikd88d46c2013-06-10 12:59:04 +00003251 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003252 if (extent_logical + extent_len <
3253 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003254 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003255 /*
3256 * loop until we find next data stripe
3257 * or we have finished all stripes.
3258 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003259loop:
3260 physical += map->stripe_len;
3261 ret = get_raid56_logic_offset(physical,
3262 num, map, &logical,
3263 &stripe_logical);
3264 logical += base;
3265
3266 if (ret && physical < physical_end) {
3267 stripe_logical += base;
3268 stripe_end = stripe_logical +
3269 increment - 1;
3270 ret = scrub_raid56_parity(sctx,
3271 map, scrub_dev, ppath,
3272 stripe_logical,
3273 stripe_end);
3274 if (ret)
3275 goto out;
3276 goto loop;
3277 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003278 } else {
3279 physical += map->stripe_len;
3280 logical += increment;
3281 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003282 if (logical < key.objectid + bytes) {
3283 cond_resched();
3284 goto again;
3285 }
3286
Wang Shilong3b080b22014-04-01 18:01:43 +08003287 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003288 stop_loop = 1;
3289 break;
3290 }
3291 }
Arne Jansena2de7332011-03-08 14:14:00 +01003292next:
3293 path->slots[0]++;
3294 }
Chris Mason71267332011-05-23 06:30:52 -04003295 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003296skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003297 logical += increment;
3298 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003299 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003300 if (stop_loop)
3301 sctx->stat.last_physical = map->stripes[num].physical +
3302 length;
3303 else
3304 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003305 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003306 if (stop_loop)
3307 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003308 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003309out:
Arne Jansena2de7332011-03-08 14:14:00 +01003310 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003311 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003312 mutex_lock(&sctx->wr_ctx.wr_lock);
3313 scrub_wr_submit(sctx);
3314 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003315
Arne Jansene7786c32011-05-28 20:58:38 +00003316 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003317 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003318 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003319 return ret < 0 ? ret : 0;
3320}
3321
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003322static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003323 struct btrfs_device *scrub_dev,
3324 u64 chunk_tree, u64 chunk_objectid,
3325 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003326 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003327{
3328 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003329 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003330 struct map_lookup *map;
3331 struct extent_map *em;
3332 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003333 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003334
3335 read_lock(&map_tree->map_tree.lock);
3336 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3337 read_unlock(&map_tree->map_tree.lock);
3338
3339 if (!em)
3340 return -EINVAL;
3341
3342 map = (struct map_lookup *)em->bdev;
3343 if (em->start != chunk_offset)
3344 goto out;
3345
3346 if (em->len < length)
3347 goto out;
3348
3349 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003350 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003351 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003352 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003353 chunk_offset, length,
3354 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003355 if (ret)
3356 goto out;
3357 }
3358 }
3359out:
3360 free_extent_map(em);
3361
3362 return ret;
3363}
3364
3365static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003366int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003367 struct btrfs_device *scrub_dev, u64 start, u64 end,
3368 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003369{
3370 struct btrfs_dev_extent *dev_extent = NULL;
3371 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003372 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003373 struct btrfs_fs_info *fs_info = root->fs_info;
3374 u64 length;
3375 u64 chunk_tree;
3376 u64 chunk_objectid;
3377 u64 chunk_offset;
3378 int ret;
3379 int slot;
3380 struct extent_buffer *l;
3381 struct btrfs_key key;
3382 struct btrfs_key found_key;
3383 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003384 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003385
3386 path = btrfs_alloc_path();
3387 if (!path)
3388 return -ENOMEM;
3389
3390 path->reada = 2;
3391 path->search_commit_root = 1;
3392 path->skip_locking = 1;
3393
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003394 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003395 key.offset = 0ull;
3396 key.type = BTRFS_DEV_EXTENT_KEY;
3397
Arne Jansena2de7332011-03-08 14:14:00 +01003398 while (1) {
3399 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3400 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003401 break;
3402 if (ret > 0) {
3403 if (path->slots[0] >=
3404 btrfs_header_nritems(path->nodes[0])) {
3405 ret = btrfs_next_leaf(root, path);
3406 if (ret)
3407 break;
3408 }
3409 }
Arne Jansena2de7332011-03-08 14:14:00 +01003410
3411 l = path->nodes[0];
3412 slot = path->slots[0];
3413
3414 btrfs_item_key_to_cpu(l, &found_key, slot);
3415
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003416 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003417 break;
3418
David Sterba962a2982014-06-04 18:41:45 +02003419 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003420 break;
3421
3422 if (found_key.offset >= end)
3423 break;
3424
3425 if (found_key.offset < key.offset)
3426 break;
3427
3428 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3429 length = btrfs_dev_extent_length(l, dev_extent);
3430
Qu Wenruoced96ed2014-06-19 10:42:51 +08003431 if (found_key.offset + length <= start)
3432 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003433
3434 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3435 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3436 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3437
3438 /*
3439 * get a reference on the corresponding block group to prevent
3440 * the chunk from going away while we scrub it
3441 */
3442 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003443
3444 /* some chunks are removed but not committed to disk yet,
3445 * continue scrubbing */
3446 if (!cache)
3447 goto skip;
3448
Stefan Behrensff023aa2012-11-06 11:43:11 +01003449 dev_replace->cursor_right = found_key.offset + length;
3450 dev_replace->cursor_left = found_key.offset;
3451 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003452 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003453 chunk_offset, length, found_key.offset,
3454 is_dev_replace);
3455
3456 /*
3457 * flush, submit all pending read and write bios, afterwards
3458 * wait for them.
3459 * Note that in the dev replace case, a read request causes
3460 * write requests that are submitted in the read completion
3461 * worker. Therefore in the current situation, it is required
3462 * that all write requests are flushed, so that all read and
3463 * write requests are really completed when bios_in_flight
3464 * changes to 0.
3465 */
3466 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3467 scrub_submit(sctx);
3468 mutex_lock(&sctx->wr_ctx.wr_lock);
3469 scrub_wr_submit(sctx);
3470 mutex_unlock(&sctx->wr_ctx.wr_lock);
3471
3472 wait_event(sctx->list_wait,
3473 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003474 atomic_inc(&fs_info->scrubs_paused);
3475 wake_up(&fs_info->scrub_pause_wait);
3476
3477 /*
3478 * must be called before we decrease @scrub_paused.
3479 * make sure we don't block transaction commit while
3480 * we are waiting pending workers finished.
3481 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003482 wait_event(sctx->list_wait,
3483 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003484 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3485
3486 mutex_lock(&fs_info->scrub_lock);
3487 __scrub_blocked_if_needed(fs_info);
3488 atomic_dec(&fs_info->scrubs_paused);
3489 mutex_unlock(&fs_info->scrub_lock);
3490 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003491
Arne Jansena2de7332011-03-08 14:14:00 +01003492 btrfs_put_block_group(cache);
3493 if (ret)
3494 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003495 if (is_dev_replace &&
3496 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003497 ret = -EIO;
3498 break;
3499 }
3500 if (sctx->stat.malloc_errors > 0) {
3501 ret = -ENOMEM;
3502 break;
3503 }
Arne Jansena2de7332011-03-08 14:14:00 +01003504
Ilya Dryomov539f3582013-10-07 13:42:57 +03003505 dev_replace->cursor_left = dev_replace->cursor_right;
3506 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003507skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003508 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003509 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003510 }
3511
Arne Jansena2de7332011-03-08 14:14:00 +01003512 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003513
3514 /*
3515 * ret can still be 1 from search_slot or next_leaf,
3516 * that's not an error
3517 */
3518 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003519}
3520
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003521static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3522 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003523{
3524 int i;
3525 u64 bytenr;
3526 u64 gen;
3527 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003528 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003529
Miao Xie87533c42013-01-29 10:14:48 +00003530 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003531 return -EIO;
3532
Miao Xie5f546062014-07-24 11:37:09 +08003533 /* Seed devices of a new filesystem has their own generation. */
3534 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3535 gen = scrub_dev->generation;
3536 else
3537 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003538
3539 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3540 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003541 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3542 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003543 break;
3544
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003545 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003546 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003547 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003548 if (ret)
3549 return ret;
3550 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003551 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003552
3553 return 0;
3554}
3555
3556/*
3557 * get a reference count on fs_info->scrub_workers. start worker if necessary
3558 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003559static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3560 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003561{
Josef Bacik0dc3b842011-11-18 14:37:27 -05003562 int ret = 0;
David Sterba6f011052015-02-16 18:34:01 +01003563 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003564 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003565
Arne Jansen632dd772011-06-10 12:07:07 +02003566 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003567 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003568 fs_info->scrub_workers =
3569 btrfs_alloc_workqueue("btrfs-scrub", flags,
3570 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003571 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003572 fs_info->scrub_workers =
3573 btrfs_alloc_workqueue("btrfs-scrub", flags,
3574 max_active, 4);
3575 if (!fs_info->scrub_workers) {
3576 ret = -ENOMEM;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003577 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003578 }
3579 fs_info->scrub_wr_completion_workers =
3580 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3581 max_active, 2);
3582 if (!fs_info->scrub_wr_completion_workers) {
3583 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003584 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003585 }
3586 fs_info->scrub_nocow_workers =
3587 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
3588 if (!fs_info->scrub_nocow_workers) {
3589 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003590 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003591 }
Arne Jansen632dd772011-06-10 12:07:07 +02003592 }
Arne Jansena2de7332011-03-08 14:14:00 +01003593 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003594out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05003595 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003596}
3597
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003598static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003599{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003600 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003601 btrfs_destroy_workqueue(fs_info->scrub_workers);
3602 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3603 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003604 }
Arne Jansena2de7332011-03-08 14:14:00 +01003605 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003606}
3607
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003608int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3609 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003610 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003611{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003612 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003613 int ret;
3614 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003615 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003616
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003617 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003618 return -EINVAL;
3619
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003620 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003621 /*
3622 * in this case scrub is unable to calculate the checksum
3623 * the way scrub is implemented. Do not handle this
3624 * situation at all because it won't ever happen.
3625 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003626 btrfs_err(fs_info,
3627 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003628 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003629 return -EINVAL;
3630 }
3631
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003632 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003633 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003634 btrfs_err(fs_info,
3635 "scrub: size assumption sectorsize != PAGE_SIZE "
3636 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003637 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003638 return -EINVAL;
3639 }
3640
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003641 if (fs_info->chunk_root->nodesize >
3642 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3643 fs_info->chunk_root->sectorsize >
3644 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3645 /*
3646 * would exhaust the array bounds of pagev member in
3647 * struct scrub_block
3648 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003649 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3650 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003651 fs_info->chunk_root->nodesize,
3652 SCRUB_MAX_PAGES_PER_BLOCK,
3653 fs_info->chunk_root->sectorsize,
3654 SCRUB_MAX_PAGES_PER_BLOCK);
3655 return -EINVAL;
3656 }
3657
Arne Jansena2de7332011-03-08 14:14:00 +01003658
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003659 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3660 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003661 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003662 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003663 return -ENODEV;
3664 }
Arne Jansena2de7332011-03-08 14:14:00 +01003665
Miao Xie5d68da32014-07-24 11:37:07 +08003666 if (!is_dev_replace && !readonly && !dev->writeable) {
3667 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3668 rcu_read_lock();
3669 name = rcu_dereference(dev->name);
3670 btrfs_err(fs_info, "scrub: device %s is not writable",
3671 name->str);
3672 rcu_read_unlock();
3673 return -EROFS;
3674 }
3675
Wang Shilong3b7a0162013-10-12 02:11:12 +08003676 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003677 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003678 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003679 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003680 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003681 }
3682
Stefan Behrens8dabb742012-11-06 13:15:27 +01003683 btrfs_dev_replace_lock(&fs_info->dev_replace);
3684 if (dev->scrub_device ||
3685 (!is_dev_replace &&
3686 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3687 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003688 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003689 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003690 return -EINPROGRESS;
3691 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003692 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003693
3694 ret = scrub_workers_get(fs_info, is_dev_replace);
3695 if (ret) {
3696 mutex_unlock(&fs_info->scrub_lock);
3697 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3698 return ret;
3699 }
3700
Stefan Behrens63a212a2012-11-05 18:29:28 +01003701 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003702 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003703 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003704 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3705 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003706 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003707 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003708 sctx->readonly = readonly;
3709 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003710 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003711
Wang Shilong3cb09292013-12-04 21:15:19 +08003712 /*
3713 * checking @scrub_pause_req here, we can avoid
3714 * race between committing transaction and scrubbing.
3715 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003716 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003717 atomic_inc(&fs_info->scrubs_running);
3718 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003719
Stefan Behrensff023aa2012-11-06 11:43:11 +01003720 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003721 /*
3722 * by holding device list mutex, we can
3723 * kick off writing super in log tree sync.
3724 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003725 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003726 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003727 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003728 }
Arne Jansena2de7332011-03-08 14:14:00 +01003729
3730 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003731 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3732 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003733
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003734 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003735 atomic_dec(&fs_info->scrubs_running);
3736 wake_up(&fs_info->scrub_pause_wait);
3737
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003738 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003739
Arne Jansena2de7332011-03-08 14:14:00 +01003740 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003741 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003742
3743 mutex_lock(&fs_info->scrub_lock);
3744 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003745 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003746 mutex_unlock(&fs_info->scrub_lock);
3747
Filipe Mananaf55985f2015-02-09 21:14:24 +00003748 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003749
3750 return ret;
3751}
3752
Jeff Mahoney143bede2012-03-01 14:56:26 +01003753void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003754{
3755 struct btrfs_fs_info *fs_info = root->fs_info;
3756
3757 mutex_lock(&fs_info->scrub_lock);
3758 atomic_inc(&fs_info->scrub_pause_req);
3759 while (atomic_read(&fs_info->scrubs_paused) !=
3760 atomic_read(&fs_info->scrubs_running)) {
3761 mutex_unlock(&fs_info->scrub_lock);
3762 wait_event(fs_info->scrub_pause_wait,
3763 atomic_read(&fs_info->scrubs_paused) ==
3764 atomic_read(&fs_info->scrubs_running));
3765 mutex_lock(&fs_info->scrub_lock);
3766 }
3767 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003768}
3769
Jeff Mahoney143bede2012-03-01 14:56:26 +01003770void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003771{
3772 struct btrfs_fs_info *fs_info = root->fs_info;
3773
3774 atomic_dec(&fs_info->scrub_pause_req);
3775 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003776}
3777
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003778int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003779{
Arne Jansena2de7332011-03-08 14:14:00 +01003780 mutex_lock(&fs_info->scrub_lock);
3781 if (!atomic_read(&fs_info->scrubs_running)) {
3782 mutex_unlock(&fs_info->scrub_lock);
3783 return -ENOTCONN;
3784 }
3785
3786 atomic_inc(&fs_info->scrub_cancel_req);
3787 while (atomic_read(&fs_info->scrubs_running)) {
3788 mutex_unlock(&fs_info->scrub_lock);
3789 wait_event(fs_info->scrub_pause_wait,
3790 atomic_read(&fs_info->scrubs_running) == 0);
3791 mutex_lock(&fs_info->scrub_lock);
3792 }
3793 atomic_dec(&fs_info->scrub_cancel_req);
3794 mutex_unlock(&fs_info->scrub_lock);
3795
3796 return 0;
3797}
3798
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003799int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3800 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003801{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003802 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003803
3804 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003805 sctx = dev->scrub_device;
3806 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003807 mutex_unlock(&fs_info->scrub_lock);
3808 return -ENOTCONN;
3809 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003810 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003811 while (dev->scrub_device) {
3812 mutex_unlock(&fs_info->scrub_lock);
3813 wait_event(fs_info->scrub_pause_wait,
3814 dev->scrub_device == NULL);
3815 mutex_lock(&fs_info->scrub_lock);
3816 }
3817 mutex_unlock(&fs_info->scrub_lock);
3818
3819 return 0;
3820}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003821
Arne Jansena2de7332011-03-08 14:14:00 +01003822int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3823 struct btrfs_scrub_progress *progress)
3824{
3825 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003826 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003827
3828 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003829 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003830 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003831 sctx = dev->scrub_device;
3832 if (sctx)
3833 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003834 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3835
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003836 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003837}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003838
3839static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3840 u64 extent_logical, u64 extent_len,
3841 u64 *extent_physical,
3842 struct btrfs_device **extent_dev,
3843 int *extent_mirror_num)
3844{
3845 u64 mapped_length;
3846 struct btrfs_bio *bbio = NULL;
3847 int ret;
3848
3849 mapped_length = extent_len;
3850 ret = btrfs_map_block(fs_info, READ, extent_logical,
3851 &mapped_length, &bbio, 0);
3852 if (ret || !bbio || mapped_length < extent_len ||
3853 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08003854 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003855 return;
3856 }
3857
3858 *extent_physical = bbio->stripes[0].physical;
3859 *extent_mirror_num = bbio->mirror_num;
3860 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08003861 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003862}
3863
3864static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3865 struct scrub_wr_ctx *wr_ctx,
3866 struct btrfs_fs_info *fs_info,
3867 struct btrfs_device *dev,
3868 int is_dev_replace)
3869{
3870 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3871
3872 mutex_init(&wr_ctx->wr_lock);
3873 wr_ctx->wr_curr_bio = NULL;
3874 if (!is_dev_replace)
3875 return 0;
3876
3877 WARN_ON(!dev->bdev);
3878 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3879 bio_get_nr_vecs(dev->bdev));
3880 wr_ctx->tgtdev = dev;
3881 atomic_set(&wr_ctx->flush_all_writes, 0);
3882 return 0;
3883}
3884
3885static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3886{
3887 mutex_lock(&wr_ctx->wr_lock);
3888 kfree(wr_ctx->wr_curr_bio);
3889 wr_ctx->wr_curr_bio = NULL;
3890 mutex_unlock(&wr_ctx->wr_lock);
3891}
3892
3893static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3894 int mirror_num, u64 physical_for_dev_replace)
3895{
3896 struct scrub_copy_nocow_ctx *nocow_ctx;
3897 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3898
3899 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3900 if (!nocow_ctx) {
3901 spin_lock(&sctx->stat_lock);
3902 sctx->stat.malloc_errors++;
3903 spin_unlock(&sctx->stat_lock);
3904 return -ENOMEM;
3905 }
3906
3907 scrub_pending_trans_workers_inc(sctx);
3908
3909 nocow_ctx->sctx = sctx;
3910 nocow_ctx->logical = logical;
3911 nocow_ctx->len = len;
3912 nocow_ctx->mirror_num = mirror_num;
3913 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08003914 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3915 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003916 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003917 btrfs_queue_work(fs_info->scrub_nocow_workers,
3918 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003919
3920 return 0;
3921}
3922
Josef Bacik652f25a2013-09-12 16:58:28 -04003923static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3924{
3925 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3926 struct scrub_nocow_inode *nocow_inode;
3927
3928 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3929 if (!nocow_inode)
3930 return -ENOMEM;
3931 nocow_inode->inum = inum;
3932 nocow_inode->offset = offset;
3933 nocow_inode->root = root;
3934 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3935 return 0;
3936}
3937
3938#define COPY_COMPLETE 1
3939
Stefan Behrensff023aa2012-11-06 11:43:11 +01003940static void copy_nocow_pages_worker(struct btrfs_work *work)
3941{
3942 struct scrub_copy_nocow_ctx *nocow_ctx =
3943 container_of(work, struct scrub_copy_nocow_ctx, work);
3944 struct scrub_ctx *sctx = nocow_ctx->sctx;
3945 u64 logical = nocow_ctx->logical;
3946 u64 len = nocow_ctx->len;
3947 int mirror_num = nocow_ctx->mirror_num;
3948 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3949 int ret;
3950 struct btrfs_trans_handle *trans = NULL;
3951 struct btrfs_fs_info *fs_info;
3952 struct btrfs_path *path;
3953 struct btrfs_root *root;
3954 int not_written = 0;
3955
3956 fs_info = sctx->dev_root->fs_info;
3957 root = fs_info->extent_root;
3958
3959 path = btrfs_alloc_path();
3960 if (!path) {
3961 spin_lock(&sctx->stat_lock);
3962 sctx->stat.malloc_errors++;
3963 spin_unlock(&sctx->stat_lock);
3964 not_written = 1;
3965 goto out;
3966 }
3967
3968 trans = btrfs_join_transaction(root);
3969 if (IS_ERR(trans)) {
3970 not_written = 1;
3971 goto out;
3972 }
3973
3974 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04003975 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003976 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003977 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
3978 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02003979 logical, physical_for_dev_replace, len, mirror_num,
3980 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003981 not_written = 1;
3982 goto out;
3983 }
3984
Josef Bacik652f25a2013-09-12 16:58:28 -04003985 btrfs_end_transaction(trans, root);
3986 trans = NULL;
3987 while (!list_empty(&nocow_ctx->inodes)) {
3988 struct scrub_nocow_inode *entry;
3989 entry = list_first_entry(&nocow_ctx->inodes,
3990 struct scrub_nocow_inode,
3991 list);
3992 list_del_init(&entry->list);
3993 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
3994 entry->root, nocow_ctx);
3995 kfree(entry);
3996 if (ret == COPY_COMPLETE) {
3997 ret = 0;
3998 break;
3999 } else if (ret) {
4000 break;
4001 }
4002 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004003out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004004 while (!list_empty(&nocow_ctx->inodes)) {
4005 struct scrub_nocow_inode *entry;
4006 entry = list_first_entry(&nocow_ctx->inodes,
4007 struct scrub_nocow_inode,
4008 list);
4009 list_del_init(&entry->list);
4010 kfree(entry);
4011 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004012 if (trans && !IS_ERR(trans))
4013 btrfs_end_transaction(trans, root);
4014 if (not_written)
4015 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4016 num_uncorrectable_read_errors);
4017
4018 btrfs_free_path(path);
4019 kfree(nocow_ctx);
4020
4021 scrub_pending_trans_workers_dec(sctx);
4022}
4023
Gui Hecheng32159242014-11-10 15:36:08 +08004024static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4025 u64 logical)
4026{
4027 struct extent_state *cached_state = NULL;
4028 struct btrfs_ordered_extent *ordered;
4029 struct extent_io_tree *io_tree;
4030 struct extent_map *em;
4031 u64 lockstart = start, lockend = start + len - 1;
4032 int ret = 0;
4033
4034 io_tree = &BTRFS_I(inode)->io_tree;
4035
4036 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4037 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4038 if (ordered) {
4039 btrfs_put_ordered_extent(ordered);
4040 ret = 1;
4041 goto out_unlock;
4042 }
4043
4044 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4045 if (IS_ERR(em)) {
4046 ret = PTR_ERR(em);
4047 goto out_unlock;
4048 }
4049
4050 /*
4051 * This extent does not actually cover the logical extent anymore,
4052 * move on to the next inode.
4053 */
4054 if (em->block_start > logical ||
4055 em->block_start + em->block_len < logical + len) {
4056 free_extent_map(em);
4057 ret = 1;
4058 goto out_unlock;
4059 }
4060 free_extent_map(em);
4061
4062out_unlock:
4063 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4064 GFP_NOFS);
4065 return ret;
4066}
4067
Josef Bacik652f25a2013-09-12 16:58:28 -04004068static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4069 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004070{
Miao Xie826aa0a2013-06-27 18:50:59 +08004071 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004072 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004073 struct inode *inode;
4074 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004075 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004076 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004077 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004078 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004079 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004080 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004081 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004082 int ret = 0;
4083 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004084
4085 key.objectid = root;
4086 key.type = BTRFS_ROOT_ITEM_KEY;
4087 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004088
4089 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4090
Stefan Behrensff023aa2012-11-06 11:43:11 +01004091 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004092 if (IS_ERR(local_root)) {
4093 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004094 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004095 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004096
4097 key.type = BTRFS_INODE_ITEM_KEY;
4098 key.objectid = inum;
4099 key.offset = 0;
4100 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004101 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004102 if (IS_ERR(inode))
4103 return PTR_ERR(inode);
4104
Miao Xieedd14002013-06-27 18:51:00 +08004105 /* Avoid truncate/dio/punch hole.. */
4106 mutex_lock(&inode->i_mutex);
4107 inode_dio_wait(inode);
4108
Stefan Behrensff023aa2012-11-06 11:43:11 +01004109 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004110 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004111 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004112
Gui Hecheng32159242014-11-10 15:36:08 +08004113 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4114 if (ret) {
4115 ret = ret > 0 ? 0 : ret;
4116 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004117 }
4118
Stefan Behrensff023aa2012-11-06 11:43:11 +01004119 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004120 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004121again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004122 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4123 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004124 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004125 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004126 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004127 }
4128
4129 if (PageUptodate(page)) {
4130 if (PageDirty(page))
4131 goto next_page;
4132 } else {
4133 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004134 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004135 btrfs_get_extent,
4136 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004137 if (err) {
4138 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004139 goto next_page;
4140 }
Miao Xieedd14002013-06-27 18:51:00 +08004141
Miao Xie26b258912013-06-27 18:50:58 +08004142 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004143 /*
4144 * If the page has been remove from the page cache,
4145 * the data on it is meaningless, because it may be
4146 * old one, the new data may be written into the new
4147 * page in the page cache.
4148 */
4149 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004150 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004151 page_cache_release(page);
4152 goto again;
4153 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004154 if (!PageUptodate(page)) {
4155 ret = -EIO;
4156 goto next_page;
4157 }
4158 }
Gui Hecheng32159242014-11-10 15:36:08 +08004159
4160 ret = check_extent_to_block(inode, offset, len,
4161 nocow_ctx_logical);
4162 if (ret) {
4163 ret = ret > 0 ? 0 : ret;
4164 goto next_page;
4165 }
4166
Miao Xie826aa0a2013-06-27 18:50:59 +08004167 err = write_page_nocow(nocow_ctx->sctx,
4168 physical_for_dev_replace, page);
4169 if (err)
4170 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004171next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004172 unlock_page(page);
4173 page_cache_release(page);
4174
4175 if (ret)
4176 break;
4177
Stefan Behrensff023aa2012-11-06 11:43:11 +01004178 offset += PAGE_CACHE_SIZE;
4179 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004180 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004181 len -= PAGE_CACHE_SIZE;
4182 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004183 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004184out:
Miao Xieedd14002013-06-27 18:51:00 +08004185 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004186 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004187 return ret;
4188}
4189
4190static int write_page_nocow(struct scrub_ctx *sctx,
4191 u64 physical_for_dev_replace, struct page *page)
4192{
4193 struct bio *bio;
4194 struct btrfs_device *dev;
4195 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004196
4197 dev = sctx->wr_ctx.tgtdev;
4198 if (!dev)
4199 return -EIO;
4200 if (!dev->bdev) {
4201 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05004202 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004203 return -EIO;
4204 }
Chris Mason9be33952013-05-17 18:30:14 -04004205 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004206 if (!bio) {
4207 spin_lock(&sctx->stat_lock);
4208 sctx->stat.malloc_errors++;
4209 spin_unlock(&sctx->stat_lock);
4210 return -ENOMEM;
4211 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004212 bio->bi_iter.bi_size = 0;
4213 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004214 bio->bi_bdev = dev->bdev;
4215 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4216 if (ret != PAGE_CACHE_SIZE) {
4217leave_with_eio:
4218 bio_put(bio);
4219 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4220 return -EIO;
4221 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004222
Kent Overstreet33879d42013-11-23 22:33:32 -08004223 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004224 goto leave_with_eio;
4225
4226 bio_put(bio);
4227 return 0;
4228}