blob: 038162456cfad0dac07f5e1b56f80c63f1517e84 [file] [log] [blame]
Arne Jansena2de7332011-03-08 14:14:00 +01001/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
Arne Jansena2de7332011-03-08 14:14:00 +01003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Arne Jansena2de7332011-03-08 14:14:00 +010019#include <linux/blkdev.h>
Jan Schmidt558540c2011-06-13 19:59:12 +020020#include <linux/ratelimit.h>
Arne Jansena2de7332011-03-08 14:14:00 +010021#include "ctree.h"
22#include "volumes.h"
23#include "disk-io.h"
24#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020025#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020026#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020027#include "extent_io.h"
Stefan Behrensff023aa2012-11-06 11:43:11 +010028#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010029#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040030#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050031#include "raid56.h"
Arne Jansena2de7332011-03-08 14:14:00 +010032
33/*
34 * This is only the first step towards a full-features scrub. It reads all
35 * extent and super block and verifies the checksums. In case a bad checksum
36 * is found or the extent cannot be read, good data will be written back if
37 * any can be found.
38 *
39 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010040 * - In case an unrepairable extent is encountered, track which files are
41 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010042 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010043 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010044 */
45
Stefan Behrensb5d67f62012-03-27 14:21:27 -040046struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010047struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010048
Stefan Behrensff023aa2012-11-06 11:43:11 +010049/*
50 * the following three values only influence the performance.
51 * The last one configures the number of parallel and outstanding I/O
52 * operations. The first two values configure an upper limit for the number
53 * of (dynamically allocated) pages that are added to a bio.
54 */
55#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
56#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
57#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010058
59/*
60 * the following value times PAGE_SIZE needs to be large enough to match the
61 * largest node/leaf/sector size that shall be supported.
62 * Values larger than BTRFS_STRIPE_LEN are not supported.
63 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040064#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010065
Miao Xieaf8e2d12014-10-23 14:42:50 +080066struct scrub_recover {
67 atomic_t refs;
68 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +080069 u64 map_length;
70};
71
Arne Jansena2de7332011-03-08 14:14:00 +010072struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040073 struct scrub_block *sblock;
74 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020075 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080076 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010077 u64 flags; /* extent flags */
78 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040079 u64 logical;
80 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010081 u64 physical_for_dev_replace;
Zhao Lei57019342015-01-20 15:11:45 +080082 atomic_t refs;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040083 struct {
84 unsigned int mirror_num:8;
85 unsigned int have_csum:1;
86 unsigned int io_error:1;
87 };
Arne Jansena2de7332011-03-08 14:14:00 +010088 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080089
90 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010091};
92
93struct scrub_bio {
94 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010095 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010096 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010097 struct bio *bio;
98 int err;
99 u64 logical;
100 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100101#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
102 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
103#else
104 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
105#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400106 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +0100107 int next_free;
108 struct btrfs_work work;
109};
110
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400111struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100112 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400113 int page_count;
114 atomic_t outstanding_pages;
Zhao Lei57019342015-01-20 15:11:45 +0800115 atomic_t refs; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100116 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800117 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400118 struct {
119 unsigned int header_error:1;
120 unsigned int checksum_error:1;
121 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200122 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800123
124 /* The following is for the data used to check parity */
125 /* It is for the data with checksum */
126 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400127 };
128};
129
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800130/* Used for the chunks with parity stripe such RAID5/6 */
131struct scrub_parity {
132 struct scrub_ctx *sctx;
133
134 struct btrfs_device *scrub_dev;
135
136 u64 logic_start;
137
138 u64 logic_end;
139
140 int nsectors;
141
142 int stripe_len;
143
Zhao Lei57019342015-01-20 15:11:45 +0800144 atomic_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800145
146 struct list_head spages;
147
148 /* Work of parity check and repair */
149 struct btrfs_work work;
150
151 /* Mark the parity blocks which have data */
152 unsigned long *dbitmap;
153
154 /*
155 * Mark the parity blocks which have data, but errors happen when
156 * read data or check data
157 */
158 unsigned long *ebitmap;
159
160 unsigned long bitmap[0];
161};
162
Stefan Behrensff023aa2012-11-06 11:43:11 +0100163struct scrub_wr_ctx {
164 struct scrub_bio *wr_curr_bio;
165 struct btrfs_device *tgtdev;
166 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
167 atomic_t flush_all_writes;
168 struct mutex wr_lock;
169};
170
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100171struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100172 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100173 struct btrfs_root *dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +0100174 int first_free;
175 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100176 atomic_t bios_in_flight;
177 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100178 spinlock_t list_lock;
179 wait_queue_head_t list_wait;
180 u16 csum_size;
181 struct list_head csum_list;
182 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100183 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100184 int pages_per_rd_bio;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400185 u32 sectorsize;
186 u32 nodesize;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100187
188 int is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100189 struct scrub_wr_ctx wr_ctx;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100190
Arne Jansena2de7332011-03-08 14:14:00 +0100191 /*
192 * statistics
193 */
194 struct btrfs_scrub_progress stat;
195 spinlock_t stat_lock;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000196
197 /*
198 * Use a ref counter to avoid use-after-free issues. Scrub workers
199 * decrement bios_in_flight and workers_pending and then do a wakeup
200 * on the list_wait wait queue. We must ensure the main scrub task
201 * doesn't free the scrub context before or while the workers are
202 * doing the wakeup() call.
203 */
204 atomic_t refs;
Arne Jansena2de7332011-03-08 14:14:00 +0100205};
206
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200207struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100208 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100209 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200210 u64 logical;
211 struct btrfs_root *root;
212 struct btrfs_work work;
213 int mirror_num;
214};
215
Josef Bacik652f25a2013-09-12 16:58:28 -0400216struct scrub_nocow_inode {
217 u64 inum;
218 u64 offset;
219 u64 root;
220 struct list_head list;
221};
222
Stefan Behrensff023aa2012-11-06 11:43:11 +0100223struct scrub_copy_nocow_ctx {
224 struct scrub_ctx *sctx;
225 u64 logical;
226 u64 len;
227 int mirror_num;
228 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -0400229 struct list_head inodes;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100230 struct btrfs_work work;
231};
232
Jan Schmidt558540c2011-06-13 19:59:12 +0200233struct scrub_warning {
234 struct btrfs_path *path;
235 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200236 const char *errstr;
237 sector_t sector;
238 u64 logical;
239 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200240};
241
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100242static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
243static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
244static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
245static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400246static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Zhao Leibe50a8d2015-01-20 15:11:42 +0800247static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100248 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100249static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
250 struct scrub_block *sblock, int is_metadata,
251 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800252 u16 csum_size, int retry_failed_mirror);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400253static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
254 struct scrub_block *sblock,
255 int is_metadata, int have_csum,
256 const u8 *csum, u64 generation,
257 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400258static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800259 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400260static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
261 struct scrub_block *sblock_good,
262 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100263static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
264static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
265 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400266static int scrub_checksum_data(struct scrub_block *sblock);
267static int scrub_checksum_tree_block(struct scrub_block *sblock);
268static int scrub_checksum_super(struct scrub_block *sblock);
269static void scrub_block_get(struct scrub_block *sblock);
270static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100271static void scrub_page_get(struct scrub_page *spage);
272static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800273static void scrub_parity_get(struct scrub_parity *sparity);
274static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100275static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
276 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100277static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100278 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100279 u64 gen, int mirror_num, u8 *csum, int force,
280 u64 physical_for_dev_replace);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400281static void scrub_bio_end_io(struct bio *bio, int err);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400282static void scrub_bio_end_io_worker(struct btrfs_work *work);
283static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100284static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
285 u64 extent_logical, u64 extent_len,
286 u64 *extent_physical,
287 struct btrfs_device **extent_dev,
288 int *extent_mirror_num);
289static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
290 struct scrub_wr_ctx *wr_ctx,
291 struct btrfs_fs_info *fs_info,
292 struct btrfs_device *dev,
293 int is_dev_replace);
294static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
295static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
296 struct scrub_page *spage);
297static void scrub_wr_submit(struct scrub_ctx *sctx);
298static void scrub_wr_bio_end_io(struct bio *bio, int err);
299static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
300static int write_page_nocow(struct scrub_ctx *sctx,
301 u64 physical_for_dev_replace, struct page *page);
302static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400303 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100304static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
305 int mirror_num, u64 physical_for_dev_replace);
306static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800307static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800308static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000309static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400310
311
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100312static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
313{
Filipe Mananaf55985f2015-02-09 21:14:24 +0000314 atomic_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100315 atomic_inc(&sctx->bios_in_flight);
316}
317
318static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
319{
320 atomic_dec(&sctx->bios_in_flight);
321 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000322 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100323}
324
Wang Shilongcb7ab022013-12-04 21:16:53 +0800325static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800326{
327 while (atomic_read(&fs_info->scrub_pause_req)) {
328 mutex_unlock(&fs_info->scrub_lock);
329 wait_event(fs_info->scrub_pause_wait,
330 atomic_read(&fs_info->scrub_pause_req) == 0);
331 mutex_lock(&fs_info->scrub_lock);
332 }
333}
334
Zhaolei0e22be82015-08-05 16:43:28 +0800335static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800336{
337 atomic_inc(&fs_info->scrubs_paused);
338 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800339}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800340
Zhaolei0e22be82015-08-05 16:43:28 +0800341static void scrub_pause_off(struct btrfs_fs_info *fs_info)
342{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800343 mutex_lock(&fs_info->scrub_lock);
344 __scrub_blocked_if_needed(fs_info);
345 atomic_dec(&fs_info->scrubs_paused);
346 mutex_unlock(&fs_info->scrub_lock);
347
348 wake_up(&fs_info->scrub_pause_wait);
349}
350
Zhaolei0e22be82015-08-05 16:43:28 +0800351static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
352{
353 scrub_pause_on(fs_info);
354 scrub_pause_off(fs_info);
355}
356
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100357/*
358 * used for workers that require transaction commits (i.e., for the
359 * NOCOW case)
360 */
361static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
362{
363 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
364
Filipe Mananaf55985f2015-02-09 21:14:24 +0000365 atomic_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100366 /*
367 * increment scrubs_running to prevent cancel requests from
368 * completing as long as a worker is running. we must also
369 * increment scrubs_paused to prevent deadlocking on pause
370 * requests used for transactions commits (as the worker uses a
371 * transaction context). it is safe to regard the worker
372 * as paused for all matters practical. effectively, we only
373 * avoid cancellation requests from completing.
374 */
375 mutex_lock(&fs_info->scrub_lock);
376 atomic_inc(&fs_info->scrubs_running);
377 atomic_inc(&fs_info->scrubs_paused);
378 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800379
380 /*
381 * check if @scrubs_running=@scrubs_paused condition
382 * inside wait_event() is not an atomic operation.
383 * which means we may inc/dec @scrub_running/paused
384 * at any time. Let's wake up @scrub_pause_wait as
385 * much as we can to let commit transaction blocked less.
386 */
387 wake_up(&fs_info->scrub_pause_wait);
388
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100389 atomic_inc(&sctx->workers_pending);
390}
391
392/* used for workers that require transaction commits */
393static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
394{
395 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
396
397 /*
398 * see scrub_pending_trans_workers_inc() why we're pretending
399 * to be paused in the scrub counters
400 */
401 mutex_lock(&fs_info->scrub_lock);
402 atomic_dec(&fs_info->scrubs_running);
403 atomic_dec(&fs_info->scrubs_paused);
404 mutex_unlock(&fs_info->scrub_lock);
405 atomic_dec(&sctx->workers_pending);
406 wake_up(&fs_info->scrub_pause_wait);
407 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000408 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100409}
410
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100411static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100412{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100413 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100414 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100415 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100416 struct btrfs_ordered_sum, list);
417 list_del(&sum->list);
418 kfree(sum);
419 }
420}
421
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100422static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100423{
424 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100425
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100426 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100427 return;
428
Stefan Behrensff023aa2012-11-06 11:43:11 +0100429 scrub_free_wr_ctx(&sctx->wr_ctx);
430
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400431 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100432 if (sctx->curr != -1) {
433 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400434
435 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100436 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400437 scrub_block_put(sbio->pagev[i]->sblock);
438 }
439 bio_put(sbio->bio);
440 }
441
Stefan Behrensff023aa2012-11-06 11:43:11 +0100442 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100443 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100444
445 if (!sbio)
446 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100447 kfree(sbio);
448 }
449
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100450 scrub_free_csums(sctx);
451 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100452}
453
Filipe Mananaf55985f2015-02-09 21:14:24 +0000454static void scrub_put_ctx(struct scrub_ctx *sctx)
455{
456 if (atomic_dec_and_test(&sctx->refs))
457 scrub_free_ctx(sctx);
458}
459
Arne Jansena2de7332011-03-08 14:14:00 +0100460static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100461struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100462{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100463 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100464 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100465 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100466 int pages_per_rd_bio;
467 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100468
Stefan Behrensff023aa2012-11-06 11:43:11 +0100469 /*
470 * the setting of pages_per_rd_bio is correct for scrub but might
471 * be wrong for the dev_replace code where we might read from
472 * different devices in the initial huge bios. However, that
473 * code is able to correctly handle the case when adding a page
474 * to a bio fails.
475 */
476 if (dev->bdev)
477 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
478 bio_get_nr_vecs(dev->bdev));
479 else
480 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100481 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
482 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100483 goto nomem;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000484 atomic_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100485 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100486 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100487 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100488 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100489 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100490 struct scrub_bio *sbio;
491
492 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
493 if (!sbio)
494 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100495 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100496
Arne Jansena2de7332011-03-08 14:14:00 +0100497 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100498 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400499 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800500 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
501 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100502
Stefan Behrensff023aa2012-11-06 11:43:11 +0100503 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100504 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200505 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100506 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100507 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100508 sctx->first_free = 0;
509 sctx->nodesize = dev->dev_root->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100510 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100511 atomic_set(&sctx->bios_in_flight, 0);
512 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100513 atomic_set(&sctx->cancel_req, 0);
514 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
515 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100516
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100517 spin_lock_init(&sctx->list_lock);
518 spin_lock_init(&sctx->stat_lock);
519 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100520
521 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
522 fs_info->dev_replace.tgtdev, is_dev_replace);
523 if (ret) {
524 scrub_free_ctx(sctx);
525 return ERR_PTR(ret);
526 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100527 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100528
529nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100530 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100531 return ERR_PTR(-ENOMEM);
532}
533
Stefan Behrensff023aa2012-11-06 11:43:11 +0100534static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
535 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200536{
537 u64 isize;
538 u32 nlink;
539 int ret;
540 int i;
541 struct extent_buffer *eb;
542 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100543 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200544 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
545 struct inode_fs_paths *ipath = NULL;
546 struct btrfs_root *local_root;
547 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100548 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200549
550 root_key.objectid = root;
551 root_key.type = BTRFS_ROOT_ITEM_KEY;
552 root_key.offset = (u64)-1;
553 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
554 if (IS_ERR(local_root)) {
555 ret = PTR_ERR(local_root);
556 goto err;
557 }
558
David Sterba14692cc2015-01-02 18:55:46 +0100559 /*
560 * this makes the path point to (inum INODE_ITEM ioff)
561 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100562 key.objectid = inum;
563 key.type = BTRFS_INODE_ITEM_KEY;
564 key.offset = 0;
565
566 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200567 if (ret) {
568 btrfs_release_path(swarn->path);
569 goto err;
570 }
571
572 eb = swarn->path->nodes[0];
573 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
574 struct btrfs_inode_item);
575 isize = btrfs_inode_size(eb, inode_item);
576 nlink = btrfs_inode_nlink(eb, inode_item);
577 btrfs_release_path(swarn->path);
578
579 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300580 if (IS_ERR(ipath)) {
581 ret = PTR_ERR(ipath);
582 ipath = NULL;
583 goto err;
584 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200585 ret = paths_from_inode(inum, ipath);
586
587 if (ret < 0)
588 goto err;
589
590 /*
591 * we deliberately ignore the bit ipath might have been too small to
592 * hold all of the paths here
593 */
594 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Frank Holtonefe120a2013-12-20 11:37:06 -0500595 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200596 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
597 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400598 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200599 (unsigned long long)swarn->sector, root, inum, offset,
600 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500601 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200602
603 free_ipath(ipath);
604 return 0;
605
606err:
Frank Holtonefe120a2013-12-20 11:37:06 -0500607 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200608 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
609 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400610 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200611 (unsigned long long)swarn->sector, root, inum, offset, ret);
612
613 free_ipath(ipath);
614 return 0;
615}
616
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400617static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200618{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100619 struct btrfs_device *dev;
620 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200621 struct btrfs_path *path;
622 struct btrfs_key found_key;
623 struct extent_buffer *eb;
624 struct btrfs_extent_item *ei;
625 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200626 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100627 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600628 u64 flags = 0;
629 u64 ref_root;
630 u32 item_size;
631 u8 ref_level;
Liu Bo69917e42012-09-07 20:01:28 -0600632 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200633
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100634 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100635 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100636 fs_info = sblock->sctx->dev_root->fs_info;
637
Jan Schmidt558540c2011-06-13 19:59:12 +0200638 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200639 if (!path)
640 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200641
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100642 swarn.sector = (sblock->pagev[0]->physical) >> 9;
643 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200644 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100645 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200646
Liu Bo69917e42012-09-07 20:01:28 -0600647 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
648 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200649 if (ret < 0)
650 goto out;
651
Jan Schmidt4692cf52011-12-02 14:56:41 +0100652 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200653 swarn.extent_item_size = found_key.offset;
654
655 eb = path->nodes[0];
656 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
657 item_size = btrfs_item_size_nr(eb, path->slots[0]);
658
Liu Bo69917e42012-09-07 20:01:28 -0600659 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200660 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800661 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
662 item_size, &ref_root,
663 &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400664 printk_in_rcu(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -0500665 "BTRFS: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200666 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400667 "%llu\n", errstr, swarn.logical,
668 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200669 (unsigned long long)swarn.sector,
670 ref_level ? "node" : "leaf",
671 ret < 0 ? -1 : ref_level,
672 ret < 0 ? -1 : ref_root);
673 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600674 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200675 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600676 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200677 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100678 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100679 iterate_extent_inodes(fs_info, found_key.objectid,
680 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200681 scrub_print_warning_inode, &swarn);
682 }
683
684out:
685 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200686}
687
Stefan Behrensff023aa2012-11-06 11:43:11 +0100688static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200689{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200690 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200691 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100692 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200693 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200694 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200695 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200696 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000697 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200698 u64 end = offset + PAGE_SIZE - 1;
699 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000700 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200701
702 key.objectid = root;
703 key.type = BTRFS_ROOT_ITEM_KEY;
704 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000705
706 fs_info = fixup->root->fs_info;
707 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
708
709 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
710 if (IS_ERR(local_root)) {
711 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200712 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000713 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200714
715 key.type = BTRFS_INODE_ITEM_KEY;
716 key.objectid = inum;
717 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000718 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
719 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200720 if (IS_ERR(inode))
721 return PTR_ERR(inode);
722
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200723 index = offset >> PAGE_CACHE_SHIFT;
724
725 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200726 if (!page) {
727 ret = -ENOMEM;
728 goto out;
729 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200730
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200731 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200732 if (PageDirty(page)) {
733 /*
734 * we need to write the data to the defect sector. the
735 * data that was in that sector is not in memory,
736 * because the page was modified. we must not write the
737 * modified page to that sector.
738 *
739 * TODO: what could be done here: wait for the delalloc
740 * runner to write out that page (might involve
741 * COW) and see whether the sector is still
742 * referenced afterwards.
743 *
744 * For the meantime, we'll treat this error
745 * incorrectable, although there is a chance that a
746 * later scrub will find the bad sector again and that
747 * there's no dirty page in memory, then.
748 */
749 ret = -EIO;
750 goto out;
751 }
Miao Xie1203b682014-09-12 18:44:01 +0800752 ret = repair_io_failure(inode, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200753 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800754 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200755 fixup->mirror_num);
756 unlock_page(page);
757 corrected = !ret;
758 } else {
759 /*
760 * we need to get good data first. the general readpage path
761 * will call repair_io_failure for us, we just have to make
762 * sure we read the bad mirror.
763 */
764 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
765 EXTENT_DAMAGED, GFP_NOFS);
766 if (ret) {
767 /* set_extent_bits should give proper error */
768 WARN_ON(ret > 0);
769 if (ret > 0)
770 ret = -EFAULT;
771 goto out;
772 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200773
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200774 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
775 btrfs_get_extent,
776 fixup->mirror_num);
777 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200778
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200779 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
780 end, EXTENT_DAMAGED, 0, NULL);
781 if (!corrected)
782 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
783 EXTENT_DAMAGED, GFP_NOFS);
784 }
785
786out:
787 if (page)
788 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200789
790 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200791
792 if (ret < 0)
793 return ret;
794
795 if (ret == 0 && corrected) {
796 /*
797 * we only need to call readpage for one of the inodes belonging
798 * to this extent. so make iterate_extent_inodes stop
799 */
800 return 1;
801 }
802
803 return -EIO;
804}
805
806static void scrub_fixup_nodatasum(struct btrfs_work *work)
807{
808 int ret;
809 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100810 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200811 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200812 struct btrfs_path *path;
813 int uncorrectable = 0;
814
815 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100816 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200817
818 path = btrfs_alloc_path();
819 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100820 spin_lock(&sctx->stat_lock);
821 ++sctx->stat.malloc_errors;
822 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200823 uncorrectable = 1;
824 goto out;
825 }
826
827 trans = btrfs_join_transaction(fixup->root);
828 if (IS_ERR(trans)) {
829 uncorrectable = 1;
830 goto out;
831 }
832
833 /*
834 * the idea is to trigger a regular read through the standard path. we
835 * read a page from the (failed) logical address by specifying the
836 * corresponding copynum of the failed sector. thus, that readpage is
837 * expected to fail.
838 * that is the point where on-the-fly error correction will kick in
839 * (once it's finished) and rewrite the failed sector if a good copy
840 * can be found.
841 */
842 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
843 path, scrub_fixup_readpage,
844 fixup);
845 if (ret < 0) {
846 uncorrectable = 1;
847 goto out;
848 }
849 WARN_ON(ret != 1);
850
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100851 spin_lock(&sctx->stat_lock);
852 ++sctx->stat.corrected_errors;
853 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200854
855out:
856 if (trans && !IS_ERR(trans))
857 btrfs_end_transaction(trans, fixup->root);
858 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100859 spin_lock(&sctx->stat_lock);
860 ++sctx->stat.uncorrectable_errors;
861 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100862 btrfs_dev_replace_stats_inc(
863 &sctx->dev_root->fs_info->dev_replace.
864 num_uncorrectable_read_errors);
Frank Holtonefe120a2013-12-20 11:37:06 -0500865 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
866 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200867 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200868 }
869
870 btrfs_free_path(path);
871 kfree(fixup);
872
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100873 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200874}
875
Miao Xieaf8e2d12014-10-23 14:42:50 +0800876static inline void scrub_get_recover(struct scrub_recover *recover)
877{
878 atomic_inc(&recover->refs);
879}
880
881static inline void scrub_put_recover(struct scrub_recover *recover)
882{
883 if (atomic_dec_and_test(&recover->refs)) {
Zhao Lei6e9606d2015-01-20 15:11:34 +0800884 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800885 kfree(recover);
886 }
887}
888
Arne Jansena2de7332011-03-08 14:14:00 +0100889/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400890 * scrub_handle_errored_block gets called when either verification of the
891 * pages failed or the bio failed to read, e.g. with EIO. In the latter
892 * case, this function handles all pages in the bio, even though only one
893 * may be bad.
894 * The goal of this function is to repair the errored block by using the
895 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100896 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400897static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100898{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100899 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100900 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400901 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100902 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400903 u64 logical;
904 u64 generation;
905 unsigned int failed_mirror_index;
906 unsigned int is_metadata;
907 unsigned int have_csum;
908 u8 *csum;
909 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
910 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100911 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400912 int mirror_index;
913 int page_num;
914 int success;
915 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
916 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100917
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400918 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100919 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000920 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
921 /*
922 * if we find an error in a super block, we just report it.
923 * They will get written with the next transaction commit
924 * anyway
925 */
926 spin_lock(&sctx->stat_lock);
927 ++sctx->stat.super_errors;
928 spin_unlock(&sctx->stat_lock);
929 return 0;
930 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400931 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100932 logical = sblock_to_check->pagev[0]->logical;
933 generation = sblock_to_check->pagev[0]->generation;
934 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
935 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
936 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400937 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100938 have_csum = sblock_to_check->pagev[0]->have_csum;
939 csum = sblock_to_check->pagev[0]->csum;
940 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400941
Stefan Behrensff023aa2012-11-06 11:43:11 +0100942 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
943 sblocks_for_recheck = NULL;
944 goto nodatasum_case;
945 }
946
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400947 /*
948 * read all mirrors one after the other. This includes to
949 * re-read the extent or metadata block that failed (that was
950 * the cause that this fixup code is called) another time,
951 * page by page this time in order to know which pages
952 * caused I/O errors and which ones are good (for all mirrors).
953 * It is the goal to handle the situation when more than one
954 * mirror contains I/O errors, but the errors do not
955 * overlap, i.e. the data can be repaired by selecting the
956 * pages from those mirrors without I/O error on the
957 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
958 * would be that mirror #1 has an I/O error on the first page,
959 * the second page is good, and mirror #2 has an I/O error on
960 * the second page, but the first page is good.
961 * Then the first page of the first mirror can be repaired by
962 * taking the first page of the second mirror, and the
963 * second page of the second mirror can be repaired by
964 * copying the contents of the 2nd page of the 1st mirror.
965 * One more note: if the pages of one mirror contain I/O
966 * errors, the checksum cannot be verified. In order to get
967 * the best data for repairing, the first attempt is to find
968 * a mirror without I/O errors and with a validated checksum.
969 * Only if this is not possible, the pages are picked from
970 * mirrors with I/O errors without considering the checksum.
971 * If the latter is the case, at the end, the checksum of the
972 * repaired area is verified in order to correctly maintain
973 * the statistics.
974 */
975
David Sterba31e818f2015-02-20 18:00:26 +0100976 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
977 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400978 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100979 spin_lock(&sctx->stat_lock);
980 sctx->stat.malloc_errors++;
981 sctx->stat.read_errors++;
982 sctx->stat.uncorrectable_errors++;
983 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100984 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400985 goto out;
986 }
987
988 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800989 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400990 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100991 spin_lock(&sctx->stat_lock);
992 sctx->stat.read_errors++;
993 sctx->stat.uncorrectable_errors++;
994 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100995 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400996 goto out;
997 }
998 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
999 sblock_bad = sblocks_for_recheck + failed_mirror_index;
1000
1001 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001002 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001003 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001004
1005 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
1006 sblock_bad->no_io_error_seen) {
1007 /*
1008 * the error disappeared after reading page by page, or
1009 * the area was part of a huge bio and other parts of the
1010 * bio caused I/O errors, or the block layer merged several
1011 * read requests into one and the error is caused by a
1012 * different bio (usually one of the two latter cases is
1013 * the cause)
1014 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001015 spin_lock(&sctx->stat_lock);
1016 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001017 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001018 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001019
Stefan Behrensff023aa2012-11-06 11:43:11 +01001020 if (sctx->is_dev_replace)
1021 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001022 goto out;
1023 }
1024
1025 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001026 spin_lock(&sctx->stat_lock);
1027 sctx->stat.read_errors++;
1028 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001029 if (__ratelimit(&_rs))
1030 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001031 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001032 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001033 spin_lock(&sctx->stat_lock);
1034 sctx->stat.csum_errors++;
1035 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001036 if (__ratelimit(&_rs))
1037 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001038 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001039 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001040 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001041 spin_lock(&sctx->stat_lock);
1042 sctx->stat.verify_errors++;
1043 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001044 if (__ratelimit(&_rs))
1045 scrub_print_warning("checksum/header error",
1046 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001047 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001048 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001049 BTRFS_DEV_STAT_GENERATION_ERRS);
1050 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001051 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001052 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001053 }
1054
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001055 if (sctx->readonly) {
1056 ASSERT(!sctx->is_dev_replace);
1057 goto out;
1058 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001059
1060 if (!is_metadata && !have_csum) {
1061 struct scrub_fixup_nodatasum *fixup_nodatasum;
1062
Stefan Behrensff023aa2012-11-06 11:43:11 +01001063 WARN_ON(sctx->is_dev_replace);
1064
Zhao Leib25c94c2015-01-20 15:11:35 +08001065nodatasum_case:
1066
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001067 /*
1068 * !is_metadata and !have_csum, this means that the data
1069 * might not be COW'ed, that it might be modified
1070 * concurrently. The general strategy to work on the
1071 * commit root does not help in the case when COW is not
1072 * used.
1073 */
1074 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1075 if (!fixup_nodatasum)
1076 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001077 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001078 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001079 fixup_nodatasum->logical = logical;
1080 fixup_nodatasum->root = fs_info->extent_root;
1081 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001082 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001083 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1084 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001085 btrfs_queue_work(fs_info->scrub_workers,
1086 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001087 goto out;
1088 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001089
1090 /*
1091 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001092 * checksums.
1093 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001094 * errors and also does not have a checksum error.
1095 * If one is found, and if a checksum is present, the full block
1096 * that is known to contain an error is rewritten. Afterwards
1097 * the block is known to be corrected.
1098 * If a mirror is found which is completely correct, and no
1099 * checksum is present, only those pages are rewritten that had
1100 * an I/O error in the block to be repaired, since it cannot be
1101 * determined, which copy of the other pages is better (and it
1102 * could happen otherwise that a correct page would be
1103 * overwritten by a bad one).
1104 */
1105 for (mirror_index = 0;
1106 mirror_index < BTRFS_MAX_MIRRORS &&
1107 sblocks_for_recheck[mirror_index].page_count > 0;
1108 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001109 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001110
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001111 if (mirror_index == failed_mirror_index)
1112 continue;
1113 sblock_other = sblocks_for_recheck + mirror_index;
1114
1115 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001116 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1117 have_csum, csum, generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001118 sctx->csum_size, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001119
1120 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001121 !sblock_other->checksum_error &&
1122 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001123 if (sctx->is_dev_replace) {
1124 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001125 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001126 } else {
1127 ret = scrub_repair_block_from_good_copy(
1128 sblock_bad, sblock_other);
1129 if (!ret)
1130 goto corrected_error;
1131 }
Arne Jansena2de7332011-03-08 14:14:00 +01001132 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001133 }
1134
Zhao Leib968fed2015-01-20 15:11:41 +08001135 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1136 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001137
1138 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001139 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001140 * repaired, continue by picking good copies of those pages.
1141 * Select the good pages from mirrors to rewrite bad pages from
1142 * the area to fix. Afterwards verify the checksum of the block
1143 * that is supposed to be repaired. This verification step is
1144 * only done for the purpose of statistic counting and for the
1145 * final scrub report, whether errors remain.
1146 * A perfect algorithm could make use of the checksum and try
1147 * all possible combinations of pages from the different mirrors
1148 * until the checksum verification succeeds. For example, when
1149 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1150 * of mirror #2 is readable but the final checksum test fails,
1151 * then the 2nd page of mirror #3 could be tried, whether now
1152 * the final checksum succeedes. But this would be a rare
1153 * exception and is therefore not implemented. At least it is
1154 * avoided that the good copy is overwritten.
1155 * A more useful improvement would be to pick the sectors
1156 * without I/O error based on sector sizes (512 bytes on legacy
1157 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1158 * mirror could be repaired by taking 512 byte of a different
1159 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1160 * area are unreadable.
1161 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001162 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001163 for (page_num = 0; page_num < sblock_bad->page_count;
1164 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001165 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001166 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001167
Zhao Leib968fed2015-01-20 15:11:41 +08001168 /* skip no-io-error page in scrub */
1169 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001170 continue;
1171
Zhao Leib968fed2015-01-20 15:11:41 +08001172 /* try to find no-io-error page in mirrors */
1173 if (page_bad->io_error) {
1174 for (mirror_index = 0;
1175 mirror_index < BTRFS_MAX_MIRRORS &&
1176 sblocks_for_recheck[mirror_index].page_count > 0;
1177 mirror_index++) {
1178 if (!sblocks_for_recheck[mirror_index].
1179 pagev[page_num]->io_error) {
1180 sblock_other = sblocks_for_recheck +
1181 mirror_index;
1182 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001183 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001184 }
Zhao Leib968fed2015-01-20 15:11:41 +08001185 if (!sblock_other)
1186 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001187 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001188
Zhao Leib968fed2015-01-20 15:11:41 +08001189 if (sctx->is_dev_replace) {
1190 /*
1191 * did not find a mirror to fetch the page
1192 * from. scrub_write_page_to_dev_replace()
1193 * handles this case (page->io_error), by
1194 * filling the block with zeros before
1195 * submitting the write request
1196 */
1197 if (!sblock_other)
1198 sblock_other = sblock_bad;
1199
1200 if (scrub_write_page_to_dev_replace(sblock_other,
1201 page_num) != 0) {
1202 btrfs_dev_replace_stats_inc(
1203 &sctx->dev_root->
1204 fs_info->dev_replace.
1205 num_write_errors);
1206 success = 0;
1207 }
1208 } else if (sblock_other) {
1209 ret = scrub_repair_page_from_good_copy(sblock_bad,
1210 sblock_other,
1211 page_num, 0);
1212 if (0 == ret)
1213 page_bad->io_error = 0;
1214 else
1215 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001216 }
1217 }
1218
Zhao Leib968fed2015-01-20 15:11:41 +08001219 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001220 if (is_metadata || have_csum) {
1221 /*
1222 * need to verify the checksum now that all
1223 * sectors on disk are repaired (the write
1224 * request for data to be repaired is on its way).
1225 * Just be lazy and use scrub_recheck_block()
1226 * which re-reads the data before the checksum
1227 * is verified, but most likely the data comes out
1228 * of the page cache.
1229 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001230 scrub_recheck_block(fs_info, sblock_bad,
1231 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001232 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001233 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001234 !sblock_bad->checksum_error &&
1235 sblock_bad->no_io_error_seen)
1236 goto corrected_error;
1237 else
1238 goto did_not_correct_error;
1239 } else {
1240corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001241 spin_lock(&sctx->stat_lock);
1242 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001243 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001244 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001245 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001246 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001247 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001248 }
1249 } else {
1250did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001251 spin_lock(&sctx->stat_lock);
1252 sctx->stat.uncorrectable_errors++;
1253 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001254 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001255 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001256 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001257 }
1258
1259out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001260 if (sblocks_for_recheck) {
1261 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1262 mirror_index++) {
1263 struct scrub_block *sblock = sblocks_for_recheck +
1264 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001265 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001266 int page_index;
1267
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001268 for (page_index = 0; page_index < sblock->page_count;
1269 page_index++) {
1270 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001271 recover = sblock->pagev[page_index]->recover;
1272 if (recover) {
1273 scrub_put_recover(recover);
1274 sblock->pagev[page_index]->recover =
1275 NULL;
1276 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001277 scrub_page_put(sblock->pagev[page_index]);
1278 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001279 }
1280 kfree(sblocks_for_recheck);
1281 }
1282
1283 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001284}
1285
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001286static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001287{
Zhao Lei10f11902015-01-20 15:11:43 +08001288 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1289 return 2;
1290 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1291 return 3;
1292 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001293 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001294}
1295
Zhao Lei10f11902015-01-20 15:11:43 +08001296static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1297 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001298 u64 mapped_length,
1299 int nstripes, int mirror,
1300 int *stripe_index,
1301 u64 *stripe_offset)
1302{
1303 int i;
1304
Zhao Leiffe2d202015-01-20 15:11:44 +08001305 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001306 /* RAID5/6 */
1307 for (i = 0; i < nstripes; i++) {
1308 if (raid_map[i] == RAID6_Q_STRIPE ||
1309 raid_map[i] == RAID5_P_STRIPE)
1310 continue;
1311
1312 if (logical >= raid_map[i] &&
1313 logical < raid_map[i] + mapped_length)
1314 break;
1315 }
1316
1317 *stripe_index = i;
1318 *stripe_offset = logical - raid_map[i];
1319 } else {
1320 /* The other RAID type */
1321 *stripe_index = mirror;
1322 *stripe_offset = 0;
1323 }
1324}
1325
Zhao Leibe50a8d2015-01-20 15:11:42 +08001326static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001327 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001328{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001329 struct scrub_ctx *sctx = original_sblock->sctx;
1330 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1331 u64 length = original_sblock->page_count * PAGE_SIZE;
1332 u64 logical = original_sblock->pagev[0]->logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001333 struct scrub_recover *recover;
1334 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001335 u64 sublen;
1336 u64 mapped_length;
1337 u64 stripe_offset;
1338 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001339 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001340 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001341 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001342 int ret;
1343
1344 /*
Zhao Lei57019342015-01-20 15:11:45 +08001345 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001346 * are not used (and not set) in the blocks that are used for
1347 * the recheck procedure
1348 */
1349
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001350 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001351 sublen = min_t(u64, length, PAGE_SIZE);
1352 mapped_length = sublen;
1353 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001354
1355 /*
1356 * with a length of PAGE_SIZE, each returned stripe
1357 * represents one mirror
1358 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001359 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001360 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001361 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001362 btrfs_put_bbio(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001363 return -EIO;
1364 }
1365
Miao Xieaf8e2d12014-10-23 14:42:50 +08001366 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1367 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001368 btrfs_put_bbio(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001369 return -ENOMEM;
1370 }
1371
1372 atomic_set(&recover->refs, 1);
1373 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001374 recover->map_length = mapped_length;
1375
Stefan Behrensff023aa2012-11-06 11:43:11 +01001376 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001377
Zhao Leibe50a8d2015-01-20 15:11:42 +08001378 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001379
Miao Xieaf8e2d12014-10-23 14:42:50 +08001380 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001381 mirror_index++) {
1382 struct scrub_block *sblock;
1383 struct scrub_page *page;
1384
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001385 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001386 sblock->sctx = sctx;
1387 page = kzalloc(sizeof(*page), GFP_NOFS);
1388 if (!page) {
1389leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001390 spin_lock(&sctx->stat_lock);
1391 sctx->stat.malloc_errors++;
1392 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001393 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001394 return -ENOMEM;
1395 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001396 scrub_page_get(page);
1397 sblock->pagev[page_index] = page;
1398 page->logical = logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001399
Zhao Lei10f11902015-01-20 15:11:43 +08001400 scrub_stripe_index_and_offset(logical,
1401 bbio->map_type,
1402 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001403 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001404 bbio->num_stripes -
1405 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001406 mirror_index,
1407 &stripe_index,
1408 &stripe_offset);
1409 page->physical = bbio->stripes[stripe_index].physical +
1410 stripe_offset;
1411 page->dev = bbio->stripes[stripe_index].dev;
1412
Stefan Behrensff023aa2012-11-06 11:43:11 +01001413 BUG_ON(page_index >= original_sblock->page_count);
1414 page->physical_for_dev_replace =
1415 original_sblock->pagev[page_index]->
1416 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001417 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001418 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001419 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001420 page->page = alloc_page(GFP_NOFS);
1421 if (!page->page)
1422 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001423
1424 scrub_get_recover(recover);
1425 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001426 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001427 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001428 length -= sublen;
1429 logical += sublen;
1430 page_index++;
1431 }
1432
1433 return 0;
1434}
1435
Miao Xieaf8e2d12014-10-23 14:42:50 +08001436struct scrub_bio_ret {
1437 struct completion event;
1438 int error;
1439};
1440
1441static void scrub_bio_wait_endio(struct bio *bio, int error)
1442{
1443 struct scrub_bio_ret *ret = bio->bi_private;
1444
1445 ret->error = error;
1446 complete(&ret->event);
1447}
1448
1449static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1450{
Zhao Lei10f11902015-01-20 15:11:43 +08001451 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001452 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001453}
1454
1455static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1456 struct bio *bio,
1457 struct scrub_page *page)
1458{
1459 struct scrub_bio_ret done;
1460 int ret;
1461
1462 init_completion(&done.event);
1463 done.error = 0;
1464 bio->bi_iter.bi_sector = page->logical >> 9;
1465 bio->bi_private = &done;
1466 bio->bi_end_io = scrub_bio_wait_endio;
1467
1468 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001469 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001470 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001471 if (ret)
1472 return ret;
1473
1474 wait_for_completion(&done.event);
1475 if (done.error)
1476 return -EIO;
1477
1478 return 0;
1479}
1480
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001481/*
1482 * this function will check the on disk data for checksum errors, header
1483 * errors and read I/O errors. If any I/O errors happen, the exact pages
1484 * which are errored are marked as being bad. The goal is to enable scrub
1485 * to take those pages that are not errored from all the mirrors so that
1486 * the pages that are errored in the just handled mirror can be repaired.
1487 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001488static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1489 struct scrub_block *sblock, int is_metadata,
1490 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001491 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001492{
1493 int page_num;
1494
1495 sblock->no_io_error_seen = 1;
1496 sblock->header_error = 0;
1497 sblock->checksum_error = 0;
1498
1499 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1500 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001501 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001502
Stefan Behrens442a4f62012-05-25 16:06:08 +02001503 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001504 page->io_error = 1;
1505 sblock->no_io_error_seen = 0;
1506 continue;
1507 }
1508
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001509 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001510 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001511 if (!bio) {
1512 page->io_error = 1;
1513 sblock->no_io_error_seen = 0;
1514 continue;
1515 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001516 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001517
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001518 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001519 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1520 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1521 sblock->no_io_error_seen = 0;
1522 } else {
1523 bio->bi_iter.bi_sector = page->physical >> 9;
1524
1525 if (btrfsic_submit_bio_wait(READ, bio))
1526 sblock->no_io_error_seen = 0;
1527 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001528
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001529 bio_put(bio);
1530 }
1531
1532 if (sblock->no_io_error_seen)
1533 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1534 have_csum, csum, generation,
1535 csum_size);
1536
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001537 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001538}
1539
Miao Xie17a9be22014-07-24 11:37:08 +08001540static inline int scrub_check_fsid(u8 fsid[],
1541 struct scrub_page *spage)
1542{
1543 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1544 int ret;
1545
1546 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1547 return !ret;
1548}
1549
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001550static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1551 struct scrub_block *sblock,
1552 int is_metadata, int have_csum,
1553 const u8 *csum, u64 generation,
1554 u16 csum_size)
1555{
1556 int page_num;
1557 u8 calculated_csum[BTRFS_CSUM_SIZE];
1558 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001559 void *mapped_buffer;
1560
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001561 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001562 if (is_metadata) {
1563 struct btrfs_header *h;
1564
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001565 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001566 h = (struct btrfs_header *)mapped_buffer;
1567
Qu Wenruo3cae2102013-07-16 11:19:18 +08001568 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001569 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001570 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001571 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001572 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001573 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001574 sblock->header_error = 1;
1575 sblock->generation_error = 1;
1576 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001577 csum = h->csum;
1578 } else {
1579 if (!have_csum)
1580 return;
1581
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001582 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001583 }
1584
1585 for (page_num = 0;;) {
1586 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001587 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001588 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1589 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1590 else
Liu Bob0496682013-03-14 14:57:45 +00001591 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001592
Linus Torvalds9613beb2012-03-30 12:44:29 -07001593 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001594 page_num++;
1595 if (page_num >= sblock->page_count)
1596 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001597 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001598
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001599 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001600 }
1601
1602 btrfs_csum_final(crc, calculated_csum);
1603 if (memcmp(calculated_csum, csum, csum_size))
1604 sblock->checksum_error = 1;
1605}
1606
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001607static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001608 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001609{
1610 int page_num;
1611 int ret = 0;
1612
1613 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1614 int ret_sub;
1615
1616 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1617 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001618 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001619 if (ret_sub)
1620 ret = ret_sub;
1621 }
1622
1623 return ret;
1624}
1625
1626static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1627 struct scrub_block *sblock_good,
1628 int page_num, int force_write)
1629{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001630 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1631 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001632
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001633 BUG_ON(page_bad->page == NULL);
1634 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001635 if (force_write || sblock_bad->header_error ||
1636 sblock_bad->checksum_error || page_bad->io_error) {
1637 struct bio *bio;
1638 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001639
Stefan Behrensff023aa2012-11-06 11:43:11 +01001640 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001641 printk_ratelimited(KERN_WARNING "BTRFS: "
1642 "scrub_repair_page_from_good_copy(bdev == NULL) "
1643 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001644 return -EIO;
1645 }
1646
Chris Mason9be33952013-05-17 18:30:14 -04001647 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001648 if (!bio)
1649 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001650 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001651 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001652
1653 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1654 if (PAGE_SIZE != ret) {
1655 bio_put(bio);
1656 return -EIO;
1657 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001658
Kent Overstreet33879d42013-11-23 22:33:32 -08001659 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001660 btrfs_dev_stat_inc_and_print(page_bad->dev,
1661 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001662 btrfs_dev_replace_stats_inc(
1663 &sblock_bad->sctx->dev_root->fs_info->
1664 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001665 bio_put(bio);
1666 return -EIO;
1667 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001668 bio_put(bio);
1669 }
1670
1671 return 0;
1672}
1673
Stefan Behrensff023aa2012-11-06 11:43:11 +01001674static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1675{
1676 int page_num;
1677
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001678 /*
1679 * This block is used for the check of the parity on the source device,
1680 * so the data needn't be written into the destination device.
1681 */
1682 if (sblock->sparity)
1683 return;
1684
Stefan Behrensff023aa2012-11-06 11:43:11 +01001685 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1686 int ret;
1687
1688 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1689 if (ret)
1690 btrfs_dev_replace_stats_inc(
1691 &sblock->sctx->dev_root->fs_info->dev_replace.
1692 num_write_errors);
1693 }
1694}
1695
1696static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1697 int page_num)
1698{
1699 struct scrub_page *spage = sblock->pagev[page_num];
1700
1701 BUG_ON(spage->page == NULL);
1702 if (spage->io_error) {
1703 void *mapped_buffer = kmap_atomic(spage->page);
1704
1705 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1706 flush_dcache_page(spage->page);
1707 kunmap_atomic(mapped_buffer);
1708 }
1709 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1710}
1711
1712static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1713 struct scrub_page *spage)
1714{
1715 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1716 struct scrub_bio *sbio;
1717 int ret;
1718
1719 mutex_lock(&wr_ctx->wr_lock);
1720again:
1721 if (!wr_ctx->wr_curr_bio) {
1722 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1723 GFP_NOFS);
1724 if (!wr_ctx->wr_curr_bio) {
1725 mutex_unlock(&wr_ctx->wr_lock);
1726 return -ENOMEM;
1727 }
1728 wr_ctx->wr_curr_bio->sctx = sctx;
1729 wr_ctx->wr_curr_bio->page_count = 0;
1730 }
1731 sbio = wr_ctx->wr_curr_bio;
1732 if (sbio->page_count == 0) {
1733 struct bio *bio;
1734
1735 sbio->physical = spage->physical_for_dev_replace;
1736 sbio->logical = spage->logical;
1737 sbio->dev = wr_ctx->tgtdev;
1738 bio = sbio->bio;
1739 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001740 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001741 if (!bio) {
1742 mutex_unlock(&wr_ctx->wr_lock);
1743 return -ENOMEM;
1744 }
1745 sbio->bio = bio;
1746 }
1747
1748 bio->bi_private = sbio;
1749 bio->bi_end_io = scrub_wr_bio_end_io;
1750 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001751 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001752 sbio->err = 0;
1753 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1754 spage->physical_for_dev_replace ||
1755 sbio->logical + sbio->page_count * PAGE_SIZE !=
1756 spage->logical) {
1757 scrub_wr_submit(sctx);
1758 goto again;
1759 }
1760
1761 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1762 if (ret != PAGE_SIZE) {
1763 if (sbio->page_count < 1) {
1764 bio_put(sbio->bio);
1765 sbio->bio = NULL;
1766 mutex_unlock(&wr_ctx->wr_lock);
1767 return -EIO;
1768 }
1769 scrub_wr_submit(sctx);
1770 goto again;
1771 }
1772
1773 sbio->pagev[sbio->page_count] = spage;
1774 scrub_page_get(spage);
1775 sbio->page_count++;
1776 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1777 scrub_wr_submit(sctx);
1778 mutex_unlock(&wr_ctx->wr_lock);
1779
1780 return 0;
1781}
1782
1783static void scrub_wr_submit(struct scrub_ctx *sctx)
1784{
1785 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1786 struct scrub_bio *sbio;
1787
1788 if (!wr_ctx->wr_curr_bio)
1789 return;
1790
1791 sbio = wr_ctx->wr_curr_bio;
1792 wr_ctx->wr_curr_bio = NULL;
1793 WARN_ON(!sbio->bio->bi_bdev);
1794 scrub_pending_bio_inc(sctx);
1795 /* process all writes in a single worker thread. Then the block layer
1796 * orders the requests before sending them to the driver which
1797 * doubled the write performance on spinning disks when measured
1798 * with Linux 3.5 */
1799 btrfsic_submit_bio(WRITE, sbio->bio);
1800}
1801
1802static void scrub_wr_bio_end_io(struct bio *bio, int err)
1803{
1804 struct scrub_bio *sbio = bio->bi_private;
1805 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1806
1807 sbio->err = err;
1808 sbio->bio = bio;
1809
Liu Bo9e0af232014-08-15 23:36:53 +08001810 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1811 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001812 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001813}
1814
1815static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1816{
1817 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1818 struct scrub_ctx *sctx = sbio->sctx;
1819 int i;
1820
1821 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1822 if (sbio->err) {
1823 struct btrfs_dev_replace *dev_replace =
1824 &sbio->sctx->dev_root->fs_info->dev_replace;
1825
1826 for (i = 0; i < sbio->page_count; i++) {
1827 struct scrub_page *spage = sbio->pagev[i];
1828
1829 spage->io_error = 1;
1830 btrfs_dev_replace_stats_inc(&dev_replace->
1831 num_write_errors);
1832 }
1833 }
1834
1835 for (i = 0; i < sbio->page_count; i++)
1836 scrub_page_put(sbio->pagev[i]);
1837
1838 bio_put(sbio->bio);
1839 kfree(sbio);
1840 scrub_pending_bio_dec(sctx);
1841}
1842
1843static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001844{
1845 u64 flags;
1846 int ret;
1847
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001848 WARN_ON(sblock->page_count < 1);
1849 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001850 ret = 0;
1851 if (flags & BTRFS_EXTENT_FLAG_DATA)
1852 ret = scrub_checksum_data(sblock);
1853 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1854 ret = scrub_checksum_tree_block(sblock);
1855 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1856 (void)scrub_checksum_super(sblock);
1857 else
1858 WARN_ON(1);
1859 if (ret)
1860 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001861
1862 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001863}
1864
1865static int scrub_checksum_data(struct scrub_block *sblock)
1866{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001867 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001868 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001869 u8 *on_disk_csum;
1870 struct page *page;
1871 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001872 u32 crc = ~(u32)0;
1873 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001874 u64 len;
1875 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001876
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001877 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001878 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001879 return 0;
1880
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001881 on_disk_csum = sblock->pagev[0]->csum;
1882 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001883 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001884
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001885 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001886 index = 0;
1887 for (;;) {
1888 u64 l = min_t(u64, len, PAGE_SIZE);
1889
Liu Bob0496682013-03-14 14:57:45 +00001890 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001891 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001892 len -= l;
1893 if (len == 0)
1894 break;
1895 index++;
1896 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001897 BUG_ON(!sblock->pagev[index]->page);
1898 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001899 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001900 }
1901
Arne Jansena2de7332011-03-08 14:14:00 +01001902 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001903 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001904 fail = 1;
1905
Arne Jansena2de7332011-03-08 14:14:00 +01001906 return fail;
1907}
1908
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001909static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001910{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001911 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001912 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001913 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001914 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001915 u8 calculated_csum[BTRFS_CSUM_SIZE];
1916 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1917 struct page *page;
1918 void *mapped_buffer;
1919 u64 mapped_size;
1920 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001921 u32 crc = ~(u32)0;
1922 int fail = 0;
1923 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001924 u64 len;
1925 int index;
1926
1927 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001928 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001929 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001930 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001931 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001932
1933 /*
1934 * we don't use the getter functions here, as we
1935 * a) don't have an extent buffer and
1936 * b) the page is already kmapped
1937 */
Arne Jansena2de7332011-03-08 14:14:00 +01001938
Qu Wenruo3cae2102013-07-16 11:19:18 +08001939 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001940 ++fail;
1941
Qu Wenruo3cae2102013-07-16 11:19:18 +08001942 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001943 ++fail;
1944
Miao Xie17a9be22014-07-24 11:37:08 +08001945 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001946 ++fail;
1947
1948 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1949 BTRFS_UUID_SIZE))
1950 ++fail;
1951
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001952 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001953 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1954 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1955 index = 0;
1956 for (;;) {
1957 u64 l = min_t(u64, len, mapped_size);
1958
Liu Bob0496682013-03-14 14:57:45 +00001959 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001960 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001961 len -= l;
1962 if (len == 0)
1963 break;
1964 index++;
1965 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001966 BUG_ON(!sblock->pagev[index]->page);
1967 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001968 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001969 mapped_size = PAGE_SIZE;
1970 p = mapped_buffer;
1971 }
1972
1973 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001974 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001975 ++crc_fail;
1976
Arne Jansena2de7332011-03-08 14:14:00 +01001977 return fail || crc_fail;
1978}
1979
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001980static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001981{
1982 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001983 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001984 u8 calculated_csum[BTRFS_CSUM_SIZE];
1985 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1986 struct page *page;
1987 void *mapped_buffer;
1988 u64 mapped_size;
1989 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001990 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001991 int fail_gen = 0;
1992 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001993 u64 len;
1994 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001995
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001996 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001997 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001998 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001999 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002000 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002001
Qu Wenruo3cae2102013-07-16 11:19:18 +08002002 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002003 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002004
Qu Wenruo3cae2102013-07-16 11:19:18 +08002005 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002006 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002007
Miao Xie17a9be22014-07-24 11:37:08 +08002008 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002009 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002010
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002011 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2012 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2013 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2014 index = 0;
2015 for (;;) {
2016 u64 l = min_t(u64, len, mapped_size);
2017
Liu Bob0496682013-03-14 14:57:45 +00002018 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002019 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002020 len -= l;
2021 if (len == 0)
2022 break;
2023 index++;
2024 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002025 BUG_ON(!sblock->pagev[index]->page);
2026 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002027 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002028 mapped_size = PAGE_SIZE;
2029 p = mapped_buffer;
2030 }
2031
2032 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002033 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002034 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002035
Stefan Behrens442a4f62012-05-25 16:06:08 +02002036 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002037 /*
2038 * if we find an error in a super block, we just report it.
2039 * They will get written with the next transaction commit
2040 * anyway
2041 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002042 spin_lock(&sctx->stat_lock);
2043 ++sctx->stat.super_errors;
2044 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002045 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002046 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002047 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2048 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002049 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002050 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002051 }
2052
Stefan Behrens442a4f62012-05-25 16:06:08 +02002053 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002054}
2055
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002056static void scrub_block_get(struct scrub_block *sblock)
2057{
Zhao Lei57019342015-01-20 15:11:45 +08002058 atomic_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002059}
2060
2061static void scrub_block_put(struct scrub_block *sblock)
2062{
Zhao Lei57019342015-01-20 15:11:45 +08002063 if (atomic_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002064 int i;
2065
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002066 if (sblock->sparity)
2067 scrub_parity_put(sblock->sparity);
2068
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002069 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002070 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002071 kfree(sblock);
2072 }
2073}
2074
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002075static void scrub_page_get(struct scrub_page *spage)
2076{
Zhao Lei57019342015-01-20 15:11:45 +08002077 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002078}
2079
2080static void scrub_page_put(struct scrub_page *spage)
2081{
Zhao Lei57019342015-01-20 15:11:45 +08002082 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002083 if (spage->page)
2084 __free_page(spage->page);
2085 kfree(spage);
2086 }
2087}
2088
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002089static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002090{
2091 struct scrub_bio *sbio;
2092
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002093 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002094 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002095
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002096 sbio = sctx->bios[sctx->curr];
2097 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002098 scrub_pending_bio_inc(sctx);
Omar Sandoval03679ad2015-06-19 11:52:48 -07002099 btrfsic_submit_bio(READ, sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002100}
2101
Stefan Behrensff023aa2012-11-06 11:43:11 +01002102static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2103 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002104{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002105 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002106 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002107 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002108
2109again:
2110 /*
2111 * grab a fresh bio or wait for one to become available
2112 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002113 while (sctx->curr == -1) {
2114 spin_lock(&sctx->list_lock);
2115 sctx->curr = sctx->first_free;
2116 if (sctx->curr != -1) {
2117 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2118 sctx->bios[sctx->curr]->next_free = -1;
2119 sctx->bios[sctx->curr]->page_count = 0;
2120 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002121 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002122 spin_unlock(&sctx->list_lock);
2123 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002124 }
2125 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002126 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002127 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002128 struct bio *bio;
2129
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002130 sbio->physical = spage->physical;
2131 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002132 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002133 bio = sbio->bio;
2134 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002135 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002136 if (!bio)
2137 return -ENOMEM;
2138 sbio->bio = bio;
2139 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002140
2141 bio->bi_private = sbio;
2142 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002143 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002144 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002145 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002146 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2147 spage->physical ||
2148 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002149 spage->logical ||
2150 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002151 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002152 goto again;
2153 }
2154
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002155 sbio->pagev[sbio->page_count] = spage;
2156 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2157 if (ret != PAGE_SIZE) {
2158 if (sbio->page_count < 1) {
2159 bio_put(sbio->bio);
2160 sbio->bio = NULL;
2161 return -EIO;
2162 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002163 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002164 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002165 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002166
Stefan Behrensff023aa2012-11-06 11:43:11 +01002167 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002168 atomic_inc(&sblock->outstanding_pages);
2169 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002170 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002171 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002172
2173 return 0;
2174}
2175
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002176static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002177 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002178 u64 gen, int mirror_num, u8 *csum, int force,
2179 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002180{
2181 struct scrub_block *sblock;
2182 int index;
2183
2184 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2185 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002186 spin_lock(&sctx->stat_lock);
2187 sctx->stat.malloc_errors++;
2188 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002189 return -ENOMEM;
2190 }
2191
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002192 /* one ref inside this function, plus one for each page added to
2193 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002194 atomic_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002195 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002196 sblock->no_io_error_seen = 1;
2197
2198 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002199 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002200 u64 l = min_t(u64, len, PAGE_SIZE);
2201
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002202 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2203 if (!spage) {
2204leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002205 spin_lock(&sctx->stat_lock);
2206 sctx->stat.malloc_errors++;
2207 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002208 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002209 return -ENOMEM;
2210 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002211 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2212 scrub_page_get(spage);
2213 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002214 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002215 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002216 spage->flags = flags;
2217 spage->generation = gen;
2218 spage->logical = logical;
2219 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002220 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002221 spage->mirror_num = mirror_num;
2222 if (csum) {
2223 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002224 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002225 } else {
2226 spage->have_csum = 0;
2227 }
2228 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002229 spage->page = alloc_page(GFP_NOFS);
2230 if (!spage->page)
2231 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002232 len -= l;
2233 logical += l;
2234 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002235 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002236 }
2237
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002238 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002239 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002240 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002241 int ret;
2242
Stefan Behrensff023aa2012-11-06 11:43:11 +01002243 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002244 if (ret) {
2245 scrub_block_put(sblock);
2246 return ret;
2247 }
2248 }
2249
2250 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002251 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002252
2253 /* last one frees, either here or in bio completion for last page */
2254 scrub_block_put(sblock);
2255 return 0;
2256}
2257
2258static void scrub_bio_end_io(struct bio *bio, int err)
2259{
2260 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002261 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002262
2263 sbio->err = err;
2264 sbio->bio = bio;
2265
Qu Wenruo0339ef22014-02-28 10:46:17 +08002266 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002267}
2268
2269static void scrub_bio_end_io_worker(struct btrfs_work *work)
2270{
2271 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002272 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002273 int i;
2274
Stefan Behrensff023aa2012-11-06 11:43:11 +01002275 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002276 if (sbio->err) {
2277 for (i = 0; i < sbio->page_count; i++) {
2278 struct scrub_page *spage = sbio->pagev[i];
2279
2280 spage->io_error = 1;
2281 spage->sblock->no_io_error_seen = 0;
2282 }
2283 }
2284
2285 /* now complete the scrub_block items that have all pages completed */
2286 for (i = 0; i < sbio->page_count; i++) {
2287 struct scrub_page *spage = sbio->pagev[i];
2288 struct scrub_block *sblock = spage->sblock;
2289
2290 if (atomic_dec_and_test(&sblock->outstanding_pages))
2291 scrub_block_complete(sblock);
2292 scrub_block_put(sblock);
2293 }
2294
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002295 bio_put(sbio->bio);
2296 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002297 spin_lock(&sctx->list_lock);
2298 sbio->next_free = sctx->first_free;
2299 sctx->first_free = sbio->index;
2300 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002301
2302 if (sctx->is_dev_replace &&
2303 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2304 mutex_lock(&sctx->wr_ctx.wr_lock);
2305 scrub_wr_submit(sctx);
2306 mutex_unlock(&sctx->wr_ctx.wr_lock);
2307 }
2308
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002309 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002310}
2311
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002312static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2313 unsigned long *bitmap,
2314 u64 start, u64 len)
2315{
David Sterba9d644a62015-02-20 18:42:11 +01002316 u32 offset;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002317 int nsectors;
2318 int sectorsize = sparity->sctx->dev_root->sectorsize;
2319
2320 if (len >= sparity->stripe_len) {
2321 bitmap_set(bitmap, 0, sparity->nsectors);
2322 return;
2323 }
2324
2325 start -= sparity->logic_start;
David Sterba47c57132015-02-20 18:43:47 +01002326 start = div_u64_rem(start, sparity->stripe_len, &offset);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002327 offset /= sectorsize;
2328 nsectors = (int)len / sectorsize;
2329
2330 if (offset + nsectors <= sparity->nsectors) {
2331 bitmap_set(bitmap, offset, nsectors);
2332 return;
2333 }
2334
2335 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2336 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2337}
2338
2339static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2340 u64 start, u64 len)
2341{
2342 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2343}
2344
2345static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2346 u64 start, u64 len)
2347{
2348 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2349}
2350
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002351static void scrub_block_complete(struct scrub_block *sblock)
2352{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002353 int corrupted = 0;
2354
Stefan Behrensff023aa2012-11-06 11:43:11 +01002355 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002356 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002357 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002358 } else {
2359 /*
2360 * if has checksum error, write via repair mechanism in
2361 * dev replace case, otherwise write here in dev replace
2362 * case.
2363 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002364 corrupted = scrub_checksum(sblock);
2365 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002366 scrub_write_block_to_dev_replace(sblock);
2367 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002368
2369 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2370 u64 start = sblock->pagev[0]->logical;
2371 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2372 PAGE_SIZE;
2373
2374 scrub_parity_mark_sectors_error(sblock->sparity,
2375 start, end - start);
2376 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002377}
2378
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002379static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002380 u8 *csum)
2381{
2382 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002383 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002384 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002385
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002386 while (!list_empty(&sctx->csum_list)) {
2387 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002388 struct btrfs_ordered_sum, list);
2389 if (sum->bytenr > logical)
2390 return 0;
2391 if (sum->bytenr + sum->len > logical)
2392 break;
2393
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002394 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002395 list_del(&sum->list);
2396 kfree(sum);
2397 sum = NULL;
2398 }
2399 if (!sum)
2400 return 0;
2401
Miao Xief51a4a12013-06-19 10:36:09 +08002402 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002403 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002404 memcpy(csum, sum->sums + index, sctx->csum_size);
2405 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002406 list_del(&sum->list);
2407 kfree(sum);
2408 }
Miao Xief51a4a12013-06-19 10:36:09 +08002409 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002410}
2411
2412/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002413static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002414 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002415 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002416{
2417 int ret;
2418 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002419 u32 blocksize;
2420
2421 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002422 blocksize = sctx->sectorsize;
2423 spin_lock(&sctx->stat_lock);
2424 sctx->stat.data_extents_scrubbed++;
2425 sctx->stat.data_bytes_scrubbed += len;
2426 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002427 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002428 blocksize = sctx->nodesize;
2429 spin_lock(&sctx->stat_lock);
2430 sctx->stat.tree_extents_scrubbed++;
2431 sctx->stat.tree_bytes_scrubbed += len;
2432 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002433 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002434 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002435 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002436 }
Arne Jansena2de7332011-03-08 14:14:00 +01002437
2438 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002439 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002440 int have_csum = 0;
2441
2442 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2443 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002444 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002445 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002446 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002447 if (sctx->is_dev_replace && !have_csum) {
2448 ret = copy_nocow_pages(sctx, logical, l,
2449 mirror_num,
2450 physical_for_dev_replace);
2451 goto behind_scrub_pages;
2452 }
Arne Jansena2de7332011-03-08 14:14:00 +01002453 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002454 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002455 mirror_num, have_csum ? csum : NULL, 0,
2456 physical_for_dev_replace);
2457behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002458 if (ret)
2459 return ret;
2460 len -= l;
2461 logical += l;
2462 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002463 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002464 }
2465 return 0;
2466}
2467
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002468static int scrub_pages_for_parity(struct scrub_parity *sparity,
2469 u64 logical, u64 len,
2470 u64 physical, struct btrfs_device *dev,
2471 u64 flags, u64 gen, int mirror_num, u8 *csum)
2472{
2473 struct scrub_ctx *sctx = sparity->sctx;
2474 struct scrub_block *sblock;
2475 int index;
2476
2477 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2478 if (!sblock) {
2479 spin_lock(&sctx->stat_lock);
2480 sctx->stat.malloc_errors++;
2481 spin_unlock(&sctx->stat_lock);
2482 return -ENOMEM;
2483 }
2484
2485 /* one ref inside this function, plus one for each page added to
2486 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002487 atomic_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002488 sblock->sctx = sctx;
2489 sblock->no_io_error_seen = 1;
2490 sblock->sparity = sparity;
2491 scrub_parity_get(sparity);
2492
2493 for (index = 0; len > 0; index++) {
2494 struct scrub_page *spage;
2495 u64 l = min_t(u64, len, PAGE_SIZE);
2496
2497 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2498 if (!spage) {
2499leave_nomem:
2500 spin_lock(&sctx->stat_lock);
2501 sctx->stat.malloc_errors++;
2502 spin_unlock(&sctx->stat_lock);
2503 scrub_block_put(sblock);
2504 return -ENOMEM;
2505 }
2506 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2507 /* For scrub block */
2508 scrub_page_get(spage);
2509 sblock->pagev[index] = spage;
2510 /* For scrub parity */
2511 scrub_page_get(spage);
2512 list_add_tail(&spage->list, &sparity->spages);
2513 spage->sblock = sblock;
2514 spage->dev = dev;
2515 spage->flags = flags;
2516 spage->generation = gen;
2517 spage->logical = logical;
2518 spage->physical = physical;
2519 spage->mirror_num = mirror_num;
2520 if (csum) {
2521 spage->have_csum = 1;
2522 memcpy(spage->csum, csum, sctx->csum_size);
2523 } else {
2524 spage->have_csum = 0;
2525 }
2526 sblock->page_count++;
2527 spage->page = alloc_page(GFP_NOFS);
2528 if (!spage->page)
2529 goto leave_nomem;
2530 len -= l;
2531 logical += l;
2532 physical += l;
2533 }
2534
2535 WARN_ON(sblock->page_count == 0);
2536 for (index = 0; index < sblock->page_count; index++) {
2537 struct scrub_page *spage = sblock->pagev[index];
2538 int ret;
2539
2540 ret = scrub_add_page_to_rd_bio(sctx, spage);
2541 if (ret) {
2542 scrub_block_put(sblock);
2543 return ret;
2544 }
2545 }
2546
2547 /* last one frees, either here or in bio completion for last page */
2548 scrub_block_put(sblock);
2549 return 0;
2550}
2551
2552static int scrub_extent_for_parity(struct scrub_parity *sparity,
2553 u64 logical, u64 len,
2554 u64 physical, struct btrfs_device *dev,
2555 u64 flags, u64 gen, int mirror_num)
2556{
2557 struct scrub_ctx *sctx = sparity->sctx;
2558 int ret;
2559 u8 csum[BTRFS_CSUM_SIZE];
2560 u32 blocksize;
2561
2562 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2563 blocksize = sctx->sectorsize;
2564 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2565 blocksize = sctx->nodesize;
2566 } else {
2567 blocksize = sctx->sectorsize;
2568 WARN_ON(1);
2569 }
2570
2571 while (len) {
2572 u64 l = min_t(u64, len, blocksize);
2573 int have_csum = 0;
2574
2575 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2576 /* push csums to sbio */
2577 have_csum = scrub_find_csum(sctx, logical, l, csum);
2578 if (have_csum == 0)
2579 goto skip;
2580 }
2581 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2582 flags, gen, mirror_num,
2583 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002584 if (ret)
2585 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002586skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002587 len -= l;
2588 logical += l;
2589 physical += l;
2590 }
2591 return 0;
2592}
2593
Wang Shilong3b080b22014-04-01 18:01:43 +08002594/*
2595 * Given a physical address, this will calculate it's
2596 * logical offset. if this is a parity stripe, it will return
2597 * the most left data stripe's logical offset.
2598 *
2599 * return 0 if it is a data stripe, 1 means parity stripe.
2600 */
2601static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002602 struct map_lookup *map, u64 *offset,
2603 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002604{
2605 int i;
2606 int j = 0;
2607 u64 stripe_nr;
2608 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002609 u32 stripe_index;
2610 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002611
2612 last_offset = (physical - map->stripes[num].physical) *
2613 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002614 if (stripe_start)
2615 *stripe_start = last_offset;
2616
Wang Shilong3b080b22014-04-01 18:01:43 +08002617 *offset = last_offset;
2618 for (i = 0; i < nr_data_stripes(map); i++) {
2619 *offset = last_offset + i * map->stripe_len;
2620
David Sterbab8b93ad2015-01-16 17:26:13 +01002621 stripe_nr = div_u64(*offset, map->stripe_len);
2622 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002623
2624 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002625 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002626 /* calculate which stripe this data locates */
2627 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002628 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002629 if (stripe_index == num)
2630 return 0;
2631 if (stripe_index < num)
2632 j++;
2633 }
2634 *offset = last_offset + j * map->stripe_len;
2635 return 1;
2636}
2637
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002638static void scrub_free_parity(struct scrub_parity *sparity)
2639{
2640 struct scrub_ctx *sctx = sparity->sctx;
2641 struct scrub_page *curr, *next;
2642 int nbits;
2643
2644 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2645 if (nbits) {
2646 spin_lock(&sctx->stat_lock);
2647 sctx->stat.read_errors += nbits;
2648 sctx->stat.uncorrectable_errors += nbits;
2649 spin_unlock(&sctx->stat_lock);
2650 }
2651
2652 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2653 list_del_init(&curr->list);
2654 scrub_page_put(curr);
2655 }
2656
2657 kfree(sparity);
2658}
2659
Zhao Lei20b2e302015-06-04 20:09:15 +08002660static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2661{
2662 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2663 work);
2664 struct scrub_ctx *sctx = sparity->sctx;
2665
2666 scrub_free_parity(sparity);
2667 scrub_pending_bio_dec(sctx);
2668}
2669
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002670static void scrub_parity_bio_endio(struct bio *bio, int error)
2671{
2672 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002673
2674 if (error)
2675 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2676 sparity->nsectors);
2677
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002678 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002679
2680 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2681 scrub_parity_bio_endio_worker, NULL, NULL);
2682 btrfs_queue_work(sparity->sctx->dev_root->fs_info->scrub_parity_workers,
2683 &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002684}
2685
2686static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2687{
2688 struct scrub_ctx *sctx = sparity->sctx;
2689 struct bio *bio;
2690 struct btrfs_raid_bio *rbio;
2691 struct scrub_page *spage;
2692 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002693 u64 length;
2694 int ret;
2695
2696 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2697 sparity->nsectors))
2698 goto out;
2699
Zhao Leia0dd59d2015-07-21 15:42:26 +08002700 length = sparity->logic_end - sparity->logic_start;
Miao Xie76035972014-11-14 17:45:42 +08002701 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002702 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002703 &length, &bbio, 0, 1);
2704 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002705 goto bbio_out;
2706
2707 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2708 if (!bio)
2709 goto bbio_out;
2710
2711 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2712 bio->bi_private = sparity;
2713 bio->bi_end_io = scrub_parity_bio_endio;
2714
2715 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002716 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002717 sparity->dbitmap,
2718 sparity->nsectors);
2719 if (!rbio)
2720 goto rbio_out;
2721
2722 list_for_each_entry(spage, &sparity->spages, list)
Omar Sandovalb4ee1782015-06-19 11:52:50 -07002723 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002724
2725 scrub_pending_bio_inc(sctx);
2726 raid56_parity_submit_scrub_rbio(rbio);
2727 return;
2728
2729rbio_out:
2730 bio_put(bio);
2731bbio_out:
Zhao Lei6e9606d2015-01-20 15:11:34 +08002732 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002733 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2734 sparity->nsectors);
2735 spin_lock(&sctx->stat_lock);
2736 sctx->stat.malloc_errors++;
2737 spin_unlock(&sctx->stat_lock);
2738out:
2739 scrub_free_parity(sparity);
2740}
2741
2742static inline int scrub_calc_parity_bitmap_len(int nsectors)
2743{
2744 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2745}
2746
2747static void scrub_parity_get(struct scrub_parity *sparity)
2748{
Zhao Lei57019342015-01-20 15:11:45 +08002749 atomic_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002750}
2751
2752static void scrub_parity_put(struct scrub_parity *sparity)
2753{
Zhao Lei57019342015-01-20 15:11:45 +08002754 if (!atomic_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002755 return;
2756
2757 scrub_parity_check_and_repair(sparity);
2758}
2759
2760static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2761 struct map_lookup *map,
2762 struct btrfs_device *sdev,
2763 struct btrfs_path *path,
2764 u64 logic_start,
2765 u64 logic_end)
2766{
2767 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2768 struct btrfs_root *root = fs_info->extent_root;
2769 struct btrfs_root *csum_root = fs_info->csum_root;
2770 struct btrfs_extent_item *extent;
2771 u64 flags;
2772 int ret;
2773 int slot;
2774 struct extent_buffer *l;
2775 struct btrfs_key key;
2776 u64 generation;
2777 u64 extent_logical;
2778 u64 extent_physical;
2779 u64 extent_len;
2780 struct btrfs_device *extent_dev;
2781 struct scrub_parity *sparity;
2782 int nsectors;
2783 int bitmap_len;
2784 int extent_mirror_num;
2785 int stop_loop = 0;
2786
2787 nsectors = map->stripe_len / root->sectorsize;
2788 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2789 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2790 GFP_NOFS);
2791 if (!sparity) {
2792 spin_lock(&sctx->stat_lock);
2793 sctx->stat.malloc_errors++;
2794 spin_unlock(&sctx->stat_lock);
2795 return -ENOMEM;
2796 }
2797
2798 sparity->stripe_len = map->stripe_len;
2799 sparity->nsectors = nsectors;
2800 sparity->sctx = sctx;
2801 sparity->scrub_dev = sdev;
2802 sparity->logic_start = logic_start;
2803 sparity->logic_end = logic_end;
Zhao Lei57019342015-01-20 15:11:45 +08002804 atomic_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002805 INIT_LIST_HEAD(&sparity->spages);
2806 sparity->dbitmap = sparity->bitmap;
2807 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2808
2809 ret = 0;
2810 while (logic_start < logic_end) {
2811 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2812 key.type = BTRFS_METADATA_ITEM_KEY;
2813 else
2814 key.type = BTRFS_EXTENT_ITEM_KEY;
2815 key.objectid = logic_start;
2816 key.offset = (u64)-1;
2817
2818 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2819 if (ret < 0)
2820 goto out;
2821
2822 if (ret > 0) {
2823 ret = btrfs_previous_extent_item(root, path, 0);
2824 if (ret < 0)
2825 goto out;
2826 if (ret > 0) {
2827 btrfs_release_path(path);
2828 ret = btrfs_search_slot(NULL, root, &key,
2829 path, 0, 0);
2830 if (ret < 0)
2831 goto out;
2832 }
2833 }
2834
2835 stop_loop = 0;
2836 while (1) {
2837 u64 bytes;
2838
2839 l = path->nodes[0];
2840 slot = path->slots[0];
2841 if (slot >= btrfs_header_nritems(l)) {
2842 ret = btrfs_next_leaf(root, path);
2843 if (ret == 0)
2844 continue;
2845 if (ret < 0)
2846 goto out;
2847
2848 stop_loop = 1;
2849 break;
2850 }
2851 btrfs_item_key_to_cpu(l, &key, slot);
2852
Zhao Leid7cad232015-07-22 13:14:48 +08002853 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2854 key.type != BTRFS_METADATA_ITEM_KEY)
2855 goto next;
2856
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002857 if (key.type == BTRFS_METADATA_ITEM_KEY)
2858 bytes = root->nodesize;
2859 else
2860 bytes = key.offset;
2861
2862 if (key.objectid + bytes <= logic_start)
2863 goto next;
2864
Zhao Leia0dd59d2015-07-21 15:42:26 +08002865 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002866 stop_loop = 1;
2867 break;
2868 }
2869
2870 while (key.objectid >= logic_start + map->stripe_len)
2871 logic_start += map->stripe_len;
2872
2873 extent = btrfs_item_ptr(l, slot,
2874 struct btrfs_extent_item);
2875 flags = btrfs_extent_flags(l, extent);
2876 generation = btrfs_extent_generation(l, extent);
2877
Zhao Leia323e812015-07-23 12:29:49 +08002878 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
2879 (key.objectid < logic_start ||
2880 key.objectid + bytes >
2881 logic_start + map->stripe_len)) {
2882 btrfs_err(fs_info, "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2883 key.objectid, logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002884 goto next;
2885 }
2886again:
2887 extent_logical = key.objectid;
2888 extent_len = bytes;
2889
2890 if (extent_logical < logic_start) {
2891 extent_len -= logic_start - extent_logical;
2892 extent_logical = logic_start;
2893 }
2894
2895 if (extent_logical + extent_len >
2896 logic_start + map->stripe_len)
2897 extent_len = logic_start + map->stripe_len -
2898 extent_logical;
2899
2900 scrub_parity_mark_sectors_data(sparity, extent_logical,
2901 extent_len);
2902
2903 scrub_remap_extent(fs_info, extent_logical,
2904 extent_len, &extent_physical,
2905 &extent_dev,
2906 &extent_mirror_num);
2907
2908 ret = btrfs_lookup_csums_range(csum_root,
2909 extent_logical,
2910 extent_logical + extent_len - 1,
2911 &sctx->csum_list, 1);
2912 if (ret)
2913 goto out;
2914
2915 ret = scrub_extent_for_parity(sparity, extent_logical,
2916 extent_len,
2917 extent_physical,
2918 extent_dev, flags,
2919 generation,
2920 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08002921
2922 scrub_free_csums(sctx);
2923
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002924 if (ret)
2925 goto out;
2926
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002927 if (extent_logical + extent_len <
2928 key.objectid + bytes) {
2929 logic_start += map->stripe_len;
2930
2931 if (logic_start >= logic_end) {
2932 stop_loop = 1;
2933 break;
2934 }
2935
2936 if (logic_start < key.objectid + bytes) {
2937 cond_resched();
2938 goto again;
2939 }
2940 }
2941next:
2942 path->slots[0]++;
2943 }
2944
2945 btrfs_release_path(path);
2946
2947 if (stop_loop)
2948 break;
2949
2950 logic_start += map->stripe_len;
2951 }
2952out:
2953 if (ret < 0)
2954 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08002955 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002956 scrub_parity_put(sparity);
2957 scrub_submit(sctx);
2958 mutex_lock(&sctx->wr_ctx.wr_lock);
2959 scrub_wr_submit(sctx);
2960 mutex_unlock(&sctx->wr_ctx.wr_lock);
2961
2962 btrfs_release_path(path);
2963 return ret < 0 ? ret : 0;
2964}
2965
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002966static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002967 struct map_lookup *map,
2968 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002969 int num, u64 base, u64 length,
2970 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002971{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002972 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002973 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002974 struct btrfs_root *root = fs_info->extent_root;
2975 struct btrfs_root *csum_root = fs_info->csum_root;
2976 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002977 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002978 u64 flags;
2979 int ret;
2980 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002981 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002982 struct extent_buffer *l;
2983 struct btrfs_key key;
2984 u64 physical;
2985 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002986 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08002987 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002988 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002989 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002990 struct reada_control *reada1;
2991 struct reada_control *reada2;
2992 struct btrfs_key key_start;
2993 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002994 u64 increment = map->stripe_len;
2995 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002996 u64 extent_logical;
2997 u64 extent_physical;
2998 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002999 u64 stripe_logical;
3000 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003001 struct btrfs_device *extent_dev;
3002 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003003 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003004
Wang Shilong3b080b22014-04-01 18:01:43 +08003005 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003006 offset = 0;
David Sterbab8b93ad2015-01-16 17:26:13 +01003007 nstripes = div_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003008 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3009 offset = map->stripe_len * num;
3010 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003011 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003012 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3013 int factor = map->num_stripes / map->sub_stripes;
3014 offset = map->stripe_len * (num / map->sub_stripes);
3015 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003016 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003017 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3018 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003019 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003020 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3021 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003022 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003023 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003024 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003025 increment = map->stripe_len * nr_data_stripes(map);
3026 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003027 } else {
3028 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003029 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003030 }
3031
3032 path = btrfs_alloc_path();
3033 if (!path)
3034 return -ENOMEM;
3035
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003036 ppath = btrfs_alloc_path();
3037 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003038 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003039 return -ENOMEM;
3040 }
3041
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003042 /*
3043 * work on commit root. The related disk blocks are static as
3044 * long as COW is applied. This means, it is save to rewrite
3045 * them to repair disk errors without any race conditions
3046 */
Arne Jansena2de7332011-03-08 14:14:00 +01003047 path->search_commit_root = 1;
3048 path->skip_locking = 1;
3049
Gui Hecheng063c54d2015-01-09 09:39:40 +08003050 ppath->search_commit_root = 1;
3051 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003052 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003053 * trigger the readahead for extent tree csum tree and wait for
3054 * completion. During readahead, the scrub is officially paused
3055 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003056 */
3057 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003058 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003059 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003060 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003061 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003062 logic_end += base;
3063 } else {
3064 logic_end = logical + increment * nstripes;
3065 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003066 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003067 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003068 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003069
Arne Jansen7a262852011-06-10 12:39:23 +02003070 /* FIXME it might be better to start readahead at commit root */
3071 key_start.objectid = logical;
3072 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3073 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003074 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003075 key_end.type = BTRFS_METADATA_ITEM_KEY;
3076 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003077 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003078
Arne Jansen7a262852011-06-10 12:39:23 +02003079 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3080 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3081 key_start.offset = logical;
3082 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3083 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003084 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003085 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003086
Arne Jansen7a262852011-06-10 12:39:23 +02003087 if (!IS_ERR(reada1))
3088 btrfs_reada_wait(reada1);
3089 if (!IS_ERR(reada2))
3090 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003091
Arne Jansena2de7332011-03-08 14:14:00 +01003092
3093 /*
3094 * collect all data csums for the stripe to avoid seeking during
3095 * the scrub. This might currently (crc32) end up to be about 1MB
3096 */
Arne Jansene7786c32011-05-28 20:58:38 +00003097 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003098
Arne Jansena2de7332011-03-08 14:14:00 +01003099 /*
3100 * now find all extents for each stripe and scrub them
3101 */
Arne Jansena2de7332011-03-08 14:14:00 +01003102 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003103 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003104 /*
3105 * canceled?
3106 */
3107 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003108 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003109 ret = -ECANCELED;
3110 goto out;
3111 }
3112 /*
3113 * check to see if we have to pause
3114 */
3115 if (atomic_read(&fs_info->scrub_pause_req)) {
3116 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003117 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003118 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003119 mutex_lock(&sctx->wr_ctx.wr_lock);
3120 scrub_wr_submit(sctx);
3121 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003122 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003123 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003124 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003125 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003126 }
3127
Zhao Leif2f66a22015-07-21 12:22:29 +08003128 /* for raid56, we skip parity stripe */
3129 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3130 ret = get_raid56_logic_offset(physical, num, map,
3131 &logical,
3132 &stripe_logical);
3133 logical += base;
3134 if (ret) {
3135 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003136 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003137 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3138 ppath, stripe_logical,
3139 stripe_end);
3140 if (ret)
3141 goto out;
3142 goto skip;
3143 }
3144 }
3145
Wang Shilong7c76edb2014-01-12 21:38:32 +08003146 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3147 key.type = BTRFS_METADATA_ITEM_KEY;
3148 else
3149 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003150 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003151 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003152
3153 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3154 if (ret < 0)
3155 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003156
Arne Jansen8c510322011-06-03 10:09:26 +02003157 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003158 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003159 if (ret < 0)
3160 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003161 if (ret > 0) {
3162 /* there's no smaller item, so stick with the
3163 * larger one */
3164 btrfs_release_path(path);
3165 ret = btrfs_search_slot(NULL, root, &key,
3166 path, 0, 0);
3167 if (ret < 0)
3168 goto out;
3169 }
Arne Jansena2de7332011-03-08 14:14:00 +01003170 }
3171
Liu Bo625f1c8d2013-04-27 02:56:57 +00003172 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003173 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003174 u64 bytes;
3175
Arne Jansena2de7332011-03-08 14:14:00 +01003176 l = path->nodes[0];
3177 slot = path->slots[0];
3178 if (slot >= btrfs_header_nritems(l)) {
3179 ret = btrfs_next_leaf(root, path);
3180 if (ret == 0)
3181 continue;
3182 if (ret < 0)
3183 goto out;
3184
Liu Bo625f1c8d2013-04-27 02:56:57 +00003185 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003186 break;
3187 }
3188 btrfs_item_key_to_cpu(l, &key, slot);
3189
Zhao Leid7cad232015-07-22 13:14:48 +08003190 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3191 key.type != BTRFS_METADATA_ITEM_KEY)
3192 goto next;
3193
Josef Bacik3173a182013-03-07 14:22:04 -05003194 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003195 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003196 else
3197 bytes = key.offset;
3198
3199 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003200 goto next;
3201
Liu Bo625f1c8d2013-04-27 02:56:57 +00003202 if (key.objectid >= logical + map->stripe_len) {
3203 /* out of this device extent */
3204 if (key.objectid >= logic_end)
3205 stop_loop = 1;
3206 break;
3207 }
Arne Jansena2de7332011-03-08 14:14:00 +01003208
3209 extent = btrfs_item_ptr(l, slot,
3210 struct btrfs_extent_item);
3211 flags = btrfs_extent_flags(l, extent);
3212 generation = btrfs_extent_generation(l, extent);
3213
Zhao Leia323e812015-07-23 12:29:49 +08003214 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3215 (key.objectid < logical ||
3216 key.objectid + bytes >
3217 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003218 btrfs_err(fs_info,
3219 "scrub: tree block %llu spanning "
3220 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003221 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01003222 goto next;
3223 }
3224
Liu Bo625f1c8d2013-04-27 02:56:57 +00003225again:
3226 extent_logical = key.objectid;
3227 extent_len = bytes;
3228
Arne Jansena2de7332011-03-08 14:14:00 +01003229 /*
3230 * trim extent to this stripe
3231 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003232 if (extent_logical < logical) {
3233 extent_len -= logical - extent_logical;
3234 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003235 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003236 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003237 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003238 extent_len = logical + map->stripe_len -
3239 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003240 }
3241
Liu Bo625f1c8d2013-04-27 02:56:57 +00003242 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003243 extent_dev = scrub_dev;
3244 extent_mirror_num = mirror_num;
3245 if (is_dev_replace)
3246 scrub_remap_extent(fs_info, extent_logical,
3247 extent_len, &extent_physical,
3248 &extent_dev,
3249 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003250
Zhao Leife8cf652015-07-22 13:14:47 +08003251 ret = btrfs_lookup_csums_range(csum_root,
3252 extent_logical,
3253 extent_logical +
3254 extent_len - 1,
3255 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003256 if (ret)
3257 goto out;
3258
Liu Bo625f1c8d2013-04-27 02:56:57 +00003259 ret = scrub_extent(sctx, extent_logical, extent_len,
3260 extent_physical, extent_dev, flags,
3261 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003262 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003263
3264 scrub_free_csums(sctx);
3265
Liu Bo625f1c8d2013-04-27 02:56:57 +00003266 if (ret)
3267 goto out;
3268
3269 if (extent_logical + extent_len <
3270 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003271 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003272 /*
3273 * loop until we find next data stripe
3274 * or we have finished all stripes.
3275 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003276loop:
3277 physical += map->stripe_len;
3278 ret = get_raid56_logic_offset(physical,
3279 num, map, &logical,
3280 &stripe_logical);
3281 logical += base;
3282
3283 if (ret && physical < physical_end) {
3284 stripe_logical += base;
3285 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003286 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003287 ret = scrub_raid56_parity(sctx,
3288 map, scrub_dev, ppath,
3289 stripe_logical,
3290 stripe_end);
3291 if (ret)
3292 goto out;
3293 goto loop;
3294 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003295 } else {
3296 physical += map->stripe_len;
3297 logical += increment;
3298 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003299 if (logical < key.objectid + bytes) {
3300 cond_resched();
3301 goto again;
3302 }
3303
Wang Shilong3b080b22014-04-01 18:01:43 +08003304 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003305 stop_loop = 1;
3306 break;
3307 }
3308 }
Arne Jansena2de7332011-03-08 14:14:00 +01003309next:
3310 path->slots[0]++;
3311 }
Chris Mason71267332011-05-23 06:30:52 -04003312 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003313skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003314 logical += increment;
3315 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003316 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003317 if (stop_loop)
3318 sctx->stat.last_physical = map->stripes[num].physical +
3319 length;
3320 else
3321 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003322 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003323 if (stop_loop)
3324 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003325 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003326out:
Arne Jansena2de7332011-03-08 14:14:00 +01003327 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003328 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003329 mutex_lock(&sctx->wr_ctx.wr_lock);
3330 scrub_wr_submit(sctx);
3331 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003332
Arne Jansene7786c32011-05-28 20:58:38 +00003333 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003334 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003335 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003336 return ret < 0 ? ret : 0;
3337}
3338
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003339static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003340 struct btrfs_device *scrub_dev,
3341 u64 chunk_tree, u64 chunk_objectid,
3342 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003343 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003344{
3345 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003346 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003347 struct map_lookup *map;
3348 struct extent_map *em;
3349 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003350 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003351
3352 read_lock(&map_tree->map_tree.lock);
3353 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3354 read_unlock(&map_tree->map_tree.lock);
3355
3356 if (!em)
3357 return -EINVAL;
3358
3359 map = (struct map_lookup *)em->bdev;
3360 if (em->start != chunk_offset)
3361 goto out;
3362
3363 if (em->len < length)
3364 goto out;
3365
3366 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003367 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003368 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003369 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003370 chunk_offset, length,
3371 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003372 if (ret)
3373 goto out;
3374 }
3375 }
3376out:
3377 free_extent_map(em);
3378
3379 return ret;
3380}
3381
3382static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003383int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003384 struct btrfs_device *scrub_dev, u64 start, u64 end,
3385 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003386{
3387 struct btrfs_dev_extent *dev_extent = NULL;
3388 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003389 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003390 struct btrfs_fs_info *fs_info = root->fs_info;
3391 u64 length;
3392 u64 chunk_tree;
3393 u64 chunk_objectid;
3394 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003395 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003396 int slot;
3397 struct extent_buffer *l;
3398 struct btrfs_key key;
3399 struct btrfs_key found_key;
3400 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003401 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003402
3403 path = btrfs_alloc_path();
3404 if (!path)
3405 return -ENOMEM;
3406
3407 path->reada = 2;
3408 path->search_commit_root = 1;
3409 path->skip_locking = 1;
3410
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003411 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003412 key.offset = 0ull;
3413 key.type = BTRFS_DEV_EXTENT_KEY;
3414
Arne Jansena2de7332011-03-08 14:14:00 +01003415 while (1) {
3416 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3417 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003418 break;
3419 if (ret > 0) {
3420 if (path->slots[0] >=
3421 btrfs_header_nritems(path->nodes[0])) {
3422 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003423 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003424 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003425 if (ret > 0) {
3426 ret = 0;
3427 break;
3428 }
3429 } else {
3430 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003431 }
3432 }
Arne Jansena2de7332011-03-08 14:14:00 +01003433
3434 l = path->nodes[0];
3435 slot = path->slots[0];
3436
3437 btrfs_item_key_to_cpu(l, &found_key, slot);
3438
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003439 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003440 break;
3441
David Sterba962a2982014-06-04 18:41:45 +02003442 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003443 break;
3444
3445 if (found_key.offset >= end)
3446 break;
3447
3448 if (found_key.offset < key.offset)
3449 break;
3450
3451 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3452 length = btrfs_dev_extent_length(l, dev_extent);
3453
Qu Wenruoced96ed2014-06-19 10:42:51 +08003454 if (found_key.offset + length <= start)
3455 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003456
3457 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3458 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3459 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3460
3461 /*
3462 * get a reference on the corresponding block group to prevent
3463 * the chunk from going away while we scrub it
3464 */
3465 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003466
3467 /* some chunks are removed but not committed to disk yet,
3468 * continue scrubbing */
3469 if (!cache)
3470 goto skip;
3471
Zhaolei55e3a602015-08-05 16:43:30 +08003472 /*
3473 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3474 * to avoid deadlock caused by:
3475 * btrfs_inc_block_group_ro()
3476 * -> btrfs_wait_for_commit()
3477 * -> btrfs_commit_transaction()
3478 * -> btrfs_scrub_pause()
3479 */
3480 scrub_pause_on(fs_info);
3481 ret = btrfs_inc_block_group_ro(root, cache);
3482 scrub_pause_off(fs_info);
3483 if (ret) {
3484 btrfs_put_block_group(cache);
3485 break;
3486 }
3487
Stefan Behrensff023aa2012-11-06 11:43:11 +01003488 dev_replace->cursor_right = found_key.offset + length;
3489 dev_replace->cursor_left = found_key.offset;
3490 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003491 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003492 chunk_offset, length, found_key.offset,
3493 is_dev_replace);
3494
3495 /*
3496 * flush, submit all pending read and write bios, afterwards
3497 * wait for them.
3498 * Note that in the dev replace case, a read request causes
3499 * write requests that are submitted in the read completion
3500 * worker. Therefore in the current situation, it is required
3501 * that all write requests are flushed, so that all read and
3502 * write requests are really completed when bios_in_flight
3503 * changes to 0.
3504 */
3505 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3506 scrub_submit(sctx);
3507 mutex_lock(&sctx->wr_ctx.wr_lock);
3508 scrub_wr_submit(sctx);
3509 mutex_unlock(&sctx->wr_ctx.wr_lock);
3510
3511 wait_event(sctx->list_wait,
3512 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003513
3514 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003515
3516 /*
3517 * must be called before we decrease @scrub_paused.
3518 * make sure we don't block transaction commit while
3519 * we are waiting pending workers finished.
3520 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003521 wait_event(sctx->list_wait,
3522 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003523 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3524
Zhaoleib708ce92015-08-05 16:43:29 +08003525 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003526
Zhaolei55e3a602015-08-05 16:43:30 +08003527 btrfs_dec_block_group_ro(root, cache);
3528
Arne Jansena2de7332011-03-08 14:14:00 +01003529 btrfs_put_block_group(cache);
3530 if (ret)
3531 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003532 if (is_dev_replace &&
3533 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003534 ret = -EIO;
3535 break;
3536 }
3537 if (sctx->stat.malloc_errors > 0) {
3538 ret = -ENOMEM;
3539 break;
3540 }
Arne Jansena2de7332011-03-08 14:14:00 +01003541
Ilya Dryomov539f3582013-10-07 13:42:57 +03003542 dev_replace->cursor_left = dev_replace->cursor_right;
3543 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003544skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003545 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003546 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003547 }
3548
Arne Jansena2de7332011-03-08 14:14:00 +01003549 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003550
Zhaolei55e3a602015-08-05 16:43:30 +08003551 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003552}
3553
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003554static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3555 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003556{
3557 int i;
3558 u64 bytenr;
3559 u64 gen;
3560 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003561 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003562
Miao Xie87533c42013-01-29 10:14:48 +00003563 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003564 return -EIO;
3565
Miao Xie5f546062014-07-24 11:37:09 +08003566 /* Seed devices of a new filesystem has their own generation. */
3567 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3568 gen = scrub_dev->generation;
3569 else
3570 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003571
3572 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3573 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003574 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3575 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003576 break;
3577
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003578 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003579 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003580 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003581 if (ret)
3582 return ret;
3583 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003584 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003585
3586 return 0;
3587}
3588
3589/*
3590 * get a reference count on fs_info->scrub_workers. start worker if necessary
3591 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003592static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3593 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003594{
David Sterba6f011052015-02-16 18:34:01 +01003595 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003596 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003597
Arne Jansen632dd772011-06-10 12:07:07 +02003598 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003599 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003600 fs_info->scrub_workers =
3601 btrfs_alloc_workqueue("btrfs-scrub", flags,
3602 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003603 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003604 fs_info->scrub_workers =
3605 btrfs_alloc_workqueue("btrfs-scrub", flags,
3606 max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08003607 if (!fs_info->scrub_workers)
3608 goto fail_scrub_workers;
3609
Qu Wenruo0339ef22014-02-28 10:46:17 +08003610 fs_info->scrub_wr_completion_workers =
3611 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3612 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003613 if (!fs_info->scrub_wr_completion_workers)
3614 goto fail_scrub_wr_completion_workers;
3615
Qu Wenruo0339ef22014-02-28 10:46:17 +08003616 fs_info->scrub_nocow_workers =
3617 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
Zhao Leie82afc52015-06-12 20:36:58 +08003618 if (!fs_info->scrub_nocow_workers)
3619 goto fail_scrub_nocow_workers;
Zhao Lei20b2e302015-06-04 20:09:15 +08003620 fs_info->scrub_parity_workers =
3621 btrfs_alloc_workqueue("btrfs-scrubparity", flags,
3622 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003623 if (!fs_info->scrub_parity_workers)
3624 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02003625 }
Arne Jansena2de7332011-03-08 14:14:00 +01003626 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08003627 return 0;
3628
3629fail_scrub_parity_workers:
3630 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
3631fail_scrub_nocow_workers:
3632 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3633fail_scrub_wr_completion_workers:
3634 btrfs_destroy_workqueue(fs_info->scrub_workers);
3635fail_scrub_workers:
3636 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01003637}
3638
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003639static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003640{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003641 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003642 btrfs_destroy_workqueue(fs_info->scrub_workers);
3643 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3644 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08003645 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003646 }
Arne Jansena2de7332011-03-08 14:14:00 +01003647 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003648}
3649
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003650int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3651 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003652 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003653{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003654 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003655 int ret;
3656 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003657 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003658
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003659 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003660 return -EINVAL;
3661
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003662 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003663 /*
3664 * in this case scrub is unable to calculate the checksum
3665 * the way scrub is implemented. Do not handle this
3666 * situation at all because it won't ever happen.
3667 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003668 btrfs_err(fs_info,
3669 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003670 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003671 return -EINVAL;
3672 }
3673
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003674 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003675 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003676 btrfs_err(fs_info,
3677 "scrub: size assumption sectorsize != PAGE_SIZE "
3678 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003679 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003680 return -EINVAL;
3681 }
3682
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003683 if (fs_info->chunk_root->nodesize >
3684 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3685 fs_info->chunk_root->sectorsize >
3686 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3687 /*
3688 * would exhaust the array bounds of pagev member in
3689 * struct scrub_block
3690 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003691 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3692 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003693 fs_info->chunk_root->nodesize,
3694 SCRUB_MAX_PAGES_PER_BLOCK,
3695 fs_info->chunk_root->sectorsize,
3696 SCRUB_MAX_PAGES_PER_BLOCK);
3697 return -EINVAL;
3698 }
3699
Arne Jansena2de7332011-03-08 14:14:00 +01003700
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003701 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3702 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003703 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003704 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003705 return -ENODEV;
3706 }
Arne Jansena2de7332011-03-08 14:14:00 +01003707
Miao Xie5d68da32014-07-24 11:37:07 +08003708 if (!is_dev_replace && !readonly && !dev->writeable) {
3709 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3710 rcu_read_lock();
3711 name = rcu_dereference(dev->name);
3712 btrfs_err(fs_info, "scrub: device %s is not writable",
3713 name->str);
3714 rcu_read_unlock();
3715 return -EROFS;
3716 }
3717
Wang Shilong3b7a0162013-10-12 02:11:12 +08003718 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003719 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003720 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003721 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003722 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003723 }
3724
Stefan Behrens8dabb742012-11-06 13:15:27 +01003725 btrfs_dev_replace_lock(&fs_info->dev_replace);
3726 if (dev->scrub_device ||
3727 (!is_dev_replace &&
3728 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3729 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003730 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003731 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003732 return -EINPROGRESS;
3733 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003734 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003735
3736 ret = scrub_workers_get(fs_info, is_dev_replace);
3737 if (ret) {
3738 mutex_unlock(&fs_info->scrub_lock);
3739 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3740 return ret;
3741 }
3742
Stefan Behrens63a212a2012-11-05 18:29:28 +01003743 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003744 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003745 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003746 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3747 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003748 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003749 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003750 sctx->readonly = readonly;
3751 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003752 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003753
Wang Shilong3cb09292013-12-04 21:15:19 +08003754 /*
3755 * checking @scrub_pause_req here, we can avoid
3756 * race between committing transaction and scrubbing.
3757 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003758 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003759 atomic_inc(&fs_info->scrubs_running);
3760 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003761
Stefan Behrensff023aa2012-11-06 11:43:11 +01003762 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003763 /*
3764 * by holding device list mutex, we can
3765 * kick off writing super in log tree sync.
3766 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003767 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003768 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003769 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003770 }
Arne Jansena2de7332011-03-08 14:14:00 +01003771
3772 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003773 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3774 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003775
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003776 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003777 atomic_dec(&fs_info->scrubs_running);
3778 wake_up(&fs_info->scrub_pause_wait);
3779
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003780 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003781
Arne Jansena2de7332011-03-08 14:14:00 +01003782 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003783 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003784
3785 mutex_lock(&fs_info->scrub_lock);
3786 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003787 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003788 mutex_unlock(&fs_info->scrub_lock);
3789
Filipe Mananaf55985f2015-02-09 21:14:24 +00003790 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003791
3792 return ret;
3793}
3794
Jeff Mahoney143bede2012-03-01 14:56:26 +01003795void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003796{
3797 struct btrfs_fs_info *fs_info = root->fs_info;
3798
3799 mutex_lock(&fs_info->scrub_lock);
3800 atomic_inc(&fs_info->scrub_pause_req);
3801 while (atomic_read(&fs_info->scrubs_paused) !=
3802 atomic_read(&fs_info->scrubs_running)) {
3803 mutex_unlock(&fs_info->scrub_lock);
3804 wait_event(fs_info->scrub_pause_wait,
3805 atomic_read(&fs_info->scrubs_paused) ==
3806 atomic_read(&fs_info->scrubs_running));
3807 mutex_lock(&fs_info->scrub_lock);
3808 }
3809 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003810}
3811
Jeff Mahoney143bede2012-03-01 14:56:26 +01003812void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003813{
3814 struct btrfs_fs_info *fs_info = root->fs_info;
3815
3816 atomic_dec(&fs_info->scrub_pause_req);
3817 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003818}
3819
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003820int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003821{
Arne Jansena2de7332011-03-08 14:14:00 +01003822 mutex_lock(&fs_info->scrub_lock);
3823 if (!atomic_read(&fs_info->scrubs_running)) {
3824 mutex_unlock(&fs_info->scrub_lock);
3825 return -ENOTCONN;
3826 }
3827
3828 atomic_inc(&fs_info->scrub_cancel_req);
3829 while (atomic_read(&fs_info->scrubs_running)) {
3830 mutex_unlock(&fs_info->scrub_lock);
3831 wait_event(fs_info->scrub_pause_wait,
3832 atomic_read(&fs_info->scrubs_running) == 0);
3833 mutex_lock(&fs_info->scrub_lock);
3834 }
3835 atomic_dec(&fs_info->scrub_cancel_req);
3836 mutex_unlock(&fs_info->scrub_lock);
3837
3838 return 0;
3839}
3840
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003841int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3842 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003843{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003844 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003845
3846 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003847 sctx = dev->scrub_device;
3848 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003849 mutex_unlock(&fs_info->scrub_lock);
3850 return -ENOTCONN;
3851 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003852 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003853 while (dev->scrub_device) {
3854 mutex_unlock(&fs_info->scrub_lock);
3855 wait_event(fs_info->scrub_pause_wait,
3856 dev->scrub_device == NULL);
3857 mutex_lock(&fs_info->scrub_lock);
3858 }
3859 mutex_unlock(&fs_info->scrub_lock);
3860
3861 return 0;
3862}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003863
Arne Jansena2de7332011-03-08 14:14:00 +01003864int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3865 struct btrfs_scrub_progress *progress)
3866{
3867 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003868 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003869
3870 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003871 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003872 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003873 sctx = dev->scrub_device;
3874 if (sctx)
3875 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003876 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3877
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003878 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003879}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003880
3881static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3882 u64 extent_logical, u64 extent_len,
3883 u64 *extent_physical,
3884 struct btrfs_device **extent_dev,
3885 int *extent_mirror_num)
3886{
3887 u64 mapped_length;
3888 struct btrfs_bio *bbio = NULL;
3889 int ret;
3890
3891 mapped_length = extent_len;
3892 ret = btrfs_map_block(fs_info, READ, extent_logical,
3893 &mapped_length, &bbio, 0);
3894 if (ret || !bbio || mapped_length < extent_len ||
3895 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08003896 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003897 return;
3898 }
3899
3900 *extent_physical = bbio->stripes[0].physical;
3901 *extent_mirror_num = bbio->mirror_num;
3902 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08003903 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003904}
3905
3906static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3907 struct scrub_wr_ctx *wr_ctx,
3908 struct btrfs_fs_info *fs_info,
3909 struct btrfs_device *dev,
3910 int is_dev_replace)
3911{
3912 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3913
3914 mutex_init(&wr_ctx->wr_lock);
3915 wr_ctx->wr_curr_bio = NULL;
3916 if (!is_dev_replace)
3917 return 0;
3918
3919 WARN_ON(!dev->bdev);
3920 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3921 bio_get_nr_vecs(dev->bdev));
3922 wr_ctx->tgtdev = dev;
3923 atomic_set(&wr_ctx->flush_all_writes, 0);
3924 return 0;
3925}
3926
3927static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3928{
3929 mutex_lock(&wr_ctx->wr_lock);
3930 kfree(wr_ctx->wr_curr_bio);
3931 wr_ctx->wr_curr_bio = NULL;
3932 mutex_unlock(&wr_ctx->wr_lock);
3933}
3934
3935static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3936 int mirror_num, u64 physical_for_dev_replace)
3937{
3938 struct scrub_copy_nocow_ctx *nocow_ctx;
3939 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3940
3941 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3942 if (!nocow_ctx) {
3943 spin_lock(&sctx->stat_lock);
3944 sctx->stat.malloc_errors++;
3945 spin_unlock(&sctx->stat_lock);
3946 return -ENOMEM;
3947 }
3948
3949 scrub_pending_trans_workers_inc(sctx);
3950
3951 nocow_ctx->sctx = sctx;
3952 nocow_ctx->logical = logical;
3953 nocow_ctx->len = len;
3954 nocow_ctx->mirror_num = mirror_num;
3955 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08003956 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3957 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003958 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003959 btrfs_queue_work(fs_info->scrub_nocow_workers,
3960 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003961
3962 return 0;
3963}
3964
Josef Bacik652f25a2013-09-12 16:58:28 -04003965static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3966{
3967 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3968 struct scrub_nocow_inode *nocow_inode;
3969
3970 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3971 if (!nocow_inode)
3972 return -ENOMEM;
3973 nocow_inode->inum = inum;
3974 nocow_inode->offset = offset;
3975 nocow_inode->root = root;
3976 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3977 return 0;
3978}
3979
3980#define COPY_COMPLETE 1
3981
Stefan Behrensff023aa2012-11-06 11:43:11 +01003982static void copy_nocow_pages_worker(struct btrfs_work *work)
3983{
3984 struct scrub_copy_nocow_ctx *nocow_ctx =
3985 container_of(work, struct scrub_copy_nocow_ctx, work);
3986 struct scrub_ctx *sctx = nocow_ctx->sctx;
3987 u64 logical = nocow_ctx->logical;
3988 u64 len = nocow_ctx->len;
3989 int mirror_num = nocow_ctx->mirror_num;
3990 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3991 int ret;
3992 struct btrfs_trans_handle *trans = NULL;
3993 struct btrfs_fs_info *fs_info;
3994 struct btrfs_path *path;
3995 struct btrfs_root *root;
3996 int not_written = 0;
3997
3998 fs_info = sctx->dev_root->fs_info;
3999 root = fs_info->extent_root;
4000
4001 path = btrfs_alloc_path();
4002 if (!path) {
4003 spin_lock(&sctx->stat_lock);
4004 sctx->stat.malloc_errors++;
4005 spin_unlock(&sctx->stat_lock);
4006 not_written = 1;
4007 goto out;
4008 }
4009
4010 trans = btrfs_join_transaction(root);
4011 if (IS_ERR(trans)) {
4012 not_written = 1;
4013 goto out;
4014 }
4015
4016 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04004017 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004018 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004019 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
4020 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02004021 logical, physical_for_dev_replace, len, mirror_num,
4022 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004023 not_written = 1;
4024 goto out;
4025 }
4026
Josef Bacik652f25a2013-09-12 16:58:28 -04004027 btrfs_end_transaction(trans, root);
4028 trans = NULL;
4029 while (!list_empty(&nocow_ctx->inodes)) {
4030 struct scrub_nocow_inode *entry;
4031 entry = list_first_entry(&nocow_ctx->inodes,
4032 struct scrub_nocow_inode,
4033 list);
4034 list_del_init(&entry->list);
4035 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4036 entry->root, nocow_ctx);
4037 kfree(entry);
4038 if (ret == COPY_COMPLETE) {
4039 ret = 0;
4040 break;
4041 } else if (ret) {
4042 break;
4043 }
4044 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004045out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004046 while (!list_empty(&nocow_ctx->inodes)) {
4047 struct scrub_nocow_inode *entry;
4048 entry = list_first_entry(&nocow_ctx->inodes,
4049 struct scrub_nocow_inode,
4050 list);
4051 list_del_init(&entry->list);
4052 kfree(entry);
4053 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004054 if (trans && !IS_ERR(trans))
4055 btrfs_end_transaction(trans, root);
4056 if (not_written)
4057 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4058 num_uncorrectable_read_errors);
4059
4060 btrfs_free_path(path);
4061 kfree(nocow_ctx);
4062
4063 scrub_pending_trans_workers_dec(sctx);
4064}
4065
Gui Hecheng32159242014-11-10 15:36:08 +08004066static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4067 u64 logical)
4068{
4069 struct extent_state *cached_state = NULL;
4070 struct btrfs_ordered_extent *ordered;
4071 struct extent_io_tree *io_tree;
4072 struct extent_map *em;
4073 u64 lockstart = start, lockend = start + len - 1;
4074 int ret = 0;
4075
4076 io_tree = &BTRFS_I(inode)->io_tree;
4077
4078 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4079 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4080 if (ordered) {
4081 btrfs_put_ordered_extent(ordered);
4082 ret = 1;
4083 goto out_unlock;
4084 }
4085
4086 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4087 if (IS_ERR(em)) {
4088 ret = PTR_ERR(em);
4089 goto out_unlock;
4090 }
4091
4092 /*
4093 * This extent does not actually cover the logical extent anymore,
4094 * move on to the next inode.
4095 */
4096 if (em->block_start > logical ||
4097 em->block_start + em->block_len < logical + len) {
4098 free_extent_map(em);
4099 ret = 1;
4100 goto out_unlock;
4101 }
4102 free_extent_map(em);
4103
4104out_unlock:
4105 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4106 GFP_NOFS);
4107 return ret;
4108}
4109
Josef Bacik652f25a2013-09-12 16:58:28 -04004110static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4111 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004112{
Miao Xie826aa0a2013-06-27 18:50:59 +08004113 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004114 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004115 struct inode *inode;
4116 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004117 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004118 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004119 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004120 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004121 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004122 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004123 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004124 int ret = 0;
4125 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004126
4127 key.objectid = root;
4128 key.type = BTRFS_ROOT_ITEM_KEY;
4129 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004130
4131 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4132
Stefan Behrensff023aa2012-11-06 11:43:11 +01004133 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004134 if (IS_ERR(local_root)) {
4135 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004136 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004137 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004138
4139 key.type = BTRFS_INODE_ITEM_KEY;
4140 key.objectid = inum;
4141 key.offset = 0;
4142 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004143 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004144 if (IS_ERR(inode))
4145 return PTR_ERR(inode);
4146
Miao Xieedd14002013-06-27 18:51:00 +08004147 /* Avoid truncate/dio/punch hole.. */
4148 mutex_lock(&inode->i_mutex);
4149 inode_dio_wait(inode);
4150
Stefan Behrensff023aa2012-11-06 11:43:11 +01004151 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004152 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004153 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004154
Gui Hecheng32159242014-11-10 15:36:08 +08004155 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4156 if (ret) {
4157 ret = ret > 0 ? 0 : ret;
4158 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004159 }
4160
Stefan Behrensff023aa2012-11-06 11:43:11 +01004161 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004162 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004163again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004164 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4165 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004166 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004167 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004168 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004169 }
4170
4171 if (PageUptodate(page)) {
4172 if (PageDirty(page))
4173 goto next_page;
4174 } else {
4175 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004176 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004177 btrfs_get_extent,
4178 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004179 if (err) {
4180 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004181 goto next_page;
4182 }
Miao Xieedd14002013-06-27 18:51:00 +08004183
Miao Xie26b258912013-06-27 18:50:58 +08004184 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004185 /*
4186 * If the page has been remove from the page cache,
4187 * the data on it is meaningless, because it may be
4188 * old one, the new data may be written into the new
4189 * page in the page cache.
4190 */
4191 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004192 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004193 page_cache_release(page);
4194 goto again;
4195 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004196 if (!PageUptodate(page)) {
4197 ret = -EIO;
4198 goto next_page;
4199 }
4200 }
Gui Hecheng32159242014-11-10 15:36:08 +08004201
4202 ret = check_extent_to_block(inode, offset, len,
4203 nocow_ctx_logical);
4204 if (ret) {
4205 ret = ret > 0 ? 0 : ret;
4206 goto next_page;
4207 }
4208
Miao Xie826aa0a2013-06-27 18:50:59 +08004209 err = write_page_nocow(nocow_ctx->sctx,
4210 physical_for_dev_replace, page);
4211 if (err)
4212 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004213next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004214 unlock_page(page);
4215 page_cache_release(page);
4216
4217 if (ret)
4218 break;
4219
Stefan Behrensff023aa2012-11-06 11:43:11 +01004220 offset += PAGE_CACHE_SIZE;
4221 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004222 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004223 len -= PAGE_CACHE_SIZE;
4224 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004225 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004226out:
Miao Xieedd14002013-06-27 18:51:00 +08004227 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004228 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004229 return ret;
4230}
4231
4232static int write_page_nocow(struct scrub_ctx *sctx,
4233 u64 physical_for_dev_replace, struct page *page)
4234{
4235 struct bio *bio;
4236 struct btrfs_device *dev;
4237 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004238
4239 dev = sctx->wr_ctx.tgtdev;
4240 if (!dev)
4241 return -EIO;
4242 if (!dev->bdev) {
4243 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05004244 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004245 return -EIO;
4246 }
Chris Mason9be33952013-05-17 18:30:14 -04004247 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004248 if (!bio) {
4249 spin_lock(&sctx->stat_lock);
4250 sctx->stat.malloc_errors++;
4251 spin_unlock(&sctx->stat_lock);
4252 return -ENOMEM;
4253 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004254 bio->bi_iter.bi_size = 0;
4255 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004256 bio->bi_bdev = dev->bdev;
4257 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4258 if (ret != PAGE_CACHE_SIZE) {
4259leave_with_eio:
4260 bio_put(bio);
4261 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4262 return -EIO;
4263 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004264
Kent Overstreet33879d42013-11-23 22:33:32 -08004265 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004266 goto leave_with_eio;
4267
4268 bio_put(bio);
4269 return 0;
4270}