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