blob: 2e9a322773f287f2444e657799900c16ea92479b [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Arne Jansena2de7332011-03-08 14:14:00 +01002/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
Arne Jansena2de7332011-03-08 14:14:00 +01004 */
5
Arne Jansena2de7332011-03-08 14:14:00 +01006#include <linux/blkdev.h>
Jan Schmidt558540c2011-06-13 19:59:12 +02007#include <linux/ratelimit.h>
David Sterbade2491f2017-05-31 19:21:38 +02008#include <linux/sched/mm.h>
Johannes Thumshirnd5178572019-06-03 16:58:57 +02009#include <crypto/hash.h>
Arne Jansena2de7332011-03-08 14:14:00 +010010#include "ctree.h"
Dennis Zhou6e80d4f2019-12-13 16:22:15 -080011#include "discard.h"
Arne Jansena2de7332011-03-08 14:14:00 +010012#include "volumes.h"
13#include "disk-io.h"
14#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020015#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020016#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020017#include "extent_io.h"
Stefan Behrensff023aa2012-11-06 11:43:11 +010018#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010019#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040020#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050021#include "raid56.h"
Josef Bacikaac00232019-06-20 15:37:44 -040022#include "block-group.h"
Naohiro Aota12659252020-11-10 20:26:14 +090023#include "zoned.h"
Arne Jansena2de7332011-03-08 14:14:00 +010024
25/*
26 * This is only the first step towards a full-features scrub. It reads all
27 * extent and super block and verifies the checksums. In case a bad checksum
28 * is found or the extent cannot be read, good data will be written back if
29 * any can be found.
30 *
31 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010032 * - In case an unrepairable extent is encountered, track which files are
33 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010034 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010035 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010036 */
37
Stefan Behrensb5d67f62012-03-27 14:21:27 -040038struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010039struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010040
Stefan Behrensff023aa2012-11-06 11:43:11 +010041/*
Qu Wenruoc9d328c2021-12-06 13:52:58 +080042 * The following three values only influence the performance.
43 *
Stefan Behrensff023aa2012-11-06 11:43:11 +010044 * The last one configures the number of parallel and outstanding I/O
Qu Wenruoc9d328c2021-12-06 13:52:58 +080045 * operations. The first one configures an upper limit for the number
Stefan Behrensff023aa2012-11-06 11:43:11 +010046 * of (dynamically allocated) pages that are added to a bio.
47 */
Qu Wenruoc9d328c2021-12-06 13:52:58 +080048#define SCRUB_PAGES_PER_BIO 32 /* 128KiB per bio for x86 */
49#define SCRUB_BIOS_PER_SCTX 64 /* 8MiB per device in flight for x86 */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010050
51/*
Qu Wenruo0bb3acd2021-12-06 13:52:57 +080052 * The following value times PAGE_SIZE needs to be large enough to match the
Stefan Behrens7a9e9982012-11-02 14:58:04 +010053 * largest node/leaf/sector size that shall be supported.
Stefan Behrens7a9e9982012-11-02 14:58:04 +010054 */
Qu Wenruo0bb3acd2021-12-06 13:52:57 +080055#define SCRUB_MAX_PAGES_PER_BLOCK (BTRFS_MAX_METADATA_BLOCKSIZE / SZ_4K)
Arne Jansena2de7332011-03-08 14:14:00 +010056
Miao Xieaf8e2d12014-10-23 14:42:50 +080057struct scrub_recover {
Elena Reshetova6f615012017-03-03 10:55:21 +020058 refcount_t refs;
Qu Wenruo4c664612021-09-15 15:17:16 +080059 struct btrfs_io_context *bioc;
Miao Xieaf8e2d12014-10-23 14:42:50 +080060 u64 map_length;
61};
62
Arne Jansena2de7332011-03-08 14:14:00 +010063struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040064 struct scrub_block *sblock;
65 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020066 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080067 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010068 u64 flags; /* extent flags */
69 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040070 u64 logical;
71 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010072 u64 physical_for_dev_replace;
Zhao Lei57019342015-01-20 15:11:45 +080073 atomic_t refs;
Qu Wenruo2c363952020-11-13 20:51:44 +080074 u8 mirror_num;
Colin Ian Kingd08e38b2021-11-10 19:20:08 +000075 unsigned int have_csum:1;
76 unsigned int io_error:1;
Arne Jansena2de7332011-03-08 14:14:00 +010077 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080078
79 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010080};
81
82struct scrub_bio {
83 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010084 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010085 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010086 struct bio *bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020087 blk_status_t status;
Arne Jansena2de7332011-03-08 14:14:00 +010088 u64 logical;
89 u64 physical;
Qu Wenruoc9d328c2021-12-06 13:52:58 +080090 struct scrub_page *pagev[SCRUB_PAGES_PER_BIO];
Stefan Behrensb5d67f62012-03-27 14:21:27 -040091 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +010092 int next_free;
93 struct btrfs_work work;
94};
95
Stefan Behrensb5d67f62012-03-27 14:21:27 -040096struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +010097 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -040098 int page_count;
99 atomic_t outstanding_pages;
Elena Reshetova186debd2017-03-03 10:55:23 +0200100 refcount_t refs; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100101 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800102 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400103 struct {
104 unsigned int header_error:1;
105 unsigned int checksum_error:1;
106 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200107 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800108
109 /* The following is for the data used to check parity */
110 /* It is for the data with checksum */
111 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400112 };
Omar Sandoval73ff61d2015-06-19 11:52:51 -0700113 struct btrfs_work work;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400114};
115
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800116/* Used for the chunks with parity stripe such RAID5/6 */
117struct scrub_parity {
118 struct scrub_ctx *sctx;
119
120 struct btrfs_device *scrub_dev;
121
122 u64 logic_start;
123
124 u64 logic_end;
125
126 int nsectors;
127
Qu Wenruofa485d22020-12-02 14:48:07 +0800128 u32 stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800129
Elena Reshetova78a76452017-03-03 10:55:24 +0200130 refcount_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800131
132 struct list_head spages;
133
134 /* Work of parity check and repair */
135 struct btrfs_work work;
136
137 /* Mark the parity blocks which have data */
138 unsigned long *dbitmap;
139
140 /*
141 * Mark the parity blocks which have data, but errors happen when
142 * read data or check data
143 */
144 unsigned long *ebitmap;
145
Gustavo A. R. Silvaa8753ee2020-03-06 16:13:33 -0600146 unsigned long bitmap[];
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800147};
148
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100149struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100150 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400151 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100152 int first_free;
153 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100154 atomic_t bios_in_flight;
155 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100156 spinlock_t list_lock;
157 wait_queue_head_t list_wait;
Arne Jansena2de7332011-03-08 14:14:00 +0100158 struct list_head csum_list;
159 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100160 int readonly;
Qu Wenruoc9d328c2021-12-06 13:52:58 +0800161 int pages_per_bio;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100162
David Sterbaeb3b5052019-10-09 13:58:13 +0200163 /* State of IO submission throttling affecting the associated device */
164 ktime_t throttle_deadline;
165 u64 throttle_sent;
166
Stefan Behrens63a212a2012-11-05 18:29:28 +0100167 int is_dev_replace;
Naohiro Aotade17add2021-02-04 19:22:13 +0900168 u64 write_pointer;
David Sterba3fb99302017-05-16 19:10:32 +0200169
170 struct scrub_bio *wr_curr_bio;
171 struct mutex wr_lock;
David Sterba3fb99302017-05-16 19:10:32 +0200172 struct btrfs_device *wr_tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200173 bool flush_all_writes;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100174
Arne Jansena2de7332011-03-08 14:14:00 +0100175 /*
176 * statistics
177 */
178 struct btrfs_scrub_progress stat;
179 spinlock_t stat_lock;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000180
181 /*
182 * Use a ref counter to avoid use-after-free issues. Scrub workers
183 * decrement bios_in_flight and workers_pending and then do a wakeup
184 * on the list_wait wait queue. We must ensure the main scrub task
185 * doesn't free the scrub context before or while the workers are
186 * doing the wakeup() call.
187 */
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200188 refcount_t refs;
Arne Jansena2de7332011-03-08 14:14:00 +0100189};
190
Jan Schmidt558540c2011-06-13 19:59:12 +0200191struct scrub_warning {
192 struct btrfs_path *path;
193 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200194 const char *errstr;
David Sterba6aa21262017-10-04 17:07:07 +0200195 u64 physical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200196 u64 logical;
197 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200198};
199
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800200struct full_stripe_lock {
201 struct rb_node node;
202 u64 logical;
203 u64 refs;
204 struct mutex mutex;
205};
206
Zhao Leibe50a8d2015-01-20 15:11:42 +0800207static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100208 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100209static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +0800210 struct scrub_block *sblock,
211 int retry_failed_mirror);
Zhao Leiba7cf982015-08-24 21:18:02 +0800212static void scrub_recheck_block_checksum(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400213static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800214 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400215static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
216 struct scrub_block *sblock_good,
217 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100218static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
219static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
220 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400221static int scrub_checksum_data(struct scrub_block *sblock);
222static int scrub_checksum_tree_block(struct scrub_block *sblock);
223static int scrub_checksum_super(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400224static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100225static void scrub_page_get(struct scrub_page *spage);
226static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800227static void scrub_parity_get(struct scrub_parity *sparity);
228static void scrub_parity_put(struct scrub_parity *sparity);
Qu Wenruofa485d22020-12-02 14:48:07 +0800229static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u32 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100230 u64 physical, struct btrfs_device *dev, u64 flags,
Qu Wenruo96e63a42020-11-03 21:31:02 +0800231 u64 gen, int mirror_num, u8 *csum,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100232 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200233static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400234static void scrub_bio_end_io_worker(struct btrfs_work *work);
235static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100236static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
Qu Wenruofa485d22020-12-02 14:48:07 +0800237 u64 extent_logical, u32 extent_len,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100238 u64 *extent_physical,
239 struct btrfs_device **extent_dev,
240 int *extent_mirror_num);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100241static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
242 struct scrub_page *spage);
243static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200244static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100245static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000246static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400247
Qu Wenruo261d2dc2020-11-03 21:31:01 +0800248static inline int scrub_is_page_on_raid56(struct scrub_page *spage)
Liu Bo762221f2018-01-02 13:36:42 -0700249{
Qu Wenruo261d2dc2020-11-03 21:31:01 +0800250 return spage->recover &&
Qu Wenruo4c664612021-09-15 15:17:16 +0800251 (spage->recover->bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Liu Bo762221f2018-01-02 13:36:42 -0700252}
Stefan Behrens1623ede2012-03-27 14:21:26 -0400253
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100254static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
255{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200256 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100257 atomic_inc(&sctx->bios_in_flight);
258}
259
260static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
261{
262 atomic_dec(&sctx->bios_in_flight);
263 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000264 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100265}
266
Wang Shilongcb7ab022013-12-04 21:16:53 +0800267static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800268{
269 while (atomic_read(&fs_info->scrub_pause_req)) {
270 mutex_unlock(&fs_info->scrub_lock);
271 wait_event(fs_info->scrub_pause_wait,
272 atomic_read(&fs_info->scrub_pause_req) == 0);
273 mutex_lock(&fs_info->scrub_lock);
274 }
275}
276
Zhaolei0e22be82015-08-05 16:43:28 +0800277static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800278{
279 atomic_inc(&fs_info->scrubs_paused);
280 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800281}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800282
Zhaolei0e22be82015-08-05 16:43:28 +0800283static void scrub_pause_off(struct btrfs_fs_info *fs_info)
284{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800285 mutex_lock(&fs_info->scrub_lock);
286 __scrub_blocked_if_needed(fs_info);
287 atomic_dec(&fs_info->scrubs_paused);
288 mutex_unlock(&fs_info->scrub_lock);
289
290 wake_up(&fs_info->scrub_pause_wait);
291}
292
Zhaolei0e22be82015-08-05 16:43:28 +0800293static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
294{
295 scrub_pause_on(fs_info);
296 scrub_pause_off(fs_info);
297}
298
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100299/*
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800300 * Insert new full stripe lock into full stripe locks tree
301 *
302 * Return pointer to existing or newly inserted full_stripe_lock structure if
303 * everything works well.
304 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
305 *
306 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
307 * function
308 */
309static struct full_stripe_lock *insert_full_stripe_lock(
310 struct btrfs_full_stripe_locks_tree *locks_root,
311 u64 fstripe_logical)
312{
313 struct rb_node **p;
314 struct rb_node *parent = NULL;
315 struct full_stripe_lock *entry;
316 struct full_stripe_lock *ret;
317
David Sterbaa32bf9a2018-03-16 02:21:22 +0100318 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800319
320 p = &locks_root->root.rb_node;
321 while (*p) {
322 parent = *p;
323 entry = rb_entry(parent, struct full_stripe_lock, node);
324 if (fstripe_logical < entry->logical) {
325 p = &(*p)->rb_left;
326 } else if (fstripe_logical > entry->logical) {
327 p = &(*p)->rb_right;
328 } else {
329 entry->refs++;
330 return entry;
331 }
332 }
333
Filipe Mananaa5fb1142018-11-26 20:07:17 +0000334 /*
335 * Insert new lock.
Filipe Mananaa5fb1142018-11-26 20:07:17 +0000336 */
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800337 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
338 if (!ret)
339 return ERR_PTR(-ENOMEM);
340 ret->logical = fstripe_logical;
341 ret->refs = 1;
342 mutex_init(&ret->mutex);
343
344 rb_link_node(&ret->node, parent, p);
345 rb_insert_color(&ret->node, &locks_root->root);
346 return ret;
347}
348
349/*
350 * Search for a full stripe lock of a block group
351 *
352 * Return pointer to existing full stripe lock if found
353 * Return NULL if not found
354 */
355static struct full_stripe_lock *search_full_stripe_lock(
356 struct btrfs_full_stripe_locks_tree *locks_root,
357 u64 fstripe_logical)
358{
359 struct rb_node *node;
360 struct full_stripe_lock *entry;
361
David Sterbaa32bf9a2018-03-16 02:21:22 +0100362 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800363
364 node = locks_root->root.rb_node;
365 while (node) {
366 entry = rb_entry(node, struct full_stripe_lock, node);
367 if (fstripe_logical < entry->logical)
368 node = node->rb_left;
369 else if (fstripe_logical > entry->logical)
370 node = node->rb_right;
371 else
372 return entry;
373 }
374 return NULL;
375}
376
377/*
378 * Helper to get full stripe logical from a normal bytenr.
379 *
380 * Caller must ensure @cache is a RAID56 block group.
381 */
David Sterba32da53862019-10-29 19:20:18 +0100382static u64 get_full_stripe_logical(struct btrfs_block_group *cache, u64 bytenr)
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800383{
384 u64 ret;
385
386 /*
387 * Due to chunk item size limit, full stripe length should not be
388 * larger than U32_MAX. Just a sanity check here.
389 */
390 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
391
392 /*
393 * round_down() can only handle power of 2, while RAID56 full
394 * stripe length can be 64KiB * n, so we need to manually round down.
395 */
David Sterbab3470b52019-10-23 18:48:22 +0200396 ret = div64_u64(bytenr - cache->start, cache->full_stripe_len) *
397 cache->full_stripe_len + cache->start;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800398 return ret;
399}
400
401/*
402 * Lock a full stripe to avoid concurrency of recovery and read
403 *
404 * It's only used for profiles with parities (RAID5/6), for other profiles it
405 * does nothing.
406 *
407 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
408 * So caller must call unlock_full_stripe() at the same context.
409 *
410 * Return <0 if encounters error.
411 */
412static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
413 bool *locked_ret)
414{
David Sterba32da53862019-10-29 19:20:18 +0100415 struct btrfs_block_group *bg_cache;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800416 struct btrfs_full_stripe_locks_tree *locks_root;
417 struct full_stripe_lock *existing;
418 u64 fstripe_start;
419 int ret = 0;
420
421 *locked_ret = false;
422 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
423 if (!bg_cache) {
424 ASSERT(0);
425 return -ENOENT;
426 }
427
428 /* Profiles not based on parity don't need full stripe lock */
429 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
430 goto out;
431 locks_root = &bg_cache->full_stripe_locks_root;
432
433 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
434
435 /* Now insert the full stripe lock */
436 mutex_lock(&locks_root->lock);
437 existing = insert_full_stripe_lock(locks_root, fstripe_start);
438 mutex_unlock(&locks_root->lock);
439 if (IS_ERR(existing)) {
440 ret = PTR_ERR(existing);
441 goto out;
442 }
443 mutex_lock(&existing->mutex);
444 *locked_ret = true;
445out:
446 btrfs_put_block_group(bg_cache);
447 return ret;
448}
449
450/*
451 * Unlock a full stripe.
452 *
453 * NOTE: Caller must ensure it's the same context calling corresponding
454 * lock_full_stripe().
455 *
456 * Return 0 if we unlock full stripe without problem.
457 * Return <0 for error
458 */
459static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
460 bool locked)
461{
David Sterba32da53862019-10-29 19:20:18 +0100462 struct btrfs_block_group *bg_cache;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800463 struct btrfs_full_stripe_locks_tree *locks_root;
464 struct full_stripe_lock *fstripe_lock;
465 u64 fstripe_start;
466 bool freeit = false;
467 int ret = 0;
468
469 /* If we didn't acquire full stripe lock, no need to continue */
470 if (!locked)
471 return 0;
472
473 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
474 if (!bg_cache) {
475 ASSERT(0);
476 return -ENOENT;
477 }
478 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
479 goto out;
480
481 locks_root = &bg_cache->full_stripe_locks_root;
482 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
483
484 mutex_lock(&locks_root->lock);
485 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
486 /* Unpaired unlock_full_stripe() detected */
487 if (!fstripe_lock) {
488 WARN_ON(1);
489 ret = -ENOENT;
490 mutex_unlock(&locks_root->lock);
491 goto out;
492 }
493
494 if (fstripe_lock->refs == 0) {
495 WARN_ON(1);
496 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
497 fstripe_lock->logical);
498 } else {
499 fstripe_lock->refs--;
500 }
501
502 if (fstripe_lock->refs == 0) {
503 rb_erase(&fstripe_lock->node, &locks_root->root);
504 freeit = true;
505 }
506 mutex_unlock(&locks_root->lock);
507
508 mutex_unlock(&fstripe_lock->mutex);
509 if (freeit)
510 kfree(fstripe_lock);
511out:
512 btrfs_put_block_group(bg_cache);
513 return ret;
514}
515
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100516static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100517{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100518 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100519 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100520 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100521 struct btrfs_ordered_sum, list);
522 list_del(&sum->list);
523 kfree(sum);
524 }
525}
526
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100527static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100528{
529 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100530
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100531 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100532 return;
533
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400534 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100535 if (sctx->curr != -1) {
536 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400537
538 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100539 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400540 scrub_block_put(sbio->pagev[i]->sblock);
541 }
542 bio_put(sbio->bio);
543 }
544
Stefan Behrensff023aa2012-11-06 11:43:11 +0100545 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100546 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100547
548 if (!sbio)
549 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100550 kfree(sbio);
551 }
552
David Sterba3fb99302017-05-16 19:10:32 +0200553 kfree(sctx->wr_curr_bio);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100554 scrub_free_csums(sctx);
555 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100556}
557
Filipe Mananaf55985f2015-02-09 21:14:24 +0000558static void scrub_put_ctx(struct scrub_ctx *sctx)
559{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200560 if (refcount_dec_and_test(&sctx->refs))
Filipe Mananaf55985f2015-02-09 21:14:24 +0000561 scrub_free_ctx(sctx);
562}
563
David Sterba92f7ba42018-12-04 16:11:55 +0100564static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
565 struct btrfs_fs_info *fs_info, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100566{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100567 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100568 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100569
David Sterba58c4e172016-02-11 10:49:42 +0100570 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100571 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100572 goto nomem;
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200573 refcount_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100574 sctx->is_dev_replace = is_dev_replace;
Qu Wenruoc9d328c2021-12-06 13:52:58 +0800575 sctx->pages_per_bio = SCRUB_PAGES_PER_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100576 sctx->curr = -1;
David Sterba92f7ba42018-12-04 16:11:55 +0100577 sctx->fs_info = fs_info;
Dan Robertsone49be142019-02-19 02:56:43 +0000578 INIT_LIST_HEAD(&sctx->csum_list);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100579 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100580 struct scrub_bio *sbio;
581
David Sterba58c4e172016-02-11 10:49:42 +0100582 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
Arne Jansena2de7332011-03-08 14:14:00 +0100583 if (!sbio)
584 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100585 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100586
Arne Jansena2de7332011-03-08 14:14:00 +0100587 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100588 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400589 sbio->page_count = 0;
Omar Sandovala0cac0e2019-09-16 11:30:57 -0700590 btrfs_init_work(&sbio->work, scrub_bio_end_io_worker, NULL,
591 NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100592
Stefan Behrensff023aa2012-11-06 11:43:11 +0100593 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100594 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200595 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100596 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100597 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100598 sctx->first_free = 0;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100599 atomic_set(&sctx->bios_in_flight, 0);
600 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100601 atomic_set(&sctx->cancel_req, 0);
Arne Jansena2de7332011-03-08 14:14:00 +0100602
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100603 spin_lock_init(&sctx->list_lock);
604 spin_lock_init(&sctx->stat_lock);
605 init_waitqueue_head(&sctx->list_wait);
David Sterbaeb3b5052019-10-09 13:58:13 +0200606 sctx->throttle_deadline = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100607
David Sterba3fb99302017-05-16 19:10:32 +0200608 WARN_ON(sctx->wr_curr_bio != NULL);
609 mutex_init(&sctx->wr_lock);
610 sctx->wr_curr_bio = NULL;
David Sterba8fcdac32017-05-16 19:10:23 +0200611 if (is_dev_replace) {
David Sterbaded56182017-06-26 15:19:00 +0200612 WARN_ON(!fs_info->dev_replace.tgtdev);
David Sterbaded56182017-06-26 15:19:00 +0200613 sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200614 sctx->flush_all_writes = false;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100615 }
David Sterba8fcdac32017-05-16 19:10:23 +0200616
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100617 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100618
619nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100620 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100621 return ERR_PTR(-ENOMEM);
622}
623
Stefan Behrensff023aa2012-11-06 11:43:11 +0100624static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
625 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200626{
Jan Schmidt558540c2011-06-13 19:59:12 +0200627 u32 nlink;
628 int ret;
629 int i;
David Sterbade2491f2017-05-31 19:21:38 +0200630 unsigned nofs_flag;
Jan Schmidt558540c2011-06-13 19:59:12 +0200631 struct extent_buffer *eb;
632 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100633 struct scrub_warning *swarn = warn_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400634 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200635 struct inode_fs_paths *ipath = NULL;
636 struct btrfs_root *local_root;
David Sterba1d4c08e2015-01-02 19:36:14 +0100637 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200638
David Sterba56e93572020-05-15 19:35:55 +0200639 local_root = btrfs_get_fs_root(fs_info, root, true);
Jan Schmidt558540c2011-06-13 19:59:12 +0200640 if (IS_ERR(local_root)) {
641 ret = PTR_ERR(local_root);
642 goto err;
643 }
644
David Sterba14692cc2015-01-02 18:55:46 +0100645 /*
646 * this makes the path point to (inum INODE_ITEM ioff)
647 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100648 key.objectid = inum;
649 key.type = BTRFS_INODE_ITEM_KEY;
650 key.offset = 0;
651
652 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200653 if (ret) {
Josef Bacik00246522020-01-24 09:33:01 -0500654 btrfs_put_root(local_root);
Jan Schmidt558540c2011-06-13 19:59:12 +0200655 btrfs_release_path(swarn->path);
656 goto err;
657 }
658
659 eb = swarn->path->nodes[0];
660 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
661 struct btrfs_inode_item);
Jan Schmidt558540c2011-06-13 19:59:12 +0200662 nlink = btrfs_inode_nlink(eb, inode_item);
663 btrfs_release_path(swarn->path);
664
David Sterbade2491f2017-05-31 19:21:38 +0200665 /*
666 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
667 * uses GFP_NOFS in this context, so we keep it consistent but it does
668 * not seem to be strictly necessary.
669 */
670 nofs_flag = memalloc_nofs_save();
Jan Schmidt558540c2011-06-13 19:59:12 +0200671 ipath = init_ipath(4096, local_root, swarn->path);
David Sterbade2491f2017-05-31 19:21:38 +0200672 memalloc_nofs_restore(nofs_flag);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300673 if (IS_ERR(ipath)) {
Josef Bacik00246522020-01-24 09:33:01 -0500674 btrfs_put_root(local_root);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300675 ret = PTR_ERR(ipath);
676 ipath = NULL;
677 goto err;
678 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200679 ret = paths_from_inode(inum, ipath);
680
681 if (ret < 0)
682 goto err;
683
684 /*
685 * we deliberately ignore the bit ipath might have been too small to
686 * hold all of the paths here
687 */
688 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400689 btrfs_warn_in_rcu(fs_info,
Qu Wenruo8df507c2021-04-22 19:02:46 +0800690"%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %u, links %u (path: %s)",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400691 swarn->errstr, swarn->logical,
692 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200693 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400694 root, inum, offset,
Qu Wenruo8df507c2021-04-22 19:02:46 +0800695 fs_info->sectorsize, nlink,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400696 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200697
Josef Bacik00246522020-01-24 09:33:01 -0500698 btrfs_put_root(local_root);
Jan Schmidt558540c2011-06-13 19:59:12 +0200699 free_ipath(ipath);
700 return 0;
701
702err:
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400703 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200704 "%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 -0400705 swarn->errstr, swarn->logical,
706 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200707 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400708 root, inum, offset, ret);
Jan Schmidt558540c2011-06-13 19:59:12 +0200709
710 free_ipath(ipath);
711 return 0;
712}
713
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400714static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200715{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100716 struct btrfs_device *dev;
717 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200718 struct btrfs_path *path;
719 struct btrfs_key found_key;
720 struct extent_buffer *eb;
721 struct btrfs_extent_item *ei;
722 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200723 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100724 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600725 u64 flags = 0;
726 u64 ref_root;
727 u32 item_size;
Dan Carpenter07c9a8e2016-03-11 11:08:56 +0300728 u8 ref_level = 0;
Liu Bo69917e42012-09-07 20:01:28 -0600729 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200730
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100731 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100732 dev = sblock->pagev[0]->dev;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400733 fs_info = sblock->sctx->fs_info;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100734
Jan Schmidt558540c2011-06-13 19:59:12 +0200735 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200736 if (!path)
737 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200738
David Sterba6aa21262017-10-04 17:07:07 +0200739 swarn.physical = sblock->pagev[0]->physical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100740 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200741 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100742 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200743
Liu Bo69917e42012-09-07 20:01:28 -0600744 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
745 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200746 if (ret < 0)
747 goto out;
748
Jan Schmidt4692cf52011-12-02 14:56:41 +0100749 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200750 swarn.extent_item_size = found_key.offset;
751
752 eb = path->nodes[0];
753 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
Josef Bacik3212fa12021-10-21 14:58:35 -0400754 item_size = btrfs_item_size(eb, path->slots[0]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200755
Liu Bo69917e42012-09-07 20:01:28 -0600756 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200757 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800758 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
759 item_size, &ref_root,
760 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200761 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200762"%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400763 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400764 rcu_str_deref(dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200765 swarn.physical,
Jan Schmidt558540c2011-06-13 19:59:12 +0200766 ref_level ? "node" : "leaf",
767 ret < 0 ? -1 : ref_level,
768 ret < 0 ? -1 : ref_root);
769 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600770 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200771 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600772 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200773 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100774 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100775 iterate_extent_inodes(fs_info, found_key.objectid,
776 extent_item_pos, 1,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400777 scrub_print_warning_inode, &swarn, false);
Jan Schmidt558540c2011-06-13 19:59:12 +0200778 }
779
780out:
781 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200782}
783
Miao Xieaf8e2d12014-10-23 14:42:50 +0800784static inline void scrub_get_recover(struct scrub_recover *recover)
785{
Elena Reshetova6f615012017-03-03 10:55:21 +0200786 refcount_inc(&recover->refs);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800787}
788
Qu Wenruoe501bfe2017-03-29 09:33:22 +0800789static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
790 struct scrub_recover *recover)
Miao Xieaf8e2d12014-10-23 14:42:50 +0800791{
Elena Reshetova6f615012017-03-03 10:55:21 +0200792 if (refcount_dec_and_test(&recover->refs)) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +0800793 btrfs_bio_counter_dec(fs_info);
Qu Wenruo4c664612021-09-15 15:17:16 +0800794 btrfs_put_bioc(recover->bioc);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800795 kfree(recover);
796 }
797}
798
Arne Jansena2de7332011-03-08 14:14:00 +0100799/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400800 * scrub_handle_errored_block gets called when either verification of the
801 * pages failed or the bio failed to read, e.g. with EIO. In the latter
802 * case, this function handles all pages in the bio, even though only one
803 * may be bad.
804 * The goal of this function is to repair the errored block by using the
805 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100806 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400807static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100808{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100809 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100810 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400811 struct btrfs_fs_info *fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400812 u64 logical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400813 unsigned int failed_mirror_index;
814 unsigned int is_metadata;
815 unsigned int have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400816 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
817 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100818 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400819 int mirror_index;
820 int page_num;
821 int success;
Qu Wenruo28d70e22017-04-14 08:35:55 +0800822 bool full_stripe_locked;
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000823 unsigned int nofs_flag;
David Sterba8bb1cf12020-08-17 12:12:38 +0200824 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400825 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100826
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400827 BUG_ON(sblock_to_check->page_count < 1);
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400828 fs_info = sctx->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000829 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
830 /*
831 * if we find an error in a super block, we just report it.
832 * They will get written with the next transaction commit
833 * anyway
834 */
835 spin_lock(&sctx->stat_lock);
836 ++sctx->stat.super_errors;
837 spin_unlock(&sctx->stat_lock);
838 return 0;
839 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100840 logical = sblock_to_check->pagev[0]->logical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100841 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
842 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
843 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400844 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100845 have_csum = sblock_to_check->pagev[0]->have_csum;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100846 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400847
Johannes Thumshirn554aed72021-12-07 06:28:36 -0800848 if (!sctx->is_dev_replace && btrfs_repair_one_zone(fs_info, logical))
849 return 0;
Naohiro Aotaf7ef5282021-02-04 19:22:16 +0900850
Qu Wenruo28d70e22017-04-14 08:35:55 +0800851 /*
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000852 * We must use GFP_NOFS because the scrub task might be waiting for a
853 * worker task executing this function and in turn a transaction commit
854 * might be waiting the scrub task to pause (which needs to wait for all
855 * the worker tasks to complete before pausing).
856 * We do allocations in the workers through insert_full_stripe_lock()
857 * and scrub_add_page_to_wr_bio(), which happens down the call chain of
858 * this function.
859 */
860 nofs_flag = memalloc_nofs_save();
861 /*
Qu Wenruo28d70e22017-04-14 08:35:55 +0800862 * For RAID5/6, race can happen for a different device scrub thread.
863 * For data corruption, Parity and Data threads will both try
864 * to recovery the data.
865 * Race can lead to doubly added csum error, or even unrecoverable
866 * error.
867 */
868 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
869 if (ret < 0) {
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000870 memalloc_nofs_restore(nofs_flag);
Qu Wenruo28d70e22017-04-14 08:35:55 +0800871 spin_lock(&sctx->stat_lock);
872 if (ret == -ENOMEM)
873 sctx->stat.malloc_errors++;
874 sctx->stat.read_errors++;
875 sctx->stat.uncorrectable_errors++;
876 spin_unlock(&sctx->stat_lock);
877 return ret;
878 }
879
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400880 /*
881 * read all mirrors one after the other. This includes to
882 * re-read the extent or metadata block that failed (that was
883 * the cause that this fixup code is called) another time,
Qu Wenruo8df507c2021-04-22 19:02:46 +0800884 * sector by sector this time in order to know which sectors
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400885 * caused I/O errors and which ones are good (for all mirrors).
886 * It is the goal to handle the situation when more than one
887 * mirror contains I/O errors, but the errors do not
888 * overlap, i.e. the data can be repaired by selecting the
Qu Wenruo8df507c2021-04-22 19:02:46 +0800889 * sectors from those mirrors without I/O error on the
890 * particular sectors. One example (with blocks >= 2 * sectorsize)
891 * would be that mirror #1 has an I/O error on the first sector,
892 * the second sector is good, and mirror #2 has an I/O error on
893 * the second sector, but the first sector is good.
894 * Then the first sector of the first mirror can be repaired by
895 * taking the first sector of the second mirror, and the
896 * second sector of the second mirror can be repaired by
897 * copying the contents of the 2nd sector of the 1st mirror.
898 * One more note: if the sectors of one mirror contain I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400899 * errors, the checksum cannot be verified. In order to get
900 * the best data for repairing, the first attempt is to find
901 * a mirror without I/O errors and with a validated checksum.
Qu Wenruo8df507c2021-04-22 19:02:46 +0800902 * Only if this is not possible, the sectors are picked from
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400903 * mirrors with I/O errors without considering the checksum.
904 * If the latter is the case, at the end, the checksum of the
905 * repaired area is verified in order to correctly maintain
906 * the statistics.
907 */
908
David Sterba31e818f2015-02-20 18:00:26 +0100909 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000910 sizeof(*sblocks_for_recheck), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400911 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100912 spin_lock(&sctx->stat_lock);
913 sctx->stat.malloc_errors++;
914 sctx->stat.read_errors++;
915 sctx->stat.uncorrectable_errors++;
916 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100917 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400918 goto out;
919 }
920
921 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800922 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400923 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100924 spin_lock(&sctx->stat_lock);
925 sctx->stat.read_errors++;
926 sctx->stat.uncorrectable_errors++;
927 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100928 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400929 goto out;
930 }
931 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
932 sblock_bad = sblocks_for_recheck + failed_mirror_index;
933
934 /* build and submit the bios for the failed mirror, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +0800935 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400936
937 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
938 sblock_bad->no_io_error_seen) {
939 /*
940 * the error disappeared after reading page by page, or
941 * the area was part of a huge bio and other parts of the
942 * bio caused I/O errors, or the block layer merged several
943 * read requests into one and the error is caused by a
944 * different bio (usually one of the two latter cases is
945 * the cause)
946 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100947 spin_lock(&sctx->stat_lock);
948 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800949 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100950 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400951
Stefan Behrensff023aa2012-11-06 11:43:11 +0100952 if (sctx->is_dev_replace)
953 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400954 goto out;
955 }
956
957 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100958 spin_lock(&sctx->stat_lock);
959 sctx->stat.read_errors++;
960 spin_unlock(&sctx->stat_lock);
David Sterba8bb1cf12020-08-17 12:12:38 +0200961 if (__ratelimit(&rs))
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400962 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100963 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400964 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100965 spin_lock(&sctx->stat_lock);
966 sctx->stat.csum_errors++;
967 spin_unlock(&sctx->stat_lock);
David Sterba8bb1cf12020-08-17 12:12:38 +0200968 if (__ratelimit(&rs))
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400969 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100970 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200971 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400972 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100973 spin_lock(&sctx->stat_lock);
974 sctx->stat.verify_errors++;
975 spin_unlock(&sctx->stat_lock);
David Sterba8bb1cf12020-08-17 12:12:38 +0200976 if (__ratelimit(&rs))
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400977 scrub_print_warning("checksum/header error",
978 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200979 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100980 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200981 BTRFS_DEV_STAT_GENERATION_ERRS);
982 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100983 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200984 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400985 }
986
Ilya Dryomov33ef30a2013-11-03 19:06:38 +0200987 if (sctx->readonly) {
988 ASSERT(!sctx->is_dev_replace);
989 goto out;
990 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400991
Qu Wenruo665d4952018-07-11 13:41:21 +0800992 /*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400993 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +0100994 * checksums.
995 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400996 * errors and also does not have a checksum error.
997 * If one is found, and if a checksum is present, the full block
998 * that is known to contain an error is rewritten. Afterwards
999 * the block is known to be corrected.
1000 * If a mirror is found which is completely correct, and no
1001 * checksum is present, only those pages are rewritten that had
1002 * an I/O error in the block to be repaired, since it cannot be
1003 * determined, which copy of the other pages is better (and it
1004 * could happen otherwise that a correct page would be
1005 * overwritten by a bad one).
1006 */
Liu Bo762221f2018-01-02 13:36:42 -07001007 for (mirror_index = 0; ;mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001008 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001009
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001010 if (mirror_index == failed_mirror_index)
1011 continue;
Liu Bo762221f2018-01-02 13:36:42 -07001012
1013 /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
1014 if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1015 if (mirror_index >= BTRFS_MAX_MIRRORS)
1016 break;
1017 if (!sblocks_for_recheck[mirror_index].page_count)
1018 break;
1019
1020 sblock_other = sblocks_for_recheck + mirror_index;
1021 } else {
1022 struct scrub_recover *r = sblock_bad->pagev[0]->recover;
Qu Wenruo4c664612021-09-15 15:17:16 +08001023 int max_allowed = r->bioc->num_stripes - r->bioc->num_tgtdevs;
Liu Bo762221f2018-01-02 13:36:42 -07001024
1025 if (mirror_index >= max_allowed)
1026 break;
1027 if (!sblocks_for_recheck[1].page_count)
1028 break;
1029
1030 ASSERT(failed_mirror_index == 0);
1031 sblock_other = sblocks_for_recheck + 1;
1032 sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
1033 }
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001034
1035 /* build and submit the bios, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001036 scrub_recheck_block(fs_info, sblock_other, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001037
1038 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001039 !sblock_other->checksum_error &&
1040 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001041 if (sctx->is_dev_replace) {
1042 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001043 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001044 } else {
1045 ret = scrub_repair_block_from_good_copy(
1046 sblock_bad, sblock_other);
1047 if (!ret)
1048 goto corrected_error;
1049 }
Arne Jansena2de7332011-03-08 14:14:00 +01001050 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001051 }
1052
Zhao Leib968fed2015-01-20 15:11:41 +08001053 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1054 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001055
1056 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001057 * In case of I/O errors in the area that is supposed to be
Qu Wenruo8df507c2021-04-22 19:02:46 +08001058 * repaired, continue by picking good copies of those sectors.
1059 * Select the good sectors from mirrors to rewrite bad sectors from
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001060 * the area to fix. Afterwards verify the checksum of the block
1061 * that is supposed to be repaired. This verification step is
1062 * only done for the purpose of statistic counting and for the
1063 * final scrub report, whether errors remain.
1064 * A perfect algorithm could make use of the checksum and try
Qu Wenruo8df507c2021-04-22 19:02:46 +08001065 * all possible combinations of sectors from the different mirrors
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001066 * until the checksum verification succeeds. For example, when
Qu Wenruo8df507c2021-04-22 19:02:46 +08001067 * the 2nd sector of mirror #1 faces I/O errors, and the 2nd sector
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001068 * of mirror #2 is readable but the final checksum test fails,
Qu Wenruo8df507c2021-04-22 19:02:46 +08001069 * then the 2nd sector of mirror #3 could be tried, whether now
Nicholas D Steeves01327612016-05-19 21:18:45 -04001070 * the final checksum succeeds. But this would be a rare
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001071 * exception and is therefore not implemented. At least it is
1072 * avoided that the good copy is overwritten.
1073 * A more useful improvement would be to pick the sectors
1074 * without I/O error based on sector sizes (512 bytes on legacy
Qu Wenruo8df507c2021-04-22 19:02:46 +08001075 * disks) instead of on sectorsize. Then maybe 512 byte of one
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001076 * mirror could be repaired by taking 512 byte of a different
Qu Wenruo8df507c2021-04-22 19:02:46 +08001077 * mirror, even if other 512 byte sectors in the same sectorsize
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001078 * area are unreadable.
1079 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001080 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001081 for (page_num = 0; page_num < sblock_bad->page_count;
1082 page_num++) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001083 struct scrub_page *spage_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001084 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001085
Zhao Leib968fed2015-01-20 15:11:41 +08001086 /* skip no-io-error page in scrub */
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001087 if (!spage_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001088 continue;
1089
Liu Bo47597002018-03-02 16:10:41 -07001090 if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1091 /*
1092 * In case of dev replace, if raid56 rebuild process
1093 * didn't work out correct data, then copy the content
1094 * in sblock_bad to make sure target device is identical
1095 * to source device, instead of writing garbage data in
1096 * sblock_for_recheck array to target device.
1097 */
1098 sblock_other = NULL;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001099 } else if (spage_bad->io_error) {
Liu Bo47597002018-03-02 16:10:41 -07001100 /* try to find no-io-error page in mirrors */
Zhao Leib968fed2015-01-20 15:11:41 +08001101 for (mirror_index = 0;
1102 mirror_index < BTRFS_MAX_MIRRORS &&
1103 sblocks_for_recheck[mirror_index].page_count > 0;
1104 mirror_index++) {
1105 if (!sblocks_for_recheck[mirror_index].
1106 pagev[page_num]->io_error) {
1107 sblock_other = sblocks_for_recheck +
1108 mirror_index;
1109 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001110 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001111 }
Zhao Leib968fed2015-01-20 15:11:41 +08001112 if (!sblock_other)
1113 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001114 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001115
Zhao Leib968fed2015-01-20 15:11:41 +08001116 if (sctx->is_dev_replace) {
1117 /*
1118 * did not find a mirror to fetch the page
1119 * from. scrub_write_page_to_dev_replace()
1120 * handles this case (page->io_error), by
1121 * filling the block with zeros before
1122 * submitting the write request
1123 */
1124 if (!sblock_other)
1125 sblock_other = sblock_bad;
1126
1127 if (scrub_write_page_to_dev_replace(sblock_other,
1128 page_num) != 0) {
David Sterbae37abe92018-04-04 17:20:52 +02001129 atomic64_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001130 &fs_info->dev_replace.num_write_errors);
Zhao Leib968fed2015-01-20 15:11:41 +08001131 success = 0;
1132 }
1133 } else if (sblock_other) {
1134 ret = scrub_repair_page_from_good_copy(sblock_bad,
1135 sblock_other,
1136 page_num, 0);
1137 if (0 == ret)
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001138 spage_bad->io_error = 0;
Zhao Leib968fed2015-01-20 15:11:41 +08001139 else
1140 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001141 }
1142 }
1143
Zhao Leib968fed2015-01-20 15:11:41 +08001144 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001145 if (is_metadata || have_csum) {
1146 /*
1147 * need to verify the checksum now that all
1148 * sectors on disk are repaired (the write
1149 * request for data to be repaired is on its way).
1150 * Just be lazy and use scrub_recheck_block()
1151 * which re-reads the data before the checksum
1152 * is verified, but most likely the data comes out
1153 * of the page cache.
1154 */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001155 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001156 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001157 !sblock_bad->checksum_error &&
1158 sblock_bad->no_io_error_seen)
1159 goto corrected_error;
1160 else
1161 goto did_not_correct_error;
1162 } else {
1163corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001164 spin_lock(&sctx->stat_lock);
1165 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001166 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001167 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001168 btrfs_err_rl_in_rcu(fs_info,
1169 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001170 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001171 }
1172 } else {
1173did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001174 spin_lock(&sctx->stat_lock);
1175 sctx->stat.uncorrectable_errors++;
1176 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001177 btrfs_err_rl_in_rcu(fs_info,
1178 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001179 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001180 }
1181
1182out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001183 if (sblocks_for_recheck) {
1184 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1185 mirror_index++) {
1186 struct scrub_block *sblock = sblocks_for_recheck +
1187 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001188 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001189 int page_index;
1190
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001191 for (page_index = 0; page_index < sblock->page_count;
1192 page_index++) {
1193 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001194 recover = sblock->pagev[page_index]->recover;
1195 if (recover) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001196 scrub_put_recover(fs_info, recover);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001197 sblock->pagev[page_index]->recover =
1198 NULL;
1199 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001200 scrub_page_put(sblock->pagev[page_index]);
1201 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001202 }
1203 kfree(sblocks_for_recheck);
1204 }
1205
Qu Wenruo28d70e22017-04-14 08:35:55 +08001206 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
Filipe Manana7c3c7cb2018-12-07 13:23:32 +00001207 memalloc_nofs_restore(nofs_flag);
Qu Wenruo28d70e22017-04-14 08:35:55 +08001208 if (ret < 0)
1209 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001210 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001211}
1212
Qu Wenruo4c664612021-09-15 15:17:16 +08001213static inline int scrub_nr_raid_mirrors(struct btrfs_io_context *bioc)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001214{
Qu Wenruo4c664612021-09-15 15:17:16 +08001215 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID5)
Zhao Lei10f11902015-01-20 15:11:43 +08001216 return 2;
Qu Wenruo4c664612021-09-15 15:17:16 +08001217 else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID6)
Zhao Lei10f11902015-01-20 15:11:43 +08001218 return 3;
1219 else
Qu Wenruo4c664612021-09-15 15:17:16 +08001220 return (int)bioc->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001221}
1222
Zhao Lei10f11902015-01-20 15:11:43 +08001223static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1224 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001225 u64 mapped_length,
1226 int nstripes, int mirror,
1227 int *stripe_index,
1228 u64 *stripe_offset)
1229{
1230 int i;
1231
Zhao Leiffe2d202015-01-20 15:11:44 +08001232 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001233 /* RAID5/6 */
1234 for (i = 0; i < nstripes; i++) {
1235 if (raid_map[i] == RAID6_Q_STRIPE ||
1236 raid_map[i] == RAID5_P_STRIPE)
1237 continue;
1238
1239 if (logical >= raid_map[i] &&
1240 logical < raid_map[i] + mapped_length)
1241 break;
1242 }
1243
1244 *stripe_index = i;
1245 *stripe_offset = logical - raid_map[i];
1246 } else {
1247 /* The other RAID type */
1248 *stripe_index = mirror;
1249 *stripe_offset = 0;
1250 }
1251}
1252
Zhao Leibe50a8d2015-01-20 15:11:42 +08001253static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001254 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001255{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001256 struct scrub_ctx *sctx = original_sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001257 struct btrfs_fs_info *fs_info = sctx->fs_info;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001258 u64 length = original_sblock->page_count * fs_info->sectorsize;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001259 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001260 u64 generation = original_sblock->pagev[0]->generation;
1261 u64 flags = original_sblock->pagev[0]->flags;
1262 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001263 struct scrub_recover *recover;
Qu Wenruo4c664612021-09-15 15:17:16 +08001264 struct btrfs_io_context *bioc;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001265 u64 sublen;
1266 u64 mapped_length;
1267 u64 stripe_offset;
1268 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001269 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001270 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001271 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001272 int ret;
1273
1274 /*
Zhao Lei57019342015-01-20 15:11:45 +08001275 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001276 * are not used (and not set) in the blocks that are used for
1277 * the recheck procedure
1278 */
1279
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001280 while (length > 0) {
Qu Wenruo8df507c2021-04-22 19:02:46 +08001281 sublen = min_t(u64, length, fs_info->sectorsize);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001282 mapped_length = sublen;
Qu Wenruo4c664612021-09-15 15:17:16 +08001283 bioc = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001284
1285 /*
Qu Wenruo8df507c2021-04-22 19:02:46 +08001286 * With a length of sectorsize, each returned stripe represents
1287 * one mirror
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001288 */
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001289 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001290 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
Qu Wenruo4c664612021-09-15 15:17:16 +08001291 logical, &mapped_length, &bioc);
1292 if (ret || !bioc || mapped_length < sublen) {
1293 btrfs_put_bioc(bioc);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001294 btrfs_bio_counter_dec(fs_info);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001295 return -EIO;
1296 }
1297
Miao Xieaf8e2d12014-10-23 14:42:50 +08001298 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1299 if (!recover) {
Qu Wenruo4c664612021-09-15 15:17:16 +08001300 btrfs_put_bioc(bioc);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001301 btrfs_bio_counter_dec(fs_info);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001302 return -ENOMEM;
1303 }
1304
Elena Reshetova6f615012017-03-03 10:55:21 +02001305 refcount_set(&recover->refs, 1);
Qu Wenruo4c664612021-09-15 15:17:16 +08001306 recover->bioc = bioc;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001307 recover->map_length = mapped_length;
1308
Qu Wenruo0bb3acd2021-12-06 13:52:57 +08001309 ASSERT(page_index < SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001310
Qu Wenruo4c664612021-09-15 15:17:16 +08001311 nmirrors = min(scrub_nr_raid_mirrors(bioc), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001312
Miao Xieaf8e2d12014-10-23 14:42:50 +08001313 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001314 mirror_index++) {
1315 struct scrub_block *sblock;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001316 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001317
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001318 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001319 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001320
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001321 spage = kzalloc(sizeof(*spage), GFP_NOFS);
1322 if (!spage) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001323leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001324 spin_lock(&sctx->stat_lock);
1325 sctx->stat.malloc_errors++;
1326 spin_unlock(&sctx->stat_lock);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001327 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001328 return -ENOMEM;
1329 }
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001330 scrub_page_get(spage);
1331 sblock->pagev[page_index] = spage;
1332 spage->sblock = sblock;
1333 spage->flags = flags;
1334 spage->generation = generation;
1335 spage->logical = logical;
1336 spage->have_csum = have_csum;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001337 if (have_csum)
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001338 memcpy(spage->csum,
Zhao Lei4734b7e2015-08-19 22:39:18 +08001339 original_sblock->pagev[0]->csum,
David Sterba2ae0c2d2020-06-30 17:44:49 +02001340 sctx->fs_info->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001341
Zhao Lei10f11902015-01-20 15:11:43 +08001342 scrub_stripe_index_and_offset(logical,
Qu Wenruo4c664612021-09-15 15:17:16 +08001343 bioc->map_type,
1344 bioc->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001345 mapped_length,
Qu Wenruo4c664612021-09-15 15:17:16 +08001346 bioc->num_stripes -
1347 bioc->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001348 mirror_index,
1349 &stripe_index,
1350 &stripe_offset);
Qu Wenruo4c664612021-09-15 15:17:16 +08001351 spage->physical = bioc->stripes[stripe_index].physical +
Miao Xieaf8e2d12014-10-23 14:42:50 +08001352 stripe_offset;
Qu Wenruo4c664612021-09-15 15:17:16 +08001353 spage->dev = bioc->stripes[stripe_index].dev;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001354
Stefan Behrensff023aa2012-11-06 11:43:11 +01001355 BUG_ON(page_index >= original_sblock->page_count);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001356 spage->physical_for_dev_replace =
Stefan Behrensff023aa2012-11-06 11:43:11 +01001357 original_sblock->pagev[page_index]->
1358 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001359 /* for missing devices, dev->bdev is NULL */
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001360 spage->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001361 sblock->page_count++;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001362 spage->page = alloc_page(GFP_NOFS);
1363 if (!spage->page)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001364 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001365
1366 scrub_get_recover(recover);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001367 spage->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001368 }
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001369 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001370 length -= sublen;
1371 logical += sublen;
1372 page_index++;
1373 }
1374
1375 return 0;
1376}
1377
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001378static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001379{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001380 complete(bio->bi_private);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001381}
1382
Miao Xieaf8e2d12014-10-23 14:42:50 +08001383static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1384 struct bio *bio,
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001385 struct scrub_page *spage)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001386{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001387 DECLARE_COMPLETION_ONSTACK(done);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001388 int ret;
Liu Bo762221f2018-01-02 13:36:42 -07001389 int mirror_num;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001390
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001391 bio->bi_iter.bi_sector = spage->logical >> 9;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001392 bio->bi_private = &done;
1393 bio->bi_end_io = scrub_bio_wait_endio;
1394
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001395 mirror_num = spage->sblock->pagev[0]->mirror_num;
Qu Wenruo6a258d72021-09-23 14:00:09 +08001396 ret = raid56_parity_recover(bio, spage->recover->bioc,
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001397 spage->recover->map_length,
Liu Bo762221f2018-01-02 13:36:42 -07001398 mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001399 if (ret)
1400 return ret;
1401
Liu Bob4ff5ad2017-11-30 17:26:39 -07001402 wait_for_completion_io(&done);
1403 return blk_status_to_errno(bio->bi_status);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001404}
1405
Liu Bo6ca17652018-03-07 12:08:09 -07001406static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
1407 struct scrub_block *sblock)
1408{
1409 struct scrub_page *first_page = sblock->pagev[0];
1410 struct bio *bio;
1411 int page_num;
1412
1413 /* All pages in sblock belong to the same stripe on the same device. */
1414 ASSERT(first_page->dev);
1415 if (!first_page->dev->bdev)
1416 goto out;
1417
Qu Wenruoc3a3b192021-09-15 15:17:18 +08001418 bio = btrfs_bio_alloc(BIO_MAX_VECS);
Liu Bo6ca17652018-03-07 12:08:09 -07001419 bio_set_dev(bio, first_page->dev->bdev);
1420
1421 for (page_num = 0; page_num < sblock->page_count; page_num++) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001422 struct scrub_page *spage = sblock->pagev[page_num];
Liu Bo6ca17652018-03-07 12:08:09 -07001423
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001424 WARN_ON(!spage->page);
1425 bio_add_page(bio, spage->page, PAGE_SIZE, 0);
Liu Bo6ca17652018-03-07 12:08:09 -07001426 }
1427
1428 if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) {
1429 bio_put(bio);
1430 goto out;
1431 }
1432
1433 bio_put(bio);
1434
1435 scrub_recheck_block_checksum(sblock);
1436
1437 return;
1438out:
1439 for (page_num = 0; page_num < sblock->page_count; page_num++)
1440 sblock->pagev[page_num]->io_error = 1;
1441
1442 sblock->no_io_error_seen = 0;
1443}
1444
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001445/*
1446 * this function will check the on disk data for checksum errors, header
1447 * errors and read I/O errors. If any I/O errors happen, the exact pages
1448 * which are errored are marked as being bad. The goal is to enable scrub
1449 * to take those pages that are not errored from all the mirrors so that
1450 * the pages that are errored in the just handled mirror can be repaired.
1451 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001452static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001453 struct scrub_block *sblock,
1454 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001455{
1456 int page_num;
1457
1458 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001459
Liu Bo6ca17652018-03-07 12:08:09 -07001460 /* short cut for raid56 */
1461 if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0]))
1462 return scrub_recheck_block_on_raid56(fs_info, sblock);
1463
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001464 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1465 struct bio *bio;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001466 struct scrub_page *spage = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001467
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001468 if (spage->dev->bdev == NULL) {
1469 spage->io_error = 1;
Stefan Behrensea9947b2012-05-04 15:16:07 -04001470 sblock->no_io_error_seen = 0;
1471 continue;
1472 }
1473
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001474 WARN_ON(!spage->page);
Qu Wenruoc3a3b192021-09-15 15:17:18 +08001475 bio = btrfs_bio_alloc(1);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001476 bio_set_dev(bio, spage->dev->bdev);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001477
Qu Wenruo8df507c2021-04-22 19:02:46 +08001478 bio_add_page(bio, spage->page, fs_info->sectorsize, 0);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001479 bio->bi_iter.bi_sector = spage->physical >> 9;
Liu Bo6ca17652018-03-07 12:08:09 -07001480 bio->bi_opf = REQ_OP_READ;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001481
Liu Bo6ca17652018-03-07 12:08:09 -07001482 if (btrfsic_submit_bio_wait(bio)) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001483 spage->io_error = 1;
Liu Bo6ca17652018-03-07 12:08:09 -07001484 sblock->no_io_error_seen = 0;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001485 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001486
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001487 bio_put(bio);
1488 }
1489
1490 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001491 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001492}
1493
Miao Xie17a9be22014-07-24 11:37:08 +08001494static inline int scrub_check_fsid(u8 fsid[],
1495 struct scrub_page *spage)
1496{
1497 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1498 int ret;
1499
Anand Jain44880fd2017-07-29 17:50:09 +08001500 ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Miao Xie17a9be22014-07-24 11:37:08 +08001501 return !ret;
1502}
1503
Zhao Leiba7cf982015-08-24 21:18:02 +08001504static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001505{
Zhao Leiba7cf982015-08-24 21:18:02 +08001506 sblock->header_error = 0;
1507 sblock->checksum_error = 0;
1508 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001509
Zhao Leiba7cf982015-08-24 21:18:02 +08001510 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1511 scrub_checksum_data(sblock);
1512 else
1513 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001514}
1515
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001516static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001517 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001518{
1519 int page_num;
1520 int ret = 0;
1521
1522 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1523 int ret_sub;
1524
1525 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1526 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001527 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001528 if (ret_sub)
1529 ret = ret_sub;
1530 }
1531
1532 return ret;
1533}
1534
1535static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1536 struct scrub_block *sblock_good,
1537 int page_num, int force_write)
1538{
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001539 struct scrub_page *spage_bad = sblock_bad->pagev[page_num];
1540 struct scrub_page *spage_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001541 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001542 const u32 sectorsize = fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001543
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001544 BUG_ON(spage_bad->page == NULL);
1545 BUG_ON(spage_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001546 if (force_write || sblock_bad->header_error ||
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001547 sblock_bad->checksum_error || spage_bad->io_error) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001548 struct bio *bio;
1549 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001550
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001551 if (!spage_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001552 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001553 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001554 return -EIO;
1555 }
1556
Qu Wenruoc3a3b192021-09-15 15:17:18 +08001557 bio = btrfs_bio_alloc(1);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001558 bio_set_dev(bio, spage_bad->dev->bdev);
1559 bio->bi_iter.bi_sector = spage_bad->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02001560 bio->bi_opf = REQ_OP_WRITE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001561
Qu Wenruo8df507c2021-04-22 19:02:46 +08001562 ret = bio_add_page(bio, spage_good->page, sectorsize, 0);
1563 if (ret != sectorsize) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001564 bio_put(bio);
1565 return -EIO;
1566 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001567
Mike Christie4e49ea42016-06-05 14:31:41 -05001568 if (btrfsic_submit_bio_wait(bio)) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001569 btrfs_dev_stat_inc_and_print(spage_bad->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001570 BTRFS_DEV_STAT_WRITE_ERRS);
David Sterbae37abe92018-04-04 17:20:52 +02001571 atomic64_inc(&fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001572 bio_put(bio);
1573 return -EIO;
1574 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001575 bio_put(bio);
1576 }
1577
1578 return 0;
1579}
1580
Stefan Behrensff023aa2012-11-06 11:43:11 +01001581static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1582{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001583 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001584 int page_num;
1585
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001586 /*
1587 * This block is used for the check of the parity on the source device,
1588 * so the data needn't be written into the destination device.
1589 */
1590 if (sblock->sparity)
1591 return;
1592
Stefan Behrensff023aa2012-11-06 11:43:11 +01001593 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1594 int ret;
1595
1596 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1597 if (ret)
David Sterbae37abe92018-04-04 17:20:52 +02001598 atomic64_inc(&fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001599 }
1600}
1601
1602static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1603 int page_num)
1604{
1605 struct scrub_page *spage = sblock->pagev[page_num];
1606
1607 BUG_ON(spage->page == NULL);
David Sterbaa8b3a892020-05-29 15:26:07 +02001608 if (spage->io_error)
1609 clear_page(page_address(spage->page));
Stefan Behrensff023aa2012-11-06 11:43:11 +01001610
Stefan Behrensff023aa2012-11-06 11:43:11 +01001611 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1612}
1613
Naohiro Aotade17add2021-02-04 19:22:13 +09001614static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
1615{
1616 int ret = 0;
1617 u64 length;
1618
1619 if (!btrfs_is_zoned(sctx->fs_info))
1620 return 0;
1621
Naohiro Aota7db1c5d2021-02-04 19:22:14 +09001622 if (!btrfs_dev_is_sequential(sctx->wr_tgtdev, physical))
1623 return 0;
1624
Naohiro Aotade17add2021-02-04 19:22:13 +09001625 if (sctx->write_pointer < physical) {
1626 length = physical - sctx->write_pointer;
1627
1628 ret = btrfs_zoned_issue_zeroout(sctx->wr_tgtdev,
1629 sctx->write_pointer, length);
1630 if (!ret)
1631 sctx->write_pointer = physical;
1632 }
1633 return ret;
1634}
1635
Stefan Behrensff023aa2012-11-06 11:43:11 +01001636static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1637 struct scrub_page *spage)
1638{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001639 struct scrub_bio *sbio;
1640 int ret;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001641 const u32 sectorsize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001642
David Sterba3fb99302017-05-16 19:10:32 +02001643 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001644again:
David Sterba3fb99302017-05-16 19:10:32 +02001645 if (!sctx->wr_curr_bio) {
1646 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001647 GFP_KERNEL);
David Sterba3fb99302017-05-16 19:10:32 +02001648 if (!sctx->wr_curr_bio) {
1649 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001650 return -ENOMEM;
1651 }
David Sterba3fb99302017-05-16 19:10:32 +02001652 sctx->wr_curr_bio->sctx = sctx;
1653 sctx->wr_curr_bio->page_count = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001654 }
David Sterba3fb99302017-05-16 19:10:32 +02001655 sbio = sctx->wr_curr_bio;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001656 if (sbio->page_count == 0) {
1657 struct bio *bio;
1658
Naohiro Aotade17add2021-02-04 19:22:13 +09001659 ret = fill_writer_pointer_gap(sctx,
1660 spage->physical_for_dev_replace);
1661 if (ret) {
1662 mutex_unlock(&sctx->wr_lock);
1663 return ret;
1664 }
1665
Stefan Behrensff023aa2012-11-06 11:43:11 +01001666 sbio->physical = spage->physical_for_dev_replace;
1667 sbio->logical = spage->logical;
David Sterba3fb99302017-05-16 19:10:32 +02001668 sbio->dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001669 bio = sbio->bio;
1670 if (!bio) {
Qu Wenruoc9d328c2021-12-06 13:52:58 +08001671 bio = btrfs_bio_alloc(sctx->pages_per_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001672 sbio->bio = bio;
1673 }
1674
1675 bio->bi_private = sbio;
1676 bio->bi_end_io = scrub_wr_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001677 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001678 bio->bi_iter.bi_sector = sbio->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02001679 bio->bi_opf = REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001680 sbio->status = 0;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001681 } else if (sbio->physical + sbio->page_count * sectorsize !=
Stefan Behrensff023aa2012-11-06 11:43:11 +01001682 spage->physical_for_dev_replace ||
Qu Wenruo8df507c2021-04-22 19:02:46 +08001683 sbio->logical + sbio->page_count * sectorsize !=
Stefan Behrensff023aa2012-11-06 11:43:11 +01001684 spage->logical) {
1685 scrub_wr_submit(sctx);
1686 goto again;
1687 }
1688
Qu Wenruo8df507c2021-04-22 19:02:46 +08001689 ret = bio_add_page(sbio->bio, spage->page, sectorsize, 0);
1690 if (ret != sectorsize) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001691 if (sbio->page_count < 1) {
1692 bio_put(sbio->bio);
1693 sbio->bio = NULL;
David Sterba3fb99302017-05-16 19:10:32 +02001694 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001695 return -EIO;
1696 }
1697 scrub_wr_submit(sctx);
1698 goto again;
1699 }
1700
1701 sbio->pagev[sbio->page_count] = spage;
1702 scrub_page_get(spage);
1703 sbio->page_count++;
Qu Wenruoc9d328c2021-12-06 13:52:58 +08001704 if (sbio->page_count == sctx->pages_per_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001705 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02001706 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001707
1708 return 0;
1709}
1710
1711static void scrub_wr_submit(struct scrub_ctx *sctx)
1712{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001713 struct scrub_bio *sbio;
1714
David Sterba3fb99302017-05-16 19:10:32 +02001715 if (!sctx->wr_curr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001716 return;
1717
David Sterba3fb99302017-05-16 19:10:32 +02001718 sbio = sctx->wr_curr_bio;
1719 sctx->wr_curr_bio = NULL;
Christoph Hellwig309dca302021-01-24 11:02:34 +01001720 WARN_ON(!sbio->bio->bi_bdev);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001721 scrub_pending_bio_inc(sctx);
1722 /* process all writes in a single worker thread. Then the block layer
1723 * orders the requests before sending them to the driver which
1724 * doubled the write performance on spinning disks when measured
1725 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001726 btrfsic_submit_bio(sbio->bio);
Naohiro Aotade17add2021-02-04 19:22:13 +09001727
1728 if (btrfs_is_zoned(sctx->fs_info))
Qu Wenruo8df507c2021-04-22 19:02:46 +08001729 sctx->write_pointer = sbio->physical + sbio->page_count *
1730 sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001731}
1732
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001733static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001734{
1735 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001736 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001737
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001738 sbio->status = bio->bi_status;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001739 sbio->bio = bio;
1740
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001741 btrfs_init_work(&sbio->work, scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001742 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001743}
1744
1745static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1746{
1747 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1748 struct scrub_ctx *sctx = sbio->sctx;
1749 int i;
1750
Qu Wenruoc9d328c2021-12-06 13:52:58 +08001751 ASSERT(sbio->page_count <= SCRUB_PAGES_PER_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001752 if (sbio->status) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001753 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001754 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001755
1756 for (i = 0; i < sbio->page_count; i++) {
1757 struct scrub_page *spage = sbio->pagev[i];
1758
1759 spage->io_error = 1;
David Sterbae37abe92018-04-04 17:20:52 +02001760 atomic64_inc(&dev_replace->num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001761 }
1762 }
1763
1764 for (i = 0; i < sbio->page_count; i++)
1765 scrub_page_put(sbio->pagev[i]);
1766
1767 bio_put(sbio->bio);
1768 kfree(sbio);
1769 scrub_pending_bio_dec(sctx);
1770}
1771
1772static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001773{
1774 u64 flags;
1775 int ret;
1776
Zhao Leiba7cf982015-08-24 21:18:02 +08001777 /*
1778 * No need to initialize these stats currently,
1779 * because this function only use return value
1780 * instead of these stats value.
1781 *
1782 * Todo:
1783 * always use stats
1784 */
1785 sblock->header_error = 0;
1786 sblock->generation_error = 0;
1787 sblock->checksum_error = 0;
1788
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001789 WARN_ON(sblock->page_count < 1);
1790 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001791 ret = 0;
1792 if (flags & BTRFS_EXTENT_FLAG_DATA)
1793 ret = scrub_checksum_data(sblock);
1794 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1795 ret = scrub_checksum_tree_block(sblock);
1796 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1797 (void)scrub_checksum_super(sblock);
1798 else
1799 WARN_ON(1);
1800 if (ret)
1801 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001802
1803 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001804}
1805
1806static int scrub_checksum_data(struct scrub_block *sblock)
1807{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001808 struct scrub_ctx *sctx = sblock->sctx;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001809 struct btrfs_fs_info *fs_info = sctx->fs_info;
1810 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Arne Jansena2de7332011-03-08 14:14:00 +01001811 u8 csum[BTRFS_CSUM_SIZE];
David Sterbad41ebef2020-05-29 16:20:35 +02001812 struct scrub_page *spage;
David Sterbab0485252020-05-29 15:32:51 +02001813 char *kaddr;
Arne Jansena2de7332011-03-08 14:14:00 +01001814
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001815 BUG_ON(sblock->page_count < 1);
David Sterbad41ebef2020-05-29 16:20:35 +02001816 spage = sblock->pagev[0];
1817 if (!spage->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001818 return 0;
1819
David Sterbad41ebef2020-05-29 16:20:35 +02001820 kaddr = page_address(spage->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001821
David Sterba771aba02020-05-29 15:54:41 +02001822 shash->tfm = fs_info->csum_shash;
1823 crypto_shash_init(shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001824
Qu Wenruob29dca42020-12-02 14:48:10 +08001825 /*
1826 * In scrub_pages() and scrub_pages_for_parity() we ensure each spage
1827 * only contains one sector of data.
1828 */
1829 crypto_shash_digest(shash, kaddr, fs_info->sectorsize, csum);
1830
1831 if (memcmp(csum, spage->csum, fs_info->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001832 sblock->checksum_error = 1;
Zhao Leiba7cf982015-08-24 21:18:02 +08001833 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001834}
1835
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001836static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001837{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001838 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001839 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001840 struct btrfs_fs_info *fs_info = sctx->fs_info;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001841 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001842 u8 calculated_csum[BTRFS_CSUM_SIZE];
1843 u8 on_disk_csum[BTRFS_CSUM_SIZE];
Qu Wenruo53f32512020-12-02 14:48:09 +08001844 /*
1845 * This is done in sectorsize steps even for metadata as there's a
1846 * constraint for nodesize to be aligned to sectorsize. This will need
1847 * to change so we don't misuse data and metadata units like that.
1848 */
1849 const u32 sectorsize = sctx->fs_info->sectorsize;
1850 const int num_sectors = fs_info->nodesize >> fs_info->sectorsize_bits;
David Sterba521e1022020-05-29 15:54:41 +02001851 int i;
David Sterba100aa5d2020-05-29 16:20:35 +02001852 struct scrub_page *spage;
David Sterbab0485252020-05-29 15:32:51 +02001853 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001854
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001855 BUG_ON(sblock->page_count < 1);
Qu Wenruo53f32512020-12-02 14:48:09 +08001856
1857 /* Each member in pagev is just one block, not a full page */
1858 ASSERT(sblock->page_count == num_sectors);
1859
David Sterba100aa5d2020-05-29 16:20:35 +02001860 spage = sblock->pagev[0];
1861 kaddr = page_address(spage->page);
David Sterbab0485252020-05-29 15:32:51 +02001862 h = (struct btrfs_header *)kaddr;
David Sterba2ae0c2d2020-06-30 17:44:49 +02001863 memcpy(on_disk_csum, h->csum, sctx->fs_info->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001864
1865 /*
1866 * we don't use the getter functions here, as we
1867 * a) don't have an extent buffer and
1868 * b) the page is already kmapped
1869 */
David Sterba100aa5d2020-05-29 16:20:35 +02001870 if (spage->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08001871 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001872
David Sterba100aa5d2020-05-29 16:20:35 +02001873 if (spage->generation != btrfs_stack_header_generation(h)) {
Zhao Leiba7cf982015-08-24 21:18:02 +08001874 sblock->header_error = 1;
1875 sblock->generation_error = 1;
1876 }
Arne Jansena2de7332011-03-08 14:14:00 +01001877
David Sterba100aa5d2020-05-29 16:20:35 +02001878 if (!scrub_check_fsid(h->fsid, spage))
Zhao Leiba7cf982015-08-24 21:18:02 +08001879 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001880
1881 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1882 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08001883 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001884
David Sterba521e1022020-05-29 15:54:41 +02001885 shash->tfm = fs_info->csum_shash;
1886 crypto_shash_init(shash);
1887 crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
Qu Wenruo53f32512020-12-02 14:48:09 +08001888 sectorsize - BTRFS_CSUM_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001889
Qu Wenruo53f32512020-12-02 14:48:09 +08001890 for (i = 1; i < num_sectors; i++) {
David Sterba521e1022020-05-29 15:54:41 +02001891 kaddr = page_address(sblock->pagev[i]->page);
Qu Wenruo53f32512020-12-02 14:48:09 +08001892 crypto_shash_update(shash, kaddr, sectorsize);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001893 }
1894
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001895 crypto_shash_final(shash, calculated_csum);
David Sterba2ae0c2d2020-06-30 17:44:49 +02001896 if (memcmp(calculated_csum, on_disk_csum, sctx->fs_info->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001897 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001898
Zhao Leiba7cf982015-08-24 21:18:02 +08001899 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001900}
1901
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001902static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001903{
1904 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001905 struct scrub_ctx *sctx = sblock->sctx;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001906 struct btrfs_fs_info *fs_info = sctx->fs_info;
1907 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001908 u8 calculated_csum[BTRFS_CSUM_SIZE];
David Sterbac7460542020-05-29 15:47:05 +02001909 struct scrub_page *spage;
David Sterbab0485252020-05-29 15:32:51 +02001910 char *kaddr;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001911 int fail_gen = 0;
1912 int fail_cor = 0;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001913
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001914 BUG_ON(sblock->page_count < 1);
David Sterbac7460542020-05-29 15:47:05 +02001915 spage = sblock->pagev[0];
1916 kaddr = page_address(spage->page);
David Sterbab0485252020-05-29 15:32:51 +02001917 s = (struct btrfs_super_block *)kaddr;
Arne Jansena2de7332011-03-08 14:14:00 +01001918
David Sterbac7460542020-05-29 15:47:05 +02001919 if (spage->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001920 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001921
David Sterbac7460542020-05-29 15:47:05 +02001922 if (spage->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001923 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001924
David Sterbac7460542020-05-29 15:47:05 +02001925 if (!scrub_check_fsid(s->fsid, spage))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001926 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001927
David Sterba83cf6d52020-05-29 15:40:36 +02001928 shash->tfm = fs_info->csum_shash;
1929 crypto_shash_init(shash);
1930 crypto_shash_digest(shash, kaddr + BTRFS_CSUM_SIZE,
1931 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, calculated_csum);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001932
David Sterba2ae0c2d2020-06-30 17:44:49 +02001933 if (memcmp(calculated_csum, s->csum, sctx->fs_info->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001934 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001935
Stefan Behrens442a4f62012-05-25 16:06:08 +02001936 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001937 /*
1938 * if we find an error in a super block, we just report it.
1939 * They will get written with the next transaction commit
1940 * anyway
1941 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001942 spin_lock(&sctx->stat_lock);
1943 ++sctx->stat.super_errors;
1944 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001945 if (fail_cor)
David Sterbac7460542020-05-29 15:47:05 +02001946 btrfs_dev_stat_inc_and_print(spage->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001947 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1948 else
David Sterbac7460542020-05-29 15:47:05 +02001949 btrfs_dev_stat_inc_and_print(spage->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001950 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001951 }
1952
Stefan Behrens442a4f62012-05-25 16:06:08 +02001953 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001954}
1955
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001956static void scrub_block_get(struct scrub_block *sblock)
1957{
Elena Reshetova186debd2017-03-03 10:55:23 +02001958 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001959}
1960
1961static void scrub_block_put(struct scrub_block *sblock)
1962{
Elena Reshetova186debd2017-03-03 10:55:23 +02001963 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001964 int i;
1965
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001966 if (sblock->sparity)
1967 scrub_parity_put(sblock->sparity);
1968
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001969 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001970 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001971 kfree(sblock);
1972 }
1973}
1974
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001975static void scrub_page_get(struct scrub_page *spage)
1976{
Zhao Lei57019342015-01-20 15:11:45 +08001977 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001978}
1979
1980static void scrub_page_put(struct scrub_page *spage)
1981{
Zhao Lei57019342015-01-20 15:11:45 +08001982 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001983 if (spage->page)
1984 __free_page(spage->page);
1985 kfree(spage);
1986 }
1987}
1988
David Sterbaeb3b5052019-10-09 13:58:13 +02001989/*
1990 * Throttling of IO submission, bandwidth-limit based, the timeslice is 1
1991 * second. Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max.
1992 */
1993static void scrub_throttle(struct scrub_ctx *sctx)
1994{
1995 const int time_slice = 1000;
1996 struct scrub_bio *sbio;
1997 struct btrfs_device *device;
1998 s64 delta;
1999 ktime_t now;
2000 u32 div;
2001 u64 bwlimit;
2002
2003 sbio = sctx->bios[sctx->curr];
2004 device = sbio->dev;
2005 bwlimit = READ_ONCE(device->scrub_speed_max);
2006 if (bwlimit == 0)
2007 return;
2008
2009 /*
2010 * Slice is divided into intervals when the IO is submitted, adjust by
2011 * bwlimit and maximum of 64 intervals.
2012 */
2013 div = max_t(u32, 1, (u32)(bwlimit / (16 * 1024 * 1024)));
2014 div = min_t(u32, 64, div);
2015
2016 /* Start new epoch, set deadline */
2017 now = ktime_get();
2018 if (sctx->throttle_deadline == 0) {
2019 sctx->throttle_deadline = ktime_add_ms(now, time_slice / div);
2020 sctx->throttle_sent = 0;
2021 }
2022
2023 /* Still in the time to send? */
2024 if (ktime_before(now, sctx->throttle_deadline)) {
2025 /* If current bio is within the limit, send it */
2026 sctx->throttle_sent += sbio->bio->bi_iter.bi_size;
2027 if (sctx->throttle_sent <= div_u64(bwlimit, div))
2028 return;
2029
2030 /* We're over the limit, sleep until the rest of the slice */
2031 delta = ktime_ms_delta(sctx->throttle_deadline, now);
2032 } else {
2033 /* New request after deadline, start new epoch */
2034 delta = 0;
2035 }
2036
2037 if (delta) {
2038 long timeout;
2039
2040 timeout = div_u64(delta * HZ, 1000);
2041 schedule_timeout_interruptible(timeout);
2042 }
2043
2044 /* Next call will start the deadline period */
2045 sctx->throttle_deadline = 0;
2046}
2047
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002048static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002049{
2050 struct scrub_bio *sbio;
2051
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002052 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002053 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002054
David Sterbaeb3b5052019-10-09 13:58:13 +02002055 scrub_throttle(sctx);
2056
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002057 sbio = sctx->bios[sctx->curr];
2058 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002059 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002060 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002061}
2062
Stefan Behrensff023aa2012-11-06 11:43:11 +01002063static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2064 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002065{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002066 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002067 struct scrub_bio *sbio;
Qu Wenruo8df507c2021-04-22 19:02:46 +08002068 const u32 sectorsize = sctx->fs_info->sectorsize;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002069 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002070
2071again:
2072 /*
2073 * grab a fresh bio or wait for one to become available
2074 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002075 while (sctx->curr == -1) {
2076 spin_lock(&sctx->list_lock);
2077 sctx->curr = sctx->first_free;
2078 if (sctx->curr != -1) {
2079 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2080 sctx->bios[sctx->curr]->next_free = -1;
2081 sctx->bios[sctx->curr]->page_count = 0;
2082 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002083 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002084 spin_unlock(&sctx->list_lock);
2085 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002086 }
2087 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002088 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002089 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002090 struct bio *bio;
2091
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002092 sbio->physical = spage->physical;
2093 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002094 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002095 bio = sbio->bio;
2096 if (!bio) {
Qu Wenruoc9d328c2021-12-06 13:52:58 +08002097 bio = btrfs_bio_alloc(sctx->pages_per_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002098 sbio->bio = bio;
2099 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002100
2101 bio->bi_private = sbio;
2102 bio->bi_end_io = scrub_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002103 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002104 bio->bi_iter.bi_sector = sbio->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02002105 bio->bi_opf = REQ_OP_READ;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002106 sbio->status = 0;
Qu Wenruo8df507c2021-04-22 19:02:46 +08002107 } else if (sbio->physical + sbio->page_count * sectorsize !=
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002108 spage->physical ||
Qu Wenruo8df507c2021-04-22 19:02:46 +08002109 sbio->logical + sbio->page_count * sectorsize !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002110 spage->logical ||
2111 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002112 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002113 goto again;
2114 }
2115
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002116 sbio->pagev[sbio->page_count] = spage;
Qu Wenruo8df507c2021-04-22 19:02:46 +08002117 ret = bio_add_page(sbio->bio, spage->page, sectorsize, 0);
2118 if (ret != sectorsize) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002119 if (sbio->page_count < 1) {
2120 bio_put(sbio->bio);
2121 sbio->bio = NULL;
2122 return -EIO;
2123 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002124 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002125 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002126 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002127
Stefan Behrensff023aa2012-11-06 11:43:11 +01002128 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002129 atomic_inc(&sblock->outstanding_pages);
2130 sbio->page_count++;
Qu Wenruoc9d328c2021-12-06 13:52:58 +08002131 if (sbio->page_count == sctx->pages_per_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002132 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002133
2134 return 0;
2135}
2136
Linus Torvalds22365972015-09-05 15:14:43 -07002137static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002138{
2139 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002140 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002141
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002142 if (bio->bi_status)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002143 sblock->no_io_error_seen = 0;
2144
Scott Talbert46732722016-05-09 09:14:28 -04002145 bio_put(bio);
2146
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002147 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2148}
2149
2150static void scrub_missing_raid56_worker(struct btrfs_work *work)
2151{
2152 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2153 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002154 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002155 u64 logical;
2156 struct btrfs_device *dev;
2157
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002158 logical = sblock->pagev[0]->logical;
2159 dev = sblock->pagev[0]->dev;
2160
Zhao Leiaffe4a52015-08-24 21:32:06 +08002161 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002162 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002163
2164 if (!sblock->no_io_error_seen) {
2165 spin_lock(&sctx->stat_lock);
2166 sctx->stat.read_errors++;
2167 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002168 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002169 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002170 logical, rcu_str_deref(dev->name));
2171 } else if (sblock->header_error || sblock->checksum_error) {
2172 spin_lock(&sctx->stat_lock);
2173 sctx->stat.uncorrectable_errors++;
2174 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002175 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002176 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002177 logical, rcu_str_deref(dev->name));
2178 } else {
2179 scrub_write_block_to_dev_replace(sblock);
2180 }
2181
David Sterba2073c4c2017-03-31 17:12:51 +02002182 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002183 mutex_lock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002184 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002185 mutex_unlock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002186 }
2187
Omar Sandoval57d4f0b2019-09-16 11:30:56 -07002188 scrub_block_put(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002189 scrub_pending_bio_dec(sctx);
2190}
2191
2192static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2193{
2194 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002195 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002196 u64 length = sblock->page_count * PAGE_SIZE;
2197 u64 logical = sblock->pagev[0]->logical;
Qu Wenruo4c664612021-09-15 15:17:16 +08002198 struct btrfs_io_context *bioc = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002199 struct bio *bio;
2200 struct btrfs_raid_bio *rbio;
2201 int ret;
2202 int i;
2203
Qu Wenruoae6529c2017-03-29 09:33:21 +08002204 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002205 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
Qu Wenruo4c664612021-09-15 15:17:16 +08002206 &length, &bioc);
2207 if (ret || !bioc || !bioc->raid_map)
2208 goto bioc_out;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002209
2210 if (WARN_ON(!sctx->is_dev_replace ||
Qu Wenruo4c664612021-09-15 15:17:16 +08002211 !(bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002212 /*
2213 * We shouldn't be scrubbing a missing device. Even for dev
2214 * replace, we should only get here for RAID 5/6. We either
2215 * managed to mount something with no mirrors remaining or
2216 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2217 */
Qu Wenruo4c664612021-09-15 15:17:16 +08002218 goto bioc_out;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002219 }
2220
Qu Wenruoc3a3b192021-09-15 15:17:18 +08002221 bio = btrfs_bio_alloc(BIO_MAX_VECS);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002222 bio->bi_iter.bi_sector = logical >> 9;
2223 bio->bi_private = sblock;
2224 bio->bi_end_io = scrub_missing_raid56_end_io;
2225
Qu Wenruo6a258d72021-09-23 14:00:09 +08002226 rbio = raid56_alloc_missing_rbio(bio, bioc, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002227 if (!rbio)
2228 goto rbio_out;
2229
2230 for (i = 0; i < sblock->page_count; i++) {
2231 struct scrub_page *spage = sblock->pagev[i];
2232
2233 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2234 }
2235
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002236 btrfs_init_work(&sblock->work, scrub_missing_raid56_worker, NULL, NULL);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002237 scrub_block_get(sblock);
2238 scrub_pending_bio_inc(sctx);
2239 raid56_submit_missing_rbio(rbio);
2240 return;
2241
2242rbio_out:
2243 bio_put(bio);
Qu Wenruo4c664612021-09-15 15:17:16 +08002244bioc_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002245 btrfs_bio_counter_dec(fs_info);
Qu Wenruo4c664612021-09-15 15:17:16 +08002246 btrfs_put_bioc(bioc);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002247 spin_lock(&sctx->stat_lock);
2248 sctx->stat.malloc_errors++;
2249 spin_unlock(&sctx->stat_lock);
2250}
2251
Qu Wenruofa485d22020-12-02 14:48:07 +08002252static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u32 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002253 u64 physical, struct btrfs_device *dev, u64 flags,
Qu Wenruo96e63a42020-11-03 21:31:02 +08002254 u64 gen, int mirror_num, u8 *csum,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002255 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002256{
2257 struct scrub_block *sblock;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002258 const u32 sectorsize = sctx->fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002259 int index;
2260
David Sterba58c4e172016-02-11 10:49:42 +01002261 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002262 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002263 spin_lock(&sctx->stat_lock);
2264 sctx->stat.malloc_errors++;
2265 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002266 return -ENOMEM;
2267 }
2268
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002269 /* one ref inside this function, plus one for each page added to
2270 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002271 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002272 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002273 sblock->no_io_error_seen = 1;
2274
2275 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002276 struct scrub_page *spage;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002277 /*
2278 * Here we will allocate one page for one sector to scrub.
2279 * This is fine if PAGE_SIZE == sectorsize, but will cost
2280 * more memory for PAGE_SIZE > sectorsize case.
2281 */
2282 u32 l = min(sectorsize, len);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002283
David Sterba58c4e172016-02-11 10:49:42 +01002284 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002285 if (!spage) {
2286leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002287 spin_lock(&sctx->stat_lock);
2288 sctx->stat.malloc_errors++;
2289 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002290 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002291 return -ENOMEM;
2292 }
Qu Wenruo0bb3acd2021-12-06 13:52:57 +08002293 ASSERT(index < SCRUB_MAX_PAGES_PER_BLOCK);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002294 scrub_page_get(spage);
2295 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002296 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002297 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002298 spage->flags = flags;
2299 spage->generation = gen;
2300 spage->logical = logical;
2301 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002302 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002303 spage->mirror_num = mirror_num;
2304 if (csum) {
2305 spage->have_csum = 1;
David Sterba2ae0c2d2020-06-30 17:44:49 +02002306 memcpy(spage->csum, csum, sctx->fs_info->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002307 } else {
2308 spage->have_csum = 0;
2309 }
2310 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002311 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002312 if (!spage->page)
2313 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002314 len -= l;
2315 logical += l;
2316 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002317 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002318 }
2319
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002320 WARN_ON(sblock->page_count == 0);
Anand Jaine6e674b2017-12-04 12:54:54 +08002321 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002322 /*
2323 * This case should only be hit for RAID 5/6 device replace. See
2324 * the comment in scrub_missing_raid56_pages() for details.
2325 */
2326 scrub_missing_raid56_pages(sblock);
2327 } else {
2328 for (index = 0; index < sblock->page_count; index++) {
2329 struct scrub_page *spage = sblock->pagev[index];
2330 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002331
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002332 ret = scrub_add_page_to_rd_bio(sctx, spage);
2333 if (ret) {
2334 scrub_block_put(sblock);
2335 return ret;
2336 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002337 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002338
Qu Wenruo96e63a42020-11-03 21:31:02 +08002339 if (flags & BTRFS_EXTENT_FLAG_SUPER)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002340 scrub_submit(sctx);
2341 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002342
2343 /* last one frees, either here or in bio completion for last page */
2344 scrub_block_put(sblock);
2345 return 0;
2346}
2347
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002348static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002349{
2350 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002351 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002352
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002353 sbio->status = bio->bi_status;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002354 sbio->bio = bio;
2355
Qu Wenruo0339ef22014-02-28 10:46:17 +08002356 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002357}
2358
2359static void scrub_bio_end_io_worker(struct btrfs_work *work)
2360{
2361 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002362 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002363 int i;
2364
Qu Wenruoc9d328c2021-12-06 13:52:58 +08002365 ASSERT(sbio->page_count <= SCRUB_PAGES_PER_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002366 if (sbio->status) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002367 for (i = 0; i < sbio->page_count; i++) {
2368 struct scrub_page *spage = sbio->pagev[i];
2369
2370 spage->io_error = 1;
2371 spage->sblock->no_io_error_seen = 0;
2372 }
2373 }
2374
2375 /* now complete the scrub_block items that have all pages completed */
2376 for (i = 0; i < sbio->page_count; i++) {
2377 struct scrub_page *spage = sbio->pagev[i];
2378 struct scrub_block *sblock = spage->sblock;
2379
2380 if (atomic_dec_and_test(&sblock->outstanding_pages))
2381 scrub_block_complete(sblock);
2382 scrub_block_put(sblock);
2383 }
2384
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002385 bio_put(sbio->bio);
2386 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002387 spin_lock(&sctx->list_lock);
2388 sbio->next_free = sctx->first_free;
2389 sctx->first_free = sbio->index;
2390 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002391
David Sterba2073c4c2017-03-31 17:12:51 +02002392 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002393 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002394 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002395 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002396 }
2397
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002398 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002399}
2400
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002401static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2402 unsigned long *bitmap,
Qu Wenruofa485d22020-12-02 14:48:07 +08002403 u64 start, u32 len)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002404{
Liu Bo972d7212017-04-03 13:45:33 -07002405 u64 offset;
David Sterba7736b0a2017-03-31 18:02:48 +02002406 u32 nsectors;
David Sterbaab108d92020-07-01 20:45:04 +02002407 u32 sectorsize_bits = sparity->sctx->fs_info->sectorsize_bits;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002408
2409 if (len >= sparity->stripe_len) {
2410 bitmap_set(bitmap, 0, sparity->nsectors);
2411 return;
2412 }
2413
2414 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002415 start = div64_u64_rem(start, sparity->stripe_len, &offset);
David Sterbaab108d92020-07-01 20:45:04 +02002416 offset = offset >> sectorsize_bits;
Qu Wenruofa485d22020-12-02 14:48:07 +08002417 nsectors = len >> sectorsize_bits;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002418
2419 if (offset + nsectors <= sparity->nsectors) {
2420 bitmap_set(bitmap, offset, nsectors);
2421 return;
2422 }
2423
2424 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2425 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2426}
2427
2428static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002429 u64 start, u32 len)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002430{
2431 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2432}
2433
2434static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002435 u64 start, u32 len)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002436{
2437 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2438}
2439
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002440static void scrub_block_complete(struct scrub_block *sblock)
2441{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002442 int corrupted = 0;
2443
Stefan Behrensff023aa2012-11-06 11:43:11 +01002444 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002445 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002446 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002447 } else {
2448 /*
2449 * if has checksum error, write via repair mechanism in
2450 * dev replace case, otherwise write here in dev replace
2451 * case.
2452 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002453 corrupted = scrub_checksum(sblock);
2454 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002455 scrub_write_block_to_dev_replace(sblock);
2456 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002457
2458 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2459 u64 start = sblock->pagev[0]->logical;
2460 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
Qu Wenruo8df507c2021-04-22 19:02:46 +08002461 sblock->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002462
Qu Wenruofa485d22020-12-02 14:48:07 +08002463 ASSERT(end - start <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002464 scrub_parity_mark_sectors_error(sblock->sparity,
2465 start, end - start);
2466 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002467}
2468
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002469static void drop_csum_range(struct scrub_ctx *sctx, struct btrfs_ordered_sum *sum)
2470{
2471 sctx->stat.csum_discards += sum->len >> sctx->fs_info->sectorsize_bits;
2472 list_del(&sum->list);
2473 kfree(sum);
2474}
2475
2476/*
2477 * Find the desired csum for range [logical, logical + sectorsize), and store
2478 * the csum into @csum.
2479 *
2480 * The search source is sctx->csum_list, which is a pre-populated list
David Sterba1a9fd412021-05-21 17:42:23 +02002481 * storing bytenr ordered csum ranges. We're responsible to cleanup any range
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002482 * that is before @logical.
2483 *
2484 * Return 0 if there is no csum for the range.
2485 * Return 1 if there is csum for the range and copied to @csum.
2486 */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002487static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002488{
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002489 bool found = false;
Arne Jansena2de7332011-03-08 14:14:00 +01002490
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002491 while (!list_empty(&sctx->csum_list)) {
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002492 struct btrfs_ordered_sum *sum = NULL;
2493 unsigned long index;
2494 unsigned long num_sectors;
2495
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002496 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002497 struct btrfs_ordered_sum, list);
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002498 /* The current csum range is beyond our range, no csum found */
Arne Jansena2de7332011-03-08 14:14:00 +01002499 if (sum->bytenr > logical)
Arne Jansena2de7332011-03-08 14:14:00 +01002500 break;
2501
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002502 /*
2503 * The current sum is before our bytenr, since scrub is always
2504 * done in bytenr order, the csum will never be used anymore,
2505 * clean it up so that later calls won't bother with the range,
2506 * and continue search the next range.
2507 */
2508 if (sum->bytenr + sum->len <= logical) {
2509 drop_csum_range(sctx, sum);
2510 continue;
2511 }
2512
2513 /* Now the csum range covers our bytenr, copy the csum */
2514 found = true;
2515 index = (logical - sum->bytenr) >> sctx->fs_info->sectorsize_bits;
2516 num_sectors = sum->len >> sctx->fs_info->sectorsize_bits;
2517
2518 memcpy(csum, sum->sums + index * sctx->fs_info->csum_size,
2519 sctx->fs_info->csum_size);
2520
2521 /* Cleanup the range if we're at the end of the csum range */
2522 if (index == num_sectors - 1)
2523 drop_csum_range(sctx, sum);
2524 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002525 }
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002526 if (!found)
Arne Jansena2de7332011-03-08 14:14:00 +01002527 return 0;
Miao Xief51a4a12013-06-19 10:36:09 +08002528 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002529}
2530
2531/* scrub extent tries to collect up to 64 kB for each bio */
Liu Bo6ca17652018-03-07 12:08:09 -07002532static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map,
Qu Wenruofa485d22020-12-02 14:48:07 +08002533 u64 logical, u32 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002534 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002535 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002536{
2537 int ret;
2538 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002539 u32 blocksize;
2540
2541 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002542 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2543 blocksize = map->stripe_len;
2544 else
2545 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002546 spin_lock(&sctx->stat_lock);
2547 sctx->stat.data_extents_scrubbed++;
2548 sctx->stat.data_bytes_scrubbed += len;
2549 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002550 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002551 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2552 blocksize = map->stripe_len;
2553 else
2554 blocksize = sctx->fs_info->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002555 spin_lock(&sctx->stat_lock);
2556 sctx->stat.tree_extents_scrubbed++;
2557 sctx->stat.tree_bytes_scrubbed += len;
2558 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002559 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002560 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002561 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002562 }
Arne Jansena2de7332011-03-08 14:14:00 +01002563
2564 while (len) {
Qu Wenruofa485d22020-12-02 14:48:07 +08002565 u32 l = min(len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002566 int have_csum = 0;
2567
2568 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2569 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002570 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002571 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002572 ++sctx->stat.no_csum;
Arne Jansena2de7332011-03-08 14:14:00 +01002573 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002574 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Qu Wenruo96e63a42020-11-03 21:31:02 +08002575 mirror_num, have_csum ? csum : NULL,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002576 physical_for_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002577 if (ret)
2578 return ret;
2579 len -= l;
2580 logical += l;
2581 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002582 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002583 }
2584 return 0;
2585}
2586
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002587static int scrub_pages_for_parity(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002588 u64 logical, u32 len,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002589 u64 physical, struct btrfs_device *dev,
2590 u64 flags, u64 gen, int mirror_num, u8 *csum)
2591{
2592 struct scrub_ctx *sctx = sparity->sctx;
2593 struct scrub_block *sblock;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002594 const u32 sectorsize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002595 int index;
2596
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002597 ASSERT(IS_ALIGNED(len, sectorsize));
2598
David Sterba58c4e172016-02-11 10:49:42 +01002599 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002600 if (!sblock) {
2601 spin_lock(&sctx->stat_lock);
2602 sctx->stat.malloc_errors++;
2603 spin_unlock(&sctx->stat_lock);
2604 return -ENOMEM;
2605 }
2606
2607 /* one ref inside this function, plus one for each page added to
2608 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002609 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002610 sblock->sctx = sctx;
2611 sblock->no_io_error_seen = 1;
2612 sblock->sparity = sparity;
2613 scrub_parity_get(sparity);
2614
2615 for (index = 0; len > 0; index++) {
2616 struct scrub_page *spage;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002617
David Sterba58c4e172016-02-11 10:49:42 +01002618 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002619 if (!spage) {
2620leave_nomem:
2621 spin_lock(&sctx->stat_lock);
2622 sctx->stat.malloc_errors++;
2623 spin_unlock(&sctx->stat_lock);
2624 scrub_block_put(sblock);
2625 return -ENOMEM;
2626 }
Qu Wenruo0bb3acd2021-12-06 13:52:57 +08002627 ASSERT(index < SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002628 /* For scrub block */
2629 scrub_page_get(spage);
2630 sblock->pagev[index] = spage;
2631 /* For scrub parity */
2632 scrub_page_get(spage);
2633 list_add_tail(&spage->list, &sparity->spages);
2634 spage->sblock = sblock;
2635 spage->dev = dev;
2636 spage->flags = flags;
2637 spage->generation = gen;
2638 spage->logical = logical;
2639 spage->physical = physical;
2640 spage->mirror_num = mirror_num;
2641 if (csum) {
2642 spage->have_csum = 1;
David Sterba2ae0c2d2020-06-30 17:44:49 +02002643 memcpy(spage->csum, csum, sctx->fs_info->csum_size);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002644 } else {
2645 spage->have_csum = 0;
2646 }
2647 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002648 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002649 if (!spage->page)
2650 goto leave_nomem;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002651
2652
2653 /* Iterate over the stripe range in sectorsize steps */
2654 len -= sectorsize;
2655 logical += sectorsize;
2656 physical += sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002657 }
2658
2659 WARN_ON(sblock->page_count == 0);
2660 for (index = 0; index < sblock->page_count; index++) {
2661 struct scrub_page *spage = sblock->pagev[index];
2662 int ret;
2663
2664 ret = scrub_add_page_to_rd_bio(sctx, spage);
2665 if (ret) {
2666 scrub_block_put(sblock);
2667 return ret;
2668 }
2669 }
2670
2671 /* last one frees, either here or in bio completion for last page */
2672 scrub_block_put(sblock);
2673 return 0;
2674}
2675
2676static int scrub_extent_for_parity(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002677 u64 logical, u32 len,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002678 u64 physical, struct btrfs_device *dev,
2679 u64 flags, u64 gen, int mirror_num)
2680{
2681 struct scrub_ctx *sctx = sparity->sctx;
2682 int ret;
2683 u8 csum[BTRFS_CSUM_SIZE];
2684 u32 blocksize;
2685
Anand Jaine6e674b2017-12-04 12:54:54 +08002686 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval4a770892015-06-19 11:52:52 -07002687 scrub_parity_mark_sectors_error(sparity, logical, len);
2688 return 0;
2689 }
2690
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002691 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002692 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002693 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002694 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002695 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002696 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002697 WARN_ON(1);
2698 }
2699
2700 while (len) {
Qu Wenruofa485d22020-12-02 14:48:07 +08002701 u32 l = min(len, blocksize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002702 int have_csum = 0;
2703
2704 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2705 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002706 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002707 if (have_csum == 0)
2708 goto skip;
2709 }
2710 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2711 flags, gen, mirror_num,
2712 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002713 if (ret)
2714 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002715skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002716 len -= l;
2717 logical += l;
2718 physical += l;
2719 }
2720 return 0;
2721}
2722
Wang Shilong3b080b22014-04-01 18:01:43 +08002723/*
2724 * Given a physical address, this will calculate it's
2725 * logical offset. if this is a parity stripe, it will return
2726 * the most left data stripe's logical offset.
2727 *
2728 * return 0 if it is a data stripe, 1 means parity stripe.
2729 */
2730static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002731 struct map_lookup *map, u64 *offset,
2732 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002733{
2734 int i;
2735 int j = 0;
2736 u64 stripe_nr;
2737 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002738 u32 stripe_index;
2739 u32 rot;
David Sterbacff82672019-05-17 11:43:45 +02002740 const int data_stripes = nr_data_stripes(map);
Wang Shilong3b080b22014-04-01 18:01:43 +08002741
David Sterbacff82672019-05-17 11:43:45 +02002742 last_offset = (physical - map->stripes[num].physical) * data_stripes;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002743 if (stripe_start)
2744 *stripe_start = last_offset;
2745
Wang Shilong3b080b22014-04-01 18:01:43 +08002746 *offset = last_offset;
David Sterbacff82672019-05-17 11:43:45 +02002747 for (i = 0; i < data_stripes; i++) {
Wang Shilong3b080b22014-04-01 18:01:43 +08002748 *offset = last_offset + i * map->stripe_len;
2749
Liu Bo42c61ab2017-04-03 13:45:24 -07002750 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbacff82672019-05-17 11:43:45 +02002751 stripe_nr = div_u64(stripe_nr, data_stripes);
Wang Shilong3b080b22014-04-01 18:01:43 +08002752
2753 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002754 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002755 /* calculate which stripe this data locates */
2756 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002757 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002758 if (stripe_index == num)
2759 return 0;
2760 if (stripe_index < num)
2761 j++;
2762 }
2763 *offset = last_offset + j * map->stripe_len;
2764 return 1;
2765}
2766
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002767static void scrub_free_parity(struct scrub_parity *sparity)
2768{
2769 struct scrub_ctx *sctx = sparity->sctx;
2770 struct scrub_page *curr, *next;
2771 int nbits;
2772
2773 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2774 if (nbits) {
2775 spin_lock(&sctx->stat_lock);
2776 sctx->stat.read_errors += nbits;
2777 sctx->stat.uncorrectable_errors += nbits;
2778 spin_unlock(&sctx->stat_lock);
2779 }
2780
2781 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2782 list_del_init(&curr->list);
2783 scrub_page_put(curr);
2784 }
2785
2786 kfree(sparity);
2787}
2788
Zhao Lei20b2e302015-06-04 20:09:15 +08002789static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2790{
2791 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2792 work);
2793 struct scrub_ctx *sctx = sparity->sctx;
2794
2795 scrub_free_parity(sparity);
2796 scrub_pending_bio_dec(sctx);
2797}
2798
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002799static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002800{
2801 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002802 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002803
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002804 if (bio->bi_status)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002805 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2806 sparity->nsectors);
2807
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002808 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002809
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002810 btrfs_init_work(&sparity->work, scrub_parity_bio_endio_worker, NULL,
2811 NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002812 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002813}
2814
2815static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2816{
2817 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002818 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002819 struct bio *bio;
2820 struct btrfs_raid_bio *rbio;
Qu Wenruo4c664612021-09-15 15:17:16 +08002821 struct btrfs_io_context *bioc = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002822 u64 length;
2823 int ret;
2824
2825 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2826 sparity->nsectors))
2827 goto out;
2828
Zhao Leia0dd59d2015-07-21 15:42:26 +08002829 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08002830
2831 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002832 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
Qu Wenruo4c664612021-09-15 15:17:16 +08002833 &length, &bioc);
2834 if (ret || !bioc || !bioc->raid_map)
2835 goto bioc_out;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002836
Qu Wenruoc3a3b192021-09-15 15:17:18 +08002837 bio = btrfs_bio_alloc(BIO_MAX_VECS);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002838 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2839 bio->bi_private = sparity;
2840 bio->bi_end_io = scrub_parity_bio_endio;
2841
Qu Wenruo6a258d72021-09-23 14:00:09 +08002842 rbio = raid56_parity_alloc_scrub_rbio(bio, bioc, length,
2843 sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002844 sparity->dbitmap,
2845 sparity->nsectors);
2846 if (!rbio)
2847 goto rbio_out;
2848
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002849 scrub_pending_bio_inc(sctx);
2850 raid56_parity_submit_scrub_rbio(rbio);
2851 return;
2852
2853rbio_out:
2854 bio_put(bio);
Qu Wenruo4c664612021-09-15 15:17:16 +08002855bioc_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002856 btrfs_bio_counter_dec(fs_info);
Qu Wenruo4c664612021-09-15 15:17:16 +08002857 btrfs_put_bioc(bioc);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002858 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2859 sparity->nsectors);
2860 spin_lock(&sctx->stat_lock);
2861 sctx->stat.malloc_errors++;
2862 spin_unlock(&sctx->stat_lock);
2863out:
2864 scrub_free_parity(sparity);
2865}
2866
2867static inline int scrub_calc_parity_bitmap_len(int nsectors)
2868{
Zhao Leibfca9a62014-12-08 19:55:57 +08002869 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002870}
2871
2872static void scrub_parity_get(struct scrub_parity *sparity)
2873{
Elena Reshetova78a76452017-03-03 10:55:24 +02002874 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002875}
2876
2877static void scrub_parity_put(struct scrub_parity *sparity)
2878{
Elena Reshetova78a76452017-03-03 10:55:24 +02002879 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002880 return;
2881
2882 scrub_parity_check_and_repair(sparity);
2883}
2884
2885static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2886 struct map_lookup *map,
2887 struct btrfs_device *sdev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002888 u64 logic_start,
2889 u64 logic_end)
2890{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002891 struct btrfs_fs_info *fs_info = sctx->fs_info;
Josef Bacik29cbcf42021-11-05 16:45:45 -04002892 struct btrfs_root *root = btrfs_extent_root(fs_info, logic_start);
Josef Bacikfc28b252021-11-05 16:45:48 -04002893 struct btrfs_root *csum_root;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002894 struct btrfs_extent_item *extent;
Qu Wenruo4c664612021-09-15 15:17:16 +08002895 struct btrfs_io_context *bioc = NULL;
Qu Wenruo2522dbe2021-12-14 21:01:43 +08002896 struct btrfs_path *path;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002897 u64 flags;
2898 int ret;
2899 int slot;
2900 struct extent_buffer *l;
2901 struct btrfs_key key;
2902 u64 generation;
2903 u64 extent_logical;
2904 u64 extent_physical;
Qu Wenruofa485d22020-12-02 14:48:07 +08002905 /* Check the comment in scrub_stripe() for why u32 is enough here */
2906 u32 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07002907 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002908 struct btrfs_device *extent_dev;
2909 struct scrub_parity *sparity;
2910 int nsectors;
2911 int bitmap_len;
2912 int extent_mirror_num;
2913 int stop_loop = 0;
2914
Qu Wenruo2522dbe2021-12-14 21:01:43 +08002915 path = btrfs_alloc_path();
2916 if (!path) {
2917 spin_lock(&sctx->stat_lock);
2918 sctx->stat.malloc_errors++;
2919 spin_unlock(&sctx->stat_lock);
2920 return -ENOMEM;
2921 }
2922 path->search_commit_root = 1;
2923 path->skip_locking = 1;
2924
Qu Wenruofa485d22020-12-02 14:48:07 +08002925 ASSERT(map->stripe_len <= U32_MAX);
David Sterbaab108d92020-07-01 20:45:04 +02002926 nsectors = map->stripe_len >> fs_info->sectorsize_bits;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002927 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2928 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2929 GFP_NOFS);
2930 if (!sparity) {
2931 spin_lock(&sctx->stat_lock);
2932 sctx->stat.malloc_errors++;
2933 spin_unlock(&sctx->stat_lock);
Qu Wenruo2522dbe2021-12-14 21:01:43 +08002934 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002935 return -ENOMEM;
2936 }
2937
Qu Wenruofa485d22020-12-02 14:48:07 +08002938 ASSERT(map->stripe_len <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002939 sparity->stripe_len = map->stripe_len;
2940 sparity->nsectors = nsectors;
2941 sparity->sctx = sctx;
2942 sparity->scrub_dev = sdev;
2943 sparity->logic_start = logic_start;
2944 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02002945 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002946 INIT_LIST_HEAD(&sparity->spages);
2947 sparity->dbitmap = sparity->bitmap;
2948 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2949
2950 ret = 0;
2951 while (logic_start < logic_end) {
2952 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2953 key.type = BTRFS_METADATA_ITEM_KEY;
2954 else
2955 key.type = BTRFS_EXTENT_ITEM_KEY;
2956 key.objectid = logic_start;
2957 key.offset = (u64)-1;
2958
2959 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2960 if (ret < 0)
2961 goto out;
2962
2963 if (ret > 0) {
2964 ret = btrfs_previous_extent_item(root, path, 0);
2965 if (ret < 0)
2966 goto out;
2967 if (ret > 0) {
2968 btrfs_release_path(path);
2969 ret = btrfs_search_slot(NULL, root, &key,
2970 path, 0, 0);
2971 if (ret < 0)
2972 goto out;
2973 }
2974 }
2975
2976 stop_loop = 0;
2977 while (1) {
2978 u64 bytes;
2979
2980 l = path->nodes[0];
2981 slot = path->slots[0];
2982 if (slot >= btrfs_header_nritems(l)) {
2983 ret = btrfs_next_leaf(root, path);
2984 if (ret == 0)
2985 continue;
2986 if (ret < 0)
2987 goto out;
2988
2989 stop_loop = 1;
2990 break;
2991 }
2992 btrfs_item_key_to_cpu(l, &key, slot);
2993
Zhao Leid7cad232015-07-22 13:14:48 +08002994 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2995 key.type != BTRFS_METADATA_ITEM_KEY)
2996 goto next;
2997
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002998 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002999 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003000 else
3001 bytes = key.offset;
3002
3003 if (key.objectid + bytes <= logic_start)
3004 goto next;
3005
Zhao Leia0dd59d2015-07-21 15:42:26 +08003006 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003007 stop_loop = 1;
3008 break;
3009 }
3010
3011 while (key.objectid >= logic_start + map->stripe_len)
3012 logic_start += map->stripe_len;
3013
3014 extent = btrfs_item_ptr(l, slot,
3015 struct btrfs_extent_item);
3016 flags = btrfs_extent_flags(l, extent);
3017 generation = btrfs_extent_generation(l, extent);
3018
Zhao Leia323e812015-07-23 12:29:49 +08003019 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3020 (key.objectid < logic_start ||
3021 key.objectid + bytes >
3022 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003023 btrfs_err(fs_info,
3024 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08003025 key.objectid, logic_start);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003026 spin_lock(&sctx->stat_lock);
3027 sctx->stat.uncorrectable_errors++;
3028 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003029 goto next;
3030 }
3031again:
3032 extent_logical = key.objectid;
Qu Wenruofa485d22020-12-02 14:48:07 +08003033 ASSERT(bytes <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003034 extent_len = bytes;
3035
3036 if (extent_logical < logic_start) {
3037 extent_len -= logic_start - extent_logical;
3038 extent_logical = logic_start;
3039 }
3040
3041 if (extent_logical + extent_len >
3042 logic_start + map->stripe_len)
3043 extent_len = logic_start + map->stripe_len -
3044 extent_logical;
3045
3046 scrub_parity_mark_sectors_data(sparity, extent_logical,
3047 extent_len);
3048
Omar Sandoval4a770892015-06-19 11:52:52 -07003049 mapped_length = extent_len;
Qu Wenruo4c664612021-09-15 15:17:16 +08003050 bioc = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003051 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
Qu Wenruo4c664612021-09-15 15:17:16 +08003052 extent_logical, &mapped_length, &bioc,
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003053 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07003054 if (!ret) {
Qu Wenruo4c664612021-09-15 15:17:16 +08003055 if (!bioc || mapped_length < extent_len)
Omar Sandoval4a770892015-06-19 11:52:52 -07003056 ret = -EIO;
3057 }
3058 if (ret) {
Qu Wenruo4c664612021-09-15 15:17:16 +08003059 btrfs_put_bioc(bioc);
Omar Sandoval4a770892015-06-19 11:52:52 -07003060 goto out;
3061 }
Qu Wenruo4c664612021-09-15 15:17:16 +08003062 extent_physical = bioc->stripes[0].physical;
3063 extent_mirror_num = bioc->mirror_num;
3064 extent_dev = bioc->stripes[0].dev;
3065 btrfs_put_bioc(bioc);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003066
Josef Bacikfc28b252021-11-05 16:45:48 -04003067 csum_root = btrfs_csum_root(fs_info, extent_logical);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003068 ret = btrfs_lookup_csums_range(csum_root,
3069 extent_logical,
3070 extent_logical + extent_len - 1,
3071 &sctx->csum_list, 1);
3072 if (ret)
3073 goto out;
3074
3075 ret = scrub_extent_for_parity(sparity, extent_logical,
3076 extent_len,
3077 extent_physical,
3078 extent_dev, flags,
3079 generation,
3080 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003081
3082 scrub_free_csums(sctx);
3083
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003084 if (ret)
3085 goto out;
3086
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003087 if (extent_logical + extent_len <
3088 key.objectid + bytes) {
3089 logic_start += map->stripe_len;
3090
3091 if (logic_start >= logic_end) {
3092 stop_loop = 1;
3093 break;
3094 }
3095
3096 if (logic_start < key.objectid + bytes) {
3097 cond_resched();
3098 goto again;
3099 }
3100 }
3101next:
3102 path->slots[0]++;
3103 }
3104
3105 btrfs_release_path(path);
3106
3107 if (stop_loop)
3108 break;
3109
3110 logic_start += map->stripe_len;
3111 }
3112out:
Qu Wenruofa485d22020-12-02 14:48:07 +08003113 if (ret < 0) {
3114 ASSERT(logic_end - logic_start <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003115 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003116 logic_end - logic_start);
Qu Wenruofa485d22020-12-02 14:48:07 +08003117 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003118 scrub_parity_put(sparity);
3119 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003120 mutex_lock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003121 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003122 mutex_unlock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003123
Qu Wenruo2522dbe2021-12-14 21:01:43 +08003124 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003125 return ret < 0 ? ret : 0;
3126}
3127
Naohiro Aotade17add2021-02-04 19:22:13 +09003128static void sync_replace_for_zoned(struct scrub_ctx *sctx)
3129{
3130 if (!btrfs_is_zoned(sctx->fs_info))
3131 return;
3132
3133 sctx->flush_all_writes = true;
3134 scrub_submit(sctx);
3135 mutex_lock(&sctx->wr_lock);
3136 scrub_wr_submit(sctx);
3137 mutex_unlock(&sctx->wr_lock);
3138
3139 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3140}
3141
Naohiro Aota7db1c5d2021-02-04 19:22:14 +09003142static int sync_write_pointer_for_zoned(struct scrub_ctx *sctx, u64 logical,
3143 u64 physical, u64 physical_end)
3144{
3145 struct btrfs_fs_info *fs_info = sctx->fs_info;
3146 int ret = 0;
3147
3148 if (!btrfs_is_zoned(fs_info))
3149 return 0;
3150
3151 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3152
3153 mutex_lock(&sctx->wr_lock);
3154 if (sctx->write_pointer < physical_end) {
3155 ret = btrfs_sync_zone_write_pointer(sctx->wr_tgtdev, logical,
3156 physical,
3157 sctx->write_pointer);
3158 if (ret)
3159 btrfs_err(fs_info,
3160 "zoned: failed to recover write pointer");
3161 }
3162 mutex_unlock(&sctx->wr_lock);
3163 btrfs_dev_clear_zone_empty(sctx->wr_tgtdev, physical);
3164
3165 return ret;
3166}
3167
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003168static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003169 struct btrfs_block_group *bg,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003170 struct map_lookup *map,
3171 struct btrfs_device *scrub_dev,
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003172 int stripe_index, u64 dev_extent_len)
Arne Jansena2de7332011-03-08 14:14:00 +01003173{
Qu Wenruo2522dbe2021-12-14 21:01:43 +08003174 struct btrfs_path *path;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003175 struct btrfs_fs_info *fs_info = sctx->fs_info;
Josef Bacik29cbcf42021-11-05 16:45:45 -04003176 struct btrfs_root *root;
Josef Bacikfc28b252021-11-05 16:45:48 -04003177 struct btrfs_root *csum_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003178 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003179 struct blk_plug plug;
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003180 const u64 chunk_logical = bg->start;
Arne Jansena2de7332011-03-08 14:14:00 +01003181 u64 flags;
3182 int ret;
3183 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003184 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003185 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003186 u64 physical;
3187 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003188 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003189 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003190 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003191 int mirror_num;
David Sterbae6c11f92016-03-24 18:00:53 +01003192 struct btrfs_key key;
Arne Jansena2de7332011-03-08 14:14:00 +01003193 u64 increment = map->stripe_len;
3194 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003195 u64 extent_logical;
3196 u64 extent_physical;
Qu Wenruofa485d22020-12-02 14:48:07 +08003197 /*
3198 * Unlike chunk length, extent length should never go beyond
3199 * BTRFS_MAX_EXTENT_SIZE, thus u32 is enough here.
3200 */
3201 u32 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003202 u64 stripe_logical;
3203 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003204 struct btrfs_device *extent_dev;
3205 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003206 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003207
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003208 physical = map->stripes[stripe_index].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003209 offset = 0;
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003210 nstripes = div64_u64(dev_extent_len, map->stripe_len);
David Sterba7735cd72019-11-28 15:37:46 +01003211 mirror_num = 1;
3212 increment = map->stripe_len;
Arne Jansena2de7332011-03-08 14:14:00 +01003213 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003214 offset = map->stripe_len * stripe_index;
Arne Jansena2de7332011-03-08 14:14:00 +01003215 increment = map->stripe_len * map->num_stripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003216 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3217 int factor = map->num_stripes / map->sub_stripes;
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003218 offset = map->stripe_len * (stripe_index / map->sub_stripes);
Arne Jansena2de7332011-03-08 14:14:00 +01003219 increment = map->stripe_len * factor;
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003220 mirror_num = stripe_index % map->sub_stripes + 1;
David Sterbac7369b32019-05-31 15:39:31 +02003221 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003222 mirror_num = stripe_index % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003223 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003224 mirror_num = stripe_index % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003225 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003226 get_raid56_logic_offset(physical, stripe_index, map, &offset,
3227 NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003228 increment = map->stripe_len * nr_data_stripes(map);
Arne Jansena2de7332011-03-08 14:14:00 +01003229 }
3230
3231 path = btrfs_alloc_path();
3232 if (!path)
3233 return -ENOMEM;
3234
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003235 /*
3236 * work on commit root. The related disk blocks are static as
3237 * long as COW is applied. This means, it is save to rewrite
3238 * them to repair disk errors without any race conditions
3239 */
Arne Jansena2de7332011-03-08 14:14:00 +01003240 path->search_commit_root = 1;
3241 path->skip_locking = 1;
Qu Wenruodcf62b22021-12-14 21:01:44 +08003242 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003243
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003244 logical = chunk_logical + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003245 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003246 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003247 get_raid56_logic_offset(physical_end, stripe_index,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003248 map, &logic_end, NULL);
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003249 logic_end += chunk_logical;
Wang Shilong3b080b22014-04-01 18:01:43 +08003250 } else {
3251 logic_end = logical + increment * nstripes;
3252 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003253 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003254 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003255 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003256
Josef Bacik29cbcf42021-11-05 16:45:45 -04003257 root = btrfs_extent_root(fs_info, logical);
Josef Bacikfc28b252021-11-05 16:45:48 -04003258 csum_root = btrfs_csum_root(fs_info, logical);
3259
Arne Jansena2de7332011-03-08 14:14:00 +01003260 /*
3261 * collect all data csums for the stripe to avoid seeking during
3262 * the scrub. This might currently (crc32) end up to be about 1MB
3263 */
Arne Jansene7786c32011-05-28 20:58:38 +00003264 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003265
Naohiro Aotade17add2021-02-04 19:22:13 +09003266 if (sctx->is_dev_replace &&
3267 btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) {
3268 mutex_lock(&sctx->wr_lock);
3269 sctx->write_pointer = physical;
3270 mutex_unlock(&sctx->wr_lock);
3271 sctx->flush_all_writes = true;
3272 }
3273
Arne Jansena2de7332011-03-08 14:14:00 +01003274 /*
3275 * now find all extents for each stripe and scrub them
3276 */
Arne Jansena2de7332011-03-08 14:14:00 +01003277 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003278 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003279 /*
3280 * canceled?
3281 */
3282 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003283 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003284 ret = -ECANCELED;
3285 goto out;
3286 }
3287 /*
3288 * check to see if we have to pause
3289 */
3290 if (atomic_read(&fs_info->scrub_pause_req)) {
3291 /* push queued extents */
David Sterba2073c4c2017-03-31 17:12:51 +02003292 sctx->flush_all_writes = true;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003293 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003294 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003295 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003296 mutex_unlock(&sctx->wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003297 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003298 atomic_read(&sctx->bios_in_flight) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003299 sctx->flush_all_writes = false;
Wang Shilong3cb09292013-12-04 21:15:19 +08003300 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003301 }
3302
Zhao Leif2f66a22015-07-21 12:22:29 +08003303 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003304 ret = get_raid56_logic_offset(physical, stripe_index,
3305 map, &logical,
Zhao Leif2f66a22015-07-21 12:22:29 +08003306 &stripe_logical);
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003307 logical += chunk_logical;
Zhao Leif2f66a22015-07-21 12:22:29 +08003308 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003309 /* it is parity strip */
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003310 stripe_logical += chunk_logical;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003311 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003312 ret = scrub_raid56_parity(sctx, map, scrub_dev,
Qu Wenruo2522dbe2021-12-14 21:01:43 +08003313 stripe_logical,
Zhao Leif2f66a22015-07-21 12:22:29 +08003314 stripe_end);
3315 if (ret)
3316 goto out;
3317 goto skip;
3318 }
3319 }
3320
Wang Shilong7c76edb2014-01-12 21:38:32 +08003321 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3322 key.type = BTRFS_METADATA_ITEM_KEY;
3323 else
3324 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003325 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003326 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003327
3328 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3329 if (ret < 0)
3330 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003331
Arne Jansen8c510322011-06-03 10:09:26 +02003332 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003333 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003334 if (ret < 0)
3335 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003336 if (ret > 0) {
3337 /* there's no smaller item, so stick with the
3338 * larger one */
3339 btrfs_release_path(path);
3340 ret = btrfs_search_slot(NULL, root, &key,
3341 path, 0, 0);
3342 if (ret < 0)
3343 goto out;
3344 }
Arne Jansena2de7332011-03-08 14:14:00 +01003345 }
3346
Liu Bo625f1c8d2013-04-27 02:56:57 +00003347 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003348 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003349 u64 bytes;
3350
Arne Jansena2de7332011-03-08 14:14:00 +01003351 l = path->nodes[0];
3352 slot = path->slots[0];
3353 if (slot >= btrfs_header_nritems(l)) {
3354 ret = btrfs_next_leaf(root, path);
3355 if (ret == 0)
3356 continue;
3357 if (ret < 0)
3358 goto out;
3359
Liu Bo625f1c8d2013-04-27 02:56:57 +00003360 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003361 break;
3362 }
3363 btrfs_item_key_to_cpu(l, &key, slot);
3364
Zhao Leid7cad232015-07-22 13:14:48 +08003365 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3366 key.type != BTRFS_METADATA_ITEM_KEY)
3367 goto next;
3368
Josef Bacik3173a182013-03-07 14:22:04 -05003369 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003370 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003371 else
3372 bytes = key.offset;
3373
3374 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003375 goto next;
3376
Liu Bo625f1c8d2013-04-27 02:56:57 +00003377 if (key.objectid >= logical + map->stripe_len) {
3378 /* out of this device extent */
3379 if (key.objectid >= logic_end)
3380 stop_loop = 1;
3381 break;
3382 }
Arne Jansena2de7332011-03-08 14:14:00 +01003383
Filipe Manana2473d242020-05-08 11:01:10 +01003384 /*
3385 * If our block group was removed in the meanwhile, just
3386 * stop scrubbing since there is no point in continuing.
3387 * Continuing would prevent reusing its device extents
3388 * for new block groups for a long time.
3389 */
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003390 spin_lock(&bg->lock);
3391 if (bg->removed) {
3392 spin_unlock(&bg->lock);
Filipe Manana2473d242020-05-08 11:01:10 +01003393 ret = 0;
3394 goto out;
3395 }
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003396 spin_unlock(&bg->lock);
Filipe Manana2473d242020-05-08 11:01:10 +01003397
Arne Jansena2de7332011-03-08 14:14:00 +01003398 extent = btrfs_item_ptr(l, slot,
3399 struct btrfs_extent_item);
3400 flags = btrfs_extent_flags(l, extent);
3401 generation = btrfs_extent_generation(l, extent);
3402
Zhao Leia323e812015-07-23 12:29:49 +08003403 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3404 (key.objectid < logical ||
3405 key.objectid + bytes >
3406 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003407 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003408 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003409 key.objectid, logical);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003410 spin_lock(&sctx->stat_lock);
3411 sctx->stat.uncorrectable_errors++;
3412 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003413 goto next;
3414 }
3415
Liu Bo625f1c8d2013-04-27 02:56:57 +00003416again:
3417 extent_logical = key.objectid;
Qu Wenruofa485d22020-12-02 14:48:07 +08003418 ASSERT(bytes <= U32_MAX);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003419 extent_len = bytes;
3420
Arne Jansena2de7332011-03-08 14:14:00 +01003421 /*
3422 * trim extent to this stripe
3423 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003424 if (extent_logical < logical) {
3425 extent_len -= logical - extent_logical;
3426 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003427 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003428 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003429 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003430 extent_len = logical + map->stripe_len -
3431 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003432 }
3433
Liu Bo625f1c8d2013-04-27 02:56:57 +00003434 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003435 extent_dev = scrub_dev;
3436 extent_mirror_num = mirror_num;
Omar Sandoval32934282018-08-14 11:09:52 -07003437 if (sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003438 scrub_remap_extent(fs_info, extent_logical,
3439 extent_len, &extent_physical,
3440 &extent_dev,
3441 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003442
Filipe Manana89490302020-05-08 11:02:07 +01003443 if (flags & BTRFS_EXTENT_FLAG_DATA) {
3444 ret = btrfs_lookup_csums_range(csum_root,
3445 extent_logical,
3446 extent_logical + extent_len - 1,
3447 &sctx->csum_list, 1);
3448 if (ret)
3449 goto out;
3450 }
Arne Jansena2de7332011-03-08 14:14:00 +01003451
Liu Bo6ca17652018-03-07 12:08:09 -07003452 ret = scrub_extent(sctx, map, extent_logical, extent_len,
Liu Bo625f1c8d2013-04-27 02:56:57 +00003453 extent_physical, extent_dev, flags,
3454 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003455 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003456
3457 scrub_free_csums(sctx);
3458
Liu Bo625f1c8d2013-04-27 02:56:57 +00003459 if (ret)
3460 goto out;
3461
Naohiro Aotade17add2021-02-04 19:22:13 +09003462 if (sctx->is_dev_replace)
3463 sync_replace_for_zoned(sctx);
3464
Liu Bo625f1c8d2013-04-27 02:56:57 +00003465 if (extent_logical + extent_len <
3466 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003467 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003468 /*
3469 * loop until we find next data stripe
3470 * or we have finished all stripes.
3471 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003472loop:
3473 physical += map->stripe_len;
3474 ret = get_raid56_logic_offset(physical,
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003475 stripe_index, map,
3476 &logical, &stripe_logical);
3477 logical += chunk_logical;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003478
3479 if (ret && physical < physical_end) {
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003480 stripe_logical += chunk_logical;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003481 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003482 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003483 ret = scrub_raid56_parity(sctx,
Qu Wenruo2522dbe2021-12-14 21:01:43 +08003484 map, scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003485 stripe_logical,
3486 stripe_end);
3487 if (ret)
3488 goto out;
3489 goto loop;
3490 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003491 } else {
3492 physical += map->stripe_len;
3493 logical += increment;
3494 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003495 if (logical < key.objectid + bytes) {
3496 cond_resched();
3497 goto again;
3498 }
3499
Wang Shilong3b080b22014-04-01 18:01:43 +08003500 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003501 stop_loop = 1;
3502 break;
3503 }
3504 }
Arne Jansena2de7332011-03-08 14:14:00 +01003505next:
3506 path->slots[0]++;
3507 }
Chris Mason71267332011-05-23 06:30:52 -04003508 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003509skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003510 logical += increment;
3511 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003512 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003513 if (stop_loop)
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003514 sctx->stat.last_physical = map->stripes[stripe_index].physical +
3515 dev_extent_len;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003516 else
3517 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003518 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003519 if (stop_loop)
3520 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003521 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003522out:
Arne Jansena2de7332011-03-08 14:14:00 +01003523 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003524 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003525 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003526 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003527 mutex_unlock(&sctx->wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003528
Arne Jansene7786c32011-05-28 20:58:38 +00003529 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003530 btrfs_free_path(path);
Naohiro Aota7db1c5d2021-02-04 19:22:14 +09003531
3532 if (sctx->is_dev_replace && ret >= 0) {
3533 int ret2;
3534
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003535 ret2 = sync_write_pointer_for_zoned(sctx,
3536 chunk_logical + offset,
3537 map->stripes[stripe_index].physical,
3538 physical_end);
Naohiro Aota7db1c5d2021-02-04 19:22:14 +09003539 if (ret2)
3540 ret = ret2;
3541 }
3542
Arne Jansena2de7332011-03-08 14:14:00 +01003543 return ret < 0 ? ret : 0;
3544}
3545
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003546static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Qu Wenruod04fbe12021-12-15 14:59:41 +08003547 struct btrfs_block_group *bg,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003548 struct btrfs_device *scrub_dev,
Filipe Manana020d5b72015-11-19 10:57:20 +00003549 u64 dev_offset,
Qu Wenruod04fbe12021-12-15 14:59:41 +08003550 u64 dev_extent_len)
Arne Jansena2de7332011-03-08 14:14:00 +01003551{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003552 struct btrfs_fs_info *fs_info = sctx->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02003553 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003554 struct map_lookup *map;
3555 struct extent_map *em;
3556 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003557 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003558
David Sterbac8bf1b62019-05-17 11:43:17 +02003559 read_lock(&map_tree->lock);
Qu Wenruod04fbe12021-12-15 14:59:41 +08003560 em = lookup_extent_mapping(map_tree, bg->start, bg->length);
David Sterbac8bf1b62019-05-17 11:43:17 +02003561 read_unlock(&map_tree->lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003562
Filipe Manana020d5b72015-11-19 10:57:20 +00003563 if (!em) {
3564 /*
3565 * Might have been an unused block group deleted by the cleaner
3566 * kthread or relocation.
3567 */
Qu Wenruod04fbe12021-12-15 14:59:41 +08003568 spin_lock(&bg->lock);
3569 if (!bg->removed)
Filipe Manana020d5b72015-11-19 10:57:20 +00003570 ret = -EINVAL;
Qu Wenruod04fbe12021-12-15 14:59:41 +08003571 spin_unlock(&bg->lock);
Filipe Manana020d5b72015-11-19 10:57:20 +00003572
3573 return ret;
3574 }
Qu Wenruod04fbe12021-12-15 14:59:41 +08003575 if (em->start != bg->start)
3576 goto out;
3577 if (em->len < dev_extent_len)
3578 goto out;
Arne Jansena2de7332011-03-08 14:14:00 +01003579
Jeff Mahoney95617d62015-06-03 10:55:48 -04003580 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003581 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003582 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003583 map->stripes[i].physical == dev_offset) {
Qu Wenruo2ae8ae32021-12-15 14:59:42 +08003584 ret = scrub_stripe(sctx, bg, map, scrub_dev, i,
3585 dev_extent_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003586 if (ret)
3587 goto out;
3588 }
3589 }
3590out:
3591 free_extent_map(em);
3592
3593 return ret;
3594}
3595
Naohiro Aotade17add2021-02-04 19:22:13 +09003596static int finish_extent_writes_for_zoned(struct btrfs_root *root,
3597 struct btrfs_block_group *cache)
3598{
3599 struct btrfs_fs_info *fs_info = cache->fs_info;
3600 struct btrfs_trans_handle *trans;
3601
3602 if (!btrfs_is_zoned(fs_info))
3603 return 0;
3604
3605 btrfs_wait_block_group_reservations(cache);
3606 btrfs_wait_nocow_writers(cache);
3607 btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start, cache->length);
3608
3609 trans = btrfs_join_transaction(root);
3610 if (IS_ERR(trans))
3611 return PTR_ERR(trans);
3612 return btrfs_commit_transaction(trans);
3613}
3614
Arne Jansena2de7332011-03-08 14:14:00 +01003615static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003616int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Omar Sandoval32934282018-08-14 11:09:52 -07003617 struct btrfs_device *scrub_dev, u64 start, u64 end)
Arne Jansena2de7332011-03-08 14:14:00 +01003618{
3619 struct btrfs_dev_extent *dev_extent = NULL;
3620 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003621 struct btrfs_fs_info *fs_info = sctx->fs_info;
3622 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003623 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003624 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003625 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003626 int slot;
3627 struct extent_buffer *l;
3628 struct btrfs_key key;
3629 struct btrfs_key found_key;
David Sterba32da53862019-10-29 19:20:18 +01003630 struct btrfs_block_group *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003631 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003632
3633 path = btrfs_alloc_path();
3634 if (!path)
3635 return -ENOMEM;
3636
David Sterbae4058b52015-11-27 16:31:35 +01003637 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003638 path->search_commit_root = 1;
3639 path->skip_locking = 1;
3640
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003641 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003642 key.offset = 0ull;
3643 key.type = BTRFS_DEV_EXTENT_KEY;
3644
Arne Jansena2de7332011-03-08 14:14:00 +01003645 while (1) {
Qu Wenruod04fbe12021-12-15 14:59:41 +08003646 u64 dev_extent_len;
3647
Arne Jansena2de7332011-03-08 14:14:00 +01003648 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3649 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003650 break;
3651 if (ret > 0) {
3652 if (path->slots[0] >=
3653 btrfs_header_nritems(path->nodes[0])) {
3654 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003655 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003656 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003657 if (ret > 0) {
3658 ret = 0;
3659 break;
3660 }
3661 } else {
3662 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003663 }
3664 }
Arne Jansena2de7332011-03-08 14:14:00 +01003665
3666 l = path->nodes[0];
3667 slot = path->slots[0];
3668
3669 btrfs_item_key_to_cpu(l, &found_key, slot);
3670
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003671 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003672 break;
3673
David Sterba962a2982014-06-04 18:41:45 +02003674 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003675 break;
3676
3677 if (found_key.offset >= end)
3678 break;
3679
3680 if (found_key.offset < key.offset)
3681 break;
3682
3683 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Qu Wenruod04fbe12021-12-15 14:59:41 +08003684 dev_extent_len = btrfs_dev_extent_length(l, dev_extent);
Arne Jansena2de7332011-03-08 14:14:00 +01003685
Qu Wenruod04fbe12021-12-15 14:59:41 +08003686 if (found_key.offset + dev_extent_len <= start)
Qu Wenruoced96ed2014-06-19 10:42:51 +08003687 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003688
Arne Jansena2de7332011-03-08 14:14:00 +01003689 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3690
3691 /*
3692 * get a reference on the corresponding block group to prevent
3693 * the chunk from going away while we scrub it
3694 */
3695 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003696
3697 /* some chunks are removed but not committed to disk yet,
3698 * continue scrubbing */
3699 if (!cache)
3700 goto skip;
3701
Naohiro Aota78ce9fc2021-02-04 19:22:11 +09003702 if (sctx->is_dev_replace && btrfs_is_zoned(fs_info)) {
3703 spin_lock(&cache->lock);
3704 if (!cache->to_copy) {
3705 spin_unlock(&cache->lock);
Filipe Manana0dc16ef2021-04-14 14:05:26 +01003706 btrfs_put_block_group(cache);
3707 goto skip;
Naohiro Aota78ce9fc2021-02-04 19:22:11 +09003708 }
3709 spin_unlock(&cache->lock);
3710 }
3711
Zhaolei55e3a602015-08-05 16:43:30 +08003712 /*
Filipe Manana2473d242020-05-08 11:01:10 +01003713 * Make sure that while we are scrubbing the corresponding block
3714 * group doesn't get its logical address and its device extents
3715 * reused for another block group, which can possibly be of a
3716 * different type and different profile. We do this to prevent
3717 * false error detections and crashes due to bogus attempts to
3718 * repair extents.
3719 */
3720 spin_lock(&cache->lock);
3721 if (cache->removed) {
3722 spin_unlock(&cache->lock);
3723 btrfs_put_block_group(cache);
3724 goto skip;
3725 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01003726 btrfs_freeze_block_group(cache);
Filipe Manana2473d242020-05-08 11:01:10 +01003727 spin_unlock(&cache->lock);
3728
3729 /*
Zhaolei55e3a602015-08-05 16:43:30 +08003730 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3731 * to avoid deadlock caused by:
3732 * btrfs_inc_block_group_ro()
3733 * -> btrfs_wait_for_commit()
3734 * -> btrfs_commit_transaction()
3735 * -> btrfs_scrub_pause()
3736 */
3737 scrub_pause_on(fs_info);
Qu Wenruob12de522019-11-15 10:09:00 +08003738
3739 /*
3740 * Don't do chunk preallocation for scrub.
3741 *
3742 * This is especially important for SYSTEM bgs, or we can hit
3743 * -EFBIG from btrfs_finish_chunk_alloc() like:
3744 * 1. The only SYSTEM bg is marked RO.
3745 * Since SYSTEM bg is small, that's pretty common.
3746 * 2. New SYSTEM bg will be allocated
3747 * Due to regular version will allocate new chunk.
3748 * 3. New SYSTEM bg is empty and will get cleaned up
3749 * Before cleanup really happens, it's marked RO again.
3750 * 4. Empty SYSTEM bg get scrubbed
3751 * We go back to 2.
3752 *
3753 * This can easily boost the amount of SYSTEM chunks if cleaner
3754 * thread can't be triggered fast enough, and use up all space
3755 * of btrfs_super_block::sys_chunk_array
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003756 *
3757 * While for dev replace, we need to try our best to mark block
3758 * group RO, to prevent race between:
3759 * - Write duplication
3760 * Contains latest data
3761 * - Scrub copy
3762 * Contains data from commit tree
3763 *
3764 * If target block group is not marked RO, nocow writes can
3765 * be overwritten by scrub copy, causing data corruption.
3766 * So for dev-replace, it's not allowed to continue if a block
3767 * group is not RO.
Qu Wenruob12de522019-11-15 10:09:00 +08003768 */
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003769 ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace);
Naohiro Aotade17add2021-02-04 19:22:13 +09003770 if (!ret && sctx->is_dev_replace) {
3771 ret = finish_extent_writes_for_zoned(root, cache);
3772 if (ret) {
3773 btrfs_dec_block_group_ro(cache);
3774 scrub_pause_off(fs_info);
3775 btrfs_put_block_group(cache);
3776 break;
3777 }
3778 }
3779
Zhaolei76a8efa2015-11-17 18:46:17 +08003780 if (ret == 0) {
3781 ro_set = 1;
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003782 } else if (ret == -ENOSPC && !sctx->is_dev_replace) {
Zhaolei76a8efa2015-11-17 18:46:17 +08003783 /*
3784 * btrfs_inc_block_group_ro return -ENOSPC when it
3785 * failed in creating new chunk for metadata.
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003786 * It is not a problem for scrub, because
Zhaolei76a8efa2015-11-17 18:46:17 +08003787 * metadata are always cowed, and our scrub paused
3788 * commit_transactions.
3789 */
3790 ro_set = 0;
Filipe Manana195a49e2021-02-05 12:55:37 +00003791 } else if (ret == -ETXTBSY) {
3792 btrfs_warn(fs_info,
3793 "skipping scrub of block group %llu due to active swapfile",
3794 cache->start);
3795 scrub_pause_off(fs_info);
3796 ret = 0;
3797 goto skip_unfreeze;
Zhaolei76a8efa2015-11-17 18:46:17 +08003798 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003799 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02003800 "failed setting block group ro: %d", ret);
Filipe Manana6b7304a2020-05-08 11:01:47 +01003801 btrfs_unfreeze_block_group(cache);
Zhaolei55e3a602015-08-05 16:43:30 +08003802 btrfs_put_block_group(cache);
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003803 scrub_pause_off(fs_info);
Zhaolei55e3a602015-08-05 16:43:30 +08003804 break;
3805 }
3806
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003807 /*
3808 * Now the target block is marked RO, wait for nocow writes to
3809 * finish before dev-replace.
3810 * COW is fine, as COW never overwrites extents in commit tree.
3811 */
3812 if (sctx->is_dev_replace) {
3813 btrfs_wait_nocow_writers(cache);
3814 btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start,
3815 cache->length);
3816 }
3817
3818 scrub_pause_off(fs_info);
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003819 down_write(&dev_replace->rwsem);
Qu Wenruod04fbe12021-12-15 14:59:41 +08003820 dev_replace->cursor_right = found_key.offset + dev_extent_len;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003821 dev_replace->cursor_left = found_key.offset;
3822 dev_replace->item_needs_writeback = 1;
David Sterbacb5583d2018-09-07 16:11:23 +02003823 up_write(&dev_replace->rwsem);
3824
Qu Wenruod04fbe12021-12-15 14:59:41 +08003825 ASSERT(cache->start == chunk_offset);
3826 ret = scrub_chunk(sctx, cache, scrub_dev, found_key.offset,
3827 dev_extent_len);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003828
3829 /*
3830 * flush, submit all pending read and write bios, afterwards
3831 * wait for them.
3832 * Note that in the dev replace case, a read request causes
3833 * write requests that are submitted in the read completion
3834 * worker. Therefore in the current situation, it is required
3835 * that all write requests are flushed, so that all read and
3836 * write requests are really completed when bios_in_flight
3837 * changes to 0.
3838 */
David Sterba2073c4c2017-03-31 17:12:51 +02003839 sctx->flush_all_writes = true;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003840 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003841 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003842 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003843 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003844
3845 wait_event(sctx->list_wait,
3846 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003847
3848 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003849
3850 /*
3851 * must be called before we decrease @scrub_paused.
3852 * make sure we don't block transaction commit while
3853 * we are waiting pending workers finished.
3854 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003855 wait_event(sctx->list_wait,
3856 atomic_read(&sctx->workers_pending) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003857 sctx->flush_all_writes = false;
Wang Shilong12cf9372014-02-19 19:24:17 +08003858
Zhaoleib708ce92015-08-05 16:43:29 +08003859 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003860
Naohiro Aota78ce9fc2021-02-04 19:22:11 +09003861 if (sctx->is_dev_replace &&
3862 !btrfs_finish_block_group_to_copy(dev_replace->srcdev,
3863 cache, found_key.offset))
3864 ro_set = 0;
3865
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003866 down_write(&dev_replace->rwsem);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003867 dev_replace->cursor_left = dev_replace->cursor_right;
3868 dev_replace->item_needs_writeback = 1;
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003869 up_write(&dev_replace->rwsem);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003870
Zhaolei76a8efa2015-11-17 18:46:17 +08003871 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003872 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003873
Filipe Manana758f2df2015-11-19 11:45:48 +00003874 /*
3875 * We might have prevented the cleaner kthread from deleting
3876 * this block group if it was already unused because we raced
3877 * and set it to RO mode first. So add it back to the unused
3878 * list, otherwise it might not ever be deleted unless a manual
3879 * balance is triggered or it becomes used and unused again.
3880 */
3881 spin_lock(&cache->lock);
3882 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
David Sterbabf38be62019-10-23 18:48:11 +02003883 cache->used == 0) {
Filipe Manana758f2df2015-11-19 11:45:48 +00003884 spin_unlock(&cache->lock);
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08003885 if (btrfs_test_opt(fs_info, DISCARD_ASYNC))
3886 btrfs_discard_queue_work(&fs_info->discard_ctl,
3887 cache);
3888 else
3889 btrfs_mark_bg_unused(cache);
Filipe Manana758f2df2015-11-19 11:45:48 +00003890 } else {
3891 spin_unlock(&cache->lock);
3892 }
Filipe Manana195a49e2021-02-05 12:55:37 +00003893skip_unfreeze:
Filipe Manana6b7304a2020-05-08 11:01:47 +01003894 btrfs_unfreeze_block_group(cache);
Arne Jansena2de7332011-03-08 14:14:00 +01003895 btrfs_put_block_group(cache);
3896 if (ret)
3897 break;
Omar Sandoval32934282018-08-14 11:09:52 -07003898 if (sctx->is_dev_replace &&
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003899 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003900 ret = -EIO;
3901 break;
3902 }
3903 if (sctx->stat.malloc_errors > 0) {
3904 ret = -ENOMEM;
3905 break;
3906 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003907skip:
Qu Wenruod04fbe12021-12-15 14:59:41 +08003908 key.offset = found_key.offset + dev_extent_len;
Chris Mason71267332011-05-23 06:30:52 -04003909 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003910 }
3911
Arne Jansena2de7332011-03-08 14:14:00 +01003912 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003913
Zhaolei55e3a602015-08-05 16:43:30 +08003914 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003915}
3916
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003917static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3918 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003919{
3920 int i;
3921 u64 bytenr;
3922 u64 gen;
3923 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003924 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003925
Josef Bacik84961532021-10-05 16:35:25 -04003926 if (BTRFS_FS_ERROR(fs_info))
Josef Bacikfbabd4a2020-07-21 10:38:37 -04003927 return -EROFS;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003928
Miao Xie5f546062014-07-24 11:37:09 +08003929 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003930 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08003931 gen = scrub_dev->generation;
3932 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003933 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003934
3935 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3936 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003937 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3938 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003939 break;
Naohiro Aota12659252020-11-10 20:26:14 +09003940 if (!btrfs_check_super_location(scrub_dev, bytenr))
3941 continue;
Arne Jansena2de7332011-03-08 14:14:00 +01003942
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003943 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003944 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Qu Wenruo96e63a42020-11-03 21:31:02 +08003945 NULL, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003946 if (ret)
3947 return ret;
3948 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003949 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003950
3951 return 0;
3952}
3953
Josef Bacike89c4a92020-08-10 11:42:29 -04003954static void scrub_workers_put(struct btrfs_fs_info *fs_info)
3955{
3956 if (refcount_dec_and_mutex_lock(&fs_info->scrub_workers_refcnt,
3957 &fs_info->scrub_lock)) {
3958 struct btrfs_workqueue *scrub_workers = NULL;
3959 struct btrfs_workqueue *scrub_wr_comp = NULL;
3960 struct btrfs_workqueue *scrub_parity = NULL;
3961
3962 scrub_workers = fs_info->scrub_workers;
3963 scrub_wr_comp = fs_info->scrub_wr_completion_workers;
3964 scrub_parity = fs_info->scrub_parity_workers;
3965
3966 fs_info->scrub_workers = NULL;
3967 fs_info->scrub_wr_completion_workers = NULL;
3968 fs_info->scrub_parity_workers = NULL;
3969 mutex_unlock(&fs_info->scrub_lock);
3970
3971 btrfs_destroy_workqueue(scrub_workers);
3972 btrfs_destroy_workqueue(scrub_wr_comp);
3973 btrfs_destroy_workqueue(scrub_parity);
3974 }
3975}
3976
Arne Jansena2de7332011-03-08 14:14:00 +01003977/*
3978 * get a reference count on fs_info->scrub_workers. start worker if necessary
3979 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003980static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3981 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003982{
Josef Bacike89c4a92020-08-10 11:42:29 -04003983 struct btrfs_workqueue *scrub_workers = NULL;
3984 struct btrfs_workqueue *scrub_wr_comp = NULL;
3985 struct btrfs_workqueue *scrub_parity = NULL;
David Sterba6f011052015-02-16 18:34:01 +01003986 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003987 int max_active = fs_info->thread_pool_size;
Josef Bacike89c4a92020-08-10 11:42:29 -04003988 int ret = -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01003989
Josef Bacike89c4a92020-08-10 11:42:29 -04003990 if (refcount_inc_not_zero(&fs_info->scrub_workers_refcnt))
3991 return 0;
Anand Jaineb4318e2019-01-30 14:45:01 +08003992
Josef Bacike89c4a92020-08-10 11:42:29 -04003993 scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub", flags,
3994 is_dev_replace ? 1 : max_active, 4);
3995 if (!scrub_workers)
3996 goto fail_scrub_workers;
3997
3998 scrub_wr_comp = btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
3999 max_active, 2);
4000 if (!scrub_wr_comp)
4001 goto fail_scrub_wr_completion_workers;
4002
4003 scrub_parity = btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
4004 max_active, 2);
4005 if (!scrub_parity)
4006 goto fail_scrub_parity_workers;
4007
4008 mutex_lock(&fs_info->scrub_lock);
Anand Jainff09c4c2019-01-30 14:45:02 +08004009 if (refcount_read(&fs_info->scrub_workers_refcnt) == 0) {
Josef Bacike89c4a92020-08-10 11:42:29 -04004010 ASSERT(fs_info->scrub_workers == NULL &&
4011 fs_info->scrub_wr_completion_workers == NULL &&
4012 fs_info->scrub_parity_workers == NULL);
4013 fs_info->scrub_workers = scrub_workers;
4014 fs_info->scrub_wr_completion_workers = scrub_wr_comp;
4015 fs_info->scrub_parity_workers = scrub_parity;
Anand Jainff09c4c2019-01-30 14:45:02 +08004016 refcount_set(&fs_info->scrub_workers_refcnt, 1);
Josef Bacike89c4a92020-08-10 11:42:29 -04004017 mutex_unlock(&fs_info->scrub_lock);
4018 return 0;
Arne Jansen632dd772011-06-10 12:07:07 +02004019 }
Josef Bacike89c4a92020-08-10 11:42:29 -04004020 /* Other thread raced in and created the workers for us */
4021 refcount_inc(&fs_info->scrub_workers_refcnt);
4022 mutex_unlock(&fs_info->scrub_lock);
Zhao Leie82afc52015-06-12 20:36:58 +08004023
Josef Bacike89c4a92020-08-10 11:42:29 -04004024 ret = 0;
4025 btrfs_destroy_workqueue(scrub_parity);
Zhao Leie82afc52015-06-12 20:36:58 +08004026fail_scrub_parity_workers:
Josef Bacike89c4a92020-08-10 11:42:29 -04004027 btrfs_destroy_workqueue(scrub_wr_comp);
Zhao Leie82afc52015-06-12 20:36:58 +08004028fail_scrub_wr_completion_workers:
Josef Bacike89c4a92020-08-10 11:42:29 -04004029 btrfs_destroy_workqueue(scrub_workers);
Zhao Leie82afc52015-06-12 20:36:58 +08004030fail_scrub_workers:
Josef Bacike89c4a92020-08-10 11:42:29 -04004031 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01004032}
4033
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004034int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
4035 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004036 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004037{
Josef Bacik562d7b12021-10-05 16:12:42 -04004038 struct btrfs_dev_lookup_args args = { .devid = devid };
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004039 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004040 int ret;
4041 struct btrfs_device *dev;
Filipe Mananaa5fb1142018-11-26 20:07:17 +00004042 unsigned int nofs_flag;
Arne Jansena2de7332011-03-08 14:14:00 +01004043
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004044 if (btrfs_fs_closing(fs_info))
David Sterba6c3abed2019-02-25 19:57:41 +01004045 return -EAGAIN;
Arne Jansena2de7332011-03-08 14:14:00 +01004046
Jeff Mahoneyda170662016-06-15 09:22:56 -04004047 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004048 /*
4049 * in this case scrub is unable to calculate the checksum
4050 * the way scrub is implemented. Do not handle this
4051 * situation at all because it won't ever happen.
4052 */
Frank Holtonefe120a2013-12-20 11:37:06 -05004053 btrfs_err(fs_info,
4054 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004055 fs_info->nodesize,
4056 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004057 return -EINVAL;
4058 }
4059
Jeff Mahoneyda170662016-06-15 09:22:56 -04004060 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004061 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04004062 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004063 /*
4064 * would exhaust the array bounds of pagev member in
4065 * struct scrub_block
4066 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004067 btrfs_err(fs_info,
4068 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004069 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004070 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004071 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004072 SCRUB_MAX_PAGES_PER_BLOCK);
4073 return -EINVAL;
4074 }
4075
David Sterba0e94c4f42018-12-04 16:11:56 +01004076 /* Allocate outside of device_list_mutex */
4077 sctx = scrub_setup_ctx(fs_info, is_dev_replace);
4078 if (IS_ERR(sctx))
4079 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004080
Josef Bacike89c4a92020-08-10 11:42:29 -04004081 ret = scrub_workers_get(fs_info, is_dev_replace);
4082 if (ret)
4083 goto out_free_ctx;
4084
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004085 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik562d7b12021-10-05 16:12:42 -04004086 dev = btrfs_find_device(fs_info->fs_devices, &args);
Anand Jaine6e674b2017-12-04 12:54:54 +08004087 if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
4088 !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004089 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01004090 ret = -ENODEV;
Josef Bacike89c4a92020-08-10 11:42:29 -04004091 goto out;
Arne Jansena2de7332011-03-08 14:14:00 +01004092 }
Arne Jansena2de7332011-03-08 14:14:00 +01004093
Anand Jainebbede42017-12-04 12:54:52 +08004094 if (!is_dev_replace && !readonly &&
4095 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Miao Xie5d68da32014-07-24 11:37:07 +08004096 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterbaa4852cf2020-07-09 11:25:40 +02004097 btrfs_err_in_rcu(fs_info,
4098 "scrub on devid %llu: filesystem on %s is not writable",
4099 devid, rcu_str_deref(dev->name));
David Sterba0e94c4f42018-12-04 16:11:56 +01004100 ret = -EROFS;
Josef Bacike89c4a92020-08-10 11:42:29 -04004101 goto out;
Miao Xie5d68da32014-07-24 11:37:07 +08004102 }
4103
Wang Shilong3b7a0162013-10-12 02:11:12 +08004104 mutex_lock(&fs_info->scrub_lock);
Anand Jaine12c9622017-12-04 12:54:53 +08004105 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004106 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004107 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004108 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01004109 ret = -EIO;
Josef Bacike89c4a92020-08-10 11:42:29 -04004110 goto out;
Arne Jansena2de7332011-03-08 14:14:00 +01004111 }
4112
David Sterbacb5583d2018-09-07 16:11:23 +02004113 down_read(&fs_info->dev_replace.rwsem);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004114 if (dev->scrub_ctx ||
Stefan Behrens8dabb742012-11-06 13:15:27 +01004115 (!is_dev_replace &&
4116 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
David Sterbacb5583d2018-09-07 16:11:23 +02004117 up_read(&fs_info->dev_replace.rwsem);
Arne Jansena2de7332011-03-08 14:14:00 +01004118 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004119 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01004120 ret = -EINPROGRESS;
Josef Bacike89c4a92020-08-10 11:42:29 -04004121 goto out;
Arne Jansena2de7332011-03-08 14:14:00 +01004122 }
David Sterbacb5583d2018-09-07 16:11:23 +02004123 up_read(&fs_info->dev_replace.rwsem);
Wang Shilong3b7a0162013-10-12 02:11:12 +08004124
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004125 sctx->readonly = readonly;
Anand Jaincadbc0a2018-01-03 16:08:30 +08004126 dev->scrub_ctx = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08004127 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004128
Wang Shilong3cb09292013-12-04 21:15:19 +08004129 /*
4130 * checking @scrub_pause_req here, we can avoid
4131 * race between committing transaction and scrubbing.
4132 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08004133 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004134 atomic_inc(&fs_info->scrubs_running);
4135 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004136
Filipe Mananaa5fb1142018-11-26 20:07:17 +00004137 /*
4138 * In order to avoid deadlock with reclaim when there is a transaction
4139 * trying to pause scrub, make sure we use GFP_NOFS for all the
4140 * allocations done at btrfs_scrub_pages() and scrub_pages_for_parity()
4141 * invoked by our callees. The pausing request is done when the
4142 * transaction commit starts, and it blocks the transaction until scrub
4143 * is paused (done at specific points at scrub_stripe() or right above
4144 * before incrementing fs_info->scrubs_running).
4145 */
4146 nofs_flag = memalloc_nofs_save();
Stefan Behrensff023aa2012-11-06 11:43:11 +01004147 if (!is_dev_replace) {
Anand Jaind1e14422019-01-03 16:17:40 +08004148 btrfs_info(fs_info, "scrub: started on devid %llu", devid);
Wang Shilong9b011ad2013-10-25 19:12:02 +08004149 /*
4150 * by holding device list mutex, we can
4151 * kick off writing super in log tree sync.
4152 */
Wang Shilong3cb09292013-12-04 21:15:19 +08004153 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004154 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08004155 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004156 }
Arne Jansena2de7332011-03-08 14:14:00 +01004157
4158 if (!ret)
Omar Sandoval32934282018-08-14 11:09:52 -07004159 ret = scrub_enumerate_chunks(sctx, dev, start, end);
Filipe Mananaa5fb1142018-11-26 20:07:17 +00004160 memalloc_nofs_restore(nofs_flag);
Arne Jansena2de7332011-03-08 14:14:00 +01004161
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004162 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004163 atomic_dec(&fs_info->scrubs_running);
4164 wake_up(&fs_info->scrub_pause_wait);
4165
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004166 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02004167
Arne Jansena2de7332011-03-08 14:14:00 +01004168 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004169 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01004170
Anand Jaind1e14422019-01-03 16:17:40 +08004171 if (!is_dev_replace)
4172 btrfs_info(fs_info, "scrub: %s on devid %llu with status: %d",
4173 ret ? "not finished" : "finished", devid, ret);
4174
Arne Jansena2de7332011-03-08 14:14:00 +01004175 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004176 dev->scrub_ctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004177 mutex_unlock(&fs_info->scrub_lock);
4178
Josef Bacike89c4a92020-08-10 11:42:29 -04004179 scrub_workers_put(fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +00004180 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004181
4182 return ret;
Josef Bacike89c4a92020-08-10 11:42:29 -04004183out:
4184 scrub_workers_put(fs_info);
David Sterba0e94c4f42018-12-04 16:11:56 +01004185out_free_ctx:
4186 scrub_free_ctx(sctx);
4187
4188 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01004189}
4190
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004191void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004192{
Arne Jansena2de7332011-03-08 14:14:00 +01004193 mutex_lock(&fs_info->scrub_lock);
4194 atomic_inc(&fs_info->scrub_pause_req);
4195 while (atomic_read(&fs_info->scrubs_paused) !=
4196 atomic_read(&fs_info->scrubs_running)) {
4197 mutex_unlock(&fs_info->scrub_lock);
4198 wait_event(fs_info->scrub_pause_wait,
4199 atomic_read(&fs_info->scrubs_paused) ==
4200 atomic_read(&fs_info->scrubs_running));
4201 mutex_lock(&fs_info->scrub_lock);
4202 }
4203 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004204}
4205
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004206void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004207{
Arne Jansena2de7332011-03-08 14:14:00 +01004208 atomic_dec(&fs_info->scrub_pause_req);
4209 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01004210}
4211
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004212int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004213{
Arne Jansena2de7332011-03-08 14:14:00 +01004214 mutex_lock(&fs_info->scrub_lock);
4215 if (!atomic_read(&fs_info->scrubs_running)) {
4216 mutex_unlock(&fs_info->scrub_lock);
4217 return -ENOTCONN;
4218 }
4219
4220 atomic_inc(&fs_info->scrub_cancel_req);
4221 while (atomic_read(&fs_info->scrubs_running)) {
4222 mutex_unlock(&fs_info->scrub_lock);
4223 wait_event(fs_info->scrub_pause_wait,
4224 atomic_read(&fs_info->scrubs_running) == 0);
4225 mutex_lock(&fs_info->scrub_lock);
4226 }
4227 atomic_dec(&fs_info->scrub_cancel_req);
4228 mutex_unlock(&fs_info->scrub_lock);
4229
4230 return 0;
4231}
4232
David Sterba163e97e2019-03-20 16:32:55 +01004233int btrfs_scrub_cancel_dev(struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004234{
David Sterba163e97e2019-03-20 16:32:55 +01004235 struct btrfs_fs_info *fs_info = dev->fs_info;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004236 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004237
4238 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004239 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004240 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004241 mutex_unlock(&fs_info->scrub_lock);
4242 return -ENOTCONN;
4243 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004244 atomic_inc(&sctx->cancel_req);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004245 while (dev->scrub_ctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004246 mutex_unlock(&fs_info->scrub_lock);
4247 wait_event(fs_info->scrub_pause_wait,
Anand Jaincadbc0a2018-01-03 16:08:30 +08004248 dev->scrub_ctx == NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004249 mutex_lock(&fs_info->scrub_lock);
4250 }
4251 mutex_unlock(&fs_info->scrub_lock);
4252
4253 return 0;
4254}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004255
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004256int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01004257 struct btrfs_scrub_progress *progress)
4258{
Josef Bacik562d7b12021-10-05 16:12:42 -04004259 struct btrfs_dev_lookup_args args = { .devid = devid };
Arne Jansena2de7332011-03-08 14:14:00 +01004260 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004261 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004262
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004263 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik562d7b12021-10-05 16:12:42 -04004264 dev = btrfs_find_device(fs_info->fs_devices, &args);
Arne Jansena2de7332011-03-08 14:14:00 +01004265 if (dev)
Anand Jaincadbc0a2018-01-03 16:08:30 +08004266 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004267 if (sctx)
4268 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004269 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004270
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004271 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004272}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004273
4274static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
Qu Wenruofa485d22020-12-02 14:48:07 +08004275 u64 extent_logical, u32 extent_len,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004276 u64 *extent_physical,
4277 struct btrfs_device **extent_dev,
4278 int *extent_mirror_num)
4279{
4280 u64 mapped_length;
Qu Wenruo4c664612021-09-15 15:17:16 +08004281 struct btrfs_io_context *bioc = NULL;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004282 int ret;
4283
4284 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004285 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Qu Wenruo4c664612021-09-15 15:17:16 +08004286 &mapped_length, &bioc, 0);
4287 if (ret || !bioc || mapped_length < extent_len ||
4288 !bioc->stripes[0].dev->bdev) {
4289 btrfs_put_bioc(bioc);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004290 return;
4291 }
4292
Qu Wenruo4c664612021-09-15 15:17:16 +08004293 *extent_physical = bioc->stripes[0].physical;
4294 *extent_mirror_num = bioc->mirror_num;
4295 *extent_dev = bioc->stripes[0].dev;
4296 btrfs_put_bioc(bioc);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004297}