blob: adaf8ab694d544df626a5b572af8ec5db80ec92c [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>
Johannes Thumshirnd5178572019-06-03 16:58:57 +02009#include <crypto/hash.h>
Arne Jansena2de7332011-03-08 14:14:00 +010010#include "ctree.h"
Dennis Zhou6e80d4f2019-12-13 16:22:15 -080011#include "discard.h"
Arne Jansena2de7332011-03-08 14:14:00 +010012#include "volumes.h"
13#include "disk-io.h"
14#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020015#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020016#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020017#include "extent_io.h"
Stefan Behrensff023aa2012-11-06 11:43:11 +010018#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010019#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040020#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050021#include "raid56.h"
Josef Bacikaac00232019-06-20 15:37:44 -040022#include "block-group.h"
Arne Jansena2de7332011-03-08 14:14:00 +010023
24/*
25 * This is only the first step towards a full-features scrub. It reads all
26 * extent and super block and verifies the checksums. In case a bad checksum
27 * is found or the extent cannot be read, good data will be written back if
28 * any can be found.
29 *
30 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010031 * - In case an unrepairable extent is encountered, track which files are
32 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010033 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010034 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010035 */
36
Stefan Behrensb5d67f62012-03-27 14:21:27 -040037struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010038struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010039
Stefan Behrensff023aa2012-11-06 11:43:11 +010040/*
41 * the following three values only influence the performance.
42 * The last one configures the number of parallel and outstanding I/O
43 * operations. The first two values configure an upper limit for the number
44 * of (dynamically allocated) pages that are added to a bio.
45 */
46#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
47#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
48#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010049
50/*
51 * the following value times PAGE_SIZE needs to be large enough to match the
52 * largest node/leaf/sector size that shall be supported.
53 * Values larger than BTRFS_STRIPE_LEN are not supported.
54 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040055#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010056
Miao Xieaf8e2d12014-10-23 14:42:50 +080057struct scrub_recover {
Elena Reshetova6f615012017-03-03 10:55:21 +020058 refcount_t refs;
Miao Xieaf8e2d12014-10-23 14:42:50 +080059 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +080060 u64 map_length;
61};
62
Arne Jansena2de7332011-03-08 14:14:00 +010063struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040064 struct scrub_block *sblock;
65 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020066 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080067 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010068 u64 flags; /* extent flags */
69 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040070 u64 logical;
71 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010072 u64 physical_for_dev_replace;
Zhao Lei57019342015-01-20 15:11:45 +080073 atomic_t refs;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040074 struct {
75 unsigned int mirror_num:8;
76 unsigned int have_csum:1;
77 unsigned int io_error:1;
78 };
Arne Jansena2de7332011-03-08 14:14:00 +010079 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080080
81 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010082};
83
84struct scrub_bio {
85 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010086 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010087 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010088 struct bio *bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020089 blk_status_t status;
Arne Jansena2de7332011-03-08 14:14:00 +010090 u64 logical;
91 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010092#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
93 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
94#else
95 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
96#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -040097 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +010098 int next_free;
99 struct btrfs_work work;
100};
101
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400102struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100103 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400104 int page_count;
105 atomic_t outstanding_pages;
Elena Reshetova186debd2017-03-03 10:55:23 +0200106 refcount_t refs; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100107 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800108 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400109 struct {
110 unsigned int header_error:1;
111 unsigned int checksum_error:1;
112 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200113 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800114
115 /* The following is for the data used to check parity */
116 /* It is for the data with checksum */
117 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400118 };
Omar Sandoval73ff61d2015-06-19 11:52:51 -0700119 struct btrfs_work work;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400120};
121
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800122/* Used for the chunks with parity stripe such RAID5/6 */
123struct scrub_parity {
124 struct scrub_ctx *sctx;
125
126 struct btrfs_device *scrub_dev;
127
128 u64 logic_start;
129
130 u64 logic_end;
131
132 int nsectors;
133
Liu Bo972d7212017-04-03 13:45:33 -0700134 u64 stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800135
Elena Reshetova78a76452017-03-03 10:55:24 +0200136 refcount_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800137
138 struct list_head spages;
139
140 /* Work of parity check and repair */
141 struct btrfs_work work;
142
143 /* Mark the parity blocks which have data */
144 unsigned long *dbitmap;
145
146 /*
147 * Mark the parity blocks which have data, but errors happen when
148 * read data or check data
149 */
150 unsigned long *ebitmap;
151
Gustavo A. R. Silvaa8753ee2020-03-06 16:13:33 -0600152 unsigned long bitmap[];
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800153};
154
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100155struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100156 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400157 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100158 int first_free;
159 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100160 atomic_t bios_in_flight;
161 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100162 spinlock_t list_lock;
163 wait_queue_head_t list_wait;
164 u16 csum_size;
165 struct list_head csum_list;
166 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100167 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100168 int pages_per_rd_bio;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100169
170 int is_dev_replace;
David Sterba3fb99302017-05-16 19:10:32 +0200171
172 struct scrub_bio *wr_curr_bio;
173 struct mutex wr_lock;
174 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
David Sterba3fb99302017-05-16 19:10:32 +0200175 struct btrfs_device *wr_tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200176 bool flush_all_writes;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100177
Arne Jansena2de7332011-03-08 14:14:00 +0100178 /*
179 * statistics
180 */
181 struct btrfs_scrub_progress stat;
182 spinlock_t stat_lock;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000183
184 /*
185 * Use a ref counter to avoid use-after-free issues. Scrub workers
186 * decrement bios_in_flight and workers_pending and then do a wakeup
187 * on the list_wait wait queue. We must ensure the main scrub task
188 * doesn't free the scrub context before or while the workers are
189 * doing the wakeup() call.
190 */
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200191 refcount_t refs;
Arne Jansena2de7332011-03-08 14:14:00 +0100192};
193
Jan Schmidt558540c2011-06-13 19:59:12 +0200194struct scrub_warning {
195 struct btrfs_path *path;
196 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200197 const char *errstr;
David Sterba6aa21262017-10-04 17:07:07 +0200198 u64 physical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200199 u64 logical;
200 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200201};
202
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800203struct full_stripe_lock {
204 struct rb_node node;
205 u64 logical;
206 u64 refs;
207 struct mutex mutex;
208};
209
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100210static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
211static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400212static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Zhao Leibe50a8d2015-01-20 15:11:42 +0800213static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100214 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100215static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +0800216 struct scrub_block *sblock,
217 int retry_failed_mirror);
Zhao Leiba7cf982015-08-24 21:18:02 +0800218static void scrub_recheck_block_checksum(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400219static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800220 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400221static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
222 struct scrub_block *sblock_good,
223 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100224static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
225static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
226 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400227static int scrub_checksum_data(struct scrub_block *sblock);
228static int scrub_checksum_tree_block(struct scrub_block *sblock);
229static int scrub_checksum_super(struct scrub_block *sblock);
230static void scrub_block_get(struct scrub_block *sblock);
231static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100232static void scrub_page_get(struct scrub_page *spage);
233static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800234static void scrub_parity_get(struct scrub_parity *sparity);
235static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100236static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
237 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100238static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100239 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100240 u64 gen, int mirror_num, u8 *csum, int force,
241 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200242static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400243static void scrub_bio_end_io_worker(struct btrfs_work *work);
244static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100245static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
246 u64 extent_logical, u64 extent_len,
247 u64 *extent_physical,
248 struct btrfs_device **extent_dev,
249 int *extent_mirror_num);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100250static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
251 struct scrub_page *spage);
252static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200253static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100254static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800255static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800256static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000257static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400258
Liu Bo762221f2018-01-02 13:36:42 -0700259static inline int scrub_is_page_on_raid56(struct scrub_page *page)
260{
261 return page->recover &&
262 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
263}
Stefan Behrens1623ede2012-03-27 14:21:26 -0400264
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100265static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
266{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200267 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100268 atomic_inc(&sctx->bios_in_flight);
269}
270
271static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
272{
273 atomic_dec(&sctx->bios_in_flight);
274 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000275 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100276}
277
Wang Shilongcb7ab022013-12-04 21:16:53 +0800278static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800279{
280 while (atomic_read(&fs_info->scrub_pause_req)) {
281 mutex_unlock(&fs_info->scrub_lock);
282 wait_event(fs_info->scrub_pause_wait,
283 atomic_read(&fs_info->scrub_pause_req) == 0);
284 mutex_lock(&fs_info->scrub_lock);
285 }
286}
287
Zhaolei0e22be82015-08-05 16:43:28 +0800288static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800289{
290 atomic_inc(&fs_info->scrubs_paused);
291 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800292}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800293
Zhaolei0e22be82015-08-05 16:43:28 +0800294static void scrub_pause_off(struct btrfs_fs_info *fs_info)
295{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800296 mutex_lock(&fs_info->scrub_lock);
297 __scrub_blocked_if_needed(fs_info);
298 atomic_dec(&fs_info->scrubs_paused);
299 mutex_unlock(&fs_info->scrub_lock);
300
301 wake_up(&fs_info->scrub_pause_wait);
302}
303
Zhaolei0e22be82015-08-05 16:43:28 +0800304static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
305{
306 scrub_pause_on(fs_info);
307 scrub_pause_off(fs_info);
308}
309
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100310/*
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800311 * Insert new full stripe lock into full stripe locks tree
312 *
313 * Return pointer to existing or newly inserted full_stripe_lock structure if
314 * everything works well.
315 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
316 *
317 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
318 * function
319 */
320static struct full_stripe_lock *insert_full_stripe_lock(
321 struct btrfs_full_stripe_locks_tree *locks_root,
322 u64 fstripe_logical)
323{
324 struct rb_node **p;
325 struct rb_node *parent = NULL;
326 struct full_stripe_lock *entry;
327 struct full_stripe_lock *ret;
328
David Sterbaa32bf9a2018-03-16 02:21:22 +0100329 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800330
331 p = &locks_root->root.rb_node;
332 while (*p) {
333 parent = *p;
334 entry = rb_entry(parent, struct full_stripe_lock, node);
335 if (fstripe_logical < entry->logical) {
336 p = &(*p)->rb_left;
337 } else if (fstripe_logical > entry->logical) {
338 p = &(*p)->rb_right;
339 } else {
340 entry->refs++;
341 return entry;
342 }
343 }
344
Filipe Mananaa5fb1142018-11-26 20:07:17 +0000345 /*
346 * Insert new lock.
Filipe Mananaa5fb1142018-11-26 20:07:17 +0000347 */
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800348 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
349 if (!ret)
350 return ERR_PTR(-ENOMEM);
351 ret->logical = fstripe_logical;
352 ret->refs = 1;
353 mutex_init(&ret->mutex);
354
355 rb_link_node(&ret->node, parent, p);
356 rb_insert_color(&ret->node, &locks_root->root);
357 return ret;
358}
359
360/*
361 * Search for a full stripe lock of a block group
362 *
363 * Return pointer to existing full stripe lock if found
364 * Return NULL if not found
365 */
366static struct full_stripe_lock *search_full_stripe_lock(
367 struct btrfs_full_stripe_locks_tree *locks_root,
368 u64 fstripe_logical)
369{
370 struct rb_node *node;
371 struct full_stripe_lock *entry;
372
David Sterbaa32bf9a2018-03-16 02:21:22 +0100373 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800374
375 node = locks_root->root.rb_node;
376 while (node) {
377 entry = rb_entry(node, struct full_stripe_lock, node);
378 if (fstripe_logical < entry->logical)
379 node = node->rb_left;
380 else if (fstripe_logical > entry->logical)
381 node = node->rb_right;
382 else
383 return entry;
384 }
385 return NULL;
386}
387
388/*
389 * Helper to get full stripe logical from a normal bytenr.
390 *
391 * Caller must ensure @cache is a RAID56 block group.
392 */
David Sterba32da53862019-10-29 19:20:18 +0100393static u64 get_full_stripe_logical(struct btrfs_block_group *cache, u64 bytenr)
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800394{
395 u64 ret;
396
397 /*
398 * Due to chunk item size limit, full stripe length should not be
399 * larger than U32_MAX. Just a sanity check here.
400 */
401 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
402
403 /*
404 * round_down() can only handle power of 2, while RAID56 full
405 * stripe length can be 64KiB * n, so we need to manually round down.
406 */
David Sterbab3470b52019-10-23 18:48:22 +0200407 ret = div64_u64(bytenr - cache->start, cache->full_stripe_len) *
408 cache->full_stripe_len + cache->start;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800409 return ret;
410}
411
412/*
413 * Lock a full stripe to avoid concurrency of recovery and read
414 *
415 * It's only used for profiles with parities (RAID5/6), for other profiles it
416 * does nothing.
417 *
418 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
419 * So caller must call unlock_full_stripe() at the same context.
420 *
421 * Return <0 if encounters error.
422 */
423static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
424 bool *locked_ret)
425{
David Sterba32da53862019-10-29 19:20:18 +0100426 struct btrfs_block_group *bg_cache;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800427 struct btrfs_full_stripe_locks_tree *locks_root;
428 struct full_stripe_lock *existing;
429 u64 fstripe_start;
430 int ret = 0;
431
432 *locked_ret = false;
433 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
434 if (!bg_cache) {
435 ASSERT(0);
436 return -ENOENT;
437 }
438
439 /* Profiles not based on parity don't need full stripe lock */
440 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
441 goto out;
442 locks_root = &bg_cache->full_stripe_locks_root;
443
444 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
445
446 /* Now insert the full stripe lock */
447 mutex_lock(&locks_root->lock);
448 existing = insert_full_stripe_lock(locks_root, fstripe_start);
449 mutex_unlock(&locks_root->lock);
450 if (IS_ERR(existing)) {
451 ret = PTR_ERR(existing);
452 goto out;
453 }
454 mutex_lock(&existing->mutex);
455 *locked_ret = true;
456out:
457 btrfs_put_block_group(bg_cache);
458 return ret;
459}
460
461/*
462 * Unlock a full stripe.
463 *
464 * NOTE: Caller must ensure it's the same context calling corresponding
465 * lock_full_stripe().
466 *
467 * Return 0 if we unlock full stripe without problem.
468 * Return <0 for error
469 */
470static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
471 bool locked)
472{
David Sterba32da53862019-10-29 19:20:18 +0100473 struct btrfs_block_group *bg_cache;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800474 struct btrfs_full_stripe_locks_tree *locks_root;
475 struct full_stripe_lock *fstripe_lock;
476 u64 fstripe_start;
477 bool freeit = false;
478 int ret = 0;
479
480 /* If we didn't acquire full stripe lock, no need to continue */
481 if (!locked)
482 return 0;
483
484 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
485 if (!bg_cache) {
486 ASSERT(0);
487 return -ENOENT;
488 }
489 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
490 goto out;
491
492 locks_root = &bg_cache->full_stripe_locks_root;
493 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
494
495 mutex_lock(&locks_root->lock);
496 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
497 /* Unpaired unlock_full_stripe() detected */
498 if (!fstripe_lock) {
499 WARN_ON(1);
500 ret = -ENOENT;
501 mutex_unlock(&locks_root->lock);
502 goto out;
503 }
504
505 if (fstripe_lock->refs == 0) {
506 WARN_ON(1);
507 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
508 fstripe_lock->logical);
509 } else {
510 fstripe_lock->refs--;
511 }
512
513 if (fstripe_lock->refs == 0) {
514 rb_erase(&fstripe_lock->node, &locks_root->root);
515 freeit = true;
516 }
517 mutex_unlock(&locks_root->lock);
518
519 mutex_unlock(&fstripe_lock->mutex);
520 if (freeit)
521 kfree(fstripe_lock);
522out:
523 btrfs_put_block_group(bg_cache);
524 return ret;
525}
526
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100527static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100528{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100529 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100530 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100531 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100532 struct btrfs_ordered_sum, list);
533 list_del(&sum->list);
534 kfree(sum);
535 }
536}
537
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100538static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100539{
540 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100541
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100542 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100543 return;
544
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400545 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100546 if (sctx->curr != -1) {
547 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400548
549 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100550 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400551 scrub_block_put(sbio->pagev[i]->sblock);
552 }
553 bio_put(sbio->bio);
554 }
555
Stefan Behrensff023aa2012-11-06 11:43:11 +0100556 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100557 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100558
559 if (!sbio)
560 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100561 kfree(sbio);
562 }
563
David Sterba3fb99302017-05-16 19:10:32 +0200564 kfree(sctx->wr_curr_bio);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100565 scrub_free_csums(sctx);
566 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100567}
568
Filipe Mananaf55985f2015-02-09 21:14:24 +0000569static void scrub_put_ctx(struct scrub_ctx *sctx)
570{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200571 if (refcount_dec_and_test(&sctx->refs))
Filipe Mananaf55985f2015-02-09 21:14:24 +0000572 scrub_free_ctx(sctx);
573}
574
David Sterba92f7ba42018-12-04 16:11:55 +0100575static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
576 struct btrfs_fs_info *fs_info, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100577{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100578 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100579 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100580
David Sterba58c4e172016-02-11 10:49:42 +0100581 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100582 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100583 goto nomem;
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200584 refcount_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100585 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200586 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100587 sctx->curr = -1;
David Sterba92f7ba42018-12-04 16:11:55 +0100588 sctx->fs_info = fs_info;
Dan Robertsone49be142019-02-19 02:56:43 +0000589 INIT_LIST_HEAD(&sctx->csum_list);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100590 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100591 struct scrub_bio *sbio;
592
David Sterba58c4e172016-02-11 10:49:42 +0100593 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
Arne Jansena2de7332011-03-08 14:14:00 +0100594 if (!sbio)
595 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100596 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100597
Arne Jansena2de7332011-03-08 14:14:00 +0100598 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100599 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400600 sbio->page_count = 0;
Omar Sandovala0cac0e2019-09-16 11:30:57 -0700601 btrfs_init_work(&sbio->work, scrub_bio_end_io_worker, NULL,
602 NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100603
Stefan Behrensff023aa2012-11-06 11:43:11 +0100604 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100605 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200606 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100607 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100608 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100609 sctx->first_free = 0;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100610 atomic_set(&sctx->bios_in_flight, 0);
611 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100612 atomic_set(&sctx->cancel_req, 0);
613 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
Arne Jansena2de7332011-03-08 14:14:00 +0100614
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100615 spin_lock_init(&sctx->list_lock);
616 spin_lock_init(&sctx->stat_lock);
617 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100618
David Sterba3fb99302017-05-16 19:10:32 +0200619 WARN_ON(sctx->wr_curr_bio != NULL);
620 mutex_init(&sctx->wr_lock);
621 sctx->wr_curr_bio = NULL;
David Sterba8fcdac32017-05-16 19:10:23 +0200622 if (is_dev_replace) {
David Sterbaded56182017-06-26 15:19:00 +0200623 WARN_ON(!fs_info->dev_replace.tgtdev);
David Sterba3fb99302017-05-16 19:10:32 +0200624 sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
David Sterbaded56182017-06-26 15:19:00 +0200625 sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200626 sctx->flush_all_writes = false;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100627 }
David Sterba8fcdac32017-05-16 19:10:23 +0200628
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100629 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100630
631nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100632 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100633 return ERR_PTR(-ENOMEM);
634}
635
Stefan Behrensff023aa2012-11-06 11:43:11 +0100636static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
637 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200638{
639 u64 isize;
640 u32 nlink;
641 int ret;
642 int i;
David Sterbade2491f2017-05-31 19:21:38 +0200643 unsigned nofs_flag;
Jan Schmidt558540c2011-06-13 19:59:12 +0200644 struct extent_buffer *eb;
645 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100646 struct scrub_warning *swarn = warn_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400647 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200648 struct inode_fs_paths *ipath = NULL;
649 struct btrfs_root *local_root;
650 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100651 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200652
653 root_key.objectid = root;
654 root_key.type = BTRFS_ROOT_ITEM_KEY;
655 root_key.offset = (u64)-1;
Josef Bacik3619c942020-01-24 09:32:24 -0500656 local_root = btrfs_get_fs_root(fs_info, &root_key, true);
Jan Schmidt558540c2011-06-13 19:59:12 +0200657 if (IS_ERR(local_root)) {
658 ret = PTR_ERR(local_root);
659 goto err;
660 }
661
David Sterba14692cc2015-01-02 18:55:46 +0100662 /*
663 * this makes the path point to (inum INODE_ITEM ioff)
664 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100665 key.objectid = inum;
666 key.type = BTRFS_INODE_ITEM_KEY;
667 key.offset = 0;
668
669 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200670 if (ret) {
Josef Bacik00246522020-01-24 09:33:01 -0500671 btrfs_put_root(local_root);
Jan Schmidt558540c2011-06-13 19:59:12 +0200672 btrfs_release_path(swarn->path);
673 goto err;
674 }
675
676 eb = swarn->path->nodes[0];
677 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
678 struct btrfs_inode_item);
679 isize = btrfs_inode_size(eb, inode_item);
680 nlink = btrfs_inode_nlink(eb, inode_item);
681 btrfs_release_path(swarn->path);
682
David Sterbade2491f2017-05-31 19:21:38 +0200683 /*
684 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
685 * uses GFP_NOFS in this context, so we keep it consistent but it does
686 * not seem to be strictly necessary.
687 */
688 nofs_flag = memalloc_nofs_save();
Jan Schmidt558540c2011-06-13 19:59:12 +0200689 ipath = init_ipath(4096, local_root, swarn->path);
David Sterbade2491f2017-05-31 19:21:38 +0200690 memalloc_nofs_restore(nofs_flag);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300691 if (IS_ERR(ipath)) {
Josef Bacik00246522020-01-24 09:33:01 -0500692 btrfs_put_root(local_root);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300693 ret = PTR_ERR(ipath);
694 ipath = NULL;
695 goto err;
696 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200697 ret = paths_from_inode(inum, ipath);
698
699 if (ret < 0)
700 goto err;
701
702 /*
703 * we deliberately ignore the bit ipath might have been too small to
704 * hold all of the paths here
705 */
706 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400707 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200708"%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 -0400709 swarn->errstr, swarn->logical,
710 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200711 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400712 root, inum, offset,
713 min(isize - offset, (u64)PAGE_SIZE), nlink,
714 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200715
Josef Bacik00246522020-01-24 09:33:01 -0500716 btrfs_put_root(local_root);
Jan Schmidt558540c2011-06-13 19:59:12 +0200717 free_ipath(ipath);
718 return 0;
719
720err:
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400721 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200722 "%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 -0400723 swarn->errstr, swarn->logical,
724 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200725 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400726 root, inum, offset, ret);
Jan Schmidt558540c2011-06-13 19:59:12 +0200727
728 free_ipath(ipath);
729 return 0;
730}
731
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400732static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200733{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100734 struct btrfs_device *dev;
735 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200736 struct btrfs_path *path;
737 struct btrfs_key found_key;
738 struct extent_buffer *eb;
739 struct btrfs_extent_item *ei;
740 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200741 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100742 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600743 u64 flags = 0;
744 u64 ref_root;
745 u32 item_size;
Dan Carpenter07c9a8e2016-03-11 11:08:56 +0300746 u8 ref_level = 0;
Liu Bo69917e42012-09-07 20:01:28 -0600747 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200748
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100749 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100750 dev = sblock->pagev[0]->dev;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400751 fs_info = sblock->sctx->fs_info;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100752
Jan Schmidt558540c2011-06-13 19:59:12 +0200753 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200754 if (!path)
755 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200756
David Sterba6aa21262017-10-04 17:07:07 +0200757 swarn.physical = sblock->pagev[0]->physical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100758 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200759 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100760 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200761
Liu Bo69917e42012-09-07 20:01:28 -0600762 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
763 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200764 if (ret < 0)
765 goto out;
766
Jan Schmidt4692cf52011-12-02 14:56:41 +0100767 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200768 swarn.extent_item_size = found_key.offset;
769
770 eb = path->nodes[0];
771 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
772 item_size = btrfs_item_size_nr(eb, path->slots[0]);
773
Liu Bo69917e42012-09-07 20:01:28 -0600774 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200775 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800776 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
777 item_size, &ref_root,
778 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200779 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200780"%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400781 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400782 rcu_str_deref(dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200783 swarn.physical,
Jan Schmidt558540c2011-06-13 19:59:12 +0200784 ref_level ? "node" : "leaf",
785 ret < 0 ? -1 : ref_level,
786 ret < 0 ? -1 : ref_root);
787 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600788 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200789 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600790 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200791 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100792 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100793 iterate_extent_inodes(fs_info, found_key.objectid,
794 extent_item_pos, 1,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400795 scrub_print_warning_inode, &swarn, false);
Jan Schmidt558540c2011-06-13 19:59:12 +0200796 }
797
798out:
799 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200800}
801
Miao Xieaf8e2d12014-10-23 14:42:50 +0800802static inline void scrub_get_recover(struct scrub_recover *recover)
803{
Elena Reshetova6f615012017-03-03 10:55:21 +0200804 refcount_inc(&recover->refs);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800805}
806
Qu Wenruoe501bfe2017-03-29 09:33:22 +0800807static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
808 struct scrub_recover *recover)
Miao Xieaf8e2d12014-10-23 14:42:50 +0800809{
Elena Reshetova6f615012017-03-03 10:55:21 +0200810 if (refcount_dec_and_test(&recover->refs)) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +0800811 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +0800812 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800813 kfree(recover);
814 }
815}
816
Arne Jansena2de7332011-03-08 14:14:00 +0100817/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400818 * scrub_handle_errored_block gets called when either verification of the
819 * pages failed or the bio failed to read, e.g. with EIO. In the latter
820 * case, this function handles all pages in the bio, even though only one
821 * may be bad.
822 * The goal of this function is to repair the errored block by using the
823 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100824 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400825static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100826{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100827 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100828 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400829 struct btrfs_fs_info *fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400830 u64 logical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400831 unsigned int failed_mirror_index;
832 unsigned int is_metadata;
833 unsigned int have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400834 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
835 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100836 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400837 int mirror_index;
838 int page_num;
839 int success;
Qu Wenruo28d70e22017-04-14 08:35:55 +0800840 bool full_stripe_locked;
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000841 unsigned int nofs_flag;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400842 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
843 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100844
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400845 BUG_ON(sblock_to_check->page_count < 1);
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400846 fs_info = sctx->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000847 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
848 /*
849 * if we find an error in a super block, we just report it.
850 * They will get written with the next transaction commit
851 * anyway
852 */
853 spin_lock(&sctx->stat_lock);
854 ++sctx->stat.super_errors;
855 spin_unlock(&sctx->stat_lock);
856 return 0;
857 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100858 logical = sblock_to_check->pagev[0]->logical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100859 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
860 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
861 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400862 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100863 have_csum = sblock_to_check->pagev[0]->have_csum;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100864 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400865
Qu Wenruo28d70e22017-04-14 08:35:55 +0800866 /*
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000867 * We must use GFP_NOFS because the scrub task might be waiting for a
868 * worker task executing this function and in turn a transaction commit
869 * might be waiting the scrub task to pause (which needs to wait for all
870 * the worker tasks to complete before pausing).
871 * We do allocations in the workers through insert_full_stripe_lock()
872 * and scrub_add_page_to_wr_bio(), which happens down the call chain of
873 * this function.
874 */
875 nofs_flag = memalloc_nofs_save();
876 /*
Qu Wenruo28d70e22017-04-14 08:35:55 +0800877 * For RAID5/6, race can happen for a different device scrub thread.
878 * For data corruption, Parity and Data threads will both try
879 * to recovery the data.
880 * Race can lead to doubly added csum error, or even unrecoverable
881 * error.
882 */
883 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
884 if (ret < 0) {
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000885 memalloc_nofs_restore(nofs_flag);
Qu Wenruo28d70e22017-04-14 08:35:55 +0800886 spin_lock(&sctx->stat_lock);
887 if (ret == -ENOMEM)
888 sctx->stat.malloc_errors++;
889 sctx->stat.read_errors++;
890 sctx->stat.uncorrectable_errors++;
891 spin_unlock(&sctx->stat_lock);
892 return ret;
893 }
894
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400895 /*
896 * read all mirrors one after the other. This includes to
897 * re-read the extent or metadata block that failed (that was
898 * the cause that this fixup code is called) another time,
899 * page by page this time in order to know which pages
900 * caused I/O errors and which ones are good (for all mirrors).
901 * It is the goal to handle the situation when more than one
902 * mirror contains I/O errors, but the errors do not
903 * overlap, i.e. the data can be repaired by selecting the
904 * pages from those mirrors without I/O error on the
905 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
906 * would be that mirror #1 has an I/O error on the first page,
907 * the second page is good, and mirror #2 has an I/O error on
908 * the second page, but the first page is good.
909 * Then the first page of the first mirror can be repaired by
910 * taking the first page of the second mirror, and the
911 * second page of the second mirror can be repaired by
912 * copying the contents of the 2nd page of the 1st mirror.
913 * One more note: if the pages of one mirror contain I/O
914 * errors, the checksum cannot be verified. In order to get
915 * the best data for repairing, the first attempt is to find
916 * a mirror without I/O errors and with a validated checksum.
917 * Only if this is not possible, the pages are picked from
918 * mirrors with I/O errors without considering the checksum.
919 * If the latter is the case, at the end, the checksum of the
920 * repaired area is verified in order to correctly maintain
921 * the statistics.
922 */
923
David Sterba31e818f2015-02-20 18:00:26 +0100924 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000925 sizeof(*sblocks_for_recheck), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400926 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100927 spin_lock(&sctx->stat_lock);
928 sctx->stat.malloc_errors++;
929 sctx->stat.read_errors++;
930 sctx->stat.uncorrectable_errors++;
931 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100932 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400933 goto out;
934 }
935
936 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800937 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400938 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100939 spin_lock(&sctx->stat_lock);
940 sctx->stat.read_errors++;
941 sctx->stat.uncorrectable_errors++;
942 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100943 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400944 goto out;
945 }
946 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
947 sblock_bad = sblocks_for_recheck + failed_mirror_index;
948
949 /* build and submit the bios for the failed mirror, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +0800950 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400951
952 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
953 sblock_bad->no_io_error_seen) {
954 /*
955 * the error disappeared after reading page by page, or
956 * the area was part of a huge bio and other parts of the
957 * bio caused I/O errors, or the block layer merged several
958 * read requests into one and the error is caused by a
959 * different bio (usually one of the two latter cases is
960 * the cause)
961 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100962 spin_lock(&sctx->stat_lock);
963 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800964 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100965 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400966
Stefan Behrensff023aa2012-11-06 11:43:11 +0100967 if (sctx->is_dev_replace)
968 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400969 goto out;
970 }
971
972 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100973 spin_lock(&sctx->stat_lock);
974 sctx->stat.read_errors++;
975 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400976 if (__ratelimit(&_rs))
977 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100978 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400979 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100980 spin_lock(&sctx->stat_lock);
981 sctx->stat.csum_errors++;
982 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400983 if (__ratelimit(&_rs))
984 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100985 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200986 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400987 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100988 spin_lock(&sctx->stat_lock);
989 sctx->stat.verify_errors++;
990 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400991 if (__ratelimit(&_rs))
992 scrub_print_warning("checksum/header error",
993 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200994 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100995 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200996 BTRFS_DEV_STAT_GENERATION_ERRS);
997 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100998 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200999 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001000 }
1001
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001002 if (sctx->readonly) {
1003 ASSERT(!sctx->is_dev_replace);
1004 goto out;
1005 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001006
Qu Wenruo665d4952018-07-11 13:41:21 +08001007 /*
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001008 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001009 * checksums.
1010 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001011 * errors and also does not have a checksum error.
1012 * If one is found, and if a checksum is present, the full block
1013 * that is known to contain an error is rewritten. Afterwards
1014 * the block is known to be corrected.
1015 * If a mirror is found which is completely correct, and no
1016 * checksum is present, only those pages are rewritten that had
1017 * an I/O error in the block to be repaired, since it cannot be
1018 * determined, which copy of the other pages is better (and it
1019 * could happen otherwise that a correct page would be
1020 * overwritten by a bad one).
1021 */
Liu Bo762221f2018-01-02 13:36:42 -07001022 for (mirror_index = 0; ;mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001023 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001024
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001025 if (mirror_index == failed_mirror_index)
1026 continue;
Liu Bo762221f2018-01-02 13:36:42 -07001027
1028 /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
1029 if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1030 if (mirror_index >= BTRFS_MAX_MIRRORS)
1031 break;
1032 if (!sblocks_for_recheck[mirror_index].page_count)
1033 break;
1034
1035 sblock_other = sblocks_for_recheck + mirror_index;
1036 } else {
1037 struct scrub_recover *r = sblock_bad->pagev[0]->recover;
1038 int max_allowed = r->bbio->num_stripes -
1039 r->bbio->num_tgtdevs;
1040
1041 if (mirror_index >= max_allowed)
1042 break;
1043 if (!sblocks_for_recheck[1].page_count)
1044 break;
1045
1046 ASSERT(failed_mirror_index == 0);
1047 sblock_other = sblocks_for_recheck + 1;
1048 sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
1049 }
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001050
1051 /* build and submit the bios, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001052 scrub_recheck_block(fs_info, sblock_other, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001053
1054 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001055 !sblock_other->checksum_error &&
1056 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001057 if (sctx->is_dev_replace) {
1058 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001059 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001060 } else {
1061 ret = scrub_repair_block_from_good_copy(
1062 sblock_bad, sblock_other);
1063 if (!ret)
1064 goto corrected_error;
1065 }
Arne Jansena2de7332011-03-08 14:14:00 +01001066 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001067 }
1068
Zhao Leib968fed2015-01-20 15:11:41 +08001069 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1070 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001071
1072 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001073 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001074 * repaired, continue by picking good copies of those pages.
1075 * Select the good pages from mirrors to rewrite bad pages from
1076 * the area to fix. Afterwards verify the checksum of the block
1077 * that is supposed to be repaired. This verification step is
1078 * only done for the purpose of statistic counting and for the
1079 * final scrub report, whether errors remain.
1080 * A perfect algorithm could make use of the checksum and try
1081 * all possible combinations of pages from the different mirrors
1082 * until the checksum verification succeeds. For example, when
1083 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1084 * of mirror #2 is readable but the final checksum test fails,
1085 * then the 2nd page of mirror #3 could be tried, whether now
Nicholas D Steeves01327612016-05-19 21:18:45 -04001086 * the final checksum succeeds. But this would be a rare
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001087 * exception and is therefore not implemented. At least it is
1088 * avoided that the good copy is overwritten.
1089 * A more useful improvement would be to pick the sectors
1090 * without I/O error based on sector sizes (512 bytes on legacy
1091 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1092 * mirror could be repaired by taking 512 byte of a different
1093 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1094 * area are unreadable.
1095 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001096 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001097 for (page_num = 0; page_num < sblock_bad->page_count;
1098 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001099 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001100 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001101
Zhao Leib968fed2015-01-20 15:11:41 +08001102 /* skip no-io-error page in scrub */
1103 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001104 continue;
1105
Liu Bo47597002018-03-02 16:10:41 -07001106 if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1107 /*
1108 * In case of dev replace, if raid56 rebuild process
1109 * didn't work out correct data, then copy the content
1110 * in sblock_bad to make sure target device is identical
1111 * to source device, instead of writing garbage data in
1112 * sblock_for_recheck array to target device.
1113 */
1114 sblock_other = NULL;
1115 } else if (page_bad->io_error) {
1116 /* try to find no-io-error page in mirrors */
Zhao Leib968fed2015-01-20 15:11:41 +08001117 for (mirror_index = 0;
1118 mirror_index < BTRFS_MAX_MIRRORS &&
1119 sblocks_for_recheck[mirror_index].page_count > 0;
1120 mirror_index++) {
1121 if (!sblocks_for_recheck[mirror_index].
1122 pagev[page_num]->io_error) {
1123 sblock_other = sblocks_for_recheck +
1124 mirror_index;
1125 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001126 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001127 }
Zhao Leib968fed2015-01-20 15:11:41 +08001128 if (!sblock_other)
1129 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001130 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001131
Zhao Leib968fed2015-01-20 15:11:41 +08001132 if (sctx->is_dev_replace) {
1133 /*
1134 * did not find a mirror to fetch the page
1135 * from. scrub_write_page_to_dev_replace()
1136 * handles this case (page->io_error), by
1137 * filling the block with zeros before
1138 * submitting the write request
1139 */
1140 if (!sblock_other)
1141 sblock_other = sblock_bad;
1142
1143 if (scrub_write_page_to_dev_replace(sblock_other,
1144 page_num) != 0) {
David Sterbae37abe92018-04-04 17:20:52 +02001145 atomic64_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001146 &fs_info->dev_replace.num_write_errors);
Zhao Leib968fed2015-01-20 15:11:41 +08001147 success = 0;
1148 }
1149 } else if (sblock_other) {
1150 ret = scrub_repair_page_from_good_copy(sblock_bad,
1151 sblock_other,
1152 page_num, 0);
1153 if (0 == ret)
1154 page_bad->io_error = 0;
1155 else
1156 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001157 }
1158 }
1159
Zhao Leib968fed2015-01-20 15:11:41 +08001160 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001161 if (is_metadata || have_csum) {
1162 /*
1163 * need to verify the checksum now that all
1164 * sectors on disk are repaired (the write
1165 * request for data to be repaired is on its way).
1166 * Just be lazy and use scrub_recheck_block()
1167 * which re-reads the data before the checksum
1168 * is verified, but most likely the data comes out
1169 * of the page cache.
1170 */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001171 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001172 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001173 !sblock_bad->checksum_error &&
1174 sblock_bad->no_io_error_seen)
1175 goto corrected_error;
1176 else
1177 goto did_not_correct_error;
1178 } else {
1179corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001180 spin_lock(&sctx->stat_lock);
1181 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001182 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001183 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001184 btrfs_err_rl_in_rcu(fs_info,
1185 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001186 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001187 }
1188 } else {
1189did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001190 spin_lock(&sctx->stat_lock);
1191 sctx->stat.uncorrectable_errors++;
1192 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001193 btrfs_err_rl_in_rcu(fs_info,
1194 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001195 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001196 }
1197
1198out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001199 if (sblocks_for_recheck) {
1200 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1201 mirror_index++) {
1202 struct scrub_block *sblock = sblocks_for_recheck +
1203 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001204 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001205 int page_index;
1206
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001207 for (page_index = 0; page_index < sblock->page_count;
1208 page_index++) {
1209 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001210 recover = sblock->pagev[page_index]->recover;
1211 if (recover) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001212 scrub_put_recover(fs_info, recover);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001213 sblock->pagev[page_index]->recover =
1214 NULL;
1215 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001216 scrub_page_put(sblock->pagev[page_index]);
1217 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001218 }
1219 kfree(sblocks_for_recheck);
1220 }
1221
Qu Wenruo28d70e22017-04-14 08:35:55 +08001222 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
Filipe Manana7c3c7cb2018-12-07 13:23:32 +00001223 memalloc_nofs_restore(nofs_flag);
Qu Wenruo28d70e22017-04-14 08:35:55 +08001224 if (ret < 0)
1225 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001226 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001227}
1228
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001229static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001230{
Zhao Lei10f11902015-01-20 15:11:43 +08001231 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1232 return 2;
1233 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1234 return 3;
1235 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001236 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001237}
1238
Zhao Lei10f11902015-01-20 15:11:43 +08001239static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1240 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001241 u64 mapped_length,
1242 int nstripes, int mirror,
1243 int *stripe_index,
1244 u64 *stripe_offset)
1245{
1246 int i;
1247
Zhao Leiffe2d202015-01-20 15:11:44 +08001248 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001249 /* RAID5/6 */
1250 for (i = 0; i < nstripes; i++) {
1251 if (raid_map[i] == RAID6_Q_STRIPE ||
1252 raid_map[i] == RAID5_P_STRIPE)
1253 continue;
1254
1255 if (logical >= raid_map[i] &&
1256 logical < raid_map[i] + mapped_length)
1257 break;
1258 }
1259
1260 *stripe_index = i;
1261 *stripe_offset = logical - raid_map[i];
1262 } else {
1263 /* The other RAID type */
1264 *stripe_index = mirror;
1265 *stripe_offset = 0;
1266 }
1267}
1268
Zhao Leibe50a8d2015-01-20 15:11:42 +08001269static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001270 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001271{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001272 struct scrub_ctx *sctx = original_sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001273 struct btrfs_fs_info *fs_info = sctx->fs_info;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001274 u64 length = original_sblock->page_count * PAGE_SIZE;
1275 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001276 u64 generation = original_sblock->pagev[0]->generation;
1277 u64 flags = original_sblock->pagev[0]->flags;
1278 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001279 struct scrub_recover *recover;
1280 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001281 u64 sublen;
1282 u64 mapped_length;
1283 u64 stripe_offset;
1284 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001285 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001286 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001287 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001288 int ret;
1289
1290 /*
Zhao Lei57019342015-01-20 15:11:45 +08001291 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001292 * are not used (and not set) in the blocks that are used for
1293 * the recheck procedure
1294 */
1295
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001296 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001297 sublen = min_t(u64, length, PAGE_SIZE);
1298 mapped_length = sublen;
1299 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001300
1301 /*
1302 * with a length of PAGE_SIZE, each returned stripe
1303 * represents one mirror
1304 */
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001305 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001306 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
David Sterba825ad4c2017-03-28 14:45:22 +02001307 logical, &mapped_length, &bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001308 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001309 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001310 btrfs_bio_counter_dec(fs_info);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001311 return -EIO;
1312 }
1313
Miao Xieaf8e2d12014-10-23 14:42:50 +08001314 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1315 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001316 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001317 btrfs_bio_counter_dec(fs_info);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001318 return -ENOMEM;
1319 }
1320
Elena Reshetova6f615012017-03-03 10:55:21 +02001321 refcount_set(&recover->refs, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001322 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001323 recover->map_length = mapped_length;
1324
Ashish Samant24731142016-04-29 18:33:59 -07001325 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001326
Zhao Leibe50a8d2015-01-20 15:11:42 +08001327 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001328
Miao Xieaf8e2d12014-10-23 14:42:50 +08001329 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001330 mirror_index++) {
1331 struct scrub_block *sblock;
1332 struct scrub_page *page;
1333
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001334 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001335 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001336
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001337 page = kzalloc(sizeof(*page), GFP_NOFS);
1338 if (!page) {
1339leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001340 spin_lock(&sctx->stat_lock);
1341 sctx->stat.malloc_errors++;
1342 spin_unlock(&sctx->stat_lock);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001343 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001344 return -ENOMEM;
1345 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001346 scrub_page_get(page);
1347 sblock->pagev[page_index] = page;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001348 page->sblock = sblock;
1349 page->flags = flags;
1350 page->generation = generation;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001351 page->logical = logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001352 page->have_csum = have_csum;
1353 if (have_csum)
1354 memcpy(page->csum,
1355 original_sblock->pagev[0]->csum,
1356 sctx->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001357
Zhao Lei10f11902015-01-20 15:11:43 +08001358 scrub_stripe_index_and_offset(logical,
1359 bbio->map_type,
1360 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001361 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001362 bbio->num_stripes -
1363 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001364 mirror_index,
1365 &stripe_index,
1366 &stripe_offset);
1367 page->physical = bbio->stripes[stripe_index].physical +
1368 stripe_offset;
1369 page->dev = bbio->stripes[stripe_index].dev;
1370
Stefan Behrensff023aa2012-11-06 11:43:11 +01001371 BUG_ON(page_index >= original_sblock->page_count);
1372 page->physical_for_dev_replace =
1373 original_sblock->pagev[page_index]->
1374 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001375 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001376 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001377 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001378 page->page = alloc_page(GFP_NOFS);
1379 if (!page->page)
1380 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001381
1382 scrub_get_recover(recover);
1383 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001384 }
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001385 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001386 length -= sublen;
1387 logical += sublen;
1388 page_index++;
1389 }
1390
1391 return 0;
1392}
1393
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001394static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001395{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001396 complete(bio->bi_private);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001397}
1398
Miao Xieaf8e2d12014-10-23 14:42:50 +08001399static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1400 struct bio *bio,
1401 struct scrub_page *page)
1402{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001403 DECLARE_COMPLETION_ONSTACK(done);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001404 int ret;
Liu Bo762221f2018-01-02 13:36:42 -07001405 int mirror_num;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001406
Miao Xieaf8e2d12014-10-23 14:42:50 +08001407 bio->bi_iter.bi_sector = page->logical >> 9;
1408 bio->bi_private = &done;
1409 bio->bi_end_io = scrub_bio_wait_endio;
1410
Liu Bo762221f2018-01-02 13:36:42 -07001411 mirror_num = page->sblock->pagev[0]->mirror_num;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001412 ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001413 page->recover->map_length,
Liu Bo762221f2018-01-02 13:36:42 -07001414 mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001415 if (ret)
1416 return ret;
1417
Liu Bob4ff5ad2017-11-30 17:26:39 -07001418 wait_for_completion_io(&done);
1419 return blk_status_to_errno(bio->bi_status);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001420}
1421
Liu Bo6ca17652018-03-07 12:08:09 -07001422static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
1423 struct scrub_block *sblock)
1424{
1425 struct scrub_page *first_page = sblock->pagev[0];
1426 struct bio *bio;
1427 int page_num;
1428
1429 /* All pages in sblock belong to the same stripe on the same device. */
1430 ASSERT(first_page->dev);
1431 if (!first_page->dev->bdev)
1432 goto out;
1433
1434 bio = btrfs_io_bio_alloc(BIO_MAX_PAGES);
1435 bio_set_dev(bio, first_page->dev->bdev);
1436
1437 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1438 struct scrub_page *page = sblock->pagev[page_num];
1439
1440 WARN_ON(!page->page);
1441 bio_add_page(bio, page->page, PAGE_SIZE, 0);
1442 }
1443
1444 if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) {
1445 bio_put(bio);
1446 goto out;
1447 }
1448
1449 bio_put(bio);
1450
1451 scrub_recheck_block_checksum(sblock);
1452
1453 return;
1454out:
1455 for (page_num = 0; page_num < sblock->page_count; page_num++)
1456 sblock->pagev[page_num]->io_error = 1;
1457
1458 sblock->no_io_error_seen = 0;
1459}
1460
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001461/*
1462 * this function will check the on disk data for checksum errors, header
1463 * errors and read I/O errors. If any I/O errors happen, the exact pages
1464 * which are errored are marked as being bad. The goal is to enable scrub
1465 * to take those pages that are not errored from all the mirrors so that
1466 * the pages that are errored in the just handled mirror can be repaired.
1467 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001468static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001469 struct scrub_block *sblock,
1470 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001471{
1472 int page_num;
1473
1474 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001475
Liu Bo6ca17652018-03-07 12:08:09 -07001476 /* short cut for raid56 */
1477 if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0]))
1478 return scrub_recheck_block_on_raid56(fs_info, sblock);
1479
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001480 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1481 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001482 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001483
Stefan Behrens442a4f62012-05-25 16:06:08 +02001484 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001485 page->io_error = 1;
1486 sblock->no_io_error_seen = 0;
1487 continue;
1488 }
1489
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001490 WARN_ON(!page->page);
David Sterbac5e4c3d2017-06-12 17:29:41 +02001491 bio = btrfs_io_bio_alloc(1);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001492 bio_set_dev(bio, page->dev->bdev);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001493
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001494 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Liu Bo6ca17652018-03-07 12:08:09 -07001495 bio->bi_iter.bi_sector = page->physical >> 9;
1496 bio->bi_opf = REQ_OP_READ;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001497
Liu Bo6ca17652018-03-07 12:08:09 -07001498 if (btrfsic_submit_bio_wait(bio)) {
1499 page->io_error = 1;
1500 sblock->no_io_error_seen = 0;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001501 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001502
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001503 bio_put(bio);
1504 }
1505
1506 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001507 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001508}
1509
Miao Xie17a9be22014-07-24 11:37:08 +08001510static inline int scrub_check_fsid(u8 fsid[],
1511 struct scrub_page *spage)
1512{
1513 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1514 int ret;
1515
Anand Jain44880fd2017-07-29 17:50:09 +08001516 ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Miao Xie17a9be22014-07-24 11:37:08 +08001517 return !ret;
1518}
1519
Zhao Leiba7cf982015-08-24 21:18:02 +08001520static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001521{
Zhao Leiba7cf982015-08-24 21:18:02 +08001522 sblock->header_error = 0;
1523 sblock->checksum_error = 0;
1524 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001525
Zhao Leiba7cf982015-08-24 21:18:02 +08001526 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1527 scrub_checksum_data(sblock);
1528 else
1529 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001530}
1531
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001532static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001533 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001534{
1535 int page_num;
1536 int ret = 0;
1537
1538 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1539 int ret_sub;
1540
1541 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1542 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001543 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001544 if (ret_sub)
1545 ret = ret_sub;
1546 }
1547
1548 return ret;
1549}
1550
1551static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1552 struct scrub_block *sblock_good,
1553 int page_num, int force_write)
1554{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001555 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1556 struct scrub_page *page_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001557 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001558
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001559 BUG_ON(page_bad->page == NULL);
1560 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001561 if (force_write || sblock_bad->header_error ||
1562 sblock_bad->checksum_error || page_bad->io_error) {
1563 struct bio *bio;
1564 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001565
Stefan Behrensff023aa2012-11-06 11:43:11 +01001566 if (!page_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001567 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001568 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001569 return -EIO;
1570 }
1571
David Sterbac5e4c3d2017-06-12 17:29:41 +02001572 bio = btrfs_io_bio_alloc(1);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001573 bio_set_dev(bio, page_bad->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001574 bio->bi_iter.bi_sector = page_bad->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02001575 bio->bi_opf = REQ_OP_WRITE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001576
1577 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1578 if (PAGE_SIZE != ret) {
1579 bio_put(bio);
1580 return -EIO;
1581 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001582
Mike Christie4e49ea42016-06-05 14:31:41 -05001583 if (btrfsic_submit_bio_wait(bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001584 btrfs_dev_stat_inc_and_print(page_bad->dev,
1585 BTRFS_DEV_STAT_WRITE_ERRS);
David Sterbae37abe92018-04-04 17:20:52 +02001586 atomic64_inc(&fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001587 bio_put(bio);
1588 return -EIO;
1589 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001590 bio_put(bio);
1591 }
1592
1593 return 0;
1594}
1595
Stefan Behrensff023aa2012-11-06 11:43:11 +01001596static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1597{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001598 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001599 int page_num;
1600
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001601 /*
1602 * This block is used for the check of the parity on the source device,
1603 * so the data needn't be written into the destination device.
1604 */
1605 if (sblock->sparity)
1606 return;
1607
Stefan Behrensff023aa2012-11-06 11:43:11 +01001608 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1609 int ret;
1610
1611 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1612 if (ret)
David Sterbae37abe92018-04-04 17:20:52 +02001613 atomic64_inc(&fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001614 }
1615}
1616
1617static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1618 int page_num)
1619{
1620 struct scrub_page *spage = sblock->pagev[page_num];
1621
1622 BUG_ON(spage->page == NULL);
1623 if (spage->io_error) {
1624 void *mapped_buffer = kmap_atomic(spage->page);
1625
David Sterba619a9742017-03-29 20:48:44 +02001626 clear_page(mapped_buffer);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001627 flush_dcache_page(spage->page);
1628 kunmap_atomic(mapped_buffer);
1629 }
1630 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1631}
1632
1633static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1634 struct scrub_page *spage)
1635{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001636 struct scrub_bio *sbio;
1637 int ret;
1638
David Sterba3fb99302017-05-16 19:10:32 +02001639 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001640again:
David Sterba3fb99302017-05-16 19:10:32 +02001641 if (!sctx->wr_curr_bio) {
1642 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001643 GFP_KERNEL);
David Sterba3fb99302017-05-16 19:10:32 +02001644 if (!sctx->wr_curr_bio) {
1645 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001646 return -ENOMEM;
1647 }
David Sterba3fb99302017-05-16 19:10:32 +02001648 sctx->wr_curr_bio->sctx = sctx;
1649 sctx->wr_curr_bio->page_count = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001650 }
David Sterba3fb99302017-05-16 19:10:32 +02001651 sbio = sctx->wr_curr_bio;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001652 if (sbio->page_count == 0) {
1653 struct bio *bio;
1654
1655 sbio->physical = spage->physical_for_dev_replace;
1656 sbio->logical = spage->logical;
David Sterba3fb99302017-05-16 19:10:32 +02001657 sbio->dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001658 bio = sbio->bio;
1659 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02001660 bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001661 sbio->bio = bio;
1662 }
1663
1664 bio->bi_private = sbio;
1665 bio->bi_end_io = scrub_wr_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001666 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001667 bio->bi_iter.bi_sector = sbio->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02001668 bio->bi_opf = REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001669 sbio->status = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001670 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1671 spage->physical_for_dev_replace ||
1672 sbio->logical + sbio->page_count * PAGE_SIZE !=
1673 spage->logical) {
1674 scrub_wr_submit(sctx);
1675 goto again;
1676 }
1677
1678 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1679 if (ret != PAGE_SIZE) {
1680 if (sbio->page_count < 1) {
1681 bio_put(sbio->bio);
1682 sbio->bio = NULL;
David Sterba3fb99302017-05-16 19:10:32 +02001683 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001684 return -EIO;
1685 }
1686 scrub_wr_submit(sctx);
1687 goto again;
1688 }
1689
1690 sbio->pagev[sbio->page_count] = spage;
1691 scrub_page_get(spage);
1692 sbio->page_count++;
David Sterba3fb99302017-05-16 19:10:32 +02001693 if (sbio->page_count == sctx->pages_per_wr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001694 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02001695 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001696
1697 return 0;
1698}
1699
1700static void scrub_wr_submit(struct scrub_ctx *sctx)
1701{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001702 struct scrub_bio *sbio;
1703
David Sterba3fb99302017-05-16 19:10:32 +02001704 if (!sctx->wr_curr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001705 return;
1706
David Sterba3fb99302017-05-16 19:10:32 +02001707 sbio = sctx->wr_curr_bio;
1708 sctx->wr_curr_bio = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001709 WARN_ON(!sbio->bio->bi_disk);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001710 scrub_pending_bio_inc(sctx);
1711 /* process all writes in a single worker thread. Then the block layer
1712 * orders the requests before sending them to the driver which
1713 * doubled the write performance on spinning disks when measured
1714 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001715 btrfsic_submit_bio(sbio->bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001716}
1717
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001718static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001719{
1720 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001721 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001722
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001723 sbio->status = bio->bi_status;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001724 sbio->bio = bio;
1725
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001726 btrfs_init_work(&sbio->work, scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001727 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001728}
1729
1730static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1731{
1732 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1733 struct scrub_ctx *sctx = sbio->sctx;
1734 int i;
1735
1736 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001737 if (sbio->status) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001738 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001739 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001740
1741 for (i = 0; i < sbio->page_count; i++) {
1742 struct scrub_page *spage = sbio->pagev[i];
1743
1744 spage->io_error = 1;
David Sterbae37abe92018-04-04 17:20:52 +02001745 atomic64_inc(&dev_replace->num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001746 }
1747 }
1748
1749 for (i = 0; i < sbio->page_count; i++)
1750 scrub_page_put(sbio->pagev[i]);
1751
1752 bio_put(sbio->bio);
1753 kfree(sbio);
1754 scrub_pending_bio_dec(sctx);
1755}
1756
1757static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001758{
1759 u64 flags;
1760 int ret;
1761
Zhao Leiba7cf982015-08-24 21:18:02 +08001762 /*
1763 * No need to initialize these stats currently,
1764 * because this function only use return value
1765 * instead of these stats value.
1766 *
1767 * Todo:
1768 * always use stats
1769 */
1770 sblock->header_error = 0;
1771 sblock->generation_error = 0;
1772 sblock->checksum_error = 0;
1773
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001774 WARN_ON(sblock->page_count < 1);
1775 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001776 ret = 0;
1777 if (flags & BTRFS_EXTENT_FLAG_DATA)
1778 ret = scrub_checksum_data(sblock);
1779 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1780 ret = scrub_checksum_tree_block(sblock);
1781 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1782 (void)scrub_checksum_super(sblock);
1783 else
1784 WARN_ON(1);
1785 if (ret)
1786 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001787
1788 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001789}
1790
1791static int scrub_checksum_data(struct scrub_block *sblock)
1792{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001793 struct scrub_ctx *sctx = sblock->sctx;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001794 struct btrfs_fs_info *fs_info = sctx->fs_info;
1795 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Arne Jansena2de7332011-03-08 14:14:00 +01001796 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001797 u8 *on_disk_csum;
1798 struct page *page;
1799 void *buffer;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001800 u64 len;
1801 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001802
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001803 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001804 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001805 return 0;
1806
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001807 shash->tfm = fs_info->csum_shash;
1808 crypto_shash_init(shash);
1809
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001810 on_disk_csum = sblock->pagev[0]->csum;
1811 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001812 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001813
David Sterba25cc1222017-05-16 19:10:41 +02001814 len = sctx->fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001815 index = 0;
1816 for (;;) {
1817 u64 l = min_t(u64, len, PAGE_SIZE);
1818
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001819 crypto_shash_update(shash, buffer, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001820 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001821 len -= l;
1822 if (len == 0)
1823 break;
1824 index++;
1825 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001826 BUG_ON(!sblock->pagev[index]->page);
1827 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001828 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001829 }
1830
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001831 crypto_shash_final(shash, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001832 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001833 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001834
Zhao Leiba7cf982015-08-24 21:18:02 +08001835 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001836}
1837
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001838static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001839{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001840 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001841 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001842 struct btrfs_fs_info *fs_info = sctx->fs_info;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001843 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001844 u8 calculated_csum[BTRFS_CSUM_SIZE];
1845 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1846 struct page *page;
1847 void *mapped_buffer;
1848 u64 mapped_size;
1849 void *p;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001850 u64 len;
1851 int index;
1852
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001853 shash->tfm = fs_info->csum_shash;
1854 crypto_shash_init(shash);
1855
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001856 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001857 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001858 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001859 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001860 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001861
1862 /*
1863 * we don't use the getter functions here, as we
1864 * a) don't have an extent buffer and
1865 * b) the page is already kmapped
1866 */
Qu Wenruo3cae2102013-07-16 11:19:18 +08001867 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08001868 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001869
Zhao Leiba7cf982015-08-24 21:18:02 +08001870 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
1871 sblock->header_error = 1;
1872 sblock->generation_error = 1;
1873 }
Arne Jansena2de7332011-03-08 14:14:00 +01001874
Miao Xie17a9be22014-07-24 11:37:08 +08001875 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Zhao Leiba7cf982015-08-24 21:18:02 +08001876 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001877
1878 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1879 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08001880 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001881
David Sterba25cc1222017-05-16 19:10:41 +02001882 len = sctx->fs_info->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001883 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1884 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1885 index = 0;
1886 for (;;) {
1887 u64 l = min_t(u64, len, mapped_size);
1888
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001889 crypto_shash_update(shash, p, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001890 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001891 len -= l;
1892 if (len == 0)
1893 break;
1894 index++;
1895 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001896 BUG_ON(!sblock->pagev[index]->page);
1897 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001898 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001899 mapped_size = PAGE_SIZE;
1900 p = mapped_buffer;
1901 }
1902
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001903 crypto_shash_final(shash, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001904 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001905 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001906
Zhao Leiba7cf982015-08-24 21:18:02 +08001907 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001908}
1909
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001910static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001911{
1912 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001913 struct scrub_ctx *sctx = sblock->sctx;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001914 struct btrfs_fs_info *fs_info = sctx->fs_info;
1915 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001916 u8 calculated_csum[BTRFS_CSUM_SIZE];
1917 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1918 struct page *page;
1919 void *mapped_buffer;
1920 u64 mapped_size;
1921 void *p;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001922 int fail_gen = 0;
1923 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001924 u64 len;
1925 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001926
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001927 shash->tfm = fs_info->csum_shash;
1928 crypto_shash_init(shash);
1929
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001930 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001931 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001932 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001933 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001934 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001935
Qu Wenruo3cae2102013-07-16 11:19:18 +08001936 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001937 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001938
Qu Wenruo3cae2102013-07-16 11:19:18 +08001939 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001940 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001941
Miao Xie17a9be22014-07-24 11:37:08 +08001942 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001943 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001944
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001945 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1946 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1947 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1948 index = 0;
1949 for (;;) {
1950 u64 l = min_t(u64, len, mapped_size);
1951
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001952 crypto_shash_update(shash, p, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001953 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001954 len -= l;
1955 if (len == 0)
1956 break;
1957 index++;
1958 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001959 BUG_ON(!sblock->pagev[index]->page);
1960 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001961 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001962 mapped_size = PAGE_SIZE;
1963 p = mapped_buffer;
1964 }
1965
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001966 crypto_shash_final(shash, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001967 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001968 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001969
Stefan Behrens442a4f62012-05-25 16:06:08 +02001970 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001971 /*
1972 * if we find an error in a super block, we just report it.
1973 * They will get written with the next transaction commit
1974 * anyway
1975 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001976 spin_lock(&sctx->stat_lock);
1977 ++sctx->stat.super_errors;
1978 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001979 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001980 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001981 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1982 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001983 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001984 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001985 }
1986
Stefan Behrens442a4f62012-05-25 16:06:08 +02001987 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001988}
1989
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001990static void scrub_block_get(struct scrub_block *sblock)
1991{
Elena Reshetova186debd2017-03-03 10:55:23 +02001992 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001993}
1994
1995static void scrub_block_put(struct scrub_block *sblock)
1996{
Elena Reshetova186debd2017-03-03 10:55:23 +02001997 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001998 int i;
1999
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002000 if (sblock->sparity)
2001 scrub_parity_put(sblock->sparity);
2002
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002003 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002004 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002005 kfree(sblock);
2006 }
2007}
2008
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002009static void scrub_page_get(struct scrub_page *spage)
2010{
Zhao Lei57019342015-01-20 15:11:45 +08002011 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002012}
2013
2014static void scrub_page_put(struct scrub_page *spage)
2015{
Zhao Lei57019342015-01-20 15:11:45 +08002016 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002017 if (spage->page)
2018 __free_page(spage->page);
2019 kfree(spage);
2020 }
2021}
2022
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002023static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002024{
2025 struct scrub_bio *sbio;
2026
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002027 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002028 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002029
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002030 sbio = sctx->bios[sctx->curr];
2031 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002032 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002033 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002034}
2035
Stefan Behrensff023aa2012-11-06 11:43:11 +01002036static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2037 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002038{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002039 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002040 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002041 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002042
2043again:
2044 /*
2045 * grab a fresh bio or wait for one to become available
2046 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002047 while (sctx->curr == -1) {
2048 spin_lock(&sctx->list_lock);
2049 sctx->curr = sctx->first_free;
2050 if (sctx->curr != -1) {
2051 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2052 sctx->bios[sctx->curr]->next_free = -1;
2053 sctx->bios[sctx->curr]->page_count = 0;
2054 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002055 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002056 spin_unlock(&sctx->list_lock);
2057 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002058 }
2059 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002060 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002061 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002062 struct bio *bio;
2063
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002064 sbio->physical = spage->physical;
2065 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002066 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002067 bio = sbio->bio;
2068 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02002069 bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002070 sbio->bio = bio;
2071 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002072
2073 bio->bi_private = sbio;
2074 bio->bi_end_io = scrub_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002075 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002076 bio->bi_iter.bi_sector = sbio->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02002077 bio->bi_opf = REQ_OP_READ;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002078 sbio->status = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002079 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2080 spage->physical ||
2081 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002082 spage->logical ||
2083 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002084 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002085 goto again;
2086 }
2087
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002088 sbio->pagev[sbio->page_count] = spage;
2089 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2090 if (ret != PAGE_SIZE) {
2091 if (sbio->page_count < 1) {
2092 bio_put(sbio->bio);
2093 sbio->bio = NULL;
2094 return -EIO;
2095 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002096 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002097 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002098 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002099
Stefan Behrensff023aa2012-11-06 11:43:11 +01002100 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002101 atomic_inc(&sblock->outstanding_pages);
2102 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002103 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002104 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002105
2106 return 0;
2107}
2108
Linus Torvalds22365972015-09-05 15:14:43 -07002109static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002110{
2111 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002112 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002113
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002114 if (bio->bi_status)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002115 sblock->no_io_error_seen = 0;
2116
Scott Talbert46732722016-05-09 09:14:28 -04002117 bio_put(bio);
2118
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002119 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2120}
2121
2122static void scrub_missing_raid56_worker(struct btrfs_work *work)
2123{
2124 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2125 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002126 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002127 u64 logical;
2128 struct btrfs_device *dev;
2129
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002130 logical = sblock->pagev[0]->logical;
2131 dev = sblock->pagev[0]->dev;
2132
Zhao Leiaffe4a52015-08-24 21:32:06 +08002133 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002134 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002135
2136 if (!sblock->no_io_error_seen) {
2137 spin_lock(&sctx->stat_lock);
2138 sctx->stat.read_errors++;
2139 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002140 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002141 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002142 logical, rcu_str_deref(dev->name));
2143 } else if (sblock->header_error || sblock->checksum_error) {
2144 spin_lock(&sctx->stat_lock);
2145 sctx->stat.uncorrectable_errors++;
2146 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002147 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002148 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002149 logical, rcu_str_deref(dev->name));
2150 } else {
2151 scrub_write_block_to_dev_replace(sblock);
2152 }
2153
David Sterba2073c4c2017-03-31 17:12:51 +02002154 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002155 mutex_lock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002156 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002157 mutex_unlock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002158 }
2159
Omar Sandoval57d4f0b2019-09-16 11:30:56 -07002160 scrub_block_put(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002161 scrub_pending_bio_dec(sctx);
2162}
2163
2164static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2165{
2166 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002167 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002168 u64 length = sblock->page_count * PAGE_SIZE;
2169 u64 logical = sblock->pagev[0]->logical;
Zhao Leif1fee652016-05-17 17:37:38 +08002170 struct btrfs_bio *bbio = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002171 struct bio *bio;
2172 struct btrfs_raid_bio *rbio;
2173 int ret;
2174 int i;
2175
Qu Wenruoae6529c2017-03-29 09:33:21 +08002176 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002177 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
David Sterba825ad4c2017-03-28 14:45:22 +02002178 &length, &bbio);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002179 if (ret || !bbio || !bbio->raid_map)
2180 goto bbio_out;
2181
2182 if (WARN_ON(!sctx->is_dev_replace ||
2183 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2184 /*
2185 * We shouldn't be scrubbing a missing device. Even for dev
2186 * replace, we should only get here for RAID 5/6. We either
2187 * managed to mount something with no mirrors remaining or
2188 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2189 */
2190 goto bbio_out;
2191 }
2192
David Sterbac5e4c3d2017-06-12 17:29:41 +02002193 bio = btrfs_io_bio_alloc(0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002194 bio->bi_iter.bi_sector = logical >> 9;
2195 bio->bi_private = sblock;
2196 bio->bi_end_io = scrub_missing_raid56_end_io;
2197
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002198 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002199 if (!rbio)
2200 goto rbio_out;
2201
2202 for (i = 0; i < sblock->page_count; i++) {
2203 struct scrub_page *spage = sblock->pagev[i];
2204
2205 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2206 }
2207
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002208 btrfs_init_work(&sblock->work, scrub_missing_raid56_worker, NULL, NULL);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002209 scrub_block_get(sblock);
2210 scrub_pending_bio_inc(sctx);
2211 raid56_submit_missing_rbio(rbio);
2212 return;
2213
2214rbio_out:
2215 bio_put(bio);
2216bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002217 btrfs_bio_counter_dec(fs_info);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002218 btrfs_put_bbio(bbio);
2219 spin_lock(&sctx->stat_lock);
2220 sctx->stat.malloc_errors++;
2221 spin_unlock(&sctx->stat_lock);
2222}
2223
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002224static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002225 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002226 u64 gen, int mirror_num, u8 *csum, int force,
2227 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002228{
2229 struct scrub_block *sblock;
2230 int index;
2231
David Sterba58c4e172016-02-11 10:49:42 +01002232 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002233 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002234 spin_lock(&sctx->stat_lock);
2235 sctx->stat.malloc_errors++;
2236 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002237 return -ENOMEM;
2238 }
2239
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002240 /* one ref inside this function, plus one for each page added to
2241 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002242 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002243 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002244 sblock->no_io_error_seen = 1;
2245
2246 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002247 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002248 u64 l = min_t(u64, len, PAGE_SIZE);
2249
David Sterba58c4e172016-02-11 10:49:42 +01002250 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002251 if (!spage) {
2252leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002253 spin_lock(&sctx->stat_lock);
2254 sctx->stat.malloc_errors++;
2255 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002256 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002257 return -ENOMEM;
2258 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002259 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2260 scrub_page_get(spage);
2261 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002262 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002263 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002264 spage->flags = flags;
2265 spage->generation = gen;
2266 spage->logical = logical;
2267 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002268 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002269 spage->mirror_num = mirror_num;
2270 if (csum) {
2271 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002272 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002273 } else {
2274 spage->have_csum = 0;
2275 }
2276 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002277 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002278 if (!spage->page)
2279 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002280 len -= l;
2281 logical += l;
2282 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002283 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002284 }
2285
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002286 WARN_ON(sblock->page_count == 0);
Anand Jaine6e674b2017-12-04 12:54:54 +08002287 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002288 /*
2289 * This case should only be hit for RAID 5/6 device replace. See
2290 * the comment in scrub_missing_raid56_pages() for details.
2291 */
2292 scrub_missing_raid56_pages(sblock);
2293 } else {
2294 for (index = 0; index < sblock->page_count; index++) {
2295 struct scrub_page *spage = sblock->pagev[index];
2296 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002297
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002298 ret = scrub_add_page_to_rd_bio(sctx, spage);
2299 if (ret) {
2300 scrub_block_put(sblock);
2301 return ret;
2302 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002303 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002304
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002305 if (force)
2306 scrub_submit(sctx);
2307 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002308
2309 /* last one frees, either here or in bio completion for last page */
2310 scrub_block_put(sblock);
2311 return 0;
2312}
2313
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002314static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002315{
2316 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002317 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002318
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002319 sbio->status = bio->bi_status;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002320 sbio->bio = bio;
2321
Qu Wenruo0339ef22014-02-28 10:46:17 +08002322 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002323}
2324
2325static void scrub_bio_end_io_worker(struct btrfs_work *work)
2326{
2327 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002328 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002329 int i;
2330
Stefan Behrensff023aa2012-11-06 11:43:11 +01002331 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002332 if (sbio->status) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002333 for (i = 0; i < sbio->page_count; i++) {
2334 struct scrub_page *spage = sbio->pagev[i];
2335
2336 spage->io_error = 1;
2337 spage->sblock->no_io_error_seen = 0;
2338 }
2339 }
2340
2341 /* now complete the scrub_block items that have all pages completed */
2342 for (i = 0; i < sbio->page_count; i++) {
2343 struct scrub_page *spage = sbio->pagev[i];
2344 struct scrub_block *sblock = spage->sblock;
2345
2346 if (atomic_dec_and_test(&sblock->outstanding_pages))
2347 scrub_block_complete(sblock);
2348 scrub_block_put(sblock);
2349 }
2350
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002351 bio_put(sbio->bio);
2352 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002353 spin_lock(&sctx->list_lock);
2354 sbio->next_free = sctx->first_free;
2355 sctx->first_free = sbio->index;
2356 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002357
David Sterba2073c4c2017-03-31 17:12:51 +02002358 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002359 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002360 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002361 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002362 }
2363
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002364 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002365}
2366
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002367static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2368 unsigned long *bitmap,
2369 u64 start, u64 len)
2370{
Liu Bo972d7212017-04-03 13:45:33 -07002371 u64 offset;
David Sterba7736b0a2017-03-31 18:02:48 +02002372 u64 nsectors64;
2373 u32 nsectors;
Jeff Mahoneyda170662016-06-15 09:22:56 -04002374 int sectorsize = sparity->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002375
2376 if (len >= sparity->stripe_len) {
2377 bitmap_set(bitmap, 0, sparity->nsectors);
2378 return;
2379 }
2380
2381 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002382 start = div64_u64_rem(start, sparity->stripe_len, &offset);
2383 offset = div_u64(offset, sectorsize);
David Sterba7736b0a2017-03-31 18:02:48 +02002384 nsectors64 = div_u64(len, sectorsize);
2385
2386 ASSERT(nsectors64 < UINT_MAX);
2387 nsectors = (u32)nsectors64;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002388
2389 if (offset + nsectors <= sparity->nsectors) {
2390 bitmap_set(bitmap, offset, nsectors);
2391 return;
2392 }
2393
2394 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2395 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2396}
2397
2398static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2399 u64 start, u64 len)
2400{
2401 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2402}
2403
2404static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2405 u64 start, u64 len)
2406{
2407 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2408}
2409
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002410static void scrub_block_complete(struct scrub_block *sblock)
2411{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002412 int corrupted = 0;
2413
Stefan Behrensff023aa2012-11-06 11:43:11 +01002414 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002415 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002416 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002417 } else {
2418 /*
2419 * if has checksum error, write via repair mechanism in
2420 * dev replace case, otherwise write here in dev replace
2421 * case.
2422 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002423 corrupted = scrub_checksum(sblock);
2424 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002425 scrub_write_block_to_dev_replace(sblock);
2426 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002427
2428 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2429 u64 start = sblock->pagev[0]->logical;
2430 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2431 PAGE_SIZE;
2432
2433 scrub_parity_mark_sectors_error(sblock->sparity,
2434 start, end - start);
2435 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002436}
2437
Zhao Lei3b5753e2015-08-24 22:03:02 +08002438static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002439{
2440 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002441 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002442 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002443
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002444 while (!list_empty(&sctx->csum_list)) {
2445 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002446 struct btrfs_ordered_sum, list);
2447 if (sum->bytenr > logical)
2448 return 0;
2449 if (sum->bytenr + sum->len > logical)
2450 break;
2451
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002452 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002453 list_del(&sum->list);
2454 kfree(sum);
2455 sum = NULL;
2456 }
2457 if (!sum)
2458 return 0;
2459
David Sterba1d1bf922017-03-31 18:02:48 +02002460 index = div_u64(logical - sum->bytenr, sctx->fs_info->sectorsize);
2461 ASSERT(index < UINT_MAX);
2462
David Sterba25cc1222017-05-16 19:10:41 +02002463 num_sectors = sum->len / sctx->fs_info->sectorsize;
Johannes Thumshirn1e25a2e2019-05-22 10:19:01 +02002464 memcpy(csum, sum->sums + index * sctx->csum_size, sctx->csum_size);
Miao Xief51a4a12013-06-19 10:36:09 +08002465 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002466 list_del(&sum->list);
2467 kfree(sum);
2468 }
Miao Xief51a4a12013-06-19 10:36:09 +08002469 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002470}
2471
2472/* scrub extent tries to collect up to 64 kB for each bio */
Liu Bo6ca17652018-03-07 12:08:09 -07002473static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map,
2474 u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002475 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002476 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002477{
2478 int ret;
2479 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002480 u32 blocksize;
2481
2482 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002483 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2484 blocksize = map->stripe_len;
2485 else
2486 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002487 spin_lock(&sctx->stat_lock);
2488 sctx->stat.data_extents_scrubbed++;
2489 sctx->stat.data_bytes_scrubbed += len;
2490 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002491 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002492 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2493 blocksize = map->stripe_len;
2494 else
2495 blocksize = sctx->fs_info->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002496 spin_lock(&sctx->stat_lock);
2497 sctx->stat.tree_extents_scrubbed++;
2498 sctx->stat.tree_bytes_scrubbed += len;
2499 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002500 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002501 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002502 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002503 }
Arne Jansena2de7332011-03-08 14:14:00 +01002504
2505 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002506 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002507 int have_csum = 0;
2508
2509 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2510 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002511 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002512 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002513 ++sctx->stat.no_csum;
Arne Jansena2de7332011-03-08 14:14:00 +01002514 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002515 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002516 mirror_num, have_csum ? csum : NULL, 0,
2517 physical_for_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002518 if (ret)
2519 return ret;
2520 len -= l;
2521 logical += l;
2522 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002523 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002524 }
2525 return 0;
2526}
2527
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002528static int scrub_pages_for_parity(struct scrub_parity *sparity,
2529 u64 logical, u64 len,
2530 u64 physical, struct btrfs_device *dev,
2531 u64 flags, u64 gen, int mirror_num, u8 *csum)
2532{
2533 struct scrub_ctx *sctx = sparity->sctx;
2534 struct scrub_block *sblock;
2535 int index;
2536
David Sterba58c4e172016-02-11 10:49:42 +01002537 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002538 if (!sblock) {
2539 spin_lock(&sctx->stat_lock);
2540 sctx->stat.malloc_errors++;
2541 spin_unlock(&sctx->stat_lock);
2542 return -ENOMEM;
2543 }
2544
2545 /* one ref inside this function, plus one for each page added to
2546 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002547 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002548 sblock->sctx = sctx;
2549 sblock->no_io_error_seen = 1;
2550 sblock->sparity = sparity;
2551 scrub_parity_get(sparity);
2552
2553 for (index = 0; len > 0; index++) {
2554 struct scrub_page *spage;
2555 u64 l = min_t(u64, len, PAGE_SIZE);
2556
David Sterba58c4e172016-02-11 10:49:42 +01002557 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002558 if (!spage) {
2559leave_nomem:
2560 spin_lock(&sctx->stat_lock);
2561 sctx->stat.malloc_errors++;
2562 spin_unlock(&sctx->stat_lock);
2563 scrub_block_put(sblock);
2564 return -ENOMEM;
2565 }
2566 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2567 /* For scrub block */
2568 scrub_page_get(spage);
2569 sblock->pagev[index] = spage;
2570 /* For scrub parity */
2571 scrub_page_get(spage);
2572 list_add_tail(&spage->list, &sparity->spages);
2573 spage->sblock = sblock;
2574 spage->dev = dev;
2575 spage->flags = flags;
2576 spage->generation = gen;
2577 spage->logical = logical;
2578 spage->physical = physical;
2579 spage->mirror_num = mirror_num;
2580 if (csum) {
2581 spage->have_csum = 1;
2582 memcpy(spage->csum, csum, sctx->csum_size);
2583 } else {
2584 spage->have_csum = 0;
2585 }
2586 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002587 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002588 if (!spage->page)
2589 goto leave_nomem;
2590 len -= l;
2591 logical += l;
2592 physical += l;
2593 }
2594
2595 WARN_ON(sblock->page_count == 0);
2596 for (index = 0; index < sblock->page_count; index++) {
2597 struct scrub_page *spage = sblock->pagev[index];
2598 int ret;
2599
2600 ret = scrub_add_page_to_rd_bio(sctx, spage);
2601 if (ret) {
2602 scrub_block_put(sblock);
2603 return ret;
2604 }
2605 }
2606
2607 /* last one frees, either here or in bio completion for last page */
2608 scrub_block_put(sblock);
2609 return 0;
2610}
2611
2612static int scrub_extent_for_parity(struct scrub_parity *sparity,
2613 u64 logical, u64 len,
2614 u64 physical, struct btrfs_device *dev,
2615 u64 flags, u64 gen, int mirror_num)
2616{
2617 struct scrub_ctx *sctx = sparity->sctx;
2618 int ret;
2619 u8 csum[BTRFS_CSUM_SIZE];
2620 u32 blocksize;
2621
Anand Jaine6e674b2017-12-04 12:54:54 +08002622 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval4a770892015-06-19 11:52:52 -07002623 scrub_parity_mark_sectors_error(sparity, logical, len);
2624 return 0;
2625 }
2626
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002627 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002628 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002629 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002630 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002631 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002632 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002633 WARN_ON(1);
2634 }
2635
2636 while (len) {
2637 u64 l = min_t(u64, len, blocksize);
2638 int have_csum = 0;
2639
2640 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2641 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002642 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002643 if (have_csum == 0)
2644 goto skip;
2645 }
2646 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2647 flags, gen, mirror_num,
2648 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002649 if (ret)
2650 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002651skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002652 len -= l;
2653 logical += l;
2654 physical += l;
2655 }
2656 return 0;
2657}
2658
Wang Shilong3b080b22014-04-01 18:01:43 +08002659/*
2660 * Given a physical address, this will calculate it's
2661 * logical offset. if this is a parity stripe, it will return
2662 * the most left data stripe's logical offset.
2663 *
2664 * return 0 if it is a data stripe, 1 means parity stripe.
2665 */
2666static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002667 struct map_lookup *map, u64 *offset,
2668 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002669{
2670 int i;
2671 int j = 0;
2672 u64 stripe_nr;
2673 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002674 u32 stripe_index;
2675 u32 rot;
David Sterbacff82672019-05-17 11:43:45 +02002676 const int data_stripes = nr_data_stripes(map);
Wang Shilong3b080b22014-04-01 18:01:43 +08002677
David Sterbacff82672019-05-17 11:43:45 +02002678 last_offset = (physical - map->stripes[num].physical) * data_stripes;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002679 if (stripe_start)
2680 *stripe_start = last_offset;
2681
Wang Shilong3b080b22014-04-01 18:01:43 +08002682 *offset = last_offset;
David Sterbacff82672019-05-17 11:43:45 +02002683 for (i = 0; i < data_stripes; i++) {
Wang Shilong3b080b22014-04-01 18:01:43 +08002684 *offset = last_offset + i * map->stripe_len;
2685
Liu Bo42c61ab2017-04-03 13:45:24 -07002686 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbacff82672019-05-17 11:43:45 +02002687 stripe_nr = div_u64(stripe_nr, data_stripes);
Wang Shilong3b080b22014-04-01 18:01:43 +08002688
2689 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002690 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002691 /* calculate which stripe this data locates */
2692 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002693 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002694 if (stripe_index == num)
2695 return 0;
2696 if (stripe_index < num)
2697 j++;
2698 }
2699 *offset = last_offset + j * map->stripe_len;
2700 return 1;
2701}
2702
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002703static void scrub_free_parity(struct scrub_parity *sparity)
2704{
2705 struct scrub_ctx *sctx = sparity->sctx;
2706 struct scrub_page *curr, *next;
2707 int nbits;
2708
2709 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2710 if (nbits) {
2711 spin_lock(&sctx->stat_lock);
2712 sctx->stat.read_errors += nbits;
2713 sctx->stat.uncorrectable_errors += nbits;
2714 spin_unlock(&sctx->stat_lock);
2715 }
2716
2717 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2718 list_del_init(&curr->list);
2719 scrub_page_put(curr);
2720 }
2721
2722 kfree(sparity);
2723}
2724
Zhao Lei20b2e302015-06-04 20:09:15 +08002725static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2726{
2727 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2728 work);
2729 struct scrub_ctx *sctx = sparity->sctx;
2730
2731 scrub_free_parity(sparity);
2732 scrub_pending_bio_dec(sctx);
2733}
2734
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002735static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002736{
2737 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002738 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002739
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002740 if (bio->bi_status)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002741 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2742 sparity->nsectors);
2743
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002744 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002745
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002746 btrfs_init_work(&sparity->work, scrub_parity_bio_endio_worker, NULL,
2747 NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002748 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002749}
2750
2751static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2752{
2753 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002754 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002755 struct bio *bio;
2756 struct btrfs_raid_bio *rbio;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002757 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002758 u64 length;
2759 int ret;
2760
2761 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2762 sparity->nsectors))
2763 goto out;
2764
Zhao Leia0dd59d2015-07-21 15:42:26 +08002765 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08002766
2767 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002768 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
David Sterba825ad4c2017-03-28 14:45:22 +02002769 &length, &bbio);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002770 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002771 goto bbio_out;
2772
David Sterbac5e4c3d2017-06-12 17:29:41 +02002773 bio = btrfs_io_bio_alloc(0);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002774 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2775 bio->bi_private = sparity;
2776 bio->bi_end_io = scrub_parity_bio_endio;
2777
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002778 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002779 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002780 sparity->dbitmap,
2781 sparity->nsectors);
2782 if (!rbio)
2783 goto rbio_out;
2784
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002785 scrub_pending_bio_inc(sctx);
2786 raid56_parity_submit_scrub_rbio(rbio);
2787 return;
2788
2789rbio_out:
2790 bio_put(bio);
2791bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002792 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08002793 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002794 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2795 sparity->nsectors);
2796 spin_lock(&sctx->stat_lock);
2797 sctx->stat.malloc_errors++;
2798 spin_unlock(&sctx->stat_lock);
2799out:
2800 scrub_free_parity(sparity);
2801}
2802
2803static inline int scrub_calc_parity_bitmap_len(int nsectors)
2804{
Zhao Leibfca9a62014-12-08 19:55:57 +08002805 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002806}
2807
2808static void scrub_parity_get(struct scrub_parity *sparity)
2809{
Elena Reshetova78a76452017-03-03 10:55:24 +02002810 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002811}
2812
2813static void scrub_parity_put(struct scrub_parity *sparity)
2814{
Elena Reshetova78a76452017-03-03 10:55:24 +02002815 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002816 return;
2817
2818 scrub_parity_check_and_repair(sparity);
2819}
2820
2821static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2822 struct map_lookup *map,
2823 struct btrfs_device *sdev,
2824 struct btrfs_path *path,
2825 u64 logic_start,
2826 u64 logic_end)
2827{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002828 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002829 struct btrfs_root *root = fs_info->extent_root;
2830 struct btrfs_root *csum_root = fs_info->csum_root;
2831 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07002832 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002833 u64 flags;
2834 int ret;
2835 int slot;
2836 struct extent_buffer *l;
2837 struct btrfs_key key;
2838 u64 generation;
2839 u64 extent_logical;
2840 u64 extent_physical;
2841 u64 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07002842 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002843 struct btrfs_device *extent_dev;
2844 struct scrub_parity *sparity;
2845 int nsectors;
2846 int bitmap_len;
2847 int extent_mirror_num;
2848 int stop_loop = 0;
2849
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002850 nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002851 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2852 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2853 GFP_NOFS);
2854 if (!sparity) {
2855 spin_lock(&sctx->stat_lock);
2856 sctx->stat.malloc_errors++;
2857 spin_unlock(&sctx->stat_lock);
2858 return -ENOMEM;
2859 }
2860
2861 sparity->stripe_len = map->stripe_len;
2862 sparity->nsectors = nsectors;
2863 sparity->sctx = sctx;
2864 sparity->scrub_dev = sdev;
2865 sparity->logic_start = logic_start;
2866 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02002867 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002868 INIT_LIST_HEAD(&sparity->spages);
2869 sparity->dbitmap = sparity->bitmap;
2870 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2871
2872 ret = 0;
2873 while (logic_start < logic_end) {
2874 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2875 key.type = BTRFS_METADATA_ITEM_KEY;
2876 else
2877 key.type = BTRFS_EXTENT_ITEM_KEY;
2878 key.objectid = logic_start;
2879 key.offset = (u64)-1;
2880
2881 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2882 if (ret < 0)
2883 goto out;
2884
2885 if (ret > 0) {
2886 ret = btrfs_previous_extent_item(root, path, 0);
2887 if (ret < 0)
2888 goto out;
2889 if (ret > 0) {
2890 btrfs_release_path(path);
2891 ret = btrfs_search_slot(NULL, root, &key,
2892 path, 0, 0);
2893 if (ret < 0)
2894 goto out;
2895 }
2896 }
2897
2898 stop_loop = 0;
2899 while (1) {
2900 u64 bytes;
2901
2902 l = path->nodes[0];
2903 slot = path->slots[0];
2904 if (slot >= btrfs_header_nritems(l)) {
2905 ret = btrfs_next_leaf(root, path);
2906 if (ret == 0)
2907 continue;
2908 if (ret < 0)
2909 goto out;
2910
2911 stop_loop = 1;
2912 break;
2913 }
2914 btrfs_item_key_to_cpu(l, &key, slot);
2915
Zhao Leid7cad232015-07-22 13:14:48 +08002916 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2917 key.type != BTRFS_METADATA_ITEM_KEY)
2918 goto next;
2919
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002920 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002921 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002922 else
2923 bytes = key.offset;
2924
2925 if (key.objectid + bytes <= logic_start)
2926 goto next;
2927
Zhao Leia0dd59d2015-07-21 15:42:26 +08002928 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002929 stop_loop = 1;
2930 break;
2931 }
2932
2933 while (key.objectid >= logic_start + map->stripe_len)
2934 logic_start += map->stripe_len;
2935
2936 extent = btrfs_item_ptr(l, slot,
2937 struct btrfs_extent_item);
2938 flags = btrfs_extent_flags(l, extent);
2939 generation = btrfs_extent_generation(l, extent);
2940
Zhao Leia323e812015-07-23 12:29:49 +08002941 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
2942 (key.objectid < logic_start ||
2943 key.objectid + bytes >
2944 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002945 btrfs_err(fs_info,
2946 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08002947 key.objectid, logic_start);
Zhao Lei9799d2c32015-08-25 21:31:40 +08002948 spin_lock(&sctx->stat_lock);
2949 sctx->stat.uncorrectable_errors++;
2950 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002951 goto next;
2952 }
2953again:
2954 extent_logical = key.objectid;
2955 extent_len = bytes;
2956
2957 if (extent_logical < logic_start) {
2958 extent_len -= logic_start - extent_logical;
2959 extent_logical = logic_start;
2960 }
2961
2962 if (extent_logical + extent_len >
2963 logic_start + map->stripe_len)
2964 extent_len = logic_start + map->stripe_len -
2965 extent_logical;
2966
2967 scrub_parity_mark_sectors_data(sparity, extent_logical,
2968 extent_len);
2969
Omar Sandoval4a770892015-06-19 11:52:52 -07002970 mapped_length = extent_len;
Zhao Leif1fee652016-05-17 17:37:38 +08002971 bbio = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002972 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
2973 extent_logical, &mapped_length, &bbio,
2974 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07002975 if (!ret) {
2976 if (!bbio || mapped_length < extent_len)
2977 ret = -EIO;
2978 }
2979 if (ret) {
2980 btrfs_put_bbio(bbio);
2981 goto out;
2982 }
2983 extent_physical = bbio->stripes[0].physical;
2984 extent_mirror_num = bbio->mirror_num;
2985 extent_dev = bbio->stripes[0].dev;
2986 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002987
2988 ret = btrfs_lookup_csums_range(csum_root,
2989 extent_logical,
2990 extent_logical + extent_len - 1,
2991 &sctx->csum_list, 1);
2992 if (ret)
2993 goto out;
2994
2995 ret = scrub_extent_for_parity(sparity, extent_logical,
2996 extent_len,
2997 extent_physical,
2998 extent_dev, flags,
2999 generation,
3000 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003001
3002 scrub_free_csums(sctx);
3003
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003004 if (ret)
3005 goto out;
3006
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003007 if (extent_logical + extent_len <
3008 key.objectid + bytes) {
3009 logic_start += map->stripe_len;
3010
3011 if (logic_start >= logic_end) {
3012 stop_loop = 1;
3013 break;
3014 }
3015
3016 if (logic_start < key.objectid + bytes) {
3017 cond_resched();
3018 goto again;
3019 }
3020 }
3021next:
3022 path->slots[0]++;
3023 }
3024
3025 btrfs_release_path(path);
3026
3027 if (stop_loop)
3028 break;
3029
3030 logic_start += map->stripe_len;
3031 }
3032out:
3033 if (ret < 0)
3034 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003035 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003036 scrub_parity_put(sparity);
3037 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003038 mutex_lock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003039 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003040 mutex_unlock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003041
3042 btrfs_release_path(path);
3043 return ret < 0 ? ret : 0;
3044}
3045
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003046static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003047 struct map_lookup *map,
3048 struct btrfs_device *scrub_dev,
Omar Sandoval32934282018-08-14 11:09:52 -07003049 int num, u64 base, u64 length)
Arne Jansena2de7332011-03-08 14:14:00 +01003050{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003051 struct btrfs_path *path, *ppath;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003052 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003053 struct btrfs_root *root = fs_info->extent_root;
3054 struct btrfs_root *csum_root = fs_info->csum_root;
3055 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003056 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003057 u64 flags;
3058 int ret;
3059 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003060 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003061 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003062 u64 physical;
3063 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003064 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003065 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003066 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003067 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003068 struct reada_control *reada1;
3069 struct reada_control *reada2;
David Sterbae6c11f92016-03-24 18:00:53 +01003070 struct btrfs_key key;
Arne Jansen7a262852011-06-10 12:39:23 +02003071 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003072 u64 increment = map->stripe_len;
3073 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003074 u64 extent_logical;
3075 u64 extent_physical;
3076 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003077 u64 stripe_logical;
3078 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003079 struct btrfs_device *extent_dev;
3080 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003081 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003082
Wang Shilong3b080b22014-04-01 18:01:43 +08003083 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003084 offset = 0;
Liu Bo42c61ab2017-04-03 13:45:24 -07003085 nstripes = div64_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003086 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3087 offset = map->stripe_len * num;
3088 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003089 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003090 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3091 int factor = map->num_stripes / map->sub_stripes;
3092 offset = map->stripe_len * (num / map->sub_stripes);
3093 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003094 mirror_num = num % map->sub_stripes + 1;
David Sterbac7369b32019-05-31 15:39:31 +02003095 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
Arne Jansena2de7332011-03-08 14:14:00 +01003096 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003097 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003098 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3099 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003100 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003101 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003102 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003103 increment = map->stripe_len * nr_data_stripes(map);
3104 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003105 } else {
3106 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003107 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003108 }
3109
3110 path = btrfs_alloc_path();
3111 if (!path)
3112 return -ENOMEM;
3113
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003114 ppath = btrfs_alloc_path();
3115 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003116 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003117 return -ENOMEM;
3118 }
3119
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003120 /*
3121 * work on commit root. The related disk blocks are static as
3122 * long as COW is applied. This means, it is save to rewrite
3123 * them to repair disk errors without any race conditions
3124 */
Arne Jansena2de7332011-03-08 14:14:00 +01003125 path->search_commit_root = 1;
3126 path->skip_locking = 1;
3127
Gui Hecheng063c54d2015-01-09 09:39:40 +08003128 ppath->search_commit_root = 1;
3129 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003130 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003131 * trigger the readahead for extent tree csum tree and wait for
3132 * completion. During readahead, the scrub is officially paused
3133 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003134 */
3135 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003136 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003137 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003138 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003139 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003140 logic_end += base;
3141 } else {
3142 logic_end = logical + increment * nstripes;
3143 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003144 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003145 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003146 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003147
Arne Jansen7a262852011-06-10 12:39:23 +02003148 /* FIXME it might be better to start readahead at commit root */
David Sterbae6c11f92016-03-24 18:00:53 +01003149 key.objectid = logical;
3150 key.type = BTRFS_EXTENT_ITEM_KEY;
3151 key.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003152 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003153 key_end.type = BTRFS_METADATA_ITEM_KEY;
3154 key_end.offset = (u64)-1;
David Sterbae6c11f92016-03-24 18:00:53 +01003155 reada1 = btrfs_reada_add(root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003156
David Sterbae6c11f92016-03-24 18:00:53 +01003157 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3158 key.type = BTRFS_EXTENT_CSUM_KEY;
3159 key.offset = logical;
Arne Jansen7a262852011-06-10 12:39:23 +02003160 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3161 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003162 key_end.offset = logic_end;
David Sterbae6c11f92016-03-24 18:00:53 +01003163 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003164
Arne Jansen7a262852011-06-10 12:39:23 +02003165 if (!IS_ERR(reada1))
3166 btrfs_reada_wait(reada1);
3167 if (!IS_ERR(reada2))
3168 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003169
Arne Jansena2de7332011-03-08 14:14:00 +01003170
3171 /*
3172 * collect all data csums for the stripe to avoid seeking during
3173 * the scrub. This might currently (crc32) end up to be about 1MB
3174 */
Arne Jansene7786c32011-05-28 20:58:38 +00003175 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003176
Arne Jansena2de7332011-03-08 14:14:00 +01003177 /*
3178 * now find all extents for each stripe and scrub them
3179 */
Arne Jansena2de7332011-03-08 14:14:00 +01003180 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003181 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003182 /*
3183 * canceled?
3184 */
3185 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003186 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003187 ret = -ECANCELED;
3188 goto out;
3189 }
3190 /*
3191 * check to see if we have to pause
3192 */
3193 if (atomic_read(&fs_info->scrub_pause_req)) {
3194 /* push queued extents */
David Sterba2073c4c2017-03-31 17:12:51 +02003195 sctx->flush_all_writes = true;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003196 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003197 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003198 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003199 mutex_unlock(&sctx->wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003200 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003201 atomic_read(&sctx->bios_in_flight) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003202 sctx->flush_all_writes = false;
Wang Shilong3cb09292013-12-04 21:15:19 +08003203 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003204 }
3205
Zhao Leif2f66a22015-07-21 12:22:29 +08003206 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3207 ret = get_raid56_logic_offset(physical, num, map,
3208 &logical,
3209 &stripe_logical);
3210 logical += base;
3211 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003212 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003213 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003214 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003215 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3216 ppath, stripe_logical,
3217 stripe_end);
3218 if (ret)
3219 goto out;
3220 goto skip;
3221 }
3222 }
3223
Wang Shilong7c76edb2014-01-12 21:38:32 +08003224 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3225 key.type = BTRFS_METADATA_ITEM_KEY;
3226 else
3227 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003228 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003229 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003230
3231 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3232 if (ret < 0)
3233 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003234
Arne Jansen8c510322011-06-03 10:09:26 +02003235 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003236 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003237 if (ret < 0)
3238 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003239 if (ret > 0) {
3240 /* there's no smaller item, so stick with the
3241 * larger one */
3242 btrfs_release_path(path);
3243 ret = btrfs_search_slot(NULL, root, &key,
3244 path, 0, 0);
3245 if (ret < 0)
3246 goto out;
3247 }
Arne Jansena2de7332011-03-08 14:14:00 +01003248 }
3249
Liu Bo625f1c8d2013-04-27 02:56:57 +00003250 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003251 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003252 u64 bytes;
3253
Arne Jansena2de7332011-03-08 14:14:00 +01003254 l = path->nodes[0];
3255 slot = path->slots[0];
3256 if (slot >= btrfs_header_nritems(l)) {
3257 ret = btrfs_next_leaf(root, path);
3258 if (ret == 0)
3259 continue;
3260 if (ret < 0)
3261 goto out;
3262
Liu Bo625f1c8d2013-04-27 02:56:57 +00003263 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003264 break;
3265 }
3266 btrfs_item_key_to_cpu(l, &key, slot);
3267
Zhao Leid7cad232015-07-22 13:14:48 +08003268 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3269 key.type != BTRFS_METADATA_ITEM_KEY)
3270 goto next;
3271
Josef Bacik3173a182013-03-07 14:22:04 -05003272 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003273 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003274 else
3275 bytes = key.offset;
3276
3277 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003278 goto next;
3279
Liu Bo625f1c8d2013-04-27 02:56:57 +00003280 if (key.objectid >= logical + map->stripe_len) {
3281 /* out of this device extent */
3282 if (key.objectid >= logic_end)
3283 stop_loop = 1;
3284 break;
3285 }
Arne Jansena2de7332011-03-08 14:14:00 +01003286
3287 extent = btrfs_item_ptr(l, slot,
3288 struct btrfs_extent_item);
3289 flags = btrfs_extent_flags(l, extent);
3290 generation = btrfs_extent_generation(l, extent);
3291
Zhao Leia323e812015-07-23 12:29:49 +08003292 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3293 (key.objectid < logical ||
3294 key.objectid + bytes >
3295 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003296 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003297 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003298 key.objectid, logical);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003299 spin_lock(&sctx->stat_lock);
3300 sctx->stat.uncorrectable_errors++;
3301 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003302 goto next;
3303 }
3304
Liu Bo625f1c8d2013-04-27 02:56:57 +00003305again:
3306 extent_logical = key.objectid;
3307 extent_len = bytes;
3308
Arne Jansena2de7332011-03-08 14:14:00 +01003309 /*
3310 * trim extent to this stripe
3311 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003312 if (extent_logical < logical) {
3313 extent_len -= logical - extent_logical;
3314 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003315 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003316 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003317 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003318 extent_len = logical + map->stripe_len -
3319 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003320 }
3321
Liu Bo625f1c8d2013-04-27 02:56:57 +00003322 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003323 extent_dev = scrub_dev;
3324 extent_mirror_num = mirror_num;
Omar Sandoval32934282018-08-14 11:09:52 -07003325 if (sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003326 scrub_remap_extent(fs_info, extent_logical,
3327 extent_len, &extent_physical,
3328 &extent_dev,
3329 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003330
Zhao Leife8cf652015-07-22 13:14:47 +08003331 ret = btrfs_lookup_csums_range(csum_root,
3332 extent_logical,
3333 extent_logical +
3334 extent_len - 1,
3335 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003336 if (ret)
3337 goto out;
3338
Liu Bo6ca17652018-03-07 12:08:09 -07003339 ret = scrub_extent(sctx, map, extent_logical, extent_len,
Liu Bo625f1c8d2013-04-27 02:56:57 +00003340 extent_physical, extent_dev, flags,
3341 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003342 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003343
3344 scrub_free_csums(sctx);
3345
Liu Bo625f1c8d2013-04-27 02:56:57 +00003346 if (ret)
3347 goto out;
3348
3349 if (extent_logical + extent_len <
3350 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003351 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003352 /*
3353 * loop until we find next data stripe
3354 * or we have finished all stripes.
3355 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003356loop:
3357 physical += map->stripe_len;
3358 ret = get_raid56_logic_offset(physical,
3359 num, map, &logical,
3360 &stripe_logical);
3361 logical += base;
3362
3363 if (ret && physical < physical_end) {
3364 stripe_logical += base;
3365 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003366 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003367 ret = scrub_raid56_parity(sctx,
3368 map, scrub_dev, ppath,
3369 stripe_logical,
3370 stripe_end);
3371 if (ret)
3372 goto out;
3373 goto loop;
3374 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003375 } else {
3376 physical += map->stripe_len;
3377 logical += increment;
3378 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003379 if (logical < key.objectid + bytes) {
3380 cond_resched();
3381 goto again;
3382 }
3383
Wang Shilong3b080b22014-04-01 18:01:43 +08003384 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003385 stop_loop = 1;
3386 break;
3387 }
3388 }
Arne Jansena2de7332011-03-08 14:14:00 +01003389next:
3390 path->slots[0]++;
3391 }
Chris Mason71267332011-05-23 06:30:52 -04003392 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003393skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003394 logical += increment;
3395 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003396 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003397 if (stop_loop)
3398 sctx->stat.last_physical = map->stripes[num].physical +
3399 length;
3400 else
3401 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003402 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003403 if (stop_loop)
3404 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003405 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003406out:
Arne Jansena2de7332011-03-08 14:14:00 +01003407 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003408 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003409 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003410 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003411 mutex_unlock(&sctx->wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003412
Arne Jansene7786c32011-05-28 20:58:38 +00003413 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003414 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003415 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003416 return ret < 0 ? ret : 0;
3417}
3418
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003419static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003420 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003421 u64 chunk_offset, u64 length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003422 u64 dev_offset,
David Sterba32da53862019-10-29 19:20:18 +01003423 struct btrfs_block_group *cache)
Arne Jansena2de7332011-03-08 14:14:00 +01003424{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003425 struct btrfs_fs_info *fs_info = sctx->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02003426 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003427 struct map_lookup *map;
3428 struct extent_map *em;
3429 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003430 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003431
David Sterbac8bf1b62019-05-17 11:43:17 +02003432 read_lock(&map_tree->lock);
3433 em = lookup_extent_mapping(map_tree, chunk_offset, 1);
3434 read_unlock(&map_tree->lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003435
Filipe Manana020d5b72015-11-19 10:57:20 +00003436 if (!em) {
3437 /*
3438 * Might have been an unused block group deleted by the cleaner
3439 * kthread or relocation.
3440 */
3441 spin_lock(&cache->lock);
3442 if (!cache->removed)
3443 ret = -EINVAL;
3444 spin_unlock(&cache->lock);
3445
3446 return ret;
3447 }
Arne Jansena2de7332011-03-08 14:14:00 +01003448
Jeff Mahoney95617d62015-06-03 10:55:48 -04003449 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003450 if (em->start != chunk_offset)
3451 goto out;
3452
3453 if (em->len < length)
3454 goto out;
3455
3456 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003457 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003458 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003459 ret = scrub_stripe(sctx, map, scrub_dev, i,
Omar Sandoval32934282018-08-14 11:09:52 -07003460 chunk_offset, length);
Arne Jansena2de7332011-03-08 14:14:00 +01003461 if (ret)
3462 goto out;
3463 }
3464 }
3465out:
3466 free_extent_map(em);
3467
3468 return ret;
3469}
3470
3471static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003472int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Omar Sandoval32934282018-08-14 11:09:52 -07003473 struct btrfs_device *scrub_dev, u64 start, u64 end)
Arne Jansena2de7332011-03-08 14:14:00 +01003474{
3475 struct btrfs_dev_extent *dev_extent = NULL;
3476 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003477 struct btrfs_fs_info *fs_info = sctx->fs_info;
3478 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003479 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003480 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003481 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003482 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003483 int slot;
3484 struct extent_buffer *l;
3485 struct btrfs_key key;
3486 struct btrfs_key found_key;
David Sterba32da53862019-10-29 19:20:18 +01003487 struct btrfs_block_group *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003488 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003489
3490 path = btrfs_alloc_path();
3491 if (!path)
3492 return -ENOMEM;
3493
David Sterbae4058b52015-11-27 16:31:35 +01003494 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003495 path->search_commit_root = 1;
3496 path->skip_locking = 1;
3497
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003498 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003499 key.offset = 0ull;
3500 key.type = BTRFS_DEV_EXTENT_KEY;
3501
Arne Jansena2de7332011-03-08 14:14:00 +01003502 while (1) {
3503 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3504 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003505 break;
3506 if (ret > 0) {
3507 if (path->slots[0] >=
3508 btrfs_header_nritems(path->nodes[0])) {
3509 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003510 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003511 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003512 if (ret > 0) {
3513 ret = 0;
3514 break;
3515 }
3516 } else {
3517 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003518 }
3519 }
Arne Jansena2de7332011-03-08 14:14:00 +01003520
3521 l = path->nodes[0];
3522 slot = path->slots[0];
3523
3524 btrfs_item_key_to_cpu(l, &found_key, slot);
3525
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003526 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003527 break;
3528
David Sterba962a2982014-06-04 18:41:45 +02003529 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003530 break;
3531
3532 if (found_key.offset >= end)
3533 break;
3534
3535 if (found_key.offset < key.offset)
3536 break;
3537
3538 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3539 length = btrfs_dev_extent_length(l, dev_extent);
3540
Qu Wenruoced96ed2014-06-19 10:42:51 +08003541 if (found_key.offset + length <= start)
3542 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003543
Arne Jansena2de7332011-03-08 14:14:00 +01003544 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3545
3546 /*
3547 * get a reference on the corresponding block group to prevent
3548 * the chunk from going away while we scrub it
3549 */
3550 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003551
3552 /* some chunks are removed but not committed to disk yet,
3553 * continue scrubbing */
3554 if (!cache)
3555 goto skip;
3556
Zhaolei55e3a602015-08-05 16:43:30 +08003557 /*
3558 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3559 * to avoid deadlock caused by:
3560 * btrfs_inc_block_group_ro()
3561 * -> btrfs_wait_for_commit()
3562 * -> btrfs_commit_transaction()
3563 * -> btrfs_scrub_pause()
3564 */
3565 scrub_pause_on(fs_info);
Qu Wenruob12de522019-11-15 10:09:00 +08003566
3567 /*
3568 * Don't do chunk preallocation for scrub.
3569 *
3570 * This is especially important for SYSTEM bgs, or we can hit
3571 * -EFBIG from btrfs_finish_chunk_alloc() like:
3572 * 1. The only SYSTEM bg is marked RO.
3573 * Since SYSTEM bg is small, that's pretty common.
3574 * 2. New SYSTEM bg will be allocated
3575 * Due to regular version will allocate new chunk.
3576 * 3. New SYSTEM bg is empty and will get cleaned up
3577 * Before cleanup really happens, it's marked RO again.
3578 * 4. Empty SYSTEM bg get scrubbed
3579 * We go back to 2.
3580 *
3581 * This can easily boost the amount of SYSTEM chunks if cleaner
3582 * thread can't be triggered fast enough, and use up all space
3583 * of btrfs_super_block::sys_chunk_array
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003584 *
3585 * While for dev replace, we need to try our best to mark block
3586 * group RO, to prevent race between:
3587 * - Write duplication
3588 * Contains latest data
3589 * - Scrub copy
3590 * Contains data from commit tree
3591 *
3592 * If target block group is not marked RO, nocow writes can
3593 * be overwritten by scrub copy, causing data corruption.
3594 * So for dev-replace, it's not allowed to continue if a block
3595 * group is not RO.
Qu Wenruob12de522019-11-15 10:09:00 +08003596 */
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003597 ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace);
Zhaolei76a8efa2015-11-17 18:46:17 +08003598 if (ret == 0) {
3599 ro_set = 1;
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003600 } else if (ret == -ENOSPC && !sctx->is_dev_replace) {
Zhaolei76a8efa2015-11-17 18:46:17 +08003601 /*
3602 * btrfs_inc_block_group_ro return -ENOSPC when it
3603 * failed in creating new chunk for metadata.
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003604 * It is not a problem for scrub, because
Zhaolei76a8efa2015-11-17 18:46:17 +08003605 * metadata are always cowed, and our scrub paused
3606 * commit_transactions.
3607 */
3608 ro_set = 0;
3609 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003610 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02003611 "failed setting block group ro: %d", ret);
Zhaolei55e3a602015-08-05 16:43:30 +08003612 btrfs_put_block_group(cache);
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003613 scrub_pause_off(fs_info);
Zhaolei55e3a602015-08-05 16:43:30 +08003614 break;
3615 }
3616
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003617 /*
3618 * Now the target block is marked RO, wait for nocow writes to
3619 * finish before dev-replace.
3620 * COW is fine, as COW never overwrites extents in commit tree.
3621 */
3622 if (sctx->is_dev_replace) {
3623 btrfs_wait_nocow_writers(cache);
3624 btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start,
3625 cache->length);
3626 }
3627
3628 scrub_pause_off(fs_info);
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003629 down_write(&dev_replace->rwsem);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003630 dev_replace->cursor_right = found_key.offset + length;
3631 dev_replace->cursor_left = found_key.offset;
3632 dev_replace->item_needs_writeback = 1;
David Sterbacb5583d2018-09-07 16:11:23 +02003633 up_write(&dev_replace->rwsem);
3634
Zhao Lei8c204c92015-08-19 15:02:40 +08003635 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
Omar Sandoval32934282018-08-14 11:09:52 -07003636 found_key.offset, cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003637
3638 /*
3639 * flush, submit all pending read and write bios, afterwards
3640 * wait for them.
3641 * Note that in the dev replace case, a read request causes
3642 * write requests that are submitted in the read completion
3643 * worker. Therefore in the current situation, it is required
3644 * that all write requests are flushed, so that all read and
3645 * write requests are really completed when bios_in_flight
3646 * changes to 0.
3647 */
David Sterba2073c4c2017-03-31 17:12:51 +02003648 sctx->flush_all_writes = true;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003649 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003650 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003651 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003652 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003653
3654 wait_event(sctx->list_wait,
3655 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003656
3657 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003658
3659 /*
3660 * must be called before we decrease @scrub_paused.
3661 * make sure we don't block transaction commit while
3662 * we are waiting pending workers finished.
3663 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003664 wait_event(sctx->list_wait,
3665 atomic_read(&sctx->workers_pending) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003666 sctx->flush_all_writes = false;
Wang Shilong12cf9372014-02-19 19:24:17 +08003667
Zhaoleib708ce92015-08-05 16:43:29 +08003668 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003669
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003670 down_write(&dev_replace->rwsem);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003671 dev_replace->cursor_left = dev_replace->cursor_right;
3672 dev_replace->item_needs_writeback = 1;
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003673 up_write(&dev_replace->rwsem);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003674
Zhaolei76a8efa2015-11-17 18:46:17 +08003675 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003676 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003677
Filipe Manana758f2df2015-11-19 11:45:48 +00003678 /*
3679 * We might have prevented the cleaner kthread from deleting
3680 * this block group if it was already unused because we raced
3681 * and set it to RO mode first. So add it back to the unused
3682 * list, otherwise it might not ever be deleted unless a manual
3683 * balance is triggered or it becomes used and unused again.
3684 */
3685 spin_lock(&cache->lock);
3686 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
David Sterbabf38be62019-10-23 18:48:11 +02003687 cache->used == 0) {
Filipe Manana758f2df2015-11-19 11:45:48 +00003688 spin_unlock(&cache->lock);
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08003689 if (btrfs_test_opt(fs_info, DISCARD_ASYNC))
3690 btrfs_discard_queue_work(&fs_info->discard_ctl,
3691 cache);
3692 else
3693 btrfs_mark_bg_unused(cache);
Filipe Manana758f2df2015-11-19 11:45:48 +00003694 } else {
3695 spin_unlock(&cache->lock);
3696 }
3697
Arne Jansena2de7332011-03-08 14:14:00 +01003698 btrfs_put_block_group(cache);
3699 if (ret)
3700 break;
Omar Sandoval32934282018-08-14 11:09:52 -07003701 if (sctx->is_dev_replace &&
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003702 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003703 ret = -EIO;
3704 break;
3705 }
3706 if (sctx->stat.malloc_errors > 0) {
3707 ret = -ENOMEM;
3708 break;
3709 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003710skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003711 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003712 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003713 }
3714
Arne Jansena2de7332011-03-08 14:14:00 +01003715 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003716
Zhaolei55e3a602015-08-05 16:43:30 +08003717 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003718}
3719
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003720static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3721 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003722{
3723 int i;
3724 u64 bytenr;
3725 u64 gen;
3726 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003727 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003728
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003729 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003730 return -EIO;
3731
Miao Xie5f546062014-07-24 11:37:09 +08003732 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003733 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08003734 gen = scrub_dev->generation;
3735 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003736 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003737
3738 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3739 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003740 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3741 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003742 break;
3743
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003744 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003745 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003746 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003747 if (ret)
3748 return ret;
3749 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003750 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003751
3752 return 0;
3753}
3754
3755/*
3756 * get a reference count on fs_info->scrub_workers. start worker if necessary
3757 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003758static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3759 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003760{
David Sterba6f011052015-02-16 18:34:01 +01003761 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003762 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003763
Anand Jaineb4318e2019-01-30 14:45:01 +08003764 lockdep_assert_held(&fs_info->scrub_lock);
3765
Anand Jainff09c4c2019-01-30 14:45:02 +08003766 if (refcount_read(&fs_info->scrub_workers_refcnt) == 0) {
David Sterbac8352942019-02-12 16:51:18 +01003767 ASSERT(fs_info->scrub_workers == NULL);
David Sterbaaf1cbe02017-03-31 18:42:57 +02003768 fs_info->scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub",
3769 flags, is_dev_replace ? 1 : max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08003770 if (!fs_info->scrub_workers)
3771 goto fail_scrub_workers;
3772
David Sterbac8352942019-02-12 16:51:18 +01003773 ASSERT(fs_info->scrub_wr_completion_workers == NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003774 fs_info->scrub_wr_completion_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04003775 btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08003776 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003777 if (!fs_info->scrub_wr_completion_workers)
3778 goto fail_scrub_wr_completion_workers;
3779
David Sterbac8352942019-02-12 16:51:18 +01003780 ASSERT(fs_info->scrub_parity_workers == NULL);
Zhao Lei20b2e302015-06-04 20:09:15 +08003781 fs_info->scrub_parity_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04003782 btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
Zhao Lei20b2e302015-06-04 20:09:15 +08003783 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003784 if (!fs_info->scrub_parity_workers)
3785 goto fail_scrub_parity_workers;
Anand Jainff09c4c2019-01-30 14:45:02 +08003786
3787 refcount_set(&fs_info->scrub_workers_refcnt, 1);
3788 } else {
3789 refcount_inc(&fs_info->scrub_workers_refcnt);
Arne Jansen632dd772011-06-10 12:07:07 +02003790 }
Zhao Leie82afc52015-06-12 20:36:58 +08003791 return 0;
3792
3793fail_scrub_parity_workers:
Zhao Leie82afc52015-06-12 20:36:58 +08003794 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3795fail_scrub_wr_completion_workers:
3796 btrfs_destroy_workqueue(fs_info->scrub_workers);
3797fail_scrub_workers:
3798 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01003799}
3800
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003801int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3802 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003803 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003804{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003805 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003806 int ret;
3807 struct btrfs_device *dev;
Filipe Mananaa5fb1142018-11-26 20:07:17 +00003808 unsigned int nofs_flag;
Anand Jain1cec3f272019-01-30 14:45:00 +08003809 struct btrfs_workqueue *scrub_workers = NULL;
3810 struct btrfs_workqueue *scrub_wr_comp = NULL;
3811 struct btrfs_workqueue *scrub_parity = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003812
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003813 if (btrfs_fs_closing(fs_info))
David Sterba6c3abed2019-02-25 19:57:41 +01003814 return -EAGAIN;
Arne Jansena2de7332011-03-08 14:14:00 +01003815
Jeff Mahoneyda170662016-06-15 09:22:56 -04003816 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003817 /*
3818 * in this case scrub is unable to calculate the checksum
3819 * the way scrub is implemented. Do not handle this
3820 * situation at all because it won't ever happen.
3821 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003822 btrfs_err(fs_info,
3823 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04003824 fs_info->nodesize,
3825 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003826 return -EINVAL;
3827 }
3828
Jeff Mahoneyda170662016-06-15 09:22:56 -04003829 if (fs_info->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003830 /* not supported for data w/o checksums */
Chandan Rajendra751bebb2016-07-04 10:04:39 +05303831 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003832 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04003833 fs_info->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003834 return -EINVAL;
3835 }
3836
Jeff Mahoneyda170662016-06-15 09:22:56 -04003837 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003838 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04003839 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003840 /*
3841 * would exhaust the array bounds of pagev member in
3842 * struct scrub_block
3843 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003844 btrfs_err(fs_info,
3845 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04003846 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003847 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04003848 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003849 SCRUB_MAX_PAGES_PER_BLOCK);
3850 return -EINVAL;
3851 }
3852
David Sterba0e94c4f42018-12-04 16:11:56 +01003853 /* Allocate outside of device_list_mutex */
3854 sctx = scrub_setup_ctx(fs_info, is_dev_replace);
3855 if (IS_ERR(sctx))
3856 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003857
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003858 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +08003859 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Anand Jaine6e674b2017-12-04 12:54:54 +08003860 if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
3861 !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003862 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01003863 ret = -ENODEV;
3864 goto out_free_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003865 }
Arne Jansena2de7332011-03-08 14:14:00 +01003866
Anand Jainebbede42017-12-04 12:54:52 +08003867 if (!is_dev_replace && !readonly &&
3868 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Miao Xie5d68da32014-07-24 11:37:07 +08003869 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Misono Tomohiro672d5992018-08-02 16:19:07 +09003870 btrfs_err_in_rcu(fs_info, "scrub: device %s is not writable",
3871 rcu_str_deref(dev->name));
David Sterba0e94c4f42018-12-04 16:11:56 +01003872 ret = -EROFS;
3873 goto out_free_ctx;
Miao Xie5d68da32014-07-24 11:37:07 +08003874 }
3875
Wang Shilong3b7a0162013-10-12 02:11:12 +08003876 mutex_lock(&fs_info->scrub_lock);
Anand Jaine12c9622017-12-04 12:54:53 +08003877 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08003878 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003879 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003880 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01003881 ret = -EIO;
3882 goto out_free_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003883 }
3884
David Sterbacb5583d2018-09-07 16:11:23 +02003885 down_read(&fs_info->dev_replace.rwsem);
Anand Jaincadbc0a2018-01-03 16:08:30 +08003886 if (dev->scrub_ctx ||
Stefan Behrens8dabb742012-11-06 13:15:27 +01003887 (!is_dev_replace &&
3888 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
David Sterbacb5583d2018-09-07 16:11:23 +02003889 up_read(&fs_info->dev_replace.rwsem);
Arne Jansena2de7332011-03-08 14:14:00 +01003890 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003891 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01003892 ret = -EINPROGRESS;
3893 goto out_free_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003894 }
David Sterbacb5583d2018-09-07 16:11:23 +02003895 up_read(&fs_info->dev_replace.rwsem);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003896
3897 ret = scrub_workers_get(fs_info, is_dev_replace);
3898 if (ret) {
3899 mutex_unlock(&fs_info->scrub_lock);
3900 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01003901 goto out_free_ctx;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003902 }
3903
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003904 sctx->readonly = readonly;
Anand Jaincadbc0a2018-01-03 16:08:30 +08003905 dev->scrub_ctx = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003906 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003907
Wang Shilong3cb09292013-12-04 21:15:19 +08003908 /*
3909 * checking @scrub_pause_req here, we can avoid
3910 * race between committing transaction and scrubbing.
3911 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003912 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003913 atomic_inc(&fs_info->scrubs_running);
3914 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003915
Filipe Mananaa5fb1142018-11-26 20:07:17 +00003916 /*
3917 * In order to avoid deadlock with reclaim when there is a transaction
3918 * trying to pause scrub, make sure we use GFP_NOFS for all the
3919 * allocations done at btrfs_scrub_pages() and scrub_pages_for_parity()
3920 * invoked by our callees. The pausing request is done when the
3921 * transaction commit starts, and it blocks the transaction until scrub
3922 * is paused (done at specific points at scrub_stripe() or right above
3923 * before incrementing fs_info->scrubs_running).
3924 */
3925 nofs_flag = memalloc_nofs_save();
Stefan Behrensff023aa2012-11-06 11:43:11 +01003926 if (!is_dev_replace) {
Anand Jaind1e14422019-01-03 16:17:40 +08003927 btrfs_info(fs_info, "scrub: started on devid %llu", devid);
Wang Shilong9b011ad2013-10-25 19:12:02 +08003928 /*
3929 * by holding device list mutex, we can
3930 * kick off writing super in log tree sync.
3931 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003932 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003933 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003934 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003935 }
Arne Jansena2de7332011-03-08 14:14:00 +01003936
3937 if (!ret)
Omar Sandoval32934282018-08-14 11:09:52 -07003938 ret = scrub_enumerate_chunks(sctx, dev, start, end);
Filipe Mananaa5fb1142018-11-26 20:07:17 +00003939 memalloc_nofs_restore(nofs_flag);
Arne Jansena2de7332011-03-08 14:14:00 +01003940
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003941 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003942 atomic_dec(&fs_info->scrubs_running);
3943 wake_up(&fs_info->scrub_pause_wait);
3944
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003945 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003946
Arne Jansena2de7332011-03-08 14:14:00 +01003947 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003948 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003949
Anand Jaind1e14422019-01-03 16:17:40 +08003950 if (!is_dev_replace)
3951 btrfs_info(fs_info, "scrub: %s on devid %llu with status: %d",
3952 ret ? "not finished" : "finished", devid, ret);
3953
Arne Jansena2de7332011-03-08 14:14:00 +01003954 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08003955 dev->scrub_ctx = NULL;
Anand Jainff09c4c2019-01-30 14:45:02 +08003956 if (refcount_dec_and_test(&fs_info->scrub_workers_refcnt)) {
Anand Jain1cec3f272019-01-30 14:45:00 +08003957 scrub_workers = fs_info->scrub_workers;
3958 scrub_wr_comp = fs_info->scrub_wr_completion_workers;
3959 scrub_parity = fs_info->scrub_parity_workers;
David Sterbac8352942019-02-12 16:51:18 +01003960
3961 fs_info->scrub_workers = NULL;
3962 fs_info->scrub_wr_completion_workers = NULL;
3963 fs_info->scrub_parity_workers = NULL;
Anand Jain1cec3f272019-01-30 14:45:00 +08003964 }
Arne Jansena2de7332011-03-08 14:14:00 +01003965 mutex_unlock(&fs_info->scrub_lock);
3966
Anand Jain1cec3f272019-01-30 14:45:00 +08003967 btrfs_destroy_workqueue(scrub_workers);
3968 btrfs_destroy_workqueue(scrub_wr_comp);
3969 btrfs_destroy_workqueue(scrub_parity);
Filipe Mananaf55985f2015-02-09 21:14:24 +00003970 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003971
3972 return ret;
David Sterba0e94c4f42018-12-04 16:11:56 +01003973
3974out_free_ctx:
3975 scrub_free_ctx(sctx);
3976
3977 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003978}
3979
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003980void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003981{
Arne Jansena2de7332011-03-08 14:14:00 +01003982 mutex_lock(&fs_info->scrub_lock);
3983 atomic_inc(&fs_info->scrub_pause_req);
3984 while (atomic_read(&fs_info->scrubs_paused) !=
3985 atomic_read(&fs_info->scrubs_running)) {
3986 mutex_unlock(&fs_info->scrub_lock);
3987 wait_event(fs_info->scrub_pause_wait,
3988 atomic_read(&fs_info->scrubs_paused) ==
3989 atomic_read(&fs_info->scrubs_running));
3990 mutex_lock(&fs_info->scrub_lock);
3991 }
3992 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003993}
3994
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003995void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003996{
Arne Jansena2de7332011-03-08 14:14:00 +01003997 atomic_dec(&fs_info->scrub_pause_req);
3998 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003999}
4000
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004001int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004002{
Arne Jansena2de7332011-03-08 14:14:00 +01004003 mutex_lock(&fs_info->scrub_lock);
4004 if (!atomic_read(&fs_info->scrubs_running)) {
4005 mutex_unlock(&fs_info->scrub_lock);
4006 return -ENOTCONN;
4007 }
4008
4009 atomic_inc(&fs_info->scrub_cancel_req);
4010 while (atomic_read(&fs_info->scrubs_running)) {
4011 mutex_unlock(&fs_info->scrub_lock);
4012 wait_event(fs_info->scrub_pause_wait,
4013 atomic_read(&fs_info->scrubs_running) == 0);
4014 mutex_lock(&fs_info->scrub_lock);
4015 }
4016 atomic_dec(&fs_info->scrub_cancel_req);
4017 mutex_unlock(&fs_info->scrub_lock);
4018
4019 return 0;
4020}
4021
David Sterba163e97e2019-03-20 16:32:55 +01004022int btrfs_scrub_cancel_dev(struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004023{
David Sterba163e97e2019-03-20 16:32:55 +01004024 struct btrfs_fs_info *fs_info = dev->fs_info;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004025 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004026
4027 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004028 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004029 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004030 mutex_unlock(&fs_info->scrub_lock);
4031 return -ENOTCONN;
4032 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004033 atomic_inc(&sctx->cancel_req);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004034 while (dev->scrub_ctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004035 mutex_unlock(&fs_info->scrub_lock);
4036 wait_event(fs_info->scrub_pause_wait,
Anand Jaincadbc0a2018-01-03 16:08:30 +08004037 dev->scrub_ctx == NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004038 mutex_lock(&fs_info->scrub_lock);
4039 }
4040 mutex_unlock(&fs_info->scrub_lock);
4041
4042 return 0;
4043}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004044
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004045int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01004046 struct btrfs_scrub_progress *progress)
4047{
4048 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004049 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004050
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004051 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +08004052 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Arne Jansena2de7332011-03-08 14:14:00 +01004053 if (dev)
Anand Jaincadbc0a2018-01-03 16:08:30 +08004054 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004055 if (sctx)
4056 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004057 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004058
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004059 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004060}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004061
4062static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4063 u64 extent_logical, u64 extent_len,
4064 u64 *extent_physical,
4065 struct btrfs_device **extent_dev,
4066 int *extent_mirror_num)
4067{
4068 u64 mapped_length;
4069 struct btrfs_bio *bbio = NULL;
4070 int ret;
4071
4072 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004073 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004074 &mapped_length, &bbio, 0);
4075 if (ret || !bbio || mapped_length < extent_len ||
4076 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08004077 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004078 return;
4079 }
4080
4081 *extent_physical = bbio->stripes[0].physical;
4082 *extent_mirror_num = bbio->mirror_num;
4083 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004084 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004085}