blob: 3be1456b5116b3e80dca1f466f374c4eb46a28cd [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Arne Jansena2de7332011-03-08 14:14:00 +01002/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
Arne Jansena2de7332011-03-08 14:14:00 +01004 */
5
Arne Jansena2de7332011-03-08 14:14:00 +01006#include <linux/blkdev.h>
Jan Schmidt558540c2011-06-13 19:59:12 +02007#include <linux/ratelimit.h>
David Sterbade2491f2017-05-31 19:21:38 +02008#include <linux/sched/mm.h>
Arne Jansena2de7332011-03-08 14:14:00 +01009#include "ctree.h"
10#include "volumes.h"
11#include "disk-io.h"
12#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020013#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020014#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020015#include "extent_io.h"
Stefan Behrensff023aa2012-11-06 11:43:11 +010016#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010017#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040018#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050019#include "raid56.h"
Arne Jansena2de7332011-03-08 14:14:00 +010020
21/*
22 * This is only the first step towards a full-features scrub. It reads all
23 * extent and super block and verifies the checksums. In case a bad checksum
24 * is found or the extent cannot be read, good data will be written back if
25 * any can be found.
26 *
27 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010028 * - In case an unrepairable extent is encountered, track which files are
29 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010030 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010031 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010032 */
33
Stefan Behrensb5d67f62012-03-27 14:21:27 -040034struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010035struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010036
Stefan Behrensff023aa2012-11-06 11:43:11 +010037/*
38 * the following three values only influence the performance.
39 * The last one configures the number of parallel and outstanding I/O
40 * operations. The first two values configure an upper limit for the number
41 * of (dynamically allocated) pages that are added to a bio.
42 */
43#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
44#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
45#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010046
47/*
48 * the following value times PAGE_SIZE needs to be large enough to match the
49 * largest node/leaf/sector size that shall be supported.
50 * Values larger than BTRFS_STRIPE_LEN are not supported.
51 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040052#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010053
Miao Xieaf8e2d12014-10-23 14:42:50 +080054struct scrub_recover {
Elena Reshetova6f615012017-03-03 10:55:21 +020055 refcount_t refs;
Miao Xieaf8e2d12014-10-23 14:42:50 +080056 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +080057 u64 map_length;
58};
59
Arne Jansena2de7332011-03-08 14:14:00 +010060struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040061 struct scrub_block *sblock;
62 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020063 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080064 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010065 u64 flags; /* extent flags */
66 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040067 u64 logical;
68 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010069 u64 physical_for_dev_replace;
Zhao Lei57019342015-01-20 15:11:45 +080070 atomic_t refs;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040071 struct {
72 unsigned int mirror_num:8;
73 unsigned int have_csum:1;
74 unsigned int io_error:1;
75 };
Arne Jansena2de7332011-03-08 14:14:00 +010076 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080077
78 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010079};
80
81struct scrub_bio {
82 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010083 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010084 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010085 struct bio *bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020086 blk_status_t status;
Arne Jansena2de7332011-03-08 14:14:00 +010087 u64 logical;
88 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010089#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
90 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
91#else
92 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
93#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -040094 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +010095 int next_free;
96 struct btrfs_work work;
97};
98
Stefan Behrensb5d67f62012-03-27 14:21:27 -040099struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100100 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400101 int page_count;
102 atomic_t outstanding_pages;
Elena Reshetova186debd2017-03-03 10:55:23 +0200103 refcount_t refs; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100104 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800105 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400106 struct {
107 unsigned int header_error:1;
108 unsigned int checksum_error:1;
109 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200110 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800111
112 /* The following is for the data used to check parity */
113 /* It is for the data with checksum */
114 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400115 };
Omar Sandoval73ff61d2015-06-19 11:52:51 -0700116 struct btrfs_work work;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400117};
118
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800119/* Used for the chunks with parity stripe such RAID5/6 */
120struct scrub_parity {
121 struct scrub_ctx *sctx;
122
123 struct btrfs_device *scrub_dev;
124
125 u64 logic_start;
126
127 u64 logic_end;
128
129 int nsectors;
130
Liu Bo972d7212017-04-03 13:45:33 -0700131 u64 stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800132
Elena Reshetova78a76452017-03-03 10:55:24 +0200133 refcount_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800134
135 struct list_head spages;
136
137 /* Work of parity check and repair */
138 struct btrfs_work work;
139
140 /* Mark the parity blocks which have data */
141 unsigned long *dbitmap;
142
143 /*
144 * Mark the parity blocks which have data, but errors happen when
145 * read data or check data
146 */
147 unsigned long *ebitmap;
148
149 unsigned long bitmap[0];
150};
151
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100152struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100153 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400154 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100155 int first_free;
156 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100157 atomic_t bios_in_flight;
158 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100159 spinlock_t list_lock;
160 wait_queue_head_t list_wait;
161 u16 csum_size;
162 struct list_head csum_list;
163 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100164 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100165 int pages_per_rd_bio;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100166
167 int is_dev_replace;
David Sterba3fb99302017-05-16 19:10:32 +0200168
169 struct scrub_bio *wr_curr_bio;
170 struct mutex wr_lock;
171 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
David Sterba3fb99302017-05-16 19:10:32 +0200172 struct btrfs_device *wr_tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200173 bool flush_all_writes;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100174
Arne Jansena2de7332011-03-08 14:14:00 +0100175 /*
176 * statistics
177 */
178 struct btrfs_scrub_progress stat;
179 spinlock_t stat_lock;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000180
181 /*
182 * Use a ref counter to avoid use-after-free issues. Scrub workers
183 * decrement bios_in_flight and workers_pending and then do a wakeup
184 * on the list_wait wait queue. We must ensure the main scrub task
185 * doesn't free the scrub context before or while the workers are
186 * doing the wakeup() call.
187 */
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200188 refcount_t refs;
Arne Jansena2de7332011-03-08 14:14:00 +0100189};
190
Jan Schmidt558540c2011-06-13 19:59:12 +0200191struct scrub_warning {
192 struct btrfs_path *path;
193 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200194 const char *errstr;
David Sterba6aa21262017-10-04 17:07:07 +0200195 u64 physical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200196 u64 logical;
197 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200198};
199
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800200struct full_stripe_lock {
201 struct rb_node node;
202 u64 logical;
203 u64 refs;
204 struct mutex mutex;
205};
206
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100207static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
208static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400209static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Zhao Leibe50a8d2015-01-20 15:11:42 +0800210static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100211 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100212static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +0800213 struct scrub_block *sblock,
214 int retry_failed_mirror);
Zhao Leiba7cf982015-08-24 21:18:02 +0800215static void scrub_recheck_block_checksum(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400216static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800217 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400218static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
219 struct scrub_block *sblock_good,
220 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100221static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
222static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
223 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400224static int scrub_checksum_data(struct scrub_block *sblock);
225static int scrub_checksum_tree_block(struct scrub_block *sblock);
226static int scrub_checksum_super(struct scrub_block *sblock);
227static void scrub_block_get(struct scrub_block *sblock);
228static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100229static void scrub_page_get(struct scrub_page *spage);
230static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800231static void scrub_parity_get(struct scrub_parity *sparity);
232static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100233static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
234 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100235static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100236 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100237 u64 gen, int mirror_num, u8 *csum, int force,
238 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200239static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400240static void scrub_bio_end_io_worker(struct btrfs_work *work);
241static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100242static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
243 u64 extent_logical, u64 extent_len,
244 u64 *extent_physical,
245 struct btrfs_device **extent_dev,
246 int *extent_mirror_num);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100247static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
248 struct scrub_page *spage);
249static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200250static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100251static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800252static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800253static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000254static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400255
Liu Bo762221f2018-01-02 13:36:42 -0700256static inline int scrub_is_page_on_raid56(struct scrub_page *page)
257{
258 return page->recover &&
259 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
260}
Stefan Behrens1623ede2012-03-27 14:21:26 -0400261
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100262static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
263{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200264 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100265 atomic_inc(&sctx->bios_in_flight);
266}
267
268static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
269{
270 atomic_dec(&sctx->bios_in_flight);
271 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000272 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100273}
274
Wang Shilongcb7ab022013-12-04 21:16:53 +0800275static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800276{
277 while (atomic_read(&fs_info->scrub_pause_req)) {
278 mutex_unlock(&fs_info->scrub_lock);
279 wait_event(fs_info->scrub_pause_wait,
280 atomic_read(&fs_info->scrub_pause_req) == 0);
281 mutex_lock(&fs_info->scrub_lock);
282 }
283}
284
Zhaolei0e22be82015-08-05 16:43:28 +0800285static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800286{
287 atomic_inc(&fs_info->scrubs_paused);
288 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800289}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800290
Zhaolei0e22be82015-08-05 16:43:28 +0800291static void scrub_pause_off(struct btrfs_fs_info *fs_info)
292{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800293 mutex_lock(&fs_info->scrub_lock);
294 __scrub_blocked_if_needed(fs_info);
295 atomic_dec(&fs_info->scrubs_paused);
296 mutex_unlock(&fs_info->scrub_lock);
297
298 wake_up(&fs_info->scrub_pause_wait);
299}
300
Zhaolei0e22be82015-08-05 16:43:28 +0800301static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
302{
303 scrub_pause_on(fs_info);
304 scrub_pause_off(fs_info);
305}
306
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100307/*
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800308 * Insert new full stripe lock into full stripe locks tree
309 *
310 * Return pointer to existing or newly inserted full_stripe_lock structure if
311 * everything works well.
312 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
313 *
314 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
315 * function
316 */
317static struct full_stripe_lock *insert_full_stripe_lock(
318 struct btrfs_full_stripe_locks_tree *locks_root,
319 u64 fstripe_logical)
320{
321 struct rb_node **p;
322 struct rb_node *parent = NULL;
323 struct full_stripe_lock *entry;
324 struct full_stripe_lock *ret;
325
David Sterbaa32bf9a2018-03-16 02:21:22 +0100326 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800327
328 p = &locks_root->root.rb_node;
329 while (*p) {
330 parent = *p;
331 entry = rb_entry(parent, struct full_stripe_lock, node);
332 if (fstripe_logical < entry->logical) {
333 p = &(*p)->rb_left;
334 } else if (fstripe_logical > entry->logical) {
335 p = &(*p)->rb_right;
336 } else {
337 entry->refs++;
338 return entry;
339 }
340 }
341
342 /* Insert new lock */
343 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
344 if (!ret)
345 return ERR_PTR(-ENOMEM);
346 ret->logical = fstripe_logical;
347 ret->refs = 1;
348 mutex_init(&ret->mutex);
349
350 rb_link_node(&ret->node, parent, p);
351 rb_insert_color(&ret->node, &locks_root->root);
352 return ret;
353}
354
355/*
356 * Search for a full stripe lock of a block group
357 *
358 * Return pointer to existing full stripe lock if found
359 * Return NULL if not found
360 */
361static struct full_stripe_lock *search_full_stripe_lock(
362 struct btrfs_full_stripe_locks_tree *locks_root,
363 u64 fstripe_logical)
364{
365 struct rb_node *node;
366 struct full_stripe_lock *entry;
367
David Sterbaa32bf9a2018-03-16 02:21:22 +0100368 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800369
370 node = locks_root->root.rb_node;
371 while (node) {
372 entry = rb_entry(node, struct full_stripe_lock, node);
373 if (fstripe_logical < entry->logical)
374 node = node->rb_left;
375 else if (fstripe_logical > entry->logical)
376 node = node->rb_right;
377 else
378 return entry;
379 }
380 return NULL;
381}
382
383/*
384 * Helper to get full stripe logical from a normal bytenr.
385 *
386 * Caller must ensure @cache is a RAID56 block group.
387 */
388static u64 get_full_stripe_logical(struct btrfs_block_group_cache *cache,
389 u64 bytenr)
390{
391 u64 ret;
392
393 /*
394 * Due to chunk item size limit, full stripe length should not be
395 * larger than U32_MAX. Just a sanity check here.
396 */
397 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
398
399 /*
400 * round_down() can only handle power of 2, while RAID56 full
401 * stripe length can be 64KiB * n, so we need to manually round down.
402 */
403 ret = div64_u64(bytenr - cache->key.objectid, cache->full_stripe_len) *
404 cache->full_stripe_len + cache->key.objectid;
405 return ret;
406}
407
408/*
409 * Lock a full stripe to avoid concurrency of recovery and read
410 *
411 * It's only used for profiles with parities (RAID5/6), for other profiles it
412 * does nothing.
413 *
414 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
415 * So caller must call unlock_full_stripe() at the same context.
416 *
417 * Return <0 if encounters error.
418 */
419static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
420 bool *locked_ret)
421{
422 struct btrfs_block_group_cache *bg_cache;
423 struct btrfs_full_stripe_locks_tree *locks_root;
424 struct full_stripe_lock *existing;
425 u64 fstripe_start;
426 int ret = 0;
427
428 *locked_ret = false;
429 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
430 if (!bg_cache) {
431 ASSERT(0);
432 return -ENOENT;
433 }
434
435 /* Profiles not based on parity don't need full stripe lock */
436 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
437 goto out;
438 locks_root = &bg_cache->full_stripe_locks_root;
439
440 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
441
442 /* Now insert the full stripe lock */
443 mutex_lock(&locks_root->lock);
444 existing = insert_full_stripe_lock(locks_root, fstripe_start);
445 mutex_unlock(&locks_root->lock);
446 if (IS_ERR(existing)) {
447 ret = PTR_ERR(existing);
448 goto out;
449 }
450 mutex_lock(&existing->mutex);
451 *locked_ret = true;
452out:
453 btrfs_put_block_group(bg_cache);
454 return ret;
455}
456
457/*
458 * Unlock a full stripe.
459 *
460 * NOTE: Caller must ensure it's the same context calling corresponding
461 * lock_full_stripe().
462 *
463 * Return 0 if we unlock full stripe without problem.
464 * Return <0 for error
465 */
466static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
467 bool locked)
468{
469 struct btrfs_block_group_cache *bg_cache;
470 struct btrfs_full_stripe_locks_tree *locks_root;
471 struct full_stripe_lock *fstripe_lock;
472 u64 fstripe_start;
473 bool freeit = false;
474 int ret = 0;
475
476 /* If we didn't acquire full stripe lock, no need to continue */
477 if (!locked)
478 return 0;
479
480 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
481 if (!bg_cache) {
482 ASSERT(0);
483 return -ENOENT;
484 }
485 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
486 goto out;
487
488 locks_root = &bg_cache->full_stripe_locks_root;
489 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
490
491 mutex_lock(&locks_root->lock);
492 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
493 /* Unpaired unlock_full_stripe() detected */
494 if (!fstripe_lock) {
495 WARN_ON(1);
496 ret = -ENOENT;
497 mutex_unlock(&locks_root->lock);
498 goto out;
499 }
500
501 if (fstripe_lock->refs == 0) {
502 WARN_ON(1);
503 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
504 fstripe_lock->logical);
505 } else {
506 fstripe_lock->refs--;
507 }
508
509 if (fstripe_lock->refs == 0) {
510 rb_erase(&fstripe_lock->node, &locks_root->root);
511 freeit = true;
512 }
513 mutex_unlock(&locks_root->lock);
514
515 mutex_unlock(&fstripe_lock->mutex);
516 if (freeit)
517 kfree(fstripe_lock);
518out:
519 btrfs_put_block_group(bg_cache);
520 return ret;
521}
522
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100523static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100524{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100525 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100526 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100527 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100528 struct btrfs_ordered_sum, list);
529 list_del(&sum->list);
530 kfree(sum);
531 }
532}
533
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100534static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100535{
536 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100537
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100538 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100539 return;
540
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400541 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100542 if (sctx->curr != -1) {
543 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400544
545 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100546 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400547 scrub_block_put(sbio->pagev[i]->sblock);
548 }
549 bio_put(sbio->bio);
550 }
551
Stefan Behrensff023aa2012-11-06 11:43:11 +0100552 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100553 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100554
555 if (!sbio)
556 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100557 kfree(sbio);
558 }
559
David Sterba3fb99302017-05-16 19:10:32 +0200560 kfree(sctx->wr_curr_bio);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100561 scrub_free_csums(sctx);
562 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100563}
564
Filipe Mananaf55985f2015-02-09 21:14:24 +0000565static void scrub_put_ctx(struct scrub_ctx *sctx)
566{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200567 if (refcount_dec_and_test(&sctx->refs))
Filipe Mananaf55985f2015-02-09 21:14:24 +0000568 scrub_free_ctx(sctx);
569}
570
Arne Jansena2de7332011-03-08 14:14:00 +0100571static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100572struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100573{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100574 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100575 int i;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400576 struct btrfs_fs_info *fs_info = dev->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100577
David Sterba58c4e172016-02-11 10:49:42 +0100578 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100579 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100580 goto nomem;
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200581 refcount_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100582 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200583 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100584 sctx->curr = -1;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400585 sctx->fs_info = dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100586 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100587 struct scrub_bio *sbio;
588
David Sterba58c4e172016-02-11 10:49:42 +0100589 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
Arne Jansena2de7332011-03-08 14:14:00 +0100590 if (!sbio)
591 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100592 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100593
Arne Jansena2de7332011-03-08 14:14:00 +0100594 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100595 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400596 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800597 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
598 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100599
Stefan Behrensff023aa2012-11-06 11:43:11 +0100600 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100601 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200602 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100603 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100604 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100605 sctx->first_free = 0;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100606 atomic_set(&sctx->bios_in_flight, 0);
607 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100608 atomic_set(&sctx->cancel_req, 0);
609 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
610 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100611
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100612 spin_lock_init(&sctx->list_lock);
613 spin_lock_init(&sctx->stat_lock);
614 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100615
David Sterba3fb99302017-05-16 19:10:32 +0200616 WARN_ON(sctx->wr_curr_bio != NULL);
617 mutex_init(&sctx->wr_lock);
618 sctx->wr_curr_bio = NULL;
David Sterba8fcdac32017-05-16 19:10:23 +0200619 if (is_dev_replace) {
David Sterbaded56182017-06-26 15:19:00 +0200620 WARN_ON(!fs_info->dev_replace.tgtdev);
David Sterba3fb99302017-05-16 19:10:32 +0200621 sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
David Sterbaded56182017-06-26 15:19:00 +0200622 sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200623 sctx->flush_all_writes = false;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100624 }
David Sterba8fcdac32017-05-16 19:10:23 +0200625
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100626 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100627
628nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100629 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100630 return ERR_PTR(-ENOMEM);
631}
632
Stefan Behrensff023aa2012-11-06 11:43:11 +0100633static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
634 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200635{
636 u64 isize;
637 u32 nlink;
638 int ret;
639 int i;
David Sterbade2491f2017-05-31 19:21:38 +0200640 unsigned nofs_flag;
Jan Schmidt558540c2011-06-13 19:59:12 +0200641 struct extent_buffer *eb;
642 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100643 struct scrub_warning *swarn = warn_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400644 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200645 struct inode_fs_paths *ipath = NULL;
646 struct btrfs_root *local_root;
647 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100648 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200649
650 root_key.objectid = root;
651 root_key.type = BTRFS_ROOT_ITEM_KEY;
652 root_key.offset = (u64)-1;
653 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
654 if (IS_ERR(local_root)) {
655 ret = PTR_ERR(local_root);
656 goto err;
657 }
658
David Sterba14692cc2015-01-02 18:55:46 +0100659 /*
660 * this makes the path point to (inum INODE_ITEM ioff)
661 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100662 key.objectid = inum;
663 key.type = BTRFS_INODE_ITEM_KEY;
664 key.offset = 0;
665
666 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200667 if (ret) {
668 btrfs_release_path(swarn->path);
669 goto err;
670 }
671
672 eb = swarn->path->nodes[0];
673 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
674 struct btrfs_inode_item);
675 isize = btrfs_inode_size(eb, inode_item);
676 nlink = btrfs_inode_nlink(eb, inode_item);
677 btrfs_release_path(swarn->path);
678
David Sterbade2491f2017-05-31 19:21:38 +0200679 /*
680 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
681 * uses GFP_NOFS in this context, so we keep it consistent but it does
682 * not seem to be strictly necessary.
683 */
684 nofs_flag = memalloc_nofs_save();
Jan Schmidt558540c2011-06-13 19:59:12 +0200685 ipath = init_ipath(4096, local_root, swarn->path);
David Sterbade2491f2017-05-31 19:21:38 +0200686 memalloc_nofs_restore(nofs_flag);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300687 if (IS_ERR(ipath)) {
688 ret = PTR_ERR(ipath);
689 ipath = NULL;
690 goto err;
691 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200692 ret = paths_from_inode(inum, ipath);
693
694 if (ret < 0)
695 goto err;
696
697 /*
698 * we deliberately ignore the bit ipath might have been too small to
699 * hold all of the paths here
700 */
701 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400702 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200703"%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400704 swarn->errstr, swarn->logical,
705 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200706 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400707 root, inum, offset,
708 min(isize - offset, (u64)PAGE_SIZE), nlink,
709 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200710
711 free_ipath(ipath);
712 return 0;
713
714err:
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400715 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200716 "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400717 swarn->errstr, swarn->logical,
718 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200719 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400720 root, inum, offset, ret);
Jan Schmidt558540c2011-06-13 19:59:12 +0200721
722 free_ipath(ipath);
723 return 0;
724}
725
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400726static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200727{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100728 struct btrfs_device *dev;
729 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200730 struct btrfs_path *path;
731 struct btrfs_key found_key;
732 struct extent_buffer *eb;
733 struct btrfs_extent_item *ei;
734 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200735 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100736 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600737 u64 flags = 0;
738 u64 ref_root;
739 u32 item_size;
Dan Carpenter07c9a8e2016-03-11 11:08:56 +0300740 u8 ref_level = 0;
Liu Bo69917e42012-09-07 20:01:28 -0600741 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200742
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100743 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100744 dev = sblock->pagev[0]->dev;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400745 fs_info = sblock->sctx->fs_info;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100746
Jan Schmidt558540c2011-06-13 19:59:12 +0200747 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200748 if (!path)
749 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200750
David Sterba6aa21262017-10-04 17:07:07 +0200751 swarn.physical = sblock->pagev[0]->physical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100752 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200753 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100754 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200755
Liu Bo69917e42012-09-07 20:01:28 -0600756 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
757 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200758 if (ret < 0)
759 goto out;
760
Jan Schmidt4692cf52011-12-02 14:56:41 +0100761 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200762 swarn.extent_item_size = found_key.offset;
763
764 eb = path->nodes[0];
765 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
766 item_size = btrfs_item_size_nr(eb, path->slots[0]);
767
Liu Bo69917e42012-09-07 20:01:28 -0600768 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200769 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800770 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
771 item_size, &ref_root,
772 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200773 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200774"%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400775 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400776 rcu_str_deref(dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200777 swarn.physical,
Jan Schmidt558540c2011-06-13 19:59:12 +0200778 ref_level ? "node" : "leaf",
779 ret < 0 ? -1 : ref_level,
780 ret < 0 ? -1 : ref_root);
781 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600782 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200783 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600784 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200785 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100786 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100787 iterate_extent_inodes(fs_info, found_key.objectid,
788 extent_item_pos, 1,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400789 scrub_print_warning_inode, &swarn, false);
Jan Schmidt558540c2011-06-13 19:59:12 +0200790 }
791
792out:
793 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200794}
795
Miao Xieaf8e2d12014-10-23 14:42:50 +0800796static inline void scrub_get_recover(struct scrub_recover *recover)
797{
Elena Reshetova6f615012017-03-03 10:55:21 +0200798 refcount_inc(&recover->refs);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800799}
800
Qu Wenruoe501bfe2017-03-29 09:33:22 +0800801static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
802 struct scrub_recover *recover)
Miao Xieaf8e2d12014-10-23 14:42:50 +0800803{
Elena Reshetova6f615012017-03-03 10:55:21 +0200804 if (refcount_dec_and_test(&recover->refs)) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +0800805 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +0800806 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800807 kfree(recover);
808 }
809}
810
Arne Jansena2de7332011-03-08 14:14:00 +0100811/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400812 * scrub_handle_errored_block gets called when either verification of the
813 * pages failed or the bio failed to read, e.g. with EIO. In the latter
814 * case, this function handles all pages in the bio, even though only one
815 * may be bad.
816 * The goal of this function is to repair the errored block by using the
817 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100818 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400819static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100820{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100821 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100822 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400823 struct btrfs_fs_info *fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400824 u64 logical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400825 unsigned int failed_mirror_index;
826 unsigned int is_metadata;
827 unsigned int have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400828 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
829 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100830 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400831 int mirror_index;
832 int page_num;
833 int success;
Qu Wenruo28d70e22017-04-14 08:35:55 +0800834 bool full_stripe_locked;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400835 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
836 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100837
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400838 BUG_ON(sblock_to_check->page_count < 1);
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400839 fs_info = sctx->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000840 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
841 /*
842 * if we find an error in a super block, we just report it.
843 * They will get written with the next transaction commit
844 * anyway
845 */
846 spin_lock(&sctx->stat_lock);
847 ++sctx->stat.super_errors;
848 spin_unlock(&sctx->stat_lock);
849 return 0;
850 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100851 logical = sblock_to_check->pagev[0]->logical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100852 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
853 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
854 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400855 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100856 have_csum = sblock_to_check->pagev[0]->have_csum;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100857 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400858
Qu Wenruo28d70e22017-04-14 08:35:55 +0800859 /*
860 * For RAID5/6, race can happen for a different device scrub thread.
861 * For data corruption, Parity and Data threads will both try
862 * to recovery the data.
863 * Race can lead to doubly added csum error, or even unrecoverable
864 * error.
865 */
866 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
867 if (ret < 0) {
868 spin_lock(&sctx->stat_lock);
869 if (ret == -ENOMEM)
870 sctx->stat.malloc_errors++;
871 sctx->stat.read_errors++;
872 sctx->stat.uncorrectable_errors++;
873 spin_unlock(&sctx->stat_lock);
874 return ret;
875 }
876
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400877 /*
878 * read all mirrors one after the other. This includes to
879 * re-read the extent or metadata block that failed (that was
880 * the cause that this fixup code is called) another time,
881 * page by page this time in order to know which pages
882 * caused I/O errors and which ones are good (for all mirrors).
883 * It is the goal to handle the situation when more than one
884 * mirror contains I/O errors, but the errors do not
885 * overlap, i.e. the data can be repaired by selecting the
886 * pages from those mirrors without I/O error on the
887 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
888 * would be that mirror #1 has an I/O error on the first page,
889 * the second page is good, and mirror #2 has an I/O error on
890 * the second page, but the first page is good.
891 * Then the first page of the first mirror can be repaired by
892 * taking the first page of the second mirror, and the
893 * second page of the second mirror can be repaired by
894 * copying the contents of the 2nd page of the 1st mirror.
895 * One more note: if the pages of one mirror contain I/O
896 * errors, the checksum cannot be verified. In order to get
897 * the best data for repairing, the first attempt is to find
898 * a mirror without I/O errors and with a validated checksum.
899 * Only if this is not possible, the pages are picked from
900 * mirrors with I/O errors without considering the checksum.
901 * If the latter is the case, at the end, the checksum of the
902 * repaired area is verified in order to correctly maintain
903 * the statistics.
904 */
905
David Sterba31e818f2015-02-20 18:00:26 +0100906 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
907 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400908 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100909 spin_lock(&sctx->stat_lock);
910 sctx->stat.malloc_errors++;
911 sctx->stat.read_errors++;
912 sctx->stat.uncorrectable_errors++;
913 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100914 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400915 goto out;
916 }
917
918 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800919 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400920 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100921 spin_lock(&sctx->stat_lock);
922 sctx->stat.read_errors++;
923 sctx->stat.uncorrectable_errors++;
924 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100925 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400926 goto out;
927 }
928 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
929 sblock_bad = sblocks_for_recheck + failed_mirror_index;
930
931 /* build and submit the bios for the failed mirror, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +0800932 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400933
934 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
935 sblock_bad->no_io_error_seen) {
936 /*
937 * the error disappeared after reading page by page, or
938 * the area was part of a huge bio and other parts of the
939 * bio caused I/O errors, or the block layer merged several
940 * read requests into one and the error is caused by a
941 * different bio (usually one of the two latter cases is
942 * the cause)
943 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100944 spin_lock(&sctx->stat_lock);
945 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800946 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100947 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400948
Stefan Behrensff023aa2012-11-06 11:43:11 +0100949 if (sctx->is_dev_replace)
950 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400951 goto out;
952 }
953
954 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100955 spin_lock(&sctx->stat_lock);
956 sctx->stat.read_errors++;
957 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400958 if (__ratelimit(&_rs))
959 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100960 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400961 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100962 spin_lock(&sctx->stat_lock);
963 sctx->stat.csum_errors++;
964 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400965 if (__ratelimit(&_rs))
966 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100967 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200968 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400969 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100970 spin_lock(&sctx->stat_lock);
971 sctx->stat.verify_errors++;
972 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400973 if (__ratelimit(&_rs))
974 scrub_print_warning("checksum/header error",
975 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200976 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100977 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200978 BTRFS_DEV_STAT_GENERATION_ERRS);
979 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100980 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200981 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400982 }
983
Ilya Dryomov33ef30a2013-11-03 19:06:38 +0200984 if (sctx->readonly) {
985 ASSERT(!sctx->is_dev_replace);
986 goto out;
987 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400988
Qu Wenruo665d4952018-07-11 13:41:21 +0800989 /*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400990 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100991 * checksums.
992 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400993 * errors and also does not have a checksum error.
994 * If one is found, and if a checksum is present, the full block
995 * that is known to contain an error is rewritten. Afterwards
996 * the block is known to be corrected.
997 * If a mirror is found which is completely correct, and no
998 * checksum is present, only those pages are rewritten that had
999 * an I/O error in the block to be repaired, since it cannot be
1000 * determined, which copy of the other pages is better (and it
1001 * could happen otherwise that a correct page would be
1002 * overwritten by a bad one).
1003 */
Liu Bo762221f2018-01-02 13:36:42 -07001004 for (mirror_index = 0; ;mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001005 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001006
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001007 if (mirror_index == failed_mirror_index)
1008 continue;
Liu Bo762221f2018-01-02 13:36:42 -07001009
1010 /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
1011 if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1012 if (mirror_index >= BTRFS_MAX_MIRRORS)
1013 break;
1014 if (!sblocks_for_recheck[mirror_index].page_count)
1015 break;
1016
1017 sblock_other = sblocks_for_recheck + mirror_index;
1018 } else {
1019 struct scrub_recover *r = sblock_bad->pagev[0]->recover;
1020 int max_allowed = r->bbio->num_stripes -
1021 r->bbio->num_tgtdevs;
1022
1023 if (mirror_index >= max_allowed)
1024 break;
1025 if (!sblocks_for_recheck[1].page_count)
1026 break;
1027
1028 ASSERT(failed_mirror_index == 0);
1029 sblock_other = sblocks_for_recheck + 1;
1030 sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
1031 }
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001032
1033 /* build and submit the bios, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001034 scrub_recheck_block(fs_info, sblock_other, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001035
1036 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001037 !sblock_other->checksum_error &&
1038 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001039 if (sctx->is_dev_replace) {
1040 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001041 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001042 } else {
1043 ret = scrub_repair_block_from_good_copy(
1044 sblock_bad, sblock_other);
1045 if (!ret)
1046 goto corrected_error;
1047 }
Arne Jansena2de7332011-03-08 14:14:00 +01001048 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001049 }
1050
Zhao Leib968fed2015-01-20 15:11:41 +08001051 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1052 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001053
1054 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001055 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001056 * repaired, continue by picking good copies of those pages.
1057 * Select the good pages from mirrors to rewrite bad pages from
1058 * the area to fix. Afterwards verify the checksum of the block
1059 * that is supposed to be repaired. This verification step is
1060 * only done for the purpose of statistic counting and for the
1061 * final scrub report, whether errors remain.
1062 * A perfect algorithm could make use of the checksum and try
1063 * all possible combinations of pages from the different mirrors
1064 * until the checksum verification succeeds. For example, when
1065 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1066 * of mirror #2 is readable but the final checksum test fails,
1067 * then the 2nd page of mirror #3 could be tried, whether now
Nicholas D Steeves01327612016-05-19 21:18:45 -04001068 * the final checksum succeeds. But this would be a rare
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001069 * exception and is therefore not implemented. At least it is
1070 * avoided that the good copy is overwritten.
1071 * A more useful improvement would be to pick the sectors
1072 * without I/O error based on sector sizes (512 bytes on legacy
1073 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1074 * mirror could be repaired by taking 512 byte of a different
1075 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1076 * area are unreadable.
1077 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001078 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001079 for (page_num = 0; page_num < sblock_bad->page_count;
1080 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001081 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001082 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001083
Zhao Leib968fed2015-01-20 15:11:41 +08001084 /* skip no-io-error page in scrub */
1085 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001086 continue;
1087
Liu Bo47597002018-03-02 16:10:41 -07001088 if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1089 /*
1090 * In case of dev replace, if raid56 rebuild process
1091 * didn't work out correct data, then copy the content
1092 * in sblock_bad to make sure target device is identical
1093 * to source device, instead of writing garbage data in
1094 * sblock_for_recheck array to target device.
1095 */
1096 sblock_other = NULL;
1097 } else if (page_bad->io_error) {
1098 /* try to find no-io-error page in mirrors */
Zhao Leib968fed2015-01-20 15:11:41 +08001099 for (mirror_index = 0;
1100 mirror_index < BTRFS_MAX_MIRRORS &&
1101 sblocks_for_recheck[mirror_index].page_count > 0;
1102 mirror_index++) {
1103 if (!sblocks_for_recheck[mirror_index].
1104 pagev[page_num]->io_error) {
1105 sblock_other = sblocks_for_recheck +
1106 mirror_index;
1107 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001108 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001109 }
Zhao Leib968fed2015-01-20 15:11:41 +08001110 if (!sblock_other)
1111 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001112 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001113
Zhao Leib968fed2015-01-20 15:11:41 +08001114 if (sctx->is_dev_replace) {
1115 /*
1116 * did not find a mirror to fetch the page
1117 * from. scrub_write_page_to_dev_replace()
1118 * handles this case (page->io_error), by
1119 * filling the block with zeros before
1120 * submitting the write request
1121 */
1122 if (!sblock_other)
1123 sblock_other = sblock_bad;
1124
1125 if (scrub_write_page_to_dev_replace(sblock_other,
1126 page_num) != 0) {
1127 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001128 &fs_info->dev_replace.num_write_errors);
Zhao Leib968fed2015-01-20 15:11:41 +08001129 success = 0;
1130 }
1131 } else if (sblock_other) {
1132 ret = scrub_repair_page_from_good_copy(sblock_bad,
1133 sblock_other,
1134 page_num, 0);
1135 if (0 == ret)
1136 page_bad->io_error = 0;
1137 else
1138 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001139 }
1140 }
1141
Zhao Leib968fed2015-01-20 15:11:41 +08001142 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001143 if (is_metadata || have_csum) {
1144 /*
1145 * need to verify the checksum now that all
1146 * sectors on disk are repaired (the write
1147 * request for data to be repaired is on its way).
1148 * Just be lazy and use scrub_recheck_block()
1149 * which re-reads the data before the checksum
1150 * is verified, but most likely the data comes out
1151 * of the page cache.
1152 */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001153 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001154 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001155 !sblock_bad->checksum_error &&
1156 sblock_bad->no_io_error_seen)
1157 goto corrected_error;
1158 else
1159 goto did_not_correct_error;
1160 } else {
1161corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001162 spin_lock(&sctx->stat_lock);
1163 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001164 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001165 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001166 btrfs_err_rl_in_rcu(fs_info,
1167 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001168 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001169 }
1170 } else {
1171did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001172 spin_lock(&sctx->stat_lock);
1173 sctx->stat.uncorrectable_errors++;
1174 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001175 btrfs_err_rl_in_rcu(fs_info,
1176 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001177 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001178 }
1179
1180out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001181 if (sblocks_for_recheck) {
1182 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1183 mirror_index++) {
1184 struct scrub_block *sblock = sblocks_for_recheck +
1185 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001186 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001187 int page_index;
1188
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001189 for (page_index = 0; page_index < sblock->page_count;
1190 page_index++) {
1191 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001192 recover = sblock->pagev[page_index]->recover;
1193 if (recover) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001194 scrub_put_recover(fs_info, recover);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001195 sblock->pagev[page_index]->recover =
1196 NULL;
1197 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001198 scrub_page_put(sblock->pagev[page_index]);
1199 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001200 }
1201 kfree(sblocks_for_recheck);
1202 }
1203
Qu Wenruo28d70e22017-04-14 08:35:55 +08001204 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
1205 if (ret < 0)
1206 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001207 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001208}
1209
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001210static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001211{
Zhao Lei10f11902015-01-20 15:11:43 +08001212 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1213 return 2;
1214 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1215 return 3;
1216 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001217 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001218}
1219
Zhao Lei10f11902015-01-20 15:11:43 +08001220static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1221 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001222 u64 mapped_length,
1223 int nstripes, int mirror,
1224 int *stripe_index,
1225 u64 *stripe_offset)
1226{
1227 int i;
1228
Zhao Leiffe2d202015-01-20 15:11:44 +08001229 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001230 /* RAID5/6 */
1231 for (i = 0; i < nstripes; i++) {
1232 if (raid_map[i] == RAID6_Q_STRIPE ||
1233 raid_map[i] == RAID5_P_STRIPE)
1234 continue;
1235
1236 if (logical >= raid_map[i] &&
1237 logical < raid_map[i] + mapped_length)
1238 break;
1239 }
1240
1241 *stripe_index = i;
1242 *stripe_offset = logical - raid_map[i];
1243 } else {
1244 /* The other RAID type */
1245 *stripe_index = mirror;
1246 *stripe_offset = 0;
1247 }
1248}
1249
Zhao Leibe50a8d2015-01-20 15:11:42 +08001250static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001251 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001252{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001253 struct scrub_ctx *sctx = original_sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001254 struct btrfs_fs_info *fs_info = sctx->fs_info;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001255 u64 length = original_sblock->page_count * PAGE_SIZE;
1256 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001257 u64 generation = original_sblock->pagev[0]->generation;
1258 u64 flags = original_sblock->pagev[0]->flags;
1259 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001260 struct scrub_recover *recover;
1261 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001262 u64 sublen;
1263 u64 mapped_length;
1264 u64 stripe_offset;
1265 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001266 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001267 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001268 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001269 int ret;
1270
1271 /*
Zhao Lei57019342015-01-20 15:11:45 +08001272 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001273 * are not used (and not set) in the blocks that are used for
1274 * the recheck procedure
1275 */
1276
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001277 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001278 sublen = min_t(u64, length, PAGE_SIZE);
1279 mapped_length = sublen;
1280 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001281
1282 /*
1283 * with a length of PAGE_SIZE, each returned stripe
1284 * represents one mirror
1285 */
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001286 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001287 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
David Sterba825ad4c2017-03-28 14:45:22 +02001288 logical, &mapped_length, &bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001289 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001290 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001291 btrfs_bio_counter_dec(fs_info);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001292 return -EIO;
1293 }
1294
Miao Xieaf8e2d12014-10-23 14:42:50 +08001295 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1296 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001297 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001298 btrfs_bio_counter_dec(fs_info);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001299 return -ENOMEM;
1300 }
1301
Elena Reshetova6f615012017-03-03 10:55:21 +02001302 refcount_set(&recover->refs, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001303 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001304 recover->map_length = mapped_length;
1305
Ashish Samant24731142016-04-29 18:33:59 -07001306 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001307
Zhao Leibe50a8d2015-01-20 15:11:42 +08001308 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001309
Miao Xieaf8e2d12014-10-23 14:42:50 +08001310 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001311 mirror_index++) {
1312 struct scrub_block *sblock;
1313 struct scrub_page *page;
1314
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001315 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001316 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001317
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001318 page = kzalloc(sizeof(*page), GFP_NOFS);
1319 if (!page) {
1320leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001321 spin_lock(&sctx->stat_lock);
1322 sctx->stat.malloc_errors++;
1323 spin_unlock(&sctx->stat_lock);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001324 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001325 return -ENOMEM;
1326 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001327 scrub_page_get(page);
1328 sblock->pagev[page_index] = page;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001329 page->sblock = sblock;
1330 page->flags = flags;
1331 page->generation = generation;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001332 page->logical = logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001333 page->have_csum = have_csum;
1334 if (have_csum)
1335 memcpy(page->csum,
1336 original_sblock->pagev[0]->csum,
1337 sctx->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001338
Zhao Lei10f11902015-01-20 15:11:43 +08001339 scrub_stripe_index_and_offset(logical,
1340 bbio->map_type,
1341 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001342 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001343 bbio->num_stripes -
1344 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001345 mirror_index,
1346 &stripe_index,
1347 &stripe_offset);
1348 page->physical = bbio->stripes[stripe_index].physical +
1349 stripe_offset;
1350 page->dev = bbio->stripes[stripe_index].dev;
1351
Stefan Behrensff023aa2012-11-06 11:43:11 +01001352 BUG_ON(page_index >= original_sblock->page_count);
1353 page->physical_for_dev_replace =
1354 original_sblock->pagev[page_index]->
1355 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001356 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001357 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001358 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001359 page->page = alloc_page(GFP_NOFS);
1360 if (!page->page)
1361 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001362
1363 scrub_get_recover(recover);
1364 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001365 }
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001366 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001367 length -= sublen;
1368 logical += sublen;
1369 page_index++;
1370 }
1371
1372 return 0;
1373}
1374
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001375static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001376{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001377 complete(bio->bi_private);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001378}
1379
Miao Xieaf8e2d12014-10-23 14:42:50 +08001380static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1381 struct bio *bio,
1382 struct scrub_page *page)
1383{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001384 DECLARE_COMPLETION_ONSTACK(done);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001385 int ret;
Liu Bo762221f2018-01-02 13:36:42 -07001386 int mirror_num;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001387
Miao Xieaf8e2d12014-10-23 14:42:50 +08001388 bio->bi_iter.bi_sector = page->logical >> 9;
1389 bio->bi_private = &done;
1390 bio->bi_end_io = scrub_bio_wait_endio;
1391
Liu Bo762221f2018-01-02 13:36:42 -07001392 mirror_num = page->sblock->pagev[0]->mirror_num;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001393 ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001394 page->recover->map_length,
Liu Bo762221f2018-01-02 13:36:42 -07001395 mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001396 if (ret)
1397 return ret;
1398
Liu Bob4ff5ad2017-11-30 17:26:39 -07001399 wait_for_completion_io(&done);
1400 return blk_status_to_errno(bio->bi_status);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001401}
1402
Liu Bo6ca17652018-03-07 12:08:09 -07001403static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
1404 struct scrub_block *sblock)
1405{
1406 struct scrub_page *first_page = sblock->pagev[0];
1407 struct bio *bio;
1408 int page_num;
1409
1410 /* All pages in sblock belong to the same stripe on the same device. */
1411 ASSERT(first_page->dev);
1412 if (!first_page->dev->bdev)
1413 goto out;
1414
1415 bio = btrfs_io_bio_alloc(BIO_MAX_PAGES);
1416 bio_set_dev(bio, first_page->dev->bdev);
1417
1418 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1419 struct scrub_page *page = sblock->pagev[page_num];
1420
1421 WARN_ON(!page->page);
1422 bio_add_page(bio, page->page, PAGE_SIZE, 0);
1423 }
1424
1425 if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) {
1426 bio_put(bio);
1427 goto out;
1428 }
1429
1430 bio_put(bio);
1431
1432 scrub_recheck_block_checksum(sblock);
1433
1434 return;
1435out:
1436 for (page_num = 0; page_num < sblock->page_count; page_num++)
1437 sblock->pagev[page_num]->io_error = 1;
1438
1439 sblock->no_io_error_seen = 0;
1440}
1441
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001442/*
1443 * this function will check the on disk data for checksum errors, header
1444 * errors and read I/O errors. If any I/O errors happen, the exact pages
1445 * which are errored are marked as being bad. The goal is to enable scrub
1446 * to take those pages that are not errored from all the mirrors so that
1447 * the pages that are errored in the just handled mirror can be repaired.
1448 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001449static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001450 struct scrub_block *sblock,
1451 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001452{
1453 int page_num;
1454
1455 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001456
Liu Bo6ca17652018-03-07 12:08:09 -07001457 /* short cut for raid56 */
1458 if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0]))
1459 return scrub_recheck_block_on_raid56(fs_info, sblock);
1460
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001461 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1462 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001463 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001464
Stefan Behrens442a4f62012-05-25 16:06:08 +02001465 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001466 page->io_error = 1;
1467 sblock->no_io_error_seen = 0;
1468 continue;
1469 }
1470
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001471 WARN_ON(!page->page);
David Sterbac5e4c3d2017-06-12 17:29:41 +02001472 bio = btrfs_io_bio_alloc(1);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001473 bio_set_dev(bio, page->dev->bdev);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001474
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001475 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Liu Bo6ca17652018-03-07 12:08:09 -07001476 bio->bi_iter.bi_sector = page->physical >> 9;
1477 bio->bi_opf = REQ_OP_READ;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001478
Liu Bo6ca17652018-03-07 12:08:09 -07001479 if (btrfsic_submit_bio_wait(bio)) {
1480 page->io_error = 1;
1481 sblock->no_io_error_seen = 0;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001482 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001483
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001484 bio_put(bio);
1485 }
1486
1487 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001488 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001489}
1490
Miao Xie17a9be22014-07-24 11:37:08 +08001491static inline int scrub_check_fsid(u8 fsid[],
1492 struct scrub_page *spage)
1493{
1494 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1495 int ret;
1496
Anand Jain44880fd2017-07-29 17:50:09 +08001497 ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Miao Xie17a9be22014-07-24 11:37:08 +08001498 return !ret;
1499}
1500
Zhao Leiba7cf982015-08-24 21:18:02 +08001501static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001502{
Zhao Leiba7cf982015-08-24 21:18:02 +08001503 sblock->header_error = 0;
1504 sblock->checksum_error = 0;
1505 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001506
Zhao Leiba7cf982015-08-24 21:18:02 +08001507 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1508 scrub_checksum_data(sblock);
1509 else
1510 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001511}
1512
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001513static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001514 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001515{
1516 int page_num;
1517 int ret = 0;
1518
1519 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1520 int ret_sub;
1521
1522 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1523 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001524 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001525 if (ret_sub)
1526 ret = ret_sub;
1527 }
1528
1529 return ret;
1530}
1531
1532static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1533 struct scrub_block *sblock_good,
1534 int page_num, int force_write)
1535{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001536 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1537 struct scrub_page *page_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001538 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001539
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001540 BUG_ON(page_bad->page == NULL);
1541 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001542 if (force_write || sblock_bad->header_error ||
1543 sblock_bad->checksum_error || page_bad->io_error) {
1544 struct bio *bio;
1545 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001546
Stefan Behrensff023aa2012-11-06 11:43:11 +01001547 if (!page_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001548 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001549 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001550 return -EIO;
1551 }
1552
David Sterbac5e4c3d2017-06-12 17:29:41 +02001553 bio = btrfs_io_bio_alloc(1);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001554 bio_set_dev(bio, page_bad->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001555 bio->bi_iter.bi_sector = page_bad->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02001556 bio->bi_opf = REQ_OP_WRITE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001557
1558 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1559 if (PAGE_SIZE != ret) {
1560 bio_put(bio);
1561 return -EIO;
1562 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001563
Mike Christie4e49ea42016-06-05 14:31:41 -05001564 if (btrfsic_submit_bio_wait(bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001565 btrfs_dev_stat_inc_and_print(page_bad->dev,
1566 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001567 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001568 &fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001569 bio_put(bio);
1570 return -EIO;
1571 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001572 bio_put(bio);
1573 }
1574
1575 return 0;
1576}
1577
Stefan Behrensff023aa2012-11-06 11:43:11 +01001578static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1579{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001580 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001581 int page_num;
1582
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001583 /*
1584 * This block is used for the check of the parity on the source device,
1585 * so the data needn't be written into the destination device.
1586 */
1587 if (sblock->sparity)
1588 return;
1589
Stefan Behrensff023aa2012-11-06 11:43:11 +01001590 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1591 int ret;
1592
1593 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1594 if (ret)
1595 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001596 &fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001597 }
1598}
1599
1600static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1601 int page_num)
1602{
1603 struct scrub_page *spage = sblock->pagev[page_num];
1604
1605 BUG_ON(spage->page == NULL);
1606 if (spage->io_error) {
1607 void *mapped_buffer = kmap_atomic(spage->page);
1608
David Sterba619a9742017-03-29 20:48:44 +02001609 clear_page(mapped_buffer);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001610 flush_dcache_page(spage->page);
1611 kunmap_atomic(mapped_buffer);
1612 }
1613 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1614}
1615
1616static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1617 struct scrub_page *spage)
1618{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001619 struct scrub_bio *sbio;
1620 int ret;
1621
David Sterba3fb99302017-05-16 19:10:32 +02001622 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001623again:
David Sterba3fb99302017-05-16 19:10:32 +02001624 if (!sctx->wr_curr_bio) {
1625 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001626 GFP_KERNEL);
David Sterba3fb99302017-05-16 19:10:32 +02001627 if (!sctx->wr_curr_bio) {
1628 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001629 return -ENOMEM;
1630 }
David Sterba3fb99302017-05-16 19:10:32 +02001631 sctx->wr_curr_bio->sctx = sctx;
1632 sctx->wr_curr_bio->page_count = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001633 }
David Sterba3fb99302017-05-16 19:10:32 +02001634 sbio = sctx->wr_curr_bio;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001635 if (sbio->page_count == 0) {
1636 struct bio *bio;
1637
1638 sbio->physical = spage->physical_for_dev_replace;
1639 sbio->logical = spage->logical;
David Sterba3fb99302017-05-16 19:10:32 +02001640 sbio->dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001641 bio = sbio->bio;
1642 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02001643 bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001644 sbio->bio = bio;
1645 }
1646
1647 bio->bi_private = sbio;
1648 bio->bi_end_io = scrub_wr_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001649 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001650 bio->bi_iter.bi_sector = sbio->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02001651 bio->bi_opf = REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001652 sbio->status = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001653 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1654 spage->physical_for_dev_replace ||
1655 sbio->logical + sbio->page_count * PAGE_SIZE !=
1656 spage->logical) {
1657 scrub_wr_submit(sctx);
1658 goto again;
1659 }
1660
1661 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1662 if (ret != PAGE_SIZE) {
1663 if (sbio->page_count < 1) {
1664 bio_put(sbio->bio);
1665 sbio->bio = NULL;
David Sterba3fb99302017-05-16 19:10:32 +02001666 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001667 return -EIO;
1668 }
1669 scrub_wr_submit(sctx);
1670 goto again;
1671 }
1672
1673 sbio->pagev[sbio->page_count] = spage;
1674 scrub_page_get(spage);
1675 sbio->page_count++;
David Sterba3fb99302017-05-16 19:10:32 +02001676 if (sbio->page_count == sctx->pages_per_wr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001677 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02001678 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001679
1680 return 0;
1681}
1682
1683static void scrub_wr_submit(struct scrub_ctx *sctx)
1684{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001685 struct scrub_bio *sbio;
1686
David Sterba3fb99302017-05-16 19:10:32 +02001687 if (!sctx->wr_curr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001688 return;
1689
David Sterba3fb99302017-05-16 19:10:32 +02001690 sbio = sctx->wr_curr_bio;
1691 sctx->wr_curr_bio = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001692 WARN_ON(!sbio->bio->bi_disk);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001693 scrub_pending_bio_inc(sctx);
1694 /* process all writes in a single worker thread. Then the block layer
1695 * orders the requests before sending them to the driver which
1696 * doubled the write performance on spinning disks when measured
1697 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001698 btrfsic_submit_bio(sbio->bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001699}
1700
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001701static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001702{
1703 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001704 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001705
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001706 sbio->status = bio->bi_status;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001707 sbio->bio = bio;
1708
Liu Bo9e0af232014-08-15 23:36:53 +08001709 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1710 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001711 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001712}
1713
1714static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1715{
1716 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1717 struct scrub_ctx *sctx = sbio->sctx;
1718 int i;
1719
1720 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001721 if (sbio->status) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001722 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001723 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001724
1725 for (i = 0; i < sbio->page_count; i++) {
1726 struct scrub_page *spage = sbio->pagev[i];
1727
1728 spage->io_error = 1;
1729 btrfs_dev_replace_stats_inc(&dev_replace->
1730 num_write_errors);
1731 }
1732 }
1733
1734 for (i = 0; i < sbio->page_count; i++)
1735 scrub_page_put(sbio->pagev[i]);
1736
1737 bio_put(sbio->bio);
1738 kfree(sbio);
1739 scrub_pending_bio_dec(sctx);
1740}
1741
1742static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001743{
1744 u64 flags;
1745 int ret;
1746
Zhao Leiba7cf982015-08-24 21:18:02 +08001747 /*
1748 * No need to initialize these stats currently,
1749 * because this function only use return value
1750 * instead of these stats value.
1751 *
1752 * Todo:
1753 * always use stats
1754 */
1755 sblock->header_error = 0;
1756 sblock->generation_error = 0;
1757 sblock->checksum_error = 0;
1758
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001759 WARN_ON(sblock->page_count < 1);
1760 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001761 ret = 0;
1762 if (flags & BTRFS_EXTENT_FLAG_DATA)
1763 ret = scrub_checksum_data(sblock);
1764 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1765 ret = scrub_checksum_tree_block(sblock);
1766 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1767 (void)scrub_checksum_super(sblock);
1768 else
1769 WARN_ON(1);
1770 if (ret)
1771 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001772
1773 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001774}
1775
1776static int scrub_checksum_data(struct scrub_block *sblock)
1777{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001778 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001779 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001780 u8 *on_disk_csum;
1781 struct page *page;
1782 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001783 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001784 u64 len;
1785 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001786
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001787 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001788 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001789 return 0;
1790
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001791 on_disk_csum = sblock->pagev[0]->csum;
1792 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001793 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001794
David Sterba25cc1222017-05-16 19:10:41 +02001795 len = sctx->fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001796 index = 0;
1797 for (;;) {
1798 u64 l = min_t(u64, len, PAGE_SIZE);
1799
Liu Bob0496682013-03-14 14:57:45 +00001800 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001801 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001802 len -= l;
1803 if (len == 0)
1804 break;
1805 index++;
1806 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001807 BUG_ON(!sblock->pagev[index]->page);
1808 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001809 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001810 }
1811
Arne Jansena2de7332011-03-08 14:14:00 +01001812 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001813 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001814 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001815
Zhao Leiba7cf982015-08-24 21:18:02 +08001816 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001817}
1818
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001819static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001820{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001821 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001822 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001823 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001824 u8 calculated_csum[BTRFS_CSUM_SIZE];
1825 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1826 struct page *page;
1827 void *mapped_buffer;
1828 u64 mapped_size;
1829 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001830 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001831 u64 len;
1832 int index;
1833
1834 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001835 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001836 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001837 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001838 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001839
1840 /*
1841 * we don't use the getter functions here, as we
1842 * a) don't have an extent buffer and
1843 * b) the page is already kmapped
1844 */
Qu Wenruo3cae2102013-07-16 11:19:18 +08001845 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08001846 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001847
Zhao Leiba7cf982015-08-24 21:18:02 +08001848 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
1849 sblock->header_error = 1;
1850 sblock->generation_error = 1;
1851 }
Arne Jansena2de7332011-03-08 14:14:00 +01001852
Miao Xie17a9be22014-07-24 11:37:08 +08001853 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Zhao Leiba7cf982015-08-24 21:18:02 +08001854 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001855
1856 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1857 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08001858 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001859
David Sterba25cc1222017-05-16 19:10:41 +02001860 len = sctx->fs_info->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001861 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1862 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1863 index = 0;
1864 for (;;) {
1865 u64 l = min_t(u64, len, mapped_size);
1866
Liu Bob0496682013-03-14 14:57:45 +00001867 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001868 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001869 len -= l;
1870 if (len == 0)
1871 break;
1872 index++;
1873 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001874 BUG_ON(!sblock->pagev[index]->page);
1875 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001876 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001877 mapped_size = PAGE_SIZE;
1878 p = mapped_buffer;
1879 }
1880
1881 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001882 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001883 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001884
Zhao Leiba7cf982015-08-24 21:18:02 +08001885 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001886}
1887
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001888static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001889{
1890 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001891 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001892 u8 calculated_csum[BTRFS_CSUM_SIZE];
1893 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1894 struct page *page;
1895 void *mapped_buffer;
1896 u64 mapped_size;
1897 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001898 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001899 int fail_gen = 0;
1900 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001901 u64 len;
1902 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001903
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001904 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001905 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001906 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001907 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001908 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001909
Qu Wenruo3cae2102013-07-16 11:19:18 +08001910 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001911 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001912
Qu Wenruo3cae2102013-07-16 11:19:18 +08001913 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001914 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001915
Miao Xie17a9be22014-07-24 11:37:08 +08001916 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001917 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001918
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001919 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1920 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1921 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1922 index = 0;
1923 for (;;) {
1924 u64 l = min_t(u64, len, mapped_size);
1925
Liu Bob0496682013-03-14 14:57:45 +00001926 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001927 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001928 len -= l;
1929 if (len == 0)
1930 break;
1931 index++;
1932 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001933 BUG_ON(!sblock->pagev[index]->page);
1934 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001935 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001936 mapped_size = PAGE_SIZE;
1937 p = mapped_buffer;
1938 }
1939
1940 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001941 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001942 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001943
Stefan Behrens442a4f62012-05-25 16:06:08 +02001944 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001945 /*
1946 * if we find an error in a super block, we just report it.
1947 * They will get written with the next transaction commit
1948 * anyway
1949 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001950 spin_lock(&sctx->stat_lock);
1951 ++sctx->stat.super_errors;
1952 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001953 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001954 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001955 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1956 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001957 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001958 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001959 }
1960
Stefan Behrens442a4f62012-05-25 16:06:08 +02001961 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001962}
1963
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001964static void scrub_block_get(struct scrub_block *sblock)
1965{
Elena Reshetova186debd2017-03-03 10:55:23 +02001966 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001967}
1968
1969static void scrub_block_put(struct scrub_block *sblock)
1970{
Elena Reshetova186debd2017-03-03 10:55:23 +02001971 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001972 int i;
1973
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001974 if (sblock->sparity)
1975 scrub_parity_put(sblock->sparity);
1976
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001977 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001978 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001979 kfree(sblock);
1980 }
1981}
1982
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001983static void scrub_page_get(struct scrub_page *spage)
1984{
Zhao Lei57019342015-01-20 15:11:45 +08001985 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001986}
1987
1988static void scrub_page_put(struct scrub_page *spage)
1989{
Zhao Lei57019342015-01-20 15:11:45 +08001990 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001991 if (spage->page)
1992 __free_page(spage->page);
1993 kfree(spage);
1994 }
1995}
1996
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001997static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01001998{
1999 struct scrub_bio *sbio;
2000
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002001 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002002 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002003
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002004 sbio = sctx->bios[sctx->curr];
2005 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002006 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002007 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002008}
2009
Stefan Behrensff023aa2012-11-06 11:43:11 +01002010static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2011 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002012{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002013 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002014 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002015 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002016
2017again:
2018 /*
2019 * grab a fresh bio or wait for one to become available
2020 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002021 while (sctx->curr == -1) {
2022 spin_lock(&sctx->list_lock);
2023 sctx->curr = sctx->first_free;
2024 if (sctx->curr != -1) {
2025 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2026 sctx->bios[sctx->curr]->next_free = -1;
2027 sctx->bios[sctx->curr]->page_count = 0;
2028 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002029 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002030 spin_unlock(&sctx->list_lock);
2031 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002032 }
2033 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002034 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002035 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002036 struct bio *bio;
2037
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002038 sbio->physical = spage->physical;
2039 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002040 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002041 bio = sbio->bio;
2042 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02002043 bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002044 sbio->bio = bio;
2045 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002046
2047 bio->bi_private = sbio;
2048 bio->bi_end_io = scrub_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002049 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002050 bio->bi_iter.bi_sector = sbio->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02002051 bio->bi_opf = REQ_OP_READ;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002052 sbio->status = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002053 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2054 spage->physical ||
2055 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002056 spage->logical ||
2057 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002058 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002059 goto again;
2060 }
2061
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002062 sbio->pagev[sbio->page_count] = spage;
2063 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2064 if (ret != PAGE_SIZE) {
2065 if (sbio->page_count < 1) {
2066 bio_put(sbio->bio);
2067 sbio->bio = NULL;
2068 return -EIO;
2069 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002070 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002071 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002072 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002073
Stefan Behrensff023aa2012-11-06 11:43:11 +01002074 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002075 atomic_inc(&sblock->outstanding_pages);
2076 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002077 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002078 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002079
2080 return 0;
2081}
2082
Linus Torvalds22365972015-09-05 15:14:43 -07002083static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002084{
2085 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002086 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002087
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002088 if (bio->bi_status)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002089 sblock->no_io_error_seen = 0;
2090
Scott Talbert46732722016-05-09 09:14:28 -04002091 bio_put(bio);
2092
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002093 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2094}
2095
2096static void scrub_missing_raid56_worker(struct btrfs_work *work)
2097{
2098 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2099 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002100 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002101 u64 logical;
2102 struct btrfs_device *dev;
2103
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002104 logical = sblock->pagev[0]->logical;
2105 dev = sblock->pagev[0]->dev;
2106
Zhao Leiaffe4a52015-08-24 21:32:06 +08002107 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002108 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002109
2110 if (!sblock->no_io_error_seen) {
2111 spin_lock(&sctx->stat_lock);
2112 sctx->stat.read_errors++;
2113 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002114 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002115 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002116 logical, rcu_str_deref(dev->name));
2117 } else if (sblock->header_error || sblock->checksum_error) {
2118 spin_lock(&sctx->stat_lock);
2119 sctx->stat.uncorrectable_errors++;
2120 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002121 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002122 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002123 logical, rcu_str_deref(dev->name));
2124 } else {
2125 scrub_write_block_to_dev_replace(sblock);
2126 }
2127
2128 scrub_block_put(sblock);
2129
David Sterba2073c4c2017-03-31 17:12:51 +02002130 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002131 mutex_lock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002132 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002133 mutex_unlock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002134 }
2135
2136 scrub_pending_bio_dec(sctx);
2137}
2138
2139static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2140{
2141 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002142 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002143 u64 length = sblock->page_count * PAGE_SIZE;
2144 u64 logical = sblock->pagev[0]->logical;
Zhao Leif1fee652016-05-17 17:37:38 +08002145 struct btrfs_bio *bbio = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002146 struct bio *bio;
2147 struct btrfs_raid_bio *rbio;
2148 int ret;
2149 int i;
2150
Qu Wenruoae6529c2017-03-29 09:33:21 +08002151 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002152 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
David Sterba825ad4c2017-03-28 14:45:22 +02002153 &length, &bbio);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002154 if (ret || !bbio || !bbio->raid_map)
2155 goto bbio_out;
2156
2157 if (WARN_ON(!sctx->is_dev_replace ||
2158 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2159 /*
2160 * We shouldn't be scrubbing a missing device. Even for dev
2161 * replace, we should only get here for RAID 5/6. We either
2162 * managed to mount something with no mirrors remaining or
2163 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2164 */
2165 goto bbio_out;
2166 }
2167
David Sterbac5e4c3d2017-06-12 17:29:41 +02002168 bio = btrfs_io_bio_alloc(0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002169 bio->bi_iter.bi_sector = logical >> 9;
2170 bio->bi_private = sblock;
2171 bio->bi_end_io = scrub_missing_raid56_end_io;
2172
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002173 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002174 if (!rbio)
2175 goto rbio_out;
2176
2177 for (i = 0; i < sblock->page_count; i++) {
2178 struct scrub_page *spage = sblock->pagev[i];
2179
2180 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2181 }
2182
2183 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2184 scrub_missing_raid56_worker, NULL, NULL);
2185 scrub_block_get(sblock);
2186 scrub_pending_bio_inc(sctx);
2187 raid56_submit_missing_rbio(rbio);
2188 return;
2189
2190rbio_out:
2191 bio_put(bio);
2192bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002193 btrfs_bio_counter_dec(fs_info);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002194 btrfs_put_bbio(bbio);
2195 spin_lock(&sctx->stat_lock);
2196 sctx->stat.malloc_errors++;
2197 spin_unlock(&sctx->stat_lock);
2198}
2199
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002200static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002201 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002202 u64 gen, int mirror_num, u8 *csum, int force,
2203 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002204{
2205 struct scrub_block *sblock;
2206 int index;
2207
David Sterba58c4e172016-02-11 10:49:42 +01002208 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002209 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002210 spin_lock(&sctx->stat_lock);
2211 sctx->stat.malloc_errors++;
2212 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002213 return -ENOMEM;
2214 }
2215
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002216 /* one ref inside this function, plus one for each page added to
2217 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002218 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002219 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002220 sblock->no_io_error_seen = 1;
2221
2222 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002223 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002224 u64 l = min_t(u64, len, PAGE_SIZE);
2225
David Sterba58c4e172016-02-11 10:49:42 +01002226 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002227 if (!spage) {
2228leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002229 spin_lock(&sctx->stat_lock);
2230 sctx->stat.malloc_errors++;
2231 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002232 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002233 return -ENOMEM;
2234 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002235 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2236 scrub_page_get(spage);
2237 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002238 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002239 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002240 spage->flags = flags;
2241 spage->generation = gen;
2242 spage->logical = logical;
2243 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002244 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002245 spage->mirror_num = mirror_num;
2246 if (csum) {
2247 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002248 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002249 } else {
2250 spage->have_csum = 0;
2251 }
2252 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002253 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002254 if (!spage->page)
2255 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002256 len -= l;
2257 logical += l;
2258 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002259 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002260 }
2261
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002262 WARN_ON(sblock->page_count == 0);
Anand Jaine6e674b2017-12-04 12:54:54 +08002263 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002264 /*
2265 * This case should only be hit for RAID 5/6 device replace. See
2266 * the comment in scrub_missing_raid56_pages() for details.
2267 */
2268 scrub_missing_raid56_pages(sblock);
2269 } else {
2270 for (index = 0; index < sblock->page_count; index++) {
2271 struct scrub_page *spage = sblock->pagev[index];
2272 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002273
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002274 ret = scrub_add_page_to_rd_bio(sctx, spage);
2275 if (ret) {
2276 scrub_block_put(sblock);
2277 return ret;
2278 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002279 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002280
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002281 if (force)
2282 scrub_submit(sctx);
2283 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002284
2285 /* last one frees, either here or in bio completion for last page */
2286 scrub_block_put(sblock);
2287 return 0;
2288}
2289
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002290static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002291{
2292 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002293 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002294
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002295 sbio->status = bio->bi_status;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002296 sbio->bio = bio;
2297
Qu Wenruo0339ef22014-02-28 10:46:17 +08002298 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002299}
2300
2301static void scrub_bio_end_io_worker(struct btrfs_work *work)
2302{
2303 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002304 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002305 int i;
2306
Stefan Behrensff023aa2012-11-06 11:43:11 +01002307 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002308 if (sbio->status) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002309 for (i = 0; i < sbio->page_count; i++) {
2310 struct scrub_page *spage = sbio->pagev[i];
2311
2312 spage->io_error = 1;
2313 spage->sblock->no_io_error_seen = 0;
2314 }
2315 }
2316
2317 /* now complete the scrub_block items that have all pages completed */
2318 for (i = 0; i < sbio->page_count; i++) {
2319 struct scrub_page *spage = sbio->pagev[i];
2320 struct scrub_block *sblock = spage->sblock;
2321
2322 if (atomic_dec_and_test(&sblock->outstanding_pages))
2323 scrub_block_complete(sblock);
2324 scrub_block_put(sblock);
2325 }
2326
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002327 bio_put(sbio->bio);
2328 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002329 spin_lock(&sctx->list_lock);
2330 sbio->next_free = sctx->first_free;
2331 sctx->first_free = sbio->index;
2332 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002333
David Sterba2073c4c2017-03-31 17:12:51 +02002334 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002335 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002336 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002337 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002338 }
2339
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002340 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002341}
2342
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002343static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2344 unsigned long *bitmap,
2345 u64 start, u64 len)
2346{
Liu Bo972d7212017-04-03 13:45:33 -07002347 u64 offset;
David Sterba7736b0a2017-03-31 18:02:48 +02002348 u64 nsectors64;
2349 u32 nsectors;
Jeff Mahoneyda170662016-06-15 09:22:56 -04002350 int sectorsize = sparity->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002351
2352 if (len >= sparity->stripe_len) {
2353 bitmap_set(bitmap, 0, sparity->nsectors);
2354 return;
2355 }
2356
2357 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002358 start = div64_u64_rem(start, sparity->stripe_len, &offset);
2359 offset = div_u64(offset, sectorsize);
David Sterba7736b0a2017-03-31 18:02:48 +02002360 nsectors64 = div_u64(len, sectorsize);
2361
2362 ASSERT(nsectors64 < UINT_MAX);
2363 nsectors = (u32)nsectors64;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002364
2365 if (offset + nsectors <= sparity->nsectors) {
2366 bitmap_set(bitmap, offset, nsectors);
2367 return;
2368 }
2369
2370 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2371 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2372}
2373
2374static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2375 u64 start, u64 len)
2376{
2377 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2378}
2379
2380static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2381 u64 start, u64 len)
2382{
2383 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2384}
2385
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002386static void scrub_block_complete(struct scrub_block *sblock)
2387{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002388 int corrupted = 0;
2389
Stefan Behrensff023aa2012-11-06 11:43:11 +01002390 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002391 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002392 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002393 } else {
2394 /*
2395 * if has checksum error, write via repair mechanism in
2396 * dev replace case, otherwise write here in dev replace
2397 * case.
2398 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002399 corrupted = scrub_checksum(sblock);
2400 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002401 scrub_write_block_to_dev_replace(sblock);
2402 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002403
2404 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2405 u64 start = sblock->pagev[0]->logical;
2406 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2407 PAGE_SIZE;
2408
2409 scrub_parity_mark_sectors_error(sblock->sparity,
2410 start, end - start);
2411 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002412}
2413
Zhao Lei3b5753e2015-08-24 22:03:02 +08002414static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002415{
2416 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002417 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002418 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002419
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002420 while (!list_empty(&sctx->csum_list)) {
2421 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002422 struct btrfs_ordered_sum, list);
2423 if (sum->bytenr > logical)
2424 return 0;
2425 if (sum->bytenr + sum->len > logical)
2426 break;
2427
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002428 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002429 list_del(&sum->list);
2430 kfree(sum);
2431 sum = NULL;
2432 }
2433 if (!sum)
2434 return 0;
2435
David Sterba1d1bf922017-03-31 18:02:48 +02002436 index = div_u64(logical - sum->bytenr, sctx->fs_info->sectorsize);
2437 ASSERT(index < UINT_MAX);
2438
David Sterba25cc1222017-05-16 19:10:41 +02002439 num_sectors = sum->len / sctx->fs_info->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002440 memcpy(csum, sum->sums + index, sctx->csum_size);
2441 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002442 list_del(&sum->list);
2443 kfree(sum);
2444 }
Miao Xief51a4a12013-06-19 10:36:09 +08002445 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002446}
2447
2448/* scrub extent tries to collect up to 64 kB for each bio */
Liu Bo6ca17652018-03-07 12:08:09 -07002449static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map,
2450 u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002451 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002452 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002453{
2454 int ret;
2455 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002456 u32 blocksize;
2457
2458 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002459 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2460 blocksize = map->stripe_len;
2461 else
2462 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002463 spin_lock(&sctx->stat_lock);
2464 sctx->stat.data_extents_scrubbed++;
2465 sctx->stat.data_bytes_scrubbed += len;
2466 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002467 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002468 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2469 blocksize = map->stripe_len;
2470 else
2471 blocksize = sctx->fs_info->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002472 spin_lock(&sctx->stat_lock);
2473 sctx->stat.tree_extents_scrubbed++;
2474 sctx->stat.tree_bytes_scrubbed += len;
2475 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002476 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002477 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002478 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002479 }
Arne Jansena2de7332011-03-08 14:14:00 +01002480
2481 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002482 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002483 int have_csum = 0;
2484
2485 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2486 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002487 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002488 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002489 ++sctx->stat.no_csum;
Arne Jansena2de7332011-03-08 14:14:00 +01002490 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002491 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002492 mirror_num, have_csum ? csum : NULL, 0,
2493 physical_for_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002494 if (ret)
2495 return ret;
2496 len -= l;
2497 logical += l;
2498 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002499 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002500 }
2501 return 0;
2502}
2503
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002504static int scrub_pages_for_parity(struct scrub_parity *sparity,
2505 u64 logical, u64 len,
2506 u64 physical, struct btrfs_device *dev,
2507 u64 flags, u64 gen, int mirror_num, u8 *csum)
2508{
2509 struct scrub_ctx *sctx = sparity->sctx;
2510 struct scrub_block *sblock;
2511 int index;
2512
David Sterba58c4e172016-02-11 10:49:42 +01002513 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002514 if (!sblock) {
2515 spin_lock(&sctx->stat_lock);
2516 sctx->stat.malloc_errors++;
2517 spin_unlock(&sctx->stat_lock);
2518 return -ENOMEM;
2519 }
2520
2521 /* one ref inside this function, plus one for each page added to
2522 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002523 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002524 sblock->sctx = sctx;
2525 sblock->no_io_error_seen = 1;
2526 sblock->sparity = sparity;
2527 scrub_parity_get(sparity);
2528
2529 for (index = 0; len > 0; index++) {
2530 struct scrub_page *spage;
2531 u64 l = min_t(u64, len, PAGE_SIZE);
2532
David Sterba58c4e172016-02-11 10:49:42 +01002533 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002534 if (!spage) {
2535leave_nomem:
2536 spin_lock(&sctx->stat_lock);
2537 sctx->stat.malloc_errors++;
2538 spin_unlock(&sctx->stat_lock);
2539 scrub_block_put(sblock);
2540 return -ENOMEM;
2541 }
2542 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2543 /* For scrub block */
2544 scrub_page_get(spage);
2545 sblock->pagev[index] = spage;
2546 /* For scrub parity */
2547 scrub_page_get(spage);
2548 list_add_tail(&spage->list, &sparity->spages);
2549 spage->sblock = sblock;
2550 spage->dev = dev;
2551 spage->flags = flags;
2552 spage->generation = gen;
2553 spage->logical = logical;
2554 spage->physical = physical;
2555 spage->mirror_num = mirror_num;
2556 if (csum) {
2557 spage->have_csum = 1;
2558 memcpy(spage->csum, csum, sctx->csum_size);
2559 } else {
2560 spage->have_csum = 0;
2561 }
2562 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002563 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002564 if (!spage->page)
2565 goto leave_nomem;
2566 len -= l;
2567 logical += l;
2568 physical += l;
2569 }
2570
2571 WARN_ON(sblock->page_count == 0);
2572 for (index = 0; index < sblock->page_count; index++) {
2573 struct scrub_page *spage = sblock->pagev[index];
2574 int ret;
2575
2576 ret = scrub_add_page_to_rd_bio(sctx, spage);
2577 if (ret) {
2578 scrub_block_put(sblock);
2579 return ret;
2580 }
2581 }
2582
2583 /* last one frees, either here or in bio completion for last page */
2584 scrub_block_put(sblock);
2585 return 0;
2586}
2587
2588static int scrub_extent_for_parity(struct scrub_parity *sparity,
2589 u64 logical, u64 len,
2590 u64 physical, struct btrfs_device *dev,
2591 u64 flags, u64 gen, int mirror_num)
2592{
2593 struct scrub_ctx *sctx = sparity->sctx;
2594 int ret;
2595 u8 csum[BTRFS_CSUM_SIZE];
2596 u32 blocksize;
2597
Anand Jaine6e674b2017-12-04 12:54:54 +08002598 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval4a770892015-06-19 11:52:52 -07002599 scrub_parity_mark_sectors_error(sparity, logical, len);
2600 return 0;
2601 }
2602
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002603 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002604 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002605 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002606 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002607 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002608 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002609 WARN_ON(1);
2610 }
2611
2612 while (len) {
2613 u64 l = min_t(u64, len, blocksize);
2614 int have_csum = 0;
2615
2616 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2617 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002618 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002619 if (have_csum == 0)
2620 goto skip;
2621 }
2622 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2623 flags, gen, mirror_num,
2624 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002625 if (ret)
2626 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002627skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002628 len -= l;
2629 logical += l;
2630 physical += l;
2631 }
2632 return 0;
2633}
2634
Wang Shilong3b080b22014-04-01 18:01:43 +08002635/*
2636 * Given a physical address, this will calculate it's
2637 * logical offset. if this is a parity stripe, it will return
2638 * the most left data stripe's logical offset.
2639 *
2640 * return 0 if it is a data stripe, 1 means parity stripe.
2641 */
2642static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002643 struct map_lookup *map, u64 *offset,
2644 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002645{
2646 int i;
2647 int j = 0;
2648 u64 stripe_nr;
2649 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002650 u32 stripe_index;
2651 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002652
2653 last_offset = (physical - map->stripes[num].physical) *
2654 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002655 if (stripe_start)
2656 *stripe_start = last_offset;
2657
Wang Shilong3b080b22014-04-01 18:01:43 +08002658 *offset = last_offset;
2659 for (i = 0; i < nr_data_stripes(map); i++) {
2660 *offset = last_offset + i * map->stripe_len;
2661
Liu Bo42c61ab2017-04-03 13:45:24 -07002662 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01002663 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002664
2665 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002666 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002667 /* calculate which stripe this data locates */
2668 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002669 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002670 if (stripe_index == num)
2671 return 0;
2672 if (stripe_index < num)
2673 j++;
2674 }
2675 *offset = last_offset + j * map->stripe_len;
2676 return 1;
2677}
2678
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002679static void scrub_free_parity(struct scrub_parity *sparity)
2680{
2681 struct scrub_ctx *sctx = sparity->sctx;
2682 struct scrub_page *curr, *next;
2683 int nbits;
2684
2685 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2686 if (nbits) {
2687 spin_lock(&sctx->stat_lock);
2688 sctx->stat.read_errors += nbits;
2689 sctx->stat.uncorrectable_errors += nbits;
2690 spin_unlock(&sctx->stat_lock);
2691 }
2692
2693 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2694 list_del_init(&curr->list);
2695 scrub_page_put(curr);
2696 }
2697
2698 kfree(sparity);
2699}
2700
Zhao Lei20b2e302015-06-04 20:09:15 +08002701static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2702{
2703 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2704 work);
2705 struct scrub_ctx *sctx = sparity->sctx;
2706
2707 scrub_free_parity(sparity);
2708 scrub_pending_bio_dec(sctx);
2709}
2710
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002711static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002712{
2713 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002714 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002715
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002716 if (bio->bi_status)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002717 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2718 sparity->nsectors);
2719
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002720 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002721
2722 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2723 scrub_parity_bio_endio_worker, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002724 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002725}
2726
2727static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2728{
2729 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002730 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002731 struct bio *bio;
2732 struct btrfs_raid_bio *rbio;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002733 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002734 u64 length;
2735 int ret;
2736
2737 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2738 sparity->nsectors))
2739 goto out;
2740
Zhao Leia0dd59d2015-07-21 15:42:26 +08002741 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08002742
2743 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002744 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
David Sterba825ad4c2017-03-28 14:45:22 +02002745 &length, &bbio);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002746 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002747 goto bbio_out;
2748
David Sterbac5e4c3d2017-06-12 17:29:41 +02002749 bio = btrfs_io_bio_alloc(0);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002750 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2751 bio->bi_private = sparity;
2752 bio->bi_end_io = scrub_parity_bio_endio;
2753
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002754 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002755 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002756 sparity->dbitmap,
2757 sparity->nsectors);
2758 if (!rbio)
2759 goto rbio_out;
2760
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002761 scrub_pending_bio_inc(sctx);
2762 raid56_parity_submit_scrub_rbio(rbio);
2763 return;
2764
2765rbio_out:
2766 bio_put(bio);
2767bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002768 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08002769 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002770 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2771 sparity->nsectors);
2772 spin_lock(&sctx->stat_lock);
2773 sctx->stat.malloc_errors++;
2774 spin_unlock(&sctx->stat_lock);
2775out:
2776 scrub_free_parity(sparity);
2777}
2778
2779static inline int scrub_calc_parity_bitmap_len(int nsectors)
2780{
Zhao Leibfca9a62014-12-08 19:55:57 +08002781 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002782}
2783
2784static void scrub_parity_get(struct scrub_parity *sparity)
2785{
Elena Reshetova78a76452017-03-03 10:55:24 +02002786 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002787}
2788
2789static void scrub_parity_put(struct scrub_parity *sparity)
2790{
Elena Reshetova78a76452017-03-03 10:55:24 +02002791 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002792 return;
2793
2794 scrub_parity_check_and_repair(sparity);
2795}
2796
2797static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2798 struct map_lookup *map,
2799 struct btrfs_device *sdev,
2800 struct btrfs_path *path,
2801 u64 logic_start,
2802 u64 logic_end)
2803{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002804 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002805 struct btrfs_root *root = fs_info->extent_root;
2806 struct btrfs_root *csum_root = fs_info->csum_root;
2807 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07002808 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002809 u64 flags;
2810 int ret;
2811 int slot;
2812 struct extent_buffer *l;
2813 struct btrfs_key key;
2814 u64 generation;
2815 u64 extent_logical;
2816 u64 extent_physical;
2817 u64 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07002818 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002819 struct btrfs_device *extent_dev;
2820 struct scrub_parity *sparity;
2821 int nsectors;
2822 int bitmap_len;
2823 int extent_mirror_num;
2824 int stop_loop = 0;
2825
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002826 nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002827 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2828 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2829 GFP_NOFS);
2830 if (!sparity) {
2831 spin_lock(&sctx->stat_lock);
2832 sctx->stat.malloc_errors++;
2833 spin_unlock(&sctx->stat_lock);
2834 return -ENOMEM;
2835 }
2836
2837 sparity->stripe_len = map->stripe_len;
2838 sparity->nsectors = nsectors;
2839 sparity->sctx = sctx;
2840 sparity->scrub_dev = sdev;
2841 sparity->logic_start = logic_start;
2842 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02002843 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002844 INIT_LIST_HEAD(&sparity->spages);
2845 sparity->dbitmap = sparity->bitmap;
2846 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2847
2848 ret = 0;
2849 while (logic_start < logic_end) {
2850 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2851 key.type = BTRFS_METADATA_ITEM_KEY;
2852 else
2853 key.type = BTRFS_EXTENT_ITEM_KEY;
2854 key.objectid = logic_start;
2855 key.offset = (u64)-1;
2856
2857 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2858 if (ret < 0)
2859 goto out;
2860
2861 if (ret > 0) {
2862 ret = btrfs_previous_extent_item(root, path, 0);
2863 if (ret < 0)
2864 goto out;
2865 if (ret > 0) {
2866 btrfs_release_path(path);
2867 ret = btrfs_search_slot(NULL, root, &key,
2868 path, 0, 0);
2869 if (ret < 0)
2870 goto out;
2871 }
2872 }
2873
2874 stop_loop = 0;
2875 while (1) {
2876 u64 bytes;
2877
2878 l = path->nodes[0];
2879 slot = path->slots[0];
2880 if (slot >= btrfs_header_nritems(l)) {
2881 ret = btrfs_next_leaf(root, path);
2882 if (ret == 0)
2883 continue;
2884 if (ret < 0)
2885 goto out;
2886
2887 stop_loop = 1;
2888 break;
2889 }
2890 btrfs_item_key_to_cpu(l, &key, slot);
2891
Zhao Leid7cad232015-07-22 13:14:48 +08002892 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2893 key.type != BTRFS_METADATA_ITEM_KEY)
2894 goto next;
2895
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002896 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002897 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002898 else
2899 bytes = key.offset;
2900
2901 if (key.objectid + bytes <= logic_start)
2902 goto next;
2903
Zhao Leia0dd59d2015-07-21 15:42:26 +08002904 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002905 stop_loop = 1;
2906 break;
2907 }
2908
2909 while (key.objectid >= logic_start + map->stripe_len)
2910 logic_start += map->stripe_len;
2911
2912 extent = btrfs_item_ptr(l, slot,
2913 struct btrfs_extent_item);
2914 flags = btrfs_extent_flags(l, extent);
2915 generation = btrfs_extent_generation(l, extent);
2916
Zhao Leia323e812015-07-23 12:29:49 +08002917 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
2918 (key.objectid < logic_start ||
2919 key.objectid + bytes >
2920 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002921 btrfs_err(fs_info,
2922 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08002923 key.objectid, logic_start);
Zhao Lei9799d2c32015-08-25 21:31:40 +08002924 spin_lock(&sctx->stat_lock);
2925 sctx->stat.uncorrectable_errors++;
2926 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002927 goto next;
2928 }
2929again:
2930 extent_logical = key.objectid;
2931 extent_len = bytes;
2932
2933 if (extent_logical < logic_start) {
2934 extent_len -= logic_start - extent_logical;
2935 extent_logical = logic_start;
2936 }
2937
2938 if (extent_logical + extent_len >
2939 logic_start + map->stripe_len)
2940 extent_len = logic_start + map->stripe_len -
2941 extent_logical;
2942
2943 scrub_parity_mark_sectors_data(sparity, extent_logical,
2944 extent_len);
2945
Omar Sandoval4a770892015-06-19 11:52:52 -07002946 mapped_length = extent_len;
Zhao Leif1fee652016-05-17 17:37:38 +08002947 bbio = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002948 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
2949 extent_logical, &mapped_length, &bbio,
2950 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07002951 if (!ret) {
2952 if (!bbio || mapped_length < extent_len)
2953 ret = -EIO;
2954 }
2955 if (ret) {
2956 btrfs_put_bbio(bbio);
2957 goto out;
2958 }
2959 extent_physical = bbio->stripes[0].physical;
2960 extent_mirror_num = bbio->mirror_num;
2961 extent_dev = bbio->stripes[0].dev;
2962 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002963
2964 ret = btrfs_lookup_csums_range(csum_root,
2965 extent_logical,
2966 extent_logical + extent_len - 1,
2967 &sctx->csum_list, 1);
2968 if (ret)
2969 goto out;
2970
2971 ret = scrub_extent_for_parity(sparity, extent_logical,
2972 extent_len,
2973 extent_physical,
2974 extent_dev, flags,
2975 generation,
2976 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08002977
2978 scrub_free_csums(sctx);
2979
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002980 if (ret)
2981 goto out;
2982
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002983 if (extent_logical + extent_len <
2984 key.objectid + bytes) {
2985 logic_start += map->stripe_len;
2986
2987 if (logic_start >= logic_end) {
2988 stop_loop = 1;
2989 break;
2990 }
2991
2992 if (logic_start < key.objectid + bytes) {
2993 cond_resched();
2994 goto again;
2995 }
2996 }
2997next:
2998 path->slots[0]++;
2999 }
3000
3001 btrfs_release_path(path);
3002
3003 if (stop_loop)
3004 break;
3005
3006 logic_start += map->stripe_len;
3007 }
3008out:
3009 if (ret < 0)
3010 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003011 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003012 scrub_parity_put(sparity);
3013 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003014 mutex_lock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003015 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003016 mutex_unlock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003017
3018 btrfs_release_path(path);
3019 return ret < 0 ? ret : 0;
3020}
3021
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003022static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003023 struct map_lookup *map,
3024 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003025 int num, u64 base, u64 length,
3026 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003027{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003028 struct btrfs_path *path, *ppath;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003029 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003030 struct btrfs_root *root = fs_info->extent_root;
3031 struct btrfs_root *csum_root = fs_info->csum_root;
3032 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003033 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003034 u64 flags;
3035 int ret;
3036 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003037 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003038 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003039 u64 physical;
3040 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003041 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003042 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003043 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003044 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003045 struct reada_control *reada1;
3046 struct reada_control *reada2;
David Sterbae6c11f92016-03-24 18:00:53 +01003047 struct btrfs_key key;
Arne Jansen7a262852011-06-10 12:39:23 +02003048 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003049 u64 increment = map->stripe_len;
3050 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003051 u64 extent_logical;
3052 u64 extent_physical;
3053 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003054 u64 stripe_logical;
3055 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003056 struct btrfs_device *extent_dev;
3057 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003058 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003059
Wang Shilong3b080b22014-04-01 18:01:43 +08003060 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003061 offset = 0;
Liu Bo42c61ab2017-04-03 13:45:24 -07003062 nstripes = div64_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003063 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3064 offset = map->stripe_len * num;
3065 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003066 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003067 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3068 int factor = map->num_stripes / map->sub_stripes;
3069 offset = map->stripe_len * (num / map->sub_stripes);
3070 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003071 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003072 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3073 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003074 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003075 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3076 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003077 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003078 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003079 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003080 increment = map->stripe_len * nr_data_stripes(map);
3081 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003082 } else {
3083 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003084 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003085 }
3086
3087 path = btrfs_alloc_path();
3088 if (!path)
3089 return -ENOMEM;
3090
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003091 ppath = btrfs_alloc_path();
3092 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003093 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003094 return -ENOMEM;
3095 }
3096
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003097 /*
3098 * work on commit root. The related disk blocks are static as
3099 * long as COW is applied. This means, it is save to rewrite
3100 * them to repair disk errors without any race conditions
3101 */
Arne Jansena2de7332011-03-08 14:14:00 +01003102 path->search_commit_root = 1;
3103 path->skip_locking = 1;
3104
Gui Hecheng063c54d2015-01-09 09:39:40 +08003105 ppath->search_commit_root = 1;
3106 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003107 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003108 * trigger the readahead for extent tree csum tree and wait for
3109 * completion. During readahead, the scrub is officially paused
3110 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003111 */
3112 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003113 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003114 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003115 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003116 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003117 logic_end += base;
3118 } else {
3119 logic_end = logical + increment * nstripes;
3120 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003121 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003122 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003123 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003124
Arne Jansen7a262852011-06-10 12:39:23 +02003125 /* FIXME it might be better to start readahead at commit root */
David Sterbae6c11f92016-03-24 18:00:53 +01003126 key.objectid = logical;
3127 key.type = BTRFS_EXTENT_ITEM_KEY;
3128 key.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003129 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003130 key_end.type = BTRFS_METADATA_ITEM_KEY;
3131 key_end.offset = (u64)-1;
David Sterbae6c11f92016-03-24 18:00:53 +01003132 reada1 = btrfs_reada_add(root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003133
David Sterbae6c11f92016-03-24 18:00:53 +01003134 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3135 key.type = BTRFS_EXTENT_CSUM_KEY;
3136 key.offset = logical;
Arne Jansen7a262852011-06-10 12:39:23 +02003137 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3138 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003139 key_end.offset = logic_end;
David Sterbae6c11f92016-03-24 18:00:53 +01003140 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003141
Arne Jansen7a262852011-06-10 12:39:23 +02003142 if (!IS_ERR(reada1))
3143 btrfs_reada_wait(reada1);
3144 if (!IS_ERR(reada2))
3145 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003146
Arne Jansena2de7332011-03-08 14:14:00 +01003147
3148 /*
3149 * collect all data csums for the stripe to avoid seeking during
3150 * the scrub. This might currently (crc32) end up to be about 1MB
3151 */
Arne Jansene7786c32011-05-28 20:58:38 +00003152 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003153
Arne Jansena2de7332011-03-08 14:14:00 +01003154 /*
3155 * now find all extents for each stripe and scrub them
3156 */
Arne Jansena2de7332011-03-08 14:14:00 +01003157 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003158 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003159 /*
3160 * canceled?
3161 */
3162 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003163 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003164 ret = -ECANCELED;
3165 goto out;
3166 }
3167 /*
3168 * check to see if we have to pause
3169 */
3170 if (atomic_read(&fs_info->scrub_pause_req)) {
3171 /* push queued extents */
David Sterba2073c4c2017-03-31 17:12:51 +02003172 sctx->flush_all_writes = true;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003173 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003174 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003175 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003176 mutex_unlock(&sctx->wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003177 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003178 atomic_read(&sctx->bios_in_flight) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003179 sctx->flush_all_writes = false;
Wang Shilong3cb09292013-12-04 21:15:19 +08003180 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003181 }
3182
Zhao Leif2f66a22015-07-21 12:22:29 +08003183 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3184 ret = get_raid56_logic_offset(physical, num, map,
3185 &logical,
3186 &stripe_logical);
3187 logical += base;
3188 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003189 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003190 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003191 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003192 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3193 ppath, stripe_logical,
3194 stripe_end);
3195 if (ret)
3196 goto out;
3197 goto skip;
3198 }
3199 }
3200
Wang Shilong7c76edb2014-01-12 21:38:32 +08003201 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3202 key.type = BTRFS_METADATA_ITEM_KEY;
3203 else
3204 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003205 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003206 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003207
3208 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3209 if (ret < 0)
3210 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003211
Arne Jansen8c510322011-06-03 10:09:26 +02003212 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003213 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003214 if (ret < 0)
3215 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003216 if (ret > 0) {
3217 /* there's no smaller item, so stick with the
3218 * larger one */
3219 btrfs_release_path(path);
3220 ret = btrfs_search_slot(NULL, root, &key,
3221 path, 0, 0);
3222 if (ret < 0)
3223 goto out;
3224 }
Arne Jansena2de7332011-03-08 14:14:00 +01003225 }
3226
Liu Bo625f1c8d2013-04-27 02:56:57 +00003227 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003228 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003229 u64 bytes;
3230
Arne Jansena2de7332011-03-08 14:14:00 +01003231 l = path->nodes[0];
3232 slot = path->slots[0];
3233 if (slot >= btrfs_header_nritems(l)) {
3234 ret = btrfs_next_leaf(root, path);
3235 if (ret == 0)
3236 continue;
3237 if (ret < 0)
3238 goto out;
3239
Liu Bo625f1c8d2013-04-27 02:56:57 +00003240 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003241 break;
3242 }
3243 btrfs_item_key_to_cpu(l, &key, slot);
3244
Zhao Leid7cad232015-07-22 13:14:48 +08003245 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3246 key.type != BTRFS_METADATA_ITEM_KEY)
3247 goto next;
3248
Josef Bacik3173a182013-03-07 14:22:04 -05003249 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003250 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003251 else
3252 bytes = key.offset;
3253
3254 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003255 goto next;
3256
Liu Bo625f1c8d2013-04-27 02:56:57 +00003257 if (key.objectid >= logical + map->stripe_len) {
3258 /* out of this device extent */
3259 if (key.objectid >= logic_end)
3260 stop_loop = 1;
3261 break;
3262 }
Arne Jansena2de7332011-03-08 14:14:00 +01003263
3264 extent = btrfs_item_ptr(l, slot,
3265 struct btrfs_extent_item);
3266 flags = btrfs_extent_flags(l, extent);
3267 generation = btrfs_extent_generation(l, extent);
3268
Zhao Leia323e812015-07-23 12:29:49 +08003269 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3270 (key.objectid < logical ||
3271 key.objectid + bytes >
3272 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003273 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003274 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003275 key.objectid, logical);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003276 spin_lock(&sctx->stat_lock);
3277 sctx->stat.uncorrectable_errors++;
3278 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003279 goto next;
3280 }
3281
Liu Bo625f1c8d2013-04-27 02:56:57 +00003282again:
3283 extent_logical = key.objectid;
3284 extent_len = bytes;
3285
Arne Jansena2de7332011-03-08 14:14:00 +01003286 /*
3287 * trim extent to this stripe
3288 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003289 if (extent_logical < logical) {
3290 extent_len -= logical - extent_logical;
3291 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003292 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003293 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003294 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003295 extent_len = logical + map->stripe_len -
3296 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003297 }
3298
Liu Bo625f1c8d2013-04-27 02:56:57 +00003299 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003300 extent_dev = scrub_dev;
3301 extent_mirror_num = mirror_num;
3302 if (is_dev_replace)
3303 scrub_remap_extent(fs_info, extent_logical,
3304 extent_len, &extent_physical,
3305 &extent_dev,
3306 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003307
Zhao Leife8cf652015-07-22 13:14:47 +08003308 ret = btrfs_lookup_csums_range(csum_root,
3309 extent_logical,
3310 extent_logical +
3311 extent_len - 1,
3312 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003313 if (ret)
3314 goto out;
3315
Liu Bo6ca17652018-03-07 12:08:09 -07003316 ret = scrub_extent(sctx, map, extent_logical, extent_len,
Liu Bo625f1c8d2013-04-27 02:56:57 +00003317 extent_physical, extent_dev, flags,
3318 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003319 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003320
3321 scrub_free_csums(sctx);
3322
Liu Bo625f1c8d2013-04-27 02:56:57 +00003323 if (ret)
3324 goto out;
3325
3326 if (extent_logical + extent_len <
3327 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003328 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003329 /*
3330 * loop until we find next data stripe
3331 * or we have finished all stripes.
3332 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003333loop:
3334 physical += map->stripe_len;
3335 ret = get_raid56_logic_offset(physical,
3336 num, map, &logical,
3337 &stripe_logical);
3338 logical += base;
3339
3340 if (ret && physical < physical_end) {
3341 stripe_logical += base;
3342 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003343 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003344 ret = scrub_raid56_parity(sctx,
3345 map, scrub_dev, ppath,
3346 stripe_logical,
3347 stripe_end);
3348 if (ret)
3349 goto out;
3350 goto loop;
3351 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003352 } else {
3353 physical += map->stripe_len;
3354 logical += increment;
3355 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003356 if (logical < key.objectid + bytes) {
3357 cond_resched();
3358 goto again;
3359 }
3360
Wang Shilong3b080b22014-04-01 18:01:43 +08003361 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003362 stop_loop = 1;
3363 break;
3364 }
3365 }
Arne Jansena2de7332011-03-08 14:14:00 +01003366next:
3367 path->slots[0]++;
3368 }
Chris Mason71267332011-05-23 06:30:52 -04003369 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003370skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003371 logical += increment;
3372 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003373 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003374 if (stop_loop)
3375 sctx->stat.last_physical = map->stripes[num].physical +
3376 length;
3377 else
3378 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003379 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003380 if (stop_loop)
3381 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003382 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003383out:
Arne Jansena2de7332011-03-08 14:14:00 +01003384 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003385 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003386 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003387 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003388 mutex_unlock(&sctx->wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003389
Arne Jansene7786c32011-05-28 20:58:38 +00003390 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003391 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003392 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003393 return ret < 0 ? ret : 0;
3394}
3395
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003396static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003397 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003398 u64 chunk_offset, u64 length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003399 u64 dev_offset,
3400 struct btrfs_block_group_cache *cache,
3401 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003402{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003403 struct btrfs_fs_info *fs_info = sctx->fs_info;
3404 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003405 struct map_lookup *map;
3406 struct extent_map *em;
3407 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003408 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003409
3410 read_lock(&map_tree->map_tree.lock);
3411 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3412 read_unlock(&map_tree->map_tree.lock);
3413
Filipe Manana020d5b72015-11-19 10:57:20 +00003414 if (!em) {
3415 /*
3416 * Might have been an unused block group deleted by the cleaner
3417 * kthread or relocation.
3418 */
3419 spin_lock(&cache->lock);
3420 if (!cache->removed)
3421 ret = -EINVAL;
3422 spin_unlock(&cache->lock);
3423
3424 return ret;
3425 }
Arne Jansena2de7332011-03-08 14:14:00 +01003426
Jeff Mahoney95617d62015-06-03 10:55:48 -04003427 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003428 if (em->start != chunk_offset)
3429 goto out;
3430
3431 if (em->len < length)
3432 goto out;
3433
3434 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003435 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003436 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003437 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003438 chunk_offset, length,
3439 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003440 if (ret)
3441 goto out;
3442 }
3443 }
3444out:
3445 free_extent_map(em);
3446
3447 return ret;
3448}
3449
3450static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003451int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003452 struct btrfs_device *scrub_dev, u64 start, u64 end,
3453 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003454{
3455 struct btrfs_dev_extent *dev_extent = NULL;
3456 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003457 struct btrfs_fs_info *fs_info = sctx->fs_info;
3458 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003459 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003460 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003461 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003462 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003463 int slot;
3464 struct extent_buffer *l;
3465 struct btrfs_key key;
3466 struct btrfs_key found_key;
3467 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003468 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003469
3470 path = btrfs_alloc_path();
3471 if (!path)
3472 return -ENOMEM;
3473
David Sterbae4058b52015-11-27 16:31:35 +01003474 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003475 path->search_commit_root = 1;
3476 path->skip_locking = 1;
3477
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003478 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003479 key.offset = 0ull;
3480 key.type = BTRFS_DEV_EXTENT_KEY;
3481
Arne Jansena2de7332011-03-08 14:14:00 +01003482 while (1) {
3483 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3484 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003485 break;
3486 if (ret > 0) {
3487 if (path->slots[0] >=
3488 btrfs_header_nritems(path->nodes[0])) {
3489 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003490 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003491 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003492 if (ret > 0) {
3493 ret = 0;
3494 break;
3495 }
3496 } else {
3497 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003498 }
3499 }
Arne Jansena2de7332011-03-08 14:14:00 +01003500
3501 l = path->nodes[0];
3502 slot = path->slots[0];
3503
3504 btrfs_item_key_to_cpu(l, &found_key, slot);
3505
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003506 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003507 break;
3508
David Sterba962a2982014-06-04 18:41:45 +02003509 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003510 break;
3511
3512 if (found_key.offset >= end)
3513 break;
3514
3515 if (found_key.offset < key.offset)
3516 break;
3517
3518 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3519 length = btrfs_dev_extent_length(l, dev_extent);
3520
Qu Wenruoced96ed2014-06-19 10:42:51 +08003521 if (found_key.offset + length <= start)
3522 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003523
Arne Jansena2de7332011-03-08 14:14:00 +01003524 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3525
3526 /*
3527 * get a reference on the corresponding block group to prevent
3528 * the chunk from going away while we scrub it
3529 */
3530 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003531
3532 /* some chunks are removed but not committed to disk yet,
3533 * continue scrubbing */
3534 if (!cache)
3535 goto skip;
3536
Zhaolei55e3a602015-08-05 16:43:30 +08003537 /*
3538 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3539 * to avoid deadlock caused by:
3540 * btrfs_inc_block_group_ro()
3541 * -> btrfs_wait_for_commit()
3542 * -> btrfs_commit_transaction()
3543 * -> btrfs_scrub_pause()
3544 */
3545 scrub_pause_on(fs_info);
Nikolay Borisovc83488a2018-06-20 15:49:14 +03003546 ret = btrfs_inc_block_group_ro(cache);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003547 if (!ret && is_dev_replace) {
3548 /*
3549 * If we are doing a device replace wait for any tasks
3550 * that started dellaloc right before we set the block
3551 * group to RO mode, as they might have just allocated
3552 * an extent from it or decided they could do a nocow
3553 * write. And if any such tasks did that, wait for their
3554 * ordered extents to complete and then commit the
3555 * current transaction, so that we can later see the new
3556 * extent items in the extent tree - the ordered extents
3557 * create delayed data references (for cow writes) when
3558 * they complete, which will be run and insert the
3559 * corresponding extent items into the extent tree when
3560 * we commit the transaction they used when running
3561 * inode.c:btrfs_finish_ordered_io(). We later use
3562 * the commit root of the extent tree to find extents
3563 * to copy from the srcdev into the tgtdev, and we don't
3564 * want to miss any new extents.
3565 */
3566 btrfs_wait_block_group_reservations(cache);
3567 btrfs_wait_nocow_writers(cache);
Chris Mason6374e57a2017-06-23 09:48:21 -07003568 ret = btrfs_wait_ordered_roots(fs_info, U64_MAX,
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003569 cache->key.objectid,
3570 cache->key.offset);
3571 if (ret > 0) {
3572 struct btrfs_trans_handle *trans;
3573
3574 trans = btrfs_join_transaction(root);
3575 if (IS_ERR(trans))
3576 ret = PTR_ERR(trans);
3577 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003578 ret = btrfs_commit_transaction(trans);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003579 if (ret) {
3580 scrub_pause_off(fs_info);
3581 btrfs_put_block_group(cache);
3582 break;
3583 }
3584 }
3585 }
Zhaolei55e3a602015-08-05 16:43:30 +08003586 scrub_pause_off(fs_info);
Zhaolei76a8efa2015-11-17 18:46:17 +08003587
3588 if (ret == 0) {
3589 ro_set = 1;
3590 } else if (ret == -ENOSPC) {
3591 /*
3592 * btrfs_inc_block_group_ro return -ENOSPC when it
3593 * failed in creating new chunk for metadata.
3594 * It is not a problem for scrub/replace, because
3595 * metadata are always cowed, and our scrub paused
3596 * commit_transactions.
3597 */
3598 ro_set = 0;
3599 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003600 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02003601 "failed setting block group ro: %d", ret);
Zhaolei55e3a602015-08-05 16:43:30 +08003602 btrfs_put_block_group(cache);
3603 break;
3604 }
3605
David Sterba7e79cb82018-03-24 02:11:38 +01003606 btrfs_dev_replace_write_lock(&fs_info->dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003607 dev_replace->cursor_right = found_key.offset + length;
3608 dev_replace->cursor_left = found_key.offset;
3609 dev_replace->item_needs_writeback = 1;
David Sterba7e79cb82018-03-24 02:11:38 +01003610 btrfs_dev_replace_write_unlock(&fs_info->dev_replace);
Zhao Lei8c204c92015-08-19 15:02:40 +08003611 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003612 found_key.offset, cache, is_dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003613
3614 /*
3615 * flush, submit all pending read and write bios, afterwards
3616 * wait for them.
3617 * Note that in the dev replace case, a read request causes
3618 * write requests that are submitted in the read completion
3619 * worker. Therefore in the current situation, it is required
3620 * that all write requests are flushed, so that all read and
3621 * write requests are really completed when bios_in_flight
3622 * changes to 0.
3623 */
David Sterba2073c4c2017-03-31 17:12:51 +02003624 sctx->flush_all_writes = true;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003625 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003626 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003627 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003628 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003629
3630 wait_event(sctx->list_wait,
3631 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003632
3633 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003634
3635 /*
3636 * must be called before we decrease @scrub_paused.
3637 * make sure we don't block transaction commit while
3638 * we are waiting pending workers finished.
3639 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003640 wait_event(sctx->list_wait,
3641 atomic_read(&sctx->workers_pending) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003642 sctx->flush_all_writes = false;
Wang Shilong12cf9372014-02-19 19:24:17 +08003643
Zhaoleib708ce92015-08-05 16:43:29 +08003644 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003645
David Sterba7e79cb82018-03-24 02:11:38 +01003646 btrfs_dev_replace_write_lock(&fs_info->dev_replace);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003647 dev_replace->cursor_left = dev_replace->cursor_right;
3648 dev_replace->item_needs_writeback = 1;
David Sterba7e79cb82018-03-24 02:11:38 +01003649 btrfs_dev_replace_write_unlock(&fs_info->dev_replace);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003650
Zhaolei76a8efa2015-11-17 18:46:17 +08003651 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003652 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003653
Filipe Manana758f2df2015-11-19 11:45:48 +00003654 /*
3655 * We might have prevented the cleaner kthread from deleting
3656 * this block group if it was already unused because we raced
3657 * and set it to RO mode first. So add it back to the unused
3658 * list, otherwise it might not ever be deleted unless a manual
3659 * balance is triggered or it becomes used and unused again.
3660 */
3661 spin_lock(&cache->lock);
3662 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3663 btrfs_block_group_used(&cache->item) == 0) {
3664 spin_unlock(&cache->lock);
Qu Wenruo031f24d2018-05-22 16:43:47 +08003665 btrfs_mark_bg_unused(cache);
Filipe Manana758f2df2015-11-19 11:45:48 +00003666 } else {
3667 spin_unlock(&cache->lock);
3668 }
3669
Arne Jansena2de7332011-03-08 14:14:00 +01003670 btrfs_put_block_group(cache);
3671 if (ret)
3672 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003673 if (is_dev_replace &&
3674 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003675 ret = -EIO;
3676 break;
3677 }
3678 if (sctx->stat.malloc_errors > 0) {
3679 ret = -ENOMEM;
3680 break;
3681 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003682skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003683 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003684 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003685 }
3686
Arne Jansena2de7332011-03-08 14:14:00 +01003687 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003688
Zhaolei55e3a602015-08-05 16:43:30 +08003689 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003690}
3691
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003692static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3693 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003694{
3695 int i;
3696 u64 bytenr;
3697 u64 gen;
3698 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003699 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003700
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003701 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003702 return -EIO;
3703
Miao Xie5f546062014-07-24 11:37:09 +08003704 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003705 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08003706 gen = scrub_dev->generation;
3707 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003708 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003709
3710 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3711 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003712 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3713 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003714 break;
3715
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003716 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003717 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003718 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003719 if (ret)
3720 return ret;
3721 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003722 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003723
3724 return 0;
3725}
3726
3727/*
3728 * get a reference count on fs_info->scrub_workers. start worker if necessary
3729 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003730static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3731 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003732{
David Sterba6f011052015-02-16 18:34:01 +01003733 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003734 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003735
Arne Jansen632dd772011-06-10 12:07:07 +02003736 if (fs_info->scrub_workers_refcnt == 0) {
David Sterbaaf1cbe02017-03-31 18:42:57 +02003737 fs_info->scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub",
3738 flags, is_dev_replace ? 1 : max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08003739 if (!fs_info->scrub_workers)
3740 goto fail_scrub_workers;
3741
Qu Wenruo0339ef22014-02-28 10:46:17 +08003742 fs_info->scrub_wr_completion_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04003743 btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08003744 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003745 if (!fs_info->scrub_wr_completion_workers)
3746 goto fail_scrub_wr_completion_workers;
3747
Zhao Lei20b2e302015-06-04 20:09:15 +08003748 fs_info->scrub_parity_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04003749 btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
Zhao Lei20b2e302015-06-04 20:09:15 +08003750 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003751 if (!fs_info->scrub_parity_workers)
3752 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02003753 }
Arne Jansena2de7332011-03-08 14:14:00 +01003754 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08003755 return 0;
3756
3757fail_scrub_parity_workers:
Zhao Leie82afc52015-06-12 20:36:58 +08003758 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3759fail_scrub_wr_completion_workers:
3760 btrfs_destroy_workqueue(fs_info->scrub_workers);
3761fail_scrub_workers:
3762 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01003763}
3764
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003765static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003766{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003767 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003768 btrfs_destroy_workqueue(fs_info->scrub_workers);
3769 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08003770 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003771 }
Arne Jansena2de7332011-03-08 14:14:00 +01003772 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003773}
3774
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003775int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3776 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003777 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003778{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003779 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003780 int ret;
3781 struct btrfs_device *dev;
3782
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003783 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003784 return -EINVAL;
3785
Jeff Mahoneyda170662016-06-15 09:22:56 -04003786 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003787 /*
3788 * in this case scrub is unable to calculate the checksum
3789 * the way scrub is implemented. Do not handle this
3790 * situation at all because it won't ever happen.
3791 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003792 btrfs_err(fs_info,
3793 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04003794 fs_info->nodesize,
3795 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003796 return -EINVAL;
3797 }
3798
Jeff Mahoneyda170662016-06-15 09:22:56 -04003799 if (fs_info->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003800 /* not supported for data w/o checksums */
Chandan Rajendra751bebbe2016-07-04 10:04:39 +05303801 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003802 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04003803 fs_info->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003804 return -EINVAL;
3805 }
3806
Jeff Mahoneyda170662016-06-15 09:22:56 -04003807 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003808 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04003809 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003810 /*
3811 * would exhaust the array bounds of pagev member in
3812 * struct scrub_block
3813 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003814 btrfs_err(fs_info,
3815 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04003816 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003817 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04003818 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003819 SCRUB_MAX_PAGES_PER_BLOCK);
3820 return -EINVAL;
3821 }
3822
Arne Jansena2de7332011-03-08 14:14:00 +01003823
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003824 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3825 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jaine6e674b2017-12-04 12:54:54 +08003826 if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
3827 !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003828 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003829 return -ENODEV;
3830 }
Arne Jansena2de7332011-03-08 14:14:00 +01003831
Anand Jainebbede42017-12-04 12:54:52 +08003832 if (!is_dev_replace && !readonly &&
3833 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Miao Xie5d68da32014-07-24 11:37:07 +08003834 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Misono Tomohiro672d5992018-08-02 16:19:07 +09003835 btrfs_err_in_rcu(fs_info, "scrub: device %s is not writable",
3836 rcu_str_deref(dev->name));
Miao Xie5d68da32014-07-24 11:37:07 +08003837 return -EROFS;
3838 }
3839
Wang Shilong3b7a0162013-10-12 02:11:12 +08003840 mutex_lock(&fs_info->scrub_lock);
Anand Jaine12c9622017-12-04 12:54:53 +08003841 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08003842 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003843 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003844 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003845 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003846 }
3847
David Sterba7e79cb82018-03-24 02:11:38 +01003848 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Anand Jaincadbc0a2018-01-03 16:08:30 +08003849 if (dev->scrub_ctx ||
Stefan Behrens8dabb742012-11-06 13:15:27 +01003850 (!is_dev_replace &&
3851 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
David Sterba7e79cb82018-03-24 02:11:38 +01003852 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003853 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003854 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003855 return -EINPROGRESS;
3856 }
David Sterba7e79cb82018-03-24 02:11:38 +01003857 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003858
3859 ret = scrub_workers_get(fs_info, is_dev_replace);
3860 if (ret) {
3861 mutex_unlock(&fs_info->scrub_lock);
3862 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3863 return ret;
3864 }
3865
Stefan Behrens63a212a2012-11-05 18:29:28 +01003866 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003867 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003868 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003869 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3870 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003871 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003872 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003873 sctx->readonly = readonly;
Anand Jaincadbc0a2018-01-03 16:08:30 +08003874 dev->scrub_ctx = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003875 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003876
Wang Shilong3cb09292013-12-04 21:15:19 +08003877 /*
3878 * checking @scrub_pause_req here, we can avoid
3879 * race between committing transaction and scrubbing.
3880 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003881 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003882 atomic_inc(&fs_info->scrubs_running);
3883 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003884
Stefan Behrensff023aa2012-11-06 11:43:11 +01003885 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003886 /*
3887 * by holding device list mutex, we can
3888 * kick off writing super in log tree sync.
3889 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003890 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003891 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003892 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003893 }
Arne Jansena2de7332011-03-08 14:14:00 +01003894
3895 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003896 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3897 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003898
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003899 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003900 atomic_dec(&fs_info->scrubs_running);
3901 wake_up(&fs_info->scrub_pause_wait);
3902
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003903 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003904
Arne Jansena2de7332011-03-08 14:14:00 +01003905 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003906 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003907
3908 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08003909 dev->scrub_ctx = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003910 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003911 mutex_unlock(&fs_info->scrub_lock);
3912
Filipe Mananaf55985f2015-02-09 21:14:24 +00003913 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003914
3915 return ret;
3916}
3917
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003918void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003919{
Arne Jansena2de7332011-03-08 14:14:00 +01003920 mutex_lock(&fs_info->scrub_lock);
3921 atomic_inc(&fs_info->scrub_pause_req);
3922 while (atomic_read(&fs_info->scrubs_paused) !=
3923 atomic_read(&fs_info->scrubs_running)) {
3924 mutex_unlock(&fs_info->scrub_lock);
3925 wait_event(fs_info->scrub_pause_wait,
3926 atomic_read(&fs_info->scrubs_paused) ==
3927 atomic_read(&fs_info->scrubs_running));
3928 mutex_lock(&fs_info->scrub_lock);
3929 }
3930 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003931}
3932
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003933void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003934{
Arne Jansena2de7332011-03-08 14:14:00 +01003935 atomic_dec(&fs_info->scrub_pause_req);
3936 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003937}
3938
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003939int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003940{
Arne Jansena2de7332011-03-08 14:14:00 +01003941 mutex_lock(&fs_info->scrub_lock);
3942 if (!atomic_read(&fs_info->scrubs_running)) {
3943 mutex_unlock(&fs_info->scrub_lock);
3944 return -ENOTCONN;
3945 }
3946
3947 atomic_inc(&fs_info->scrub_cancel_req);
3948 while (atomic_read(&fs_info->scrubs_running)) {
3949 mutex_unlock(&fs_info->scrub_lock);
3950 wait_event(fs_info->scrub_pause_wait,
3951 atomic_read(&fs_info->scrubs_running) == 0);
3952 mutex_lock(&fs_info->scrub_lock);
3953 }
3954 atomic_dec(&fs_info->scrub_cancel_req);
3955 mutex_unlock(&fs_info->scrub_lock);
3956
3957 return 0;
3958}
3959
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003960int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3961 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003962{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003963 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003964
3965 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08003966 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003967 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003968 mutex_unlock(&fs_info->scrub_lock);
3969 return -ENOTCONN;
3970 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003971 atomic_inc(&sctx->cancel_req);
Anand Jaincadbc0a2018-01-03 16:08:30 +08003972 while (dev->scrub_ctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003973 mutex_unlock(&fs_info->scrub_lock);
3974 wait_event(fs_info->scrub_pause_wait,
Anand Jaincadbc0a2018-01-03 16:08:30 +08003975 dev->scrub_ctx == NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003976 mutex_lock(&fs_info->scrub_lock);
3977 }
3978 mutex_unlock(&fs_info->scrub_lock);
3979
3980 return 0;
3981}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003982
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003983int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01003984 struct btrfs_scrub_progress *progress)
3985{
3986 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003987 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003988
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003989 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3990 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003991 if (dev)
Anand Jaincadbc0a2018-01-03 16:08:30 +08003992 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003993 if (sctx)
3994 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003995 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003996
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003997 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003998}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003999
4000static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4001 u64 extent_logical, u64 extent_len,
4002 u64 *extent_physical,
4003 struct btrfs_device **extent_dev,
4004 int *extent_mirror_num)
4005{
4006 u64 mapped_length;
4007 struct btrfs_bio *bbio = NULL;
4008 int ret;
4009
4010 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004011 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004012 &mapped_length, &bbio, 0);
4013 if (ret || !bbio || mapped_length < extent_len ||
4014 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08004015 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004016 return;
4017 }
4018
4019 *extent_physical = bbio->stripes[0].physical;
4020 *extent_mirror_num = bbio->mirror_num;
4021 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004022 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004023}