blob: c40ffbc9581f781be9872184f0ef19410ce777fa [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;
Stefan Behrens7a9e9982012-11-02 14:58:04 +010082 atomic_t ref_count;
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;
115 atomic_t ref_count; /* 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
144 atomic_t ref_count;
145
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;
196};
197
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200198struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100199 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100200 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200201 u64 logical;
202 struct btrfs_root *root;
203 struct btrfs_work work;
204 int mirror_num;
205};
206
Josef Bacik652f25a2013-09-12 16:58:28 -0400207struct scrub_nocow_inode {
208 u64 inum;
209 u64 offset;
210 u64 root;
211 struct list_head list;
212};
213
Stefan Behrensff023aa2012-11-06 11:43:11 +0100214struct scrub_copy_nocow_ctx {
215 struct scrub_ctx *sctx;
216 u64 logical;
217 u64 len;
218 int mirror_num;
219 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -0400220 struct list_head inodes;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100221 struct btrfs_work work;
222};
223
Jan Schmidt558540c2011-06-13 19:59:12 +0200224struct scrub_warning {
225 struct btrfs_path *path;
226 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200227 const char *errstr;
228 sector_t sector;
229 u64 logical;
230 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200231};
232
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100233static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
234static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
235static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
236static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400237static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100238static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100239 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100240 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400241 u64 length, u64 logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100242 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100243static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
244 struct scrub_block *sblock, int is_metadata,
245 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800246 u16 csum_size, int retry_failed_mirror);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400247static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
248 struct scrub_block *sblock,
249 int is_metadata, int have_csum,
250 const u8 *csum, u64 generation,
251 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400252static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800253 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400254static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
255 struct scrub_block *sblock_good,
256 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100257static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
258static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
259 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400260static int scrub_checksum_data(struct scrub_block *sblock);
261static int scrub_checksum_tree_block(struct scrub_block *sblock);
262static int scrub_checksum_super(struct scrub_block *sblock);
263static void scrub_block_get(struct scrub_block *sblock);
264static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100265static void scrub_page_get(struct scrub_page *spage);
266static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800267static void scrub_parity_get(struct scrub_parity *sparity);
268static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100269static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
270 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100271static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100272 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100273 u64 gen, int mirror_num, u8 *csum, int force,
274 u64 physical_for_dev_replace);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400275static void scrub_bio_end_io(struct bio *bio, int err);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400276static void scrub_bio_end_io_worker(struct btrfs_work *work);
277static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100278static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
279 u64 extent_logical, u64 extent_len,
280 u64 *extent_physical,
281 struct btrfs_device **extent_dev,
282 int *extent_mirror_num);
283static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
284 struct scrub_wr_ctx *wr_ctx,
285 struct btrfs_fs_info *fs_info,
286 struct btrfs_device *dev,
287 int is_dev_replace);
288static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
289static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
290 struct scrub_page *spage);
291static void scrub_wr_submit(struct scrub_ctx *sctx);
292static void scrub_wr_bio_end_io(struct bio *bio, int err);
293static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
294static int write_page_nocow(struct scrub_ctx *sctx,
295 u64 physical_for_dev_replace, struct page *page);
296static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400297 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100298static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
299 int mirror_num, u64 physical_for_dev_replace);
300static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800301static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800302static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400303
304
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100305static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
306{
307 atomic_inc(&sctx->bios_in_flight);
308}
309
310static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
311{
312 atomic_dec(&sctx->bios_in_flight);
313 wake_up(&sctx->list_wait);
314}
315
Wang Shilongcb7ab022013-12-04 21:16:53 +0800316static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800317{
318 while (atomic_read(&fs_info->scrub_pause_req)) {
319 mutex_unlock(&fs_info->scrub_lock);
320 wait_event(fs_info->scrub_pause_wait,
321 atomic_read(&fs_info->scrub_pause_req) == 0);
322 mutex_lock(&fs_info->scrub_lock);
323 }
324}
325
Wang Shilongcb7ab022013-12-04 21:16:53 +0800326static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
327{
328 atomic_inc(&fs_info->scrubs_paused);
329 wake_up(&fs_info->scrub_pause_wait);
330
331 mutex_lock(&fs_info->scrub_lock);
332 __scrub_blocked_if_needed(fs_info);
333 atomic_dec(&fs_info->scrubs_paused);
334 mutex_unlock(&fs_info->scrub_lock);
335
336 wake_up(&fs_info->scrub_pause_wait);
337}
338
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100339/*
340 * used for workers that require transaction commits (i.e., for the
341 * NOCOW case)
342 */
343static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
344{
345 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
346
347 /*
348 * increment scrubs_running to prevent cancel requests from
349 * completing as long as a worker is running. we must also
350 * increment scrubs_paused to prevent deadlocking on pause
351 * requests used for transactions commits (as the worker uses a
352 * transaction context). it is safe to regard the worker
353 * as paused for all matters practical. effectively, we only
354 * avoid cancellation requests from completing.
355 */
356 mutex_lock(&fs_info->scrub_lock);
357 atomic_inc(&fs_info->scrubs_running);
358 atomic_inc(&fs_info->scrubs_paused);
359 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800360
361 /*
362 * check if @scrubs_running=@scrubs_paused condition
363 * inside wait_event() is not an atomic operation.
364 * which means we may inc/dec @scrub_running/paused
365 * at any time. Let's wake up @scrub_pause_wait as
366 * much as we can to let commit transaction blocked less.
367 */
368 wake_up(&fs_info->scrub_pause_wait);
369
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100370 atomic_inc(&sctx->workers_pending);
371}
372
373/* used for workers that require transaction commits */
374static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
375{
376 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
377
378 /*
379 * see scrub_pending_trans_workers_inc() why we're pretending
380 * to be paused in the scrub counters
381 */
382 mutex_lock(&fs_info->scrub_lock);
383 atomic_dec(&fs_info->scrubs_running);
384 atomic_dec(&fs_info->scrubs_paused);
385 mutex_unlock(&fs_info->scrub_lock);
386 atomic_dec(&sctx->workers_pending);
387 wake_up(&fs_info->scrub_pause_wait);
388 wake_up(&sctx->list_wait);
389}
390
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100391static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100392{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100393 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100394 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100395 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100396 struct btrfs_ordered_sum, list);
397 list_del(&sum->list);
398 kfree(sum);
399 }
400}
401
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100402static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100403{
404 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100405
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100406 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100407 return;
408
Stefan Behrensff023aa2012-11-06 11:43:11 +0100409 scrub_free_wr_ctx(&sctx->wr_ctx);
410
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400411 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100412 if (sctx->curr != -1) {
413 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400414
415 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100416 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400417 scrub_block_put(sbio->pagev[i]->sblock);
418 }
419 bio_put(sbio->bio);
420 }
421
Stefan Behrensff023aa2012-11-06 11:43:11 +0100422 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100423 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100424
425 if (!sbio)
426 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100427 kfree(sbio);
428 }
429
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100430 scrub_free_csums(sctx);
431 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100432}
433
434static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100435struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100436{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100437 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100438 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100439 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100440 int pages_per_rd_bio;
441 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100442
Stefan Behrensff023aa2012-11-06 11:43:11 +0100443 /*
444 * the setting of pages_per_rd_bio is correct for scrub but might
445 * be wrong for the dev_replace code where we might read from
446 * different devices in the initial huge bios. However, that
447 * code is able to correctly handle the case when adding a page
448 * to a bio fails.
449 */
450 if (dev->bdev)
451 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
452 bio_get_nr_vecs(dev->bdev));
453 else
454 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100455 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
456 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100457 goto nomem;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100458 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100459 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100460 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100461 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100462 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100463 struct scrub_bio *sbio;
464
465 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
466 if (!sbio)
467 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100468 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100469
Arne Jansena2de7332011-03-08 14:14:00 +0100470 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100471 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400472 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800473 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
474 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100475
Stefan Behrensff023aa2012-11-06 11:43:11 +0100476 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100477 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200478 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100479 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100480 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100481 sctx->first_free = 0;
482 sctx->nodesize = dev->dev_root->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100483 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100484 atomic_set(&sctx->bios_in_flight, 0);
485 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100486 atomic_set(&sctx->cancel_req, 0);
487 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
488 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100489
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100490 spin_lock_init(&sctx->list_lock);
491 spin_lock_init(&sctx->stat_lock);
492 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100493
494 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
495 fs_info->dev_replace.tgtdev, is_dev_replace);
496 if (ret) {
497 scrub_free_ctx(sctx);
498 return ERR_PTR(ret);
499 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100500 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100501
502nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100503 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100504 return ERR_PTR(-ENOMEM);
505}
506
Stefan Behrensff023aa2012-11-06 11:43:11 +0100507static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
508 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200509{
510 u64 isize;
511 u32 nlink;
512 int ret;
513 int i;
514 struct extent_buffer *eb;
515 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100516 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200517 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
518 struct inode_fs_paths *ipath = NULL;
519 struct btrfs_root *local_root;
520 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100521 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200522
523 root_key.objectid = root;
524 root_key.type = BTRFS_ROOT_ITEM_KEY;
525 root_key.offset = (u64)-1;
526 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
527 if (IS_ERR(local_root)) {
528 ret = PTR_ERR(local_root);
529 goto err;
530 }
531
David Sterba14692cc2015-01-02 18:55:46 +0100532 /*
533 * this makes the path point to (inum INODE_ITEM ioff)
534 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100535 key.objectid = inum;
536 key.type = BTRFS_INODE_ITEM_KEY;
537 key.offset = 0;
538
539 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200540 if (ret) {
541 btrfs_release_path(swarn->path);
542 goto err;
543 }
544
545 eb = swarn->path->nodes[0];
546 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
547 struct btrfs_inode_item);
548 isize = btrfs_inode_size(eb, inode_item);
549 nlink = btrfs_inode_nlink(eb, inode_item);
550 btrfs_release_path(swarn->path);
551
552 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300553 if (IS_ERR(ipath)) {
554 ret = PTR_ERR(ipath);
555 ipath = NULL;
556 goto err;
557 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200558 ret = paths_from_inode(inum, ipath);
559
560 if (ret < 0)
561 goto err;
562
563 /*
564 * we deliberately ignore the bit ipath might have been too small to
565 * hold all of the paths here
566 */
567 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Frank Holtonefe120a2013-12-20 11:37:06 -0500568 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200569 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
570 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400571 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200572 (unsigned long long)swarn->sector, root, inum, offset,
573 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500574 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200575
576 free_ipath(ipath);
577 return 0;
578
579err:
Frank Holtonefe120a2013-12-20 11:37:06 -0500580 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200581 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
582 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400583 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200584 (unsigned long long)swarn->sector, root, inum, offset, ret);
585
586 free_ipath(ipath);
587 return 0;
588}
589
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400590static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200591{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100592 struct btrfs_device *dev;
593 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200594 struct btrfs_path *path;
595 struct btrfs_key found_key;
596 struct extent_buffer *eb;
597 struct btrfs_extent_item *ei;
598 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200599 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100600 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600601 u64 flags = 0;
602 u64 ref_root;
603 u32 item_size;
604 u8 ref_level;
Liu Bo69917e42012-09-07 20:01:28 -0600605 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200606
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100607 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100608 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100609 fs_info = sblock->sctx->dev_root->fs_info;
610
Jan Schmidt558540c2011-06-13 19:59:12 +0200611 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200612 if (!path)
613 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200614
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100615 swarn.sector = (sblock->pagev[0]->physical) >> 9;
616 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200617 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100618 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200619
Liu Bo69917e42012-09-07 20:01:28 -0600620 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
621 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200622 if (ret < 0)
623 goto out;
624
Jan Schmidt4692cf52011-12-02 14:56:41 +0100625 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200626 swarn.extent_item_size = found_key.offset;
627
628 eb = path->nodes[0];
629 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
630 item_size = btrfs_item_size_nr(eb, path->slots[0]);
631
Liu Bo69917e42012-09-07 20:01:28 -0600632 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200633 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800634 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
635 item_size, &ref_root,
636 &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400637 printk_in_rcu(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -0500638 "BTRFS: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200639 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400640 "%llu\n", errstr, swarn.logical,
641 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200642 (unsigned long long)swarn.sector,
643 ref_level ? "node" : "leaf",
644 ret < 0 ? -1 : ref_level,
645 ret < 0 ? -1 : ref_root);
646 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600647 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200648 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600649 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200650 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100651 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100652 iterate_extent_inodes(fs_info, found_key.objectid,
653 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200654 scrub_print_warning_inode, &swarn);
655 }
656
657out:
658 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200659}
660
Stefan Behrensff023aa2012-11-06 11:43:11 +0100661static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200662{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200663 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200664 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100665 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200666 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200667 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200668 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200669 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000670 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200671 u64 end = offset + PAGE_SIZE - 1;
672 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000673 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200674
675 key.objectid = root;
676 key.type = BTRFS_ROOT_ITEM_KEY;
677 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000678
679 fs_info = fixup->root->fs_info;
680 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
681
682 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
683 if (IS_ERR(local_root)) {
684 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200685 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000686 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200687
688 key.type = BTRFS_INODE_ITEM_KEY;
689 key.objectid = inum;
690 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000691 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
692 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200693 if (IS_ERR(inode))
694 return PTR_ERR(inode);
695
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200696 index = offset >> PAGE_CACHE_SHIFT;
697
698 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200699 if (!page) {
700 ret = -ENOMEM;
701 goto out;
702 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200703
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200704 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200705 if (PageDirty(page)) {
706 /*
707 * we need to write the data to the defect sector. the
708 * data that was in that sector is not in memory,
709 * because the page was modified. we must not write the
710 * modified page to that sector.
711 *
712 * TODO: what could be done here: wait for the delalloc
713 * runner to write out that page (might involve
714 * COW) and see whether the sector is still
715 * referenced afterwards.
716 *
717 * For the meantime, we'll treat this error
718 * incorrectable, although there is a chance that a
719 * later scrub will find the bad sector again and that
720 * there's no dirty page in memory, then.
721 */
722 ret = -EIO;
723 goto out;
724 }
Miao Xie1203b682014-09-12 18:44:01 +0800725 ret = repair_io_failure(inode, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200726 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800727 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200728 fixup->mirror_num);
729 unlock_page(page);
730 corrected = !ret;
731 } else {
732 /*
733 * we need to get good data first. the general readpage path
734 * will call repair_io_failure for us, we just have to make
735 * sure we read the bad mirror.
736 */
737 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
738 EXTENT_DAMAGED, GFP_NOFS);
739 if (ret) {
740 /* set_extent_bits should give proper error */
741 WARN_ON(ret > 0);
742 if (ret > 0)
743 ret = -EFAULT;
744 goto out;
745 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200746
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200747 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
748 btrfs_get_extent,
749 fixup->mirror_num);
750 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200751
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200752 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
753 end, EXTENT_DAMAGED, 0, NULL);
754 if (!corrected)
755 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
756 EXTENT_DAMAGED, GFP_NOFS);
757 }
758
759out:
760 if (page)
761 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200762
763 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200764
765 if (ret < 0)
766 return ret;
767
768 if (ret == 0 && corrected) {
769 /*
770 * we only need to call readpage for one of the inodes belonging
771 * to this extent. so make iterate_extent_inodes stop
772 */
773 return 1;
774 }
775
776 return -EIO;
777}
778
779static void scrub_fixup_nodatasum(struct btrfs_work *work)
780{
781 int ret;
782 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100783 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200784 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200785 struct btrfs_path *path;
786 int uncorrectable = 0;
787
788 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100789 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200790
791 path = btrfs_alloc_path();
792 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100793 spin_lock(&sctx->stat_lock);
794 ++sctx->stat.malloc_errors;
795 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200796 uncorrectable = 1;
797 goto out;
798 }
799
800 trans = btrfs_join_transaction(fixup->root);
801 if (IS_ERR(trans)) {
802 uncorrectable = 1;
803 goto out;
804 }
805
806 /*
807 * the idea is to trigger a regular read through the standard path. we
808 * read a page from the (failed) logical address by specifying the
809 * corresponding copynum of the failed sector. thus, that readpage is
810 * expected to fail.
811 * that is the point where on-the-fly error correction will kick in
812 * (once it's finished) and rewrite the failed sector if a good copy
813 * can be found.
814 */
815 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
816 path, scrub_fixup_readpage,
817 fixup);
818 if (ret < 0) {
819 uncorrectable = 1;
820 goto out;
821 }
822 WARN_ON(ret != 1);
823
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100824 spin_lock(&sctx->stat_lock);
825 ++sctx->stat.corrected_errors;
826 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200827
828out:
829 if (trans && !IS_ERR(trans))
830 btrfs_end_transaction(trans, fixup->root);
831 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100832 spin_lock(&sctx->stat_lock);
833 ++sctx->stat.uncorrectable_errors;
834 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100835 btrfs_dev_replace_stats_inc(
836 &sctx->dev_root->fs_info->dev_replace.
837 num_uncorrectable_read_errors);
Frank Holtonefe120a2013-12-20 11:37:06 -0500838 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
839 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200840 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200841 }
842
843 btrfs_free_path(path);
844 kfree(fixup);
845
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100846 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200847}
848
Miao Xieaf8e2d12014-10-23 14:42:50 +0800849static inline void scrub_get_recover(struct scrub_recover *recover)
850{
851 atomic_inc(&recover->refs);
852}
853
854static inline void scrub_put_recover(struct scrub_recover *recover)
855{
856 if (atomic_dec_and_test(&recover->refs)) {
Zhao Lei6e9606d2015-01-20 15:11:34 +0800857 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800858 kfree(recover);
859 }
860}
861
Arne Jansena2de7332011-03-08 14:14:00 +0100862/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400863 * scrub_handle_errored_block gets called when either verification of the
864 * pages failed or the bio failed to read, e.g. with EIO. In the latter
865 * case, this function handles all pages in the bio, even though only one
866 * may be bad.
867 * The goal of this function is to repair the errored block by using the
868 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100869 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400870static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100871{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100872 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100873 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400874 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100875 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400876 u64 logical;
877 u64 generation;
878 unsigned int failed_mirror_index;
879 unsigned int is_metadata;
880 unsigned int have_csum;
881 u8 *csum;
882 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
883 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100884 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400885 int mirror_index;
886 int page_num;
887 int success;
888 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
889 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100890
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400891 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100892 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000893 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
894 /*
895 * if we find an error in a super block, we just report it.
896 * They will get written with the next transaction commit
897 * anyway
898 */
899 spin_lock(&sctx->stat_lock);
900 ++sctx->stat.super_errors;
901 spin_unlock(&sctx->stat_lock);
902 return 0;
903 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400904 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100905 logical = sblock_to_check->pagev[0]->logical;
906 generation = sblock_to_check->pagev[0]->generation;
907 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
908 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
909 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400910 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100911 have_csum = sblock_to_check->pagev[0]->have_csum;
912 csum = sblock_to_check->pagev[0]->csum;
913 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400914
Stefan Behrensff023aa2012-11-06 11:43:11 +0100915 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
916 sblocks_for_recheck = NULL;
917 goto nodatasum_case;
918 }
919
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400920 /*
921 * read all mirrors one after the other. This includes to
922 * re-read the extent or metadata block that failed (that was
923 * the cause that this fixup code is called) another time,
924 * page by page this time in order to know which pages
925 * caused I/O errors and which ones are good (for all mirrors).
926 * It is the goal to handle the situation when more than one
927 * mirror contains I/O errors, but the errors do not
928 * overlap, i.e. the data can be repaired by selecting the
929 * pages from those mirrors without I/O error on the
930 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
931 * would be that mirror #1 has an I/O error on the first page,
932 * the second page is good, and mirror #2 has an I/O error on
933 * the second page, but the first page is good.
934 * Then the first page of the first mirror can be repaired by
935 * taking the first page of the second mirror, and the
936 * second page of the second mirror can be repaired by
937 * copying the contents of the 2nd page of the 1st mirror.
938 * One more note: if the pages of one mirror contain I/O
939 * errors, the checksum cannot be verified. In order to get
940 * the best data for repairing, the first attempt is to find
941 * a mirror without I/O errors and with a validated checksum.
942 * Only if this is not possible, the pages are picked from
943 * mirrors with I/O errors without considering the checksum.
944 * If the latter is the case, at the end, the checksum of the
945 * repaired area is verified in order to correctly maintain
946 * the statistics.
947 */
948
949 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
950 sizeof(*sblocks_for_recheck),
951 GFP_NOFS);
952 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100953 spin_lock(&sctx->stat_lock);
954 sctx->stat.malloc_errors++;
955 sctx->stat.read_errors++;
956 sctx->stat.uncorrectable_errors++;
957 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100958 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400959 goto out;
960 }
961
962 /* setup the context, map the logical blocks and alloc the pages */
Stefan Behrensff023aa2012-11-06 11:43:11 +0100963 ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400964 logical, sblocks_for_recheck);
965 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100966 spin_lock(&sctx->stat_lock);
967 sctx->stat.read_errors++;
968 sctx->stat.uncorrectable_errors++;
969 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100970 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400971 goto out;
972 }
973 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
974 sblock_bad = sblocks_for_recheck + failed_mirror_index;
975
976 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100977 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800978 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400979
980 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
981 sblock_bad->no_io_error_seen) {
982 /*
983 * the error disappeared after reading page by page, or
984 * the area was part of a huge bio and other parts of the
985 * bio caused I/O errors, or the block layer merged several
986 * read requests into one and the error is caused by a
987 * different bio (usually one of the two latter cases is
988 * the cause)
989 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100990 spin_lock(&sctx->stat_lock);
991 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800992 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100993 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400994
Stefan Behrensff023aa2012-11-06 11:43:11 +0100995 if (sctx->is_dev_replace)
996 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400997 goto out;
998 }
999
1000 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001001 spin_lock(&sctx->stat_lock);
1002 sctx->stat.read_errors++;
1003 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001004 if (__ratelimit(&_rs))
1005 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001006 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001007 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001008 spin_lock(&sctx->stat_lock);
1009 sctx->stat.csum_errors++;
1010 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001011 if (__ratelimit(&_rs))
1012 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001013 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001014 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001015 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001016 spin_lock(&sctx->stat_lock);
1017 sctx->stat.verify_errors++;
1018 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001019 if (__ratelimit(&_rs))
1020 scrub_print_warning("checksum/header error",
1021 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001022 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001023 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001024 BTRFS_DEV_STAT_GENERATION_ERRS);
1025 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001026 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001027 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001028 }
1029
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001030 if (sctx->readonly) {
1031 ASSERT(!sctx->is_dev_replace);
1032 goto out;
1033 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001034
1035 if (!is_metadata && !have_csum) {
1036 struct scrub_fixup_nodatasum *fixup_nodatasum;
1037
Stefan Behrensff023aa2012-11-06 11:43:11 +01001038 WARN_ON(sctx->is_dev_replace);
1039
Zhao Leib25c94c2015-01-20 15:11:35 +08001040nodatasum_case:
1041
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001042 /*
1043 * !is_metadata and !have_csum, this means that the data
1044 * might not be COW'ed, that it might be modified
1045 * concurrently. The general strategy to work on the
1046 * commit root does not help in the case when COW is not
1047 * used.
1048 */
1049 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1050 if (!fixup_nodatasum)
1051 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001052 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001053 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001054 fixup_nodatasum->logical = logical;
1055 fixup_nodatasum->root = fs_info->extent_root;
1056 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001057 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001058 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1059 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001060 btrfs_queue_work(fs_info->scrub_workers,
1061 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001062 goto out;
1063 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001064
1065 /*
1066 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001067 * checksums.
1068 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001069 * errors and also does not have a checksum error.
1070 * If one is found, and if a checksum is present, the full block
1071 * that is known to contain an error is rewritten. Afterwards
1072 * the block is known to be corrected.
1073 * If a mirror is found which is completely correct, and no
1074 * checksum is present, only those pages are rewritten that had
1075 * an I/O error in the block to be repaired, since it cannot be
1076 * determined, which copy of the other pages is better (and it
1077 * could happen otherwise that a correct page would be
1078 * overwritten by a bad one).
1079 */
1080 for (mirror_index = 0;
1081 mirror_index < BTRFS_MAX_MIRRORS &&
1082 sblocks_for_recheck[mirror_index].page_count > 0;
1083 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001084 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001085
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001086 if (mirror_index == failed_mirror_index)
1087 continue;
1088 sblock_other = sblocks_for_recheck + mirror_index;
1089
1090 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001091 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1092 have_csum, csum, generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001093 sctx->csum_size, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001094
1095 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001096 !sblock_other->checksum_error &&
1097 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001098 if (sctx->is_dev_replace) {
1099 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001100 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001101 } else {
1102 ret = scrub_repair_block_from_good_copy(
1103 sblock_bad, sblock_other);
1104 if (!ret)
1105 goto corrected_error;
1106 }
Arne Jansena2de7332011-03-08 14:14:00 +01001107 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001108 }
1109
1110 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001111 * for dev_replace, pick good pages and write to the target device.
1112 */
1113 if (sctx->is_dev_replace) {
1114 success = 1;
1115 for (page_num = 0; page_num < sblock_bad->page_count;
1116 page_num++) {
Zhao Lei8d6738c2015-01-20 15:11:40 +08001117 struct scrub_block *sblock_other = NULL;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001118
Stefan Behrensff023aa2012-11-06 11:43:11 +01001119 for (mirror_index = 0;
1120 mirror_index < BTRFS_MAX_MIRRORS &&
1121 sblocks_for_recheck[mirror_index].page_count > 0;
1122 mirror_index++) {
Zhao Lei8d6738c2015-01-20 15:11:40 +08001123 if (!sblocks_for_recheck[mirror_index].
1124 pagev[page_num]->io_error) {
1125 sblock_other = sblocks_for_recheck +
1126 mirror_index;
1127 break;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001128 }
1129 }
1130
Zhao Lei8d6738c2015-01-20 15:11:40 +08001131 if (!sblock_other) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001132 /*
1133 * did not find a mirror to fetch the page
1134 * from. scrub_write_page_to_dev_replace()
1135 * handles this case (page->io_error), by
1136 * filling the block with zeros before
1137 * submitting the write request
1138 */
Zhao Lei8d6738c2015-01-20 15:11:40 +08001139 sblock_other = sblock_bad;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001140 success = 0;
Zhao Lei8d6738c2015-01-20 15:11:40 +08001141 }
1142
1143 if (scrub_write_page_to_dev_replace(sblock_other,
1144 page_num) != 0) {
1145 btrfs_dev_replace_stats_inc(
1146 &sctx->dev_root->
1147 fs_info->dev_replace.
1148 num_write_errors);
1149 success = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001150 }
1151 }
1152
1153 goto out;
1154 }
1155
1156 /*
1157 * for regular scrub, repair those pages that are errored.
1158 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001159 * repaired, continue by picking good copies of those pages.
1160 * Select the good pages from mirrors to rewrite bad pages from
1161 * the area to fix. Afterwards verify the checksum of the block
1162 * that is supposed to be repaired. This verification step is
1163 * only done for the purpose of statistic counting and for the
1164 * final scrub report, whether errors remain.
1165 * A perfect algorithm could make use of the checksum and try
1166 * all possible combinations of pages from the different mirrors
1167 * until the checksum verification succeeds. For example, when
1168 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1169 * of mirror #2 is readable but the final checksum test fails,
1170 * then the 2nd page of mirror #3 could be tried, whether now
1171 * the final checksum succeedes. But this would be a rare
1172 * exception and is therefore not implemented. At least it is
1173 * avoided that the good copy is overwritten.
1174 * A more useful improvement would be to pick the sectors
1175 * without I/O error based on sector sizes (512 bytes on legacy
1176 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1177 * mirror could be repaired by taking 512 byte of a different
1178 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1179 * area are unreadable.
1180 */
1181
1182 /* can only fix I/O errors from here on */
1183 if (sblock_bad->no_io_error_seen)
1184 goto did_not_correct_error;
1185
1186 success = 1;
1187 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001188 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001189
1190 if (!page_bad->io_error)
1191 continue;
1192
1193 for (mirror_index = 0;
1194 mirror_index < BTRFS_MAX_MIRRORS &&
1195 sblocks_for_recheck[mirror_index].page_count > 0;
1196 mirror_index++) {
1197 struct scrub_block *sblock_other = sblocks_for_recheck +
1198 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001199 struct scrub_page *page_other = sblock_other->pagev[
1200 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001201
1202 if (!page_other->io_error) {
1203 ret = scrub_repair_page_from_good_copy(
1204 sblock_bad, sblock_other, page_num, 0);
1205 if (0 == ret) {
1206 page_bad->io_error = 0;
1207 break; /* succeeded for this page */
1208 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001209 }
1210 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001211
1212 if (page_bad->io_error) {
1213 /* did not find a mirror to copy the page from */
1214 success = 0;
1215 }
1216 }
1217
1218 if (success) {
1219 if (is_metadata || have_csum) {
1220 /*
1221 * need to verify the checksum now that all
1222 * sectors on disk are repaired (the write
1223 * request for data to be repaired is on its way).
1224 * Just be lazy and use scrub_recheck_block()
1225 * which re-reads the data before the checksum
1226 * is verified, but most likely the data comes out
1227 * of the page cache.
1228 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001229 scrub_recheck_block(fs_info, sblock_bad,
1230 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001231 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001232 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001233 !sblock_bad->checksum_error &&
1234 sblock_bad->no_io_error_seen)
1235 goto corrected_error;
1236 else
1237 goto did_not_correct_error;
1238 } else {
1239corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001240 spin_lock(&sctx->stat_lock);
1241 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001242 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001243 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001244 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001245 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001246 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001247 }
1248 } else {
1249did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001250 spin_lock(&sctx->stat_lock);
1251 sctx->stat.uncorrectable_errors++;
1252 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001253 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001254 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001255 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001256 }
1257
1258out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001259 if (sblocks_for_recheck) {
1260 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1261 mirror_index++) {
1262 struct scrub_block *sblock = sblocks_for_recheck +
1263 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001264 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001265 int page_index;
1266
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001267 for (page_index = 0; page_index < sblock->page_count;
1268 page_index++) {
1269 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001270 recover = sblock->pagev[page_index]->recover;
1271 if (recover) {
1272 scrub_put_recover(recover);
1273 sblock->pagev[page_index]->recover =
1274 NULL;
1275 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001276 scrub_page_put(sblock->pagev[page_index]);
1277 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001278 }
1279 kfree(sblocks_for_recheck);
1280 }
1281
1282 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001283}
1284
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001285static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001286{
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001287 if (bbio->raid_map) {
Zhao Leie34c3302015-01-20 15:11:31 +08001288 int real_stripes = bbio->num_stripes - bbio->num_tgtdevs;
1289
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001290 if (bbio->raid_map[real_stripes - 1] == RAID6_Q_STRIPE)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001291 return 3;
1292 else
1293 return 2;
1294 } else {
1295 return (int)bbio->num_stripes;
1296 }
1297}
1298
1299static inline void scrub_stripe_index_and_offset(u64 logical, u64 *raid_map,
1300 u64 mapped_length,
1301 int nstripes, int mirror,
1302 int *stripe_index,
1303 u64 *stripe_offset)
1304{
1305 int i;
1306
1307 if (raid_map) {
1308 /* RAID5/6 */
1309 for (i = 0; i < nstripes; i++) {
1310 if (raid_map[i] == RAID6_Q_STRIPE ||
1311 raid_map[i] == RAID5_P_STRIPE)
1312 continue;
1313
1314 if (logical >= raid_map[i] &&
1315 logical < raid_map[i] + mapped_length)
1316 break;
1317 }
1318
1319 *stripe_index = i;
1320 *stripe_offset = logical - raid_map[i];
1321 } else {
1322 /* The other RAID type */
1323 *stripe_index = mirror;
1324 *stripe_offset = 0;
1325 }
1326}
1327
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001328static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001329 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001330 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001331 u64 length, u64 logical,
1332 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001333{
Miao Xieaf8e2d12014-10-23 14:42:50 +08001334 struct scrub_recover *recover;
1335 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001336 u64 sublen;
1337 u64 mapped_length;
1338 u64 stripe_offset;
1339 int stripe_index;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001340 int page_index;
1341 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001342 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001343 int ret;
1344
1345 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001346 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001347 * are not used (and not set) in the blocks that are used for
1348 * the recheck procedure
1349 */
1350
1351 page_index = 0;
1352 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001353 sublen = min_t(u64, length, PAGE_SIZE);
1354 mapped_length = sublen;
1355 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001356
1357 /*
1358 * with a length of PAGE_SIZE, each returned stripe
1359 * represents one mirror
1360 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001361 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001362 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001363 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001364 btrfs_put_bbio(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001365 return -EIO;
1366 }
1367
Miao Xieaf8e2d12014-10-23 14:42:50 +08001368 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1369 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001370 btrfs_put_bbio(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001371 return -ENOMEM;
1372 }
1373
1374 atomic_set(&recover->refs, 1);
1375 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001376 recover->map_length = mapped_length;
1377
Stefan Behrensff023aa2012-11-06 11:43:11 +01001378 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001379
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001380 nmirrors = scrub_nr_raid_mirrors(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001381 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001382 mirror_index++) {
1383 struct scrub_block *sblock;
1384 struct scrub_page *page;
1385
1386 if (mirror_index >= BTRFS_MAX_MIRRORS)
Zhao Leidc5f7a32015-01-20 15:11:39 +08001387 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001388
1389 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001390 sblock->sctx = sctx;
1391 page = kzalloc(sizeof(*page), GFP_NOFS);
1392 if (!page) {
1393leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001394 spin_lock(&sctx->stat_lock);
1395 sctx->stat.malloc_errors++;
1396 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001397 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001398 return -ENOMEM;
1399 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001400 scrub_page_get(page);
1401 sblock->pagev[page_index] = page;
1402 page->logical = logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001403
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001404 scrub_stripe_index_and_offset(logical, bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001405 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001406 bbio->num_stripes -
1407 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001408 mirror_index,
1409 &stripe_index,
1410 &stripe_offset);
1411 page->physical = bbio->stripes[stripe_index].physical +
1412 stripe_offset;
1413 page->dev = bbio->stripes[stripe_index].dev;
1414
Stefan Behrensff023aa2012-11-06 11:43:11 +01001415 BUG_ON(page_index >= original_sblock->page_count);
1416 page->physical_for_dev_replace =
1417 original_sblock->pagev[page_index]->
1418 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001419 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001420 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001421 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001422 page->page = alloc_page(GFP_NOFS);
1423 if (!page->page)
1424 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001425
1426 scrub_get_recover(recover);
1427 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001428 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001429 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001430 length -= sublen;
1431 logical += sublen;
1432 page_index++;
1433 }
1434
1435 return 0;
1436}
1437
Miao Xieaf8e2d12014-10-23 14:42:50 +08001438struct scrub_bio_ret {
1439 struct completion event;
1440 int error;
1441};
1442
1443static void scrub_bio_wait_endio(struct bio *bio, int error)
1444{
1445 struct scrub_bio_ret *ret = bio->bi_private;
1446
1447 ret->error = error;
1448 complete(&ret->event);
1449}
1450
1451static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1452{
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001453 return page->recover && page->recover->bbio->raid_map;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001454}
1455
1456static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1457 struct bio *bio,
1458 struct scrub_page *page)
1459{
1460 struct scrub_bio_ret done;
1461 int ret;
1462
1463 init_completion(&done.event);
1464 done.error = 0;
1465 bio->bi_iter.bi_sector = page->logical >> 9;
1466 bio->bi_private = &done;
1467 bio->bi_end_io = scrub_bio_wait_endio;
1468
1469 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001470 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001471 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001472 if (ret)
1473 return ret;
1474
1475 wait_for_completion(&done.event);
1476 if (done.error)
1477 return -EIO;
1478
1479 return 0;
1480}
1481
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001482/*
1483 * this function will check the on disk data for checksum errors, header
1484 * errors and read I/O errors. If any I/O errors happen, the exact pages
1485 * which are errored are marked as being bad. The goal is to enable scrub
1486 * to take those pages that are not errored from all the mirrors so that
1487 * the pages that are errored in the just handled mirror can be repaired.
1488 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001489static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1490 struct scrub_block *sblock, int is_metadata,
1491 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001492 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001493{
1494 int page_num;
1495
1496 sblock->no_io_error_seen = 1;
1497 sblock->header_error = 0;
1498 sblock->checksum_error = 0;
1499
1500 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1501 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001502 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001503
Stefan Behrens442a4f62012-05-25 16:06:08 +02001504 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001505 page->io_error = 1;
1506 sblock->no_io_error_seen = 0;
1507 continue;
1508 }
1509
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001510 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001511 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001512 if (!bio) {
1513 page->io_error = 1;
1514 sblock->no_io_error_seen = 0;
1515 continue;
1516 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001517 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001518
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001519 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001520 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1521 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1522 sblock->no_io_error_seen = 0;
1523 } else {
1524 bio->bi_iter.bi_sector = page->physical >> 9;
1525
1526 if (btrfsic_submit_bio_wait(READ, bio))
1527 sblock->no_io_error_seen = 0;
1528 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001529
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001530 bio_put(bio);
1531 }
1532
1533 if (sblock->no_io_error_seen)
1534 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1535 have_csum, csum, generation,
1536 csum_size);
1537
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001538 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001539}
1540
Miao Xie17a9be22014-07-24 11:37:08 +08001541static inline int scrub_check_fsid(u8 fsid[],
1542 struct scrub_page *spage)
1543{
1544 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1545 int ret;
1546
1547 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1548 return !ret;
1549}
1550
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001551static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1552 struct scrub_block *sblock,
1553 int is_metadata, int have_csum,
1554 const u8 *csum, u64 generation,
1555 u16 csum_size)
1556{
1557 int page_num;
1558 u8 calculated_csum[BTRFS_CSUM_SIZE];
1559 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001560 void *mapped_buffer;
1561
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001562 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001563 if (is_metadata) {
1564 struct btrfs_header *h;
1565
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001566 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001567 h = (struct btrfs_header *)mapped_buffer;
1568
Qu Wenruo3cae2102013-07-16 11:19:18 +08001569 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001570 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001571 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001572 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001573 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001574 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001575 sblock->header_error = 1;
1576 sblock->generation_error = 1;
1577 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001578 csum = h->csum;
1579 } else {
1580 if (!have_csum)
1581 return;
1582
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001583 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001584 }
1585
1586 for (page_num = 0;;) {
1587 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001588 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001589 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1590 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1591 else
Liu Bob0496682013-03-14 14:57:45 +00001592 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001593
Linus Torvalds9613beb2012-03-30 12:44:29 -07001594 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001595 page_num++;
1596 if (page_num >= sblock->page_count)
1597 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001598 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001599
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001600 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001601 }
1602
1603 btrfs_csum_final(crc, calculated_csum);
1604 if (memcmp(calculated_csum, csum, csum_size))
1605 sblock->checksum_error = 1;
1606}
1607
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001608static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001609 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001610{
1611 int page_num;
1612 int ret = 0;
1613
1614 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1615 int ret_sub;
1616
1617 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1618 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001619 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001620 if (ret_sub)
1621 ret = ret_sub;
1622 }
1623
1624 return ret;
1625}
1626
1627static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1628 struct scrub_block *sblock_good,
1629 int page_num, int force_write)
1630{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001631 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1632 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001633
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001634 BUG_ON(page_bad->page == NULL);
1635 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001636 if (force_write || sblock_bad->header_error ||
1637 sblock_bad->checksum_error || page_bad->io_error) {
1638 struct bio *bio;
1639 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001640
Stefan Behrensff023aa2012-11-06 11:43:11 +01001641 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001642 printk_ratelimited(KERN_WARNING "BTRFS: "
1643 "scrub_repair_page_from_good_copy(bdev == NULL) "
1644 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001645 return -EIO;
1646 }
1647
Chris Mason9be33952013-05-17 18:30:14 -04001648 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001649 if (!bio)
1650 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001651 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001652 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001653
1654 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1655 if (PAGE_SIZE != ret) {
1656 bio_put(bio);
1657 return -EIO;
1658 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001659
Kent Overstreet33879d42013-11-23 22:33:32 -08001660 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001661 btrfs_dev_stat_inc_and_print(page_bad->dev,
1662 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001663 btrfs_dev_replace_stats_inc(
1664 &sblock_bad->sctx->dev_root->fs_info->
1665 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001666 bio_put(bio);
1667 return -EIO;
1668 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001669 bio_put(bio);
1670 }
1671
1672 return 0;
1673}
1674
Stefan Behrensff023aa2012-11-06 11:43:11 +01001675static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1676{
1677 int page_num;
1678
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001679 /*
1680 * This block is used for the check of the parity on the source device,
1681 * so the data needn't be written into the destination device.
1682 */
1683 if (sblock->sparity)
1684 return;
1685
Stefan Behrensff023aa2012-11-06 11:43:11 +01001686 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1687 int ret;
1688
1689 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1690 if (ret)
1691 btrfs_dev_replace_stats_inc(
1692 &sblock->sctx->dev_root->fs_info->dev_replace.
1693 num_write_errors);
1694 }
1695}
1696
1697static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1698 int page_num)
1699{
1700 struct scrub_page *spage = sblock->pagev[page_num];
1701
1702 BUG_ON(spage->page == NULL);
1703 if (spage->io_error) {
1704 void *mapped_buffer = kmap_atomic(spage->page);
1705
1706 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1707 flush_dcache_page(spage->page);
1708 kunmap_atomic(mapped_buffer);
1709 }
1710 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1711}
1712
1713static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1714 struct scrub_page *spage)
1715{
1716 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1717 struct scrub_bio *sbio;
1718 int ret;
1719
1720 mutex_lock(&wr_ctx->wr_lock);
1721again:
1722 if (!wr_ctx->wr_curr_bio) {
1723 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1724 GFP_NOFS);
1725 if (!wr_ctx->wr_curr_bio) {
1726 mutex_unlock(&wr_ctx->wr_lock);
1727 return -ENOMEM;
1728 }
1729 wr_ctx->wr_curr_bio->sctx = sctx;
1730 wr_ctx->wr_curr_bio->page_count = 0;
1731 }
1732 sbio = wr_ctx->wr_curr_bio;
1733 if (sbio->page_count == 0) {
1734 struct bio *bio;
1735
1736 sbio->physical = spage->physical_for_dev_replace;
1737 sbio->logical = spage->logical;
1738 sbio->dev = wr_ctx->tgtdev;
1739 bio = sbio->bio;
1740 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001741 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001742 if (!bio) {
1743 mutex_unlock(&wr_ctx->wr_lock);
1744 return -ENOMEM;
1745 }
1746 sbio->bio = bio;
1747 }
1748
1749 bio->bi_private = sbio;
1750 bio->bi_end_io = scrub_wr_bio_end_io;
1751 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001752 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001753 sbio->err = 0;
1754 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1755 spage->physical_for_dev_replace ||
1756 sbio->logical + sbio->page_count * PAGE_SIZE !=
1757 spage->logical) {
1758 scrub_wr_submit(sctx);
1759 goto again;
1760 }
1761
1762 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1763 if (ret != PAGE_SIZE) {
1764 if (sbio->page_count < 1) {
1765 bio_put(sbio->bio);
1766 sbio->bio = NULL;
1767 mutex_unlock(&wr_ctx->wr_lock);
1768 return -EIO;
1769 }
1770 scrub_wr_submit(sctx);
1771 goto again;
1772 }
1773
1774 sbio->pagev[sbio->page_count] = spage;
1775 scrub_page_get(spage);
1776 sbio->page_count++;
1777 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1778 scrub_wr_submit(sctx);
1779 mutex_unlock(&wr_ctx->wr_lock);
1780
1781 return 0;
1782}
1783
1784static void scrub_wr_submit(struct scrub_ctx *sctx)
1785{
1786 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1787 struct scrub_bio *sbio;
1788
1789 if (!wr_ctx->wr_curr_bio)
1790 return;
1791
1792 sbio = wr_ctx->wr_curr_bio;
1793 wr_ctx->wr_curr_bio = NULL;
1794 WARN_ON(!sbio->bio->bi_bdev);
1795 scrub_pending_bio_inc(sctx);
1796 /* process all writes in a single worker thread. Then the block layer
1797 * orders the requests before sending them to the driver which
1798 * doubled the write performance on spinning disks when measured
1799 * with Linux 3.5 */
1800 btrfsic_submit_bio(WRITE, sbio->bio);
1801}
1802
1803static void scrub_wr_bio_end_io(struct bio *bio, int err)
1804{
1805 struct scrub_bio *sbio = bio->bi_private;
1806 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1807
1808 sbio->err = err;
1809 sbio->bio = bio;
1810
Liu Bo9e0af232014-08-15 23:36:53 +08001811 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1812 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001813 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001814}
1815
1816static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1817{
1818 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1819 struct scrub_ctx *sctx = sbio->sctx;
1820 int i;
1821
1822 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1823 if (sbio->err) {
1824 struct btrfs_dev_replace *dev_replace =
1825 &sbio->sctx->dev_root->fs_info->dev_replace;
1826
1827 for (i = 0; i < sbio->page_count; i++) {
1828 struct scrub_page *spage = sbio->pagev[i];
1829
1830 spage->io_error = 1;
1831 btrfs_dev_replace_stats_inc(&dev_replace->
1832 num_write_errors);
1833 }
1834 }
1835
1836 for (i = 0; i < sbio->page_count; i++)
1837 scrub_page_put(sbio->pagev[i]);
1838
1839 bio_put(sbio->bio);
1840 kfree(sbio);
1841 scrub_pending_bio_dec(sctx);
1842}
1843
1844static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001845{
1846 u64 flags;
1847 int ret;
1848
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001849 WARN_ON(sblock->page_count < 1);
1850 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001851 ret = 0;
1852 if (flags & BTRFS_EXTENT_FLAG_DATA)
1853 ret = scrub_checksum_data(sblock);
1854 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1855 ret = scrub_checksum_tree_block(sblock);
1856 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1857 (void)scrub_checksum_super(sblock);
1858 else
1859 WARN_ON(1);
1860 if (ret)
1861 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001862
1863 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001864}
1865
1866static int scrub_checksum_data(struct scrub_block *sblock)
1867{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001868 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001869 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001870 u8 *on_disk_csum;
1871 struct page *page;
1872 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001873 u32 crc = ~(u32)0;
1874 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001875 u64 len;
1876 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001877
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001878 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001879 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001880 return 0;
1881
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001882 on_disk_csum = sblock->pagev[0]->csum;
1883 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001884 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001885
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001886 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001887 index = 0;
1888 for (;;) {
1889 u64 l = min_t(u64, len, PAGE_SIZE);
1890
Liu Bob0496682013-03-14 14:57:45 +00001891 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001892 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001893 len -= l;
1894 if (len == 0)
1895 break;
1896 index++;
1897 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001898 BUG_ON(!sblock->pagev[index]->page);
1899 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001900 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001901 }
1902
Arne Jansena2de7332011-03-08 14:14:00 +01001903 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001904 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001905 fail = 1;
1906
Arne Jansena2de7332011-03-08 14:14:00 +01001907 return fail;
1908}
1909
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001910static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001911{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001912 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001913 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001914 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001915 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001916 u8 calculated_csum[BTRFS_CSUM_SIZE];
1917 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1918 struct page *page;
1919 void *mapped_buffer;
1920 u64 mapped_size;
1921 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001922 u32 crc = ~(u32)0;
1923 int fail = 0;
1924 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001925 u64 len;
1926 int index;
1927
1928 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001929 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001930 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001931 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001932 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001933
1934 /*
1935 * we don't use the getter functions here, as we
1936 * a) don't have an extent buffer and
1937 * b) the page is already kmapped
1938 */
Arne Jansena2de7332011-03-08 14:14:00 +01001939
Qu Wenruo3cae2102013-07-16 11:19:18 +08001940 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001941 ++fail;
1942
Qu Wenruo3cae2102013-07-16 11:19:18 +08001943 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001944 ++fail;
1945
Miao Xie17a9be22014-07-24 11:37:08 +08001946 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001947 ++fail;
1948
1949 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1950 BTRFS_UUID_SIZE))
1951 ++fail;
1952
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001953 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001954 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1955 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1956 index = 0;
1957 for (;;) {
1958 u64 l = min_t(u64, len, mapped_size);
1959
Liu Bob0496682013-03-14 14:57:45 +00001960 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001961 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001962 len -= l;
1963 if (len == 0)
1964 break;
1965 index++;
1966 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001967 BUG_ON(!sblock->pagev[index]->page);
1968 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001969 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001970 mapped_size = PAGE_SIZE;
1971 p = mapped_buffer;
1972 }
1973
1974 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001975 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001976 ++crc_fail;
1977
Arne Jansena2de7332011-03-08 14:14:00 +01001978 return fail || crc_fail;
1979}
1980
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001981static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001982{
1983 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001984 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001985 u8 calculated_csum[BTRFS_CSUM_SIZE];
1986 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1987 struct page *page;
1988 void *mapped_buffer;
1989 u64 mapped_size;
1990 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001991 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001992 int fail_gen = 0;
1993 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001994 u64 len;
1995 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001996
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001997 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001998 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001999 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002000 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002001 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002002
Qu Wenruo3cae2102013-07-16 11:19:18 +08002003 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002004 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002005
Qu Wenruo3cae2102013-07-16 11:19:18 +08002006 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002007 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002008
Miao Xie17a9be22014-07-24 11:37:08 +08002009 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002010 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002011
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002012 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2013 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2014 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2015 index = 0;
2016 for (;;) {
2017 u64 l = min_t(u64, len, mapped_size);
2018
Liu Bob0496682013-03-14 14:57:45 +00002019 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002020 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002021 len -= l;
2022 if (len == 0)
2023 break;
2024 index++;
2025 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002026 BUG_ON(!sblock->pagev[index]->page);
2027 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002028 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002029 mapped_size = PAGE_SIZE;
2030 p = mapped_buffer;
2031 }
2032
2033 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002034 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002035 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002036
Stefan Behrens442a4f62012-05-25 16:06:08 +02002037 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002038 /*
2039 * if we find an error in a super block, we just report it.
2040 * They will get written with the next transaction commit
2041 * anyway
2042 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002043 spin_lock(&sctx->stat_lock);
2044 ++sctx->stat.super_errors;
2045 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002046 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002047 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002048 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2049 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002050 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002051 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002052 }
2053
Stefan Behrens442a4f62012-05-25 16:06:08 +02002054 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002055}
2056
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002057static void scrub_block_get(struct scrub_block *sblock)
2058{
2059 atomic_inc(&sblock->ref_count);
2060}
2061
2062static void scrub_block_put(struct scrub_block *sblock)
2063{
2064 if (atomic_dec_and_test(&sblock->ref_count)) {
2065 int i;
2066
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002067 if (sblock->sparity)
2068 scrub_parity_put(sblock->sparity);
2069
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002070 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002071 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002072 kfree(sblock);
2073 }
2074}
2075
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002076static void scrub_page_get(struct scrub_page *spage)
2077{
2078 atomic_inc(&spage->ref_count);
2079}
2080
2081static void scrub_page_put(struct scrub_page *spage)
2082{
2083 if (atomic_dec_and_test(&spage->ref_count)) {
2084 if (spage->page)
2085 __free_page(spage->page);
2086 kfree(spage);
2087 }
2088}
2089
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002090static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002091{
2092 struct scrub_bio *sbio;
2093
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002094 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002095 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002096
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002097 sbio = sctx->bios[sctx->curr];
2098 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002099 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002100
Stefan Behrensff023aa2012-11-06 11:43:11 +01002101 if (!sbio->bio->bi_bdev) {
2102 /*
2103 * this case should not happen. If btrfs_map_block() is
2104 * wrong, it could happen for dev-replace operations on
2105 * missing devices when no mirrors are available, but in
2106 * this case it should already fail the mount.
2107 * This case is handled correctly (but _very_ slowly).
2108 */
2109 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05002110 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01002111 bio_endio(sbio->bio, -EIO);
2112 } else {
2113 btrfsic_submit_bio(READ, sbio->bio);
2114 }
Arne Jansena2de7332011-03-08 14:14:00 +01002115}
2116
Stefan Behrensff023aa2012-11-06 11:43:11 +01002117static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2118 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002119{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002120 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002121 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002122 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002123
2124again:
2125 /*
2126 * grab a fresh bio or wait for one to become available
2127 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002128 while (sctx->curr == -1) {
2129 spin_lock(&sctx->list_lock);
2130 sctx->curr = sctx->first_free;
2131 if (sctx->curr != -1) {
2132 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2133 sctx->bios[sctx->curr]->next_free = -1;
2134 sctx->bios[sctx->curr]->page_count = 0;
2135 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002136 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002137 spin_unlock(&sctx->list_lock);
2138 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002139 }
2140 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002141 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002142 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002143 struct bio *bio;
2144
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002145 sbio->physical = spage->physical;
2146 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002147 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002148 bio = sbio->bio;
2149 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002150 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002151 if (!bio)
2152 return -ENOMEM;
2153 sbio->bio = bio;
2154 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002155
2156 bio->bi_private = sbio;
2157 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002158 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002159 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002160 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002161 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2162 spage->physical ||
2163 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002164 spage->logical ||
2165 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002166 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002167 goto again;
2168 }
2169
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002170 sbio->pagev[sbio->page_count] = spage;
2171 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2172 if (ret != PAGE_SIZE) {
2173 if (sbio->page_count < 1) {
2174 bio_put(sbio->bio);
2175 sbio->bio = NULL;
2176 return -EIO;
2177 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002178 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002179 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002180 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002181
Stefan Behrensff023aa2012-11-06 11:43:11 +01002182 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002183 atomic_inc(&sblock->outstanding_pages);
2184 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002185 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002186 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002187
2188 return 0;
2189}
2190
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002191static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002192 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002193 u64 gen, int mirror_num, u8 *csum, int force,
2194 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002195{
2196 struct scrub_block *sblock;
2197 int index;
2198
2199 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2200 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002201 spin_lock(&sctx->stat_lock);
2202 sctx->stat.malloc_errors++;
2203 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002204 return -ENOMEM;
2205 }
2206
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002207 /* one ref inside this function, plus one for each page added to
2208 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002209 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002210 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002211 sblock->no_io_error_seen = 1;
2212
2213 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002214 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002215 u64 l = min_t(u64, len, PAGE_SIZE);
2216
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002217 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2218 if (!spage) {
2219leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002220 spin_lock(&sctx->stat_lock);
2221 sctx->stat.malloc_errors++;
2222 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002223 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002224 return -ENOMEM;
2225 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002226 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2227 scrub_page_get(spage);
2228 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002229 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002230 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002231 spage->flags = flags;
2232 spage->generation = gen;
2233 spage->logical = logical;
2234 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002235 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002236 spage->mirror_num = mirror_num;
2237 if (csum) {
2238 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002239 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002240 } else {
2241 spage->have_csum = 0;
2242 }
2243 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002244 spage->page = alloc_page(GFP_NOFS);
2245 if (!spage->page)
2246 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002247 len -= l;
2248 logical += l;
2249 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002250 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002251 }
2252
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002253 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002254 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002255 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002256 int ret;
2257
Stefan Behrensff023aa2012-11-06 11:43:11 +01002258 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002259 if (ret) {
2260 scrub_block_put(sblock);
2261 return ret;
2262 }
2263 }
2264
2265 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002266 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002267
2268 /* last one frees, either here or in bio completion for last page */
2269 scrub_block_put(sblock);
2270 return 0;
2271}
2272
2273static void scrub_bio_end_io(struct bio *bio, int err)
2274{
2275 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002276 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002277
2278 sbio->err = err;
2279 sbio->bio = bio;
2280
Qu Wenruo0339ef22014-02-28 10:46:17 +08002281 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002282}
2283
2284static void scrub_bio_end_io_worker(struct btrfs_work *work)
2285{
2286 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002287 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002288 int i;
2289
Stefan Behrensff023aa2012-11-06 11:43:11 +01002290 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002291 if (sbio->err) {
2292 for (i = 0; i < sbio->page_count; i++) {
2293 struct scrub_page *spage = sbio->pagev[i];
2294
2295 spage->io_error = 1;
2296 spage->sblock->no_io_error_seen = 0;
2297 }
2298 }
2299
2300 /* now complete the scrub_block items that have all pages completed */
2301 for (i = 0; i < sbio->page_count; i++) {
2302 struct scrub_page *spage = sbio->pagev[i];
2303 struct scrub_block *sblock = spage->sblock;
2304
2305 if (atomic_dec_and_test(&sblock->outstanding_pages))
2306 scrub_block_complete(sblock);
2307 scrub_block_put(sblock);
2308 }
2309
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002310 bio_put(sbio->bio);
2311 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002312 spin_lock(&sctx->list_lock);
2313 sbio->next_free = sctx->first_free;
2314 sctx->first_free = sbio->index;
2315 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002316
2317 if (sctx->is_dev_replace &&
2318 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2319 mutex_lock(&sctx->wr_ctx.wr_lock);
2320 scrub_wr_submit(sctx);
2321 mutex_unlock(&sctx->wr_ctx.wr_lock);
2322 }
2323
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002324 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002325}
2326
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002327static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2328 unsigned long *bitmap,
2329 u64 start, u64 len)
2330{
2331 int offset;
2332 int nsectors;
2333 int sectorsize = sparity->sctx->dev_root->sectorsize;
2334
2335 if (len >= sparity->stripe_len) {
2336 bitmap_set(bitmap, 0, sparity->nsectors);
2337 return;
2338 }
2339
2340 start -= sparity->logic_start;
2341 offset = (int)do_div(start, sparity->stripe_len);
2342 offset /= sectorsize;
2343 nsectors = (int)len / sectorsize;
2344
2345 if (offset + nsectors <= sparity->nsectors) {
2346 bitmap_set(bitmap, offset, nsectors);
2347 return;
2348 }
2349
2350 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2351 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2352}
2353
2354static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2355 u64 start, u64 len)
2356{
2357 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2358}
2359
2360static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2361 u64 start, u64 len)
2362{
2363 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2364}
2365
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002366static void scrub_block_complete(struct scrub_block *sblock)
2367{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002368 int corrupted = 0;
2369
Stefan Behrensff023aa2012-11-06 11:43:11 +01002370 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002371 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002372 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002373 } else {
2374 /*
2375 * if has checksum error, write via repair mechanism in
2376 * dev replace case, otherwise write here in dev replace
2377 * case.
2378 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002379 corrupted = scrub_checksum(sblock);
2380 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002381 scrub_write_block_to_dev_replace(sblock);
2382 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002383
2384 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2385 u64 start = sblock->pagev[0]->logical;
2386 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2387 PAGE_SIZE;
2388
2389 scrub_parity_mark_sectors_error(sblock->sparity,
2390 start, end - start);
2391 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002392}
2393
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002394static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002395 u8 *csum)
2396{
2397 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002398 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002399 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002400
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002401 while (!list_empty(&sctx->csum_list)) {
2402 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002403 struct btrfs_ordered_sum, list);
2404 if (sum->bytenr > logical)
2405 return 0;
2406 if (sum->bytenr + sum->len > logical)
2407 break;
2408
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002409 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002410 list_del(&sum->list);
2411 kfree(sum);
2412 sum = NULL;
2413 }
2414 if (!sum)
2415 return 0;
2416
Miao Xief51a4a12013-06-19 10:36:09 +08002417 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002418 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002419 memcpy(csum, sum->sums + index, sctx->csum_size);
2420 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002421 list_del(&sum->list);
2422 kfree(sum);
2423 }
Miao Xief51a4a12013-06-19 10:36:09 +08002424 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002425}
2426
2427/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002428static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002429 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002430 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002431{
2432 int ret;
2433 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002434 u32 blocksize;
2435
2436 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002437 blocksize = sctx->sectorsize;
2438 spin_lock(&sctx->stat_lock);
2439 sctx->stat.data_extents_scrubbed++;
2440 sctx->stat.data_bytes_scrubbed += len;
2441 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002442 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002443 blocksize = sctx->nodesize;
2444 spin_lock(&sctx->stat_lock);
2445 sctx->stat.tree_extents_scrubbed++;
2446 sctx->stat.tree_bytes_scrubbed += len;
2447 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002448 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002449 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002450 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002451 }
Arne Jansena2de7332011-03-08 14:14:00 +01002452
2453 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002454 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002455 int have_csum = 0;
2456
2457 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2458 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002459 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002460 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002461 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002462 if (sctx->is_dev_replace && !have_csum) {
2463 ret = copy_nocow_pages(sctx, logical, l,
2464 mirror_num,
2465 physical_for_dev_replace);
2466 goto behind_scrub_pages;
2467 }
Arne Jansena2de7332011-03-08 14:14:00 +01002468 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002469 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002470 mirror_num, have_csum ? csum : NULL, 0,
2471 physical_for_dev_replace);
2472behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002473 if (ret)
2474 return ret;
2475 len -= l;
2476 logical += l;
2477 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002478 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002479 }
2480 return 0;
2481}
2482
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002483static int scrub_pages_for_parity(struct scrub_parity *sparity,
2484 u64 logical, u64 len,
2485 u64 physical, struct btrfs_device *dev,
2486 u64 flags, u64 gen, int mirror_num, u8 *csum)
2487{
2488 struct scrub_ctx *sctx = sparity->sctx;
2489 struct scrub_block *sblock;
2490 int index;
2491
2492 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2493 if (!sblock) {
2494 spin_lock(&sctx->stat_lock);
2495 sctx->stat.malloc_errors++;
2496 spin_unlock(&sctx->stat_lock);
2497 return -ENOMEM;
2498 }
2499
2500 /* one ref inside this function, plus one for each page added to
2501 * a bio later on */
2502 atomic_set(&sblock->ref_count, 1);
2503 sblock->sctx = sctx;
2504 sblock->no_io_error_seen = 1;
2505 sblock->sparity = sparity;
2506 scrub_parity_get(sparity);
2507
2508 for (index = 0; len > 0; index++) {
2509 struct scrub_page *spage;
2510 u64 l = min_t(u64, len, PAGE_SIZE);
2511
2512 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2513 if (!spage) {
2514leave_nomem:
2515 spin_lock(&sctx->stat_lock);
2516 sctx->stat.malloc_errors++;
2517 spin_unlock(&sctx->stat_lock);
2518 scrub_block_put(sblock);
2519 return -ENOMEM;
2520 }
2521 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2522 /* For scrub block */
2523 scrub_page_get(spage);
2524 sblock->pagev[index] = spage;
2525 /* For scrub parity */
2526 scrub_page_get(spage);
2527 list_add_tail(&spage->list, &sparity->spages);
2528 spage->sblock = sblock;
2529 spage->dev = dev;
2530 spage->flags = flags;
2531 spage->generation = gen;
2532 spage->logical = logical;
2533 spage->physical = physical;
2534 spage->mirror_num = mirror_num;
2535 if (csum) {
2536 spage->have_csum = 1;
2537 memcpy(spage->csum, csum, sctx->csum_size);
2538 } else {
2539 spage->have_csum = 0;
2540 }
2541 sblock->page_count++;
2542 spage->page = alloc_page(GFP_NOFS);
2543 if (!spage->page)
2544 goto leave_nomem;
2545 len -= l;
2546 logical += l;
2547 physical += l;
2548 }
2549
2550 WARN_ON(sblock->page_count == 0);
2551 for (index = 0; index < sblock->page_count; index++) {
2552 struct scrub_page *spage = sblock->pagev[index];
2553 int ret;
2554
2555 ret = scrub_add_page_to_rd_bio(sctx, spage);
2556 if (ret) {
2557 scrub_block_put(sblock);
2558 return ret;
2559 }
2560 }
2561
2562 /* last one frees, either here or in bio completion for last page */
2563 scrub_block_put(sblock);
2564 return 0;
2565}
2566
2567static int scrub_extent_for_parity(struct scrub_parity *sparity,
2568 u64 logical, u64 len,
2569 u64 physical, struct btrfs_device *dev,
2570 u64 flags, u64 gen, int mirror_num)
2571{
2572 struct scrub_ctx *sctx = sparity->sctx;
2573 int ret;
2574 u8 csum[BTRFS_CSUM_SIZE];
2575 u32 blocksize;
2576
2577 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2578 blocksize = sctx->sectorsize;
2579 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2580 blocksize = sctx->nodesize;
2581 } else {
2582 blocksize = sctx->sectorsize;
2583 WARN_ON(1);
2584 }
2585
2586 while (len) {
2587 u64 l = min_t(u64, len, blocksize);
2588 int have_csum = 0;
2589
2590 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2591 /* push csums to sbio */
2592 have_csum = scrub_find_csum(sctx, logical, l, csum);
2593 if (have_csum == 0)
2594 goto skip;
2595 }
2596 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2597 flags, gen, mirror_num,
2598 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002599 if (ret)
2600 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002601skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002602 len -= l;
2603 logical += l;
2604 physical += l;
2605 }
2606 return 0;
2607}
2608
Wang Shilong3b080b22014-04-01 18:01:43 +08002609/*
2610 * Given a physical address, this will calculate it's
2611 * logical offset. if this is a parity stripe, it will return
2612 * the most left data stripe's logical offset.
2613 *
2614 * return 0 if it is a data stripe, 1 means parity stripe.
2615 */
2616static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002617 struct map_lookup *map, u64 *offset,
2618 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002619{
2620 int i;
2621 int j = 0;
2622 u64 stripe_nr;
2623 u64 last_offset;
2624 int stripe_index;
2625 int rot;
2626
2627 last_offset = (physical - map->stripes[num].physical) *
2628 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002629 if (stripe_start)
2630 *stripe_start = last_offset;
2631
Wang Shilong3b080b22014-04-01 18:01:43 +08002632 *offset = last_offset;
2633 for (i = 0; i < nr_data_stripes(map); i++) {
2634 *offset = last_offset + i * map->stripe_len;
2635
2636 stripe_nr = *offset;
2637 do_div(stripe_nr, map->stripe_len);
2638 do_div(stripe_nr, nr_data_stripes(map));
2639
2640 /* Work out the disk rotation on this stripe-set */
2641 rot = do_div(stripe_nr, map->num_stripes);
2642 /* calculate which stripe this data locates */
2643 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002644 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002645 if (stripe_index == num)
2646 return 0;
2647 if (stripe_index < num)
2648 j++;
2649 }
2650 *offset = last_offset + j * map->stripe_len;
2651 return 1;
2652}
2653
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002654static void scrub_free_parity(struct scrub_parity *sparity)
2655{
2656 struct scrub_ctx *sctx = sparity->sctx;
2657 struct scrub_page *curr, *next;
2658 int nbits;
2659
2660 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2661 if (nbits) {
2662 spin_lock(&sctx->stat_lock);
2663 sctx->stat.read_errors += nbits;
2664 sctx->stat.uncorrectable_errors += nbits;
2665 spin_unlock(&sctx->stat_lock);
2666 }
2667
2668 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2669 list_del_init(&curr->list);
2670 scrub_page_put(curr);
2671 }
2672
2673 kfree(sparity);
2674}
2675
2676static void scrub_parity_bio_endio(struct bio *bio, int error)
2677{
2678 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2679 struct scrub_ctx *sctx = sparity->sctx;
2680
2681 if (error)
2682 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2683 sparity->nsectors);
2684
2685 scrub_free_parity(sparity);
2686 scrub_pending_bio_dec(sctx);
2687 bio_put(bio);
2688}
2689
2690static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2691{
2692 struct scrub_ctx *sctx = sparity->sctx;
2693 struct bio *bio;
2694 struct btrfs_raid_bio *rbio;
2695 struct scrub_page *spage;
2696 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002697 u64 length;
2698 int ret;
2699
2700 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2701 sparity->nsectors))
2702 goto out;
2703
2704 length = sparity->logic_end - sparity->logic_start + 1;
Miao Xie76035972014-11-14 17:45:42 +08002705 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002706 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002707 &length, &bbio, 0, 1);
2708 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002709 goto bbio_out;
2710
2711 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2712 if (!bio)
2713 goto bbio_out;
2714
2715 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2716 bio->bi_private = sparity;
2717 bio->bi_end_io = scrub_parity_bio_endio;
2718
2719 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002720 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002721 sparity->dbitmap,
2722 sparity->nsectors);
2723 if (!rbio)
2724 goto rbio_out;
2725
2726 list_for_each_entry(spage, &sparity->spages, list)
2727 raid56_parity_add_scrub_pages(rbio, spage->page,
2728 spage->logical);
2729
2730 scrub_pending_bio_inc(sctx);
2731 raid56_parity_submit_scrub_rbio(rbio);
2732 return;
2733
2734rbio_out:
2735 bio_put(bio);
2736bbio_out:
Zhao Lei6e9606d2015-01-20 15:11:34 +08002737 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002738 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2739 sparity->nsectors);
2740 spin_lock(&sctx->stat_lock);
2741 sctx->stat.malloc_errors++;
2742 spin_unlock(&sctx->stat_lock);
2743out:
2744 scrub_free_parity(sparity);
2745}
2746
2747static inline int scrub_calc_parity_bitmap_len(int nsectors)
2748{
2749 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2750}
2751
2752static void scrub_parity_get(struct scrub_parity *sparity)
2753{
2754 atomic_inc(&sparity->ref_count);
2755}
2756
2757static void scrub_parity_put(struct scrub_parity *sparity)
2758{
2759 if (!atomic_dec_and_test(&sparity->ref_count))
2760 return;
2761
2762 scrub_parity_check_and_repair(sparity);
2763}
2764
2765static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2766 struct map_lookup *map,
2767 struct btrfs_device *sdev,
2768 struct btrfs_path *path,
2769 u64 logic_start,
2770 u64 logic_end)
2771{
2772 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2773 struct btrfs_root *root = fs_info->extent_root;
2774 struct btrfs_root *csum_root = fs_info->csum_root;
2775 struct btrfs_extent_item *extent;
2776 u64 flags;
2777 int ret;
2778 int slot;
2779 struct extent_buffer *l;
2780 struct btrfs_key key;
2781 u64 generation;
2782 u64 extent_logical;
2783 u64 extent_physical;
2784 u64 extent_len;
2785 struct btrfs_device *extent_dev;
2786 struct scrub_parity *sparity;
2787 int nsectors;
2788 int bitmap_len;
2789 int extent_mirror_num;
2790 int stop_loop = 0;
2791
2792 nsectors = map->stripe_len / root->sectorsize;
2793 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2794 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2795 GFP_NOFS);
2796 if (!sparity) {
2797 spin_lock(&sctx->stat_lock);
2798 sctx->stat.malloc_errors++;
2799 spin_unlock(&sctx->stat_lock);
2800 return -ENOMEM;
2801 }
2802
2803 sparity->stripe_len = map->stripe_len;
2804 sparity->nsectors = nsectors;
2805 sparity->sctx = sctx;
2806 sparity->scrub_dev = sdev;
2807 sparity->logic_start = logic_start;
2808 sparity->logic_end = logic_end;
2809 atomic_set(&sparity->ref_count, 1);
2810 INIT_LIST_HEAD(&sparity->spages);
2811 sparity->dbitmap = sparity->bitmap;
2812 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2813
2814 ret = 0;
2815 while (logic_start < logic_end) {
2816 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2817 key.type = BTRFS_METADATA_ITEM_KEY;
2818 else
2819 key.type = BTRFS_EXTENT_ITEM_KEY;
2820 key.objectid = logic_start;
2821 key.offset = (u64)-1;
2822
2823 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2824 if (ret < 0)
2825 goto out;
2826
2827 if (ret > 0) {
2828 ret = btrfs_previous_extent_item(root, path, 0);
2829 if (ret < 0)
2830 goto out;
2831 if (ret > 0) {
2832 btrfs_release_path(path);
2833 ret = btrfs_search_slot(NULL, root, &key,
2834 path, 0, 0);
2835 if (ret < 0)
2836 goto out;
2837 }
2838 }
2839
2840 stop_loop = 0;
2841 while (1) {
2842 u64 bytes;
2843
2844 l = path->nodes[0];
2845 slot = path->slots[0];
2846 if (slot >= btrfs_header_nritems(l)) {
2847 ret = btrfs_next_leaf(root, path);
2848 if (ret == 0)
2849 continue;
2850 if (ret < 0)
2851 goto out;
2852
2853 stop_loop = 1;
2854 break;
2855 }
2856 btrfs_item_key_to_cpu(l, &key, slot);
2857
2858 if (key.type == BTRFS_METADATA_ITEM_KEY)
2859 bytes = root->nodesize;
2860 else
2861 bytes = key.offset;
2862
2863 if (key.objectid + bytes <= logic_start)
2864 goto next;
2865
2866 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2867 key.type != BTRFS_METADATA_ITEM_KEY)
2868 goto next;
2869
2870 if (key.objectid > logic_end) {
2871 stop_loop = 1;
2872 break;
2873 }
2874
2875 while (key.objectid >= logic_start + map->stripe_len)
2876 logic_start += map->stripe_len;
2877
2878 extent = btrfs_item_ptr(l, slot,
2879 struct btrfs_extent_item);
2880 flags = btrfs_extent_flags(l, extent);
2881 generation = btrfs_extent_generation(l, extent);
2882
2883 if (key.objectid < logic_start &&
2884 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2885 btrfs_err(fs_info,
2886 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2887 key.objectid, logic_start);
2888 goto next;
2889 }
2890again:
2891 extent_logical = key.objectid;
2892 extent_len = bytes;
2893
2894 if (extent_logical < logic_start) {
2895 extent_len -= logic_start - extent_logical;
2896 extent_logical = logic_start;
2897 }
2898
2899 if (extent_logical + extent_len >
2900 logic_start + map->stripe_len)
2901 extent_len = logic_start + map->stripe_len -
2902 extent_logical;
2903
2904 scrub_parity_mark_sectors_data(sparity, extent_logical,
2905 extent_len);
2906
2907 scrub_remap_extent(fs_info, extent_logical,
2908 extent_len, &extent_physical,
2909 &extent_dev,
2910 &extent_mirror_num);
2911
2912 ret = btrfs_lookup_csums_range(csum_root,
2913 extent_logical,
2914 extent_logical + extent_len - 1,
2915 &sctx->csum_list, 1);
2916 if (ret)
2917 goto out;
2918
2919 ret = scrub_extent_for_parity(sparity, extent_logical,
2920 extent_len,
2921 extent_physical,
2922 extent_dev, flags,
2923 generation,
2924 extent_mirror_num);
2925 if (ret)
2926 goto out;
2927
2928 scrub_free_csums(sctx);
2929 if (extent_logical + extent_len <
2930 key.objectid + bytes) {
2931 logic_start += map->stripe_len;
2932
2933 if (logic_start >= logic_end) {
2934 stop_loop = 1;
2935 break;
2936 }
2937
2938 if (logic_start < key.objectid + bytes) {
2939 cond_resched();
2940 goto again;
2941 }
2942 }
2943next:
2944 path->slots[0]++;
2945 }
2946
2947 btrfs_release_path(path);
2948
2949 if (stop_loop)
2950 break;
2951
2952 logic_start += map->stripe_len;
2953 }
2954out:
2955 if (ret < 0)
2956 scrub_parity_mark_sectors_error(sparity, logic_start,
2957 logic_end - logic_start + 1);
2958 scrub_parity_put(sparity);
2959 scrub_submit(sctx);
2960 mutex_lock(&sctx->wr_ctx.wr_lock);
2961 scrub_wr_submit(sctx);
2962 mutex_unlock(&sctx->wr_ctx.wr_lock);
2963
2964 btrfs_release_path(path);
2965 return ret < 0 ? ret : 0;
2966}
2967
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002968static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002969 struct map_lookup *map,
2970 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002971 int num, u64 base, u64 length,
2972 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002973{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002974 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002975 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002976 struct btrfs_root *root = fs_info->extent_root;
2977 struct btrfs_root *csum_root = fs_info->csum_root;
2978 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002979 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002980 u64 flags;
2981 int ret;
2982 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002983 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002984 struct extent_buffer *l;
2985 struct btrfs_key key;
2986 u64 physical;
2987 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002988 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08002989 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002990 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002991 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002992 struct reada_control *reada1;
2993 struct reada_control *reada2;
2994 struct btrfs_key key_start;
2995 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002996 u64 increment = map->stripe_len;
2997 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002998 u64 extent_logical;
2999 u64 extent_physical;
3000 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003001 u64 stripe_logical;
3002 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003003 struct btrfs_device *extent_dev;
3004 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003005 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003006
Arne Jansena2de7332011-03-08 14:14:00 +01003007 nstripes = length;
Wang Shilong3b080b22014-04-01 18:01:43 +08003008 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003009 offset = 0;
3010 do_div(nstripes, map->stripe_len);
3011 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3012 offset = map->stripe_len * num;
3013 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003014 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003015 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3016 int factor = map->num_stripes / map->sub_stripes;
3017 offset = map->stripe_len * (num / map->sub_stripes);
3018 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003019 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003020 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3021 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003022 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003023 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3024 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003025 mirror_num = num % map->num_stripes + 1;
Wang Shilong3b080b22014-04-01 18:01:43 +08003026 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3027 BTRFS_BLOCK_GROUP_RAID6)) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003028 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003029 increment = map->stripe_len * nr_data_stripes(map);
3030 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003031 } else {
3032 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003033 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003034 }
3035
3036 path = btrfs_alloc_path();
3037 if (!path)
3038 return -ENOMEM;
3039
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003040 ppath = btrfs_alloc_path();
3041 if (!ppath) {
3042 btrfs_free_path(ppath);
3043 return -ENOMEM;
3044 }
3045
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003046 /*
3047 * work on commit root. The related disk blocks are static as
3048 * long as COW is applied. This means, it is save to rewrite
3049 * them to repair disk errors without any race conditions
3050 */
Arne Jansena2de7332011-03-08 14:14:00 +01003051 path->search_commit_root = 1;
3052 path->skip_locking = 1;
3053
3054 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003055 * trigger the readahead for extent tree csum tree and wait for
3056 * completion. During readahead, the scrub is officially paused
3057 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003058 */
3059 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003060 physical_end = physical + nstripes * map->stripe_len;
3061 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3062 BTRFS_BLOCK_GROUP_RAID6)) {
3063 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003064 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003065 logic_end += base;
3066 } else {
3067 logic_end = logical + increment * nstripes;
3068 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003069 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003070 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003071 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003072
Arne Jansen7a262852011-06-10 12:39:23 +02003073 /* FIXME it might be better to start readahead at commit root */
3074 key_start.objectid = logical;
3075 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3076 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003077 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003078 key_end.type = BTRFS_METADATA_ITEM_KEY;
3079 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003080 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003081
Arne Jansen7a262852011-06-10 12:39:23 +02003082 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3083 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3084 key_start.offset = logical;
3085 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3086 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003087 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003088 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003089
Arne Jansen7a262852011-06-10 12:39:23 +02003090 if (!IS_ERR(reada1))
3091 btrfs_reada_wait(reada1);
3092 if (!IS_ERR(reada2))
3093 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003094
Arne Jansena2de7332011-03-08 14:14:00 +01003095
3096 /*
3097 * collect all data csums for the stripe to avoid seeking during
3098 * the scrub. This might currently (crc32) end up to be about 1MB
3099 */
Arne Jansene7786c32011-05-28 20:58:38 +00003100 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003101
Arne Jansena2de7332011-03-08 14:14:00 +01003102 /*
3103 * now find all extents for each stripe and scrub them
3104 */
Arne Jansena2de7332011-03-08 14:14:00 +01003105 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003106 while (physical < physical_end) {
3107 /* for raid56, we skip parity stripe */
3108 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3109 BTRFS_BLOCK_GROUP_RAID6)) {
3110 ret = get_raid56_logic_offset(physical, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003111 map, &logical, &stripe_logical);
Wang Shilong3b080b22014-04-01 18:01:43 +08003112 logical += base;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003113 if (ret) {
3114 stripe_logical += base;
3115 stripe_end = stripe_logical + increment - 1;
3116 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3117 ppath, stripe_logical,
3118 stripe_end);
3119 if (ret)
3120 goto out;
Wang Shilong3b080b22014-04-01 18:01:43 +08003121 goto skip;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003122 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003123 }
Arne Jansena2de7332011-03-08 14:14:00 +01003124 /*
3125 * canceled?
3126 */
3127 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003128 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003129 ret = -ECANCELED;
3130 goto out;
3131 }
3132 /*
3133 * check to see if we have to pause
3134 */
3135 if (atomic_read(&fs_info->scrub_pause_req)) {
3136 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003137 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003138 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003139 mutex_lock(&sctx->wr_ctx.wr_lock);
3140 scrub_wr_submit(sctx);
3141 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003142 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003143 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003144 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003145 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003146 }
3147
Wang Shilong7c76edb2014-01-12 21:38:32 +08003148 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3149 key.type = BTRFS_METADATA_ITEM_KEY;
3150 else
3151 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003152 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003153 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003154
3155 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3156 if (ret < 0)
3157 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003158
Arne Jansen8c510322011-06-03 10:09:26 +02003159 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003160 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003161 if (ret < 0)
3162 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003163 if (ret > 0) {
3164 /* there's no smaller item, so stick with the
3165 * larger one */
3166 btrfs_release_path(path);
3167 ret = btrfs_search_slot(NULL, root, &key,
3168 path, 0, 0);
3169 if (ret < 0)
3170 goto out;
3171 }
Arne Jansena2de7332011-03-08 14:14:00 +01003172 }
3173
Liu Bo625f1c8d2013-04-27 02:56:57 +00003174 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003175 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003176 u64 bytes;
3177
Arne Jansena2de7332011-03-08 14:14:00 +01003178 l = path->nodes[0];
3179 slot = path->slots[0];
3180 if (slot >= btrfs_header_nritems(l)) {
3181 ret = btrfs_next_leaf(root, path);
3182 if (ret == 0)
3183 continue;
3184 if (ret < 0)
3185 goto out;
3186
Liu Bo625f1c8d2013-04-27 02:56:57 +00003187 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003188 break;
3189 }
3190 btrfs_item_key_to_cpu(l, &key, slot);
3191
Josef Bacik3173a182013-03-07 14:22:04 -05003192 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003193 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003194 else
3195 bytes = key.offset;
3196
3197 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003198 goto next;
3199
Liu Bo625f1c8d2013-04-27 02:56:57 +00003200 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3201 key.type != BTRFS_METADATA_ITEM_KEY)
3202 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01003203
Liu Bo625f1c8d2013-04-27 02:56:57 +00003204 if (key.objectid >= logical + map->stripe_len) {
3205 /* out of this device extent */
3206 if (key.objectid >= logic_end)
3207 stop_loop = 1;
3208 break;
3209 }
Arne Jansena2de7332011-03-08 14:14:00 +01003210
3211 extent = btrfs_item_ptr(l, slot,
3212 struct btrfs_extent_item);
3213 flags = btrfs_extent_flags(l, extent);
3214 generation = btrfs_extent_generation(l, extent);
3215
3216 if (key.objectid < logical &&
3217 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003218 btrfs_err(fs_info,
3219 "scrub: tree block %llu spanning "
3220 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003221 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01003222 goto next;
3223 }
3224
Liu Bo625f1c8d2013-04-27 02:56:57 +00003225again:
3226 extent_logical = key.objectid;
3227 extent_len = bytes;
3228
Arne Jansena2de7332011-03-08 14:14:00 +01003229 /*
3230 * trim extent to this stripe
3231 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003232 if (extent_logical < logical) {
3233 extent_len -= logical - extent_logical;
3234 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003235 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003236 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003237 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003238 extent_len = logical + map->stripe_len -
3239 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003240 }
3241
Liu Bo625f1c8d2013-04-27 02:56:57 +00003242 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003243 extent_dev = scrub_dev;
3244 extent_mirror_num = mirror_num;
3245 if (is_dev_replace)
3246 scrub_remap_extent(fs_info, extent_logical,
3247 extent_len, &extent_physical,
3248 &extent_dev,
3249 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003250
3251 ret = btrfs_lookup_csums_range(csum_root, logical,
3252 logical + map->stripe_len - 1,
3253 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003254 if (ret)
3255 goto out;
3256
Liu Bo625f1c8d2013-04-27 02:56:57 +00003257 ret = scrub_extent(sctx, extent_logical, extent_len,
3258 extent_physical, extent_dev, flags,
3259 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003260 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003261 if (ret)
3262 goto out;
3263
Josef Bacikd88d46c2013-06-10 12:59:04 +00003264 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003265 if (extent_logical + extent_len <
3266 key.objectid + bytes) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003267 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3268 BTRFS_BLOCK_GROUP_RAID6)) {
3269 /*
3270 * loop until we find next data stripe
3271 * or we have finished all stripes.
3272 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003273loop:
3274 physical += map->stripe_len;
3275 ret = get_raid56_logic_offset(physical,
3276 num, map, &logical,
3277 &stripe_logical);
3278 logical += base;
3279
3280 if (ret && physical < physical_end) {
3281 stripe_logical += base;
3282 stripe_end = stripe_logical +
3283 increment - 1;
3284 ret = scrub_raid56_parity(sctx,
3285 map, scrub_dev, ppath,
3286 stripe_logical,
3287 stripe_end);
3288 if (ret)
3289 goto out;
3290 goto loop;
3291 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003292 } else {
3293 physical += map->stripe_len;
3294 logical += increment;
3295 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003296 if (logical < key.objectid + bytes) {
3297 cond_resched();
3298 goto again;
3299 }
3300
Wang Shilong3b080b22014-04-01 18:01:43 +08003301 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003302 stop_loop = 1;
3303 break;
3304 }
3305 }
Arne Jansena2de7332011-03-08 14:14:00 +01003306next:
3307 path->slots[0]++;
3308 }
Chris Mason71267332011-05-23 06:30:52 -04003309 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003310skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003311 logical += increment;
3312 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003313 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003314 if (stop_loop)
3315 sctx->stat.last_physical = map->stripes[num].physical +
3316 length;
3317 else
3318 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003319 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003320 if (stop_loop)
3321 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003322 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003323out:
Arne Jansena2de7332011-03-08 14:14:00 +01003324 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003325 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003326 mutex_lock(&sctx->wr_ctx.wr_lock);
3327 scrub_wr_submit(sctx);
3328 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003329
Arne Jansene7786c32011-05-28 20:58:38 +00003330 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003331 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003332 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003333 return ret < 0 ? ret : 0;
3334}
3335
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003336static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003337 struct btrfs_device *scrub_dev,
3338 u64 chunk_tree, u64 chunk_objectid,
3339 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003340 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003341{
3342 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003343 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003344 struct map_lookup *map;
3345 struct extent_map *em;
3346 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003347 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003348
3349 read_lock(&map_tree->map_tree.lock);
3350 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3351 read_unlock(&map_tree->map_tree.lock);
3352
3353 if (!em)
3354 return -EINVAL;
3355
3356 map = (struct map_lookup *)em->bdev;
3357 if (em->start != chunk_offset)
3358 goto out;
3359
3360 if (em->len < length)
3361 goto out;
3362
3363 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003364 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003365 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003366 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003367 chunk_offset, length,
3368 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003369 if (ret)
3370 goto out;
3371 }
3372 }
3373out:
3374 free_extent_map(em);
3375
3376 return ret;
3377}
3378
3379static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003380int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003381 struct btrfs_device *scrub_dev, u64 start, u64 end,
3382 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003383{
3384 struct btrfs_dev_extent *dev_extent = NULL;
3385 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003386 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003387 struct btrfs_fs_info *fs_info = root->fs_info;
3388 u64 length;
3389 u64 chunk_tree;
3390 u64 chunk_objectid;
3391 u64 chunk_offset;
3392 int ret;
3393 int slot;
3394 struct extent_buffer *l;
3395 struct btrfs_key key;
3396 struct btrfs_key found_key;
3397 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003398 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003399
3400 path = btrfs_alloc_path();
3401 if (!path)
3402 return -ENOMEM;
3403
3404 path->reada = 2;
3405 path->search_commit_root = 1;
3406 path->skip_locking = 1;
3407
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003408 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003409 key.offset = 0ull;
3410 key.type = BTRFS_DEV_EXTENT_KEY;
3411
Arne Jansena2de7332011-03-08 14:14:00 +01003412 while (1) {
3413 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3414 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003415 break;
3416 if (ret > 0) {
3417 if (path->slots[0] >=
3418 btrfs_header_nritems(path->nodes[0])) {
3419 ret = btrfs_next_leaf(root, path);
3420 if (ret)
3421 break;
3422 }
3423 }
Arne Jansena2de7332011-03-08 14:14:00 +01003424
3425 l = path->nodes[0];
3426 slot = path->slots[0];
3427
3428 btrfs_item_key_to_cpu(l, &found_key, slot);
3429
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003430 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003431 break;
3432
David Sterba962a2982014-06-04 18:41:45 +02003433 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003434 break;
3435
3436 if (found_key.offset >= end)
3437 break;
3438
3439 if (found_key.offset < key.offset)
3440 break;
3441
3442 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3443 length = btrfs_dev_extent_length(l, dev_extent);
3444
Qu Wenruoced96ed2014-06-19 10:42:51 +08003445 if (found_key.offset + length <= start)
3446 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003447
3448 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3449 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3450 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3451
3452 /*
3453 * get a reference on the corresponding block group to prevent
3454 * the chunk from going away while we scrub it
3455 */
3456 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003457
3458 /* some chunks are removed but not committed to disk yet,
3459 * continue scrubbing */
3460 if (!cache)
3461 goto skip;
3462
Stefan Behrensff023aa2012-11-06 11:43:11 +01003463 dev_replace->cursor_right = found_key.offset + length;
3464 dev_replace->cursor_left = found_key.offset;
3465 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003466 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003467 chunk_offset, length, found_key.offset,
3468 is_dev_replace);
3469
3470 /*
3471 * flush, submit all pending read and write bios, afterwards
3472 * wait for them.
3473 * Note that in the dev replace case, a read request causes
3474 * write requests that are submitted in the read completion
3475 * worker. Therefore in the current situation, it is required
3476 * that all write requests are flushed, so that all read and
3477 * write requests are really completed when bios_in_flight
3478 * changes to 0.
3479 */
3480 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3481 scrub_submit(sctx);
3482 mutex_lock(&sctx->wr_ctx.wr_lock);
3483 scrub_wr_submit(sctx);
3484 mutex_unlock(&sctx->wr_ctx.wr_lock);
3485
3486 wait_event(sctx->list_wait,
3487 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003488 atomic_inc(&fs_info->scrubs_paused);
3489 wake_up(&fs_info->scrub_pause_wait);
3490
3491 /*
3492 * must be called before we decrease @scrub_paused.
3493 * make sure we don't block transaction commit while
3494 * we are waiting pending workers finished.
3495 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003496 wait_event(sctx->list_wait,
3497 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003498 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3499
3500 mutex_lock(&fs_info->scrub_lock);
3501 __scrub_blocked_if_needed(fs_info);
3502 atomic_dec(&fs_info->scrubs_paused);
3503 mutex_unlock(&fs_info->scrub_lock);
3504 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003505
Arne Jansena2de7332011-03-08 14:14:00 +01003506 btrfs_put_block_group(cache);
3507 if (ret)
3508 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003509 if (is_dev_replace &&
3510 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003511 ret = -EIO;
3512 break;
3513 }
3514 if (sctx->stat.malloc_errors > 0) {
3515 ret = -ENOMEM;
3516 break;
3517 }
Arne Jansena2de7332011-03-08 14:14:00 +01003518
Ilya Dryomov539f3582013-10-07 13:42:57 +03003519 dev_replace->cursor_left = dev_replace->cursor_right;
3520 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003521skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003522 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003523 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003524 }
3525
Arne Jansena2de7332011-03-08 14:14:00 +01003526 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003527
3528 /*
3529 * ret can still be 1 from search_slot or next_leaf,
3530 * that's not an error
3531 */
3532 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003533}
3534
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003535static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3536 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003537{
3538 int i;
3539 u64 bytenr;
3540 u64 gen;
3541 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003542 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003543
Miao Xie87533c42013-01-29 10:14:48 +00003544 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003545 return -EIO;
3546
Miao Xie5f546062014-07-24 11:37:09 +08003547 /* Seed devices of a new filesystem has their own generation. */
3548 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3549 gen = scrub_dev->generation;
3550 else
3551 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003552
3553 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3554 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003555 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3556 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003557 break;
3558
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003559 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003560 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003561 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003562 if (ret)
3563 return ret;
3564 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003565 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003566
3567 return 0;
3568}
3569
3570/*
3571 * get a reference count on fs_info->scrub_workers. start worker if necessary
3572 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003573static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3574 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003575{
Josef Bacik0dc3b842011-11-18 14:37:27 -05003576 int ret = 0;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003577 int flags = WQ_FREEZABLE | WQ_UNBOUND;
3578 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003579
Arne Jansen632dd772011-06-10 12:07:07 +02003580 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003581 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003582 fs_info->scrub_workers =
3583 btrfs_alloc_workqueue("btrfs-scrub", flags,
3584 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003585 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003586 fs_info->scrub_workers =
3587 btrfs_alloc_workqueue("btrfs-scrub", flags,
3588 max_active, 4);
3589 if (!fs_info->scrub_workers) {
3590 ret = -ENOMEM;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003591 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003592 }
3593 fs_info->scrub_wr_completion_workers =
3594 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3595 max_active, 2);
3596 if (!fs_info->scrub_wr_completion_workers) {
3597 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003598 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003599 }
3600 fs_info->scrub_nocow_workers =
3601 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
3602 if (!fs_info->scrub_nocow_workers) {
3603 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003604 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003605 }
Arne Jansen632dd772011-06-10 12:07:07 +02003606 }
Arne Jansena2de7332011-03-08 14:14:00 +01003607 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003608out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05003609 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003610}
3611
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003612static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003613{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003614 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003615 btrfs_destroy_workqueue(fs_info->scrub_workers);
3616 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3617 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003618 }
Arne Jansena2de7332011-03-08 14:14:00 +01003619 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003620}
3621
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003622int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3623 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003624 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003625{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003626 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003627 int ret;
3628 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003629 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003630
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003631 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003632 return -EINVAL;
3633
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003634 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003635 /*
3636 * in this case scrub is unable to calculate the checksum
3637 * the way scrub is implemented. Do not handle this
3638 * situation at all because it won't ever happen.
3639 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003640 btrfs_err(fs_info,
3641 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003642 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003643 return -EINVAL;
3644 }
3645
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003646 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003647 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003648 btrfs_err(fs_info,
3649 "scrub: size assumption sectorsize != PAGE_SIZE "
3650 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003651 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003652 return -EINVAL;
3653 }
3654
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003655 if (fs_info->chunk_root->nodesize >
3656 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3657 fs_info->chunk_root->sectorsize >
3658 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3659 /*
3660 * would exhaust the array bounds of pagev member in
3661 * struct scrub_block
3662 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003663 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3664 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003665 fs_info->chunk_root->nodesize,
3666 SCRUB_MAX_PAGES_PER_BLOCK,
3667 fs_info->chunk_root->sectorsize,
3668 SCRUB_MAX_PAGES_PER_BLOCK);
3669 return -EINVAL;
3670 }
3671
Arne Jansena2de7332011-03-08 14:14:00 +01003672
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003673 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3674 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003675 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003676 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003677 return -ENODEV;
3678 }
Arne Jansena2de7332011-03-08 14:14:00 +01003679
Miao Xie5d68da32014-07-24 11:37:07 +08003680 if (!is_dev_replace && !readonly && !dev->writeable) {
3681 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3682 rcu_read_lock();
3683 name = rcu_dereference(dev->name);
3684 btrfs_err(fs_info, "scrub: device %s is not writable",
3685 name->str);
3686 rcu_read_unlock();
3687 return -EROFS;
3688 }
3689
Wang Shilong3b7a0162013-10-12 02:11:12 +08003690 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003691 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003692 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003693 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003694 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003695 }
3696
Stefan Behrens8dabb742012-11-06 13:15:27 +01003697 btrfs_dev_replace_lock(&fs_info->dev_replace);
3698 if (dev->scrub_device ||
3699 (!is_dev_replace &&
3700 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3701 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003702 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003703 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003704 return -EINPROGRESS;
3705 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003706 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003707
3708 ret = scrub_workers_get(fs_info, is_dev_replace);
3709 if (ret) {
3710 mutex_unlock(&fs_info->scrub_lock);
3711 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3712 return ret;
3713 }
3714
Stefan Behrens63a212a2012-11-05 18:29:28 +01003715 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003716 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003717 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003718 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3719 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003720 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003721 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003722 sctx->readonly = readonly;
3723 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003724 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003725
Wang Shilong3cb09292013-12-04 21:15:19 +08003726 /*
3727 * checking @scrub_pause_req here, we can avoid
3728 * race between committing transaction and scrubbing.
3729 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003730 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003731 atomic_inc(&fs_info->scrubs_running);
3732 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003733
Stefan Behrensff023aa2012-11-06 11:43:11 +01003734 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003735 /*
3736 * by holding device list mutex, we can
3737 * kick off writing super in log tree sync.
3738 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003739 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003740 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003741 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003742 }
Arne Jansena2de7332011-03-08 14:14:00 +01003743
3744 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003745 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3746 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003747
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003748 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003749 atomic_dec(&fs_info->scrubs_running);
3750 wake_up(&fs_info->scrub_pause_wait);
3751
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003752 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003753
Arne Jansena2de7332011-03-08 14:14:00 +01003754 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003755 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003756
3757 mutex_lock(&fs_info->scrub_lock);
3758 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003759 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003760 mutex_unlock(&fs_info->scrub_lock);
3761
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003762 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003763
3764 return ret;
3765}
3766
Jeff Mahoney143bede2012-03-01 14:56:26 +01003767void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003768{
3769 struct btrfs_fs_info *fs_info = root->fs_info;
3770
3771 mutex_lock(&fs_info->scrub_lock);
3772 atomic_inc(&fs_info->scrub_pause_req);
3773 while (atomic_read(&fs_info->scrubs_paused) !=
3774 atomic_read(&fs_info->scrubs_running)) {
3775 mutex_unlock(&fs_info->scrub_lock);
3776 wait_event(fs_info->scrub_pause_wait,
3777 atomic_read(&fs_info->scrubs_paused) ==
3778 atomic_read(&fs_info->scrubs_running));
3779 mutex_lock(&fs_info->scrub_lock);
3780 }
3781 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003782}
3783
Jeff Mahoney143bede2012-03-01 14:56:26 +01003784void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003785{
3786 struct btrfs_fs_info *fs_info = root->fs_info;
3787
3788 atomic_dec(&fs_info->scrub_pause_req);
3789 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003790}
3791
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003792int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003793{
Arne Jansena2de7332011-03-08 14:14:00 +01003794 mutex_lock(&fs_info->scrub_lock);
3795 if (!atomic_read(&fs_info->scrubs_running)) {
3796 mutex_unlock(&fs_info->scrub_lock);
3797 return -ENOTCONN;
3798 }
3799
3800 atomic_inc(&fs_info->scrub_cancel_req);
3801 while (atomic_read(&fs_info->scrubs_running)) {
3802 mutex_unlock(&fs_info->scrub_lock);
3803 wait_event(fs_info->scrub_pause_wait,
3804 atomic_read(&fs_info->scrubs_running) == 0);
3805 mutex_lock(&fs_info->scrub_lock);
3806 }
3807 atomic_dec(&fs_info->scrub_cancel_req);
3808 mutex_unlock(&fs_info->scrub_lock);
3809
3810 return 0;
3811}
3812
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003813int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3814 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003815{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003816 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003817
3818 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003819 sctx = dev->scrub_device;
3820 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003821 mutex_unlock(&fs_info->scrub_lock);
3822 return -ENOTCONN;
3823 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003824 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003825 while (dev->scrub_device) {
3826 mutex_unlock(&fs_info->scrub_lock);
3827 wait_event(fs_info->scrub_pause_wait,
3828 dev->scrub_device == NULL);
3829 mutex_lock(&fs_info->scrub_lock);
3830 }
3831 mutex_unlock(&fs_info->scrub_lock);
3832
3833 return 0;
3834}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003835
Arne Jansena2de7332011-03-08 14:14:00 +01003836int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3837 struct btrfs_scrub_progress *progress)
3838{
3839 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003840 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003841
3842 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003843 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003844 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003845 sctx = dev->scrub_device;
3846 if (sctx)
3847 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003848 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3849
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003850 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003851}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003852
3853static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3854 u64 extent_logical, u64 extent_len,
3855 u64 *extent_physical,
3856 struct btrfs_device **extent_dev,
3857 int *extent_mirror_num)
3858{
3859 u64 mapped_length;
3860 struct btrfs_bio *bbio = NULL;
3861 int ret;
3862
3863 mapped_length = extent_len;
3864 ret = btrfs_map_block(fs_info, READ, extent_logical,
3865 &mapped_length, &bbio, 0);
3866 if (ret || !bbio || mapped_length < extent_len ||
3867 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08003868 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003869 return;
3870 }
3871
3872 *extent_physical = bbio->stripes[0].physical;
3873 *extent_mirror_num = bbio->mirror_num;
3874 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08003875 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003876}
3877
3878static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3879 struct scrub_wr_ctx *wr_ctx,
3880 struct btrfs_fs_info *fs_info,
3881 struct btrfs_device *dev,
3882 int is_dev_replace)
3883{
3884 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3885
3886 mutex_init(&wr_ctx->wr_lock);
3887 wr_ctx->wr_curr_bio = NULL;
3888 if (!is_dev_replace)
3889 return 0;
3890
3891 WARN_ON(!dev->bdev);
3892 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3893 bio_get_nr_vecs(dev->bdev));
3894 wr_ctx->tgtdev = dev;
3895 atomic_set(&wr_ctx->flush_all_writes, 0);
3896 return 0;
3897}
3898
3899static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3900{
3901 mutex_lock(&wr_ctx->wr_lock);
3902 kfree(wr_ctx->wr_curr_bio);
3903 wr_ctx->wr_curr_bio = NULL;
3904 mutex_unlock(&wr_ctx->wr_lock);
3905}
3906
3907static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3908 int mirror_num, u64 physical_for_dev_replace)
3909{
3910 struct scrub_copy_nocow_ctx *nocow_ctx;
3911 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3912
3913 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3914 if (!nocow_ctx) {
3915 spin_lock(&sctx->stat_lock);
3916 sctx->stat.malloc_errors++;
3917 spin_unlock(&sctx->stat_lock);
3918 return -ENOMEM;
3919 }
3920
3921 scrub_pending_trans_workers_inc(sctx);
3922
3923 nocow_ctx->sctx = sctx;
3924 nocow_ctx->logical = logical;
3925 nocow_ctx->len = len;
3926 nocow_ctx->mirror_num = mirror_num;
3927 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08003928 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3929 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003930 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003931 btrfs_queue_work(fs_info->scrub_nocow_workers,
3932 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003933
3934 return 0;
3935}
3936
Josef Bacik652f25a2013-09-12 16:58:28 -04003937static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3938{
3939 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3940 struct scrub_nocow_inode *nocow_inode;
3941
3942 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3943 if (!nocow_inode)
3944 return -ENOMEM;
3945 nocow_inode->inum = inum;
3946 nocow_inode->offset = offset;
3947 nocow_inode->root = root;
3948 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3949 return 0;
3950}
3951
3952#define COPY_COMPLETE 1
3953
Stefan Behrensff023aa2012-11-06 11:43:11 +01003954static void copy_nocow_pages_worker(struct btrfs_work *work)
3955{
3956 struct scrub_copy_nocow_ctx *nocow_ctx =
3957 container_of(work, struct scrub_copy_nocow_ctx, work);
3958 struct scrub_ctx *sctx = nocow_ctx->sctx;
3959 u64 logical = nocow_ctx->logical;
3960 u64 len = nocow_ctx->len;
3961 int mirror_num = nocow_ctx->mirror_num;
3962 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3963 int ret;
3964 struct btrfs_trans_handle *trans = NULL;
3965 struct btrfs_fs_info *fs_info;
3966 struct btrfs_path *path;
3967 struct btrfs_root *root;
3968 int not_written = 0;
3969
3970 fs_info = sctx->dev_root->fs_info;
3971 root = fs_info->extent_root;
3972
3973 path = btrfs_alloc_path();
3974 if (!path) {
3975 spin_lock(&sctx->stat_lock);
3976 sctx->stat.malloc_errors++;
3977 spin_unlock(&sctx->stat_lock);
3978 not_written = 1;
3979 goto out;
3980 }
3981
3982 trans = btrfs_join_transaction(root);
3983 if (IS_ERR(trans)) {
3984 not_written = 1;
3985 goto out;
3986 }
3987
3988 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04003989 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003990 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003991 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
3992 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02003993 logical, physical_for_dev_replace, len, mirror_num,
3994 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003995 not_written = 1;
3996 goto out;
3997 }
3998
Josef Bacik652f25a2013-09-12 16:58:28 -04003999 btrfs_end_transaction(trans, root);
4000 trans = NULL;
4001 while (!list_empty(&nocow_ctx->inodes)) {
4002 struct scrub_nocow_inode *entry;
4003 entry = list_first_entry(&nocow_ctx->inodes,
4004 struct scrub_nocow_inode,
4005 list);
4006 list_del_init(&entry->list);
4007 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4008 entry->root, nocow_ctx);
4009 kfree(entry);
4010 if (ret == COPY_COMPLETE) {
4011 ret = 0;
4012 break;
4013 } else if (ret) {
4014 break;
4015 }
4016 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004017out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004018 while (!list_empty(&nocow_ctx->inodes)) {
4019 struct scrub_nocow_inode *entry;
4020 entry = list_first_entry(&nocow_ctx->inodes,
4021 struct scrub_nocow_inode,
4022 list);
4023 list_del_init(&entry->list);
4024 kfree(entry);
4025 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004026 if (trans && !IS_ERR(trans))
4027 btrfs_end_transaction(trans, root);
4028 if (not_written)
4029 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4030 num_uncorrectable_read_errors);
4031
4032 btrfs_free_path(path);
4033 kfree(nocow_ctx);
4034
4035 scrub_pending_trans_workers_dec(sctx);
4036}
4037
Gui Hecheng32159242014-11-10 15:36:08 +08004038static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4039 u64 logical)
4040{
4041 struct extent_state *cached_state = NULL;
4042 struct btrfs_ordered_extent *ordered;
4043 struct extent_io_tree *io_tree;
4044 struct extent_map *em;
4045 u64 lockstart = start, lockend = start + len - 1;
4046 int ret = 0;
4047
4048 io_tree = &BTRFS_I(inode)->io_tree;
4049
4050 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4051 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4052 if (ordered) {
4053 btrfs_put_ordered_extent(ordered);
4054 ret = 1;
4055 goto out_unlock;
4056 }
4057
4058 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4059 if (IS_ERR(em)) {
4060 ret = PTR_ERR(em);
4061 goto out_unlock;
4062 }
4063
4064 /*
4065 * This extent does not actually cover the logical extent anymore,
4066 * move on to the next inode.
4067 */
4068 if (em->block_start > logical ||
4069 em->block_start + em->block_len < logical + len) {
4070 free_extent_map(em);
4071 ret = 1;
4072 goto out_unlock;
4073 }
4074 free_extent_map(em);
4075
4076out_unlock:
4077 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4078 GFP_NOFS);
4079 return ret;
4080}
4081
Josef Bacik652f25a2013-09-12 16:58:28 -04004082static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4083 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004084{
Miao Xie826aa0a2013-06-27 18:50:59 +08004085 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004086 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004087 struct inode *inode;
4088 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004089 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004090 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004091 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004092 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004093 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004094 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004095 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004096 int ret = 0;
4097 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004098
4099 key.objectid = root;
4100 key.type = BTRFS_ROOT_ITEM_KEY;
4101 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004102
4103 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4104
Stefan Behrensff023aa2012-11-06 11:43:11 +01004105 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004106 if (IS_ERR(local_root)) {
4107 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004108 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004109 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004110
4111 key.type = BTRFS_INODE_ITEM_KEY;
4112 key.objectid = inum;
4113 key.offset = 0;
4114 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004115 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004116 if (IS_ERR(inode))
4117 return PTR_ERR(inode);
4118
Miao Xieedd14002013-06-27 18:51:00 +08004119 /* Avoid truncate/dio/punch hole.. */
4120 mutex_lock(&inode->i_mutex);
4121 inode_dio_wait(inode);
4122
Stefan Behrensff023aa2012-11-06 11:43:11 +01004123 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004124 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004125 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004126
Gui Hecheng32159242014-11-10 15:36:08 +08004127 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4128 if (ret) {
4129 ret = ret > 0 ? 0 : ret;
4130 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004131 }
4132
Stefan Behrensff023aa2012-11-06 11:43:11 +01004133 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004134 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004135again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004136 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4137 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004138 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004139 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004140 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004141 }
4142
4143 if (PageUptodate(page)) {
4144 if (PageDirty(page))
4145 goto next_page;
4146 } else {
4147 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004148 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004149 btrfs_get_extent,
4150 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004151 if (err) {
4152 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004153 goto next_page;
4154 }
Miao Xieedd14002013-06-27 18:51:00 +08004155
Miao Xie26b258912013-06-27 18:50:58 +08004156 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004157 /*
4158 * If the page has been remove from the page cache,
4159 * the data on it is meaningless, because it may be
4160 * old one, the new data may be written into the new
4161 * page in the page cache.
4162 */
4163 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004164 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004165 page_cache_release(page);
4166 goto again;
4167 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004168 if (!PageUptodate(page)) {
4169 ret = -EIO;
4170 goto next_page;
4171 }
4172 }
Gui Hecheng32159242014-11-10 15:36:08 +08004173
4174 ret = check_extent_to_block(inode, offset, len,
4175 nocow_ctx_logical);
4176 if (ret) {
4177 ret = ret > 0 ? 0 : ret;
4178 goto next_page;
4179 }
4180
Miao Xie826aa0a2013-06-27 18:50:59 +08004181 err = write_page_nocow(nocow_ctx->sctx,
4182 physical_for_dev_replace, page);
4183 if (err)
4184 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004185next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004186 unlock_page(page);
4187 page_cache_release(page);
4188
4189 if (ret)
4190 break;
4191
Stefan Behrensff023aa2012-11-06 11:43:11 +01004192 offset += PAGE_CACHE_SIZE;
4193 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004194 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004195 len -= PAGE_CACHE_SIZE;
4196 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004197 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004198out:
Miao Xieedd14002013-06-27 18:51:00 +08004199 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004200 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004201 return ret;
4202}
4203
4204static int write_page_nocow(struct scrub_ctx *sctx,
4205 u64 physical_for_dev_replace, struct page *page)
4206{
4207 struct bio *bio;
4208 struct btrfs_device *dev;
4209 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004210
4211 dev = sctx->wr_ctx.tgtdev;
4212 if (!dev)
4213 return -EIO;
4214 if (!dev->bdev) {
4215 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05004216 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004217 return -EIO;
4218 }
Chris Mason9be33952013-05-17 18:30:14 -04004219 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004220 if (!bio) {
4221 spin_lock(&sctx->stat_lock);
4222 sctx->stat.malloc_errors++;
4223 spin_unlock(&sctx->stat_lock);
4224 return -ENOMEM;
4225 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004226 bio->bi_iter.bi_size = 0;
4227 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004228 bio->bi_bdev = dev->bdev;
4229 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4230 if (ret != PAGE_CACHE_SIZE) {
4231leave_with_eio:
4232 bio_put(bio);
4233 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4234 return -EIO;
4235 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004236
Kent Overstreet33879d42013-11-23 22:33:32 -08004237 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004238 goto leave_with_eio;
4239
4240 bio_put(bio);
4241 return 0;
4242}