blob: c3a98931980f59ff3f80d3fc29bb8e21a98da6e2 [file] [log] [blame]
Arne Jansena2de7332011-03-08 14:14:00 +01001/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
Arne Jansena2de7332011-03-08 14:14:00 +01003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Arne Jansena2de7332011-03-08 14:14:00 +010019#include <linux/blkdev.h>
Jan Schmidt558540c2011-06-13 19:59:12 +020020#include <linux/ratelimit.h>
Arne Jansena2de7332011-03-08 14:14:00 +010021#include "ctree.h"
22#include "volumes.h"
23#include "disk-io.h"
24#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020025#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020026#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020027#include "extent_io.h"
Stefan Behrensff023aa2012-11-06 11:43:11 +010028#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010029#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040030#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050031#include "raid56.h"
Arne Jansena2de7332011-03-08 14:14:00 +010032
33/*
34 * This is only the first step towards a full-features scrub. It reads all
35 * extent and super block and verifies the checksums. In case a bad checksum
36 * is found or the extent cannot be read, good data will be written back if
37 * any can be found.
38 *
39 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010040 * - In case an unrepairable extent is encountered, track which files are
41 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010042 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010043 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010044 */
45
Stefan Behrensb5d67f62012-03-27 14:21:27 -040046struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010047struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010048
Stefan Behrensff023aa2012-11-06 11:43:11 +010049/*
50 * the following three values only influence the performance.
51 * The last one configures the number of parallel and outstanding I/O
52 * operations. The first two values configure an upper limit for the number
53 * of (dynamically allocated) pages that are added to a bio.
54 */
55#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
56#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
57#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010058
59/*
60 * the following value times PAGE_SIZE needs to be large enough to match the
61 * largest node/leaf/sector size that shall be supported.
62 * Values larger than BTRFS_STRIPE_LEN are not supported.
63 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040064#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010065
Miao Xieaf8e2d12014-10-23 14:42:50 +080066struct scrub_recover {
67 atomic_t refs;
68 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +080069 u64 map_length;
70};
71
Arne Jansena2de7332011-03-08 14:14:00 +010072struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040073 struct scrub_block *sblock;
74 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020075 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080076 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010077 u64 flags; /* extent flags */
78 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040079 u64 logical;
80 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010081 u64 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +010082 atomic_t ref_count;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040083 struct {
84 unsigned int mirror_num:8;
85 unsigned int have_csum:1;
86 unsigned int io_error:1;
87 };
Arne Jansena2de7332011-03-08 14:14:00 +010088 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080089
90 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010091};
92
93struct scrub_bio {
94 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010095 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010096 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010097 struct bio *bio;
98 int err;
99 u64 logical;
100 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100101#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
102 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
103#else
104 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
105#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400106 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +0100107 int next_free;
108 struct btrfs_work work;
109};
110
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400111struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100112 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400113 int page_count;
114 atomic_t outstanding_pages;
115 atomic_t ref_count; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100116 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800117 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400118 struct {
119 unsigned int header_error:1;
120 unsigned int checksum_error:1;
121 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200122 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800123
124 /* The following is for the data used to check parity */
125 /* It is for the data with checksum */
126 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400127 };
128};
129
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800130/* Used for the chunks with parity stripe such RAID5/6 */
131struct scrub_parity {
132 struct scrub_ctx *sctx;
133
134 struct btrfs_device *scrub_dev;
135
136 u64 logic_start;
137
138 u64 logic_end;
139
140 int nsectors;
141
142 int stripe_len;
143
144 atomic_t ref_count;
145
146 struct list_head spages;
147
148 /* Work of parity check and repair */
149 struct btrfs_work work;
150
151 /* Mark the parity blocks which have data */
152 unsigned long *dbitmap;
153
154 /*
155 * Mark the parity blocks which have data, but errors happen when
156 * read data or check data
157 */
158 unsigned long *ebitmap;
159
160 unsigned long bitmap[0];
161};
162
Stefan Behrensff023aa2012-11-06 11:43:11 +0100163struct scrub_wr_ctx {
164 struct scrub_bio *wr_curr_bio;
165 struct btrfs_device *tgtdev;
166 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
167 atomic_t flush_all_writes;
168 struct mutex wr_lock;
169};
170
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100171struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100172 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100173 struct btrfs_root *dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +0100174 int first_free;
175 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100176 atomic_t bios_in_flight;
177 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100178 spinlock_t list_lock;
179 wait_queue_head_t list_wait;
180 u16 csum_size;
181 struct list_head csum_list;
182 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100183 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100184 int pages_per_rd_bio;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400185 u32 sectorsize;
186 u32 nodesize;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100187
188 int is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100189 struct scrub_wr_ctx wr_ctx;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100190
Arne Jansena2de7332011-03-08 14:14:00 +0100191 /*
192 * statistics
193 */
194 struct btrfs_scrub_progress stat;
195 spinlock_t stat_lock;
196};
197
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200198struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100199 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100200 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200201 u64 logical;
202 struct btrfs_root *root;
203 struct btrfs_work work;
204 int mirror_num;
205};
206
Josef Bacik652f25a2013-09-12 16:58:28 -0400207struct scrub_nocow_inode {
208 u64 inum;
209 u64 offset;
210 u64 root;
211 struct list_head list;
212};
213
Stefan Behrensff023aa2012-11-06 11:43:11 +0100214struct scrub_copy_nocow_ctx {
215 struct scrub_ctx *sctx;
216 u64 logical;
217 u64 len;
218 int mirror_num;
219 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -0400220 struct list_head inodes;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100221 struct btrfs_work work;
222};
223
Jan Schmidt558540c2011-06-13 19:59:12 +0200224struct scrub_warning {
225 struct btrfs_path *path;
226 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200227 const char *errstr;
228 sector_t sector;
229 u64 logical;
230 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200231};
232
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100233static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
234static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
235static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
236static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400237static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Zhao Leibe50a8d2015-01-20 15:11:42 +0800238static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100239 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100240static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
241 struct scrub_block *sblock, int is_metadata,
242 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800243 u16 csum_size, int retry_failed_mirror);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400244static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
245 struct scrub_block *sblock,
246 int is_metadata, int have_csum,
247 const u8 *csum, u64 generation,
248 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400249static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800250 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400251static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
252 struct scrub_block *sblock_good,
253 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100254static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
255static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
256 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400257static int scrub_checksum_data(struct scrub_block *sblock);
258static int scrub_checksum_tree_block(struct scrub_block *sblock);
259static int scrub_checksum_super(struct scrub_block *sblock);
260static void scrub_block_get(struct scrub_block *sblock);
261static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100262static void scrub_page_get(struct scrub_page *spage);
263static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800264static void scrub_parity_get(struct scrub_parity *sparity);
265static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100266static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
267 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100268static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100269 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100270 u64 gen, int mirror_num, u8 *csum, int force,
271 u64 physical_for_dev_replace);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400272static void scrub_bio_end_io(struct bio *bio, int err);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400273static void scrub_bio_end_io_worker(struct btrfs_work *work);
274static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100275static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
276 u64 extent_logical, u64 extent_len,
277 u64 *extent_physical,
278 struct btrfs_device **extent_dev,
279 int *extent_mirror_num);
280static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
281 struct scrub_wr_ctx *wr_ctx,
282 struct btrfs_fs_info *fs_info,
283 struct btrfs_device *dev,
284 int is_dev_replace);
285static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
286static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
287 struct scrub_page *spage);
288static void scrub_wr_submit(struct scrub_ctx *sctx);
289static void scrub_wr_bio_end_io(struct bio *bio, int err);
290static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
291static int write_page_nocow(struct scrub_ctx *sctx,
292 u64 physical_for_dev_replace, struct page *page);
293static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400294 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100295static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
296 int mirror_num, u64 physical_for_dev_replace);
297static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800298static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800299static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400300
301
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100302static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
303{
304 atomic_inc(&sctx->bios_in_flight);
305}
306
307static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
308{
309 atomic_dec(&sctx->bios_in_flight);
310 wake_up(&sctx->list_wait);
311}
312
Wang Shilongcb7ab022013-12-04 21:16:53 +0800313static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800314{
315 while (atomic_read(&fs_info->scrub_pause_req)) {
316 mutex_unlock(&fs_info->scrub_lock);
317 wait_event(fs_info->scrub_pause_wait,
318 atomic_read(&fs_info->scrub_pause_req) == 0);
319 mutex_lock(&fs_info->scrub_lock);
320 }
321}
322
Wang Shilongcb7ab022013-12-04 21:16:53 +0800323static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
324{
325 atomic_inc(&fs_info->scrubs_paused);
326 wake_up(&fs_info->scrub_pause_wait);
327
328 mutex_lock(&fs_info->scrub_lock);
329 __scrub_blocked_if_needed(fs_info);
330 atomic_dec(&fs_info->scrubs_paused);
331 mutex_unlock(&fs_info->scrub_lock);
332
333 wake_up(&fs_info->scrub_pause_wait);
334}
335
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100336/*
337 * used for workers that require transaction commits (i.e., for the
338 * NOCOW case)
339 */
340static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
341{
342 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
343
344 /*
345 * increment scrubs_running to prevent cancel requests from
346 * completing as long as a worker is running. we must also
347 * increment scrubs_paused to prevent deadlocking on pause
348 * requests used for transactions commits (as the worker uses a
349 * transaction context). it is safe to regard the worker
350 * as paused for all matters practical. effectively, we only
351 * avoid cancellation requests from completing.
352 */
353 mutex_lock(&fs_info->scrub_lock);
354 atomic_inc(&fs_info->scrubs_running);
355 atomic_inc(&fs_info->scrubs_paused);
356 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800357
358 /*
359 * check if @scrubs_running=@scrubs_paused condition
360 * inside wait_event() is not an atomic operation.
361 * which means we may inc/dec @scrub_running/paused
362 * at any time. Let's wake up @scrub_pause_wait as
363 * much as we can to let commit transaction blocked less.
364 */
365 wake_up(&fs_info->scrub_pause_wait);
366
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100367 atomic_inc(&sctx->workers_pending);
368}
369
370/* used for workers that require transaction commits */
371static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
372{
373 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
374
375 /*
376 * see scrub_pending_trans_workers_inc() why we're pretending
377 * to be paused in the scrub counters
378 */
379 mutex_lock(&fs_info->scrub_lock);
380 atomic_dec(&fs_info->scrubs_running);
381 atomic_dec(&fs_info->scrubs_paused);
382 mutex_unlock(&fs_info->scrub_lock);
383 atomic_dec(&sctx->workers_pending);
384 wake_up(&fs_info->scrub_pause_wait);
385 wake_up(&sctx->list_wait);
386}
387
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100388static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100389{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100390 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100391 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100392 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100393 struct btrfs_ordered_sum, list);
394 list_del(&sum->list);
395 kfree(sum);
396 }
397}
398
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100399static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100400{
401 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100402
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100403 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100404 return;
405
Stefan Behrensff023aa2012-11-06 11:43:11 +0100406 scrub_free_wr_ctx(&sctx->wr_ctx);
407
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400408 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100409 if (sctx->curr != -1) {
410 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400411
412 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100413 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400414 scrub_block_put(sbio->pagev[i]->sblock);
415 }
416 bio_put(sbio->bio);
417 }
418
Stefan Behrensff023aa2012-11-06 11:43:11 +0100419 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100420 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100421
422 if (!sbio)
423 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100424 kfree(sbio);
425 }
426
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100427 scrub_free_csums(sctx);
428 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100429}
430
431static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100432struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100433{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100434 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100435 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100436 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100437 int pages_per_rd_bio;
438 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100439
Stefan Behrensff023aa2012-11-06 11:43:11 +0100440 /*
441 * the setting of pages_per_rd_bio is correct for scrub but might
442 * be wrong for the dev_replace code where we might read from
443 * different devices in the initial huge bios. However, that
444 * code is able to correctly handle the case when adding a page
445 * to a bio fails.
446 */
447 if (dev->bdev)
448 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
449 bio_get_nr_vecs(dev->bdev));
450 else
451 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100452 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
453 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100454 goto nomem;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100455 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100456 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100457 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100458 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100459 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100460 struct scrub_bio *sbio;
461
462 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
463 if (!sbio)
464 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100465 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100466
Arne Jansena2de7332011-03-08 14:14:00 +0100467 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100468 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400469 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800470 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
471 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100472
Stefan Behrensff023aa2012-11-06 11:43:11 +0100473 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100474 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200475 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100476 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100477 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100478 sctx->first_free = 0;
479 sctx->nodesize = dev->dev_root->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100480 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100481 atomic_set(&sctx->bios_in_flight, 0);
482 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100483 atomic_set(&sctx->cancel_req, 0);
484 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
485 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100486
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100487 spin_lock_init(&sctx->list_lock);
488 spin_lock_init(&sctx->stat_lock);
489 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100490
491 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
492 fs_info->dev_replace.tgtdev, is_dev_replace);
493 if (ret) {
494 scrub_free_ctx(sctx);
495 return ERR_PTR(ret);
496 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100497 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100498
499nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100500 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100501 return ERR_PTR(-ENOMEM);
502}
503
Stefan Behrensff023aa2012-11-06 11:43:11 +0100504static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
505 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200506{
507 u64 isize;
508 u32 nlink;
509 int ret;
510 int i;
511 struct extent_buffer *eb;
512 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100513 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200514 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
515 struct inode_fs_paths *ipath = NULL;
516 struct btrfs_root *local_root;
517 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100518 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200519
520 root_key.objectid = root;
521 root_key.type = BTRFS_ROOT_ITEM_KEY;
522 root_key.offset = (u64)-1;
523 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
524 if (IS_ERR(local_root)) {
525 ret = PTR_ERR(local_root);
526 goto err;
527 }
528
David Sterba14692cc2015-01-02 18:55:46 +0100529 /*
530 * this makes the path point to (inum INODE_ITEM ioff)
531 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100532 key.objectid = inum;
533 key.type = BTRFS_INODE_ITEM_KEY;
534 key.offset = 0;
535
536 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200537 if (ret) {
538 btrfs_release_path(swarn->path);
539 goto err;
540 }
541
542 eb = swarn->path->nodes[0];
543 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
544 struct btrfs_inode_item);
545 isize = btrfs_inode_size(eb, inode_item);
546 nlink = btrfs_inode_nlink(eb, inode_item);
547 btrfs_release_path(swarn->path);
548
549 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300550 if (IS_ERR(ipath)) {
551 ret = PTR_ERR(ipath);
552 ipath = NULL;
553 goto err;
554 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200555 ret = paths_from_inode(inum, ipath);
556
557 if (ret < 0)
558 goto err;
559
560 /*
561 * we deliberately ignore the bit ipath might have been too small to
562 * hold all of the paths here
563 */
564 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Frank Holtonefe120a2013-12-20 11:37:06 -0500565 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200566 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
567 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400568 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200569 (unsigned long long)swarn->sector, root, inum, offset,
570 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500571 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200572
573 free_ipath(ipath);
574 return 0;
575
576err:
Frank Holtonefe120a2013-12-20 11:37:06 -0500577 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200578 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
579 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400580 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200581 (unsigned long long)swarn->sector, root, inum, offset, ret);
582
583 free_ipath(ipath);
584 return 0;
585}
586
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400587static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200588{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100589 struct btrfs_device *dev;
590 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200591 struct btrfs_path *path;
592 struct btrfs_key found_key;
593 struct extent_buffer *eb;
594 struct btrfs_extent_item *ei;
595 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200596 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100597 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600598 u64 flags = 0;
599 u64 ref_root;
600 u32 item_size;
601 u8 ref_level;
Liu Bo69917e42012-09-07 20:01:28 -0600602 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200603
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100604 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100605 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100606 fs_info = sblock->sctx->dev_root->fs_info;
607
Jan Schmidt558540c2011-06-13 19:59:12 +0200608 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200609 if (!path)
610 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200611
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100612 swarn.sector = (sblock->pagev[0]->physical) >> 9;
613 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200614 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100615 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200616
Liu Bo69917e42012-09-07 20:01:28 -0600617 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
618 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200619 if (ret < 0)
620 goto out;
621
Jan Schmidt4692cf52011-12-02 14:56:41 +0100622 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200623 swarn.extent_item_size = found_key.offset;
624
625 eb = path->nodes[0];
626 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
627 item_size = btrfs_item_size_nr(eb, path->slots[0]);
628
Liu Bo69917e42012-09-07 20:01:28 -0600629 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200630 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800631 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
632 item_size, &ref_root,
633 &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400634 printk_in_rcu(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -0500635 "BTRFS: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200636 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400637 "%llu\n", errstr, swarn.logical,
638 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200639 (unsigned long long)swarn.sector,
640 ref_level ? "node" : "leaf",
641 ret < 0 ? -1 : ref_level,
642 ret < 0 ? -1 : ref_root);
643 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600644 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200645 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600646 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200647 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100648 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100649 iterate_extent_inodes(fs_info, found_key.objectid,
650 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200651 scrub_print_warning_inode, &swarn);
652 }
653
654out:
655 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200656}
657
Stefan Behrensff023aa2012-11-06 11:43:11 +0100658static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200659{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200660 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200661 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100662 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200663 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200664 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200665 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200666 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000667 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200668 u64 end = offset + PAGE_SIZE - 1;
669 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000670 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200671
672 key.objectid = root;
673 key.type = BTRFS_ROOT_ITEM_KEY;
674 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000675
676 fs_info = fixup->root->fs_info;
677 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
678
679 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
680 if (IS_ERR(local_root)) {
681 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200682 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000683 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200684
685 key.type = BTRFS_INODE_ITEM_KEY;
686 key.objectid = inum;
687 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000688 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
689 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200690 if (IS_ERR(inode))
691 return PTR_ERR(inode);
692
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200693 index = offset >> PAGE_CACHE_SHIFT;
694
695 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200696 if (!page) {
697 ret = -ENOMEM;
698 goto out;
699 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200700
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200701 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200702 if (PageDirty(page)) {
703 /*
704 * we need to write the data to the defect sector. the
705 * data that was in that sector is not in memory,
706 * because the page was modified. we must not write the
707 * modified page to that sector.
708 *
709 * TODO: what could be done here: wait for the delalloc
710 * runner to write out that page (might involve
711 * COW) and see whether the sector is still
712 * referenced afterwards.
713 *
714 * For the meantime, we'll treat this error
715 * incorrectable, although there is a chance that a
716 * later scrub will find the bad sector again and that
717 * there's no dirty page in memory, then.
718 */
719 ret = -EIO;
720 goto out;
721 }
Miao Xie1203b682014-09-12 18:44:01 +0800722 ret = repair_io_failure(inode, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200723 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800724 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200725 fixup->mirror_num);
726 unlock_page(page);
727 corrected = !ret;
728 } else {
729 /*
730 * we need to get good data first. the general readpage path
731 * will call repair_io_failure for us, we just have to make
732 * sure we read the bad mirror.
733 */
734 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
735 EXTENT_DAMAGED, GFP_NOFS);
736 if (ret) {
737 /* set_extent_bits should give proper error */
738 WARN_ON(ret > 0);
739 if (ret > 0)
740 ret = -EFAULT;
741 goto out;
742 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200743
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200744 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
745 btrfs_get_extent,
746 fixup->mirror_num);
747 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200748
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200749 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
750 end, EXTENT_DAMAGED, 0, NULL);
751 if (!corrected)
752 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
753 EXTENT_DAMAGED, GFP_NOFS);
754 }
755
756out:
757 if (page)
758 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200759
760 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200761
762 if (ret < 0)
763 return ret;
764
765 if (ret == 0 && corrected) {
766 /*
767 * we only need to call readpage for one of the inodes belonging
768 * to this extent. so make iterate_extent_inodes stop
769 */
770 return 1;
771 }
772
773 return -EIO;
774}
775
776static void scrub_fixup_nodatasum(struct btrfs_work *work)
777{
778 int ret;
779 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100780 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200781 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200782 struct btrfs_path *path;
783 int uncorrectable = 0;
784
785 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100786 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200787
788 path = btrfs_alloc_path();
789 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100790 spin_lock(&sctx->stat_lock);
791 ++sctx->stat.malloc_errors;
792 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200793 uncorrectable = 1;
794 goto out;
795 }
796
797 trans = btrfs_join_transaction(fixup->root);
798 if (IS_ERR(trans)) {
799 uncorrectable = 1;
800 goto out;
801 }
802
803 /*
804 * the idea is to trigger a regular read through the standard path. we
805 * read a page from the (failed) logical address by specifying the
806 * corresponding copynum of the failed sector. thus, that readpage is
807 * expected to fail.
808 * that is the point where on-the-fly error correction will kick in
809 * (once it's finished) and rewrite the failed sector if a good copy
810 * can be found.
811 */
812 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
813 path, scrub_fixup_readpage,
814 fixup);
815 if (ret < 0) {
816 uncorrectable = 1;
817 goto out;
818 }
819 WARN_ON(ret != 1);
820
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100821 spin_lock(&sctx->stat_lock);
822 ++sctx->stat.corrected_errors;
823 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200824
825out:
826 if (trans && !IS_ERR(trans))
827 btrfs_end_transaction(trans, fixup->root);
828 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100829 spin_lock(&sctx->stat_lock);
830 ++sctx->stat.uncorrectable_errors;
831 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100832 btrfs_dev_replace_stats_inc(
833 &sctx->dev_root->fs_info->dev_replace.
834 num_uncorrectable_read_errors);
Frank Holtonefe120a2013-12-20 11:37:06 -0500835 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
836 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200837 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200838 }
839
840 btrfs_free_path(path);
841 kfree(fixup);
842
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100843 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200844}
845
Miao Xieaf8e2d12014-10-23 14:42:50 +0800846static inline void scrub_get_recover(struct scrub_recover *recover)
847{
848 atomic_inc(&recover->refs);
849}
850
851static inline void scrub_put_recover(struct scrub_recover *recover)
852{
853 if (atomic_dec_and_test(&recover->refs)) {
Zhao Lei6e9606d2015-01-20 15:11:34 +0800854 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800855 kfree(recover);
856 }
857}
858
Arne Jansena2de7332011-03-08 14:14:00 +0100859/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400860 * scrub_handle_errored_block gets called when either verification of the
861 * pages failed or the bio failed to read, e.g. with EIO. In the latter
862 * case, this function handles all pages in the bio, even though only one
863 * may be bad.
864 * The goal of this function is to repair the errored block by using the
865 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100866 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400867static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100868{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100869 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100870 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400871 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100872 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400873 u64 logical;
874 u64 generation;
875 unsigned int failed_mirror_index;
876 unsigned int is_metadata;
877 unsigned int have_csum;
878 u8 *csum;
879 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
880 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100881 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400882 int mirror_index;
883 int page_num;
884 int success;
885 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
886 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100887
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400888 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100889 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000890 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
891 /*
892 * if we find an error in a super block, we just report it.
893 * They will get written with the next transaction commit
894 * anyway
895 */
896 spin_lock(&sctx->stat_lock);
897 ++sctx->stat.super_errors;
898 spin_unlock(&sctx->stat_lock);
899 return 0;
900 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400901 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100902 logical = sblock_to_check->pagev[0]->logical;
903 generation = sblock_to_check->pagev[0]->generation;
904 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
905 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
906 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400907 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100908 have_csum = sblock_to_check->pagev[0]->have_csum;
909 csum = sblock_to_check->pagev[0]->csum;
910 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400911
Stefan Behrensff023aa2012-11-06 11:43:11 +0100912 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
913 sblocks_for_recheck = NULL;
914 goto nodatasum_case;
915 }
916
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400917 /*
918 * read all mirrors one after the other. This includes to
919 * re-read the extent or metadata block that failed (that was
920 * the cause that this fixup code is called) another time,
921 * page by page this time in order to know which pages
922 * caused I/O errors and which ones are good (for all mirrors).
923 * It is the goal to handle the situation when more than one
924 * mirror contains I/O errors, but the errors do not
925 * overlap, i.e. the data can be repaired by selecting the
926 * pages from those mirrors without I/O error on the
927 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
928 * would be that mirror #1 has an I/O error on the first page,
929 * the second page is good, and mirror #2 has an I/O error on
930 * the second page, but the first page is good.
931 * Then the first page of the first mirror can be repaired by
932 * taking the first page of the second mirror, and the
933 * second page of the second mirror can be repaired by
934 * copying the contents of the 2nd page of the 1st mirror.
935 * One more note: if the pages of one mirror contain I/O
936 * errors, the checksum cannot be verified. In order to get
937 * the best data for repairing, the first attempt is to find
938 * a mirror without I/O errors and with a validated checksum.
939 * Only if this is not possible, the pages are picked from
940 * mirrors with I/O errors without considering the checksum.
941 * If the latter is the case, at the end, the checksum of the
942 * repaired area is verified in order to correctly maintain
943 * the statistics.
944 */
945
946 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
947 sizeof(*sblocks_for_recheck),
948 GFP_NOFS);
949 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100950 spin_lock(&sctx->stat_lock);
951 sctx->stat.malloc_errors++;
952 sctx->stat.read_errors++;
953 sctx->stat.uncorrectable_errors++;
954 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100955 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400956 goto out;
957 }
958
959 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800960 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400961 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100962 spin_lock(&sctx->stat_lock);
963 sctx->stat.read_errors++;
964 sctx->stat.uncorrectable_errors++;
965 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100966 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400967 goto out;
968 }
969 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
970 sblock_bad = sblocks_for_recheck + failed_mirror_index;
971
972 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100973 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800974 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400975
976 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
977 sblock_bad->no_io_error_seen) {
978 /*
979 * the error disappeared after reading page by page, or
980 * the area was part of a huge bio and other parts of the
981 * bio caused I/O errors, or the block layer merged several
982 * read requests into one and the error is caused by a
983 * different bio (usually one of the two latter cases is
984 * the cause)
985 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100986 spin_lock(&sctx->stat_lock);
987 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800988 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100989 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400990
Stefan Behrensff023aa2012-11-06 11:43:11 +0100991 if (sctx->is_dev_replace)
992 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400993 goto out;
994 }
995
996 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100997 spin_lock(&sctx->stat_lock);
998 sctx->stat.read_errors++;
999 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001000 if (__ratelimit(&_rs))
1001 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001002 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001003 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001004 spin_lock(&sctx->stat_lock);
1005 sctx->stat.csum_errors++;
1006 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001007 if (__ratelimit(&_rs))
1008 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001009 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001010 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001011 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001012 spin_lock(&sctx->stat_lock);
1013 sctx->stat.verify_errors++;
1014 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001015 if (__ratelimit(&_rs))
1016 scrub_print_warning("checksum/header error",
1017 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001018 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001019 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001020 BTRFS_DEV_STAT_GENERATION_ERRS);
1021 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001022 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001023 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001024 }
1025
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001026 if (sctx->readonly) {
1027 ASSERT(!sctx->is_dev_replace);
1028 goto out;
1029 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001030
1031 if (!is_metadata && !have_csum) {
1032 struct scrub_fixup_nodatasum *fixup_nodatasum;
1033
Stefan Behrensff023aa2012-11-06 11:43:11 +01001034 WARN_ON(sctx->is_dev_replace);
1035
Zhao Leib25c94c2015-01-20 15:11:35 +08001036nodatasum_case:
1037
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001038 /*
1039 * !is_metadata and !have_csum, this means that the data
1040 * might not be COW'ed, that it might be modified
1041 * concurrently. The general strategy to work on the
1042 * commit root does not help in the case when COW is not
1043 * used.
1044 */
1045 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1046 if (!fixup_nodatasum)
1047 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001048 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001049 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001050 fixup_nodatasum->logical = logical;
1051 fixup_nodatasum->root = fs_info->extent_root;
1052 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001053 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001054 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1055 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001056 btrfs_queue_work(fs_info->scrub_workers,
1057 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001058 goto out;
1059 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001060
1061 /*
1062 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001063 * checksums.
1064 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001065 * errors and also does not have a checksum error.
1066 * If one is found, and if a checksum is present, the full block
1067 * that is known to contain an error is rewritten. Afterwards
1068 * the block is known to be corrected.
1069 * If a mirror is found which is completely correct, and no
1070 * checksum is present, only those pages are rewritten that had
1071 * an I/O error in the block to be repaired, since it cannot be
1072 * determined, which copy of the other pages is better (and it
1073 * could happen otherwise that a correct page would be
1074 * overwritten by a bad one).
1075 */
1076 for (mirror_index = 0;
1077 mirror_index < BTRFS_MAX_MIRRORS &&
1078 sblocks_for_recheck[mirror_index].page_count > 0;
1079 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001080 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001081
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001082 if (mirror_index == failed_mirror_index)
1083 continue;
1084 sblock_other = sblocks_for_recheck + mirror_index;
1085
1086 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001087 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1088 have_csum, csum, generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001089 sctx->csum_size, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001090
1091 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001092 !sblock_other->checksum_error &&
1093 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001094 if (sctx->is_dev_replace) {
1095 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001096 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001097 } else {
1098 ret = scrub_repair_block_from_good_copy(
1099 sblock_bad, sblock_other);
1100 if (!ret)
1101 goto corrected_error;
1102 }
Arne Jansena2de7332011-03-08 14:14:00 +01001103 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001104 }
1105
Zhao Leib968fed2015-01-20 15:11:41 +08001106 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1107 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001108
1109 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001110 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001111 * repaired, continue by picking good copies of those pages.
1112 * Select the good pages from mirrors to rewrite bad pages from
1113 * the area to fix. Afterwards verify the checksum of the block
1114 * that is supposed to be repaired. This verification step is
1115 * only done for the purpose of statistic counting and for the
1116 * final scrub report, whether errors remain.
1117 * A perfect algorithm could make use of the checksum and try
1118 * all possible combinations of pages from the different mirrors
1119 * until the checksum verification succeeds. For example, when
1120 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1121 * of mirror #2 is readable but the final checksum test fails,
1122 * then the 2nd page of mirror #3 could be tried, whether now
1123 * the final checksum succeedes. But this would be a rare
1124 * exception and is therefore not implemented. At least it is
1125 * avoided that the good copy is overwritten.
1126 * A more useful improvement would be to pick the sectors
1127 * without I/O error based on sector sizes (512 bytes on legacy
1128 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1129 * mirror could be repaired by taking 512 byte of a different
1130 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1131 * area are unreadable.
1132 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001133 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001134 for (page_num = 0; page_num < sblock_bad->page_count;
1135 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001136 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001137 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001138
Zhao Leib968fed2015-01-20 15:11:41 +08001139 /* skip no-io-error page in scrub */
1140 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001141 continue;
1142
Zhao Leib968fed2015-01-20 15:11:41 +08001143 /* try to find no-io-error page in mirrors */
1144 if (page_bad->io_error) {
1145 for (mirror_index = 0;
1146 mirror_index < BTRFS_MAX_MIRRORS &&
1147 sblocks_for_recheck[mirror_index].page_count > 0;
1148 mirror_index++) {
1149 if (!sblocks_for_recheck[mirror_index].
1150 pagev[page_num]->io_error) {
1151 sblock_other = sblocks_for_recheck +
1152 mirror_index;
1153 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001154 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001155 }
Zhao Leib968fed2015-01-20 15:11:41 +08001156 if (!sblock_other)
1157 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001158 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001159
Zhao Leib968fed2015-01-20 15:11:41 +08001160 if (sctx->is_dev_replace) {
1161 /*
1162 * did not find a mirror to fetch the page
1163 * from. scrub_write_page_to_dev_replace()
1164 * handles this case (page->io_error), by
1165 * filling the block with zeros before
1166 * submitting the write request
1167 */
1168 if (!sblock_other)
1169 sblock_other = sblock_bad;
1170
1171 if (scrub_write_page_to_dev_replace(sblock_other,
1172 page_num) != 0) {
1173 btrfs_dev_replace_stats_inc(
1174 &sctx->dev_root->
1175 fs_info->dev_replace.
1176 num_write_errors);
1177 success = 0;
1178 }
1179 } else if (sblock_other) {
1180 ret = scrub_repair_page_from_good_copy(sblock_bad,
1181 sblock_other,
1182 page_num, 0);
1183 if (0 == ret)
1184 page_bad->io_error = 0;
1185 else
1186 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001187 }
1188 }
1189
Zhao Leib968fed2015-01-20 15:11:41 +08001190 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001191 if (is_metadata || have_csum) {
1192 /*
1193 * need to verify the checksum now that all
1194 * sectors on disk are repaired (the write
1195 * request for data to be repaired is on its way).
1196 * Just be lazy and use scrub_recheck_block()
1197 * which re-reads the data before the checksum
1198 * is verified, but most likely the data comes out
1199 * of the page cache.
1200 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001201 scrub_recheck_block(fs_info, sblock_bad,
1202 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001203 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001204 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001205 !sblock_bad->checksum_error &&
1206 sblock_bad->no_io_error_seen)
1207 goto corrected_error;
1208 else
1209 goto did_not_correct_error;
1210 } else {
1211corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001212 spin_lock(&sctx->stat_lock);
1213 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001214 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001215 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001216 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001217 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001218 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001219 }
1220 } else {
1221did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001222 spin_lock(&sctx->stat_lock);
1223 sctx->stat.uncorrectable_errors++;
1224 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001225 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001226 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001227 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001228 }
1229
1230out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001231 if (sblocks_for_recheck) {
1232 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1233 mirror_index++) {
1234 struct scrub_block *sblock = sblocks_for_recheck +
1235 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001236 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001237 int page_index;
1238
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001239 for (page_index = 0; page_index < sblock->page_count;
1240 page_index++) {
1241 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001242 recover = sblock->pagev[page_index]->recover;
1243 if (recover) {
1244 scrub_put_recover(recover);
1245 sblock->pagev[page_index]->recover =
1246 NULL;
1247 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001248 scrub_page_put(sblock->pagev[page_index]);
1249 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001250 }
1251 kfree(sblocks_for_recheck);
1252 }
1253
1254 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001255}
1256
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001257static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001258{
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001259 if (bbio->raid_map) {
Zhao Leie34c3302015-01-20 15:11:31 +08001260 int real_stripes = bbio->num_stripes - bbio->num_tgtdevs;
1261
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001262 if (bbio->raid_map[real_stripes - 1] == RAID6_Q_STRIPE)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001263 return 3;
1264 else
1265 return 2;
1266 } else {
1267 return (int)bbio->num_stripes;
1268 }
1269}
1270
1271static inline void scrub_stripe_index_and_offset(u64 logical, u64 *raid_map,
1272 u64 mapped_length,
1273 int nstripes, int mirror,
1274 int *stripe_index,
1275 u64 *stripe_offset)
1276{
1277 int i;
1278
1279 if (raid_map) {
1280 /* RAID5/6 */
1281 for (i = 0; i < nstripes; i++) {
1282 if (raid_map[i] == RAID6_Q_STRIPE ||
1283 raid_map[i] == RAID5_P_STRIPE)
1284 continue;
1285
1286 if (logical >= raid_map[i] &&
1287 logical < raid_map[i] + mapped_length)
1288 break;
1289 }
1290
1291 *stripe_index = i;
1292 *stripe_offset = logical - raid_map[i];
1293 } else {
1294 /* The other RAID type */
1295 *stripe_index = mirror;
1296 *stripe_offset = 0;
1297 }
1298}
1299
Zhao Leibe50a8d2015-01-20 15:11:42 +08001300static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001301 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001302{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001303 struct scrub_ctx *sctx = original_sblock->sctx;
1304 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1305 u64 length = original_sblock->page_count * PAGE_SIZE;
1306 u64 logical = original_sblock->pagev[0]->logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001307 struct scrub_recover *recover;
1308 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001309 u64 sublen;
1310 u64 mapped_length;
1311 u64 stripe_offset;
1312 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001313 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001314 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001315 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001316 int ret;
1317
1318 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001319 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001320 * are not used (and not set) in the blocks that are used for
1321 * the recheck procedure
1322 */
1323
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001324 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001325 sublen = min_t(u64, length, PAGE_SIZE);
1326 mapped_length = sublen;
1327 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001328
1329 /*
1330 * with a length of PAGE_SIZE, each returned stripe
1331 * represents one mirror
1332 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001333 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001334 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001335 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001336 btrfs_put_bbio(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001337 return -EIO;
1338 }
1339
Miao Xieaf8e2d12014-10-23 14:42:50 +08001340 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1341 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001342 btrfs_put_bbio(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001343 return -ENOMEM;
1344 }
1345
1346 atomic_set(&recover->refs, 1);
1347 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001348 recover->map_length = mapped_length;
1349
Stefan Behrensff023aa2012-11-06 11:43:11 +01001350 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001351
Zhao Leibe50a8d2015-01-20 15:11:42 +08001352 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001353 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001354 mirror_index++) {
1355 struct scrub_block *sblock;
1356 struct scrub_page *page;
1357
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001358 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001359 sblock->sctx = sctx;
1360 page = kzalloc(sizeof(*page), GFP_NOFS);
1361 if (!page) {
1362leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001363 spin_lock(&sctx->stat_lock);
1364 sctx->stat.malloc_errors++;
1365 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001366 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001367 return -ENOMEM;
1368 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001369 scrub_page_get(page);
1370 sblock->pagev[page_index] = page;
1371 page->logical = logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001372
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001373 scrub_stripe_index_and_offset(logical, bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001374 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001375 bbio->num_stripes -
1376 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001377 mirror_index,
1378 &stripe_index,
1379 &stripe_offset);
1380 page->physical = bbio->stripes[stripe_index].physical +
1381 stripe_offset;
1382 page->dev = bbio->stripes[stripe_index].dev;
1383
Stefan Behrensff023aa2012-11-06 11:43:11 +01001384 BUG_ON(page_index >= original_sblock->page_count);
1385 page->physical_for_dev_replace =
1386 original_sblock->pagev[page_index]->
1387 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001388 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001389 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001390 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001391 page->page = alloc_page(GFP_NOFS);
1392 if (!page->page)
1393 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001394
1395 scrub_get_recover(recover);
1396 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001397 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001398 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001399 length -= sublen;
1400 logical += sublen;
1401 page_index++;
1402 }
1403
1404 return 0;
1405}
1406
Miao Xieaf8e2d12014-10-23 14:42:50 +08001407struct scrub_bio_ret {
1408 struct completion event;
1409 int error;
1410};
1411
1412static void scrub_bio_wait_endio(struct bio *bio, int error)
1413{
1414 struct scrub_bio_ret *ret = bio->bi_private;
1415
1416 ret->error = error;
1417 complete(&ret->event);
1418}
1419
1420static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1421{
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001422 return page->recover && page->recover->bbio->raid_map;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001423}
1424
1425static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1426 struct bio *bio,
1427 struct scrub_page *page)
1428{
1429 struct scrub_bio_ret done;
1430 int ret;
1431
1432 init_completion(&done.event);
1433 done.error = 0;
1434 bio->bi_iter.bi_sector = page->logical >> 9;
1435 bio->bi_private = &done;
1436 bio->bi_end_io = scrub_bio_wait_endio;
1437
1438 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001439 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001440 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001441 if (ret)
1442 return ret;
1443
1444 wait_for_completion(&done.event);
1445 if (done.error)
1446 return -EIO;
1447
1448 return 0;
1449}
1450
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001451/*
1452 * this function will check the on disk data for checksum errors, header
1453 * errors and read I/O errors. If any I/O errors happen, the exact pages
1454 * which are errored are marked as being bad. The goal is to enable scrub
1455 * to take those pages that are not errored from all the mirrors so that
1456 * the pages that are errored in the just handled mirror can be repaired.
1457 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001458static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1459 struct scrub_block *sblock, int is_metadata,
1460 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001461 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001462{
1463 int page_num;
1464
1465 sblock->no_io_error_seen = 1;
1466 sblock->header_error = 0;
1467 sblock->checksum_error = 0;
1468
1469 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1470 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001471 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001472
Stefan Behrens442a4f62012-05-25 16:06:08 +02001473 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001474 page->io_error = 1;
1475 sblock->no_io_error_seen = 0;
1476 continue;
1477 }
1478
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001479 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001480 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001481 if (!bio) {
1482 page->io_error = 1;
1483 sblock->no_io_error_seen = 0;
1484 continue;
1485 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001486 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001487
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001488 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001489 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1490 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1491 sblock->no_io_error_seen = 0;
1492 } else {
1493 bio->bi_iter.bi_sector = page->physical >> 9;
1494
1495 if (btrfsic_submit_bio_wait(READ, bio))
1496 sblock->no_io_error_seen = 0;
1497 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001498
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001499 bio_put(bio);
1500 }
1501
1502 if (sblock->no_io_error_seen)
1503 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1504 have_csum, csum, generation,
1505 csum_size);
1506
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001507 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001508}
1509
Miao Xie17a9be22014-07-24 11:37:08 +08001510static inline int scrub_check_fsid(u8 fsid[],
1511 struct scrub_page *spage)
1512{
1513 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1514 int ret;
1515
1516 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1517 return !ret;
1518}
1519
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001520static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1521 struct scrub_block *sblock,
1522 int is_metadata, int have_csum,
1523 const u8 *csum, u64 generation,
1524 u16 csum_size)
1525{
1526 int page_num;
1527 u8 calculated_csum[BTRFS_CSUM_SIZE];
1528 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001529 void *mapped_buffer;
1530
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001531 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001532 if (is_metadata) {
1533 struct btrfs_header *h;
1534
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001535 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001536 h = (struct btrfs_header *)mapped_buffer;
1537
Qu Wenruo3cae2102013-07-16 11:19:18 +08001538 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001539 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001540 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001541 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001542 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001543 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001544 sblock->header_error = 1;
1545 sblock->generation_error = 1;
1546 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001547 csum = h->csum;
1548 } else {
1549 if (!have_csum)
1550 return;
1551
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001552 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001553 }
1554
1555 for (page_num = 0;;) {
1556 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001557 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001558 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1559 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1560 else
Liu Bob0496682013-03-14 14:57:45 +00001561 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001562
Linus Torvalds9613beb2012-03-30 12:44:29 -07001563 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001564 page_num++;
1565 if (page_num >= sblock->page_count)
1566 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001567 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001568
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001569 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001570 }
1571
1572 btrfs_csum_final(crc, calculated_csum);
1573 if (memcmp(calculated_csum, csum, csum_size))
1574 sblock->checksum_error = 1;
1575}
1576
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001577static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001578 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001579{
1580 int page_num;
1581 int ret = 0;
1582
1583 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1584 int ret_sub;
1585
1586 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1587 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001588 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001589 if (ret_sub)
1590 ret = ret_sub;
1591 }
1592
1593 return ret;
1594}
1595
1596static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1597 struct scrub_block *sblock_good,
1598 int page_num, int force_write)
1599{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001600 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1601 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001602
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001603 BUG_ON(page_bad->page == NULL);
1604 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001605 if (force_write || sblock_bad->header_error ||
1606 sblock_bad->checksum_error || page_bad->io_error) {
1607 struct bio *bio;
1608 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001609
Stefan Behrensff023aa2012-11-06 11:43:11 +01001610 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001611 printk_ratelimited(KERN_WARNING "BTRFS: "
1612 "scrub_repair_page_from_good_copy(bdev == NULL) "
1613 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001614 return -EIO;
1615 }
1616
Chris Mason9be33952013-05-17 18:30:14 -04001617 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001618 if (!bio)
1619 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001620 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001621 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001622
1623 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1624 if (PAGE_SIZE != ret) {
1625 bio_put(bio);
1626 return -EIO;
1627 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001628
Kent Overstreet33879d42013-11-23 22:33:32 -08001629 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001630 btrfs_dev_stat_inc_and_print(page_bad->dev,
1631 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001632 btrfs_dev_replace_stats_inc(
1633 &sblock_bad->sctx->dev_root->fs_info->
1634 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001635 bio_put(bio);
1636 return -EIO;
1637 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001638 bio_put(bio);
1639 }
1640
1641 return 0;
1642}
1643
Stefan Behrensff023aa2012-11-06 11:43:11 +01001644static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1645{
1646 int page_num;
1647
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001648 /*
1649 * This block is used for the check of the parity on the source device,
1650 * so the data needn't be written into the destination device.
1651 */
1652 if (sblock->sparity)
1653 return;
1654
Stefan Behrensff023aa2012-11-06 11:43:11 +01001655 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1656 int ret;
1657
1658 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1659 if (ret)
1660 btrfs_dev_replace_stats_inc(
1661 &sblock->sctx->dev_root->fs_info->dev_replace.
1662 num_write_errors);
1663 }
1664}
1665
1666static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1667 int page_num)
1668{
1669 struct scrub_page *spage = sblock->pagev[page_num];
1670
1671 BUG_ON(spage->page == NULL);
1672 if (spage->io_error) {
1673 void *mapped_buffer = kmap_atomic(spage->page);
1674
1675 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1676 flush_dcache_page(spage->page);
1677 kunmap_atomic(mapped_buffer);
1678 }
1679 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1680}
1681
1682static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1683 struct scrub_page *spage)
1684{
1685 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1686 struct scrub_bio *sbio;
1687 int ret;
1688
1689 mutex_lock(&wr_ctx->wr_lock);
1690again:
1691 if (!wr_ctx->wr_curr_bio) {
1692 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1693 GFP_NOFS);
1694 if (!wr_ctx->wr_curr_bio) {
1695 mutex_unlock(&wr_ctx->wr_lock);
1696 return -ENOMEM;
1697 }
1698 wr_ctx->wr_curr_bio->sctx = sctx;
1699 wr_ctx->wr_curr_bio->page_count = 0;
1700 }
1701 sbio = wr_ctx->wr_curr_bio;
1702 if (sbio->page_count == 0) {
1703 struct bio *bio;
1704
1705 sbio->physical = spage->physical_for_dev_replace;
1706 sbio->logical = spage->logical;
1707 sbio->dev = wr_ctx->tgtdev;
1708 bio = sbio->bio;
1709 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001710 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001711 if (!bio) {
1712 mutex_unlock(&wr_ctx->wr_lock);
1713 return -ENOMEM;
1714 }
1715 sbio->bio = bio;
1716 }
1717
1718 bio->bi_private = sbio;
1719 bio->bi_end_io = scrub_wr_bio_end_io;
1720 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001721 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001722 sbio->err = 0;
1723 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1724 spage->physical_for_dev_replace ||
1725 sbio->logical + sbio->page_count * PAGE_SIZE !=
1726 spage->logical) {
1727 scrub_wr_submit(sctx);
1728 goto again;
1729 }
1730
1731 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1732 if (ret != PAGE_SIZE) {
1733 if (sbio->page_count < 1) {
1734 bio_put(sbio->bio);
1735 sbio->bio = NULL;
1736 mutex_unlock(&wr_ctx->wr_lock);
1737 return -EIO;
1738 }
1739 scrub_wr_submit(sctx);
1740 goto again;
1741 }
1742
1743 sbio->pagev[sbio->page_count] = spage;
1744 scrub_page_get(spage);
1745 sbio->page_count++;
1746 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1747 scrub_wr_submit(sctx);
1748 mutex_unlock(&wr_ctx->wr_lock);
1749
1750 return 0;
1751}
1752
1753static void scrub_wr_submit(struct scrub_ctx *sctx)
1754{
1755 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1756 struct scrub_bio *sbio;
1757
1758 if (!wr_ctx->wr_curr_bio)
1759 return;
1760
1761 sbio = wr_ctx->wr_curr_bio;
1762 wr_ctx->wr_curr_bio = NULL;
1763 WARN_ON(!sbio->bio->bi_bdev);
1764 scrub_pending_bio_inc(sctx);
1765 /* process all writes in a single worker thread. Then the block layer
1766 * orders the requests before sending them to the driver which
1767 * doubled the write performance on spinning disks when measured
1768 * with Linux 3.5 */
1769 btrfsic_submit_bio(WRITE, sbio->bio);
1770}
1771
1772static void scrub_wr_bio_end_io(struct bio *bio, int err)
1773{
1774 struct scrub_bio *sbio = bio->bi_private;
1775 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1776
1777 sbio->err = err;
1778 sbio->bio = bio;
1779
Liu Bo9e0af232014-08-15 23:36:53 +08001780 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1781 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001782 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001783}
1784
1785static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1786{
1787 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1788 struct scrub_ctx *sctx = sbio->sctx;
1789 int i;
1790
1791 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1792 if (sbio->err) {
1793 struct btrfs_dev_replace *dev_replace =
1794 &sbio->sctx->dev_root->fs_info->dev_replace;
1795
1796 for (i = 0; i < sbio->page_count; i++) {
1797 struct scrub_page *spage = sbio->pagev[i];
1798
1799 spage->io_error = 1;
1800 btrfs_dev_replace_stats_inc(&dev_replace->
1801 num_write_errors);
1802 }
1803 }
1804
1805 for (i = 0; i < sbio->page_count; i++)
1806 scrub_page_put(sbio->pagev[i]);
1807
1808 bio_put(sbio->bio);
1809 kfree(sbio);
1810 scrub_pending_bio_dec(sctx);
1811}
1812
1813static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001814{
1815 u64 flags;
1816 int ret;
1817
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001818 WARN_ON(sblock->page_count < 1);
1819 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001820 ret = 0;
1821 if (flags & BTRFS_EXTENT_FLAG_DATA)
1822 ret = scrub_checksum_data(sblock);
1823 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1824 ret = scrub_checksum_tree_block(sblock);
1825 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1826 (void)scrub_checksum_super(sblock);
1827 else
1828 WARN_ON(1);
1829 if (ret)
1830 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001831
1832 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001833}
1834
1835static int scrub_checksum_data(struct scrub_block *sblock)
1836{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001837 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001838 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001839 u8 *on_disk_csum;
1840 struct page *page;
1841 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001842 u32 crc = ~(u32)0;
1843 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001844 u64 len;
1845 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001846
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001847 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001848 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001849 return 0;
1850
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001851 on_disk_csum = sblock->pagev[0]->csum;
1852 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001853 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001854
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001855 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001856 index = 0;
1857 for (;;) {
1858 u64 l = min_t(u64, len, PAGE_SIZE);
1859
Liu Bob0496682013-03-14 14:57:45 +00001860 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001861 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001862 len -= l;
1863 if (len == 0)
1864 break;
1865 index++;
1866 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001867 BUG_ON(!sblock->pagev[index]->page);
1868 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001869 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001870 }
1871
Arne Jansena2de7332011-03-08 14:14:00 +01001872 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001873 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001874 fail = 1;
1875
Arne Jansena2de7332011-03-08 14:14:00 +01001876 return fail;
1877}
1878
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001879static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001880{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001881 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001882 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001883 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001884 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001885 u8 calculated_csum[BTRFS_CSUM_SIZE];
1886 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1887 struct page *page;
1888 void *mapped_buffer;
1889 u64 mapped_size;
1890 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001891 u32 crc = ~(u32)0;
1892 int fail = 0;
1893 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001894 u64 len;
1895 int index;
1896
1897 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001898 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001899 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001900 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001901 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001902
1903 /*
1904 * we don't use the getter functions here, as we
1905 * a) don't have an extent buffer and
1906 * b) the page is already kmapped
1907 */
Arne Jansena2de7332011-03-08 14:14:00 +01001908
Qu Wenruo3cae2102013-07-16 11:19:18 +08001909 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001910 ++fail;
1911
Qu Wenruo3cae2102013-07-16 11:19:18 +08001912 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001913 ++fail;
1914
Miao Xie17a9be22014-07-24 11:37:08 +08001915 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001916 ++fail;
1917
1918 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1919 BTRFS_UUID_SIZE))
1920 ++fail;
1921
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001922 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001923 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1924 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1925 index = 0;
1926 for (;;) {
1927 u64 l = min_t(u64, len, mapped_size);
1928
Liu Bob0496682013-03-14 14:57:45 +00001929 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001930 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001931 len -= l;
1932 if (len == 0)
1933 break;
1934 index++;
1935 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001936 BUG_ON(!sblock->pagev[index]->page);
1937 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001938 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001939 mapped_size = PAGE_SIZE;
1940 p = mapped_buffer;
1941 }
1942
1943 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001944 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001945 ++crc_fail;
1946
Arne Jansena2de7332011-03-08 14:14:00 +01001947 return fail || crc_fail;
1948}
1949
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001950static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001951{
1952 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001953 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001954 u8 calculated_csum[BTRFS_CSUM_SIZE];
1955 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1956 struct page *page;
1957 void *mapped_buffer;
1958 u64 mapped_size;
1959 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001960 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001961 int fail_gen = 0;
1962 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001963 u64 len;
1964 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001965
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001966 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001967 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001968 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001969 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001970 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001971
Qu Wenruo3cae2102013-07-16 11:19:18 +08001972 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001973 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001974
Qu Wenruo3cae2102013-07-16 11:19:18 +08001975 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001976 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001977
Miao Xie17a9be22014-07-24 11:37:08 +08001978 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001979 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001980
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001981 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1982 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1983 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1984 index = 0;
1985 for (;;) {
1986 u64 l = min_t(u64, len, mapped_size);
1987
Liu Bob0496682013-03-14 14:57:45 +00001988 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001989 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001990 len -= l;
1991 if (len == 0)
1992 break;
1993 index++;
1994 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001995 BUG_ON(!sblock->pagev[index]->page);
1996 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001997 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001998 mapped_size = PAGE_SIZE;
1999 p = mapped_buffer;
2000 }
2001
2002 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002003 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002004 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002005
Stefan Behrens442a4f62012-05-25 16:06:08 +02002006 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002007 /*
2008 * if we find an error in a super block, we just report it.
2009 * They will get written with the next transaction commit
2010 * anyway
2011 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002012 spin_lock(&sctx->stat_lock);
2013 ++sctx->stat.super_errors;
2014 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002015 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002016 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002017 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2018 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002019 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002020 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002021 }
2022
Stefan Behrens442a4f62012-05-25 16:06:08 +02002023 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002024}
2025
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002026static void scrub_block_get(struct scrub_block *sblock)
2027{
2028 atomic_inc(&sblock->ref_count);
2029}
2030
2031static void scrub_block_put(struct scrub_block *sblock)
2032{
2033 if (atomic_dec_and_test(&sblock->ref_count)) {
2034 int i;
2035
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002036 if (sblock->sparity)
2037 scrub_parity_put(sblock->sparity);
2038
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002039 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002040 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002041 kfree(sblock);
2042 }
2043}
2044
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002045static void scrub_page_get(struct scrub_page *spage)
2046{
2047 atomic_inc(&spage->ref_count);
2048}
2049
2050static void scrub_page_put(struct scrub_page *spage)
2051{
2052 if (atomic_dec_and_test(&spage->ref_count)) {
2053 if (spage->page)
2054 __free_page(spage->page);
2055 kfree(spage);
2056 }
2057}
2058
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002059static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002060{
2061 struct scrub_bio *sbio;
2062
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002063 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002064 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002065
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002066 sbio = sctx->bios[sctx->curr];
2067 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002068 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002069
Stefan Behrensff023aa2012-11-06 11:43:11 +01002070 if (!sbio->bio->bi_bdev) {
2071 /*
2072 * this case should not happen. If btrfs_map_block() is
2073 * wrong, it could happen for dev-replace operations on
2074 * missing devices when no mirrors are available, but in
2075 * this case it should already fail the mount.
2076 * This case is handled correctly (but _very_ slowly).
2077 */
2078 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05002079 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01002080 bio_endio(sbio->bio, -EIO);
2081 } else {
2082 btrfsic_submit_bio(READ, sbio->bio);
2083 }
Arne Jansena2de7332011-03-08 14:14:00 +01002084}
2085
Stefan Behrensff023aa2012-11-06 11:43:11 +01002086static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2087 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002088{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002089 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002090 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002091 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002092
2093again:
2094 /*
2095 * grab a fresh bio or wait for one to become available
2096 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002097 while (sctx->curr == -1) {
2098 spin_lock(&sctx->list_lock);
2099 sctx->curr = sctx->first_free;
2100 if (sctx->curr != -1) {
2101 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2102 sctx->bios[sctx->curr]->next_free = -1;
2103 sctx->bios[sctx->curr]->page_count = 0;
2104 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002105 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002106 spin_unlock(&sctx->list_lock);
2107 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002108 }
2109 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002110 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002111 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002112 struct bio *bio;
2113
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002114 sbio->physical = spage->physical;
2115 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002116 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002117 bio = sbio->bio;
2118 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002119 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002120 if (!bio)
2121 return -ENOMEM;
2122 sbio->bio = bio;
2123 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002124
2125 bio->bi_private = sbio;
2126 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002127 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002128 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002129 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002130 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2131 spage->physical ||
2132 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002133 spage->logical ||
2134 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002135 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002136 goto again;
2137 }
2138
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002139 sbio->pagev[sbio->page_count] = spage;
2140 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2141 if (ret != PAGE_SIZE) {
2142 if (sbio->page_count < 1) {
2143 bio_put(sbio->bio);
2144 sbio->bio = NULL;
2145 return -EIO;
2146 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002147 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002148 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002149 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002150
Stefan Behrensff023aa2012-11-06 11:43:11 +01002151 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002152 atomic_inc(&sblock->outstanding_pages);
2153 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002154 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002155 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002156
2157 return 0;
2158}
2159
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002160static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002161 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002162 u64 gen, int mirror_num, u8 *csum, int force,
2163 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002164{
2165 struct scrub_block *sblock;
2166 int index;
2167
2168 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2169 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002170 spin_lock(&sctx->stat_lock);
2171 sctx->stat.malloc_errors++;
2172 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002173 return -ENOMEM;
2174 }
2175
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002176 /* one ref inside this function, plus one for each page added to
2177 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002178 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002179 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002180 sblock->no_io_error_seen = 1;
2181
2182 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002183 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002184 u64 l = min_t(u64, len, PAGE_SIZE);
2185
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002186 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2187 if (!spage) {
2188leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002189 spin_lock(&sctx->stat_lock);
2190 sctx->stat.malloc_errors++;
2191 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002192 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002193 return -ENOMEM;
2194 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002195 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2196 scrub_page_get(spage);
2197 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002198 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002199 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002200 spage->flags = flags;
2201 spage->generation = gen;
2202 spage->logical = logical;
2203 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002204 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002205 spage->mirror_num = mirror_num;
2206 if (csum) {
2207 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002208 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002209 } else {
2210 spage->have_csum = 0;
2211 }
2212 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002213 spage->page = alloc_page(GFP_NOFS);
2214 if (!spage->page)
2215 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002216 len -= l;
2217 logical += l;
2218 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002219 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002220 }
2221
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002222 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002223 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002224 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002225 int ret;
2226
Stefan Behrensff023aa2012-11-06 11:43:11 +01002227 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002228 if (ret) {
2229 scrub_block_put(sblock);
2230 return ret;
2231 }
2232 }
2233
2234 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002235 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002236
2237 /* last one frees, either here or in bio completion for last page */
2238 scrub_block_put(sblock);
2239 return 0;
2240}
2241
2242static void scrub_bio_end_io(struct bio *bio, int err)
2243{
2244 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002245 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002246
2247 sbio->err = err;
2248 sbio->bio = bio;
2249
Qu Wenruo0339ef22014-02-28 10:46:17 +08002250 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002251}
2252
2253static void scrub_bio_end_io_worker(struct btrfs_work *work)
2254{
2255 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002256 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002257 int i;
2258
Stefan Behrensff023aa2012-11-06 11:43:11 +01002259 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002260 if (sbio->err) {
2261 for (i = 0; i < sbio->page_count; i++) {
2262 struct scrub_page *spage = sbio->pagev[i];
2263
2264 spage->io_error = 1;
2265 spage->sblock->no_io_error_seen = 0;
2266 }
2267 }
2268
2269 /* now complete the scrub_block items that have all pages completed */
2270 for (i = 0; i < sbio->page_count; i++) {
2271 struct scrub_page *spage = sbio->pagev[i];
2272 struct scrub_block *sblock = spage->sblock;
2273
2274 if (atomic_dec_and_test(&sblock->outstanding_pages))
2275 scrub_block_complete(sblock);
2276 scrub_block_put(sblock);
2277 }
2278
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002279 bio_put(sbio->bio);
2280 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002281 spin_lock(&sctx->list_lock);
2282 sbio->next_free = sctx->first_free;
2283 sctx->first_free = sbio->index;
2284 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002285
2286 if (sctx->is_dev_replace &&
2287 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2288 mutex_lock(&sctx->wr_ctx.wr_lock);
2289 scrub_wr_submit(sctx);
2290 mutex_unlock(&sctx->wr_ctx.wr_lock);
2291 }
2292
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002293 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002294}
2295
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002296static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2297 unsigned long *bitmap,
2298 u64 start, u64 len)
2299{
2300 int offset;
2301 int nsectors;
2302 int sectorsize = sparity->sctx->dev_root->sectorsize;
2303
2304 if (len >= sparity->stripe_len) {
2305 bitmap_set(bitmap, 0, sparity->nsectors);
2306 return;
2307 }
2308
2309 start -= sparity->logic_start;
2310 offset = (int)do_div(start, sparity->stripe_len);
2311 offset /= sectorsize;
2312 nsectors = (int)len / sectorsize;
2313
2314 if (offset + nsectors <= sparity->nsectors) {
2315 bitmap_set(bitmap, offset, nsectors);
2316 return;
2317 }
2318
2319 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2320 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2321}
2322
2323static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2324 u64 start, u64 len)
2325{
2326 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2327}
2328
2329static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2330 u64 start, u64 len)
2331{
2332 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2333}
2334
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002335static void scrub_block_complete(struct scrub_block *sblock)
2336{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002337 int corrupted = 0;
2338
Stefan Behrensff023aa2012-11-06 11:43:11 +01002339 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002340 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002341 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002342 } else {
2343 /*
2344 * if has checksum error, write via repair mechanism in
2345 * dev replace case, otherwise write here in dev replace
2346 * case.
2347 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002348 corrupted = scrub_checksum(sblock);
2349 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002350 scrub_write_block_to_dev_replace(sblock);
2351 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002352
2353 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2354 u64 start = sblock->pagev[0]->logical;
2355 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2356 PAGE_SIZE;
2357
2358 scrub_parity_mark_sectors_error(sblock->sparity,
2359 start, end - start);
2360 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002361}
2362
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002363static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002364 u8 *csum)
2365{
2366 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002367 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002368 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002369
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002370 while (!list_empty(&sctx->csum_list)) {
2371 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002372 struct btrfs_ordered_sum, list);
2373 if (sum->bytenr > logical)
2374 return 0;
2375 if (sum->bytenr + sum->len > logical)
2376 break;
2377
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002378 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002379 list_del(&sum->list);
2380 kfree(sum);
2381 sum = NULL;
2382 }
2383 if (!sum)
2384 return 0;
2385
Miao Xief51a4a12013-06-19 10:36:09 +08002386 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002387 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002388 memcpy(csum, sum->sums + index, sctx->csum_size);
2389 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002390 list_del(&sum->list);
2391 kfree(sum);
2392 }
Miao Xief51a4a12013-06-19 10:36:09 +08002393 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002394}
2395
2396/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002397static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002398 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002399 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002400{
2401 int ret;
2402 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002403 u32 blocksize;
2404
2405 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002406 blocksize = sctx->sectorsize;
2407 spin_lock(&sctx->stat_lock);
2408 sctx->stat.data_extents_scrubbed++;
2409 sctx->stat.data_bytes_scrubbed += len;
2410 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002411 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002412 blocksize = sctx->nodesize;
2413 spin_lock(&sctx->stat_lock);
2414 sctx->stat.tree_extents_scrubbed++;
2415 sctx->stat.tree_bytes_scrubbed += len;
2416 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002417 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002418 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002419 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002420 }
Arne Jansena2de7332011-03-08 14:14:00 +01002421
2422 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002423 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002424 int have_csum = 0;
2425
2426 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2427 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002428 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002429 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002430 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002431 if (sctx->is_dev_replace && !have_csum) {
2432 ret = copy_nocow_pages(sctx, logical, l,
2433 mirror_num,
2434 physical_for_dev_replace);
2435 goto behind_scrub_pages;
2436 }
Arne Jansena2de7332011-03-08 14:14:00 +01002437 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002438 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002439 mirror_num, have_csum ? csum : NULL, 0,
2440 physical_for_dev_replace);
2441behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002442 if (ret)
2443 return ret;
2444 len -= l;
2445 logical += l;
2446 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002447 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002448 }
2449 return 0;
2450}
2451
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002452static int scrub_pages_for_parity(struct scrub_parity *sparity,
2453 u64 logical, u64 len,
2454 u64 physical, struct btrfs_device *dev,
2455 u64 flags, u64 gen, int mirror_num, u8 *csum)
2456{
2457 struct scrub_ctx *sctx = sparity->sctx;
2458 struct scrub_block *sblock;
2459 int index;
2460
2461 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2462 if (!sblock) {
2463 spin_lock(&sctx->stat_lock);
2464 sctx->stat.malloc_errors++;
2465 spin_unlock(&sctx->stat_lock);
2466 return -ENOMEM;
2467 }
2468
2469 /* one ref inside this function, plus one for each page added to
2470 * a bio later on */
2471 atomic_set(&sblock->ref_count, 1);
2472 sblock->sctx = sctx;
2473 sblock->no_io_error_seen = 1;
2474 sblock->sparity = sparity;
2475 scrub_parity_get(sparity);
2476
2477 for (index = 0; len > 0; index++) {
2478 struct scrub_page *spage;
2479 u64 l = min_t(u64, len, PAGE_SIZE);
2480
2481 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2482 if (!spage) {
2483leave_nomem:
2484 spin_lock(&sctx->stat_lock);
2485 sctx->stat.malloc_errors++;
2486 spin_unlock(&sctx->stat_lock);
2487 scrub_block_put(sblock);
2488 return -ENOMEM;
2489 }
2490 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2491 /* For scrub block */
2492 scrub_page_get(spage);
2493 sblock->pagev[index] = spage;
2494 /* For scrub parity */
2495 scrub_page_get(spage);
2496 list_add_tail(&spage->list, &sparity->spages);
2497 spage->sblock = sblock;
2498 spage->dev = dev;
2499 spage->flags = flags;
2500 spage->generation = gen;
2501 spage->logical = logical;
2502 spage->physical = physical;
2503 spage->mirror_num = mirror_num;
2504 if (csum) {
2505 spage->have_csum = 1;
2506 memcpy(spage->csum, csum, sctx->csum_size);
2507 } else {
2508 spage->have_csum = 0;
2509 }
2510 sblock->page_count++;
2511 spage->page = alloc_page(GFP_NOFS);
2512 if (!spage->page)
2513 goto leave_nomem;
2514 len -= l;
2515 logical += l;
2516 physical += l;
2517 }
2518
2519 WARN_ON(sblock->page_count == 0);
2520 for (index = 0; index < sblock->page_count; index++) {
2521 struct scrub_page *spage = sblock->pagev[index];
2522 int ret;
2523
2524 ret = scrub_add_page_to_rd_bio(sctx, spage);
2525 if (ret) {
2526 scrub_block_put(sblock);
2527 return ret;
2528 }
2529 }
2530
2531 /* last one frees, either here or in bio completion for last page */
2532 scrub_block_put(sblock);
2533 return 0;
2534}
2535
2536static int scrub_extent_for_parity(struct scrub_parity *sparity,
2537 u64 logical, u64 len,
2538 u64 physical, struct btrfs_device *dev,
2539 u64 flags, u64 gen, int mirror_num)
2540{
2541 struct scrub_ctx *sctx = sparity->sctx;
2542 int ret;
2543 u8 csum[BTRFS_CSUM_SIZE];
2544 u32 blocksize;
2545
2546 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2547 blocksize = sctx->sectorsize;
2548 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2549 blocksize = sctx->nodesize;
2550 } else {
2551 blocksize = sctx->sectorsize;
2552 WARN_ON(1);
2553 }
2554
2555 while (len) {
2556 u64 l = min_t(u64, len, blocksize);
2557 int have_csum = 0;
2558
2559 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2560 /* push csums to sbio */
2561 have_csum = scrub_find_csum(sctx, logical, l, csum);
2562 if (have_csum == 0)
2563 goto skip;
2564 }
2565 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2566 flags, gen, mirror_num,
2567 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002568 if (ret)
2569 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002570skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002571 len -= l;
2572 logical += l;
2573 physical += l;
2574 }
2575 return 0;
2576}
2577
Wang Shilong3b080b22014-04-01 18:01:43 +08002578/*
2579 * Given a physical address, this will calculate it's
2580 * logical offset. if this is a parity stripe, it will return
2581 * the most left data stripe's logical offset.
2582 *
2583 * return 0 if it is a data stripe, 1 means parity stripe.
2584 */
2585static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002586 struct map_lookup *map, u64 *offset,
2587 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002588{
2589 int i;
2590 int j = 0;
2591 u64 stripe_nr;
2592 u64 last_offset;
2593 int stripe_index;
2594 int rot;
2595
2596 last_offset = (physical - map->stripes[num].physical) *
2597 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002598 if (stripe_start)
2599 *stripe_start = last_offset;
2600
Wang Shilong3b080b22014-04-01 18:01:43 +08002601 *offset = last_offset;
2602 for (i = 0; i < nr_data_stripes(map); i++) {
2603 *offset = last_offset + i * map->stripe_len;
2604
2605 stripe_nr = *offset;
2606 do_div(stripe_nr, map->stripe_len);
2607 do_div(stripe_nr, nr_data_stripes(map));
2608
2609 /* Work out the disk rotation on this stripe-set */
2610 rot = do_div(stripe_nr, map->num_stripes);
2611 /* calculate which stripe this data locates */
2612 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002613 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002614 if (stripe_index == num)
2615 return 0;
2616 if (stripe_index < num)
2617 j++;
2618 }
2619 *offset = last_offset + j * map->stripe_len;
2620 return 1;
2621}
2622
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002623static void scrub_free_parity(struct scrub_parity *sparity)
2624{
2625 struct scrub_ctx *sctx = sparity->sctx;
2626 struct scrub_page *curr, *next;
2627 int nbits;
2628
2629 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2630 if (nbits) {
2631 spin_lock(&sctx->stat_lock);
2632 sctx->stat.read_errors += nbits;
2633 sctx->stat.uncorrectable_errors += nbits;
2634 spin_unlock(&sctx->stat_lock);
2635 }
2636
2637 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2638 list_del_init(&curr->list);
2639 scrub_page_put(curr);
2640 }
2641
2642 kfree(sparity);
2643}
2644
2645static void scrub_parity_bio_endio(struct bio *bio, int error)
2646{
2647 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2648 struct scrub_ctx *sctx = sparity->sctx;
2649
2650 if (error)
2651 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2652 sparity->nsectors);
2653
2654 scrub_free_parity(sparity);
2655 scrub_pending_bio_dec(sctx);
2656 bio_put(bio);
2657}
2658
2659static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2660{
2661 struct scrub_ctx *sctx = sparity->sctx;
2662 struct bio *bio;
2663 struct btrfs_raid_bio *rbio;
2664 struct scrub_page *spage;
2665 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002666 u64 length;
2667 int ret;
2668
2669 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2670 sparity->nsectors))
2671 goto out;
2672
2673 length = sparity->logic_end - sparity->logic_start + 1;
Miao Xie76035972014-11-14 17:45:42 +08002674 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002675 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002676 &length, &bbio, 0, 1);
2677 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002678 goto bbio_out;
2679
2680 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2681 if (!bio)
2682 goto bbio_out;
2683
2684 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2685 bio->bi_private = sparity;
2686 bio->bi_end_io = scrub_parity_bio_endio;
2687
2688 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002689 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002690 sparity->dbitmap,
2691 sparity->nsectors);
2692 if (!rbio)
2693 goto rbio_out;
2694
2695 list_for_each_entry(spage, &sparity->spages, list)
2696 raid56_parity_add_scrub_pages(rbio, spage->page,
2697 spage->logical);
2698
2699 scrub_pending_bio_inc(sctx);
2700 raid56_parity_submit_scrub_rbio(rbio);
2701 return;
2702
2703rbio_out:
2704 bio_put(bio);
2705bbio_out:
Zhao Lei6e9606d2015-01-20 15:11:34 +08002706 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002707 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2708 sparity->nsectors);
2709 spin_lock(&sctx->stat_lock);
2710 sctx->stat.malloc_errors++;
2711 spin_unlock(&sctx->stat_lock);
2712out:
2713 scrub_free_parity(sparity);
2714}
2715
2716static inline int scrub_calc_parity_bitmap_len(int nsectors)
2717{
2718 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2719}
2720
2721static void scrub_parity_get(struct scrub_parity *sparity)
2722{
2723 atomic_inc(&sparity->ref_count);
2724}
2725
2726static void scrub_parity_put(struct scrub_parity *sparity)
2727{
2728 if (!atomic_dec_and_test(&sparity->ref_count))
2729 return;
2730
2731 scrub_parity_check_and_repair(sparity);
2732}
2733
2734static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2735 struct map_lookup *map,
2736 struct btrfs_device *sdev,
2737 struct btrfs_path *path,
2738 u64 logic_start,
2739 u64 logic_end)
2740{
2741 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2742 struct btrfs_root *root = fs_info->extent_root;
2743 struct btrfs_root *csum_root = fs_info->csum_root;
2744 struct btrfs_extent_item *extent;
2745 u64 flags;
2746 int ret;
2747 int slot;
2748 struct extent_buffer *l;
2749 struct btrfs_key key;
2750 u64 generation;
2751 u64 extent_logical;
2752 u64 extent_physical;
2753 u64 extent_len;
2754 struct btrfs_device *extent_dev;
2755 struct scrub_parity *sparity;
2756 int nsectors;
2757 int bitmap_len;
2758 int extent_mirror_num;
2759 int stop_loop = 0;
2760
2761 nsectors = map->stripe_len / root->sectorsize;
2762 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2763 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2764 GFP_NOFS);
2765 if (!sparity) {
2766 spin_lock(&sctx->stat_lock);
2767 sctx->stat.malloc_errors++;
2768 spin_unlock(&sctx->stat_lock);
2769 return -ENOMEM;
2770 }
2771
2772 sparity->stripe_len = map->stripe_len;
2773 sparity->nsectors = nsectors;
2774 sparity->sctx = sctx;
2775 sparity->scrub_dev = sdev;
2776 sparity->logic_start = logic_start;
2777 sparity->logic_end = logic_end;
2778 atomic_set(&sparity->ref_count, 1);
2779 INIT_LIST_HEAD(&sparity->spages);
2780 sparity->dbitmap = sparity->bitmap;
2781 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2782
2783 ret = 0;
2784 while (logic_start < logic_end) {
2785 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2786 key.type = BTRFS_METADATA_ITEM_KEY;
2787 else
2788 key.type = BTRFS_EXTENT_ITEM_KEY;
2789 key.objectid = logic_start;
2790 key.offset = (u64)-1;
2791
2792 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2793 if (ret < 0)
2794 goto out;
2795
2796 if (ret > 0) {
2797 ret = btrfs_previous_extent_item(root, path, 0);
2798 if (ret < 0)
2799 goto out;
2800 if (ret > 0) {
2801 btrfs_release_path(path);
2802 ret = btrfs_search_slot(NULL, root, &key,
2803 path, 0, 0);
2804 if (ret < 0)
2805 goto out;
2806 }
2807 }
2808
2809 stop_loop = 0;
2810 while (1) {
2811 u64 bytes;
2812
2813 l = path->nodes[0];
2814 slot = path->slots[0];
2815 if (slot >= btrfs_header_nritems(l)) {
2816 ret = btrfs_next_leaf(root, path);
2817 if (ret == 0)
2818 continue;
2819 if (ret < 0)
2820 goto out;
2821
2822 stop_loop = 1;
2823 break;
2824 }
2825 btrfs_item_key_to_cpu(l, &key, slot);
2826
2827 if (key.type == BTRFS_METADATA_ITEM_KEY)
2828 bytes = root->nodesize;
2829 else
2830 bytes = key.offset;
2831
2832 if (key.objectid + bytes <= logic_start)
2833 goto next;
2834
2835 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2836 key.type != BTRFS_METADATA_ITEM_KEY)
2837 goto next;
2838
2839 if (key.objectid > logic_end) {
2840 stop_loop = 1;
2841 break;
2842 }
2843
2844 while (key.objectid >= logic_start + map->stripe_len)
2845 logic_start += map->stripe_len;
2846
2847 extent = btrfs_item_ptr(l, slot,
2848 struct btrfs_extent_item);
2849 flags = btrfs_extent_flags(l, extent);
2850 generation = btrfs_extent_generation(l, extent);
2851
2852 if (key.objectid < logic_start &&
2853 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2854 btrfs_err(fs_info,
2855 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2856 key.objectid, logic_start);
2857 goto next;
2858 }
2859again:
2860 extent_logical = key.objectid;
2861 extent_len = bytes;
2862
2863 if (extent_logical < logic_start) {
2864 extent_len -= logic_start - extent_logical;
2865 extent_logical = logic_start;
2866 }
2867
2868 if (extent_logical + extent_len >
2869 logic_start + map->stripe_len)
2870 extent_len = logic_start + map->stripe_len -
2871 extent_logical;
2872
2873 scrub_parity_mark_sectors_data(sparity, extent_logical,
2874 extent_len);
2875
2876 scrub_remap_extent(fs_info, extent_logical,
2877 extent_len, &extent_physical,
2878 &extent_dev,
2879 &extent_mirror_num);
2880
2881 ret = btrfs_lookup_csums_range(csum_root,
2882 extent_logical,
2883 extent_logical + extent_len - 1,
2884 &sctx->csum_list, 1);
2885 if (ret)
2886 goto out;
2887
2888 ret = scrub_extent_for_parity(sparity, extent_logical,
2889 extent_len,
2890 extent_physical,
2891 extent_dev, flags,
2892 generation,
2893 extent_mirror_num);
2894 if (ret)
2895 goto out;
2896
2897 scrub_free_csums(sctx);
2898 if (extent_logical + extent_len <
2899 key.objectid + bytes) {
2900 logic_start += map->stripe_len;
2901
2902 if (logic_start >= logic_end) {
2903 stop_loop = 1;
2904 break;
2905 }
2906
2907 if (logic_start < key.objectid + bytes) {
2908 cond_resched();
2909 goto again;
2910 }
2911 }
2912next:
2913 path->slots[0]++;
2914 }
2915
2916 btrfs_release_path(path);
2917
2918 if (stop_loop)
2919 break;
2920
2921 logic_start += map->stripe_len;
2922 }
2923out:
2924 if (ret < 0)
2925 scrub_parity_mark_sectors_error(sparity, logic_start,
2926 logic_end - logic_start + 1);
2927 scrub_parity_put(sparity);
2928 scrub_submit(sctx);
2929 mutex_lock(&sctx->wr_ctx.wr_lock);
2930 scrub_wr_submit(sctx);
2931 mutex_unlock(&sctx->wr_ctx.wr_lock);
2932
2933 btrfs_release_path(path);
2934 return ret < 0 ? ret : 0;
2935}
2936
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002937static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002938 struct map_lookup *map,
2939 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002940 int num, u64 base, u64 length,
2941 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002942{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002943 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002944 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002945 struct btrfs_root *root = fs_info->extent_root;
2946 struct btrfs_root *csum_root = fs_info->csum_root;
2947 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002948 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002949 u64 flags;
2950 int ret;
2951 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002952 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002953 struct extent_buffer *l;
2954 struct btrfs_key key;
2955 u64 physical;
2956 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002957 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08002958 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002959 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002960 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002961 struct reada_control *reada1;
2962 struct reada_control *reada2;
2963 struct btrfs_key key_start;
2964 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002965 u64 increment = map->stripe_len;
2966 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002967 u64 extent_logical;
2968 u64 extent_physical;
2969 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002970 u64 stripe_logical;
2971 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002972 struct btrfs_device *extent_dev;
2973 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08002974 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002975
Arne Jansena2de7332011-03-08 14:14:00 +01002976 nstripes = length;
Wang Shilong3b080b22014-04-01 18:01:43 +08002977 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01002978 offset = 0;
2979 do_div(nstripes, map->stripe_len);
2980 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2981 offset = map->stripe_len * num;
2982 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02002983 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002984 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2985 int factor = map->num_stripes / map->sub_stripes;
2986 offset = map->stripe_len * (num / map->sub_stripes);
2987 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02002988 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002989 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2990 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002991 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002992 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2993 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002994 mirror_num = num % map->num_stripes + 1;
Wang Shilong3b080b22014-04-01 18:01:43 +08002995 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
2996 BTRFS_BLOCK_GROUP_RAID6)) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002997 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08002998 increment = map->stripe_len * nr_data_stripes(map);
2999 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003000 } else {
3001 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003002 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003003 }
3004
3005 path = btrfs_alloc_path();
3006 if (!path)
3007 return -ENOMEM;
3008
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003009 ppath = btrfs_alloc_path();
3010 if (!ppath) {
3011 btrfs_free_path(ppath);
3012 return -ENOMEM;
3013 }
3014
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003015 /*
3016 * work on commit root. The related disk blocks are static as
3017 * long as COW is applied. This means, it is save to rewrite
3018 * them to repair disk errors without any race conditions
3019 */
Arne Jansena2de7332011-03-08 14:14:00 +01003020 path->search_commit_root = 1;
3021 path->skip_locking = 1;
3022
3023 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003024 * trigger the readahead for extent tree csum tree and wait for
3025 * completion. During readahead, the scrub is officially paused
3026 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003027 */
3028 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003029 physical_end = physical + nstripes * map->stripe_len;
3030 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3031 BTRFS_BLOCK_GROUP_RAID6)) {
3032 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003033 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003034 logic_end += base;
3035 } else {
3036 logic_end = logical + increment * nstripes;
3037 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003038 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003039 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003040 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003041
Arne Jansen7a262852011-06-10 12:39:23 +02003042 /* FIXME it might be better to start readahead at commit root */
3043 key_start.objectid = logical;
3044 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3045 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003046 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003047 key_end.type = BTRFS_METADATA_ITEM_KEY;
3048 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003049 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003050
Arne Jansen7a262852011-06-10 12:39:23 +02003051 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3052 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3053 key_start.offset = logical;
3054 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3055 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003056 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003057 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003058
Arne Jansen7a262852011-06-10 12:39:23 +02003059 if (!IS_ERR(reada1))
3060 btrfs_reada_wait(reada1);
3061 if (!IS_ERR(reada2))
3062 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003063
Arne Jansena2de7332011-03-08 14:14:00 +01003064
3065 /*
3066 * collect all data csums for the stripe to avoid seeking during
3067 * the scrub. This might currently (crc32) end up to be about 1MB
3068 */
Arne Jansene7786c32011-05-28 20:58:38 +00003069 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003070
Arne Jansena2de7332011-03-08 14:14:00 +01003071 /*
3072 * now find all extents for each stripe and scrub them
3073 */
Arne Jansena2de7332011-03-08 14:14:00 +01003074 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003075 while (physical < physical_end) {
3076 /* for raid56, we skip parity stripe */
3077 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3078 BTRFS_BLOCK_GROUP_RAID6)) {
3079 ret = get_raid56_logic_offset(physical, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003080 map, &logical, &stripe_logical);
Wang Shilong3b080b22014-04-01 18:01:43 +08003081 logical += base;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003082 if (ret) {
3083 stripe_logical += base;
3084 stripe_end = stripe_logical + increment - 1;
3085 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3086 ppath, stripe_logical,
3087 stripe_end);
3088 if (ret)
3089 goto out;
Wang Shilong3b080b22014-04-01 18:01:43 +08003090 goto skip;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003091 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003092 }
Arne Jansena2de7332011-03-08 14:14:00 +01003093 /*
3094 * canceled?
3095 */
3096 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003097 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003098 ret = -ECANCELED;
3099 goto out;
3100 }
3101 /*
3102 * check to see if we have to pause
3103 */
3104 if (atomic_read(&fs_info->scrub_pause_req)) {
3105 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003106 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003107 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003108 mutex_lock(&sctx->wr_ctx.wr_lock);
3109 scrub_wr_submit(sctx);
3110 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003111 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003112 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003113 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003114 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003115 }
3116
Wang Shilong7c76edb2014-01-12 21:38:32 +08003117 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3118 key.type = BTRFS_METADATA_ITEM_KEY;
3119 else
3120 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003121 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003122 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003123
3124 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3125 if (ret < 0)
3126 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003127
Arne Jansen8c510322011-06-03 10:09:26 +02003128 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003129 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003130 if (ret < 0)
3131 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003132 if (ret > 0) {
3133 /* there's no smaller item, so stick with the
3134 * larger one */
3135 btrfs_release_path(path);
3136 ret = btrfs_search_slot(NULL, root, &key,
3137 path, 0, 0);
3138 if (ret < 0)
3139 goto out;
3140 }
Arne Jansena2de7332011-03-08 14:14:00 +01003141 }
3142
Liu Bo625f1c8d2013-04-27 02:56:57 +00003143 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003144 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003145 u64 bytes;
3146
Arne Jansena2de7332011-03-08 14:14:00 +01003147 l = path->nodes[0];
3148 slot = path->slots[0];
3149 if (slot >= btrfs_header_nritems(l)) {
3150 ret = btrfs_next_leaf(root, path);
3151 if (ret == 0)
3152 continue;
3153 if (ret < 0)
3154 goto out;
3155
Liu Bo625f1c8d2013-04-27 02:56:57 +00003156 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003157 break;
3158 }
3159 btrfs_item_key_to_cpu(l, &key, slot);
3160
Josef Bacik3173a182013-03-07 14:22:04 -05003161 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003162 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003163 else
3164 bytes = key.offset;
3165
3166 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003167 goto next;
3168
Liu Bo625f1c8d2013-04-27 02:56:57 +00003169 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3170 key.type != BTRFS_METADATA_ITEM_KEY)
3171 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01003172
Liu Bo625f1c8d2013-04-27 02:56:57 +00003173 if (key.objectid >= logical + map->stripe_len) {
3174 /* out of this device extent */
3175 if (key.objectid >= logic_end)
3176 stop_loop = 1;
3177 break;
3178 }
Arne Jansena2de7332011-03-08 14:14:00 +01003179
3180 extent = btrfs_item_ptr(l, slot,
3181 struct btrfs_extent_item);
3182 flags = btrfs_extent_flags(l, extent);
3183 generation = btrfs_extent_generation(l, extent);
3184
3185 if (key.objectid < logical &&
3186 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003187 btrfs_err(fs_info,
3188 "scrub: tree block %llu spanning "
3189 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003190 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01003191 goto next;
3192 }
3193
Liu Bo625f1c8d2013-04-27 02:56:57 +00003194again:
3195 extent_logical = key.objectid;
3196 extent_len = bytes;
3197
Arne Jansena2de7332011-03-08 14:14:00 +01003198 /*
3199 * trim extent to this stripe
3200 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003201 if (extent_logical < logical) {
3202 extent_len -= logical - extent_logical;
3203 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003204 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003205 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003206 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003207 extent_len = logical + map->stripe_len -
3208 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003209 }
3210
Liu Bo625f1c8d2013-04-27 02:56:57 +00003211 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003212 extent_dev = scrub_dev;
3213 extent_mirror_num = mirror_num;
3214 if (is_dev_replace)
3215 scrub_remap_extent(fs_info, extent_logical,
3216 extent_len, &extent_physical,
3217 &extent_dev,
3218 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003219
3220 ret = btrfs_lookup_csums_range(csum_root, logical,
3221 logical + map->stripe_len - 1,
3222 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003223 if (ret)
3224 goto out;
3225
Liu Bo625f1c8d2013-04-27 02:56:57 +00003226 ret = scrub_extent(sctx, extent_logical, extent_len,
3227 extent_physical, extent_dev, flags,
3228 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003229 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003230 if (ret)
3231 goto out;
3232
Josef Bacikd88d46c2013-06-10 12:59:04 +00003233 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003234 if (extent_logical + extent_len <
3235 key.objectid + bytes) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003236 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3237 BTRFS_BLOCK_GROUP_RAID6)) {
3238 /*
3239 * loop until we find next data stripe
3240 * or we have finished all stripes.
3241 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003242loop:
3243 physical += map->stripe_len;
3244 ret = get_raid56_logic_offset(physical,
3245 num, map, &logical,
3246 &stripe_logical);
3247 logical += base;
3248
3249 if (ret && physical < physical_end) {
3250 stripe_logical += base;
3251 stripe_end = stripe_logical +
3252 increment - 1;
3253 ret = scrub_raid56_parity(sctx,
3254 map, scrub_dev, ppath,
3255 stripe_logical,
3256 stripe_end);
3257 if (ret)
3258 goto out;
3259 goto loop;
3260 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003261 } else {
3262 physical += map->stripe_len;
3263 logical += increment;
3264 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003265 if (logical < key.objectid + bytes) {
3266 cond_resched();
3267 goto again;
3268 }
3269
Wang Shilong3b080b22014-04-01 18:01:43 +08003270 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003271 stop_loop = 1;
3272 break;
3273 }
3274 }
Arne Jansena2de7332011-03-08 14:14:00 +01003275next:
3276 path->slots[0]++;
3277 }
Chris Mason71267332011-05-23 06:30:52 -04003278 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003279skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003280 logical += increment;
3281 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003282 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003283 if (stop_loop)
3284 sctx->stat.last_physical = map->stripes[num].physical +
3285 length;
3286 else
3287 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003288 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003289 if (stop_loop)
3290 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003291 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003292out:
Arne Jansena2de7332011-03-08 14:14:00 +01003293 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003294 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003295 mutex_lock(&sctx->wr_ctx.wr_lock);
3296 scrub_wr_submit(sctx);
3297 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003298
Arne Jansene7786c32011-05-28 20:58:38 +00003299 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003300 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003301 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003302 return ret < 0 ? ret : 0;
3303}
3304
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003305static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003306 struct btrfs_device *scrub_dev,
3307 u64 chunk_tree, u64 chunk_objectid,
3308 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003309 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003310{
3311 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003312 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003313 struct map_lookup *map;
3314 struct extent_map *em;
3315 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003316 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003317
3318 read_lock(&map_tree->map_tree.lock);
3319 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3320 read_unlock(&map_tree->map_tree.lock);
3321
3322 if (!em)
3323 return -EINVAL;
3324
3325 map = (struct map_lookup *)em->bdev;
3326 if (em->start != chunk_offset)
3327 goto out;
3328
3329 if (em->len < length)
3330 goto out;
3331
3332 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003333 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003334 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003335 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003336 chunk_offset, length,
3337 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003338 if (ret)
3339 goto out;
3340 }
3341 }
3342out:
3343 free_extent_map(em);
3344
3345 return ret;
3346}
3347
3348static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003349int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003350 struct btrfs_device *scrub_dev, u64 start, u64 end,
3351 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003352{
3353 struct btrfs_dev_extent *dev_extent = NULL;
3354 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003355 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003356 struct btrfs_fs_info *fs_info = root->fs_info;
3357 u64 length;
3358 u64 chunk_tree;
3359 u64 chunk_objectid;
3360 u64 chunk_offset;
3361 int ret;
3362 int slot;
3363 struct extent_buffer *l;
3364 struct btrfs_key key;
3365 struct btrfs_key found_key;
3366 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003367 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003368
3369 path = btrfs_alloc_path();
3370 if (!path)
3371 return -ENOMEM;
3372
3373 path->reada = 2;
3374 path->search_commit_root = 1;
3375 path->skip_locking = 1;
3376
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003377 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003378 key.offset = 0ull;
3379 key.type = BTRFS_DEV_EXTENT_KEY;
3380
Arne Jansena2de7332011-03-08 14:14:00 +01003381 while (1) {
3382 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3383 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003384 break;
3385 if (ret > 0) {
3386 if (path->slots[0] >=
3387 btrfs_header_nritems(path->nodes[0])) {
3388 ret = btrfs_next_leaf(root, path);
3389 if (ret)
3390 break;
3391 }
3392 }
Arne Jansena2de7332011-03-08 14:14:00 +01003393
3394 l = path->nodes[0];
3395 slot = path->slots[0];
3396
3397 btrfs_item_key_to_cpu(l, &found_key, slot);
3398
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003399 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003400 break;
3401
David Sterba962a2982014-06-04 18:41:45 +02003402 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003403 break;
3404
3405 if (found_key.offset >= end)
3406 break;
3407
3408 if (found_key.offset < key.offset)
3409 break;
3410
3411 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3412 length = btrfs_dev_extent_length(l, dev_extent);
3413
Qu Wenruoced96ed2014-06-19 10:42:51 +08003414 if (found_key.offset + length <= start)
3415 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003416
3417 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3418 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3419 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3420
3421 /*
3422 * get a reference on the corresponding block group to prevent
3423 * the chunk from going away while we scrub it
3424 */
3425 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003426
3427 /* some chunks are removed but not committed to disk yet,
3428 * continue scrubbing */
3429 if (!cache)
3430 goto skip;
3431
Stefan Behrensff023aa2012-11-06 11:43:11 +01003432 dev_replace->cursor_right = found_key.offset + length;
3433 dev_replace->cursor_left = found_key.offset;
3434 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003435 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003436 chunk_offset, length, found_key.offset,
3437 is_dev_replace);
3438
3439 /*
3440 * flush, submit all pending read and write bios, afterwards
3441 * wait for them.
3442 * Note that in the dev replace case, a read request causes
3443 * write requests that are submitted in the read completion
3444 * worker. Therefore in the current situation, it is required
3445 * that all write requests are flushed, so that all read and
3446 * write requests are really completed when bios_in_flight
3447 * changes to 0.
3448 */
3449 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3450 scrub_submit(sctx);
3451 mutex_lock(&sctx->wr_ctx.wr_lock);
3452 scrub_wr_submit(sctx);
3453 mutex_unlock(&sctx->wr_ctx.wr_lock);
3454
3455 wait_event(sctx->list_wait,
3456 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003457 atomic_inc(&fs_info->scrubs_paused);
3458 wake_up(&fs_info->scrub_pause_wait);
3459
3460 /*
3461 * must be called before we decrease @scrub_paused.
3462 * make sure we don't block transaction commit while
3463 * we are waiting pending workers finished.
3464 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003465 wait_event(sctx->list_wait,
3466 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003467 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3468
3469 mutex_lock(&fs_info->scrub_lock);
3470 __scrub_blocked_if_needed(fs_info);
3471 atomic_dec(&fs_info->scrubs_paused);
3472 mutex_unlock(&fs_info->scrub_lock);
3473 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003474
Arne Jansena2de7332011-03-08 14:14:00 +01003475 btrfs_put_block_group(cache);
3476 if (ret)
3477 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003478 if (is_dev_replace &&
3479 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003480 ret = -EIO;
3481 break;
3482 }
3483 if (sctx->stat.malloc_errors > 0) {
3484 ret = -ENOMEM;
3485 break;
3486 }
Arne Jansena2de7332011-03-08 14:14:00 +01003487
Ilya Dryomov539f3582013-10-07 13:42:57 +03003488 dev_replace->cursor_left = dev_replace->cursor_right;
3489 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003490skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003491 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003492 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003493 }
3494
Arne Jansena2de7332011-03-08 14:14:00 +01003495 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003496
3497 /*
3498 * ret can still be 1 from search_slot or next_leaf,
3499 * that's not an error
3500 */
3501 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003502}
3503
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003504static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3505 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003506{
3507 int i;
3508 u64 bytenr;
3509 u64 gen;
3510 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003511 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003512
Miao Xie87533c42013-01-29 10:14:48 +00003513 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003514 return -EIO;
3515
Miao Xie5f546062014-07-24 11:37:09 +08003516 /* Seed devices of a new filesystem has their own generation. */
3517 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3518 gen = scrub_dev->generation;
3519 else
3520 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003521
3522 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3523 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003524 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3525 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003526 break;
3527
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003528 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003529 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003530 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003531 if (ret)
3532 return ret;
3533 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003534 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003535
3536 return 0;
3537}
3538
3539/*
3540 * get a reference count on fs_info->scrub_workers. start worker if necessary
3541 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003542static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3543 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003544{
Josef Bacik0dc3b842011-11-18 14:37:27 -05003545 int ret = 0;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003546 int flags = WQ_FREEZABLE | WQ_UNBOUND;
3547 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003548
Arne Jansen632dd772011-06-10 12:07:07 +02003549 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003550 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003551 fs_info->scrub_workers =
3552 btrfs_alloc_workqueue("btrfs-scrub", flags,
3553 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003554 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003555 fs_info->scrub_workers =
3556 btrfs_alloc_workqueue("btrfs-scrub", flags,
3557 max_active, 4);
3558 if (!fs_info->scrub_workers) {
3559 ret = -ENOMEM;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003560 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003561 }
3562 fs_info->scrub_wr_completion_workers =
3563 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3564 max_active, 2);
3565 if (!fs_info->scrub_wr_completion_workers) {
3566 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003567 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003568 }
3569 fs_info->scrub_nocow_workers =
3570 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
3571 if (!fs_info->scrub_nocow_workers) {
3572 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003573 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003574 }
Arne Jansen632dd772011-06-10 12:07:07 +02003575 }
Arne Jansena2de7332011-03-08 14:14:00 +01003576 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003577out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05003578 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003579}
3580
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003581static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003582{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003583 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003584 btrfs_destroy_workqueue(fs_info->scrub_workers);
3585 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3586 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003587 }
Arne Jansena2de7332011-03-08 14:14:00 +01003588 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003589}
3590
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003591int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3592 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003593 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003594{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003595 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003596 int ret;
3597 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003598 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003599
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003600 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003601 return -EINVAL;
3602
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003603 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003604 /*
3605 * in this case scrub is unable to calculate the checksum
3606 * the way scrub is implemented. Do not handle this
3607 * situation at all because it won't ever happen.
3608 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003609 btrfs_err(fs_info,
3610 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003611 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003612 return -EINVAL;
3613 }
3614
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003615 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003616 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003617 btrfs_err(fs_info,
3618 "scrub: size assumption sectorsize != PAGE_SIZE "
3619 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003620 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003621 return -EINVAL;
3622 }
3623
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003624 if (fs_info->chunk_root->nodesize >
3625 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3626 fs_info->chunk_root->sectorsize >
3627 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3628 /*
3629 * would exhaust the array bounds of pagev member in
3630 * struct scrub_block
3631 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003632 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3633 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003634 fs_info->chunk_root->nodesize,
3635 SCRUB_MAX_PAGES_PER_BLOCK,
3636 fs_info->chunk_root->sectorsize,
3637 SCRUB_MAX_PAGES_PER_BLOCK);
3638 return -EINVAL;
3639 }
3640
Arne Jansena2de7332011-03-08 14:14:00 +01003641
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003642 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3643 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003644 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003645 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003646 return -ENODEV;
3647 }
Arne Jansena2de7332011-03-08 14:14:00 +01003648
Miao Xie5d68da32014-07-24 11:37:07 +08003649 if (!is_dev_replace && !readonly && !dev->writeable) {
3650 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3651 rcu_read_lock();
3652 name = rcu_dereference(dev->name);
3653 btrfs_err(fs_info, "scrub: device %s is not writable",
3654 name->str);
3655 rcu_read_unlock();
3656 return -EROFS;
3657 }
3658
Wang Shilong3b7a0162013-10-12 02:11:12 +08003659 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003660 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003661 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003662 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003663 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003664 }
3665
Stefan Behrens8dabb742012-11-06 13:15:27 +01003666 btrfs_dev_replace_lock(&fs_info->dev_replace);
3667 if (dev->scrub_device ||
3668 (!is_dev_replace &&
3669 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3670 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003671 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003672 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003673 return -EINPROGRESS;
3674 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003675 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003676
3677 ret = scrub_workers_get(fs_info, is_dev_replace);
3678 if (ret) {
3679 mutex_unlock(&fs_info->scrub_lock);
3680 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3681 return ret;
3682 }
3683
Stefan Behrens63a212a2012-11-05 18:29:28 +01003684 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003685 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003686 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003687 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3688 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003689 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003690 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003691 sctx->readonly = readonly;
3692 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003693 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003694
Wang Shilong3cb09292013-12-04 21:15:19 +08003695 /*
3696 * checking @scrub_pause_req here, we can avoid
3697 * race between committing transaction and scrubbing.
3698 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003699 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003700 atomic_inc(&fs_info->scrubs_running);
3701 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003702
Stefan Behrensff023aa2012-11-06 11:43:11 +01003703 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003704 /*
3705 * by holding device list mutex, we can
3706 * kick off writing super in log tree sync.
3707 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003708 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003709 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003710 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003711 }
Arne Jansena2de7332011-03-08 14:14:00 +01003712
3713 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003714 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3715 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003716
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003717 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003718 atomic_dec(&fs_info->scrubs_running);
3719 wake_up(&fs_info->scrub_pause_wait);
3720
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003721 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003722
Arne Jansena2de7332011-03-08 14:14:00 +01003723 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003724 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003725
3726 mutex_lock(&fs_info->scrub_lock);
3727 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003728 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003729 mutex_unlock(&fs_info->scrub_lock);
3730
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003731 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003732
3733 return ret;
3734}
3735
Jeff Mahoney143bede2012-03-01 14:56:26 +01003736void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003737{
3738 struct btrfs_fs_info *fs_info = root->fs_info;
3739
3740 mutex_lock(&fs_info->scrub_lock);
3741 atomic_inc(&fs_info->scrub_pause_req);
3742 while (atomic_read(&fs_info->scrubs_paused) !=
3743 atomic_read(&fs_info->scrubs_running)) {
3744 mutex_unlock(&fs_info->scrub_lock);
3745 wait_event(fs_info->scrub_pause_wait,
3746 atomic_read(&fs_info->scrubs_paused) ==
3747 atomic_read(&fs_info->scrubs_running));
3748 mutex_lock(&fs_info->scrub_lock);
3749 }
3750 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003751}
3752
Jeff Mahoney143bede2012-03-01 14:56:26 +01003753void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003754{
3755 struct btrfs_fs_info *fs_info = root->fs_info;
3756
3757 atomic_dec(&fs_info->scrub_pause_req);
3758 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003759}
3760
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003761int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003762{
Arne Jansena2de7332011-03-08 14:14:00 +01003763 mutex_lock(&fs_info->scrub_lock);
3764 if (!atomic_read(&fs_info->scrubs_running)) {
3765 mutex_unlock(&fs_info->scrub_lock);
3766 return -ENOTCONN;
3767 }
3768
3769 atomic_inc(&fs_info->scrub_cancel_req);
3770 while (atomic_read(&fs_info->scrubs_running)) {
3771 mutex_unlock(&fs_info->scrub_lock);
3772 wait_event(fs_info->scrub_pause_wait,
3773 atomic_read(&fs_info->scrubs_running) == 0);
3774 mutex_lock(&fs_info->scrub_lock);
3775 }
3776 atomic_dec(&fs_info->scrub_cancel_req);
3777 mutex_unlock(&fs_info->scrub_lock);
3778
3779 return 0;
3780}
3781
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003782int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3783 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003784{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003785 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003786
3787 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003788 sctx = dev->scrub_device;
3789 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003790 mutex_unlock(&fs_info->scrub_lock);
3791 return -ENOTCONN;
3792 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003793 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003794 while (dev->scrub_device) {
3795 mutex_unlock(&fs_info->scrub_lock);
3796 wait_event(fs_info->scrub_pause_wait,
3797 dev->scrub_device == NULL);
3798 mutex_lock(&fs_info->scrub_lock);
3799 }
3800 mutex_unlock(&fs_info->scrub_lock);
3801
3802 return 0;
3803}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003804
Arne Jansena2de7332011-03-08 14:14:00 +01003805int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3806 struct btrfs_scrub_progress *progress)
3807{
3808 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003809 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003810
3811 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003812 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003813 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003814 sctx = dev->scrub_device;
3815 if (sctx)
3816 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003817 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3818
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003819 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003820}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003821
3822static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3823 u64 extent_logical, u64 extent_len,
3824 u64 *extent_physical,
3825 struct btrfs_device **extent_dev,
3826 int *extent_mirror_num)
3827{
3828 u64 mapped_length;
3829 struct btrfs_bio *bbio = NULL;
3830 int ret;
3831
3832 mapped_length = extent_len;
3833 ret = btrfs_map_block(fs_info, READ, extent_logical,
3834 &mapped_length, &bbio, 0);
3835 if (ret || !bbio || mapped_length < extent_len ||
3836 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08003837 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003838 return;
3839 }
3840
3841 *extent_physical = bbio->stripes[0].physical;
3842 *extent_mirror_num = bbio->mirror_num;
3843 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08003844 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003845}
3846
3847static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3848 struct scrub_wr_ctx *wr_ctx,
3849 struct btrfs_fs_info *fs_info,
3850 struct btrfs_device *dev,
3851 int is_dev_replace)
3852{
3853 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3854
3855 mutex_init(&wr_ctx->wr_lock);
3856 wr_ctx->wr_curr_bio = NULL;
3857 if (!is_dev_replace)
3858 return 0;
3859
3860 WARN_ON(!dev->bdev);
3861 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3862 bio_get_nr_vecs(dev->bdev));
3863 wr_ctx->tgtdev = dev;
3864 atomic_set(&wr_ctx->flush_all_writes, 0);
3865 return 0;
3866}
3867
3868static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3869{
3870 mutex_lock(&wr_ctx->wr_lock);
3871 kfree(wr_ctx->wr_curr_bio);
3872 wr_ctx->wr_curr_bio = NULL;
3873 mutex_unlock(&wr_ctx->wr_lock);
3874}
3875
3876static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3877 int mirror_num, u64 physical_for_dev_replace)
3878{
3879 struct scrub_copy_nocow_ctx *nocow_ctx;
3880 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3881
3882 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3883 if (!nocow_ctx) {
3884 spin_lock(&sctx->stat_lock);
3885 sctx->stat.malloc_errors++;
3886 spin_unlock(&sctx->stat_lock);
3887 return -ENOMEM;
3888 }
3889
3890 scrub_pending_trans_workers_inc(sctx);
3891
3892 nocow_ctx->sctx = sctx;
3893 nocow_ctx->logical = logical;
3894 nocow_ctx->len = len;
3895 nocow_ctx->mirror_num = mirror_num;
3896 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08003897 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3898 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003899 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003900 btrfs_queue_work(fs_info->scrub_nocow_workers,
3901 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003902
3903 return 0;
3904}
3905
Josef Bacik652f25a2013-09-12 16:58:28 -04003906static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3907{
3908 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3909 struct scrub_nocow_inode *nocow_inode;
3910
3911 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3912 if (!nocow_inode)
3913 return -ENOMEM;
3914 nocow_inode->inum = inum;
3915 nocow_inode->offset = offset;
3916 nocow_inode->root = root;
3917 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3918 return 0;
3919}
3920
3921#define COPY_COMPLETE 1
3922
Stefan Behrensff023aa2012-11-06 11:43:11 +01003923static void copy_nocow_pages_worker(struct btrfs_work *work)
3924{
3925 struct scrub_copy_nocow_ctx *nocow_ctx =
3926 container_of(work, struct scrub_copy_nocow_ctx, work);
3927 struct scrub_ctx *sctx = nocow_ctx->sctx;
3928 u64 logical = nocow_ctx->logical;
3929 u64 len = nocow_ctx->len;
3930 int mirror_num = nocow_ctx->mirror_num;
3931 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3932 int ret;
3933 struct btrfs_trans_handle *trans = NULL;
3934 struct btrfs_fs_info *fs_info;
3935 struct btrfs_path *path;
3936 struct btrfs_root *root;
3937 int not_written = 0;
3938
3939 fs_info = sctx->dev_root->fs_info;
3940 root = fs_info->extent_root;
3941
3942 path = btrfs_alloc_path();
3943 if (!path) {
3944 spin_lock(&sctx->stat_lock);
3945 sctx->stat.malloc_errors++;
3946 spin_unlock(&sctx->stat_lock);
3947 not_written = 1;
3948 goto out;
3949 }
3950
3951 trans = btrfs_join_transaction(root);
3952 if (IS_ERR(trans)) {
3953 not_written = 1;
3954 goto out;
3955 }
3956
3957 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04003958 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003959 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003960 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
3961 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02003962 logical, physical_for_dev_replace, len, mirror_num,
3963 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003964 not_written = 1;
3965 goto out;
3966 }
3967
Josef Bacik652f25a2013-09-12 16:58:28 -04003968 btrfs_end_transaction(trans, root);
3969 trans = NULL;
3970 while (!list_empty(&nocow_ctx->inodes)) {
3971 struct scrub_nocow_inode *entry;
3972 entry = list_first_entry(&nocow_ctx->inodes,
3973 struct scrub_nocow_inode,
3974 list);
3975 list_del_init(&entry->list);
3976 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
3977 entry->root, nocow_ctx);
3978 kfree(entry);
3979 if (ret == COPY_COMPLETE) {
3980 ret = 0;
3981 break;
3982 } else if (ret) {
3983 break;
3984 }
3985 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003986out:
Josef Bacik652f25a2013-09-12 16:58:28 -04003987 while (!list_empty(&nocow_ctx->inodes)) {
3988 struct scrub_nocow_inode *entry;
3989 entry = list_first_entry(&nocow_ctx->inodes,
3990 struct scrub_nocow_inode,
3991 list);
3992 list_del_init(&entry->list);
3993 kfree(entry);
3994 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003995 if (trans && !IS_ERR(trans))
3996 btrfs_end_transaction(trans, root);
3997 if (not_written)
3998 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
3999 num_uncorrectable_read_errors);
4000
4001 btrfs_free_path(path);
4002 kfree(nocow_ctx);
4003
4004 scrub_pending_trans_workers_dec(sctx);
4005}
4006
Gui Hecheng32159242014-11-10 15:36:08 +08004007static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4008 u64 logical)
4009{
4010 struct extent_state *cached_state = NULL;
4011 struct btrfs_ordered_extent *ordered;
4012 struct extent_io_tree *io_tree;
4013 struct extent_map *em;
4014 u64 lockstart = start, lockend = start + len - 1;
4015 int ret = 0;
4016
4017 io_tree = &BTRFS_I(inode)->io_tree;
4018
4019 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4020 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4021 if (ordered) {
4022 btrfs_put_ordered_extent(ordered);
4023 ret = 1;
4024 goto out_unlock;
4025 }
4026
4027 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4028 if (IS_ERR(em)) {
4029 ret = PTR_ERR(em);
4030 goto out_unlock;
4031 }
4032
4033 /*
4034 * This extent does not actually cover the logical extent anymore,
4035 * move on to the next inode.
4036 */
4037 if (em->block_start > logical ||
4038 em->block_start + em->block_len < logical + len) {
4039 free_extent_map(em);
4040 ret = 1;
4041 goto out_unlock;
4042 }
4043 free_extent_map(em);
4044
4045out_unlock:
4046 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4047 GFP_NOFS);
4048 return ret;
4049}
4050
Josef Bacik652f25a2013-09-12 16:58:28 -04004051static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4052 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004053{
Miao Xie826aa0a2013-06-27 18:50:59 +08004054 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004055 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004056 struct inode *inode;
4057 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004058 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004059 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004060 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004061 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004062 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004063 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004064 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004065 int ret = 0;
4066 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004067
4068 key.objectid = root;
4069 key.type = BTRFS_ROOT_ITEM_KEY;
4070 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004071
4072 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4073
Stefan Behrensff023aa2012-11-06 11:43:11 +01004074 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004075 if (IS_ERR(local_root)) {
4076 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004077 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004078 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004079
4080 key.type = BTRFS_INODE_ITEM_KEY;
4081 key.objectid = inum;
4082 key.offset = 0;
4083 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004084 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004085 if (IS_ERR(inode))
4086 return PTR_ERR(inode);
4087
Miao Xieedd14002013-06-27 18:51:00 +08004088 /* Avoid truncate/dio/punch hole.. */
4089 mutex_lock(&inode->i_mutex);
4090 inode_dio_wait(inode);
4091
Stefan Behrensff023aa2012-11-06 11:43:11 +01004092 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004093 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004094 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004095
Gui Hecheng32159242014-11-10 15:36:08 +08004096 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4097 if (ret) {
4098 ret = ret > 0 ? 0 : ret;
4099 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004100 }
4101
Stefan Behrensff023aa2012-11-06 11:43:11 +01004102 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004103 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004104again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004105 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4106 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004107 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004108 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004109 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004110 }
4111
4112 if (PageUptodate(page)) {
4113 if (PageDirty(page))
4114 goto next_page;
4115 } else {
4116 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004117 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004118 btrfs_get_extent,
4119 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004120 if (err) {
4121 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004122 goto next_page;
4123 }
Miao Xieedd14002013-06-27 18:51:00 +08004124
Miao Xie26b258912013-06-27 18:50:58 +08004125 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004126 /*
4127 * If the page has been remove from the page cache,
4128 * the data on it is meaningless, because it may be
4129 * old one, the new data may be written into the new
4130 * page in the page cache.
4131 */
4132 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004133 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004134 page_cache_release(page);
4135 goto again;
4136 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004137 if (!PageUptodate(page)) {
4138 ret = -EIO;
4139 goto next_page;
4140 }
4141 }
Gui Hecheng32159242014-11-10 15:36:08 +08004142
4143 ret = check_extent_to_block(inode, offset, len,
4144 nocow_ctx_logical);
4145 if (ret) {
4146 ret = ret > 0 ? 0 : ret;
4147 goto next_page;
4148 }
4149
Miao Xie826aa0a2013-06-27 18:50:59 +08004150 err = write_page_nocow(nocow_ctx->sctx,
4151 physical_for_dev_replace, page);
4152 if (err)
4153 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004154next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004155 unlock_page(page);
4156 page_cache_release(page);
4157
4158 if (ret)
4159 break;
4160
Stefan Behrensff023aa2012-11-06 11:43:11 +01004161 offset += PAGE_CACHE_SIZE;
4162 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004163 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004164 len -= PAGE_CACHE_SIZE;
4165 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004166 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004167out:
Miao Xieedd14002013-06-27 18:51:00 +08004168 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004169 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004170 return ret;
4171}
4172
4173static int write_page_nocow(struct scrub_ctx *sctx,
4174 u64 physical_for_dev_replace, struct page *page)
4175{
4176 struct bio *bio;
4177 struct btrfs_device *dev;
4178 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004179
4180 dev = sctx->wr_ctx.tgtdev;
4181 if (!dev)
4182 return -EIO;
4183 if (!dev->bdev) {
4184 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05004185 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004186 return -EIO;
4187 }
Chris Mason9be33952013-05-17 18:30:14 -04004188 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004189 if (!bio) {
4190 spin_lock(&sctx->stat_lock);
4191 sctx->stat.malloc_errors++;
4192 spin_unlock(&sctx->stat_lock);
4193 return -ENOMEM;
4194 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004195 bio->bi_iter.bi_size = 0;
4196 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004197 bio->bi_bdev = dev->bdev;
4198 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4199 if (ret != PAGE_CACHE_SIZE) {
4200leave_with_eio:
4201 bio_put(bio);
4202 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4203 return -EIO;
4204 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004205
Kent Overstreet33879d42013-11-23 22:33:32 -08004206 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004207 goto leave_with_eio;
4208
4209 bio_put(bio);
4210 return 0;
4211}