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