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