blob: f9e108bdfc45642cdbf58b70913818405dc050b0 [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++) {
1117 int sub_success;
1118
1119 sub_success = 0;
1120 for (mirror_index = 0;
1121 mirror_index < BTRFS_MAX_MIRRORS &&
1122 sblocks_for_recheck[mirror_index].page_count > 0;
1123 mirror_index++) {
1124 struct scrub_block *sblock_other =
1125 sblocks_for_recheck + mirror_index;
1126 struct scrub_page *page_other =
1127 sblock_other->pagev[page_num];
1128
1129 if (!page_other->io_error) {
1130 ret = scrub_write_page_to_dev_replace(
1131 sblock_other, page_num);
1132 if (ret == 0) {
1133 /* succeeded for this page */
1134 sub_success = 1;
1135 break;
1136 } else {
1137 btrfs_dev_replace_stats_inc(
1138 &sctx->dev_root->
1139 fs_info->dev_replace.
1140 num_write_errors);
1141 }
1142 }
1143 }
1144
1145 if (!sub_success) {
1146 /*
1147 * did not find a mirror to fetch the page
1148 * from. scrub_write_page_to_dev_replace()
1149 * handles this case (page->io_error), by
1150 * filling the block with zeros before
1151 * submitting the write request
1152 */
1153 success = 0;
1154 ret = scrub_write_page_to_dev_replace(
1155 sblock_bad, page_num);
1156 if (ret)
1157 btrfs_dev_replace_stats_inc(
1158 &sctx->dev_root->fs_info->
1159 dev_replace.num_write_errors);
1160 }
1161 }
1162
1163 goto out;
1164 }
1165
1166 /*
1167 * for regular scrub, repair those pages that are errored.
1168 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001169 * repaired, continue by picking good copies of those pages.
1170 * Select the good pages from mirrors to rewrite bad pages from
1171 * the area to fix. Afterwards verify the checksum of the block
1172 * that is supposed to be repaired. This verification step is
1173 * only done for the purpose of statistic counting and for the
1174 * final scrub report, whether errors remain.
1175 * A perfect algorithm could make use of the checksum and try
1176 * all possible combinations of pages from the different mirrors
1177 * until the checksum verification succeeds. For example, when
1178 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1179 * of mirror #2 is readable but the final checksum test fails,
1180 * then the 2nd page of mirror #3 could be tried, whether now
1181 * the final checksum succeedes. But this would be a rare
1182 * exception and is therefore not implemented. At least it is
1183 * avoided that the good copy is overwritten.
1184 * A more useful improvement would be to pick the sectors
1185 * without I/O error based on sector sizes (512 bytes on legacy
1186 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1187 * mirror could be repaired by taking 512 byte of a different
1188 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1189 * area are unreadable.
1190 */
1191
1192 /* can only fix I/O errors from here on */
1193 if (sblock_bad->no_io_error_seen)
1194 goto did_not_correct_error;
1195
1196 success = 1;
1197 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001198 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001199
1200 if (!page_bad->io_error)
1201 continue;
1202
1203 for (mirror_index = 0;
1204 mirror_index < BTRFS_MAX_MIRRORS &&
1205 sblocks_for_recheck[mirror_index].page_count > 0;
1206 mirror_index++) {
1207 struct scrub_block *sblock_other = sblocks_for_recheck +
1208 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001209 struct scrub_page *page_other = sblock_other->pagev[
1210 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001211
1212 if (!page_other->io_error) {
1213 ret = scrub_repair_page_from_good_copy(
1214 sblock_bad, sblock_other, page_num, 0);
1215 if (0 == ret) {
1216 page_bad->io_error = 0;
1217 break; /* succeeded for this page */
1218 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001219 }
1220 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001221
1222 if (page_bad->io_error) {
1223 /* did not find a mirror to copy the page from */
1224 success = 0;
1225 }
1226 }
1227
1228 if (success) {
1229 if (is_metadata || have_csum) {
1230 /*
1231 * need to verify the checksum now that all
1232 * sectors on disk are repaired (the write
1233 * request for data to be repaired is on its way).
1234 * Just be lazy and use scrub_recheck_block()
1235 * which re-reads the data before the checksum
1236 * is verified, but most likely the data comes out
1237 * of the page cache.
1238 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001239 scrub_recheck_block(fs_info, sblock_bad,
1240 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001241 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001242 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001243 !sblock_bad->checksum_error &&
1244 sblock_bad->no_io_error_seen)
1245 goto corrected_error;
1246 else
1247 goto did_not_correct_error;
1248 } else {
1249corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001250 spin_lock(&sctx->stat_lock);
1251 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001252 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001253 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001254 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001255 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001256 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001257 }
1258 } else {
1259did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001260 spin_lock(&sctx->stat_lock);
1261 sctx->stat.uncorrectable_errors++;
1262 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001263 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001264 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001265 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001266 }
1267
1268out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001269 if (sblocks_for_recheck) {
1270 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1271 mirror_index++) {
1272 struct scrub_block *sblock = sblocks_for_recheck +
1273 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001274 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001275 int page_index;
1276
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001277 for (page_index = 0; page_index < sblock->page_count;
1278 page_index++) {
1279 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001280 recover = sblock->pagev[page_index]->recover;
1281 if (recover) {
1282 scrub_put_recover(recover);
1283 sblock->pagev[page_index]->recover =
1284 NULL;
1285 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001286 scrub_page_put(sblock->pagev[page_index]);
1287 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001288 }
1289 kfree(sblocks_for_recheck);
1290 }
1291
1292 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001293}
1294
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001295static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001296{
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001297 if (bbio->raid_map) {
Zhao Leie34c3302015-01-20 15:11:31 +08001298 int real_stripes = bbio->num_stripes - bbio->num_tgtdevs;
1299
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001300 if (bbio->raid_map[real_stripes - 1] == RAID6_Q_STRIPE)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001301 return 3;
1302 else
1303 return 2;
1304 } else {
1305 return (int)bbio->num_stripes;
1306 }
1307}
1308
1309static inline void scrub_stripe_index_and_offset(u64 logical, u64 *raid_map,
1310 u64 mapped_length,
1311 int nstripes, int mirror,
1312 int *stripe_index,
1313 u64 *stripe_offset)
1314{
1315 int i;
1316
1317 if (raid_map) {
1318 /* RAID5/6 */
1319 for (i = 0; i < nstripes; i++) {
1320 if (raid_map[i] == RAID6_Q_STRIPE ||
1321 raid_map[i] == RAID5_P_STRIPE)
1322 continue;
1323
1324 if (logical >= raid_map[i] &&
1325 logical < raid_map[i] + mapped_length)
1326 break;
1327 }
1328
1329 *stripe_index = i;
1330 *stripe_offset = logical - raid_map[i];
1331 } else {
1332 /* The other RAID type */
1333 *stripe_index = mirror;
1334 *stripe_offset = 0;
1335 }
1336}
1337
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001338static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001339 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001340 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001341 u64 length, u64 logical,
1342 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001343{
Miao Xieaf8e2d12014-10-23 14:42:50 +08001344 struct scrub_recover *recover;
1345 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001346 u64 sublen;
1347 u64 mapped_length;
1348 u64 stripe_offset;
1349 int stripe_index;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001350 int page_index;
1351 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001352 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001353 int ret;
1354
1355 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001356 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001357 * are not used (and not set) in the blocks that are used for
1358 * the recheck procedure
1359 */
1360
1361 page_index = 0;
1362 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001363 sublen = min_t(u64, length, PAGE_SIZE);
1364 mapped_length = sublen;
1365 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001366
1367 /*
1368 * with a length of PAGE_SIZE, each returned stripe
1369 * represents one mirror
1370 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001371 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001372 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001373 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001374 btrfs_put_bbio(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001375 return -EIO;
1376 }
1377
Miao Xieaf8e2d12014-10-23 14:42:50 +08001378 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1379 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001380 btrfs_put_bbio(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001381 return -ENOMEM;
1382 }
1383
1384 atomic_set(&recover->refs, 1);
1385 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001386 recover->map_length = mapped_length;
1387
Stefan Behrensff023aa2012-11-06 11:43:11 +01001388 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001389
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001390 nmirrors = scrub_nr_raid_mirrors(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001391 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001392 mirror_index++) {
1393 struct scrub_block *sblock;
1394 struct scrub_page *page;
1395
1396 if (mirror_index >= BTRFS_MAX_MIRRORS)
1397 continue;
1398
1399 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001400 sblock->sctx = sctx;
1401 page = kzalloc(sizeof(*page), GFP_NOFS);
1402 if (!page) {
1403leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001404 spin_lock(&sctx->stat_lock);
1405 sctx->stat.malloc_errors++;
1406 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001407 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001408 return -ENOMEM;
1409 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001410 scrub_page_get(page);
1411 sblock->pagev[page_index] = page;
1412 page->logical = logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001413
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001414 scrub_stripe_index_and_offset(logical, bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001415 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001416 bbio->num_stripes -
1417 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001418 mirror_index,
1419 &stripe_index,
1420 &stripe_offset);
1421 page->physical = bbio->stripes[stripe_index].physical +
1422 stripe_offset;
1423 page->dev = bbio->stripes[stripe_index].dev;
1424
Stefan Behrensff023aa2012-11-06 11:43:11 +01001425 BUG_ON(page_index >= original_sblock->page_count);
1426 page->physical_for_dev_replace =
1427 original_sblock->pagev[page_index]->
1428 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001429 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001430 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001431 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001432 page->page = alloc_page(GFP_NOFS);
1433 if (!page->page)
1434 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001435
1436 scrub_get_recover(recover);
1437 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001438 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001439 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001440 length -= sublen;
1441 logical += sublen;
1442 page_index++;
1443 }
1444
1445 return 0;
1446}
1447
Miao Xieaf8e2d12014-10-23 14:42:50 +08001448struct scrub_bio_ret {
1449 struct completion event;
1450 int error;
1451};
1452
1453static void scrub_bio_wait_endio(struct bio *bio, int error)
1454{
1455 struct scrub_bio_ret *ret = bio->bi_private;
1456
1457 ret->error = error;
1458 complete(&ret->event);
1459}
1460
1461static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1462{
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001463 return page->recover && page->recover->bbio->raid_map;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001464}
1465
1466static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1467 struct bio *bio,
1468 struct scrub_page *page)
1469{
1470 struct scrub_bio_ret done;
1471 int ret;
1472
1473 init_completion(&done.event);
1474 done.error = 0;
1475 bio->bi_iter.bi_sector = page->logical >> 9;
1476 bio->bi_private = &done;
1477 bio->bi_end_io = scrub_bio_wait_endio;
1478
1479 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001480 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001481 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001482 if (ret)
1483 return ret;
1484
1485 wait_for_completion(&done.event);
1486 if (done.error)
1487 return -EIO;
1488
1489 return 0;
1490}
1491
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001492/*
1493 * this function will check the on disk data for checksum errors, header
1494 * errors and read I/O errors. If any I/O errors happen, the exact pages
1495 * which are errored are marked as being bad. The goal is to enable scrub
1496 * to take those pages that are not errored from all the mirrors so that
1497 * the pages that are errored in the just handled mirror can be repaired.
1498 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001499static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1500 struct scrub_block *sblock, int is_metadata,
1501 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001502 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001503{
1504 int page_num;
1505
1506 sblock->no_io_error_seen = 1;
1507 sblock->header_error = 0;
1508 sblock->checksum_error = 0;
1509
1510 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1511 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001512 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001513
Stefan Behrens442a4f62012-05-25 16:06:08 +02001514 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001515 page->io_error = 1;
1516 sblock->no_io_error_seen = 0;
1517 continue;
1518 }
1519
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001520 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001521 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001522 if (!bio) {
1523 page->io_error = 1;
1524 sblock->no_io_error_seen = 0;
1525 continue;
1526 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001527 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001528
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001529 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001530 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1531 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1532 sblock->no_io_error_seen = 0;
1533 } else {
1534 bio->bi_iter.bi_sector = page->physical >> 9;
1535
1536 if (btrfsic_submit_bio_wait(READ, bio))
1537 sblock->no_io_error_seen = 0;
1538 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001539
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001540 bio_put(bio);
1541 }
1542
1543 if (sblock->no_io_error_seen)
1544 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1545 have_csum, csum, generation,
1546 csum_size);
1547
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001548 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001549}
1550
Miao Xie17a9be22014-07-24 11:37:08 +08001551static inline int scrub_check_fsid(u8 fsid[],
1552 struct scrub_page *spage)
1553{
1554 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1555 int ret;
1556
1557 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1558 return !ret;
1559}
1560
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001561static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1562 struct scrub_block *sblock,
1563 int is_metadata, int have_csum,
1564 const u8 *csum, u64 generation,
1565 u16 csum_size)
1566{
1567 int page_num;
1568 u8 calculated_csum[BTRFS_CSUM_SIZE];
1569 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001570 void *mapped_buffer;
1571
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001572 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001573 if (is_metadata) {
1574 struct btrfs_header *h;
1575
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001576 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001577 h = (struct btrfs_header *)mapped_buffer;
1578
Qu Wenruo3cae2102013-07-16 11:19:18 +08001579 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001580 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001581 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001582 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001583 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001584 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001585 sblock->header_error = 1;
1586 sblock->generation_error = 1;
1587 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001588 csum = h->csum;
1589 } else {
1590 if (!have_csum)
1591 return;
1592
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001593 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001594 }
1595
1596 for (page_num = 0;;) {
1597 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001598 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001599 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1600 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1601 else
Liu Bob0496682013-03-14 14:57:45 +00001602 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001603
Linus Torvalds9613beb2012-03-30 12:44:29 -07001604 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001605 page_num++;
1606 if (page_num >= sblock->page_count)
1607 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001608 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001609
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001610 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001611 }
1612
1613 btrfs_csum_final(crc, calculated_csum);
1614 if (memcmp(calculated_csum, csum, csum_size))
1615 sblock->checksum_error = 1;
1616}
1617
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001618static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001619 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001620{
1621 int page_num;
1622 int ret = 0;
1623
1624 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1625 int ret_sub;
1626
1627 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1628 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001629 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001630 if (ret_sub)
1631 ret = ret_sub;
1632 }
1633
1634 return ret;
1635}
1636
1637static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1638 struct scrub_block *sblock_good,
1639 int page_num, int force_write)
1640{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001641 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1642 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001643
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001644 BUG_ON(page_bad->page == NULL);
1645 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001646 if (force_write || sblock_bad->header_error ||
1647 sblock_bad->checksum_error || page_bad->io_error) {
1648 struct bio *bio;
1649 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001650
Stefan Behrensff023aa2012-11-06 11:43:11 +01001651 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001652 printk_ratelimited(KERN_WARNING "BTRFS: "
1653 "scrub_repair_page_from_good_copy(bdev == NULL) "
1654 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001655 return -EIO;
1656 }
1657
Chris Mason9be33952013-05-17 18:30:14 -04001658 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001659 if (!bio)
1660 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001661 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001662 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001663
1664 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1665 if (PAGE_SIZE != ret) {
1666 bio_put(bio);
1667 return -EIO;
1668 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001669
Kent Overstreet33879d42013-11-23 22:33:32 -08001670 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001671 btrfs_dev_stat_inc_and_print(page_bad->dev,
1672 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001673 btrfs_dev_replace_stats_inc(
1674 &sblock_bad->sctx->dev_root->fs_info->
1675 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001676 bio_put(bio);
1677 return -EIO;
1678 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001679 bio_put(bio);
1680 }
1681
1682 return 0;
1683}
1684
Stefan Behrensff023aa2012-11-06 11:43:11 +01001685static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1686{
1687 int page_num;
1688
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001689 /*
1690 * This block is used for the check of the parity on the source device,
1691 * so the data needn't be written into the destination device.
1692 */
1693 if (sblock->sparity)
1694 return;
1695
Stefan Behrensff023aa2012-11-06 11:43:11 +01001696 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1697 int ret;
1698
1699 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1700 if (ret)
1701 btrfs_dev_replace_stats_inc(
1702 &sblock->sctx->dev_root->fs_info->dev_replace.
1703 num_write_errors);
1704 }
1705}
1706
1707static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1708 int page_num)
1709{
1710 struct scrub_page *spage = sblock->pagev[page_num];
1711
1712 BUG_ON(spage->page == NULL);
1713 if (spage->io_error) {
1714 void *mapped_buffer = kmap_atomic(spage->page);
1715
1716 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1717 flush_dcache_page(spage->page);
1718 kunmap_atomic(mapped_buffer);
1719 }
1720 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1721}
1722
1723static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1724 struct scrub_page *spage)
1725{
1726 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1727 struct scrub_bio *sbio;
1728 int ret;
1729
1730 mutex_lock(&wr_ctx->wr_lock);
1731again:
1732 if (!wr_ctx->wr_curr_bio) {
1733 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1734 GFP_NOFS);
1735 if (!wr_ctx->wr_curr_bio) {
1736 mutex_unlock(&wr_ctx->wr_lock);
1737 return -ENOMEM;
1738 }
1739 wr_ctx->wr_curr_bio->sctx = sctx;
1740 wr_ctx->wr_curr_bio->page_count = 0;
1741 }
1742 sbio = wr_ctx->wr_curr_bio;
1743 if (sbio->page_count == 0) {
1744 struct bio *bio;
1745
1746 sbio->physical = spage->physical_for_dev_replace;
1747 sbio->logical = spage->logical;
1748 sbio->dev = wr_ctx->tgtdev;
1749 bio = sbio->bio;
1750 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001751 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001752 if (!bio) {
1753 mutex_unlock(&wr_ctx->wr_lock);
1754 return -ENOMEM;
1755 }
1756 sbio->bio = bio;
1757 }
1758
1759 bio->bi_private = sbio;
1760 bio->bi_end_io = scrub_wr_bio_end_io;
1761 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001762 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001763 sbio->err = 0;
1764 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1765 spage->physical_for_dev_replace ||
1766 sbio->logical + sbio->page_count * PAGE_SIZE !=
1767 spage->logical) {
1768 scrub_wr_submit(sctx);
1769 goto again;
1770 }
1771
1772 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1773 if (ret != PAGE_SIZE) {
1774 if (sbio->page_count < 1) {
1775 bio_put(sbio->bio);
1776 sbio->bio = NULL;
1777 mutex_unlock(&wr_ctx->wr_lock);
1778 return -EIO;
1779 }
1780 scrub_wr_submit(sctx);
1781 goto again;
1782 }
1783
1784 sbio->pagev[sbio->page_count] = spage;
1785 scrub_page_get(spage);
1786 sbio->page_count++;
1787 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1788 scrub_wr_submit(sctx);
1789 mutex_unlock(&wr_ctx->wr_lock);
1790
1791 return 0;
1792}
1793
1794static void scrub_wr_submit(struct scrub_ctx *sctx)
1795{
1796 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1797 struct scrub_bio *sbio;
1798
1799 if (!wr_ctx->wr_curr_bio)
1800 return;
1801
1802 sbio = wr_ctx->wr_curr_bio;
1803 wr_ctx->wr_curr_bio = NULL;
1804 WARN_ON(!sbio->bio->bi_bdev);
1805 scrub_pending_bio_inc(sctx);
1806 /* process all writes in a single worker thread. Then the block layer
1807 * orders the requests before sending them to the driver which
1808 * doubled the write performance on spinning disks when measured
1809 * with Linux 3.5 */
1810 btrfsic_submit_bio(WRITE, sbio->bio);
1811}
1812
1813static void scrub_wr_bio_end_io(struct bio *bio, int err)
1814{
1815 struct scrub_bio *sbio = bio->bi_private;
1816 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1817
1818 sbio->err = err;
1819 sbio->bio = bio;
1820
Liu Bo9e0af232014-08-15 23:36:53 +08001821 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1822 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001823 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001824}
1825
1826static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1827{
1828 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1829 struct scrub_ctx *sctx = sbio->sctx;
1830 int i;
1831
1832 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1833 if (sbio->err) {
1834 struct btrfs_dev_replace *dev_replace =
1835 &sbio->sctx->dev_root->fs_info->dev_replace;
1836
1837 for (i = 0; i < sbio->page_count; i++) {
1838 struct scrub_page *spage = sbio->pagev[i];
1839
1840 spage->io_error = 1;
1841 btrfs_dev_replace_stats_inc(&dev_replace->
1842 num_write_errors);
1843 }
1844 }
1845
1846 for (i = 0; i < sbio->page_count; i++)
1847 scrub_page_put(sbio->pagev[i]);
1848
1849 bio_put(sbio->bio);
1850 kfree(sbio);
1851 scrub_pending_bio_dec(sctx);
1852}
1853
1854static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001855{
1856 u64 flags;
1857 int ret;
1858
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001859 WARN_ON(sblock->page_count < 1);
1860 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001861 ret = 0;
1862 if (flags & BTRFS_EXTENT_FLAG_DATA)
1863 ret = scrub_checksum_data(sblock);
1864 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1865 ret = scrub_checksum_tree_block(sblock);
1866 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1867 (void)scrub_checksum_super(sblock);
1868 else
1869 WARN_ON(1);
1870 if (ret)
1871 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001872
1873 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001874}
1875
1876static int scrub_checksum_data(struct scrub_block *sblock)
1877{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001878 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001879 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001880 u8 *on_disk_csum;
1881 struct page *page;
1882 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001883 u32 crc = ~(u32)0;
1884 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001885 u64 len;
1886 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001887
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001888 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001889 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001890 return 0;
1891
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001892 on_disk_csum = sblock->pagev[0]->csum;
1893 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001894 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001895
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001896 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001897 index = 0;
1898 for (;;) {
1899 u64 l = min_t(u64, len, PAGE_SIZE);
1900
Liu Bob0496682013-03-14 14:57:45 +00001901 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001902 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001903 len -= l;
1904 if (len == 0)
1905 break;
1906 index++;
1907 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001908 BUG_ON(!sblock->pagev[index]->page);
1909 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001910 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001911 }
1912
Arne Jansena2de7332011-03-08 14:14:00 +01001913 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001914 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001915 fail = 1;
1916
Arne Jansena2de7332011-03-08 14:14:00 +01001917 return fail;
1918}
1919
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001920static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001921{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001922 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001923 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001924 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001925 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001926 u8 calculated_csum[BTRFS_CSUM_SIZE];
1927 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1928 struct page *page;
1929 void *mapped_buffer;
1930 u64 mapped_size;
1931 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001932 u32 crc = ~(u32)0;
1933 int fail = 0;
1934 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001935 u64 len;
1936 int index;
1937
1938 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001939 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001940 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001941 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001942 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001943
1944 /*
1945 * we don't use the getter functions here, as we
1946 * a) don't have an extent buffer and
1947 * b) the page is already kmapped
1948 */
Arne Jansena2de7332011-03-08 14:14:00 +01001949
Qu Wenruo3cae2102013-07-16 11:19:18 +08001950 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001951 ++fail;
1952
Qu Wenruo3cae2102013-07-16 11:19:18 +08001953 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001954 ++fail;
1955
Miao Xie17a9be22014-07-24 11:37:08 +08001956 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001957 ++fail;
1958
1959 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1960 BTRFS_UUID_SIZE))
1961 ++fail;
1962
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001963 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001964 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1965 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1966 index = 0;
1967 for (;;) {
1968 u64 l = min_t(u64, len, mapped_size);
1969
Liu Bob0496682013-03-14 14:57:45 +00001970 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001971 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001972 len -= l;
1973 if (len == 0)
1974 break;
1975 index++;
1976 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001977 BUG_ON(!sblock->pagev[index]->page);
1978 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001979 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001980 mapped_size = PAGE_SIZE;
1981 p = mapped_buffer;
1982 }
1983
1984 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001985 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001986 ++crc_fail;
1987
Arne Jansena2de7332011-03-08 14:14:00 +01001988 return fail || crc_fail;
1989}
1990
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001991static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001992{
1993 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001994 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001995 u8 calculated_csum[BTRFS_CSUM_SIZE];
1996 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1997 struct page *page;
1998 void *mapped_buffer;
1999 u64 mapped_size;
2000 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002001 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002002 int fail_gen = 0;
2003 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002004 u64 len;
2005 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002006
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002007 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002008 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002009 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002010 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002011 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002012
Qu Wenruo3cae2102013-07-16 11:19:18 +08002013 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002014 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002015
Qu Wenruo3cae2102013-07-16 11:19:18 +08002016 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002017 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002018
Miao Xie17a9be22014-07-24 11:37:08 +08002019 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002020 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002021
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002022 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2023 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2024 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2025 index = 0;
2026 for (;;) {
2027 u64 l = min_t(u64, len, mapped_size);
2028
Liu Bob0496682013-03-14 14:57:45 +00002029 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002030 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002031 len -= l;
2032 if (len == 0)
2033 break;
2034 index++;
2035 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002036 BUG_ON(!sblock->pagev[index]->page);
2037 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002038 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002039 mapped_size = PAGE_SIZE;
2040 p = mapped_buffer;
2041 }
2042
2043 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002044 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002045 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002046
Stefan Behrens442a4f62012-05-25 16:06:08 +02002047 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002048 /*
2049 * if we find an error in a super block, we just report it.
2050 * They will get written with the next transaction commit
2051 * anyway
2052 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002053 spin_lock(&sctx->stat_lock);
2054 ++sctx->stat.super_errors;
2055 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002056 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002057 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002058 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2059 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002060 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002061 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002062 }
2063
Stefan Behrens442a4f62012-05-25 16:06:08 +02002064 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002065}
2066
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002067static void scrub_block_get(struct scrub_block *sblock)
2068{
2069 atomic_inc(&sblock->ref_count);
2070}
2071
2072static void scrub_block_put(struct scrub_block *sblock)
2073{
2074 if (atomic_dec_and_test(&sblock->ref_count)) {
2075 int i;
2076
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002077 if (sblock->sparity)
2078 scrub_parity_put(sblock->sparity);
2079
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002080 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002081 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002082 kfree(sblock);
2083 }
2084}
2085
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002086static void scrub_page_get(struct scrub_page *spage)
2087{
2088 atomic_inc(&spage->ref_count);
2089}
2090
2091static void scrub_page_put(struct scrub_page *spage)
2092{
2093 if (atomic_dec_and_test(&spage->ref_count)) {
2094 if (spage->page)
2095 __free_page(spage->page);
2096 kfree(spage);
2097 }
2098}
2099
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002100static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002101{
2102 struct scrub_bio *sbio;
2103
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002104 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002105 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002106
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002107 sbio = sctx->bios[sctx->curr];
2108 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002109 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002110
Stefan Behrensff023aa2012-11-06 11:43:11 +01002111 if (!sbio->bio->bi_bdev) {
2112 /*
2113 * this case should not happen. If btrfs_map_block() is
2114 * wrong, it could happen for dev-replace operations on
2115 * missing devices when no mirrors are available, but in
2116 * this case it should already fail the mount.
2117 * This case is handled correctly (but _very_ slowly).
2118 */
2119 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05002120 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01002121 bio_endio(sbio->bio, -EIO);
2122 } else {
2123 btrfsic_submit_bio(READ, sbio->bio);
2124 }
Arne Jansena2de7332011-03-08 14:14:00 +01002125}
2126
Stefan Behrensff023aa2012-11-06 11:43:11 +01002127static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2128 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002129{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002130 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002131 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002132 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002133
2134again:
2135 /*
2136 * grab a fresh bio or wait for one to become available
2137 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002138 while (sctx->curr == -1) {
2139 spin_lock(&sctx->list_lock);
2140 sctx->curr = sctx->first_free;
2141 if (sctx->curr != -1) {
2142 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2143 sctx->bios[sctx->curr]->next_free = -1;
2144 sctx->bios[sctx->curr]->page_count = 0;
2145 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002146 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002147 spin_unlock(&sctx->list_lock);
2148 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002149 }
2150 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002151 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002152 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002153 struct bio *bio;
2154
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002155 sbio->physical = spage->physical;
2156 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002157 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002158 bio = sbio->bio;
2159 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002160 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002161 if (!bio)
2162 return -ENOMEM;
2163 sbio->bio = bio;
2164 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002165
2166 bio->bi_private = sbio;
2167 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002168 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002169 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002170 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002171 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2172 spage->physical ||
2173 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002174 spage->logical ||
2175 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002176 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002177 goto again;
2178 }
2179
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002180 sbio->pagev[sbio->page_count] = spage;
2181 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2182 if (ret != PAGE_SIZE) {
2183 if (sbio->page_count < 1) {
2184 bio_put(sbio->bio);
2185 sbio->bio = NULL;
2186 return -EIO;
2187 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002188 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002189 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002190 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002191
Stefan Behrensff023aa2012-11-06 11:43:11 +01002192 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002193 atomic_inc(&sblock->outstanding_pages);
2194 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002195 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002196 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002197
2198 return 0;
2199}
2200
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002201static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002202 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002203 u64 gen, int mirror_num, u8 *csum, int force,
2204 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002205{
2206 struct scrub_block *sblock;
2207 int index;
2208
2209 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2210 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002211 spin_lock(&sctx->stat_lock);
2212 sctx->stat.malloc_errors++;
2213 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002214 return -ENOMEM;
2215 }
2216
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002217 /* one ref inside this function, plus one for each page added to
2218 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002219 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002220 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002221 sblock->no_io_error_seen = 1;
2222
2223 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002224 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002225 u64 l = min_t(u64, len, PAGE_SIZE);
2226
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002227 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2228 if (!spage) {
2229leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002230 spin_lock(&sctx->stat_lock);
2231 sctx->stat.malloc_errors++;
2232 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002233 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002234 return -ENOMEM;
2235 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002236 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2237 scrub_page_get(spage);
2238 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002239 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002240 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002241 spage->flags = flags;
2242 spage->generation = gen;
2243 spage->logical = logical;
2244 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002245 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002246 spage->mirror_num = mirror_num;
2247 if (csum) {
2248 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002249 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002250 } else {
2251 spage->have_csum = 0;
2252 }
2253 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002254 spage->page = alloc_page(GFP_NOFS);
2255 if (!spage->page)
2256 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002257 len -= l;
2258 logical += l;
2259 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002260 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002261 }
2262
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002263 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002264 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002265 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002266 int ret;
2267
Stefan Behrensff023aa2012-11-06 11:43:11 +01002268 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002269 if (ret) {
2270 scrub_block_put(sblock);
2271 return ret;
2272 }
2273 }
2274
2275 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002276 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002277
2278 /* last one frees, either here or in bio completion for last page */
2279 scrub_block_put(sblock);
2280 return 0;
2281}
2282
2283static void scrub_bio_end_io(struct bio *bio, int err)
2284{
2285 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002286 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002287
2288 sbio->err = err;
2289 sbio->bio = bio;
2290
Qu Wenruo0339ef22014-02-28 10:46:17 +08002291 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002292}
2293
2294static void scrub_bio_end_io_worker(struct btrfs_work *work)
2295{
2296 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002297 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002298 int i;
2299
Stefan Behrensff023aa2012-11-06 11:43:11 +01002300 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002301 if (sbio->err) {
2302 for (i = 0; i < sbio->page_count; i++) {
2303 struct scrub_page *spage = sbio->pagev[i];
2304
2305 spage->io_error = 1;
2306 spage->sblock->no_io_error_seen = 0;
2307 }
2308 }
2309
2310 /* now complete the scrub_block items that have all pages completed */
2311 for (i = 0; i < sbio->page_count; i++) {
2312 struct scrub_page *spage = sbio->pagev[i];
2313 struct scrub_block *sblock = spage->sblock;
2314
2315 if (atomic_dec_and_test(&sblock->outstanding_pages))
2316 scrub_block_complete(sblock);
2317 scrub_block_put(sblock);
2318 }
2319
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002320 bio_put(sbio->bio);
2321 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002322 spin_lock(&sctx->list_lock);
2323 sbio->next_free = sctx->first_free;
2324 sctx->first_free = sbio->index;
2325 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002326
2327 if (sctx->is_dev_replace &&
2328 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2329 mutex_lock(&sctx->wr_ctx.wr_lock);
2330 scrub_wr_submit(sctx);
2331 mutex_unlock(&sctx->wr_ctx.wr_lock);
2332 }
2333
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002334 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002335}
2336
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002337static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2338 unsigned long *bitmap,
2339 u64 start, u64 len)
2340{
2341 int offset;
2342 int nsectors;
2343 int sectorsize = sparity->sctx->dev_root->sectorsize;
2344
2345 if (len >= sparity->stripe_len) {
2346 bitmap_set(bitmap, 0, sparity->nsectors);
2347 return;
2348 }
2349
2350 start -= sparity->logic_start;
2351 offset = (int)do_div(start, sparity->stripe_len);
2352 offset /= sectorsize;
2353 nsectors = (int)len / sectorsize;
2354
2355 if (offset + nsectors <= sparity->nsectors) {
2356 bitmap_set(bitmap, offset, nsectors);
2357 return;
2358 }
2359
2360 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2361 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2362}
2363
2364static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2365 u64 start, u64 len)
2366{
2367 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2368}
2369
2370static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2371 u64 start, u64 len)
2372{
2373 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2374}
2375
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002376static void scrub_block_complete(struct scrub_block *sblock)
2377{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002378 int corrupted = 0;
2379
Stefan Behrensff023aa2012-11-06 11:43:11 +01002380 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002381 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002382 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002383 } else {
2384 /*
2385 * if has checksum error, write via repair mechanism in
2386 * dev replace case, otherwise write here in dev replace
2387 * case.
2388 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002389 corrupted = scrub_checksum(sblock);
2390 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002391 scrub_write_block_to_dev_replace(sblock);
2392 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002393
2394 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2395 u64 start = sblock->pagev[0]->logical;
2396 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2397 PAGE_SIZE;
2398
2399 scrub_parity_mark_sectors_error(sblock->sparity,
2400 start, end - start);
2401 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002402}
2403
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002404static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002405 u8 *csum)
2406{
2407 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002408 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002409 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002410
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002411 while (!list_empty(&sctx->csum_list)) {
2412 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002413 struct btrfs_ordered_sum, list);
2414 if (sum->bytenr > logical)
2415 return 0;
2416 if (sum->bytenr + sum->len > logical)
2417 break;
2418
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002419 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002420 list_del(&sum->list);
2421 kfree(sum);
2422 sum = NULL;
2423 }
2424 if (!sum)
2425 return 0;
2426
Miao Xief51a4a12013-06-19 10:36:09 +08002427 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002428 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002429 memcpy(csum, sum->sums + index, sctx->csum_size);
2430 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002431 list_del(&sum->list);
2432 kfree(sum);
2433 }
Miao Xief51a4a12013-06-19 10:36:09 +08002434 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002435}
2436
2437/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002438static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002439 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002440 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002441{
2442 int ret;
2443 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002444 u32 blocksize;
2445
2446 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002447 blocksize = sctx->sectorsize;
2448 spin_lock(&sctx->stat_lock);
2449 sctx->stat.data_extents_scrubbed++;
2450 sctx->stat.data_bytes_scrubbed += len;
2451 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002452 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002453 blocksize = sctx->nodesize;
2454 spin_lock(&sctx->stat_lock);
2455 sctx->stat.tree_extents_scrubbed++;
2456 sctx->stat.tree_bytes_scrubbed += len;
2457 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002458 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002459 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002460 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002461 }
Arne Jansena2de7332011-03-08 14:14:00 +01002462
2463 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002464 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002465 int have_csum = 0;
2466
2467 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2468 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002469 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002470 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002471 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002472 if (sctx->is_dev_replace && !have_csum) {
2473 ret = copy_nocow_pages(sctx, logical, l,
2474 mirror_num,
2475 physical_for_dev_replace);
2476 goto behind_scrub_pages;
2477 }
Arne Jansena2de7332011-03-08 14:14:00 +01002478 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002479 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002480 mirror_num, have_csum ? csum : NULL, 0,
2481 physical_for_dev_replace);
2482behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002483 if (ret)
2484 return ret;
2485 len -= l;
2486 logical += l;
2487 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002488 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002489 }
2490 return 0;
2491}
2492
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002493static int scrub_pages_for_parity(struct scrub_parity *sparity,
2494 u64 logical, u64 len,
2495 u64 physical, struct btrfs_device *dev,
2496 u64 flags, u64 gen, int mirror_num, u8 *csum)
2497{
2498 struct scrub_ctx *sctx = sparity->sctx;
2499 struct scrub_block *sblock;
2500 int index;
2501
2502 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2503 if (!sblock) {
2504 spin_lock(&sctx->stat_lock);
2505 sctx->stat.malloc_errors++;
2506 spin_unlock(&sctx->stat_lock);
2507 return -ENOMEM;
2508 }
2509
2510 /* one ref inside this function, plus one for each page added to
2511 * a bio later on */
2512 atomic_set(&sblock->ref_count, 1);
2513 sblock->sctx = sctx;
2514 sblock->no_io_error_seen = 1;
2515 sblock->sparity = sparity;
2516 scrub_parity_get(sparity);
2517
2518 for (index = 0; len > 0; index++) {
2519 struct scrub_page *spage;
2520 u64 l = min_t(u64, len, PAGE_SIZE);
2521
2522 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2523 if (!spage) {
2524leave_nomem:
2525 spin_lock(&sctx->stat_lock);
2526 sctx->stat.malloc_errors++;
2527 spin_unlock(&sctx->stat_lock);
2528 scrub_block_put(sblock);
2529 return -ENOMEM;
2530 }
2531 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2532 /* For scrub block */
2533 scrub_page_get(spage);
2534 sblock->pagev[index] = spage;
2535 /* For scrub parity */
2536 scrub_page_get(spage);
2537 list_add_tail(&spage->list, &sparity->spages);
2538 spage->sblock = sblock;
2539 spage->dev = dev;
2540 spage->flags = flags;
2541 spage->generation = gen;
2542 spage->logical = logical;
2543 spage->physical = physical;
2544 spage->mirror_num = mirror_num;
2545 if (csum) {
2546 spage->have_csum = 1;
2547 memcpy(spage->csum, csum, sctx->csum_size);
2548 } else {
2549 spage->have_csum = 0;
2550 }
2551 sblock->page_count++;
2552 spage->page = alloc_page(GFP_NOFS);
2553 if (!spage->page)
2554 goto leave_nomem;
2555 len -= l;
2556 logical += l;
2557 physical += l;
2558 }
2559
2560 WARN_ON(sblock->page_count == 0);
2561 for (index = 0; index < sblock->page_count; index++) {
2562 struct scrub_page *spage = sblock->pagev[index];
2563 int ret;
2564
2565 ret = scrub_add_page_to_rd_bio(sctx, spage);
2566 if (ret) {
2567 scrub_block_put(sblock);
2568 return ret;
2569 }
2570 }
2571
2572 /* last one frees, either here or in bio completion for last page */
2573 scrub_block_put(sblock);
2574 return 0;
2575}
2576
2577static int scrub_extent_for_parity(struct scrub_parity *sparity,
2578 u64 logical, u64 len,
2579 u64 physical, struct btrfs_device *dev,
2580 u64 flags, u64 gen, int mirror_num)
2581{
2582 struct scrub_ctx *sctx = sparity->sctx;
2583 int ret;
2584 u8 csum[BTRFS_CSUM_SIZE];
2585 u32 blocksize;
2586
2587 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2588 blocksize = sctx->sectorsize;
2589 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2590 blocksize = sctx->nodesize;
2591 } else {
2592 blocksize = sctx->sectorsize;
2593 WARN_ON(1);
2594 }
2595
2596 while (len) {
2597 u64 l = min_t(u64, len, blocksize);
2598 int have_csum = 0;
2599
2600 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2601 /* push csums to sbio */
2602 have_csum = scrub_find_csum(sctx, logical, l, csum);
2603 if (have_csum == 0)
2604 goto skip;
2605 }
2606 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2607 flags, gen, mirror_num,
2608 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002609 if (ret)
2610 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002611skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002612 len -= l;
2613 logical += l;
2614 physical += l;
2615 }
2616 return 0;
2617}
2618
Wang Shilong3b080b22014-04-01 18:01:43 +08002619/*
2620 * Given a physical address, this will calculate it's
2621 * logical offset. if this is a parity stripe, it will return
2622 * the most left data stripe's logical offset.
2623 *
2624 * return 0 if it is a data stripe, 1 means parity stripe.
2625 */
2626static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002627 struct map_lookup *map, u64 *offset,
2628 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002629{
2630 int i;
2631 int j = 0;
2632 u64 stripe_nr;
2633 u64 last_offset;
2634 int stripe_index;
2635 int rot;
2636
2637 last_offset = (physical - map->stripes[num].physical) *
2638 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002639 if (stripe_start)
2640 *stripe_start = last_offset;
2641
Wang Shilong3b080b22014-04-01 18:01:43 +08002642 *offset = last_offset;
2643 for (i = 0; i < nr_data_stripes(map); i++) {
2644 *offset = last_offset + i * map->stripe_len;
2645
2646 stripe_nr = *offset;
2647 do_div(stripe_nr, map->stripe_len);
2648 do_div(stripe_nr, nr_data_stripes(map));
2649
2650 /* Work out the disk rotation on this stripe-set */
2651 rot = do_div(stripe_nr, map->num_stripes);
2652 /* calculate which stripe this data locates */
2653 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002654 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002655 if (stripe_index == num)
2656 return 0;
2657 if (stripe_index < num)
2658 j++;
2659 }
2660 *offset = last_offset + j * map->stripe_len;
2661 return 1;
2662}
2663
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002664static void scrub_free_parity(struct scrub_parity *sparity)
2665{
2666 struct scrub_ctx *sctx = sparity->sctx;
2667 struct scrub_page *curr, *next;
2668 int nbits;
2669
2670 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2671 if (nbits) {
2672 spin_lock(&sctx->stat_lock);
2673 sctx->stat.read_errors += nbits;
2674 sctx->stat.uncorrectable_errors += nbits;
2675 spin_unlock(&sctx->stat_lock);
2676 }
2677
2678 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2679 list_del_init(&curr->list);
2680 scrub_page_put(curr);
2681 }
2682
2683 kfree(sparity);
2684}
2685
2686static void scrub_parity_bio_endio(struct bio *bio, int error)
2687{
2688 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2689 struct scrub_ctx *sctx = sparity->sctx;
2690
2691 if (error)
2692 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2693 sparity->nsectors);
2694
2695 scrub_free_parity(sparity);
2696 scrub_pending_bio_dec(sctx);
2697 bio_put(bio);
2698}
2699
2700static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2701{
2702 struct scrub_ctx *sctx = sparity->sctx;
2703 struct bio *bio;
2704 struct btrfs_raid_bio *rbio;
2705 struct scrub_page *spage;
2706 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002707 u64 length;
2708 int ret;
2709
2710 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2711 sparity->nsectors))
2712 goto out;
2713
2714 length = sparity->logic_end - sparity->logic_start + 1;
Miao Xie76035972014-11-14 17:45:42 +08002715 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002716 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002717 &length, &bbio, 0, 1);
2718 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002719 goto bbio_out;
2720
2721 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2722 if (!bio)
2723 goto bbio_out;
2724
2725 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2726 bio->bi_private = sparity;
2727 bio->bi_end_io = scrub_parity_bio_endio;
2728
2729 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002730 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002731 sparity->dbitmap,
2732 sparity->nsectors);
2733 if (!rbio)
2734 goto rbio_out;
2735
2736 list_for_each_entry(spage, &sparity->spages, list)
2737 raid56_parity_add_scrub_pages(rbio, spage->page,
2738 spage->logical);
2739
2740 scrub_pending_bio_inc(sctx);
2741 raid56_parity_submit_scrub_rbio(rbio);
2742 return;
2743
2744rbio_out:
2745 bio_put(bio);
2746bbio_out:
Zhao Lei6e9606d2015-01-20 15:11:34 +08002747 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002748 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2749 sparity->nsectors);
2750 spin_lock(&sctx->stat_lock);
2751 sctx->stat.malloc_errors++;
2752 spin_unlock(&sctx->stat_lock);
2753out:
2754 scrub_free_parity(sparity);
2755}
2756
2757static inline int scrub_calc_parity_bitmap_len(int nsectors)
2758{
2759 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2760}
2761
2762static void scrub_parity_get(struct scrub_parity *sparity)
2763{
2764 atomic_inc(&sparity->ref_count);
2765}
2766
2767static void scrub_parity_put(struct scrub_parity *sparity)
2768{
2769 if (!atomic_dec_and_test(&sparity->ref_count))
2770 return;
2771
2772 scrub_parity_check_and_repair(sparity);
2773}
2774
2775static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2776 struct map_lookup *map,
2777 struct btrfs_device *sdev,
2778 struct btrfs_path *path,
2779 u64 logic_start,
2780 u64 logic_end)
2781{
2782 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2783 struct btrfs_root *root = fs_info->extent_root;
2784 struct btrfs_root *csum_root = fs_info->csum_root;
2785 struct btrfs_extent_item *extent;
2786 u64 flags;
2787 int ret;
2788 int slot;
2789 struct extent_buffer *l;
2790 struct btrfs_key key;
2791 u64 generation;
2792 u64 extent_logical;
2793 u64 extent_physical;
2794 u64 extent_len;
2795 struct btrfs_device *extent_dev;
2796 struct scrub_parity *sparity;
2797 int nsectors;
2798 int bitmap_len;
2799 int extent_mirror_num;
2800 int stop_loop = 0;
2801
2802 nsectors = map->stripe_len / root->sectorsize;
2803 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2804 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2805 GFP_NOFS);
2806 if (!sparity) {
2807 spin_lock(&sctx->stat_lock);
2808 sctx->stat.malloc_errors++;
2809 spin_unlock(&sctx->stat_lock);
2810 return -ENOMEM;
2811 }
2812
2813 sparity->stripe_len = map->stripe_len;
2814 sparity->nsectors = nsectors;
2815 sparity->sctx = sctx;
2816 sparity->scrub_dev = sdev;
2817 sparity->logic_start = logic_start;
2818 sparity->logic_end = logic_end;
2819 atomic_set(&sparity->ref_count, 1);
2820 INIT_LIST_HEAD(&sparity->spages);
2821 sparity->dbitmap = sparity->bitmap;
2822 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2823
2824 ret = 0;
2825 while (logic_start < logic_end) {
2826 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2827 key.type = BTRFS_METADATA_ITEM_KEY;
2828 else
2829 key.type = BTRFS_EXTENT_ITEM_KEY;
2830 key.objectid = logic_start;
2831 key.offset = (u64)-1;
2832
2833 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2834 if (ret < 0)
2835 goto out;
2836
2837 if (ret > 0) {
2838 ret = btrfs_previous_extent_item(root, path, 0);
2839 if (ret < 0)
2840 goto out;
2841 if (ret > 0) {
2842 btrfs_release_path(path);
2843 ret = btrfs_search_slot(NULL, root, &key,
2844 path, 0, 0);
2845 if (ret < 0)
2846 goto out;
2847 }
2848 }
2849
2850 stop_loop = 0;
2851 while (1) {
2852 u64 bytes;
2853
2854 l = path->nodes[0];
2855 slot = path->slots[0];
2856 if (slot >= btrfs_header_nritems(l)) {
2857 ret = btrfs_next_leaf(root, path);
2858 if (ret == 0)
2859 continue;
2860 if (ret < 0)
2861 goto out;
2862
2863 stop_loop = 1;
2864 break;
2865 }
2866 btrfs_item_key_to_cpu(l, &key, slot);
2867
2868 if (key.type == BTRFS_METADATA_ITEM_KEY)
2869 bytes = root->nodesize;
2870 else
2871 bytes = key.offset;
2872
2873 if (key.objectid + bytes <= logic_start)
2874 goto next;
2875
2876 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2877 key.type != BTRFS_METADATA_ITEM_KEY)
2878 goto next;
2879
2880 if (key.objectid > logic_end) {
2881 stop_loop = 1;
2882 break;
2883 }
2884
2885 while (key.objectid >= logic_start + map->stripe_len)
2886 logic_start += map->stripe_len;
2887
2888 extent = btrfs_item_ptr(l, slot,
2889 struct btrfs_extent_item);
2890 flags = btrfs_extent_flags(l, extent);
2891 generation = btrfs_extent_generation(l, extent);
2892
2893 if (key.objectid < logic_start &&
2894 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2895 btrfs_err(fs_info,
2896 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2897 key.objectid, logic_start);
2898 goto next;
2899 }
2900again:
2901 extent_logical = key.objectid;
2902 extent_len = bytes;
2903
2904 if (extent_logical < logic_start) {
2905 extent_len -= logic_start - extent_logical;
2906 extent_logical = logic_start;
2907 }
2908
2909 if (extent_logical + extent_len >
2910 logic_start + map->stripe_len)
2911 extent_len = logic_start + map->stripe_len -
2912 extent_logical;
2913
2914 scrub_parity_mark_sectors_data(sparity, extent_logical,
2915 extent_len);
2916
2917 scrub_remap_extent(fs_info, extent_logical,
2918 extent_len, &extent_physical,
2919 &extent_dev,
2920 &extent_mirror_num);
2921
2922 ret = btrfs_lookup_csums_range(csum_root,
2923 extent_logical,
2924 extent_logical + extent_len - 1,
2925 &sctx->csum_list, 1);
2926 if (ret)
2927 goto out;
2928
2929 ret = scrub_extent_for_parity(sparity, extent_logical,
2930 extent_len,
2931 extent_physical,
2932 extent_dev, flags,
2933 generation,
2934 extent_mirror_num);
2935 if (ret)
2936 goto out;
2937
2938 scrub_free_csums(sctx);
2939 if (extent_logical + extent_len <
2940 key.objectid + bytes) {
2941 logic_start += map->stripe_len;
2942
2943 if (logic_start >= logic_end) {
2944 stop_loop = 1;
2945 break;
2946 }
2947
2948 if (logic_start < key.objectid + bytes) {
2949 cond_resched();
2950 goto again;
2951 }
2952 }
2953next:
2954 path->slots[0]++;
2955 }
2956
2957 btrfs_release_path(path);
2958
2959 if (stop_loop)
2960 break;
2961
2962 logic_start += map->stripe_len;
2963 }
2964out:
2965 if (ret < 0)
2966 scrub_parity_mark_sectors_error(sparity, logic_start,
2967 logic_end - logic_start + 1);
2968 scrub_parity_put(sparity);
2969 scrub_submit(sctx);
2970 mutex_lock(&sctx->wr_ctx.wr_lock);
2971 scrub_wr_submit(sctx);
2972 mutex_unlock(&sctx->wr_ctx.wr_lock);
2973
2974 btrfs_release_path(path);
2975 return ret < 0 ? ret : 0;
2976}
2977
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002978static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002979 struct map_lookup *map,
2980 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002981 int num, u64 base, u64 length,
2982 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002983{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002984 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002985 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002986 struct btrfs_root *root = fs_info->extent_root;
2987 struct btrfs_root *csum_root = fs_info->csum_root;
2988 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002989 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002990 u64 flags;
2991 int ret;
2992 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002993 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002994 struct extent_buffer *l;
2995 struct btrfs_key key;
2996 u64 physical;
2997 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002998 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08002999 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003000 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003001 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003002 struct reada_control *reada1;
3003 struct reada_control *reada2;
3004 struct btrfs_key key_start;
3005 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003006 u64 increment = map->stripe_len;
3007 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003008 u64 extent_logical;
3009 u64 extent_physical;
3010 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003011 u64 stripe_logical;
3012 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003013 struct btrfs_device *extent_dev;
3014 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003015 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003016
Arne Jansena2de7332011-03-08 14:14:00 +01003017 nstripes = length;
Wang Shilong3b080b22014-04-01 18:01:43 +08003018 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003019 offset = 0;
3020 do_div(nstripes, map->stripe_len);
3021 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3022 offset = map->stripe_len * num;
3023 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003024 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003025 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3026 int factor = map->num_stripes / map->sub_stripes;
3027 offset = map->stripe_len * (num / map->sub_stripes);
3028 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003029 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003030 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3031 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003032 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003033 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3034 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003035 mirror_num = num % map->num_stripes + 1;
Wang Shilong3b080b22014-04-01 18:01:43 +08003036 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3037 BTRFS_BLOCK_GROUP_RAID6)) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003038 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003039 increment = map->stripe_len * nr_data_stripes(map);
3040 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003041 } else {
3042 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003043 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003044 }
3045
3046 path = btrfs_alloc_path();
3047 if (!path)
3048 return -ENOMEM;
3049
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003050 ppath = btrfs_alloc_path();
3051 if (!ppath) {
3052 btrfs_free_path(ppath);
3053 return -ENOMEM;
3054 }
3055
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003056 /*
3057 * work on commit root. The related disk blocks are static as
3058 * long as COW is applied. This means, it is save to rewrite
3059 * them to repair disk errors without any race conditions
3060 */
Arne Jansena2de7332011-03-08 14:14:00 +01003061 path->search_commit_root = 1;
3062 path->skip_locking = 1;
3063
3064 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003065 * trigger the readahead for extent tree csum tree and wait for
3066 * completion. During readahead, the scrub is officially paused
3067 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003068 */
3069 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003070 physical_end = physical + nstripes * map->stripe_len;
3071 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3072 BTRFS_BLOCK_GROUP_RAID6)) {
3073 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003074 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003075 logic_end += base;
3076 } else {
3077 logic_end = logical + increment * nstripes;
3078 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003079 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003080 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003081 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003082
Arne Jansen7a262852011-06-10 12:39:23 +02003083 /* FIXME it might be better to start readahead at commit root */
3084 key_start.objectid = logical;
3085 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3086 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003087 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003088 key_end.type = BTRFS_METADATA_ITEM_KEY;
3089 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003090 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003091
Arne Jansen7a262852011-06-10 12:39:23 +02003092 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3093 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3094 key_start.offset = logical;
3095 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3096 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003097 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003098 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003099
Arne Jansen7a262852011-06-10 12:39:23 +02003100 if (!IS_ERR(reada1))
3101 btrfs_reada_wait(reada1);
3102 if (!IS_ERR(reada2))
3103 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003104
Arne Jansena2de7332011-03-08 14:14:00 +01003105
3106 /*
3107 * collect all data csums for the stripe to avoid seeking during
3108 * the scrub. This might currently (crc32) end up to be about 1MB
3109 */
Arne Jansene7786c32011-05-28 20:58:38 +00003110 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003111
Arne Jansena2de7332011-03-08 14:14:00 +01003112 /*
3113 * now find all extents for each stripe and scrub them
3114 */
Arne Jansena2de7332011-03-08 14:14:00 +01003115 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003116 while (physical < physical_end) {
3117 /* for raid56, we skip parity stripe */
3118 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3119 BTRFS_BLOCK_GROUP_RAID6)) {
3120 ret = get_raid56_logic_offset(physical, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003121 map, &logical, &stripe_logical);
Wang Shilong3b080b22014-04-01 18:01:43 +08003122 logical += base;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003123 if (ret) {
3124 stripe_logical += base;
3125 stripe_end = stripe_logical + increment - 1;
3126 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3127 ppath, stripe_logical,
3128 stripe_end);
3129 if (ret)
3130 goto out;
Wang Shilong3b080b22014-04-01 18:01:43 +08003131 goto skip;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003132 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003133 }
Arne Jansena2de7332011-03-08 14:14:00 +01003134 /*
3135 * canceled?
3136 */
3137 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003138 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003139 ret = -ECANCELED;
3140 goto out;
3141 }
3142 /*
3143 * check to see if we have to pause
3144 */
3145 if (atomic_read(&fs_info->scrub_pause_req)) {
3146 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003147 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003148 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003149 mutex_lock(&sctx->wr_ctx.wr_lock);
3150 scrub_wr_submit(sctx);
3151 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003152 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003153 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003154 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003155 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003156 }
3157
Wang Shilong7c76edb2014-01-12 21:38:32 +08003158 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3159 key.type = BTRFS_METADATA_ITEM_KEY;
3160 else
3161 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003162 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003163 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003164
3165 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3166 if (ret < 0)
3167 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003168
Arne Jansen8c510322011-06-03 10:09:26 +02003169 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003170 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003171 if (ret < 0)
3172 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003173 if (ret > 0) {
3174 /* there's no smaller item, so stick with the
3175 * larger one */
3176 btrfs_release_path(path);
3177 ret = btrfs_search_slot(NULL, root, &key,
3178 path, 0, 0);
3179 if (ret < 0)
3180 goto out;
3181 }
Arne Jansena2de7332011-03-08 14:14:00 +01003182 }
3183
Liu Bo625f1c8d2013-04-27 02:56:57 +00003184 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003185 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003186 u64 bytes;
3187
Arne Jansena2de7332011-03-08 14:14:00 +01003188 l = path->nodes[0];
3189 slot = path->slots[0];
3190 if (slot >= btrfs_header_nritems(l)) {
3191 ret = btrfs_next_leaf(root, path);
3192 if (ret == 0)
3193 continue;
3194 if (ret < 0)
3195 goto out;
3196
Liu Bo625f1c8d2013-04-27 02:56:57 +00003197 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003198 break;
3199 }
3200 btrfs_item_key_to_cpu(l, &key, slot);
3201
Josef Bacik3173a182013-03-07 14:22:04 -05003202 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003203 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003204 else
3205 bytes = key.offset;
3206
3207 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003208 goto next;
3209
Liu Bo625f1c8d2013-04-27 02:56:57 +00003210 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3211 key.type != BTRFS_METADATA_ITEM_KEY)
3212 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01003213
Liu Bo625f1c8d2013-04-27 02:56:57 +00003214 if (key.objectid >= logical + map->stripe_len) {
3215 /* out of this device extent */
3216 if (key.objectid >= logic_end)
3217 stop_loop = 1;
3218 break;
3219 }
Arne Jansena2de7332011-03-08 14:14:00 +01003220
3221 extent = btrfs_item_ptr(l, slot,
3222 struct btrfs_extent_item);
3223 flags = btrfs_extent_flags(l, extent);
3224 generation = btrfs_extent_generation(l, extent);
3225
3226 if (key.objectid < logical &&
3227 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003228 btrfs_err(fs_info,
3229 "scrub: tree block %llu spanning "
3230 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003231 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01003232 goto next;
3233 }
3234
Liu Bo625f1c8d2013-04-27 02:56:57 +00003235again:
3236 extent_logical = key.objectid;
3237 extent_len = bytes;
3238
Arne Jansena2de7332011-03-08 14:14:00 +01003239 /*
3240 * trim extent to this stripe
3241 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003242 if (extent_logical < logical) {
3243 extent_len -= logical - extent_logical;
3244 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003245 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003246 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003247 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003248 extent_len = logical + map->stripe_len -
3249 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003250 }
3251
Liu Bo625f1c8d2013-04-27 02:56:57 +00003252 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003253 extent_dev = scrub_dev;
3254 extent_mirror_num = mirror_num;
3255 if (is_dev_replace)
3256 scrub_remap_extent(fs_info, extent_logical,
3257 extent_len, &extent_physical,
3258 &extent_dev,
3259 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003260
3261 ret = btrfs_lookup_csums_range(csum_root, logical,
3262 logical + map->stripe_len - 1,
3263 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003264 if (ret)
3265 goto out;
3266
Liu Bo625f1c8d2013-04-27 02:56:57 +00003267 ret = scrub_extent(sctx, extent_logical, extent_len,
3268 extent_physical, extent_dev, flags,
3269 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003270 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003271 if (ret)
3272 goto out;
3273
Josef Bacikd88d46c2013-06-10 12:59:04 +00003274 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003275 if (extent_logical + extent_len <
3276 key.objectid + bytes) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003277 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3278 BTRFS_BLOCK_GROUP_RAID6)) {
3279 /*
3280 * loop until we find next data stripe
3281 * or we have finished all stripes.
3282 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003283loop:
3284 physical += map->stripe_len;
3285 ret = get_raid56_logic_offset(physical,
3286 num, map, &logical,
3287 &stripe_logical);
3288 logical += base;
3289
3290 if (ret && physical < physical_end) {
3291 stripe_logical += base;
3292 stripe_end = stripe_logical +
3293 increment - 1;
3294 ret = scrub_raid56_parity(sctx,
3295 map, scrub_dev, ppath,
3296 stripe_logical,
3297 stripe_end);
3298 if (ret)
3299 goto out;
3300 goto loop;
3301 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003302 } else {
3303 physical += map->stripe_len;
3304 logical += increment;
3305 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003306 if (logical < key.objectid + bytes) {
3307 cond_resched();
3308 goto again;
3309 }
3310
Wang Shilong3b080b22014-04-01 18:01:43 +08003311 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003312 stop_loop = 1;
3313 break;
3314 }
3315 }
Arne Jansena2de7332011-03-08 14:14:00 +01003316next:
3317 path->slots[0]++;
3318 }
Chris Mason71267332011-05-23 06:30:52 -04003319 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003320skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003321 logical += increment;
3322 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003323 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003324 if (stop_loop)
3325 sctx->stat.last_physical = map->stripes[num].physical +
3326 length;
3327 else
3328 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003329 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003330 if (stop_loop)
3331 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003332 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003333out:
Arne Jansena2de7332011-03-08 14:14:00 +01003334 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003335 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003336 mutex_lock(&sctx->wr_ctx.wr_lock);
3337 scrub_wr_submit(sctx);
3338 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003339
Arne Jansene7786c32011-05-28 20:58:38 +00003340 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003341 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003342 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003343 return ret < 0 ? ret : 0;
3344}
3345
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003346static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003347 struct btrfs_device *scrub_dev,
3348 u64 chunk_tree, u64 chunk_objectid,
3349 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003350 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003351{
3352 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003353 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003354 struct map_lookup *map;
3355 struct extent_map *em;
3356 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003357 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003358
3359 read_lock(&map_tree->map_tree.lock);
3360 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3361 read_unlock(&map_tree->map_tree.lock);
3362
3363 if (!em)
3364 return -EINVAL;
3365
3366 map = (struct map_lookup *)em->bdev;
3367 if (em->start != chunk_offset)
3368 goto out;
3369
3370 if (em->len < length)
3371 goto out;
3372
3373 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003374 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003375 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003376 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003377 chunk_offset, length,
3378 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003379 if (ret)
3380 goto out;
3381 }
3382 }
3383out:
3384 free_extent_map(em);
3385
3386 return ret;
3387}
3388
3389static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003390int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003391 struct btrfs_device *scrub_dev, u64 start, u64 end,
3392 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003393{
3394 struct btrfs_dev_extent *dev_extent = NULL;
3395 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003396 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003397 struct btrfs_fs_info *fs_info = root->fs_info;
3398 u64 length;
3399 u64 chunk_tree;
3400 u64 chunk_objectid;
3401 u64 chunk_offset;
3402 int ret;
3403 int slot;
3404 struct extent_buffer *l;
3405 struct btrfs_key key;
3406 struct btrfs_key found_key;
3407 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003408 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003409
3410 path = btrfs_alloc_path();
3411 if (!path)
3412 return -ENOMEM;
3413
3414 path->reada = 2;
3415 path->search_commit_root = 1;
3416 path->skip_locking = 1;
3417
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003418 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003419 key.offset = 0ull;
3420 key.type = BTRFS_DEV_EXTENT_KEY;
3421
Arne Jansena2de7332011-03-08 14:14:00 +01003422 while (1) {
3423 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3424 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003425 break;
3426 if (ret > 0) {
3427 if (path->slots[0] >=
3428 btrfs_header_nritems(path->nodes[0])) {
3429 ret = btrfs_next_leaf(root, path);
3430 if (ret)
3431 break;
3432 }
3433 }
Arne Jansena2de7332011-03-08 14:14:00 +01003434
3435 l = path->nodes[0];
3436 slot = path->slots[0];
3437
3438 btrfs_item_key_to_cpu(l, &found_key, slot);
3439
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003440 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003441 break;
3442
David Sterba962a2982014-06-04 18:41:45 +02003443 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003444 break;
3445
3446 if (found_key.offset >= end)
3447 break;
3448
3449 if (found_key.offset < key.offset)
3450 break;
3451
3452 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3453 length = btrfs_dev_extent_length(l, dev_extent);
3454
Qu Wenruoced96ed2014-06-19 10:42:51 +08003455 if (found_key.offset + length <= start)
3456 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003457
3458 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3459 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3460 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3461
3462 /*
3463 * get a reference on the corresponding block group to prevent
3464 * the chunk from going away while we scrub it
3465 */
3466 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003467
3468 /* some chunks are removed but not committed to disk yet,
3469 * continue scrubbing */
3470 if (!cache)
3471 goto skip;
3472
Stefan Behrensff023aa2012-11-06 11:43:11 +01003473 dev_replace->cursor_right = found_key.offset + length;
3474 dev_replace->cursor_left = found_key.offset;
3475 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003476 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003477 chunk_offset, length, found_key.offset,
3478 is_dev_replace);
3479
3480 /*
3481 * flush, submit all pending read and write bios, afterwards
3482 * wait for them.
3483 * Note that in the dev replace case, a read request causes
3484 * write requests that are submitted in the read completion
3485 * worker. Therefore in the current situation, it is required
3486 * that all write requests are flushed, so that all read and
3487 * write requests are really completed when bios_in_flight
3488 * changes to 0.
3489 */
3490 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3491 scrub_submit(sctx);
3492 mutex_lock(&sctx->wr_ctx.wr_lock);
3493 scrub_wr_submit(sctx);
3494 mutex_unlock(&sctx->wr_ctx.wr_lock);
3495
3496 wait_event(sctx->list_wait,
3497 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003498 atomic_inc(&fs_info->scrubs_paused);
3499 wake_up(&fs_info->scrub_pause_wait);
3500
3501 /*
3502 * must be called before we decrease @scrub_paused.
3503 * make sure we don't block transaction commit while
3504 * we are waiting pending workers finished.
3505 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003506 wait_event(sctx->list_wait,
3507 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003508 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3509
3510 mutex_lock(&fs_info->scrub_lock);
3511 __scrub_blocked_if_needed(fs_info);
3512 atomic_dec(&fs_info->scrubs_paused);
3513 mutex_unlock(&fs_info->scrub_lock);
3514 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003515
Arne Jansena2de7332011-03-08 14:14:00 +01003516 btrfs_put_block_group(cache);
3517 if (ret)
3518 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003519 if (is_dev_replace &&
3520 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003521 ret = -EIO;
3522 break;
3523 }
3524 if (sctx->stat.malloc_errors > 0) {
3525 ret = -ENOMEM;
3526 break;
3527 }
Arne Jansena2de7332011-03-08 14:14:00 +01003528
Ilya Dryomov539f3582013-10-07 13:42:57 +03003529 dev_replace->cursor_left = dev_replace->cursor_right;
3530 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003531skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003532 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003533 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003534 }
3535
Arne Jansena2de7332011-03-08 14:14:00 +01003536 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003537
3538 /*
3539 * ret can still be 1 from search_slot or next_leaf,
3540 * that's not an error
3541 */
3542 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003543}
3544
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003545static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3546 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003547{
3548 int i;
3549 u64 bytenr;
3550 u64 gen;
3551 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003552 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003553
Miao Xie87533c42013-01-29 10:14:48 +00003554 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003555 return -EIO;
3556
Miao Xie5f546062014-07-24 11:37:09 +08003557 /* Seed devices of a new filesystem has their own generation. */
3558 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3559 gen = scrub_dev->generation;
3560 else
3561 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003562
3563 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3564 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003565 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3566 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003567 break;
3568
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003569 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003570 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003571 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003572 if (ret)
3573 return ret;
3574 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003575 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003576
3577 return 0;
3578}
3579
3580/*
3581 * get a reference count on fs_info->scrub_workers. start worker if necessary
3582 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003583static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3584 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003585{
Josef Bacik0dc3b842011-11-18 14:37:27 -05003586 int ret = 0;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003587 int flags = WQ_FREEZABLE | WQ_UNBOUND;
3588 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003589
Arne Jansen632dd772011-06-10 12:07:07 +02003590 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003591 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003592 fs_info->scrub_workers =
3593 btrfs_alloc_workqueue("btrfs-scrub", flags,
3594 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003595 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003596 fs_info->scrub_workers =
3597 btrfs_alloc_workqueue("btrfs-scrub", flags,
3598 max_active, 4);
3599 if (!fs_info->scrub_workers) {
3600 ret = -ENOMEM;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003601 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003602 }
3603 fs_info->scrub_wr_completion_workers =
3604 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3605 max_active, 2);
3606 if (!fs_info->scrub_wr_completion_workers) {
3607 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003608 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003609 }
3610 fs_info->scrub_nocow_workers =
3611 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
3612 if (!fs_info->scrub_nocow_workers) {
3613 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003614 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003615 }
Arne Jansen632dd772011-06-10 12:07:07 +02003616 }
Arne Jansena2de7332011-03-08 14:14:00 +01003617 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003618out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05003619 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003620}
3621
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003622static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003623{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003624 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003625 btrfs_destroy_workqueue(fs_info->scrub_workers);
3626 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3627 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003628 }
Arne Jansena2de7332011-03-08 14:14:00 +01003629 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003630}
3631
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003632int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3633 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003634 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003635{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003636 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003637 int ret;
3638 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003639 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003640
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003641 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003642 return -EINVAL;
3643
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003644 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003645 /*
3646 * in this case scrub is unable to calculate the checksum
3647 * the way scrub is implemented. Do not handle this
3648 * situation at all because it won't ever happen.
3649 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003650 btrfs_err(fs_info,
3651 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003652 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003653 return -EINVAL;
3654 }
3655
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003656 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003657 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003658 btrfs_err(fs_info,
3659 "scrub: size assumption sectorsize != PAGE_SIZE "
3660 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003661 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003662 return -EINVAL;
3663 }
3664
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003665 if (fs_info->chunk_root->nodesize >
3666 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3667 fs_info->chunk_root->sectorsize >
3668 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3669 /*
3670 * would exhaust the array bounds of pagev member in
3671 * struct scrub_block
3672 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003673 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3674 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003675 fs_info->chunk_root->nodesize,
3676 SCRUB_MAX_PAGES_PER_BLOCK,
3677 fs_info->chunk_root->sectorsize,
3678 SCRUB_MAX_PAGES_PER_BLOCK);
3679 return -EINVAL;
3680 }
3681
Arne Jansena2de7332011-03-08 14:14:00 +01003682
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003683 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3684 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003685 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003686 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003687 return -ENODEV;
3688 }
Arne Jansena2de7332011-03-08 14:14:00 +01003689
Miao Xie5d68da32014-07-24 11:37:07 +08003690 if (!is_dev_replace && !readonly && !dev->writeable) {
3691 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3692 rcu_read_lock();
3693 name = rcu_dereference(dev->name);
3694 btrfs_err(fs_info, "scrub: device %s is not writable",
3695 name->str);
3696 rcu_read_unlock();
3697 return -EROFS;
3698 }
3699
Wang Shilong3b7a0162013-10-12 02:11:12 +08003700 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003701 if (!dev->in_fs_metadata || dev->is_tgtdev_for_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);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003704 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003705 }
3706
Stefan Behrens8dabb742012-11-06 13:15:27 +01003707 btrfs_dev_replace_lock(&fs_info->dev_replace);
3708 if (dev->scrub_device ||
3709 (!is_dev_replace &&
3710 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3711 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003712 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003713 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003714 return -EINPROGRESS;
3715 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003716 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003717
3718 ret = scrub_workers_get(fs_info, is_dev_replace);
3719 if (ret) {
3720 mutex_unlock(&fs_info->scrub_lock);
3721 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3722 return ret;
3723 }
3724
Stefan Behrens63a212a2012-11-05 18:29:28 +01003725 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003726 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003727 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003728 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3729 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003730 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003731 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003732 sctx->readonly = readonly;
3733 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003734 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003735
Wang Shilong3cb09292013-12-04 21:15:19 +08003736 /*
3737 * checking @scrub_pause_req here, we can avoid
3738 * race between committing transaction and scrubbing.
3739 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003740 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003741 atomic_inc(&fs_info->scrubs_running);
3742 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003743
Stefan Behrensff023aa2012-11-06 11:43:11 +01003744 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003745 /*
3746 * by holding device list mutex, we can
3747 * kick off writing super in log tree sync.
3748 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003749 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003750 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003751 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003752 }
Arne Jansena2de7332011-03-08 14:14:00 +01003753
3754 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003755 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3756 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003757
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003758 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003759 atomic_dec(&fs_info->scrubs_running);
3760 wake_up(&fs_info->scrub_pause_wait);
3761
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003762 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003763
Arne Jansena2de7332011-03-08 14:14:00 +01003764 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003765 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003766
3767 mutex_lock(&fs_info->scrub_lock);
3768 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003769 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003770 mutex_unlock(&fs_info->scrub_lock);
3771
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003772 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003773
3774 return ret;
3775}
3776
Jeff Mahoney143bede2012-03-01 14:56:26 +01003777void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003778{
3779 struct btrfs_fs_info *fs_info = root->fs_info;
3780
3781 mutex_lock(&fs_info->scrub_lock);
3782 atomic_inc(&fs_info->scrub_pause_req);
3783 while (atomic_read(&fs_info->scrubs_paused) !=
3784 atomic_read(&fs_info->scrubs_running)) {
3785 mutex_unlock(&fs_info->scrub_lock);
3786 wait_event(fs_info->scrub_pause_wait,
3787 atomic_read(&fs_info->scrubs_paused) ==
3788 atomic_read(&fs_info->scrubs_running));
3789 mutex_lock(&fs_info->scrub_lock);
3790 }
3791 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003792}
3793
Jeff Mahoney143bede2012-03-01 14:56:26 +01003794void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003795{
3796 struct btrfs_fs_info *fs_info = root->fs_info;
3797
3798 atomic_dec(&fs_info->scrub_pause_req);
3799 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003800}
3801
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003802int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003803{
Arne Jansena2de7332011-03-08 14:14:00 +01003804 mutex_lock(&fs_info->scrub_lock);
3805 if (!atomic_read(&fs_info->scrubs_running)) {
3806 mutex_unlock(&fs_info->scrub_lock);
3807 return -ENOTCONN;
3808 }
3809
3810 atomic_inc(&fs_info->scrub_cancel_req);
3811 while (atomic_read(&fs_info->scrubs_running)) {
3812 mutex_unlock(&fs_info->scrub_lock);
3813 wait_event(fs_info->scrub_pause_wait,
3814 atomic_read(&fs_info->scrubs_running) == 0);
3815 mutex_lock(&fs_info->scrub_lock);
3816 }
3817 atomic_dec(&fs_info->scrub_cancel_req);
3818 mutex_unlock(&fs_info->scrub_lock);
3819
3820 return 0;
3821}
3822
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003823int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3824 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003825{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003826 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003827
3828 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003829 sctx = dev->scrub_device;
3830 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003831 mutex_unlock(&fs_info->scrub_lock);
3832 return -ENOTCONN;
3833 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003834 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003835 while (dev->scrub_device) {
3836 mutex_unlock(&fs_info->scrub_lock);
3837 wait_event(fs_info->scrub_pause_wait,
3838 dev->scrub_device == NULL);
3839 mutex_lock(&fs_info->scrub_lock);
3840 }
3841 mutex_unlock(&fs_info->scrub_lock);
3842
3843 return 0;
3844}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003845
Arne Jansena2de7332011-03-08 14:14:00 +01003846int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3847 struct btrfs_scrub_progress *progress)
3848{
3849 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003850 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003851
3852 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003853 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003854 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003855 sctx = dev->scrub_device;
3856 if (sctx)
3857 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003858 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3859
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003860 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003861}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003862
3863static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3864 u64 extent_logical, u64 extent_len,
3865 u64 *extent_physical,
3866 struct btrfs_device **extent_dev,
3867 int *extent_mirror_num)
3868{
3869 u64 mapped_length;
3870 struct btrfs_bio *bbio = NULL;
3871 int ret;
3872
3873 mapped_length = extent_len;
3874 ret = btrfs_map_block(fs_info, READ, extent_logical,
3875 &mapped_length, &bbio, 0);
3876 if (ret || !bbio || mapped_length < extent_len ||
3877 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08003878 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003879 return;
3880 }
3881
3882 *extent_physical = bbio->stripes[0].physical;
3883 *extent_mirror_num = bbio->mirror_num;
3884 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08003885 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003886}
3887
3888static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3889 struct scrub_wr_ctx *wr_ctx,
3890 struct btrfs_fs_info *fs_info,
3891 struct btrfs_device *dev,
3892 int is_dev_replace)
3893{
3894 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3895
3896 mutex_init(&wr_ctx->wr_lock);
3897 wr_ctx->wr_curr_bio = NULL;
3898 if (!is_dev_replace)
3899 return 0;
3900
3901 WARN_ON(!dev->bdev);
3902 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3903 bio_get_nr_vecs(dev->bdev));
3904 wr_ctx->tgtdev = dev;
3905 atomic_set(&wr_ctx->flush_all_writes, 0);
3906 return 0;
3907}
3908
3909static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3910{
3911 mutex_lock(&wr_ctx->wr_lock);
3912 kfree(wr_ctx->wr_curr_bio);
3913 wr_ctx->wr_curr_bio = NULL;
3914 mutex_unlock(&wr_ctx->wr_lock);
3915}
3916
3917static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3918 int mirror_num, u64 physical_for_dev_replace)
3919{
3920 struct scrub_copy_nocow_ctx *nocow_ctx;
3921 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3922
3923 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3924 if (!nocow_ctx) {
3925 spin_lock(&sctx->stat_lock);
3926 sctx->stat.malloc_errors++;
3927 spin_unlock(&sctx->stat_lock);
3928 return -ENOMEM;
3929 }
3930
3931 scrub_pending_trans_workers_inc(sctx);
3932
3933 nocow_ctx->sctx = sctx;
3934 nocow_ctx->logical = logical;
3935 nocow_ctx->len = len;
3936 nocow_ctx->mirror_num = mirror_num;
3937 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08003938 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3939 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003940 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003941 btrfs_queue_work(fs_info->scrub_nocow_workers,
3942 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003943
3944 return 0;
3945}
3946
Josef Bacik652f25a2013-09-12 16:58:28 -04003947static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3948{
3949 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3950 struct scrub_nocow_inode *nocow_inode;
3951
3952 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3953 if (!nocow_inode)
3954 return -ENOMEM;
3955 nocow_inode->inum = inum;
3956 nocow_inode->offset = offset;
3957 nocow_inode->root = root;
3958 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3959 return 0;
3960}
3961
3962#define COPY_COMPLETE 1
3963
Stefan Behrensff023aa2012-11-06 11:43:11 +01003964static void copy_nocow_pages_worker(struct btrfs_work *work)
3965{
3966 struct scrub_copy_nocow_ctx *nocow_ctx =
3967 container_of(work, struct scrub_copy_nocow_ctx, work);
3968 struct scrub_ctx *sctx = nocow_ctx->sctx;
3969 u64 logical = nocow_ctx->logical;
3970 u64 len = nocow_ctx->len;
3971 int mirror_num = nocow_ctx->mirror_num;
3972 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3973 int ret;
3974 struct btrfs_trans_handle *trans = NULL;
3975 struct btrfs_fs_info *fs_info;
3976 struct btrfs_path *path;
3977 struct btrfs_root *root;
3978 int not_written = 0;
3979
3980 fs_info = sctx->dev_root->fs_info;
3981 root = fs_info->extent_root;
3982
3983 path = btrfs_alloc_path();
3984 if (!path) {
3985 spin_lock(&sctx->stat_lock);
3986 sctx->stat.malloc_errors++;
3987 spin_unlock(&sctx->stat_lock);
3988 not_written = 1;
3989 goto out;
3990 }
3991
3992 trans = btrfs_join_transaction(root);
3993 if (IS_ERR(trans)) {
3994 not_written = 1;
3995 goto out;
3996 }
3997
3998 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04003999 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004000 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004001 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
4002 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02004003 logical, physical_for_dev_replace, len, mirror_num,
4004 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004005 not_written = 1;
4006 goto out;
4007 }
4008
Josef Bacik652f25a2013-09-12 16:58:28 -04004009 btrfs_end_transaction(trans, root);
4010 trans = NULL;
4011 while (!list_empty(&nocow_ctx->inodes)) {
4012 struct scrub_nocow_inode *entry;
4013 entry = list_first_entry(&nocow_ctx->inodes,
4014 struct scrub_nocow_inode,
4015 list);
4016 list_del_init(&entry->list);
4017 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4018 entry->root, nocow_ctx);
4019 kfree(entry);
4020 if (ret == COPY_COMPLETE) {
4021 ret = 0;
4022 break;
4023 } else if (ret) {
4024 break;
4025 }
4026 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004027out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004028 while (!list_empty(&nocow_ctx->inodes)) {
4029 struct scrub_nocow_inode *entry;
4030 entry = list_first_entry(&nocow_ctx->inodes,
4031 struct scrub_nocow_inode,
4032 list);
4033 list_del_init(&entry->list);
4034 kfree(entry);
4035 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004036 if (trans && !IS_ERR(trans))
4037 btrfs_end_transaction(trans, root);
4038 if (not_written)
4039 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4040 num_uncorrectable_read_errors);
4041
4042 btrfs_free_path(path);
4043 kfree(nocow_ctx);
4044
4045 scrub_pending_trans_workers_dec(sctx);
4046}
4047
Gui Hecheng32159242014-11-10 15:36:08 +08004048static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4049 u64 logical)
4050{
4051 struct extent_state *cached_state = NULL;
4052 struct btrfs_ordered_extent *ordered;
4053 struct extent_io_tree *io_tree;
4054 struct extent_map *em;
4055 u64 lockstart = start, lockend = start + len - 1;
4056 int ret = 0;
4057
4058 io_tree = &BTRFS_I(inode)->io_tree;
4059
4060 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4061 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4062 if (ordered) {
4063 btrfs_put_ordered_extent(ordered);
4064 ret = 1;
4065 goto out_unlock;
4066 }
4067
4068 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4069 if (IS_ERR(em)) {
4070 ret = PTR_ERR(em);
4071 goto out_unlock;
4072 }
4073
4074 /*
4075 * This extent does not actually cover the logical extent anymore,
4076 * move on to the next inode.
4077 */
4078 if (em->block_start > logical ||
4079 em->block_start + em->block_len < logical + len) {
4080 free_extent_map(em);
4081 ret = 1;
4082 goto out_unlock;
4083 }
4084 free_extent_map(em);
4085
4086out_unlock:
4087 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4088 GFP_NOFS);
4089 return ret;
4090}
4091
Josef Bacik652f25a2013-09-12 16:58:28 -04004092static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4093 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004094{
Miao Xie826aa0a2013-06-27 18:50:59 +08004095 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004096 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004097 struct inode *inode;
4098 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004099 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004100 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004101 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004102 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004103 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004104 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004105 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004106 int ret = 0;
4107 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004108
4109 key.objectid = root;
4110 key.type = BTRFS_ROOT_ITEM_KEY;
4111 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004112
4113 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4114
Stefan Behrensff023aa2012-11-06 11:43:11 +01004115 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004116 if (IS_ERR(local_root)) {
4117 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004118 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004119 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004120
4121 key.type = BTRFS_INODE_ITEM_KEY;
4122 key.objectid = inum;
4123 key.offset = 0;
4124 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004125 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004126 if (IS_ERR(inode))
4127 return PTR_ERR(inode);
4128
Miao Xieedd14002013-06-27 18:51:00 +08004129 /* Avoid truncate/dio/punch hole.. */
4130 mutex_lock(&inode->i_mutex);
4131 inode_dio_wait(inode);
4132
Stefan Behrensff023aa2012-11-06 11:43:11 +01004133 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004134 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004135 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004136
Gui Hecheng32159242014-11-10 15:36:08 +08004137 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4138 if (ret) {
4139 ret = ret > 0 ? 0 : ret;
4140 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004141 }
4142
Stefan Behrensff023aa2012-11-06 11:43:11 +01004143 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004144 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004145again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004146 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4147 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004148 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004149 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004150 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004151 }
4152
4153 if (PageUptodate(page)) {
4154 if (PageDirty(page))
4155 goto next_page;
4156 } else {
4157 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004158 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004159 btrfs_get_extent,
4160 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004161 if (err) {
4162 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004163 goto next_page;
4164 }
Miao Xieedd14002013-06-27 18:51:00 +08004165
Miao Xie26b258912013-06-27 18:50:58 +08004166 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004167 /*
4168 * If the page has been remove from the page cache,
4169 * the data on it is meaningless, because it may be
4170 * old one, the new data may be written into the new
4171 * page in the page cache.
4172 */
4173 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004174 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004175 page_cache_release(page);
4176 goto again;
4177 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004178 if (!PageUptodate(page)) {
4179 ret = -EIO;
4180 goto next_page;
4181 }
4182 }
Gui Hecheng32159242014-11-10 15:36:08 +08004183
4184 ret = check_extent_to_block(inode, offset, len,
4185 nocow_ctx_logical);
4186 if (ret) {
4187 ret = ret > 0 ? 0 : ret;
4188 goto next_page;
4189 }
4190
Miao Xie826aa0a2013-06-27 18:50:59 +08004191 err = write_page_nocow(nocow_ctx->sctx,
4192 physical_for_dev_replace, page);
4193 if (err)
4194 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004195next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004196 unlock_page(page);
4197 page_cache_release(page);
4198
4199 if (ret)
4200 break;
4201
Stefan Behrensff023aa2012-11-06 11:43:11 +01004202 offset += PAGE_CACHE_SIZE;
4203 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004204 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004205 len -= PAGE_CACHE_SIZE;
4206 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004207 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004208out:
Miao Xieedd14002013-06-27 18:51:00 +08004209 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004210 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004211 return ret;
4212}
4213
4214static int write_page_nocow(struct scrub_ctx *sctx,
4215 u64 physical_for_dev_replace, struct page *page)
4216{
4217 struct bio *bio;
4218 struct btrfs_device *dev;
4219 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004220
4221 dev = sctx->wr_ctx.tgtdev;
4222 if (!dev)
4223 return -EIO;
4224 if (!dev->bdev) {
4225 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05004226 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004227 return -EIO;
4228 }
Chris Mason9be33952013-05-17 18:30:14 -04004229 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004230 if (!bio) {
4231 spin_lock(&sctx->stat_lock);
4232 sctx->stat.malloc_errors++;
4233 spin_unlock(&sctx->stat_lock);
4234 return -ENOMEM;
4235 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004236 bio->bi_iter.bi_size = 0;
4237 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004238 bio->bi_bdev = dev->bdev;
4239 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4240 if (ret != PAGE_CACHE_SIZE) {
4241leave_with_eio:
4242 bio_put(bio);
4243 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4244 return -EIO;
4245 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004246
Kent Overstreet33879d42013-11-23 22:33:32 -08004247 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004248 goto leave_with_eio;
4249
4250 bio_put(bio);
4251 return 0;
4252}