Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
a2de733c | 2 | /* |
b6bfebc1 | 3 | * Copyright (C) 2011, 2012 STRATO. All rights reserved. |
a2de733c AJ |
4 | */ |
5 | ||
a2de733c | 6 | #include <linux/blkdev.h> |
558540c1 | 7 | #include <linux/ratelimit.h> |
de2491fd | 8 | #include <linux/sched/mm.h> |
d5178578 | 9 | #include <crypto/hash.h> |
a2de733c | 10 | #include "ctree.h" |
6e80d4f8 | 11 | #include "discard.h" |
a2de733c AJ |
12 | #include "volumes.h" |
13 | #include "disk-io.h" | |
14 | #include "ordered-data.h" | |
0ef8e451 | 15 | #include "transaction.h" |
558540c1 | 16 | #include "backref.h" |
5da6fcbc | 17 | #include "extent_io.h" |
ff023aac | 18 | #include "dev-replace.h" |
21adbd5c | 19 | #include "check-integrity.h" |
53b381b3 | 20 | #include "raid56.h" |
aac0023c | 21 | #include "block-group.h" |
12659251 | 22 | #include "zoned.h" |
c7f13d42 | 23 | #include "fs.h" |
07e81dc9 | 24 | #include "accessors.h" |
7c8ede16 | 25 | #include "file-item.h" |
2fc6822c | 26 | #include "scrub.h" |
a2de733c AJ |
27 | |
28 | /* | |
29 | * This is only the first step towards a full-features scrub. It reads all | |
30 | * extent and super block and verifies the checksums. In case a bad checksum | |
31 | * is found or the extent cannot be read, good data will be written back if | |
32 | * any can be found. | |
33 | * | |
34 | * Future enhancements: | |
a2de733c AJ |
35 | * - In case an unrepairable extent is encountered, track which files are |
36 | * affected and report them | |
a2de733c | 37 | * - track and record media errors, throw out bad devices |
a2de733c | 38 | * - add a mode to also read unallocated space |
a2de733c AJ |
39 | */ |
40 | ||
d9d181c1 | 41 | struct scrub_ctx; |
a2de733c | 42 | |
ff023aac | 43 | /* |
c9d328c0 QW |
44 | * The following three values only influence the performance. |
45 | * | |
ff023aac | 46 | * The last one configures the number of parallel and outstanding I/O |
c9d328c0 | 47 | * operations. The first one configures an upper limit for the number |
ff023aac SB |
48 | * of (dynamically allocated) pages that are added to a bio. |
49 | */ | |
e360d2f5 QW |
50 | #define SCRUB_SECTORS_PER_BIO 32 /* 128KiB per bio for 4KiB pages */ |
51 | #define SCRUB_BIOS_PER_SCTX 64 /* 8MiB per device in flight for 4KiB pages */ | |
54765392 | 52 | #define SCRUB_STRIPES_PER_SCTX 8 /* That would be 8 64K stripe per-device. */ |
7a9e9987 SB |
53 | |
54 | /* | |
0bb3acdc | 55 | * The following value times PAGE_SIZE needs to be large enough to match the |
7a9e9987 | 56 | * largest node/leaf/sector size that shall be supported. |
7a9e9987 | 57 | */ |
7e737cbc | 58 | #define SCRUB_MAX_SECTORS_PER_BLOCK (BTRFS_MAX_METADATA_BLOCKSIZE / SZ_4K) |
a2de733c | 59 | |
f3e01e0e QW |
60 | #define SCRUB_MAX_PAGES (DIV_ROUND_UP(BTRFS_MAX_METADATA_BLOCKSIZE, PAGE_SIZE)) |
61 | ||
ed4c491a JB |
62 | /* |
63 | * Maximum number of mirrors that can be available for all profiles counting | |
64 | * the target device of dev-replace as one. During an active device replace | |
65 | * procedure, the target device of the copy operation is a mirror for the | |
66 | * filesystem data as well that can be used to read data in order to repair | |
67 | * read errors on other disks. | |
68 | * | |
69 | * Current value is derived from RAID1C4 with 4 copies. | |
70 | */ | |
71 | #define BTRFS_MAX_MIRRORS (4 + 1) | |
72 | ||
2af2aaf9 QW |
73 | /* Represent one sector and its needed info to verify the content. */ |
74 | struct scrub_sector_verification { | |
75 | bool is_metadata; | |
76 | ||
77 | union { | |
78 | /* | |
79 | * Csum pointer for data csum verification. Should point to a | |
80 | * sector csum inside scrub_stripe::csums. | |
81 | * | |
82 | * NULL if this data sector has no csum. | |
83 | */ | |
84 | u8 *csum; | |
85 | ||
86 | /* | |
87 | * Extra info for metadata verification. All sectors inside a | |
88 | * tree block share the same generation. | |
89 | */ | |
90 | u64 generation; | |
91 | }; | |
92 | }; | |
93 | ||
94 | enum scrub_stripe_flags { | |
95 | /* Set when @mirror_num, @dev, @physical and @logical are set. */ | |
96 | SCRUB_STRIPE_FLAG_INITIALIZED, | |
97 | ||
98 | /* Set when the read-repair is finished. */ | |
99 | SCRUB_STRIPE_FLAG_REPAIR_DONE, | |
1009254b QW |
100 | |
101 | /* | |
102 | * Set for data stripes if it's triggered from P/Q stripe. | |
103 | * During such scrub, we should not report errors in data stripes, nor | |
104 | * update the accounting. | |
105 | */ | |
106 | SCRUB_STRIPE_FLAG_NO_REPORT, | |
2af2aaf9 QW |
107 | }; |
108 | ||
109 | #define SCRUB_STRIPE_PAGES (BTRFS_STRIPE_LEN / PAGE_SIZE) | |
110 | ||
111 | /* | |
112 | * Represent one contiguous range with a length of BTRFS_STRIPE_LEN. | |
113 | */ | |
114 | struct scrub_stripe { | |
00965807 | 115 | struct scrub_ctx *sctx; |
2af2aaf9 QW |
116 | struct btrfs_block_group *bg; |
117 | ||
118 | struct page *pages[SCRUB_STRIPE_PAGES]; | |
119 | struct scrub_sector_verification *sectors; | |
120 | ||
121 | struct btrfs_device *dev; | |
122 | u64 logical; | |
123 | u64 physical; | |
124 | ||
125 | u16 mirror_num; | |
126 | ||
127 | /* Should be BTRFS_STRIPE_LEN / sectorsize. */ | |
128 | u16 nr_sectors; | |
129 | ||
00965807 QW |
130 | /* |
131 | * How many data/meta extents are in this stripe. Only for scrub status | |
132 | * reporting purposes. | |
133 | */ | |
134 | u16 nr_data_extents; | |
135 | u16 nr_meta_extents; | |
136 | ||
2af2aaf9 QW |
137 | atomic_t pending_io; |
138 | wait_queue_head_t io_wait; | |
9ecb5ef5 | 139 | wait_queue_head_t repair_wait; |
2af2aaf9 QW |
140 | |
141 | /* | |
142 | * Indicate the states of the stripe. Bits are defined in | |
143 | * scrub_stripe_flags enum. | |
144 | */ | |
145 | unsigned long state; | |
146 | ||
147 | /* Indicate which sectors are covered by extent items. */ | |
148 | unsigned long extent_sector_bitmap; | |
149 | ||
150 | /* | |
151 | * The errors hit during the initial read of the stripe. | |
152 | * | |
153 | * Would be utilized for error reporting and repair. | |
154 | */ | |
155 | unsigned long init_error_bitmap; | |
156 | ||
157 | /* | |
158 | * The following error bitmaps are all for the current status. | |
159 | * Every time we submit a new read, these bitmaps may be updated. | |
160 | * | |
161 | * error_bitmap = io_error_bitmap | csum_error_bitmap | meta_error_bitmap; | |
162 | * | |
163 | * IO and csum errors can happen for both metadata and data. | |
164 | */ | |
165 | unsigned long error_bitmap; | |
166 | unsigned long io_error_bitmap; | |
167 | unsigned long csum_error_bitmap; | |
168 | unsigned long meta_error_bitmap; | |
169 | ||
058e09e6 QW |
170 | /* For writeback (repair or replace) error reporting. */ |
171 | unsigned long write_error_bitmap; | |
172 | ||
173 | /* Writeback can be concurrent, thus we need to protect the bitmap. */ | |
174 | spinlock_t write_error_lock; | |
175 | ||
2af2aaf9 QW |
176 | /* |
177 | * Checksum for the whole stripe if this stripe is inside a data block | |
178 | * group. | |
179 | */ | |
180 | u8 *csums; | |
9ecb5ef5 QW |
181 | |
182 | struct work_struct work; | |
2af2aaf9 QW |
183 | }; |
184 | ||
a2de733c AJ |
185 | struct scrub_bio { |
186 | int index; | |
d9d181c1 | 187 | struct scrub_ctx *sctx; |
a36cf8b8 | 188 | struct btrfs_device *dev; |
a2de733c | 189 | struct bio *bio; |
4e4cbee9 | 190 | blk_status_t status; |
a2de733c AJ |
191 | u64 logical; |
192 | u64 physical; | |
e360d2f5 | 193 | int sector_count; |
a2de733c | 194 | int next_free; |
be539518 | 195 | struct work_struct work; |
a2de733c AJ |
196 | }; |
197 | ||
d9d181c1 | 198 | struct scrub_ctx { |
ff023aac | 199 | struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX]; |
54765392 | 200 | struct scrub_stripe stripes[SCRUB_STRIPES_PER_SCTX]; |
1009254b | 201 | struct scrub_stripe *raid56_data_stripes; |
fb456252 | 202 | struct btrfs_fs_info *fs_info; |
a2de733c AJ |
203 | int first_free; |
204 | int curr; | |
54765392 | 205 | int cur_stripe; |
b6bfebc1 SB |
206 | atomic_t bios_in_flight; |
207 | atomic_t workers_pending; | |
a2de733c AJ |
208 | spinlock_t list_lock; |
209 | wait_queue_head_t list_wait; | |
a2de733c AJ |
210 | struct list_head csum_list; |
211 | atomic_t cancel_req; | |
8628764e | 212 | int readonly; |
e360d2f5 | 213 | int sectors_per_bio; |
63a212ab | 214 | |
eb3b5053 DS |
215 | /* State of IO submission throttling affecting the associated device */ |
216 | ktime_t throttle_deadline; | |
217 | u64 throttle_sent; | |
218 | ||
63a212ab | 219 | int is_dev_replace; |
de17addc | 220 | u64 write_pointer; |
3fb99303 | 221 | |
3fb99303 | 222 | struct mutex wr_lock; |
3fb99303 | 223 | struct btrfs_device *wr_tgtdev; |
63a212ab | 224 | |
a2de733c AJ |
225 | /* |
226 | * statistics | |
227 | */ | |
228 | struct btrfs_scrub_progress stat; | |
229 | spinlock_t stat_lock; | |
f55985f4 FM |
230 | |
231 | /* | |
232 | * Use a ref counter to avoid use-after-free issues. Scrub workers | |
233 | * decrement bios_in_flight and workers_pending and then do a wakeup | |
234 | * on the list_wait wait queue. We must ensure the main scrub task | |
235 | * doesn't free the scrub context before or while the workers are | |
236 | * doing the wakeup() call. | |
237 | */ | |
99f4cdb1 | 238 | refcount_t refs; |
a2de733c AJ |
239 | }; |
240 | ||
558540c1 JS |
241 | struct scrub_warning { |
242 | struct btrfs_path *path; | |
243 | u64 extent_item_size; | |
558540c1 | 244 | const char *errstr; |
6aa21263 | 245 | u64 physical; |
558540c1 JS |
246 | u64 logical; |
247 | struct btrfs_device *dev; | |
558540c1 JS |
248 | }; |
249 | ||
2af2aaf9 QW |
250 | static void release_scrub_stripe(struct scrub_stripe *stripe) |
251 | { | |
252 | if (!stripe) | |
253 | return; | |
254 | ||
255 | for (int i = 0; i < SCRUB_STRIPE_PAGES; i++) { | |
256 | if (stripe->pages[i]) | |
257 | __free_page(stripe->pages[i]); | |
258 | stripe->pages[i] = NULL; | |
259 | } | |
260 | kfree(stripe->sectors); | |
261 | kfree(stripe->csums); | |
262 | stripe->sectors = NULL; | |
263 | stripe->csums = NULL; | |
00965807 | 264 | stripe->sctx = NULL; |
2af2aaf9 QW |
265 | stripe->state = 0; |
266 | } | |
267 | ||
54765392 QW |
268 | static int init_scrub_stripe(struct btrfs_fs_info *fs_info, |
269 | struct scrub_stripe *stripe) | |
2af2aaf9 QW |
270 | { |
271 | int ret; | |
272 | ||
273 | memset(stripe, 0, sizeof(*stripe)); | |
274 | ||
275 | stripe->nr_sectors = BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits; | |
276 | stripe->state = 0; | |
277 | ||
278 | init_waitqueue_head(&stripe->io_wait); | |
9ecb5ef5 | 279 | init_waitqueue_head(&stripe->repair_wait); |
2af2aaf9 | 280 | atomic_set(&stripe->pending_io, 0); |
058e09e6 | 281 | spin_lock_init(&stripe->write_error_lock); |
2af2aaf9 QW |
282 | |
283 | ret = btrfs_alloc_page_array(SCRUB_STRIPE_PAGES, stripe->pages); | |
284 | if (ret < 0) | |
285 | goto error; | |
286 | ||
287 | stripe->sectors = kcalloc(stripe->nr_sectors, | |
288 | sizeof(struct scrub_sector_verification), | |
289 | GFP_KERNEL); | |
290 | if (!stripe->sectors) | |
291 | goto error; | |
292 | ||
293 | stripe->csums = kcalloc(BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits, | |
294 | fs_info->csum_size, GFP_KERNEL); | |
295 | if (!stripe->csums) | |
296 | goto error; | |
297 | return 0; | |
298 | error: | |
299 | release_scrub_stripe(stripe); | |
300 | return -ENOMEM; | |
301 | } | |
302 | ||
9ecb5ef5 | 303 | static void wait_scrub_stripe_io(struct scrub_stripe *stripe) |
2af2aaf9 QW |
304 | { |
305 | wait_event(stripe->io_wait, atomic_read(&stripe->pending_io) == 0); | |
306 | } | |
307 | ||
be539518 | 308 | static void scrub_bio_end_io_worker(struct work_struct *work); |
f55985f4 | 309 | static void scrub_put_ctx(struct scrub_ctx *sctx); |
1623edeb | 310 | |
b6bfebc1 SB |
311 | static void scrub_pending_bio_inc(struct scrub_ctx *sctx) |
312 | { | |
99f4cdb1 | 313 | refcount_inc(&sctx->refs); |
b6bfebc1 SB |
314 | atomic_inc(&sctx->bios_in_flight); |
315 | } | |
316 | ||
317 | static void scrub_pending_bio_dec(struct scrub_ctx *sctx) | |
318 | { | |
319 | atomic_dec(&sctx->bios_in_flight); | |
320 | wake_up(&sctx->list_wait); | |
f55985f4 | 321 | scrub_put_ctx(sctx); |
b6bfebc1 SB |
322 | } |
323 | ||
cb7ab021 | 324 | static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info) |
3cb0929a WS |
325 | { |
326 | while (atomic_read(&fs_info->scrub_pause_req)) { | |
327 | mutex_unlock(&fs_info->scrub_lock); | |
328 | wait_event(fs_info->scrub_pause_wait, | |
329 | atomic_read(&fs_info->scrub_pause_req) == 0); | |
330 | mutex_lock(&fs_info->scrub_lock); | |
331 | } | |
332 | } | |
333 | ||
0e22be89 | 334 | static void scrub_pause_on(struct btrfs_fs_info *fs_info) |
cb7ab021 WS |
335 | { |
336 | atomic_inc(&fs_info->scrubs_paused); | |
337 | wake_up(&fs_info->scrub_pause_wait); | |
0e22be89 | 338 | } |
cb7ab021 | 339 | |
0e22be89 Z |
340 | static void scrub_pause_off(struct btrfs_fs_info *fs_info) |
341 | { | |
cb7ab021 WS |
342 | mutex_lock(&fs_info->scrub_lock); |
343 | __scrub_blocked_if_needed(fs_info); | |
344 | atomic_dec(&fs_info->scrubs_paused); | |
345 | mutex_unlock(&fs_info->scrub_lock); | |
346 | ||
347 | wake_up(&fs_info->scrub_pause_wait); | |
348 | } | |
349 | ||
0e22be89 Z |
350 | static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info) |
351 | { | |
352 | scrub_pause_on(fs_info); | |
353 | scrub_pause_off(fs_info); | |
354 | } | |
355 | ||
d9d181c1 | 356 | static void scrub_free_csums(struct scrub_ctx *sctx) |
a2de733c | 357 | { |
d9d181c1 | 358 | while (!list_empty(&sctx->csum_list)) { |
a2de733c | 359 | struct btrfs_ordered_sum *sum; |
d9d181c1 | 360 | sum = list_first_entry(&sctx->csum_list, |
a2de733c AJ |
361 | struct btrfs_ordered_sum, list); |
362 | list_del(&sum->list); | |
363 | kfree(sum); | |
364 | } | |
365 | } | |
366 | ||
d9d181c1 | 367 | static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx) |
a2de733c AJ |
368 | { |
369 | int i; | |
a2de733c | 370 | |
d9d181c1 | 371 | if (!sctx) |
a2de733c AJ |
372 | return; |
373 | ||
b5d67f64 | 374 | /* this can happen when scrub is cancelled */ |
d9d181c1 SB |
375 | if (sctx->curr != -1) { |
376 | struct scrub_bio *sbio = sctx->bios[sctx->curr]; | |
b5d67f64 | 377 | |
b5d67f64 SB |
378 | bio_put(sbio->bio); |
379 | } | |
380 | ||
ff023aac | 381 | for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) { |
d9d181c1 | 382 | struct scrub_bio *sbio = sctx->bios[i]; |
a2de733c AJ |
383 | |
384 | if (!sbio) | |
385 | break; | |
a2de733c AJ |
386 | kfree(sbio); |
387 | } | |
388 | ||
54765392 QW |
389 | for (i = 0; i < SCRUB_STRIPES_PER_SCTX; i++) |
390 | release_scrub_stripe(&sctx->stripes[i]); | |
391 | ||
d9d181c1 SB |
392 | scrub_free_csums(sctx); |
393 | kfree(sctx); | |
a2de733c AJ |
394 | } |
395 | ||
f55985f4 FM |
396 | static void scrub_put_ctx(struct scrub_ctx *sctx) |
397 | { | |
99f4cdb1 | 398 | if (refcount_dec_and_test(&sctx->refs)) |
f55985f4 FM |
399 | scrub_free_ctx(sctx); |
400 | } | |
401 | ||
92f7ba43 DS |
402 | static noinline_for_stack struct scrub_ctx *scrub_setup_ctx( |
403 | struct btrfs_fs_info *fs_info, int is_dev_replace) | |
a2de733c | 404 | { |
d9d181c1 | 405 | struct scrub_ctx *sctx; |
a2de733c | 406 | int i; |
a2de733c | 407 | |
58c4e173 | 408 | sctx = kzalloc(sizeof(*sctx), GFP_KERNEL); |
d9d181c1 | 409 | if (!sctx) |
a2de733c | 410 | goto nomem; |
99f4cdb1 | 411 | refcount_set(&sctx->refs, 1); |
63a212ab | 412 | sctx->is_dev_replace = is_dev_replace; |
e360d2f5 | 413 | sctx->sectors_per_bio = SCRUB_SECTORS_PER_BIO; |
d9d181c1 | 414 | sctx->curr = -1; |
92f7ba43 | 415 | sctx->fs_info = fs_info; |
e49be14b | 416 | INIT_LIST_HEAD(&sctx->csum_list); |
ff023aac | 417 | for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) { |
a2de733c AJ |
418 | struct scrub_bio *sbio; |
419 | ||
58c4e173 | 420 | sbio = kzalloc(sizeof(*sbio), GFP_KERNEL); |
a2de733c AJ |
421 | if (!sbio) |
422 | goto nomem; | |
d9d181c1 | 423 | sctx->bios[i] = sbio; |
a2de733c | 424 | |
a2de733c | 425 | sbio->index = i; |
d9d181c1 | 426 | sbio->sctx = sctx; |
e360d2f5 | 427 | sbio->sector_count = 0; |
be539518 | 428 | INIT_WORK(&sbio->work, scrub_bio_end_io_worker); |
a2de733c | 429 | |
ff023aac | 430 | if (i != SCRUB_BIOS_PER_SCTX - 1) |
d9d181c1 | 431 | sctx->bios[i]->next_free = i + 1; |
0ef8e451 | 432 | else |
d9d181c1 SB |
433 | sctx->bios[i]->next_free = -1; |
434 | } | |
54765392 QW |
435 | for (i = 0; i < SCRUB_STRIPES_PER_SCTX; i++) { |
436 | int ret; | |
437 | ||
438 | ret = init_scrub_stripe(fs_info, &sctx->stripes[i]); | |
439 | if (ret < 0) | |
440 | goto nomem; | |
441 | sctx->stripes[i].sctx = sctx; | |
442 | } | |
d9d181c1 | 443 | sctx->first_free = 0; |
b6bfebc1 SB |
444 | atomic_set(&sctx->bios_in_flight, 0); |
445 | atomic_set(&sctx->workers_pending, 0); | |
d9d181c1 | 446 | atomic_set(&sctx->cancel_req, 0); |
d9d181c1 SB |
447 | |
448 | spin_lock_init(&sctx->list_lock); | |
449 | spin_lock_init(&sctx->stat_lock); | |
450 | init_waitqueue_head(&sctx->list_wait); | |
eb3b5053 | 451 | sctx->throttle_deadline = 0; |
ff023aac | 452 | |
3fb99303 | 453 | mutex_init(&sctx->wr_lock); |
8fcdac3f | 454 | if (is_dev_replace) { |
ded56184 | 455 | WARN_ON(!fs_info->dev_replace.tgtdev); |
ded56184 | 456 | sctx->wr_tgtdev = fs_info->dev_replace.tgtdev; |
ff023aac | 457 | } |
8fcdac3f | 458 | |
d9d181c1 | 459 | return sctx; |
a2de733c AJ |
460 | |
461 | nomem: | |
d9d181c1 | 462 | scrub_free_ctx(sctx); |
a2de733c AJ |
463 | return ERR_PTR(-ENOMEM); |
464 | } | |
465 | ||
c7499a64 FM |
466 | static int scrub_print_warning_inode(u64 inum, u64 offset, u64 num_bytes, |
467 | u64 root, void *warn_ctx) | |
558540c1 | 468 | { |
558540c1 JS |
469 | u32 nlink; |
470 | int ret; | |
471 | int i; | |
de2491fd | 472 | unsigned nofs_flag; |
558540c1 JS |
473 | struct extent_buffer *eb; |
474 | struct btrfs_inode_item *inode_item; | |
ff023aac | 475 | struct scrub_warning *swarn = warn_ctx; |
fb456252 | 476 | struct btrfs_fs_info *fs_info = swarn->dev->fs_info; |
558540c1 JS |
477 | struct inode_fs_paths *ipath = NULL; |
478 | struct btrfs_root *local_root; | |
1d4c08e0 | 479 | struct btrfs_key key; |
558540c1 | 480 | |
56e9357a | 481 | local_root = btrfs_get_fs_root(fs_info, root, true); |
558540c1 JS |
482 | if (IS_ERR(local_root)) { |
483 | ret = PTR_ERR(local_root); | |
484 | goto err; | |
485 | } | |
486 | ||
14692cc1 DS |
487 | /* |
488 | * this makes the path point to (inum INODE_ITEM ioff) | |
489 | */ | |
1d4c08e0 DS |
490 | key.objectid = inum; |
491 | key.type = BTRFS_INODE_ITEM_KEY; | |
492 | key.offset = 0; | |
493 | ||
494 | ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0); | |
558540c1 | 495 | if (ret) { |
00246528 | 496 | btrfs_put_root(local_root); |
558540c1 JS |
497 | btrfs_release_path(swarn->path); |
498 | goto err; | |
499 | } | |
500 | ||
501 | eb = swarn->path->nodes[0]; | |
502 | inode_item = btrfs_item_ptr(eb, swarn->path->slots[0], | |
503 | struct btrfs_inode_item); | |
558540c1 JS |
504 | nlink = btrfs_inode_nlink(eb, inode_item); |
505 | btrfs_release_path(swarn->path); | |
506 | ||
de2491fd DS |
507 | /* |
508 | * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub | |
509 | * uses GFP_NOFS in this context, so we keep it consistent but it does | |
510 | * not seem to be strictly necessary. | |
511 | */ | |
512 | nofs_flag = memalloc_nofs_save(); | |
558540c1 | 513 | ipath = init_ipath(4096, local_root, swarn->path); |
de2491fd | 514 | memalloc_nofs_restore(nofs_flag); |
26bdef54 | 515 | if (IS_ERR(ipath)) { |
00246528 | 516 | btrfs_put_root(local_root); |
26bdef54 DC |
517 | ret = PTR_ERR(ipath); |
518 | ipath = NULL; | |
519 | goto err; | |
520 | } | |
558540c1 JS |
521 | ret = paths_from_inode(inum, ipath); |
522 | ||
523 | if (ret < 0) | |
524 | goto err; | |
525 | ||
526 | /* | |
527 | * we deliberately ignore the bit ipath might have been too small to | |
528 | * hold all of the paths here | |
529 | */ | |
530 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) | |
5d163e0e | 531 | btrfs_warn_in_rcu(fs_info, |
8df507cb | 532 | "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %u, links %u (path: %s)", |
5d163e0e | 533 | swarn->errstr, swarn->logical, |
cb3e217b | 534 | btrfs_dev_name(swarn->dev), |
6aa21263 | 535 | swarn->physical, |
5d163e0e | 536 | root, inum, offset, |
8df507cb | 537 | fs_info->sectorsize, nlink, |
5d163e0e | 538 | (char *)(unsigned long)ipath->fspath->val[i]); |
558540c1 | 539 | |
00246528 | 540 | btrfs_put_root(local_root); |
558540c1 JS |
541 | free_ipath(ipath); |
542 | return 0; | |
543 | ||
544 | err: | |
5d163e0e | 545 | btrfs_warn_in_rcu(fs_info, |
6aa21263 | 546 | "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d", |
5d163e0e | 547 | swarn->errstr, swarn->logical, |
cb3e217b | 548 | btrfs_dev_name(swarn->dev), |
6aa21263 | 549 | swarn->physical, |
5d163e0e | 550 | root, inum, offset, ret); |
558540c1 JS |
551 | |
552 | free_ipath(ipath); | |
553 | return 0; | |
554 | } | |
555 | ||
00965807 QW |
556 | static void scrub_print_common_warning(const char *errstr, struct btrfs_device *dev, |
557 | bool is_super, u64 logical, u64 physical) | |
558540c1 | 558 | { |
00965807 | 559 | struct btrfs_fs_info *fs_info = dev->fs_info; |
558540c1 JS |
560 | struct btrfs_path *path; |
561 | struct btrfs_key found_key; | |
562 | struct extent_buffer *eb; | |
563 | struct btrfs_extent_item *ei; | |
564 | struct scrub_warning swarn; | |
69917e43 | 565 | unsigned long ptr = 0; |
69917e43 | 566 | u64 flags = 0; |
558540c1 | 567 | u64 ref_root; |
69917e43 | 568 | u32 item_size; |
07c9a8e0 | 569 | u8 ref_level = 0; |
69917e43 | 570 | int ret; |
558540c1 | 571 | |
e69bf81c | 572 | /* Super block error, no need to search extent tree. */ |
00965807 | 573 | if (is_super) { |
e69bf81c | 574 | btrfs_warn_in_rcu(fs_info, "%s on device %s, physical %llu", |
00965807 | 575 | errstr, btrfs_dev_name(dev), physical); |
e69bf81c QW |
576 | return; |
577 | } | |
558540c1 | 578 | path = btrfs_alloc_path(); |
8b9456da DS |
579 | if (!path) |
580 | return; | |
558540c1 | 581 | |
00965807 QW |
582 | swarn.physical = physical; |
583 | swarn.logical = logical; | |
558540c1 | 584 | swarn.errstr = errstr; |
a36cf8b8 | 585 | swarn.dev = NULL; |
558540c1 | 586 | |
69917e43 LB |
587 | ret = extent_from_logical(fs_info, swarn.logical, path, &found_key, |
588 | &flags); | |
558540c1 JS |
589 | if (ret < 0) |
590 | goto out; | |
591 | ||
558540c1 JS |
592 | swarn.extent_item_size = found_key.offset; |
593 | ||
594 | eb = path->nodes[0]; | |
595 | ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); | |
3212fa14 | 596 | item_size = btrfs_item_size(eb, path->slots[0]); |
558540c1 | 597 | |
69917e43 | 598 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
558540c1 | 599 | do { |
6eda71d0 LB |
600 | ret = tree_backref_for_extent(&ptr, eb, &found_key, ei, |
601 | item_size, &ref_root, | |
602 | &ref_level); | |
ecaeb14b | 603 | btrfs_warn_in_rcu(fs_info, |
6aa21263 | 604 | "%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu", |
5d163e0e | 605 | errstr, swarn.logical, |
cb3e217b | 606 | btrfs_dev_name(dev), |
6aa21263 | 607 | swarn.physical, |
558540c1 JS |
608 | ref_level ? "node" : "leaf", |
609 | ret < 0 ? -1 : ref_level, | |
610 | ret < 0 ? -1 : ref_root); | |
611 | } while (ret != 1); | |
d8fe29e9 | 612 | btrfs_release_path(path); |
558540c1 | 613 | } else { |
a2c8d27e FM |
614 | struct btrfs_backref_walk_ctx ctx = { 0 }; |
615 | ||
d8fe29e9 | 616 | btrfs_release_path(path); |
a2c8d27e FM |
617 | |
618 | ctx.bytenr = found_key.objectid; | |
619 | ctx.extent_item_pos = swarn.logical - found_key.objectid; | |
620 | ctx.fs_info = fs_info; | |
621 | ||
558540c1 | 622 | swarn.path = path; |
a36cf8b8 | 623 | swarn.dev = dev; |
a2c8d27e FM |
624 | |
625 | iterate_extent_inodes(&ctx, true, scrub_print_warning_inode, &swarn); | |
558540c1 JS |
626 | } |
627 | ||
628 | out: | |
629 | btrfs_free_path(path); | |
558540c1 JS |
630 | } |
631 | ||
4c664611 | 632 | static inline int scrub_nr_raid_mirrors(struct btrfs_io_context *bioc) |
af8e2d1d | 633 | { |
4c664611 | 634 | if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID5) |
10f11900 | 635 | return 2; |
4c664611 | 636 | else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID6) |
10f11900 ZL |
637 | return 3; |
638 | else | |
4c664611 | 639 | return (int)bioc->num_stripes; |
af8e2d1d MX |
640 | } |
641 | ||
10f11900 | 642 | static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type, |
18d758a2 | 643 | u64 full_stripe_logical, |
af8e2d1d MX |
644 | int nstripes, int mirror, |
645 | int *stripe_index, | |
646 | u64 *stripe_offset) | |
647 | { | |
648 | int i; | |
649 | ||
ffe2d203 | 650 | if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
18d758a2 QW |
651 | const int nr_data_stripes = (map_type & BTRFS_BLOCK_GROUP_RAID5) ? |
652 | nstripes - 1 : nstripes - 2; | |
653 | ||
af8e2d1d | 654 | /* RAID5/6 */ |
18d758a2 QW |
655 | for (i = 0; i < nr_data_stripes; i++) { |
656 | const u64 data_stripe_start = full_stripe_logical + | |
657 | (i * BTRFS_STRIPE_LEN); | |
af8e2d1d | 658 | |
18d758a2 QW |
659 | if (logical >= data_stripe_start && |
660 | logical < data_stripe_start + BTRFS_STRIPE_LEN) | |
af8e2d1d MX |
661 | break; |
662 | } | |
663 | ||
664 | *stripe_index = i; | |
18d758a2 QW |
665 | *stripe_offset = (logical - full_stripe_logical) & |
666 | BTRFS_STRIPE_LEN_MASK; | |
af8e2d1d MX |
667 | } else { |
668 | /* The other RAID type */ | |
669 | *stripe_index = mirror; | |
670 | *stripe_offset = 0; | |
671 | } | |
672 | } | |
673 | ||
de17addc NA |
674 | static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical) |
675 | { | |
676 | int ret = 0; | |
677 | u64 length; | |
678 | ||
679 | if (!btrfs_is_zoned(sctx->fs_info)) | |
680 | return 0; | |
681 | ||
7db1c5d1 NA |
682 | if (!btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) |
683 | return 0; | |
684 | ||
de17addc NA |
685 | if (sctx->write_pointer < physical) { |
686 | length = physical - sctx->write_pointer; | |
687 | ||
688 | ret = btrfs_zoned_issue_zeroout(sctx->wr_tgtdev, | |
689 | sctx->write_pointer, length); | |
690 | if (!ret) | |
691 | sctx->write_pointer = physical; | |
692 | } | |
693 | return ret; | |
694 | } | |
695 | ||
a3ddbaeb QW |
696 | static struct page *scrub_stripe_get_page(struct scrub_stripe *stripe, int sector_nr) |
697 | { | |
698 | struct btrfs_fs_info *fs_info = stripe->bg->fs_info; | |
699 | int page_index = (sector_nr << fs_info->sectorsize_bits) >> PAGE_SHIFT; | |
700 | ||
701 | return stripe->pages[page_index]; | |
702 | } | |
703 | ||
704 | static unsigned int scrub_stripe_get_page_offset(struct scrub_stripe *stripe, | |
705 | int sector_nr) | |
706 | { | |
707 | struct btrfs_fs_info *fs_info = stripe->bg->fs_info; | |
708 | ||
709 | return offset_in_page(sector_nr << fs_info->sectorsize_bits); | |
710 | } | |
711 | ||
97cf8f37 | 712 | static void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nr) |
a3ddbaeb QW |
713 | { |
714 | struct btrfs_fs_info *fs_info = stripe->bg->fs_info; | |
715 | const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits; | |
716 | const u64 logical = stripe->logical + (sector_nr << fs_info->sectorsize_bits); | |
717 | const struct page *first_page = scrub_stripe_get_page(stripe, sector_nr); | |
718 | const unsigned int first_off = scrub_stripe_get_page_offset(stripe, sector_nr); | |
719 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
720 | u8 on_disk_csum[BTRFS_CSUM_SIZE]; | |
721 | u8 calculated_csum[BTRFS_CSUM_SIZE]; | |
722 | struct btrfs_header *header; | |
723 | ||
724 | /* | |
725 | * Here we don't have a good way to attach the pages (and subpages) | |
726 | * to a dummy extent buffer, thus we have to directly grab the members | |
727 | * from pages. | |
728 | */ | |
729 | header = (struct btrfs_header *)(page_address(first_page) + first_off); | |
730 | memcpy(on_disk_csum, header->csum, fs_info->csum_size); | |
731 | ||
732 | if (logical != btrfs_stack_header_bytenr(header)) { | |
733 | bitmap_set(&stripe->csum_error_bitmap, sector_nr, sectors_per_tree); | |
734 | bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree); | |
735 | btrfs_warn_rl(fs_info, | |
736 | "tree block %llu mirror %u has bad bytenr, has %llu want %llu", | |
737 | logical, stripe->mirror_num, | |
738 | btrfs_stack_header_bytenr(header), logical); | |
739 | return; | |
740 | } | |
741 | if (memcmp(header->fsid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE) != 0) { | |
742 | bitmap_set(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree); | |
743 | bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree); | |
744 | btrfs_warn_rl(fs_info, | |
745 | "tree block %llu mirror %u has bad fsid, has %pU want %pU", | |
746 | logical, stripe->mirror_num, | |
747 | header->fsid, fs_info->fs_devices->fsid); | |
748 | return; | |
749 | } | |
750 | if (memcmp(header->chunk_tree_uuid, fs_info->chunk_tree_uuid, | |
751 | BTRFS_UUID_SIZE) != 0) { | |
752 | bitmap_set(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree); | |
753 | bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree); | |
754 | btrfs_warn_rl(fs_info, | |
755 | "tree block %llu mirror %u has bad chunk tree uuid, has %pU want %pU", | |
756 | logical, stripe->mirror_num, | |
757 | header->chunk_tree_uuid, fs_info->chunk_tree_uuid); | |
758 | return; | |
759 | } | |
760 | ||
761 | /* Now check tree block csum. */ | |
762 | shash->tfm = fs_info->csum_shash; | |
763 | crypto_shash_init(shash); | |
764 | crypto_shash_update(shash, page_address(first_page) + first_off + | |
765 | BTRFS_CSUM_SIZE, fs_info->sectorsize - BTRFS_CSUM_SIZE); | |
766 | ||
767 | for (int i = sector_nr + 1; i < sector_nr + sectors_per_tree; i++) { | |
768 | struct page *page = scrub_stripe_get_page(stripe, i); | |
769 | unsigned int page_off = scrub_stripe_get_page_offset(stripe, i); | |
770 | ||
771 | crypto_shash_update(shash, page_address(page) + page_off, | |
772 | fs_info->sectorsize); | |
773 | } | |
774 | ||
775 | crypto_shash_final(shash, calculated_csum); | |
776 | if (memcmp(calculated_csum, on_disk_csum, fs_info->csum_size) != 0) { | |
777 | bitmap_set(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree); | |
778 | bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree); | |
779 | btrfs_warn_rl(fs_info, | |
780 | "tree block %llu mirror %u has bad csum, has " CSUM_FMT " want " CSUM_FMT, | |
781 | logical, stripe->mirror_num, | |
782 | CSUM_FMT_VALUE(fs_info->csum_size, on_disk_csum), | |
783 | CSUM_FMT_VALUE(fs_info->csum_size, calculated_csum)); | |
784 | return; | |
785 | } | |
786 | if (stripe->sectors[sector_nr].generation != | |
787 | btrfs_stack_header_generation(header)) { | |
788 | bitmap_set(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree); | |
789 | bitmap_set(&stripe->error_bitmap, sector_nr, sectors_per_tree); | |
790 | btrfs_warn_rl(fs_info, | |
791 | "tree block %llu mirror %u has bad generation, has %llu want %llu", | |
792 | logical, stripe->mirror_num, | |
793 | btrfs_stack_header_generation(header), | |
794 | stripe->sectors[sector_nr].generation); | |
795 | return; | |
796 | } | |
797 | bitmap_clear(&stripe->error_bitmap, sector_nr, sectors_per_tree); | |
798 | bitmap_clear(&stripe->csum_error_bitmap, sector_nr, sectors_per_tree); | |
799 | bitmap_clear(&stripe->meta_error_bitmap, sector_nr, sectors_per_tree); | |
800 | } | |
801 | ||
97cf8f37 QW |
802 | static void scrub_verify_one_sector(struct scrub_stripe *stripe, int sector_nr) |
803 | { | |
804 | struct btrfs_fs_info *fs_info = stripe->bg->fs_info; | |
805 | struct scrub_sector_verification *sector = &stripe->sectors[sector_nr]; | |
806 | const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits; | |
807 | struct page *page = scrub_stripe_get_page(stripe, sector_nr); | |
808 | unsigned int pgoff = scrub_stripe_get_page_offset(stripe, sector_nr); | |
809 | u8 csum_buf[BTRFS_CSUM_SIZE]; | |
810 | int ret; | |
811 | ||
812 | ASSERT(sector_nr >= 0 && sector_nr < stripe->nr_sectors); | |
813 | ||
814 | /* Sector not utilized, skip it. */ | |
815 | if (!test_bit(sector_nr, &stripe->extent_sector_bitmap)) | |
816 | return; | |
817 | ||
818 | /* IO error, no need to check. */ | |
819 | if (test_bit(sector_nr, &stripe->io_error_bitmap)) | |
820 | return; | |
821 | ||
822 | /* Metadata, verify the full tree block. */ | |
823 | if (sector->is_metadata) { | |
824 | /* | |
825 | * Check if the tree block crosses the stripe boudary. If | |
826 | * crossed the boundary, we cannot verify it but only give a | |
827 | * warning. | |
828 | * | |
829 | * This can only happen on a very old filesystem where chunks | |
830 | * are not ensured to be stripe aligned. | |
831 | */ | |
832 | if (unlikely(sector_nr + sectors_per_tree > stripe->nr_sectors)) { | |
833 | btrfs_warn_rl(fs_info, | |
834 | "tree block at %llu crosses stripe boundary %llu", | |
835 | stripe->logical + | |
836 | (sector_nr << fs_info->sectorsize_bits), | |
837 | stripe->logical); | |
838 | return; | |
839 | } | |
840 | scrub_verify_one_metadata(stripe, sector_nr); | |
841 | return; | |
842 | } | |
843 | ||
844 | /* | |
845 | * Data is easier, we just verify the data csum (if we have it). For | |
846 | * cases without csum, we have no other choice but to trust it. | |
847 | */ | |
848 | if (!sector->csum) { | |
849 | clear_bit(sector_nr, &stripe->error_bitmap); | |
850 | return; | |
851 | } | |
852 | ||
853 | ret = btrfs_check_sector_csum(fs_info, page, pgoff, csum_buf, sector->csum); | |
854 | if (ret < 0) { | |
855 | set_bit(sector_nr, &stripe->csum_error_bitmap); | |
856 | set_bit(sector_nr, &stripe->error_bitmap); | |
857 | } else { | |
858 | clear_bit(sector_nr, &stripe->csum_error_bitmap); | |
859 | clear_bit(sector_nr, &stripe->error_bitmap); | |
860 | } | |
861 | } | |
862 | ||
863 | /* Verify specified sectors of a stripe. */ | |
9ecb5ef5 | 864 | static void scrub_verify_one_stripe(struct scrub_stripe *stripe, unsigned long bitmap) |
97cf8f37 QW |
865 | { |
866 | struct btrfs_fs_info *fs_info = stripe->bg->fs_info; | |
867 | const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits; | |
868 | int sector_nr; | |
869 | ||
870 | for_each_set_bit(sector_nr, &bitmap, stripe->nr_sectors) { | |
871 | scrub_verify_one_sector(stripe, sector_nr); | |
872 | if (stripe->sectors[sector_nr].is_metadata) | |
873 | sector_nr += sectors_per_tree - 1; | |
874 | } | |
875 | } | |
876 | ||
9ecb5ef5 QW |
877 | static int calc_sector_number(struct scrub_stripe *stripe, struct bio_vec *first_bvec) |
878 | { | |
879 | int i; | |
880 | ||
881 | for (i = 0; i < stripe->nr_sectors; i++) { | |
882 | if (scrub_stripe_get_page(stripe, i) == first_bvec->bv_page && | |
883 | scrub_stripe_get_page_offset(stripe, i) == first_bvec->bv_offset) | |
884 | break; | |
885 | } | |
886 | ASSERT(i < stripe->nr_sectors); | |
887 | return i; | |
888 | } | |
889 | ||
890 | /* | |
891 | * Repair read is different to the regular read: | |
892 | * | |
893 | * - Only reads the failed sectors | |
894 | * - May have extra blocksize limits | |
895 | */ | |
896 | static void scrub_repair_read_endio(struct btrfs_bio *bbio) | |
897 | { | |
898 | struct scrub_stripe *stripe = bbio->private; | |
899 | struct btrfs_fs_info *fs_info = stripe->bg->fs_info; | |
900 | struct bio_vec *bvec; | |
901 | int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio)); | |
902 | u32 bio_size = 0; | |
903 | int i; | |
904 | ||
905 | ASSERT(sector_nr < stripe->nr_sectors); | |
906 | ||
907 | bio_for_each_bvec_all(bvec, &bbio->bio, i) | |
908 | bio_size += bvec->bv_len; | |
909 | ||
910 | if (bbio->bio.bi_status) { | |
911 | bitmap_set(&stripe->io_error_bitmap, sector_nr, | |
912 | bio_size >> fs_info->sectorsize_bits); | |
913 | bitmap_set(&stripe->error_bitmap, sector_nr, | |
914 | bio_size >> fs_info->sectorsize_bits); | |
915 | } else { | |
916 | bitmap_clear(&stripe->io_error_bitmap, sector_nr, | |
917 | bio_size >> fs_info->sectorsize_bits); | |
918 | } | |
919 | bio_put(&bbio->bio); | |
920 | if (atomic_dec_and_test(&stripe->pending_io)) | |
921 | wake_up(&stripe->io_wait); | |
922 | } | |
923 | ||
924 | static int calc_next_mirror(int mirror, int num_copies) | |
925 | { | |
926 | ASSERT(mirror <= num_copies); | |
927 | return (mirror + 1 > num_copies) ? 1 : mirror + 1; | |
928 | } | |
929 | ||
930 | static void scrub_stripe_submit_repair_read(struct scrub_stripe *stripe, | |
931 | int mirror, int blocksize, bool wait) | |
932 | { | |
933 | struct btrfs_fs_info *fs_info = stripe->bg->fs_info; | |
934 | struct btrfs_bio *bbio = NULL; | |
935 | const unsigned long old_error_bitmap = stripe->error_bitmap; | |
936 | int i; | |
937 | ||
938 | ASSERT(stripe->mirror_num >= 1); | |
939 | ASSERT(atomic_read(&stripe->pending_io) == 0); | |
940 | ||
941 | for_each_set_bit(i, &old_error_bitmap, stripe->nr_sectors) { | |
942 | struct page *page; | |
943 | int pgoff; | |
944 | int ret; | |
945 | ||
946 | page = scrub_stripe_get_page(stripe, i); | |
947 | pgoff = scrub_stripe_get_page_offset(stripe, i); | |
948 | ||
949 | /* The current sector cannot be merged, submit the bio. */ | |
950 | if (bbio && ((i > 0 && !test_bit(i - 1, &stripe->error_bitmap)) || | |
951 | bbio->bio.bi_iter.bi_size >= blocksize)) { | |
952 | ASSERT(bbio->bio.bi_iter.bi_size); | |
953 | atomic_inc(&stripe->pending_io); | |
954 | btrfs_submit_bio(bbio, mirror); | |
955 | if (wait) | |
956 | wait_scrub_stripe_io(stripe); | |
957 | bbio = NULL; | |
958 | } | |
959 | ||
960 | if (!bbio) { | |
961 | bbio = btrfs_bio_alloc(stripe->nr_sectors, REQ_OP_READ, | |
962 | fs_info, scrub_repair_read_endio, stripe); | |
963 | bbio->bio.bi_iter.bi_sector = (stripe->logical + | |
964 | (i << fs_info->sectorsize_bits)) >> SECTOR_SHIFT; | |
965 | } | |
966 | ||
967 | ret = bio_add_page(&bbio->bio, page, fs_info->sectorsize, pgoff); | |
968 | ASSERT(ret == fs_info->sectorsize); | |
969 | } | |
970 | if (bbio) { | |
971 | ASSERT(bbio->bio.bi_iter.bi_size); | |
972 | atomic_inc(&stripe->pending_io); | |
973 | btrfs_submit_bio(bbio, mirror); | |
974 | if (wait) | |
975 | wait_scrub_stripe_io(stripe); | |
976 | } | |
977 | } | |
978 | ||
00965807 QW |
979 | static void scrub_stripe_report_errors(struct scrub_ctx *sctx, |
980 | struct scrub_stripe *stripe) | |
981 | { | |
982 | static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, | |
983 | DEFAULT_RATELIMIT_BURST); | |
984 | struct btrfs_fs_info *fs_info = sctx->fs_info; | |
985 | struct btrfs_device *dev = NULL; | |
986 | u64 physical = 0; | |
987 | int nr_data_sectors = 0; | |
988 | int nr_meta_sectors = 0; | |
989 | int nr_nodatacsum_sectors = 0; | |
990 | int nr_repaired_sectors = 0; | |
991 | int sector_nr; | |
992 | ||
1009254b QW |
993 | if (test_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state)) |
994 | return; | |
995 | ||
00965807 QW |
996 | /* |
997 | * Init needed infos for error reporting. | |
998 | * | |
999 | * Although our scrub_stripe infrastucture is mostly based on btrfs_submit_bio() | |
1000 | * thus no need for dev/physical, error reporting still needs dev and physical. | |
1001 | */ | |
1002 | if (!bitmap_empty(&stripe->init_error_bitmap, stripe->nr_sectors)) { | |
1003 | u64 mapped_len = fs_info->sectorsize; | |
1004 | struct btrfs_io_context *bioc = NULL; | |
1005 | int stripe_index = stripe->mirror_num - 1; | |
1006 | int ret; | |
1007 | ||
1008 | /* For scrub, our mirror_num should always start at 1. */ | |
1009 | ASSERT(stripe->mirror_num >= 1); | |
1010 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, | |
1011 | stripe->logical, &mapped_len, &bioc); | |
1012 | /* | |
1013 | * If we failed, dev will be NULL, and later detailed reports | |
1014 | * will just be skipped. | |
1015 | */ | |
1016 | if (ret < 0) | |
1017 | goto skip; | |
1018 | physical = bioc->stripes[stripe_index].physical; | |
1019 | dev = bioc->stripes[stripe_index].dev; | |
1020 | btrfs_put_bioc(bioc); | |
1021 | } | |
1022 | ||
1023 | skip: | |
1024 | for_each_set_bit(sector_nr, &stripe->extent_sector_bitmap, stripe->nr_sectors) { | |
1025 | bool repaired = false; | |
1026 | ||
1027 | if (stripe->sectors[sector_nr].is_metadata) { | |
1028 | nr_meta_sectors++; | |
1029 | } else { | |
1030 | nr_data_sectors++; | |
1031 | if (!stripe->sectors[sector_nr].csum) | |
1032 | nr_nodatacsum_sectors++; | |
1033 | } | |
1034 | ||
1035 | if (test_bit(sector_nr, &stripe->init_error_bitmap) && | |
1036 | !test_bit(sector_nr, &stripe->error_bitmap)) { | |
1037 | nr_repaired_sectors++; | |
1038 | repaired = true; | |
1039 | } | |
1040 | ||
1041 | /* Good sector from the beginning, nothing need to be done. */ | |
1042 | if (!test_bit(sector_nr, &stripe->init_error_bitmap)) | |
1043 | continue; | |
1044 | ||
1045 | /* | |
1046 | * Report error for the corrupted sectors. If repaired, just | |
1047 | * output the message of repaired message. | |
1048 | */ | |
1049 | if (repaired) { | |
1050 | if (dev) { | |
1051 | btrfs_err_rl_in_rcu(fs_info, | |
1052 | "fixed up error at logical %llu on dev %s physical %llu", | |
1053 | stripe->logical, btrfs_dev_name(dev), | |
1054 | physical); | |
1055 | } else { | |
1056 | btrfs_err_rl_in_rcu(fs_info, | |
1057 | "fixed up error at logical %llu on mirror %u", | |
1058 | stripe->logical, stripe->mirror_num); | |
1059 | } | |
1060 | continue; | |
1061 | } | |
1062 | ||
1063 | /* The remaining are all for unrepaired. */ | |
1064 | if (dev) { | |
1065 | btrfs_err_rl_in_rcu(fs_info, | |
1066 | "unable to fixup (regular) error at logical %llu on dev %s physical %llu", | |
1067 | stripe->logical, btrfs_dev_name(dev), | |
1068 | physical); | |
1069 | } else { | |
1070 | btrfs_err_rl_in_rcu(fs_info, | |
1071 | "unable to fixup (regular) error at logical %llu on mirror %u", | |
1072 | stripe->logical, stripe->mirror_num); | |
1073 | } | |
1074 | ||
1075 | if (test_bit(sector_nr, &stripe->io_error_bitmap)) | |
1076 | if (__ratelimit(&rs) && dev) | |
1077 | scrub_print_common_warning("i/o error", dev, false, | |
1078 | stripe->logical, physical); | |
1079 | if (test_bit(sector_nr, &stripe->csum_error_bitmap)) | |
1080 | if (__ratelimit(&rs) && dev) | |
1081 | scrub_print_common_warning("checksum error", dev, false, | |
1082 | stripe->logical, physical); | |
1083 | if (test_bit(sector_nr, &stripe->meta_error_bitmap)) | |
1084 | if (__ratelimit(&rs) && dev) | |
1085 | scrub_print_common_warning("header error", dev, false, | |
1086 | stripe->logical, physical); | |
1087 | } | |
1088 | ||
1089 | spin_lock(&sctx->stat_lock); | |
1090 | sctx->stat.data_extents_scrubbed += stripe->nr_data_extents; | |
1091 | sctx->stat.tree_extents_scrubbed += stripe->nr_meta_extents; | |
1092 | sctx->stat.data_bytes_scrubbed += nr_data_sectors << fs_info->sectorsize_bits; | |
1093 | sctx->stat.tree_bytes_scrubbed += nr_meta_sectors << fs_info->sectorsize_bits; | |
1094 | sctx->stat.no_csum += nr_nodatacsum_sectors; | |
1095 | sctx->stat.read_errors += | |
1096 | bitmap_weight(&stripe->io_error_bitmap, stripe->nr_sectors); | |
1097 | sctx->stat.csum_errors += | |
1098 | bitmap_weight(&stripe->csum_error_bitmap, stripe->nr_sectors); | |
1099 | sctx->stat.verify_errors += | |
1100 | bitmap_weight(&stripe->meta_error_bitmap, stripe->nr_sectors); | |
1101 | sctx->stat.uncorrectable_errors += | |
1102 | bitmap_weight(&stripe->error_bitmap, stripe->nr_sectors); | |
1103 | sctx->stat.corrected_errors += nr_repaired_sectors; | |
1104 | spin_unlock(&sctx->stat_lock); | |
1105 | } | |
1106 | ||
9ecb5ef5 QW |
1107 | /* |
1108 | * The main entrance for all read related scrub work, including: | |
1109 | * | |
1110 | * - Wait for the initial read to finish | |
1111 | * - Verify and locate any bad sectors | |
1112 | * - Go through the remaining mirrors and try to read as large blocksize as | |
1113 | * possible | |
1114 | * - Go through all mirrors (including the failed mirror) sector-by-sector | |
1115 | * | |
1116 | * Writeback does not happen here, it needs extra synchronization. | |
1117 | */ | |
1118 | static void scrub_stripe_read_repair_worker(struct work_struct *work) | |
1119 | { | |
1120 | struct scrub_stripe *stripe = container_of(work, struct scrub_stripe, work); | |
1121 | struct btrfs_fs_info *fs_info = stripe->bg->fs_info; | |
1122 | int num_copies = btrfs_num_copies(fs_info, stripe->bg->start, | |
1123 | stripe->bg->length); | |
1124 | int mirror; | |
1125 | int i; | |
1126 | ||
1127 | ASSERT(stripe->mirror_num > 0); | |
1128 | ||
1129 | wait_scrub_stripe_io(stripe); | |
1130 | scrub_verify_one_stripe(stripe, stripe->extent_sector_bitmap); | |
1131 | /* Save the initial failed bitmap for later repair and report usage. */ | |
1132 | stripe->init_error_bitmap = stripe->error_bitmap; | |
1133 | ||
1134 | if (bitmap_empty(&stripe->init_error_bitmap, stripe->nr_sectors)) | |
1135 | goto out; | |
1136 | ||
1137 | /* | |
1138 | * Try all remaining mirrors. | |
1139 | * | |
1140 | * Here we still try to read as large block as possible, as this is | |
1141 | * faster and we have extra safety nets to rely on. | |
1142 | */ | |
1143 | for (mirror = calc_next_mirror(stripe->mirror_num, num_copies); | |
1144 | mirror != stripe->mirror_num; | |
1145 | mirror = calc_next_mirror(mirror, num_copies)) { | |
1146 | const unsigned long old_error_bitmap = stripe->error_bitmap; | |
1147 | ||
1148 | scrub_stripe_submit_repair_read(stripe, mirror, | |
1149 | BTRFS_STRIPE_LEN, false); | |
1150 | wait_scrub_stripe_io(stripe); | |
1151 | scrub_verify_one_stripe(stripe, old_error_bitmap); | |
1152 | if (bitmap_empty(&stripe->error_bitmap, stripe->nr_sectors)) | |
1153 | goto out; | |
1154 | } | |
1155 | ||
1156 | /* | |
1157 | * Last safety net, try re-checking all mirrors, including the failed | |
1158 | * one, sector-by-sector. | |
1159 | * | |
1160 | * As if one sector failed the drive's internal csum, the whole read | |
1161 | * containing the offending sector would be marked as error. | |
1162 | * Thus here we do sector-by-sector read. | |
1163 | * | |
1164 | * This can be slow, thus we only try it as the last resort. | |
1165 | */ | |
1166 | ||
1167 | for (i = 0, mirror = stripe->mirror_num; | |
1168 | i < num_copies; | |
1169 | i++, mirror = calc_next_mirror(mirror, num_copies)) { | |
1170 | const unsigned long old_error_bitmap = stripe->error_bitmap; | |
1171 | ||
1172 | scrub_stripe_submit_repair_read(stripe, mirror, | |
1173 | fs_info->sectorsize, true); | |
1174 | wait_scrub_stripe_io(stripe); | |
1175 | scrub_verify_one_stripe(stripe, old_error_bitmap); | |
1176 | if (bitmap_empty(&stripe->error_bitmap, stripe->nr_sectors)) | |
1177 | goto out; | |
1178 | } | |
1179 | out: | |
00965807 | 1180 | scrub_stripe_report_errors(stripe->sctx, stripe); |
9ecb5ef5 QW |
1181 | set_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state); |
1182 | wake_up(&stripe->repair_wait); | |
1183 | } | |
1184 | ||
54765392 | 1185 | static void scrub_read_endio(struct btrfs_bio *bbio) |
9ecb5ef5 QW |
1186 | { |
1187 | struct scrub_stripe *stripe = bbio->private; | |
1188 | ||
1189 | if (bbio->bio.bi_status) { | |
1190 | bitmap_set(&stripe->io_error_bitmap, 0, stripe->nr_sectors); | |
1191 | bitmap_set(&stripe->error_bitmap, 0, stripe->nr_sectors); | |
1192 | } else { | |
1193 | bitmap_clear(&stripe->io_error_bitmap, 0, stripe->nr_sectors); | |
1194 | } | |
1195 | bio_put(&bbio->bio); | |
1196 | if (atomic_dec_and_test(&stripe->pending_io)) { | |
1197 | wake_up(&stripe->io_wait); | |
1198 | INIT_WORK(&stripe->work, scrub_stripe_read_repair_worker); | |
1199 | queue_work(stripe->bg->fs_info->scrub_workers, &stripe->work); | |
1200 | } | |
1201 | } | |
1202 | ||
058e09e6 QW |
1203 | static void scrub_write_endio(struct btrfs_bio *bbio) |
1204 | { | |
1205 | struct scrub_stripe *stripe = bbio->private; | |
1206 | struct btrfs_fs_info *fs_info = stripe->bg->fs_info; | |
1207 | struct bio_vec *bvec; | |
1208 | int sector_nr = calc_sector_number(stripe, bio_first_bvec_all(&bbio->bio)); | |
1209 | u32 bio_size = 0; | |
1210 | int i; | |
1211 | ||
1212 | bio_for_each_bvec_all(bvec, &bbio->bio, i) | |
1213 | bio_size += bvec->bv_len; | |
1214 | ||
1215 | if (bbio->bio.bi_status) { | |
1216 | unsigned long flags; | |
1217 | ||
1218 | spin_lock_irqsave(&stripe->write_error_lock, flags); | |
1219 | bitmap_set(&stripe->write_error_bitmap, sector_nr, | |
1220 | bio_size >> fs_info->sectorsize_bits); | |
1221 | spin_unlock_irqrestore(&stripe->write_error_lock, flags); | |
1222 | } | |
1223 | bio_put(&bbio->bio); | |
1224 | ||
1225 | if (atomic_dec_and_test(&stripe->pending_io)) | |
1226 | wake_up(&stripe->io_wait); | |
1227 | } | |
1228 | ||
1229 | /* | |
1230 | * Submit the write bio(s) for the sectors specified by @write_bitmap. | |
1231 | * | |
1232 | * Here we utilize btrfs_submit_repair_write(), which has some extra benefits: | |
1233 | * | |
1234 | * - Only needs logical bytenr and mirror_num | |
1235 | * Just like the scrub read path | |
1236 | * | |
1237 | * - Would only result in writes to the specified mirror | |
1238 | * Unlike the regular writeback path, which would write back to all stripes | |
1239 | * | |
1240 | * - Handle dev-replace and read-repair writeback differently | |
1241 | */ | |
54765392 QW |
1242 | static void scrub_write_sectors(struct scrub_ctx *sctx, struct scrub_stripe *stripe, |
1243 | unsigned long write_bitmap, bool dev_replace) | |
058e09e6 QW |
1244 | { |
1245 | struct btrfs_fs_info *fs_info = stripe->bg->fs_info; | |
1246 | struct btrfs_bio *bbio = NULL; | |
1247 | const bool zoned = btrfs_is_zoned(fs_info); | |
1248 | int sector_nr; | |
1249 | ||
1250 | for_each_set_bit(sector_nr, &write_bitmap, stripe->nr_sectors) { | |
1251 | struct page *page = scrub_stripe_get_page(stripe, sector_nr); | |
1252 | unsigned int pgoff = scrub_stripe_get_page_offset(stripe, sector_nr); | |
1253 | int ret; | |
1254 | ||
1255 | /* We should only writeback sectors covered by an extent. */ | |
1256 | ASSERT(test_bit(sector_nr, &stripe->extent_sector_bitmap)); | |
1257 | ||
1258 | /* Cannot merge with previous sector, submit the current one. */ | |
1259 | if (bbio && sector_nr && !test_bit(sector_nr - 1, &write_bitmap)) { | |
1260 | fill_writer_pointer_gap(sctx, stripe->physical + | |
1261 | (sector_nr << fs_info->sectorsize_bits)); | |
1262 | atomic_inc(&stripe->pending_io); | |
1263 | btrfs_submit_repair_write(bbio, stripe->mirror_num, dev_replace); | |
1264 | /* For zoned writeback, queue depth must be 1. */ | |
1265 | if (zoned) | |
1266 | wait_scrub_stripe_io(stripe); | |
1267 | bbio = NULL; | |
1268 | } | |
1269 | if (!bbio) { | |
1270 | bbio = btrfs_bio_alloc(stripe->nr_sectors, REQ_OP_WRITE, | |
1271 | fs_info, scrub_write_endio, stripe); | |
1272 | bbio->bio.bi_iter.bi_sector = (stripe->logical + | |
1273 | (sector_nr << fs_info->sectorsize_bits)) >> | |
1274 | SECTOR_SHIFT; | |
1275 | } | |
1276 | ret = bio_add_page(&bbio->bio, page, fs_info->sectorsize, pgoff); | |
1277 | ASSERT(ret == fs_info->sectorsize); | |
1278 | } | |
1279 | if (bbio) { | |
1280 | fill_writer_pointer_gap(sctx, bbio->bio.bi_iter.bi_sector << | |
1281 | SECTOR_SHIFT); | |
1282 | atomic_inc(&stripe->pending_io); | |
1283 | btrfs_submit_repair_write(bbio, stripe->mirror_num, dev_replace); | |
1284 | if (zoned) | |
1285 | wait_scrub_stripe_io(stripe); | |
1286 | } | |
1287 | } | |
1288 | ||
e02ee89b QW |
1289 | static void scrub_throttle_dev_io(struct scrub_ctx *sctx, struct btrfs_device *device, |
1290 | unsigned int bio_size) | |
eb3b5053 DS |
1291 | { |
1292 | const int time_slice = 1000; | |
eb3b5053 DS |
1293 | s64 delta; |
1294 | ktime_t now; | |
1295 | u32 div; | |
1296 | u64 bwlimit; | |
1297 | ||
eb3b5053 DS |
1298 | bwlimit = READ_ONCE(device->scrub_speed_max); |
1299 | if (bwlimit == 0) | |
1300 | return; | |
1301 | ||
1302 | /* | |
1303 | * Slice is divided into intervals when the IO is submitted, adjust by | |
1304 | * bwlimit and maximum of 64 intervals. | |
1305 | */ | |
1306 | div = max_t(u32, 1, (u32)(bwlimit / (16 * 1024 * 1024))); | |
1307 | div = min_t(u32, 64, div); | |
1308 | ||
1309 | /* Start new epoch, set deadline */ | |
1310 | now = ktime_get(); | |
1311 | if (sctx->throttle_deadline == 0) { | |
1312 | sctx->throttle_deadline = ktime_add_ms(now, time_slice / div); | |
1313 | sctx->throttle_sent = 0; | |
1314 | } | |
1315 | ||
1316 | /* Still in the time to send? */ | |
1317 | if (ktime_before(now, sctx->throttle_deadline)) { | |
1318 | /* If current bio is within the limit, send it */ | |
e02ee89b | 1319 | sctx->throttle_sent += bio_size; |
eb3b5053 DS |
1320 | if (sctx->throttle_sent <= div_u64(bwlimit, div)) |
1321 | return; | |
1322 | ||
1323 | /* We're over the limit, sleep until the rest of the slice */ | |
1324 | delta = ktime_ms_delta(sctx->throttle_deadline, now); | |
1325 | } else { | |
1326 | /* New request after deadline, start new epoch */ | |
1327 | delta = 0; | |
1328 | } | |
1329 | ||
1330 | if (delta) { | |
1331 | long timeout; | |
1332 | ||
1333 | timeout = div_u64(delta * HZ, 1000); | |
1334 | schedule_timeout_interruptible(timeout); | |
1335 | } | |
1336 | ||
1337 | /* Next call will start the deadline period */ | |
1338 | sctx->throttle_deadline = 0; | |
1339 | } | |
1340 | ||
e02ee89b QW |
1341 | /* |
1342 | * Throttling of IO submission, bandwidth-limit based, the timeslice is 1 | |
1343 | * second. Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max. | |
1344 | */ | |
1345 | static void scrub_throttle(struct scrub_ctx *sctx) | |
1346 | { | |
1347 | struct scrub_bio *sbio = sctx->bios[sctx->curr]; | |
1348 | ||
1349 | scrub_throttle_dev_io(sctx, sbio->dev, sbio->bio->bi_iter.bi_size); | |
1350 | } | |
1351 | ||
d9d181c1 | 1352 | static void scrub_submit(struct scrub_ctx *sctx) |
a2de733c AJ |
1353 | { |
1354 | struct scrub_bio *sbio; | |
1355 | ||
d9d181c1 | 1356 | if (sctx->curr == -1) |
1623edeb | 1357 | return; |
a2de733c | 1358 | |
eb3b5053 DS |
1359 | scrub_throttle(sctx); |
1360 | ||
d9d181c1 SB |
1361 | sbio = sctx->bios[sctx->curr]; |
1362 | sctx->curr = -1; | |
b6bfebc1 | 1363 | scrub_pending_bio_inc(sctx); |
58ff51f1 CH |
1364 | btrfsic_check_bio(sbio->bio); |
1365 | submit_bio(sbio->bio); | |
a2de733c AJ |
1366 | } |
1367 | ||
be539518 | 1368 | static void scrub_bio_end_io_worker(struct work_struct *work) |
b5d67f64 SB |
1369 | { |
1370 | struct scrub_bio *sbio = container_of(work, struct scrub_bio, work); | |
d9d181c1 | 1371 | struct scrub_ctx *sctx = sbio->sctx; |
b5d67f64 | 1372 | |
e360d2f5 | 1373 | ASSERT(sbio->sector_count <= SCRUB_SECTORS_PER_BIO); |
b5d67f64 | 1374 | |
b5d67f64 SB |
1375 | bio_put(sbio->bio); |
1376 | sbio->bio = NULL; | |
d9d181c1 SB |
1377 | spin_lock(&sctx->list_lock); |
1378 | sbio->next_free = sctx->first_free; | |
1379 | sctx->first_free = sbio->index; | |
1380 | spin_unlock(&sctx->list_lock); | |
ff023aac | 1381 | |
b6bfebc1 | 1382 | scrub_pending_bio_dec(sctx); |
b5d67f64 SB |
1383 | } |
1384 | ||
480a8ec8 QW |
1385 | static void drop_csum_range(struct scrub_ctx *sctx, struct btrfs_ordered_sum *sum) |
1386 | { | |
1387 | sctx->stat.csum_discards += sum->len >> sctx->fs_info->sectorsize_bits; | |
1388 | list_del(&sum->list); | |
1389 | kfree(sum); | |
1390 | } | |
1391 | ||
1392 | /* | |
1393 | * Find the desired csum for range [logical, logical + sectorsize), and store | |
1394 | * the csum into @csum. | |
1395 | * | |
1396 | * The search source is sctx->csum_list, which is a pre-populated list | |
1a9fd417 | 1397 | * storing bytenr ordered csum ranges. We're responsible to cleanup any range |
480a8ec8 QW |
1398 | * that is before @logical. |
1399 | * | |
1400 | * Return 0 if there is no csum for the range. | |
1401 | * Return 1 if there is csum for the range and copied to @csum. | |
1402 | */ | |
5dc96f8d | 1403 | int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum) |
a2de733c | 1404 | { |
480a8ec8 | 1405 | bool found = false; |
a2de733c | 1406 | |
d9d181c1 | 1407 | while (!list_empty(&sctx->csum_list)) { |
480a8ec8 QW |
1408 | struct btrfs_ordered_sum *sum = NULL; |
1409 | unsigned long index; | |
1410 | unsigned long num_sectors; | |
1411 | ||
d9d181c1 | 1412 | sum = list_first_entry(&sctx->csum_list, |
a2de733c | 1413 | struct btrfs_ordered_sum, list); |
480a8ec8 | 1414 | /* The current csum range is beyond our range, no csum found */ |
a2de733c | 1415 | if (sum->bytenr > logical) |
a2de733c AJ |
1416 | break; |
1417 | ||
480a8ec8 QW |
1418 | /* |
1419 | * The current sum is before our bytenr, since scrub is always | |
1420 | * done in bytenr order, the csum will never be used anymore, | |
1421 | * clean it up so that later calls won't bother with the range, | |
1422 | * and continue search the next range. | |
1423 | */ | |
1424 | if (sum->bytenr + sum->len <= logical) { | |
1425 | drop_csum_range(sctx, sum); | |
1426 | continue; | |
1427 | } | |
a2de733c | 1428 | |
480a8ec8 QW |
1429 | /* Now the csum range covers our bytenr, copy the csum */ |
1430 | found = true; | |
1431 | index = (logical - sum->bytenr) >> sctx->fs_info->sectorsize_bits; | |
1432 | num_sectors = sum->len >> sctx->fs_info->sectorsize_bits; | |
1d1bf92d | 1433 | |
480a8ec8 QW |
1434 | memcpy(csum, sum->sums + index * sctx->fs_info->csum_size, |
1435 | sctx->fs_info->csum_size); | |
1436 | ||
1437 | /* Cleanup the range if we're at the end of the csum range */ | |
1438 | if (index == num_sectors - 1) | |
1439 | drop_csum_range(sctx, sum); | |
1440 | break; | |
a2de733c | 1441 | } |
480a8ec8 QW |
1442 | if (!found) |
1443 | return 0; | |
f51a4a18 | 1444 | return 1; |
a2de733c AJ |
1445 | } |
1446 | ||
3b080b25 WS |
1447 | /* |
1448 | * Given a physical address, this will calculate it's | |
1449 | * logical offset. if this is a parity stripe, it will return | |
1450 | * the most left data stripe's logical offset. | |
1451 | * | |
1452 | * return 0 if it is a data stripe, 1 means parity stripe. | |
1453 | */ | |
1454 | static int get_raid56_logic_offset(u64 physical, int num, | |
5a6ac9ea MX |
1455 | struct map_lookup *map, u64 *offset, |
1456 | u64 *stripe_start) | |
3b080b25 WS |
1457 | { |
1458 | int i; | |
1459 | int j = 0; | |
3b080b25 | 1460 | u64 last_offset; |
cff82672 | 1461 | const int data_stripes = nr_data_stripes(map); |
3b080b25 | 1462 | |
cff82672 | 1463 | last_offset = (physical - map->stripes[num].physical) * data_stripes; |
5a6ac9ea MX |
1464 | if (stripe_start) |
1465 | *stripe_start = last_offset; | |
1466 | ||
3b080b25 | 1467 | *offset = last_offset; |
cff82672 | 1468 | for (i = 0; i < data_stripes; i++) { |
6ded22c1 QW |
1469 | u32 stripe_nr; |
1470 | u32 stripe_index; | |
1471 | u32 rot; | |
1472 | ||
a97699d1 | 1473 | *offset = last_offset + (i << BTRFS_STRIPE_LEN_SHIFT); |
3b080b25 | 1474 | |
6ded22c1 | 1475 | stripe_nr = (u32)(*offset >> BTRFS_STRIPE_LEN_SHIFT) / data_stripes; |
3b080b25 WS |
1476 | |
1477 | /* Work out the disk rotation on this stripe-set */ | |
6ded22c1 QW |
1478 | rot = stripe_nr % map->num_stripes; |
1479 | stripe_nr /= map->num_stripes; | |
3b080b25 WS |
1480 | /* calculate which stripe this data locates */ |
1481 | rot += i; | |
e4fbaee2 | 1482 | stripe_index = rot % map->num_stripes; |
3b080b25 WS |
1483 | if (stripe_index == num) |
1484 | return 0; | |
1485 | if (stripe_index < num) | |
1486 | j++; | |
1487 | } | |
a97699d1 | 1488 | *offset = last_offset + (j << BTRFS_STRIPE_LEN_SHIFT); |
3b080b25 WS |
1489 | return 1; |
1490 | } | |
1491 | ||
416bd7e7 QW |
1492 | /* |
1493 | * Return 0 if the extent item range covers any byte of the range. | |
1494 | * Return <0 if the extent item is before @search_start. | |
1495 | * Return >0 if the extent item is after @start_start + @search_len. | |
1496 | */ | |
1497 | static int compare_extent_item_range(struct btrfs_path *path, | |
1498 | u64 search_start, u64 search_len) | |
1499 | { | |
1500 | struct btrfs_fs_info *fs_info = path->nodes[0]->fs_info; | |
1501 | u64 len; | |
1502 | struct btrfs_key key; | |
1503 | ||
1504 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1505 | ASSERT(key.type == BTRFS_EXTENT_ITEM_KEY || | |
1506 | key.type == BTRFS_METADATA_ITEM_KEY); | |
1507 | if (key.type == BTRFS_METADATA_ITEM_KEY) | |
1508 | len = fs_info->nodesize; | |
1509 | else | |
1510 | len = key.offset; | |
1511 | ||
1512 | if (key.objectid + len <= search_start) | |
1513 | return -1; | |
1514 | if (key.objectid >= search_start + search_len) | |
1515 | return 1; | |
1516 | return 0; | |
1517 | } | |
1518 | ||
1519 | /* | |
1520 | * Locate one extent item which covers any byte in range | |
1521 | * [@search_start, @search_start + @search_length) | |
1522 | * | |
1523 | * If the path is not initialized, we will initialize the search by doing | |
1524 | * a btrfs_search_slot(). | |
1525 | * If the path is already initialized, we will use the path as the initial | |
1526 | * slot, to avoid duplicated btrfs_search_slot() calls. | |
1527 | * | |
1528 | * NOTE: If an extent item starts before @search_start, we will still | |
1529 | * return the extent item. This is for data extent crossing stripe boundary. | |
1530 | * | |
1531 | * Return 0 if we found such extent item, and @path will point to the extent item. | |
1532 | * Return >0 if no such extent item can be found, and @path will be released. | |
1533 | * Return <0 if hit fatal error, and @path will be released. | |
1534 | */ | |
1535 | static int find_first_extent_item(struct btrfs_root *extent_root, | |
1536 | struct btrfs_path *path, | |
1537 | u64 search_start, u64 search_len) | |
1538 | { | |
1539 | struct btrfs_fs_info *fs_info = extent_root->fs_info; | |
1540 | struct btrfs_key key; | |
1541 | int ret; | |
1542 | ||
1543 | /* Continue using the existing path */ | |
1544 | if (path->nodes[0]) | |
1545 | goto search_forward; | |
1546 | ||
1547 | if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) | |
1548 | key.type = BTRFS_METADATA_ITEM_KEY; | |
1549 | else | |
1550 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
1551 | key.objectid = search_start; | |
1552 | key.offset = (u64)-1; | |
1553 | ||
1554 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); | |
1555 | if (ret < 0) | |
1556 | return ret; | |
1557 | ||
1558 | ASSERT(ret > 0); | |
1559 | /* | |
1560 | * Here we intentionally pass 0 as @min_objectid, as there could be | |
1561 | * an extent item starting before @search_start. | |
1562 | */ | |
1563 | ret = btrfs_previous_extent_item(extent_root, path, 0); | |
1564 | if (ret < 0) | |
1565 | return ret; | |
1566 | /* | |
1567 | * No matter whether we have found an extent item, the next loop will | |
1568 | * properly do every check on the key. | |
1569 | */ | |
1570 | search_forward: | |
1571 | while (true) { | |
1572 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1573 | if (key.objectid >= search_start + search_len) | |
1574 | break; | |
1575 | if (key.type != BTRFS_METADATA_ITEM_KEY && | |
1576 | key.type != BTRFS_EXTENT_ITEM_KEY) | |
1577 | goto next; | |
1578 | ||
1579 | ret = compare_extent_item_range(path, search_start, search_len); | |
1580 | if (ret == 0) | |
1581 | return ret; | |
1582 | if (ret > 0) | |
1583 | break; | |
1584 | next: | |
1585 | path->slots[0]++; | |
1586 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | |
1587 | ret = btrfs_next_leaf(extent_root, path); | |
1588 | if (ret) { | |
1589 | /* Either no more item or fatal error */ | |
1590 | btrfs_release_path(path); | |
1591 | return ret; | |
1592 | } | |
1593 | } | |
1594 | } | |
1595 | btrfs_release_path(path); | |
1596 | return 1; | |
1597 | } | |
1598 | ||
09022b14 QW |
1599 | static void get_extent_info(struct btrfs_path *path, u64 *extent_start_ret, |
1600 | u64 *size_ret, u64 *flags_ret, u64 *generation_ret) | |
1601 | { | |
1602 | struct btrfs_key key; | |
1603 | struct btrfs_extent_item *ei; | |
1604 | ||
1605 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1606 | ASSERT(key.type == BTRFS_METADATA_ITEM_KEY || | |
1607 | key.type == BTRFS_EXTENT_ITEM_KEY); | |
1608 | *extent_start_ret = key.objectid; | |
1609 | if (key.type == BTRFS_METADATA_ITEM_KEY) | |
1610 | *size_ret = path->nodes[0]->fs_info->nodesize; | |
1611 | else | |
1612 | *size_ret = key.offset; | |
1613 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], struct btrfs_extent_item); | |
1614 | *flags_ret = btrfs_extent_flags(path->nodes[0], ei); | |
1615 | *generation_ret = btrfs_extent_generation(path->nodes[0], ei); | |
1616 | } | |
1617 | ||
7db1c5d1 NA |
1618 | static int sync_write_pointer_for_zoned(struct scrub_ctx *sctx, u64 logical, |
1619 | u64 physical, u64 physical_end) | |
1620 | { | |
1621 | struct btrfs_fs_info *fs_info = sctx->fs_info; | |
1622 | int ret = 0; | |
1623 | ||
1624 | if (!btrfs_is_zoned(fs_info)) | |
1625 | return 0; | |
1626 | ||
1627 | wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0); | |
1628 | ||
1629 | mutex_lock(&sctx->wr_lock); | |
1630 | if (sctx->write_pointer < physical_end) { | |
1631 | ret = btrfs_sync_zone_write_pointer(sctx->wr_tgtdev, logical, | |
1632 | physical, | |
1633 | sctx->write_pointer); | |
1634 | if (ret) | |
1635 | btrfs_err(fs_info, | |
1636 | "zoned: failed to recover write pointer"); | |
1637 | } | |
1638 | mutex_unlock(&sctx->wr_lock); | |
1639 | btrfs_dev_clear_zone_empty(sctx->wr_tgtdev, physical); | |
1640 | ||
1641 | return ret; | |
1642 | } | |
1643 | ||
b9795475 QW |
1644 | static void fill_one_extent_info(struct btrfs_fs_info *fs_info, |
1645 | struct scrub_stripe *stripe, | |
1646 | u64 extent_start, u64 extent_len, | |
1647 | u64 extent_flags, u64 extent_gen) | |
1648 | { | |
1649 | for (u64 cur_logical = max(stripe->logical, extent_start); | |
1650 | cur_logical < min(stripe->logical + BTRFS_STRIPE_LEN, | |
1651 | extent_start + extent_len); | |
1652 | cur_logical += fs_info->sectorsize) { | |
1653 | const int nr_sector = (cur_logical - stripe->logical) >> | |
1654 | fs_info->sectorsize_bits; | |
1655 | struct scrub_sector_verification *sector = | |
1656 | &stripe->sectors[nr_sector]; | |
1657 | ||
1658 | set_bit(nr_sector, &stripe->extent_sector_bitmap); | |
1659 | if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | |
1660 | sector->is_metadata = true; | |
1661 | sector->generation = extent_gen; | |
1662 | } | |
1663 | } | |
1664 | } | |
1665 | ||
1666 | static void scrub_stripe_reset_bitmaps(struct scrub_stripe *stripe) | |
1667 | { | |
1668 | stripe->extent_sector_bitmap = 0; | |
1669 | stripe->init_error_bitmap = 0; | |
1670 | stripe->error_bitmap = 0; | |
1671 | stripe->io_error_bitmap = 0; | |
1672 | stripe->csum_error_bitmap = 0; | |
1673 | stripe->meta_error_bitmap = 0; | |
1674 | } | |
1675 | ||
1676 | /* | |
1677 | * Locate one stripe which has at least one extent in its range. | |
1678 | * | |
1679 | * Return 0 if found such stripe, and store its info into @stripe. | |
1680 | * Return >0 if there is no such stripe in the specified range. | |
1681 | * Return <0 for error. | |
1682 | */ | |
54765392 QW |
1683 | static int scrub_find_fill_first_stripe(struct btrfs_block_group *bg, |
1684 | struct btrfs_device *dev, u64 physical, | |
1685 | int mirror_num, u64 logical_start, | |
1686 | u32 logical_len, | |
1687 | struct scrub_stripe *stripe) | |
b9795475 QW |
1688 | { |
1689 | struct btrfs_fs_info *fs_info = bg->fs_info; | |
1690 | struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bg->start); | |
1691 | struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bg->start); | |
1692 | const u64 logical_end = logical_start + logical_len; | |
1693 | struct btrfs_path path = { 0 }; | |
1694 | u64 cur_logical = logical_start; | |
1695 | u64 stripe_end; | |
1696 | u64 extent_start; | |
1697 | u64 extent_len; | |
1698 | u64 extent_flags; | |
1699 | u64 extent_gen; | |
1700 | int ret; | |
1701 | ||
1702 | memset(stripe->sectors, 0, sizeof(struct scrub_sector_verification) * | |
1703 | stripe->nr_sectors); | |
1704 | scrub_stripe_reset_bitmaps(stripe); | |
1705 | ||
1706 | /* The range must be inside the bg. */ | |
1707 | ASSERT(logical_start >= bg->start && logical_end <= bg->start + bg->length); | |
1708 | ||
1709 | path.search_commit_root = 1; | |
1710 | path.skip_locking = 1; | |
1711 | ||
1712 | ret = find_first_extent_item(extent_root, &path, logical_start, logical_len); | |
1713 | /* Either error or not found. */ | |
1714 | if (ret) | |
1715 | goto out; | |
1716 | get_extent_info(&path, &extent_start, &extent_len, &extent_flags, &extent_gen); | |
00965807 QW |
1717 | if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
1718 | stripe->nr_meta_extents++; | |
1719 | if (extent_flags & BTRFS_EXTENT_FLAG_DATA) | |
1720 | stripe->nr_data_extents++; | |
b9795475 QW |
1721 | cur_logical = max(extent_start, cur_logical); |
1722 | ||
1723 | /* | |
1724 | * Round down to stripe boundary. | |
1725 | * | |
1726 | * The extra calculation against bg->start is to handle block groups | |
1727 | * whose logical bytenr is not BTRFS_STRIPE_LEN aligned. | |
1728 | */ | |
1729 | stripe->logical = round_down(cur_logical - bg->start, BTRFS_STRIPE_LEN) + | |
1730 | bg->start; | |
1731 | stripe->physical = physical + stripe->logical - logical_start; | |
1732 | stripe->dev = dev; | |
1733 | stripe->bg = bg; | |
1734 | stripe->mirror_num = mirror_num; | |
1735 | stripe_end = stripe->logical + BTRFS_STRIPE_LEN - 1; | |
1736 | ||
1737 | /* Fill the first extent info into stripe->sectors[] array. */ | |
1738 | fill_one_extent_info(fs_info, stripe, extent_start, extent_len, | |
1739 | extent_flags, extent_gen); | |
1740 | cur_logical = extent_start + extent_len; | |
1741 | ||
1742 | /* Fill the extent info for the remaining sectors. */ | |
1743 | while (cur_logical <= stripe_end) { | |
1744 | ret = find_first_extent_item(extent_root, &path, cur_logical, | |
1745 | stripe_end - cur_logical + 1); | |
1746 | if (ret < 0) | |
1747 | goto out; | |
1748 | if (ret > 0) { | |
1749 | ret = 0; | |
1750 | break; | |
1751 | } | |
1752 | get_extent_info(&path, &extent_start, &extent_len, | |
1753 | &extent_flags, &extent_gen); | |
00965807 QW |
1754 | if (extent_flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
1755 | stripe->nr_meta_extents++; | |
1756 | if (extent_flags & BTRFS_EXTENT_FLAG_DATA) | |
1757 | stripe->nr_data_extents++; | |
b9795475 QW |
1758 | fill_one_extent_info(fs_info, stripe, extent_start, extent_len, |
1759 | extent_flags, extent_gen); | |
1760 | cur_logical = extent_start + extent_len; | |
1761 | } | |
1762 | ||
1763 | /* Now fill the data csum. */ | |
1764 | if (bg->flags & BTRFS_BLOCK_GROUP_DATA) { | |
1765 | int sector_nr; | |
1766 | unsigned long csum_bitmap = 0; | |
1767 | ||
1768 | /* Csum space should have already been allocated. */ | |
1769 | ASSERT(stripe->csums); | |
1770 | ||
1771 | /* | |
1772 | * Our csum bitmap should be large enough, as BTRFS_STRIPE_LEN | |
1773 | * should contain at most 16 sectors. | |
1774 | */ | |
1775 | ASSERT(BITS_PER_LONG >= BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits); | |
1776 | ||
1777 | ret = btrfs_lookup_csums_bitmap(csum_root, stripe->logical, | |
1778 | stripe_end, stripe->csums, | |
1779 | &csum_bitmap, true); | |
1780 | if (ret < 0) | |
1781 | goto out; | |
1782 | if (ret > 0) | |
1783 | ret = 0; | |
1784 | ||
1785 | for_each_set_bit(sector_nr, &csum_bitmap, stripe->nr_sectors) { | |
1786 | stripe->sectors[sector_nr].csum = stripe->csums + | |
1787 | sector_nr * fs_info->csum_size; | |
1788 | } | |
1789 | } | |
1790 | set_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state); | |
1791 | out: | |
1792 | btrfs_release_path(&path); | |
1793 | return ret; | |
1794 | } | |
1795 | ||
54765392 QW |
1796 | static void scrub_reset_stripe(struct scrub_stripe *stripe) |
1797 | { | |
1798 | scrub_stripe_reset_bitmaps(stripe); | |
1799 | ||
1800 | stripe->nr_meta_extents = 0; | |
1801 | stripe->nr_data_extents = 0; | |
1802 | stripe->state = 0; | |
1803 | ||
1804 | for (int i = 0; i < stripe->nr_sectors; i++) { | |
1805 | stripe->sectors[i].is_metadata = false; | |
1806 | stripe->sectors[i].csum = NULL; | |
1807 | stripe->sectors[i].generation = 0; | |
1808 | } | |
1809 | } | |
1810 | ||
1811 | static void scrub_submit_initial_read(struct scrub_ctx *sctx, | |
1812 | struct scrub_stripe *stripe) | |
1813 | { | |
1814 | struct btrfs_fs_info *fs_info = sctx->fs_info; | |
1815 | struct btrfs_bio *bbio; | |
1816 | int mirror = stripe->mirror_num; | |
1817 | ||
1818 | ASSERT(stripe->bg); | |
1819 | ASSERT(stripe->mirror_num > 0); | |
1820 | ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state)); | |
1821 | ||
1822 | bbio = btrfs_bio_alloc(SCRUB_STRIPE_PAGES, REQ_OP_READ, fs_info, | |
1823 | scrub_read_endio, stripe); | |
1824 | ||
1825 | /* Read the whole stripe. */ | |
1826 | bbio->bio.bi_iter.bi_sector = stripe->logical >> SECTOR_SHIFT; | |
1827 | for (int i = 0; i < BTRFS_STRIPE_LEN >> PAGE_SHIFT; i++) { | |
1828 | int ret; | |
1829 | ||
1830 | ret = bio_add_page(&bbio->bio, stripe->pages[i], PAGE_SIZE, 0); | |
1831 | /* We should have allocated enough bio vectors. */ | |
1832 | ASSERT(ret == PAGE_SIZE); | |
1833 | } | |
1834 | atomic_inc(&stripe->pending_io); | |
1835 | ||
1836 | /* | |
1837 | * For dev-replace, either user asks to avoid the source dev, or | |
1838 | * the device is missing, we try the next mirror instead. | |
1839 | */ | |
1840 | if (sctx->is_dev_replace && | |
1841 | (fs_info->dev_replace.cont_reading_from_srcdev_mode == | |
1842 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID || | |
1843 | !stripe->dev->bdev)) { | |
1844 | int num_copies = btrfs_num_copies(fs_info, stripe->bg->start, | |
1845 | stripe->bg->length); | |
1846 | ||
1847 | mirror = calc_next_mirror(mirror, num_copies); | |
1848 | } | |
1849 | btrfs_submit_bio(bbio, mirror); | |
1850 | } | |
1851 | ||
1852 | static void flush_scrub_stripes(struct scrub_ctx *sctx) | |
1853 | { | |
1854 | struct btrfs_fs_info *fs_info = sctx->fs_info; | |
1855 | struct scrub_stripe *stripe; | |
1856 | const int nr_stripes = sctx->cur_stripe; | |
1857 | ||
1858 | if (!nr_stripes) | |
1859 | return; | |
1860 | ||
1861 | ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &sctx->stripes[0].state)); | |
e02ee89b QW |
1862 | |
1863 | scrub_throttle_dev_io(sctx, sctx->stripes[0].dev, | |
1864 | nr_stripes << BTRFS_STRIPE_LEN_SHIFT); | |
54765392 QW |
1865 | for (int i = 0; i < nr_stripes; i++) { |
1866 | stripe = &sctx->stripes[i]; | |
1867 | scrub_submit_initial_read(sctx, stripe); | |
1868 | } | |
1869 | ||
1870 | for (int i = 0; i < nr_stripes; i++) { | |
1871 | stripe = &sctx->stripes[i]; | |
1872 | ||
1873 | wait_event(stripe->repair_wait, | |
1874 | test_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state)); | |
1875 | } | |
1876 | ||
1877 | /* | |
1878 | * Submit the repaired sectors. For zoned case, we cannot do repair | |
1879 | * in-place, but queue the bg to be relocated. | |
1880 | */ | |
1881 | if (btrfs_is_zoned(fs_info)) { | |
1882 | for (int i = 0; i < nr_stripes; i++) { | |
1883 | stripe = &sctx->stripes[i]; | |
1884 | ||
1885 | if (!bitmap_empty(&stripe->error_bitmap, stripe->nr_sectors)) { | |
1886 | btrfs_repair_one_zone(fs_info, | |
1887 | sctx->stripes[0].bg->start); | |
1888 | break; | |
1889 | } | |
1890 | } | |
1891 | } else { | |
1892 | for (int i = 0; i < nr_stripes; i++) { | |
1893 | unsigned long repaired; | |
1894 | ||
1895 | stripe = &sctx->stripes[i]; | |
1896 | ||
1897 | bitmap_andnot(&repaired, &stripe->init_error_bitmap, | |
1898 | &stripe->error_bitmap, stripe->nr_sectors); | |
1899 | scrub_write_sectors(sctx, stripe, repaired, false); | |
1900 | } | |
1901 | } | |
1902 | ||
1903 | /* Submit for dev-replace. */ | |
1904 | if (sctx->is_dev_replace) { | |
1905 | for (int i = 0; i < nr_stripes; i++) { | |
1906 | unsigned long good; | |
1907 | ||
1908 | stripe = &sctx->stripes[i]; | |
1909 | ||
1910 | ASSERT(stripe->dev == fs_info->dev_replace.srcdev); | |
1911 | ||
1912 | bitmap_andnot(&good, &stripe->extent_sector_bitmap, | |
1913 | &stripe->error_bitmap, stripe->nr_sectors); | |
1914 | scrub_write_sectors(sctx, stripe, good, true); | |
1915 | } | |
1916 | } | |
1917 | ||
1918 | /* Wait for the above writebacks to finish. */ | |
1919 | for (int i = 0; i < nr_stripes; i++) { | |
1920 | stripe = &sctx->stripes[i]; | |
1921 | ||
1922 | wait_scrub_stripe_io(stripe); | |
1923 | scrub_reset_stripe(stripe); | |
1924 | } | |
1925 | sctx->cur_stripe = 0; | |
1926 | } | |
1927 | ||
1009254b QW |
1928 | static void raid56_scrub_wait_endio(struct bio *bio) |
1929 | { | |
1930 | complete(bio->bi_private); | |
1931 | } | |
1932 | ||
e02ee89b QW |
1933 | static int queue_scrub_stripe(struct scrub_ctx *sctx, struct btrfs_block_group *bg, |
1934 | struct btrfs_device *dev, int mirror_num, | |
1935 | u64 logical, u32 length, u64 physical) | |
54765392 QW |
1936 | { |
1937 | struct scrub_stripe *stripe; | |
1938 | int ret; | |
1939 | ||
1940 | /* No available slot, submit all stripes and wait for them. */ | |
1941 | if (sctx->cur_stripe >= SCRUB_STRIPES_PER_SCTX) | |
1942 | flush_scrub_stripes(sctx); | |
1943 | ||
1944 | stripe = &sctx->stripes[sctx->cur_stripe]; | |
1945 | ||
1946 | /* We can queue one stripe using the remaining slot. */ | |
1947 | scrub_reset_stripe(stripe); | |
1948 | ret = scrub_find_fill_first_stripe(bg, dev, physical, mirror_num, | |
1949 | logical, length, stripe); | |
1950 | /* Either >0 as no more extents or <0 for error. */ | |
1951 | if (ret) | |
1952 | return ret; | |
1953 | sctx->cur_stripe++; | |
1954 | return 0; | |
1955 | } | |
1956 | ||
1009254b QW |
1957 | static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx, |
1958 | struct btrfs_device *scrub_dev, | |
1959 | struct btrfs_block_group *bg, | |
1960 | struct map_lookup *map, | |
1961 | u64 full_stripe_start) | |
1962 | { | |
1963 | DECLARE_COMPLETION_ONSTACK(io_done); | |
1964 | struct btrfs_fs_info *fs_info = sctx->fs_info; | |
1965 | struct btrfs_raid_bio *rbio; | |
1966 | struct btrfs_io_context *bioc = NULL; | |
1967 | struct bio *bio; | |
1968 | struct scrub_stripe *stripe; | |
1969 | bool all_empty = true; | |
1970 | const int data_stripes = nr_data_stripes(map); | |
1971 | unsigned long extent_bitmap = 0; | |
1972 | u64 length = data_stripes << BTRFS_STRIPE_LEN_SHIFT; | |
1973 | int ret; | |
1974 | ||
1975 | ASSERT(sctx->raid56_data_stripes); | |
1976 | ||
1977 | for (int i = 0; i < data_stripes; i++) { | |
1978 | int stripe_index; | |
1979 | int rot; | |
1980 | u64 physical; | |
1981 | ||
1982 | stripe = &sctx->raid56_data_stripes[i]; | |
1983 | rot = div_u64(full_stripe_start - bg->start, | |
1984 | data_stripes) >> BTRFS_STRIPE_LEN_SHIFT; | |
1985 | stripe_index = (i + rot) % map->num_stripes; | |
1986 | physical = map->stripes[stripe_index].physical + | |
1987 | (rot << BTRFS_STRIPE_LEN_SHIFT); | |
1988 | ||
1989 | scrub_reset_stripe(stripe); | |
1990 | set_bit(SCRUB_STRIPE_FLAG_NO_REPORT, &stripe->state); | |
1991 | ret = scrub_find_fill_first_stripe(bg, | |
1992 | map->stripes[stripe_index].dev, physical, 1, | |
1993 | full_stripe_start + (i << BTRFS_STRIPE_LEN_SHIFT), | |
1994 | BTRFS_STRIPE_LEN, stripe); | |
1995 | if (ret < 0) | |
1996 | goto out; | |
1997 | /* | |
1998 | * No extent in this data stripe, need to manually mark them | |
1999 | * initialized to make later read submission happy. | |
2000 | */ | |
2001 | if (ret > 0) { | |
2002 | stripe->logical = full_stripe_start + | |
2003 | (i << BTRFS_STRIPE_LEN_SHIFT); | |
2004 | stripe->dev = map->stripes[stripe_index].dev; | |
2005 | stripe->mirror_num = 1; | |
2006 | set_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state); | |
2007 | } | |
2008 | } | |
2009 | ||
2010 | /* Check if all data stripes are empty. */ | |
2011 | for (int i = 0; i < data_stripes; i++) { | |
2012 | stripe = &sctx->raid56_data_stripes[i]; | |
2013 | if (!bitmap_empty(&stripe->extent_sector_bitmap, stripe->nr_sectors)) { | |
2014 | all_empty = false; | |
2015 | break; | |
2016 | } | |
2017 | } | |
2018 | if (all_empty) { | |
2019 | ret = 0; | |
2020 | goto out; | |
2021 | } | |
2022 | ||
2023 | for (int i = 0; i < data_stripes; i++) { | |
2024 | stripe = &sctx->raid56_data_stripes[i]; | |
2025 | scrub_submit_initial_read(sctx, stripe); | |
2026 | } | |
2027 | for (int i = 0; i < data_stripes; i++) { | |
2028 | stripe = &sctx->raid56_data_stripes[i]; | |
2029 | ||
2030 | wait_event(stripe->repair_wait, | |
2031 | test_bit(SCRUB_STRIPE_FLAG_REPAIR_DONE, &stripe->state)); | |
2032 | } | |
2033 | /* For now, no zoned support for RAID56. */ | |
2034 | ASSERT(!btrfs_is_zoned(sctx->fs_info)); | |
2035 | ||
2036 | /* Writeback for the repaired sectors. */ | |
2037 | for (int i = 0; i < data_stripes; i++) { | |
2038 | unsigned long repaired; | |
2039 | ||
2040 | stripe = &sctx->raid56_data_stripes[i]; | |
2041 | ||
2042 | bitmap_andnot(&repaired, &stripe->init_error_bitmap, | |
2043 | &stripe->error_bitmap, stripe->nr_sectors); | |
2044 | scrub_write_sectors(sctx, stripe, repaired, false); | |
2045 | } | |
2046 | ||
2047 | /* Wait for the above writebacks to finish. */ | |
2048 | for (int i = 0; i < data_stripes; i++) { | |
2049 | stripe = &sctx->raid56_data_stripes[i]; | |
2050 | ||
2051 | wait_scrub_stripe_io(stripe); | |
2052 | } | |
2053 | ||
2054 | /* | |
2055 | * Now all data stripes are properly verified. Check if we have any | |
2056 | * unrepaired, if so abort immediately or we could further corrupt the | |
2057 | * P/Q stripes. | |
2058 | * | |
2059 | * During the loop, also populate extent_bitmap. | |
2060 | */ | |
2061 | for (int i = 0; i < data_stripes; i++) { | |
2062 | unsigned long error; | |
2063 | ||
2064 | stripe = &sctx->raid56_data_stripes[i]; | |
2065 | ||
2066 | /* | |
2067 | * We should only check the errors where there is an extent. | |
2068 | * As we may hit an empty data stripe while it's missing. | |
2069 | */ | |
2070 | bitmap_and(&error, &stripe->error_bitmap, | |
2071 | &stripe->extent_sector_bitmap, stripe->nr_sectors); | |
2072 | if (!bitmap_empty(&error, stripe->nr_sectors)) { | |
2073 | btrfs_err(fs_info, | |
2074 | "unrepaired sectors detected, full stripe %llu data stripe %u errors %*pbl", | |
2075 | full_stripe_start, i, stripe->nr_sectors, | |
2076 | &error); | |
2077 | ret = -EIO; | |
2078 | goto out; | |
2079 | } | |
2080 | bitmap_or(&extent_bitmap, &extent_bitmap, | |
2081 | &stripe->extent_sector_bitmap, stripe->nr_sectors); | |
2082 | } | |
2083 | ||
2084 | /* Now we can check and regenerate the P/Q stripe. */ | |
2085 | bio = bio_alloc(NULL, 1, REQ_OP_READ, GFP_NOFS); | |
2086 | bio->bi_iter.bi_sector = full_stripe_start >> SECTOR_SHIFT; | |
2087 | bio->bi_private = &io_done; | |
2088 | bio->bi_end_io = raid56_scrub_wait_endio; | |
2089 | ||
2090 | btrfs_bio_counter_inc_blocked(fs_info); | |
2091 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, full_stripe_start, | |
2092 | &length, &bioc); | |
2093 | if (ret < 0) { | |
2094 | btrfs_put_bioc(bioc); | |
2095 | btrfs_bio_counter_dec(fs_info); | |
2096 | goto out; | |
2097 | } | |
2098 | rbio = raid56_parity_alloc_scrub_rbio(bio, bioc, scrub_dev, &extent_bitmap, | |
2099 | BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits); | |
2100 | btrfs_put_bioc(bioc); | |
2101 | if (!rbio) { | |
2102 | ret = -ENOMEM; | |
2103 | btrfs_bio_counter_dec(fs_info); | |
2104 | goto out; | |
2105 | } | |
2106 | raid56_parity_submit_scrub_rbio(rbio); | |
2107 | wait_for_completion_io(&io_done); | |
2108 | ret = blk_status_to_errno(bio->bi_status); | |
2109 | bio_put(bio); | |
2110 | btrfs_bio_counter_dec(fs_info); | |
2111 | ||
2112 | out: | |
2113 | return ret; | |
2114 | } | |
2115 | ||
09022b14 QW |
2116 | /* |
2117 | * Scrub one range which can only has simple mirror based profile. | |
2118 | * (Including all range in SINGLE/DUP/RAID1/RAID1C*, and each stripe in | |
2119 | * RAID0/RAID10). | |
2120 | * | |
2121 | * Since we may need to handle a subset of block group, we need @logical_start | |
2122 | * and @logical_length parameter. | |
2123 | */ | |
2124 | static int scrub_simple_mirror(struct scrub_ctx *sctx, | |
09022b14 QW |
2125 | struct btrfs_block_group *bg, |
2126 | struct map_lookup *map, | |
2127 | u64 logical_start, u64 logical_length, | |
2128 | struct btrfs_device *device, | |
2129 | u64 physical, int mirror_num) | |
2130 | { | |
2131 | struct btrfs_fs_info *fs_info = sctx->fs_info; | |
2132 | const u64 logical_end = logical_start + logical_length; | |
2133 | /* An artificial limit, inherit from old scrub behavior */ | |
09022b14 QW |
2134 | struct btrfs_path path = { 0 }; |
2135 | u64 cur_logical = logical_start; | |
2136 | int ret; | |
2137 | ||
2138 | /* The range must be inside the bg */ | |
2139 | ASSERT(logical_start >= bg->start && logical_end <= bg->start + bg->length); | |
2140 | ||
2141 | path.search_commit_root = 1; | |
2142 | path.skip_locking = 1; | |
2143 | /* Go through each extent items inside the logical range */ | |
2144 | while (cur_logical < logical_end) { | |
e02ee89b | 2145 | u64 cur_physical = physical + cur_logical - logical_start; |
09022b14 QW |
2146 | |
2147 | /* Canceled? */ | |
2148 | if (atomic_read(&fs_info->scrub_cancel_req) || | |
2149 | atomic_read(&sctx->cancel_req)) { | |
2150 | ret = -ECANCELED; | |
2151 | break; | |
2152 | } | |
2153 | /* Paused? */ | |
2154 | if (atomic_read(&fs_info->scrub_pause_req)) { | |
2155 | /* Push queued extents */ | |
09022b14 QW |
2156 | scrub_submit(sctx); |
2157 | mutex_lock(&sctx->wr_lock); | |
09022b14 QW |
2158 | mutex_unlock(&sctx->wr_lock); |
2159 | wait_event(sctx->list_wait, | |
2160 | atomic_read(&sctx->bios_in_flight) == 0); | |
09022b14 QW |
2161 | scrub_blocked_if_needed(fs_info); |
2162 | } | |
2163 | /* Block group removed? */ | |
2164 | spin_lock(&bg->lock); | |
3349b57f | 2165 | if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) { |
09022b14 QW |
2166 | spin_unlock(&bg->lock); |
2167 | ret = 0; | |
2168 | break; | |
2169 | } | |
2170 | spin_unlock(&bg->lock); | |
2171 | ||
e02ee89b QW |
2172 | ret = queue_scrub_stripe(sctx, bg, device, mirror_num, |
2173 | cur_logical, logical_end - cur_logical, | |
2174 | cur_physical); | |
09022b14 QW |
2175 | if (ret > 0) { |
2176 | /* No more extent, just update the accounting */ | |
2177 | sctx->stat.last_physical = physical + logical_length; | |
2178 | ret = 0; | |
2179 | break; | |
2180 | } | |
2181 | if (ret < 0) | |
2182 | break; | |
09022b14 | 2183 | |
e02ee89b QW |
2184 | ASSERT(sctx->cur_stripe > 0); |
2185 | cur_logical = sctx->stripes[sctx->cur_stripe - 1].logical | |
2186 | + BTRFS_STRIPE_LEN; | |
2187 | ||
09022b14 QW |
2188 | /* Don't hold CPU for too long time */ |
2189 | cond_resched(); | |
2190 | } | |
2191 | btrfs_release_path(&path); | |
2192 | return ret; | |
2193 | } | |
2194 | ||
8557635e QW |
2195 | /* Calculate the full stripe length for simple stripe based profiles */ |
2196 | static u64 simple_stripe_full_stripe_len(const struct map_lookup *map) | |
2197 | { | |
2198 | ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
2199 | BTRFS_BLOCK_GROUP_RAID10)); | |
2200 | ||
a97699d1 | 2201 | return (map->num_stripes / map->sub_stripes) << BTRFS_STRIPE_LEN_SHIFT; |
8557635e QW |
2202 | } |
2203 | ||
2204 | /* Get the logical bytenr for the stripe */ | |
2205 | static u64 simple_stripe_get_logical(struct map_lookup *map, | |
2206 | struct btrfs_block_group *bg, | |
2207 | int stripe_index) | |
2208 | { | |
2209 | ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
2210 | BTRFS_BLOCK_GROUP_RAID10)); | |
2211 | ASSERT(stripe_index < map->num_stripes); | |
2212 | ||
2213 | /* | |
2214 | * (stripe_index / sub_stripes) gives how many data stripes we need to | |
2215 | * skip. | |
2216 | */ | |
a97699d1 QW |
2217 | return ((stripe_index / map->sub_stripes) << BTRFS_STRIPE_LEN_SHIFT) + |
2218 | bg->start; | |
8557635e QW |
2219 | } |
2220 | ||
2221 | /* Get the mirror number for the stripe */ | |
2222 | static int simple_stripe_mirror_num(struct map_lookup *map, int stripe_index) | |
2223 | { | |
2224 | ASSERT(map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
2225 | BTRFS_BLOCK_GROUP_RAID10)); | |
2226 | ASSERT(stripe_index < map->num_stripes); | |
2227 | ||
2228 | /* For RAID0, it's fixed to 1, for RAID10 it's 0,1,0,1... */ | |
2229 | return stripe_index % map->sub_stripes + 1; | |
2230 | } | |
2231 | ||
2232 | static int scrub_simple_stripe(struct scrub_ctx *sctx, | |
8557635e QW |
2233 | struct btrfs_block_group *bg, |
2234 | struct map_lookup *map, | |
2235 | struct btrfs_device *device, | |
2236 | int stripe_index) | |
2237 | { | |
2238 | const u64 logical_increment = simple_stripe_full_stripe_len(map); | |
2239 | const u64 orig_logical = simple_stripe_get_logical(map, bg, stripe_index); | |
2240 | const u64 orig_physical = map->stripes[stripe_index].physical; | |
2241 | const int mirror_num = simple_stripe_mirror_num(map, stripe_index); | |
2242 | u64 cur_logical = orig_logical; | |
2243 | u64 cur_physical = orig_physical; | |
2244 | int ret = 0; | |
2245 | ||
2246 | while (cur_logical < bg->start + bg->length) { | |
2247 | /* | |
2248 | * Inside each stripe, RAID0 is just SINGLE, and RAID10 is | |
2249 | * just RAID1, so we can reuse scrub_simple_mirror() to scrub | |
2250 | * this stripe. | |
2251 | */ | |
6b4d375a QW |
2252 | ret = scrub_simple_mirror(sctx, bg, map, cur_logical, |
2253 | BTRFS_STRIPE_LEN, device, cur_physical, | |
2254 | mirror_num); | |
8557635e QW |
2255 | if (ret) |
2256 | return ret; | |
2257 | /* Skip to next stripe which belongs to the target device */ | |
2258 | cur_logical += logical_increment; | |
2259 | /* For physical offset, we just go to next stripe */ | |
a97699d1 | 2260 | cur_physical += BTRFS_STRIPE_LEN; |
8557635e QW |
2261 | } |
2262 | return ret; | |
2263 | } | |
2264 | ||
d9d181c1 | 2265 | static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, |
2ae8ae3d | 2266 | struct btrfs_block_group *bg, |
bc88b486 | 2267 | struct extent_map *em, |
a36cf8b8 | 2268 | struct btrfs_device *scrub_dev, |
bc88b486 | 2269 | int stripe_index) |
a2de733c | 2270 | { |
fb456252 | 2271 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
bc88b486 | 2272 | struct map_lookup *map = em->map_lookup; |
09022b14 | 2273 | const u64 profile = map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK; |
2ae8ae3d | 2274 | const u64 chunk_logical = bg->start; |
a2de733c | 2275 | int ret; |
1194a824 | 2276 | u64 physical = map->stripes[stripe_index].physical; |
bc88b486 QW |
2277 | const u64 dev_stripe_len = btrfs_calc_stripe_length(em); |
2278 | const u64 physical_end = physical + dev_stripe_len; | |
a2de733c | 2279 | u64 logical; |
625f1c8d | 2280 | u64 logic_end; |
18d30ab9 | 2281 | /* The logical increment after finishing one stripe */ |
5c07c53f | 2282 | u64 increment; |
18d30ab9 | 2283 | /* Offset inside the chunk */ |
a2de733c | 2284 | u64 offset; |
5a6ac9ea | 2285 | u64 stripe_logical; |
3b080b25 | 2286 | int stop_loop = 0; |
53b381b3 | 2287 | |
d9d181c1 | 2288 | wait_event(sctx->list_wait, |
b6bfebc1 | 2289 | atomic_read(&sctx->bios_in_flight) == 0); |
cb7ab021 | 2290 | scrub_blocked_if_needed(fs_info); |
7a26285e | 2291 | |
de17addc NA |
2292 | if (sctx->is_dev_replace && |
2293 | btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) { | |
2294 | mutex_lock(&sctx->wr_lock); | |
2295 | sctx->write_pointer = physical; | |
2296 | mutex_unlock(&sctx->wr_lock); | |
de17addc NA |
2297 | } |
2298 | ||
1009254b QW |
2299 | /* Prepare the extra data stripes used by RAID56. */ |
2300 | if (profile & BTRFS_BLOCK_GROUP_RAID56_MASK) { | |
2301 | ASSERT(sctx->raid56_data_stripes == NULL); | |
2302 | ||
2303 | sctx->raid56_data_stripes = kcalloc(nr_data_stripes(map), | |
2304 | sizeof(struct scrub_stripe), | |
2305 | GFP_KERNEL); | |
2306 | if (!sctx->raid56_data_stripes) { | |
2307 | ret = -ENOMEM; | |
2308 | goto out; | |
2309 | } | |
2310 | for (int i = 0; i < nr_data_stripes(map); i++) { | |
2311 | ret = init_scrub_stripe(fs_info, | |
2312 | &sctx->raid56_data_stripes[i]); | |
2313 | if (ret < 0) | |
2314 | goto out; | |
2315 | sctx->raid56_data_stripes[i].bg = bg; | |
2316 | sctx->raid56_data_stripes[i].sctx = sctx; | |
2317 | } | |
2318 | } | |
09022b14 QW |
2319 | /* |
2320 | * There used to be a big double loop to handle all profiles using the | |
2321 | * same routine, which grows larger and more gross over time. | |
2322 | * | |
2323 | * So here we handle each profile differently, so simpler profiles | |
2324 | * have simpler scrubbing function. | |
2325 | */ | |
2326 | if (!(profile & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10 | | |
2327 | BTRFS_BLOCK_GROUP_RAID56_MASK))) { | |
2328 | /* | |
2329 | * Above check rules out all complex profile, the remaining | |
2330 | * profiles are SINGLE|DUP|RAID1|RAID1C*, which is simple | |
2331 | * mirrored duplication without stripe. | |
2332 | * | |
2333 | * Only @physical and @mirror_num needs to calculated using | |
2334 | * @stripe_index. | |
2335 | */ | |
6b4d375a QW |
2336 | ret = scrub_simple_mirror(sctx, bg, map, bg->start, bg->length, |
2337 | scrub_dev, map->stripes[stripe_index].physical, | |
09022b14 | 2338 | stripe_index + 1); |
e430c428 | 2339 | offset = 0; |
09022b14 QW |
2340 | goto out; |
2341 | } | |
8557635e | 2342 | if (profile & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
6b4d375a | 2343 | ret = scrub_simple_stripe(sctx, bg, map, scrub_dev, stripe_index); |
a97699d1 | 2344 | offset = (stripe_index / map->sub_stripes) << BTRFS_STRIPE_LEN_SHIFT; |
8557635e QW |
2345 | goto out; |
2346 | } | |
2347 | ||
2348 | /* Only RAID56 goes through the old code */ | |
2349 | ASSERT(map->type & BTRFS_BLOCK_GROUP_RAID56_MASK); | |
a2de733c | 2350 | ret = 0; |
e430c428 QW |
2351 | |
2352 | /* Calculate the logical end of the stripe */ | |
2353 | get_raid56_logic_offset(physical_end, stripe_index, | |
2354 | map, &logic_end, NULL); | |
2355 | logic_end += chunk_logical; | |
2356 | ||
2357 | /* Initialize @offset in case we need to go to out: label */ | |
2358 | get_raid56_logic_offset(physical, stripe_index, map, &offset, NULL); | |
a97699d1 | 2359 | increment = nr_data_stripes(map) << BTRFS_STRIPE_LEN_SHIFT; |
e430c428 | 2360 | |
18d30ab9 QW |
2361 | /* |
2362 | * Due to the rotation, for RAID56 it's better to iterate each stripe | |
2363 | * using their physical offset. | |
2364 | */ | |
3b080b25 | 2365 | while (physical < physical_end) { |
18d30ab9 QW |
2366 | ret = get_raid56_logic_offset(physical, stripe_index, map, |
2367 | &logical, &stripe_logical); | |
e430c428 QW |
2368 | logical += chunk_logical; |
2369 | if (ret) { | |
2370 | /* it is parity strip */ | |
2371 | stripe_logical += chunk_logical; | |
1009254b QW |
2372 | ret = scrub_raid56_parity_stripe(sctx, scrub_dev, bg, |
2373 | map, stripe_logical); | |
e430c428 QW |
2374 | if (ret) |
2375 | goto out; | |
18d30ab9 | 2376 | goto next; |
f2f66a2f ZL |
2377 | } |
2378 | ||
18d30ab9 QW |
2379 | /* |
2380 | * Now we're at a data stripe, scrub each extents in the range. | |
2381 | * | |
2382 | * At this stage, if we ignore the repair part, inside each data | |
2383 | * stripe it is no different than SINGLE profile. | |
2384 | * We can reuse scrub_simple_mirror() here, as the repair part | |
2385 | * is still based on @mirror_num. | |
2386 | */ | |
6b4d375a | 2387 | ret = scrub_simple_mirror(sctx, bg, map, logical, BTRFS_STRIPE_LEN, |
18d30ab9 | 2388 | scrub_dev, physical, 1); |
a2de733c AJ |
2389 | if (ret < 0) |
2390 | goto out; | |
a2de733c | 2391 | next: |
a2de733c | 2392 | logical += increment; |
a97699d1 | 2393 | physical += BTRFS_STRIPE_LEN; |
d9d181c1 | 2394 | spin_lock(&sctx->stat_lock); |
625f1c8d | 2395 | if (stop_loop) |
bc88b486 QW |
2396 | sctx->stat.last_physical = |
2397 | map->stripes[stripe_index].physical + dev_stripe_len; | |
625f1c8d LB |
2398 | else |
2399 | sctx->stat.last_physical = physical; | |
d9d181c1 | 2400 | spin_unlock(&sctx->stat_lock); |
625f1c8d LB |
2401 | if (stop_loop) |
2402 | break; | |
a2de733c | 2403 | } |
ff023aac | 2404 | out: |
a2de733c | 2405 | /* push queued extents */ |
d9d181c1 | 2406 | scrub_submit(sctx); |
e02ee89b | 2407 | flush_scrub_stripes(sctx); |
1009254b QW |
2408 | if (sctx->raid56_data_stripes) { |
2409 | for (int i = 0; i < nr_data_stripes(map); i++) | |
2410 | release_scrub_stripe(&sctx->raid56_data_stripes[i]); | |
2411 | kfree(sctx->raid56_data_stripes); | |
2412 | sctx->raid56_data_stripes = NULL; | |
2413 | } | |
7db1c5d1 NA |
2414 | |
2415 | if (sctx->is_dev_replace && ret >= 0) { | |
2416 | int ret2; | |
2417 | ||
2ae8ae3d QW |
2418 | ret2 = sync_write_pointer_for_zoned(sctx, |
2419 | chunk_logical + offset, | |
2420 | map->stripes[stripe_index].physical, | |
2421 | physical_end); | |
7db1c5d1 NA |
2422 | if (ret2) |
2423 | ret = ret2; | |
2424 | } | |
2425 | ||
a2de733c AJ |
2426 | return ret < 0 ? ret : 0; |
2427 | } | |
2428 | ||
d9d181c1 | 2429 | static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx, |
d04fbe19 | 2430 | struct btrfs_block_group *bg, |
a36cf8b8 | 2431 | struct btrfs_device *scrub_dev, |
020d5b73 | 2432 | u64 dev_offset, |
d04fbe19 | 2433 | u64 dev_extent_len) |
a2de733c | 2434 | { |
fb456252 | 2435 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
c8bf1b67 | 2436 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
a2de733c AJ |
2437 | struct map_lookup *map; |
2438 | struct extent_map *em; | |
2439 | int i; | |
ff023aac | 2440 | int ret = 0; |
a2de733c | 2441 | |
c8bf1b67 | 2442 | read_lock(&map_tree->lock); |
d04fbe19 | 2443 | em = lookup_extent_mapping(map_tree, bg->start, bg->length); |
c8bf1b67 | 2444 | read_unlock(&map_tree->lock); |
a2de733c | 2445 | |
020d5b73 FM |
2446 | if (!em) { |
2447 | /* | |
2448 | * Might have been an unused block group deleted by the cleaner | |
2449 | * kthread or relocation. | |
2450 | */ | |
d04fbe19 | 2451 | spin_lock(&bg->lock); |
3349b57f | 2452 | if (!test_bit(BLOCK_GROUP_FLAG_REMOVED, &bg->runtime_flags)) |
020d5b73 | 2453 | ret = -EINVAL; |
d04fbe19 | 2454 | spin_unlock(&bg->lock); |
020d5b73 FM |
2455 | |
2456 | return ret; | |
2457 | } | |
d04fbe19 | 2458 | if (em->start != bg->start) |
a2de733c | 2459 | goto out; |
d04fbe19 | 2460 | if (em->len < dev_extent_len) |
a2de733c AJ |
2461 | goto out; |
2462 | ||
d04fbe19 | 2463 | map = em->map_lookup; |
a2de733c | 2464 | for (i = 0; i < map->num_stripes; ++i) { |
a36cf8b8 | 2465 | if (map->stripes[i].dev->bdev == scrub_dev->bdev && |
859acaf1 | 2466 | map->stripes[i].physical == dev_offset) { |
bc88b486 | 2467 | ret = scrub_stripe(sctx, bg, em, scrub_dev, i); |
a2de733c AJ |
2468 | if (ret) |
2469 | goto out; | |
2470 | } | |
2471 | } | |
2472 | out: | |
2473 | free_extent_map(em); | |
2474 | ||
2475 | return ret; | |
2476 | } | |
2477 | ||
de17addc NA |
2478 | static int finish_extent_writes_for_zoned(struct btrfs_root *root, |
2479 | struct btrfs_block_group *cache) | |
2480 | { | |
2481 | struct btrfs_fs_info *fs_info = cache->fs_info; | |
2482 | struct btrfs_trans_handle *trans; | |
2483 | ||
2484 | if (!btrfs_is_zoned(fs_info)) | |
2485 | return 0; | |
2486 | ||
2487 | btrfs_wait_block_group_reservations(cache); | |
2488 | btrfs_wait_nocow_writers(cache); | |
2489 | btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start, cache->length); | |
2490 | ||
2491 | trans = btrfs_join_transaction(root); | |
2492 | if (IS_ERR(trans)) | |
2493 | return PTR_ERR(trans); | |
2494 | return btrfs_commit_transaction(trans); | |
2495 | } | |
2496 | ||
a2de733c | 2497 | static noinline_for_stack |
a36cf8b8 | 2498 | int scrub_enumerate_chunks(struct scrub_ctx *sctx, |
32934280 | 2499 | struct btrfs_device *scrub_dev, u64 start, u64 end) |
a2de733c AJ |
2500 | { |
2501 | struct btrfs_dev_extent *dev_extent = NULL; | |
2502 | struct btrfs_path *path; | |
0b246afa JM |
2503 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
2504 | struct btrfs_root *root = fs_info->dev_root; | |
a2de733c | 2505 | u64 chunk_offset; |
55e3a601 | 2506 | int ret = 0; |
76a8efa1 | 2507 | int ro_set; |
a2de733c AJ |
2508 | int slot; |
2509 | struct extent_buffer *l; | |
2510 | struct btrfs_key key; | |
2511 | struct btrfs_key found_key; | |
32da5386 | 2512 | struct btrfs_block_group *cache; |
ff023aac | 2513 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
a2de733c AJ |
2514 | |
2515 | path = btrfs_alloc_path(); | |
2516 | if (!path) | |
2517 | return -ENOMEM; | |
2518 | ||
e4058b54 | 2519 | path->reada = READA_FORWARD; |
a2de733c AJ |
2520 | path->search_commit_root = 1; |
2521 | path->skip_locking = 1; | |
2522 | ||
a36cf8b8 | 2523 | key.objectid = scrub_dev->devid; |
a2de733c AJ |
2524 | key.offset = 0ull; |
2525 | key.type = BTRFS_DEV_EXTENT_KEY; | |
2526 | ||
a2de733c | 2527 | while (1) { |
d04fbe19 QW |
2528 | u64 dev_extent_len; |
2529 | ||
a2de733c AJ |
2530 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
2531 | if (ret < 0) | |
8c51032f AJ |
2532 | break; |
2533 | if (ret > 0) { | |
2534 | if (path->slots[0] >= | |
2535 | btrfs_header_nritems(path->nodes[0])) { | |
2536 | ret = btrfs_next_leaf(root, path); | |
55e3a601 Z |
2537 | if (ret < 0) |
2538 | break; | |
2539 | if (ret > 0) { | |
2540 | ret = 0; | |
8c51032f | 2541 | break; |
55e3a601 Z |
2542 | } |
2543 | } else { | |
2544 | ret = 0; | |
8c51032f AJ |
2545 | } |
2546 | } | |
a2de733c AJ |
2547 | |
2548 | l = path->nodes[0]; | |
2549 | slot = path->slots[0]; | |
2550 | ||
2551 | btrfs_item_key_to_cpu(l, &found_key, slot); | |
2552 | ||
a36cf8b8 | 2553 | if (found_key.objectid != scrub_dev->devid) |
a2de733c AJ |
2554 | break; |
2555 | ||
962a298f | 2556 | if (found_key.type != BTRFS_DEV_EXTENT_KEY) |
a2de733c AJ |
2557 | break; |
2558 | ||
2559 | if (found_key.offset >= end) | |
2560 | break; | |
2561 | ||
2562 | if (found_key.offset < key.offset) | |
2563 | break; | |
2564 | ||
2565 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
d04fbe19 | 2566 | dev_extent_len = btrfs_dev_extent_length(l, dev_extent); |
a2de733c | 2567 | |
d04fbe19 | 2568 | if (found_key.offset + dev_extent_len <= start) |
ced96edc | 2569 | goto skip; |
a2de733c | 2570 | |
a2de733c AJ |
2571 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
2572 | ||
2573 | /* | |
2574 | * get a reference on the corresponding block group to prevent | |
2575 | * the chunk from going away while we scrub it | |
2576 | */ | |
2577 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); | |
ced96edc QW |
2578 | |
2579 | /* some chunks are removed but not committed to disk yet, | |
2580 | * continue scrubbing */ | |
2581 | if (!cache) | |
2582 | goto skip; | |
2583 | ||
a692e13d FM |
2584 | ASSERT(cache->start <= chunk_offset); |
2585 | /* | |
2586 | * We are using the commit root to search for device extents, so | |
2587 | * that means we could have found a device extent item from a | |
2588 | * block group that was deleted in the current transaction. The | |
2589 | * logical start offset of the deleted block group, stored at | |
2590 | * @chunk_offset, might be part of the logical address range of | |
2591 | * a new block group (which uses different physical extents). | |
2592 | * In this case btrfs_lookup_block_group() has returned the new | |
2593 | * block group, and its start address is less than @chunk_offset. | |
2594 | * | |
2595 | * We skip such new block groups, because it's pointless to | |
2596 | * process them, as we won't find their extents because we search | |
2597 | * for them using the commit root of the extent tree. For a device | |
2598 | * replace it's also fine to skip it, we won't miss copying them | |
2599 | * to the target device because we have the write duplication | |
2600 | * setup through the regular write path (by btrfs_map_block()), | |
2601 | * and we have committed a transaction when we started the device | |
2602 | * replace, right after setting up the device replace state. | |
2603 | */ | |
2604 | if (cache->start < chunk_offset) { | |
2605 | btrfs_put_block_group(cache); | |
2606 | goto skip; | |
2607 | } | |
2608 | ||
78ce9fc2 | 2609 | if (sctx->is_dev_replace && btrfs_is_zoned(fs_info)) { |
3349b57f | 2610 | if (!test_bit(BLOCK_GROUP_FLAG_TO_COPY, &cache->runtime_flags)) { |
0dc16ef4 FM |
2611 | btrfs_put_block_group(cache); |
2612 | goto skip; | |
78ce9fc2 | 2613 | } |
78ce9fc2 NA |
2614 | } |
2615 | ||
2473d24f FM |
2616 | /* |
2617 | * Make sure that while we are scrubbing the corresponding block | |
2618 | * group doesn't get its logical address and its device extents | |
2619 | * reused for another block group, which can possibly be of a | |
2620 | * different type and different profile. We do this to prevent | |
2621 | * false error detections and crashes due to bogus attempts to | |
2622 | * repair extents. | |
2623 | */ | |
2624 | spin_lock(&cache->lock); | |
3349b57f | 2625 | if (test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags)) { |
2473d24f FM |
2626 | spin_unlock(&cache->lock); |
2627 | btrfs_put_block_group(cache); | |
2628 | goto skip; | |
2629 | } | |
6b7304af | 2630 | btrfs_freeze_block_group(cache); |
2473d24f FM |
2631 | spin_unlock(&cache->lock); |
2632 | ||
55e3a601 Z |
2633 | /* |
2634 | * we need call btrfs_inc_block_group_ro() with scrubs_paused, | |
2635 | * to avoid deadlock caused by: | |
2636 | * btrfs_inc_block_group_ro() | |
2637 | * -> btrfs_wait_for_commit() | |
2638 | * -> btrfs_commit_transaction() | |
2639 | * -> btrfs_scrub_pause() | |
2640 | */ | |
2641 | scrub_pause_on(fs_info); | |
b12de528 QW |
2642 | |
2643 | /* | |
2644 | * Don't do chunk preallocation for scrub. | |
2645 | * | |
2646 | * This is especially important for SYSTEM bgs, or we can hit | |
2647 | * -EFBIG from btrfs_finish_chunk_alloc() like: | |
2648 | * 1. The only SYSTEM bg is marked RO. | |
2649 | * Since SYSTEM bg is small, that's pretty common. | |
2650 | * 2. New SYSTEM bg will be allocated | |
2651 | * Due to regular version will allocate new chunk. | |
2652 | * 3. New SYSTEM bg is empty and will get cleaned up | |
2653 | * Before cleanup really happens, it's marked RO again. | |
2654 | * 4. Empty SYSTEM bg get scrubbed | |
2655 | * We go back to 2. | |
2656 | * | |
2657 | * This can easily boost the amount of SYSTEM chunks if cleaner | |
2658 | * thread can't be triggered fast enough, and use up all space | |
2659 | * of btrfs_super_block::sys_chunk_array | |
1bbb97b8 QW |
2660 | * |
2661 | * While for dev replace, we need to try our best to mark block | |
2662 | * group RO, to prevent race between: | |
2663 | * - Write duplication | |
2664 | * Contains latest data | |
2665 | * - Scrub copy | |
2666 | * Contains data from commit tree | |
2667 | * | |
2668 | * If target block group is not marked RO, nocow writes can | |
2669 | * be overwritten by scrub copy, causing data corruption. | |
2670 | * So for dev-replace, it's not allowed to continue if a block | |
2671 | * group is not RO. | |
b12de528 | 2672 | */ |
1bbb97b8 | 2673 | ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace); |
de17addc NA |
2674 | if (!ret && sctx->is_dev_replace) { |
2675 | ret = finish_extent_writes_for_zoned(root, cache); | |
2676 | if (ret) { | |
2677 | btrfs_dec_block_group_ro(cache); | |
2678 | scrub_pause_off(fs_info); | |
2679 | btrfs_put_block_group(cache); | |
2680 | break; | |
2681 | } | |
2682 | } | |
2683 | ||
76a8efa1 Z |
2684 | if (ret == 0) { |
2685 | ro_set = 1; | |
1bbb97b8 | 2686 | } else if (ret == -ENOSPC && !sctx->is_dev_replace) { |
76a8efa1 Z |
2687 | /* |
2688 | * btrfs_inc_block_group_ro return -ENOSPC when it | |
2689 | * failed in creating new chunk for metadata. | |
1bbb97b8 | 2690 | * It is not a problem for scrub, because |
76a8efa1 Z |
2691 | * metadata are always cowed, and our scrub paused |
2692 | * commit_transactions. | |
2693 | */ | |
2694 | ro_set = 0; | |
195a49ea FM |
2695 | } else if (ret == -ETXTBSY) { |
2696 | btrfs_warn(fs_info, | |
2697 | "skipping scrub of block group %llu due to active swapfile", | |
2698 | cache->start); | |
2699 | scrub_pause_off(fs_info); | |
2700 | ret = 0; | |
2701 | goto skip_unfreeze; | |
76a8efa1 | 2702 | } else { |
5d163e0e | 2703 | btrfs_warn(fs_info, |
913e1535 | 2704 | "failed setting block group ro: %d", ret); |
6b7304af | 2705 | btrfs_unfreeze_block_group(cache); |
55e3a601 | 2706 | btrfs_put_block_group(cache); |
1bbb97b8 | 2707 | scrub_pause_off(fs_info); |
55e3a601 Z |
2708 | break; |
2709 | } | |
2710 | ||
1bbb97b8 QW |
2711 | /* |
2712 | * Now the target block is marked RO, wait for nocow writes to | |
2713 | * finish before dev-replace. | |
2714 | * COW is fine, as COW never overwrites extents in commit tree. | |
2715 | */ | |
2716 | if (sctx->is_dev_replace) { | |
2717 | btrfs_wait_nocow_writers(cache); | |
2718 | btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start, | |
2719 | cache->length); | |
2720 | } | |
2721 | ||
2722 | scrub_pause_off(fs_info); | |
3ec17a67 | 2723 | down_write(&dev_replace->rwsem); |
d04fbe19 | 2724 | dev_replace->cursor_right = found_key.offset + dev_extent_len; |
ff023aac SB |
2725 | dev_replace->cursor_left = found_key.offset; |
2726 | dev_replace->item_needs_writeback = 1; | |
cb5583dd DS |
2727 | up_write(&dev_replace->rwsem); |
2728 | ||
d04fbe19 QW |
2729 | ret = scrub_chunk(sctx, cache, scrub_dev, found_key.offset, |
2730 | dev_extent_len); | |
ff023aac SB |
2731 | |
2732 | /* | |
2733 | * flush, submit all pending read and write bios, afterwards | |
2734 | * wait for them. | |
2735 | * Note that in the dev replace case, a read request causes | |
2736 | * write requests that are submitted in the read completion | |
2737 | * worker. Therefore in the current situation, it is required | |
2738 | * that all write requests are flushed, so that all read and | |
2739 | * write requests are really completed when bios_in_flight | |
2740 | * changes to 0. | |
2741 | */ | |
ff023aac | 2742 | scrub_submit(sctx); |
ff023aac SB |
2743 | |
2744 | wait_event(sctx->list_wait, | |
2745 | atomic_read(&sctx->bios_in_flight) == 0); | |
b708ce96 Z |
2746 | |
2747 | scrub_pause_on(fs_info); | |
12cf9372 WS |
2748 | |
2749 | /* | |
2750 | * must be called before we decrease @scrub_paused. | |
2751 | * make sure we don't block transaction commit while | |
2752 | * we are waiting pending workers finished. | |
2753 | */ | |
ff023aac SB |
2754 | wait_event(sctx->list_wait, |
2755 | atomic_read(&sctx->workers_pending) == 0); | |
12cf9372 | 2756 | |
b708ce96 | 2757 | scrub_pause_off(fs_info); |
ff023aac | 2758 | |
78ce9fc2 NA |
2759 | if (sctx->is_dev_replace && |
2760 | !btrfs_finish_block_group_to_copy(dev_replace->srcdev, | |
2761 | cache, found_key.offset)) | |
2762 | ro_set = 0; | |
2763 | ||
3ec17a67 | 2764 | down_write(&dev_replace->rwsem); |
1a1a8b73 FM |
2765 | dev_replace->cursor_left = dev_replace->cursor_right; |
2766 | dev_replace->item_needs_writeback = 1; | |
3ec17a67 | 2767 | up_write(&dev_replace->rwsem); |
1a1a8b73 | 2768 | |
76a8efa1 | 2769 | if (ro_set) |
2ff7e61e | 2770 | btrfs_dec_block_group_ro(cache); |
ff023aac | 2771 | |
758f2dfc FM |
2772 | /* |
2773 | * We might have prevented the cleaner kthread from deleting | |
2774 | * this block group if it was already unused because we raced | |
2775 | * and set it to RO mode first. So add it back to the unused | |
2776 | * list, otherwise it might not ever be deleted unless a manual | |
2777 | * balance is triggered or it becomes used and unused again. | |
2778 | */ | |
2779 | spin_lock(&cache->lock); | |
3349b57f JB |
2780 | if (!test_bit(BLOCK_GROUP_FLAG_REMOVED, &cache->runtime_flags) && |
2781 | !cache->ro && cache->reserved == 0 && cache->used == 0) { | |
758f2dfc | 2782 | spin_unlock(&cache->lock); |
6e80d4f8 DZ |
2783 | if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) |
2784 | btrfs_discard_queue_work(&fs_info->discard_ctl, | |
2785 | cache); | |
2786 | else | |
2787 | btrfs_mark_bg_unused(cache); | |
758f2dfc FM |
2788 | } else { |
2789 | spin_unlock(&cache->lock); | |
2790 | } | |
195a49ea | 2791 | skip_unfreeze: |
6b7304af | 2792 | btrfs_unfreeze_block_group(cache); |
a2de733c AJ |
2793 | btrfs_put_block_group(cache); |
2794 | if (ret) | |
2795 | break; | |
32934280 | 2796 | if (sctx->is_dev_replace && |
af1be4f8 | 2797 | atomic64_read(&dev_replace->num_write_errors) > 0) { |
ff023aac SB |
2798 | ret = -EIO; |
2799 | break; | |
2800 | } | |
2801 | if (sctx->stat.malloc_errors > 0) { | |
2802 | ret = -ENOMEM; | |
2803 | break; | |
2804 | } | |
ced96edc | 2805 | skip: |
d04fbe19 | 2806 | key.offset = found_key.offset + dev_extent_len; |
71267333 | 2807 | btrfs_release_path(path); |
a2de733c AJ |
2808 | } |
2809 | ||
a2de733c | 2810 | btrfs_free_path(path); |
8c51032f | 2811 | |
55e3a601 | 2812 | return ret; |
a2de733c AJ |
2813 | } |
2814 | ||
2a2dc22f QW |
2815 | static int scrub_one_super(struct scrub_ctx *sctx, struct btrfs_device *dev, |
2816 | struct page *page, u64 physical, u64 generation) | |
2817 | { | |
2818 | struct btrfs_fs_info *fs_info = sctx->fs_info; | |
2819 | struct bio_vec bvec; | |
2820 | struct bio bio; | |
2821 | struct btrfs_super_block *sb = page_address(page); | |
2822 | int ret; | |
2823 | ||
2824 | bio_init(&bio, dev->bdev, &bvec, 1, REQ_OP_READ); | |
2825 | bio.bi_iter.bi_sector = physical >> SECTOR_SHIFT; | |
2826 | __bio_add_page(&bio, page, BTRFS_SUPER_INFO_SIZE, 0); | |
2827 | ret = submit_bio_wait(&bio); | |
2828 | bio_uninit(&bio); | |
2829 | ||
2830 | if (ret < 0) | |
2831 | return ret; | |
2832 | ret = btrfs_check_super_csum(fs_info, sb); | |
2833 | if (ret != 0) { | |
2834 | btrfs_err_rl(fs_info, | |
2835 | "super block at physical %llu devid %llu has bad csum", | |
2836 | physical, dev->devid); | |
2837 | return -EIO; | |
2838 | } | |
2839 | if (btrfs_super_generation(sb) != generation) { | |
2840 | btrfs_err_rl(fs_info, | |
2841 | "super block at physical %llu devid %llu has bad generation %llu expect %llu", | |
2842 | physical, dev->devid, | |
2843 | btrfs_super_generation(sb), generation); | |
2844 | return -EUCLEAN; | |
2845 | } | |
2846 | ||
2847 | return btrfs_validate_super(fs_info, sb, -1); | |
2848 | } | |
2849 | ||
a36cf8b8 SB |
2850 | static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx, |
2851 | struct btrfs_device *scrub_dev) | |
a2de733c AJ |
2852 | { |
2853 | int i; | |
2854 | u64 bytenr; | |
2855 | u64 gen; | |
2a2dc22f QW |
2856 | int ret = 0; |
2857 | struct page *page; | |
0b246afa | 2858 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
a2de733c | 2859 | |
84961539 | 2860 | if (BTRFS_FS_ERROR(fs_info)) |
fbabd4a3 | 2861 | return -EROFS; |
79787eaa | 2862 | |
2a2dc22f QW |
2863 | page = alloc_page(GFP_KERNEL); |
2864 | if (!page) { | |
2865 | spin_lock(&sctx->stat_lock); | |
2866 | sctx->stat.malloc_errors++; | |
2867 | spin_unlock(&sctx->stat_lock); | |
2868 | return -ENOMEM; | |
2869 | } | |
2870 | ||
5f546063 | 2871 | /* Seed devices of a new filesystem has their own generation. */ |
0b246afa | 2872 | if (scrub_dev->fs_devices != fs_info->fs_devices) |
5f546063 MX |
2873 | gen = scrub_dev->generation; |
2874 | else | |
0b246afa | 2875 | gen = fs_info->last_trans_committed; |
a2de733c AJ |
2876 | |
2877 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { | |
2878 | bytenr = btrfs_sb_offset(i); | |
935e5cc9 MX |
2879 | if (bytenr + BTRFS_SUPER_INFO_SIZE > |
2880 | scrub_dev->commit_total_bytes) | |
a2de733c | 2881 | break; |
12659251 NA |
2882 | if (!btrfs_check_super_location(scrub_dev, bytenr)) |
2883 | continue; | |
a2de733c | 2884 | |
2a2dc22f QW |
2885 | ret = scrub_one_super(sctx, scrub_dev, page, bytenr, gen); |
2886 | if (ret) { | |
2887 | spin_lock(&sctx->stat_lock); | |
2888 | sctx->stat.super_errors++; | |
2889 | spin_unlock(&sctx->stat_lock); | |
2890 | } | |
a2de733c | 2891 | } |
2a2dc22f | 2892 | __free_page(page); |
a2de733c AJ |
2893 | return 0; |
2894 | } | |
2895 | ||
e89c4a9c JB |
2896 | static void scrub_workers_put(struct btrfs_fs_info *fs_info) |
2897 | { | |
2898 | if (refcount_dec_and_mutex_lock(&fs_info->scrub_workers_refcnt, | |
2899 | &fs_info->scrub_lock)) { | |
be539518 CH |
2900 | struct workqueue_struct *scrub_workers = fs_info->scrub_workers; |
2901 | struct workqueue_struct *scrub_wr_comp = | |
2902 | fs_info->scrub_wr_completion_workers; | |
e89c4a9c JB |
2903 | |
2904 | fs_info->scrub_workers = NULL; | |
2905 | fs_info->scrub_wr_completion_workers = NULL; | |
e89c4a9c JB |
2906 | mutex_unlock(&fs_info->scrub_lock); |
2907 | ||
be539518 CH |
2908 | if (scrub_workers) |
2909 | destroy_workqueue(scrub_workers); | |
2910 | if (scrub_wr_comp) | |
2911 | destroy_workqueue(scrub_wr_comp); | |
e89c4a9c JB |
2912 | } |
2913 | } | |
2914 | ||
a2de733c AJ |
2915 | /* |
2916 | * get a reference count on fs_info->scrub_workers. start worker if necessary | |
2917 | */ | |
ff023aac SB |
2918 | static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info, |
2919 | int is_dev_replace) | |
a2de733c | 2920 | { |
be539518 CH |
2921 | struct workqueue_struct *scrub_workers = NULL; |
2922 | struct workqueue_struct *scrub_wr_comp = NULL; | |
6f011058 | 2923 | unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND; |
0339ef2f | 2924 | int max_active = fs_info->thread_pool_size; |
e89c4a9c | 2925 | int ret = -ENOMEM; |
a2de733c | 2926 | |
e89c4a9c JB |
2927 | if (refcount_inc_not_zero(&fs_info->scrub_workers_refcnt)) |
2928 | return 0; | |
eb4318e5 | 2929 | |
be539518 CH |
2930 | scrub_workers = alloc_workqueue("btrfs-scrub", flags, |
2931 | is_dev_replace ? 1 : max_active); | |
e89c4a9c JB |
2932 | if (!scrub_workers) |
2933 | goto fail_scrub_workers; | |
e82afc52 | 2934 | |
be539518 | 2935 | scrub_wr_comp = alloc_workqueue("btrfs-scrubwrc", flags, max_active); |
e89c4a9c JB |
2936 | if (!scrub_wr_comp) |
2937 | goto fail_scrub_wr_completion_workers; | |
ff09c4ca | 2938 | |
e89c4a9c JB |
2939 | mutex_lock(&fs_info->scrub_lock); |
2940 | if (refcount_read(&fs_info->scrub_workers_refcnt) == 0) { | |
2941 | ASSERT(fs_info->scrub_workers == NULL && | |
5dc96f8d | 2942 | fs_info->scrub_wr_completion_workers == NULL); |
e89c4a9c JB |
2943 | fs_info->scrub_workers = scrub_workers; |
2944 | fs_info->scrub_wr_completion_workers = scrub_wr_comp; | |
ff09c4ca | 2945 | refcount_set(&fs_info->scrub_workers_refcnt, 1); |
e89c4a9c JB |
2946 | mutex_unlock(&fs_info->scrub_lock); |
2947 | return 0; | |
632dd772 | 2948 | } |
e89c4a9c JB |
2949 | /* Other thread raced in and created the workers for us */ |
2950 | refcount_inc(&fs_info->scrub_workers_refcnt); | |
2951 | mutex_unlock(&fs_info->scrub_lock); | |
e82afc52 | 2952 | |
e89c4a9c | 2953 | ret = 0; |
5dc96f8d | 2954 | |
be539518 | 2955 | destroy_workqueue(scrub_wr_comp); |
e82afc52 | 2956 | fail_scrub_wr_completion_workers: |
be539518 | 2957 | destroy_workqueue(scrub_workers); |
e82afc52 | 2958 | fail_scrub_workers: |
e89c4a9c | 2959 | return ret; |
a2de733c AJ |
2960 | } |
2961 | ||
aa1b8cd4 SB |
2962 | int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, |
2963 | u64 end, struct btrfs_scrub_progress *progress, | |
63a212ab | 2964 | int readonly, int is_dev_replace) |
a2de733c | 2965 | { |
562d7b15 | 2966 | struct btrfs_dev_lookup_args args = { .devid = devid }; |
d9d181c1 | 2967 | struct scrub_ctx *sctx; |
a2de733c AJ |
2968 | int ret; |
2969 | struct btrfs_device *dev; | |
a5fb1142 | 2970 | unsigned int nofs_flag; |
f9eab5f0 | 2971 | bool need_commit = false; |
a2de733c | 2972 | |
aa1b8cd4 | 2973 | if (btrfs_fs_closing(fs_info)) |
6c3abeda | 2974 | return -EAGAIN; |
a2de733c | 2975 | |
fc65bb53 QW |
2976 | /* At mount time we have ensured nodesize is in the range of [4K, 64K]. */ |
2977 | ASSERT(fs_info->nodesize <= BTRFS_STRIPE_LEN); | |
b5d67f64 | 2978 | |
fc65bb53 QW |
2979 | /* |
2980 | * SCRUB_MAX_SECTORS_PER_BLOCK is calculated using the largest possible | |
2981 | * value (max nodesize / min sectorsize), thus nodesize should always | |
2982 | * be fine. | |
2983 | */ | |
2984 | ASSERT(fs_info->nodesize <= | |
2985 | SCRUB_MAX_SECTORS_PER_BLOCK << fs_info->sectorsize_bits); | |
7a9e9987 | 2986 | |
0e94c4f4 DS |
2987 | /* Allocate outside of device_list_mutex */ |
2988 | sctx = scrub_setup_ctx(fs_info, is_dev_replace); | |
2989 | if (IS_ERR(sctx)) | |
2990 | return PTR_ERR(sctx); | |
a2de733c | 2991 | |
e89c4a9c JB |
2992 | ret = scrub_workers_get(fs_info, is_dev_replace); |
2993 | if (ret) | |
2994 | goto out_free_ctx; | |
2995 | ||
aa1b8cd4 | 2996 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
562d7b15 | 2997 | dev = btrfs_find_device(fs_info->fs_devices, &args); |
e6e674bd AJ |
2998 | if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) && |
2999 | !is_dev_replace)) { | |
aa1b8cd4 | 3000 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
0e94c4f4 | 3001 | ret = -ENODEV; |
e89c4a9c | 3002 | goto out; |
a2de733c | 3003 | } |
a2de733c | 3004 | |
ebbede42 AJ |
3005 | if (!is_dev_replace && !readonly && |
3006 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) { | |
5d68da3b | 3007 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
a4852cf2 DS |
3008 | btrfs_err_in_rcu(fs_info, |
3009 | "scrub on devid %llu: filesystem on %s is not writable", | |
cb3e217b | 3010 | devid, btrfs_dev_name(dev)); |
0e94c4f4 | 3011 | ret = -EROFS; |
e89c4a9c | 3012 | goto out; |
5d68da3b MX |
3013 | } |
3014 | ||
3b7a016f | 3015 | mutex_lock(&fs_info->scrub_lock); |
e12c9621 | 3016 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || |
401e29c1 | 3017 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) { |
a2de733c | 3018 | mutex_unlock(&fs_info->scrub_lock); |
aa1b8cd4 | 3019 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
0e94c4f4 | 3020 | ret = -EIO; |
e89c4a9c | 3021 | goto out; |
a2de733c AJ |
3022 | } |
3023 | ||
cb5583dd | 3024 | down_read(&fs_info->dev_replace.rwsem); |
cadbc0a0 | 3025 | if (dev->scrub_ctx || |
8dabb742 SB |
3026 | (!is_dev_replace && |
3027 | btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) { | |
cb5583dd | 3028 | up_read(&fs_info->dev_replace.rwsem); |
a2de733c | 3029 | mutex_unlock(&fs_info->scrub_lock); |
aa1b8cd4 | 3030 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
0e94c4f4 | 3031 | ret = -EINPROGRESS; |
e89c4a9c | 3032 | goto out; |
a2de733c | 3033 | } |
cb5583dd | 3034 | up_read(&fs_info->dev_replace.rwsem); |
3b7a016f | 3035 | |
d9d181c1 | 3036 | sctx->readonly = readonly; |
cadbc0a0 | 3037 | dev->scrub_ctx = sctx; |
3cb0929a | 3038 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
a2de733c | 3039 | |
3cb0929a WS |
3040 | /* |
3041 | * checking @scrub_pause_req here, we can avoid | |
3042 | * race between committing transaction and scrubbing. | |
3043 | */ | |
cb7ab021 | 3044 | __scrub_blocked_if_needed(fs_info); |
a2de733c AJ |
3045 | atomic_inc(&fs_info->scrubs_running); |
3046 | mutex_unlock(&fs_info->scrub_lock); | |
a2de733c | 3047 | |
a5fb1142 FM |
3048 | /* |
3049 | * In order to avoid deadlock with reclaim when there is a transaction | |
3050 | * trying to pause scrub, make sure we use GFP_NOFS for all the | |
46343501 | 3051 | * allocations done at btrfs_scrub_sectors() and scrub_sectors_for_parity() |
a5fb1142 FM |
3052 | * invoked by our callees. The pausing request is done when the |
3053 | * transaction commit starts, and it blocks the transaction until scrub | |
3054 | * is paused (done at specific points at scrub_stripe() or right above | |
3055 | * before incrementing fs_info->scrubs_running). | |
3056 | */ | |
3057 | nofs_flag = memalloc_nofs_save(); | |
ff023aac | 3058 | if (!is_dev_replace) { |
f9eab5f0 QW |
3059 | u64 old_super_errors; |
3060 | ||
3061 | spin_lock(&sctx->stat_lock); | |
3062 | old_super_errors = sctx->stat.super_errors; | |
3063 | spin_unlock(&sctx->stat_lock); | |
3064 | ||
d1e14420 | 3065 | btrfs_info(fs_info, "scrub: started on devid %llu", devid); |
9b011adf WS |
3066 | /* |
3067 | * by holding device list mutex, we can | |
3068 | * kick off writing super in log tree sync. | |
3069 | */ | |
3cb0929a | 3070 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
ff023aac | 3071 | ret = scrub_supers(sctx, dev); |
3cb0929a | 3072 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
f9eab5f0 QW |
3073 | |
3074 | spin_lock(&sctx->stat_lock); | |
3075 | /* | |
3076 | * Super block errors found, but we can not commit transaction | |
3077 | * at current context, since btrfs_commit_transaction() needs | |
3078 | * to pause the current running scrub (hold by ourselves). | |
3079 | */ | |
3080 | if (sctx->stat.super_errors > old_super_errors && !sctx->readonly) | |
3081 | need_commit = true; | |
3082 | spin_unlock(&sctx->stat_lock); | |
ff023aac | 3083 | } |
a2de733c AJ |
3084 | |
3085 | if (!ret) | |
32934280 | 3086 | ret = scrub_enumerate_chunks(sctx, dev, start, end); |
a5fb1142 | 3087 | memalloc_nofs_restore(nofs_flag); |
a2de733c | 3088 | |
b6bfebc1 | 3089 | wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0); |
a2de733c AJ |
3090 | atomic_dec(&fs_info->scrubs_running); |
3091 | wake_up(&fs_info->scrub_pause_wait); | |
3092 | ||
b6bfebc1 | 3093 | wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0); |
0ef8e451 | 3094 | |
a2de733c | 3095 | if (progress) |
d9d181c1 | 3096 | memcpy(progress, &sctx->stat, sizeof(*progress)); |
a2de733c | 3097 | |
d1e14420 AJ |
3098 | if (!is_dev_replace) |
3099 | btrfs_info(fs_info, "scrub: %s on devid %llu with status: %d", | |
3100 | ret ? "not finished" : "finished", devid, ret); | |
3101 | ||
a2de733c | 3102 | mutex_lock(&fs_info->scrub_lock); |
cadbc0a0 | 3103 | dev->scrub_ctx = NULL; |
a2de733c AJ |
3104 | mutex_unlock(&fs_info->scrub_lock); |
3105 | ||
e89c4a9c | 3106 | scrub_workers_put(fs_info); |
f55985f4 | 3107 | scrub_put_ctx(sctx); |
a2de733c | 3108 | |
f9eab5f0 QW |
3109 | /* |
3110 | * We found some super block errors before, now try to force a | |
3111 | * transaction commit, as scrub has finished. | |
3112 | */ | |
3113 | if (need_commit) { | |
3114 | struct btrfs_trans_handle *trans; | |
3115 | ||
3116 | trans = btrfs_start_transaction(fs_info->tree_root, 0); | |
3117 | if (IS_ERR(trans)) { | |
3118 | ret = PTR_ERR(trans); | |
3119 | btrfs_err(fs_info, | |
3120 | "scrub: failed to start transaction to fix super block errors: %d", ret); | |
3121 | return ret; | |
3122 | } | |
3123 | ret = btrfs_commit_transaction(trans); | |
3124 | if (ret < 0) | |
3125 | btrfs_err(fs_info, | |
3126 | "scrub: failed to commit transaction to fix super block errors: %d", ret); | |
3127 | } | |
0e94c4f4 | 3128 | return ret; |
e89c4a9c JB |
3129 | out: |
3130 | scrub_workers_put(fs_info); | |
0e94c4f4 DS |
3131 | out_free_ctx: |
3132 | scrub_free_ctx(sctx); | |
3133 | ||
a2de733c AJ |
3134 | return ret; |
3135 | } | |
3136 | ||
2ff7e61e | 3137 | void btrfs_scrub_pause(struct btrfs_fs_info *fs_info) |
a2de733c | 3138 | { |
a2de733c AJ |
3139 | mutex_lock(&fs_info->scrub_lock); |
3140 | atomic_inc(&fs_info->scrub_pause_req); | |
3141 | while (atomic_read(&fs_info->scrubs_paused) != | |
3142 | atomic_read(&fs_info->scrubs_running)) { | |
3143 | mutex_unlock(&fs_info->scrub_lock); | |
3144 | wait_event(fs_info->scrub_pause_wait, | |
3145 | atomic_read(&fs_info->scrubs_paused) == | |
3146 | atomic_read(&fs_info->scrubs_running)); | |
3147 | mutex_lock(&fs_info->scrub_lock); | |
3148 | } | |
3149 | mutex_unlock(&fs_info->scrub_lock); | |
a2de733c AJ |
3150 | } |
3151 | ||
2ff7e61e | 3152 | void btrfs_scrub_continue(struct btrfs_fs_info *fs_info) |
a2de733c | 3153 | { |
a2de733c AJ |
3154 | atomic_dec(&fs_info->scrub_pause_req); |
3155 | wake_up(&fs_info->scrub_pause_wait); | |
a2de733c AJ |
3156 | } |
3157 | ||
aa1b8cd4 | 3158 | int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info) |
a2de733c | 3159 | { |
a2de733c AJ |
3160 | mutex_lock(&fs_info->scrub_lock); |
3161 | if (!atomic_read(&fs_info->scrubs_running)) { | |
3162 | mutex_unlock(&fs_info->scrub_lock); | |
3163 | return -ENOTCONN; | |
3164 | } | |
3165 | ||
3166 | atomic_inc(&fs_info->scrub_cancel_req); | |
3167 | while (atomic_read(&fs_info->scrubs_running)) { | |
3168 | mutex_unlock(&fs_info->scrub_lock); | |
3169 | wait_event(fs_info->scrub_pause_wait, | |
3170 | atomic_read(&fs_info->scrubs_running) == 0); | |
3171 | mutex_lock(&fs_info->scrub_lock); | |
3172 | } | |
3173 | atomic_dec(&fs_info->scrub_cancel_req); | |
3174 | mutex_unlock(&fs_info->scrub_lock); | |
3175 | ||
3176 | return 0; | |
3177 | } | |
3178 | ||
163e97ee | 3179 | int btrfs_scrub_cancel_dev(struct btrfs_device *dev) |
49b25e05 | 3180 | { |
163e97ee | 3181 | struct btrfs_fs_info *fs_info = dev->fs_info; |
d9d181c1 | 3182 | struct scrub_ctx *sctx; |
a2de733c AJ |
3183 | |
3184 | mutex_lock(&fs_info->scrub_lock); | |
cadbc0a0 | 3185 | sctx = dev->scrub_ctx; |
d9d181c1 | 3186 | if (!sctx) { |
a2de733c AJ |
3187 | mutex_unlock(&fs_info->scrub_lock); |
3188 | return -ENOTCONN; | |
3189 | } | |
d9d181c1 | 3190 | atomic_inc(&sctx->cancel_req); |
cadbc0a0 | 3191 | while (dev->scrub_ctx) { |
a2de733c AJ |
3192 | mutex_unlock(&fs_info->scrub_lock); |
3193 | wait_event(fs_info->scrub_pause_wait, | |
cadbc0a0 | 3194 | dev->scrub_ctx == NULL); |
a2de733c AJ |
3195 | mutex_lock(&fs_info->scrub_lock); |
3196 | } | |
3197 | mutex_unlock(&fs_info->scrub_lock); | |
3198 | ||
3199 | return 0; | |
3200 | } | |
1623edeb | 3201 | |
2ff7e61e | 3202 | int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid, |
a2de733c AJ |
3203 | struct btrfs_scrub_progress *progress) |
3204 | { | |
562d7b15 | 3205 | struct btrfs_dev_lookup_args args = { .devid = devid }; |
a2de733c | 3206 | struct btrfs_device *dev; |
d9d181c1 | 3207 | struct scrub_ctx *sctx = NULL; |
a2de733c | 3208 | |
0b246afa | 3209 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
562d7b15 | 3210 | dev = btrfs_find_device(fs_info->fs_devices, &args); |
a2de733c | 3211 | if (dev) |
cadbc0a0 | 3212 | sctx = dev->scrub_ctx; |
d9d181c1 SB |
3213 | if (sctx) |
3214 | memcpy(progress, &sctx->stat, sizeof(*progress)); | |
0b246afa | 3215 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
a2de733c | 3216 | |
d9d181c1 | 3217 | return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV; |
a2de733c | 3218 | } |