blob: 9d07c981ec82f5373c5df36dfb2214c63e5ae1d3 [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,
253 struct scrub_block *sblock_good,
254 int force_write);
255static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
256 struct scrub_block *sblock_good,
257 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100258static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
259static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
260 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400261static int scrub_checksum_data(struct scrub_block *sblock);
262static int scrub_checksum_tree_block(struct scrub_block *sblock);
263static int scrub_checksum_super(struct scrub_block *sblock);
264static void scrub_block_get(struct scrub_block *sblock);
265static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100266static void scrub_page_get(struct scrub_page *spage);
267static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800268static void scrub_parity_get(struct scrub_parity *sparity);
269static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100270static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
271 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100272static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100273 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100274 u64 gen, int mirror_num, u8 *csum, int force,
275 u64 physical_for_dev_replace);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400276static void scrub_bio_end_io(struct bio *bio, int err);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400277static void scrub_bio_end_io_worker(struct btrfs_work *work);
278static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100279static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
280 u64 extent_logical, u64 extent_len,
281 u64 *extent_physical,
282 struct btrfs_device **extent_dev,
283 int *extent_mirror_num);
284static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
285 struct scrub_wr_ctx *wr_ctx,
286 struct btrfs_fs_info *fs_info,
287 struct btrfs_device *dev,
288 int is_dev_replace);
289static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
290static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
291 struct scrub_page *spage);
292static void scrub_wr_submit(struct scrub_ctx *sctx);
293static void scrub_wr_bio_end_io(struct bio *bio, int err);
294static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
295static int write_page_nocow(struct scrub_ctx *sctx,
296 u64 physical_for_dev_replace, struct page *page);
297static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400298 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100299static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
300 int mirror_num, u64 physical_for_dev_replace);
301static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800302static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800303static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400304
305
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100306static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
307{
308 atomic_inc(&sctx->bios_in_flight);
309}
310
311static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
312{
313 atomic_dec(&sctx->bios_in_flight);
314 wake_up(&sctx->list_wait);
315}
316
Wang Shilongcb7ab022013-12-04 21:16:53 +0800317static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800318{
319 while (atomic_read(&fs_info->scrub_pause_req)) {
320 mutex_unlock(&fs_info->scrub_lock);
321 wait_event(fs_info->scrub_pause_wait,
322 atomic_read(&fs_info->scrub_pause_req) == 0);
323 mutex_lock(&fs_info->scrub_lock);
324 }
325}
326
Wang Shilongcb7ab022013-12-04 21:16:53 +0800327static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
328{
329 atomic_inc(&fs_info->scrubs_paused);
330 wake_up(&fs_info->scrub_pause_wait);
331
332 mutex_lock(&fs_info->scrub_lock);
333 __scrub_blocked_if_needed(fs_info);
334 atomic_dec(&fs_info->scrubs_paused);
335 mutex_unlock(&fs_info->scrub_lock);
336
337 wake_up(&fs_info->scrub_pause_wait);
338}
339
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100340/*
341 * used for workers that require transaction commits (i.e., for the
342 * NOCOW case)
343 */
344static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
345{
346 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
347
348 /*
349 * increment scrubs_running to prevent cancel requests from
350 * completing as long as a worker is running. we must also
351 * increment scrubs_paused to prevent deadlocking on pause
352 * requests used for transactions commits (as the worker uses a
353 * transaction context). it is safe to regard the worker
354 * as paused for all matters practical. effectively, we only
355 * avoid cancellation requests from completing.
356 */
357 mutex_lock(&fs_info->scrub_lock);
358 atomic_inc(&fs_info->scrubs_running);
359 atomic_inc(&fs_info->scrubs_paused);
360 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800361
362 /*
363 * check if @scrubs_running=@scrubs_paused condition
364 * inside wait_event() is not an atomic operation.
365 * which means we may inc/dec @scrub_running/paused
366 * at any time. Let's wake up @scrub_pause_wait as
367 * much as we can to let commit transaction blocked less.
368 */
369 wake_up(&fs_info->scrub_pause_wait);
370
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100371 atomic_inc(&sctx->workers_pending);
372}
373
374/* used for workers that require transaction commits */
375static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
376{
377 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
378
379 /*
380 * see scrub_pending_trans_workers_inc() why we're pretending
381 * to be paused in the scrub counters
382 */
383 mutex_lock(&fs_info->scrub_lock);
384 atomic_dec(&fs_info->scrubs_running);
385 atomic_dec(&fs_info->scrubs_paused);
386 mutex_unlock(&fs_info->scrub_lock);
387 atomic_dec(&sctx->workers_pending);
388 wake_up(&fs_info->scrub_pause_wait);
389 wake_up(&sctx->list_wait);
390}
391
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100392static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100393{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100394 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100395 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100396 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100397 struct btrfs_ordered_sum, list);
398 list_del(&sum->list);
399 kfree(sum);
400 }
401}
402
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100403static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100404{
405 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100406
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100407 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100408 return;
409
Stefan Behrensff023aa2012-11-06 11:43:11 +0100410 scrub_free_wr_ctx(&sctx->wr_ctx);
411
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400412 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100413 if (sctx->curr != -1) {
414 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400415
416 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100417 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400418 scrub_block_put(sbio->pagev[i]->sblock);
419 }
420 bio_put(sbio->bio);
421 }
422
Stefan Behrensff023aa2012-11-06 11:43:11 +0100423 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100424 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100425
426 if (!sbio)
427 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100428 kfree(sbio);
429 }
430
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100431 scrub_free_csums(sctx);
432 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100433}
434
435static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100436struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100437{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100438 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100439 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100440 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100441 int pages_per_rd_bio;
442 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100443
Stefan Behrensff023aa2012-11-06 11:43:11 +0100444 /*
445 * the setting of pages_per_rd_bio is correct for scrub but might
446 * be wrong for the dev_replace code where we might read from
447 * different devices in the initial huge bios. However, that
448 * code is able to correctly handle the case when adding a page
449 * to a bio fails.
450 */
451 if (dev->bdev)
452 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
453 bio_get_nr_vecs(dev->bdev));
454 else
455 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100456 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
457 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100458 goto nomem;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100459 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100460 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100461 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100462 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100463 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100464 struct scrub_bio *sbio;
465
466 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
467 if (!sbio)
468 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100469 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100470
Arne Jansena2de7332011-03-08 14:14:00 +0100471 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100472 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400473 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800474 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
475 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100476
Stefan Behrensff023aa2012-11-06 11:43:11 +0100477 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100478 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200479 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100480 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100481 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100482 sctx->first_free = 0;
483 sctx->nodesize = dev->dev_root->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100484 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100485 atomic_set(&sctx->bios_in_flight, 0);
486 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100487 atomic_set(&sctx->cancel_req, 0);
488 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
489 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100490
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100491 spin_lock_init(&sctx->list_lock);
492 spin_lock_init(&sctx->stat_lock);
493 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100494
495 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
496 fs_info->dev_replace.tgtdev, is_dev_replace);
497 if (ret) {
498 scrub_free_ctx(sctx);
499 return ERR_PTR(ret);
500 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100501 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100502
503nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100504 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100505 return ERR_PTR(-ENOMEM);
506}
507
Stefan Behrensff023aa2012-11-06 11:43:11 +0100508static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
509 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200510{
511 u64 isize;
512 u32 nlink;
513 int ret;
514 int i;
515 struct extent_buffer *eb;
516 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100517 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200518 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
519 struct inode_fs_paths *ipath = NULL;
520 struct btrfs_root *local_root;
521 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100522 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200523
524 root_key.objectid = root;
525 root_key.type = BTRFS_ROOT_ITEM_KEY;
526 root_key.offset = (u64)-1;
527 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
528 if (IS_ERR(local_root)) {
529 ret = PTR_ERR(local_root);
530 goto err;
531 }
532
David Sterba14692cc2015-01-02 18:55:46 +0100533 /*
534 * this makes the path point to (inum INODE_ITEM ioff)
535 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100536 key.objectid = inum;
537 key.type = BTRFS_INODE_ITEM_KEY;
538 key.offset = 0;
539
540 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200541 if (ret) {
542 btrfs_release_path(swarn->path);
543 goto err;
544 }
545
546 eb = swarn->path->nodes[0];
547 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
548 struct btrfs_inode_item);
549 isize = btrfs_inode_size(eb, inode_item);
550 nlink = btrfs_inode_nlink(eb, inode_item);
551 btrfs_release_path(swarn->path);
552
553 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300554 if (IS_ERR(ipath)) {
555 ret = PTR_ERR(ipath);
556 ipath = NULL;
557 goto err;
558 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200559 ret = paths_from_inode(inum, ipath);
560
561 if (ret < 0)
562 goto err;
563
564 /*
565 * we deliberately ignore the bit ipath might have been too small to
566 * hold all of the paths here
567 */
568 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Frank Holtonefe120a2013-12-20 11:37:06 -0500569 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200570 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
571 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400572 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200573 (unsigned long long)swarn->sector, root, inum, offset,
574 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500575 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200576
577 free_ipath(ipath);
578 return 0;
579
580err:
Frank Holtonefe120a2013-12-20 11:37:06 -0500581 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200582 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
583 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400584 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200585 (unsigned long long)swarn->sector, root, inum, offset, ret);
586
587 free_ipath(ipath);
588 return 0;
589}
590
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400591static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200592{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100593 struct btrfs_device *dev;
594 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200595 struct btrfs_path *path;
596 struct btrfs_key found_key;
597 struct extent_buffer *eb;
598 struct btrfs_extent_item *ei;
599 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200600 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100601 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600602 u64 flags = 0;
603 u64 ref_root;
604 u32 item_size;
605 u8 ref_level;
Liu Bo69917e42012-09-07 20:01:28 -0600606 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200607
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100608 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100609 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100610 fs_info = sblock->sctx->dev_root->fs_info;
611
Jan Schmidt558540c2011-06-13 19:59:12 +0200612 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200613 if (!path)
614 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200615
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100616 swarn.sector = (sblock->pagev[0]->physical) >> 9;
617 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200618 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100619 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200620
Liu Bo69917e42012-09-07 20:01:28 -0600621 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
622 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200623 if (ret < 0)
624 goto out;
625
Jan Schmidt4692cf52011-12-02 14:56:41 +0100626 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200627 swarn.extent_item_size = found_key.offset;
628
629 eb = path->nodes[0];
630 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
631 item_size = btrfs_item_size_nr(eb, path->slots[0]);
632
Liu Bo69917e42012-09-07 20:01:28 -0600633 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200634 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800635 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
636 item_size, &ref_root,
637 &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400638 printk_in_rcu(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -0500639 "BTRFS: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200640 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400641 "%llu\n", errstr, swarn.logical,
642 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200643 (unsigned long long)swarn.sector,
644 ref_level ? "node" : "leaf",
645 ret < 0 ? -1 : ref_level,
646 ret < 0 ? -1 : ref_root);
647 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600648 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200649 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600650 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200651 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100652 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100653 iterate_extent_inodes(fs_info, found_key.objectid,
654 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200655 scrub_print_warning_inode, &swarn);
656 }
657
658out:
659 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200660}
661
Stefan Behrensff023aa2012-11-06 11:43:11 +0100662static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200663{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200664 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200665 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100666 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200667 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200668 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200669 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200670 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000671 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200672 u64 end = offset + PAGE_SIZE - 1;
673 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000674 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200675
676 key.objectid = root;
677 key.type = BTRFS_ROOT_ITEM_KEY;
678 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000679
680 fs_info = fixup->root->fs_info;
681 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
682
683 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
684 if (IS_ERR(local_root)) {
685 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200686 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000687 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200688
689 key.type = BTRFS_INODE_ITEM_KEY;
690 key.objectid = inum;
691 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000692 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
693 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200694 if (IS_ERR(inode))
695 return PTR_ERR(inode);
696
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200697 index = offset >> PAGE_CACHE_SHIFT;
698
699 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200700 if (!page) {
701 ret = -ENOMEM;
702 goto out;
703 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200704
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200705 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200706 if (PageDirty(page)) {
707 /*
708 * we need to write the data to the defect sector. the
709 * data that was in that sector is not in memory,
710 * because the page was modified. we must not write the
711 * modified page to that sector.
712 *
713 * TODO: what could be done here: wait for the delalloc
714 * runner to write out that page (might involve
715 * COW) and see whether the sector is still
716 * referenced afterwards.
717 *
718 * For the meantime, we'll treat this error
719 * incorrectable, although there is a chance that a
720 * later scrub will find the bad sector again and that
721 * there's no dirty page in memory, then.
722 */
723 ret = -EIO;
724 goto out;
725 }
Miao Xie1203b682014-09-12 18:44:01 +0800726 ret = repair_io_failure(inode, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200727 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800728 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200729 fixup->mirror_num);
730 unlock_page(page);
731 corrected = !ret;
732 } else {
733 /*
734 * we need to get good data first. the general readpage path
735 * will call repair_io_failure for us, we just have to make
736 * sure we read the bad mirror.
737 */
738 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
739 EXTENT_DAMAGED, GFP_NOFS);
740 if (ret) {
741 /* set_extent_bits should give proper error */
742 WARN_ON(ret > 0);
743 if (ret > 0)
744 ret = -EFAULT;
745 goto out;
746 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200747
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200748 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
749 btrfs_get_extent,
750 fixup->mirror_num);
751 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200752
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200753 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
754 end, EXTENT_DAMAGED, 0, NULL);
755 if (!corrected)
756 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
757 EXTENT_DAMAGED, GFP_NOFS);
758 }
759
760out:
761 if (page)
762 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200763
764 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200765
766 if (ret < 0)
767 return ret;
768
769 if (ret == 0 && corrected) {
770 /*
771 * we only need to call readpage for one of the inodes belonging
772 * to this extent. so make iterate_extent_inodes stop
773 */
774 return 1;
775 }
776
777 return -EIO;
778}
779
780static void scrub_fixup_nodatasum(struct btrfs_work *work)
781{
782 int ret;
783 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100784 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200785 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200786 struct btrfs_path *path;
787 int uncorrectable = 0;
788
789 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100790 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200791
792 path = btrfs_alloc_path();
793 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100794 spin_lock(&sctx->stat_lock);
795 ++sctx->stat.malloc_errors;
796 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200797 uncorrectable = 1;
798 goto out;
799 }
800
801 trans = btrfs_join_transaction(fixup->root);
802 if (IS_ERR(trans)) {
803 uncorrectable = 1;
804 goto out;
805 }
806
807 /*
808 * the idea is to trigger a regular read through the standard path. we
809 * read a page from the (failed) logical address by specifying the
810 * corresponding copynum of the failed sector. thus, that readpage is
811 * expected to fail.
812 * that is the point where on-the-fly error correction will kick in
813 * (once it's finished) and rewrite the failed sector if a good copy
814 * can be found.
815 */
816 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
817 path, scrub_fixup_readpage,
818 fixup);
819 if (ret < 0) {
820 uncorrectable = 1;
821 goto out;
822 }
823 WARN_ON(ret != 1);
824
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100825 spin_lock(&sctx->stat_lock);
826 ++sctx->stat.corrected_errors;
827 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200828
829out:
830 if (trans && !IS_ERR(trans))
831 btrfs_end_transaction(trans, fixup->root);
832 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100833 spin_lock(&sctx->stat_lock);
834 ++sctx->stat.uncorrectable_errors;
835 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100836 btrfs_dev_replace_stats_inc(
837 &sctx->dev_root->fs_info->dev_replace.
838 num_uncorrectable_read_errors);
Frank Holtonefe120a2013-12-20 11:37:06 -0500839 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
840 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200841 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200842 }
843
844 btrfs_free_path(path);
845 kfree(fixup);
846
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100847 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200848}
849
Miao Xieaf8e2d12014-10-23 14:42:50 +0800850static inline void scrub_get_recover(struct scrub_recover *recover)
851{
852 atomic_inc(&recover->refs);
853}
854
855static inline void scrub_put_recover(struct scrub_recover *recover)
856{
857 if (atomic_dec_and_test(&recover->refs)) {
858 kfree(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800859 kfree(recover);
860 }
861}
862
Arne Jansena2de7332011-03-08 14:14:00 +0100863/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400864 * scrub_handle_errored_block gets called when either verification of the
865 * pages failed or the bio failed to read, e.g. with EIO. In the latter
866 * case, this function handles all pages in the bio, even though only one
867 * may be bad.
868 * The goal of this function is to repair the errored block by using the
869 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100870 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400871static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100872{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100873 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100874 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400875 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100876 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400877 u64 logical;
878 u64 generation;
879 unsigned int failed_mirror_index;
880 unsigned int is_metadata;
881 unsigned int have_csum;
882 u8 *csum;
883 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
884 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100885 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400886 int mirror_index;
887 int page_num;
888 int success;
889 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
890 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100891
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400892 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100893 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000894 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
895 /*
896 * if we find an error in a super block, we just report it.
897 * They will get written with the next transaction commit
898 * anyway
899 */
900 spin_lock(&sctx->stat_lock);
901 ++sctx->stat.super_errors;
902 spin_unlock(&sctx->stat_lock);
903 return 0;
904 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400905 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100906 logical = sblock_to_check->pagev[0]->logical;
907 generation = sblock_to_check->pagev[0]->generation;
908 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
909 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
910 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400911 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100912 have_csum = sblock_to_check->pagev[0]->have_csum;
913 csum = sblock_to_check->pagev[0]->csum;
914 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400915
Stefan Behrensff023aa2012-11-06 11:43:11 +0100916 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
917 sblocks_for_recheck = NULL;
918 goto nodatasum_case;
919 }
920
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400921 /*
922 * read all mirrors one after the other. This includes to
923 * re-read the extent or metadata block that failed (that was
924 * the cause that this fixup code is called) another time,
925 * page by page this time in order to know which pages
926 * caused I/O errors and which ones are good (for all mirrors).
927 * It is the goal to handle the situation when more than one
928 * mirror contains I/O errors, but the errors do not
929 * overlap, i.e. the data can be repaired by selecting the
930 * pages from those mirrors without I/O error on the
931 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
932 * would be that mirror #1 has an I/O error on the first page,
933 * the second page is good, and mirror #2 has an I/O error on
934 * the second page, but the first page is good.
935 * Then the first page of the first mirror can be repaired by
936 * taking the first page of the second mirror, and the
937 * second page of the second mirror can be repaired by
938 * copying the contents of the 2nd page of the 1st mirror.
939 * One more note: if the pages of one mirror contain I/O
940 * errors, the checksum cannot be verified. In order to get
941 * the best data for repairing, the first attempt is to find
942 * a mirror without I/O errors and with a validated checksum.
943 * Only if this is not possible, the pages are picked from
944 * mirrors with I/O errors without considering the checksum.
945 * If the latter is the case, at the end, the checksum of the
946 * repaired area is verified in order to correctly maintain
947 * the statistics.
948 */
949
950 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
951 sizeof(*sblocks_for_recheck),
952 GFP_NOFS);
953 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100954 spin_lock(&sctx->stat_lock);
955 sctx->stat.malloc_errors++;
956 sctx->stat.read_errors++;
957 sctx->stat.uncorrectable_errors++;
958 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100959 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400960 goto out;
961 }
962
963 /* setup the context, map the logical blocks and alloc the pages */
Stefan Behrensff023aa2012-11-06 11:43:11 +0100964 ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400965 logical, sblocks_for_recheck);
966 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100967 spin_lock(&sctx->stat_lock);
968 sctx->stat.read_errors++;
969 sctx->stat.uncorrectable_errors++;
970 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100971 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400972 goto out;
973 }
974 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
975 sblock_bad = sblocks_for_recheck + failed_mirror_index;
976
977 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100978 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800979 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400980
981 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
982 sblock_bad->no_io_error_seen) {
983 /*
984 * the error disappeared after reading page by page, or
985 * the area was part of a huge bio and other parts of the
986 * bio caused I/O errors, or the block layer merged several
987 * read requests into one and the error is caused by a
988 * different bio (usually one of the two latter cases is
989 * the cause)
990 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100991 spin_lock(&sctx->stat_lock);
992 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800993 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100994 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400995
Stefan Behrensff023aa2012-11-06 11:43:11 +0100996 if (sctx->is_dev_replace)
997 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400998 goto out;
999 }
1000
1001 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001002 spin_lock(&sctx->stat_lock);
1003 sctx->stat.read_errors++;
1004 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001005 if (__ratelimit(&_rs))
1006 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001007 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001008 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001009 spin_lock(&sctx->stat_lock);
1010 sctx->stat.csum_errors++;
1011 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001012 if (__ratelimit(&_rs))
1013 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001014 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001015 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001016 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001017 spin_lock(&sctx->stat_lock);
1018 sctx->stat.verify_errors++;
1019 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001020 if (__ratelimit(&_rs))
1021 scrub_print_warning("checksum/header error",
1022 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001023 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001024 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001025 BTRFS_DEV_STAT_GENERATION_ERRS);
1026 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001027 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001028 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001029 }
1030
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001031 if (sctx->readonly) {
1032 ASSERT(!sctx->is_dev_replace);
1033 goto out;
1034 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001035
1036 if (!is_metadata && !have_csum) {
1037 struct scrub_fixup_nodatasum *fixup_nodatasum;
1038
Stefan Behrensff023aa2012-11-06 11:43:11 +01001039nodatasum_case:
1040 WARN_ON(sctx->is_dev_replace);
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);
1100 } else {
1101 int force_write = is_metadata || have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001102
Stefan Behrensff023aa2012-11-06 11:43:11 +01001103 ret = scrub_repair_block_from_good_copy(
1104 sblock_bad, sblock_other,
1105 force_write);
1106 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001107 if (0 == ret)
1108 goto corrected_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001109 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001110 }
1111
1112 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001113 * for dev_replace, pick good pages and write to the target device.
1114 */
1115 if (sctx->is_dev_replace) {
1116 success = 1;
1117 for (page_num = 0; page_num < sblock_bad->page_count;
1118 page_num++) {
1119 int sub_success;
1120
1121 sub_success = 0;
1122 for (mirror_index = 0;
1123 mirror_index < BTRFS_MAX_MIRRORS &&
1124 sblocks_for_recheck[mirror_index].page_count > 0;
1125 mirror_index++) {
1126 struct scrub_block *sblock_other =
1127 sblocks_for_recheck + mirror_index;
1128 struct scrub_page *page_other =
1129 sblock_other->pagev[page_num];
1130
1131 if (!page_other->io_error) {
1132 ret = scrub_write_page_to_dev_replace(
1133 sblock_other, page_num);
1134 if (ret == 0) {
1135 /* succeeded for this page */
1136 sub_success = 1;
1137 break;
1138 } else {
1139 btrfs_dev_replace_stats_inc(
1140 &sctx->dev_root->
1141 fs_info->dev_replace.
1142 num_write_errors);
1143 }
1144 }
1145 }
1146
1147 if (!sub_success) {
1148 /*
1149 * did not find a mirror to fetch the page
1150 * from. scrub_write_page_to_dev_replace()
1151 * handles this case (page->io_error), by
1152 * filling the block with zeros before
1153 * submitting the write request
1154 */
1155 success = 0;
1156 ret = scrub_write_page_to_dev_replace(
1157 sblock_bad, page_num);
1158 if (ret)
1159 btrfs_dev_replace_stats_inc(
1160 &sctx->dev_root->fs_info->
1161 dev_replace.num_write_errors);
1162 }
1163 }
1164
1165 goto out;
1166 }
1167
1168 /*
1169 * for regular scrub, repair those pages that are errored.
1170 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001171 * repaired, continue by picking good copies of those pages.
1172 * Select the good pages from mirrors to rewrite bad pages from
1173 * the area to fix. Afterwards verify the checksum of the block
1174 * that is supposed to be repaired. This verification step is
1175 * only done for the purpose of statistic counting and for the
1176 * final scrub report, whether errors remain.
1177 * A perfect algorithm could make use of the checksum and try
1178 * all possible combinations of pages from the different mirrors
1179 * until the checksum verification succeeds. For example, when
1180 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1181 * of mirror #2 is readable but the final checksum test fails,
1182 * then the 2nd page of mirror #3 could be tried, whether now
1183 * the final checksum succeedes. But this would be a rare
1184 * exception and is therefore not implemented. At least it is
1185 * avoided that the good copy is overwritten.
1186 * A more useful improvement would be to pick the sectors
1187 * without I/O error based on sector sizes (512 bytes on legacy
1188 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1189 * mirror could be repaired by taking 512 byte of a different
1190 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1191 * area are unreadable.
1192 */
1193
1194 /* can only fix I/O errors from here on */
1195 if (sblock_bad->no_io_error_seen)
1196 goto did_not_correct_error;
1197
1198 success = 1;
1199 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001200 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001201
1202 if (!page_bad->io_error)
1203 continue;
1204
1205 for (mirror_index = 0;
1206 mirror_index < BTRFS_MAX_MIRRORS &&
1207 sblocks_for_recheck[mirror_index].page_count > 0;
1208 mirror_index++) {
1209 struct scrub_block *sblock_other = sblocks_for_recheck +
1210 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001211 struct scrub_page *page_other = sblock_other->pagev[
1212 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001213
1214 if (!page_other->io_error) {
1215 ret = scrub_repair_page_from_good_copy(
1216 sblock_bad, sblock_other, page_num, 0);
1217 if (0 == ret) {
1218 page_bad->io_error = 0;
1219 break; /* succeeded for this page */
1220 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001221 }
1222 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001223
1224 if (page_bad->io_error) {
1225 /* did not find a mirror to copy the page from */
1226 success = 0;
1227 }
1228 }
1229
1230 if (success) {
1231 if (is_metadata || have_csum) {
1232 /*
1233 * need to verify the checksum now that all
1234 * sectors on disk are repaired (the write
1235 * request for data to be repaired is on its way).
1236 * Just be lazy and use scrub_recheck_block()
1237 * which re-reads the data before the checksum
1238 * is verified, but most likely the data comes out
1239 * of the page cache.
1240 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001241 scrub_recheck_block(fs_info, sblock_bad,
1242 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001243 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001244 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001245 !sblock_bad->checksum_error &&
1246 sblock_bad->no_io_error_seen)
1247 goto corrected_error;
1248 else
1249 goto did_not_correct_error;
1250 } else {
1251corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001252 spin_lock(&sctx->stat_lock);
1253 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001254 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001255 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001256 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001257 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001258 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001259 }
1260 } else {
1261did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001262 spin_lock(&sctx->stat_lock);
1263 sctx->stat.uncorrectable_errors++;
1264 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001265 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001266 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001267 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001268 }
1269
1270out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001271 if (sblocks_for_recheck) {
1272 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1273 mirror_index++) {
1274 struct scrub_block *sblock = sblocks_for_recheck +
1275 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001276 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001277 int page_index;
1278
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001279 for (page_index = 0; page_index < sblock->page_count;
1280 page_index++) {
1281 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001282 recover = sblock->pagev[page_index]->recover;
1283 if (recover) {
1284 scrub_put_recover(recover);
1285 sblock->pagev[page_index]->recover =
1286 NULL;
1287 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001288 scrub_page_put(sblock->pagev[page_index]);
1289 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001290 }
1291 kfree(sblocks_for_recheck);
1292 }
1293
1294 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001295}
1296
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001297static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001298{
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001299 if (bbio->raid_map) {
Zhao Leie34c3302015-01-20 15:11:31 +08001300 int real_stripes = bbio->num_stripes - bbio->num_tgtdevs;
1301
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001302 if (bbio->raid_map[real_stripes - 1] == RAID6_Q_STRIPE)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001303 return 3;
1304 else
1305 return 2;
1306 } else {
1307 return (int)bbio->num_stripes;
1308 }
1309}
1310
1311static inline void scrub_stripe_index_and_offset(u64 logical, u64 *raid_map,
1312 u64 mapped_length,
1313 int nstripes, int mirror,
1314 int *stripe_index,
1315 u64 *stripe_offset)
1316{
1317 int i;
1318
1319 if (raid_map) {
1320 /* RAID5/6 */
1321 for (i = 0; i < nstripes; i++) {
1322 if (raid_map[i] == RAID6_Q_STRIPE ||
1323 raid_map[i] == RAID5_P_STRIPE)
1324 continue;
1325
1326 if (logical >= raid_map[i] &&
1327 logical < raid_map[i] + mapped_length)
1328 break;
1329 }
1330
1331 *stripe_index = i;
1332 *stripe_offset = logical - raid_map[i];
1333 } else {
1334 /* The other RAID type */
1335 *stripe_index = mirror;
1336 *stripe_offset = 0;
1337 }
1338}
1339
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001340static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001341 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001342 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001343 u64 length, u64 logical,
1344 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001345{
Miao Xieaf8e2d12014-10-23 14:42:50 +08001346 struct scrub_recover *recover;
1347 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001348 u64 sublen;
1349 u64 mapped_length;
1350 u64 stripe_offset;
1351 int stripe_index;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001352 int page_index;
1353 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001354 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001355 int ret;
1356
1357 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001358 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001359 * are not used (and not set) in the blocks that are used for
1360 * the recheck procedure
1361 */
1362
1363 page_index = 0;
1364 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001365 sublen = min_t(u64, length, PAGE_SIZE);
1366 mapped_length = sublen;
1367 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001368
1369 /*
1370 * with a length of PAGE_SIZE, each returned stripe
1371 * represents one mirror
1372 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001373 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001374 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001375 if (ret || !bbio || mapped_length < sublen) {
1376 kfree(bbio);
1377 return -EIO;
1378 }
1379
Miao Xieaf8e2d12014-10-23 14:42:50 +08001380 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1381 if (!recover) {
1382 kfree(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001383 return -ENOMEM;
1384 }
1385
1386 atomic_set(&recover->refs, 1);
1387 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001388 recover->map_length = mapped_length;
1389
Stefan Behrensff023aa2012-11-06 11:43:11 +01001390 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001391
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001392 nmirrors = scrub_nr_raid_mirrors(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001393 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001394 mirror_index++) {
1395 struct scrub_block *sblock;
1396 struct scrub_page *page;
1397
1398 if (mirror_index >= BTRFS_MAX_MIRRORS)
1399 continue;
1400
1401 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001402 sblock->sctx = sctx;
1403 page = kzalloc(sizeof(*page), GFP_NOFS);
1404 if (!page) {
1405leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001406 spin_lock(&sctx->stat_lock);
1407 sctx->stat.malloc_errors++;
1408 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001409 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001410 return -ENOMEM;
1411 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001412 scrub_page_get(page);
1413 sblock->pagev[page_index] = page;
1414 page->logical = logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001415
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001416 scrub_stripe_index_and_offset(logical, bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001417 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001418 bbio->num_stripes -
1419 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001420 mirror_index,
1421 &stripe_index,
1422 &stripe_offset);
1423 page->physical = bbio->stripes[stripe_index].physical +
1424 stripe_offset;
1425 page->dev = bbio->stripes[stripe_index].dev;
1426
Stefan Behrensff023aa2012-11-06 11:43:11 +01001427 BUG_ON(page_index >= original_sblock->page_count);
1428 page->physical_for_dev_replace =
1429 original_sblock->pagev[page_index]->
1430 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001431 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001432 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001433 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001434 page->page = alloc_page(GFP_NOFS);
1435 if (!page->page)
1436 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001437
1438 scrub_get_recover(recover);
1439 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001440 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001441 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001442 length -= sublen;
1443 logical += sublen;
1444 page_index++;
1445 }
1446
1447 return 0;
1448}
1449
Miao Xieaf8e2d12014-10-23 14:42:50 +08001450struct scrub_bio_ret {
1451 struct completion event;
1452 int error;
1453};
1454
1455static void scrub_bio_wait_endio(struct bio *bio, int error)
1456{
1457 struct scrub_bio_ret *ret = bio->bi_private;
1458
1459 ret->error = error;
1460 complete(&ret->event);
1461}
1462
1463static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1464{
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001465 return page->recover && page->recover->bbio->raid_map;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001466}
1467
1468static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1469 struct bio *bio,
1470 struct scrub_page *page)
1471{
1472 struct scrub_bio_ret done;
1473 int ret;
1474
1475 init_completion(&done.event);
1476 done.error = 0;
1477 bio->bi_iter.bi_sector = page->logical >> 9;
1478 bio->bi_private = &done;
1479 bio->bi_end_io = scrub_bio_wait_endio;
1480
1481 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001482 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001483 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001484 if (ret)
1485 return ret;
1486
1487 wait_for_completion(&done.event);
1488 if (done.error)
1489 return -EIO;
1490
1491 return 0;
1492}
1493
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001494/*
1495 * this function will check the on disk data for checksum errors, header
1496 * errors and read I/O errors. If any I/O errors happen, the exact pages
1497 * which are errored are marked as being bad. The goal is to enable scrub
1498 * to take those pages that are not errored from all the mirrors so that
1499 * the pages that are errored in the just handled mirror can be repaired.
1500 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001501static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1502 struct scrub_block *sblock, int is_metadata,
1503 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001504 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001505{
1506 int page_num;
1507
1508 sblock->no_io_error_seen = 1;
1509 sblock->header_error = 0;
1510 sblock->checksum_error = 0;
1511
1512 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1513 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001514 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001515
Stefan Behrens442a4f62012-05-25 16:06:08 +02001516 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001517 page->io_error = 1;
1518 sblock->no_io_error_seen = 0;
1519 continue;
1520 }
1521
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001522 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001523 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001524 if (!bio) {
1525 page->io_error = 1;
1526 sblock->no_io_error_seen = 0;
1527 continue;
1528 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001529 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001530
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001531 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001532 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1533 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1534 sblock->no_io_error_seen = 0;
1535 } else {
1536 bio->bi_iter.bi_sector = page->physical >> 9;
1537
1538 if (btrfsic_submit_bio_wait(READ, bio))
1539 sblock->no_io_error_seen = 0;
1540 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001541
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001542 bio_put(bio);
1543 }
1544
1545 if (sblock->no_io_error_seen)
1546 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1547 have_csum, csum, generation,
1548 csum_size);
1549
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001550 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001551}
1552
Miao Xie17a9be22014-07-24 11:37:08 +08001553static inline int scrub_check_fsid(u8 fsid[],
1554 struct scrub_page *spage)
1555{
1556 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1557 int ret;
1558
1559 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1560 return !ret;
1561}
1562
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001563static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1564 struct scrub_block *sblock,
1565 int is_metadata, int have_csum,
1566 const u8 *csum, u64 generation,
1567 u16 csum_size)
1568{
1569 int page_num;
1570 u8 calculated_csum[BTRFS_CSUM_SIZE];
1571 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001572 void *mapped_buffer;
1573
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001574 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001575 if (is_metadata) {
1576 struct btrfs_header *h;
1577
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001578 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001579 h = (struct btrfs_header *)mapped_buffer;
1580
Qu Wenruo3cae2102013-07-16 11:19:18 +08001581 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001582 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001583 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001584 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001585 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001586 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001587 sblock->header_error = 1;
1588 sblock->generation_error = 1;
1589 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001590 csum = h->csum;
1591 } else {
1592 if (!have_csum)
1593 return;
1594
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001595 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001596 }
1597
1598 for (page_num = 0;;) {
1599 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001600 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001601 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1602 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1603 else
Liu Bob0496682013-03-14 14:57:45 +00001604 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001605
Linus Torvalds9613beb2012-03-30 12:44:29 -07001606 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001607 page_num++;
1608 if (page_num >= sblock->page_count)
1609 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001610 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001611
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001612 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001613 }
1614
1615 btrfs_csum_final(crc, calculated_csum);
1616 if (memcmp(calculated_csum, csum, csum_size))
1617 sblock->checksum_error = 1;
1618}
1619
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001620static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1621 struct scrub_block *sblock_good,
1622 int force_write)
1623{
1624 int page_num;
1625 int ret = 0;
1626
1627 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1628 int ret_sub;
1629
1630 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1631 sblock_good,
1632 page_num,
1633 force_write);
1634 if (ret_sub)
1635 ret = ret_sub;
1636 }
1637
1638 return ret;
1639}
1640
1641static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1642 struct scrub_block *sblock_good,
1643 int page_num, int force_write)
1644{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001645 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1646 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001647
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001648 BUG_ON(page_bad->page == NULL);
1649 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001650 if (force_write || sblock_bad->header_error ||
1651 sblock_bad->checksum_error || page_bad->io_error) {
1652 struct bio *bio;
1653 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001654
Stefan Behrensff023aa2012-11-06 11:43:11 +01001655 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001656 printk_ratelimited(KERN_WARNING "BTRFS: "
1657 "scrub_repair_page_from_good_copy(bdev == NULL) "
1658 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001659 return -EIO;
1660 }
1661
Chris Mason9be33952013-05-17 18:30:14 -04001662 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001663 if (!bio)
1664 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001665 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001666 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001667
1668 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1669 if (PAGE_SIZE != ret) {
1670 bio_put(bio);
1671 return -EIO;
1672 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001673
Kent Overstreet33879d42013-11-23 22:33:32 -08001674 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001675 btrfs_dev_stat_inc_and_print(page_bad->dev,
1676 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001677 btrfs_dev_replace_stats_inc(
1678 &sblock_bad->sctx->dev_root->fs_info->
1679 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001680 bio_put(bio);
1681 return -EIO;
1682 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001683 bio_put(bio);
1684 }
1685
1686 return 0;
1687}
1688
Stefan Behrensff023aa2012-11-06 11:43:11 +01001689static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1690{
1691 int page_num;
1692
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001693 /*
1694 * This block is used for the check of the parity on the source device,
1695 * so the data needn't be written into the destination device.
1696 */
1697 if (sblock->sparity)
1698 return;
1699
Stefan Behrensff023aa2012-11-06 11:43:11 +01001700 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1701 int ret;
1702
1703 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1704 if (ret)
1705 btrfs_dev_replace_stats_inc(
1706 &sblock->sctx->dev_root->fs_info->dev_replace.
1707 num_write_errors);
1708 }
1709}
1710
1711static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1712 int page_num)
1713{
1714 struct scrub_page *spage = sblock->pagev[page_num];
1715
1716 BUG_ON(spage->page == NULL);
1717 if (spage->io_error) {
1718 void *mapped_buffer = kmap_atomic(spage->page);
1719
1720 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1721 flush_dcache_page(spage->page);
1722 kunmap_atomic(mapped_buffer);
1723 }
1724 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1725}
1726
1727static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1728 struct scrub_page *spage)
1729{
1730 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1731 struct scrub_bio *sbio;
1732 int ret;
1733
1734 mutex_lock(&wr_ctx->wr_lock);
1735again:
1736 if (!wr_ctx->wr_curr_bio) {
1737 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1738 GFP_NOFS);
1739 if (!wr_ctx->wr_curr_bio) {
1740 mutex_unlock(&wr_ctx->wr_lock);
1741 return -ENOMEM;
1742 }
1743 wr_ctx->wr_curr_bio->sctx = sctx;
1744 wr_ctx->wr_curr_bio->page_count = 0;
1745 }
1746 sbio = wr_ctx->wr_curr_bio;
1747 if (sbio->page_count == 0) {
1748 struct bio *bio;
1749
1750 sbio->physical = spage->physical_for_dev_replace;
1751 sbio->logical = spage->logical;
1752 sbio->dev = wr_ctx->tgtdev;
1753 bio = sbio->bio;
1754 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001755 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001756 if (!bio) {
1757 mutex_unlock(&wr_ctx->wr_lock);
1758 return -ENOMEM;
1759 }
1760 sbio->bio = bio;
1761 }
1762
1763 bio->bi_private = sbio;
1764 bio->bi_end_io = scrub_wr_bio_end_io;
1765 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001766 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001767 sbio->err = 0;
1768 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1769 spage->physical_for_dev_replace ||
1770 sbio->logical + sbio->page_count * PAGE_SIZE !=
1771 spage->logical) {
1772 scrub_wr_submit(sctx);
1773 goto again;
1774 }
1775
1776 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1777 if (ret != PAGE_SIZE) {
1778 if (sbio->page_count < 1) {
1779 bio_put(sbio->bio);
1780 sbio->bio = NULL;
1781 mutex_unlock(&wr_ctx->wr_lock);
1782 return -EIO;
1783 }
1784 scrub_wr_submit(sctx);
1785 goto again;
1786 }
1787
1788 sbio->pagev[sbio->page_count] = spage;
1789 scrub_page_get(spage);
1790 sbio->page_count++;
1791 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1792 scrub_wr_submit(sctx);
1793 mutex_unlock(&wr_ctx->wr_lock);
1794
1795 return 0;
1796}
1797
1798static void scrub_wr_submit(struct scrub_ctx *sctx)
1799{
1800 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1801 struct scrub_bio *sbio;
1802
1803 if (!wr_ctx->wr_curr_bio)
1804 return;
1805
1806 sbio = wr_ctx->wr_curr_bio;
1807 wr_ctx->wr_curr_bio = NULL;
1808 WARN_ON(!sbio->bio->bi_bdev);
1809 scrub_pending_bio_inc(sctx);
1810 /* process all writes in a single worker thread. Then the block layer
1811 * orders the requests before sending them to the driver which
1812 * doubled the write performance on spinning disks when measured
1813 * with Linux 3.5 */
1814 btrfsic_submit_bio(WRITE, sbio->bio);
1815}
1816
1817static void scrub_wr_bio_end_io(struct bio *bio, int err)
1818{
1819 struct scrub_bio *sbio = bio->bi_private;
1820 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1821
1822 sbio->err = err;
1823 sbio->bio = bio;
1824
Liu Bo9e0af232014-08-15 23:36:53 +08001825 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1826 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001827 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001828}
1829
1830static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1831{
1832 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1833 struct scrub_ctx *sctx = sbio->sctx;
1834 int i;
1835
1836 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1837 if (sbio->err) {
1838 struct btrfs_dev_replace *dev_replace =
1839 &sbio->sctx->dev_root->fs_info->dev_replace;
1840
1841 for (i = 0; i < sbio->page_count; i++) {
1842 struct scrub_page *spage = sbio->pagev[i];
1843
1844 spage->io_error = 1;
1845 btrfs_dev_replace_stats_inc(&dev_replace->
1846 num_write_errors);
1847 }
1848 }
1849
1850 for (i = 0; i < sbio->page_count; i++)
1851 scrub_page_put(sbio->pagev[i]);
1852
1853 bio_put(sbio->bio);
1854 kfree(sbio);
1855 scrub_pending_bio_dec(sctx);
1856}
1857
1858static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001859{
1860 u64 flags;
1861 int ret;
1862
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001863 WARN_ON(sblock->page_count < 1);
1864 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001865 ret = 0;
1866 if (flags & BTRFS_EXTENT_FLAG_DATA)
1867 ret = scrub_checksum_data(sblock);
1868 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1869 ret = scrub_checksum_tree_block(sblock);
1870 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1871 (void)scrub_checksum_super(sblock);
1872 else
1873 WARN_ON(1);
1874 if (ret)
1875 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001876
1877 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001878}
1879
1880static int scrub_checksum_data(struct scrub_block *sblock)
1881{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001882 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001883 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001884 u8 *on_disk_csum;
1885 struct page *page;
1886 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001887 u32 crc = ~(u32)0;
1888 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001889 u64 len;
1890 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001891
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001892 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001893 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001894 return 0;
1895
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001896 on_disk_csum = sblock->pagev[0]->csum;
1897 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001898 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001899
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001900 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001901 index = 0;
1902 for (;;) {
1903 u64 l = min_t(u64, len, PAGE_SIZE);
1904
Liu Bob0496682013-03-14 14:57:45 +00001905 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001906 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001907 len -= l;
1908 if (len == 0)
1909 break;
1910 index++;
1911 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001912 BUG_ON(!sblock->pagev[index]->page);
1913 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001914 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001915 }
1916
Arne Jansena2de7332011-03-08 14:14:00 +01001917 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001918 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001919 fail = 1;
1920
Arne Jansena2de7332011-03-08 14:14:00 +01001921 return fail;
1922}
1923
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001924static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001925{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001926 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001927 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001928 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001929 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001930 u8 calculated_csum[BTRFS_CSUM_SIZE];
1931 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1932 struct page *page;
1933 void *mapped_buffer;
1934 u64 mapped_size;
1935 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001936 u32 crc = ~(u32)0;
1937 int fail = 0;
1938 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001939 u64 len;
1940 int index;
1941
1942 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001943 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001944 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001945 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001946 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001947
1948 /*
1949 * we don't use the getter functions here, as we
1950 * a) don't have an extent buffer and
1951 * b) the page is already kmapped
1952 */
Arne Jansena2de7332011-03-08 14:14:00 +01001953
Qu Wenruo3cae2102013-07-16 11:19:18 +08001954 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001955 ++fail;
1956
Qu Wenruo3cae2102013-07-16 11:19:18 +08001957 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001958 ++fail;
1959
Miao Xie17a9be22014-07-24 11:37:08 +08001960 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001961 ++fail;
1962
1963 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1964 BTRFS_UUID_SIZE))
1965 ++fail;
1966
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001967 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001968 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1969 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1970 index = 0;
1971 for (;;) {
1972 u64 l = min_t(u64, len, mapped_size);
1973
Liu Bob0496682013-03-14 14:57:45 +00001974 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001975 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001976 len -= l;
1977 if (len == 0)
1978 break;
1979 index++;
1980 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001981 BUG_ON(!sblock->pagev[index]->page);
1982 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001983 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001984 mapped_size = PAGE_SIZE;
1985 p = mapped_buffer;
1986 }
1987
1988 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001989 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001990 ++crc_fail;
1991
Arne Jansena2de7332011-03-08 14:14:00 +01001992 return fail || crc_fail;
1993}
1994
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001995static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001996{
1997 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001998 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001999 u8 calculated_csum[BTRFS_CSUM_SIZE];
2000 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2001 struct page *page;
2002 void *mapped_buffer;
2003 u64 mapped_size;
2004 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002005 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002006 int fail_gen = 0;
2007 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002008 u64 len;
2009 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002010
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002011 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002012 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002013 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002014 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002015 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002016
Qu Wenruo3cae2102013-07-16 11:19:18 +08002017 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002018 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002019
Qu Wenruo3cae2102013-07-16 11:19:18 +08002020 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002021 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002022
Miao Xie17a9be22014-07-24 11:37:08 +08002023 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002024 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002025
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002026 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2027 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2028 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2029 index = 0;
2030 for (;;) {
2031 u64 l = min_t(u64, len, mapped_size);
2032
Liu Bob0496682013-03-14 14:57:45 +00002033 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002034 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002035 len -= l;
2036 if (len == 0)
2037 break;
2038 index++;
2039 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002040 BUG_ON(!sblock->pagev[index]->page);
2041 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002042 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002043 mapped_size = PAGE_SIZE;
2044 p = mapped_buffer;
2045 }
2046
2047 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002048 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002049 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002050
Stefan Behrens442a4f62012-05-25 16:06:08 +02002051 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002052 /*
2053 * if we find an error in a super block, we just report it.
2054 * They will get written with the next transaction commit
2055 * anyway
2056 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002057 spin_lock(&sctx->stat_lock);
2058 ++sctx->stat.super_errors;
2059 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002060 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002061 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002062 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2063 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002064 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002065 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002066 }
2067
Stefan Behrens442a4f62012-05-25 16:06:08 +02002068 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002069}
2070
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002071static void scrub_block_get(struct scrub_block *sblock)
2072{
2073 atomic_inc(&sblock->ref_count);
2074}
2075
2076static void scrub_block_put(struct scrub_block *sblock)
2077{
2078 if (atomic_dec_and_test(&sblock->ref_count)) {
2079 int i;
2080
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002081 if (sblock->sparity)
2082 scrub_parity_put(sblock->sparity);
2083
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002084 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002085 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002086 kfree(sblock);
2087 }
2088}
2089
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002090static void scrub_page_get(struct scrub_page *spage)
2091{
2092 atomic_inc(&spage->ref_count);
2093}
2094
2095static void scrub_page_put(struct scrub_page *spage)
2096{
2097 if (atomic_dec_and_test(&spage->ref_count)) {
2098 if (spage->page)
2099 __free_page(spage->page);
2100 kfree(spage);
2101 }
2102}
2103
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002104static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002105{
2106 struct scrub_bio *sbio;
2107
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002108 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002109 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002110
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002111 sbio = sctx->bios[sctx->curr];
2112 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002113 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002114
Stefan Behrensff023aa2012-11-06 11:43:11 +01002115 if (!sbio->bio->bi_bdev) {
2116 /*
2117 * this case should not happen. If btrfs_map_block() is
2118 * wrong, it could happen for dev-replace operations on
2119 * missing devices when no mirrors are available, but in
2120 * this case it should already fail the mount.
2121 * This case is handled correctly (but _very_ slowly).
2122 */
2123 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05002124 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01002125 bio_endio(sbio->bio, -EIO);
2126 } else {
2127 btrfsic_submit_bio(READ, sbio->bio);
2128 }
Arne Jansena2de7332011-03-08 14:14:00 +01002129}
2130
Stefan Behrensff023aa2012-11-06 11:43:11 +01002131static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2132 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002133{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002134 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002135 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002136 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002137
2138again:
2139 /*
2140 * grab a fresh bio or wait for one to become available
2141 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002142 while (sctx->curr == -1) {
2143 spin_lock(&sctx->list_lock);
2144 sctx->curr = sctx->first_free;
2145 if (sctx->curr != -1) {
2146 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2147 sctx->bios[sctx->curr]->next_free = -1;
2148 sctx->bios[sctx->curr]->page_count = 0;
2149 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002150 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002151 spin_unlock(&sctx->list_lock);
2152 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002153 }
2154 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002155 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002156 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002157 struct bio *bio;
2158
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002159 sbio->physical = spage->physical;
2160 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002161 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002162 bio = sbio->bio;
2163 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002164 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002165 if (!bio)
2166 return -ENOMEM;
2167 sbio->bio = bio;
2168 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002169
2170 bio->bi_private = sbio;
2171 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002172 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002173 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002174 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002175 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2176 spage->physical ||
2177 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002178 spage->logical ||
2179 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002180 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002181 goto again;
2182 }
2183
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002184 sbio->pagev[sbio->page_count] = spage;
2185 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2186 if (ret != PAGE_SIZE) {
2187 if (sbio->page_count < 1) {
2188 bio_put(sbio->bio);
2189 sbio->bio = NULL;
2190 return -EIO;
2191 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002192 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002193 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002194 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002195
Stefan Behrensff023aa2012-11-06 11:43:11 +01002196 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002197 atomic_inc(&sblock->outstanding_pages);
2198 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002199 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002200 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002201
2202 return 0;
2203}
2204
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002205static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002206 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002207 u64 gen, int mirror_num, u8 *csum, int force,
2208 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002209{
2210 struct scrub_block *sblock;
2211 int index;
2212
2213 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2214 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002215 spin_lock(&sctx->stat_lock);
2216 sctx->stat.malloc_errors++;
2217 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002218 return -ENOMEM;
2219 }
2220
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002221 /* one ref inside this function, plus one for each page added to
2222 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002223 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002224 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002225 sblock->no_io_error_seen = 1;
2226
2227 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002228 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002229 u64 l = min_t(u64, len, PAGE_SIZE);
2230
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002231 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2232 if (!spage) {
2233leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002234 spin_lock(&sctx->stat_lock);
2235 sctx->stat.malloc_errors++;
2236 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002237 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002238 return -ENOMEM;
2239 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002240 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2241 scrub_page_get(spage);
2242 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002243 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002244 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002245 spage->flags = flags;
2246 spage->generation = gen;
2247 spage->logical = logical;
2248 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002249 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002250 spage->mirror_num = mirror_num;
2251 if (csum) {
2252 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002253 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002254 } else {
2255 spage->have_csum = 0;
2256 }
2257 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002258 spage->page = alloc_page(GFP_NOFS);
2259 if (!spage->page)
2260 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002261 len -= l;
2262 logical += l;
2263 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002264 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002265 }
2266
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002267 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002268 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002269 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002270 int ret;
2271
Stefan Behrensff023aa2012-11-06 11:43:11 +01002272 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002273 if (ret) {
2274 scrub_block_put(sblock);
2275 return ret;
2276 }
2277 }
2278
2279 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002280 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002281
2282 /* last one frees, either here or in bio completion for last page */
2283 scrub_block_put(sblock);
2284 return 0;
2285}
2286
2287static void scrub_bio_end_io(struct bio *bio, int err)
2288{
2289 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002290 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002291
2292 sbio->err = err;
2293 sbio->bio = bio;
2294
Qu Wenruo0339ef22014-02-28 10:46:17 +08002295 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002296}
2297
2298static void scrub_bio_end_io_worker(struct btrfs_work *work)
2299{
2300 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002301 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002302 int i;
2303
Stefan Behrensff023aa2012-11-06 11:43:11 +01002304 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002305 if (sbio->err) {
2306 for (i = 0; i < sbio->page_count; i++) {
2307 struct scrub_page *spage = sbio->pagev[i];
2308
2309 spage->io_error = 1;
2310 spage->sblock->no_io_error_seen = 0;
2311 }
2312 }
2313
2314 /* now complete the scrub_block items that have all pages completed */
2315 for (i = 0; i < sbio->page_count; i++) {
2316 struct scrub_page *spage = sbio->pagev[i];
2317 struct scrub_block *sblock = spage->sblock;
2318
2319 if (atomic_dec_and_test(&sblock->outstanding_pages))
2320 scrub_block_complete(sblock);
2321 scrub_block_put(sblock);
2322 }
2323
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002324 bio_put(sbio->bio);
2325 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002326 spin_lock(&sctx->list_lock);
2327 sbio->next_free = sctx->first_free;
2328 sctx->first_free = sbio->index;
2329 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002330
2331 if (sctx->is_dev_replace &&
2332 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2333 mutex_lock(&sctx->wr_ctx.wr_lock);
2334 scrub_wr_submit(sctx);
2335 mutex_unlock(&sctx->wr_ctx.wr_lock);
2336 }
2337
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002338 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002339}
2340
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002341static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2342 unsigned long *bitmap,
2343 u64 start, u64 len)
2344{
2345 int offset;
2346 int nsectors;
2347 int sectorsize = sparity->sctx->dev_root->sectorsize;
2348
2349 if (len >= sparity->stripe_len) {
2350 bitmap_set(bitmap, 0, sparity->nsectors);
2351 return;
2352 }
2353
2354 start -= sparity->logic_start;
2355 offset = (int)do_div(start, sparity->stripe_len);
2356 offset /= sectorsize;
2357 nsectors = (int)len / sectorsize;
2358
2359 if (offset + nsectors <= sparity->nsectors) {
2360 bitmap_set(bitmap, offset, nsectors);
2361 return;
2362 }
2363
2364 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2365 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2366}
2367
2368static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2369 u64 start, u64 len)
2370{
2371 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2372}
2373
2374static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2375 u64 start, u64 len)
2376{
2377 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2378}
2379
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002380static void scrub_block_complete(struct scrub_block *sblock)
2381{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002382 int corrupted = 0;
2383
Stefan Behrensff023aa2012-11-06 11:43:11 +01002384 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002385 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002386 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002387 } else {
2388 /*
2389 * if has checksum error, write via repair mechanism in
2390 * dev replace case, otherwise write here in dev replace
2391 * case.
2392 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002393 corrupted = scrub_checksum(sblock);
2394 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002395 scrub_write_block_to_dev_replace(sblock);
2396 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002397
2398 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2399 u64 start = sblock->pagev[0]->logical;
2400 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2401 PAGE_SIZE;
2402
2403 scrub_parity_mark_sectors_error(sblock->sparity,
2404 start, end - start);
2405 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002406}
2407
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002408static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002409 u8 *csum)
2410{
2411 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002412 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002413 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002414
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002415 while (!list_empty(&sctx->csum_list)) {
2416 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002417 struct btrfs_ordered_sum, list);
2418 if (sum->bytenr > logical)
2419 return 0;
2420 if (sum->bytenr + sum->len > logical)
2421 break;
2422
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002423 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002424 list_del(&sum->list);
2425 kfree(sum);
2426 sum = NULL;
2427 }
2428 if (!sum)
2429 return 0;
2430
Miao Xief51a4a12013-06-19 10:36:09 +08002431 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002432 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002433 memcpy(csum, sum->sums + index, sctx->csum_size);
2434 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002435 list_del(&sum->list);
2436 kfree(sum);
2437 }
Miao Xief51a4a12013-06-19 10:36:09 +08002438 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002439}
2440
2441/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002442static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002443 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002444 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002445{
2446 int ret;
2447 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002448 u32 blocksize;
2449
2450 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002451 blocksize = sctx->sectorsize;
2452 spin_lock(&sctx->stat_lock);
2453 sctx->stat.data_extents_scrubbed++;
2454 sctx->stat.data_bytes_scrubbed += len;
2455 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002456 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002457 blocksize = sctx->nodesize;
2458 spin_lock(&sctx->stat_lock);
2459 sctx->stat.tree_extents_scrubbed++;
2460 sctx->stat.tree_bytes_scrubbed += len;
2461 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002462 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002463 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002464 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002465 }
Arne Jansena2de7332011-03-08 14:14:00 +01002466
2467 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002468 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002469 int have_csum = 0;
2470
2471 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2472 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002473 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002474 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002475 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002476 if (sctx->is_dev_replace && !have_csum) {
2477 ret = copy_nocow_pages(sctx, logical, l,
2478 mirror_num,
2479 physical_for_dev_replace);
2480 goto behind_scrub_pages;
2481 }
Arne Jansena2de7332011-03-08 14:14:00 +01002482 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002483 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002484 mirror_num, have_csum ? csum : NULL, 0,
2485 physical_for_dev_replace);
2486behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002487 if (ret)
2488 return ret;
2489 len -= l;
2490 logical += l;
2491 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002492 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002493 }
2494 return 0;
2495}
2496
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002497static int scrub_pages_for_parity(struct scrub_parity *sparity,
2498 u64 logical, u64 len,
2499 u64 physical, struct btrfs_device *dev,
2500 u64 flags, u64 gen, int mirror_num, u8 *csum)
2501{
2502 struct scrub_ctx *sctx = sparity->sctx;
2503 struct scrub_block *sblock;
2504 int index;
2505
2506 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2507 if (!sblock) {
2508 spin_lock(&sctx->stat_lock);
2509 sctx->stat.malloc_errors++;
2510 spin_unlock(&sctx->stat_lock);
2511 return -ENOMEM;
2512 }
2513
2514 /* one ref inside this function, plus one for each page added to
2515 * a bio later on */
2516 atomic_set(&sblock->ref_count, 1);
2517 sblock->sctx = sctx;
2518 sblock->no_io_error_seen = 1;
2519 sblock->sparity = sparity;
2520 scrub_parity_get(sparity);
2521
2522 for (index = 0; len > 0; index++) {
2523 struct scrub_page *spage;
2524 u64 l = min_t(u64, len, PAGE_SIZE);
2525
2526 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2527 if (!spage) {
2528leave_nomem:
2529 spin_lock(&sctx->stat_lock);
2530 sctx->stat.malloc_errors++;
2531 spin_unlock(&sctx->stat_lock);
2532 scrub_block_put(sblock);
2533 return -ENOMEM;
2534 }
2535 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2536 /* For scrub block */
2537 scrub_page_get(spage);
2538 sblock->pagev[index] = spage;
2539 /* For scrub parity */
2540 scrub_page_get(spage);
2541 list_add_tail(&spage->list, &sparity->spages);
2542 spage->sblock = sblock;
2543 spage->dev = dev;
2544 spage->flags = flags;
2545 spage->generation = gen;
2546 spage->logical = logical;
2547 spage->physical = physical;
2548 spage->mirror_num = mirror_num;
2549 if (csum) {
2550 spage->have_csum = 1;
2551 memcpy(spage->csum, csum, sctx->csum_size);
2552 } else {
2553 spage->have_csum = 0;
2554 }
2555 sblock->page_count++;
2556 spage->page = alloc_page(GFP_NOFS);
2557 if (!spage->page)
2558 goto leave_nomem;
2559 len -= l;
2560 logical += l;
2561 physical += l;
2562 }
2563
2564 WARN_ON(sblock->page_count == 0);
2565 for (index = 0; index < sblock->page_count; index++) {
2566 struct scrub_page *spage = sblock->pagev[index];
2567 int ret;
2568
2569 ret = scrub_add_page_to_rd_bio(sctx, spage);
2570 if (ret) {
2571 scrub_block_put(sblock);
2572 return ret;
2573 }
2574 }
2575
2576 /* last one frees, either here or in bio completion for last page */
2577 scrub_block_put(sblock);
2578 return 0;
2579}
2580
2581static int scrub_extent_for_parity(struct scrub_parity *sparity,
2582 u64 logical, u64 len,
2583 u64 physical, struct btrfs_device *dev,
2584 u64 flags, u64 gen, int mirror_num)
2585{
2586 struct scrub_ctx *sctx = sparity->sctx;
2587 int ret;
2588 u8 csum[BTRFS_CSUM_SIZE];
2589 u32 blocksize;
2590
2591 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2592 blocksize = sctx->sectorsize;
2593 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2594 blocksize = sctx->nodesize;
2595 } else {
2596 blocksize = sctx->sectorsize;
2597 WARN_ON(1);
2598 }
2599
2600 while (len) {
2601 u64 l = min_t(u64, len, blocksize);
2602 int have_csum = 0;
2603
2604 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2605 /* push csums to sbio */
2606 have_csum = scrub_find_csum(sctx, logical, l, csum);
2607 if (have_csum == 0)
2608 goto skip;
2609 }
2610 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2611 flags, gen, mirror_num,
2612 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002613 if (ret)
2614 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002615skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002616 len -= l;
2617 logical += l;
2618 physical += l;
2619 }
2620 return 0;
2621}
2622
Wang Shilong3b080b22014-04-01 18:01:43 +08002623/*
2624 * Given a physical address, this will calculate it's
2625 * logical offset. if this is a parity stripe, it will return
2626 * the most left data stripe's logical offset.
2627 *
2628 * return 0 if it is a data stripe, 1 means parity stripe.
2629 */
2630static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002631 struct map_lookup *map, u64 *offset,
2632 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002633{
2634 int i;
2635 int j = 0;
2636 u64 stripe_nr;
2637 u64 last_offset;
2638 int stripe_index;
2639 int rot;
2640
2641 last_offset = (physical - map->stripes[num].physical) *
2642 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002643 if (stripe_start)
2644 *stripe_start = last_offset;
2645
Wang Shilong3b080b22014-04-01 18:01:43 +08002646 *offset = last_offset;
2647 for (i = 0; i < nr_data_stripes(map); i++) {
2648 *offset = last_offset + i * map->stripe_len;
2649
2650 stripe_nr = *offset;
2651 do_div(stripe_nr, map->stripe_len);
2652 do_div(stripe_nr, nr_data_stripes(map));
2653
2654 /* Work out the disk rotation on this stripe-set */
2655 rot = do_div(stripe_nr, map->num_stripes);
2656 /* calculate which stripe this data locates */
2657 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002658 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002659 if (stripe_index == num)
2660 return 0;
2661 if (stripe_index < num)
2662 j++;
2663 }
2664 *offset = last_offset + j * map->stripe_len;
2665 return 1;
2666}
2667
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002668static void scrub_free_parity(struct scrub_parity *sparity)
2669{
2670 struct scrub_ctx *sctx = sparity->sctx;
2671 struct scrub_page *curr, *next;
2672 int nbits;
2673
2674 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2675 if (nbits) {
2676 spin_lock(&sctx->stat_lock);
2677 sctx->stat.read_errors += nbits;
2678 sctx->stat.uncorrectable_errors += nbits;
2679 spin_unlock(&sctx->stat_lock);
2680 }
2681
2682 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2683 list_del_init(&curr->list);
2684 scrub_page_put(curr);
2685 }
2686
2687 kfree(sparity);
2688}
2689
2690static void scrub_parity_bio_endio(struct bio *bio, int error)
2691{
2692 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2693 struct scrub_ctx *sctx = sparity->sctx;
2694
2695 if (error)
2696 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2697 sparity->nsectors);
2698
2699 scrub_free_parity(sparity);
2700 scrub_pending_bio_dec(sctx);
2701 bio_put(bio);
2702}
2703
2704static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2705{
2706 struct scrub_ctx *sctx = sparity->sctx;
2707 struct bio *bio;
2708 struct btrfs_raid_bio *rbio;
2709 struct scrub_page *spage;
2710 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002711 u64 length;
2712 int ret;
2713
2714 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2715 sparity->nsectors))
2716 goto out;
2717
2718 length = sparity->logic_end - sparity->logic_start + 1;
Miao Xie76035972014-11-14 17:45:42 +08002719 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002720 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002721 &length, &bbio, 0, 1);
2722 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002723 goto bbio_out;
2724
2725 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2726 if (!bio)
2727 goto bbio_out;
2728
2729 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2730 bio->bi_private = sparity;
2731 bio->bi_end_io = scrub_parity_bio_endio;
2732
2733 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002734 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002735 sparity->dbitmap,
2736 sparity->nsectors);
2737 if (!rbio)
2738 goto rbio_out;
2739
2740 list_for_each_entry(spage, &sparity->spages, list)
2741 raid56_parity_add_scrub_pages(rbio, spage->page,
2742 spage->logical);
2743
2744 scrub_pending_bio_inc(sctx);
2745 raid56_parity_submit_scrub_rbio(rbio);
2746 return;
2747
2748rbio_out:
2749 bio_put(bio);
2750bbio_out:
2751 kfree(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002752 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2753 sparity->nsectors);
2754 spin_lock(&sctx->stat_lock);
2755 sctx->stat.malloc_errors++;
2756 spin_unlock(&sctx->stat_lock);
2757out:
2758 scrub_free_parity(sparity);
2759}
2760
2761static inline int scrub_calc_parity_bitmap_len(int nsectors)
2762{
2763 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2764}
2765
2766static void scrub_parity_get(struct scrub_parity *sparity)
2767{
2768 atomic_inc(&sparity->ref_count);
2769}
2770
2771static void scrub_parity_put(struct scrub_parity *sparity)
2772{
2773 if (!atomic_dec_and_test(&sparity->ref_count))
2774 return;
2775
2776 scrub_parity_check_and_repair(sparity);
2777}
2778
2779static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2780 struct map_lookup *map,
2781 struct btrfs_device *sdev,
2782 struct btrfs_path *path,
2783 u64 logic_start,
2784 u64 logic_end)
2785{
2786 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2787 struct btrfs_root *root = fs_info->extent_root;
2788 struct btrfs_root *csum_root = fs_info->csum_root;
2789 struct btrfs_extent_item *extent;
2790 u64 flags;
2791 int ret;
2792 int slot;
2793 struct extent_buffer *l;
2794 struct btrfs_key key;
2795 u64 generation;
2796 u64 extent_logical;
2797 u64 extent_physical;
2798 u64 extent_len;
2799 struct btrfs_device *extent_dev;
2800 struct scrub_parity *sparity;
2801 int nsectors;
2802 int bitmap_len;
2803 int extent_mirror_num;
2804 int stop_loop = 0;
2805
2806 nsectors = map->stripe_len / root->sectorsize;
2807 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2808 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2809 GFP_NOFS);
2810 if (!sparity) {
2811 spin_lock(&sctx->stat_lock);
2812 sctx->stat.malloc_errors++;
2813 spin_unlock(&sctx->stat_lock);
2814 return -ENOMEM;
2815 }
2816
2817 sparity->stripe_len = map->stripe_len;
2818 sparity->nsectors = nsectors;
2819 sparity->sctx = sctx;
2820 sparity->scrub_dev = sdev;
2821 sparity->logic_start = logic_start;
2822 sparity->logic_end = logic_end;
2823 atomic_set(&sparity->ref_count, 1);
2824 INIT_LIST_HEAD(&sparity->spages);
2825 sparity->dbitmap = sparity->bitmap;
2826 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2827
2828 ret = 0;
2829 while (logic_start < logic_end) {
2830 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2831 key.type = BTRFS_METADATA_ITEM_KEY;
2832 else
2833 key.type = BTRFS_EXTENT_ITEM_KEY;
2834 key.objectid = logic_start;
2835 key.offset = (u64)-1;
2836
2837 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2838 if (ret < 0)
2839 goto out;
2840
2841 if (ret > 0) {
2842 ret = btrfs_previous_extent_item(root, path, 0);
2843 if (ret < 0)
2844 goto out;
2845 if (ret > 0) {
2846 btrfs_release_path(path);
2847 ret = btrfs_search_slot(NULL, root, &key,
2848 path, 0, 0);
2849 if (ret < 0)
2850 goto out;
2851 }
2852 }
2853
2854 stop_loop = 0;
2855 while (1) {
2856 u64 bytes;
2857
2858 l = path->nodes[0];
2859 slot = path->slots[0];
2860 if (slot >= btrfs_header_nritems(l)) {
2861 ret = btrfs_next_leaf(root, path);
2862 if (ret == 0)
2863 continue;
2864 if (ret < 0)
2865 goto out;
2866
2867 stop_loop = 1;
2868 break;
2869 }
2870 btrfs_item_key_to_cpu(l, &key, slot);
2871
2872 if (key.type == BTRFS_METADATA_ITEM_KEY)
2873 bytes = root->nodesize;
2874 else
2875 bytes = key.offset;
2876
2877 if (key.objectid + bytes <= logic_start)
2878 goto next;
2879
2880 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2881 key.type != BTRFS_METADATA_ITEM_KEY)
2882 goto next;
2883
2884 if (key.objectid > logic_end) {
2885 stop_loop = 1;
2886 break;
2887 }
2888
2889 while (key.objectid >= logic_start + map->stripe_len)
2890 logic_start += map->stripe_len;
2891
2892 extent = btrfs_item_ptr(l, slot,
2893 struct btrfs_extent_item);
2894 flags = btrfs_extent_flags(l, extent);
2895 generation = btrfs_extent_generation(l, extent);
2896
2897 if (key.objectid < logic_start &&
2898 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2899 btrfs_err(fs_info,
2900 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2901 key.objectid, logic_start);
2902 goto next;
2903 }
2904again:
2905 extent_logical = key.objectid;
2906 extent_len = bytes;
2907
2908 if (extent_logical < logic_start) {
2909 extent_len -= logic_start - extent_logical;
2910 extent_logical = logic_start;
2911 }
2912
2913 if (extent_logical + extent_len >
2914 logic_start + map->stripe_len)
2915 extent_len = logic_start + map->stripe_len -
2916 extent_logical;
2917
2918 scrub_parity_mark_sectors_data(sparity, extent_logical,
2919 extent_len);
2920
2921 scrub_remap_extent(fs_info, extent_logical,
2922 extent_len, &extent_physical,
2923 &extent_dev,
2924 &extent_mirror_num);
2925
2926 ret = btrfs_lookup_csums_range(csum_root,
2927 extent_logical,
2928 extent_logical + extent_len - 1,
2929 &sctx->csum_list, 1);
2930 if (ret)
2931 goto out;
2932
2933 ret = scrub_extent_for_parity(sparity, extent_logical,
2934 extent_len,
2935 extent_physical,
2936 extent_dev, flags,
2937 generation,
2938 extent_mirror_num);
2939 if (ret)
2940 goto out;
2941
2942 scrub_free_csums(sctx);
2943 if (extent_logical + extent_len <
2944 key.objectid + bytes) {
2945 logic_start += map->stripe_len;
2946
2947 if (logic_start >= logic_end) {
2948 stop_loop = 1;
2949 break;
2950 }
2951
2952 if (logic_start < key.objectid + bytes) {
2953 cond_resched();
2954 goto again;
2955 }
2956 }
2957next:
2958 path->slots[0]++;
2959 }
2960
2961 btrfs_release_path(path);
2962
2963 if (stop_loop)
2964 break;
2965
2966 logic_start += map->stripe_len;
2967 }
2968out:
2969 if (ret < 0)
2970 scrub_parity_mark_sectors_error(sparity, logic_start,
2971 logic_end - logic_start + 1);
2972 scrub_parity_put(sparity);
2973 scrub_submit(sctx);
2974 mutex_lock(&sctx->wr_ctx.wr_lock);
2975 scrub_wr_submit(sctx);
2976 mutex_unlock(&sctx->wr_ctx.wr_lock);
2977
2978 btrfs_release_path(path);
2979 return ret < 0 ? ret : 0;
2980}
2981
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002982static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002983 struct map_lookup *map,
2984 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002985 int num, u64 base, u64 length,
2986 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002987{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002988 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002989 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002990 struct btrfs_root *root = fs_info->extent_root;
2991 struct btrfs_root *csum_root = fs_info->csum_root;
2992 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002993 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002994 u64 flags;
2995 int ret;
2996 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002997 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002998 struct extent_buffer *l;
2999 struct btrfs_key key;
3000 u64 physical;
3001 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003002 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003003 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003004 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003005 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003006 struct reada_control *reada1;
3007 struct reada_control *reada2;
3008 struct btrfs_key key_start;
3009 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003010 u64 increment = map->stripe_len;
3011 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003012 u64 extent_logical;
3013 u64 extent_physical;
3014 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003015 u64 stripe_logical;
3016 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003017 struct btrfs_device *extent_dev;
3018 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003019 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003020
Arne Jansena2de7332011-03-08 14:14:00 +01003021 nstripes = length;
Wang Shilong3b080b22014-04-01 18:01:43 +08003022 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003023 offset = 0;
3024 do_div(nstripes, map->stripe_len);
3025 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3026 offset = map->stripe_len * num;
3027 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003028 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003029 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3030 int factor = map->num_stripes / map->sub_stripes;
3031 offset = map->stripe_len * (num / map->sub_stripes);
3032 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003033 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003034 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3035 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003036 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003037 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3038 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003039 mirror_num = num % map->num_stripes + 1;
Wang Shilong3b080b22014-04-01 18:01:43 +08003040 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3041 BTRFS_BLOCK_GROUP_RAID6)) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003042 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003043 increment = map->stripe_len * nr_data_stripes(map);
3044 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003045 } else {
3046 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003047 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003048 }
3049
3050 path = btrfs_alloc_path();
3051 if (!path)
3052 return -ENOMEM;
3053
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003054 ppath = btrfs_alloc_path();
3055 if (!ppath) {
3056 btrfs_free_path(ppath);
3057 return -ENOMEM;
3058 }
3059
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003060 /*
3061 * work on commit root. The related disk blocks are static as
3062 * long as COW is applied. This means, it is save to rewrite
3063 * them to repair disk errors without any race conditions
3064 */
Arne Jansena2de7332011-03-08 14:14:00 +01003065 path->search_commit_root = 1;
3066 path->skip_locking = 1;
3067
3068 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003069 * trigger the readahead for extent tree csum tree and wait for
3070 * completion. During readahead, the scrub is officially paused
3071 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003072 */
3073 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003074 physical_end = physical + nstripes * map->stripe_len;
3075 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3076 BTRFS_BLOCK_GROUP_RAID6)) {
3077 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003078 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003079 logic_end += base;
3080 } else {
3081 logic_end = logical + increment * nstripes;
3082 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003083 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003084 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003085 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003086
Arne Jansen7a262852011-06-10 12:39:23 +02003087 /* FIXME it might be better to start readahead at commit root */
3088 key_start.objectid = logical;
3089 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3090 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003091 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003092 key_end.type = BTRFS_METADATA_ITEM_KEY;
3093 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003094 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003095
Arne Jansen7a262852011-06-10 12:39:23 +02003096 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3097 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3098 key_start.offset = logical;
3099 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3100 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003101 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003102 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003103
Arne Jansen7a262852011-06-10 12:39:23 +02003104 if (!IS_ERR(reada1))
3105 btrfs_reada_wait(reada1);
3106 if (!IS_ERR(reada2))
3107 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003108
Arne Jansena2de7332011-03-08 14:14:00 +01003109
3110 /*
3111 * collect all data csums for the stripe to avoid seeking during
3112 * the scrub. This might currently (crc32) end up to be about 1MB
3113 */
Arne Jansene7786c32011-05-28 20:58:38 +00003114 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003115
Arne Jansena2de7332011-03-08 14:14:00 +01003116 /*
3117 * now find all extents for each stripe and scrub them
3118 */
Arne Jansena2de7332011-03-08 14:14:00 +01003119 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003120 while (physical < physical_end) {
3121 /* for raid56, we skip parity stripe */
3122 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3123 BTRFS_BLOCK_GROUP_RAID6)) {
3124 ret = get_raid56_logic_offset(physical, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003125 map, &logical, &stripe_logical);
Wang Shilong3b080b22014-04-01 18:01:43 +08003126 logical += base;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003127 if (ret) {
3128 stripe_logical += base;
3129 stripe_end = stripe_logical + increment - 1;
3130 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3131 ppath, stripe_logical,
3132 stripe_end);
3133 if (ret)
3134 goto out;
Wang Shilong3b080b22014-04-01 18:01:43 +08003135 goto skip;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003136 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003137 }
Arne Jansena2de7332011-03-08 14:14:00 +01003138 /*
3139 * canceled?
3140 */
3141 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003142 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003143 ret = -ECANCELED;
3144 goto out;
3145 }
3146 /*
3147 * check to see if we have to pause
3148 */
3149 if (atomic_read(&fs_info->scrub_pause_req)) {
3150 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003151 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003152 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003153 mutex_lock(&sctx->wr_ctx.wr_lock);
3154 scrub_wr_submit(sctx);
3155 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003156 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003157 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003158 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003159 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003160 }
3161
Wang Shilong7c76edb2014-01-12 21:38:32 +08003162 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3163 key.type = BTRFS_METADATA_ITEM_KEY;
3164 else
3165 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003166 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003167 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003168
3169 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3170 if (ret < 0)
3171 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003172
Arne Jansen8c510322011-06-03 10:09:26 +02003173 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003174 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003175 if (ret < 0)
3176 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003177 if (ret > 0) {
3178 /* there's no smaller item, so stick with the
3179 * larger one */
3180 btrfs_release_path(path);
3181 ret = btrfs_search_slot(NULL, root, &key,
3182 path, 0, 0);
3183 if (ret < 0)
3184 goto out;
3185 }
Arne Jansena2de7332011-03-08 14:14:00 +01003186 }
3187
Liu Bo625f1c8d2013-04-27 02:56:57 +00003188 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003189 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003190 u64 bytes;
3191
Arne Jansena2de7332011-03-08 14:14:00 +01003192 l = path->nodes[0];
3193 slot = path->slots[0];
3194 if (slot >= btrfs_header_nritems(l)) {
3195 ret = btrfs_next_leaf(root, path);
3196 if (ret == 0)
3197 continue;
3198 if (ret < 0)
3199 goto out;
3200
Liu Bo625f1c8d2013-04-27 02:56:57 +00003201 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003202 break;
3203 }
3204 btrfs_item_key_to_cpu(l, &key, slot);
3205
Josef Bacik3173a182013-03-07 14:22:04 -05003206 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003207 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003208 else
3209 bytes = key.offset;
3210
3211 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003212 goto next;
3213
Liu Bo625f1c8d2013-04-27 02:56:57 +00003214 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3215 key.type != BTRFS_METADATA_ITEM_KEY)
3216 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01003217
Liu Bo625f1c8d2013-04-27 02:56:57 +00003218 if (key.objectid >= logical + map->stripe_len) {
3219 /* out of this device extent */
3220 if (key.objectid >= logic_end)
3221 stop_loop = 1;
3222 break;
3223 }
Arne Jansena2de7332011-03-08 14:14:00 +01003224
3225 extent = btrfs_item_ptr(l, slot,
3226 struct btrfs_extent_item);
3227 flags = btrfs_extent_flags(l, extent);
3228 generation = btrfs_extent_generation(l, extent);
3229
3230 if (key.objectid < logical &&
3231 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003232 btrfs_err(fs_info,
3233 "scrub: tree block %llu spanning "
3234 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003235 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01003236 goto next;
3237 }
3238
Liu Bo625f1c8d2013-04-27 02:56:57 +00003239again:
3240 extent_logical = key.objectid;
3241 extent_len = bytes;
3242
Arne Jansena2de7332011-03-08 14:14:00 +01003243 /*
3244 * trim extent to this stripe
3245 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003246 if (extent_logical < logical) {
3247 extent_len -= logical - extent_logical;
3248 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003249 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003250 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003251 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003252 extent_len = logical + map->stripe_len -
3253 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003254 }
3255
Liu Bo625f1c8d2013-04-27 02:56:57 +00003256 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003257 extent_dev = scrub_dev;
3258 extent_mirror_num = mirror_num;
3259 if (is_dev_replace)
3260 scrub_remap_extent(fs_info, extent_logical,
3261 extent_len, &extent_physical,
3262 &extent_dev,
3263 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003264
3265 ret = btrfs_lookup_csums_range(csum_root, logical,
3266 logical + map->stripe_len - 1,
3267 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003268 if (ret)
3269 goto out;
3270
Liu Bo625f1c8d2013-04-27 02:56:57 +00003271 ret = scrub_extent(sctx, extent_logical, extent_len,
3272 extent_physical, extent_dev, flags,
3273 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003274 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003275 if (ret)
3276 goto out;
3277
Josef Bacikd88d46c2013-06-10 12:59:04 +00003278 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003279 if (extent_logical + extent_len <
3280 key.objectid + bytes) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003281 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3282 BTRFS_BLOCK_GROUP_RAID6)) {
3283 /*
3284 * loop until we find next data stripe
3285 * or we have finished all stripes.
3286 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003287loop:
3288 physical += map->stripe_len;
3289 ret = get_raid56_logic_offset(physical,
3290 num, map, &logical,
3291 &stripe_logical);
3292 logical += base;
3293
3294 if (ret && physical < physical_end) {
3295 stripe_logical += base;
3296 stripe_end = stripe_logical +
3297 increment - 1;
3298 ret = scrub_raid56_parity(sctx,
3299 map, scrub_dev, ppath,
3300 stripe_logical,
3301 stripe_end);
3302 if (ret)
3303 goto out;
3304 goto loop;
3305 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003306 } else {
3307 physical += map->stripe_len;
3308 logical += increment;
3309 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003310 if (logical < key.objectid + bytes) {
3311 cond_resched();
3312 goto again;
3313 }
3314
Wang Shilong3b080b22014-04-01 18:01:43 +08003315 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003316 stop_loop = 1;
3317 break;
3318 }
3319 }
Arne Jansena2de7332011-03-08 14:14:00 +01003320next:
3321 path->slots[0]++;
3322 }
Chris Mason71267332011-05-23 06:30:52 -04003323 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003324skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003325 logical += increment;
3326 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003327 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003328 if (stop_loop)
3329 sctx->stat.last_physical = map->stripes[num].physical +
3330 length;
3331 else
3332 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003333 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003334 if (stop_loop)
3335 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003336 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003337out:
Arne Jansena2de7332011-03-08 14:14:00 +01003338 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003339 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003340 mutex_lock(&sctx->wr_ctx.wr_lock);
3341 scrub_wr_submit(sctx);
3342 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003343
Arne Jansene7786c32011-05-28 20:58:38 +00003344 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003345 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003346 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003347 return ret < 0 ? ret : 0;
3348}
3349
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003350static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003351 struct btrfs_device *scrub_dev,
3352 u64 chunk_tree, u64 chunk_objectid,
3353 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003354 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003355{
3356 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003357 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003358 struct map_lookup *map;
3359 struct extent_map *em;
3360 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003361 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003362
3363 read_lock(&map_tree->map_tree.lock);
3364 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3365 read_unlock(&map_tree->map_tree.lock);
3366
3367 if (!em)
3368 return -EINVAL;
3369
3370 map = (struct map_lookup *)em->bdev;
3371 if (em->start != chunk_offset)
3372 goto out;
3373
3374 if (em->len < length)
3375 goto out;
3376
3377 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003378 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003379 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003380 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003381 chunk_offset, length,
3382 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003383 if (ret)
3384 goto out;
3385 }
3386 }
3387out:
3388 free_extent_map(em);
3389
3390 return ret;
3391}
3392
3393static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003394int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003395 struct btrfs_device *scrub_dev, u64 start, u64 end,
3396 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003397{
3398 struct btrfs_dev_extent *dev_extent = NULL;
3399 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003400 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003401 struct btrfs_fs_info *fs_info = root->fs_info;
3402 u64 length;
3403 u64 chunk_tree;
3404 u64 chunk_objectid;
3405 u64 chunk_offset;
3406 int ret;
3407 int slot;
3408 struct extent_buffer *l;
3409 struct btrfs_key key;
3410 struct btrfs_key found_key;
3411 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003412 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003413
3414 path = btrfs_alloc_path();
3415 if (!path)
3416 return -ENOMEM;
3417
3418 path->reada = 2;
3419 path->search_commit_root = 1;
3420 path->skip_locking = 1;
3421
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003422 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003423 key.offset = 0ull;
3424 key.type = BTRFS_DEV_EXTENT_KEY;
3425
Arne Jansena2de7332011-03-08 14:14:00 +01003426 while (1) {
3427 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3428 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003429 break;
3430 if (ret > 0) {
3431 if (path->slots[0] >=
3432 btrfs_header_nritems(path->nodes[0])) {
3433 ret = btrfs_next_leaf(root, path);
3434 if (ret)
3435 break;
3436 }
3437 }
Arne Jansena2de7332011-03-08 14:14:00 +01003438
3439 l = path->nodes[0];
3440 slot = path->slots[0];
3441
3442 btrfs_item_key_to_cpu(l, &found_key, slot);
3443
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003444 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003445 break;
3446
David Sterba962a2982014-06-04 18:41:45 +02003447 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003448 break;
3449
3450 if (found_key.offset >= end)
3451 break;
3452
3453 if (found_key.offset < key.offset)
3454 break;
3455
3456 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3457 length = btrfs_dev_extent_length(l, dev_extent);
3458
Qu Wenruoced96ed2014-06-19 10:42:51 +08003459 if (found_key.offset + length <= start)
3460 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003461
3462 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3463 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3464 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3465
3466 /*
3467 * get a reference on the corresponding block group to prevent
3468 * the chunk from going away while we scrub it
3469 */
3470 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003471
3472 /* some chunks are removed but not committed to disk yet,
3473 * continue scrubbing */
3474 if (!cache)
3475 goto skip;
3476
Stefan Behrensff023aa2012-11-06 11:43:11 +01003477 dev_replace->cursor_right = found_key.offset + length;
3478 dev_replace->cursor_left = found_key.offset;
3479 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003480 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003481 chunk_offset, length, found_key.offset,
3482 is_dev_replace);
3483
3484 /*
3485 * flush, submit all pending read and write bios, afterwards
3486 * wait for them.
3487 * Note that in the dev replace case, a read request causes
3488 * write requests that are submitted in the read completion
3489 * worker. Therefore in the current situation, it is required
3490 * that all write requests are flushed, so that all read and
3491 * write requests are really completed when bios_in_flight
3492 * changes to 0.
3493 */
3494 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3495 scrub_submit(sctx);
3496 mutex_lock(&sctx->wr_ctx.wr_lock);
3497 scrub_wr_submit(sctx);
3498 mutex_unlock(&sctx->wr_ctx.wr_lock);
3499
3500 wait_event(sctx->list_wait,
3501 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003502 atomic_inc(&fs_info->scrubs_paused);
3503 wake_up(&fs_info->scrub_pause_wait);
3504
3505 /*
3506 * must be called before we decrease @scrub_paused.
3507 * make sure we don't block transaction commit while
3508 * we are waiting pending workers finished.
3509 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003510 wait_event(sctx->list_wait,
3511 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003512 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3513
3514 mutex_lock(&fs_info->scrub_lock);
3515 __scrub_blocked_if_needed(fs_info);
3516 atomic_dec(&fs_info->scrubs_paused);
3517 mutex_unlock(&fs_info->scrub_lock);
3518 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003519
Arne Jansena2de7332011-03-08 14:14:00 +01003520 btrfs_put_block_group(cache);
3521 if (ret)
3522 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003523 if (is_dev_replace &&
3524 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003525 ret = -EIO;
3526 break;
3527 }
3528 if (sctx->stat.malloc_errors > 0) {
3529 ret = -ENOMEM;
3530 break;
3531 }
Arne Jansena2de7332011-03-08 14:14:00 +01003532
Ilya Dryomov539f3582013-10-07 13:42:57 +03003533 dev_replace->cursor_left = dev_replace->cursor_right;
3534 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003535skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003536 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003537 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003538 }
3539
Arne Jansena2de7332011-03-08 14:14:00 +01003540 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003541
3542 /*
3543 * ret can still be 1 from search_slot or next_leaf,
3544 * that's not an error
3545 */
3546 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003547}
3548
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003549static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3550 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003551{
3552 int i;
3553 u64 bytenr;
3554 u64 gen;
3555 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003556 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003557
Miao Xie87533c42013-01-29 10:14:48 +00003558 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003559 return -EIO;
3560
Miao Xie5f546062014-07-24 11:37:09 +08003561 /* Seed devices of a new filesystem has their own generation. */
3562 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3563 gen = scrub_dev->generation;
3564 else
3565 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003566
3567 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3568 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003569 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3570 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003571 break;
3572
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003573 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003574 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003575 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003576 if (ret)
3577 return ret;
3578 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003579 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003580
3581 return 0;
3582}
3583
3584/*
3585 * get a reference count on fs_info->scrub_workers. start worker if necessary
3586 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003587static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3588 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003589{
Josef Bacik0dc3b842011-11-18 14:37:27 -05003590 int ret = 0;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003591 int flags = WQ_FREEZABLE | WQ_UNBOUND;
3592 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003593
Arne Jansen632dd772011-06-10 12:07:07 +02003594 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003595 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003596 fs_info->scrub_workers =
3597 btrfs_alloc_workqueue("btrfs-scrub", flags,
3598 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003599 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003600 fs_info->scrub_workers =
3601 btrfs_alloc_workqueue("btrfs-scrub", flags,
3602 max_active, 4);
3603 if (!fs_info->scrub_workers) {
3604 ret = -ENOMEM;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003605 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003606 }
3607 fs_info->scrub_wr_completion_workers =
3608 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3609 max_active, 2);
3610 if (!fs_info->scrub_wr_completion_workers) {
3611 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003612 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003613 }
3614 fs_info->scrub_nocow_workers =
3615 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
3616 if (!fs_info->scrub_nocow_workers) {
3617 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003618 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003619 }
Arne Jansen632dd772011-06-10 12:07:07 +02003620 }
Arne Jansena2de7332011-03-08 14:14:00 +01003621 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003622out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05003623 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003624}
3625
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003626static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003627{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003628 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003629 btrfs_destroy_workqueue(fs_info->scrub_workers);
3630 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3631 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003632 }
Arne Jansena2de7332011-03-08 14:14:00 +01003633 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003634}
3635
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003636int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3637 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003638 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003639{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003640 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003641 int ret;
3642 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003643 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003644
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003645 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003646 return -EINVAL;
3647
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003648 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003649 /*
3650 * in this case scrub is unable to calculate the checksum
3651 * the way scrub is implemented. Do not handle this
3652 * situation at all because it won't ever happen.
3653 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003654 btrfs_err(fs_info,
3655 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003656 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003657 return -EINVAL;
3658 }
3659
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003660 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003661 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003662 btrfs_err(fs_info,
3663 "scrub: size assumption sectorsize != PAGE_SIZE "
3664 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003665 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003666 return -EINVAL;
3667 }
3668
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003669 if (fs_info->chunk_root->nodesize >
3670 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3671 fs_info->chunk_root->sectorsize >
3672 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3673 /*
3674 * would exhaust the array bounds of pagev member in
3675 * struct scrub_block
3676 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003677 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3678 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003679 fs_info->chunk_root->nodesize,
3680 SCRUB_MAX_PAGES_PER_BLOCK,
3681 fs_info->chunk_root->sectorsize,
3682 SCRUB_MAX_PAGES_PER_BLOCK);
3683 return -EINVAL;
3684 }
3685
Arne Jansena2de7332011-03-08 14:14:00 +01003686
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003687 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3688 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003689 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003690 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003691 return -ENODEV;
3692 }
Arne Jansena2de7332011-03-08 14:14:00 +01003693
Miao Xie5d68da32014-07-24 11:37:07 +08003694 if (!is_dev_replace && !readonly && !dev->writeable) {
3695 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3696 rcu_read_lock();
3697 name = rcu_dereference(dev->name);
3698 btrfs_err(fs_info, "scrub: device %s is not writable",
3699 name->str);
3700 rcu_read_unlock();
3701 return -EROFS;
3702 }
3703
Wang Shilong3b7a0162013-10-12 02:11:12 +08003704 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003705 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003706 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003707 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003708 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003709 }
3710
Stefan Behrens8dabb742012-11-06 13:15:27 +01003711 btrfs_dev_replace_lock(&fs_info->dev_replace);
3712 if (dev->scrub_device ||
3713 (!is_dev_replace &&
3714 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3715 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003716 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003717 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003718 return -EINPROGRESS;
3719 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003720 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003721
3722 ret = scrub_workers_get(fs_info, is_dev_replace);
3723 if (ret) {
3724 mutex_unlock(&fs_info->scrub_lock);
3725 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3726 return ret;
3727 }
3728
Stefan Behrens63a212a2012-11-05 18:29:28 +01003729 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003730 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003731 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003732 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3733 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003734 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003735 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003736 sctx->readonly = readonly;
3737 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003738 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003739
Wang Shilong3cb09292013-12-04 21:15:19 +08003740 /*
3741 * checking @scrub_pause_req here, we can avoid
3742 * race between committing transaction and scrubbing.
3743 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003744 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003745 atomic_inc(&fs_info->scrubs_running);
3746 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003747
Stefan Behrensff023aa2012-11-06 11:43:11 +01003748 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003749 /*
3750 * by holding device list mutex, we can
3751 * kick off writing super in log tree sync.
3752 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003753 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003754 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003755 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003756 }
Arne Jansena2de7332011-03-08 14:14:00 +01003757
3758 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003759 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3760 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003761
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003762 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003763 atomic_dec(&fs_info->scrubs_running);
3764 wake_up(&fs_info->scrub_pause_wait);
3765
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003766 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003767
Arne Jansena2de7332011-03-08 14:14:00 +01003768 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003769 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003770
3771 mutex_lock(&fs_info->scrub_lock);
3772 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003773 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003774 mutex_unlock(&fs_info->scrub_lock);
3775
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003776 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003777
3778 return ret;
3779}
3780
Jeff Mahoney143bede2012-03-01 14:56:26 +01003781void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003782{
3783 struct btrfs_fs_info *fs_info = root->fs_info;
3784
3785 mutex_lock(&fs_info->scrub_lock);
3786 atomic_inc(&fs_info->scrub_pause_req);
3787 while (atomic_read(&fs_info->scrubs_paused) !=
3788 atomic_read(&fs_info->scrubs_running)) {
3789 mutex_unlock(&fs_info->scrub_lock);
3790 wait_event(fs_info->scrub_pause_wait,
3791 atomic_read(&fs_info->scrubs_paused) ==
3792 atomic_read(&fs_info->scrubs_running));
3793 mutex_lock(&fs_info->scrub_lock);
3794 }
3795 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003796}
3797
Jeff Mahoney143bede2012-03-01 14:56:26 +01003798void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003799{
3800 struct btrfs_fs_info *fs_info = root->fs_info;
3801
3802 atomic_dec(&fs_info->scrub_pause_req);
3803 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003804}
3805
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003806int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003807{
Arne Jansena2de7332011-03-08 14:14:00 +01003808 mutex_lock(&fs_info->scrub_lock);
3809 if (!atomic_read(&fs_info->scrubs_running)) {
3810 mutex_unlock(&fs_info->scrub_lock);
3811 return -ENOTCONN;
3812 }
3813
3814 atomic_inc(&fs_info->scrub_cancel_req);
3815 while (atomic_read(&fs_info->scrubs_running)) {
3816 mutex_unlock(&fs_info->scrub_lock);
3817 wait_event(fs_info->scrub_pause_wait,
3818 atomic_read(&fs_info->scrubs_running) == 0);
3819 mutex_lock(&fs_info->scrub_lock);
3820 }
3821 atomic_dec(&fs_info->scrub_cancel_req);
3822 mutex_unlock(&fs_info->scrub_lock);
3823
3824 return 0;
3825}
3826
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003827int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3828 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003829{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003830 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003831
3832 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003833 sctx = dev->scrub_device;
3834 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003835 mutex_unlock(&fs_info->scrub_lock);
3836 return -ENOTCONN;
3837 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003838 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003839 while (dev->scrub_device) {
3840 mutex_unlock(&fs_info->scrub_lock);
3841 wait_event(fs_info->scrub_pause_wait,
3842 dev->scrub_device == NULL);
3843 mutex_lock(&fs_info->scrub_lock);
3844 }
3845 mutex_unlock(&fs_info->scrub_lock);
3846
3847 return 0;
3848}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003849
Arne Jansena2de7332011-03-08 14:14:00 +01003850int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3851 struct btrfs_scrub_progress *progress)
3852{
3853 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003854 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003855
3856 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003857 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003858 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003859 sctx = dev->scrub_device;
3860 if (sctx)
3861 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003862 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3863
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003864 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003865}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003866
3867static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3868 u64 extent_logical, u64 extent_len,
3869 u64 *extent_physical,
3870 struct btrfs_device **extent_dev,
3871 int *extent_mirror_num)
3872{
3873 u64 mapped_length;
3874 struct btrfs_bio *bbio = NULL;
3875 int ret;
3876
3877 mapped_length = extent_len;
3878 ret = btrfs_map_block(fs_info, READ, extent_logical,
3879 &mapped_length, &bbio, 0);
3880 if (ret || !bbio || mapped_length < extent_len ||
3881 !bbio->stripes[0].dev->bdev) {
3882 kfree(bbio);
3883 return;
3884 }
3885
3886 *extent_physical = bbio->stripes[0].physical;
3887 *extent_mirror_num = bbio->mirror_num;
3888 *extent_dev = bbio->stripes[0].dev;
3889 kfree(bbio);
3890}
3891
3892static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3893 struct scrub_wr_ctx *wr_ctx,
3894 struct btrfs_fs_info *fs_info,
3895 struct btrfs_device *dev,
3896 int is_dev_replace)
3897{
3898 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3899
3900 mutex_init(&wr_ctx->wr_lock);
3901 wr_ctx->wr_curr_bio = NULL;
3902 if (!is_dev_replace)
3903 return 0;
3904
3905 WARN_ON(!dev->bdev);
3906 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3907 bio_get_nr_vecs(dev->bdev));
3908 wr_ctx->tgtdev = dev;
3909 atomic_set(&wr_ctx->flush_all_writes, 0);
3910 return 0;
3911}
3912
3913static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3914{
3915 mutex_lock(&wr_ctx->wr_lock);
3916 kfree(wr_ctx->wr_curr_bio);
3917 wr_ctx->wr_curr_bio = NULL;
3918 mutex_unlock(&wr_ctx->wr_lock);
3919}
3920
3921static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3922 int mirror_num, u64 physical_for_dev_replace)
3923{
3924 struct scrub_copy_nocow_ctx *nocow_ctx;
3925 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3926
3927 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3928 if (!nocow_ctx) {
3929 spin_lock(&sctx->stat_lock);
3930 sctx->stat.malloc_errors++;
3931 spin_unlock(&sctx->stat_lock);
3932 return -ENOMEM;
3933 }
3934
3935 scrub_pending_trans_workers_inc(sctx);
3936
3937 nocow_ctx->sctx = sctx;
3938 nocow_ctx->logical = logical;
3939 nocow_ctx->len = len;
3940 nocow_ctx->mirror_num = mirror_num;
3941 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08003942 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3943 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003944 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003945 btrfs_queue_work(fs_info->scrub_nocow_workers,
3946 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003947
3948 return 0;
3949}
3950
Josef Bacik652f25a2013-09-12 16:58:28 -04003951static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3952{
3953 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3954 struct scrub_nocow_inode *nocow_inode;
3955
3956 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3957 if (!nocow_inode)
3958 return -ENOMEM;
3959 nocow_inode->inum = inum;
3960 nocow_inode->offset = offset;
3961 nocow_inode->root = root;
3962 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3963 return 0;
3964}
3965
3966#define COPY_COMPLETE 1
3967
Stefan Behrensff023aa2012-11-06 11:43:11 +01003968static void copy_nocow_pages_worker(struct btrfs_work *work)
3969{
3970 struct scrub_copy_nocow_ctx *nocow_ctx =
3971 container_of(work, struct scrub_copy_nocow_ctx, work);
3972 struct scrub_ctx *sctx = nocow_ctx->sctx;
3973 u64 logical = nocow_ctx->logical;
3974 u64 len = nocow_ctx->len;
3975 int mirror_num = nocow_ctx->mirror_num;
3976 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3977 int ret;
3978 struct btrfs_trans_handle *trans = NULL;
3979 struct btrfs_fs_info *fs_info;
3980 struct btrfs_path *path;
3981 struct btrfs_root *root;
3982 int not_written = 0;
3983
3984 fs_info = sctx->dev_root->fs_info;
3985 root = fs_info->extent_root;
3986
3987 path = btrfs_alloc_path();
3988 if (!path) {
3989 spin_lock(&sctx->stat_lock);
3990 sctx->stat.malloc_errors++;
3991 spin_unlock(&sctx->stat_lock);
3992 not_written = 1;
3993 goto out;
3994 }
3995
3996 trans = btrfs_join_transaction(root);
3997 if (IS_ERR(trans)) {
3998 not_written = 1;
3999 goto out;
4000 }
4001
4002 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04004003 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004004 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004005 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
4006 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02004007 logical, physical_for_dev_replace, len, mirror_num,
4008 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004009 not_written = 1;
4010 goto out;
4011 }
4012
Josef Bacik652f25a2013-09-12 16:58:28 -04004013 btrfs_end_transaction(trans, root);
4014 trans = NULL;
4015 while (!list_empty(&nocow_ctx->inodes)) {
4016 struct scrub_nocow_inode *entry;
4017 entry = list_first_entry(&nocow_ctx->inodes,
4018 struct scrub_nocow_inode,
4019 list);
4020 list_del_init(&entry->list);
4021 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4022 entry->root, nocow_ctx);
4023 kfree(entry);
4024 if (ret == COPY_COMPLETE) {
4025 ret = 0;
4026 break;
4027 } else if (ret) {
4028 break;
4029 }
4030 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004031out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004032 while (!list_empty(&nocow_ctx->inodes)) {
4033 struct scrub_nocow_inode *entry;
4034 entry = list_first_entry(&nocow_ctx->inodes,
4035 struct scrub_nocow_inode,
4036 list);
4037 list_del_init(&entry->list);
4038 kfree(entry);
4039 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004040 if (trans && !IS_ERR(trans))
4041 btrfs_end_transaction(trans, root);
4042 if (not_written)
4043 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4044 num_uncorrectable_read_errors);
4045
4046 btrfs_free_path(path);
4047 kfree(nocow_ctx);
4048
4049 scrub_pending_trans_workers_dec(sctx);
4050}
4051
Gui Hecheng32159242014-11-10 15:36:08 +08004052static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4053 u64 logical)
4054{
4055 struct extent_state *cached_state = NULL;
4056 struct btrfs_ordered_extent *ordered;
4057 struct extent_io_tree *io_tree;
4058 struct extent_map *em;
4059 u64 lockstart = start, lockend = start + len - 1;
4060 int ret = 0;
4061
4062 io_tree = &BTRFS_I(inode)->io_tree;
4063
4064 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4065 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4066 if (ordered) {
4067 btrfs_put_ordered_extent(ordered);
4068 ret = 1;
4069 goto out_unlock;
4070 }
4071
4072 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4073 if (IS_ERR(em)) {
4074 ret = PTR_ERR(em);
4075 goto out_unlock;
4076 }
4077
4078 /*
4079 * This extent does not actually cover the logical extent anymore,
4080 * move on to the next inode.
4081 */
4082 if (em->block_start > logical ||
4083 em->block_start + em->block_len < logical + len) {
4084 free_extent_map(em);
4085 ret = 1;
4086 goto out_unlock;
4087 }
4088 free_extent_map(em);
4089
4090out_unlock:
4091 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4092 GFP_NOFS);
4093 return ret;
4094}
4095
Josef Bacik652f25a2013-09-12 16:58:28 -04004096static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4097 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004098{
Miao Xie826aa0a2013-06-27 18:50:59 +08004099 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004100 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004101 struct inode *inode;
4102 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004103 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004104 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004105 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004106 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004107 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004108 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004109 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004110 int ret = 0;
4111 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004112
4113 key.objectid = root;
4114 key.type = BTRFS_ROOT_ITEM_KEY;
4115 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004116
4117 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4118
Stefan Behrensff023aa2012-11-06 11:43:11 +01004119 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004120 if (IS_ERR(local_root)) {
4121 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004122 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004123 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004124
4125 key.type = BTRFS_INODE_ITEM_KEY;
4126 key.objectid = inum;
4127 key.offset = 0;
4128 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004129 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004130 if (IS_ERR(inode))
4131 return PTR_ERR(inode);
4132
Miao Xieedd14002013-06-27 18:51:00 +08004133 /* Avoid truncate/dio/punch hole.. */
4134 mutex_lock(&inode->i_mutex);
4135 inode_dio_wait(inode);
4136
Stefan Behrensff023aa2012-11-06 11:43:11 +01004137 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004138 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004139 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004140
Gui Hecheng32159242014-11-10 15:36:08 +08004141 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4142 if (ret) {
4143 ret = ret > 0 ? 0 : ret;
4144 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004145 }
4146
Stefan Behrensff023aa2012-11-06 11:43:11 +01004147 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004148 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004149again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004150 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4151 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004152 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004153 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004154 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004155 }
4156
4157 if (PageUptodate(page)) {
4158 if (PageDirty(page))
4159 goto next_page;
4160 } else {
4161 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004162 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004163 btrfs_get_extent,
4164 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004165 if (err) {
4166 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004167 goto next_page;
4168 }
Miao Xieedd14002013-06-27 18:51:00 +08004169
Miao Xie26b258912013-06-27 18:50:58 +08004170 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004171 /*
4172 * If the page has been remove from the page cache,
4173 * the data on it is meaningless, because it may be
4174 * old one, the new data may be written into the new
4175 * page in the page cache.
4176 */
4177 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004178 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004179 page_cache_release(page);
4180 goto again;
4181 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004182 if (!PageUptodate(page)) {
4183 ret = -EIO;
4184 goto next_page;
4185 }
4186 }
Gui Hecheng32159242014-11-10 15:36:08 +08004187
4188 ret = check_extent_to_block(inode, offset, len,
4189 nocow_ctx_logical);
4190 if (ret) {
4191 ret = ret > 0 ? 0 : ret;
4192 goto next_page;
4193 }
4194
Miao Xie826aa0a2013-06-27 18:50:59 +08004195 err = write_page_nocow(nocow_ctx->sctx,
4196 physical_for_dev_replace, page);
4197 if (err)
4198 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004199next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004200 unlock_page(page);
4201 page_cache_release(page);
4202
4203 if (ret)
4204 break;
4205
Stefan Behrensff023aa2012-11-06 11:43:11 +01004206 offset += PAGE_CACHE_SIZE;
4207 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004208 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004209 len -= PAGE_CACHE_SIZE;
4210 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004211 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004212out:
Miao Xieedd14002013-06-27 18:51:00 +08004213 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004214 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004215 return ret;
4216}
4217
4218static int write_page_nocow(struct scrub_ctx *sctx,
4219 u64 physical_for_dev_replace, struct page *page)
4220{
4221 struct bio *bio;
4222 struct btrfs_device *dev;
4223 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004224
4225 dev = sctx->wr_ctx.tgtdev;
4226 if (!dev)
4227 return -EIO;
4228 if (!dev->bdev) {
4229 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05004230 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004231 return -EIO;
4232 }
Chris Mason9be33952013-05-17 18:30:14 -04004233 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004234 if (!bio) {
4235 spin_lock(&sctx->stat_lock);
4236 sctx->stat.malloc_errors++;
4237 spin_unlock(&sctx->stat_lock);
4238 return -ENOMEM;
4239 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004240 bio->bi_iter.bi_size = 0;
4241 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004242 bio->bi_bdev = dev->bdev;
4243 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4244 if (ret != PAGE_CACHE_SIZE) {
4245leave_with_eio:
4246 bio_put(bio);
4247 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4248 return -EIO;
4249 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004250
Kent Overstreet33879d42013-11-23 22:33:32 -08004251 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004252 goto leave_with_eio;
4253
4254 bio_put(bio);
4255 return 0;
4256}