blob: 67bb003323e72c496558e516ce640ef67024abf3 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Arne Jansena2de7332011-03-08 14:14:00 +01002/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
Arne Jansena2de7332011-03-08 14:14:00 +01004 */
5
Arne Jansena2de7332011-03-08 14:14:00 +01006#include <linux/blkdev.h>
Jan Schmidt558540c2011-06-13 19:59:12 +02007#include <linux/ratelimit.h>
David Sterbade2491f2017-05-31 19:21:38 +02008#include <linux/sched/mm.h>
Arne Jansena2de7332011-03-08 14:14:00 +01009#include "ctree.h"
10#include "volumes.h"
11#include "disk-io.h"
12#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020013#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020014#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020015#include "extent_io.h"
Stefan Behrensff023aa2012-11-06 11:43:11 +010016#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010017#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040018#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050019#include "raid56.h"
Arne Jansena2de7332011-03-08 14:14:00 +010020
21/*
22 * This is only the first step towards a full-features scrub. It reads all
23 * extent and super block and verifies the checksums. In case a bad checksum
24 * is found or the extent cannot be read, good data will be written back if
25 * any can be found.
26 *
27 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010028 * - In case an unrepairable extent is encountered, track which files are
29 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010030 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010031 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010032 */
33
Stefan Behrensb5d67f62012-03-27 14:21:27 -040034struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010035struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010036
Stefan Behrensff023aa2012-11-06 11:43:11 +010037/*
38 * the following three values only influence the performance.
39 * The last one configures the number of parallel and outstanding I/O
40 * operations. The first two values configure an upper limit for the number
41 * of (dynamically allocated) pages that are added to a bio.
42 */
43#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
44#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
45#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010046
47/*
48 * the following value times PAGE_SIZE needs to be large enough to match the
49 * largest node/leaf/sector size that shall be supported.
50 * Values larger than BTRFS_STRIPE_LEN are not supported.
51 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040052#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010053
Miao Xieaf8e2d12014-10-23 14:42:50 +080054struct scrub_recover {
Elena Reshetova6f615012017-03-03 10:55:21 +020055 refcount_t refs;
Miao Xieaf8e2d12014-10-23 14:42:50 +080056 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +080057 u64 map_length;
58};
59
Arne Jansena2de7332011-03-08 14:14:00 +010060struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040061 struct scrub_block *sblock;
62 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020063 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080064 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010065 u64 flags; /* extent flags */
66 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040067 u64 logical;
68 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010069 u64 physical_for_dev_replace;
Zhao Lei57019342015-01-20 15:11:45 +080070 atomic_t refs;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040071 struct {
72 unsigned int mirror_num:8;
73 unsigned int have_csum:1;
74 unsigned int io_error:1;
75 };
Arne Jansena2de7332011-03-08 14:14:00 +010076 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080077
78 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010079};
80
81struct scrub_bio {
82 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010083 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010084 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010085 struct bio *bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020086 blk_status_t status;
Arne Jansena2de7332011-03-08 14:14:00 +010087 u64 logical;
88 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010089#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
90 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
91#else
92 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
93#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -040094 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +010095 int next_free;
96 struct btrfs_work work;
97};
98
Stefan Behrensb5d67f62012-03-27 14:21:27 -040099struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100100 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400101 int page_count;
102 atomic_t outstanding_pages;
Elena Reshetova186debd2017-03-03 10:55:23 +0200103 refcount_t refs; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100104 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800105 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400106 struct {
107 unsigned int header_error:1;
108 unsigned int checksum_error:1;
109 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200110 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800111
112 /* The following is for the data used to check parity */
113 /* It is for the data with checksum */
114 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400115 };
Omar Sandoval73ff61d2015-06-19 11:52:51 -0700116 struct btrfs_work work;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400117};
118
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800119/* Used for the chunks with parity stripe such RAID5/6 */
120struct scrub_parity {
121 struct scrub_ctx *sctx;
122
123 struct btrfs_device *scrub_dev;
124
125 u64 logic_start;
126
127 u64 logic_end;
128
129 int nsectors;
130
Liu Bo972d7212017-04-03 13:45:33 -0700131 u64 stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800132
Elena Reshetova78a76452017-03-03 10:55:24 +0200133 refcount_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800134
135 struct list_head spages;
136
137 /* Work of parity check and repair */
138 struct btrfs_work work;
139
140 /* Mark the parity blocks which have data */
141 unsigned long *dbitmap;
142
143 /*
144 * Mark the parity blocks which have data, but errors happen when
145 * read data or check data
146 */
147 unsigned long *ebitmap;
148
149 unsigned long bitmap[0];
150};
151
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100152struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100153 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400154 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100155 int first_free;
156 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100157 atomic_t bios_in_flight;
158 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100159 spinlock_t list_lock;
160 wait_queue_head_t list_wait;
161 u16 csum_size;
162 struct list_head csum_list;
163 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100164 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100165 int pages_per_rd_bio;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100166
167 int is_dev_replace;
David Sterba3fb99302017-05-16 19:10:32 +0200168
169 struct scrub_bio *wr_curr_bio;
170 struct mutex wr_lock;
171 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
David Sterba3fb99302017-05-16 19:10:32 +0200172 struct btrfs_device *wr_tgtdev;
David Sterba2073c4c22017-03-31 17:12:51 +0200173 bool flush_all_writes;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100174
Arne Jansena2de7332011-03-08 14:14:00 +0100175 /*
176 * statistics
177 */
178 struct btrfs_scrub_progress stat;
179 spinlock_t stat_lock;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000180
181 /*
182 * Use a ref counter to avoid use-after-free issues. Scrub workers
183 * decrement bios_in_flight and workers_pending and then do a wakeup
184 * on the list_wait wait queue. We must ensure the main scrub task
185 * doesn't free the scrub context before or while the workers are
186 * doing the wakeup() call.
187 */
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200188 refcount_t refs;
Arne Jansena2de7332011-03-08 14:14:00 +0100189};
190
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200191struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100192 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100193 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200194 u64 logical;
195 struct btrfs_root *root;
196 struct btrfs_work work;
197 int mirror_num;
198};
199
Jan Schmidt558540c2011-06-13 19:59:12 +0200200struct scrub_warning {
201 struct btrfs_path *path;
202 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200203 const char *errstr;
David Sterba6aa21262017-10-04 17:07:07 +0200204 u64 physical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200205 u64 logical;
206 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200207};
208
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800209struct full_stripe_lock {
210 struct rb_node node;
211 u64 logical;
212 u64 refs;
213 struct mutex mutex;
214};
215
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100216static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
217static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
218static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
219static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400220static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Zhao Leibe50a8d2015-01-20 15:11:42 +0800221static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100222 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100223static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +0800224 struct scrub_block *sblock,
225 int retry_failed_mirror);
Zhao Leiba7cf982015-08-24 21:18:02 +0800226static void scrub_recheck_block_checksum(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400227static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800228 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400229static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
230 struct scrub_block *sblock_good,
231 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100232static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
233static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
234 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400235static int scrub_checksum_data(struct scrub_block *sblock);
236static int scrub_checksum_tree_block(struct scrub_block *sblock);
237static int scrub_checksum_super(struct scrub_block *sblock);
238static void scrub_block_get(struct scrub_block *sblock);
239static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100240static void scrub_page_get(struct scrub_page *spage);
241static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800242static void scrub_parity_get(struct scrub_parity *sparity);
243static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100244static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
245 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100246static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100247 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100248 u64 gen, int mirror_num, u8 *csum, int force,
249 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200250static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400251static void scrub_bio_end_io_worker(struct btrfs_work *work);
252static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100253static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
254 u64 extent_logical, u64 extent_len,
255 u64 *extent_physical,
256 struct btrfs_device **extent_dev,
257 int *extent_mirror_num);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100258static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
259 struct scrub_page *spage);
260static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200261static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100262static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800263static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800264static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000265static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400266
Liu Bo762221f2018-01-02 13:36:42 -0700267static inline int scrub_is_page_on_raid56(struct scrub_page *page)
268{
269 return page->recover &&
270 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
271}
Stefan Behrens1623ede2012-03-27 14:21:26 -0400272
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100273static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
274{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200275 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100276 atomic_inc(&sctx->bios_in_flight);
277}
278
279static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
280{
281 atomic_dec(&sctx->bios_in_flight);
282 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000283 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100284}
285
Wang Shilongcb7ab022013-12-04 21:16:53 +0800286static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800287{
288 while (atomic_read(&fs_info->scrub_pause_req)) {
289 mutex_unlock(&fs_info->scrub_lock);
290 wait_event(fs_info->scrub_pause_wait,
291 atomic_read(&fs_info->scrub_pause_req) == 0);
292 mutex_lock(&fs_info->scrub_lock);
293 }
294}
295
Zhaolei0e22be82015-08-05 16:43:28 +0800296static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800297{
298 atomic_inc(&fs_info->scrubs_paused);
299 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800300}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800301
Zhaolei0e22be82015-08-05 16:43:28 +0800302static void scrub_pause_off(struct btrfs_fs_info *fs_info)
303{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800304 mutex_lock(&fs_info->scrub_lock);
305 __scrub_blocked_if_needed(fs_info);
306 atomic_dec(&fs_info->scrubs_paused);
307 mutex_unlock(&fs_info->scrub_lock);
308
309 wake_up(&fs_info->scrub_pause_wait);
310}
311
Zhaolei0e22be82015-08-05 16:43:28 +0800312static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
313{
314 scrub_pause_on(fs_info);
315 scrub_pause_off(fs_info);
316}
317
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100318/*
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800319 * Insert new full stripe lock into full stripe locks tree
320 *
321 * Return pointer to existing or newly inserted full_stripe_lock structure if
322 * everything works well.
323 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
324 *
325 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
326 * function
327 */
328static struct full_stripe_lock *insert_full_stripe_lock(
329 struct btrfs_full_stripe_locks_tree *locks_root,
330 u64 fstripe_logical)
331{
332 struct rb_node **p;
333 struct rb_node *parent = NULL;
334 struct full_stripe_lock *entry;
335 struct full_stripe_lock *ret;
336
David Sterbaa32bf9a2018-03-16 02:21:22 +0100337 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800338
339 p = &locks_root->root.rb_node;
340 while (*p) {
341 parent = *p;
342 entry = rb_entry(parent, struct full_stripe_lock, node);
343 if (fstripe_logical < entry->logical) {
344 p = &(*p)->rb_left;
345 } else if (fstripe_logical > entry->logical) {
346 p = &(*p)->rb_right;
347 } else {
348 entry->refs++;
349 return entry;
350 }
351 }
352
353 /* Insert new lock */
354 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
355 if (!ret)
356 return ERR_PTR(-ENOMEM);
357 ret->logical = fstripe_logical;
358 ret->refs = 1;
359 mutex_init(&ret->mutex);
360
361 rb_link_node(&ret->node, parent, p);
362 rb_insert_color(&ret->node, &locks_root->root);
363 return ret;
364}
365
366/*
367 * Search for a full stripe lock of a block group
368 *
369 * Return pointer to existing full stripe lock if found
370 * Return NULL if not found
371 */
372static struct full_stripe_lock *search_full_stripe_lock(
373 struct btrfs_full_stripe_locks_tree *locks_root,
374 u64 fstripe_logical)
375{
376 struct rb_node *node;
377 struct full_stripe_lock *entry;
378
David Sterbaa32bf9a2018-03-16 02:21:22 +0100379 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800380
381 node = locks_root->root.rb_node;
382 while (node) {
383 entry = rb_entry(node, struct full_stripe_lock, node);
384 if (fstripe_logical < entry->logical)
385 node = node->rb_left;
386 else if (fstripe_logical > entry->logical)
387 node = node->rb_right;
388 else
389 return entry;
390 }
391 return NULL;
392}
393
394/*
395 * Helper to get full stripe logical from a normal bytenr.
396 *
397 * Caller must ensure @cache is a RAID56 block group.
398 */
399static u64 get_full_stripe_logical(struct btrfs_block_group_cache *cache,
400 u64 bytenr)
401{
402 u64 ret;
403
404 /*
405 * Due to chunk item size limit, full stripe length should not be
406 * larger than U32_MAX. Just a sanity check here.
407 */
408 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
409
410 /*
411 * round_down() can only handle power of 2, while RAID56 full
412 * stripe length can be 64KiB * n, so we need to manually round down.
413 */
414 ret = div64_u64(bytenr - cache->key.objectid, cache->full_stripe_len) *
415 cache->full_stripe_len + cache->key.objectid;
416 return ret;
417}
418
419/*
420 * Lock a full stripe to avoid concurrency of recovery and read
421 *
422 * It's only used for profiles with parities (RAID5/6), for other profiles it
423 * does nothing.
424 *
425 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
426 * So caller must call unlock_full_stripe() at the same context.
427 *
428 * Return <0 if encounters error.
429 */
430static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
431 bool *locked_ret)
432{
433 struct btrfs_block_group_cache *bg_cache;
434 struct btrfs_full_stripe_locks_tree *locks_root;
435 struct full_stripe_lock *existing;
436 u64 fstripe_start;
437 int ret = 0;
438
439 *locked_ret = false;
440 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
441 if (!bg_cache) {
442 ASSERT(0);
443 return -ENOENT;
444 }
445
446 /* Profiles not based on parity don't need full stripe lock */
447 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
448 goto out;
449 locks_root = &bg_cache->full_stripe_locks_root;
450
451 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
452
453 /* Now insert the full stripe lock */
454 mutex_lock(&locks_root->lock);
455 existing = insert_full_stripe_lock(locks_root, fstripe_start);
456 mutex_unlock(&locks_root->lock);
457 if (IS_ERR(existing)) {
458 ret = PTR_ERR(existing);
459 goto out;
460 }
461 mutex_lock(&existing->mutex);
462 *locked_ret = true;
463out:
464 btrfs_put_block_group(bg_cache);
465 return ret;
466}
467
468/*
469 * Unlock a full stripe.
470 *
471 * NOTE: Caller must ensure it's the same context calling corresponding
472 * lock_full_stripe().
473 *
474 * Return 0 if we unlock full stripe without problem.
475 * Return <0 for error
476 */
477static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
478 bool locked)
479{
480 struct btrfs_block_group_cache *bg_cache;
481 struct btrfs_full_stripe_locks_tree *locks_root;
482 struct full_stripe_lock *fstripe_lock;
483 u64 fstripe_start;
484 bool freeit = false;
485 int ret = 0;
486
487 /* If we didn't acquire full stripe lock, no need to continue */
488 if (!locked)
489 return 0;
490
491 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
492 if (!bg_cache) {
493 ASSERT(0);
494 return -ENOENT;
495 }
496 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
497 goto out;
498
499 locks_root = &bg_cache->full_stripe_locks_root;
500 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
501
502 mutex_lock(&locks_root->lock);
503 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
504 /* Unpaired unlock_full_stripe() detected */
505 if (!fstripe_lock) {
506 WARN_ON(1);
507 ret = -ENOENT;
508 mutex_unlock(&locks_root->lock);
509 goto out;
510 }
511
512 if (fstripe_lock->refs == 0) {
513 WARN_ON(1);
514 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
515 fstripe_lock->logical);
516 } else {
517 fstripe_lock->refs--;
518 }
519
520 if (fstripe_lock->refs == 0) {
521 rb_erase(&fstripe_lock->node, &locks_root->root);
522 freeit = true;
523 }
524 mutex_unlock(&locks_root->lock);
525
526 mutex_unlock(&fstripe_lock->mutex);
527 if (freeit)
528 kfree(fstripe_lock);
529out:
530 btrfs_put_block_group(bg_cache);
531 return ret;
532}
533
534/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100535 * used for workers that require transaction commits (i.e., for the
536 * NOCOW case)
537 */
538static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
539{
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400540 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100541
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200542 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100543 /*
544 * increment scrubs_running to prevent cancel requests from
545 * completing as long as a worker is running. we must also
546 * increment scrubs_paused to prevent deadlocking on pause
547 * requests used for transactions commits (as the worker uses a
548 * transaction context). it is safe to regard the worker
549 * as paused for all matters practical. effectively, we only
550 * avoid cancellation requests from completing.
551 */
552 mutex_lock(&fs_info->scrub_lock);
553 atomic_inc(&fs_info->scrubs_running);
554 atomic_inc(&fs_info->scrubs_paused);
555 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800556
557 /*
558 * check if @scrubs_running=@scrubs_paused condition
559 * inside wait_event() is not an atomic operation.
560 * which means we may inc/dec @scrub_running/paused
561 * at any time. Let's wake up @scrub_pause_wait as
562 * much as we can to let commit transaction blocked less.
563 */
564 wake_up(&fs_info->scrub_pause_wait);
565
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100566 atomic_inc(&sctx->workers_pending);
567}
568
569/* used for workers that require transaction commits */
570static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
571{
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400572 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100573
574 /*
575 * see scrub_pending_trans_workers_inc() why we're pretending
576 * to be paused in the scrub counters
577 */
578 mutex_lock(&fs_info->scrub_lock);
579 atomic_dec(&fs_info->scrubs_running);
580 atomic_dec(&fs_info->scrubs_paused);
581 mutex_unlock(&fs_info->scrub_lock);
582 atomic_dec(&sctx->workers_pending);
583 wake_up(&fs_info->scrub_pause_wait);
584 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000585 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100586}
587
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100588static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100589{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100590 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100591 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100592 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100593 struct btrfs_ordered_sum, list);
594 list_del(&sum->list);
595 kfree(sum);
596 }
597}
598
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100599static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100600{
601 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100602
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100603 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100604 return;
605
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400606 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100607 if (sctx->curr != -1) {
608 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400609
610 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100611 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400612 scrub_block_put(sbio->pagev[i]->sblock);
613 }
614 bio_put(sbio->bio);
615 }
616
Stefan Behrensff023aa2012-11-06 11:43:11 +0100617 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100618 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100619
620 if (!sbio)
621 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100622 kfree(sbio);
623 }
624
David Sterba3fb99302017-05-16 19:10:32 +0200625 kfree(sctx->wr_curr_bio);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100626 scrub_free_csums(sctx);
627 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100628}
629
Filipe Mananaf55985f2015-02-09 21:14:24 +0000630static void scrub_put_ctx(struct scrub_ctx *sctx)
631{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200632 if (refcount_dec_and_test(&sctx->refs))
Filipe Mananaf55985f2015-02-09 21:14:24 +0000633 scrub_free_ctx(sctx);
634}
635
Arne Jansena2de7332011-03-08 14:14:00 +0100636static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100637struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100638{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100639 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100640 int i;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400641 struct btrfs_fs_info *fs_info = dev->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100642
David Sterba58c4e172016-02-11 10:49:42 +0100643 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100644 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100645 goto nomem;
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200646 refcount_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100647 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200648 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100649 sctx->curr = -1;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400650 sctx->fs_info = dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100651 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100652 struct scrub_bio *sbio;
653
David Sterba58c4e172016-02-11 10:49:42 +0100654 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
Arne Jansena2de7332011-03-08 14:14:00 +0100655 if (!sbio)
656 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100657 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100658
Arne Jansena2de7332011-03-08 14:14:00 +0100659 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100660 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400661 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800662 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
663 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100664
Stefan Behrensff023aa2012-11-06 11:43:11 +0100665 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100666 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200667 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100668 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100669 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100670 sctx->first_free = 0;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100671 atomic_set(&sctx->bios_in_flight, 0);
672 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100673 atomic_set(&sctx->cancel_req, 0);
674 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
675 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100676
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100677 spin_lock_init(&sctx->list_lock);
678 spin_lock_init(&sctx->stat_lock);
679 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100680
David Sterba3fb99302017-05-16 19:10:32 +0200681 WARN_ON(sctx->wr_curr_bio != NULL);
682 mutex_init(&sctx->wr_lock);
683 sctx->wr_curr_bio = NULL;
David Sterba8fcdac32017-05-16 19:10:23 +0200684 if (is_dev_replace) {
David Sterbaded56182017-06-26 15:19:00 +0200685 WARN_ON(!fs_info->dev_replace.tgtdev);
David Sterba3fb99302017-05-16 19:10:32 +0200686 sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
David Sterbaded56182017-06-26 15:19:00 +0200687 sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
David Sterba2073c4c22017-03-31 17:12:51 +0200688 sctx->flush_all_writes = false;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100689 }
David Sterba8fcdac32017-05-16 19:10:23 +0200690
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100691 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100692
693nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100694 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100695 return ERR_PTR(-ENOMEM);
696}
697
Stefan Behrensff023aa2012-11-06 11:43:11 +0100698static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
699 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200700{
701 u64 isize;
702 u32 nlink;
703 int ret;
704 int i;
David Sterbade2491f2017-05-31 19:21:38 +0200705 unsigned nofs_flag;
Jan Schmidt558540c2011-06-13 19:59:12 +0200706 struct extent_buffer *eb;
707 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100708 struct scrub_warning *swarn = warn_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400709 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200710 struct inode_fs_paths *ipath = NULL;
711 struct btrfs_root *local_root;
712 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100713 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200714
715 root_key.objectid = root;
716 root_key.type = BTRFS_ROOT_ITEM_KEY;
717 root_key.offset = (u64)-1;
718 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
719 if (IS_ERR(local_root)) {
720 ret = PTR_ERR(local_root);
721 goto err;
722 }
723
David Sterba14692cc2015-01-02 18:55:46 +0100724 /*
725 * this makes the path point to (inum INODE_ITEM ioff)
726 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100727 key.objectid = inum;
728 key.type = BTRFS_INODE_ITEM_KEY;
729 key.offset = 0;
730
731 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200732 if (ret) {
733 btrfs_release_path(swarn->path);
734 goto err;
735 }
736
737 eb = swarn->path->nodes[0];
738 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
739 struct btrfs_inode_item);
740 isize = btrfs_inode_size(eb, inode_item);
741 nlink = btrfs_inode_nlink(eb, inode_item);
742 btrfs_release_path(swarn->path);
743
David Sterbade2491f2017-05-31 19:21:38 +0200744 /*
745 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
746 * uses GFP_NOFS in this context, so we keep it consistent but it does
747 * not seem to be strictly necessary.
748 */
749 nofs_flag = memalloc_nofs_save();
Jan Schmidt558540c2011-06-13 19:59:12 +0200750 ipath = init_ipath(4096, local_root, swarn->path);
David Sterbade2491f2017-05-31 19:21:38 +0200751 memalloc_nofs_restore(nofs_flag);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300752 if (IS_ERR(ipath)) {
753 ret = PTR_ERR(ipath);
754 ipath = NULL;
755 goto err;
756 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200757 ret = paths_from_inode(inum, ipath);
758
759 if (ret < 0)
760 goto err;
761
762 /*
763 * we deliberately ignore the bit ipath might have been too small to
764 * hold all of the paths here
765 */
766 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400767 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200768"%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 -0400769 swarn->errstr, swarn->logical,
770 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200771 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400772 root, inum, offset,
773 min(isize - offset, (u64)PAGE_SIZE), nlink,
774 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200775
776 free_ipath(ipath);
777 return 0;
778
779err:
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400780 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200781 "%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 -0400782 swarn->errstr, swarn->logical,
783 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200784 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400785 root, inum, offset, ret);
Jan Schmidt558540c2011-06-13 19:59:12 +0200786
787 free_ipath(ipath);
788 return 0;
789}
790
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400791static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200792{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100793 struct btrfs_device *dev;
794 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200795 struct btrfs_path *path;
796 struct btrfs_key found_key;
797 struct extent_buffer *eb;
798 struct btrfs_extent_item *ei;
799 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200800 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100801 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600802 u64 flags = 0;
803 u64 ref_root;
804 u32 item_size;
Dan Carpenter07c9a8e2016-03-11 11:08:56 +0300805 u8 ref_level = 0;
Liu Bo69917e42012-09-07 20:01:28 -0600806 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200807
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100808 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100809 dev = sblock->pagev[0]->dev;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400810 fs_info = sblock->sctx->fs_info;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100811
Jan Schmidt558540c2011-06-13 19:59:12 +0200812 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200813 if (!path)
814 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200815
David Sterba6aa21262017-10-04 17:07:07 +0200816 swarn.physical = sblock->pagev[0]->physical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100817 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200818 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100819 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200820
Liu Bo69917e42012-09-07 20:01:28 -0600821 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
822 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200823 if (ret < 0)
824 goto out;
825
Jan Schmidt4692cf52011-12-02 14:56:41 +0100826 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200827 swarn.extent_item_size = found_key.offset;
828
829 eb = path->nodes[0];
830 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
831 item_size = btrfs_item_size_nr(eb, path->slots[0]);
832
Liu Bo69917e42012-09-07 20:01:28 -0600833 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200834 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800835 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
836 item_size, &ref_root,
837 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200838 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200839"%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400840 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400841 rcu_str_deref(dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200842 swarn.physical,
Jan Schmidt558540c2011-06-13 19:59:12 +0200843 ref_level ? "node" : "leaf",
844 ret < 0 ? -1 : ref_level,
845 ret < 0 ? -1 : ref_root);
846 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600847 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200848 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600849 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200850 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100851 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100852 iterate_extent_inodes(fs_info, found_key.objectid,
853 extent_item_pos, 1,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400854 scrub_print_warning_inode, &swarn, false);
Jan Schmidt558540c2011-06-13 19:59:12 +0200855 }
856
857out:
858 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200859}
860
Stefan Behrensff023aa2012-11-06 11:43:11 +0100861static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200862{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200863 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200864 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100865 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200866 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200867 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200868 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200869 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000870 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200871 u64 end = offset + PAGE_SIZE - 1;
872 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000873 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200874
875 key.objectid = root;
876 key.type = BTRFS_ROOT_ITEM_KEY;
877 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000878
879 fs_info = fixup->root->fs_info;
880 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
881
882 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
883 if (IS_ERR(local_root)) {
884 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200885 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000886 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200887
888 key.type = BTRFS_INODE_ITEM_KEY;
889 key.objectid = inum;
890 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000891 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
892 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200893 if (IS_ERR(inode))
894 return PTR_ERR(inode);
895
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300896 index = offset >> PAGE_SHIFT;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200897
898 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200899 if (!page) {
900 ret = -ENOMEM;
901 goto out;
902 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200903
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200904 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200905 if (PageDirty(page)) {
906 /*
907 * we need to write the data to the defect sector. the
908 * data that was in that sector is not in memory,
909 * because the page was modified. we must not write the
910 * modified page to that sector.
911 *
912 * TODO: what could be done here: wait for the delalloc
913 * runner to write out that page (might involve
914 * COW) and see whether the sector is still
915 * referenced afterwards.
916 *
917 * For the meantime, we'll treat this error
918 * incorrectable, although there is a chance that a
919 * later scrub will find the bad sector again and that
920 * there's no dirty page in memory, then.
921 */
922 ret = -EIO;
923 goto out;
924 }
Josef Bacik6ec656b2017-05-05 11:57:14 -0400925 ret = repair_io_failure(fs_info, inum, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200926 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800927 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200928 fixup->mirror_num);
929 unlock_page(page);
930 corrected = !ret;
931 } else {
932 /*
933 * we need to get good data first. the general readpage path
934 * will call repair_io_failure for us, we just have to make
935 * sure we read the bad mirror.
936 */
937 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +0200938 EXTENT_DAMAGED);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200939 if (ret) {
940 /* set_extent_bits should give proper error */
941 WARN_ON(ret > 0);
942 if (ret > 0)
943 ret = -EFAULT;
944 goto out;
945 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200946
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200947 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
948 btrfs_get_extent,
949 fixup->mirror_num);
950 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200951
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200952 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
953 end, EXTENT_DAMAGED, 0, NULL);
954 if (!corrected)
955 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
David Sterba91166212016-04-26 23:54:39 +0200956 EXTENT_DAMAGED);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200957 }
958
959out:
960 if (page)
961 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200962
963 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200964
965 if (ret < 0)
966 return ret;
967
968 if (ret == 0 && corrected) {
969 /*
970 * we only need to call readpage for one of the inodes belonging
971 * to this extent. so make iterate_extent_inodes stop
972 */
973 return 1;
974 }
975
976 return -EIO;
977}
978
979static void scrub_fixup_nodatasum(struct btrfs_work *work)
980{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400981 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200982 int ret;
983 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100984 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200985 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200986 struct btrfs_path *path;
987 int uncorrectable = 0;
988
989 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100990 sctx = fixup->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400991 fs_info = fixup->root->fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200992
993 path = btrfs_alloc_path();
994 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100995 spin_lock(&sctx->stat_lock);
996 ++sctx->stat.malloc_errors;
997 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200998 uncorrectable = 1;
999 goto out;
1000 }
1001
1002 trans = btrfs_join_transaction(fixup->root);
1003 if (IS_ERR(trans)) {
1004 uncorrectable = 1;
1005 goto out;
1006 }
1007
1008 /*
1009 * the idea is to trigger a regular read through the standard path. we
1010 * read a page from the (failed) logical address by specifying the
1011 * corresponding copynum of the failed sector. thus, that readpage is
1012 * expected to fail.
1013 * that is the point where on-the-fly error correction will kick in
1014 * (once it's finished) and rewrite the failed sector if a good copy
1015 * can be found.
1016 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001017 ret = iterate_inodes_from_logical(fixup->logical, fs_info, path,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001018 scrub_fixup_readpage, fixup, false);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001019 if (ret < 0) {
1020 uncorrectable = 1;
1021 goto out;
1022 }
1023 WARN_ON(ret != 1);
1024
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001025 spin_lock(&sctx->stat_lock);
1026 ++sctx->stat.corrected_errors;
1027 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001028
1029out:
1030 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001031 btrfs_end_transaction(trans);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001032 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001033 spin_lock(&sctx->stat_lock);
1034 ++sctx->stat.uncorrectable_errors;
1035 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001036 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001037 &fs_info->dev_replace.num_uncorrectable_read_errors);
1038 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02001039 "unable to fixup (nodatasum) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001040 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001041 }
1042
1043 btrfs_free_path(path);
1044 kfree(fixup);
1045
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001046 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001047}
1048
Miao Xieaf8e2d12014-10-23 14:42:50 +08001049static inline void scrub_get_recover(struct scrub_recover *recover)
1050{
Elena Reshetova6f615012017-03-03 10:55:21 +02001051 refcount_inc(&recover->refs);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001052}
1053
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001054static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
1055 struct scrub_recover *recover)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001056{
Elena Reshetova6f615012017-03-03 10:55:21 +02001057 if (refcount_dec_and_test(&recover->refs)) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001058 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08001059 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001060 kfree(recover);
1061 }
1062}
1063
Arne Jansena2de7332011-03-08 14:14:00 +01001064/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001065 * scrub_handle_errored_block gets called when either verification of the
1066 * pages failed or the bio failed to read, e.g. with EIO. In the latter
1067 * case, this function handles all pages in the bio, even though only one
1068 * may be bad.
1069 * The goal of this function is to repair the errored block by using the
1070 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +01001071 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001072static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +01001073{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001074 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001075 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001076 struct btrfs_fs_info *fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001077 u64 logical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001078 unsigned int failed_mirror_index;
1079 unsigned int is_metadata;
1080 unsigned int have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001081 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
1082 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +01001083 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001084 int mirror_index;
1085 int page_num;
1086 int success;
Qu Wenruo28d70e22017-04-14 08:35:55 +08001087 bool full_stripe_locked;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001088 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1089 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +01001090
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001091 BUG_ON(sblock_to_check->page_count < 1);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001092 fs_info = sctx->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +00001093 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
1094 /*
1095 * if we find an error in a super block, we just report it.
1096 * They will get written with the next transaction commit
1097 * anyway
1098 */
1099 spin_lock(&sctx->stat_lock);
1100 ++sctx->stat.super_errors;
1101 spin_unlock(&sctx->stat_lock);
1102 return 0;
1103 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001104 logical = sblock_to_check->pagev[0]->logical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001105 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
1106 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
1107 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001108 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001109 have_csum = sblock_to_check->pagev[0]->have_csum;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001110 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001111
Qu Wenruo28d70e22017-04-14 08:35:55 +08001112 /*
1113 * For RAID5/6, race can happen for a different device scrub thread.
1114 * For data corruption, Parity and Data threads will both try
1115 * to recovery the data.
1116 * Race can lead to doubly added csum error, or even unrecoverable
1117 * error.
1118 */
1119 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
1120 if (ret < 0) {
1121 spin_lock(&sctx->stat_lock);
1122 if (ret == -ENOMEM)
1123 sctx->stat.malloc_errors++;
1124 sctx->stat.read_errors++;
1125 sctx->stat.uncorrectable_errors++;
1126 spin_unlock(&sctx->stat_lock);
1127 return ret;
1128 }
1129
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001130 /*
1131 * read all mirrors one after the other. This includes to
1132 * re-read the extent or metadata block that failed (that was
1133 * the cause that this fixup code is called) another time,
1134 * page by page this time in order to know which pages
1135 * caused I/O errors and which ones are good (for all mirrors).
1136 * It is the goal to handle the situation when more than one
1137 * mirror contains I/O errors, but the errors do not
1138 * overlap, i.e. the data can be repaired by selecting the
1139 * pages from those mirrors without I/O error on the
1140 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
1141 * would be that mirror #1 has an I/O error on the first page,
1142 * the second page is good, and mirror #2 has an I/O error on
1143 * the second page, but the first page is good.
1144 * Then the first page of the first mirror can be repaired by
1145 * taking the first page of the second mirror, and the
1146 * second page of the second mirror can be repaired by
1147 * copying the contents of the 2nd page of the 1st mirror.
1148 * One more note: if the pages of one mirror contain I/O
1149 * errors, the checksum cannot be verified. In order to get
1150 * the best data for repairing, the first attempt is to find
1151 * a mirror without I/O errors and with a validated checksum.
1152 * Only if this is not possible, the pages are picked from
1153 * mirrors with I/O errors without considering the checksum.
1154 * If the latter is the case, at the end, the checksum of the
1155 * repaired area is verified in order to correctly maintain
1156 * the statistics.
1157 */
1158
David Sterba31e818f2015-02-20 18:00:26 +01001159 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
1160 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001161 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001162 spin_lock(&sctx->stat_lock);
1163 sctx->stat.malloc_errors++;
1164 sctx->stat.read_errors++;
1165 sctx->stat.uncorrectable_errors++;
1166 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001167 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001168 goto out;
1169 }
1170
1171 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +08001172 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001173 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001174 spin_lock(&sctx->stat_lock);
1175 sctx->stat.read_errors++;
1176 sctx->stat.uncorrectable_errors++;
1177 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001178 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001179 goto out;
1180 }
1181 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
1182 sblock_bad = sblocks_for_recheck + failed_mirror_index;
1183
1184 /* build and submit the bios for the failed mirror, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001185 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001186
1187 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
1188 sblock_bad->no_io_error_seen) {
1189 /*
1190 * the error disappeared after reading page by page, or
1191 * the area was part of a huge bio and other parts of the
1192 * bio caused I/O errors, or the block layer merged several
1193 * read requests into one and the error is caused by a
1194 * different bio (usually one of the two latter cases is
1195 * the cause)
1196 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001197 spin_lock(&sctx->stat_lock);
1198 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001199 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001200 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001201
Stefan Behrensff023aa2012-11-06 11:43:11 +01001202 if (sctx->is_dev_replace)
1203 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001204 goto out;
1205 }
1206
1207 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001208 spin_lock(&sctx->stat_lock);
1209 sctx->stat.read_errors++;
1210 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001211 if (__ratelimit(&_rs))
1212 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001213 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001214 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001215 spin_lock(&sctx->stat_lock);
1216 sctx->stat.csum_errors++;
1217 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001218 if (__ratelimit(&_rs))
1219 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001220 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001221 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001222 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001223 spin_lock(&sctx->stat_lock);
1224 sctx->stat.verify_errors++;
1225 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001226 if (__ratelimit(&_rs))
1227 scrub_print_warning("checksum/header error",
1228 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001229 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001230 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001231 BTRFS_DEV_STAT_GENERATION_ERRS);
1232 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001233 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001234 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001235 }
1236
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001237 if (sctx->readonly) {
1238 ASSERT(!sctx->is_dev_replace);
1239 goto out;
1240 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001241
Qu Wenruo665d4952018-07-11 13:41:21 +08001242 /*
1243 * NOTE: Even for nodatasum case, it's still possible that it's a
1244 * compressed data extent, thus scrub_fixup_nodatasum(), which write
1245 * inode page cache onto disk, could cause serious data corruption.
1246 *
1247 * So here we could only read from disk, and hope our recovery could
1248 * reach disk before the newer write.
1249 */
1250 if (0 && !is_metadata && !have_csum) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001251 struct scrub_fixup_nodatasum *fixup_nodatasum;
1252
Stefan Behrensff023aa2012-11-06 11:43:11 +01001253 WARN_ON(sctx->is_dev_replace);
1254
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001255 /*
1256 * !is_metadata and !have_csum, this means that the data
Nicholas D Steeves01327612016-05-19 21:18:45 -04001257 * might not be COWed, that it might be modified
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001258 * concurrently. The general strategy to work on the
1259 * commit root does not help in the case when COW is not
1260 * used.
1261 */
1262 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1263 if (!fixup_nodatasum)
1264 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001265 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001266 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001267 fixup_nodatasum->logical = logical;
1268 fixup_nodatasum->root = fs_info->extent_root;
1269 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001270 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001271 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1272 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001273 btrfs_queue_work(fs_info->scrub_workers,
1274 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001275 goto out;
1276 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001277
1278 /*
1279 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001280 * checksums.
1281 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001282 * errors and also does not have a checksum error.
1283 * If one is found, and if a checksum is present, the full block
1284 * that is known to contain an error is rewritten. Afterwards
1285 * the block is known to be corrected.
1286 * If a mirror is found which is completely correct, and no
1287 * checksum is present, only those pages are rewritten that had
1288 * an I/O error in the block to be repaired, since it cannot be
1289 * determined, which copy of the other pages is better (and it
1290 * could happen otherwise that a correct page would be
1291 * overwritten by a bad one).
1292 */
Liu Bo762221f2018-01-02 13:36:42 -07001293 for (mirror_index = 0; ;mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001294 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001295
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001296 if (mirror_index == failed_mirror_index)
1297 continue;
Liu Bo762221f2018-01-02 13:36:42 -07001298
1299 /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
1300 if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1301 if (mirror_index >= BTRFS_MAX_MIRRORS)
1302 break;
1303 if (!sblocks_for_recheck[mirror_index].page_count)
1304 break;
1305
1306 sblock_other = sblocks_for_recheck + mirror_index;
1307 } else {
1308 struct scrub_recover *r = sblock_bad->pagev[0]->recover;
1309 int max_allowed = r->bbio->num_stripes -
1310 r->bbio->num_tgtdevs;
1311
1312 if (mirror_index >= max_allowed)
1313 break;
1314 if (!sblocks_for_recheck[1].page_count)
1315 break;
1316
1317 ASSERT(failed_mirror_index == 0);
1318 sblock_other = sblocks_for_recheck + 1;
1319 sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
1320 }
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001321
1322 /* build and submit the bios, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001323 scrub_recheck_block(fs_info, sblock_other, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001324
1325 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001326 !sblock_other->checksum_error &&
1327 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001328 if (sctx->is_dev_replace) {
1329 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001330 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001331 } else {
1332 ret = scrub_repair_block_from_good_copy(
1333 sblock_bad, sblock_other);
1334 if (!ret)
1335 goto corrected_error;
1336 }
Arne Jansena2de7332011-03-08 14:14:00 +01001337 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001338 }
1339
Zhao Leib968fed2015-01-20 15:11:41 +08001340 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1341 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001342
1343 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001344 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001345 * repaired, continue by picking good copies of those pages.
1346 * Select the good pages from mirrors to rewrite bad pages from
1347 * the area to fix. Afterwards verify the checksum of the block
1348 * that is supposed to be repaired. This verification step is
1349 * only done for the purpose of statistic counting and for the
1350 * final scrub report, whether errors remain.
1351 * A perfect algorithm could make use of the checksum and try
1352 * all possible combinations of pages from the different mirrors
1353 * until the checksum verification succeeds. For example, when
1354 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1355 * of mirror #2 is readable but the final checksum test fails,
1356 * then the 2nd page of mirror #3 could be tried, whether now
Nicholas D Steeves01327612016-05-19 21:18:45 -04001357 * the final checksum succeeds. But this would be a rare
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001358 * exception and is therefore not implemented. At least it is
1359 * avoided that the good copy is overwritten.
1360 * A more useful improvement would be to pick the sectors
1361 * without I/O error based on sector sizes (512 bytes on legacy
1362 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1363 * mirror could be repaired by taking 512 byte of a different
1364 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1365 * area are unreadable.
1366 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001367 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001368 for (page_num = 0; page_num < sblock_bad->page_count;
1369 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001370 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001371 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001372
Zhao Leib968fed2015-01-20 15:11:41 +08001373 /* skip no-io-error page in scrub */
1374 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001375 continue;
1376
Liu Bo47597002018-03-02 16:10:41 -07001377 if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1378 /*
1379 * In case of dev replace, if raid56 rebuild process
1380 * didn't work out correct data, then copy the content
1381 * in sblock_bad to make sure target device is identical
1382 * to source device, instead of writing garbage data in
1383 * sblock_for_recheck array to target device.
1384 */
1385 sblock_other = NULL;
1386 } else if (page_bad->io_error) {
1387 /* try to find no-io-error page in mirrors */
Zhao Leib968fed2015-01-20 15:11:41 +08001388 for (mirror_index = 0;
1389 mirror_index < BTRFS_MAX_MIRRORS &&
1390 sblocks_for_recheck[mirror_index].page_count > 0;
1391 mirror_index++) {
1392 if (!sblocks_for_recheck[mirror_index].
1393 pagev[page_num]->io_error) {
1394 sblock_other = sblocks_for_recheck +
1395 mirror_index;
1396 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001397 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001398 }
Zhao Leib968fed2015-01-20 15:11:41 +08001399 if (!sblock_other)
1400 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001401 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001402
Zhao Leib968fed2015-01-20 15:11:41 +08001403 if (sctx->is_dev_replace) {
1404 /*
1405 * did not find a mirror to fetch the page
1406 * from. scrub_write_page_to_dev_replace()
1407 * handles this case (page->io_error), by
1408 * filling the block with zeros before
1409 * submitting the write request
1410 */
1411 if (!sblock_other)
1412 sblock_other = sblock_bad;
1413
1414 if (scrub_write_page_to_dev_replace(sblock_other,
1415 page_num) != 0) {
1416 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001417 &fs_info->dev_replace.num_write_errors);
Zhao Leib968fed2015-01-20 15:11:41 +08001418 success = 0;
1419 }
1420 } else if (sblock_other) {
1421 ret = scrub_repair_page_from_good_copy(sblock_bad,
1422 sblock_other,
1423 page_num, 0);
1424 if (0 == ret)
1425 page_bad->io_error = 0;
1426 else
1427 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001428 }
1429 }
1430
Zhao Leib968fed2015-01-20 15:11:41 +08001431 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001432 if (is_metadata || have_csum) {
1433 /*
1434 * need to verify the checksum now that all
1435 * sectors on disk are repaired (the write
1436 * request for data to be repaired is on its way).
1437 * Just be lazy and use scrub_recheck_block()
1438 * which re-reads the data before the checksum
1439 * is verified, but most likely the data comes out
1440 * of the page cache.
1441 */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001442 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001443 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001444 !sblock_bad->checksum_error &&
1445 sblock_bad->no_io_error_seen)
1446 goto corrected_error;
1447 else
1448 goto did_not_correct_error;
1449 } else {
1450corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001451 spin_lock(&sctx->stat_lock);
1452 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001453 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001454 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001455 btrfs_err_rl_in_rcu(fs_info,
1456 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001457 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001458 }
1459 } else {
1460did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001461 spin_lock(&sctx->stat_lock);
1462 sctx->stat.uncorrectable_errors++;
1463 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001464 btrfs_err_rl_in_rcu(fs_info,
1465 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001466 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001467 }
1468
1469out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001470 if (sblocks_for_recheck) {
1471 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1472 mirror_index++) {
1473 struct scrub_block *sblock = sblocks_for_recheck +
1474 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001475 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001476 int page_index;
1477
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001478 for (page_index = 0; page_index < sblock->page_count;
1479 page_index++) {
1480 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001481 recover = sblock->pagev[page_index]->recover;
1482 if (recover) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001483 scrub_put_recover(fs_info, recover);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001484 sblock->pagev[page_index]->recover =
1485 NULL;
1486 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001487 scrub_page_put(sblock->pagev[page_index]);
1488 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001489 }
1490 kfree(sblocks_for_recheck);
1491 }
1492
Qu Wenruo28d70e22017-04-14 08:35:55 +08001493 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
1494 if (ret < 0)
1495 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001496 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001497}
1498
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001499static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001500{
Zhao Lei10f11902015-01-20 15:11:43 +08001501 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1502 return 2;
1503 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1504 return 3;
1505 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001506 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001507}
1508
Zhao Lei10f11902015-01-20 15:11:43 +08001509static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1510 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001511 u64 mapped_length,
1512 int nstripes, int mirror,
1513 int *stripe_index,
1514 u64 *stripe_offset)
1515{
1516 int i;
1517
Zhao Leiffe2d202015-01-20 15:11:44 +08001518 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001519 /* RAID5/6 */
1520 for (i = 0; i < nstripes; i++) {
1521 if (raid_map[i] == RAID6_Q_STRIPE ||
1522 raid_map[i] == RAID5_P_STRIPE)
1523 continue;
1524
1525 if (logical >= raid_map[i] &&
1526 logical < raid_map[i] + mapped_length)
1527 break;
1528 }
1529
1530 *stripe_index = i;
1531 *stripe_offset = logical - raid_map[i];
1532 } else {
1533 /* The other RAID type */
1534 *stripe_index = mirror;
1535 *stripe_offset = 0;
1536 }
1537}
1538
Zhao Leibe50a8d2015-01-20 15:11:42 +08001539static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001540 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001541{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001542 struct scrub_ctx *sctx = original_sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001543 struct btrfs_fs_info *fs_info = sctx->fs_info;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001544 u64 length = original_sblock->page_count * PAGE_SIZE;
1545 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001546 u64 generation = original_sblock->pagev[0]->generation;
1547 u64 flags = original_sblock->pagev[0]->flags;
1548 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001549 struct scrub_recover *recover;
1550 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001551 u64 sublen;
1552 u64 mapped_length;
1553 u64 stripe_offset;
1554 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001555 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001556 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001557 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001558 int ret;
1559
1560 /*
Zhao Lei57019342015-01-20 15:11:45 +08001561 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001562 * are not used (and not set) in the blocks that are used for
1563 * the recheck procedure
1564 */
1565
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001566 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001567 sublen = min_t(u64, length, PAGE_SIZE);
1568 mapped_length = sublen;
1569 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001570
1571 /*
1572 * with a length of PAGE_SIZE, each returned stripe
1573 * represents one mirror
1574 */
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001575 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001576 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
David Sterba825ad4c2017-03-28 14:45:22 +02001577 logical, &mapped_length, &bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001578 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001579 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001580 btrfs_bio_counter_dec(fs_info);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001581 return -EIO;
1582 }
1583
Miao Xieaf8e2d12014-10-23 14:42:50 +08001584 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1585 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001586 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001587 btrfs_bio_counter_dec(fs_info);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001588 return -ENOMEM;
1589 }
1590
Elena Reshetova6f615012017-03-03 10:55:21 +02001591 refcount_set(&recover->refs, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001592 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001593 recover->map_length = mapped_length;
1594
Ashish Samant24731142016-04-29 18:33:59 -07001595 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001596
Zhao Leibe50a8d2015-01-20 15:11:42 +08001597 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001598
Miao Xieaf8e2d12014-10-23 14:42:50 +08001599 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001600 mirror_index++) {
1601 struct scrub_block *sblock;
1602 struct scrub_page *page;
1603
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001604 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001605 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001606
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001607 page = kzalloc(sizeof(*page), GFP_NOFS);
1608 if (!page) {
1609leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001610 spin_lock(&sctx->stat_lock);
1611 sctx->stat.malloc_errors++;
1612 spin_unlock(&sctx->stat_lock);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001613 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001614 return -ENOMEM;
1615 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001616 scrub_page_get(page);
1617 sblock->pagev[page_index] = page;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001618 page->sblock = sblock;
1619 page->flags = flags;
1620 page->generation = generation;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001621 page->logical = logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001622 page->have_csum = have_csum;
1623 if (have_csum)
1624 memcpy(page->csum,
1625 original_sblock->pagev[0]->csum,
1626 sctx->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001627
Zhao Lei10f11902015-01-20 15:11:43 +08001628 scrub_stripe_index_and_offset(logical,
1629 bbio->map_type,
1630 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001631 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001632 bbio->num_stripes -
1633 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001634 mirror_index,
1635 &stripe_index,
1636 &stripe_offset);
1637 page->physical = bbio->stripes[stripe_index].physical +
1638 stripe_offset;
1639 page->dev = bbio->stripes[stripe_index].dev;
1640
Stefan Behrensff023aa2012-11-06 11:43:11 +01001641 BUG_ON(page_index >= original_sblock->page_count);
1642 page->physical_for_dev_replace =
1643 original_sblock->pagev[page_index]->
1644 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001645 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001646 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001647 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001648 page->page = alloc_page(GFP_NOFS);
1649 if (!page->page)
1650 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001651
1652 scrub_get_recover(recover);
1653 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001654 }
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001655 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001656 length -= sublen;
1657 logical += sublen;
1658 page_index++;
1659 }
1660
1661 return 0;
1662}
1663
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001664static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001665{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001666 complete(bio->bi_private);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001667}
1668
Miao Xieaf8e2d12014-10-23 14:42:50 +08001669static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1670 struct bio *bio,
1671 struct scrub_page *page)
1672{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001673 DECLARE_COMPLETION_ONSTACK(done);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001674 int ret;
Liu Bo762221f2018-01-02 13:36:42 -07001675 int mirror_num;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001676
Miao Xieaf8e2d12014-10-23 14:42:50 +08001677 bio->bi_iter.bi_sector = page->logical >> 9;
1678 bio->bi_private = &done;
1679 bio->bi_end_io = scrub_bio_wait_endio;
1680
Liu Bo762221f2018-01-02 13:36:42 -07001681 mirror_num = page->sblock->pagev[0]->mirror_num;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001682 ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001683 page->recover->map_length,
Liu Bo762221f2018-01-02 13:36:42 -07001684 mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001685 if (ret)
1686 return ret;
1687
Liu Bob4ff5ad2017-11-30 17:26:39 -07001688 wait_for_completion_io(&done);
1689 return blk_status_to_errno(bio->bi_status);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001690}
1691
Liu Bo6ca17652018-03-07 12:08:09 -07001692static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
1693 struct scrub_block *sblock)
1694{
1695 struct scrub_page *first_page = sblock->pagev[0];
1696 struct bio *bio;
1697 int page_num;
1698
1699 /* All pages in sblock belong to the same stripe on the same device. */
1700 ASSERT(first_page->dev);
1701 if (!first_page->dev->bdev)
1702 goto out;
1703
1704 bio = btrfs_io_bio_alloc(BIO_MAX_PAGES);
1705 bio_set_dev(bio, first_page->dev->bdev);
1706
1707 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1708 struct scrub_page *page = sblock->pagev[page_num];
1709
1710 WARN_ON(!page->page);
1711 bio_add_page(bio, page->page, PAGE_SIZE, 0);
1712 }
1713
1714 if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) {
1715 bio_put(bio);
1716 goto out;
1717 }
1718
1719 bio_put(bio);
1720
1721 scrub_recheck_block_checksum(sblock);
1722
1723 return;
1724out:
1725 for (page_num = 0; page_num < sblock->page_count; page_num++)
1726 sblock->pagev[page_num]->io_error = 1;
1727
1728 sblock->no_io_error_seen = 0;
1729}
1730
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001731/*
1732 * this function will check the on disk data for checksum errors, header
1733 * errors and read I/O errors. If any I/O errors happen, the exact pages
1734 * which are errored are marked as being bad. The goal is to enable scrub
1735 * to take those pages that are not errored from all the mirrors so that
1736 * the pages that are errored in the just handled mirror can be repaired.
1737 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001738static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001739 struct scrub_block *sblock,
1740 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001741{
1742 int page_num;
1743
1744 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001745
Liu Bo6ca17652018-03-07 12:08:09 -07001746 /* short cut for raid56 */
1747 if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0]))
1748 return scrub_recheck_block_on_raid56(fs_info, sblock);
1749
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001750 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1751 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001752 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001753
Stefan Behrens442a4f62012-05-25 16:06:08 +02001754 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001755 page->io_error = 1;
1756 sblock->no_io_error_seen = 0;
1757 continue;
1758 }
1759
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001760 WARN_ON(!page->page);
David Sterbac5e4c3d2017-06-12 17:29:41 +02001761 bio = btrfs_io_bio_alloc(1);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001762 bio_set_dev(bio, page->dev->bdev);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001763
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001764 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Liu Bo6ca17652018-03-07 12:08:09 -07001765 bio->bi_iter.bi_sector = page->physical >> 9;
1766 bio->bi_opf = REQ_OP_READ;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001767
Liu Bo6ca17652018-03-07 12:08:09 -07001768 if (btrfsic_submit_bio_wait(bio)) {
1769 page->io_error = 1;
1770 sblock->no_io_error_seen = 0;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001771 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001772
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001773 bio_put(bio);
1774 }
1775
1776 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001777 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001778}
1779
Miao Xie17a9be22014-07-24 11:37:08 +08001780static inline int scrub_check_fsid(u8 fsid[],
1781 struct scrub_page *spage)
1782{
1783 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1784 int ret;
1785
Anand Jain44880fd2017-07-29 17:50:09 +08001786 ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Miao Xie17a9be22014-07-24 11:37:08 +08001787 return !ret;
1788}
1789
Zhao Leiba7cf982015-08-24 21:18:02 +08001790static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001791{
Zhao Leiba7cf982015-08-24 21:18:02 +08001792 sblock->header_error = 0;
1793 sblock->checksum_error = 0;
1794 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001795
Zhao Leiba7cf982015-08-24 21:18:02 +08001796 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1797 scrub_checksum_data(sblock);
1798 else
1799 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001800}
1801
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001802static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001803 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001804{
1805 int page_num;
1806 int ret = 0;
1807
1808 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1809 int ret_sub;
1810
1811 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1812 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001813 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001814 if (ret_sub)
1815 ret = ret_sub;
1816 }
1817
1818 return ret;
1819}
1820
1821static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1822 struct scrub_block *sblock_good,
1823 int page_num, int force_write)
1824{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001825 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1826 struct scrub_page *page_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001827 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001828
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001829 BUG_ON(page_bad->page == NULL);
1830 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001831 if (force_write || sblock_bad->header_error ||
1832 sblock_bad->checksum_error || page_bad->io_error) {
1833 struct bio *bio;
1834 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001835
Stefan Behrensff023aa2012-11-06 11:43:11 +01001836 if (!page_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001837 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001838 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001839 return -EIO;
1840 }
1841
David Sterbac5e4c3d2017-06-12 17:29:41 +02001842 bio = btrfs_io_bio_alloc(1);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001843 bio_set_dev(bio, page_bad->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001844 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001845 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001846
1847 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1848 if (PAGE_SIZE != ret) {
1849 bio_put(bio);
1850 return -EIO;
1851 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001852
Mike Christie4e49ea42016-06-05 14:31:41 -05001853 if (btrfsic_submit_bio_wait(bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001854 btrfs_dev_stat_inc_and_print(page_bad->dev,
1855 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001856 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001857 &fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001858 bio_put(bio);
1859 return -EIO;
1860 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001861 bio_put(bio);
1862 }
1863
1864 return 0;
1865}
1866
Stefan Behrensff023aa2012-11-06 11:43:11 +01001867static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1868{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001869 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001870 int page_num;
1871
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001872 /*
1873 * This block is used for the check of the parity on the source device,
1874 * so the data needn't be written into the destination device.
1875 */
1876 if (sblock->sparity)
1877 return;
1878
Stefan Behrensff023aa2012-11-06 11:43:11 +01001879 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1880 int ret;
1881
1882 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1883 if (ret)
1884 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001885 &fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001886 }
1887}
1888
1889static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1890 int page_num)
1891{
1892 struct scrub_page *spage = sblock->pagev[page_num];
1893
1894 BUG_ON(spage->page == NULL);
1895 if (spage->io_error) {
1896 void *mapped_buffer = kmap_atomic(spage->page);
1897
David Sterba619a9742017-03-29 20:48:44 +02001898 clear_page(mapped_buffer);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001899 flush_dcache_page(spage->page);
1900 kunmap_atomic(mapped_buffer);
1901 }
1902 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1903}
1904
1905static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1906 struct scrub_page *spage)
1907{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001908 struct scrub_bio *sbio;
1909 int ret;
1910
David Sterba3fb99302017-05-16 19:10:32 +02001911 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001912again:
David Sterba3fb99302017-05-16 19:10:32 +02001913 if (!sctx->wr_curr_bio) {
1914 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001915 GFP_KERNEL);
David Sterba3fb99302017-05-16 19:10:32 +02001916 if (!sctx->wr_curr_bio) {
1917 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001918 return -ENOMEM;
1919 }
David Sterba3fb99302017-05-16 19:10:32 +02001920 sctx->wr_curr_bio->sctx = sctx;
1921 sctx->wr_curr_bio->page_count = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001922 }
David Sterba3fb99302017-05-16 19:10:32 +02001923 sbio = sctx->wr_curr_bio;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001924 if (sbio->page_count == 0) {
1925 struct bio *bio;
1926
1927 sbio->physical = spage->physical_for_dev_replace;
1928 sbio->logical = spage->logical;
David Sterba3fb99302017-05-16 19:10:32 +02001929 sbio->dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001930 bio = sbio->bio;
1931 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02001932 bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001933 sbio->bio = bio;
1934 }
1935
1936 bio->bi_private = sbio;
1937 bio->bi_end_io = scrub_wr_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001938 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001939 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001940 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001941 sbio->status = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001942 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1943 spage->physical_for_dev_replace ||
1944 sbio->logical + sbio->page_count * PAGE_SIZE !=
1945 spage->logical) {
1946 scrub_wr_submit(sctx);
1947 goto again;
1948 }
1949
1950 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1951 if (ret != PAGE_SIZE) {
1952 if (sbio->page_count < 1) {
1953 bio_put(sbio->bio);
1954 sbio->bio = NULL;
David Sterba3fb99302017-05-16 19:10:32 +02001955 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001956 return -EIO;
1957 }
1958 scrub_wr_submit(sctx);
1959 goto again;
1960 }
1961
1962 sbio->pagev[sbio->page_count] = spage;
1963 scrub_page_get(spage);
1964 sbio->page_count++;
David Sterba3fb99302017-05-16 19:10:32 +02001965 if (sbio->page_count == sctx->pages_per_wr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001966 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02001967 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001968
1969 return 0;
1970}
1971
1972static void scrub_wr_submit(struct scrub_ctx *sctx)
1973{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001974 struct scrub_bio *sbio;
1975
David Sterba3fb99302017-05-16 19:10:32 +02001976 if (!sctx->wr_curr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001977 return;
1978
David Sterba3fb99302017-05-16 19:10:32 +02001979 sbio = sctx->wr_curr_bio;
1980 sctx->wr_curr_bio = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001981 WARN_ON(!sbio->bio->bi_disk);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001982 scrub_pending_bio_inc(sctx);
1983 /* process all writes in a single worker thread. Then the block layer
1984 * orders the requests before sending them to the driver which
1985 * doubled the write performance on spinning disks when measured
1986 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001987 btrfsic_submit_bio(sbio->bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001988}
1989
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001990static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001991{
1992 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001993 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001994
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001995 sbio->status = bio->bi_status;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001996 sbio->bio = bio;
1997
Liu Bo9e0af232014-08-15 23:36:53 +08001998 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1999 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08002000 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002001}
2002
2003static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
2004{
2005 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
2006 struct scrub_ctx *sctx = sbio->sctx;
2007 int i;
2008
2009 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002010 if (sbio->status) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002011 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002012 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002013
2014 for (i = 0; i < sbio->page_count; i++) {
2015 struct scrub_page *spage = sbio->pagev[i];
2016
2017 spage->io_error = 1;
2018 btrfs_dev_replace_stats_inc(&dev_replace->
2019 num_write_errors);
2020 }
2021 }
2022
2023 for (i = 0; i < sbio->page_count; i++)
2024 scrub_page_put(sbio->pagev[i]);
2025
2026 bio_put(sbio->bio);
2027 kfree(sbio);
2028 scrub_pending_bio_dec(sctx);
2029}
2030
2031static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002032{
2033 u64 flags;
2034 int ret;
2035
Zhao Leiba7cf982015-08-24 21:18:02 +08002036 /*
2037 * No need to initialize these stats currently,
2038 * because this function only use return value
2039 * instead of these stats value.
2040 *
2041 * Todo:
2042 * always use stats
2043 */
2044 sblock->header_error = 0;
2045 sblock->generation_error = 0;
2046 sblock->checksum_error = 0;
2047
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002048 WARN_ON(sblock->page_count < 1);
2049 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002050 ret = 0;
2051 if (flags & BTRFS_EXTENT_FLAG_DATA)
2052 ret = scrub_checksum_data(sblock);
2053 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
2054 ret = scrub_checksum_tree_block(sblock);
2055 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
2056 (void)scrub_checksum_super(sblock);
2057 else
2058 WARN_ON(1);
2059 if (ret)
2060 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002061
2062 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002063}
2064
2065static int scrub_checksum_data(struct scrub_block *sblock)
2066{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002067 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002068 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002069 u8 *on_disk_csum;
2070 struct page *page;
2071 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01002072 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002073 u64 len;
2074 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002075
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002076 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002077 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002078 return 0;
2079
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002080 on_disk_csum = sblock->pagev[0]->csum;
2081 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002082 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002083
David Sterba25cc1222017-05-16 19:10:41 +02002084 len = sctx->fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002085 index = 0;
2086 for (;;) {
2087 u64 l = min_t(u64, len, PAGE_SIZE);
2088
Liu Bob0496682013-03-14 14:57:45 +00002089 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002090 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002091 len -= l;
2092 if (len == 0)
2093 break;
2094 index++;
2095 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002096 BUG_ON(!sblock->pagev[index]->page);
2097 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002098 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002099 }
2100
Arne Jansena2de7332011-03-08 14:14:00 +01002101 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002102 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002103 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002104
Zhao Leiba7cf982015-08-24 21:18:02 +08002105 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002106}
2107
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002108static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002109{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002110 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002111 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002112 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002113 u8 calculated_csum[BTRFS_CSUM_SIZE];
2114 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2115 struct page *page;
2116 void *mapped_buffer;
2117 u64 mapped_size;
2118 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002119 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002120 u64 len;
2121 int index;
2122
2123 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002124 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002125 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002126 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002127 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002128
2129 /*
2130 * we don't use the getter functions here, as we
2131 * a) don't have an extent buffer and
2132 * b) the page is already kmapped
2133 */
Qu Wenruo3cae2102013-07-16 11:19:18 +08002134 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08002135 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002136
Zhao Leiba7cf982015-08-24 21:18:02 +08002137 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
2138 sblock->header_error = 1;
2139 sblock->generation_error = 1;
2140 }
Arne Jansena2de7332011-03-08 14:14:00 +01002141
Miao Xie17a9be22014-07-24 11:37:08 +08002142 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Zhao Leiba7cf982015-08-24 21:18:02 +08002143 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002144
2145 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
2146 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08002147 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002148
David Sterba25cc1222017-05-16 19:10:41 +02002149 len = sctx->fs_info->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002150 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2151 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2152 index = 0;
2153 for (;;) {
2154 u64 l = min_t(u64, len, mapped_size);
2155
Liu Bob0496682013-03-14 14:57:45 +00002156 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002157 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002158 len -= l;
2159 if (len == 0)
2160 break;
2161 index++;
2162 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002163 BUG_ON(!sblock->pagev[index]->page);
2164 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002165 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002166 mapped_size = PAGE_SIZE;
2167 p = mapped_buffer;
2168 }
2169
2170 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002171 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002172 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002173
Zhao Leiba7cf982015-08-24 21:18:02 +08002174 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002175}
2176
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002177static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002178{
2179 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002180 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002181 u8 calculated_csum[BTRFS_CSUM_SIZE];
2182 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2183 struct page *page;
2184 void *mapped_buffer;
2185 u64 mapped_size;
2186 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002187 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002188 int fail_gen = 0;
2189 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002190 u64 len;
2191 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002192
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002193 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002194 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002195 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002196 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002197 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002198
Qu Wenruo3cae2102013-07-16 11:19:18 +08002199 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002200 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002201
Qu Wenruo3cae2102013-07-16 11:19:18 +08002202 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002203 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002204
Miao Xie17a9be22014-07-24 11:37:08 +08002205 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002206 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002207
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002208 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2209 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2210 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2211 index = 0;
2212 for (;;) {
2213 u64 l = min_t(u64, len, mapped_size);
2214
Liu Bob0496682013-03-14 14:57:45 +00002215 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002216 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002217 len -= l;
2218 if (len == 0)
2219 break;
2220 index++;
2221 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002222 BUG_ON(!sblock->pagev[index]->page);
2223 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002224 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002225 mapped_size = PAGE_SIZE;
2226 p = mapped_buffer;
2227 }
2228
2229 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002230 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002231 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002232
Stefan Behrens442a4f62012-05-25 16:06:08 +02002233 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002234 /*
2235 * if we find an error in a super block, we just report it.
2236 * They will get written with the next transaction commit
2237 * anyway
2238 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002239 spin_lock(&sctx->stat_lock);
2240 ++sctx->stat.super_errors;
2241 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002242 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002243 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002244 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2245 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002246 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002247 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002248 }
2249
Stefan Behrens442a4f62012-05-25 16:06:08 +02002250 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002251}
2252
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002253static void scrub_block_get(struct scrub_block *sblock)
2254{
Elena Reshetova186debd2017-03-03 10:55:23 +02002255 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002256}
2257
2258static void scrub_block_put(struct scrub_block *sblock)
2259{
Elena Reshetova186debd2017-03-03 10:55:23 +02002260 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002261 int i;
2262
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002263 if (sblock->sparity)
2264 scrub_parity_put(sblock->sparity);
2265
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002266 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002267 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002268 kfree(sblock);
2269 }
2270}
2271
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002272static void scrub_page_get(struct scrub_page *spage)
2273{
Zhao Lei57019342015-01-20 15:11:45 +08002274 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002275}
2276
2277static void scrub_page_put(struct scrub_page *spage)
2278{
Zhao Lei57019342015-01-20 15:11:45 +08002279 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002280 if (spage->page)
2281 __free_page(spage->page);
2282 kfree(spage);
2283 }
2284}
2285
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002286static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002287{
2288 struct scrub_bio *sbio;
2289
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002290 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002291 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002292
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002293 sbio = sctx->bios[sctx->curr];
2294 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002295 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002296 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002297}
2298
Stefan Behrensff023aa2012-11-06 11:43:11 +01002299static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2300 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002301{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002302 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002303 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002304 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002305
2306again:
2307 /*
2308 * grab a fresh bio or wait for one to become available
2309 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002310 while (sctx->curr == -1) {
2311 spin_lock(&sctx->list_lock);
2312 sctx->curr = sctx->first_free;
2313 if (sctx->curr != -1) {
2314 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2315 sctx->bios[sctx->curr]->next_free = -1;
2316 sctx->bios[sctx->curr]->page_count = 0;
2317 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002318 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002319 spin_unlock(&sctx->list_lock);
2320 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002321 }
2322 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002323 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002324 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002325 struct bio *bio;
2326
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002327 sbio->physical = spage->physical;
2328 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002329 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002330 bio = sbio->bio;
2331 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02002332 bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002333 sbio->bio = bio;
2334 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002335
2336 bio->bi_private = sbio;
2337 bio->bi_end_io = scrub_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002338 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002339 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05002340 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002341 sbio->status = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002342 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2343 spage->physical ||
2344 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002345 spage->logical ||
2346 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002347 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002348 goto again;
2349 }
2350
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002351 sbio->pagev[sbio->page_count] = spage;
2352 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2353 if (ret != PAGE_SIZE) {
2354 if (sbio->page_count < 1) {
2355 bio_put(sbio->bio);
2356 sbio->bio = NULL;
2357 return -EIO;
2358 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002359 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002360 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002361 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002362
Stefan Behrensff023aa2012-11-06 11:43:11 +01002363 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002364 atomic_inc(&sblock->outstanding_pages);
2365 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002366 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002367 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002368
2369 return 0;
2370}
2371
Linus Torvalds22365972015-09-05 15:14:43 -07002372static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002373{
2374 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002375 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002376
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002377 if (bio->bi_status)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002378 sblock->no_io_error_seen = 0;
2379
Scott Talbert46732722016-05-09 09:14:28 -04002380 bio_put(bio);
2381
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002382 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2383}
2384
2385static void scrub_missing_raid56_worker(struct btrfs_work *work)
2386{
2387 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2388 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002389 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002390 u64 logical;
2391 struct btrfs_device *dev;
2392
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002393 logical = sblock->pagev[0]->logical;
2394 dev = sblock->pagev[0]->dev;
2395
Zhao Leiaffe4a52015-08-24 21:32:06 +08002396 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002397 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002398
2399 if (!sblock->no_io_error_seen) {
2400 spin_lock(&sctx->stat_lock);
2401 sctx->stat.read_errors++;
2402 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002403 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002404 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002405 logical, rcu_str_deref(dev->name));
2406 } else if (sblock->header_error || sblock->checksum_error) {
2407 spin_lock(&sctx->stat_lock);
2408 sctx->stat.uncorrectable_errors++;
2409 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002410 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002411 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002412 logical, rcu_str_deref(dev->name));
2413 } else {
2414 scrub_write_block_to_dev_replace(sblock);
2415 }
2416
2417 scrub_block_put(sblock);
2418
David Sterba2073c4c22017-03-31 17:12:51 +02002419 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002420 mutex_lock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002421 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002422 mutex_unlock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002423 }
2424
2425 scrub_pending_bio_dec(sctx);
2426}
2427
2428static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2429{
2430 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002431 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002432 u64 length = sblock->page_count * PAGE_SIZE;
2433 u64 logical = sblock->pagev[0]->logical;
Zhao Leif1fee652016-05-17 17:37:38 +08002434 struct btrfs_bio *bbio = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002435 struct bio *bio;
2436 struct btrfs_raid_bio *rbio;
2437 int ret;
2438 int i;
2439
Qu Wenruoae6529c2017-03-29 09:33:21 +08002440 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002441 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
David Sterba825ad4c2017-03-28 14:45:22 +02002442 &length, &bbio);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002443 if (ret || !bbio || !bbio->raid_map)
2444 goto bbio_out;
2445
2446 if (WARN_ON(!sctx->is_dev_replace ||
2447 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2448 /*
2449 * We shouldn't be scrubbing a missing device. Even for dev
2450 * replace, we should only get here for RAID 5/6. We either
2451 * managed to mount something with no mirrors remaining or
2452 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2453 */
2454 goto bbio_out;
2455 }
2456
David Sterbac5e4c3d2017-06-12 17:29:41 +02002457 bio = btrfs_io_bio_alloc(0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002458 bio->bi_iter.bi_sector = logical >> 9;
2459 bio->bi_private = sblock;
2460 bio->bi_end_io = scrub_missing_raid56_end_io;
2461
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002462 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002463 if (!rbio)
2464 goto rbio_out;
2465
2466 for (i = 0; i < sblock->page_count; i++) {
2467 struct scrub_page *spage = sblock->pagev[i];
2468
2469 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2470 }
2471
2472 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2473 scrub_missing_raid56_worker, NULL, NULL);
2474 scrub_block_get(sblock);
2475 scrub_pending_bio_inc(sctx);
2476 raid56_submit_missing_rbio(rbio);
2477 return;
2478
2479rbio_out:
2480 bio_put(bio);
2481bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002482 btrfs_bio_counter_dec(fs_info);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002483 btrfs_put_bbio(bbio);
2484 spin_lock(&sctx->stat_lock);
2485 sctx->stat.malloc_errors++;
2486 spin_unlock(&sctx->stat_lock);
2487}
2488
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002489static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002490 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002491 u64 gen, int mirror_num, u8 *csum, int force,
2492 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002493{
2494 struct scrub_block *sblock;
2495 int index;
2496
David Sterba58c4e172016-02-11 10:49:42 +01002497 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002498 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002499 spin_lock(&sctx->stat_lock);
2500 sctx->stat.malloc_errors++;
2501 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002502 return -ENOMEM;
2503 }
2504
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002505 /* one ref inside this function, plus one for each page added to
2506 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002507 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002508 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002509 sblock->no_io_error_seen = 1;
2510
2511 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002512 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002513 u64 l = min_t(u64, len, PAGE_SIZE);
2514
David Sterba58c4e172016-02-11 10:49:42 +01002515 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002516 if (!spage) {
2517leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002518 spin_lock(&sctx->stat_lock);
2519 sctx->stat.malloc_errors++;
2520 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002521 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002522 return -ENOMEM;
2523 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002524 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2525 scrub_page_get(spage);
2526 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002527 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002528 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002529 spage->flags = flags;
2530 spage->generation = gen;
2531 spage->logical = logical;
2532 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002533 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002534 spage->mirror_num = mirror_num;
2535 if (csum) {
2536 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002537 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002538 } else {
2539 spage->have_csum = 0;
2540 }
2541 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002542 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002543 if (!spage->page)
2544 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002545 len -= l;
2546 logical += l;
2547 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002548 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002549 }
2550
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002551 WARN_ON(sblock->page_count == 0);
Anand Jaine6e674b2017-12-04 12:54:54 +08002552 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002553 /*
2554 * This case should only be hit for RAID 5/6 device replace. See
2555 * the comment in scrub_missing_raid56_pages() for details.
2556 */
2557 scrub_missing_raid56_pages(sblock);
2558 } else {
2559 for (index = 0; index < sblock->page_count; index++) {
2560 struct scrub_page *spage = sblock->pagev[index];
2561 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002562
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002563 ret = scrub_add_page_to_rd_bio(sctx, spage);
2564 if (ret) {
2565 scrub_block_put(sblock);
2566 return ret;
2567 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002568 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002569
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002570 if (force)
2571 scrub_submit(sctx);
2572 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002573
2574 /* last one frees, either here or in bio completion for last page */
2575 scrub_block_put(sblock);
2576 return 0;
2577}
2578
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002579static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002580{
2581 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002582 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002583
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002584 sbio->status = bio->bi_status;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002585 sbio->bio = bio;
2586
Qu Wenruo0339ef22014-02-28 10:46:17 +08002587 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002588}
2589
2590static void scrub_bio_end_io_worker(struct btrfs_work *work)
2591{
2592 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002593 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002594 int i;
2595
Stefan Behrensff023aa2012-11-06 11:43:11 +01002596 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002597 if (sbio->status) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002598 for (i = 0; i < sbio->page_count; i++) {
2599 struct scrub_page *spage = sbio->pagev[i];
2600
2601 spage->io_error = 1;
2602 spage->sblock->no_io_error_seen = 0;
2603 }
2604 }
2605
2606 /* now complete the scrub_block items that have all pages completed */
2607 for (i = 0; i < sbio->page_count; i++) {
2608 struct scrub_page *spage = sbio->pagev[i];
2609 struct scrub_block *sblock = spage->sblock;
2610
2611 if (atomic_dec_and_test(&sblock->outstanding_pages))
2612 scrub_block_complete(sblock);
2613 scrub_block_put(sblock);
2614 }
2615
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002616 bio_put(sbio->bio);
2617 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002618 spin_lock(&sctx->list_lock);
2619 sbio->next_free = sctx->first_free;
2620 sctx->first_free = sbio->index;
2621 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002622
David Sterba2073c4c22017-03-31 17:12:51 +02002623 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002624 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002625 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002626 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002627 }
2628
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002629 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002630}
2631
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002632static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2633 unsigned long *bitmap,
2634 u64 start, u64 len)
2635{
Liu Bo972d7212017-04-03 13:45:33 -07002636 u64 offset;
David Sterba7736b0a2017-03-31 18:02:48 +02002637 u64 nsectors64;
2638 u32 nsectors;
Jeff Mahoneyda170662016-06-15 09:22:56 -04002639 int sectorsize = sparity->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002640
2641 if (len >= sparity->stripe_len) {
2642 bitmap_set(bitmap, 0, sparity->nsectors);
2643 return;
2644 }
2645
2646 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002647 start = div64_u64_rem(start, sparity->stripe_len, &offset);
2648 offset = div_u64(offset, sectorsize);
David Sterba7736b0a2017-03-31 18:02:48 +02002649 nsectors64 = div_u64(len, sectorsize);
2650
2651 ASSERT(nsectors64 < UINT_MAX);
2652 nsectors = (u32)nsectors64;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002653
2654 if (offset + nsectors <= sparity->nsectors) {
2655 bitmap_set(bitmap, offset, nsectors);
2656 return;
2657 }
2658
2659 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2660 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2661}
2662
2663static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2664 u64 start, u64 len)
2665{
2666 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2667}
2668
2669static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2670 u64 start, u64 len)
2671{
2672 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2673}
2674
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002675static void scrub_block_complete(struct scrub_block *sblock)
2676{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002677 int corrupted = 0;
2678
Stefan Behrensff023aa2012-11-06 11:43:11 +01002679 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002680 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002681 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002682 } else {
2683 /*
2684 * if has checksum error, write via repair mechanism in
2685 * dev replace case, otherwise write here in dev replace
2686 * case.
2687 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002688 corrupted = scrub_checksum(sblock);
2689 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002690 scrub_write_block_to_dev_replace(sblock);
2691 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002692
2693 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2694 u64 start = sblock->pagev[0]->logical;
2695 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2696 PAGE_SIZE;
2697
2698 scrub_parity_mark_sectors_error(sblock->sparity,
2699 start, end - start);
2700 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002701}
2702
Zhao Lei3b5753e2015-08-24 22:03:02 +08002703static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002704{
2705 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002706 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002707 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002708
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002709 while (!list_empty(&sctx->csum_list)) {
2710 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002711 struct btrfs_ordered_sum, list);
2712 if (sum->bytenr > logical)
2713 return 0;
2714 if (sum->bytenr + sum->len > logical)
2715 break;
2716
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002717 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002718 list_del(&sum->list);
2719 kfree(sum);
2720 sum = NULL;
2721 }
2722 if (!sum)
2723 return 0;
2724
David Sterba1d1bf922017-03-31 18:02:48 +02002725 index = div_u64(logical - sum->bytenr, sctx->fs_info->sectorsize);
2726 ASSERT(index < UINT_MAX);
2727
David Sterba25cc1222017-05-16 19:10:41 +02002728 num_sectors = sum->len / sctx->fs_info->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002729 memcpy(csum, sum->sums + index, sctx->csum_size);
2730 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002731 list_del(&sum->list);
2732 kfree(sum);
2733 }
Miao Xief51a4a12013-06-19 10:36:09 +08002734 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002735}
2736
2737/* scrub extent tries to collect up to 64 kB for each bio */
Liu Bo6ca17652018-03-07 12:08:09 -07002738static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map,
2739 u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002740 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002741 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002742{
2743 int ret;
2744 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002745 u32 blocksize;
2746
2747 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002748 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2749 blocksize = map->stripe_len;
2750 else
2751 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002752 spin_lock(&sctx->stat_lock);
2753 sctx->stat.data_extents_scrubbed++;
2754 sctx->stat.data_bytes_scrubbed += len;
2755 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002756 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002757 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2758 blocksize = map->stripe_len;
2759 else
2760 blocksize = sctx->fs_info->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002761 spin_lock(&sctx->stat_lock);
2762 sctx->stat.tree_extents_scrubbed++;
2763 sctx->stat.tree_bytes_scrubbed += len;
2764 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002765 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002766 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002767 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002768 }
Arne Jansena2de7332011-03-08 14:14:00 +01002769
2770 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002771 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002772 int have_csum = 0;
2773
2774 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2775 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002776 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002777 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002778 ++sctx->stat.no_csum;
Arne Jansena2de7332011-03-08 14:14:00 +01002779 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002780 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002781 mirror_num, have_csum ? csum : NULL, 0,
2782 physical_for_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002783 if (ret)
2784 return ret;
2785 len -= l;
2786 logical += l;
2787 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002788 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002789 }
2790 return 0;
2791}
2792
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002793static int scrub_pages_for_parity(struct scrub_parity *sparity,
2794 u64 logical, u64 len,
2795 u64 physical, struct btrfs_device *dev,
2796 u64 flags, u64 gen, int mirror_num, u8 *csum)
2797{
2798 struct scrub_ctx *sctx = sparity->sctx;
2799 struct scrub_block *sblock;
2800 int index;
2801
David Sterba58c4e172016-02-11 10:49:42 +01002802 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002803 if (!sblock) {
2804 spin_lock(&sctx->stat_lock);
2805 sctx->stat.malloc_errors++;
2806 spin_unlock(&sctx->stat_lock);
2807 return -ENOMEM;
2808 }
2809
2810 /* one ref inside this function, plus one for each page added to
2811 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002812 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002813 sblock->sctx = sctx;
2814 sblock->no_io_error_seen = 1;
2815 sblock->sparity = sparity;
2816 scrub_parity_get(sparity);
2817
2818 for (index = 0; len > 0; index++) {
2819 struct scrub_page *spage;
2820 u64 l = min_t(u64, len, PAGE_SIZE);
2821
David Sterba58c4e172016-02-11 10:49:42 +01002822 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002823 if (!spage) {
2824leave_nomem:
2825 spin_lock(&sctx->stat_lock);
2826 sctx->stat.malloc_errors++;
2827 spin_unlock(&sctx->stat_lock);
2828 scrub_block_put(sblock);
2829 return -ENOMEM;
2830 }
2831 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2832 /* For scrub block */
2833 scrub_page_get(spage);
2834 sblock->pagev[index] = spage;
2835 /* For scrub parity */
2836 scrub_page_get(spage);
2837 list_add_tail(&spage->list, &sparity->spages);
2838 spage->sblock = sblock;
2839 spage->dev = dev;
2840 spage->flags = flags;
2841 spage->generation = gen;
2842 spage->logical = logical;
2843 spage->physical = physical;
2844 spage->mirror_num = mirror_num;
2845 if (csum) {
2846 spage->have_csum = 1;
2847 memcpy(spage->csum, csum, sctx->csum_size);
2848 } else {
2849 spage->have_csum = 0;
2850 }
2851 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002852 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002853 if (!spage->page)
2854 goto leave_nomem;
2855 len -= l;
2856 logical += l;
2857 physical += l;
2858 }
2859
2860 WARN_ON(sblock->page_count == 0);
2861 for (index = 0; index < sblock->page_count; index++) {
2862 struct scrub_page *spage = sblock->pagev[index];
2863 int ret;
2864
2865 ret = scrub_add_page_to_rd_bio(sctx, spage);
2866 if (ret) {
2867 scrub_block_put(sblock);
2868 return ret;
2869 }
2870 }
2871
2872 /* last one frees, either here or in bio completion for last page */
2873 scrub_block_put(sblock);
2874 return 0;
2875}
2876
2877static int scrub_extent_for_parity(struct scrub_parity *sparity,
2878 u64 logical, u64 len,
2879 u64 physical, struct btrfs_device *dev,
2880 u64 flags, u64 gen, int mirror_num)
2881{
2882 struct scrub_ctx *sctx = sparity->sctx;
2883 int ret;
2884 u8 csum[BTRFS_CSUM_SIZE];
2885 u32 blocksize;
2886
Anand Jaine6e674b2017-12-04 12:54:54 +08002887 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval4a770892015-06-19 11:52:52 -07002888 scrub_parity_mark_sectors_error(sparity, logical, len);
2889 return 0;
2890 }
2891
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002892 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002893 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002894 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002895 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002896 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002897 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002898 WARN_ON(1);
2899 }
2900
2901 while (len) {
2902 u64 l = min_t(u64, len, blocksize);
2903 int have_csum = 0;
2904
2905 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2906 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002907 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002908 if (have_csum == 0)
2909 goto skip;
2910 }
2911 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2912 flags, gen, mirror_num,
2913 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002914 if (ret)
2915 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002916skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002917 len -= l;
2918 logical += l;
2919 physical += l;
2920 }
2921 return 0;
2922}
2923
Wang Shilong3b080b22014-04-01 18:01:43 +08002924/*
2925 * Given a physical address, this will calculate it's
2926 * logical offset. if this is a parity stripe, it will return
2927 * the most left data stripe's logical offset.
2928 *
2929 * return 0 if it is a data stripe, 1 means parity stripe.
2930 */
2931static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002932 struct map_lookup *map, u64 *offset,
2933 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002934{
2935 int i;
2936 int j = 0;
2937 u64 stripe_nr;
2938 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002939 u32 stripe_index;
2940 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002941
2942 last_offset = (physical - map->stripes[num].physical) *
2943 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002944 if (stripe_start)
2945 *stripe_start = last_offset;
2946
Wang Shilong3b080b22014-04-01 18:01:43 +08002947 *offset = last_offset;
2948 for (i = 0; i < nr_data_stripes(map); i++) {
2949 *offset = last_offset + i * map->stripe_len;
2950
Liu Bo42c61ab2017-04-03 13:45:24 -07002951 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01002952 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002953
2954 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002955 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002956 /* calculate which stripe this data locates */
2957 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002958 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002959 if (stripe_index == num)
2960 return 0;
2961 if (stripe_index < num)
2962 j++;
2963 }
2964 *offset = last_offset + j * map->stripe_len;
2965 return 1;
2966}
2967
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002968static void scrub_free_parity(struct scrub_parity *sparity)
2969{
2970 struct scrub_ctx *sctx = sparity->sctx;
2971 struct scrub_page *curr, *next;
2972 int nbits;
2973
2974 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2975 if (nbits) {
2976 spin_lock(&sctx->stat_lock);
2977 sctx->stat.read_errors += nbits;
2978 sctx->stat.uncorrectable_errors += nbits;
2979 spin_unlock(&sctx->stat_lock);
2980 }
2981
2982 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2983 list_del_init(&curr->list);
2984 scrub_page_put(curr);
2985 }
2986
2987 kfree(sparity);
2988}
2989
Zhao Lei20b2e302015-06-04 20:09:15 +08002990static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2991{
2992 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2993 work);
2994 struct scrub_ctx *sctx = sparity->sctx;
2995
2996 scrub_free_parity(sparity);
2997 scrub_pending_bio_dec(sctx);
2998}
2999
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003000static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003001{
3002 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003003 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003004
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003005 if (bio->bi_status)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003006 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
3007 sparity->nsectors);
3008
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003009 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08003010
3011 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
3012 scrub_parity_bio_endio_worker, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003013 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003014}
3015
3016static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
3017{
3018 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003019 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003020 struct bio *bio;
3021 struct btrfs_raid_bio *rbio;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003022 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003023 u64 length;
3024 int ret;
3025
3026 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
3027 sparity->nsectors))
3028 goto out;
3029
Zhao Leia0dd59d2015-07-21 15:42:26 +08003030 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08003031
3032 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003033 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
David Sterba825ad4c2017-03-28 14:45:22 +02003034 &length, &bbio);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003035 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003036 goto bbio_out;
3037
David Sterbac5e4c3d2017-06-12 17:29:41 +02003038 bio = btrfs_io_bio_alloc(0);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003039 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
3040 bio->bi_private = sparity;
3041 bio->bi_end_io = scrub_parity_bio_endio;
3042
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003043 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003044 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003045 sparity->dbitmap,
3046 sparity->nsectors);
3047 if (!rbio)
3048 goto rbio_out;
3049
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003050 scrub_pending_bio_inc(sctx);
3051 raid56_parity_submit_scrub_rbio(rbio);
3052 return;
3053
3054rbio_out:
3055 bio_put(bio);
3056bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08003057 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08003058 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003059 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
3060 sparity->nsectors);
3061 spin_lock(&sctx->stat_lock);
3062 sctx->stat.malloc_errors++;
3063 spin_unlock(&sctx->stat_lock);
3064out:
3065 scrub_free_parity(sparity);
3066}
3067
3068static inline int scrub_calc_parity_bitmap_len(int nsectors)
3069{
Zhao Leibfca9a62014-12-08 19:55:57 +08003070 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003071}
3072
3073static void scrub_parity_get(struct scrub_parity *sparity)
3074{
Elena Reshetova78a76452017-03-03 10:55:24 +02003075 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003076}
3077
3078static void scrub_parity_put(struct scrub_parity *sparity)
3079{
Elena Reshetova78a76452017-03-03 10:55:24 +02003080 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003081 return;
3082
3083 scrub_parity_check_and_repair(sparity);
3084}
3085
3086static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
3087 struct map_lookup *map,
3088 struct btrfs_device *sdev,
3089 struct btrfs_path *path,
3090 u64 logic_start,
3091 u64 logic_end)
3092{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003093 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003094 struct btrfs_root *root = fs_info->extent_root;
3095 struct btrfs_root *csum_root = fs_info->csum_root;
3096 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07003097 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003098 u64 flags;
3099 int ret;
3100 int slot;
3101 struct extent_buffer *l;
3102 struct btrfs_key key;
3103 u64 generation;
3104 u64 extent_logical;
3105 u64 extent_physical;
3106 u64 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07003107 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003108 struct btrfs_device *extent_dev;
3109 struct scrub_parity *sparity;
3110 int nsectors;
3111 int bitmap_len;
3112 int extent_mirror_num;
3113 int stop_loop = 0;
3114
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003115 nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003116 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
3117 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
3118 GFP_NOFS);
3119 if (!sparity) {
3120 spin_lock(&sctx->stat_lock);
3121 sctx->stat.malloc_errors++;
3122 spin_unlock(&sctx->stat_lock);
3123 return -ENOMEM;
3124 }
3125
3126 sparity->stripe_len = map->stripe_len;
3127 sparity->nsectors = nsectors;
3128 sparity->sctx = sctx;
3129 sparity->scrub_dev = sdev;
3130 sparity->logic_start = logic_start;
3131 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02003132 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003133 INIT_LIST_HEAD(&sparity->spages);
3134 sparity->dbitmap = sparity->bitmap;
3135 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
3136
3137 ret = 0;
3138 while (logic_start < logic_end) {
3139 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3140 key.type = BTRFS_METADATA_ITEM_KEY;
3141 else
3142 key.type = BTRFS_EXTENT_ITEM_KEY;
3143 key.objectid = logic_start;
3144 key.offset = (u64)-1;
3145
3146 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3147 if (ret < 0)
3148 goto out;
3149
3150 if (ret > 0) {
3151 ret = btrfs_previous_extent_item(root, path, 0);
3152 if (ret < 0)
3153 goto out;
3154 if (ret > 0) {
3155 btrfs_release_path(path);
3156 ret = btrfs_search_slot(NULL, root, &key,
3157 path, 0, 0);
3158 if (ret < 0)
3159 goto out;
3160 }
3161 }
3162
3163 stop_loop = 0;
3164 while (1) {
3165 u64 bytes;
3166
3167 l = path->nodes[0];
3168 slot = path->slots[0];
3169 if (slot >= btrfs_header_nritems(l)) {
3170 ret = btrfs_next_leaf(root, path);
3171 if (ret == 0)
3172 continue;
3173 if (ret < 0)
3174 goto out;
3175
3176 stop_loop = 1;
3177 break;
3178 }
3179 btrfs_item_key_to_cpu(l, &key, slot);
3180
Zhao Leid7cad232015-07-22 13:14:48 +08003181 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3182 key.type != BTRFS_METADATA_ITEM_KEY)
3183 goto next;
3184
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003185 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003186 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003187 else
3188 bytes = key.offset;
3189
3190 if (key.objectid + bytes <= logic_start)
3191 goto next;
3192
Zhao Leia0dd59d2015-07-21 15:42:26 +08003193 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003194 stop_loop = 1;
3195 break;
3196 }
3197
3198 while (key.objectid >= logic_start + map->stripe_len)
3199 logic_start += map->stripe_len;
3200
3201 extent = btrfs_item_ptr(l, slot,
3202 struct btrfs_extent_item);
3203 flags = btrfs_extent_flags(l, extent);
3204 generation = btrfs_extent_generation(l, extent);
3205
Zhao Leia323e812015-07-23 12:29:49 +08003206 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3207 (key.objectid < logic_start ||
3208 key.objectid + bytes >
3209 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003210 btrfs_err(fs_info,
3211 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08003212 key.objectid, logic_start);
Zhao Lei9799d2c2015-08-25 21:31:40 +08003213 spin_lock(&sctx->stat_lock);
3214 sctx->stat.uncorrectable_errors++;
3215 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003216 goto next;
3217 }
3218again:
3219 extent_logical = key.objectid;
3220 extent_len = bytes;
3221
3222 if (extent_logical < logic_start) {
3223 extent_len -= logic_start - extent_logical;
3224 extent_logical = logic_start;
3225 }
3226
3227 if (extent_logical + extent_len >
3228 logic_start + map->stripe_len)
3229 extent_len = logic_start + map->stripe_len -
3230 extent_logical;
3231
3232 scrub_parity_mark_sectors_data(sparity, extent_logical,
3233 extent_len);
3234
Omar Sandoval4a770892015-06-19 11:52:52 -07003235 mapped_length = extent_len;
Zhao Leif1fee652016-05-17 17:37:38 +08003236 bbio = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003237 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
3238 extent_logical, &mapped_length, &bbio,
3239 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07003240 if (!ret) {
3241 if (!bbio || mapped_length < extent_len)
3242 ret = -EIO;
3243 }
3244 if (ret) {
3245 btrfs_put_bbio(bbio);
3246 goto out;
3247 }
3248 extent_physical = bbio->stripes[0].physical;
3249 extent_mirror_num = bbio->mirror_num;
3250 extent_dev = bbio->stripes[0].dev;
3251 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003252
3253 ret = btrfs_lookup_csums_range(csum_root,
3254 extent_logical,
3255 extent_logical + extent_len - 1,
3256 &sctx->csum_list, 1);
3257 if (ret)
3258 goto out;
3259
3260 ret = scrub_extent_for_parity(sparity, extent_logical,
3261 extent_len,
3262 extent_physical,
3263 extent_dev, flags,
3264 generation,
3265 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003266
3267 scrub_free_csums(sctx);
3268
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003269 if (ret)
3270 goto out;
3271
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003272 if (extent_logical + extent_len <
3273 key.objectid + bytes) {
3274 logic_start += map->stripe_len;
3275
3276 if (logic_start >= logic_end) {
3277 stop_loop = 1;
3278 break;
3279 }
3280
3281 if (logic_start < key.objectid + bytes) {
3282 cond_resched();
3283 goto again;
3284 }
3285 }
3286next:
3287 path->slots[0]++;
3288 }
3289
3290 btrfs_release_path(path);
3291
3292 if (stop_loop)
3293 break;
3294
3295 logic_start += map->stripe_len;
3296 }
3297out:
3298 if (ret < 0)
3299 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003300 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003301 scrub_parity_put(sparity);
3302 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003303 mutex_lock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003304 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003305 mutex_unlock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003306
3307 btrfs_release_path(path);
3308 return ret < 0 ? ret : 0;
3309}
3310
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003311static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003312 struct map_lookup *map,
3313 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003314 int num, u64 base, u64 length,
3315 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003316{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003317 struct btrfs_path *path, *ppath;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003318 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003319 struct btrfs_root *root = fs_info->extent_root;
3320 struct btrfs_root *csum_root = fs_info->csum_root;
3321 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003322 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003323 u64 flags;
3324 int ret;
3325 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003326 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003327 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003328 u64 physical;
3329 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003330 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003331 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003332 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003333 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003334 struct reada_control *reada1;
3335 struct reada_control *reada2;
David Sterbae6c11f92016-03-24 18:00:53 +01003336 struct btrfs_key key;
Arne Jansen7a262852011-06-10 12:39:23 +02003337 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003338 u64 increment = map->stripe_len;
3339 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003340 u64 extent_logical;
3341 u64 extent_physical;
3342 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003343 u64 stripe_logical;
3344 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003345 struct btrfs_device *extent_dev;
3346 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003347 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003348
Wang Shilong3b080b22014-04-01 18:01:43 +08003349 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003350 offset = 0;
Liu Bo42c61ab2017-04-03 13:45:24 -07003351 nstripes = div64_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003352 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3353 offset = map->stripe_len * num;
3354 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003355 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003356 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3357 int factor = map->num_stripes / map->sub_stripes;
3358 offset = map->stripe_len * (num / map->sub_stripes);
3359 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003360 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003361 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3362 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003363 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003364 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3365 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003366 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003367 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003368 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003369 increment = map->stripe_len * nr_data_stripes(map);
3370 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003371 } else {
3372 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003373 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003374 }
3375
3376 path = btrfs_alloc_path();
3377 if (!path)
3378 return -ENOMEM;
3379
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003380 ppath = btrfs_alloc_path();
3381 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003382 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003383 return -ENOMEM;
3384 }
3385
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003386 /*
3387 * work on commit root. The related disk blocks are static as
3388 * long as COW is applied. This means, it is save to rewrite
3389 * them to repair disk errors without any race conditions
3390 */
Arne Jansena2de7332011-03-08 14:14:00 +01003391 path->search_commit_root = 1;
3392 path->skip_locking = 1;
3393
Gui Hecheng063c54d2015-01-09 09:39:40 +08003394 ppath->search_commit_root = 1;
3395 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003396 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003397 * trigger the readahead for extent tree csum tree and wait for
3398 * completion. During readahead, the scrub is officially paused
3399 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003400 */
3401 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003402 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003403 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003404 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003405 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003406 logic_end += base;
3407 } else {
3408 logic_end = logical + increment * nstripes;
3409 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003410 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003411 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003412 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003413
Arne Jansen7a262852011-06-10 12:39:23 +02003414 /* FIXME it might be better to start readahead at commit root */
David Sterbae6c11f92016-03-24 18:00:53 +01003415 key.objectid = logical;
3416 key.type = BTRFS_EXTENT_ITEM_KEY;
3417 key.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003418 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003419 key_end.type = BTRFS_METADATA_ITEM_KEY;
3420 key_end.offset = (u64)-1;
David Sterbae6c11f92016-03-24 18:00:53 +01003421 reada1 = btrfs_reada_add(root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003422
David Sterbae6c11f92016-03-24 18:00:53 +01003423 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3424 key.type = BTRFS_EXTENT_CSUM_KEY;
3425 key.offset = logical;
Arne Jansen7a262852011-06-10 12:39:23 +02003426 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3427 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003428 key_end.offset = logic_end;
David Sterbae6c11f92016-03-24 18:00:53 +01003429 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003430
Arne Jansen7a262852011-06-10 12:39:23 +02003431 if (!IS_ERR(reada1))
3432 btrfs_reada_wait(reada1);
3433 if (!IS_ERR(reada2))
3434 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003435
Arne Jansena2de7332011-03-08 14:14:00 +01003436
3437 /*
3438 * collect all data csums for the stripe to avoid seeking during
3439 * the scrub. This might currently (crc32) end up to be about 1MB
3440 */
Arne Jansene7786c32011-05-28 20:58:38 +00003441 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003442
Arne Jansena2de7332011-03-08 14:14:00 +01003443 /*
3444 * now find all extents for each stripe and scrub them
3445 */
Arne Jansena2de7332011-03-08 14:14:00 +01003446 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003447 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003448 /*
3449 * canceled?
3450 */
3451 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003452 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003453 ret = -ECANCELED;
3454 goto out;
3455 }
3456 /*
3457 * check to see if we have to pause
3458 */
3459 if (atomic_read(&fs_info->scrub_pause_req)) {
3460 /* push queued extents */
David Sterba2073c4c22017-03-31 17:12:51 +02003461 sctx->flush_all_writes = true;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003462 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003463 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003464 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003465 mutex_unlock(&sctx->wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003466 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003467 atomic_read(&sctx->bios_in_flight) == 0);
David Sterba2073c4c22017-03-31 17:12:51 +02003468 sctx->flush_all_writes = false;
Wang Shilong3cb09292013-12-04 21:15:19 +08003469 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003470 }
3471
Zhao Leif2f66a22015-07-21 12:22:29 +08003472 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3473 ret = get_raid56_logic_offset(physical, num, map,
3474 &logical,
3475 &stripe_logical);
3476 logical += base;
3477 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003478 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003479 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003480 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003481 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3482 ppath, stripe_logical,
3483 stripe_end);
3484 if (ret)
3485 goto out;
3486 goto skip;
3487 }
3488 }
3489
Wang Shilong7c76edb2014-01-12 21:38:32 +08003490 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3491 key.type = BTRFS_METADATA_ITEM_KEY;
3492 else
3493 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003494 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003495 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003496
3497 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3498 if (ret < 0)
3499 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003500
Arne Jansen8c510322011-06-03 10:09:26 +02003501 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003502 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003503 if (ret < 0)
3504 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003505 if (ret > 0) {
3506 /* there's no smaller item, so stick with the
3507 * larger one */
3508 btrfs_release_path(path);
3509 ret = btrfs_search_slot(NULL, root, &key,
3510 path, 0, 0);
3511 if (ret < 0)
3512 goto out;
3513 }
Arne Jansena2de7332011-03-08 14:14:00 +01003514 }
3515
Liu Bo625f1c8d2013-04-27 02:56:57 +00003516 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003517 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003518 u64 bytes;
3519
Arne Jansena2de7332011-03-08 14:14:00 +01003520 l = path->nodes[0];
3521 slot = path->slots[0];
3522 if (slot >= btrfs_header_nritems(l)) {
3523 ret = btrfs_next_leaf(root, path);
3524 if (ret == 0)
3525 continue;
3526 if (ret < 0)
3527 goto out;
3528
Liu Bo625f1c8d2013-04-27 02:56:57 +00003529 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003530 break;
3531 }
3532 btrfs_item_key_to_cpu(l, &key, slot);
3533
Zhao Leid7cad232015-07-22 13:14:48 +08003534 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3535 key.type != BTRFS_METADATA_ITEM_KEY)
3536 goto next;
3537
Josef Bacik3173a182013-03-07 14:22:04 -05003538 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003539 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003540 else
3541 bytes = key.offset;
3542
3543 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003544 goto next;
3545
Liu Bo625f1c8d2013-04-27 02:56:57 +00003546 if (key.objectid >= logical + map->stripe_len) {
3547 /* out of this device extent */
3548 if (key.objectid >= logic_end)
3549 stop_loop = 1;
3550 break;
3551 }
Arne Jansena2de7332011-03-08 14:14:00 +01003552
3553 extent = btrfs_item_ptr(l, slot,
3554 struct btrfs_extent_item);
3555 flags = btrfs_extent_flags(l, extent);
3556 generation = btrfs_extent_generation(l, extent);
3557
Zhao Leia323e812015-07-23 12:29:49 +08003558 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3559 (key.objectid < logical ||
3560 key.objectid + bytes >
3561 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003562 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003563 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003564 key.objectid, logical);
Zhao Lei9799d2c2015-08-25 21:31:40 +08003565 spin_lock(&sctx->stat_lock);
3566 sctx->stat.uncorrectable_errors++;
3567 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003568 goto next;
3569 }
3570
Liu Bo625f1c8d2013-04-27 02:56:57 +00003571again:
3572 extent_logical = key.objectid;
3573 extent_len = bytes;
3574
Arne Jansena2de7332011-03-08 14:14:00 +01003575 /*
3576 * trim extent to this stripe
3577 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003578 if (extent_logical < logical) {
3579 extent_len -= logical - extent_logical;
3580 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003581 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003582 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003583 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003584 extent_len = logical + map->stripe_len -
3585 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003586 }
3587
Liu Bo625f1c8d2013-04-27 02:56:57 +00003588 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003589 extent_dev = scrub_dev;
3590 extent_mirror_num = mirror_num;
3591 if (is_dev_replace)
3592 scrub_remap_extent(fs_info, extent_logical,
3593 extent_len, &extent_physical,
3594 &extent_dev,
3595 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003596
Zhao Leife8cf652015-07-22 13:14:47 +08003597 ret = btrfs_lookup_csums_range(csum_root,
3598 extent_logical,
3599 extent_logical +
3600 extent_len - 1,
3601 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003602 if (ret)
3603 goto out;
3604
Liu Bo6ca17652018-03-07 12:08:09 -07003605 ret = scrub_extent(sctx, map, extent_logical, extent_len,
Liu Bo625f1c8d2013-04-27 02:56:57 +00003606 extent_physical, extent_dev, flags,
3607 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003608 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003609
3610 scrub_free_csums(sctx);
3611
Liu Bo625f1c8d2013-04-27 02:56:57 +00003612 if (ret)
3613 goto out;
3614
3615 if (extent_logical + extent_len <
3616 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003617 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003618 /*
3619 * loop until we find next data stripe
3620 * or we have finished all stripes.
3621 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003622loop:
3623 physical += map->stripe_len;
3624 ret = get_raid56_logic_offset(physical,
3625 num, map, &logical,
3626 &stripe_logical);
3627 logical += base;
3628
3629 if (ret && physical < physical_end) {
3630 stripe_logical += base;
3631 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003632 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003633 ret = scrub_raid56_parity(sctx,
3634 map, scrub_dev, ppath,
3635 stripe_logical,
3636 stripe_end);
3637 if (ret)
3638 goto out;
3639 goto loop;
3640 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003641 } else {
3642 physical += map->stripe_len;
3643 logical += increment;
3644 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003645 if (logical < key.objectid + bytes) {
3646 cond_resched();
3647 goto again;
3648 }
3649
Wang Shilong3b080b22014-04-01 18:01:43 +08003650 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003651 stop_loop = 1;
3652 break;
3653 }
3654 }
Arne Jansena2de7332011-03-08 14:14:00 +01003655next:
3656 path->slots[0]++;
3657 }
Chris Mason71267332011-05-23 06:30:52 -04003658 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003659skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003660 logical += increment;
3661 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003662 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003663 if (stop_loop)
3664 sctx->stat.last_physical = map->stripes[num].physical +
3665 length;
3666 else
3667 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003668 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003669 if (stop_loop)
3670 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003671 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003672out:
Arne Jansena2de7332011-03-08 14:14:00 +01003673 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003674 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003675 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003676 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003677 mutex_unlock(&sctx->wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003678
Arne Jansene7786c32011-05-28 20:58:38 +00003679 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003680 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003681 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003682 return ret < 0 ? ret : 0;
3683}
3684
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003685static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003686 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003687 u64 chunk_offset, u64 length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003688 u64 dev_offset,
3689 struct btrfs_block_group_cache *cache,
3690 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003691{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003692 struct btrfs_fs_info *fs_info = sctx->fs_info;
3693 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003694 struct map_lookup *map;
3695 struct extent_map *em;
3696 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003697 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003698
3699 read_lock(&map_tree->map_tree.lock);
3700 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3701 read_unlock(&map_tree->map_tree.lock);
3702
Filipe Manana020d5b72015-11-19 10:57:20 +00003703 if (!em) {
3704 /*
3705 * Might have been an unused block group deleted by the cleaner
3706 * kthread or relocation.
3707 */
3708 spin_lock(&cache->lock);
3709 if (!cache->removed)
3710 ret = -EINVAL;
3711 spin_unlock(&cache->lock);
3712
3713 return ret;
3714 }
Arne Jansena2de7332011-03-08 14:14:00 +01003715
Jeff Mahoney95617d62015-06-03 10:55:48 -04003716 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003717 if (em->start != chunk_offset)
3718 goto out;
3719
3720 if (em->len < length)
3721 goto out;
3722
3723 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003724 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003725 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003726 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003727 chunk_offset, length,
3728 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003729 if (ret)
3730 goto out;
3731 }
3732 }
3733out:
3734 free_extent_map(em);
3735
3736 return ret;
3737}
3738
3739static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003740int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003741 struct btrfs_device *scrub_dev, u64 start, u64 end,
3742 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003743{
3744 struct btrfs_dev_extent *dev_extent = NULL;
3745 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003746 struct btrfs_fs_info *fs_info = sctx->fs_info;
3747 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003748 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003749 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003750 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003751 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003752 int slot;
3753 struct extent_buffer *l;
3754 struct btrfs_key key;
3755 struct btrfs_key found_key;
3756 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003757 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003758
3759 path = btrfs_alloc_path();
3760 if (!path)
3761 return -ENOMEM;
3762
David Sterbae4058b52015-11-27 16:31:35 +01003763 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003764 path->search_commit_root = 1;
3765 path->skip_locking = 1;
3766
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003767 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003768 key.offset = 0ull;
3769 key.type = BTRFS_DEV_EXTENT_KEY;
3770
Arne Jansena2de7332011-03-08 14:14:00 +01003771 while (1) {
3772 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3773 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003774 break;
3775 if (ret > 0) {
3776 if (path->slots[0] >=
3777 btrfs_header_nritems(path->nodes[0])) {
3778 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003779 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003780 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003781 if (ret > 0) {
3782 ret = 0;
3783 break;
3784 }
3785 } else {
3786 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003787 }
3788 }
Arne Jansena2de7332011-03-08 14:14:00 +01003789
3790 l = path->nodes[0];
3791 slot = path->slots[0];
3792
3793 btrfs_item_key_to_cpu(l, &found_key, slot);
3794
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003795 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003796 break;
3797
David Sterba962a2982014-06-04 18:41:45 +02003798 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003799 break;
3800
3801 if (found_key.offset >= end)
3802 break;
3803
3804 if (found_key.offset < key.offset)
3805 break;
3806
3807 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3808 length = btrfs_dev_extent_length(l, dev_extent);
3809
Qu Wenruoced96ed2014-06-19 10:42:51 +08003810 if (found_key.offset + length <= start)
3811 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003812
Arne Jansena2de7332011-03-08 14:14:00 +01003813 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3814
3815 /*
3816 * get a reference on the corresponding block group to prevent
3817 * the chunk from going away while we scrub it
3818 */
3819 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003820
3821 /* some chunks are removed but not committed to disk yet,
3822 * continue scrubbing */
3823 if (!cache)
3824 goto skip;
3825
Zhaolei55e3a602015-08-05 16:43:30 +08003826 /*
3827 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3828 * to avoid deadlock caused by:
3829 * btrfs_inc_block_group_ro()
3830 * -> btrfs_wait_for_commit()
3831 * -> btrfs_commit_transaction()
3832 * -> btrfs_scrub_pause()
3833 */
3834 scrub_pause_on(fs_info);
Nikolay Borisovc83488a2018-06-20 15:49:14 +03003835 ret = btrfs_inc_block_group_ro(cache);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003836 if (!ret && is_dev_replace) {
3837 /*
3838 * If we are doing a device replace wait for any tasks
3839 * that started dellaloc right before we set the block
3840 * group to RO mode, as they might have just allocated
3841 * an extent from it or decided they could do a nocow
3842 * write. And if any such tasks did that, wait for their
3843 * ordered extents to complete and then commit the
3844 * current transaction, so that we can later see the new
3845 * extent items in the extent tree - the ordered extents
3846 * create delayed data references (for cow writes) when
3847 * they complete, which will be run and insert the
3848 * corresponding extent items into the extent tree when
3849 * we commit the transaction they used when running
3850 * inode.c:btrfs_finish_ordered_io(). We later use
3851 * the commit root of the extent tree to find extents
3852 * to copy from the srcdev into the tgtdev, and we don't
3853 * want to miss any new extents.
3854 */
3855 btrfs_wait_block_group_reservations(cache);
3856 btrfs_wait_nocow_writers(cache);
Chris Mason6374e57a2017-06-23 09:48:21 -07003857 ret = btrfs_wait_ordered_roots(fs_info, U64_MAX,
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003858 cache->key.objectid,
3859 cache->key.offset);
3860 if (ret > 0) {
3861 struct btrfs_trans_handle *trans;
3862
3863 trans = btrfs_join_transaction(root);
3864 if (IS_ERR(trans))
3865 ret = PTR_ERR(trans);
3866 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003867 ret = btrfs_commit_transaction(trans);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003868 if (ret) {
3869 scrub_pause_off(fs_info);
3870 btrfs_put_block_group(cache);
3871 break;
3872 }
3873 }
3874 }
Zhaolei55e3a602015-08-05 16:43:30 +08003875 scrub_pause_off(fs_info);
Zhaolei76a8efa2015-11-17 18:46:17 +08003876
3877 if (ret == 0) {
3878 ro_set = 1;
3879 } else if (ret == -ENOSPC) {
3880 /*
3881 * btrfs_inc_block_group_ro return -ENOSPC when it
3882 * failed in creating new chunk for metadata.
3883 * It is not a problem for scrub/replace, because
3884 * metadata are always cowed, and our scrub paused
3885 * commit_transactions.
3886 */
3887 ro_set = 0;
3888 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003889 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02003890 "failed setting block group ro: %d", ret);
Zhaolei55e3a602015-08-05 16:43:30 +08003891 btrfs_put_block_group(cache);
3892 break;
3893 }
3894
David Sterba7e79cb82018-03-24 02:11:38 +01003895 btrfs_dev_replace_write_lock(&fs_info->dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003896 dev_replace->cursor_right = found_key.offset + length;
3897 dev_replace->cursor_left = found_key.offset;
3898 dev_replace->item_needs_writeback = 1;
David Sterba7e79cb82018-03-24 02:11:38 +01003899 btrfs_dev_replace_write_unlock(&fs_info->dev_replace);
Zhao Lei8c204c92015-08-19 15:02:40 +08003900 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003901 found_key.offset, cache, is_dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003902
3903 /*
3904 * flush, submit all pending read and write bios, afterwards
3905 * wait for them.
3906 * Note that in the dev replace case, a read request causes
3907 * write requests that are submitted in the read completion
3908 * worker. Therefore in the current situation, it is required
3909 * that all write requests are flushed, so that all read and
3910 * write requests are really completed when bios_in_flight
3911 * changes to 0.
3912 */
David Sterba2073c4c22017-03-31 17:12:51 +02003913 sctx->flush_all_writes = true;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003914 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003915 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003916 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003917 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003918
3919 wait_event(sctx->list_wait,
3920 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003921
3922 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003923
3924 /*
3925 * must be called before we decrease @scrub_paused.
3926 * make sure we don't block transaction commit while
3927 * we are waiting pending workers finished.
3928 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003929 wait_event(sctx->list_wait,
3930 atomic_read(&sctx->workers_pending) == 0);
David Sterba2073c4c22017-03-31 17:12:51 +02003931 sctx->flush_all_writes = false;
Wang Shilong12cf9372014-02-19 19:24:17 +08003932
Zhaoleib708ce92015-08-05 16:43:29 +08003933 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003934
David Sterba7e79cb82018-03-24 02:11:38 +01003935 btrfs_dev_replace_write_lock(&fs_info->dev_replace);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003936 dev_replace->cursor_left = dev_replace->cursor_right;
3937 dev_replace->item_needs_writeback = 1;
David Sterba7e79cb82018-03-24 02:11:38 +01003938 btrfs_dev_replace_write_unlock(&fs_info->dev_replace);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003939
Zhaolei76a8efa2015-11-17 18:46:17 +08003940 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003941 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003942
Filipe Manana758f2df2015-11-19 11:45:48 +00003943 /*
3944 * We might have prevented the cleaner kthread from deleting
3945 * this block group if it was already unused because we raced
3946 * and set it to RO mode first. So add it back to the unused
3947 * list, otherwise it might not ever be deleted unless a manual
3948 * balance is triggered or it becomes used and unused again.
3949 */
3950 spin_lock(&cache->lock);
3951 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3952 btrfs_block_group_used(&cache->item) == 0) {
3953 spin_unlock(&cache->lock);
3954 spin_lock(&fs_info->unused_bgs_lock);
3955 if (list_empty(&cache->bg_list)) {
3956 btrfs_get_block_group(cache);
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +08003957 trace_btrfs_add_unused_block_group(cache);
Filipe Manana758f2df2015-11-19 11:45:48 +00003958 list_add_tail(&cache->bg_list,
3959 &fs_info->unused_bgs);
3960 }
3961 spin_unlock(&fs_info->unused_bgs_lock);
3962 } else {
3963 spin_unlock(&cache->lock);
3964 }
3965
Arne Jansena2de7332011-03-08 14:14:00 +01003966 btrfs_put_block_group(cache);
3967 if (ret)
3968 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003969 if (is_dev_replace &&
3970 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003971 ret = -EIO;
3972 break;
3973 }
3974 if (sctx->stat.malloc_errors > 0) {
3975 ret = -ENOMEM;
3976 break;
3977 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003978skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003979 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003980 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003981 }
3982
Arne Jansena2de7332011-03-08 14:14:00 +01003983 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003984
Zhaolei55e3a602015-08-05 16:43:30 +08003985 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003986}
3987
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003988static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3989 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003990{
3991 int i;
3992 u64 bytenr;
3993 u64 gen;
3994 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003995 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003996
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003997 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003998 return -EIO;
3999
Miao Xie5f546062014-07-24 11:37:09 +08004000 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004001 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08004002 gen = scrub_dev->generation;
4003 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004004 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01004005
4006 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
4007 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08004008 if (bytenr + BTRFS_SUPER_INFO_SIZE >
4009 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01004010 break;
4011
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004012 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01004013 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004014 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01004015 if (ret)
4016 return ret;
4017 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004018 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004019
4020 return 0;
4021}
4022
4023/*
4024 * get a reference count on fs_info->scrub_workers. start worker if necessary
4025 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01004026static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
4027 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004028{
David Sterba6f011052015-02-16 18:34:01 +01004029 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08004030 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01004031
Arne Jansen632dd772011-06-10 12:07:07 +02004032 if (fs_info->scrub_workers_refcnt == 0) {
David Sterbaaf1cbe02017-03-31 18:42:57 +02004033 fs_info->scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub",
4034 flags, is_dev_replace ? 1 : max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08004035 if (!fs_info->scrub_workers)
4036 goto fail_scrub_workers;
4037
Qu Wenruo0339ef22014-02-28 10:46:17 +08004038 fs_info->scrub_wr_completion_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004039 btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004040 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004041 if (!fs_info->scrub_wr_completion_workers)
4042 goto fail_scrub_wr_completion_workers;
4043
Zhao Lei20b2e302015-06-04 20:09:15 +08004044 fs_info->scrub_parity_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004045 btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
Zhao Lei20b2e302015-06-04 20:09:15 +08004046 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004047 if (!fs_info->scrub_parity_workers)
4048 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02004049 }
Arne Jansena2de7332011-03-08 14:14:00 +01004050 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08004051 return 0;
4052
4053fail_scrub_parity_workers:
Zhao Leie82afc52015-06-12 20:36:58 +08004054 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4055fail_scrub_wr_completion_workers:
4056 btrfs_destroy_workqueue(fs_info->scrub_workers);
4057fail_scrub_workers:
4058 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01004059}
4060
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004061static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004062{
Stefan Behrensff023aa2012-11-06 11:43:11 +01004063 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08004064 btrfs_destroy_workqueue(fs_info->scrub_workers);
4065 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08004066 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004067 }
Arne Jansena2de7332011-03-08 14:14:00 +01004068 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004069}
4070
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004071int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
4072 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004073 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004074{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004075 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004076 int ret;
4077 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08004078 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01004079
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004080 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01004081 return -EINVAL;
4082
Jeff Mahoneyda170662016-06-15 09:22:56 -04004083 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004084 /*
4085 * in this case scrub is unable to calculate the checksum
4086 * the way scrub is implemented. Do not handle this
4087 * situation at all because it won't ever happen.
4088 */
Frank Holtonefe120a2013-12-20 11:37:06 -05004089 btrfs_err(fs_info,
4090 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004091 fs_info->nodesize,
4092 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004093 return -EINVAL;
4094 }
4095
Jeff Mahoneyda170662016-06-15 09:22:56 -04004096 if (fs_info->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004097 /* not supported for data w/o checksums */
Chandan Rajendra751bebb2016-07-04 10:04:39 +05304098 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004099 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004100 fs_info->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01004101 return -EINVAL;
4102 }
4103
Jeff Mahoneyda170662016-06-15 09:22:56 -04004104 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004105 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04004106 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004107 /*
4108 * would exhaust the array bounds of pagev member in
4109 * struct scrub_block
4110 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004111 btrfs_err(fs_info,
4112 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004113 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004114 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004115 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004116 SCRUB_MAX_PAGES_PER_BLOCK);
4117 return -EINVAL;
4118 }
4119
Arne Jansena2de7332011-03-08 14:14:00 +01004120
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004121 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4122 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jaine6e674b2017-12-04 12:54:54 +08004123 if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
4124 !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004125 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004126 return -ENODEV;
4127 }
Arne Jansena2de7332011-03-08 14:14:00 +01004128
Anand Jainebbede42017-12-04 12:54:52 +08004129 if (!is_dev_replace && !readonly &&
4130 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Miao Xie5d68da32014-07-24 11:37:07 +08004131 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4132 rcu_read_lock();
4133 name = rcu_dereference(dev->name);
4134 btrfs_err(fs_info, "scrub: device %s is not writable",
4135 name->str);
4136 rcu_read_unlock();
4137 return -EROFS;
4138 }
4139
Wang Shilong3b7a0162013-10-12 02:11:12 +08004140 mutex_lock(&fs_info->scrub_lock);
Anand Jaine12c9622017-12-04 12:54:53 +08004141 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004142 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004143 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004144 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004145 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01004146 }
4147
David Sterba7e79cb82018-03-24 02:11:38 +01004148 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004149 if (dev->scrub_ctx ||
Stefan Behrens8dabb742012-11-06 13:15:27 +01004150 (!is_dev_replace &&
4151 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
David Sterba7e79cb82018-03-24 02:11:38 +01004152 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01004153 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004154 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004155 return -EINPROGRESS;
4156 }
David Sterba7e79cb82018-03-24 02:11:38 +01004157 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08004158
4159 ret = scrub_workers_get(fs_info, is_dev_replace);
4160 if (ret) {
4161 mutex_unlock(&fs_info->scrub_lock);
4162 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4163 return ret;
4164 }
4165
Stefan Behrens63a212a2012-11-05 18:29:28 +01004166 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004167 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004168 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004169 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4170 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004171 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004172 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004173 sctx->readonly = readonly;
Anand Jaincadbc0a2018-01-03 16:08:30 +08004174 dev->scrub_ctx = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08004175 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004176
Wang Shilong3cb09292013-12-04 21:15:19 +08004177 /*
4178 * checking @scrub_pause_req here, we can avoid
4179 * race between committing transaction and scrubbing.
4180 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08004181 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004182 atomic_inc(&fs_info->scrubs_running);
4183 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004184
Stefan Behrensff023aa2012-11-06 11:43:11 +01004185 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08004186 /*
4187 * by holding device list mutex, we can
4188 * kick off writing super in log tree sync.
4189 */
Wang Shilong3cb09292013-12-04 21:15:19 +08004190 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004191 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08004192 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004193 }
Arne Jansena2de7332011-03-08 14:14:00 +01004194
4195 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004196 ret = scrub_enumerate_chunks(sctx, dev, start, end,
4197 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01004198
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004199 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004200 atomic_dec(&fs_info->scrubs_running);
4201 wake_up(&fs_info->scrub_pause_wait);
4202
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004203 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02004204
Arne Jansena2de7332011-03-08 14:14:00 +01004205 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004206 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01004207
4208 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004209 dev->scrub_ctx = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08004210 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004211 mutex_unlock(&fs_info->scrub_lock);
4212
Filipe Mananaf55985f2015-02-09 21:14:24 +00004213 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004214
4215 return ret;
4216}
4217
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004218void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004219{
Arne Jansena2de7332011-03-08 14:14:00 +01004220 mutex_lock(&fs_info->scrub_lock);
4221 atomic_inc(&fs_info->scrub_pause_req);
4222 while (atomic_read(&fs_info->scrubs_paused) !=
4223 atomic_read(&fs_info->scrubs_running)) {
4224 mutex_unlock(&fs_info->scrub_lock);
4225 wait_event(fs_info->scrub_pause_wait,
4226 atomic_read(&fs_info->scrubs_paused) ==
4227 atomic_read(&fs_info->scrubs_running));
4228 mutex_lock(&fs_info->scrub_lock);
4229 }
4230 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004231}
4232
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004233void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004234{
Arne Jansena2de7332011-03-08 14:14:00 +01004235 atomic_dec(&fs_info->scrub_pause_req);
4236 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01004237}
4238
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004239int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004240{
Arne Jansena2de7332011-03-08 14:14:00 +01004241 mutex_lock(&fs_info->scrub_lock);
4242 if (!atomic_read(&fs_info->scrubs_running)) {
4243 mutex_unlock(&fs_info->scrub_lock);
4244 return -ENOTCONN;
4245 }
4246
4247 atomic_inc(&fs_info->scrub_cancel_req);
4248 while (atomic_read(&fs_info->scrubs_running)) {
4249 mutex_unlock(&fs_info->scrub_lock);
4250 wait_event(fs_info->scrub_pause_wait,
4251 atomic_read(&fs_info->scrubs_running) == 0);
4252 mutex_lock(&fs_info->scrub_lock);
4253 }
4254 atomic_dec(&fs_info->scrub_cancel_req);
4255 mutex_unlock(&fs_info->scrub_lock);
4256
4257 return 0;
4258}
4259
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004260int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
4261 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004262{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004263 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004264
4265 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004266 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004267 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004268 mutex_unlock(&fs_info->scrub_lock);
4269 return -ENOTCONN;
4270 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004271 atomic_inc(&sctx->cancel_req);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004272 while (dev->scrub_ctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004273 mutex_unlock(&fs_info->scrub_lock);
4274 wait_event(fs_info->scrub_pause_wait,
Anand Jaincadbc0a2018-01-03 16:08:30 +08004275 dev->scrub_ctx == NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004276 mutex_lock(&fs_info->scrub_lock);
4277 }
4278 mutex_unlock(&fs_info->scrub_lock);
4279
4280 return 0;
4281}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004282
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004283int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01004284 struct btrfs_scrub_progress *progress)
4285{
4286 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004287 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004288
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004289 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4290 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004291 if (dev)
Anand Jaincadbc0a2018-01-03 16:08:30 +08004292 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004293 if (sctx)
4294 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004295 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004296
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004297 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004298}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004299
4300static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4301 u64 extent_logical, u64 extent_len,
4302 u64 *extent_physical,
4303 struct btrfs_device **extent_dev,
4304 int *extent_mirror_num)
4305{
4306 u64 mapped_length;
4307 struct btrfs_bio *bbio = NULL;
4308 int ret;
4309
4310 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004311 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004312 &mapped_length, &bbio, 0);
4313 if (ret || !bbio || mapped_length < extent_len ||
4314 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08004315 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004316 return;
4317 }
4318
4319 *extent_physical = bbio->stripes[0].physical;
4320 *extent_mirror_num = bbio->mirror_num;
4321 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004322 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004323}