blob: a0017351e5585ad7bd314f5cc1de2d5608d22bdf [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/*
42 * the following three values only influence the performance.
43 * The last one configures the number of parallel and outstanding I/O
44 * operations. The first two values configure an upper limit for the number
45 * of (dynamically allocated) pages that are added to a bio.
46 */
47#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
48#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
49#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010050
51/*
52 * the following value times PAGE_SIZE needs to be large enough to match the
53 * largest node/leaf/sector size that shall be supported.
54 * Values larger than BTRFS_STRIPE_LEN are not supported.
55 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040056#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010057
Miao Xieaf8e2d12014-10-23 14:42:50 +080058struct scrub_recover {
Elena Reshetova6f615012017-03-03 10:55:21 +020059 refcount_t refs;
Qu Wenruo4c664612021-09-15 15:17:16 +080060 struct btrfs_io_context *bioc;
Miao Xieaf8e2d12014-10-23 14:42:50 +080061 u64 map_length;
62};
63
Arne Jansena2de7332011-03-08 14:14:00 +010064struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040065 struct scrub_block *sblock;
66 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020067 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080068 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010069 u64 flags; /* extent flags */
70 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040071 u64 logical;
72 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010073 u64 physical_for_dev_replace;
Zhao Lei57019342015-01-20 15:11:45 +080074 atomic_t refs;
Qu Wenruo2c363952020-11-13 20:51:44 +080075 u8 mirror_num;
Colin Ian Kingd08e38b2021-11-10 19:20:08 +000076 unsigned int have_csum:1;
77 unsigned int io_error:1;
Arne Jansena2de7332011-03-08 14:14:00 +010078 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080079
80 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010081};
82
83struct scrub_bio {
84 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010085 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010086 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010087 struct bio *bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020088 blk_status_t status;
Arne Jansena2de7332011-03-08 14:14:00 +010089 u64 logical;
90 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010091#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
92 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
93#else
94 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
95#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -040096 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +010097 int next_free;
98 struct btrfs_work work;
99};
100
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400101struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100102 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400103 int page_count;
104 atomic_t outstanding_pages;
Elena Reshetova186debd2017-03-03 10:55:23 +0200105 refcount_t refs; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100106 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800107 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400108 struct {
109 unsigned int header_error:1;
110 unsigned int checksum_error:1;
111 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200112 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800113
114 /* The following is for the data used to check parity */
115 /* It is for the data with checksum */
116 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400117 };
Omar Sandoval73ff61d2015-06-19 11:52:51 -0700118 struct btrfs_work work;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400119};
120
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800121/* Used for the chunks with parity stripe such RAID5/6 */
122struct scrub_parity {
123 struct scrub_ctx *sctx;
124
125 struct btrfs_device *scrub_dev;
126
127 u64 logic_start;
128
129 u64 logic_end;
130
131 int nsectors;
132
Qu Wenruofa485d22020-12-02 14:48:07 +0800133 u32 stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800134
Elena Reshetova78a76452017-03-03 10:55:24 +0200135 refcount_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800136
137 struct list_head spages;
138
139 /* Work of parity check and repair */
140 struct btrfs_work work;
141
142 /* Mark the parity blocks which have data */
143 unsigned long *dbitmap;
144
145 /*
146 * Mark the parity blocks which have data, but errors happen when
147 * read data or check data
148 */
149 unsigned long *ebitmap;
150
Gustavo A. R. Silvaa8753ee2020-03-06 16:13:33 -0600151 unsigned long bitmap[];
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800152};
153
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100154struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100155 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400156 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100157 int first_free;
158 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100159 atomic_t bios_in_flight;
160 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100161 spinlock_t list_lock;
162 wait_queue_head_t list_wait;
Arne Jansena2de7332011-03-08 14:14:00 +0100163 struct list_head csum_list;
164 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100165 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100166 int pages_per_rd_bio;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100167
David Sterbaeb3b5052019-10-09 13:58:13 +0200168 /* State of IO submission throttling affecting the associated device */
169 ktime_t throttle_deadline;
170 u64 throttle_sent;
171
Stefan Behrens63a212a2012-11-05 18:29:28 +0100172 int is_dev_replace;
Naohiro Aotade17add2021-02-04 19:22:13 +0900173 u64 write_pointer;
David Sterba3fb99302017-05-16 19:10:32 +0200174
175 struct scrub_bio *wr_curr_bio;
176 struct mutex wr_lock;
177 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
David Sterba3fb99302017-05-16 19:10:32 +0200178 struct btrfs_device *wr_tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200179 bool flush_all_writes;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100180
Arne Jansena2de7332011-03-08 14:14:00 +0100181 /*
182 * statistics
183 */
184 struct btrfs_scrub_progress stat;
185 spinlock_t stat_lock;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000186
187 /*
188 * Use a ref counter to avoid use-after-free issues. Scrub workers
189 * decrement bios_in_flight and workers_pending and then do a wakeup
190 * on the list_wait wait queue. We must ensure the main scrub task
191 * doesn't free the scrub context before or while the workers are
192 * doing the wakeup() call.
193 */
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200194 refcount_t refs;
Arne Jansena2de7332011-03-08 14:14:00 +0100195};
196
Jan Schmidt558540c2011-06-13 19:59:12 +0200197struct scrub_warning {
198 struct btrfs_path *path;
199 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200200 const char *errstr;
David Sterba6aa21262017-10-04 17:07:07 +0200201 u64 physical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200202 u64 logical;
203 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200204};
205
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800206struct full_stripe_lock {
207 struct rb_node node;
208 u64 logical;
209 u64 refs;
210 struct mutex mutex;
211};
212
Zhao Leibe50a8d2015-01-20 15:11:42 +0800213static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100214 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100215static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +0800216 struct scrub_block *sblock,
217 int retry_failed_mirror);
Zhao Leiba7cf982015-08-24 21:18:02 +0800218static void scrub_recheck_block_checksum(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400219static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800220 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400221static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
222 struct scrub_block *sblock_good,
223 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100224static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
225static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
226 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400227static int scrub_checksum_data(struct scrub_block *sblock);
228static int scrub_checksum_tree_block(struct scrub_block *sblock);
229static int scrub_checksum_super(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400230static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100231static void scrub_page_get(struct scrub_page *spage);
232static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800233static void scrub_parity_get(struct scrub_parity *sparity);
234static void scrub_parity_put(struct scrub_parity *sparity);
Qu Wenruofa485d22020-12-02 14:48:07 +0800235static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u32 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100236 u64 physical, struct btrfs_device *dev, u64 flags,
Qu Wenruo96e63a42020-11-03 21:31:02 +0800237 u64 gen, int mirror_num, u8 *csum,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100238 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200239static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400240static void scrub_bio_end_io_worker(struct btrfs_work *work);
241static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100242static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
Qu Wenruofa485d22020-12-02 14:48:07 +0800243 u64 extent_logical, u32 extent_len,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100244 u64 *extent_physical,
245 struct btrfs_device **extent_dev,
246 int *extent_mirror_num);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100247static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
248 struct scrub_page *spage);
249static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200250static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100251static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000252static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400253
Qu Wenruo261d2dc2020-11-03 21:31:01 +0800254static inline int scrub_is_page_on_raid56(struct scrub_page *spage)
Liu Bo762221f2018-01-02 13:36:42 -0700255{
Qu Wenruo261d2dc2020-11-03 21:31:01 +0800256 return spage->recover &&
Qu Wenruo4c664612021-09-15 15:17:16 +0800257 (spage->recover->bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Liu Bo762221f2018-01-02 13:36:42 -0700258}
Stefan Behrens1623ede2012-03-27 14:21:26 -0400259
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100260static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
261{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200262 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100263 atomic_inc(&sctx->bios_in_flight);
264}
265
266static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
267{
268 atomic_dec(&sctx->bios_in_flight);
269 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000270 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100271}
272
Wang Shilongcb7ab022013-12-04 21:16:53 +0800273static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800274{
275 while (atomic_read(&fs_info->scrub_pause_req)) {
276 mutex_unlock(&fs_info->scrub_lock);
277 wait_event(fs_info->scrub_pause_wait,
278 atomic_read(&fs_info->scrub_pause_req) == 0);
279 mutex_lock(&fs_info->scrub_lock);
280 }
281}
282
Zhaolei0e22be82015-08-05 16:43:28 +0800283static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800284{
285 atomic_inc(&fs_info->scrubs_paused);
286 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800287}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800288
Zhaolei0e22be82015-08-05 16:43:28 +0800289static void scrub_pause_off(struct btrfs_fs_info *fs_info)
290{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800291 mutex_lock(&fs_info->scrub_lock);
292 __scrub_blocked_if_needed(fs_info);
293 atomic_dec(&fs_info->scrubs_paused);
294 mutex_unlock(&fs_info->scrub_lock);
295
296 wake_up(&fs_info->scrub_pause_wait);
297}
298
Zhaolei0e22be82015-08-05 16:43:28 +0800299static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
300{
301 scrub_pause_on(fs_info);
302 scrub_pause_off(fs_info);
303}
304
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100305/*
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800306 * Insert new full stripe lock into full stripe locks tree
307 *
308 * Return pointer to existing or newly inserted full_stripe_lock structure if
309 * everything works well.
310 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
311 *
312 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
313 * function
314 */
315static struct full_stripe_lock *insert_full_stripe_lock(
316 struct btrfs_full_stripe_locks_tree *locks_root,
317 u64 fstripe_logical)
318{
319 struct rb_node **p;
320 struct rb_node *parent = NULL;
321 struct full_stripe_lock *entry;
322 struct full_stripe_lock *ret;
323
David Sterbaa32bf9a2018-03-16 02:21:22 +0100324 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800325
326 p = &locks_root->root.rb_node;
327 while (*p) {
328 parent = *p;
329 entry = rb_entry(parent, struct full_stripe_lock, node);
330 if (fstripe_logical < entry->logical) {
331 p = &(*p)->rb_left;
332 } else if (fstripe_logical > entry->logical) {
333 p = &(*p)->rb_right;
334 } else {
335 entry->refs++;
336 return entry;
337 }
338 }
339
Filipe Mananaa5fb1142018-11-26 20:07:17 +0000340 /*
341 * Insert new lock.
Filipe Mananaa5fb1142018-11-26 20:07:17 +0000342 */
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800343 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
344 if (!ret)
345 return ERR_PTR(-ENOMEM);
346 ret->logical = fstripe_logical;
347 ret->refs = 1;
348 mutex_init(&ret->mutex);
349
350 rb_link_node(&ret->node, parent, p);
351 rb_insert_color(&ret->node, &locks_root->root);
352 return ret;
353}
354
355/*
356 * Search for a full stripe lock of a block group
357 *
358 * Return pointer to existing full stripe lock if found
359 * Return NULL if not found
360 */
361static struct full_stripe_lock *search_full_stripe_lock(
362 struct btrfs_full_stripe_locks_tree *locks_root,
363 u64 fstripe_logical)
364{
365 struct rb_node *node;
366 struct full_stripe_lock *entry;
367
David Sterbaa32bf9a2018-03-16 02:21:22 +0100368 lockdep_assert_held(&locks_root->lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800369
370 node = locks_root->root.rb_node;
371 while (node) {
372 entry = rb_entry(node, struct full_stripe_lock, node);
373 if (fstripe_logical < entry->logical)
374 node = node->rb_left;
375 else if (fstripe_logical > entry->logical)
376 node = node->rb_right;
377 else
378 return entry;
379 }
380 return NULL;
381}
382
383/*
384 * Helper to get full stripe logical from a normal bytenr.
385 *
386 * Caller must ensure @cache is a RAID56 block group.
387 */
David Sterba32da53862019-10-29 19:20:18 +0100388static u64 get_full_stripe_logical(struct btrfs_block_group *cache, u64 bytenr)
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800389{
390 u64 ret;
391
392 /*
393 * Due to chunk item size limit, full stripe length should not be
394 * larger than U32_MAX. Just a sanity check here.
395 */
396 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
397
398 /*
399 * round_down() can only handle power of 2, while RAID56 full
400 * stripe length can be 64KiB * n, so we need to manually round down.
401 */
David Sterbab3470b52019-10-23 18:48:22 +0200402 ret = div64_u64(bytenr - cache->start, cache->full_stripe_len) *
403 cache->full_stripe_len + cache->start;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800404 return ret;
405}
406
407/*
408 * Lock a full stripe to avoid concurrency of recovery and read
409 *
410 * It's only used for profiles with parities (RAID5/6), for other profiles it
411 * does nothing.
412 *
413 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
414 * So caller must call unlock_full_stripe() at the same context.
415 *
416 * Return <0 if encounters error.
417 */
418static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
419 bool *locked_ret)
420{
David Sterba32da53862019-10-29 19:20:18 +0100421 struct btrfs_block_group *bg_cache;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800422 struct btrfs_full_stripe_locks_tree *locks_root;
423 struct full_stripe_lock *existing;
424 u64 fstripe_start;
425 int ret = 0;
426
427 *locked_ret = false;
428 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
429 if (!bg_cache) {
430 ASSERT(0);
431 return -ENOENT;
432 }
433
434 /* Profiles not based on parity don't need full stripe lock */
435 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
436 goto out;
437 locks_root = &bg_cache->full_stripe_locks_root;
438
439 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
440
441 /* Now insert the full stripe lock */
442 mutex_lock(&locks_root->lock);
443 existing = insert_full_stripe_lock(locks_root, fstripe_start);
444 mutex_unlock(&locks_root->lock);
445 if (IS_ERR(existing)) {
446 ret = PTR_ERR(existing);
447 goto out;
448 }
449 mutex_lock(&existing->mutex);
450 *locked_ret = true;
451out:
452 btrfs_put_block_group(bg_cache);
453 return ret;
454}
455
456/*
457 * Unlock a full stripe.
458 *
459 * NOTE: Caller must ensure it's the same context calling corresponding
460 * lock_full_stripe().
461 *
462 * Return 0 if we unlock full stripe without problem.
463 * Return <0 for error
464 */
465static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
466 bool locked)
467{
David Sterba32da53862019-10-29 19:20:18 +0100468 struct btrfs_block_group *bg_cache;
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800469 struct btrfs_full_stripe_locks_tree *locks_root;
470 struct full_stripe_lock *fstripe_lock;
471 u64 fstripe_start;
472 bool freeit = false;
473 int ret = 0;
474
475 /* If we didn't acquire full stripe lock, no need to continue */
476 if (!locked)
477 return 0;
478
479 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
480 if (!bg_cache) {
481 ASSERT(0);
482 return -ENOENT;
483 }
484 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
485 goto out;
486
487 locks_root = &bg_cache->full_stripe_locks_root;
488 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
489
490 mutex_lock(&locks_root->lock);
491 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
492 /* Unpaired unlock_full_stripe() detected */
493 if (!fstripe_lock) {
494 WARN_ON(1);
495 ret = -ENOENT;
496 mutex_unlock(&locks_root->lock);
497 goto out;
498 }
499
500 if (fstripe_lock->refs == 0) {
501 WARN_ON(1);
502 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
503 fstripe_lock->logical);
504 } else {
505 fstripe_lock->refs--;
506 }
507
508 if (fstripe_lock->refs == 0) {
509 rb_erase(&fstripe_lock->node, &locks_root->root);
510 freeit = true;
511 }
512 mutex_unlock(&locks_root->lock);
513
514 mutex_unlock(&fstripe_lock->mutex);
515 if (freeit)
516 kfree(fstripe_lock);
517out:
518 btrfs_put_block_group(bg_cache);
519 return ret;
520}
521
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100522static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100523{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100524 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100525 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100526 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100527 struct btrfs_ordered_sum, list);
528 list_del(&sum->list);
529 kfree(sum);
530 }
531}
532
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100533static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100534{
535 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100536
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100537 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100538 return;
539
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400540 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100541 if (sctx->curr != -1) {
542 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400543
544 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100545 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400546 scrub_block_put(sbio->pagev[i]->sblock);
547 }
548 bio_put(sbio->bio);
549 }
550
Stefan Behrensff023aa2012-11-06 11:43:11 +0100551 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100552 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100553
554 if (!sbio)
555 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100556 kfree(sbio);
557 }
558
David Sterba3fb99302017-05-16 19:10:32 +0200559 kfree(sctx->wr_curr_bio);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100560 scrub_free_csums(sctx);
561 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100562}
563
Filipe Mananaf55985f2015-02-09 21:14:24 +0000564static void scrub_put_ctx(struct scrub_ctx *sctx)
565{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200566 if (refcount_dec_and_test(&sctx->refs))
Filipe Mananaf55985f2015-02-09 21:14:24 +0000567 scrub_free_ctx(sctx);
568}
569
David Sterba92f7ba42018-12-04 16:11:55 +0100570static noinline_for_stack struct scrub_ctx *scrub_setup_ctx(
571 struct btrfs_fs_info *fs_info, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100572{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100573 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100574 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100575
David Sterba58c4e172016-02-11 10:49:42 +0100576 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100577 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100578 goto nomem;
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200579 refcount_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100580 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200581 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100582 sctx->curr = -1;
David Sterba92f7ba42018-12-04 16:11:55 +0100583 sctx->fs_info = fs_info;
Dan Robertsone49be142019-02-19 02:56:43 +0000584 INIT_LIST_HEAD(&sctx->csum_list);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100585 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100586 struct scrub_bio *sbio;
587
David Sterba58c4e172016-02-11 10:49:42 +0100588 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
Arne Jansena2de7332011-03-08 14:14:00 +0100589 if (!sbio)
590 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100591 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100592
Arne Jansena2de7332011-03-08 14:14:00 +0100593 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100594 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400595 sbio->page_count = 0;
Omar Sandovala0cac0e2019-09-16 11:30:57 -0700596 btrfs_init_work(&sbio->work, scrub_bio_end_io_worker, NULL,
597 NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100598
Stefan Behrensff023aa2012-11-06 11:43:11 +0100599 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100600 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200601 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100602 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100603 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100604 sctx->first_free = 0;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100605 atomic_set(&sctx->bios_in_flight, 0);
606 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100607 atomic_set(&sctx->cancel_req, 0);
Arne Jansena2de7332011-03-08 14:14:00 +0100608
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100609 spin_lock_init(&sctx->list_lock);
610 spin_lock_init(&sctx->stat_lock);
611 init_waitqueue_head(&sctx->list_wait);
David Sterbaeb3b5052019-10-09 13:58:13 +0200612 sctx->throttle_deadline = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100613
David Sterba3fb99302017-05-16 19:10:32 +0200614 WARN_ON(sctx->wr_curr_bio != NULL);
615 mutex_init(&sctx->wr_lock);
616 sctx->wr_curr_bio = NULL;
David Sterba8fcdac32017-05-16 19:10:23 +0200617 if (is_dev_replace) {
David Sterbaded56182017-06-26 15:19:00 +0200618 WARN_ON(!fs_info->dev_replace.tgtdev);
David Sterba3fb99302017-05-16 19:10:32 +0200619 sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
David Sterbaded56182017-06-26 15:19:00 +0200620 sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200621 sctx->flush_all_writes = false;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100622 }
David Sterba8fcdac32017-05-16 19:10:23 +0200623
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100624 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100625
626nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100627 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100628 return ERR_PTR(-ENOMEM);
629}
630
Stefan Behrensff023aa2012-11-06 11:43:11 +0100631static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
632 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200633{
Jan Schmidt558540c2011-06-13 19:59:12 +0200634 u32 nlink;
635 int ret;
636 int i;
David Sterbade2491f2017-05-31 19:21:38 +0200637 unsigned nofs_flag;
Jan Schmidt558540c2011-06-13 19:59:12 +0200638 struct extent_buffer *eb;
639 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100640 struct scrub_warning *swarn = warn_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400641 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200642 struct inode_fs_paths *ipath = NULL;
643 struct btrfs_root *local_root;
David Sterba1d4c08e2015-01-02 19:36:14 +0100644 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200645
David Sterba56e93572020-05-15 19:35:55 +0200646 local_root = btrfs_get_fs_root(fs_info, root, true);
Jan Schmidt558540c2011-06-13 19:59:12 +0200647 if (IS_ERR(local_root)) {
648 ret = PTR_ERR(local_root);
649 goto err;
650 }
651
David Sterba14692cc2015-01-02 18:55:46 +0100652 /*
653 * this makes the path point to (inum INODE_ITEM ioff)
654 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100655 key.objectid = inum;
656 key.type = BTRFS_INODE_ITEM_KEY;
657 key.offset = 0;
658
659 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200660 if (ret) {
Josef Bacik00246522020-01-24 09:33:01 -0500661 btrfs_put_root(local_root);
Jan Schmidt558540c2011-06-13 19:59:12 +0200662 btrfs_release_path(swarn->path);
663 goto err;
664 }
665
666 eb = swarn->path->nodes[0];
667 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
668 struct btrfs_inode_item);
Jan Schmidt558540c2011-06-13 19:59:12 +0200669 nlink = btrfs_inode_nlink(eb, inode_item);
670 btrfs_release_path(swarn->path);
671
David Sterbade2491f2017-05-31 19:21:38 +0200672 /*
673 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
674 * uses GFP_NOFS in this context, so we keep it consistent but it does
675 * not seem to be strictly necessary.
676 */
677 nofs_flag = memalloc_nofs_save();
Jan Schmidt558540c2011-06-13 19:59:12 +0200678 ipath = init_ipath(4096, local_root, swarn->path);
David Sterbade2491f2017-05-31 19:21:38 +0200679 memalloc_nofs_restore(nofs_flag);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300680 if (IS_ERR(ipath)) {
Josef Bacik00246522020-01-24 09:33:01 -0500681 btrfs_put_root(local_root);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300682 ret = PTR_ERR(ipath);
683 ipath = NULL;
684 goto err;
685 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200686 ret = paths_from_inode(inum, ipath);
687
688 if (ret < 0)
689 goto err;
690
691 /*
692 * we deliberately ignore the bit ipath might have been too small to
693 * hold all of the paths here
694 */
695 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400696 btrfs_warn_in_rcu(fs_info,
Qu Wenruo8df507c2021-04-22 19:02:46 +0800697"%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 -0400698 swarn->errstr, swarn->logical,
699 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200700 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400701 root, inum, offset,
Qu Wenruo8df507c2021-04-22 19:02:46 +0800702 fs_info->sectorsize, nlink,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400703 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200704
Josef Bacik00246522020-01-24 09:33:01 -0500705 btrfs_put_root(local_root);
Jan Schmidt558540c2011-06-13 19:59:12 +0200706 free_ipath(ipath);
707 return 0;
708
709err:
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400710 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200711 "%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 -0400712 swarn->errstr, swarn->logical,
713 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200714 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400715 root, inum, offset, ret);
Jan Schmidt558540c2011-06-13 19:59:12 +0200716
717 free_ipath(ipath);
718 return 0;
719}
720
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400721static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200722{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100723 struct btrfs_device *dev;
724 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200725 struct btrfs_path *path;
726 struct btrfs_key found_key;
727 struct extent_buffer *eb;
728 struct btrfs_extent_item *ei;
729 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200730 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100731 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600732 u64 flags = 0;
733 u64 ref_root;
734 u32 item_size;
Dan Carpenter07c9a8e2016-03-11 11:08:56 +0300735 u8 ref_level = 0;
Liu Bo69917e42012-09-07 20:01:28 -0600736 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200737
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100738 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100739 dev = sblock->pagev[0]->dev;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400740 fs_info = sblock->sctx->fs_info;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100741
Jan Schmidt558540c2011-06-13 19:59:12 +0200742 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200743 if (!path)
744 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200745
David Sterba6aa21262017-10-04 17:07:07 +0200746 swarn.physical = sblock->pagev[0]->physical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100747 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200748 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100749 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200750
Liu Bo69917e42012-09-07 20:01:28 -0600751 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
752 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200753 if (ret < 0)
754 goto out;
755
Jan Schmidt4692cf52011-12-02 14:56:41 +0100756 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200757 swarn.extent_item_size = found_key.offset;
758
759 eb = path->nodes[0];
760 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
Josef Bacik3212fa12021-10-21 14:58:35 -0400761 item_size = btrfs_item_size(eb, path->slots[0]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200762
Liu Bo69917e42012-09-07 20:01:28 -0600763 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200764 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800765 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
766 item_size, &ref_root,
767 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200768 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200769"%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400770 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400771 rcu_str_deref(dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200772 swarn.physical,
Jan Schmidt558540c2011-06-13 19:59:12 +0200773 ref_level ? "node" : "leaf",
774 ret < 0 ? -1 : ref_level,
775 ret < 0 ? -1 : ref_root);
776 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600777 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200778 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600779 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200780 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100781 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100782 iterate_extent_inodes(fs_info, found_key.objectid,
783 extent_item_pos, 1,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400784 scrub_print_warning_inode, &swarn, false);
Jan Schmidt558540c2011-06-13 19:59:12 +0200785 }
786
787out:
788 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200789}
790
Miao Xieaf8e2d12014-10-23 14:42:50 +0800791static inline void scrub_get_recover(struct scrub_recover *recover)
792{
Elena Reshetova6f615012017-03-03 10:55:21 +0200793 refcount_inc(&recover->refs);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800794}
795
Qu Wenruoe501bfe2017-03-29 09:33:22 +0800796static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
797 struct scrub_recover *recover)
Miao Xieaf8e2d12014-10-23 14:42:50 +0800798{
Elena Reshetova6f615012017-03-03 10:55:21 +0200799 if (refcount_dec_and_test(&recover->refs)) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +0800800 btrfs_bio_counter_dec(fs_info);
Qu Wenruo4c664612021-09-15 15:17:16 +0800801 btrfs_put_bioc(recover->bioc);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800802 kfree(recover);
803 }
804}
805
Arne Jansena2de7332011-03-08 14:14:00 +0100806/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400807 * scrub_handle_errored_block gets called when either verification of the
808 * pages failed or the bio failed to read, e.g. with EIO. In the latter
809 * case, this function handles all pages in the bio, even though only one
810 * may be bad.
811 * The goal of this function is to repair the errored block by using the
812 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100813 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400814static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100815{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100816 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100817 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400818 struct btrfs_fs_info *fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400819 u64 logical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400820 unsigned int failed_mirror_index;
821 unsigned int is_metadata;
822 unsigned int have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400823 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
824 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100825 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400826 int mirror_index;
827 int page_num;
828 int success;
Qu Wenruo28d70e22017-04-14 08:35:55 +0800829 bool full_stripe_locked;
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000830 unsigned int nofs_flag;
David Sterba8bb1cf12020-08-17 12:12:38 +0200831 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400832 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100833
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400834 BUG_ON(sblock_to_check->page_count < 1);
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400835 fs_info = sctx->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000836 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
837 /*
838 * if we find an error in a super block, we just report it.
839 * They will get written with the next transaction commit
840 * anyway
841 */
842 spin_lock(&sctx->stat_lock);
843 ++sctx->stat.super_errors;
844 spin_unlock(&sctx->stat_lock);
845 return 0;
846 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100847 logical = sblock_to_check->pagev[0]->logical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100848 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
849 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
850 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400851 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100852 have_csum = sblock_to_check->pagev[0]->have_csum;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100853 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400854
Naohiro Aotaf7ef5282021-02-04 19:22:16 +0900855 if (btrfs_is_zoned(fs_info) && !sctx->is_dev_replace)
856 return btrfs_repair_one_zone(fs_info, logical);
857
Qu Wenruo28d70e22017-04-14 08:35:55 +0800858 /*
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000859 * We must use GFP_NOFS because the scrub task might be waiting for a
860 * worker task executing this function and in turn a transaction commit
861 * might be waiting the scrub task to pause (which needs to wait for all
862 * the worker tasks to complete before pausing).
863 * We do allocations in the workers through insert_full_stripe_lock()
864 * and scrub_add_page_to_wr_bio(), which happens down the call chain of
865 * this function.
866 */
867 nofs_flag = memalloc_nofs_save();
868 /*
Qu Wenruo28d70e22017-04-14 08:35:55 +0800869 * For RAID5/6, race can happen for a different device scrub thread.
870 * For data corruption, Parity and Data threads will both try
871 * to recovery the data.
872 * Race can lead to doubly added csum error, or even unrecoverable
873 * error.
874 */
875 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
876 if (ret < 0) {
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000877 memalloc_nofs_restore(nofs_flag);
Qu Wenruo28d70e22017-04-14 08:35:55 +0800878 spin_lock(&sctx->stat_lock);
879 if (ret == -ENOMEM)
880 sctx->stat.malloc_errors++;
881 sctx->stat.read_errors++;
882 sctx->stat.uncorrectable_errors++;
883 spin_unlock(&sctx->stat_lock);
884 return ret;
885 }
886
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400887 /*
888 * read all mirrors one after the other. This includes to
889 * re-read the extent or metadata block that failed (that was
890 * the cause that this fixup code is called) another time,
Qu Wenruo8df507c2021-04-22 19:02:46 +0800891 * sector by sector this time in order to know which sectors
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400892 * caused I/O errors and which ones are good (for all mirrors).
893 * It is the goal to handle the situation when more than one
894 * mirror contains I/O errors, but the errors do not
895 * overlap, i.e. the data can be repaired by selecting the
Qu Wenruo8df507c2021-04-22 19:02:46 +0800896 * sectors from those mirrors without I/O error on the
897 * particular sectors. One example (with blocks >= 2 * sectorsize)
898 * would be that mirror #1 has an I/O error on the first sector,
899 * the second sector is good, and mirror #2 has an I/O error on
900 * the second sector, but the first sector is good.
901 * Then the first sector of the first mirror can be repaired by
902 * taking the first sector of the second mirror, and the
903 * second sector of the second mirror can be repaired by
904 * copying the contents of the 2nd sector of the 1st mirror.
905 * One more note: if the sectors of one mirror contain I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400906 * errors, the checksum cannot be verified. In order to get
907 * the best data for repairing, the first attempt is to find
908 * a mirror without I/O errors and with a validated checksum.
Qu Wenruo8df507c2021-04-22 19:02:46 +0800909 * Only if this is not possible, the sectors are picked from
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400910 * mirrors with I/O errors without considering the checksum.
911 * If the latter is the case, at the end, the checksum of the
912 * repaired area is verified in order to correctly maintain
913 * the statistics.
914 */
915
David Sterba31e818f2015-02-20 18:00:26 +0100916 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
Filipe Manana7c3c7cb2018-12-07 13:23:32 +0000917 sizeof(*sblocks_for_recheck), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400918 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100919 spin_lock(&sctx->stat_lock);
920 sctx->stat.malloc_errors++;
921 sctx->stat.read_errors++;
922 sctx->stat.uncorrectable_errors++;
923 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100924 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400925 goto out;
926 }
927
928 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800929 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400930 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100931 spin_lock(&sctx->stat_lock);
932 sctx->stat.read_errors++;
933 sctx->stat.uncorrectable_errors++;
934 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100935 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400936 goto out;
937 }
938 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
939 sblock_bad = sblocks_for_recheck + failed_mirror_index;
940
941 /* build and submit the bios for the failed mirror, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +0800942 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400943
944 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
945 sblock_bad->no_io_error_seen) {
946 /*
947 * the error disappeared after reading page by page, or
948 * the area was part of a huge bio and other parts of the
949 * bio caused I/O errors, or the block layer merged several
950 * read requests into one and the error is caused by a
951 * different bio (usually one of the two latter cases is
952 * the cause)
953 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100954 spin_lock(&sctx->stat_lock);
955 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800956 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100957 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400958
Stefan Behrensff023aa2012-11-06 11:43:11 +0100959 if (sctx->is_dev_replace)
960 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400961 goto out;
962 }
963
964 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100965 spin_lock(&sctx->stat_lock);
966 sctx->stat.read_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("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100970 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400971 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100972 spin_lock(&sctx->stat_lock);
973 sctx->stat.csum_errors++;
974 spin_unlock(&sctx->stat_lock);
David Sterba8bb1cf12020-08-17 12:12:38 +0200975 if (__ratelimit(&rs))
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400976 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100977 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200978 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400979 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100980 spin_lock(&sctx->stat_lock);
981 sctx->stat.verify_errors++;
982 spin_unlock(&sctx->stat_lock);
David Sterba8bb1cf12020-08-17 12:12:38 +0200983 if (__ratelimit(&rs))
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400984 scrub_print_warning("checksum/header error",
985 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200986 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100987 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200988 BTRFS_DEV_STAT_GENERATION_ERRS);
989 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100990 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200991 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400992 }
993
Ilya Dryomov33ef30a2013-11-03 19:06:38 +0200994 if (sctx->readonly) {
995 ASSERT(!sctx->is_dev_replace);
996 goto out;
997 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400998
Qu Wenruo665d4952018-07-11 13:41:21 +0800999 /*
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001000 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001001 * checksums.
1002 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001003 * errors and also does not have a checksum error.
1004 * If one is found, and if a checksum is present, the full block
1005 * that is known to contain an error is rewritten. Afterwards
1006 * the block is known to be corrected.
1007 * If a mirror is found which is completely correct, and no
1008 * checksum is present, only those pages are rewritten that had
1009 * an I/O error in the block to be repaired, since it cannot be
1010 * determined, which copy of the other pages is better (and it
1011 * could happen otherwise that a correct page would be
1012 * overwritten by a bad one).
1013 */
Liu Bo762221f2018-01-02 13:36:42 -07001014 for (mirror_index = 0; ;mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001015 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001016
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001017 if (mirror_index == failed_mirror_index)
1018 continue;
Liu Bo762221f2018-01-02 13:36:42 -07001019
1020 /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
1021 if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1022 if (mirror_index >= BTRFS_MAX_MIRRORS)
1023 break;
1024 if (!sblocks_for_recheck[mirror_index].page_count)
1025 break;
1026
1027 sblock_other = sblocks_for_recheck + mirror_index;
1028 } else {
1029 struct scrub_recover *r = sblock_bad->pagev[0]->recover;
Qu Wenruo4c664612021-09-15 15:17:16 +08001030 int max_allowed = r->bioc->num_stripes - r->bioc->num_tgtdevs;
Liu Bo762221f2018-01-02 13:36:42 -07001031
1032 if (mirror_index >= max_allowed)
1033 break;
1034 if (!sblocks_for_recheck[1].page_count)
1035 break;
1036
1037 ASSERT(failed_mirror_index == 0);
1038 sblock_other = sblocks_for_recheck + 1;
1039 sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
1040 }
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001041
1042 /* build and submit the bios, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001043 scrub_recheck_block(fs_info, sblock_other, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001044
1045 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001046 !sblock_other->checksum_error &&
1047 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001048 if (sctx->is_dev_replace) {
1049 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001050 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001051 } else {
1052 ret = scrub_repair_block_from_good_copy(
1053 sblock_bad, sblock_other);
1054 if (!ret)
1055 goto corrected_error;
1056 }
Arne Jansena2de7332011-03-08 14:14:00 +01001057 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001058 }
1059
Zhao Leib968fed2015-01-20 15:11:41 +08001060 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1061 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001062
1063 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001064 * In case of I/O errors in the area that is supposed to be
Qu Wenruo8df507c2021-04-22 19:02:46 +08001065 * repaired, continue by picking good copies of those sectors.
1066 * Select the good sectors from mirrors to rewrite bad sectors from
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001067 * the area to fix. Afterwards verify the checksum of the block
1068 * that is supposed to be repaired. This verification step is
1069 * only done for the purpose of statistic counting and for the
1070 * final scrub report, whether errors remain.
1071 * A perfect algorithm could make use of the checksum and try
Qu Wenruo8df507c2021-04-22 19:02:46 +08001072 * all possible combinations of sectors from the different mirrors
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001073 * until the checksum verification succeeds. For example, when
Qu Wenruo8df507c2021-04-22 19:02:46 +08001074 * the 2nd sector of mirror #1 faces I/O errors, and the 2nd sector
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001075 * of mirror #2 is readable but the final checksum test fails,
Qu Wenruo8df507c2021-04-22 19:02:46 +08001076 * then the 2nd sector of mirror #3 could be tried, whether now
Nicholas D Steeves01327612016-05-19 21:18:45 -04001077 * the final checksum succeeds. But this would be a rare
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001078 * exception and is therefore not implemented. At least it is
1079 * avoided that the good copy is overwritten.
1080 * A more useful improvement would be to pick the sectors
1081 * without I/O error based on sector sizes (512 bytes on legacy
Qu Wenruo8df507c2021-04-22 19:02:46 +08001082 * disks) instead of on sectorsize. Then maybe 512 byte of one
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001083 * mirror could be repaired by taking 512 byte of a different
Qu Wenruo8df507c2021-04-22 19:02:46 +08001084 * mirror, even if other 512 byte sectors in the same sectorsize
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001085 * area are unreadable.
1086 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001087 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001088 for (page_num = 0; page_num < sblock_bad->page_count;
1089 page_num++) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001090 struct scrub_page *spage_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001091 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001092
Zhao Leib968fed2015-01-20 15:11:41 +08001093 /* skip no-io-error page in scrub */
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001094 if (!spage_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001095 continue;
1096
Liu Bo47597002018-03-02 16:10:41 -07001097 if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1098 /*
1099 * In case of dev replace, if raid56 rebuild process
1100 * didn't work out correct data, then copy the content
1101 * in sblock_bad to make sure target device is identical
1102 * to source device, instead of writing garbage data in
1103 * sblock_for_recheck array to target device.
1104 */
1105 sblock_other = NULL;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001106 } else if (spage_bad->io_error) {
Liu Bo47597002018-03-02 16:10:41 -07001107 /* try to find no-io-error page in mirrors */
Zhao Leib968fed2015-01-20 15:11:41 +08001108 for (mirror_index = 0;
1109 mirror_index < BTRFS_MAX_MIRRORS &&
1110 sblocks_for_recheck[mirror_index].page_count > 0;
1111 mirror_index++) {
1112 if (!sblocks_for_recheck[mirror_index].
1113 pagev[page_num]->io_error) {
1114 sblock_other = sblocks_for_recheck +
1115 mirror_index;
1116 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001117 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001118 }
Zhao Leib968fed2015-01-20 15:11:41 +08001119 if (!sblock_other)
1120 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001121 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001122
Zhao Leib968fed2015-01-20 15:11:41 +08001123 if (sctx->is_dev_replace) {
1124 /*
1125 * did not find a mirror to fetch the page
1126 * from. scrub_write_page_to_dev_replace()
1127 * handles this case (page->io_error), by
1128 * filling the block with zeros before
1129 * submitting the write request
1130 */
1131 if (!sblock_other)
1132 sblock_other = sblock_bad;
1133
1134 if (scrub_write_page_to_dev_replace(sblock_other,
1135 page_num) != 0) {
David Sterbae37abe92018-04-04 17:20:52 +02001136 atomic64_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001137 &fs_info->dev_replace.num_write_errors);
Zhao Leib968fed2015-01-20 15:11:41 +08001138 success = 0;
1139 }
1140 } else if (sblock_other) {
1141 ret = scrub_repair_page_from_good_copy(sblock_bad,
1142 sblock_other,
1143 page_num, 0);
1144 if (0 == ret)
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001145 spage_bad->io_error = 0;
Zhao Leib968fed2015-01-20 15:11:41 +08001146 else
1147 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001148 }
1149 }
1150
Zhao Leib968fed2015-01-20 15:11:41 +08001151 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001152 if (is_metadata || have_csum) {
1153 /*
1154 * need to verify the checksum now that all
1155 * sectors on disk are repaired (the write
1156 * request for data to be repaired is on its way).
1157 * Just be lazy and use scrub_recheck_block()
1158 * which re-reads the data before the checksum
1159 * is verified, but most likely the data comes out
1160 * of the page cache.
1161 */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001162 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001163 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001164 !sblock_bad->checksum_error &&
1165 sblock_bad->no_io_error_seen)
1166 goto corrected_error;
1167 else
1168 goto did_not_correct_error;
1169 } else {
1170corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001171 spin_lock(&sctx->stat_lock);
1172 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001173 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001174 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001175 btrfs_err_rl_in_rcu(fs_info,
1176 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001177 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001178 }
1179 } else {
1180did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001181 spin_lock(&sctx->stat_lock);
1182 sctx->stat.uncorrectable_errors++;
1183 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001184 btrfs_err_rl_in_rcu(fs_info,
1185 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001186 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001187 }
1188
1189out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001190 if (sblocks_for_recheck) {
1191 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1192 mirror_index++) {
1193 struct scrub_block *sblock = sblocks_for_recheck +
1194 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001195 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001196 int page_index;
1197
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001198 for (page_index = 0; page_index < sblock->page_count;
1199 page_index++) {
1200 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001201 recover = sblock->pagev[page_index]->recover;
1202 if (recover) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001203 scrub_put_recover(fs_info, recover);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001204 sblock->pagev[page_index]->recover =
1205 NULL;
1206 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001207 scrub_page_put(sblock->pagev[page_index]);
1208 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001209 }
1210 kfree(sblocks_for_recheck);
1211 }
1212
Qu Wenruo28d70e22017-04-14 08:35:55 +08001213 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
Filipe Manana7c3c7cb2018-12-07 13:23:32 +00001214 memalloc_nofs_restore(nofs_flag);
Qu Wenruo28d70e22017-04-14 08:35:55 +08001215 if (ret < 0)
1216 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001217 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001218}
1219
Qu Wenruo4c664612021-09-15 15:17:16 +08001220static inline int scrub_nr_raid_mirrors(struct btrfs_io_context *bioc)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001221{
Qu Wenruo4c664612021-09-15 15:17:16 +08001222 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID5)
Zhao Lei10f11902015-01-20 15:11:43 +08001223 return 2;
Qu Wenruo4c664612021-09-15 15:17:16 +08001224 else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID6)
Zhao Lei10f11902015-01-20 15:11:43 +08001225 return 3;
1226 else
Qu Wenruo4c664612021-09-15 15:17:16 +08001227 return (int)bioc->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001228}
1229
Zhao Lei10f11902015-01-20 15:11:43 +08001230static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1231 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001232 u64 mapped_length,
1233 int nstripes, int mirror,
1234 int *stripe_index,
1235 u64 *stripe_offset)
1236{
1237 int i;
1238
Zhao Leiffe2d202015-01-20 15:11:44 +08001239 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001240 /* RAID5/6 */
1241 for (i = 0; i < nstripes; i++) {
1242 if (raid_map[i] == RAID6_Q_STRIPE ||
1243 raid_map[i] == RAID5_P_STRIPE)
1244 continue;
1245
1246 if (logical >= raid_map[i] &&
1247 logical < raid_map[i] + mapped_length)
1248 break;
1249 }
1250
1251 *stripe_index = i;
1252 *stripe_offset = logical - raid_map[i];
1253 } else {
1254 /* The other RAID type */
1255 *stripe_index = mirror;
1256 *stripe_offset = 0;
1257 }
1258}
1259
Zhao Leibe50a8d2015-01-20 15:11:42 +08001260static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001261 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001262{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001263 struct scrub_ctx *sctx = original_sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001264 struct btrfs_fs_info *fs_info = sctx->fs_info;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001265 u64 length = original_sblock->page_count * fs_info->sectorsize;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001266 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001267 u64 generation = original_sblock->pagev[0]->generation;
1268 u64 flags = original_sblock->pagev[0]->flags;
1269 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001270 struct scrub_recover *recover;
Qu Wenruo4c664612021-09-15 15:17:16 +08001271 struct btrfs_io_context *bioc;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001272 u64 sublen;
1273 u64 mapped_length;
1274 u64 stripe_offset;
1275 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001276 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001277 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001278 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001279 int ret;
1280
1281 /*
Zhao Lei57019342015-01-20 15:11:45 +08001282 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001283 * are not used (and not set) in the blocks that are used for
1284 * the recheck procedure
1285 */
1286
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001287 while (length > 0) {
Qu Wenruo8df507c2021-04-22 19:02:46 +08001288 sublen = min_t(u64, length, fs_info->sectorsize);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001289 mapped_length = sublen;
Qu Wenruo4c664612021-09-15 15:17:16 +08001290 bioc = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001291
1292 /*
Qu Wenruo8df507c2021-04-22 19:02:46 +08001293 * With a length of sectorsize, each returned stripe represents
1294 * one mirror
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001295 */
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001296 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001297 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
Qu Wenruo4c664612021-09-15 15:17:16 +08001298 logical, &mapped_length, &bioc);
1299 if (ret || !bioc || mapped_length < sublen) {
1300 btrfs_put_bioc(bioc);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001301 btrfs_bio_counter_dec(fs_info);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001302 return -EIO;
1303 }
1304
Miao Xieaf8e2d12014-10-23 14:42:50 +08001305 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1306 if (!recover) {
Qu Wenruo4c664612021-09-15 15:17:16 +08001307 btrfs_put_bioc(bioc);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001308 btrfs_bio_counter_dec(fs_info);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001309 return -ENOMEM;
1310 }
1311
Elena Reshetova6f615012017-03-03 10:55:21 +02001312 refcount_set(&recover->refs, 1);
Qu Wenruo4c664612021-09-15 15:17:16 +08001313 recover->bioc = bioc;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001314 recover->map_length = mapped_length;
1315
Ashish Samant24731142016-04-29 18:33:59 -07001316 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001317
Qu Wenruo4c664612021-09-15 15:17:16 +08001318 nmirrors = min(scrub_nr_raid_mirrors(bioc), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001319
Miao Xieaf8e2d12014-10-23 14:42:50 +08001320 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001321 mirror_index++) {
1322 struct scrub_block *sblock;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001323 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001324
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001325 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001326 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001327
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001328 spage = kzalloc(sizeof(*spage), GFP_NOFS);
1329 if (!spage) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001330leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001331 spin_lock(&sctx->stat_lock);
1332 sctx->stat.malloc_errors++;
1333 spin_unlock(&sctx->stat_lock);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001334 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001335 return -ENOMEM;
1336 }
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001337 scrub_page_get(spage);
1338 sblock->pagev[page_index] = spage;
1339 spage->sblock = sblock;
1340 spage->flags = flags;
1341 spage->generation = generation;
1342 spage->logical = logical;
1343 spage->have_csum = have_csum;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001344 if (have_csum)
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001345 memcpy(spage->csum,
Zhao Lei4734b7e2015-08-19 22:39:18 +08001346 original_sblock->pagev[0]->csum,
David Sterba2ae0c2d2020-06-30 17:44:49 +02001347 sctx->fs_info->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001348
Zhao Lei10f11902015-01-20 15:11:43 +08001349 scrub_stripe_index_and_offset(logical,
Qu Wenruo4c664612021-09-15 15:17:16 +08001350 bioc->map_type,
1351 bioc->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001352 mapped_length,
Qu Wenruo4c664612021-09-15 15:17:16 +08001353 bioc->num_stripes -
1354 bioc->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001355 mirror_index,
1356 &stripe_index,
1357 &stripe_offset);
Qu Wenruo4c664612021-09-15 15:17:16 +08001358 spage->physical = bioc->stripes[stripe_index].physical +
Miao Xieaf8e2d12014-10-23 14:42:50 +08001359 stripe_offset;
Qu Wenruo4c664612021-09-15 15:17:16 +08001360 spage->dev = bioc->stripes[stripe_index].dev;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001361
Stefan Behrensff023aa2012-11-06 11:43:11 +01001362 BUG_ON(page_index >= original_sblock->page_count);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001363 spage->physical_for_dev_replace =
Stefan Behrensff023aa2012-11-06 11:43:11 +01001364 original_sblock->pagev[page_index]->
1365 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001366 /* for missing devices, dev->bdev is NULL */
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001367 spage->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001368 sblock->page_count++;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001369 spage->page = alloc_page(GFP_NOFS);
1370 if (!spage->page)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001371 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001372
1373 scrub_get_recover(recover);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001374 spage->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001375 }
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001376 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001377 length -= sublen;
1378 logical += sublen;
1379 page_index++;
1380 }
1381
1382 return 0;
1383}
1384
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001385static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001386{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001387 complete(bio->bi_private);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001388}
1389
Miao Xieaf8e2d12014-10-23 14:42:50 +08001390static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1391 struct bio *bio,
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001392 struct scrub_page *spage)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001393{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001394 DECLARE_COMPLETION_ONSTACK(done);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001395 int ret;
Liu Bo762221f2018-01-02 13:36:42 -07001396 int mirror_num;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001397
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001398 bio->bi_iter.bi_sector = spage->logical >> 9;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001399 bio->bi_private = &done;
1400 bio->bi_end_io = scrub_bio_wait_endio;
1401
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001402 mirror_num = spage->sblock->pagev[0]->mirror_num;
Qu Wenruo6a258d72021-09-23 14:00:09 +08001403 ret = raid56_parity_recover(bio, spage->recover->bioc,
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001404 spage->recover->map_length,
Liu Bo762221f2018-01-02 13:36:42 -07001405 mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001406 if (ret)
1407 return ret;
1408
Liu Bob4ff5ad2017-11-30 17:26:39 -07001409 wait_for_completion_io(&done);
1410 return blk_status_to_errno(bio->bi_status);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001411}
1412
Liu Bo6ca17652018-03-07 12:08:09 -07001413static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
1414 struct scrub_block *sblock)
1415{
1416 struct scrub_page *first_page = sblock->pagev[0];
1417 struct bio *bio;
1418 int page_num;
1419
1420 /* All pages in sblock belong to the same stripe on the same device. */
1421 ASSERT(first_page->dev);
1422 if (!first_page->dev->bdev)
1423 goto out;
1424
Qu Wenruoc3a3b192021-09-15 15:17:18 +08001425 bio = btrfs_bio_alloc(BIO_MAX_VECS);
Liu Bo6ca17652018-03-07 12:08:09 -07001426 bio_set_dev(bio, first_page->dev->bdev);
1427
1428 for (page_num = 0; page_num < sblock->page_count; page_num++) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001429 struct scrub_page *spage = sblock->pagev[page_num];
Liu Bo6ca17652018-03-07 12:08:09 -07001430
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001431 WARN_ON(!spage->page);
1432 bio_add_page(bio, spage->page, PAGE_SIZE, 0);
Liu Bo6ca17652018-03-07 12:08:09 -07001433 }
1434
1435 if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) {
1436 bio_put(bio);
1437 goto out;
1438 }
1439
1440 bio_put(bio);
1441
1442 scrub_recheck_block_checksum(sblock);
1443
1444 return;
1445out:
1446 for (page_num = 0; page_num < sblock->page_count; page_num++)
1447 sblock->pagev[page_num]->io_error = 1;
1448
1449 sblock->no_io_error_seen = 0;
1450}
1451
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001452/*
1453 * this function will check the on disk data for checksum errors, header
1454 * errors and read I/O errors. If any I/O errors happen, the exact pages
1455 * which are errored are marked as being bad. The goal is to enable scrub
1456 * to take those pages that are not errored from all the mirrors so that
1457 * the pages that are errored in the just handled mirror can be repaired.
1458 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001459static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001460 struct scrub_block *sblock,
1461 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001462{
1463 int page_num;
1464
1465 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001466
Liu Bo6ca17652018-03-07 12:08:09 -07001467 /* short cut for raid56 */
1468 if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0]))
1469 return scrub_recheck_block_on_raid56(fs_info, sblock);
1470
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001471 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1472 struct bio *bio;
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001473 struct scrub_page *spage = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001474
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001475 if (spage->dev->bdev == NULL) {
1476 spage->io_error = 1;
Stefan Behrensea9947b2012-05-04 15:16:07 -04001477 sblock->no_io_error_seen = 0;
1478 continue;
1479 }
1480
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001481 WARN_ON(!spage->page);
Qu Wenruoc3a3b192021-09-15 15:17:18 +08001482 bio = btrfs_bio_alloc(1);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001483 bio_set_dev(bio, spage->dev->bdev);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001484
Qu Wenruo8df507c2021-04-22 19:02:46 +08001485 bio_add_page(bio, spage->page, fs_info->sectorsize, 0);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001486 bio->bi_iter.bi_sector = spage->physical >> 9;
Liu Bo6ca17652018-03-07 12:08:09 -07001487 bio->bi_opf = REQ_OP_READ;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001488
Liu Bo6ca17652018-03-07 12:08:09 -07001489 if (btrfsic_submit_bio_wait(bio)) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001490 spage->io_error = 1;
Liu Bo6ca17652018-03-07 12:08:09 -07001491 sblock->no_io_error_seen = 0;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001492 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001493
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001494 bio_put(bio);
1495 }
1496
1497 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001498 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001499}
1500
Miao Xie17a9be22014-07-24 11:37:08 +08001501static inline int scrub_check_fsid(u8 fsid[],
1502 struct scrub_page *spage)
1503{
1504 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1505 int ret;
1506
Anand Jain44880fd2017-07-29 17:50:09 +08001507 ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Miao Xie17a9be22014-07-24 11:37:08 +08001508 return !ret;
1509}
1510
Zhao Leiba7cf982015-08-24 21:18:02 +08001511static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001512{
Zhao Leiba7cf982015-08-24 21:18:02 +08001513 sblock->header_error = 0;
1514 sblock->checksum_error = 0;
1515 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001516
Zhao Leiba7cf982015-08-24 21:18:02 +08001517 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1518 scrub_checksum_data(sblock);
1519 else
1520 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001521}
1522
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001523static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001524 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001525{
1526 int page_num;
1527 int ret = 0;
1528
1529 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1530 int ret_sub;
1531
1532 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1533 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001534 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001535 if (ret_sub)
1536 ret = ret_sub;
1537 }
1538
1539 return ret;
1540}
1541
1542static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1543 struct scrub_block *sblock_good,
1544 int page_num, int force_write)
1545{
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001546 struct scrub_page *spage_bad = sblock_bad->pagev[page_num];
1547 struct scrub_page *spage_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001548 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001549 const u32 sectorsize = fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001550
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001551 BUG_ON(spage_bad->page == NULL);
1552 BUG_ON(spage_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001553 if (force_write || sblock_bad->header_error ||
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001554 sblock_bad->checksum_error || spage_bad->io_error) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001555 struct bio *bio;
1556 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001557
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001558 if (!spage_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001559 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001560 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001561 return -EIO;
1562 }
1563
Qu Wenruoc3a3b192021-09-15 15:17:18 +08001564 bio = btrfs_bio_alloc(1);
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001565 bio_set_dev(bio, spage_bad->dev->bdev);
1566 bio->bi_iter.bi_sector = spage_bad->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02001567 bio->bi_opf = REQ_OP_WRITE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001568
Qu Wenruo8df507c2021-04-22 19:02:46 +08001569 ret = bio_add_page(bio, spage_good->page, sectorsize, 0);
1570 if (ret != sectorsize) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001571 bio_put(bio);
1572 return -EIO;
1573 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001574
Mike Christie4e49ea42016-06-05 14:31:41 -05001575 if (btrfsic_submit_bio_wait(bio)) {
Qu Wenruo261d2dc2020-11-03 21:31:01 +08001576 btrfs_dev_stat_inc_and_print(spage_bad->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001577 BTRFS_DEV_STAT_WRITE_ERRS);
David Sterbae37abe92018-04-04 17:20:52 +02001578 atomic64_inc(&fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001579 bio_put(bio);
1580 return -EIO;
1581 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001582 bio_put(bio);
1583 }
1584
1585 return 0;
1586}
1587
Stefan Behrensff023aa2012-11-06 11:43:11 +01001588static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1589{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001590 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001591 int page_num;
1592
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001593 /*
1594 * This block is used for the check of the parity on the source device,
1595 * so the data needn't be written into the destination device.
1596 */
1597 if (sblock->sparity)
1598 return;
1599
Stefan Behrensff023aa2012-11-06 11:43:11 +01001600 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1601 int ret;
1602
1603 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1604 if (ret)
David Sterbae37abe92018-04-04 17:20:52 +02001605 atomic64_inc(&fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001606 }
1607}
1608
1609static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1610 int page_num)
1611{
1612 struct scrub_page *spage = sblock->pagev[page_num];
1613
1614 BUG_ON(spage->page == NULL);
David Sterbaa8b3a892020-05-29 15:26:07 +02001615 if (spage->io_error)
1616 clear_page(page_address(spage->page));
Stefan Behrensff023aa2012-11-06 11:43:11 +01001617
Stefan Behrensff023aa2012-11-06 11:43:11 +01001618 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1619}
1620
Naohiro Aotade17add2021-02-04 19:22:13 +09001621static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
1622{
1623 int ret = 0;
1624 u64 length;
1625
1626 if (!btrfs_is_zoned(sctx->fs_info))
1627 return 0;
1628
Naohiro Aota7db1c5d2021-02-04 19:22:14 +09001629 if (!btrfs_dev_is_sequential(sctx->wr_tgtdev, physical))
1630 return 0;
1631
Naohiro Aotade17add2021-02-04 19:22:13 +09001632 if (sctx->write_pointer < physical) {
1633 length = physical - sctx->write_pointer;
1634
1635 ret = btrfs_zoned_issue_zeroout(sctx->wr_tgtdev,
1636 sctx->write_pointer, length);
1637 if (!ret)
1638 sctx->write_pointer = physical;
1639 }
1640 return ret;
1641}
1642
Stefan Behrensff023aa2012-11-06 11:43:11 +01001643static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1644 struct scrub_page *spage)
1645{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001646 struct scrub_bio *sbio;
1647 int ret;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001648 const u32 sectorsize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001649
David Sterba3fb99302017-05-16 19:10:32 +02001650 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001651again:
David Sterba3fb99302017-05-16 19:10:32 +02001652 if (!sctx->wr_curr_bio) {
1653 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001654 GFP_KERNEL);
David Sterba3fb99302017-05-16 19:10:32 +02001655 if (!sctx->wr_curr_bio) {
1656 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001657 return -ENOMEM;
1658 }
David Sterba3fb99302017-05-16 19:10:32 +02001659 sctx->wr_curr_bio->sctx = sctx;
1660 sctx->wr_curr_bio->page_count = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001661 }
David Sterba3fb99302017-05-16 19:10:32 +02001662 sbio = sctx->wr_curr_bio;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001663 if (sbio->page_count == 0) {
1664 struct bio *bio;
1665
Naohiro Aotade17add2021-02-04 19:22:13 +09001666 ret = fill_writer_pointer_gap(sctx,
1667 spage->physical_for_dev_replace);
1668 if (ret) {
1669 mutex_unlock(&sctx->wr_lock);
1670 return ret;
1671 }
1672
Stefan Behrensff023aa2012-11-06 11:43:11 +01001673 sbio->physical = spage->physical_for_dev_replace;
1674 sbio->logical = spage->logical;
David Sterba3fb99302017-05-16 19:10:32 +02001675 sbio->dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001676 bio = sbio->bio;
1677 if (!bio) {
Qu Wenruoc3a3b192021-09-15 15:17:18 +08001678 bio = btrfs_bio_alloc(sctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001679 sbio->bio = bio;
1680 }
1681
1682 bio->bi_private = sbio;
1683 bio->bi_end_io = scrub_wr_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001684 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001685 bio->bi_iter.bi_sector = sbio->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02001686 bio->bi_opf = REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001687 sbio->status = 0;
Qu Wenruo8df507c2021-04-22 19:02:46 +08001688 } else if (sbio->physical + sbio->page_count * sectorsize !=
Stefan Behrensff023aa2012-11-06 11:43:11 +01001689 spage->physical_for_dev_replace ||
Qu Wenruo8df507c2021-04-22 19:02:46 +08001690 sbio->logical + sbio->page_count * sectorsize !=
Stefan Behrensff023aa2012-11-06 11:43:11 +01001691 spage->logical) {
1692 scrub_wr_submit(sctx);
1693 goto again;
1694 }
1695
Qu Wenruo8df507c2021-04-22 19:02:46 +08001696 ret = bio_add_page(sbio->bio, spage->page, sectorsize, 0);
1697 if (ret != sectorsize) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001698 if (sbio->page_count < 1) {
1699 bio_put(sbio->bio);
1700 sbio->bio = NULL;
David Sterba3fb99302017-05-16 19:10:32 +02001701 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001702 return -EIO;
1703 }
1704 scrub_wr_submit(sctx);
1705 goto again;
1706 }
1707
1708 sbio->pagev[sbio->page_count] = spage;
1709 scrub_page_get(spage);
1710 sbio->page_count++;
David Sterba3fb99302017-05-16 19:10:32 +02001711 if (sbio->page_count == sctx->pages_per_wr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001712 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02001713 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001714
1715 return 0;
1716}
1717
1718static void scrub_wr_submit(struct scrub_ctx *sctx)
1719{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001720 struct scrub_bio *sbio;
1721
David Sterba3fb99302017-05-16 19:10:32 +02001722 if (!sctx->wr_curr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001723 return;
1724
David Sterba3fb99302017-05-16 19:10:32 +02001725 sbio = sctx->wr_curr_bio;
1726 sctx->wr_curr_bio = NULL;
Christoph Hellwig309dca302021-01-24 11:02:34 +01001727 WARN_ON(!sbio->bio->bi_bdev);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001728 scrub_pending_bio_inc(sctx);
1729 /* process all writes in a single worker thread. Then the block layer
1730 * orders the requests before sending them to the driver which
1731 * doubled the write performance on spinning disks when measured
1732 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001733 btrfsic_submit_bio(sbio->bio);
Naohiro Aotade17add2021-02-04 19:22:13 +09001734
1735 if (btrfs_is_zoned(sctx->fs_info))
Qu Wenruo8df507c2021-04-22 19:02:46 +08001736 sctx->write_pointer = sbio->physical + sbio->page_count *
1737 sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001738}
1739
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001740static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001741{
1742 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001743 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001744
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001745 sbio->status = bio->bi_status;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001746 sbio->bio = bio;
1747
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001748 btrfs_init_work(&sbio->work, scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001749 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001750}
1751
1752static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1753{
1754 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1755 struct scrub_ctx *sctx = sbio->sctx;
1756 int i;
1757
1758 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001759 if (sbio->status) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001760 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001761 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001762
1763 for (i = 0; i < sbio->page_count; i++) {
1764 struct scrub_page *spage = sbio->pagev[i];
1765
1766 spage->io_error = 1;
David Sterbae37abe92018-04-04 17:20:52 +02001767 atomic64_inc(&dev_replace->num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001768 }
1769 }
1770
1771 for (i = 0; i < sbio->page_count; i++)
1772 scrub_page_put(sbio->pagev[i]);
1773
1774 bio_put(sbio->bio);
1775 kfree(sbio);
1776 scrub_pending_bio_dec(sctx);
1777}
1778
1779static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001780{
1781 u64 flags;
1782 int ret;
1783
Zhao Leiba7cf982015-08-24 21:18:02 +08001784 /*
1785 * No need to initialize these stats currently,
1786 * because this function only use return value
1787 * instead of these stats value.
1788 *
1789 * Todo:
1790 * always use stats
1791 */
1792 sblock->header_error = 0;
1793 sblock->generation_error = 0;
1794 sblock->checksum_error = 0;
1795
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001796 WARN_ON(sblock->page_count < 1);
1797 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001798 ret = 0;
1799 if (flags & BTRFS_EXTENT_FLAG_DATA)
1800 ret = scrub_checksum_data(sblock);
1801 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1802 ret = scrub_checksum_tree_block(sblock);
1803 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1804 (void)scrub_checksum_super(sblock);
1805 else
1806 WARN_ON(1);
1807 if (ret)
1808 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001809
1810 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001811}
1812
1813static int scrub_checksum_data(struct scrub_block *sblock)
1814{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001815 struct scrub_ctx *sctx = sblock->sctx;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001816 struct btrfs_fs_info *fs_info = sctx->fs_info;
1817 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Arne Jansena2de7332011-03-08 14:14:00 +01001818 u8 csum[BTRFS_CSUM_SIZE];
David Sterbad41ebef2020-05-29 16:20:35 +02001819 struct scrub_page *spage;
David Sterbab0485252020-05-29 15:32:51 +02001820 char *kaddr;
Arne Jansena2de7332011-03-08 14:14:00 +01001821
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001822 BUG_ON(sblock->page_count < 1);
David Sterbad41ebef2020-05-29 16:20:35 +02001823 spage = sblock->pagev[0];
1824 if (!spage->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001825 return 0;
1826
David Sterbad41ebef2020-05-29 16:20:35 +02001827 kaddr = page_address(spage->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001828
David Sterba771aba02020-05-29 15:54:41 +02001829 shash->tfm = fs_info->csum_shash;
1830 crypto_shash_init(shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001831
Qu Wenruob29dca42020-12-02 14:48:10 +08001832 /*
1833 * In scrub_pages() and scrub_pages_for_parity() we ensure each spage
1834 * only contains one sector of data.
1835 */
1836 crypto_shash_digest(shash, kaddr, fs_info->sectorsize, csum);
1837
1838 if (memcmp(csum, spage->csum, fs_info->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001839 sblock->checksum_error = 1;
Zhao Leiba7cf982015-08-24 21:18:02 +08001840 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001841}
1842
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001843static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001844{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001845 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001846 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001847 struct btrfs_fs_info *fs_info = sctx->fs_info;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001848 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001849 u8 calculated_csum[BTRFS_CSUM_SIZE];
1850 u8 on_disk_csum[BTRFS_CSUM_SIZE];
Qu Wenruo53f32512020-12-02 14:48:09 +08001851 /*
1852 * This is done in sectorsize steps even for metadata as there's a
1853 * constraint for nodesize to be aligned to sectorsize. This will need
1854 * to change so we don't misuse data and metadata units like that.
1855 */
1856 const u32 sectorsize = sctx->fs_info->sectorsize;
1857 const int num_sectors = fs_info->nodesize >> fs_info->sectorsize_bits;
David Sterba521e1022020-05-29 15:54:41 +02001858 int i;
David Sterba100aa5d2020-05-29 16:20:35 +02001859 struct scrub_page *spage;
David Sterbab0485252020-05-29 15:32:51 +02001860 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001861
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001862 BUG_ON(sblock->page_count < 1);
Qu Wenruo53f32512020-12-02 14:48:09 +08001863
1864 /* Each member in pagev is just one block, not a full page */
1865 ASSERT(sblock->page_count == num_sectors);
1866
David Sterba100aa5d2020-05-29 16:20:35 +02001867 spage = sblock->pagev[0];
1868 kaddr = page_address(spage->page);
David Sterbab0485252020-05-29 15:32:51 +02001869 h = (struct btrfs_header *)kaddr;
David Sterba2ae0c2d2020-06-30 17:44:49 +02001870 memcpy(on_disk_csum, h->csum, sctx->fs_info->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001871
1872 /*
1873 * we don't use the getter functions here, as we
1874 * a) don't have an extent buffer and
1875 * b) the page is already kmapped
1876 */
David Sterba100aa5d2020-05-29 16:20:35 +02001877 if (spage->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08001878 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001879
David Sterba100aa5d2020-05-29 16:20:35 +02001880 if (spage->generation != btrfs_stack_header_generation(h)) {
Zhao Leiba7cf982015-08-24 21:18:02 +08001881 sblock->header_error = 1;
1882 sblock->generation_error = 1;
1883 }
Arne Jansena2de7332011-03-08 14:14:00 +01001884
David Sterba100aa5d2020-05-29 16:20:35 +02001885 if (!scrub_check_fsid(h->fsid, spage))
Zhao Leiba7cf982015-08-24 21:18:02 +08001886 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001887
1888 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1889 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08001890 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001891
David Sterba521e1022020-05-29 15:54:41 +02001892 shash->tfm = fs_info->csum_shash;
1893 crypto_shash_init(shash);
1894 crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
Qu Wenruo53f32512020-12-02 14:48:09 +08001895 sectorsize - BTRFS_CSUM_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001896
Qu Wenruo53f32512020-12-02 14:48:09 +08001897 for (i = 1; i < num_sectors; i++) {
David Sterba521e1022020-05-29 15:54:41 +02001898 kaddr = page_address(sblock->pagev[i]->page);
Qu Wenruo53f32512020-12-02 14:48:09 +08001899 crypto_shash_update(shash, kaddr, sectorsize);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001900 }
1901
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001902 crypto_shash_final(shash, calculated_csum);
David Sterba2ae0c2d2020-06-30 17:44:49 +02001903 if (memcmp(calculated_csum, on_disk_csum, sctx->fs_info->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001904 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001905
Zhao Leiba7cf982015-08-24 21:18:02 +08001906 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001907}
1908
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001909static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001910{
1911 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001912 struct scrub_ctx *sctx = sblock->sctx;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001913 struct btrfs_fs_info *fs_info = sctx->fs_info;
1914 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001915 u8 calculated_csum[BTRFS_CSUM_SIZE];
David Sterbac7460542020-05-29 15:47:05 +02001916 struct scrub_page *spage;
David Sterbab0485252020-05-29 15:32:51 +02001917 char *kaddr;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001918 int fail_gen = 0;
1919 int fail_cor = 0;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02001920
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001921 BUG_ON(sblock->page_count < 1);
David Sterbac7460542020-05-29 15:47:05 +02001922 spage = sblock->pagev[0];
1923 kaddr = page_address(spage->page);
David Sterbab0485252020-05-29 15:32:51 +02001924 s = (struct btrfs_super_block *)kaddr;
Arne Jansena2de7332011-03-08 14:14:00 +01001925
David Sterbac7460542020-05-29 15:47:05 +02001926 if (spage->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001927 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001928
David Sterbac7460542020-05-29 15:47:05 +02001929 if (spage->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001930 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001931
David Sterbac7460542020-05-29 15:47:05 +02001932 if (!scrub_check_fsid(s->fsid, spage))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001933 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001934
David Sterba83cf6d52020-05-29 15:40:36 +02001935 shash->tfm = fs_info->csum_shash;
1936 crypto_shash_init(shash);
1937 crypto_shash_digest(shash, kaddr + BTRFS_CSUM_SIZE,
1938 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, calculated_csum);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001939
David Sterba2ae0c2d2020-06-30 17:44:49 +02001940 if (memcmp(calculated_csum, s->csum, sctx->fs_info->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001941 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001942
Stefan Behrens442a4f62012-05-25 16:06:08 +02001943 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001944 /*
1945 * if we find an error in a super block, we just report it.
1946 * They will get written with the next transaction commit
1947 * anyway
1948 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001949 spin_lock(&sctx->stat_lock);
1950 ++sctx->stat.super_errors;
1951 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001952 if (fail_cor)
David Sterbac7460542020-05-29 15:47:05 +02001953 btrfs_dev_stat_inc_and_print(spage->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001954 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1955 else
David Sterbac7460542020-05-29 15:47:05 +02001956 btrfs_dev_stat_inc_and_print(spage->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001957 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001958 }
1959
Stefan Behrens442a4f62012-05-25 16:06:08 +02001960 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001961}
1962
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001963static void scrub_block_get(struct scrub_block *sblock)
1964{
Elena Reshetova186debd2017-03-03 10:55:23 +02001965 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001966}
1967
1968static void scrub_block_put(struct scrub_block *sblock)
1969{
Elena Reshetova186debd2017-03-03 10:55:23 +02001970 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001971 int i;
1972
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001973 if (sblock->sparity)
1974 scrub_parity_put(sblock->sparity);
1975
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001976 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001977 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001978 kfree(sblock);
1979 }
1980}
1981
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001982static void scrub_page_get(struct scrub_page *spage)
1983{
Zhao Lei57019342015-01-20 15:11:45 +08001984 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001985}
1986
1987static void scrub_page_put(struct scrub_page *spage)
1988{
Zhao Lei57019342015-01-20 15:11:45 +08001989 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001990 if (spage->page)
1991 __free_page(spage->page);
1992 kfree(spage);
1993 }
1994}
1995
David Sterbaeb3b5052019-10-09 13:58:13 +02001996/*
1997 * Throttling of IO submission, bandwidth-limit based, the timeslice is 1
1998 * second. Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max.
1999 */
2000static void scrub_throttle(struct scrub_ctx *sctx)
2001{
2002 const int time_slice = 1000;
2003 struct scrub_bio *sbio;
2004 struct btrfs_device *device;
2005 s64 delta;
2006 ktime_t now;
2007 u32 div;
2008 u64 bwlimit;
2009
2010 sbio = sctx->bios[sctx->curr];
2011 device = sbio->dev;
2012 bwlimit = READ_ONCE(device->scrub_speed_max);
2013 if (bwlimit == 0)
2014 return;
2015
2016 /*
2017 * Slice is divided into intervals when the IO is submitted, adjust by
2018 * bwlimit and maximum of 64 intervals.
2019 */
2020 div = max_t(u32, 1, (u32)(bwlimit / (16 * 1024 * 1024)));
2021 div = min_t(u32, 64, div);
2022
2023 /* Start new epoch, set deadline */
2024 now = ktime_get();
2025 if (sctx->throttle_deadline == 0) {
2026 sctx->throttle_deadline = ktime_add_ms(now, time_slice / div);
2027 sctx->throttle_sent = 0;
2028 }
2029
2030 /* Still in the time to send? */
2031 if (ktime_before(now, sctx->throttle_deadline)) {
2032 /* If current bio is within the limit, send it */
2033 sctx->throttle_sent += sbio->bio->bi_iter.bi_size;
2034 if (sctx->throttle_sent <= div_u64(bwlimit, div))
2035 return;
2036
2037 /* We're over the limit, sleep until the rest of the slice */
2038 delta = ktime_ms_delta(sctx->throttle_deadline, now);
2039 } else {
2040 /* New request after deadline, start new epoch */
2041 delta = 0;
2042 }
2043
2044 if (delta) {
2045 long timeout;
2046
2047 timeout = div_u64(delta * HZ, 1000);
2048 schedule_timeout_interruptible(timeout);
2049 }
2050
2051 /* Next call will start the deadline period */
2052 sctx->throttle_deadline = 0;
2053}
2054
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002055static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002056{
2057 struct scrub_bio *sbio;
2058
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002059 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002060 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002061
David Sterbaeb3b5052019-10-09 13:58:13 +02002062 scrub_throttle(sctx);
2063
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002064 sbio = sctx->bios[sctx->curr];
2065 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002066 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002067 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002068}
2069
Stefan Behrensff023aa2012-11-06 11:43:11 +01002070static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2071 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002072{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002073 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002074 struct scrub_bio *sbio;
Qu Wenruo8df507c2021-04-22 19:02:46 +08002075 const u32 sectorsize = sctx->fs_info->sectorsize;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002076 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002077
2078again:
2079 /*
2080 * grab a fresh bio or wait for one to become available
2081 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002082 while (sctx->curr == -1) {
2083 spin_lock(&sctx->list_lock);
2084 sctx->curr = sctx->first_free;
2085 if (sctx->curr != -1) {
2086 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2087 sctx->bios[sctx->curr]->next_free = -1;
2088 sctx->bios[sctx->curr]->page_count = 0;
2089 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002090 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002091 spin_unlock(&sctx->list_lock);
2092 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002093 }
2094 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002095 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002096 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002097 struct bio *bio;
2098
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002099 sbio->physical = spage->physical;
2100 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002101 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002102 bio = sbio->bio;
2103 if (!bio) {
Qu Wenruoc3a3b192021-09-15 15:17:18 +08002104 bio = btrfs_bio_alloc(sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002105 sbio->bio = bio;
2106 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002107
2108 bio->bi_private = sbio;
2109 bio->bi_end_io = scrub_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002110 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002111 bio->bi_iter.bi_sector = sbio->physical >> 9;
David Sterbaebcc3262018-06-29 10:56:53 +02002112 bio->bi_opf = REQ_OP_READ;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002113 sbio->status = 0;
Qu Wenruo8df507c2021-04-22 19:02:46 +08002114 } else if (sbio->physical + sbio->page_count * sectorsize !=
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002115 spage->physical ||
Qu Wenruo8df507c2021-04-22 19:02:46 +08002116 sbio->logical + sbio->page_count * sectorsize !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002117 spage->logical ||
2118 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002119 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002120 goto again;
2121 }
2122
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002123 sbio->pagev[sbio->page_count] = spage;
Qu Wenruo8df507c2021-04-22 19:02:46 +08002124 ret = bio_add_page(sbio->bio, spage->page, sectorsize, 0);
2125 if (ret != sectorsize) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002126 if (sbio->page_count < 1) {
2127 bio_put(sbio->bio);
2128 sbio->bio = NULL;
2129 return -EIO;
2130 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002131 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002132 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002133 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002134
Stefan Behrensff023aa2012-11-06 11:43:11 +01002135 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002136 atomic_inc(&sblock->outstanding_pages);
2137 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002138 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002139 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002140
2141 return 0;
2142}
2143
Linus Torvalds22365972015-09-05 15:14:43 -07002144static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002145{
2146 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002147 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002148
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002149 if (bio->bi_status)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002150 sblock->no_io_error_seen = 0;
2151
Scott Talbert46732722016-05-09 09:14:28 -04002152 bio_put(bio);
2153
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002154 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2155}
2156
2157static void scrub_missing_raid56_worker(struct btrfs_work *work)
2158{
2159 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2160 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002161 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002162 u64 logical;
2163 struct btrfs_device *dev;
2164
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002165 logical = sblock->pagev[0]->logical;
2166 dev = sblock->pagev[0]->dev;
2167
Zhao Leiaffe4a52015-08-24 21:32:06 +08002168 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002169 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002170
2171 if (!sblock->no_io_error_seen) {
2172 spin_lock(&sctx->stat_lock);
2173 sctx->stat.read_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 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002177 logical, rcu_str_deref(dev->name));
2178 } else if (sblock->header_error || sblock->checksum_error) {
2179 spin_lock(&sctx->stat_lock);
2180 sctx->stat.uncorrectable_errors++;
2181 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002182 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002183 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002184 logical, rcu_str_deref(dev->name));
2185 } else {
2186 scrub_write_block_to_dev_replace(sblock);
2187 }
2188
David Sterba2073c4c2017-03-31 17:12:51 +02002189 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002190 mutex_lock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002191 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002192 mutex_unlock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002193 }
2194
Omar Sandoval57d4f0b2019-09-16 11:30:56 -07002195 scrub_block_put(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002196 scrub_pending_bio_dec(sctx);
2197}
2198
2199static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2200{
2201 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002202 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002203 u64 length = sblock->page_count * PAGE_SIZE;
2204 u64 logical = sblock->pagev[0]->logical;
Qu Wenruo4c664612021-09-15 15:17:16 +08002205 struct btrfs_io_context *bioc = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002206 struct bio *bio;
2207 struct btrfs_raid_bio *rbio;
2208 int ret;
2209 int i;
2210
Qu Wenruoae6529c2017-03-29 09:33:21 +08002211 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002212 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
Qu Wenruo4c664612021-09-15 15:17:16 +08002213 &length, &bioc);
2214 if (ret || !bioc || !bioc->raid_map)
2215 goto bioc_out;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002216
2217 if (WARN_ON(!sctx->is_dev_replace ||
Qu Wenruo4c664612021-09-15 15:17:16 +08002218 !(bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002219 /*
2220 * We shouldn't be scrubbing a missing device. Even for dev
2221 * replace, we should only get here for RAID 5/6. We either
2222 * managed to mount something with no mirrors remaining or
2223 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2224 */
Qu Wenruo4c664612021-09-15 15:17:16 +08002225 goto bioc_out;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002226 }
2227
Qu Wenruoc3a3b192021-09-15 15:17:18 +08002228 bio = btrfs_bio_alloc(BIO_MAX_VECS);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002229 bio->bi_iter.bi_sector = logical >> 9;
2230 bio->bi_private = sblock;
2231 bio->bi_end_io = scrub_missing_raid56_end_io;
2232
Qu Wenruo6a258d72021-09-23 14:00:09 +08002233 rbio = raid56_alloc_missing_rbio(bio, bioc, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002234 if (!rbio)
2235 goto rbio_out;
2236
2237 for (i = 0; i < sblock->page_count; i++) {
2238 struct scrub_page *spage = sblock->pagev[i];
2239
2240 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2241 }
2242
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002243 btrfs_init_work(&sblock->work, scrub_missing_raid56_worker, NULL, NULL);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002244 scrub_block_get(sblock);
2245 scrub_pending_bio_inc(sctx);
2246 raid56_submit_missing_rbio(rbio);
2247 return;
2248
2249rbio_out:
2250 bio_put(bio);
Qu Wenruo4c664612021-09-15 15:17:16 +08002251bioc_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002252 btrfs_bio_counter_dec(fs_info);
Qu Wenruo4c664612021-09-15 15:17:16 +08002253 btrfs_put_bioc(bioc);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002254 spin_lock(&sctx->stat_lock);
2255 sctx->stat.malloc_errors++;
2256 spin_unlock(&sctx->stat_lock);
2257}
2258
Qu Wenruofa485d22020-12-02 14:48:07 +08002259static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u32 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002260 u64 physical, struct btrfs_device *dev, u64 flags,
Qu Wenruo96e63a42020-11-03 21:31:02 +08002261 u64 gen, int mirror_num, u8 *csum,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002262 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002263{
2264 struct scrub_block *sblock;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002265 const u32 sectorsize = sctx->fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002266 int index;
2267
David Sterba58c4e172016-02-11 10:49:42 +01002268 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002269 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002270 spin_lock(&sctx->stat_lock);
2271 sctx->stat.malloc_errors++;
2272 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002273 return -ENOMEM;
2274 }
2275
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002276 /* one ref inside this function, plus one for each page added to
2277 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002278 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002279 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002280 sblock->no_io_error_seen = 1;
2281
2282 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002283 struct scrub_page *spage;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002284 /*
2285 * Here we will allocate one page for one sector to scrub.
2286 * This is fine if PAGE_SIZE == sectorsize, but will cost
2287 * more memory for PAGE_SIZE > sectorsize case.
2288 */
2289 u32 l = min(sectorsize, len);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002290
David Sterba58c4e172016-02-11 10:49:42 +01002291 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002292 if (!spage) {
2293leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002294 spin_lock(&sctx->stat_lock);
2295 sctx->stat.malloc_errors++;
2296 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002297 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002298 return -ENOMEM;
2299 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002300 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2301 scrub_page_get(spage);
2302 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002303 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002304 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002305 spage->flags = flags;
2306 spage->generation = gen;
2307 spage->logical = logical;
2308 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002309 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002310 spage->mirror_num = mirror_num;
2311 if (csum) {
2312 spage->have_csum = 1;
David Sterba2ae0c2d2020-06-30 17:44:49 +02002313 memcpy(spage->csum, csum, sctx->fs_info->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002314 } else {
2315 spage->have_csum = 0;
2316 }
2317 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002318 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002319 if (!spage->page)
2320 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002321 len -= l;
2322 logical += l;
2323 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002324 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002325 }
2326
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002327 WARN_ON(sblock->page_count == 0);
Anand Jaine6e674b2017-12-04 12:54:54 +08002328 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002329 /*
2330 * This case should only be hit for RAID 5/6 device replace. See
2331 * the comment in scrub_missing_raid56_pages() for details.
2332 */
2333 scrub_missing_raid56_pages(sblock);
2334 } else {
2335 for (index = 0; index < sblock->page_count; index++) {
2336 struct scrub_page *spage = sblock->pagev[index];
2337 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002338
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002339 ret = scrub_add_page_to_rd_bio(sctx, spage);
2340 if (ret) {
2341 scrub_block_put(sblock);
2342 return ret;
2343 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002344 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002345
Qu Wenruo96e63a42020-11-03 21:31:02 +08002346 if (flags & BTRFS_EXTENT_FLAG_SUPER)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002347 scrub_submit(sctx);
2348 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002349
2350 /* last one frees, either here or in bio completion for last page */
2351 scrub_block_put(sblock);
2352 return 0;
2353}
2354
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002355static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002356{
2357 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002358 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002359
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002360 sbio->status = bio->bi_status;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002361 sbio->bio = bio;
2362
Qu Wenruo0339ef22014-02-28 10:46:17 +08002363 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002364}
2365
2366static void scrub_bio_end_io_worker(struct btrfs_work *work)
2367{
2368 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002369 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002370 int i;
2371
Stefan Behrensff023aa2012-11-06 11:43:11 +01002372 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002373 if (sbio->status) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002374 for (i = 0; i < sbio->page_count; i++) {
2375 struct scrub_page *spage = sbio->pagev[i];
2376
2377 spage->io_error = 1;
2378 spage->sblock->no_io_error_seen = 0;
2379 }
2380 }
2381
2382 /* now complete the scrub_block items that have all pages completed */
2383 for (i = 0; i < sbio->page_count; i++) {
2384 struct scrub_page *spage = sbio->pagev[i];
2385 struct scrub_block *sblock = spage->sblock;
2386
2387 if (atomic_dec_and_test(&sblock->outstanding_pages))
2388 scrub_block_complete(sblock);
2389 scrub_block_put(sblock);
2390 }
2391
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002392 bio_put(sbio->bio);
2393 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002394 spin_lock(&sctx->list_lock);
2395 sbio->next_free = sctx->first_free;
2396 sctx->first_free = sbio->index;
2397 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002398
David Sterba2073c4c2017-03-31 17:12:51 +02002399 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002400 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002401 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002402 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002403 }
2404
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002405 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002406}
2407
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002408static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2409 unsigned long *bitmap,
Qu Wenruofa485d22020-12-02 14:48:07 +08002410 u64 start, u32 len)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002411{
Liu Bo972d7212017-04-03 13:45:33 -07002412 u64 offset;
David Sterba7736b0a2017-03-31 18:02:48 +02002413 u32 nsectors;
David Sterbaab108d92020-07-01 20:45:04 +02002414 u32 sectorsize_bits = sparity->sctx->fs_info->sectorsize_bits;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002415
2416 if (len >= sparity->stripe_len) {
2417 bitmap_set(bitmap, 0, sparity->nsectors);
2418 return;
2419 }
2420
2421 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002422 start = div64_u64_rem(start, sparity->stripe_len, &offset);
David Sterbaab108d92020-07-01 20:45:04 +02002423 offset = offset >> sectorsize_bits;
Qu Wenruofa485d22020-12-02 14:48:07 +08002424 nsectors = len >> sectorsize_bits;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002425
2426 if (offset + nsectors <= sparity->nsectors) {
2427 bitmap_set(bitmap, offset, nsectors);
2428 return;
2429 }
2430
2431 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2432 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2433}
2434
2435static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002436 u64 start, u32 len)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002437{
2438 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2439}
2440
2441static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002442 u64 start, u32 len)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002443{
2444 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2445}
2446
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002447static void scrub_block_complete(struct scrub_block *sblock)
2448{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002449 int corrupted = 0;
2450
Stefan Behrensff023aa2012-11-06 11:43:11 +01002451 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002452 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002453 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002454 } else {
2455 /*
2456 * if has checksum error, write via repair mechanism in
2457 * dev replace case, otherwise write here in dev replace
2458 * case.
2459 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002460 corrupted = scrub_checksum(sblock);
2461 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002462 scrub_write_block_to_dev_replace(sblock);
2463 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002464
2465 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2466 u64 start = sblock->pagev[0]->logical;
2467 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
Qu Wenruo8df507c2021-04-22 19:02:46 +08002468 sblock->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002469
Qu Wenruofa485d22020-12-02 14:48:07 +08002470 ASSERT(end - start <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002471 scrub_parity_mark_sectors_error(sblock->sparity,
2472 start, end - start);
2473 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002474}
2475
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002476static void drop_csum_range(struct scrub_ctx *sctx, struct btrfs_ordered_sum *sum)
2477{
2478 sctx->stat.csum_discards += sum->len >> sctx->fs_info->sectorsize_bits;
2479 list_del(&sum->list);
2480 kfree(sum);
2481}
2482
2483/*
2484 * Find the desired csum for range [logical, logical + sectorsize), and store
2485 * the csum into @csum.
2486 *
2487 * The search source is sctx->csum_list, which is a pre-populated list
David Sterba1a9fd412021-05-21 17:42:23 +02002488 * storing bytenr ordered csum ranges. We're responsible to cleanup any range
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002489 * that is before @logical.
2490 *
2491 * Return 0 if there is no csum for the range.
2492 * Return 1 if there is csum for the range and copied to @csum.
2493 */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002494static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002495{
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002496 bool found = false;
Arne Jansena2de7332011-03-08 14:14:00 +01002497
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002498 while (!list_empty(&sctx->csum_list)) {
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002499 struct btrfs_ordered_sum *sum = NULL;
2500 unsigned long index;
2501 unsigned long num_sectors;
2502
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002503 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002504 struct btrfs_ordered_sum, list);
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002505 /* The current csum range is beyond our range, no csum found */
Arne Jansena2de7332011-03-08 14:14:00 +01002506 if (sum->bytenr > logical)
Arne Jansena2de7332011-03-08 14:14:00 +01002507 break;
2508
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002509 /*
2510 * The current sum is before our bytenr, since scrub is always
2511 * done in bytenr order, the csum will never be used anymore,
2512 * clean it up so that later calls won't bother with the range,
2513 * and continue search the next range.
2514 */
2515 if (sum->bytenr + sum->len <= logical) {
2516 drop_csum_range(sctx, sum);
2517 continue;
2518 }
2519
2520 /* Now the csum range covers our bytenr, copy the csum */
2521 found = true;
2522 index = (logical - sum->bytenr) >> sctx->fs_info->sectorsize_bits;
2523 num_sectors = sum->len >> sctx->fs_info->sectorsize_bits;
2524
2525 memcpy(csum, sum->sums + index * sctx->fs_info->csum_size,
2526 sctx->fs_info->csum_size);
2527
2528 /* Cleanup the range if we're at the end of the csum range */
2529 if (index == num_sectors - 1)
2530 drop_csum_range(sctx, sum);
2531 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002532 }
Qu Wenruo480a8ec2020-11-03 21:31:04 +08002533 if (!found)
Arne Jansena2de7332011-03-08 14:14:00 +01002534 return 0;
Miao Xief51a4a12013-06-19 10:36:09 +08002535 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002536}
2537
2538/* scrub extent tries to collect up to 64 kB for each bio */
Liu Bo6ca17652018-03-07 12:08:09 -07002539static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map,
Qu Wenruofa485d22020-12-02 14:48:07 +08002540 u64 logical, u32 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002541 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002542 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002543{
2544 int ret;
2545 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002546 u32 blocksize;
2547
2548 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002549 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2550 blocksize = map->stripe_len;
2551 else
2552 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002553 spin_lock(&sctx->stat_lock);
2554 sctx->stat.data_extents_scrubbed++;
2555 sctx->stat.data_bytes_scrubbed += len;
2556 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002557 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002558 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
2559 blocksize = map->stripe_len;
2560 else
2561 blocksize = sctx->fs_info->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002562 spin_lock(&sctx->stat_lock);
2563 sctx->stat.tree_extents_scrubbed++;
2564 sctx->stat.tree_bytes_scrubbed += len;
2565 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002566 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002567 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002568 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002569 }
Arne Jansena2de7332011-03-08 14:14:00 +01002570
2571 while (len) {
Qu Wenruofa485d22020-12-02 14:48:07 +08002572 u32 l = min(len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002573 int have_csum = 0;
2574
2575 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2576 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002577 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002578 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002579 ++sctx->stat.no_csum;
Arne Jansena2de7332011-03-08 14:14:00 +01002580 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002581 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Qu Wenruo96e63a42020-11-03 21:31:02 +08002582 mirror_num, have_csum ? csum : NULL,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002583 physical_for_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002584 if (ret)
2585 return ret;
2586 len -= l;
2587 logical += l;
2588 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002589 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002590 }
2591 return 0;
2592}
2593
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002594static int scrub_pages_for_parity(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002595 u64 logical, u32 len,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002596 u64 physical, struct btrfs_device *dev,
2597 u64 flags, u64 gen, int mirror_num, u8 *csum)
2598{
2599 struct scrub_ctx *sctx = sparity->sctx;
2600 struct scrub_block *sblock;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002601 const u32 sectorsize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002602 int index;
2603
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002604 ASSERT(IS_ALIGNED(len, sectorsize));
2605
David Sterba58c4e172016-02-11 10:49:42 +01002606 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002607 if (!sblock) {
2608 spin_lock(&sctx->stat_lock);
2609 sctx->stat.malloc_errors++;
2610 spin_unlock(&sctx->stat_lock);
2611 return -ENOMEM;
2612 }
2613
2614 /* one ref inside this function, plus one for each page added to
2615 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002616 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002617 sblock->sctx = sctx;
2618 sblock->no_io_error_seen = 1;
2619 sblock->sparity = sparity;
2620 scrub_parity_get(sparity);
2621
2622 for (index = 0; len > 0; index++) {
2623 struct scrub_page *spage;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002624
David Sterba58c4e172016-02-11 10:49:42 +01002625 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002626 if (!spage) {
2627leave_nomem:
2628 spin_lock(&sctx->stat_lock);
2629 sctx->stat.malloc_errors++;
2630 spin_unlock(&sctx->stat_lock);
2631 scrub_block_put(sblock);
2632 return -ENOMEM;
2633 }
2634 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2635 /* For scrub block */
2636 scrub_page_get(spage);
2637 sblock->pagev[index] = spage;
2638 /* For scrub parity */
2639 scrub_page_get(spage);
2640 list_add_tail(&spage->list, &sparity->spages);
2641 spage->sblock = sblock;
2642 spage->dev = dev;
2643 spage->flags = flags;
2644 spage->generation = gen;
2645 spage->logical = logical;
2646 spage->physical = physical;
2647 spage->mirror_num = mirror_num;
2648 if (csum) {
2649 spage->have_csum = 1;
David Sterba2ae0c2d2020-06-30 17:44:49 +02002650 memcpy(spage->csum, csum, sctx->fs_info->csum_size);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002651 } else {
2652 spage->have_csum = 0;
2653 }
2654 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002655 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002656 if (!spage->page)
2657 goto leave_nomem;
Qu Wenruod0a7a9c2020-12-02 14:48:08 +08002658
2659
2660 /* Iterate over the stripe range in sectorsize steps */
2661 len -= sectorsize;
2662 logical += sectorsize;
2663 physical += sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002664 }
2665
2666 WARN_ON(sblock->page_count == 0);
2667 for (index = 0; index < sblock->page_count; index++) {
2668 struct scrub_page *spage = sblock->pagev[index];
2669 int ret;
2670
2671 ret = scrub_add_page_to_rd_bio(sctx, spage);
2672 if (ret) {
2673 scrub_block_put(sblock);
2674 return ret;
2675 }
2676 }
2677
2678 /* last one frees, either here or in bio completion for last page */
2679 scrub_block_put(sblock);
2680 return 0;
2681}
2682
2683static int scrub_extent_for_parity(struct scrub_parity *sparity,
Qu Wenruofa485d22020-12-02 14:48:07 +08002684 u64 logical, u32 len,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002685 u64 physical, struct btrfs_device *dev,
2686 u64 flags, u64 gen, int mirror_num)
2687{
2688 struct scrub_ctx *sctx = sparity->sctx;
2689 int ret;
2690 u8 csum[BTRFS_CSUM_SIZE];
2691 u32 blocksize;
2692
Anand Jaine6e674b2017-12-04 12:54:54 +08002693 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval4a770892015-06-19 11:52:52 -07002694 scrub_parity_mark_sectors_error(sparity, logical, len);
2695 return 0;
2696 }
2697
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002698 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Liu Bo6ca17652018-03-07 12:08:09 -07002699 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002700 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Liu Bo6ca17652018-03-07 12:08:09 -07002701 blocksize = sparity->stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002702 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002703 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002704 WARN_ON(1);
2705 }
2706
2707 while (len) {
Qu Wenruofa485d22020-12-02 14:48:07 +08002708 u32 l = min(len, blocksize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002709 int have_csum = 0;
2710
2711 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2712 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002713 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002714 if (have_csum == 0)
2715 goto skip;
2716 }
2717 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2718 flags, gen, mirror_num,
2719 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002720 if (ret)
2721 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002722skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002723 len -= l;
2724 logical += l;
2725 physical += l;
2726 }
2727 return 0;
2728}
2729
Wang Shilong3b080b22014-04-01 18:01:43 +08002730/*
2731 * Given a physical address, this will calculate it's
2732 * logical offset. if this is a parity stripe, it will return
2733 * the most left data stripe's logical offset.
2734 *
2735 * return 0 if it is a data stripe, 1 means parity stripe.
2736 */
2737static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002738 struct map_lookup *map, u64 *offset,
2739 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002740{
2741 int i;
2742 int j = 0;
2743 u64 stripe_nr;
2744 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002745 u32 stripe_index;
2746 u32 rot;
David Sterbacff82672019-05-17 11:43:45 +02002747 const int data_stripes = nr_data_stripes(map);
Wang Shilong3b080b22014-04-01 18:01:43 +08002748
David Sterbacff82672019-05-17 11:43:45 +02002749 last_offset = (physical - map->stripes[num].physical) * data_stripes;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002750 if (stripe_start)
2751 *stripe_start = last_offset;
2752
Wang Shilong3b080b22014-04-01 18:01:43 +08002753 *offset = last_offset;
David Sterbacff82672019-05-17 11:43:45 +02002754 for (i = 0; i < data_stripes; i++) {
Wang Shilong3b080b22014-04-01 18:01:43 +08002755 *offset = last_offset + i * map->stripe_len;
2756
Liu Bo42c61ab2017-04-03 13:45:24 -07002757 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbacff82672019-05-17 11:43:45 +02002758 stripe_nr = div_u64(stripe_nr, data_stripes);
Wang Shilong3b080b22014-04-01 18:01:43 +08002759
2760 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002761 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002762 /* calculate which stripe this data locates */
2763 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002764 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002765 if (stripe_index == num)
2766 return 0;
2767 if (stripe_index < num)
2768 j++;
2769 }
2770 *offset = last_offset + j * map->stripe_len;
2771 return 1;
2772}
2773
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002774static void scrub_free_parity(struct scrub_parity *sparity)
2775{
2776 struct scrub_ctx *sctx = sparity->sctx;
2777 struct scrub_page *curr, *next;
2778 int nbits;
2779
2780 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2781 if (nbits) {
2782 spin_lock(&sctx->stat_lock);
2783 sctx->stat.read_errors += nbits;
2784 sctx->stat.uncorrectable_errors += nbits;
2785 spin_unlock(&sctx->stat_lock);
2786 }
2787
2788 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2789 list_del_init(&curr->list);
2790 scrub_page_put(curr);
2791 }
2792
2793 kfree(sparity);
2794}
2795
Zhao Lei20b2e302015-06-04 20:09:15 +08002796static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2797{
2798 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2799 work);
2800 struct scrub_ctx *sctx = sparity->sctx;
2801
2802 scrub_free_parity(sparity);
2803 scrub_pending_bio_dec(sctx);
2804}
2805
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002806static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002807{
2808 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002809 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002810
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002811 if (bio->bi_status)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002812 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2813 sparity->nsectors);
2814
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002815 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002816
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002817 btrfs_init_work(&sparity->work, scrub_parity_bio_endio_worker, NULL,
2818 NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002819 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002820}
2821
2822static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2823{
2824 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002825 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002826 struct bio *bio;
2827 struct btrfs_raid_bio *rbio;
Qu Wenruo4c664612021-09-15 15:17:16 +08002828 struct btrfs_io_context *bioc = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002829 u64 length;
2830 int ret;
2831
2832 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2833 sparity->nsectors))
2834 goto out;
2835
Zhao Leia0dd59d2015-07-21 15:42:26 +08002836 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08002837
2838 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002839 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
Qu Wenruo4c664612021-09-15 15:17:16 +08002840 &length, &bioc);
2841 if (ret || !bioc || !bioc->raid_map)
2842 goto bioc_out;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002843
Qu Wenruoc3a3b192021-09-15 15:17:18 +08002844 bio = btrfs_bio_alloc(BIO_MAX_VECS);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002845 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2846 bio->bi_private = sparity;
2847 bio->bi_end_io = scrub_parity_bio_endio;
2848
Qu Wenruo6a258d72021-09-23 14:00:09 +08002849 rbio = raid56_parity_alloc_scrub_rbio(bio, bioc, length,
2850 sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002851 sparity->dbitmap,
2852 sparity->nsectors);
2853 if (!rbio)
2854 goto rbio_out;
2855
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002856 scrub_pending_bio_inc(sctx);
2857 raid56_parity_submit_scrub_rbio(rbio);
2858 return;
2859
2860rbio_out:
2861 bio_put(bio);
Qu Wenruo4c664612021-09-15 15:17:16 +08002862bioc_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002863 btrfs_bio_counter_dec(fs_info);
Qu Wenruo4c664612021-09-15 15:17:16 +08002864 btrfs_put_bioc(bioc);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002865 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2866 sparity->nsectors);
2867 spin_lock(&sctx->stat_lock);
2868 sctx->stat.malloc_errors++;
2869 spin_unlock(&sctx->stat_lock);
2870out:
2871 scrub_free_parity(sparity);
2872}
2873
2874static inline int scrub_calc_parity_bitmap_len(int nsectors)
2875{
Zhao Leibfca9a62014-12-08 19:55:57 +08002876 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002877}
2878
2879static void scrub_parity_get(struct scrub_parity *sparity)
2880{
Elena Reshetova78a76452017-03-03 10:55:24 +02002881 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002882}
2883
2884static void scrub_parity_put(struct scrub_parity *sparity)
2885{
Elena Reshetova78a76452017-03-03 10:55:24 +02002886 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002887 return;
2888
2889 scrub_parity_check_and_repair(sparity);
2890}
2891
2892static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2893 struct map_lookup *map,
2894 struct btrfs_device *sdev,
2895 struct btrfs_path *path,
2896 u64 logic_start,
2897 u64 logic_end)
2898{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002899 struct btrfs_fs_info *fs_info = sctx->fs_info;
Josef Bacik29cbcf42021-11-05 16:45:45 -04002900 struct btrfs_root *root = btrfs_extent_root(fs_info, logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002901 struct btrfs_root *csum_root = fs_info->csum_root;
2902 struct btrfs_extent_item *extent;
Qu Wenruo4c664612021-09-15 15:17:16 +08002903 struct btrfs_io_context *bioc = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002904 u64 flags;
2905 int ret;
2906 int slot;
2907 struct extent_buffer *l;
2908 struct btrfs_key key;
2909 u64 generation;
2910 u64 extent_logical;
2911 u64 extent_physical;
Qu Wenruofa485d22020-12-02 14:48:07 +08002912 /* Check the comment in scrub_stripe() for why u32 is enough here */
2913 u32 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07002914 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002915 struct btrfs_device *extent_dev;
2916 struct scrub_parity *sparity;
2917 int nsectors;
2918 int bitmap_len;
2919 int extent_mirror_num;
2920 int stop_loop = 0;
2921
Qu Wenruofa485d22020-12-02 14:48:07 +08002922 ASSERT(map->stripe_len <= U32_MAX);
David Sterbaab108d92020-07-01 20:45:04 +02002923 nsectors = map->stripe_len >> fs_info->sectorsize_bits;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002924 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2925 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2926 GFP_NOFS);
2927 if (!sparity) {
2928 spin_lock(&sctx->stat_lock);
2929 sctx->stat.malloc_errors++;
2930 spin_unlock(&sctx->stat_lock);
2931 return -ENOMEM;
2932 }
2933
Qu Wenruofa485d22020-12-02 14:48:07 +08002934 ASSERT(map->stripe_len <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002935 sparity->stripe_len = map->stripe_len;
2936 sparity->nsectors = nsectors;
2937 sparity->sctx = sctx;
2938 sparity->scrub_dev = sdev;
2939 sparity->logic_start = logic_start;
2940 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02002941 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002942 INIT_LIST_HEAD(&sparity->spages);
2943 sparity->dbitmap = sparity->bitmap;
2944 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2945
2946 ret = 0;
2947 while (logic_start < logic_end) {
2948 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2949 key.type = BTRFS_METADATA_ITEM_KEY;
2950 else
2951 key.type = BTRFS_EXTENT_ITEM_KEY;
2952 key.objectid = logic_start;
2953 key.offset = (u64)-1;
2954
2955 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2956 if (ret < 0)
2957 goto out;
2958
2959 if (ret > 0) {
2960 ret = btrfs_previous_extent_item(root, path, 0);
2961 if (ret < 0)
2962 goto out;
2963 if (ret > 0) {
2964 btrfs_release_path(path);
2965 ret = btrfs_search_slot(NULL, root, &key,
2966 path, 0, 0);
2967 if (ret < 0)
2968 goto out;
2969 }
2970 }
2971
2972 stop_loop = 0;
2973 while (1) {
2974 u64 bytes;
2975
2976 l = path->nodes[0];
2977 slot = path->slots[0];
2978 if (slot >= btrfs_header_nritems(l)) {
2979 ret = btrfs_next_leaf(root, path);
2980 if (ret == 0)
2981 continue;
2982 if (ret < 0)
2983 goto out;
2984
2985 stop_loop = 1;
2986 break;
2987 }
2988 btrfs_item_key_to_cpu(l, &key, slot);
2989
Zhao Leid7cad232015-07-22 13:14:48 +08002990 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2991 key.type != BTRFS_METADATA_ITEM_KEY)
2992 goto next;
2993
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002994 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002995 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002996 else
2997 bytes = key.offset;
2998
2999 if (key.objectid + bytes <= logic_start)
3000 goto next;
3001
Zhao Leia0dd59d2015-07-21 15:42:26 +08003002 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003003 stop_loop = 1;
3004 break;
3005 }
3006
3007 while (key.objectid >= logic_start + map->stripe_len)
3008 logic_start += map->stripe_len;
3009
3010 extent = btrfs_item_ptr(l, slot,
3011 struct btrfs_extent_item);
3012 flags = btrfs_extent_flags(l, extent);
3013 generation = btrfs_extent_generation(l, extent);
3014
Zhao Leia323e812015-07-23 12:29:49 +08003015 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3016 (key.objectid < logic_start ||
3017 key.objectid + bytes >
3018 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003019 btrfs_err(fs_info,
3020 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08003021 key.objectid, logic_start);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003022 spin_lock(&sctx->stat_lock);
3023 sctx->stat.uncorrectable_errors++;
3024 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003025 goto next;
3026 }
3027again:
3028 extent_logical = key.objectid;
Qu Wenruofa485d22020-12-02 14:48:07 +08003029 ASSERT(bytes <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003030 extent_len = bytes;
3031
3032 if (extent_logical < logic_start) {
3033 extent_len -= logic_start - extent_logical;
3034 extent_logical = logic_start;
3035 }
3036
3037 if (extent_logical + extent_len >
3038 logic_start + map->stripe_len)
3039 extent_len = logic_start + map->stripe_len -
3040 extent_logical;
3041
3042 scrub_parity_mark_sectors_data(sparity, extent_logical,
3043 extent_len);
3044
Omar Sandoval4a770892015-06-19 11:52:52 -07003045 mapped_length = extent_len;
Qu Wenruo4c664612021-09-15 15:17:16 +08003046 bioc = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003047 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
Qu Wenruo4c664612021-09-15 15:17:16 +08003048 extent_logical, &mapped_length, &bioc,
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003049 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07003050 if (!ret) {
Qu Wenruo4c664612021-09-15 15:17:16 +08003051 if (!bioc || mapped_length < extent_len)
Omar Sandoval4a770892015-06-19 11:52:52 -07003052 ret = -EIO;
3053 }
3054 if (ret) {
Qu Wenruo4c664612021-09-15 15:17:16 +08003055 btrfs_put_bioc(bioc);
Omar Sandoval4a770892015-06-19 11:52:52 -07003056 goto out;
3057 }
Qu Wenruo4c664612021-09-15 15:17:16 +08003058 extent_physical = bioc->stripes[0].physical;
3059 extent_mirror_num = bioc->mirror_num;
3060 extent_dev = bioc->stripes[0].dev;
3061 btrfs_put_bioc(bioc);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003062
3063 ret = btrfs_lookup_csums_range(csum_root,
3064 extent_logical,
3065 extent_logical + extent_len - 1,
3066 &sctx->csum_list, 1);
3067 if (ret)
3068 goto out;
3069
3070 ret = scrub_extent_for_parity(sparity, extent_logical,
3071 extent_len,
3072 extent_physical,
3073 extent_dev, flags,
3074 generation,
3075 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003076
3077 scrub_free_csums(sctx);
3078
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003079 if (ret)
3080 goto out;
3081
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003082 if (extent_logical + extent_len <
3083 key.objectid + bytes) {
3084 logic_start += map->stripe_len;
3085
3086 if (logic_start >= logic_end) {
3087 stop_loop = 1;
3088 break;
3089 }
3090
3091 if (logic_start < key.objectid + bytes) {
3092 cond_resched();
3093 goto again;
3094 }
3095 }
3096next:
3097 path->slots[0]++;
3098 }
3099
3100 btrfs_release_path(path);
3101
3102 if (stop_loop)
3103 break;
3104
3105 logic_start += map->stripe_len;
3106 }
3107out:
Qu Wenruofa485d22020-12-02 14:48:07 +08003108 if (ret < 0) {
3109 ASSERT(logic_end - logic_start <= U32_MAX);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003110 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003111 logic_end - logic_start);
Qu Wenruofa485d22020-12-02 14:48:07 +08003112 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003113 scrub_parity_put(sparity);
3114 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003115 mutex_lock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003116 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003117 mutex_unlock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003118
3119 btrfs_release_path(path);
3120 return ret < 0 ? ret : 0;
3121}
3122
Naohiro Aotade17add2021-02-04 19:22:13 +09003123static void sync_replace_for_zoned(struct scrub_ctx *sctx)
3124{
3125 if (!btrfs_is_zoned(sctx->fs_info))
3126 return;
3127
3128 sctx->flush_all_writes = true;
3129 scrub_submit(sctx);
3130 mutex_lock(&sctx->wr_lock);
3131 scrub_wr_submit(sctx);
3132 mutex_unlock(&sctx->wr_lock);
3133
3134 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3135}
3136
Naohiro Aota7db1c5d2021-02-04 19:22:14 +09003137static int sync_write_pointer_for_zoned(struct scrub_ctx *sctx, u64 logical,
3138 u64 physical, u64 physical_end)
3139{
3140 struct btrfs_fs_info *fs_info = sctx->fs_info;
3141 int ret = 0;
3142
3143 if (!btrfs_is_zoned(fs_info))
3144 return 0;
3145
3146 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
3147
3148 mutex_lock(&sctx->wr_lock);
3149 if (sctx->write_pointer < physical_end) {
3150 ret = btrfs_sync_zone_write_pointer(sctx->wr_tgtdev, logical,
3151 physical,
3152 sctx->write_pointer);
3153 if (ret)
3154 btrfs_err(fs_info,
3155 "zoned: failed to recover write pointer");
3156 }
3157 mutex_unlock(&sctx->wr_lock);
3158 btrfs_dev_clear_zone_empty(sctx->wr_tgtdev, physical);
3159
3160 return ret;
3161}
3162
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003163static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003164 struct map_lookup *map,
3165 struct btrfs_device *scrub_dev,
Filipe Manana2473d242020-05-08 11:01:10 +01003166 int num, u64 base, u64 length,
3167 struct btrfs_block_group *cache)
Arne Jansena2de7332011-03-08 14:14:00 +01003168{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003169 struct btrfs_path *path, *ppath;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003170 struct btrfs_fs_info *fs_info = sctx->fs_info;
Josef Bacik29cbcf42021-11-05 16:45:45 -04003171 struct btrfs_root *root;
Arne Jansena2de7332011-03-08 14:14:00 +01003172 struct btrfs_root *csum_root = fs_info->csum_root;
3173 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003174 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003175 u64 flags;
3176 int ret;
3177 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003178 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003179 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003180 u64 physical;
3181 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003182 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003183 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003184 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003185 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003186 struct reada_control *reada1;
3187 struct reada_control *reada2;
David Sterbae6c11f92016-03-24 18:00:53 +01003188 struct btrfs_key key;
Arne Jansen7a262852011-06-10 12:39:23 +02003189 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003190 u64 increment = map->stripe_len;
3191 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003192 u64 extent_logical;
3193 u64 extent_physical;
Qu Wenruofa485d22020-12-02 14:48:07 +08003194 /*
3195 * Unlike chunk length, extent length should never go beyond
3196 * BTRFS_MAX_EXTENT_SIZE, thus u32 is enough here.
3197 */
3198 u32 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003199 u64 stripe_logical;
3200 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003201 struct btrfs_device *extent_dev;
3202 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003203 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003204
Wang Shilong3b080b22014-04-01 18:01:43 +08003205 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003206 offset = 0;
Liu Bo42c61ab2017-04-03 13:45:24 -07003207 nstripes = div64_u64(length, map->stripe_len);
David Sterba7735cd72019-11-28 15:37:46 +01003208 mirror_num = 1;
3209 increment = map->stripe_len;
Arne Jansena2de7332011-03-08 14:14:00 +01003210 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3211 offset = map->stripe_len * num;
3212 increment = map->stripe_len * map->num_stripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003213 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3214 int factor = map->num_stripes / map->sub_stripes;
3215 offset = map->stripe_len * (num / map->sub_stripes);
3216 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003217 mirror_num = num % map->sub_stripes + 1;
David Sterbac7369b32019-05-31 15:39:31 +02003218 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
Jan Schmidt193ea742011-06-13 19:56:54 +02003219 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003220 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidt193ea742011-06-13 19:56:54 +02003221 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003222 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003223 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003224 increment = map->stripe_len * nr_data_stripes(map);
Arne Jansena2de7332011-03-08 14:14:00 +01003225 }
3226
3227 path = btrfs_alloc_path();
3228 if (!path)
3229 return -ENOMEM;
3230
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003231 ppath = btrfs_alloc_path();
3232 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003233 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003234 return -ENOMEM;
3235 }
3236
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003237 /*
3238 * work on commit root. The related disk blocks are static as
3239 * long as COW is applied. This means, it is save to rewrite
3240 * them to repair disk errors without any race conditions
3241 */
Arne Jansena2de7332011-03-08 14:14:00 +01003242 path->search_commit_root = 1;
3243 path->skip_locking = 1;
3244
Gui Hecheng063c54d2015-01-09 09:39:40 +08003245 ppath->search_commit_root = 1;
3246 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003247 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003248 * trigger the readahead for extent tree csum tree and wait for
3249 * completion. During readahead, the scrub is officially paused
3250 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003251 */
3252 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003253 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003254 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003255 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003256 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003257 logic_end += base;
3258 } else {
3259 logic_end = logical + increment * nstripes;
3260 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003261 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003262 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003263 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003264
Josef Bacik29cbcf42021-11-05 16:45:45 -04003265 root = btrfs_extent_root(fs_info, logical);
3266
Arne Jansen7a262852011-06-10 12:39:23 +02003267 /* FIXME it might be better to start readahead at commit root */
David Sterbae6c11f92016-03-24 18:00:53 +01003268 key.objectid = logical;
3269 key.type = BTRFS_EXTENT_ITEM_KEY;
3270 key.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003271 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003272 key_end.type = BTRFS_METADATA_ITEM_KEY;
3273 key_end.offset = (u64)-1;
David Sterbae6c11f92016-03-24 18:00:53 +01003274 reada1 = btrfs_reada_add(root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003275
Filipe Mananaa6889ca2020-10-12 11:55:26 +01003276 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3277 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3278 key.type = BTRFS_EXTENT_CSUM_KEY;
3279 key.offset = logical;
3280 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3281 key_end.type = BTRFS_EXTENT_CSUM_KEY;
3282 key_end.offset = logic_end;
3283 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
3284 } else {
3285 reada2 = NULL;
3286 }
Arne Jansena2de7332011-03-08 14:14:00 +01003287
Arne Jansen7a262852011-06-10 12:39:23 +02003288 if (!IS_ERR(reada1))
3289 btrfs_reada_wait(reada1);
Filipe Mananaa6889ca2020-10-12 11:55:26 +01003290 if (!IS_ERR_OR_NULL(reada2))
Arne Jansen7a262852011-06-10 12:39:23 +02003291 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003292
Arne Jansena2de7332011-03-08 14:14:00 +01003293
3294 /*
3295 * collect all data csums for the stripe to avoid seeking during
3296 * the scrub. This might currently (crc32) end up to be about 1MB
3297 */
Arne Jansene7786c32011-05-28 20:58:38 +00003298 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003299
Naohiro Aotade17add2021-02-04 19:22:13 +09003300 if (sctx->is_dev_replace &&
3301 btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) {
3302 mutex_lock(&sctx->wr_lock);
3303 sctx->write_pointer = physical;
3304 mutex_unlock(&sctx->wr_lock);
3305 sctx->flush_all_writes = true;
3306 }
3307
Arne Jansena2de7332011-03-08 14:14:00 +01003308 /*
3309 * now find all extents for each stripe and scrub them
3310 */
Arne Jansena2de7332011-03-08 14:14:00 +01003311 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003312 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003313 /*
3314 * canceled?
3315 */
3316 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003317 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003318 ret = -ECANCELED;
3319 goto out;
3320 }
3321 /*
3322 * check to see if we have to pause
3323 */
3324 if (atomic_read(&fs_info->scrub_pause_req)) {
3325 /* push queued extents */
David Sterba2073c4c2017-03-31 17:12:51 +02003326 sctx->flush_all_writes = true;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003327 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003328 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003329 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003330 mutex_unlock(&sctx->wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003331 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003332 atomic_read(&sctx->bios_in_flight) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003333 sctx->flush_all_writes = false;
Wang Shilong3cb09292013-12-04 21:15:19 +08003334 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003335 }
3336
Zhao Leif2f66a22015-07-21 12:22:29 +08003337 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3338 ret = get_raid56_logic_offset(physical, num, map,
3339 &logical,
3340 &stripe_logical);
3341 logical += base;
3342 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003343 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003344 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003345 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003346 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3347 ppath, stripe_logical,
3348 stripe_end);
3349 if (ret)
3350 goto out;
3351 goto skip;
3352 }
3353 }
3354
Wang Shilong7c76edb2014-01-12 21:38:32 +08003355 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3356 key.type = BTRFS_METADATA_ITEM_KEY;
3357 else
3358 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003359 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003360 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003361
3362 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3363 if (ret < 0)
3364 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003365
Arne Jansen8c510322011-06-03 10:09:26 +02003366 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003367 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003368 if (ret < 0)
3369 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003370 if (ret > 0) {
3371 /* there's no smaller item, so stick with the
3372 * larger one */
3373 btrfs_release_path(path);
3374 ret = btrfs_search_slot(NULL, root, &key,
3375 path, 0, 0);
3376 if (ret < 0)
3377 goto out;
3378 }
Arne Jansena2de7332011-03-08 14:14:00 +01003379 }
3380
Liu Bo625f1c8d2013-04-27 02:56:57 +00003381 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003382 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003383 u64 bytes;
3384
Arne Jansena2de7332011-03-08 14:14:00 +01003385 l = path->nodes[0];
3386 slot = path->slots[0];
3387 if (slot >= btrfs_header_nritems(l)) {
3388 ret = btrfs_next_leaf(root, path);
3389 if (ret == 0)
3390 continue;
3391 if (ret < 0)
3392 goto out;
3393
Liu Bo625f1c8d2013-04-27 02:56:57 +00003394 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003395 break;
3396 }
3397 btrfs_item_key_to_cpu(l, &key, slot);
3398
Zhao Leid7cad232015-07-22 13:14:48 +08003399 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3400 key.type != BTRFS_METADATA_ITEM_KEY)
3401 goto next;
3402
Josef Bacik3173a182013-03-07 14:22:04 -05003403 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003404 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003405 else
3406 bytes = key.offset;
3407
3408 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003409 goto next;
3410
Liu Bo625f1c8d2013-04-27 02:56:57 +00003411 if (key.objectid >= logical + map->stripe_len) {
3412 /* out of this device extent */
3413 if (key.objectid >= logic_end)
3414 stop_loop = 1;
3415 break;
3416 }
Arne Jansena2de7332011-03-08 14:14:00 +01003417
Filipe Manana2473d242020-05-08 11:01:10 +01003418 /*
3419 * If our block group was removed in the meanwhile, just
3420 * stop scrubbing since there is no point in continuing.
3421 * Continuing would prevent reusing its device extents
3422 * for new block groups for a long time.
3423 */
3424 spin_lock(&cache->lock);
3425 if (cache->removed) {
3426 spin_unlock(&cache->lock);
3427 ret = 0;
3428 goto out;
3429 }
3430 spin_unlock(&cache->lock);
3431
Arne Jansena2de7332011-03-08 14:14:00 +01003432 extent = btrfs_item_ptr(l, slot,
3433 struct btrfs_extent_item);
3434 flags = btrfs_extent_flags(l, extent);
3435 generation = btrfs_extent_generation(l, extent);
3436
Zhao Leia323e812015-07-23 12:29:49 +08003437 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3438 (key.objectid < logical ||
3439 key.objectid + bytes >
3440 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003441 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003442 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003443 key.objectid, logical);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003444 spin_lock(&sctx->stat_lock);
3445 sctx->stat.uncorrectable_errors++;
3446 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003447 goto next;
3448 }
3449
Liu Bo625f1c8d2013-04-27 02:56:57 +00003450again:
3451 extent_logical = key.objectid;
Qu Wenruofa485d22020-12-02 14:48:07 +08003452 ASSERT(bytes <= U32_MAX);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003453 extent_len = bytes;
3454
Arne Jansena2de7332011-03-08 14:14:00 +01003455 /*
3456 * trim extent to this stripe
3457 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003458 if (extent_logical < logical) {
3459 extent_len -= logical - extent_logical;
3460 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003461 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003462 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003463 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003464 extent_len = logical + map->stripe_len -
3465 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003466 }
3467
Liu Bo625f1c8d2013-04-27 02:56:57 +00003468 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003469 extent_dev = scrub_dev;
3470 extent_mirror_num = mirror_num;
Omar Sandoval32934282018-08-14 11:09:52 -07003471 if (sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003472 scrub_remap_extent(fs_info, extent_logical,
3473 extent_len, &extent_physical,
3474 &extent_dev,
3475 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003476
Filipe Manana89490302020-05-08 11:02:07 +01003477 if (flags & BTRFS_EXTENT_FLAG_DATA) {
3478 ret = btrfs_lookup_csums_range(csum_root,
3479 extent_logical,
3480 extent_logical + extent_len - 1,
3481 &sctx->csum_list, 1);
3482 if (ret)
3483 goto out;
3484 }
Arne Jansena2de7332011-03-08 14:14:00 +01003485
Liu Bo6ca17652018-03-07 12:08:09 -07003486 ret = scrub_extent(sctx, map, extent_logical, extent_len,
Liu Bo625f1c8d2013-04-27 02:56:57 +00003487 extent_physical, extent_dev, flags,
3488 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003489 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003490
3491 scrub_free_csums(sctx);
3492
Liu Bo625f1c8d2013-04-27 02:56:57 +00003493 if (ret)
3494 goto out;
3495
Naohiro Aotade17add2021-02-04 19:22:13 +09003496 if (sctx->is_dev_replace)
3497 sync_replace_for_zoned(sctx);
3498
Liu Bo625f1c8d2013-04-27 02:56:57 +00003499 if (extent_logical + extent_len <
3500 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003501 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003502 /*
3503 * loop until we find next data stripe
3504 * or we have finished all stripes.
3505 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003506loop:
3507 physical += map->stripe_len;
3508 ret = get_raid56_logic_offset(physical,
3509 num, map, &logical,
3510 &stripe_logical);
3511 logical += base;
3512
3513 if (ret && physical < physical_end) {
3514 stripe_logical += base;
3515 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003516 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003517 ret = scrub_raid56_parity(sctx,
3518 map, scrub_dev, ppath,
3519 stripe_logical,
3520 stripe_end);
3521 if (ret)
3522 goto out;
3523 goto loop;
3524 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003525 } else {
3526 physical += map->stripe_len;
3527 logical += increment;
3528 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003529 if (logical < key.objectid + bytes) {
3530 cond_resched();
3531 goto again;
3532 }
3533
Wang Shilong3b080b22014-04-01 18:01:43 +08003534 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003535 stop_loop = 1;
3536 break;
3537 }
3538 }
Arne Jansena2de7332011-03-08 14:14:00 +01003539next:
3540 path->slots[0]++;
3541 }
Chris Mason71267332011-05-23 06:30:52 -04003542 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003543skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003544 logical += increment;
3545 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003546 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003547 if (stop_loop)
3548 sctx->stat.last_physical = map->stripes[num].physical +
3549 length;
3550 else
3551 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003552 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003553 if (stop_loop)
3554 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003555 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003556out:
Arne Jansena2de7332011-03-08 14:14:00 +01003557 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003558 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003559 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003560 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003561 mutex_unlock(&sctx->wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003562
Arne Jansene7786c32011-05-28 20:58:38 +00003563 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003564 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003565 btrfs_free_path(ppath);
Naohiro Aota7db1c5d2021-02-04 19:22:14 +09003566
3567 if (sctx->is_dev_replace && ret >= 0) {
3568 int ret2;
3569
3570 ret2 = sync_write_pointer_for_zoned(sctx, base + offset,
3571 map->stripes[num].physical,
3572 physical_end);
3573 if (ret2)
3574 ret = ret2;
3575 }
3576
Arne Jansena2de7332011-03-08 14:14:00 +01003577 return ret < 0 ? ret : 0;
3578}
3579
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003580static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003581 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003582 u64 chunk_offset, u64 length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003583 u64 dev_offset,
David Sterba32da53862019-10-29 19:20:18 +01003584 struct btrfs_block_group *cache)
Arne Jansena2de7332011-03-08 14:14:00 +01003585{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003586 struct btrfs_fs_info *fs_info = sctx->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02003587 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003588 struct map_lookup *map;
3589 struct extent_map *em;
3590 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003591 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003592
David Sterbac8bf1b62019-05-17 11:43:17 +02003593 read_lock(&map_tree->lock);
3594 em = lookup_extent_mapping(map_tree, chunk_offset, 1);
3595 read_unlock(&map_tree->lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003596
Filipe Manana020d5b72015-11-19 10:57:20 +00003597 if (!em) {
3598 /*
3599 * Might have been an unused block group deleted by the cleaner
3600 * kthread or relocation.
3601 */
3602 spin_lock(&cache->lock);
3603 if (!cache->removed)
3604 ret = -EINVAL;
3605 spin_unlock(&cache->lock);
3606
3607 return ret;
3608 }
Arne Jansena2de7332011-03-08 14:14:00 +01003609
Jeff Mahoney95617d62015-06-03 10:55:48 -04003610 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003611 if (em->start != chunk_offset)
3612 goto out;
3613
3614 if (em->len < length)
3615 goto out;
3616
3617 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003618 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003619 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003620 ret = scrub_stripe(sctx, map, scrub_dev, i,
Filipe Manana2473d242020-05-08 11:01:10 +01003621 chunk_offset, length, cache);
Arne Jansena2de7332011-03-08 14:14:00 +01003622 if (ret)
3623 goto out;
3624 }
3625 }
3626out:
3627 free_extent_map(em);
3628
3629 return ret;
3630}
3631
Naohiro Aotade17add2021-02-04 19:22:13 +09003632static int finish_extent_writes_for_zoned(struct btrfs_root *root,
3633 struct btrfs_block_group *cache)
3634{
3635 struct btrfs_fs_info *fs_info = cache->fs_info;
3636 struct btrfs_trans_handle *trans;
3637
3638 if (!btrfs_is_zoned(fs_info))
3639 return 0;
3640
3641 btrfs_wait_block_group_reservations(cache);
3642 btrfs_wait_nocow_writers(cache);
3643 btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start, cache->length);
3644
3645 trans = btrfs_join_transaction(root);
3646 if (IS_ERR(trans))
3647 return PTR_ERR(trans);
3648 return btrfs_commit_transaction(trans);
3649}
3650
Arne Jansena2de7332011-03-08 14:14:00 +01003651static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003652int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Omar Sandoval32934282018-08-14 11:09:52 -07003653 struct btrfs_device *scrub_dev, u64 start, u64 end)
Arne Jansena2de7332011-03-08 14:14:00 +01003654{
3655 struct btrfs_dev_extent *dev_extent = NULL;
3656 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003657 struct btrfs_fs_info *fs_info = sctx->fs_info;
3658 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003659 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003660 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003661 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003662 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003663 int slot;
3664 struct extent_buffer *l;
3665 struct btrfs_key key;
3666 struct btrfs_key found_key;
David Sterba32da53862019-10-29 19:20:18 +01003667 struct btrfs_block_group *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003668 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003669
3670 path = btrfs_alloc_path();
3671 if (!path)
3672 return -ENOMEM;
3673
David Sterbae4058b52015-11-27 16:31:35 +01003674 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003675 path->search_commit_root = 1;
3676 path->skip_locking = 1;
3677
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003678 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003679 key.offset = 0ull;
3680 key.type = BTRFS_DEV_EXTENT_KEY;
3681
Arne Jansena2de7332011-03-08 14:14:00 +01003682 while (1) {
3683 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3684 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003685 break;
3686 if (ret > 0) {
3687 if (path->slots[0] >=
3688 btrfs_header_nritems(path->nodes[0])) {
3689 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003690 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003691 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003692 if (ret > 0) {
3693 ret = 0;
3694 break;
3695 }
3696 } else {
3697 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003698 }
3699 }
Arne Jansena2de7332011-03-08 14:14:00 +01003700
3701 l = path->nodes[0];
3702 slot = path->slots[0];
3703
3704 btrfs_item_key_to_cpu(l, &found_key, slot);
3705
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003706 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003707 break;
3708
David Sterba962a2982014-06-04 18:41:45 +02003709 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003710 break;
3711
3712 if (found_key.offset >= end)
3713 break;
3714
3715 if (found_key.offset < key.offset)
3716 break;
3717
3718 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3719 length = btrfs_dev_extent_length(l, dev_extent);
3720
Qu Wenruoced96ed2014-06-19 10:42:51 +08003721 if (found_key.offset + length <= start)
3722 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003723
Arne Jansena2de7332011-03-08 14:14:00 +01003724 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3725
3726 /*
3727 * get a reference on the corresponding block group to prevent
3728 * the chunk from going away while we scrub it
3729 */
3730 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003731
3732 /* some chunks are removed but not committed to disk yet,
3733 * continue scrubbing */
3734 if (!cache)
3735 goto skip;
3736
Naohiro Aota78ce9fc2021-02-04 19:22:11 +09003737 if (sctx->is_dev_replace && btrfs_is_zoned(fs_info)) {
3738 spin_lock(&cache->lock);
3739 if (!cache->to_copy) {
3740 spin_unlock(&cache->lock);
Filipe Manana0dc16ef2021-04-14 14:05:26 +01003741 btrfs_put_block_group(cache);
3742 goto skip;
Naohiro Aota78ce9fc2021-02-04 19:22:11 +09003743 }
3744 spin_unlock(&cache->lock);
3745 }
3746
Zhaolei55e3a602015-08-05 16:43:30 +08003747 /*
Filipe Manana2473d242020-05-08 11:01:10 +01003748 * Make sure that while we are scrubbing the corresponding block
3749 * group doesn't get its logical address and its device extents
3750 * reused for another block group, which can possibly be of a
3751 * different type and different profile. We do this to prevent
3752 * false error detections and crashes due to bogus attempts to
3753 * repair extents.
3754 */
3755 spin_lock(&cache->lock);
3756 if (cache->removed) {
3757 spin_unlock(&cache->lock);
3758 btrfs_put_block_group(cache);
3759 goto skip;
3760 }
Filipe Manana6b7304a2020-05-08 11:01:47 +01003761 btrfs_freeze_block_group(cache);
Filipe Manana2473d242020-05-08 11:01:10 +01003762 spin_unlock(&cache->lock);
3763
3764 /*
Zhaolei55e3a602015-08-05 16:43:30 +08003765 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3766 * to avoid deadlock caused by:
3767 * btrfs_inc_block_group_ro()
3768 * -> btrfs_wait_for_commit()
3769 * -> btrfs_commit_transaction()
3770 * -> btrfs_scrub_pause()
3771 */
3772 scrub_pause_on(fs_info);
Qu Wenruob12de522019-11-15 10:09:00 +08003773
3774 /*
3775 * Don't do chunk preallocation for scrub.
3776 *
3777 * This is especially important for SYSTEM bgs, or we can hit
3778 * -EFBIG from btrfs_finish_chunk_alloc() like:
3779 * 1. The only SYSTEM bg is marked RO.
3780 * Since SYSTEM bg is small, that's pretty common.
3781 * 2. New SYSTEM bg will be allocated
3782 * Due to regular version will allocate new chunk.
3783 * 3. New SYSTEM bg is empty and will get cleaned up
3784 * Before cleanup really happens, it's marked RO again.
3785 * 4. Empty SYSTEM bg get scrubbed
3786 * We go back to 2.
3787 *
3788 * This can easily boost the amount of SYSTEM chunks if cleaner
3789 * thread can't be triggered fast enough, and use up all space
3790 * of btrfs_super_block::sys_chunk_array
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003791 *
3792 * While for dev replace, we need to try our best to mark block
3793 * group RO, to prevent race between:
3794 * - Write duplication
3795 * Contains latest data
3796 * - Scrub copy
3797 * Contains data from commit tree
3798 *
3799 * If target block group is not marked RO, nocow writes can
3800 * be overwritten by scrub copy, causing data corruption.
3801 * So for dev-replace, it's not allowed to continue if a block
3802 * group is not RO.
Qu Wenruob12de522019-11-15 10:09:00 +08003803 */
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003804 ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace);
Naohiro Aotade17add2021-02-04 19:22:13 +09003805 if (!ret && sctx->is_dev_replace) {
3806 ret = finish_extent_writes_for_zoned(root, cache);
3807 if (ret) {
3808 btrfs_dec_block_group_ro(cache);
3809 scrub_pause_off(fs_info);
3810 btrfs_put_block_group(cache);
3811 break;
3812 }
3813 }
3814
Zhaolei76a8efa2015-11-17 18:46:17 +08003815 if (ret == 0) {
3816 ro_set = 1;
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003817 } else if (ret == -ENOSPC && !sctx->is_dev_replace) {
Zhaolei76a8efa2015-11-17 18:46:17 +08003818 /*
3819 * btrfs_inc_block_group_ro return -ENOSPC when it
3820 * failed in creating new chunk for metadata.
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003821 * It is not a problem for scrub, because
Zhaolei76a8efa2015-11-17 18:46:17 +08003822 * metadata are always cowed, and our scrub paused
3823 * commit_transactions.
3824 */
3825 ro_set = 0;
Filipe Manana195a49e2021-02-05 12:55:37 +00003826 } else if (ret == -ETXTBSY) {
3827 btrfs_warn(fs_info,
3828 "skipping scrub of block group %llu due to active swapfile",
3829 cache->start);
3830 scrub_pause_off(fs_info);
3831 ret = 0;
3832 goto skip_unfreeze;
Zhaolei76a8efa2015-11-17 18:46:17 +08003833 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003834 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02003835 "failed setting block group ro: %d", ret);
Filipe Manana6b7304a2020-05-08 11:01:47 +01003836 btrfs_unfreeze_block_group(cache);
Zhaolei55e3a602015-08-05 16:43:30 +08003837 btrfs_put_block_group(cache);
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003838 scrub_pause_off(fs_info);
Zhaolei55e3a602015-08-05 16:43:30 +08003839 break;
3840 }
3841
Qu Wenruo1bbb97b2020-01-24 07:58:20 +08003842 /*
3843 * Now the target block is marked RO, wait for nocow writes to
3844 * finish before dev-replace.
3845 * COW is fine, as COW never overwrites extents in commit tree.
3846 */
3847 if (sctx->is_dev_replace) {
3848 btrfs_wait_nocow_writers(cache);
3849 btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start,
3850 cache->length);
3851 }
3852
3853 scrub_pause_off(fs_info);
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003854 down_write(&dev_replace->rwsem);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003855 dev_replace->cursor_right = found_key.offset + length;
3856 dev_replace->cursor_left = found_key.offset;
3857 dev_replace->item_needs_writeback = 1;
David Sterbacb5583d2018-09-07 16:11:23 +02003858 up_write(&dev_replace->rwsem);
3859
Zhao Lei8c204c92015-08-19 15:02:40 +08003860 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
Omar Sandoval32934282018-08-14 11:09:52 -07003861 found_key.offset, cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003862
3863 /*
3864 * flush, submit all pending read and write bios, afterwards
3865 * wait for them.
3866 * Note that in the dev replace case, a read request causes
3867 * write requests that are submitted in the read completion
3868 * worker. Therefore in the current situation, it is required
3869 * that all write requests are flushed, so that all read and
3870 * write requests are really completed when bios_in_flight
3871 * changes to 0.
3872 */
David Sterba2073c4c2017-03-31 17:12:51 +02003873 sctx->flush_all_writes = true;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003874 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003875 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003876 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003877 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003878
3879 wait_event(sctx->list_wait,
3880 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003881
3882 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003883
3884 /*
3885 * must be called before we decrease @scrub_paused.
3886 * make sure we don't block transaction commit while
3887 * we are waiting pending workers finished.
3888 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003889 wait_event(sctx->list_wait,
3890 atomic_read(&sctx->workers_pending) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003891 sctx->flush_all_writes = false;
Wang Shilong12cf9372014-02-19 19:24:17 +08003892
Zhaoleib708ce92015-08-05 16:43:29 +08003893 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003894
Naohiro Aota78ce9fc2021-02-04 19:22:11 +09003895 if (sctx->is_dev_replace &&
3896 !btrfs_finish_block_group_to_copy(dev_replace->srcdev,
3897 cache, found_key.offset))
3898 ro_set = 0;
3899
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003900 down_write(&dev_replace->rwsem);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003901 dev_replace->cursor_left = dev_replace->cursor_right;
3902 dev_replace->item_needs_writeback = 1;
Dan Carpenter3ec17a62019-10-31 13:55:01 +03003903 up_write(&dev_replace->rwsem);
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003904
Zhaolei76a8efa2015-11-17 18:46:17 +08003905 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003906 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003907
Filipe Manana758f2df2015-11-19 11:45:48 +00003908 /*
3909 * We might have prevented the cleaner kthread from deleting
3910 * this block group if it was already unused because we raced
3911 * and set it to RO mode first. So add it back to the unused
3912 * list, otherwise it might not ever be deleted unless a manual
3913 * balance is triggered or it becomes used and unused again.
3914 */
3915 spin_lock(&cache->lock);
3916 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
David Sterbabf38be62019-10-23 18:48:11 +02003917 cache->used == 0) {
Filipe Manana758f2df2015-11-19 11:45:48 +00003918 spin_unlock(&cache->lock);
Dennis Zhou6e80d4f2019-12-13 16:22:15 -08003919 if (btrfs_test_opt(fs_info, DISCARD_ASYNC))
3920 btrfs_discard_queue_work(&fs_info->discard_ctl,
3921 cache);
3922 else
3923 btrfs_mark_bg_unused(cache);
Filipe Manana758f2df2015-11-19 11:45:48 +00003924 } else {
3925 spin_unlock(&cache->lock);
3926 }
Filipe Manana195a49e2021-02-05 12:55:37 +00003927skip_unfreeze:
Filipe Manana6b7304a2020-05-08 11:01:47 +01003928 btrfs_unfreeze_block_group(cache);
Arne Jansena2de7332011-03-08 14:14:00 +01003929 btrfs_put_block_group(cache);
3930 if (ret)
3931 break;
Omar Sandoval32934282018-08-14 11:09:52 -07003932 if (sctx->is_dev_replace &&
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003933 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003934 ret = -EIO;
3935 break;
3936 }
3937 if (sctx->stat.malloc_errors > 0) {
3938 ret = -ENOMEM;
3939 break;
3940 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003941skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003942 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003943 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003944 }
3945
Arne Jansena2de7332011-03-08 14:14:00 +01003946 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003947
Zhaolei55e3a602015-08-05 16:43:30 +08003948 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003949}
3950
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003951static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3952 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003953{
3954 int i;
3955 u64 bytenr;
3956 u64 gen;
3957 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003958 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003959
Josef Bacik84961532021-10-05 16:35:25 -04003960 if (BTRFS_FS_ERROR(fs_info))
Josef Bacikfbabd4a2020-07-21 10:38:37 -04003961 return -EROFS;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003962
Miao Xie5f546062014-07-24 11:37:09 +08003963 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003964 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08003965 gen = scrub_dev->generation;
3966 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003967 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003968
3969 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3970 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003971 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3972 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003973 break;
Naohiro Aota12659252020-11-10 20:26:14 +09003974 if (!btrfs_check_super_location(scrub_dev, bytenr))
3975 continue;
Arne Jansena2de7332011-03-08 14:14:00 +01003976
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003977 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003978 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Qu Wenruo96e63a42020-11-03 21:31:02 +08003979 NULL, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003980 if (ret)
3981 return ret;
3982 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003983 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003984
3985 return 0;
3986}
3987
Josef Bacike89c4a92020-08-10 11:42:29 -04003988static void scrub_workers_put(struct btrfs_fs_info *fs_info)
3989{
3990 if (refcount_dec_and_mutex_lock(&fs_info->scrub_workers_refcnt,
3991 &fs_info->scrub_lock)) {
3992 struct btrfs_workqueue *scrub_workers = NULL;
3993 struct btrfs_workqueue *scrub_wr_comp = NULL;
3994 struct btrfs_workqueue *scrub_parity = NULL;
3995
3996 scrub_workers = fs_info->scrub_workers;
3997 scrub_wr_comp = fs_info->scrub_wr_completion_workers;
3998 scrub_parity = fs_info->scrub_parity_workers;
3999
4000 fs_info->scrub_workers = NULL;
4001 fs_info->scrub_wr_completion_workers = NULL;
4002 fs_info->scrub_parity_workers = NULL;
4003 mutex_unlock(&fs_info->scrub_lock);
4004
4005 btrfs_destroy_workqueue(scrub_workers);
4006 btrfs_destroy_workqueue(scrub_wr_comp);
4007 btrfs_destroy_workqueue(scrub_parity);
4008 }
4009}
4010
Arne Jansena2de7332011-03-08 14:14:00 +01004011/*
4012 * get a reference count on fs_info->scrub_workers. start worker if necessary
4013 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01004014static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
4015 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004016{
Josef Bacike89c4a92020-08-10 11:42:29 -04004017 struct btrfs_workqueue *scrub_workers = NULL;
4018 struct btrfs_workqueue *scrub_wr_comp = NULL;
4019 struct btrfs_workqueue *scrub_parity = NULL;
David Sterba6f011052015-02-16 18:34:01 +01004020 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08004021 int max_active = fs_info->thread_pool_size;
Josef Bacike89c4a92020-08-10 11:42:29 -04004022 int ret = -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01004023
Josef Bacike89c4a92020-08-10 11:42:29 -04004024 if (refcount_inc_not_zero(&fs_info->scrub_workers_refcnt))
4025 return 0;
Anand Jaineb4318e2019-01-30 14:45:01 +08004026
Josef Bacike89c4a92020-08-10 11:42:29 -04004027 scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub", flags,
4028 is_dev_replace ? 1 : max_active, 4);
4029 if (!scrub_workers)
4030 goto fail_scrub_workers;
4031
4032 scrub_wr_comp = btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
4033 max_active, 2);
4034 if (!scrub_wr_comp)
4035 goto fail_scrub_wr_completion_workers;
4036
4037 scrub_parity = btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
4038 max_active, 2);
4039 if (!scrub_parity)
4040 goto fail_scrub_parity_workers;
4041
4042 mutex_lock(&fs_info->scrub_lock);
Anand Jainff09c4c2019-01-30 14:45:02 +08004043 if (refcount_read(&fs_info->scrub_workers_refcnt) == 0) {
Josef Bacike89c4a92020-08-10 11:42:29 -04004044 ASSERT(fs_info->scrub_workers == NULL &&
4045 fs_info->scrub_wr_completion_workers == NULL &&
4046 fs_info->scrub_parity_workers == NULL);
4047 fs_info->scrub_workers = scrub_workers;
4048 fs_info->scrub_wr_completion_workers = scrub_wr_comp;
4049 fs_info->scrub_parity_workers = scrub_parity;
Anand Jainff09c4c2019-01-30 14:45:02 +08004050 refcount_set(&fs_info->scrub_workers_refcnt, 1);
Josef Bacike89c4a92020-08-10 11:42:29 -04004051 mutex_unlock(&fs_info->scrub_lock);
4052 return 0;
Arne Jansen632dd772011-06-10 12:07:07 +02004053 }
Josef Bacike89c4a92020-08-10 11:42:29 -04004054 /* Other thread raced in and created the workers for us */
4055 refcount_inc(&fs_info->scrub_workers_refcnt);
4056 mutex_unlock(&fs_info->scrub_lock);
Zhao Leie82afc52015-06-12 20:36:58 +08004057
Josef Bacike89c4a92020-08-10 11:42:29 -04004058 ret = 0;
4059 btrfs_destroy_workqueue(scrub_parity);
Zhao Leie82afc52015-06-12 20:36:58 +08004060fail_scrub_parity_workers:
Josef Bacike89c4a92020-08-10 11:42:29 -04004061 btrfs_destroy_workqueue(scrub_wr_comp);
Zhao Leie82afc52015-06-12 20:36:58 +08004062fail_scrub_wr_completion_workers:
Josef Bacike89c4a92020-08-10 11:42:29 -04004063 btrfs_destroy_workqueue(scrub_workers);
Zhao Leie82afc52015-06-12 20:36:58 +08004064fail_scrub_workers:
Josef Bacike89c4a92020-08-10 11:42:29 -04004065 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01004066}
4067
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004068int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
4069 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004070 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004071{
Josef Bacik562d7b12021-10-05 16:12:42 -04004072 struct btrfs_dev_lookup_args args = { .devid = devid };
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004073 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004074 int ret;
4075 struct btrfs_device *dev;
Filipe Mananaa5fb1142018-11-26 20:07:17 +00004076 unsigned int nofs_flag;
Arne Jansena2de7332011-03-08 14:14:00 +01004077
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004078 if (btrfs_fs_closing(fs_info))
David Sterba6c3abed2019-02-25 19:57:41 +01004079 return -EAGAIN;
Arne Jansena2de7332011-03-08 14:14:00 +01004080
Jeff Mahoneyda170662016-06-15 09:22:56 -04004081 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004082 /*
4083 * in this case scrub is unable to calculate the checksum
4084 * the way scrub is implemented. Do not handle this
4085 * situation at all because it won't ever happen.
4086 */
Frank Holtonefe120a2013-12-20 11:37:06 -05004087 btrfs_err(fs_info,
4088 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004089 fs_info->nodesize,
4090 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004091 return -EINVAL;
4092 }
4093
Jeff Mahoneyda170662016-06-15 09:22:56 -04004094 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004095 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04004096 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004097 /*
4098 * would exhaust the array bounds of pagev member in
4099 * struct scrub_block
4100 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004101 btrfs_err(fs_info,
4102 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004103 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004104 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004105 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004106 SCRUB_MAX_PAGES_PER_BLOCK);
4107 return -EINVAL;
4108 }
4109
David Sterba0e94c4f42018-12-04 16:11:56 +01004110 /* Allocate outside of device_list_mutex */
4111 sctx = scrub_setup_ctx(fs_info, is_dev_replace);
4112 if (IS_ERR(sctx))
4113 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004114
Josef Bacike89c4a92020-08-10 11:42:29 -04004115 ret = scrub_workers_get(fs_info, is_dev_replace);
4116 if (ret)
4117 goto out_free_ctx;
4118
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004119 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik562d7b12021-10-05 16:12:42 -04004120 dev = btrfs_find_device(fs_info->fs_devices, &args);
Anand Jaine6e674b2017-12-04 12:54:54 +08004121 if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
4122 !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004123 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01004124 ret = -ENODEV;
Josef Bacike89c4a92020-08-10 11:42:29 -04004125 goto out;
Arne Jansena2de7332011-03-08 14:14:00 +01004126 }
Arne Jansena2de7332011-03-08 14:14:00 +01004127
Anand Jainebbede42017-12-04 12:54:52 +08004128 if (!is_dev_replace && !readonly &&
4129 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Miao Xie5d68da32014-07-24 11:37:07 +08004130 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterbaa4852cf2020-07-09 11:25:40 +02004131 btrfs_err_in_rcu(fs_info,
4132 "scrub on devid %llu: filesystem on %s is not writable",
4133 devid, rcu_str_deref(dev->name));
David Sterba0e94c4f42018-12-04 16:11:56 +01004134 ret = -EROFS;
Josef Bacike89c4a92020-08-10 11:42:29 -04004135 goto out;
Miao Xie5d68da32014-07-24 11:37:07 +08004136 }
4137
Wang Shilong3b7a0162013-10-12 02:11:12 +08004138 mutex_lock(&fs_info->scrub_lock);
Anand Jaine12c9622017-12-04 12:54:53 +08004139 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004140 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004141 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004142 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01004143 ret = -EIO;
Josef Bacike89c4a92020-08-10 11:42:29 -04004144 goto out;
Arne Jansena2de7332011-03-08 14:14:00 +01004145 }
4146
David Sterbacb5583d2018-09-07 16:11:23 +02004147 down_read(&fs_info->dev_replace.rwsem);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004148 if (dev->scrub_ctx ||
Stefan Behrens8dabb742012-11-06 13:15:27 +01004149 (!is_dev_replace &&
4150 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
David Sterbacb5583d2018-09-07 16:11:23 +02004151 up_read(&fs_info->dev_replace.rwsem);
Arne Jansena2de7332011-03-08 14:14:00 +01004152 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004153 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
David Sterba0e94c4f42018-12-04 16:11:56 +01004154 ret = -EINPROGRESS;
Josef Bacike89c4a92020-08-10 11:42:29 -04004155 goto out;
Arne Jansena2de7332011-03-08 14:14:00 +01004156 }
David Sterbacb5583d2018-09-07 16:11:23 +02004157 up_read(&fs_info->dev_replace.rwsem);
Wang Shilong3b7a0162013-10-12 02:11:12 +08004158
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004159 sctx->readonly = readonly;
Anand Jaincadbc0a2018-01-03 16:08:30 +08004160 dev->scrub_ctx = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08004161 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004162
Wang Shilong3cb09292013-12-04 21:15:19 +08004163 /*
4164 * checking @scrub_pause_req here, we can avoid
4165 * race between committing transaction and scrubbing.
4166 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08004167 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004168 atomic_inc(&fs_info->scrubs_running);
4169 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004170
Filipe Mananaa5fb1142018-11-26 20:07:17 +00004171 /*
4172 * In order to avoid deadlock with reclaim when there is a transaction
4173 * trying to pause scrub, make sure we use GFP_NOFS for all the
4174 * allocations done at btrfs_scrub_pages() and scrub_pages_for_parity()
4175 * invoked by our callees. The pausing request is done when the
4176 * transaction commit starts, and it blocks the transaction until scrub
4177 * is paused (done at specific points at scrub_stripe() or right above
4178 * before incrementing fs_info->scrubs_running).
4179 */
4180 nofs_flag = memalloc_nofs_save();
Stefan Behrensff023aa2012-11-06 11:43:11 +01004181 if (!is_dev_replace) {
Anand Jaind1e14422019-01-03 16:17:40 +08004182 btrfs_info(fs_info, "scrub: started on devid %llu", devid);
Wang Shilong9b011ad2013-10-25 19:12:02 +08004183 /*
4184 * by holding device list mutex, we can
4185 * kick off writing super in log tree sync.
4186 */
Wang Shilong3cb09292013-12-04 21:15:19 +08004187 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004188 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08004189 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004190 }
Arne Jansena2de7332011-03-08 14:14:00 +01004191
4192 if (!ret)
Omar Sandoval32934282018-08-14 11:09:52 -07004193 ret = scrub_enumerate_chunks(sctx, dev, start, end);
Filipe Mananaa5fb1142018-11-26 20:07:17 +00004194 memalloc_nofs_restore(nofs_flag);
Arne Jansena2de7332011-03-08 14:14:00 +01004195
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004196 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004197 atomic_dec(&fs_info->scrubs_running);
4198 wake_up(&fs_info->scrub_pause_wait);
4199
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004200 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02004201
Arne Jansena2de7332011-03-08 14:14:00 +01004202 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004203 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01004204
Anand Jaind1e14422019-01-03 16:17:40 +08004205 if (!is_dev_replace)
4206 btrfs_info(fs_info, "scrub: %s on devid %llu with status: %d",
4207 ret ? "not finished" : "finished", devid, ret);
4208
Arne Jansena2de7332011-03-08 14:14:00 +01004209 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004210 dev->scrub_ctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004211 mutex_unlock(&fs_info->scrub_lock);
4212
Josef Bacike89c4a92020-08-10 11:42:29 -04004213 scrub_workers_put(fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +00004214 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004215
4216 return ret;
Josef Bacike89c4a92020-08-10 11:42:29 -04004217out:
4218 scrub_workers_put(fs_info);
David Sterba0e94c4f42018-12-04 16:11:56 +01004219out_free_ctx:
4220 scrub_free_ctx(sctx);
4221
4222 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01004223}
4224
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004225void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004226{
Arne Jansena2de7332011-03-08 14:14:00 +01004227 mutex_lock(&fs_info->scrub_lock);
4228 atomic_inc(&fs_info->scrub_pause_req);
4229 while (atomic_read(&fs_info->scrubs_paused) !=
4230 atomic_read(&fs_info->scrubs_running)) {
4231 mutex_unlock(&fs_info->scrub_lock);
4232 wait_event(fs_info->scrub_pause_wait,
4233 atomic_read(&fs_info->scrubs_paused) ==
4234 atomic_read(&fs_info->scrubs_running));
4235 mutex_lock(&fs_info->scrub_lock);
4236 }
4237 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004238}
4239
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004240void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004241{
Arne Jansena2de7332011-03-08 14:14:00 +01004242 atomic_dec(&fs_info->scrub_pause_req);
4243 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01004244}
4245
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004246int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004247{
Arne Jansena2de7332011-03-08 14:14:00 +01004248 mutex_lock(&fs_info->scrub_lock);
4249 if (!atomic_read(&fs_info->scrubs_running)) {
4250 mutex_unlock(&fs_info->scrub_lock);
4251 return -ENOTCONN;
4252 }
4253
4254 atomic_inc(&fs_info->scrub_cancel_req);
4255 while (atomic_read(&fs_info->scrubs_running)) {
4256 mutex_unlock(&fs_info->scrub_lock);
4257 wait_event(fs_info->scrub_pause_wait,
4258 atomic_read(&fs_info->scrubs_running) == 0);
4259 mutex_lock(&fs_info->scrub_lock);
4260 }
4261 atomic_dec(&fs_info->scrub_cancel_req);
4262 mutex_unlock(&fs_info->scrub_lock);
4263
4264 return 0;
4265}
4266
David Sterba163e97e2019-03-20 16:32:55 +01004267int btrfs_scrub_cancel_dev(struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004268{
David Sterba163e97e2019-03-20 16:32:55 +01004269 struct btrfs_fs_info *fs_info = dev->fs_info;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004270 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004271
4272 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004273 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004274 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004275 mutex_unlock(&fs_info->scrub_lock);
4276 return -ENOTCONN;
4277 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004278 atomic_inc(&sctx->cancel_req);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004279 while (dev->scrub_ctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004280 mutex_unlock(&fs_info->scrub_lock);
4281 wait_event(fs_info->scrub_pause_wait,
Anand Jaincadbc0a2018-01-03 16:08:30 +08004282 dev->scrub_ctx == NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004283 mutex_lock(&fs_info->scrub_lock);
4284 }
4285 mutex_unlock(&fs_info->scrub_lock);
4286
4287 return 0;
4288}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004289
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004290int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01004291 struct btrfs_scrub_progress *progress)
4292{
Josef Bacik562d7b12021-10-05 16:12:42 -04004293 struct btrfs_dev_lookup_args args = { .devid = devid };
Arne Jansena2de7332011-03-08 14:14:00 +01004294 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004295 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004296
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004297 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik562d7b12021-10-05 16:12:42 -04004298 dev = btrfs_find_device(fs_info->fs_devices, &args);
Arne Jansena2de7332011-03-08 14:14:00 +01004299 if (dev)
Anand Jaincadbc0a2018-01-03 16:08:30 +08004300 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004301 if (sctx)
4302 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004303 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004304
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004305 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004306}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004307
4308static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
Qu Wenruofa485d22020-12-02 14:48:07 +08004309 u64 extent_logical, u32 extent_len,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004310 u64 *extent_physical,
4311 struct btrfs_device **extent_dev,
4312 int *extent_mirror_num)
4313{
4314 u64 mapped_length;
Qu Wenruo4c664612021-09-15 15:17:16 +08004315 struct btrfs_io_context *bioc = NULL;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004316 int ret;
4317
4318 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004319 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Qu Wenruo4c664612021-09-15 15:17:16 +08004320 &mapped_length, &bioc, 0);
4321 if (ret || !bioc || mapped_length < extent_len ||
4322 !bioc->stripes[0].dev->bdev) {
4323 btrfs_put_bioc(bioc);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004324 return;
4325 }
4326
Qu Wenruo4c664612021-09-15 15:17:16 +08004327 *extent_physical = bioc->stripes[0].physical;
4328 *extent_mirror_num = bioc->mirror_num;
4329 *extent_dev = bioc->stripes[0].dev;
4330 btrfs_put_bioc(bioc);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004331}