Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
a2de733c | 2 | /* |
b6bfebc1 | 3 | * Copyright (C) 2011, 2012 STRATO. All rights reserved. |
a2de733c AJ |
4 | */ |
5 | ||
a2de733c | 6 | #include <linux/blkdev.h> |
558540c1 | 7 | #include <linux/ratelimit.h> |
de2491fd | 8 | #include <linux/sched/mm.h> |
d5178578 | 9 | #include <crypto/hash.h> |
a2de733c | 10 | #include "ctree.h" |
6e80d4f8 | 11 | #include "discard.h" |
a2de733c AJ |
12 | #include "volumes.h" |
13 | #include "disk-io.h" | |
14 | #include "ordered-data.h" | |
0ef8e451 | 15 | #include "transaction.h" |
558540c1 | 16 | #include "backref.h" |
5da6fcbc | 17 | #include "extent_io.h" |
ff023aac | 18 | #include "dev-replace.h" |
21adbd5c | 19 | #include "check-integrity.h" |
606686ee | 20 | #include "rcu-string.h" |
53b381b3 | 21 | #include "raid56.h" |
aac0023c | 22 | #include "block-group.h" |
12659251 | 23 | #include "zoned.h" |
a2de733c AJ |
24 | |
25 | /* | |
26 | * This is only the first step towards a full-features scrub. It reads all | |
27 | * extent and super block and verifies the checksums. In case a bad checksum | |
28 | * is found or the extent cannot be read, good data will be written back if | |
29 | * any can be found. | |
30 | * | |
31 | * Future enhancements: | |
a2de733c AJ |
32 | * - In case an unrepairable extent is encountered, track which files are |
33 | * affected and report them | |
a2de733c | 34 | * - track and record media errors, throw out bad devices |
a2de733c | 35 | * - add a mode to also read unallocated space |
a2de733c AJ |
36 | */ |
37 | ||
b5d67f64 | 38 | struct scrub_block; |
d9d181c1 | 39 | struct scrub_ctx; |
a2de733c | 40 | |
ff023aac | 41 | /* |
c9d328c0 QW |
42 | * The following three values only influence the performance. |
43 | * | |
ff023aac | 44 | * The last one configures the number of parallel and outstanding I/O |
c9d328c0 | 45 | * operations. The first one configures an upper limit for the number |
ff023aac SB |
46 | * of (dynamically allocated) pages that are added to a bio. |
47 | */ | |
c9d328c0 QW |
48 | #define SCRUB_PAGES_PER_BIO 32 /* 128KiB per bio for x86 */ |
49 | #define SCRUB_BIOS_PER_SCTX 64 /* 8MiB per device in flight for x86 */ | |
7a9e9987 SB |
50 | |
51 | /* | |
0bb3acdc | 52 | * The following value times PAGE_SIZE needs to be large enough to match the |
7a9e9987 | 53 | * largest node/leaf/sector size that shall be supported. |
7a9e9987 | 54 | */ |
0bb3acdc | 55 | #define SCRUB_MAX_PAGES_PER_BLOCK (BTRFS_MAX_METADATA_BLOCKSIZE / SZ_4K) |
a2de733c | 56 | |
af8e2d1d | 57 | struct scrub_recover { |
6f615018 | 58 | refcount_t refs; |
4c664611 | 59 | struct btrfs_io_context *bioc; |
af8e2d1d MX |
60 | u64 map_length; |
61 | }; | |
62 | ||
a2de733c | 63 | struct scrub_page { |
b5d67f64 SB |
64 | struct scrub_block *sblock; |
65 | struct page *page; | |
442a4f63 | 66 | struct btrfs_device *dev; |
5a6ac9ea | 67 | struct list_head list; |
a2de733c AJ |
68 | u64 flags; /* extent flags */ |
69 | u64 generation; | |
b5d67f64 SB |
70 | u64 logical; |
71 | u64 physical; | |
ff023aac | 72 | u64 physical_for_dev_replace; |
57019345 | 73 | atomic_t refs; |
2c363954 | 74 | u8 mirror_num; |
d08e38b6 CIK |
75 | unsigned int have_csum:1; |
76 | unsigned int io_error:1; | |
a2de733c | 77 | u8 csum[BTRFS_CSUM_SIZE]; |
af8e2d1d MX |
78 | |
79 | struct scrub_recover *recover; | |
a2de733c AJ |
80 | }; |
81 | ||
82 | struct scrub_bio { | |
83 | int index; | |
d9d181c1 | 84 | struct scrub_ctx *sctx; |
a36cf8b8 | 85 | struct btrfs_device *dev; |
a2de733c | 86 | struct bio *bio; |
4e4cbee9 | 87 | blk_status_t status; |
a2de733c AJ |
88 | u64 logical; |
89 | u64 physical; | |
c9d328c0 | 90 | struct scrub_page *pagev[SCRUB_PAGES_PER_BIO]; |
b5d67f64 | 91 | int page_count; |
a2de733c AJ |
92 | int next_free; |
93 | struct btrfs_work work; | |
94 | }; | |
95 | ||
b5d67f64 | 96 | struct scrub_block { |
7a9e9987 | 97 | struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK]; |
b5d67f64 SB |
98 | int page_count; |
99 | atomic_t outstanding_pages; | |
186debd6 | 100 | refcount_t refs; /* free mem on transition to zero */ |
d9d181c1 | 101 | struct scrub_ctx *sctx; |
5a6ac9ea | 102 | struct scrub_parity *sparity; |
b5d67f64 SB |
103 | struct { |
104 | unsigned int header_error:1; | |
105 | unsigned int checksum_error:1; | |
106 | unsigned int no_io_error_seen:1; | |
442a4f63 | 107 | unsigned int generation_error:1; /* also sets header_error */ |
5a6ac9ea MX |
108 | |
109 | /* The following is for the data used to check parity */ | |
110 | /* It is for the data with checksum */ | |
111 | unsigned int data_corrected:1; | |
b5d67f64 | 112 | }; |
73ff61db | 113 | struct btrfs_work work; |
b5d67f64 SB |
114 | }; |
115 | ||
5a6ac9ea MX |
116 | /* Used for the chunks with parity stripe such RAID5/6 */ |
117 | struct scrub_parity { | |
118 | struct scrub_ctx *sctx; | |
119 | ||
120 | struct btrfs_device *scrub_dev; | |
121 | ||
122 | u64 logic_start; | |
123 | ||
124 | u64 logic_end; | |
125 | ||
126 | int nsectors; | |
127 | ||
fa485d21 | 128 | u32 stripe_len; |
5a6ac9ea | 129 | |
78a76450 | 130 | refcount_t refs; |
5a6ac9ea MX |
131 | |
132 | struct list_head spages; | |
133 | ||
134 | /* Work of parity check and repair */ | |
135 | struct btrfs_work work; | |
136 | ||
137 | /* Mark the parity blocks which have data */ | |
138 | unsigned long *dbitmap; | |
139 | ||
140 | /* | |
141 | * Mark the parity blocks which have data, but errors happen when | |
142 | * read data or check data | |
143 | */ | |
144 | unsigned long *ebitmap; | |
145 | ||
a8753ee3 | 146 | unsigned long bitmap[]; |
5a6ac9ea MX |
147 | }; |
148 | ||
d9d181c1 | 149 | struct scrub_ctx { |
ff023aac | 150 | struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX]; |
fb456252 | 151 | struct btrfs_fs_info *fs_info; |
a2de733c AJ |
152 | int first_free; |
153 | int curr; | |
b6bfebc1 SB |
154 | atomic_t bios_in_flight; |
155 | atomic_t workers_pending; | |
a2de733c AJ |
156 | spinlock_t list_lock; |
157 | wait_queue_head_t list_wait; | |
a2de733c AJ |
158 | struct list_head csum_list; |
159 | atomic_t cancel_req; | |
8628764e | 160 | int readonly; |
c9d328c0 | 161 | int pages_per_bio; |
63a212ab | 162 | |
eb3b5053 DS |
163 | /* State of IO submission throttling affecting the associated device */ |
164 | ktime_t throttle_deadline; | |
165 | u64 throttle_sent; | |
166 | ||
63a212ab | 167 | int is_dev_replace; |
de17addc | 168 | u64 write_pointer; |
3fb99303 DS |
169 | |
170 | struct scrub_bio *wr_curr_bio; | |
171 | struct mutex wr_lock; | |
3fb99303 | 172 | struct btrfs_device *wr_tgtdev; |
2073c4c2 | 173 | bool flush_all_writes; |
63a212ab | 174 | |
a2de733c AJ |
175 | /* |
176 | * statistics | |
177 | */ | |
178 | struct btrfs_scrub_progress stat; | |
179 | spinlock_t stat_lock; | |
f55985f4 FM |
180 | |
181 | /* | |
182 | * Use a ref counter to avoid use-after-free issues. Scrub workers | |
183 | * decrement bios_in_flight and workers_pending and then do a wakeup | |
184 | * on the list_wait wait queue. We must ensure the main scrub task | |
185 | * doesn't free the scrub context before or while the workers are | |
186 | * doing the wakeup() call. | |
187 | */ | |
99f4cdb1 | 188 | refcount_t refs; |
a2de733c AJ |
189 | }; |
190 | ||
558540c1 JS |
191 | struct scrub_warning { |
192 | struct btrfs_path *path; | |
193 | u64 extent_item_size; | |
558540c1 | 194 | const char *errstr; |
6aa21263 | 195 | u64 physical; |
558540c1 JS |
196 | u64 logical; |
197 | struct btrfs_device *dev; | |
558540c1 JS |
198 | }; |
199 | ||
0966a7b1 QW |
200 | struct full_stripe_lock { |
201 | struct rb_node node; | |
202 | u64 logical; | |
203 | u64 refs; | |
204 | struct mutex mutex; | |
205 | }; | |
206 | ||
be50a8dd | 207 | static int scrub_setup_recheck_block(struct scrub_block *original_sblock, |
ff023aac | 208 | struct scrub_block *sblocks_for_recheck); |
34f5c8e9 | 209 | static void scrub_recheck_block(struct btrfs_fs_info *fs_info, |
affe4a5a ZL |
210 | struct scrub_block *sblock, |
211 | int retry_failed_mirror); | |
ba7cf988 | 212 | static void scrub_recheck_block_checksum(struct scrub_block *sblock); |
b5d67f64 | 213 | static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad, |
114ab50d | 214 | struct scrub_block *sblock_good); |
b5d67f64 SB |
215 | static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad, |
216 | struct scrub_block *sblock_good, | |
217 | int page_num, int force_write); | |
ff023aac SB |
218 | static void scrub_write_block_to_dev_replace(struct scrub_block *sblock); |
219 | static int scrub_write_page_to_dev_replace(struct scrub_block *sblock, | |
220 | int page_num); | |
b5d67f64 SB |
221 | static int scrub_checksum_data(struct scrub_block *sblock); |
222 | static int scrub_checksum_tree_block(struct scrub_block *sblock); | |
223 | static int scrub_checksum_super(struct scrub_block *sblock); | |
b5d67f64 | 224 | static void scrub_block_put(struct scrub_block *sblock); |
7a9e9987 SB |
225 | static void scrub_page_get(struct scrub_page *spage); |
226 | static void scrub_page_put(struct scrub_page *spage); | |
5a6ac9ea MX |
227 | static void scrub_parity_get(struct scrub_parity *sparity); |
228 | static void scrub_parity_put(struct scrub_parity *sparity); | |
fa485d21 | 229 | static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u32 len, |
a36cf8b8 | 230 | u64 physical, struct btrfs_device *dev, u64 flags, |
96e63a45 | 231 | u64 gen, int mirror_num, u8 *csum, |
ff023aac | 232 | u64 physical_for_dev_replace); |
4246a0b6 | 233 | static void scrub_bio_end_io(struct bio *bio); |
b5d67f64 SB |
234 | static void scrub_bio_end_io_worker(struct btrfs_work *work); |
235 | static void scrub_block_complete(struct scrub_block *sblock); | |
ff023aac | 236 | static void scrub_remap_extent(struct btrfs_fs_info *fs_info, |
fa485d21 | 237 | u64 extent_logical, u32 extent_len, |
ff023aac SB |
238 | u64 *extent_physical, |
239 | struct btrfs_device **extent_dev, | |
240 | int *extent_mirror_num); | |
ff023aac SB |
241 | static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx, |
242 | struct scrub_page *spage); | |
243 | static void scrub_wr_submit(struct scrub_ctx *sctx); | |
4246a0b6 | 244 | static void scrub_wr_bio_end_io(struct bio *bio); |
ff023aac | 245 | static void scrub_wr_bio_end_io_worker(struct btrfs_work *work); |
f55985f4 | 246 | static void scrub_put_ctx(struct scrub_ctx *sctx); |
1623edeb | 247 | |
261d2dcb | 248 | static inline int scrub_is_page_on_raid56(struct scrub_page *spage) |
762221f0 | 249 | { |
261d2dcb | 250 | return spage->recover && |
4c664611 | 251 | (spage->recover->bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK); |
762221f0 | 252 | } |
1623edeb | 253 | |
b6bfebc1 SB |
254 | static void scrub_pending_bio_inc(struct scrub_ctx *sctx) |
255 | { | |
99f4cdb1 | 256 | refcount_inc(&sctx->refs); |
b6bfebc1 SB |
257 | atomic_inc(&sctx->bios_in_flight); |
258 | } | |
259 | ||
260 | static void scrub_pending_bio_dec(struct scrub_ctx *sctx) | |
261 | { | |
262 | atomic_dec(&sctx->bios_in_flight); | |
263 | wake_up(&sctx->list_wait); | |
f55985f4 | 264 | scrub_put_ctx(sctx); |
b6bfebc1 SB |
265 | } |
266 | ||
cb7ab021 | 267 | static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info) |
3cb0929a WS |
268 | { |
269 | while (atomic_read(&fs_info->scrub_pause_req)) { | |
270 | mutex_unlock(&fs_info->scrub_lock); | |
271 | wait_event(fs_info->scrub_pause_wait, | |
272 | atomic_read(&fs_info->scrub_pause_req) == 0); | |
273 | mutex_lock(&fs_info->scrub_lock); | |
274 | } | |
275 | } | |
276 | ||
0e22be89 | 277 | static void scrub_pause_on(struct btrfs_fs_info *fs_info) |
cb7ab021 WS |
278 | { |
279 | atomic_inc(&fs_info->scrubs_paused); | |
280 | wake_up(&fs_info->scrub_pause_wait); | |
0e22be89 | 281 | } |
cb7ab021 | 282 | |
0e22be89 Z |
283 | static void scrub_pause_off(struct btrfs_fs_info *fs_info) |
284 | { | |
cb7ab021 WS |
285 | mutex_lock(&fs_info->scrub_lock); |
286 | __scrub_blocked_if_needed(fs_info); | |
287 | atomic_dec(&fs_info->scrubs_paused); | |
288 | mutex_unlock(&fs_info->scrub_lock); | |
289 | ||
290 | wake_up(&fs_info->scrub_pause_wait); | |
291 | } | |
292 | ||
0e22be89 Z |
293 | static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info) |
294 | { | |
295 | scrub_pause_on(fs_info); | |
296 | scrub_pause_off(fs_info); | |
297 | } | |
298 | ||
0966a7b1 QW |
299 | /* |
300 | * Insert new full stripe lock into full stripe locks tree | |
301 | * | |
302 | * Return pointer to existing or newly inserted full_stripe_lock structure if | |
303 | * everything works well. | |
304 | * Return ERR_PTR(-ENOMEM) if we failed to allocate memory | |
305 | * | |
306 | * NOTE: caller must hold full_stripe_locks_root->lock before calling this | |
307 | * function | |
308 | */ | |
309 | static struct full_stripe_lock *insert_full_stripe_lock( | |
310 | struct btrfs_full_stripe_locks_tree *locks_root, | |
311 | u64 fstripe_logical) | |
312 | { | |
313 | struct rb_node **p; | |
314 | struct rb_node *parent = NULL; | |
315 | struct full_stripe_lock *entry; | |
316 | struct full_stripe_lock *ret; | |
317 | ||
a32bf9a3 | 318 | lockdep_assert_held(&locks_root->lock); |
0966a7b1 QW |
319 | |
320 | p = &locks_root->root.rb_node; | |
321 | while (*p) { | |
322 | parent = *p; | |
323 | entry = rb_entry(parent, struct full_stripe_lock, node); | |
324 | if (fstripe_logical < entry->logical) { | |
325 | p = &(*p)->rb_left; | |
326 | } else if (fstripe_logical > entry->logical) { | |
327 | p = &(*p)->rb_right; | |
328 | } else { | |
329 | entry->refs++; | |
330 | return entry; | |
331 | } | |
332 | } | |
333 | ||
a5fb1142 FM |
334 | /* |
335 | * Insert new lock. | |
a5fb1142 | 336 | */ |
0966a7b1 QW |
337 | ret = kmalloc(sizeof(*ret), GFP_KERNEL); |
338 | if (!ret) | |
339 | return ERR_PTR(-ENOMEM); | |
340 | ret->logical = fstripe_logical; | |
341 | ret->refs = 1; | |
342 | mutex_init(&ret->mutex); | |
343 | ||
344 | rb_link_node(&ret->node, parent, p); | |
345 | rb_insert_color(&ret->node, &locks_root->root); | |
346 | return ret; | |
347 | } | |
348 | ||
349 | /* | |
350 | * Search for a full stripe lock of a block group | |
351 | * | |
352 | * Return pointer to existing full stripe lock if found | |
353 | * Return NULL if not found | |
354 | */ | |
355 | static struct full_stripe_lock *search_full_stripe_lock( | |
356 | struct btrfs_full_stripe_locks_tree *locks_root, | |
357 | u64 fstripe_logical) | |
358 | { | |
359 | struct rb_node *node; | |
360 | struct full_stripe_lock *entry; | |
361 | ||
a32bf9a3 | 362 | lockdep_assert_held(&locks_root->lock); |
0966a7b1 QW |
363 | |
364 | node = locks_root->root.rb_node; | |
365 | while (node) { | |
366 | entry = rb_entry(node, struct full_stripe_lock, node); | |
367 | if (fstripe_logical < entry->logical) | |
368 | node = node->rb_left; | |
369 | else if (fstripe_logical > entry->logical) | |
370 | node = node->rb_right; | |
371 | else | |
372 | return entry; | |
373 | } | |
374 | return NULL; | |
375 | } | |
376 | ||
377 | /* | |
378 | * Helper to get full stripe logical from a normal bytenr. | |
379 | * | |
380 | * Caller must ensure @cache is a RAID56 block group. | |
381 | */ | |
32da5386 | 382 | static u64 get_full_stripe_logical(struct btrfs_block_group *cache, u64 bytenr) |
0966a7b1 QW |
383 | { |
384 | u64 ret; | |
385 | ||
386 | /* | |
387 | * Due to chunk item size limit, full stripe length should not be | |
388 | * larger than U32_MAX. Just a sanity check here. | |
389 | */ | |
390 | WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX); | |
391 | ||
392 | /* | |
393 | * round_down() can only handle power of 2, while RAID56 full | |
394 | * stripe length can be 64KiB * n, so we need to manually round down. | |
395 | */ | |
b3470b5d DS |
396 | ret = div64_u64(bytenr - cache->start, cache->full_stripe_len) * |
397 | cache->full_stripe_len + cache->start; | |
0966a7b1 QW |
398 | return ret; |
399 | } | |
400 | ||
401 | /* | |
402 | * Lock a full stripe to avoid concurrency of recovery and read | |
403 | * | |
404 | * It's only used for profiles with parities (RAID5/6), for other profiles it | |
405 | * does nothing. | |
406 | * | |
407 | * Return 0 if we locked full stripe covering @bytenr, with a mutex held. | |
408 | * So caller must call unlock_full_stripe() at the same context. | |
409 | * | |
410 | * Return <0 if encounters error. | |
411 | */ | |
412 | static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr, | |
413 | bool *locked_ret) | |
414 | { | |
32da5386 | 415 | struct btrfs_block_group *bg_cache; |
0966a7b1 QW |
416 | struct btrfs_full_stripe_locks_tree *locks_root; |
417 | struct full_stripe_lock *existing; | |
418 | u64 fstripe_start; | |
419 | int ret = 0; | |
420 | ||
421 | *locked_ret = false; | |
422 | bg_cache = btrfs_lookup_block_group(fs_info, bytenr); | |
423 | if (!bg_cache) { | |
424 | ASSERT(0); | |
425 | return -ENOENT; | |
426 | } | |
427 | ||
428 | /* Profiles not based on parity don't need full stripe lock */ | |
429 | if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK)) | |
430 | goto out; | |
431 | locks_root = &bg_cache->full_stripe_locks_root; | |
432 | ||
433 | fstripe_start = get_full_stripe_logical(bg_cache, bytenr); | |
434 | ||
435 | /* Now insert the full stripe lock */ | |
436 | mutex_lock(&locks_root->lock); | |
437 | existing = insert_full_stripe_lock(locks_root, fstripe_start); | |
438 | mutex_unlock(&locks_root->lock); | |
439 | if (IS_ERR(existing)) { | |
440 | ret = PTR_ERR(existing); | |
441 | goto out; | |
442 | } | |
443 | mutex_lock(&existing->mutex); | |
444 | *locked_ret = true; | |
445 | out: | |
446 | btrfs_put_block_group(bg_cache); | |
447 | return ret; | |
448 | } | |
449 | ||
450 | /* | |
451 | * Unlock a full stripe. | |
452 | * | |
453 | * NOTE: Caller must ensure it's the same context calling corresponding | |
454 | * lock_full_stripe(). | |
455 | * | |
456 | * Return 0 if we unlock full stripe without problem. | |
457 | * Return <0 for error | |
458 | */ | |
459 | static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr, | |
460 | bool locked) | |
461 | { | |
32da5386 | 462 | struct btrfs_block_group *bg_cache; |
0966a7b1 QW |
463 | struct btrfs_full_stripe_locks_tree *locks_root; |
464 | struct full_stripe_lock *fstripe_lock; | |
465 | u64 fstripe_start; | |
466 | bool freeit = false; | |
467 | int ret = 0; | |
468 | ||
469 | /* If we didn't acquire full stripe lock, no need to continue */ | |
470 | if (!locked) | |
471 | return 0; | |
472 | ||
473 | bg_cache = btrfs_lookup_block_group(fs_info, bytenr); | |
474 | if (!bg_cache) { | |
475 | ASSERT(0); | |
476 | return -ENOENT; | |
477 | } | |
478 | if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK)) | |
479 | goto out; | |
480 | ||
481 | locks_root = &bg_cache->full_stripe_locks_root; | |
482 | fstripe_start = get_full_stripe_logical(bg_cache, bytenr); | |
483 | ||
484 | mutex_lock(&locks_root->lock); | |
485 | fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start); | |
486 | /* Unpaired unlock_full_stripe() detected */ | |
487 | if (!fstripe_lock) { | |
488 | WARN_ON(1); | |
489 | ret = -ENOENT; | |
490 | mutex_unlock(&locks_root->lock); | |
491 | goto out; | |
492 | } | |
493 | ||
494 | if (fstripe_lock->refs == 0) { | |
495 | WARN_ON(1); | |
496 | btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow", | |
497 | fstripe_lock->logical); | |
498 | } else { | |
499 | fstripe_lock->refs--; | |
500 | } | |
501 | ||
502 | if (fstripe_lock->refs == 0) { | |
503 | rb_erase(&fstripe_lock->node, &locks_root->root); | |
504 | freeit = true; | |
505 | } | |
506 | mutex_unlock(&locks_root->lock); | |
507 | ||
508 | mutex_unlock(&fstripe_lock->mutex); | |
509 | if (freeit) | |
510 | kfree(fstripe_lock); | |
511 | out: | |
512 | btrfs_put_block_group(bg_cache); | |
513 | return ret; | |
514 | } | |
515 | ||
d9d181c1 | 516 | static void scrub_free_csums(struct scrub_ctx *sctx) |
a2de733c | 517 | { |
d9d181c1 | 518 | while (!list_empty(&sctx->csum_list)) { |
a2de733c | 519 | struct btrfs_ordered_sum *sum; |
d9d181c1 | 520 | sum = list_first_entry(&sctx->csum_list, |
a2de733c AJ |
521 | struct btrfs_ordered_sum, list); |
522 | list_del(&sum->list); | |
523 | kfree(sum); | |
524 | } | |
525 | } | |
526 | ||
d9d181c1 | 527 | static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx) |
a2de733c AJ |
528 | { |
529 | int i; | |
a2de733c | 530 | |
d9d181c1 | 531 | if (!sctx) |
a2de733c AJ |
532 | return; |
533 | ||
b5d67f64 | 534 | /* this can happen when scrub is cancelled */ |
d9d181c1 SB |
535 | if (sctx->curr != -1) { |
536 | struct scrub_bio *sbio = sctx->bios[sctx->curr]; | |
b5d67f64 SB |
537 | |
538 | for (i = 0; i < sbio->page_count; i++) { | |
ff023aac | 539 | WARN_ON(!sbio->pagev[i]->page); |
b5d67f64 SB |
540 | scrub_block_put(sbio->pagev[i]->sblock); |
541 | } | |
542 | bio_put(sbio->bio); | |
543 | } | |
544 | ||
ff023aac | 545 | for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) { |
d9d181c1 | 546 | struct scrub_bio *sbio = sctx->bios[i]; |
a2de733c AJ |
547 | |
548 | if (!sbio) | |
549 | break; | |
a2de733c AJ |
550 | kfree(sbio); |
551 | } | |
552 | ||
3fb99303 | 553 | kfree(sctx->wr_curr_bio); |
d9d181c1 SB |
554 | scrub_free_csums(sctx); |
555 | kfree(sctx); | |
a2de733c AJ |
556 | } |
557 | ||
f55985f4 FM |
558 | static void scrub_put_ctx(struct scrub_ctx *sctx) |
559 | { | |
99f4cdb1 | 560 | if (refcount_dec_and_test(&sctx->refs)) |
f55985f4 FM |
561 | scrub_free_ctx(sctx); |
562 | } | |
563 | ||
92f7ba43 DS |
564 | static noinline_for_stack struct scrub_ctx *scrub_setup_ctx( |
565 | struct btrfs_fs_info *fs_info, int is_dev_replace) | |
a2de733c | 566 | { |
d9d181c1 | 567 | struct scrub_ctx *sctx; |
a2de733c | 568 | int i; |
a2de733c | 569 | |
58c4e173 | 570 | sctx = kzalloc(sizeof(*sctx), GFP_KERNEL); |
d9d181c1 | 571 | if (!sctx) |
a2de733c | 572 | goto nomem; |
99f4cdb1 | 573 | refcount_set(&sctx->refs, 1); |
63a212ab | 574 | sctx->is_dev_replace = is_dev_replace; |
c9d328c0 | 575 | sctx->pages_per_bio = SCRUB_PAGES_PER_BIO; |
d9d181c1 | 576 | sctx->curr = -1; |
92f7ba43 | 577 | sctx->fs_info = fs_info; |
e49be14b | 578 | INIT_LIST_HEAD(&sctx->csum_list); |
ff023aac | 579 | for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) { |
a2de733c AJ |
580 | struct scrub_bio *sbio; |
581 | ||
58c4e173 | 582 | sbio = kzalloc(sizeof(*sbio), GFP_KERNEL); |
a2de733c AJ |
583 | if (!sbio) |
584 | goto nomem; | |
d9d181c1 | 585 | sctx->bios[i] = sbio; |
a2de733c | 586 | |
a2de733c | 587 | sbio->index = i; |
d9d181c1 | 588 | sbio->sctx = sctx; |
b5d67f64 | 589 | sbio->page_count = 0; |
a0cac0ec OS |
590 | btrfs_init_work(&sbio->work, scrub_bio_end_io_worker, NULL, |
591 | NULL); | |
a2de733c | 592 | |
ff023aac | 593 | if (i != SCRUB_BIOS_PER_SCTX - 1) |
d9d181c1 | 594 | sctx->bios[i]->next_free = i + 1; |
0ef8e451 | 595 | else |
d9d181c1 SB |
596 | sctx->bios[i]->next_free = -1; |
597 | } | |
598 | sctx->first_free = 0; | |
b6bfebc1 SB |
599 | atomic_set(&sctx->bios_in_flight, 0); |
600 | atomic_set(&sctx->workers_pending, 0); | |
d9d181c1 | 601 | atomic_set(&sctx->cancel_req, 0); |
d9d181c1 SB |
602 | |
603 | spin_lock_init(&sctx->list_lock); | |
604 | spin_lock_init(&sctx->stat_lock); | |
605 | init_waitqueue_head(&sctx->list_wait); | |
eb3b5053 | 606 | sctx->throttle_deadline = 0; |
ff023aac | 607 | |
3fb99303 DS |
608 | WARN_ON(sctx->wr_curr_bio != NULL); |
609 | mutex_init(&sctx->wr_lock); | |
610 | sctx->wr_curr_bio = NULL; | |
8fcdac3f | 611 | if (is_dev_replace) { |
ded56184 | 612 | WARN_ON(!fs_info->dev_replace.tgtdev); |
ded56184 | 613 | sctx->wr_tgtdev = fs_info->dev_replace.tgtdev; |
2073c4c2 | 614 | sctx->flush_all_writes = false; |
ff023aac | 615 | } |
8fcdac3f | 616 | |
d9d181c1 | 617 | return sctx; |
a2de733c AJ |
618 | |
619 | nomem: | |
d9d181c1 | 620 | scrub_free_ctx(sctx); |
a2de733c AJ |
621 | return ERR_PTR(-ENOMEM); |
622 | } | |
623 | ||
ff023aac SB |
624 | static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, |
625 | void *warn_ctx) | |
558540c1 | 626 | { |
558540c1 JS |
627 | u32 nlink; |
628 | int ret; | |
629 | int i; | |
de2491fd | 630 | unsigned nofs_flag; |
558540c1 JS |
631 | struct extent_buffer *eb; |
632 | struct btrfs_inode_item *inode_item; | |
ff023aac | 633 | struct scrub_warning *swarn = warn_ctx; |
fb456252 | 634 | struct btrfs_fs_info *fs_info = swarn->dev->fs_info; |
558540c1 JS |
635 | struct inode_fs_paths *ipath = NULL; |
636 | struct btrfs_root *local_root; | |
1d4c08e0 | 637 | struct btrfs_key key; |
558540c1 | 638 | |
56e9357a | 639 | local_root = btrfs_get_fs_root(fs_info, root, true); |
558540c1 JS |
640 | if (IS_ERR(local_root)) { |
641 | ret = PTR_ERR(local_root); | |
642 | goto err; | |
643 | } | |
644 | ||
14692cc1 DS |
645 | /* |
646 | * this makes the path point to (inum INODE_ITEM ioff) | |
647 | */ | |
1d4c08e0 DS |
648 | key.objectid = inum; |
649 | key.type = BTRFS_INODE_ITEM_KEY; | |
650 | key.offset = 0; | |
651 | ||
652 | ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0); | |
558540c1 | 653 | if (ret) { |
00246528 | 654 | btrfs_put_root(local_root); |
558540c1 JS |
655 | btrfs_release_path(swarn->path); |
656 | goto err; | |
657 | } | |
658 | ||
659 | eb = swarn->path->nodes[0]; | |
660 | inode_item = btrfs_item_ptr(eb, swarn->path->slots[0], | |
661 | struct btrfs_inode_item); | |
558540c1 JS |
662 | nlink = btrfs_inode_nlink(eb, inode_item); |
663 | btrfs_release_path(swarn->path); | |
664 | ||
de2491fd DS |
665 | /* |
666 | * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub | |
667 | * uses GFP_NOFS in this context, so we keep it consistent but it does | |
668 | * not seem to be strictly necessary. | |
669 | */ | |
670 | nofs_flag = memalloc_nofs_save(); | |
558540c1 | 671 | ipath = init_ipath(4096, local_root, swarn->path); |
de2491fd | 672 | memalloc_nofs_restore(nofs_flag); |
26bdef54 | 673 | if (IS_ERR(ipath)) { |
00246528 | 674 | btrfs_put_root(local_root); |
26bdef54 DC |
675 | ret = PTR_ERR(ipath); |
676 | ipath = NULL; | |
677 | goto err; | |
678 | } | |
558540c1 JS |
679 | ret = paths_from_inode(inum, ipath); |
680 | ||
681 | if (ret < 0) | |
682 | goto err; | |
683 | ||
684 | /* | |
685 | * we deliberately ignore the bit ipath might have been too small to | |
686 | * hold all of the paths here | |
687 | */ | |
688 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) | |
5d163e0e | 689 | btrfs_warn_in_rcu(fs_info, |
8df507cb | 690 | "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %u, links %u (path: %s)", |
5d163e0e JM |
691 | swarn->errstr, swarn->logical, |
692 | rcu_str_deref(swarn->dev->name), | |
6aa21263 | 693 | swarn->physical, |
5d163e0e | 694 | root, inum, offset, |
8df507cb | 695 | fs_info->sectorsize, nlink, |
5d163e0e | 696 | (char *)(unsigned long)ipath->fspath->val[i]); |
558540c1 | 697 | |
00246528 | 698 | btrfs_put_root(local_root); |
558540c1 JS |
699 | free_ipath(ipath); |
700 | return 0; | |
701 | ||
702 | err: | |
5d163e0e | 703 | btrfs_warn_in_rcu(fs_info, |
6aa21263 | 704 | "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d", |
5d163e0e JM |
705 | swarn->errstr, swarn->logical, |
706 | rcu_str_deref(swarn->dev->name), | |
6aa21263 | 707 | swarn->physical, |
5d163e0e | 708 | root, inum, offset, ret); |
558540c1 JS |
709 | |
710 | free_ipath(ipath); | |
711 | return 0; | |
712 | } | |
713 | ||
b5d67f64 | 714 | static void scrub_print_warning(const char *errstr, struct scrub_block *sblock) |
558540c1 | 715 | { |
a36cf8b8 SB |
716 | struct btrfs_device *dev; |
717 | struct btrfs_fs_info *fs_info; | |
558540c1 JS |
718 | struct btrfs_path *path; |
719 | struct btrfs_key found_key; | |
720 | struct extent_buffer *eb; | |
721 | struct btrfs_extent_item *ei; | |
722 | struct scrub_warning swarn; | |
69917e43 LB |
723 | unsigned long ptr = 0; |
724 | u64 extent_item_pos; | |
725 | u64 flags = 0; | |
558540c1 | 726 | u64 ref_root; |
69917e43 | 727 | u32 item_size; |
07c9a8e0 | 728 | u8 ref_level = 0; |
69917e43 | 729 | int ret; |
558540c1 | 730 | |
a36cf8b8 | 731 | WARN_ON(sblock->page_count < 1); |
7a9e9987 | 732 | dev = sblock->pagev[0]->dev; |
fb456252 | 733 | fs_info = sblock->sctx->fs_info; |
a36cf8b8 | 734 | |
558540c1 | 735 | path = btrfs_alloc_path(); |
8b9456da DS |
736 | if (!path) |
737 | return; | |
558540c1 | 738 | |
6aa21263 | 739 | swarn.physical = sblock->pagev[0]->physical; |
7a9e9987 | 740 | swarn.logical = sblock->pagev[0]->logical; |
558540c1 | 741 | swarn.errstr = errstr; |
a36cf8b8 | 742 | swarn.dev = NULL; |
558540c1 | 743 | |
69917e43 LB |
744 | ret = extent_from_logical(fs_info, swarn.logical, path, &found_key, |
745 | &flags); | |
558540c1 JS |
746 | if (ret < 0) |
747 | goto out; | |
748 | ||
4692cf58 | 749 | extent_item_pos = swarn.logical - found_key.objectid; |
558540c1 JS |
750 | swarn.extent_item_size = found_key.offset; |
751 | ||
752 | eb = path->nodes[0]; | |
753 | ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); | |
3212fa14 | 754 | item_size = btrfs_item_size(eb, path->slots[0]); |
558540c1 | 755 | |
69917e43 | 756 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
558540c1 | 757 | do { |
6eda71d0 LB |
758 | ret = tree_backref_for_extent(&ptr, eb, &found_key, ei, |
759 | item_size, &ref_root, | |
760 | &ref_level); | |
ecaeb14b | 761 | btrfs_warn_in_rcu(fs_info, |
6aa21263 | 762 | "%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu", |
5d163e0e | 763 | errstr, swarn.logical, |
606686ee | 764 | rcu_str_deref(dev->name), |
6aa21263 | 765 | swarn.physical, |
558540c1 JS |
766 | ref_level ? "node" : "leaf", |
767 | ret < 0 ? -1 : ref_level, | |
768 | ret < 0 ? -1 : ref_root); | |
769 | } while (ret != 1); | |
d8fe29e9 | 770 | btrfs_release_path(path); |
558540c1 | 771 | } else { |
d8fe29e9 | 772 | btrfs_release_path(path); |
558540c1 | 773 | swarn.path = path; |
a36cf8b8 | 774 | swarn.dev = dev; |
7a3ae2f8 JS |
775 | iterate_extent_inodes(fs_info, found_key.objectid, |
776 | extent_item_pos, 1, | |
c995ab3c | 777 | scrub_print_warning_inode, &swarn, false); |
558540c1 JS |
778 | } |
779 | ||
780 | out: | |
781 | btrfs_free_path(path); | |
558540c1 JS |
782 | } |
783 | ||
af8e2d1d MX |
784 | static inline void scrub_get_recover(struct scrub_recover *recover) |
785 | { | |
6f615018 | 786 | refcount_inc(&recover->refs); |
af8e2d1d MX |
787 | } |
788 | ||
e501bfe3 QW |
789 | static inline void scrub_put_recover(struct btrfs_fs_info *fs_info, |
790 | struct scrub_recover *recover) | |
af8e2d1d | 791 | { |
6f615018 | 792 | if (refcount_dec_and_test(&recover->refs)) { |
e501bfe3 | 793 | btrfs_bio_counter_dec(fs_info); |
4c664611 | 794 | btrfs_put_bioc(recover->bioc); |
af8e2d1d MX |
795 | kfree(recover); |
796 | } | |
797 | } | |
798 | ||
a2de733c | 799 | /* |
b5d67f64 SB |
800 | * scrub_handle_errored_block gets called when either verification of the |
801 | * pages failed or the bio failed to read, e.g. with EIO. In the latter | |
802 | * case, this function handles all pages in the bio, even though only one | |
803 | * may be bad. | |
804 | * The goal of this function is to repair the errored block by using the | |
805 | * contents of one of the mirrors. | |
a2de733c | 806 | */ |
b5d67f64 | 807 | static int scrub_handle_errored_block(struct scrub_block *sblock_to_check) |
a2de733c | 808 | { |
d9d181c1 | 809 | struct scrub_ctx *sctx = sblock_to_check->sctx; |
a36cf8b8 | 810 | struct btrfs_device *dev; |
b5d67f64 | 811 | struct btrfs_fs_info *fs_info; |
b5d67f64 | 812 | u64 logical; |
b5d67f64 SB |
813 | unsigned int failed_mirror_index; |
814 | unsigned int is_metadata; | |
815 | unsigned int have_csum; | |
b5d67f64 SB |
816 | struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */ |
817 | struct scrub_block *sblock_bad; | |
818 | int ret; | |
819 | int mirror_index; | |
820 | int page_num; | |
821 | int success; | |
28d70e23 | 822 | bool full_stripe_locked; |
7c3c7cb9 | 823 | unsigned int nofs_flag; |
8bb1cf1b | 824 | static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, |
b5d67f64 SB |
825 | DEFAULT_RATELIMIT_BURST); |
826 | ||
827 | BUG_ON(sblock_to_check->page_count < 1); | |
fb456252 | 828 | fs_info = sctx->fs_info; |
4ded4f63 SB |
829 | if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) { |
830 | /* | |
831 | * if we find an error in a super block, we just report it. | |
832 | * They will get written with the next transaction commit | |
833 | * anyway | |
834 | */ | |
835 | spin_lock(&sctx->stat_lock); | |
836 | ++sctx->stat.super_errors; | |
837 | spin_unlock(&sctx->stat_lock); | |
838 | return 0; | |
839 | } | |
7a9e9987 | 840 | logical = sblock_to_check->pagev[0]->logical; |
7a9e9987 SB |
841 | BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1); |
842 | failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1; | |
843 | is_metadata = !(sblock_to_check->pagev[0]->flags & | |
b5d67f64 | 844 | BTRFS_EXTENT_FLAG_DATA); |
7a9e9987 | 845 | have_csum = sblock_to_check->pagev[0]->have_csum; |
7a9e9987 | 846 | dev = sblock_to_check->pagev[0]->dev; |
13db62b7 | 847 | |
554aed7d JT |
848 | if (!sctx->is_dev_replace && btrfs_repair_one_zone(fs_info, logical)) |
849 | return 0; | |
f7ef5287 | 850 | |
7c3c7cb9 FM |
851 | /* |
852 | * We must use GFP_NOFS because the scrub task might be waiting for a | |
853 | * worker task executing this function and in turn a transaction commit | |
854 | * might be waiting the scrub task to pause (which needs to wait for all | |
855 | * the worker tasks to complete before pausing). | |
856 | * We do allocations in the workers through insert_full_stripe_lock() | |
857 | * and scrub_add_page_to_wr_bio(), which happens down the call chain of | |
858 | * this function. | |
859 | */ | |
860 | nofs_flag = memalloc_nofs_save(); | |
28d70e23 QW |
861 | /* |
862 | * For RAID5/6, race can happen for a different device scrub thread. | |
863 | * For data corruption, Parity and Data threads will both try | |
864 | * to recovery the data. | |
865 | * Race can lead to doubly added csum error, or even unrecoverable | |
866 | * error. | |
867 | */ | |
868 | ret = lock_full_stripe(fs_info, logical, &full_stripe_locked); | |
869 | if (ret < 0) { | |
7c3c7cb9 | 870 | memalloc_nofs_restore(nofs_flag); |
28d70e23 QW |
871 | spin_lock(&sctx->stat_lock); |
872 | if (ret == -ENOMEM) | |
873 | sctx->stat.malloc_errors++; | |
874 | sctx->stat.read_errors++; | |
875 | sctx->stat.uncorrectable_errors++; | |
876 | spin_unlock(&sctx->stat_lock); | |
877 | return ret; | |
878 | } | |
879 | ||
b5d67f64 SB |
880 | /* |
881 | * read all mirrors one after the other. This includes to | |
882 | * re-read the extent or metadata block that failed (that was | |
883 | * the cause that this fixup code is called) another time, | |
8df507cb | 884 | * sector by sector this time in order to know which sectors |
b5d67f64 SB |
885 | * caused I/O errors and which ones are good (for all mirrors). |
886 | * It is the goal to handle the situation when more than one | |
887 | * mirror contains I/O errors, but the errors do not | |
888 | * overlap, i.e. the data can be repaired by selecting the | |
8df507cb QW |
889 | * sectors from those mirrors without I/O error on the |
890 | * particular sectors. One example (with blocks >= 2 * sectorsize) | |
891 | * would be that mirror #1 has an I/O error on the first sector, | |
892 | * the second sector is good, and mirror #2 has an I/O error on | |
893 | * the second sector, but the first sector is good. | |
894 | * Then the first sector of the first mirror can be repaired by | |
895 | * taking the first sector of the second mirror, and the | |
896 | * second sector of the second mirror can be repaired by | |
897 | * copying the contents of the 2nd sector of the 1st mirror. | |
898 | * One more note: if the sectors of one mirror contain I/O | |
b5d67f64 SB |
899 | * errors, the checksum cannot be verified. In order to get |
900 | * the best data for repairing, the first attempt is to find | |
901 | * a mirror without I/O errors and with a validated checksum. | |
8df507cb | 902 | * Only if this is not possible, the sectors are picked from |
b5d67f64 SB |
903 | * mirrors with I/O errors without considering the checksum. |
904 | * If the latter is the case, at the end, the checksum of the | |
905 | * repaired area is verified in order to correctly maintain | |
906 | * the statistics. | |
907 | */ | |
908 | ||
31e818fe | 909 | sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS, |
7c3c7cb9 | 910 | sizeof(*sblocks_for_recheck), GFP_KERNEL); |
b5d67f64 | 911 | if (!sblocks_for_recheck) { |
d9d181c1 SB |
912 | spin_lock(&sctx->stat_lock); |
913 | sctx->stat.malloc_errors++; | |
914 | sctx->stat.read_errors++; | |
915 | sctx->stat.uncorrectable_errors++; | |
916 | spin_unlock(&sctx->stat_lock); | |
a36cf8b8 | 917 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); |
b5d67f64 | 918 | goto out; |
a2de733c AJ |
919 | } |
920 | ||
b5d67f64 | 921 | /* setup the context, map the logical blocks and alloc the pages */ |
be50a8dd | 922 | ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck); |
b5d67f64 | 923 | if (ret) { |
d9d181c1 SB |
924 | spin_lock(&sctx->stat_lock); |
925 | sctx->stat.read_errors++; | |
926 | sctx->stat.uncorrectable_errors++; | |
927 | spin_unlock(&sctx->stat_lock); | |
a36cf8b8 | 928 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); |
b5d67f64 SB |
929 | goto out; |
930 | } | |
931 | BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS); | |
932 | sblock_bad = sblocks_for_recheck + failed_mirror_index; | |
13db62b7 | 933 | |
b5d67f64 | 934 | /* build and submit the bios for the failed mirror, check checksums */ |
affe4a5a | 935 | scrub_recheck_block(fs_info, sblock_bad, 1); |
a2de733c | 936 | |
b5d67f64 SB |
937 | if (!sblock_bad->header_error && !sblock_bad->checksum_error && |
938 | sblock_bad->no_io_error_seen) { | |
939 | /* | |
940 | * the error disappeared after reading page by page, or | |
941 | * the area was part of a huge bio and other parts of the | |
942 | * bio caused I/O errors, or the block layer merged several | |
943 | * read requests into one and the error is caused by a | |
944 | * different bio (usually one of the two latter cases is | |
945 | * the cause) | |
946 | */ | |
d9d181c1 SB |
947 | spin_lock(&sctx->stat_lock); |
948 | sctx->stat.unverified_errors++; | |
5a6ac9ea | 949 | sblock_to_check->data_corrected = 1; |
d9d181c1 | 950 | spin_unlock(&sctx->stat_lock); |
a2de733c | 951 | |
ff023aac SB |
952 | if (sctx->is_dev_replace) |
953 | scrub_write_block_to_dev_replace(sblock_bad); | |
b5d67f64 | 954 | goto out; |
a2de733c | 955 | } |
a2de733c | 956 | |
b5d67f64 | 957 | if (!sblock_bad->no_io_error_seen) { |
d9d181c1 SB |
958 | spin_lock(&sctx->stat_lock); |
959 | sctx->stat.read_errors++; | |
960 | spin_unlock(&sctx->stat_lock); | |
8bb1cf1b | 961 | if (__ratelimit(&rs)) |
b5d67f64 | 962 | scrub_print_warning("i/o error", sblock_to_check); |
a36cf8b8 | 963 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); |
b5d67f64 | 964 | } else if (sblock_bad->checksum_error) { |
d9d181c1 SB |
965 | spin_lock(&sctx->stat_lock); |
966 | sctx->stat.csum_errors++; | |
967 | spin_unlock(&sctx->stat_lock); | |
8bb1cf1b | 968 | if (__ratelimit(&rs)) |
b5d67f64 | 969 | scrub_print_warning("checksum error", sblock_to_check); |
a36cf8b8 | 970 | btrfs_dev_stat_inc_and_print(dev, |
442a4f63 | 971 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
b5d67f64 | 972 | } else if (sblock_bad->header_error) { |
d9d181c1 SB |
973 | spin_lock(&sctx->stat_lock); |
974 | sctx->stat.verify_errors++; | |
975 | spin_unlock(&sctx->stat_lock); | |
8bb1cf1b | 976 | if (__ratelimit(&rs)) |
b5d67f64 SB |
977 | scrub_print_warning("checksum/header error", |
978 | sblock_to_check); | |
442a4f63 | 979 | if (sblock_bad->generation_error) |
a36cf8b8 | 980 | btrfs_dev_stat_inc_and_print(dev, |
442a4f63 SB |
981 | BTRFS_DEV_STAT_GENERATION_ERRS); |
982 | else | |
a36cf8b8 | 983 | btrfs_dev_stat_inc_and_print(dev, |
442a4f63 | 984 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
b5d67f64 | 985 | } |
a2de733c | 986 | |
33ef30ad ID |
987 | if (sctx->readonly) { |
988 | ASSERT(!sctx->is_dev_replace); | |
989 | goto out; | |
990 | } | |
a2de733c | 991 | |
b5d67f64 SB |
992 | /* |
993 | * now build and submit the bios for the other mirrors, check | |
cb2ced73 SB |
994 | * checksums. |
995 | * First try to pick the mirror which is completely without I/O | |
b5d67f64 SB |
996 | * errors and also does not have a checksum error. |
997 | * If one is found, and if a checksum is present, the full block | |
998 | * that is known to contain an error is rewritten. Afterwards | |
999 | * the block is known to be corrected. | |
1000 | * If a mirror is found which is completely correct, and no | |
1001 | * checksum is present, only those pages are rewritten that had | |
1002 | * an I/O error in the block to be repaired, since it cannot be | |
1003 | * determined, which copy of the other pages is better (and it | |
1004 | * could happen otherwise that a correct page would be | |
1005 | * overwritten by a bad one). | |
1006 | */ | |
762221f0 | 1007 | for (mirror_index = 0; ;mirror_index++) { |
cb2ced73 | 1008 | struct scrub_block *sblock_other; |
b5d67f64 | 1009 | |
cb2ced73 SB |
1010 | if (mirror_index == failed_mirror_index) |
1011 | continue; | |
762221f0 LB |
1012 | |
1013 | /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */ | |
1014 | if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) { | |
1015 | if (mirror_index >= BTRFS_MAX_MIRRORS) | |
1016 | break; | |
1017 | if (!sblocks_for_recheck[mirror_index].page_count) | |
1018 | break; | |
1019 | ||
1020 | sblock_other = sblocks_for_recheck + mirror_index; | |
1021 | } else { | |
1022 | struct scrub_recover *r = sblock_bad->pagev[0]->recover; | |
4c664611 | 1023 | int max_allowed = r->bioc->num_stripes - r->bioc->num_tgtdevs; |
762221f0 LB |
1024 | |
1025 | if (mirror_index >= max_allowed) | |
1026 | break; | |
1027 | if (!sblocks_for_recheck[1].page_count) | |
1028 | break; | |
1029 | ||
1030 | ASSERT(failed_mirror_index == 0); | |
1031 | sblock_other = sblocks_for_recheck + 1; | |
1032 | sblock_other->pagev[0]->mirror_num = 1 + mirror_index; | |
1033 | } | |
cb2ced73 SB |
1034 | |
1035 | /* build and submit the bios, check checksums */ | |
affe4a5a | 1036 | scrub_recheck_block(fs_info, sblock_other, 0); |
34f5c8e9 SB |
1037 | |
1038 | if (!sblock_other->header_error && | |
b5d67f64 SB |
1039 | !sblock_other->checksum_error && |
1040 | sblock_other->no_io_error_seen) { | |
ff023aac SB |
1041 | if (sctx->is_dev_replace) { |
1042 | scrub_write_block_to_dev_replace(sblock_other); | |
114ab50d | 1043 | goto corrected_error; |
ff023aac | 1044 | } else { |
ff023aac | 1045 | ret = scrub_repair_block_from_good_copy( |
114ab50d ZL |
1046 | sblock_bad, sblock_other); |
1047 | if (!ret) | |
1048 | goto corrected_error; | |
ff023aac | 1049 | } |
b5d67f64 SB |
1050 | } |
1051 | } | |
a2de733c | 1052 | |
b968fed1 ZL |
1053 | if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace) |
1054 | goto did_not_correct_error; | |
ff023aac SB |
1055 | |
1056 | /* | |
ff023aac | 1057 | * In case of I/O errors in the area that is supposed to be |
8df507cb QW |
1058 | * repaired, continue by picking good copies of those sectors. |
1059 | * Select the good sectors from mirrors to rewrite bad sectors from | |
b5d67f64 SB |
1060 | * the area to fix. Afterwards verify the checksum of the block |
1061 | * that is supposed to be repaired. This verification step is | |
1062 | * only done for the purpose of statistic counting and for the | |
1063 | * final scrub report, whether errors remain. | |
1064 | * A perfect algorithm could make use of the checksum and try | |
8df507cb | 1065 | * all possible combinations of sectors from the different mirrors |
b5d67f64 | 1066 | * until the checksum verification succeeds. For example, when |
8df507cb | 1067 | * the 2nd sector of mirror #1 faces I/O errors, and the 2nd sector |
b5d67f64 | 1068 | * of mirror #2 is readable but the final checksum test fails, |
8df507cb | 1069 | * then the 2nd sector of mirror #3 could be tried, whether now |
01327610 | 1070 | * the final checksum succeeds. But this would be a rare |
b5d67f64 SB |
1071 | * exception and is therefore not implemented. At least it is |
1072 | * avoided that the good copy is overwritten. | |
1073 | * A more useful improvement would be to pick the sectors | |
1074 | * without I/O error based on sector sizes (512 bytes on legacy | |
8df507cb | 1075 | * disks) instead of on sectorsize. Then maybe 512 byte of one |
b5d67f64 | 1076 | * mirror could be repaired by taking 512 byte of a different |
8df507cb | 1077 | * mirror, even if other 512 byte sectors in the same sectorsize |
b5d67f64 | 1078 | * area are unreadable. |
a2de733c | 1079 | */ |
b5d67f64 | 1080 | success = 1; |
b968fed1 ZL |
1081 | for (page_num = 0; page_num < sblock_bad->page_count; |
1082 | page_num++) { | |
261d2dcb | 1083 | struct scrub_page *spage_bad = sblock_bad->pagev[page_num]; |
b968fed1 | 1084 | struct scrub_block *sblock_other = NULL; |
b5d67f64 | 1085 | |
b968fed1 | 1086 | /* skip no-io-error page in scrub */ |
261d2dcb | 1087 | if (!spage_bad->io_error && !sctx->is_dev_replace) |
a2de733c | 1088 | continue; |
b5d67f64 | 1089 | |
4759700a LB |
1090 | if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) { |
1091 | /* | |
1092 | * In case of dev replace, if raid56 rebuild process | |
1093 | * didn't work out correct data, then copy the content | |
1094 | * in sblock_bad to make sure target device is identical | |
1095 | * to source device, instead of writing garbage data in | |
1096 | * sblock_for_recheck array to target device. | |
1097 | */ | |
1098 | sblock_other = NULL; | |
261d2dcb | 1099 | } else if (spage_bad->io_error) { |
4759700a | 1100 | /* try to find no-io-error page in mirrors */ |
b968fed1 ZL |
1101 | for (mirror_index = 0; |
1102 | mirror_index < BTRFS_MAX_MIRRORS && | |
1103 | sblocks_for_recheck[mirror_index].page_count > 0; | |
1104 | mirror_index++) { | |
1105 | if (!sblocks_for_recheck[mirror_index]. | |
1106 | pagev[page_num]->io_error) { | |
1107 | sblock_other = sblocks_for_recheck + | |
1108 | mirror_index; | |
1109 | break; | |
b5d67f64 SB |
1110 | } |
1111 | } | |
b968fed1 ZL |
1112 | if (!sblock_other) |
1113 | success = 0; | |
96e36920 | 1114 | } |
a2de733c | 1115 | |
b968fed1 ZL |
1116 | if (sctx->is_dev_replace) { |
1117 | /* | |
1118 | * did not find a mirror to fetch the page | |
1119 | * from. scrub_write_page_to_dev_replace() | |
1120 | * handles this case (page->io_error), by | |
1121 | * filling the block with zeros before | |
1122 | * submitting the write request | |
1123 | */ | |
1124 | if (!sblock_other) | |
1125 | sblock_other = sblock_bad; | |
1126 | ||
1127 | if (scrub_write_page_to_dev_replace(sblock_other, | |
1128 | page_num) != 0) { | |
e37abe97 | 1129 | atomic64_inc( |
0b246afa | 1130 | &fs_info->dev_replace.num_write_errors); |
b968fed1 ZL |
1131 | success = 0; |
1132 | } | |
1133 | } else if (sblock_other) { | |
1134 | ret = scrub_repair_page_from_good_copy(sblock_bad, | |
1135 | sblock_other, | |
1136 | page_num, 0); | |
1137 | if (0 == ret) | |
261d2dcb | 1138 | spage_bad->io_error = 0; |
b968fed1 ZL |
1139 | else |
1140 | success = 0; | |
b5d67f64 | 1141 | } |
a2de733c | 1142 | } |
a2de733c | 1143 | |
b968fed1 | 1144 | if (success && !sctx->is_dev_replace) { |
b5d67f64 SB |
1145 | if (is_metadata || have_csum) { |
1146 | /* | |
1147 | * need to verify the checksum now that all | |
1148 | * sectors on disk are repaired (the write | |
1149 | * request for data to be repaired is on its way). | |
1150 | * Just be lazy and use scrub_recheck_block() | |
1151 | * which re-reads the data before the checksum | |
1152 | * is verified, but most likely the data comes out | |
1153 | * of the page cache. | |
1154 | */ | |
affe4a5a | 1155 | scrub_recheck_block(fs_info, sblock_bad, 1); |
34f5c8e9 | 1156 | if (!sblock_bad->header_error && |
b5d67f64 SB |
1157 | !sblock_bad->checksum_error && |
1158 | sblock_bad->no_io_error_seen) | |
1159 | goto corrected_error; | |
1160 | else | |
1161 | goto did_not_correct_error; | |
1162 | } else { | |
1163 | corrected_error: | |
d9d181c1 SB |
1164 | spin_lock(&sctx->stat_lock); |
1165 | sctx->stat.corrected_errors++; | |
5a6ac9ea | 1166 | sblock_to_check->data_corrected = 1; |
d9d181c1 | 1167 | spin_unlock(&sctx->stat_lock); |
b14af3b4 DS |
1168 | btrfs_err_rl_in_rcu(fs_info, |
1169 | "fixed up error at logical %llu on dev %s", | |
c1c9ff7c | 1170 | logical, rcu_str_deref(dev->name)); |
8628764e | 1171 | } |
b5d67f64 SB |
1172 | } else { |
1173 | did_not_correct_error: | |
d9d181c1 SB |
1174 | spin_lock(&sctx->stat_lock); |
1175 | sctx->stat.uncorrectable_errors++; | |
1176 | spin_unlock(&sctx->stat_lock); | |
b14af3b4 DS |
1177 | btrfs_err_rl_in_rcu(fs_info, |
1178 | "unable to fixup (regular) error at logical %llu on dev %s", | |
c1c9ff7c | 1179 | logical, rcu_str_deref(dev->name)); |
96e36920 | 1180 | } |
a2de733c | 1181 | |
b5d67f64 SB |
1182 | out: |
1183 | if (sblocks_for_recheck) { | |
1184 | for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS; | |
1185 | mirror_index++) { | |
1186 | struct scrub_block *sblock = sblocks_for_recheck + | |
1187 | mirror_index; | |
af8e2d1d | 1188 | struct scrub_recover *recover; |
b5d67f64 SB |
1189 | int page_index; |
1190 | ||
7a9e9987 SB |
1191 | for (page_index = 0; page_index < sblock->page_count; |
1192 | page_index++) { | |
1193 | sblock->pagev[page_index]->sblock = NULL; | |
af8e2d1d MX |
1194 | recover = sblock->pagev[page_index]->recover; |
1195 | if (recover) { | |
e501bfe3 | 1196 | scrub_put_recover(fs_info, recover); |
af8e2d1d MX |
1197 | sblock->pagev[page_index]->recover = |
1198 | NULL; | |
1199 | } | |
7a9e9987 SB |
1200 | scrub_page_put(sblock->pagev[page_index]); |
1201 | } | |
b5d67f64 SB |
1202 | } |
1203 | kfree(sblocks_for_recheck); | |
1204 | } | |
a2de733c | 1205 | |
28d70e23 | 1206 | ret = unlock_full_stripe(fs_info, logical, full_stripe_locked); |
7c3c7cb9 | 1207 | memalloc_nofs_restore(nofs_flag); |
28d70e23 QW |
1208 | if (ret < 0) |
1209 | return ret; | |
b5d67f64 SB |
1210 | return 0; |
1211 | } | |
a2de733c | 1212 | |
4c664611 | 1213 | static inline int scrub_nr_raid_mirrors(struct btrfs_io_context *bioc) |
af8e2d1d | 1214 | { |
4c664611 | 1215 | if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID5) |
10f11900 | 1216 | return 2; |
4c664611 | 1217 | else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID6) |
10f11900 ZL |
1218 | return 3; |
1219 | else | |
4c664611 | 1220 | return (int)bioc->num_stripes; |
af8e2d1d MX |
1221 | } |
1222 | ||
10f11900 ZL |
1223 | static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type, |
1224 | u64 *raid_map, | |
af8e2d1d MX |
1225 | u64 mapped_length, |
1226 | int nstripes, int mirror, | |
1227 | int *stripe_index, | |
1228 | u64 *stripe_offset) | |
1229 | { | |
1230 | int i; | |
1231 | ||
ffe2d203 | 1232 | if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
af8e2d1d MX |
1233 | /* RAID5/6 */ |
1234 | for (i = 0; i < nstripes; i++) { | |
1235 | if (raid_map[i] == RAID6_Q_STRIPE || | |
1236 | raid_map[i] == RAID5_P_STRIPE) | |
1237 | continue; | |
1238 | ||
1239 | if (logical >= raid_map[i] && | |
1240 | logical < raid_map[i] + mapped_length) | |
1241 | break; | |
1242 | } | |
1243 | ||
1244 | *stripe_index = i; | |
1245 | *stripe_offset = logical - raid_map[i]; | |
1246 | } else { | |
1247 | /* The other RAID type */ | |
1248 | *stripe_index = mirror; | |
1249 | *stripe_offset = 0; | |
1250 | } | |
1251 | } | |
1252 | ||
be50a8dd | 1253 | static int scrub_setup_recheck_block(struct scrub_block *original_sblock, |
b5d67f64 SB |
1254 | struct scrub_block *sblocks_for_recheck) |
1255 | { | |
be50a8dd | 1256 | struct scrub_ctx *sctx = original_sblock->sctx; |
fb456252 | 1257 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
8df507cb | 1258 | u64 length = original_sblock->page_count * fs_info->sectorsize; |
be50a8dd | 1259 | u64 logical = original_sblock->pagev[0]->logical; |
4734b7ed ZL |
1260 | u64 generation = original_sblock->pagev[0]->generation; |
1261 | u64 flags = original_sblock->pagev[0]->flags; | |
1262 | u64 have_csum = original_sblock->pagev[0]->have_csum; | |
af8e2d1d | 1263 | struct scrub_recover *recover; |
4c664611 | 1264 | struct btrfs_io_context *bioc; |
af8e2d1d MX |
1265 | u64 sublen; |
1266 | u64 mapped_length; | |
1267 | u64 stripe_offset; | |
1268 | int stripe_index; | |
be50a8dd | 1269 | int page_index = 0; |
b5d67f64 | 1270 | int mirror_index; |
af8e2d1d | 1271 | int nmirrors; |
b5d67f64 SB |
1272 | int ret; |
1273 | ||
1274 | /* | |
57019345 | 1275 | * note: the two members refs and outstanding_pages |
b5d67f64 SB |
1276 | * are not used (and not set) in the blocks that are used for |
1277 | * the recheck procedure | |
1278 | */ | |
1279 | ||
b5d67f64 | 1280 | while (length > 0) { |
8df507cb | 1281 | sublen = min_t(u64, length, fs_info->sectorsize); |
af8e2d1d | 1282 | mapped_length = sublen; |
4c664611 | 1283 | bioc = NULL; |
a2de733c | 1284 | |
b5d67f64 | 1285 | /* |
8df507cb QW |
1286 | * With a length of sectorsize, each returned stripe represents |
1287 | * one mirror | |
b5d67f64 | 1288 | */ |
e501bfe3 | 1289 | btrfs_bio_counter_inc_blocked(fs_info); |
cf8cddd3 | 1290 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
4c664611 QW |
1291 | logical, &mapped_length, &bioc); |
1292 | if (ret || !bioc || mapped_length < sublen) { | |
1293 | btrfs_put_bioc(bioc); | |
e501bfe3 | 1294 | btrfs_bio_counter_dec(fs_info); |
b5d67f64 SB |
1295 | return -EIO; |
1296 | } | |
a2de733c | 1297 | |
af8e2d1d MX |
1298 | recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS); |
1299 | if (!recover) { | |
4c664611 | 1300 | btrfs_put_bioc(bioc); |
e501bfe3 | 1301 | btrfs_bio_counter_dec(fs_info); |
af8e2d1d MX |
1302 | return -ENOMEM; |
1303 | } | |
1304 | ||
6f615018 | 1305 | refcount_set(&recover->refs, 1); |
4c664611 | 1306 | recover->bioc = bioc; |
af8e2d1d MX |
1307 | recover->map_length = mapped_length; |
1308 | ||
0bb3acdc | 1309 | ASSERT(page_index < SCRUB_MAX_PAGES_PER_BLOCK); |
af8e2d1d | 1310 | |
4c664611 | 1311 | nmirrors = min(scrub_nr_raid_mirrors(bioc), BTRFS_MAX_MIRRORS); |
10f11900 | 1312 | |
af8e2d1d | 1313 | for (mirror_index = 0; mirror_index < nmirrors; |
b5d67f64 SB |
1314 | mirror_index++) { |
1315 | struct scrub_block *sblock; | |
261d2dcb | 1316 | struct scrub_page *spage; |
b5d67f64 | 1317 | |
b5d67f64 | 1318 | sblock = sblocks_for_recheck + mirror_index; |
7a9e9987 | 1319 | sblock->sctx = sctx; |
4734b7ed | 1320 | |
261d2dcb QW |
1321 | spage = kzalloc(sizeof(*spage), GFP_NOFS); |
1322 | if (!spage) { | |
7a9e9987 | 1323 | leave_nomem: |
d9d181c1 SB |
1324 | spin_lock(&sctx->stat_lock); |
1325 | sctx->stat.malloc_errors++; | |
1326 | spin_unlock(&sctx->stat_lock); | |
e501bfe3 | 1327 | scrub_put_recover(fs_info, recover); |
b5d67f64 SB |
1328 | return -ENOMEM; |
1329 | } | |
261d2dcb QW |
1330 | scrub_page_get(spage); |
1331 | sblock->pagev[page_index] = spage; | |
1332 | spage->sblock = sblock; | |
1333 | spage->flags = flags; | |
1334 | spage->generation = generation; | |
1335 | spage->logical = logical; | |
1336 | spage->have_csum = have_csum; | |
4734b7ed | 1337 | if (have_csum) |
261d2dcb | 1338 | memcpy(spage->csum, |
4734b7ed | 1339 | original_sblock->pagev[0]->csum, |
2ae0c2d8 | 1340 | sctx->fs_info->csum_size); |
af8e2d1d | 1341 | |
10f11900 | 1342 | scrub_stripe_index_and_offset(logical, |
4c664611 QW |
1343 | bioc->map_type, |
1344 | bioc->raid_map, | |
af8e2d1d | 1345 | mapped_length, |
4c664611 QW |
1346 | bioc->num_stripes - |
1347 | bioc->num_tgtdevs, | |
af8e2d1d MX |
1348 | mirror_index, |
1349 | &stripe_index, | |
1350 | &stripe_offset); | |
4c664611 | 1351 | spage->physical = bioc->stripes[stripe_index].physical + |
af8e2d1d | 1352 | stripe_offset; |
4c664611 | 1353 | spage->dev = bioc->stripes[stripe_index].dev; |
af8e2d1d | 1354 | |
ff023aac | 1355 | BUG_ON(page_index >= original_sblock->page_count); |
261d2dcb | 1356 | spage->physical_for_dev_replace = |
ff023aac SB |
1357 | original_sblock->pagev[page_index]-> |
1358 | physical_for_dev_replace; | |
7a9e9987 | 1359 | /* for missing devices, dev->bdev is NULL */ |
261d2dcb | 1360 | spage->mirror_num = mirror_index + 1; |
b5d67f64 | 1361 | sblock->page_count++; |
261d2dcb QW |
1362 | spage->page = alloc_page(GFP_NOFS); |
1363 | if (!spage->page) | |
7a9e9987 | 1364 | goto leave_nomem; |
af8e2d1d MX |
1365 | |
1366 | scrub_get_recover(recover); | |
261d2dcb | 1367 | spage->recover = recover; |
b5d67f64 | 1368 | } |
e501bfe3 | 1369 | scrub_put_recover(fs_info, recover); |
b5d67f64 SB |
1370 | length -= sublen; |
1371 | logical += sublen; | |
1372 | page_index++; | |
1373 | } | |
1374 | ||
1375 | return 0; | |
96e36920 ID |
1376 | } |
1377 | ||
4246a0b6 | 1378 | static void scrub_bio_wait_endio(struct bio *bio) |
af8e2d1d | 1379 | { |
b4ff5ad7 | 1380 | complete(bio->bi_private); |
af8e2d1d MX |
1381 | } |
1382 | ||
af8e2d1d MX |
1383 | static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info, |
1384 | struct bio *bio, | |
261d2dcb | 1385 | struct scrub_page *spage) |
af8e2d1d | 1386 | { |
b4ff5ad7 | 1387 | DECLARE_COMPLETION_ONSTACK(done); |
af8e2d1d | 1388 | int ret; |
762221f0 | 1389 | int mirror_num; |
af8e2d1d | 1390 | |
261d2dcb | 1391 | bio->bi_iter.bi_sector = spage->logical >> 9; |
af8e2d1d MX |
1392 | bio->bi_private = &done; |
1393 | bio->bi_end_io = scrub_bio_wait_endio; | |
1394 | ||
261d2dcb | 1395 | mirror_num = spage->sblock->pagev[0]->mirror_num; |
6a258d72 | 1396 | ret = raid56_parity_recover(bio, spage->recover->bioc, |
261d2dcb | 1397 | spage->recover->map_length, |
762221f0 | 1398 | mirror_num, 0); |
af8e2d1d MX |
1399 | if (ret) |
1400 | return ret; | |
1401 | ||
b4ff5ad7 LB |
1402 | wait_for_completion_io(&done); |
1403 | return blk_status_to_errno(bio->bi_status); | |
af8e2d1d MX |
1404 | } |
1405 | ||
6ca1765b LB |
1406 | static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info, |
1407 | struct scrub_block *sblock) | |
1408 | { | |
1409 | struct scrub_page *first_page = sblock->pagev[0]; | |
1410 | struct bio *bio; | |
1411 | int page_num; | |
1412 | ||
1413 | /* All pages in sblock belong to the same stripe on the same device. */ | |
1414 | ASSERT(first_page->dev); | |
1415 | if (!first_page->dev->bdev) | |
1416 | goto out; | |
1417 | ||
c3a3b19b | 1418 | bio = btrfs_bio_alloc(BIO_MAX_VECS); |
6ca1765b LB |
1419 | bio_set_dev(bio, first_page->dev->bdev); |
1420 | ||
1421 | for (page_num = 0; page_num < sblock->page_count; page_num++) { | |
261d2dcb | 1422 | struct scrub_page *spage = sblock->pagev[page_num]; |
6ca1765b | 1423 | |
261d2dcb QW |
1424 | WARN_ON(!spage->page); |
1425 | bio_add_page(bio, spage->page, PAGE_SIZE, 0); | |
6ca1765b LB |
1426 | } |
1427 | ||
1428 | if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) { | |
1429 | bio_put(bio); | |
1430 | goto out; | |
1431 | } | |
1432 | ||
1433 | bio_put(bio); | |
1434 | ||
1435 | scrub_recheck_block_checksum(sblock); | |
1436 | ||
1437 | return; | |
1438 | out: | |
1439 | for (page_num = 0; page_num < sblock->page_count; page_num++) | |
1440 | sblock->pagev[page_num]->io_error = 1; | |
1441 | ||
1442 | sblock->no_io_error_seen = 0; | |
1443 | } | |
1444 | ||
b5d67f64 SB |
1445 | /* |
1446 | * this function will check the on disk data for checksum errors, header | |
1447 | * errors and read I/O errors. If any I/O errors happen, the exact pages | |
1448 | * which are errored are marked as being bad. The goal is to enable scrub | |
1449 | * to take those pages that are not errored from all the mirrors so that | |
1450 | * the pages that are errored in the just handled mirror can be repaired. | |
1451 | */ | |
34f5c8e9 | 1452 | static void scrub_recheck_block(struct btrfs_fs_info *fs_info, |
affe4a5a ZL |
1453 | struct scrub_block *sblock, |
1454 | int retry_failed_mirror) | |
96e36920 | 1455 | { |
b5d67f64 | 1456 | int page_num; |
96e36920 | 1457 | |
b5d67f64 | 1458 | sblock->no_io_error_seen = 1; |
96e36920 | 1459 | |
6ca1765b LB |
1460 | /* short cut for raid56 */ |
1461 | if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0])) | |
1462 | return scrub_recheck_block_on_raid56(fs_info, sblock); | |
1463 | ||
b5d67f64 SB |
1464 | for (page_num = 0; page_num < sblock->page_count; page_num++) { |
1465 | struct bio *bio; | |
261d2dcb | 1466 | struct scrub_page *spage = sblock->pagev[page_num]; |
b5d67f64 | 1467 | |
261d2dcb QW |
1468 | if (spage->dev->bdev == NULL) { |
1469 | spage->io_error = 1; | |
ea9947b4 SB |
1470 | sblock->no_io_error_seen = 0; |
1471 | continue; | |
1472 | } | |
1473 | ||
261d2dcb | 1474 | WARN_ON(!spage->page); |
c3a3b19b | 1475 | bio = btrfs_bio_alloc(1); |
261d2dcb | 1476 | bio_set_dev(bio, spage->dev->bdev); |
b5d67f64 | 1477 | |
8df507cb | 1478 | bio_add_page(bio, spage->page, fs_info->sectorsize, 0); |
261d2dcb | 1479 | bio->bi_iter.bi_sector = spage->physical >> 9; |
6ca1765b | 1480 | bio->bi_opf = REQ_OP_READ; |
af8e2d1d | 1481 | |
6ca1765b | 1482 | if (btrfsic_submit_bio_wait(bio)) { |
261d2dcb | 1483 | spage->io_error = 1; |
6ca1765b | 1484 | sblock->no_io_error_seen = 0; |
af8e2d1d | 1485 | } |
33879d45 | 1486 | |
b5d67f64 SB |
1487 | bio_put(bio); |
1488 | } | |
96e36920 | 1489 | |
b5d67f64 | 1490 | if (sblock->no_io_error_seen) |
ba7cf988 | 1491 | scrub_recheck_block_checksum(sblock); |
a2de733c AJ |
1492 | } |
1493 | ||
17a9be2f MX |
1494 | static inline int scrub_check_fsid(u8 fsid[], |
1495 | struct scrub_page *spage) | |
1496 | { | |
1497 | struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices; | |
1498 | int ret; | |
1499 | ||
44880fdc | 1500 | ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
17a9be2f MX |
1501 | return !ret; |
1502 | } | |
1503 | ||
ba7cf988 | 1504 | static void scrub_recheck_block_checksum(struct scrub_block *sblock) |
a2de733c | 1505 | { |
ba7cf988 ZL |
1506 | sblock->header_error = 0; |
1507 | sblock->checksum_error = 0; | |
1508 | sblock->generation_error = 0; | |
b5d67f64 | 1509 | |
ba7cf988 ZL |
1510 | if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA) |
1511 | scrub_checksum_data(sblock); | |
1512 | else | |
1513 | scrub_checksum_tree_block(sblock); | |
a2de733c AJ |
1514 | } |
1515 | ||
b5d67f64 | 1516 | static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad, |
114ab50d | 1517 | struct scrub_block *sblock_good) |
b5d67f64 SB |
1518 | { |
1519 | int page_num; | |
1520 | int ret = 0; | |
96e36920 | 1521 | |
b5d67f64 SB |
1522 | for (page_num = 0; page_num < sblock_bad->page_count; page_num++) { |
1523 | int ret_sub; | |
96e36920 | 1524 | |
b5d67f64 SB |
1525 | ret_sub = scrub_repair_page_from_good_copy(sblock_bad, |
1526 | sblock_good, | |
114ab50d | 1527 | page_num, 1); |
b5d67f64 SB |
1528 | if (ret_sub) |
1529 | ret = ret_sub; | |
a2de733c | 1530 | } |
b5d67f64 SB |
1531 | |
1532 | return ret; | |
1533 | } | |
1534 | ||
1535 | static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad, | |
1536 | struct scrub_block *sblock_good, | |
1537 | int page_num, int force_write) | |
1538 | { | |
261d2dcb QW |
1539 | struct scrub_page *spage_bad = sblock_bad->pagev[page_num]; |
1540 | struct scrub_page *spage_good = sblock_good->pagev[page_num]; | |
0b246afa | 1541 | struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info; |
8df507cb | 1542 | const u32 sectorsize = fs_info->sectorsize; |
b5d67f64 | 1543 | |
261d2dcb QW |
1544 | BUG_ON(spage_bad->page == NULL); |
1545 | BUG_ON(spage_good->page == NULL); | |
b5d67f64 | 1546 | if (force_write || sblock_bad->header_error || |
261d2dcb | 1547 | sblock_bad->checksum_error || spage_bad->io_error) { |
b5d67f64 SB |
1548 | struct bio *bio; |
1549 | int ret; | |
b5d67f64 | 1550 | |
261d2dcb | 1551 | if (!spage_bad->dev->bdev) { |
0b246afa | 1552 | btrfs_warn_rl(fs_info, |
5d163e0e | 1553 | "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected"); |
ff023aac SB |
1554 | return -EIO; |
1555 | } | |
1556 | ||
c3a3b19b | 1557 | bio = btrfs_bio_alloc(1); |
261d2dcb QW |
1558 | bio_set_dev(bio, spage_bad->dev->bdev); |
1559 | bio->bi_iter.bi_sector = spage_bad->physical >> 9; | |
ebcc3263 | 1560 | bio->bi_opf = REQ_OP_WRITE; |
b5d67f64 | 1561 | |
8df507cb QW |
1562 | ret = bio_add_page(bio, spage_good->page, sectorsize, 0); |
1563 | if (ret != sectorsize) { | |
b5d67f64 SB |
1564 | bio_put(bio); |
1565 | return -EIO; | |
13db62b7 | 1566 | } |
b5d67f64 | 1567 | |
4e49ea4a | 1568 | if (btrfsic_submit_bio_wait(bio)) { |
261d2dcb | 1569 | btrfs_dev_stat_inc_and_print(spage_bad->dev, |
442a4f63 | 1570 | BTRFS_DEV_STAT_WRITE_ERRS); |
e37abe97 | 1571 | atomic64_inc(&fs_info->dev_replace.num_write_errors); |
442a4f63 SB |
1572 | bio_put(bio); |
1573 | return -EIO; | |
1574 | } | |
b5d67f64 | 1575 | bio_put(bio); |
a2de733c AJ |
1576 | } |
1577 | ||
b5d67f64 SB |
1578 | return 0; |
1579 | } | |
1580 | ||
ff023aac SB |
1581 | static void scrub_write_block_to_dev_replace(struct scrub_block *sblock) |
1582 | { | |
0b246afa | 1583 | struct btrfs_fs_info *fs_info = sblock->sctx->fs_info; |
ff023aac SB |
1584 | int page_num; |
1585 | ||
5a6ac9ea MX |
1586 | /* |
1587 | * This block is used for the check of the parity on the source device, | |
1588 | * so the data needn't be written into the destination device. | |
1589 | */ | |
1590 | if (sblock->sparity) | |
1591 | return; | |
1592 | ||
ff023aac SB |
1593 | for (page_num = 0; page_num < sblock->page_count; page_num++) { |
1594 | int ret; | |
1595 | ||
1596 | ret = scrub_write_page_to_dev_replace(sblock, page_num); | |
1597 | if (ret) | |
e37abe97 | 1598 | atomic64_inc(&fs_info->dev_replace.num_write_errors); |
ff023aac SB |
1599 | } |
1600 | } | |
1601 | ||
1602 | static int scrub_write_page_to_dev_replace(struct scrub_block *sblock, | |
1603 | int page_num) | |
1604 | { | |
1605 | struct scrub_page *spage = sblock->pagev[page_num]; | |
1606 | ||
1607 | BUG_ON(spage->page == NULL); | |
a8b3a890 DS |
1608 | if (spage->io_error) |
1609 | clear_page(page_address(spage->page)); | |
ff023aac | 1610 | |
ff023aac SB |
1611 | return scrub_add_page_to_wr_bio(sblock->sctx, spage); |
1612 | } | |
1613 | ||
de17addc NA |
1614 | static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical) |
1615 | { | |
1616 | int ret = 0; | |
1617 | u64 length; | |
1618 | ||
1619 | if (!btrfs_is_zoned(sctx->fs_info)) | |
1620 | return 0; | |
1621 | ||
7db1c5d1 NA |
1622 | if (!btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) |
1623 | return 0; | |
1624 | ||
de17addc NA |
1625 | if (sctx->write_pointer < physical) { |
1626 | length = physical - sctx->write_pointer; | |
1627 | ||
1628 | ret = btrfs_zoned_issue_zeroout(sctx->wr_tgtdev, | |
1629 | sctx->write_pointer, length); | |
1630 | if (!ret) | |
1631 | sctx->write_pointer = physical; | |
1632 | } | |
1633 | return ret; | |
1634 | } | |
1635 | ||
ff023aac SB |
1636 | static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx, |
1637 | struct scrub_page *spage) | |
1638 | { | |
ff023aac SB |
1639 | struct scrub_bio *sbio; |
1640 | int ret; | |
8df507cb | 1641 | const u32 sectorsize = sctx->fs_info->sectorsize; |
ff023aac | 1642 | |
3fb99303 | 1643 | mutex_lock(&sctx->wr_lock); |
ff023aac | 1644 | again: |
3fb99303 DS |
1645 | if (!sctx->wr_curr_bio) { |
1646 | sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio), | |
58c4e173 | 1647 | GFP_KERNEL); |
3fb99303 DS |
1648 | if (!sctx->wr_curr_bio) { |
1649 | mutex_unlock(&sctx->wr_lock); | |
ff023aac SB |
1650 | return -ENOMEM; |
1651 | } | |
3fb99303 DS |
1652 | sctx->wr_curr_bio->sctx = sctx; |
1653 | sctx->wr_curr_bio->page_count = 0; | |
ff023aac | 1654 | } |
3fb99303 | 1655 | sbio = sctx->wr_curr_bio; |
ff023aac SB |
1656 | if (sbio->page_count == 0) { |
1657 | struct bio *bio; | |
1658 | ||
de17addc NA |
1659 | ret = fill_writer_pointer_gap(sctx, |
1660 | spage->physical_for_dev_replace); | |
1661 | if (ret) { | |
1662 | mutex_unlock(&sctx->wr_lock); | |
1663 | return ret; | |
1664 | } | |
1665 | ||
ff023aac SB |
1666 | sbio->physical = spage->physical_for_dev_replace; |
1667 | sbio->logical = spage->logical; | |
3fb99303 | 1668 | sbio->dev = sctx->wr_tgtdev; |
ff023aac SB |
1669 | bio = sbio->bio; |
1670 | if (!bio) { | |
c9d328c0 | 1671 | bio = btrfs_bio_alloc(sctx->pages_per_bio); |
ff023aac SB |
1672 | sbio->bio = bio; |
1673 | } | |
1674 | ||
1675 | bio->bi_private = sbio; | |
1676 | bio->bi_end_io = scrub_wr_bio_end_io; | |
74d46992 | 1677 | bio_set_dev(bio, sbio->dev->bdev); |
4f024f37 | 1678 | bio->bi_iter.bi_sector = sbio->physical >> 9; |
ebcc3263 | 1679 | bio->bi_opf = REQ_OP_WRITE; |
4e4cbee9 | 1680 | sbio->status = 0; |
8df507cb | 1681 | } else if (sbio->physical + sbio->page_count * sectorsize != |
ff023aac | 1682 | spage->physical_for_dev_replace || |
8df507cb | 1683 | sbio->logical + sbio->page_count * sectorsize != |
ff023aac SB |
1684 | spage->logical) { |
1685 | scrub_wr_submit(sctx); | |
1686 | goto again; | |
1687 | } | |
1688 | ||
8df507cb QW |
1689 | ret = bio_add_page(sbio->bio, spage->page, sectorsize, 0); |
1690 | if (ret != sectorsize) { | |
ff023aac SB |
1691 | if (sbio->page_count < 1) { |
1692 | bio_put(sbio->bio); | |
1693 | sbio->bio = NULL; | |
3fb99303 | 1694 | mutex_unlock(&sctx->wr_lock); |
ff023aac SB |
1695 | return -EIO; |
1696 | } | |
1697 | scrub_wr_submit(sctx); | |
1698 | goto again; | |
1699 | } | |
1700 | ||
1701 | sbio->pagev[sbio->page_count] = spage; | |
1702 | scrub_page_get(spage); | |
1703 | sbio->page_count++; | |
c9d328c0 | 1704 | if (sbio->page_count == sctx->pages_per_bio) |
ff023aac | 1705 | scrub_wr_submit(sctx); |
3fb99303 | 1706 | mutex_unlock(&sctx->wr_lock); |
ff023aac SB |
1707 | |
1708 | return 0; | |
1709 | } | |
1710 | ||
1711 | static void scrub_wr_submit(struct scrub_ctx *sctx) | |
1712 | { | |
ff023aac SB |
1713 | struct scrub_bio *sbio; |
1714 | ||
3fb99303 | 1715 | if (!sctx->wr_curr_bio) |
ff023aac SB |
1716 | return; |
1717 | ||
3fb99303 DS |
1718 | sbio = sctx->wr_curr_bio; |
1719 | sctx->wr_curr_bio = NULL; | |
309dca30 | 1720 | WARN_ON(!sbio->bio->bi_bdev); |
ff023aac SB |
1721 | scrub_pending_bio_inc(sctx); |
1722 | /* process all writes in a single worker thread. Then the block layer | |
1723 | * orders the requests before sending them to the driver which | |
1724 | * doubled the write performance on spinning disks when measured | |
1725 | * with Linux 3.5 */ | |
4e49ea4a | 1726 | btrfsic_submit_bio(sbio->bio); |
de17addc NA |
1727 | |
1728 | if (btrfs_is_zoned(sctx->fs_info)) | |
8df507cb QW |
1729 | sctx->write_pointer = sbio->physical + sbio->page_count * |
1730 | sctx->fs_info->sectorsize; | |
ff023aac SB |
1731 | } |
1732 | ||
4246a0b6 | 1733 | static void scrub_wr_bio_end_io(struct bio *bio) |
ff023aac SB |
1734 | { |
1735 | struct scrub_bio *sbio = bio->bi_private; | |
fb456252 | 1736 | struct btrfs_fs_info *fs_info = sbio->dev->fs_info; |
ff023aac | 1737 | |
4e4cbee9 | 1738 | sbio->status = bio->bi_status; |
ff023aac SB |
1739 | sbio->bio = bio; |
1740 | ||
a0cac0ec | 1741 | btrfs_init_work(&sbio->work, scrub_wr_bio_end_io_worker, NULL, NULL); |
0339ef2f | 1742 | btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work); |
ff023aac SB |
1743 | } |
1744 | ||
1745 | static void scrub_wr_bio_end_io_worker(struct btrfs_work *work) | |
1746 | { | |
1747 | struct scrub_bio *sbio = container_of(work, struct scrub_bio, work); | |
1748 | struct scrub_ctx *sctx = sbio->sctx; | |
1749 | int i; | |
1750 | ||
c9d328c0 | 1751 | ASSERT(sbio->page_count <= SCRUB_PAGES_PER_BIO); |
4e4cbee9 | 1752 | if (sbio->status) { |
ff023aac | 1753 | struct btrfs_dev_replace *dev_replace = |
fb456252 | 1754 | &sbio->sctx->fs_info->dev_replace; |
ff023aac SB |
1755 | |
1756 | for (i = 0; i < sbio->page_count; i++) { | |
1757 | struct scrub_page *spage = sbio->pagev[i]; | |
1758 | ||
1759 | spage->io_error = 1; | |
e37abe97 | 1760 | atomic64_inc(&dev_replace->num_write_errors); |
ff023aac SB |
1761 | } |
1762 | } | |
1763 | ||
1764 | for (i = 0; i < sbio->page_count; i++) | |
1765 | scrub_page_put(sbio->pagev[i]); | |
1766 | ||
1767 | bio_put(sbio->bio); | |
1768 | kfree(sbio); | |
1769 | scrub_pending_bio_dec(sctx); | |
1770 | } | |
1771 | ||
1772 | static int scrub_checksum(struct scrub_block *sblock) | |
b5d67f64 SB |
1773 | { |
1774 | u64 flags; | |
1775 | int ret; | |
1776 | ||
ba7cf988 ZL |
1777 | /* |
1778 | * No need to initialize these stats currently, | |
1779 | * because this function only use return value | |
1780 | * instead of these stats value. | |
1781 | * | |
1782 | * Todo: | |
1783 | * always use stats | |
1784 | */ | |
1785 | sblock->header_error = 0; | |
1786 | sblock->generation_error = 0; | |
1787 | sblock->checksum_error = 0; | |
1788 | ||
7a9e9987 SB |
1789 | WARN_ON(sblock->page_count < 1); |
1790 | flags = sblock->pagev[0]->flags; | |
b5d67f64 SB |
1791 | ret = 0; |
1792 | if (flags & BTRFS_EXTENT_FLAG_DATA) | |
1793 | ret = scrub_checksum_data(sblock); | |
1794 | else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) | |
1795 | ret = scrub_checksum_tree_block(sblock); | |
1796 | else if (flags & BTRFS_EXTENT_FLAG_SUPER) | |
1797 | (void)scrub_checksum_super(sblock); | |
1798 | else | |
1799 | WARN_ON(1); | |
1800 | if (ret) | |
1801 | scrub_handle_errored_block(sblock); | |
ff023aac SB |
1802 | |
1803 | return ret; | |
a2de733c AJ |
1804 | } |
1805 | ||
b5d67f64 | 1806 | static int scrub_checksum_data(struct scrub_block *sblock) |
a2de733c | 1807 | { |
d9d181c1 | 1808 | struct scrub_ctx *sctx = sblock->sctx; |
d5178578 JT |
1809 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
1810 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
a2de733c | 1811 | u8 csum[BTRFS_CSUM_SIZE]; |
d41ebef2 | 1812 | struct scrub_page *spage; |
b0485252 | 1813 | char *kaddr; |
a2de733c | 1814 | |
b5d67f64 | 1815 | BUG_ON(sblock->page_count < 1); |
d41ebef2 DS |
1816 | spage = sblock->pagev[0]; |
1817 | if (!spage->have_csum) | |
a2de733c AJ |
1818 | return 0; |
1819 | ||
d41ebef2 | 1820 | kaddr = page_address(spage->page); |
b5d67f64 | 1821 | |
771aba0d DS |
1822 | shash->tfm = fs_info->csum_shash; |
1823 | crypto_shash_init(shash); | |
b5d67f64 | 1824 | |
b29dca44 QW |
1825 | /* |
1826 | * In scrub_pages() and scrub_pages_for_parity() we ensure each spage | |
1827 | * only contains one sector of data. | |
1828 | */ | |
1829 | crypto_shash_digest(shash, kaddr, fs_info->sectorsize, csum); | |
a2de733c | 1830 | |
b29dca44 QW |
1831 | if (memcmp(csum, spage->csum, fs_info->csum_size)) |
1832 | sblock->checksum_error = 1; | |
ba7cf988 | 1833 | return sblock->checksum_error; |
a2de733c AJ |
1834 | } |
1835 | ||
b5d67f64 | 1836 | static int scrub_checksum_tree_block(struct scrub_block *sblock) |
a2de733c | 1837 | { |
d9d181c1 | 1838 | struct scrub_ctx *sctx = sblock->sctx; |
a2de733c | 1839 | struct btrfs_header *h; |
0b246afa | 1840 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
d5178578 | 1841 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
b5d67f64 SB |
1842 | u8 calculated_csum[BTRFS_CSUM_SIZE]; |
1843 | u8 on_disk_csum[BTRFS_CSUM_SIZE]; | |
53f3251d QW |
1844 | /* |
1845 | * This is done in sectorsize steps even for metadata as there's a | |
1846 | * constraint for nodesize to be aligned to sectorsize. This will need | |
1847 | * to change so we don't misuse data and metadata units like that. | |
1848 | */ | |
1849 | const u32 sectorsize = sctx->fs_info->sectorsize; | |
1850 | const int num_sectors = fs_info->nodesize >> fs_info->sectorsize_bits; | |
521e1022 | 1851 | int i; |
100aa5d9 | 1852 | struct scrub_page *spage; |
b0485252 | 1853 | char *kaddr; |
d5178578 | 1854 | |
b5d67f64 | 1855 | BUG_ON(sblock->page_count < 1); |
53f3251d QW |
1856 | |
1857 | /* Each member in pagev is just one block, not a full page */ | |
1858 | ASSERT(sblock->page_count == num_sectors); | |
1859 | ||
100aa5d9 DS |
1860 | spage = sblock->pagev[0]; |
1861 | kaddr = page_address(spage->page); | |
b0485252 | 1862 | h = (struct btrfs_header *)kaddr; |
2ae0c2d8 | 1863 | memcpy(on_disk_csum, h->csum, sctx->fs_info->csum_size); |
a2de733c AJ |
1864 | |
1865 | /* | |
1866 | * we don't use the getter functions here, as we | |
1867 | * a) don't have an extent buffer and | |
1868 | * b) the page is already kmapped | |
1869 | */ | |
100aa5d9 | 1870 | if (spage->logical != btrfs_stack_header_bytenr(h)) |
ba7cf988 | 1871 | sblock->header_error = 1; |
a2de733c | 1872 | |
100aa5d9 | 1873 | if (spage->generation != btrfs_stack_header_generation(h)) { |
ba7cf988 ZL |
1874 | sblock->header_error = 1; |
1875 | sblock->generation_error = 1; | |
1876 | } | |
a2de733c | 1877 | |
100aa5d9 | 1878 | if (!scrub_check_fsid(h->fsid, spage)) |
ba7cf988 | 1879 | sblock->header_error = 1; |
a2de733c AJ |
1880 | |
1881 | if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid, | |
1882 | BTRFS_UUID_SIZE)) | |
ba7cf988 | 1883 | sblock->header_error = 1; |
a2de733c | 1884 | |
521e1022 DS |
1885 | shash->tfm = fs_info->csum_shash; |
1886 | crypto_shash_init(shash); | |
1887 | crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE, | |
53f3251d | 1888 | sectorsize - BTRFS_CSUM_SIZE); |
b5d67f64 | 1889 | |
53f3251d | 1890 | for (i = 1; i < num_sectors; i++) { |
521e1022 | 1891 | kaddr = page_address(sblock->pagev[i]->page); |
53f3251d | 1892 | crypto_shash_update(shash, kaddr, sectorsize); |
b5d67f64 SB |
1893 | } |
1894 | ||
d5178578 | 1895 | crypto_shash_final(shash, calculated_csum); |
2ae0c2d8 | 1896 | if (memcmp(calculated_csum, on_disk_csum, sctx->fs_info->csum_size)) |
ba7cf988 | 1897 | sblock->checksum_error = 1; |
a2de733c | 1898 | |
ba7cf988 | 1899 | return sblock->header_error || sblock->checksum_error; |
a2de733c AJ |
1900 | } |
1901 | ||
b5d67f64 | 1902 | static int scrub_checksum_super(struct scrub_block *sblock) |
a2de733c AJ |
1903 | { |
1904 | struct btrfs_super_block *s; | |
d9d181c1 | 1905 | struct scrub_ctx *sctx = sblock->sctx; |
d5178578 JT |
1906 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
1907 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); | |
b5d67f64 | 1908 | u8 calculated_csum[BTRFS_CSUM_SIZE]; |
c7460541 | 1909 | struct scrub_page *spage; |
b0485252 | 1910 | char *kaddr; |
442a4f63 SB |
1911 | int fail_gen = 0; |
1912 | int fail_cor = 0; | |
d5178578 | 1913 | |
b5d67f64 | 1914 | BUG_ON(sblock->page_count < 1); |
c7460541 DS |
1915 | spage = sblock->pagev[0]; |
1916 | kaddr = page_address(spage->page); | |
b0485252 | 1917 | s = (struct btrfs_super_block *)kaddr; |
a2de733c | 1918 | |
c7460541 | 1919 | if (spage->logical != btrfs_super_bytenr(s)) |
442a4f63 | 1920 | ++fail_cor; |
a2de733c | 1921 | |
c7460541 | 1922 | if (spage->generation != btrfs_super_generation(s)) |
442a4f63 | 1923 | ++fail_gen; |
a2de733c | 1924 | |
c7460541 | 1925 | if (!scrub_check_fsid(s->fsid, spage)) |
442a4f63 | 1926 | ++fail_cor; |
a2de733c | 1927 | |
83cf6d5e DS |
1928 | shash->tfm = fs_info->csum_shash; |
1929 | crypto_shash_init(shash); | |
1930 | crypto_shash_digest(shash, kaddr + BTRFS_CSUM_SIZE, | |
1931 | BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, calculated_csum); | |
b5d67f64 | 1932 | |
2ae0c2d8 | 1933 | if (memcmp(calculated_csum, s->csum, sctx->fs_info->csum_size)) |
442a4f63 | 1934 | ++fail_cor; |
a2de733c | 1935 | |
442a4f63 | 1936 | if (fail_cor + fail_gen) { |
a2de733c AJ |
1937 | /* |
1938 | * if we find an error in a super block, we just report it. | |
1939 | * They will get written with the next transaction commit | |
1940 | * anyway | |
1941 | */ | |
d9d181c1 SB |
1942 | spin_lock(&sctx->stat_lock); |
1943 | ++sctx->stat.super_errors; | |
1944 | spin_unlock(&sctx->stat_lock); | |
442a4f63 | 1945 | if (fail_cor) |
c7460541 | 1946 | btrfs_dev_stat_inc_and_print(spage->dev, |
442a4f63 SB |
1947 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
1948 | else | |
c7460541 | 1949 | btrfs_dev_stat_inc_and_print(spage->dev, |
442a4f63 | 1950 | BTRFS_DEV_STAT_GENERATION_ERRS); |
a2de733c AJ |
1951 | } |
1952 | ||
442a4f63 | 1953 | return fail_cor + fail_gen; |
a2de733c AJ |
1954 | } |
1955 | ||
b5d67f64 SB |
1956 | static void scrub_block_get(struct scrub_block *sblock) |
1957 | { | |
186debd6 | 1958 | refcount_inc(&sblock->refs); |
b5d67f64 SB |
1959 | } |
1960 | ||
1961 | static void scrub_block_put(struct scrub_block *sblock) | |
1962 | { | |
186debd6 | 1963 | if (refcount_dec_and_test(&sblock->refs)) { |
b5d67f64 SB |
1964 | int i; |
1965 | ||
5a6ac9ea MX |
1966 | if (sblock->sparity) |
1967 | scrub_parity_put(sblock->sparity); | |
1968 | ||
b5d67f64 | 1969 | for (i = 0; i < sblock->page_count; i++) |
7a9e9987 | 1970 | scrub_page_put(sblock->pagev[i]); |
b5d67f64 SB |
1971 | kfree(sblock); |
1972 | } | |
1973 | } | |
1974 | ||
7a9e9987 SB |
1975 | static void scrub_page_get(struct scrub_page *spage) |
1976 | { | |
57019345 | 1977 | atomic_inc(&spage->refs); |
7a9e9987 SB |
1978 | } |
1979 | ||
1980 | static void scrub_page_put(struct scrub_page *spage) | |
1981 | { | |
57019345 | 1982 | if (atomic_dec_and_test(&spage->refs)) { |
7a9e9987 SB |
1983 | if (spage->page) |
1984 | __free_page(spage->page); | |
1985 | kfree(spage); | |
1986 | } | |
1987 | } | |
1988 | ||
eb3b5053 DS |
1989 | /* |
1990 | * Throttling of IO submission, bandwidth-limit based, the timeslice is 1 | |
1991 | * second. Limit can be set via /sys/fs/UUID/devinfo/devid/scrub_speed_max. | |
1992 | */ | |
1993 | static void scrub_throttle(struct scrub_ctx *sctx) | |
1994 | { | |
1995 | const int time_slice = 1000; | |
1996 | struct scrub_bio *sbio; | |
1997 | struct btrfs_device *device; | |
1998 | s64 delta; | |
1999 | ktime_t now; | |
2000 | u32 div; | |
2001 | u64 bwlimit; | |
2002 | ||
2003 | sbio = sctx->bios[sctx->curr]; | |
2004 | device = sbio->dev; | |
2005 | bwlimit = READ_ONCE(device->scrub_speed_max); | |
2006 | if (bwlimit == 0) | |
2007 | return; | |
2008 | ||
2009 | /* | |
2010 | * Slice is divided into intervals when the IO is submitted, adjust by | |
2011 | * bwlimit and maximum of 64 intervals. | |
2012 | */ | |
2013 | div = max_t(u32, 1, (u32)(bwlimit / (16 * 1024 * 1024))); | |
2014 | div = min_t(u32, 64, div); | |
2015 | ||
2016 | /* Start new epoch, set deadline */ | |
2017 | now = ktime_get(); | |
2018 | if (sctx->throttle_deadline == 0) { | |
2019 | sctx->throttle_deadline = ktime_add_ms(now, time_slice / div); | |
2020 | sctx->throttle_sent = 0; | |
2021 | } | |
2022 | ||
2023 | /* Still in the time to send? */ | |
2024 | if (ktime_before(now, sctx->throttle_deadline)) { | |
2025 | /* If current bio is within the limit, send it */ | |
2026 | sctx->throttle_sent += sbio->bio->bi_iter.bi_size; | |
2027 | if (sctx->throttle_sent <= div_u64(bwlimit, div)) | |
2028 | return; | |
2029 | ||
2030 | /* We're over the limit, sleep until the rest of the slice */ | |
2031 | delta = ktime_ms_delta(sctx->throttle_deadline, now); | |
2032 | } else { | |
2033 | /* New request after deadline, start new epoch */ | |
2034 | delta = 0; | |
2035 | } | |
2036 | ||
2037 | if (delta) { | |
2038 | long timeout; | |
2039 | ||
2040 | timeout = div_u64(delta * HZ, 1000); | |
2041 | schedule_timeout_interruptible(timeout); | |
2042 | } | |
2043 | ||
2044 | /* Next call will start the deadline period */ | |
2045 | sctx->throttle_deadline = 0; | |
2046 | } | |
2047 | ||
d9d181c1 | 2048 | static void scrub_submit(struct scrub_ctx *sctx) |
a2de733c AJ |
2049 | { |
2050 | struct scrub_bio *sbio; | |
2051 | ||
d9d181c1 | 2052 | if (sctx->curr == -1) |
1623edeb | 2053 | return; |
a2de733c | 2054 | |
eb3b5053 DS |
2055 | scrub_throttle(sctx); |
2056 | ||
d9d181c1 SB |
2057 | sbio = sctx->bios[sctx->curr]; |
2058 | sctx->curr = -1; | |
b6bfebc1 | 2059 | scrub_pending_bio_inc(sctx); |
4e49ea4a | 2060 | btrfsic_submit_bio(sbio->bio); |
a2de733c AJ |
2061 | } |
2062 | ||
ff023aac SB |
2063 | static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx, |
2064 | struct scrub_page *spage) | |
a2de733c | 2065 | { |
b5d67f64 | 2066 | struct scrub_block *sblock = spage->sblock; |
a2de733c | 2067 | struct scrub_bio *sbio; |
8df507cb | 2068 | const u32 sectorsize = sctx->fs_info->sectorsize; |
69f4cb52 | 2069 | int ret; |
a2de733c AJ |
2070 | |
2071 | again: | |
2072 | /* | |
2073 | * grab a fresh bio or wait for one to become available | |
2074 | */ | |
d9d181c1 SB |
2075 | while (sctx->curr == -1) { |
2076 | spin_lock(&sctx->list_lock); | |
2077 | sctx->curr = sctx->first_free; | |
2078 | if (sctx->curr != -1) { | |
2079 | sctx->first_free = sctx->bios[sctx->curr]->next_free; | |
2080 | sctx->bios[sctx->curr]->next_free = -1; | |
2081 | sctx->bios[sctx->curr]->page_count = 0; | |
2082 | spin_unlock(&sctx->list_lock); | |
a2de733c | 2083 | } else { |
d9d181c1 SB |
2084 | spin_unlock(&sctx->list_lock); |
2085 | wait_event(sctx->list_wait, sctx->first_free != -1); | |
a2de733c AJ |
2086 | } |
2087 | } | |
d9d181c1 | 2088 | sbio = sctx->bios[sctx->curr]; |
b5d67f64 | 2089 | if (sbio->page_count == 0) { |
69f4cb52 AJ |
2090 | struct bio *bio; |
2091 | ||
b5d67f64 SB |
2092 | sbio->physical = spage->physical; |
2093 | sbio->logical = spage->logical; | |
a36cf8b8 | 2094 | sbio->dev = spage->dev; |
b5d67f64 SB |
2095 | bio = sbio->bio; |
2096 | if (!bio) { | |
c9d328c0 | 2097 | bio = btrfs_bio_alloc(sctx->pages_per_bio); |
b5d67f64 SB |
2098 | sbio->bio = bio; |
2099 | } | |
69f4cb52 AJ |
2100 | |
2101 | bio->bi_private = sbio; | |
2102 | bio->bi_end_io = scrub_bio_end_io; | |
74d46992 | 2103 | bio_set_dev(bio, sbio->dev->bdev); |
4f024f37 | 2104 | bio->bi_iter.bi_sector = sbio->physical >> 9; |
ebcc3263 | 2105 | bio->bi_opf = REQ_OP_READ; |
4e4cbee9 | 2106 | sbio->status = 0; |
8df507cb | 2107 | } else if (sbio->physical + sbio->page_count * sectorsize != |
b5d67f64 | 2108 | spage->physical || |
8df507cb | 2109 | sbio->logical + sbio->page_count * sectorsize != |
a36cf8b8 SB |
2110 | spage->logical || |
2111 | sbio->dev != spage->dev) { | |
d9d181c1 | 2112 | scrub_submit(sctx); |
a2de733c AJ |
2113 | goto again; |
2114 | } | |
69f4cb52 | 2115 | |
b5d67f64 | 2116 | sbio->pagev[sbio->page_count] = spage; |
8df507cb QW |
2117 | ret = bio_add_page(sbio->bio, spage->page, sectorsize, 0); |
2118 | if (ret != sectorsize) { | |
b5d67f64 SB |
2119 | if (sbio->page_count < 1) { |
2120 | bio_put(sbio->bio); | |
2121 | sbio->bio = NULL; | |
2122 | return -EIO; | |
2123 | } | |
d9d181c1 | 2124 | scrub_submit(sctx); |
69f4cb52 AJ |
2125 | goto again; |
2126 | } | |
2127 | ||
ff023aac | 2128 | scrub_block_get(sblock); /* one for the page added to the bio */ |
b5d67f64 SB |
2129 | atomic_inc(&sblock->outstanding_pages); |
2130 | sbio->page_count++; | |
c9d328c0 | 2131 | if (sbio->page_count == sctx->pages_per_bio) |
d9d181c1 | 2132 | scrub_submit(sctx); |
b5d67f64 SB |
2133 | |
2134 | return 0; | |
2135 | } | |
2136 | ||
22365979 | 2137 | static void scrub_missing_raid56_end_io(struct bio *bio) |
73ff61db OS |
2138 | { |
2139 | struct scrub_block *sblock = bio->bi_private; | |
fb456252 | 2140 | struct btrfs_fs_info *fs_info = sblock->sctx->fs_info; |
73ff61db | 2141 | |
4e4cbee9 | 2142 | if (bio->bi_status) |
73ff61db OS |
2143 | sblock->no_io_error_seen = 0; |
2144 | ||
4673272f ST |
2145 | bio_put(bio); |
2146 | ||
73ff61db OS |
2147 | btrfs_queue_work(fs_info->scrub_workers, &sblock->work); |
2148 | } | |
2149 | ||
2150 | static void scrub_missing_raid56_worker(struct btrfs_work *work) | |
2151 | { | |
2152 | struct scrub_block *sblock = container_of(work, struct scrub_block, work); | |
2153 | struct scrub_ctx *sctx = sblock->sctx; | |
0b246afa | 2154 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
73ff61db OS |
2155 | u64 logical; |
2156 | struct btrfs_device *dev; | |
2157 | ||
73ff61db OS |
2158 | logical = sblock->pagev[0]->logical; |
2159 | dev = sblock->pagev[0]->dev; | |
2160 | ||
affe4a5a | 2161 | if (sblock->no_io_error_seen) |
ba7cf988 | 2162 | scrub_recheck_block_checksum(sblock); |
73ff61db OS |
2163 | |
2164 | if (!sblock->no_io_error_seen) { | |
2165 | spin_lock(&sctx->stat_lock); | |
2166 | sctx->stat.read_errors++; | |
2167 | spin_unlock(&sctx->stat_lock); | |
0b246afa | 2168 | btrfs_err_rl_in_rcu(fs_info, |
b14af3b4 | 2169 | "IO error rebuilding logical %llu for dev %s", |
73ff61db OS |
2170 | logical, rcu_str_deref(dev->name)); |
2171 | } else if (sblock->header_error || sblock->checksum_error) { | |
2172 | spin_lock(&sctx->stat_lock); | |
2173 | sctx->stat.uncorrectable_errors++; | |
2174 | spin_unlock(&sctx->stat_lock); | |
0b246afa | 2175 | btrfs_err_rl_in_rcu(fs_info, |
b14af3b4 | 2176 | "failed to rebuild valid logical %llu for dev %s", |
73ff61db OS |
2177 | logical, rcu_str_deref(dev->name)); |
2178 | } else { | |
2179 | scrub_write_block_to_dev_replace(sblock); | |
2180 | } | |
2181 | ||
2073c4c2 | 2182 | if (sctx->is_dev_replace && sctx->flush_all_writes) { |
3fb99303 | 2183 | mutex_lock(&sctx->wr_lock); |
73ff61db | 2184 | scrub_wr_submit(sctx); |
3fb99303 | 2185 | mutex_unlock(&sctx->wr_lock); |
73ff61db OS |
2186 | } |
2187 | ||
57d4f0b8 | 2188 | scrub_block_put(sblock); |
73ff61db OS |
2189 | scrub_pending_bio_dec(sctx); |
2190 | } | |
2191 | ||
2192 | static void scrub_missing_raid56_pages(struct scrub_block *sblock) | |
2193 | { | |
2194 | struct scrub_ctx *sctx = sblock->sctx; | |
fb456252 | 2195 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
73ff61db OS |
2196 | u64 length = sblock->page_count * PAGE_SIZE; |
2197 | u64 logical = sblock->pagev[0]->logical; | |
4c664611 | 2198 | struct btrfs_io_context *bioc = NULL; |
73ff61db OS |
2199 | struct bio *bio; |
2200 | struct btrfs_raid_bio *rbio; | |
2201 | int ret; | |
2202 | int i; | |
2203 | ||
ae6529c3 | 2204 | btrfs_bio_counter_inc_blocked(fs_info); |
cf8cddd3 | 2205 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical, |
4c664611 QW |
2206 | &length, &bioc); |
2207 | if (ret || !bioc || !bioc->raid_map) | |
2208 | goto bioc_out; | |
73ff61db OS |
2209 | |
2210 | if (WARN_ON(!sctx->is_dev_replace || | |
4c664611 | 2211 | !(bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) { |
73ff61db OS |
2212 | /* |
2213 | * We shouldn't be scrubbing a missing device. Even for dev | |
2214 | * replace, we should only get here for RAID 5/6. We either | |
2215 | * managed to mount something with no mirrors remaining or | |
2216 | * there's a bug in scrub_remap_extent()/btrfs_map_block(). | |
2217 | */ | |
4c664611 | 2218 | goto bioc_out; |
73ff61db OS |
2219 | } |
2220 | ||
c3a3b19b | 2221 | bio = btrfs_bio_alloc(BIO_MAX_VECS); |
73ff61db OS |
2222 | bio->bi_iter.bi_sector = logical >> 9; |
2223 | bio->bi_private = sblock; | |
2224 | bio->bi_end_io = scrub_missing_raid56_end_io; | |
2225 | ||
6a258d72 | 2226 | rbio = raid56_alloc_missing_rbio(bio, bioc, length); |
73ff61db OS |
2227 | if (!rbio) |
2228 | goto rbio_out; | |
2229 | ||
2230 | for (i = 0; i < sblock->page_count; i++) { | |
2231 | struct scrub_page *spage = sblock->pagev[i]; | |
2232 | ||
2233 | raid56_add_scrub_pages(rbio, spage->page, spage->logical); | |
2234 | } | |
2235 | ||
a0cac0ec | 2236 | btrfs_init_work(&sblock->work, scrub_missing_raid56_worker, NULL, NULL); |
73ff61db OS |
2237 | scrub_block_get(sblock); |
2238 | scrub_pending_bio_inc(sctx); | |
2239 | raid56_submit_missing_rbio(rbio); | |
2240 | return; | |
2241 | ||
2242 | rbio_out: | |
2243 | bio_put(bio); | |
4c664611 | 2244 | bioc_out: |
ae6529c3 | 2245 | btrfs_bio_counter_dec(fs_info); |
4c664611 | 2246 | btrfs_put_bioc(bioc); |
73ff61db OS |
2247 | spin_lock(&sctx->stat_lock); |
2248 | sctx->stat.malloc_errors++; | |
2249 | spin_unlock(&sctx->stat_lock); | |
2250 | } | |
2251 | ||
fa485d21 | 2252 | static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u32 len, |
a36cf8b8 | 2253 | u64 physical, struct btrfs_device *dev, u64 flags, |
96e63a45 | 2254 | u64 gen, int mirror_num, u8 *csum, |
ff023aac | 2255 | u64 physical_for_dev_replace) |
b5d67f64 SB |
2256 | { |
2257 | struct scrub_block *sblock; | |
d0a7a9c0 | 2258 | const u32 sectorsize = sctx->fs_info->sectorsize; |
b5d67f64 SB |
2259 | int index; |
2260 | ||
58c4e173 | 2261 | sblock = kzalloc(sizeof(*sblock), GFP_KERNEL); |
b5d67f64 | 2262 | if (!sblock) { |
d9d181c1 SB |
2263 | spin_lock(&sctx->stat_lock); |
2264 | sctx->stat.malloc_errors++; | |
2265 | spin_unlock(&sctx->stat_lock); | |
b5d67f64 | 2266 | return -ENOMEM; |
a2de733c | 2267 | } |
b5d67f64 | 2268 | |
7a9e9987 SB |
2269 | /* one ref inside this function, plus one for each page added to |
2270 | * a bio later on */ | |
186debd6 | 2271 | refcount_set(&sblock->refs, 1); |
d9d181c1 | 2272 | sblock->sctx = sctx; |
b5d67f64 SB |
2273 | sblock->no_io_error_seen = 1; |
2274 | ||
2275 | for (index = 0; len > 0; index++) { | |
7a9e9987 | 2276 | struct scrub_page *spage; |
d0a7a9c0 QW |
2277 | /* |
2278 | * Here we will allocate one page for one sector to scrub. | |
2279 | * This is fine if PAGE_SIZE == sectorsize, but will cost | |
2280 | * more memory for PAGE_SIZE > sectorsize case. | |
2281 | */ | |
2282 | u32 l = min(sectorsize, len); | |
b5d67f64 | 2283 | |
58c4e173 | 2284 | spage = kzalloc(sizeof(*spage), GFP_KERNEL); |
7a9e9987 SB |
2285 | if (!spage) { |
2286 | leave_nomem: | |
d9d181c1 SB |
2287 | spin_lock(&sctx->stat_lock); |
2288 | sctx->stat.malloc_errors++; | |
2289 | spin_unlock(&sctx->stat_lock); | |
7a9e9987 | 2290 | scrub_block_put(sblock); |
b5d67f64 SB |
2291 | return -ENOMEM; |
2292 | } | |
0bb3acdc | 2293 | ASSERT(index < SCRUB_MAX_PAGES_PER_BLOCK); |
7a9e9987 SB |
2294 | scrub_page_get(spage); |
2295 | sblock->pagev[index] = spage; | |
b5d67f64 | 2296 | spage->sblock = sblock; |
a36cf8b8 | 2297 | spage->dev = dev; |
b5d67f64 SB |
2298 | spage->flags = flags; |
2299 | spage->generation = gen; | |
2300 | spage->logical = logical; | |
2301 | spage->physical = physical; | |
ff023aac | 2302 | spage->physical_for_dev_replace = physical_for_dev_replace; |
b5d67f64 SB |
2303 | spage->mirror_num = mirror_num; |
2304 | if (csum) { | |
2305 | spage->have_csum = 1; | |
2ae0c2d8 | 2306 | memcpy(spage->csum, csum, sctx->fs_info->csum_size); |
b5d67f64 SB |
2307 | } else { |
2308 | spage->have_csum = 0; | |
2309 | } | |
2310 | sblock->page_count++; | |
58c4e173 | 2311 | spage->page = alloc_page(GFP_KERNEL); |
7a9e9987 SB |
2312 | if (!spage->page) |
2313 | goto leave_nomem; | |
b5d67f64 SB |
2314 | len -= l; |
2315 | logical += l; | |
2316 | physical += l; | |
ff023aac | 2317 | physical_for_dev_replace += l; |
b5d67f64 SB |
2318 | } |
2319 | ||
7a9e9987 | 2320 | WARN_ON(sblock->page_count == 0); |
e6e674bd | 2321 | if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) { |
73ff61db OS |
2322 | /* |
2323 | * This case should only be hit for RAID 5/6 device replace. See | |
2324 | * the comment in scrub_missing_raid56_pages() for details. | |
2325 | */ | |
2326 | scrub_missing_raid56_pages(sblock); | |
2327 | } else { | |
2328 | for (index = 0; index < sblock->page_count; index++) { | |
2329 | struct scrub_page *spage = sblock->pagev[index]; | |
2330 | int ret; | |
1bc87793 | 2331 | |
73ff61db OS |
2332 | ret = scrub_add_page_to_rd_bio(sctx, spage); |
2333 | if (ret) { | |
2334 | scrub_block_put(sblock); | |
2335 | return ret; | |
2336 | } | |
b5d67f64 | 2337 | } |
a2de733c | 2338 | |
96e63a45 | 2339 | if (flags & BTRFS_EXTENT_FLAG_SUPER) |
73ff61db OS |
2340 | scrub_submit(sctx); |
2341 | } | |
a2de733c | 2342 | |
b5d67f64 SB |
2343 | /* last one frees, either here or in bio completion for last page */ |
2344 | scrub_block_put(sblock); | |
a2de733c AJ |
2345 | return 0; |
2346 | } | |
2347 | ||
4246a0b6 | 2348 | static void scrub_bio_end_io(struct bio *bio) |
b5d67f64 SB |
2349 | { |
2350 | struct scrub_bio *sbio = bio->bi_private; | |
fb456252 | 2351 | struct btrfs_fs_info *fs_info = sbio->dev->fs_info; |
b5d67f64 | 2352 | |
4e4cbee9 | 2353 | sbio->status = bio->bi_status; |
b5d67f64 SB |
2354 | sbio->bio = bio; |
2355 | ||
0339ef2f | 2356 | btrfs_queue_work(fs_info->scrub_workers, &sbio->work); |
b5d67f64 SB |
2357 | } |
2358 | ||
2359 | static void scrub_bio_end_io_worker(struct btrfs_work *work) | |
2360 | { | |
2361 | struct scrub_bio *sbio = container_of(work, struct scrub_bio, work); | |
d9d181c1 | 2362 | struct scrub_ctx *sctx = sbio->sctx; |
b5d67f64 SB |
2363 | int i; |
2364 | ||
c9d328c0 | 2365 | ASSERT(sbio->page_count <= SCRUB_PAGES_PER_BIO); |
4e4cbee9 | 2366 | if (sbio->status) { |
b5d67f64 SB |
2367 | for (i = 0; i < sbio->page_count; i++) { |
2368 | struct scrub_page *spage = sbio->pagev[i]; | |
2369 | ||
2370 | spage->io_error = 1; | |
2371 | spage->sblock->no_io_error_seen = 0; | |
2372 | } | |
2373 | } | |
2374 | ||
2375 | /* now complete the scrub_block items that have all pages completed */ | |
2376 | for (i = 0; i < sbio->page_count; i++) { | |
2377 | struct scrub_page *spage = sbio->pagev[i]; | |
2378 | struct scrub_block *sblock = spage->sblock; | |
2379 | ||
2380 | if (atomic_dec_and_test(&sblock->outstanding_pages)) | |
2381 | scrub_block_complete(sblock); | |
2382 | scrub_block_put(sblock); | |
2383 | } | |
2384 | ||
b5d67f64 SB |
2385 | bio_put(sbio->bio); |
2386 | sbio->bio = NULL; | |
d9d181c1 SB |
2387 | spin_lock(&sctx->list_lock); |
2388 | sbio->next_free = sctx->first_free; | |
2389 | sctx->first_free = sbio->index; | |
2390 | spin_unlock(&sctx->list_lock); | |
ff023aac | 2391 | |
2073c4c2 | 2392 | if (sctx->is_dev_replace && sctx->flush_all_writes) { |
3fb99303 | 2393 | mutex_lock(&sctx->wr_lock); |
ff023aac | 2394 | scrub_wr_submit(sctx); |
3fb99303 | 2395 | mutex_unlock(&sctx->wr_lock); |
ff023aac SB |
2396 | } |
2397 | ||
b6bfebc1 | 2398 | scrub_pending_bio_dec(sctx); |
b5d67f64 SB |
2399 | } |
2400 | ||
5a6ac9ea MX |
2401 | static inline void __scrub_mark_bitmap(struct scrub_parity *sparity, |
2402 | unsigned long *bitmap, | |
fa485d21 | 2403 | u64 start, u32 len) |
5a6ac9ea | 2404 | { |
972d7219 | 2405 | u64 offset; |
7736b0a4 | 2406 | u32 nsectors; |
ab108d99 | 2407 | u32 sectorsize_bits = sparity->sctx->fs_info->sectorsize_bits; |
5a6ac9ea MX |
2408 | |
2409 | if (len >= sparity->stripe_len) { | |
2410 | bitmap_set(bitmap, 0, sparity->nsectors); | |
2411 | return; | |
2412 | } | |
2413 | ||
2414 | start -= sparity->logic_start; | |
972d7219 | 2415 | start = div64_u64_rem(start, sparity->stripe_len, &offset); |
ab108d99 | 2416 | offset = offset >> sectorsize_bits; |
fa485d21 | 2417 | nsectors = len >> sectorsize_bits; |
5a6ac9ea MX |
2418 | |
2419 | if (offset + nsectors <= sparity->nsectors) { | |
2420 | bitmap_set(bitmap, offset, nsectors); | |
2421 | return; | |
2422 | } | |
2423 | ||
2424 | bitmap_set(bitmap, offset, sparity->nsectors - offset); | |
2425 | bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset)); | |
2426 | } | |
2427 | ||
2428 | static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity, | |
fa485d21 | 2429 | u64 start, u32 len) |
5a6ac9ea MX |
2430 | { |
2431 | __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len); | |
2432 | } | |
2433 | ||
2434 | static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity, | |
fa485d21 | 2435 | u64 start, u32 len) |
5a6ac9ea MX |
2436 | { |
2437 | __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len); | |
2438 | } | |
2439 | ||
b5d67f64 SB |
2440 | static void scrub_block_complete(struct scrub_block *sblock) |
2441 | { | |
5a6ac9ea MX |
2442 | int corrupted = 0; |
2443 | ||
ff023aac | 2444 | if (!sblock->no_io_error_seen) { |
5a6ac9ea | 2445 | corrupted = 1; |
b5d67f64 | 2446 | scrub_handle_errored_block(sblock); |
ff023aac SB |
2447 | } else { |
2448 | /* | |
2449 | * if has checksum error, write via repair mechanism in | |
2450 | * dev replace case, otherwise write here in dev replace | |
2451 | * case. | |
2452 | */ | |
5a6ac9ea MX |
2453 | corrupted = scrub_checksum(sblock); |
2454 | if (!corrupted && sblock->sctx->is_dev_replace) | |
ff023aac SB |
2455 | scrub_write_block_to_dev_replace(sblock); |
2456 | } | |
5a6ac9ea MX |
2457 | |
2458 | if (sblock->sparity && corrupted && !sblock->data_corrected) { | |
2459 | u64 start = sblock->pagev[0]->logical; | |
2460 | u64 end = sblock->pagev[sblock->page_count - 1]->logical + | |
8df507cb | 2461 | sblock->sctx->fs_info->sectorsize; |
5a6ac9ea | 2462 | |
fa485d21 | 2463 | ASSERT(end - start <= U32_MAX); |
5a6ac9ea MX |
2464 | scrub_parity_mark_sectors_error(sblock->sparity, |
2465 | start, end - start); | |
2466 | } | |
b5d67f64 SB |
2467 | } |
2468 | ||
480a8ec8 QW |
2469 | static void drop_csum_range(struct scrub_ctx *sctx, struct btrfs_ordered_sum *sum) |
2470 | { | |
2471 | sctx->stat.csum_discards += sum->len >> sctx->fs_info->sectorsize_bits; | |
2472 | list_del(&sum->list); | |
2473 | kfree(sum); | |
2474 | } | |
2475 | ||
2476 | /* | |
2477 | * Find the desired csum for range [logical, logical + sectorsize), and store | |
2478 | * the csum into @csum. | |
2479 | * | |
2480 | * The search source is sctx->csum_list, which is a pre-populated list | |
1a9fd417 | 2481 | * storing bytenr ordered csum ranges. We're responsible to cleanup any range |
480a8ec8 QW |
2482 | * that is before @logical. |
2483 | * | |
2484 | * Return 0 if there is no csum for the range. | |
2485 | * Return 1 if there is csum for the range and copied to @csum. | |
2486 | */ | |
3b5753ec | 2487 | static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum) |
a2de733c | 2488 | { |
480a8ec8 | 2489 | bool found = false; |
a2de733c | 2490 | |
d9d181c1 | 2491 | while (!list_empty(&sctx->csum_list)) { |
480a8ec8 QW |
2492 | struct btrfs_ordered_sum *sum = NULL; |
2493 | unsigned long index; | |
2494 | unsigned long num_sectors; | |
2495 | ||
d9d181c1 | 2496 | sum = list_first_entry(&sctx->csum_list, |
a2de733c | 2497 | struct btrfs_ordered_sum, list); |
480a8ec8 | 2498 | /* The current csum range is beyond our range, no csum found */ |
a2de733c | 2499 | if (sum->bytenr > logical) |
a2de733c AJ |
2500 | break; |
2501 | ||
480a8ec8 QW |
2502 | /* |
2503 | * The current sum is before our bytenr, since scrub is always | |
2504 | * done in bytenr order, the csum will never be used anymore, | |
2505 | * clean it up so that later calls won't bother with the range, | |
2506 | * and continue search the next range. | |
2507 | */ | |
2508 | if (sum->bytenr + sum->len <= logical) { | |
2509 | drop_csum_range(sctx, sum); | |
2510 | continue; | |
2511 | } | |
a2de733c | 2512 | |
480a8ec8 QW |
2513 | /* Now the csum range covers our bytenr, copy the csum */ |
2514 | found = true; | |
2515 | index = (logical - sum->bytenr) >> sctx->fs_info->sectorsize_bits; | |
2516 | num_sectors = sum->len >> sctx->fs_info->sectorsize_bits; | |
1d1bf92d | 2517 | |
480a8ec8 QW |
2518 | memcpy(csum, sum->sums + index * sctx->fs_info->csum_size, |
2519 | sctx->fs_info->csum_size); | |
2520 | ||
2521 | /* Cleanup the range if we're at the end of the csum range */ | |
2522 | if (index == num_sectors - 1) | |
2523 | drop_csum_range(sctx, sum); | |
2524 | break; | |
a2de733c | 2525 | } |
480a8ec8 QW |
2526 | if (!found) |
2527 | return 0; | |
f51a4a18 | 2528 | return 1; |
a2de733c AJ |
2529 | } |
2530 | ||
2531 | /* scrub extent tries to collect up to 64 kB for each bio */ | |
6ca1765b | 2532 | static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map, |
fa485d21 | 2533 | u64 logical, u32 len, |
a36cf8b8 | 2534 | u64 physical, struct btrfs_device *dev, u64 flags, |
ff023aac | 2535 | u64 gen, int mirror_num, u64 physical_for_dev_replace) |
a2de733c AJ |
2536 | { |
2537 | int ret; | |
2538 | u8 csum[BTRFS_CSUM_SIZE]; | |
b5d67f64 SB |
2539 | u32 blocksize; |
2540 | ||
2541 | if (flags & BTRFS_EXTENT_FLAG_DATA) { | |
6ca1765b LB |
2542 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
2543 | blocksize = map->stripe_len; | |
2544 | else | |
2545 | blocksize = sctx->fs_info->sectorsize; | |
d9d181c1 SB |
2546 | spin_lock(&sctx->stat_lock); |
2547 | sctx->stat.data_extents_scrubbed++; | |
2548 | sctx->stat.data_bytes_scrubbed += len; | |
2549 | spin_unlock(&sctx->stat_lock); | |
b5d67f64 | 2550 | } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
6ca1765b LB |
2551 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
2552 | blocksize = map->stripe_len; | |
2553 | else | |
2554 | blocksize = sctx->fs_info->nodesize; | |
d9d181c1 SB |
2555 | spin_lock(&sctx->stat_lock); |
2556 | sctx->stat.tree_extents_scrubbed++; | |
2557 | sctx->stat.tree_bytes_scrubbed += len; | |
2558 | spin_unlock(&sctx->stat_lock); | |
b5d67f64 | 2559 | } else { |
25cc1226 | 2560 | blocksize = sctx->fs_info->sectorsize; |
ff023aac | 2561 | WARN_ON(1); |
b5d67f64 | 2562 | } |
a2de733c AJ |
2563 | |
2564 | while (len) { | |
fa485d21 | 2565 | u32 l = min(len, blocksize); |
a2de733c AJ |
2566 | int have_csum = 0; |
2567 | ||
2568 | if (flags & BTRFS_EXTENT_FLAG_DATA) { | |
2569 | /* push csums to sbio */ | |
3b5753ec | 2570 | have_csum = scrub_find_csum(sctx, logical, csum); |
a2de733c | 2571 | if (have_csum == 0) |
d9d181c1 | 2572 | ++sctx->stat.no_csum; |
a2de733c | 2573 | } |
a36cf8b8 | 2574 | ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen, |
96e63a45 | 2575 | mirror_num, have_csum ? csum : NULL, |
ff023aac | 2576 | physical_for_dev_replace); |
a2de733c AJ |
2577 | if (ret) |
2578 | return ret; | |
2579 | len -= l; | |
2580 | logical += l; | |
2581 | physical += l; | |
ff023aac | 2582 | physical_for_dev_replace += l; |
a2de733c AJ |
2583 | } |
2584 | return 0; | |
2585 | } | |
2586 | ||
5a6ac9ea | 2587 | static int scrub_pages_for_parity(struct scrub_parity *sparity, |
fa485d21 | 2588 | u64 logical, u32 len, |
5a6ac9ea MX |
2589 | u64 physical, struct btrfs_device *dev, |
2590 | u64 flags, u64 gen, int mirror_num, u8 *csum) | |
2591 | { | |
2592 | struct scrub_ctx *sctx = sparity->sctx; | |
2593 | struct scrub_block *sblock; | |
d0a7a9c0 | 2594 | const u32 sectorsize = sctx->fs_info->sectorsize; |
5a6ac9ea MX |
2595 | int index; |
2596 | ||
d0a7a9c0 QW |
2597 | ASSERT(IS_ALIGNED(len, sectorsize)); |
2598 | ||
58c4e173 | 2599 | sblock = kzalloc(sizeof(*sblock), GFP_KERNEL); |
5a6ac9ea MX |
2600 | if (!sblock) { |
2601 | spin_lock(&sctx->stat_lock); | |
2602 | sctx->stat.malloc_errors++; | |
2603 | spin_unlock(&sctx->stat_lock); | |
2604 | return -ENOMEM; | |
2605 | } | |
2606 | ||
2607 | /* one ref inside this function, plus one for each page added to | |
2608 | * a bio later on */ | |
186debd6 | 2609 | refcount_set(&sblock->refs, 1); |
5a6ac9ea MX |
2610 | sblock->sctx = sctx; |
2611 | sblock->no_io_error_seen = 1; | |
2612 | sblock->sparity = sparity; | |
2613 | scrub_parity_get(sparity); | |
2614 | ||
2615 | for (index = 0; len > 0; index++) { | |
2616 | struct scrub_page *spage; | |
5a6ac9ea | 2617 | |
58c4e173 | 2618 | spage = kzalloc(sizeof(*spage), GFP_KERNEL); |
5a6ac9ea MX |
2619 | if (!spage) { |
2620 | leave_nomem: | |
2621 | spin_lock(&sctx->stat_lock); | |
2622 | sctx->stat.malloc_errors++; | |
2623 | spin_unlock(&sctx->stat_lock); | |
2624 | scrub_block_put(sblock); | |
2625 | return -ENOMEM; | |
2626 | } | |
0bb3acdc | 2627 | ASSERT(index < SCRUB_MAX_PAGES_PER_BLOCK); |
5a6ac9ea MX |
2628 | /* For scrub block */ |
2629 | scrub_page_get(spage); | |
2630 | sblock->pagev[index] = spage; | |
2631 | /* For scrub parity */ | |
2632 | scrub_page_get(spage); | |
2633 | list_add_tail(&spage->list, &sparity->spages); | |
2634 | spage->sblock = sblock; | |
2635 | spage->dev = dev; | |
2636 | spage->flags = flags; | |
2637 | spage->generation = gen; | |
2638 | spage->logical = logical; | |
2639 | spage->physical = physical; | |
2640 | spage->mirror_num = mirror_num; | |
2641 | if (csum) { | |
2642 | spage->have_csum = 1; | |
2ae0c2d8 | 2643 | memcpy(spage->csum, csum, sctx->fs_info->csum_size); |
5a6ac9ea MX |
2644 | } else { |
2645 | spage->have_csum = 0; | |
2646 | } | |
2647 | sblock->page_count++; | |
58c4e173 | 2648 | spage->page = alloc_page(GFP_KERNEL); |
5a6ac9ea MX |
2649 | if (!spage->page) |
2650 | goto leave_nomem; | |
d0a7a9c0 QW |
2651 | |
2652 | ||
2653 | /* Iterate over the stripe range in sectorsize steps */ | |
2654 | len -= sectorsize; | |
2655 | logical += sectorsize; | |
2656 | physical += sectorsize; | |
5a6ac9ea MX |
2657 | } |
2658 | ||
2659 | WARN_ON(sblock->page_count == 0); | |
2660 | for (index = 0; index < sblock->page_count; index++) { | |
2661 | struct scrub_page *spage = sblock->pagev[index]; | |
2662 | int ret; | |
2663 | ||
2664 | ret = scrub_add_page_to_rd_bio(sctx, spage); | |
2665 | if (ret) { | |
2666 | scrub_block_put(sblock); | |
2667 | return ret; | |
2668 | } | |
2669 | } | |
2670 | ||
2671 | /* last one frees, either here or in bio completion for last page */ | |
2672 | scrub_block_put(sblock); | |
2673 | return 0; | |
2674 | } | |
2675 | ||
2676 | static int scrub_extent_for_parity(struct scrub_parity *sparity, | |
fa485d21 | 2677 | u64 logical, u32 len, |
5a6ac9ea MX |
2678 | u64 physical, struct btrfs_device *dev, |
2679 | u64 flags, u64 gen, int mirror_num) | |
2680 | { | |
2681 | struct scrub_ctx *sctx = sparity->sctx; | |
2682 | int ret; | |
2683 | u8 csum[BTRFS_CSUM_SIZE]; | |
2684 | u32 blocksize; | |
2685 | ||
e6e674bd | 2686 | if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) { |
4a770891 OS |
2687 | scrub_parity_mark_sectors_error(sparity, logical, len); |
2688 | return 0; | |
2689 | } | |
2690 | ||
5a6ac9ea | 2691 | if (flags & BTRFS_EXTENT_FLAG_DATA) { |
6ca1765b | 2692 | blocksize = sparity->stripe_len; |
5a6ac9ea | 2693 | } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
6ca1765b | 2694 | blocksize = sparity->stripe_len; |
5a6ac9ea | 2695 | } else { |
25cc1226 | 2696 | blocksize = sctx->fs_info->sectorsize; |
5a6ac9ea MX |
2697 | WARN_ON(1); |
2698 | } | |
2699 | ||
2700 | while (len) { | |
fa485d21 | 2701 | u32 l = min(len, blocksize); |
5a6ac9ea MX |
2702 | int have_csum = 0; |
2703 | ||
2704 | if (flags & BTRFS_EXTENT_FLAG_DATA) { | |
2705 | /* push csums to sbio */ | |
3b5753ec | 2706 | have_csum = scrub_find_csum(sctx, logical, csum); |
5a6ac9ea MX |
2707 | if (have_csum == 0) |
2708 | goto skip; | |
2709 | } | |
2710 | ret = scrub_pages_for_parity(sparity, logical, l, physical, dev, | |
2711 | flags, gen, mirror_num, | |
2712 | have_csum ? csum : NULL); | |
5a6ac9ea MX |
2713 | if (ret) |
2714 | return ret; | |
6b6d24b3 | 2715 | skip: |
5a6ac9ea MX |
2716 | len -= l; |
2717 | logical += l; | |
2718 | physical += l; | |
2719 | } | |
2720 | return 0; | |
2721 | } | |
2722 | ||
3b080b25 WS |
2723 | /* |
2724 | * Given a physical address, this will calculate it's | |
2725 | * logical offset. if this is a parity stripe, it will return | |
2726 | * the most left data stripe's logical offset. | |
2727 | * | |
2728 | * return 0 if it is a data stripe, 1 means parity stripe. | |
2729 | */ | |
2730 | static int get_raid56_logic_offset(u64 physical, int num, | |
5a6ac9ea MX |
2731 | struct map_lookup *map, u64 *offset, |
2732 | u64 *stripe_start) | |
3b080b25 WS |
2733 | { |
2734 | int i; | |
2735 | int j = 0; | |
2736 | u64 stripe_nr; | |
2737 | u64 last_offset; | |
9d644a62 DS |
2738 | u32 stripe_index; |
2739 | u32 rot; | |
cff82672 | 2740 | const int data_stripes = nr_data_stripes(map); |
3b080b25 | 2741 | |
cff82672 | 2742 | last_offset = (physical - map->stripes[num].physical) * data_stripes; |
5a6ac9ea MX |
2743 | if (stripe_start) |
2744 | *stripe_start = last_offset; | |
2745 | ||
3b080b25 | 2746 | *offset = last_offset; |
cff82672 | 2747 | for (i = 0; i < data_stripes; i++) { |
3b080b25 WS |
2748 | *offset = last_offset + i * map->stripe_len; |
2749 | ||
42c61ab6 | 2750 | stripe_nr = div64_u64(*offset, map->stripe_len); |
cff82672 | 2751 | stripe_nr = div_u64(stripe_nr, data_stripes); |
3b080b25 WS |
2752 | |
2753 | /* Work out the disk rotation on this stripe-set */ | |
47c5713f | 2754 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot); |
3b080b25 WS |
2755 | /* calculate which stripe this data locates */ |
2756 | rot += i; | |
e4fbaee2 | 2757 | stripe_index = rot % map->num_stripes; |
3b080b25 WS |
2758 | if (stripe_index == num) |
2759 | return 0; | |
2760 | if (stripe_index < num) | |
2761 | j++; | |
2762 | } | |
2763 | *offset = last_offset + j * map->stripe_len; | |
2764 | return 1; | |
2765 | } | |
2766 | ||
5a6ac9ea MX |
2767 | static void scrub_free_parity(struct scrub_parity *sparity) |
2768 | { | |
2769 | struct scrub_ctx *sctx = sparity->sctx; | |
2770 | struct scrub_page *curr, *next; | |
2771 | int nbits; | |
2772 | ||
2773 | nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors); | |
2774 | if (nbits) { | |
2775 | spin_lock(&sctx->stat_lock); | |
2776 | sctx->stat.read_errors += nbits; | |
2777 | sctx->stat.uncorrectable_errors += nbits; | |
2778 | spin_unlock(&sctx->stat_lock); | |
2779 | } | |
2780 | ||
2781 | list_for_each_entry_safe(curr, next, &sparity->spages, list) { | |
2782 | list_del_init(&curr->list); | |
2783 | scrub_page_put(curr); | |
2784 | } | |
2785 | ||
2786 | kfree(sparity); | |
2787 | } | |
2788 | ||
20b2e302 ZL |
2789 | static void scrub_parity_bio_endio_worker(struct btrfs_work *work) |
2790 | { | |
2791 | struct scrub_parity *sparity = container_of(work, struct scrub_parity, | |
2792 | work); | |
2793 | struct scrub_ctx *sctx = sparity->sctx; | |
2794 | ||
2795 | scrub_free_parity(sparity); | |
2796 | scrub_pending_bio_dec(sctx); | |
2797 | } | |
2798 | ||
4246a0b6 | 2799 | static void scrub_parity_bio_endio(struct bio *bio) |
5a6ac9ea MX |
2800 | { |
2801 | struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private; | |
0b246afa | 2802 | struct btrfs_fs_info *fs_info = sparity->sctx->fs_info; |
5a6ac9ea | 2803 | |
4e4cbee9 | 2804 | if (bio->bi_status) |
5a6ac9ea MX |
2805 | bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap, |
2806 | sparity->nsectors); | |
2807 | ||
5a6ac9ea | 2808 | bio_put(bio); |
20b2e302 | 2809 | |
a0cac0ec OS |
2810 | btrfs_init_work(&sparity->work, scrub_parity_bio_endio_worker, NULL, |
2811 | NULL); | |
0b246afa | 2812 | btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work); |
5a6ac9ea MX |
2813 | } |
2814 | ||
2815 | static void scrub_parity_check_and_repair(struct scrub_parity *sparity) | |
2816 | { | |
2817 | struct scrub_ctx *sctx = sparity->sctx; | |
0b246afa | 2818 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
5a6ac9ea MX |
2819 | struct bio *bio; |
2820 | struct btrfs_raid_bio *rbio; | |
4c664611 | 2821 | struct btrfs_io_context *bioc = NULL; |
5a6ac9ea MX |
2822 | u64 length; |
2823 | int ret; | |
2824 | ||
2825 | if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap, | |
2826 | sparity->nsectors)) | |
2827 | goto out; | |
2828 | ||
a0dd59de | 2829 | length = sparity->logic_end - sparity->logic_start; |
ae6529c3 QW |
2830 | |
2831 | btrfs_bio_counter_inc_blocked(fs_info); | |
0b246afa | 2832 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start, |
4c664611 QW |
2833 | &length, &bioc); |
2834 | if (ret || !bioc || !bioc->raid_map) | |
2835 | goto bioc_out; | |
5a6ac9ea | 2836 | |
c3a3b19b | 2837 | bio = btrfs_bio_alloc(BIO_MAX_VECS); |
5a6ac9ea MX |
2838 | bio->bi_iter.bi_sector = sparity->logic_start >> 9; |
2839 | bio->bi_private = sparity; | |
2840 | bio->bi_end_io = scrub_parity_bio_endio; | |
2841 | ||
6a258d72 QW |
2842 | rbio = raid56_parity_alloc_scrub_rbio(bio, bioc, length, |
2843 | sparity->scrub_dev, | |
5a6ac9ea MX |
2844 | sparity->dbitmap, |
2845 | sparity->nsectors); | |
2846 | if (!rbio) | |
2847 | goto rbio_out; | |
2848 | ||
5a6ac9ea MX |
2849 | scrub_pending_bio_inc(sctx); |
2850 | raid56_parity_submit_scrub_rbio(rbio); | |
2851 | return; | |
2852 | ||
2853 | rbio_out: | |
2854 | bio_put(bio); | |
4c664611 | 2855 | bioc_out: |
ae6529c3 | 2856 | btrfs_bio_counter_dec(fs_info); |
4c664611 | 2857 | btrfs_put_bioc(bioc); |
5a6ac9ea MX |
2858 | bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap, |
2859 | sparity->nsectors); | |
2860 | spin_lock(&sctx->stat_lock); | |
2861 | sctx->stat.malloc_errors++; | |
2862 | spin_unlock(&sctx->stat_lock); | |
2863 | out: | |
2864 | scrub_free_parity(sparity); | |
2865 | } | |
2866 | ||
2867 | static inline int scrub_calc_parity_bitmap_len(int nsectors) | |
2868 | { | |
bfca9a6d | 2869 | return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long); |
5a6ac9ea MX |
2870 | } |
2871 | ||
2872 | static void scrub_parity_get(struct scrub_parity *sparity) | |
2873 | { | |
78a76450 | 2874 | refcount_inc(&sparity->refs); |
5a6ac9ea MX |
2875 | } |
2876 | ||
2877 | static void scrub_parity_put(struct scrub_parity *sparity) | |
2878 | { | |
78a76450 | 2879 | if (!refcount_dec_and_test(&sparity->refs)) |
5a6ac9ea MX |
2880 | return; |
2881 | ||
2882 | scrub_parity_check_and_repair(sparity); | |
2883 | } | |
2884 | ||
2885 | static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx, | |
2886 | struct map_lookup *map, | |
2887 | struct btrfs_device *sdev, | |
5a6ac9ea MX |
2888 | u64 logic_start, |
2889 | u64 logic_end) | |
2890 | { | |
fb456252 | 2891 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
29cbcf40 | 2892 | struct btrfs_root *root = btrfs_extent_root(fs_info, logic_start); |
fc28b25e | 2893 | struct btrfs_root *csum_root; |
5a6ac9ea | 2894 | struct btrfs_extent_item *extent; |
4c664611 | 2895 | struct btrfs_io_context *bioc = NULL; |
2522dbe8 | 2896 | struct btrfs_path *path; |
5a6ac9ea MX |
2897 | u64 flags; |
2898 | int ret; | |
2899 | int slot; | |
2900 | struct extent_buffer *l; | |
2901 | struct btrfs_key key; | |
2902 | u64 generation; | |
2903 | u64 extent_logical; | |
2904 | u64 extent_physical; | |
fa485d21 QW |
2905 | /* Check the comment in scrub_stripe() for why u32 is enough here */ |
2906 | u32 extent_len; | |
4a770891 | 2907 | u64 mapped_length; |
5a6ac9ea MX |
2908 | struct btrfs_device *extent_dev; |
2909 | struct scrub_parity *sparity; | |
2910 | int nsectors; | |
2911 | int bitmap_len; | |
2912 | int extent_mirror_num; | |
2913 | int stop_loop = 0; | |
2914 | ||
2522dbe8 QW |
2915 | path = btrfs_alloc_path(); |
2916 | if (!path) { | |
2917 | spin_lock(&sctx->stat_lock); | |
2918 | sctx->stat.malloc_errors++; | |
2919 | spin_unlock(&sctx->stat_lock); | |
2920 | return -ENOMEM; | |
2921 | } | |
2922 | path->search_commit_root = 1; | |
2923 | path->skip_locking = 1; | |
2924 | ||
fa485d21 | 2925 | ASSERT(map->stripe_len <= U32_MAX); |
ab108d99 | 2926 | nsectors = map->stripe_len >> fs_info->sectorsize_bits; |
5a6ac9ea MX |
2927 | bitmap_len = scrub_calc_parity_bitmap_len(nsectors); |
2928 | sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len, | |
2929 | GFP_NOFS); | |
2930 | if (!sparity) { | |
2931 | spin_lock(&sctx->stat_lock); | |
2932 | sctx->stat.malloc_errors++; | |
2933 | spin_unlock(&sctx->stat_lock); | |
2522dbe8 | 2934 | btrfs_free_path(path); |
5a6ac9ea MX |
2935 | return -ENOMEM; |
2936 | } | |
2937 | ||
fa485d21 | 2938 | ASSERT(map->stripe_len <= U32_MAX); |
5a6ac9ea MX |
2939 | sparity->stripe_len = map->stripe_len; |
2940 | sparity->nsectors = nsectors; | |
2941 | sparity->sctx = sctx; | |
2942 | sparity->scrub_dev = sdev; | |
2943 | sparity->logic_start = logic_start; | |
2944 | sparity->logic_end = logic_end; | |
78a76450 | 2945 | refcount_set(&sparity->refs, 1); |
5a6ac9ea MX |
2946 | INIT_LIST_HEAD(&sparity->spages); |
2947 | sparity->dbitmap = sparity->bitmap; | |
2948 | sparity->ebitmap = (void *)sparity->bitmap + bitmap_len; | |
2949 | ||
2950 | ret = 0; | |
2951 | while (logic_start < logic_end) { | |
2952 | if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) | |
2953 | key.type = BTRFS_METADATA_ITEM_KEY; | |
2954 | else | |
2955 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
2956 | key.objectid = logic_start; | |
2957 | key.offset = (u64)-1; | |
2958 | ||
2959 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2960 | if (ret < 0) | |
2961 | goto out; | |
2962 | ||
2963 | if (ret > 0) { | |
2964 | ret = btrfs_previous_extent_item(root, path, 0); | |
2965 | if (ret < 0) | |
2966 | goto out; | |
2967 | if (ret > 0) { | |
2968 | btrfs_release_path(path); | |
2969 | ret = btrfs_search_slot(NULL, root, &key, | |
2970 | path, 0, 0); | |
2971 | if (ret < 0) | |
2972 | goto out; | |
2973 | } | |
2974 | } | |
2975 | ||
2976 | stop_loop = 0; | |
2977 | while (1) { | |
2978 | u64 bytes; | |
2979 | ||
2980 | l = path->nodes[0]; | |
2981 | slot = path->slots[0]; | |
2982 | if (slot >= btrfs_header_nritems(l)) { | |
2983 | ret = btrfs_next_leaf(root, path); | |
2984 | if (ret == 0) | |
2985 | continue; | |
2986 | if (ret < 0) | |
2987 | goto out; | |
2988 | ||
2989 | stop_loop = 1; | |
2990 | break; | |
2991 | } | |
2992 | btrfs_item_key_to_cpu(l, &key, slot); | |
2993 | ||
d7cad238 ZL |
2994 | if (key.type != BTRFS_EXTENT_ITEM_KEY && |
2995 | key.type != BTRFS_METADATA_ITEM_KEY) | |
2996 | goto next; | |
2997 | ||
5a6ac9ea | 2998 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
0b246afa | 2999 | bytes = fs_info->nodesize; |
5a6ac9ea MX |
3000 | else |
3001 | bytes = key.offset; | |
3002 | ||
3003 | if (key.objectid + bytes <= logic_start) | |
3004 | goto next; | |
3005 | ||
a0dd59de | 3006 | if (key.objectid >= logic_end) { |
5a6ac9ea MX |
3007 | stop_loop = 1; |
3008 | break; | |
3009 | } | |
3010 | ||
3011 | while (key.objectid >= logic_start + map->stripe_len) | |
3012 | logic_start += map->stripe_len; | |
3013 | ||
3014 | extent = btrfs_item_ptr(l, slot, | |
3015 | struct btrfs_extent_item); | |
3016 | flags = btrfs_extent_flags(l, extent); | |
3017 | generation = btrfs_extent_generation(l, extent); | |
3018 | ||
a323e813 ZL |
3019 | if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) && |
3020 | (key.objectid < logic_start || | |
3021 | key.objectid + bytes > | |
3022 | logic_start + map->stripe_len)) { | |
5d163e0e JM |
3023 | btrfs_err(fs_info, |
3024 | "scrub: tree block %llu spanning stripes, ignored. logical=%llu", | |
a323e813 | 3025 | key.objectid, logic_start); |
9799d2c3 ZL |
3026 | spin_lock(&sctx->stat_lock); |
3027 | sctx->stat.uncorrectable_errors++; | |
3028 | spin_unlock(&sctx->stat_lock); | |
5a6ac9ea MX |
3029 | goto next; |
3030 | } | |
3031 | again: | |
3032 | extent_logical = key.objectid; | |
fa485d21 | 3033 | ASSERT(bytes <= U32_MAX); |
5a6ac9ea MX |
3034 | extent_len = bytes; |
3035 | ||
3036 | if (extent_logical < logic_start) { | |
3037 | extent_len -= logic_start - extent_logical; | |
3038 | extent_logical = logic_start; | |
3039 | } | |
3040 | ||
3041 | if (extent_logical + extent_len > | |
3042 | logic_start + map->stripe_len) | |
3043 | extent_len = logic_start + map->stripe_len - | |
3044 | extent_logical; | |
3045 | ||
3046 | scrub_parity_mark_sectors_data(sparity, extent_logical, | |
3047 | extent_len); | |
3048 | ||
4a770891 | 3049 | mapped_length = extent_len; |
4c664611 | 3050 | bioc = NULL; |
cf8cddd3 | 3051 | ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, |
4c664611 | 3052 | extent_logical, &mapped_length, &bioc, |
cf8cddd3 | 3053 | 0); |
4a770891 | 3054 | if (!ret) { |
4c664611 | 3055 | if (!bioc || mapped_length < extent_len) |
4a770891 OS |
3056 | ret = -EIO; |
3057 | } | |
3058 | if (ret) { | |
4c664611 | 3059 | btrfs_put_bioc(bioc); |
4a770891 OS |
3060 | goto out; |
3061 | } | |
4c664611 QW |
3062 | extent_physical = bioc->stripes[0].physical; |
3063 | extent_mirror_num = bioc->mirror_num; | |
3064 | extent_dev = bioc->stripes[0].dev; | |
3065 | btrfs_put_bioc(bioc); | |
5a6ac9ea | 3066 | |
fc28b25e | 3067 | csum_root = btrfs_csum_root(fs_info, extent_logical); |
5a6ac9ea MX |
3068 | ret = btrfs_lookup_csums_range(csum_root, |
3069 | extent_logical, | |
3070 | extent_logical + extent_len - 1, | |
3071 | &sctx->csum_list, 1); | |
3072 | if (ret) | |
3073 | goto out; | |
3074 | ||
3075 | ret = scrub_extent_for_parity(sparity, extent_logical, | |
3076 | extent_len, | |
3077 | extent_physical, | |
3078 | extent_dev, flags, | |
3079 | generation, | |
3080 | extent_mirror_num); | |
6fa96d72 ZL |
3081 | |
3082 | scrub_free_csums(sctx); | |
3083 | ||
5a6ac9ea MX |
3084 | if (ret) |
3085 | goto out; | |
3086 | ||
5a6ac9ea MX |
3087 | if (extent_logical + extent_len < |
3088 | key.objectid + bytes) { | |
3089 | logic_start += map->stripe_len; | |
3090 | ||
3091 | if (logic_start >= logic_end) { | |
3092 | stop_loop = 1; | |
3093 | break; | |
3094 | } | |
3095 | ||
3096 | if (logic_start < key.objectid + bytes) { | |
3097 | cond_resched(); | |
3098 | goto again; | |
3099 | } | |
3100 | } | |
3101 | next: | |
3102 | path->slots[0]++; | |
3103 | } | |
3104 | ||
3105 | btrfs_release_path(path); | |
3106 | ||
3107 | if (stop_loop) | |
3108 | break; | |
3109 | ||
3110 | logic_start += map->stripe_len; | |
3111 | } | |
3112 | out: | |
fa485d21 QW |
3113 | if (ret < 0) { |
3114 | ASSERT(logic_end - logic_start <= U32_MAX); | |
5a6ac9ea | 3115 | scrub_parity_mark_sectors_error(sparity, logic_start, |
a0dd59de | 3116 | logic_end - logic_start); |
fa485d21 | 3117 | } |
5a6ac9ea MX |
3118 | scrub_parity_put(sparity); |
3119 | scrub_submit(sctx); | |
3fb99303 | 3120 | mutex_lock(&sctx->wr_lock); |
5a6ac9ea | 3121 | scrub_wr_submit(sctx); |
3fb99303 | 3122 | mutex_unlock(&sctx->wr_lock); |
5a6ac9ea | 3123 | |
2522dbe8 | 3124 | btrfs_free_path(path); |
5a6ac9ea MX |
3125 | return ret < 0 ? ret : 0; |
3126 | } | |
3127 | ||
de17addc NA |
3128 | static void sync_replace_for_zoned(struct scrub_ctx *sctx) |
3129 | { | |
3130 | if (!btrfs_is_zoned(sctx->fs_info)) | |
3131 | return; | |
3132 | ||
3133 | sctx->flush_all_writes = true; | |
3134 | scrub_submit(sctx); | |
3135 | mutex_lock(&sctx->wr_lock); | |
3136 | scrub_wr_submit(sctx); | |
3137 | mutex_unlock(&sctx->wr_lock); | |
3138 | ||
3139 | wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0); | |
3140 | } | |
3141 | ||
7db1c5d1 NA |
3142 | static int sync_write_pointer_for_zoned(struct scrub_ctx *sctx, u64 logical, |
3143 | u64 physical, u64 physical_end) | |
3144 | { | |
3145 | struct btrfs_fs_info *fs_info = sctx->fs_info; | |
3146 | int ret = 0; | |
3147 | ||
3148 | if (!btrfs_is_zoned(fs_info)) | |
3149 | return 0; | |
3150 | ||
3151 | wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0); | |
3152 | ||
3153 | mutex_lock(&sctx->wr_lock); | |
3154 | if (sctx->write_pointer < physical_end) { | |
3155 | ret = btrfs_sync_zone_write_pointer(sctx->wr_tgtdev, logical, | |
3156 | physical, | |
3157 | sctx->write_pointer); | |
3158 | if (ret) | |
3159 | btrfs_err(fs_info, | |
3160 | "zoned: failed to recover write pointer"); | |
3161 | } | |
3162 | mutex_unlock(&sctx->wr_lock); | |
3163 | btrfs_dev_clear_zone_empty(sctx->wr_tgtdev, physical); | |
3164 | ||
3165 | return ret; | |
3166 | } | |
3167 | ||
d9d181c1 | 3168 | static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, |
2ae8ae3d | 3169 | struct btrfs_block_group *bg, |
a36cf8b8 SB |
3170 | struct map_lookup *map, |
3171 | struct btrfs_device *scrub_dev, | |
2ae8ae3d | 3172 | int stripe_index, u64 dev_extent_len) |
a2de733c | 3173 | { |
2522dbe8 | 3174 | struct btrfs_path *path; |
fb456252 | 3175 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
29cbcf40 | 3176 | struct btrfs_root *root; |
fc28b25e | 3177 | struct btrfs_root *csum_root; |
a2de733c | 3178 | struct btrfs_extent_item *extent; |
e7786c3a | 3179 | struct blk_plug plug; |
2ae8ae3d | 3180 | const u64 chunk_logical = bg->start; |
a2de733c AJ |
3181 | u64 flags; |
3182 | int ret; | |
3183 | int slot; | |
a2de733c | 3184 | u64 nstripes; |
a2de733c | 3185 | struct extent_buffer *l; |
a2de733c AJ |
3186 | u64 physical; |
3187 | u64 logical; | |
625f1c8d | 3188 | u64 logic_end; |
3b080b25 | 3189 | u64 physical_end; |
a2de733c | 3190 | u64 generation; |
e12fa9cd | 3191 | int mirror_num; |
e6c11f9a | 3192 | struct btrfs_key key; |
5c07c53f | 3193 | u64 increment; |
a2de733c | 3194 | u64 offset; |
ff023aac SB |
3195 | u64 extent_logical; |
3196 | u64 extent_physical; | |
fa485d21 QW |
3197 | /* |
3198 | * Unlike chunk length, extent length should never go beyond | |
3199 | * BTRFS_MAX_EXTENT_SIZE, thus u32 is enough here. | |
3200 | */ | |
3201 | u32 extent_len; | |
5a6ac9ea MX |
3202 | u64 stripe_logical; |
3203 | u64 stripe_end; | |
ff023aac SB |
3204 | struct btrfs_device *extent_dev; |
3205 | int extent_mirror_num; | |
3b080b25 | 3206 | int stop_loop = 0; |
53b381b3 | 3207 | |
2ae8ae3d | 3208 | physical = map->stripes[stripe_index].physical; |
a2de733c | 3209 | offset = 0; |
2ae8ae3d | 3210 | nstripes = div64_u64(dev_extent_len, map->stripe_len); |
7735cd75 DS |
3211 | mirror_num = 1; |
3212 | increment = map->stripe_len; | |
a2de733c | 3213 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
2ae8ae3d | 3214 | offset = map->stripe_len * stripe_index; |
a2de733c | 3215 | increment = map->stripe_len * map->num_stripes; |
a2de733c AJ |
3216 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
3217 | int factor = map->num_stripes / map->sub_stripes; | |
2ae8ae3d | 3218 | offset = map->stripe_len * (stripe_index / map->sub_stripes); |
a2de733c | 3219 | increment = map->stripe_len * factor; |
2ae8ae3d | 3220 | mirror_num = stripe_index % map->sub_stripes + 1; |
c7369b3f | 3221 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) { |
2ae8ae3d | 3222 | mirror_num = stripe_index % map->num_stripes + 1; |
a2de733c | 3223 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
2ae8ae3d | 3224 | mirror_num = stripe_index % map->num_stripes + 1; |
ffe2d203 | 3225 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
2ae8ae3d QW |
3226 | get_raid56_logic_offset(physical, stripe_index, map, &offset, |
3227 | NULL); | |
3b080b25 | 3228 | increment = map->stripe_len * nr_data_stripes(map); |
a2de733c AJ |
3229 | } |
3230 | ||
3231 | path = btrfs_alloc_path(); | |
3232 | if (!path) | |
3233 | return -ENOMEM; | |
3234 | ||
b5d67f64 SB |
3235 | /* |
3236 | * work on commit root. The related disk blocks are static as | |
3237 | * long as COW is applied. This means, it is save to rewrite | |
3238 | * them to repair disk errors without any race conditions | |
3239 | */ | |
a2de733c AJ |
3240 | path->search_commit_root = 1; |
3241 | path->skip_locking = 1; | |
dcf62b20 | 3242 | path->reada = READA_FORWARD; |
a2de733c | 3243 | |
2ae8ae3d | 3244 | logical = chunk_logical + offset; |
3b080b25 | 3245 | physical_end = physical + nstripes * map->stripe_len; |
ffe2d203 | 3246 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
2ae8ae3d | 3247 | get_raid56_logic_offset(physical_end, stripe_index, |
5a6ac9ea | 3248 | map, &logic_end, NULL); |
2ae8ae3d | 3249 | logic_end += chunk_logical; |
3b080b25 WS |
3250 | } else { |
3251 | logic_end = logical + increment * nstripes; | |
3252 | } | |
d9d181c1 | 3253 | wait_event(sctx->list_wait, |
b6bfebc1 | 3254 | atomic_read(&sctx->bios_in_flight) == 0); |
cb7ab021 | 3255 | scrub_blocked_if_needed(fs_info); |
7a26285e | 3256 | |
29cbcf40 | 3257 | root = btrfs_extent_root(fs_info, logical); |
fc28b25e JB |
3258 | csum_root = btrfs_csum_root(fs_info, logical); |
3259 | ||
a2de733c AJ |
3260 | /* |
3261 | * collect all data csums for the stripe to avoid seeking during | |
3262 | * the scrub. This might currently (crc32) end up to be about 1MB | |
3263 | */ | |
e7786c3a | 3264 | blk_start_plug(&plug); |
a2de733c | 3265 | |
de17addc NA |
3266 | if (sctx->is_dev_replace && |
3267 | btrfs_dev_is_sequential(sctx->wr_tgtdev, physical)) { | |
3268 | mutex_lock(&sctx->wr_lock); | |
3269 | sctx->write_pointer = physical; | |
3270 | mutex_unlock(&sctx->wr_lock); | |
3271 | sctx->flush_all_writes = true; | |
3272 | } | |
3273 | ||
a2de733c AJ |
3274 | /* |
3275 | * now find all extents for each stripe and scrub them | |
3276 | */ | |
a2de733c | 3277 | ret = 0; |
3b080b25 | 3278 | while (physical < physical_end) { |
a2de733c AJ |
3279 | /* |
3280 | * canceled? | |
3281 | */ | |
3282 | if (atomic_read(&fs_info->scrub_cancel_req) || | |
d9d181c1 | 3283 | atomic_read(&sctx->cancel_req)) { |
a2de733c AJ |
3284 | ret = -ECANCELED; |
3285 | goto out; | |
3286 | } | |
3287 | /* | |
3288 | * check to see if we have to pause | |
3289 | */ | |
3290 | if (atomic_read(&fs_info->scrub_pause_req)) { | |
3291 | /* push queued extents */ | |
2073c4c2 | 3292 | sctx->flush_all_writes = true; |
d9d181c1 | 3293 | scrub_submit(sctx); |
3fb99303 | 3294 | mutex_lock(&sctx->wr_lock); |
ff023aac | 3295 | scrub_wr_submit(sctx); |
3fb99303 | 3296 | mutex_unlock(&sctx->wr_lock); |
d9d181c1 | 3297 | wait_event(sctx->list_wait, |
b6bfebc1 | 3298 | atomic_read(&sctx->bios_in_flight) == 0); |
2073c4c2 | 3299 | sctx->flush_all_writes = false; |
3cb0929a | 3300 | scrub_blocked_if_needed(fs_info); |
a2de733c AJ |
3301 | } |
3302 | ||
f2f66a2f | 3303 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
2ae8ae3d QW |
3304 | ret = get_raid56_logic_offset(physical, stripe_index, |
3305 | map, &logical, | |
f2f66a2f | 3306 | &stripe_logical); |
2ae8ae3d | 3307 | logical += chunk_logical; |
f2f66a2f | 3308 | if (ret) { |
7955323b | 3309 | /* it is parity strip */ |
2ae8ae3d | 3310 | stripe_logical += chunk_logical; |
a0dd59de | 3311 | stripe_end = stripe_logical + increment; |
f2f66a2f | 3312 | ret = scrub_raid56_parity(sctx, map, scrub_dev, |
2522dbe8 | 3313 | stripe_logical, |
f2f66a2f ZL |
3314 | stripe_end); |
3315 | if (ret) | |
3316 | goto out; | |
3317 | goto skip; | |
3318 | } | |
3319 | } | |
3320 | ||
7c76edb7 WS |
3321 | if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
3322 | key.type = BTRFS_METADATA_ITEM_KEY; | |
3323 | else | |
3324 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
a2de733c | 3325 | key.objectid = logical; |
625f1c8d | 3326 | key.offset = (u64)-1; |
a2de733c AJ |
3327 | |
3328 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
3329 | if (ret < 0) | |
3330 | goto out; | |
3173a18f | 3331 | |
8c51032f | 3332 | if (ret > 0) { |
ade2e0b3 | 3333 | ret = btrfs_previous_extent_item(root, path, 0); |
a2de733c AJ |
3334 | if (ret < 0) |
3335 | goto out; | |
8c51032f AJ |
3336 | if (ret > 0) { |
3337 | /* there's no smaller item, so stick with the | |
3338 | * larger one */ | |
3339 | btrfs_release_path(path); | |
3340 | ret = btrfs_search_slot(NULL, root, &key, | |
3341 | path, 0, 0); | |
3342 | if (ret < 0) | |
3343 | goto out; | |
3344 | } | |
a2de733c AJ |
3345 | } |
3346 | ||
625f1c8d | 3347 | stop_loop = 0; |
a2de733c | 3348 | while (1) { |
3173a18f JB |
3349 | u64 bytes; |
3350 | ||
a2de733c AJ |
3351 | l = path->nodes[0]; |
3352 | slot = path->slots[0]; | |
3353 | if (slot >= btrfs_header_nritems(l)) { | |
3354 | ret = btrfs_next_leaf(root, path); | |
3355 | if (ret == 0) | |
3356 | continue; | |
3357 | if (ret < 0) | |
3358 | goto out; | |
3359 | ||
625f1c8d | 3360 | stop_loop = 1; |
a2de733c AJ |
3361 | break; |
3362 | } | |
3363 | btrfs_item_key_to_cpu(l, &key, slot); | |
3364 | ||
d7cad238 ZL |
3365 | if (key.type != BTRFS_EXTENT_ITEM_KEY && |
3366 | key.type != BTRFS_METADATA_ITEM_KEY) | |
3367 | goto next; | |
3368 | ||
3173a18f | 3369 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
0b246afa | 3370 | bytes = fs_info->nodesize; |
3173a18f JB |
3371 | else |
3372 | bytes = key.offset; | |
3373 | ||
3374 | if (key.objectid + bytes <= logical) | |
a2de733c AJ |
3375 | goto next; |
3376 | ||
625f1c8d LB |
3377 | if (key.objectid >= logical + map->stripe_len) { |
3378 | /* out of this device extent */ | |
3379 | if (key.objectid >= logic_end) | |
3380 | stop_loop = 1; | |
3381 | break; | |
3382 | } | |
a2de733c | 3383 | |
2473d24f FM |
3384 | /* |
3385 | * If our block group was removed in the meanwhile, just | |
3386 | * stop scrubbing since there is no point in continuing. | |
3387 | * Continuing would prevent reusing its device extents | |
3388 | * for new block groups for a long time. | |
3389 | */ | |
2ae8ae3d QW |
3390 | spin_lock(&bg->lock); |
3391 | if (bg->removed) { | |
3392 | spin_unlock(&bg->lock); | |
2473d24f FM |
3393 | ret = 0; |
3394 | goto out; | |
3395 | } | |
2ae8ae3d | 3396 | spin_unlock(&bg->lock); |
2473d24f | 3397 | |
a2de733c AJ |
3398 | extent = btrfs_item_ptr(l, slot, |
3399 | struct btrfs_extent_item); | |
3400 | flags = btrfs_extent_flags(l, extent); | |
3401 | generation = btrfs_extent_generation(l, extent); | |
3402 | ||
a323e813 ZL |
3403 | if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) && |
3404 | (key.objectid < logical || | |
3405 | key.objectid + bytes > | |
3406 | logical + map->stripe_len)) { | |
efe120a0 | 3407 | btrfs_err(fs_info, |
5d163e0e | 3408 | "scrub: tree block %llu spanning stripes, ignored. logical=%llu", |
c1c9ff7c | 3409 | key.objectid, logical); |
9799d2c3 ZL |
3410 | spin_lock(&sctx->stat_lock); |
3411 | sctx->stat.uncorrectable_errors++; | |
3412 | spin_unlock(&sctx->stat_lock); | |
a2de733c AJ |
3413 | goto next; |
3414 | } | |
3415 | ||
625f1c8d LB |
3416 | again: |
3417 | extent_logical = key.objectid; | |
fa485d21 | 3418 | ASSERT(bytes <= U32_MAX); |
625f1c8d LB |
3419 | extent_len = bytes; |
3420 | ||
a2de733c AJ |
3421 | /* |
3422 | * trim extent to this stripe | |
3423 | */ | |
625f1c8d LB |
3424 | if (extent_logical < logical) { |
3425 | extent_len -= logical - extent_logical; | |
3426 | extent_logical = logical; | |
a2de733c | 3427 | } |
625f1c8d | 3428 | if (extent_logical + extent_len > |
a2de733c | 3429 | logical + map->stripe_len) { |
625f1c8d LB |
3430 | extent_len = logical + map->stripe_len - |
3431 | extent_logical; | |
a2de733c AJ |
3432 | } |
3433 | ||
625f1c8d | 3434 | extent_physical = extent_logical - logical + physical; |
ff023aac SB |
3435 | extent_dev = scrub_dev; |
3436 | extent_mirror_num = mirror_num; | |
32934280 | 3437 | if (sctx->is_dev_replace) |
ff023aac SB |
3438 | scrub_remap_extent(fs_info, extent_logical, |
3439 | extent_len, &extent_physical, | |
3440 | &extent_dev, | |
3441 | &extent_mirror_num); | |
625f1c8d | 3442 | |
89490303 FM |
3443 | if (flags & BTRFS_EXTENT_FLAG_DATA) { |
3444 | ret = btrfs_lookup_csums_range(csum_root, | |
3445 | extent_logical, | |
3446 | extent_logical + extent_len - 1, | |
3447 | &sctx->csum_list, 1); | |
3448 | if (ret) | |
3449 | goto out; | |
3450 | } | |
625f1c8d | 3451 | |
6ca1765b | 3452 | ret = scrub_extent(sctx, map, extent_logical, extent_len, |
ff023aac SB |
3453 | extent_physical, extent_dev, flags, |
3454 | generation, extent_mirror_num, | |
115930cb | 3455 | extent_logical - logical + physical); |
6fa96d72 ZL |
3456 | |
3457 | scrub_free_csums(sctx); | |
3458 | ||
a2de733c AJ |
3459 | if (ret) |
3460 | goto out; | |
3461 | ||
de17addc NA |
3462 | if (sctx->is_dev_replace) |
3463 | sync_replace_for_zoned(sctx); | |
3464 | ||
625f1c8d LB |
3465 | if (extent_logical + extent_len < |
3466 | key.objectid + bytes) { | |
ffe2d203 | 3467 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
3b080b25 WS |
3468 | /* |
3469 | * loop until we find next data stripe | |
3470 | * or we have finished all stripes. | |
3471 | */ | |
5a6ac9ea MX |
3472 | loop: |
3473 | physical += map->stripe_len; | |
3474 | ret = get_raid56_logic_offset(physical, | |
2ae8ae3d QW |
3475 | stripe_index, map, |
3476 | &logical, &stripe_logical); | |
3477 | logical += chunk_logical; | |
5a6ac9ea MX |
3478 | |
3479 | if (ret && physical < physical_end) { | |
2ae8ae3d | 3480 | stripe_logical += chunk_logical; |
5a6ac9ea | 3481 | stripe_end = stripe_logical + |
a0dd59de | 3482 | increment; |
5a6ac9ea | 3483 | ret = scrub_raid56_parity(sctx, |
2522dbe8 | 3484 | map, scrub_dev, |
5a6ac9ea MX |
3485 | stripe_logical, |
3486 | stripe_end); | |
3487 | if (ret) | |
3488 | goto out; | |
3489 | goto loop; | |
3490 | } | |
3b080b25 WS |
3491 | } else { |
3492 | physical += map->stripe_len; | |
3493 | logical += increment; | |
3494 | } | |
625f1c8d LB |
3495 | if (logical < key.objectid + bytes) { |
3496 | cond_resched(); | |
3497 | goto again; | |
3498 | } | |
3499 | ||
3b080b25 | 3500 | if (physical >= physical_end) { |
625f1c8d LB |
3501 | stop_loop = 1; |
3502 | break; | |
3503 | } | |
3504 | } | |
a2de733c AJ |
3505 | next: |
3506 | path->slots[0]++; | |
3507 | } | |
71267333 | 3508 | btrfs_release_path(path); |
3b080b25 | 3509 | skip: |
a2de733c AJ |
3510 | logical += increment; |
3511 | physical += map->stripe_len; | |
d9d181c1 | 3512 | spin_lock(&sctx->stat_lock); |
625f1c8d | 3513 | if (stop_loop) |
2ae8ae3d QW |
3514 | sctx->stat.last_physical = map->stripes[stripe_index].physical + |
3515 | dev_extent_len; | |
625f1c8d LB |
3516 | else |
3517 | sctx->stat.last_physical = physical; | |
d9d181c1 | 3518 | spin_unlock(&sctx->stat_lock); |
625f1c8d LB |
3519 | if (stop_loop) |
3520 | break; | |
a2de733c | 3521 | } |
ff023aac | 3522 | out: |
a2de733c | 3523 | /* push queued extents */ |
d9d181c1 | 3524 | scrub_submit(sctx); |
3fb99303 | 3525 | mutex_lock(&sctx->wr_lock); |
ff023aac | 3526 | scrub_wr_submit(sctx); |
3fb99303 | 3527 | mutex_unlock(&sctx->wr_lock); |
a2de733c | 3528 | |
e7786c3a | 3529 | blk_finish_plug(&plug); |
a2de733c | 3530 | btrfs_free_path(path); |
7db1c5d1 NA |
3531 | |
3532 | if (sctx->is_dev_replace && ret >= 0) { | |
3533 | int ret2; | |
3534 | ||
2ae8ae3d QW |
3535 | ret2 = sync_write_pointer_for_zoned(sctx, |
3536 | chunk_logical + offset, | |
3537 | map->stripes[stripe_index].physical, | |
3538 | physical_end); | |
7db1c5d1 NA |
3539 | if (ret2) |
3540 | ret = ret2; | |
3541 | } | |
3542 | ||
a2de733c AJ |
3543 | return ret < 0 ? ret : 0; |
3544 | } | |
3545 | ||
d9d181c1 | 3546 | static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx, |
d04fbe19 | 3547 | struct btrfs_block_group *bg, |
a36cf8b8 | 3548 | struct btrfs_device *scrub_dev, |
020d5b73 | 3549 | u64 dev_offset, |
d04fbe19 | 3550 | u64 dev_extent_len) |
a2de733c | 3551 | { |
fb456252 | 3552 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
c8bf1b67 | 3553 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
a2de733c AJ |
3554 | struct map_lookup *map; |
3555 | struct extent_map *em; | |
3556 | int i; | |
ff023aac | 3557 | int ret = 0; |
a2de733c | 3558 | |
c8bf1b67 | 3559 | read_lock(&map_tree->lock); |
d04fbe19 | 3560 | em = lookup_extent_mapping(map_tree, bg->start, bg->length); |
c8bf1b67 | 3561 | read_unlock(&map_tree->lock); |
a2de733c | 3562 | |
020d5b73 FM |
3563 | if (!em) { |
3564 | /* | |
3565 | * Might have been an unused block group deleted by the cleaner | |
3566 | * kthread or relocation. | |
3567 | */ | |
d04fbe19 QW |
3568 | spin_lock(&bg->lock); |
3569 | if (!bg->removed) | |
020d5b73 | 3570 | ret = -EINVAL; |
d04fbe19 | 3571 | spin_unlock(&bg->lock); |
020d5b73 FM |
3572 | |
3573 | return ret; | |
3574 | } | |
d04fbe19 | 3575 | if (em->start != bg->start) |
a2de733c | 3576 | goto out; |
d04fbe19 | 3577 | if (em->len < dev_extent_len) |
a2de733c AJ |
3578 | goto out; |
3579 | ||
d04fbe19 | 3580 | map = em->map_lookup; |
a2de733c | 3581 | for (i = 0; i < map->num_stripes; ++i) { |
a36cf8b8 | 3582 | if (map->stripes[i].dev->bdev == scrub_dev->bdev && |
859acaf1 | 3583 | map->stripes[i].physical == dev_offset) { |
2ae8ae3d QW |
3584 | ret = scrub_stripe(sctx, bg, map, scrub_dev, i, |
3585 | dev_extent_len); | |
a2de733c AJ |
3586 | if (ret) |
3587 | goto out; | |
3588 | } | |
3589 | } | |
3590 | out: | |
3591 | free_extent_map(em); | |
3592 | ||
3593 | return ret; | |
3594 | } | |
3595 | ||
de17addc NA |
3596 | static int finish_extent_writes_for_zoned(struct btrfs_root *root, |
3597 | struct btrfs_block_group *cache) | |
3598 | { | |
3599 | struct btrfs_fs_info *fs_info = cache->fs_info; | |
3600 | struct btrfs_trans_handle *trans; | |
3601 | ||
3602 | if (!btrfs_is_zoned(fs_info)) | |
3603 | return 0; | |
3604 | ||
3605 | btrfs_wait_block_group_reservations(cache); | |
3606 | btrfs_wait_nocow_writers(cache); | |
3607 | btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start, cache->length); | |
3608 | ||
3609 | trans = btrfs_join_transaction(root); | |
3610 | if (IS_ERR(trans)) | |
3611 | return PTR_ERR(trans); | |
3612 | return btrfs_commit_transaction(trans); | |
3613 | } | |
3614 | ||
a2de733c | 3615 | static noinline_for_stack |
a36cf8b8 | 3616 | int scrub_enumerate_chunks(struct scrub_ctx *sctx, |
32934280 | 3617 | struct btrfs_device *scrub_dev, u64 start, u64 end) |
a2de733c AJ |
3618 | { |
3619 | struct btrfs_dev_extent *dev_extent = NULL; | |
3620 | struct btrfs_path *path; | |
0b246afa JM |
3621 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
3622 | struct btrfs_root *root = fs_info->dev_root; | |
a2de733c | 3623 | u64 chunk_offset; |
55e3a601 | 3624 | int ret = 0; |
76a8efa1 | 3625 | int ro_set; |
a2de733c AJ |
3626 | int slot; |
3627 | struct extent_buffer *l; | |
3628 | struct btrfs_key key; | |
3629 | struct btrfs_key found_key; | |
32da5386 | 3630 | struct btrfs_block_group *cache; |
ff023aac | 3631 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
a2de733c AJ |
3632 | |
3633 | path = btrfs_alloc_path(); | |
3634 | if (!path) | |
3635 | return -ENOMEM; | |
3636 | ||
e4058b54 | 3637 | path->reada = READA_FORWARD; |
a2de733c AJ |
3638 | path->search_commit_root = 1; |
3639 | path->skip_locking = 1; | |
3640 | ||
a36cf8b8 | 3641 | key.objectid = scrub_dev->devid; |
a2de733c AJ |
3642 | key.offset = 0ull; |
3643 | key.type = BTRFS_DEV_EXTENT_KEY; | |
3644 | ||
a2de733c | 3645 | while (1) { |
d04fbe19 QW |
3646 | u64 dev_extent_len; |
3647 | ||
a2de733c AJ |
3648 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
3649 | if (ret < 0) | |
8c51032f AJ |
3650 | break; |
3651 | if (ret > 0) { | |
3652 | if (path->slots[0] >= | |
3653 | btrfs_header_nritems(path->nodes[0])) { | |
3654 | ret = btrfs_next_leaf(root, path); | |
55e3a601 Z |
3655 | if (ret < 0) |
3656 | break; | |
3657 | if (ret > 0) { | |
3658 | ret = 0; | |
8c51032f | 3659 | break; |
55e3a601 Z |
3660 | } |
3661 | } else { | |
3662 | ret = 0; | |
8c51032f AJ |
3663 | } |
3664 | } | |
a2de733c AJ |
3665 | |
3666 | l = path->nodes[0]; | |
3667 | slot = path->slots[0]; | |
3668 | ||
3669 | btrfs_item_key_to_cpu(l, &found_key, slot); | |
3670 | ||
a36cf8b8 | 3671 | if (found_key.objectid != scrub_dev->devid) |
a2de733c AJ |
3672 | break; |
3673 | ||
962a298f | 3674 | if (found_key.type != BTRFS_DEV_EXTENT_KEY) |
a2de733c AJ |
3675 | break; |
3676 | ||
3677 | if (found_key.offset >= end) | |
3678 | break; | |
3679 | ||
3680 | if (found_key.offset < key.offset) | |
3681 | break; | |
3682 | ||
3683 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
d04fbe19 | 3684 | dev_extent_len = btrfs_dev_extent_length(l, dev_extent); |
a2de733c | 3685 | |
d04fbe19 | 3686 | if (found_key.offset + dev_extent_len <= start) |
ced96edc | 3687 | goto skip; |
a2de733c | 3688 | |
a2de733c AJ |
3689 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
3690 | ||
3691 | /* | |
3692 | * get a reference on the corresponding block group to prevent | |
3693 | * the chunk from going away while we scrub it | |
3694 | */ | |
3695 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); | |
ced96edc QW |
3696 | |
3697 | /* some chunks are removed but not committed to disk yet, | |
3698 | * continue scrubbing */ | |
3699 | if (!cache) | |
3700 | goto skip; | |
3701 | ||
78ce9fc2 NA |
3702 | if (sctx->is_dev_replace && btrfs_is_zoned(fs_info)) { |
3703 | spin_lock(&cache->lock); | |
3704 | if (!cache->to_copy) { | |
3705 | spin_unlock(&cache->lock); | |
0dc16ef4 FM |
3706 | btrfs_put_block_group(cache); |
3707 | goto skip; | |
78ce9fc2 NA |
3708 | } |
3709 | spin_unlock(&cache->lock); | |
3710 | } | |
3711 | ||
2473d24f FM |
3712 | /* |
3713 | * Make sure that while we are scrubbing the corresponding block | |
3714 | * group doesn't get its logical address and its device extents | |
3715 | * reused for another block group, which can possibly be of a | |
3716 | * different type and different profile. We do this to prevent | |
3717 | * false error detections and crashes due to bogus attempts to | |
3718 | * repair extents. | |
3719 | */ | |
3720 | spin_lock(&cache->lock); | |
3721 | if (cache->removed) { | |
3722 | spin_unlock(&cache->lock); | |
3723 | btrfs_put_block_group(cache); | |
3724 | goto skip; | |
3725 | } | |
6b7304af | 3726 | btrfs_freeze_block_group(cache); |
2473d24f FM |
3727 | spin_unlock(&cache->lock); |
3728 | ||
55e3a601 Z |
3729 | /* |
3730 | * we need call btrfs_inc_block_group_ro() with scrubs_paused, | |
3731 | * to avoid deadlock caused by: | |
3732 | * btrfs_inc_block_group_ro() | |
3733 | * -> btrfs_wait_for_commit() | |
3734 | * -> btrfs_commit_transaction() | |
3735 | * -> btrfs_scrub_pause() | |
3736 | */ | |
3737 | scrub_pause_on(fs_info); | |
b12de528 QW |
3738 | |
3739 | /* | |
3740 | * Don't do chunk preallocation for scrub. | |
3741 | * | |
3742 | * This is especially important for SYSTEM bgs, or we can hit | |
3743 | * -EFBIG from btrfs_finish_chunk_alloc() like: | |
3744 | * 1. The only SYSTEM bg is marked RO. | |
3745 | * Since SYSTEM bg is small, that's pretty common. | |
3746 | * 2. New SYSTEM bg will be allocated | |
3747 | * Due to regular version will allocate new chunk. | |
3748 | * 3. New SYSTEM bg is empty and will get cleaned up | |
3749 | * Before cleanup really happens, it's marked RO again. | |
3750 | * 4. Empty SYSTEM bg get scrubbed | |
3751 | * We go back to 2. | |
3752 | * | |
3753 | * This can easily boost the amount of SYSTEM chunks if cleaner | |
3754 | * thread can't be triggered fast enough, and use up all space | |
3755 | * of btrfs_super_block::sys_chunk_array | |
1bbb97b8 QW |
3756 | * |
3757 | * While for dev replace, we need to try our best to mark block | |
3758 | * group RO, to prevent race between: | |
3759 | * - Write duplication | |
3760 | * Contains latest data | |
3761 | * - Scrub copy | |
3762 | * Contains data from commit tree | |
3763 | * | |
3764 | * If target block group is not marked RO, nocow writes can | |
3765 | * be overwritten by scrub copy, causing data corruption. | |
3766 | * So for dev-replace, it's not allowed to continue if a block | |
3767 | * group is not RO. | |
b12de528 | 3768 | */ |
1bbb97b8 | 3769 | ret = btrfs_inc_block_group_ro(cache, sctx->is_dev_replace); |
de17addc NA |
3770 | if (!ret && sctx->is_dev_replace) { |
3771 | ret = finish_extent_writes_for_zoned(root, cache); | |
3772 | if (ret) { | |
3773 | btrfs_dec_block_group_ro(cache); | |
3774 | scrub_pause_off(fs_info); | |
3775 | btrfs_put_block_group(cache); | |
3776 | break; | |
3777 | } | |
3778 | } | |
3779 | ||
76a8efa1 Z |
3780 | if (ret == 0) { |
3781 | ro_set = 1; | |
1bbb97b8 | 3782 | } else if (ret == -ENOSPC && !sctx->is_dev_replace) { |
76a8efa1 Z |
3783 | /* |
3784 | * btrfs_inc_block_group_ro return -ENOSPC when it | |
3785 | * failed in creating new chunk for metadata. | |
1bbb97b8 | 3786 | * It is not a problem for scrub, because |
76a8efa1 Z |
3787 | * metadata are always cowed, and our scrub paused |
3788 | * commit_transactions. | |
3789 | */ | |
3790 | ro_set = 0; | |
195a49ea FM |
3791 | } else if (ret == -ETXTBSY) { |
3792 | btrfs_warn(fs_info, | |
3793 | "skipping scrub of block group %llu due to active swapfile", | |
3794 | cache->start); | |
3795 | scrub_pause_off(fs_info); | |
3796 | ret = 0; | |
3797 | goto skip_unfreeze; | |
76a8efa1 | 3798 | } else { |
5d163e0e | 3799 | btrfs_warn(fs_info, |
913e1535 | 3800 | "failed setting block group ro: %d", ret); |
6b7304af | 3801 | btrfs_unfreeze_block_group(cache); |
55e3a601 | 3802 | btrfs_put_block_group(cache); |
1bbb97b8 | 3803 | scrub_pause_off(fs_info); |
55e3a601 Z |
3804 | break; |
3805 | } | |
3806 | ||
1bbb97b8 QW |
3807 | /* |
3808 | * Now the target block is marked RO, wait for nocow writes to | |
3809 | * finish before dev-replace. | |
3810 | * COW is fine, as COW never overwrites extents in commit tree. | |
3811 | */ | |
3812 | if (sctx->is_dev_replace) { | |
3813 | btrfs_wait_nocow_writers(cache); | |
3814 | btrfs_wait_ordered_roots(fs_info, U64_MAX, cache->start, | |
3815 | cache->length); | |
3816 | } | |
3817 | ||
3818 | scrub_pause_off(fs_info); | |
3ec17a67 | 3819 | down_write(&dev_replace->rwsem); |
d04fbe19 | 3820 | dev_replace->cursor_right = found_key.offset + dev_extent_len; |
ff023aac SB |
3821 | dev_replace->cursor_left = found_key.offset; |
3822 | dev_replace->item_needs_writeback = 1; | |
cb5583dd DS |
3823 | up_write(&dev_replace->rwsem); |
3824 | ||
d04fbe19 QW |
3825 | ASSERT(cache->start == chunk_offset); |
3826 | ret = scrub_chunk(sctx, cache, scrub_dev, found_key.offset, | |
3827 | dev_extent_len); | |
ff023aac SB |
3828 | |
3829 | /* | |
3830 | * flush, submit all pending read and write bios, afterwards | |
3831 | * wait for them. | |
3832 | * Note that in the dev replace case, a read request causes | |
3833 | * write requests that are submitted in the read completion | |
3834 | * worker. Therefore in the current situation, it is required | |
3835 | * that all write requests are flushed, so that all read and | |
3836 | * write requests are really completed when bios_in_flight | |
3837 | * changes to 0. | |
3838 | */ | |
2073c4c2 | 3839 | sctx->flush_all_writes = true; |
ff023aac | 3840 | scrub_submit(sctx); |
3fb99303 | 3841 | mutex_lock(&sctx->wr_lock); |
ff023aac | 3842 | scrub_wr_submit(sctx); |
3fb99303 | 3843 | mutex_unlock(&sctx->wr_lock); |
ff023aac SB |
3844 | |
3845 | wait_event(sctx->list_wait, | |
3846 | atomic_read(&sctx->bios_in_flight) == 0); | |
b708ce96 Z |
3847 | |
3848 | scrub_pause_on(fs_info); | |
12cf9372 WS |
3849 | |
3850 | /* | |
3851 | * must be called before we decrease @scrub_paused. | |
3852 | * make sure we don't block transaction commit while | |
3853 | * we are waiting pending workers finished. | |
3854 | */ | |
ff023aac SB |
3855 | wait_event(sctx->list_wait, |
3856 | atomic_read(&sctx->workers_pending) == 0); | |
2073c4c2 | 3857 | sctx->flush_all_writes = false; |
12cf9372 | 3858 | |
b708ce96 | 3859 | scrub_pause_off(fs_info); |
ff023aac | 3860 | |
78ce9fc2 NA |
3861 | if (sctx->is_dev_replace && |
3862 | !btrfs_finish_block_group_to_copy(dev_replace->srcdev, | |
3863 | cache, found_key.offset)) | |
3864 | ro_set = 0; | |
3865 | ||
3ec17a67 | 3866 | down_write(&dev_replace->rwsem); |
1a1a8b73 FM |
3867 | dev_replace->cursor_left = dev_replace->cursor_right; |
3868 | dev_replace->item_needs_writeback = 1; | |
3ec17a67 | 3869 | up_write(&dev_replace->rwsem); |
1a1a8b73 | 3870 | |
76a8efa1 | 3871 | if (ro_set) |
2ff7e61e | 3872 | btrfs_dec_block_group_ro(cache); |
ff023aac | 3873 | |
758f2dfc FM |
3874 | /* |
3875 | * We might have prevented the cleaner kthread from deleting | |
3876 | * this block group if it was already unused because we raced | |
3877 | * and set it to RO mode first. So add it back to the unused | |
3878 | * list, otherwise it might not ever be deleted unless a manual | |
3879 | * balance is triggered or it becomes used and unused again. | |
3880 | */ | |
3881 | spin_lock(&cache->lock); | |
3882 | if (!cache->removed && !cache->ro && cache->reserved == 0 && | |
bf38be65 | 3883 | cache->used == 0) { |
758f2dfc | 3884 | spin_unlock(&cache->lock); |
6e80d4f8 DZ |
3885 | if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) |
3886 | btrfs_discard_queue_work(&fs_info->discard_ctl, | |
3887 | cache); | |
3888 | else | |
3889 | btrfs_mark_bg_unused(cache); | |
758f2dfc FM |
3890 | } else { |
3891 | spin_unlock(&cache->lock); | |
3892 | } | |
195a49ea | 3893 | skip_unfreeze: |
6b7304af | 3894 | btrfs_unfreeze_block_group(cache); |
a2de733c AJ |
3895 | btrfs_put_block_group(cache); |
3896 | if (ret) | |
3897 | break; | |
32934280 | 3898 | if (sctx->is_dev_replace && |
af1be4f8 | 3899 | atomic64_read(&dev_replace->num_write_errors) > 0) { |
ff023aac SB |
3900 | ret = -EIO; |
3901 | break; | |
3902 | } | |
3903 | if (sctx->stat.malloc_errors > 0) { | |
3904 | ret = -ENOMEM; | |
3905 | break; | |
3906 | } | |
ced96edc | 3907 | skip: |
d04fbe19 | 3908 | key.offset = found_key.offset + dev_extent_len; |
71267333 | 3909 | btrfs_release_path(path); |
a2de733c AJ |
3910 | } |
3911 | ||
a2de733c | 3912 | btrfs_free_path(path); |
8c51032f | 3913 | |
55e3a601 | 3914 | return ret; |
a2de733c AJ |
3915 | } |
3916 | ||
a36cf8b8 SB |
3917 | static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx, |
3918 | struct btrfs_device *scrub_dev) | |
a2de733c AJ |
3919 | { |
3920 | int i; | |
3921 | u64 bytenr; | |
3922 | u64 gen; | |
3923 | int ret; | |
0b246afa | 3924 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
a2de733c | 3925 | |
84961539 | 3926 | if (BTRFS_FS_ERROR(fs_info)) |
fbabd4a3 | 3927 | return -EROFS; |
79787eaa | 3928 | |
5f546063 | 3929 | /* Seed devices of a new filesystem has their own generation. */ |
0b246afa | 3930 | if (scrub_dev->fs_devices != fs_info->fs_devices) |
5f546063 MX |
3931 | gen = scrub_dev->generation; |
3932 | else | |
0b246afa | 3933 | gen = fs_info->last_trans_committed; |
a2de733c AJ |
3934 | |
3935 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { | |
3936 | bytenr = btrfs_sb_offset(i); | |
935e5cc9 MX |
3937 | if (bytenr + BTRFS_SUPER_INFO_SIZE > |
3938 | scrub_dev->commit_total_bytes) | |
a2de733c | 3939 | break; |
12659251 NA |
3940 | if (!btrfs_check_super_location(scrub_dev, bytenr)) |
3941 | continue; | |
a2de733c | 3942 | |
d9d181c1 | 3943 | ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr, |
a36cf8b8 | 3944 | scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i, |
96e63a45 | 3945 | NULL, bytenr); |
a2de733c AJ |
3946 | if (ret) |
3947 | return ret; | |
3948 | } | |
b6bfebc1 | 3949 | wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0); |
a2de733c AJ |
3950 | |
3951 | return 0; | |
3952 | } | |
3953 | ||
e89c4a9c JB |
3954 | static void scrub_workers_put(struct btrfs_fs_info *fs_info) |
3955 | { | |
3956 | if (refcount_dec_and_mutex_lock(&fs_info->scrub_workers_refcnt, | |
3957 | &fs_info->scrub_lock)) { | |
3958 | struct btrfs_workqueue *scrub_workers = NULL; | |
3959 | struct btrfs_workqueue *scrub_wr_comp = NULL; | |
3960 | struct btrfs_workqueue *scrub_parity = NULL; | |
3961 | ||
3962 | scrub_workers = fs_info->scrub_workers; | |
3963 | scrub_wr_comp = fs_info->scrub_wr_completion_workers; | |
3964 | scrub_parity = fs_info->scrub_parity_workers; | |
3965 | ||
3966 | fs_info->scrub_workers = NULL; | |
3967 | fs_info->scrub_wr_completion_workers = NULL; | |
3968 | fs_info->scrub_parity_workers = NULL; | |
3969 | mutex_unlock(&fs_info->scrub_lock); | |
3970 | ||
3971 | btrfs_destroy_workqueue(scrub_workers); | |
3972 | btrfs_destroy_workqueue(scrub_wr_comp); | |
3973 | btrfs_destroy_workqueue(scrub_parity); | |
3974 | } | |
3975 | } | |
3976 | ||
a2de733c AJ |
3977 | /* |
3978 | * get a reference count on fs_info->scrub_workers. start worker if necessary | |
3979 | */ | |
ff023aac SB |
3980 | static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info, |
3981 | int is_dev_replace) | |
a2de733c | 3982 | { |
e89c4a9c JB |
3983 | struct btrfs_workqueue *scrub_workers = NULL; |
3984 | struct btrfs_workqueue *scrub_wr_comp = NULL; | |
3985 | struct btrfs_workqueue *scrub_parity = NULL; | |
6f011058 | 3986 | unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND; |
0339ef2f | 3987 | int max_active = fs_info->thread_pool_size; |
e89c4a9c | 3988 | int ret = -ENOMEM; |
a2de733c | 3989 | |
e89c4a9c JB |
3990 | if (refcount_inc_not_zero(&fs_info->scrub_workers_refcnt)) |
3991 | return 0; | |
eb4318e5 | 3992 | |
e89c4a9c JB |
3993 | scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub", flags, |
3994 | is_dev_replace ? 1 : max_active, 4); | |
3995 | if (!scrub_workers) | |
3996 | goto fail_scrub_workers; | |
e82afc52 | 3997 | |
e89c4a9c | 3998 | scrub_wr_comp = btrfs_alloc_workqueue(fs_info, "scrubwrc", flags, |
20b2e302 | 3999 | max_active, 2); |
e89c4a9c JB |
4000 | if (!scrub_wr_comp) |
4001 | goto fail_scrub_wr_completion_workers; | |
ff09c4ca | 4002 | |
e89c4a9c JB |
4003 | scrub_parity = btrfs_alloc_workqueue(fs_info, "scrubparity", flags, |
4004 | max_active, 2); | |
4005 | if (!scrub_parity) | |
4006 | goto fail_scrub_parity_workers; | |
4007 | ||
4008 | mutex_lock(&fs_info->scrub_lock); | |
4009 | if (refcount_read(&fs_info->scrub_workers_refcnt) == 0) { | |
4010 | ASSERT(fs_info->scrub_workers == NULL && | |
4011 | fs_info->scrub_wr_completion_workers == NULL && | |
4012 | fs_info->scrub_parity_workers == NULL); | |
4013 | fs_info->scrub_workers = scrub_workers; | |
4014 | fs_info->scrub_wr_completion_workers = scrub_wr_comp; | |
4015 | fs_info->scrub_parity_workers = scrub_parity; | |
ff09c4ca | 4016 | refcount_set(&fs_info->scrub_workers_refcnt, 1); |
e89c4a9c JB |
4017 | mutex_unlock(&fs_info->scrub_lock); |
4018 | return 0; | |
632dd772 | 4019 | } |
e89c4a9c JB |
4020 | /* Other thread raced in and created the workers for us */ |
4021 | refcount_inc(&fs_info->scrub_workers_refcnt); | |
4022 | mutex_unlock(&fs_info->scrub_lock); | |
e82afc52 | 4023 | |
e89c4a9c JB |
4024 | ret = 0; |
4025 | btrfs_destroy_workqueue(scrub_parity); | |
e82afc52 | 4026 | fail_scrub_parity_workers: |
e89c4a9c | 4027 | btrfs_destroy_workqueue(scrub_wr_comp); |
e82afc52 | 4028 | fail_scrub_wr_completion_workers: |
e89c4a9c | 4029 | btrfs_destroy_workqueue(scrub_workers); |
e82afc52 | 4030 | fail_scrub_workers: |
e89c4a9c | 4031 | return ret; |
a2de733c AJ |
4032 | } |
4033 | ||
aa1b8cd4 SB |
4034 | int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, |
4035 | u64 end, struct btrfs_scrub_progress *progress, | |
63a212ab | 4036 | int readonly, int is_dev_replace) |
a2de733c | 4037 | { |
562d7b15 | 4038 | struct btrfs_dev_lookup_args args = { .devid = devid }; |
d9d181c1 | 4039 | struct scrub_ctx *sctx; |
a2de733c AJ |
4040 | int ret; |
4041 | struct btrfs_device *dev; | |
a5fb1142 | 4042 | unsigned int nofs_flag; |
a2de733c | 4043 | |
aa1b8cd4 | 4044 | if (btrfs_fs_closing(fs_info)) |
6c3abeda | 4045 | return -EAGAIN; |
a2de733c | 4046 | |
da17066c | 4047 | if (fs_info->nodesize > BTRFS_STRIPE_LEN) { |
b5d67f64 SB |
4048 | /* |
4049 | * in this case scrub is unable to calculate the checksum | |
4050 | * the way scrub is implemented. Do not handle this | |
4051 | * situation at all because it won't ever happen. | |
4052 | */ | |
efe120a0 FH |
4053 | btrfs_err(fs_info, |
4054 | "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails", | |
da17066c JM |
4055 | fs_info->nodesize, |
4056 | BTRFS_STRIPE_LEN); | |
b5d67f64 SB |
4057 | return -EINVAL; |
4058 | } | |
4059 | ||
da17066c | 4060 | if (fs_info->nodesize > |
7a9e9987 | 4061 | PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK || |
da17066c | 4062 | fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) { |
7a9e9987 SB |
4063 | /* |
4064 | * would exhaust the array bounds of pagev member in | |
4065 | * struct scrub_block | |
4066 | */ | |
5d163e0e JM |
4067 | btrfs_err(fs_info, |
4068 | "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails", | |
da17066c | 4069 | fs_info->nodesize, |
7a9e9987 | 4070 | SCRUB_MAX_PAGES_PER_BLOCK, |
da17066c | 4071 | fs_info->sectorsize, |
7a9e9987 SB |
4072 | SCRUB_MAX_PAGES_PER_BLOCK); |
4073 | return -EINVAL; | |
4074 | } | |
4075 | ||
0e94c4f4 DS |
4076 | /* Allocate outside of device_list_mutex */ |
4077 | sctx = scrub_setup_ctx(fs_info, is_dev_replace); | |
4078 | if (IS_ERR(sctx)) | |
4079 | return PTR_ERR(sctx); | |
a2de733c | 4080 | |
e89c4a9c JB |
4081 | ret = scrub_workers_get(fs_info, is_dev_replace); |
4082 | if (ret) | |
4083 | goto out_free_ctx; | |
4084 | ||
aa1b8cd4 | 4085 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
562d7b15 | 4086 | dev = btrfs_find_device(fs_info->fs_devices, &args); |
e6e674bd AJ |
4087 | if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) && |
4088 | !is_dev_replace)) { | |
aa1b8cd4 | 4089 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
0e94c4f4 | 4090 | ret = -ENODEV; |
e89c4a9c | 4091 | goto out; |
a2de733c | 4092 | } |
a2de733c | 4093 | |
ebbede42 AJ |
4094 | if (!is_dev_replace && !readonly && |
4095 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) { | |
5d68da3b | 4096 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
a4852cf2 DS |
4097 | btrfs_err_in_rcu(fs_info, |
4098 | "scrub on devid %llu: filesystem on %s is not writable", | |
4099 | devid, rcu_str_deref(dev->name)); | |
0e94c4f4 | 4100 | ret = -EROFS; |
e89c4a9c | 4101 | goto out; |
5d68da3b MX |
4102 | } |
4103 | ||
3b7a016f | 4104 | mutex_lock(&fs_info->scrub_lock); |
e12c9621 | 4105 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || |
401e29c1 | 4106 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) { |
a2de733c | 4107 | mutex_unlock(&fs_info->scrub_lock); |
aa1b8cd4 | 4108 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
0e94c4f4 | 4109 | ret = -EIO; |
e89c4a9c | 4110 | goto out; |
a2de733c AJ |
4111 | } |
4112 | ||
cb5583dd | 4113 | down_read(&fs_info->dev_replace.rwsem); |
cadbc0a0 | 4114 | if (dev->scrub_ctx || |
8dabb742 SB |
4115 | (!is_dev_replace && |
4116 | btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) { | |
cb5583dd | 4117 | up_read(&fs_info->dev_replace.rwsem); |
a2de733c | 4118 | mutex_unlock(&fs_info->scrub_lock); |
aa1b8cd4 | 4119 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
0e94c4f4 | 4120 | ret = -EINPROGRESS; |
e89c4a9c | 4121 | goto out; |
a2de733c | 4122 | } |
cb5583dd | 4123 | up_read(&fs_info->dev_replace.rwsem); |
3b7a016f | 4124 | |
d9d181c1 | 4125 | sctx->readonly = readonly; |
cadbc0a0 | 4126 | dev->scrub_ctx = sctx; |
3cb0929a | 4127 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
a2de733c | 4128 | |
3cb0929a WS |
4129 | /* |
4130 | * checking @scrub_pause_req here, we can avoid | |
4131 | * race between committing transaction and scrubbing. | |
4132 | */ | |
cb7ab021 | 4133 | __scrub_blocked_if_needed(fs_info); |
a2de733c AJ |
4134 | atomic_inc(&fs_info->scrubs_running); |
4135 | mutex_unlock(&fs_info->scrub_lock); | |
a2de733c | 4136 | |
a5fb1142 FM |
4137 | /* |
4138 | * In order to avoid deadlock with reclaim when there is a transaction | |
4139 | * trying to pause scrub, make sure we use GFP_NOFS for all the | |
4140 | * allocations done at btrfs_scrub_pages() and scrub_pages_for_parity() | |
4141 | * invoked by our callees. The pausing request is done when the | |
4142 | * transaction commit starts, and it blocks the transaction until scrub | |
4143 | * is paused (done at specific points at scrub_stripe() or right above | |
4144 | * before incrementing fs_info->scrubs_running). | |
4145 | */ | |
4146 | nofs_flag = memalloc_nofs_save(); | |
ff023aac | 4147 | if (!is_dev_replace) { |
d1e14420 | 4148 | btrfs_info(fs_info, "scrub: started on devid %llu", devid); |
9b011adf WS |
4149 | /* |
4150 | * by holding device list mutex, we can | |
4151 | * kick off writing super in log tree sync. | |
4152 | */ | |
3cb0929a | 4153 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
ff023aac | 4154 | ret = scrub_supers(sctx, dev); |
3cb0929a | 4155 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
ff023aac | 4156 | } |
a2de733c AJ |
4157 | |
4158 | if (!ret) | |
32934280 | 4159 | ret = scrub_enumerate_chunks(sctx, dev, start, end); |
a5fb1142 | 4160 | memalloc_nofs_restore(nofs_flag); |
a2de733c | 4161 | |
b6bfebc1 | 4162 | wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0); |
a2de733c AJ |
4163 | atomic_dec(&fs_info->scrubs_running); |
4164 | wake_up(&fs_info->scrub_pause_wait); | |
4165 | ||
b6bfebc1 | 4166 | wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0); |
0ef8e451 | 4167 | |
a2de733c | 4168 | if (progress) |
d9d181c1 | 4169 | memcpy(progress, &sctx->stat, sizeof(*progress)); |
a2de733c | 4170 | |
d1e14420 AJ |
4171 | if (!is_dev_replace) |
4172 | btrfs_info(fs_info, "scrub: %s on devid %llu with status: %d", | |
4173 | ret ? "not finished" : "finished", devid, ret); | |
4174 | ||
a2de733c | 4175 | mutex_lock(&fs_info->scrub_lock); |
cadbc0a0 | 4176 | dev->scrub_ctx = NULL; |
a2de733c AJ |
4177 | mutex_unlock(&fs_info->scrub_lock); |
4178 | ||
e89c4a9c | 4179 | scrub_workers_put(fs_info); |
f55985f4 | 4180 | scrub_put_ctx(sctx); |
a2de733c | 4181 | |
0e94c4f4 | 4182 | return ret; |
e89c4a9c JB |
4183 | out: |
4184 | scrub_workers_put(fs_info); | |
0e94c4f4 DS |
4185 | out_free_ctx: |
4186 | scrub_free_ctx(sctx); | |
4187 | ||
a2de733c AJ |
4188 | return ret; |
4189 | } | |
4190 | ||
2ff7e61e | 4191 | void btrfs_scrub_pause(struct btrfs_fs_info *fs_info) |
a2de733c | 4192 | { |
a2de733c AJ |
4193 | mutex_lock(&fs_info->scrub_lock); |
4194 | atomic_inc(&fs_info->scrub_pause_req); | |
4195 | while (atomic_read(&fs_info->scrubs_paused) != | |
4196 | atomic_read(&fs_info->scrubs_running)) { | |
4197 | mutex_unlock(&fs_info->scrub_lock); | |
4198 | wait_event(fs_info->scrub_pause_wait, | |
4199 | atomic_read(&fs_info->scrubs_paused) == | |
4200 | atomic_read(&fs_info->scrubs_running)); | |
4201 | mutex_lock(&fs_info->scrub_lock); | |
4202 | } | |
4203 | mutex_unlock(&fs_info->scrub_lock); | |
a2de733c AJ |
4204 | } |
4205 | ||
2ff7e61e | 4206 | void btrfs_scrub_continue(struct btrfs_fs_info *fs_info) |
a2de733c | 4207 | { |
a2de733c AJ |
4208 | atomic_dec(&fs_info->scrub_pause_req); |
4209 | wake_up(&fs_info->scrub_pause_wait); | |
a2de733c AJ |
4210 | } |
4211 | ||
aa1b8cd4 | 4212 | int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info) |
a2de733c | 4213 | { |
a2de733c AJ |
4214 | mutex_lock(&fs_info->scrub_lock); |
4215 | if (!atomic_read(&fs_info->scrubs_running)) { | |
4216 | mutex_unlock(&fs_info->scrub_lock); | |
4217 | return -ENOTCONN; | |
4218 | } | |
4219 | ||
4220 | atomic_inc(&fs_info->scrub_cancel_req); | |
4221 | while (atomic_read(&fs_info->scrubs_running)) { | |
4222 | mutex_unlock(&fs_info->scrub_lock); | |
4223 | wait_event(fs_info->scrub_pause_wait, | |
4224 | atomic_read(&fs_info->scrubs_running) == 0); | |
4225 | mutex_lock(&fs_info->scrub_lock); | |
4226 | } | |
4227 | atomic_dec(&fs_info->scrub_cancel_req); | |
4228 | mutex_unlock(&fs_info->scrub_lock); | |
4229 | ||
4230 | return 0; | |
4231 | } | |
4232 | ||
163e97ee | 4233 | int btrfs_scrub_cancel_dev(struct btrfs_device *dev) |
49b25e05 | 4234 | { |
163e97ee | 4235 | struct btrfs_fs_info *fs_info = dev->fs_info; |
d9d181c1 | 4236 | struct scrub_ctx *sctx; |
a2de733c AJ |
4237 | |
4238 | mutex_lock(&fs_info->scrub_lock); | |
cadbc0a0 | 4239 | sctx = dev->scrub_ctx; |
d9d181c1 | 4240 | if (!sctx) { |
a2de733c AJ |
4241 | mutex_unlock(&fs_info->scrub_lock); |
4242 | return -ENOTCONN; | |
4243 | } | |
d9d181c1 | 4244 | atomic_inc(&sctx->cancel_req); |
cadbc0a0 | 4245 | while (dev->scrub_ctx) { |
a2de733c AJ |
4246 | mutex_unlock(&fs_info->scrub_lock); |
4247 | wait_event(fs_info->scrub_pause_wait, | |
cadbc0a0 | 4248 | dev->scrub_ctx == NULL); |
a2de733c AJ |
4249 | mutex_lock(&fs_info->scrub_lock); |
4250 | } | |
4251 | mutex_unlock(&fs_info->scrub_lock); | |
4252 | ||
4253 | return 0; | |
4254 | } | |
1623edeb | 4255 | |
2ff7e61e | 4256 | int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid, |
a2de733c AJ |
4257 | struct btrfs_scrub_progress *progress) |
4258 | { | |
562d7b15 | 4259 | struct btrfs_dev_lookup_args args = { .devid = devid }; |
a2de733c | 4260 | struct btrfs_device *dev; |
d9d181c1 | 4261 | struct scrub_ctx *sctx = NULL; |
a2de733c | 4262 | |
0b246afa | 4263 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
562d7b15 | 4264 | dev = btrfs_find_device(fs_info->fs_devices, &args); |
a2de733c | 4265 | if (dev) |
cadbc0a0 | 4266 | sctx = dev->scrub_ctx; |
d9d181c1 SB |
4267 | if (sctx) |
4268 | memcpy(progress, &sctx->stat, sizeof(*progress)); | |
0b246afa | 4269 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
a2de733c | 4270 | |
d9d181c1 | 4271 | return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV; |
a2de733c | 4272 | } |
ff023aac SB |
4273 | |
4274 | static void scrub_remap_extent(struct btrfs_fs_info *fs_info, | |
fa485d21 | 4275 | u64 extent_logical, u32 extent_len, |
ff023aac SB |
4276 | u64 *extent_physical, |
4277 | struct btrfs_device **extent_dev, | |
4278 | int *extent_mirror_num) | |
4279 | { | |
4280 | u64 mapped_length; | |
4c664611 | 4281 | struct btrfs_io_context *bioc = NULL; |
ff023aac SB |
4282 | int ret; |
4283 | ||
4284 | mapped_length = extent_len; | |
cf8cddd3 | 4285 | ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical, |
4c664611 QW |
4286 | &mapped_length, &bioc, 0); |
4287 | if (ret || !bioc || mapped_length < extent_len || | |
4288 | !bioc->stripes[0].dev->bdev) { | |
4289 | btrfs_put_bioc(bioc); | |
ff023aac SB |
4290 | return; |
4291 | } | |
4292 | ||
4c664611 QW |
4293 | *extent_physical = bioc->stripes[0].physical; |
4294 | *extent_mirror_num = bioc->mirror_num; | |
4295 | *extent_dev = bioc->stripes[0].dev; | |
4296 | btrfs_put_bioc(bioc); | |
ff023aac | 4297 | } |