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 AJ |
65 | |
66 | struct scrub_page { | |
b5d67f64 SB |
67 | struct scrub_block *sblock; |
68 | struct page *page; | |
442a4f63 | 69 | struct btrfs_device *dev; |
a2de733c AJ |
70 | u64 flags; /* extent flags */ |
71 | u64 generation; | |
b5d67f64 SB |
72 | u64 logical; |
73 | u64 physical; | |
ff023aac | 74 | u64 physical_for_dev_replace; |
7a9e9987 | 75 | atomic_t ref_count; |
b5d67f64 SB |
76 | struct { |
77 | unsigned int mirror_num:8; | |
78 | unsigned int have_csum:1; | |
79 | unsigned int io_error:1; | |
80 | }; | |
a2de733c AJ |
81 | u8 csum[BTRFS_CSUM_SIZE]; |
82 | }; | |
83 | ||
84 | struct scrub_bio { | |
85 | int index; | |
d9d181c1 | 86 | struct scrub_ctx *sctx; |
a36cf8b8 | 87 | struct btrfs_device *dev; |
a2de733c AJ |
88 | struct bio *bio; |
89 | int err; | |
90 | u64 logical; | |
91 | u64 physical; | |
ff023aac SB |
92 | #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO |
93 | struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO]; | |
94 | #else | |
95 | struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO]; | |
96 | #endif | |
b5d67f64 | 97 | int page_count; |
a2de733c | 98 | int next_free; |
0339ef2f QW |
99 | struct btrfs_work_struct |
100 | work; | |
a2de733c AJ |
101 | }; |
102 | ||
b5d67f64 | 103 | struct scrub_block { |
7a9e9987 | 104 | struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK]; |
b5d67f64 SB |
105 | int page_count; |
106 | atomic_t outstanding_pages; | |
107 | atomic_t ref_count; /* free mem on transition to zero */ | |
d9d181c1 | 108 | struct scrub_ctx *sctx; |
b5d67f64 SB |
109 | struct { |
110 | unsigned int header_error:1; | |
111 | unsigned int checksum_error:1; | |
112 | unsigned int no_io_error_seen:1; | |
442a4f63 | 113 | unsigned int generation_error:1; /* also sets header_error */ |
b5d67f64 SB |
114 | }; |
115 | }; | |
116 | ||
ff023aac SB |
117 | struct scrub_wr_ctx { |
118 | struct scrub_bio *wr_curr_bio; | |
119 | struct btrfs_device *tgtdev; | |
120 | int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */ | |
121 | atomic_t flush_all_writes; | |
122 | struct mutex wr_lock; | |
123 | }; | |
124 | ||
d9d181c1 | 125 | struct scrub_ctx { |
ff023aac | 126 | struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX]; |
a36cf8b8 | 127 | struct btrfs_root *dev_root; |
a2de733c AJ |
128 | int first_free; |
129 | int curr; | |
b6bfebc1 SB |
130 | atomic_t bios_in_flight; |
131 | atomic_t workers_pending; | |
a2de733c AJ |
132 | spinlock_t list_lock; |
133 | wait_queue_head_t list_wait; | |
134 | u16 csum_size; | |
135 | struct list_head csum_list; | |
136 | atomic_t cancel_req; | |
8628764e | 137 | int readonly; |
ff023aac | 138 | int pages_per_rd_bio; |
b5d67f64 SB |
139 | u32 sectorsize; |
140 | u32 nodesize; | |
141 | u32 leafsize; | |
63a212ab SB |
142 | |
143 | int is_dev_replace; | |
ff023aac | 144 | struct scrub_wr_ctx wr_ctx; |
63a212ab | 145 | |
a2de733c AJ |
146 | /* |
147 | * statistics | |
148 | */ | |
149 | struct btrfs_scrub_progress stat; | |
150 | spinlock_t stat_lock; | |
151 | }; | |
152 | ||
0ef8e451 | 153 | struct scrub_fixup_nodatasum { |
d9d181c1 | 154 | struct scrub_ctx *sctx; |
a36cf8b8 | 155 | struct btrfs_device *dev; |
0ef8e451 JS |
156 | u64 logical; |
157 | struct btrfs_root *root; | |
0339ef2f QW |
158 | struct btrfs_work_struct |
159 | work; | |
0ef8e451 JS |
160 | int mirror_num; |
161 | }; | |
162 | ||
652f25a2 JB |
163 | struct scrub_nocow_inode { |
164 | u64 inum; | |
165 | u64 offset; | |
166 | u64 root; | |
167 | struct list_head list; | |
168 | }; | |
169 | ||
ff023aac SB |
170 | struct scrub_copy_nocow_ctx { |
171 | struct scrub_ctx *sctx; | |
172 | u64 logical; | |
173 | u64 len; | |
174 | int mirror_num; | |
175 | u64 physical_for_dev_replace; | |
652f25a2 | 176 | struct list_head inodes; |
0339ef2f QW |
177 | struct btrfs_work_struct |
178 | work; | |
ff023aac SB |
179 | }; |
180 | ||
558540c1 JS |
181 | struct scrub_warning { |
182 | struct btrfs_path *path; | |
183 | u64 extent_item_size; | |
184 | char *scratch_buf; | |
185 | char *msg_buf; | |
186 | const char *errstr; | |
187 | sector_t sector; | |
188 | u64 logical; | |
189 | struct btrfs_device *dev; | |
190 | int msg_bufsize; | |
191 | int scratch_bufsize; | |
192 | }; | |
193 | ||
b5d67f64 | 194 | |
b6bfebc1 SB |
195 | static void scrub_pending_bio_inc(struct scrub_ctx *sctx); |
196 | static void scrub_pending_bio_dec(struct scrub_ctx *sctx); | |
197 | static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx); | |
198 | static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx); | |
b5d67f64 | 199 | static int scrub_handle_errored_block(struct scrub_block *sblock_to_check); |
d9d181c1 | 200 | static int scrub_setup_recheck_block(struct scrub_ctx *sctx, |
3ec706c8 | 201 | struct btrfs_fs_info *fs_info, |
ff023aac | 202 | struct scrub_block *original_sblock, |
b5d67f64 | 203 | u64 length, u64 logical, |
ff023aac | 204 | struct scrub_block *sblocks_for_recheck); |
34f5c8e9 SB |
205 | static void scrub_recheck_block(struct btrfs_fs_info *fs_info, |
206 | struct scrub_block *sblock, int is_metadata, | |
207 | int have_csum, u8 *csum, u64 generation, | |
208 | u16 csum_size); | |
b5d67f64 SB |
209 | static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info, |
210 | struct scrub_block *sblock, | |
211 | int is_metadata, int have_csum, | |
212 | const u8 *csum, u64 generation, | |
213 | u16 csum_size); | |
b5d67f64 SB |
214 | static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad, |
215 | struct scrub_block *sblock_good, | |
216 | int force_write); | |
217 | static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad, | |
218 | struct scrub_block *sblock_good, | |
219 | int page_num, int force_write); | |
ff023aac SB |
220 | static void scrub_write_block_to_dev_replace(struct scrub_block *sblock); |
221 | static int scrub_write_page_to_dev_replace(struct scrub_block *sblock, | |
222 | int page_num); | |
b5d67f64 SB |
223 | static int scrub_checksum_data(struct scrub_block *sblock); |
224 | static int scrub_checksum_tree_block(struct scrub_block *sblock); | |
225 | static int scrub_checksum_super(struct scrub_block *sblock); | |
226 | static void scrub_block_get(struct scrub_block *sblock); | |
227 | static void scrub_block_put(struct scrub_block *sblock); | |
7a9e9987 SB |
228 | static void scrub_page_get(struct scrub_page *spage); |
229 | static void scrub_page_put(struct scrub_page *spage); | |
ff023aac SB |
230 | static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx, |
231 | struct scrub_page *spage); | |
d9d181c1 | 232 | static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len, |
a36cf8b8 | 233 | u64 physical, struct btrfs_device *dev, u64 flags, |
ff023aac SB |
234 | u64 gen, int mirror_num, u8 *csum, int force, |
235 | u64 physical_for_dev_replace); | |
1623edeb | 236 | static void scrub_bio_end_io(struct bio *bio, int err); |
0339ef2f | 237 | static void scrub_bio_end_io_worker(struct btrfs_work_struct *work); |
b5d67f64 | 238 | static void scrub_block_complete(struct scrub_block *sblock); |
ff023aac SB |
239 | static void scrub_remap_extent(struct btrfs_fs_info *fs_info, |
240 | u64 extent_logical, u64 extent_len, | |
241 | u64 *extent_physical, | |
242 | struct btrfs_device **extent_dev, | |
243 | int *extent_mirror_num); | |
244 | static int scrub_setup_wr_ctx(struct scrub_ctx *sctx, | |
245 | struct scrub_wr_ctx *wr_ctx, | |
246 | struct btrfs_fs_info *fs_info, | |
247 | struct btrfs_device *dev, | |
248 | int is_dev_replace); | |
249 | static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx); | |
250 | static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx, | |
251 | struct scrub_page *spage); | |
252 | static void scrub_wr_submit(struct scrub_ctx *sctx); | |
253 | static void scrub_wr_bio_end_io(struct bio *bio, int err); | |
0339ef2f | 254 | static void scrub_wr_bio_end_io_worker(struct btrfs_work_struct *work); |
ff023aac SB |
255 | static int write_page_nocow(struct scrub_ctx *sctx, |
256 | u64 physical_for_dev_replace, struct page *page); | |
257 | static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root, | |
652f25a2 | 258 | struct scrub_copy_nocow_ctx *ctx); |
ff023aac SB |
259 | static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len, |
260 | int mirror_num, u64 physical_for_dev_replace); | |
0339ef2f | 261 | static void copy_nocow_pages_worker(struct btrfs_work_struct *work); |
cb7ab021 | 262 | static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info); |
3cb0929a | 263 | static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info); |
1623edeb SB |
264 | |
265 | ||
b6bfebc1 SB |
266 | static void scrub_pending_bio_inc(struct scrub_ctx *sctx) |
267 | { | |
268 | atomic_inc(&sctx->bios_in_flight); | |
269 | } | |
270 | ||
271 | static void scrub_pending_bio_dec(struct scrub_ctx *sctx) | |
272 | { | |
273 | atomic_dec(&sctx->bios_in_flight); | |
274 | wake_up(&sctx->list_wait); | |
275 | } | |
276 | ||
cb7ab021 | 277 | static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info) |
3cb0929a WS |
278 | { |
279 | while (atomic_read(&fs_info->scrub_pause_req)) { | |
280 | mutex_unlock(&fs_info->scrub_lock); | |
281 | wait_event(fs_info->scrub_pause_wait, | |
282 | atomic_read(&fs_info->scrub_pause_req) == 0); | |
283 | mutex_lock(&fs_info->scrub_lock); | |
284 | } | |
285 | } | |
286 | ||
cb7ab021 WS |
287 | static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info) |
288 | { | |
289 | atomic_inc(&fs_info->scrubs_paused); | |
290 | wake_up(&fs_info->scrub_pause_wait); | |
291 | ||
292 | mutex_lock(&fs_info->scrub_lock); | |
293 | __scrub_blocked_if_needed(fs_info); | |
294 | atomic_dec(&fs_info->scrubs_paused); | |
295 | mutex_unlock(&fs_info->scrub_lock); | |
296 | ||
297 | wake_up(&fs_info->scrub_pause_wait); | |
298 | } | |
299 | ||
b6bfebc1 SB |
300 | /* |
301 | * used for workers that require transaction commits (i.e., for the | |
302 | * NOCOW case) | |
303 | */ | |
304 | static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx) | |
305 | { | |
306 | struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info; | |
307 | ||
308 | /* | |
309 | * increment scrubs_running to prevent cancel requests from | |
310 | * completing as long as a worker is running. we must also | |
311 | * increment scrubs_paused to prevent deadlocking on pause | |
312 | * requests used for transactions commits (as the worker uses a | |
313 | * transaction context). it is safe to regard the worker | |
314 | * as paused for all matters practical. effectively, we only | |
315 | * avoid cancellation requests from completing. | |
316 | */ | |
317 | mutex_lock(&fs_info->scrub_lock); | |
318 | atomic_inc(&fs_info->scrubs_running); | |
319 | atomic_inc(&fs_info->scrubs_paused); | |
320 | mutex_unlock(&fs_info->scrub_lock); | |
32a44789 WS |
321 | |
322 | /* | |
323 | * check if @scrubs_running=@scrubs_paused condition | |
324 | * inside wait_event() is not an atomic operation. | |
325 | * which means we may inc/dec @scrub_running/paused | |
326 | * at any time. Let's wake up @scrub_pause_wait as | |
327 | * much as we can to let commit transaction blocked less. | |
328 | */ | |
329 | wake_up(&fs_info->scrub_pause_wait); | |
330 | ||
b6bfebc1 SB |
331 | atomic_inc(&sctx->workers_pending); |
332 | } | |
333 | ||
334 | /* used for workers that require transaction commits */ | |
335 | static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx) | |
336 | { | |
337 | struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info; | |
338 | ||
339 | /* | |
340 | * see scrub_pending_trans_workers_inc() why we're pretending | |
341 | * to be paused in the scrub counters | |
342 | */ | |
343 | mutex_lock(&fs_info->scrub_lock); | |
344 | atomic_dec(&fs_info->scrubs_running); | |
345 | atomic_dec(&fs_info->scrubs_paused); | |
346 | mutex_unlock(&fs_info->scrub_lock); | |
347 | atomic_dec(&sctx->workers_pending); | |
348 | wake_up(&fs_info->scrub_pause_wait); | |
349 | wake_up(&sctx->list_wait); | |
350 | } | |
351 | ||
d9d181c1 | 352 | static void scrub_free_csums(struct scrub_ctx *sctx) |
a2de733c | 353 | { |
d9d181c1 | 354 | while (!list_empty(&sctx->csum_list)) { |
a2de733c | 355 | struct btrfs_ordered_sum *sum; |
d9d181c1 | 356 | sum = list_first_entry(&sctx->csum_list, |
a2de733c AJ |
357 | struct btrfs_ordered_sum, list); |
358 | list_del(&sum->list); | |
359 | kfree(sum); | |
360 | } | |
361 | } | |
362 | ||
d9d181c1 | 363 | static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx) |
a2de733c AJ |
364 | { |
365 | int i; | |
a2de733c | 366 | |
d9d181c1 | 367 | if (!sctx) |
a2de733c AJ |
368 | return; |
369 | ||
ff023aac SB |
370 | scrub_free_wr_ctx(&sctx->wr_ctx); |
371 | ||
b5d67f64 | 372 | /* this can happen when scrub is cancelled */ |
d9d181c1 SB |
373 | if (sctx->curr != -1) { |
374 | struct scrub_bio *sbio = sctx->bios[sctx->curr]; | |
b5d67f64 SB |
375 | |
376 | for (i = 0; i < sbio->page_count; i++) { | |
ff023aac | 377 | WARN_ON(!sbio->pagev[i]->page); |
b5d67f64 SB |
378 | scrub_block_put(sbio->pagev[i]->sblock); |
379 | } | |
380 | bio_put(sbio->bio); | |
381 | } | |
382 | ||
ff023aac | 383 | for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) { |
d9d181c1 | 384 | struct scrub_bio *sbio = sctx->bios[i]; |
a2de733c AJ |
385 | |
386 | if (!sbio) | |
387 | break; | |
a2de733c AJ |
388 | kfree(sbio); |
389 | } | |
390 | ||
d9d181c1 SB |
391 | scrub_free_csums(sctx); |
392 | kfree(sctx); | |
a2de733c AJ |
393 | } |
394 | ||
395 | static noinline_for_stack | |
63a212ab | 396 | struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace) |
a2de733c | 397 | { |
d9d181c1 | 398 | struct scrub_ctx *sctx; |
a2de733c | 399 | int i; |
a2de733c | 400 | struct btrfs_fs_info *fs_info = dev->dev_root->fs_info; |
ff023aac SB |
401 | int pages_per_rd_bio; |
402 | int ret; | |
a2de733c | 403 | |
ff023aac SB |
404 | /* |
405 | * the setting of pages_per_rd_bio is correct for scrub but might | |
406 | * be wrong for the dev_replace code where we might read from | |
407 | * different devices in the initial huge bios. However, that | |
408 | * code is able to correctly handle the case when adding a page | |
409 | * to a bio fails. | |
410 | */ | |
411 | if (dev->bdev) | |
412 | pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO, | |
413 | bio_get_nr_vecs(dev->bdev)); | |
414 | else | |
415 | pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO; | |
d9d181c1 SB |
416 | sctx = kzalloc(sizeof(*sctx), GFP_NOFS); |
417 | if (!sctx) | |
a2de733c | 418 | goto nomem; |
63a212ab | 419 | sctx->is_dev_replace = is_dev_replace; |
ff023aac | 420 | sctx->pages_per_rd_bio = pages_per_rd_bio; |
d9d181c1 | 421 | sctx->curr = -1; |
a36cf8b8 | 422 | sctx->dev_root = dev->dev_root; |
ff023aac | 423 | for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) { |
a2de733c AJ |
424 | struct scrub_bio *sbio; |
425 | ||
426 | sbio = kzalloc(sizeof(*sbio), GFP_NOFS); | |
427 | if (!sbio) | |
428 | goto nomem; | |
d9d181c1 | 429 | sctx->bios[i] = sbio; |
a2de733c | 430 | |
a2de733c | 431 | sbio->index = i; |
d9d181c1 | 432 | sbio->sctx = sctx; |
b5d67f64 | 433 | sbio->page_count = 0; |
0339ef2f QW |
434 | btrfs_init_work(&sbio->work, scrub_bio_end_io_worker, |
435 | NULL, NULL); | |
a2de733c | 436 | |
ff023aac | 437 | if (i != SCRUB_BIOS_PER_SCTX - 1) |
d9d181c1 | 438 | sctx->bios[i]->next_free = i + 1; |
0ef8e451 | 439 | else |
d9d181c1 SB |
440 | sctx->bios[i]->next_free = -1; |
441 | } | |
442 | sctx->first_free = 0; | |
443 | sctx->nodesize = dev->dev_root->nodesize; | |
444 | sctx->leafsize = dev->dev_root->leafsize; | |
445 | sctx->sectorsize = dev->dev_root->sectorsize; | |
b6bfebc1 SB |
446 | atomic_set(&sctx->bios_in_flight, 0); |
447 | atomic_set(&sctx->workers_pending, 0); | |
d9d181c1 SB |
448 | atomic_set(&sctx->cancel_req, 0); |
449 | sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy); | |
450 | INIT_LIST_HEAD(&sctx->csum_list); | |
451 | ||
452 | spin_lock_init(&sctx->list_lock); | |
453 | spin_lock_init(&sctx->stat_lock); | |
454 | init_waitqueue_head(&sctx->list_wait); | |
ff023aac SB |
455 | |
456 | ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info, | |
457 | fs_info->dev_replace.tgtdev, is_dev_replace); | |
458 | if (ret) { | |
459 | scrub_free_ctx(sctx); | |
460 | return ERR_PTR(ret); | |
461 | } | |
d9d181c1 | 462 | return sctx; |
a2de733c AJ |
463 | |
464 | nomem: | |
d9d181c1 | 465 | scrub_free_ctx(sctx); |
a2de733c AJ |
466 | return ERR_PTR(-ENOMEM); |
467 | } | |
468 | ||
ff023aac SB |
469 | static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, |
470 | void *warn_ctx) | |
558540c1 JS |
471 | { |
472 | u64 isize; | |
473 | u32 nlink; | |
474 | int ret; | |
475 | int i; | |
476 | struct extent_buffer *eb; | |
477 | struct btrfs_inode_item *inode_item; | |
ff023aac | 478 | struct scrub_warning *swarn = warn_ctx; |
558540c1 JS |
479 | struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info; |
480 | struct inode_fs_paths *ipath = NULL; | |
481 | struct btrfs_root *local_root; | |
482 | struct btrfs_key root_key; | |
483 | ||
484 | root_key.objectid = root; | |
485 | root_key.type = BTRFS_ROOT_ITEM_KEY; | |
486 | root_key.offset = (u64)-1; | |
487 | local_root = btrfs_read_fs_root_no_name(fs_info, &root_key); | |
488 | if (IS_ERR(local_root)) { | |
489 | ret = PTR_ERR(local_root); | |
490 | goto err; | |
491 | } | |
492 | ||
493 | ret = inode_item_info(inum, 0, local_root, swarn->path); | |
494 | if (ret) { | |
495 | btrfs_release_path(swarn->path); | |
496 | goto err; | |
497 | } | |
498 | ||
499 | eb = swarn->path->nodes[0]; | |
500 | inode_item = btrfs_item_ptr(eb, swarn->path->slots[0], | |
501 | struct btrfs_inode_item); | |
502 | isize = btrfs_inode_size(eb, inode_item); | |
503 | nlink = btrfs_inode_nlink(eb, inode_item); | |
504 | btrfs_release_path(swarn->path); | |
505 | ||
506 | ipath = init_ipath(4096, local_root, swarn->path); | |
26bdef54 DC |
507 | if (IS_ERR(ipath)) { |
508 | ret = PTR_ERR(ipath); | |
509 | ipath = NULL; | |
510 | goto err; | |
511 | } | |
558540c1 JS |
512 | ret = paths_from_inode(inum, ipath); |
513 | ||
514 | if (ret < 0) | |
515 | goto err; | |
516 | ||
517 | /* | |
518 | * we deliberately ignore the bit ipath might have been too small to | |
519 | * hold all of the paths here | |
520 | */ | |
521 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) | |
efe120a0 | 522 | printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev " |
558540c1 JS |
523 | "%s, sector %llu, root %llu, inode %llu, offset %llu, " |
524 | "length %llu, links %u (path: %s)\n", swarn->errstr, | |
606686ee | 525 | swarn->logical, rcu_str_deref(swarn->dev->name), |
558540c1 JS |
526 | (unsigned long long)swarn->sector, root, inum, offset, |
527 | min(isize - offset, (u64)PAGE_SIZE), nlink, | |
745c4d8e | 528 | (char *)(unsigned long)ipath->fspath->val[i]); |
558540c1 JS |
529 | |
530 | free_ipath(ipath); | |
531 | return 0; | |
532 | ||
533 | err: | |
efe120a0 | 534 | printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev " |
558540c1 JS |
535 | "%s, sector %llu, root %llu, inode %llu, offset %llu: path " |
536 | "resolving failed with ret=%d\n", swarn->errstr, | |
606686ee | 537 | swarn->logical, rcu_str_deref(swarn->dev->name), |
558540c1 JS |
538 | (unsigned long long)swarn->sector, root, inum, offset, ret); |
539 | ||
540 | free_ipath(ipath); | |
541 | return 0; | |
542 | } | |
543 | ||
b5d67f64 | 544 | static void scrub_print_warning(const char *errstr, struct scrub_block *sblock) |
558540c1 | 545 | { |
a36cf8b8 SB |
546 | struct btrfs_device *dev; |
547 | struct btrfs_fs_info *fs_info; | |
558540c1 JS |
548 | struct btrfs_path *path; |
549 | struct btrfs_key found_key; | |
550 | struct extent_buffer *eb; | |
551 | struct btrfs_extent_item *ei; | |
552 | struct scrub_warning swarn; | |
69917e43 LB |
553 | unsigned long ptr = 0; |
554 | u64 extent_item_pos; | |
555 | u64 flags = 0; | |
558540c1 | 556 | u64 ref_root; |
69917e43 | 557 | u32 item_size; |
558540c1 | 558 | u8 ref_level; |
558540c1 | 559 | const int bufsize = 4096; |
69917e43 | 560 | int ret; |
558540c1 | 561 | |
a36cf8b8 | 562 | WARN_ON(sblock->page_count < 1); |
7a9e9987 | 563 | dev = sblock->pagev[0]->dev; |
a36cf8b8 SB |
564 | fs_info = sblock->sctx->dev_root->fs_info; |
565 | ||
558540c1 JS |
566 | path = btrfs_alloc_path(); |
567 | ||
568 | swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS); | |
569 | swarn.msg_buf = kmalloc(bufsize, GFP_NOFS); | |
7a9e9987 SB |
570 | swarn.sector = (sblock->pagev[0]->physical) >> 9; |
571 | swarn.logical = sblock->pagev[0]->logical; | |
558540c1 | 572 | swarn.errstr = errstr; |
a36cf8b8 | 573 | swarn.dev = NULL; |
558540c1 JS |
574 | swarn.msg_bufsize = bufsize; |
575 | swarn.scratch_bufsize = bufsize; | |
576 | ||
577 | if (!path || !swarn.scratch_buf || !swarn.msg_buf) | |
578 | goto out; | |
579 | ||
69917e43 LB |
580 | ret = extent_from_logical(fs_info, swarn.logical, path, &found_key, |
581 | &flags); | |
558540c1 JS |
582 | if (ret < 0) |
583 | goto out; | |
584 | ||
4692cf58 | 585 | extent_item_pos = swarn.logical - found_key.objectid; |
558540c1 JS |
586 | swarn.extent_item_size = found_key.offset; |
587 | ||
588 | eb = path->nodes[0]; | |
589 | ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); | |
590 | item_size = btrfs_item_size_nr(eb, path->slots[0]); | |
591 | ||
69917e43 | 592 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
558540c1 JS |
593 | do { |
594 | ret = tree_backref_for_extent(&ptr, eb, ei, item_size, | |
595 | &ref_root, &ref_level); | |
606686ee | 596 | printk_in_rcu(KERN_WARNING |
efe120a0 | 597 | "BTRFS: %s at logical %llu on dev %s, " |
558540c1 | 598 | "sector %llu: metadata %s (level %d) in tree " |
606686ee JB |
599 | "%llu\n", errstr, swarn.logical, |
600 | rcu_str_deref(dev->name), | |
558540c1 JS |
601 | (unsigned long long)swarn.sector, |
602 | ref_level ? "node" : "leaf", | |
603 | ret < 0 ? -1 : ref_level, | |
604 | ret < 0 ? -1 : ref_root); | |
605 | } while (ret != 1); | |
d8fe29e9 | 606 | btrfs_release_path(path); |
558540c1 | 607 | } else { |
d8fe29e9 | 608 | btrfs_release_path(path); |
558540c1 | 609 | swarn.path = path; |
a36cf8b8 | 610 | swarn.dev = dev; |
7a3ae2f8 JS |
611 | iterate_extent_inodes(fs_info, found_key.objectid, |
612 | extent_item_pos, 1, | |
558540c1 JS |
613 | scrub_print_warning_inode, &swarn); |
614 | } | |
615 | ||
616 | out: | |
617 | btrfs_free_path(path); | |
618 | kfree(swarn.scratch_buf); | |
619 | kfree(swarn.msg_buf); | |
620 | } | |
621 | ||
ff023aac | 622 | static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx) |
0ef8e451 | 623 | { |
5da6fcbc | 624 | struct page *page = NULL; |
0ef8e451 | 625 | unsigned long index; |
ff023aac | 626 | struct scrub_fixup_nodatasum *fixup = fixup_ctx; |
0ef8e451 | 627 | int ret; |
5da6fcbc | 628 | int corrected = 0; |
0ef8e451 | 629 | struct btrfs_key key; |
5da6fcbc | 630 | struct inode *inode = NULL; |
6f1c3605 | 631 | struct btrfs_fs_info *fs_info; |
0ef8e451 JS |
632 | u64 end = offset + PAGE_SIZE - 1; |
633 | struct btrfs_root *local_root; | |
6f1c3605 | 634 | int srcu_index; |
0ef8e451 JS |
635 | |
636 | key.objectid = root; | |
637 | key.type = BTRFS_ROOT_ITEM_KEY; | |
638 | key.offset = (u64)-1; | |
6f1c3605 LB |
639 | |
640 | fs_info = fixup->root->fs_info; | |
641 | srcu_index = srcu_read_lock(&fs_info->subvol_srcu); | |
642 | ||
643 | local_root = btrfs_read_fs_root_no_name(fs_info, &key); | |
644 | if (IS_ERR(local_root)) { | |
645 | srcu_read_unlock(&fs_info->subvol_srcu, srcu_index); | |
0ef8e451 | 646 | return PTR_ERR(local_root); |
6f1c3605 | 647 | } |
0ef8e451 JS |
648 | |
649 | key.type = BTRFS_INODE_ITEM_KEY; | |
650 | key.objectid = inum; | |
651 | key.offset = 0; | |
6f1c3605 LB |
652 | inode = btrfs_iget(fs_info->sb, &key, local_root, NULL); |
653 | srcu_read_unlock(&fs_info->subvol_srcu, srcu_index); | |
0ef8e451 JS |
654 | if (IS_ERR(inode)) |
655 | return PTR_ERR(inode); | |
656 | ||
0ef8e451 JS |
657 | index = offset >> PAGE_CACHE_SHIFT; |
658 | ||
659 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); | |
5da6fcbc JS |
660 | if (!page) { |
661 | ret = -ENOMEM; | |
662 | goto out; | |
663 | } | |
664 | ||
665 | if (PageUptodate(page)) { | |
5da6fcbc JS |
666 | if (PageDirty(page)) { |
667 | /* | |
668 | * we need to write the data to the defect sector. the | |
669 | * data that was in that sector is not in memory, | |
670 | * because the page was modified. we must not write the | |
671 | * modified page to that sector. | |
672 | * | |
673 | * TODO: what could be done here: wait for the delalloc | |
674 | * runner to write out that page (might involve | |
675 | * COW) and see whether the sector is still | |
676 | * referenced afterwards. | |
677 | * | |
678 | * For the meantime, we'll treat this error | |
679 | * incorrectable, although there is a chance that a | |
680 | * later scrub will find the bad sector again and that | |
681 | * there's no dirty page in memory, then. | |
682 | */ | |
683 | ret = -EIO; | |
684 | goto out; | |
685 | } | |
3ec706c8 SB |
686 | fs_info = BTRFS_I(inode)->root->fs_info; |
687 | ret = repair_io_failure(fs_info, offset, PAGE_SIZE, | |
5da6fcbc JS |
688 | fixup->logical, page, |
689 | fixup->mirror_num); | |
690 | unlock_page(page); | |
691 | corrected = !ret; | |
692 | } else { | |
693 | /* | |
694 | * we need to get good data first. the general readpage path | |
695 | * will call repair_io_failure for us, we just have to make | |
696 | * sure we read the bad mirror. | |
697 | */ | |
698 | ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end, | |
699 | EXTENT_DAMAGED, GFP_NOFS); | |
700 | if (ret) { | |
701 | /* set_extent_bits should give proper error */ | |
702 | WARN_ON(ret > 0); | |
703 | if (ret > 0) | |
704 | ret = -EFAULT; | |
705 | goto out; | |
706 | } | |
707 | ||
708 | ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page, | |
709 | btrfs_get_extent, | |
710 | fixup->mirror_num); | |
711 | wait_on_page_locked(page); | |
712 | ||
713 | corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset, | |
714 | end, EXTENT_DAMAGED, 0, NULL); | |
715 | if (!corrected) | |
716 | clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end, | |
717 | EXTENT_DAMAGED, GFP_NOFS); | |
718 | } | |
719 | ||
720 | out: | |
721 | if (page) | |
722 | put_page(page); | |
723 | if (inode) | |
724 | iput(inode); | |
0ef8e451 JS |
725 | |
726 | if (ret < 0) | |
727 | return ret; | |
728 | ||
729 | if (ret == 0 && corrected) { | |
730 | /* | |
731 | * we only need to call readpage for one of the inodes belonging | |
732 | * to this extent. so make iterate_extent_inodes stop | |
733 | */ | |
734 | return 1; | |
735 | } | |
736 | ||
737 | return -EIO; | |
738 | } | |
739 | ||
0339ef2f | 740 | static void scrub_fixup_nodatasum(struct btrfs_work_struct *work) |
0ef8e451 JS |
741 | { |
742 | int ret; | |
743 | struct scrub_fixup_nodatasum *fixup; | |
d9d181c1 | 744 | struct scrub_ctx *sctx; |
0ef8e451 | 745 | struct btrfs_trans_handle *trans = NULL; |
0ef8e451 JS |
746 | struct btrfs_path *path; |
747 | int uncorrectable = 0; | |
748 | ||
749 | fixup = container_of(work, struct scrub_fixup_nodatasum, work); | |
d9d181c1 | 750 | sctx = fixup->sctx; |
0ef8e451 JS |
751 | |
752 | path = btrfs_alloc_path(); | |
753 | if (!path) { | |
d9d181c1 SB |
754 | spin_lock(&sctx->stat_lock); |
755 | ++sctx->stat.malloc_errors; | |
756 | spin_unlock(&sctx->stat_lock); | |
0ef8e451 JS |
757 | uncorrectable = 1; |
758 | goto out; | |
759 | } | |
760 | ||
761 | trans = btrfs_join_transaction(fixup->root); | |
762 | if (IS_ERR(trans)) { | |
763 | uncorrectable = 1; | |
764 | goto out; | |
765 | } | |
766 | ||
767 | /* | |
768 | * the idea is to trigger a regular read through the standard path. we | |
769 | * read a page from the (failed) logical address by specifying the | |
770 | * corresponding copynum of the failed sector. thus, that readpage is | |
771 | * expected to fail. | |
772 | * that is the point where on-the-fly error correction will kick in | |
773 | * (once it's finished) and rewrite the failed sector if a good copy | |
774 | * can be found. | |
775 | */ | |
776 | ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info, | |
777 | path, scrub_fixup_readpage, | |
778 | fixup); | |
779 | if (ret < 0) { | |
780 | uncorrectable = 1; | |
781 | goto out; | |
782 | } | |
783 | WARN_ON(ret != 1); | |
784 | ||
d9d181c1 SB |
785 | spin_lock(&sctx->stat_lock); |
786 | ++sctx->stat.corrected_errors; | |
787 | spin_unlock(&sctx->stat_lock); | |
0ef8e451 JS |
788 | |
789 | out: | |
790 | if (trans && !IS_ERR(trans)) | |
791 | btrfs_end_transaction(trans, fixup->root); | |
792 | if (uncorrectable) { | |
d9d181c1 SB |
793 | spin_lock(&sctx->stat_lock); |
794 | ++sctx->stat.uncorrectable_errors; | |
795 | spin_unlock(&sctx->stat_lock); | |
ff023aac SB |
796 | btrfs_dev_replace_stats_inc( |
797 | &sctx->dev_root->fs_info->dev_replace. | |
798 | num_uncorrectable_read_errors); | |
efe120a0 FH |
799 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
800 | "unable to fixup (nodatasum) error at logical %llu on dev %s\n", | |
c1c9ff7c | 801 | fixup->logical, rcu_str_deref(fixup->dev->name)); |
0ef8e451 JS |
802 | } |
803 | ||
804 | btrfs_free_path(path); | |
805 | kfree(fixup); | |
806 | ||
b6bfebc1 | 807 | scrub_pending_trans_workers_dec(sctx); |
0ef8e451 JS |
808 | } |
809 | ||
a2de733c | 810 | /* |
b5d67f64 SB |
811 | * scrub_handle_errored_block gets called when either verification of the |
812 | * pages failed or the bio failed to read, e.g. with EIO. In the latter | |
813 | * case, this function handles all pages in the bio, even though only one | |
814 | * may be bad. | |
815 | * The goal of this function is to repair the errored block by using the | |
816 | * contents of one of the mirrors. | |
a2de733c | 817 | */ |
b5d67f64 | 818 | static int scrub_handle_errored_block(struct scrub_block *sblock_to_check) |
a2de733c | 819 | { |
d9d181c1 | 820 | struct scrub_ctx *sctx = sblock_to_check->sctx; |
a36cf8b8 | 821 | struct btrfs_device *dev; |
b5d67f64 SB |
822 | struct btrfs_fs_info *fs_info; |
823 | u64 length; | |
824 | u64 logical; | |
825 | u64 generation; | |
826 | unsigned int failed_mirror_index; | |
827 | unsigned int is_metadata; | |
828 | unsigned int have_csum; | |
829 | u8 *csum; | |
830 | struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */ | |
831 | struct scrub_block *sblock_bad; | |
832 | int ret; | |
833 | int mirror_index; | |
834 | int page_num; | |
835 | int success; | |
558540c1 | 836 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
b5d67f64 SB |
837 | DEFAULT_RATELIMIT_BURST); |
838 | ||
839 | BUG_ON(sblock_to_check->page_count < 1); | |
a36cf8b8 | 840 | fs_info = sctx->dev_root->fs_info; |
4ded4f63 SB |
841 | if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) { |
842 | /* | |
843 | * if we find an error in a super block, we just report it. | |
844 | * They will get written with the next transaction commit | |
845 | * anyway | |
846 | */ | |
847 | spin_lock(&sctx->stat_lock); | |
848 | ++sctx->stat.super_errors; | |
849 | spin_unlock(&sctx->stat_lock); | |
850 | return 0; | |
851 | } | |
b5d67f64 | 852 | length = sblock_to_check->page_count * PAGE_SIZE; |
7a9e9987 SB |
853 | logical = sblock_to_check->pagev[0]->logical; |
854 | generation = sblock_to_check->pagev[0]->generation; | |
855 | BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1); | |
856 | failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1; | |
857 | is_metadata = !(sblock_to_check->pagev[0]->flags & | |
b5d67f64 | 858 | BTRFS_EXTENT_FLAG_DATA); |
7a9e9987 SB |
859 | have_csum = sblock_to_check->pagev[0]->have_csum; |
860 | csum = sblock_to_check->pagev[0]->csum; | |
861 | dev = sblock_to_check->pagev[0]->dev; | |
13db62b7 | 862 | |
ff023aac SB |
863 | if (sctx->is_dev_replace && !is_metadata && !have_csum) { |
864 | sblocks_for_recheck = NULL; | |
865 | goto nodatasum_case; | |
866 | } | |
867 | ||
b5d67f64 SB |
868 | /* |
869 | * read all mirrors one after the other. This includes to | |
870 | * re-read the extent or metadata block that failed (that was | |
871 | * the cause that this fixup code is called) another time, | |
872 | * page by page this time in order to know which pages | |
873 | * caused I/O errors and which ones are good (for all mirrors). | |
874 | * It is the goal to handle the situation when more than one | |
875 | * mirror contains I/O errors, but the errors do not | |
876 | * overlap, i.e. the data can be repaired by selecting the | |
877 | * pages from those mirrors without I/O error on the | |
878 | * particular pages. One example (with blocks >= 2 * PAGE_SIZE) | |
879 | * would be that mirror #1 has an I/O error on the first page, | |
880 | * the second page is good, and mirror #2 has an I/O error on | |
881 | * the second page, but the first page is good. | |
882 | * Then the first page of the first mirror can be repaired by | |
883 | * taking the first page of the second mirror, and the | |
884 | * second page of the second mirror can be repaired by | |
885 | * copying the contents of the 2nd page of the 1st mirror. | |
886 | * One more note: if the pages of one mirror contain I/O | |
887 | * errors, the checksum cannot be verified. In order to get | |
888 | * the best data for repairing, the first attempt is to find | |
889 | * a mirror without I/O errors and with a validated checksum. | |
890 | * Only if this is not possible, the pages are picked from | |
891 | * mirrors with I/O errors without considering the checksum. | |
892 | * If the latter is the case, at the end, the checksum of the | |
893 | * repaired area is verified in order to correctly maintain | |
894 | * the statistics. | |
895 | */ | |
896 | ||
897 | sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS * | |
898 | sizeof(*sblocks_for_recheck), | |
899 | GFP_NOFS); | |
900 | if (!sblocks_for_recheck) { | |
d9d181c1 SB |
901 | spin_lock(&sctx->stat_lock); |
902 | sctx->stat.malloc_errors++; | |
903 | sctx->stat.read_errors++; | |
904 | sctx->stat.uncorrectable_errors++; | |
905 | spin_unlock(&sctx->stat_lock); | |
a36cf8b8 | 906 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); |
b5d67f64 | 907 | goto out; |
a2de733c AJ |
908 | } |
909 | ||
b5d67f64 | 910 | /* setup the context, map the logical blocks and alloc the pages */ |
ff023aac | 911 | ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length, |
b5d67f64 SB |
912 | logical, sblocks_for_recheck); |
913 | if (ret) { | |
d9d181c1 SB |
914 | spin_lock(&sctx->stat_lock); |
915 | sctx->stat.read_errors++; | |
916 | sctx->stat.uncorrectable_errors++; | |
917 | spin_unlock(&sctx->stat_lock); | |
a36cf8b8 | 918 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); |
b5d67f64 SB |
919 | goto out; |
920 | } | |
921 | BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS); | |
922 | sblock_bad = sblocks_for_recheck + failed_mirror_index; | |
13db62b7 | 923 | |
b5d67f64 | 924 | /* build and submit the bios for the failed mirror, check checksums */ |
34f5c8e9 SB |
925 | scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum, |
926 | csum, generation, sctx->csum_size); | |
a2de733c | 927 | |
b5d67f64 SB |
928 | if (!sblock_bad->header_error && !sblock_bad->checksum_error && |
929 | sblock_bad->no_io_error_seen) { | |
930 | /* | |
931 | * the error disappeared after reading page by page, or | |
932 | * the area was part of a huge bio and other parts of the | |
933 | * bio caused I/O errors, or the block layer merged several | |
934 | * read requests into one and the error is caused by a | |
935 | * different bio (usually one of the two latter cases is | |
936 | * the cause) | |
937 | */ | |
d9d181c1 SB |
938 | spin_lock(&sctx->stat_lock); |
939 | sctx->stat.unverified_errors++; | |
940 | spin_unlock(&sctx->stat_lock); | |
a2de733c | 941 | |
ff023aac SB |
942 | if (sctx->is_dev_replace) |
943 | scrub_write_block_to_dev_replace(sblock_bad); | |
b5d67f64 | 944 | goto out; |
a2de733c | 945 | } |
a2de733c | 946 | |
b5d67f64 | 947 | if (!sblock_bad->no_io_error_seen) { |
d9d181c1 SB |
948 | spin_lock(&sctx->stat_lock); |
949 | sctx->stat.read_errors++; | |
950 | spin_unlock(&sctx->stat_lock); | |
b5d67f64 SB |
951 | if (__ratelimit(&_rs)) |
952 | scrub_print_warning("i/o error", sblock_to_check); | |
a36cf8b8 | 953 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); |
b5d67f64 | 954 | } else if (sblock_bad->checksum_error) { |
d9d181c1 SB |
955 | spin_lock(&sctx->stat_lock); |
956 | sctx->stat.csum_errors++; | |
957 | spin_unlock(&sctx->stat_lock); | |
b5d67f64 SB |
958 | if (__ratelimit(&_rs)) |
959 | scrub_print_warning("checksum error", sblock_to_check); | |
a36cf8b8 | 960 | btrfs_dev_stat_inc_and_print(dev, |
442a4f63 | 961 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
b5d67f64 | 962 | } else if (sblock_bad->header_error) { |
d9d181c1 SB |
963 | spin_lock(&sctx->stat_lock); |
964 | sctx->stat.verify_errors++; | |
965 | spin_unlock(&sctx->stat_lock); | |
b5d67f64 SB |
966 | if (__ratelimit(&_rs)) |
967 | scrub_print_warning("checksum/header error", | |
968 | sblock_to_check); | |
442a4f63 | 969 | if (sblock_bad->generation_error) |
a36cf8b8 | 970 | btrfs_dev_stat_inc_and_print(dev, |
442a4f63 SB |
971 | BTRFS_DEV_STAT_GENERATION_ERRS); |
972 | else | |
a36cf8b8 | 973 | btrfs_dev_stat_inc_and_print(dev, |
442a4f63 | 974 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
b5d67f64 | 975 | } |
a2de733c | 976 | |
33ef30ad ID |
977 | if (sctx->readonly) { |
978 | ASSERT(!sctx->is_dev_replace); | |
979 | goto out; | |
980 | } | |
a2de733c | 981 | |
b5d67f64 SB |
982 | if (!is_metadata && !have_csum) { |
983 | struct scrub_fixup_nodatasum *fixup_nodatasum; | |
a2de733c | 984 | |
ff023aac SB |
985 | nodatasum_case: |
986 | WARN_ON(sctx->is_dev_replace); | |
987 | ||
b5d67f64 SB |
988 | /* |
989 | * !is_metadata and !have_csum, this means that the data | |
990 | * might not be COW'ed, that it might be modified | |
991 | * concurrently. The general strategy to work on the | |
992 | * commit root does not help in the case when COW is not | |
993 | * used. | |
994 | */ | |
995 | fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS); | |
996 | if (!fixup_nodatasum) | |
997 | goto did_not_correct_error; | |
d9d181c1 | 998 | fixup_nodatasum->sctx = sctx; |
a36cf8b8 | 999 | fixup_nodatasum->dev = dev; |
b5d67f64 SB |
1000 | fixup_nodatasum->logical = logical; |
1001 | fixup_nodatasum->root = fs_info->extent_root; | |
1002 | fixup_nodatasum->mirror_num = failed_mirror_index + 1; | |
b6bfebc1 | 1003 | scrub_pending_trans_workers_inc(sctx); |
0339ef2f QW |
1004 | btrfs_init_work(&fixup_nodatasum->work, scrub_fixup_nodatasum, |
1005 | NULL, NULL); | |
1006 | btrfs_queue_work(fs_info->scrub_workers, | |
1007 | &fixup_nodatasum->work); | |
b5d67f64 | 1008 | goto out; |
a2de733c AJ |
1009 | } |
1010 | ||
b5d67f64 SB |
1011 | /* |
1012 | * now build and submit the bios for the other mirrors, check | |
cb2ced73 SB |
1013 | * checksums. |
1014 | * First try to pick the mirror which is completely without I/O | |
b5d67f64 SB |
1015 | * errors and also does not have a checksum error. |
1016 | * If one is found, and if a checksum is present, the full block | |
1017 | * that is known to contain an error is rewritten. Afterwards | |
1018 | * the block is known to be corrected. | |
1019 | * If a mirror is found which is completely correct, and no | |
1020 | * checksum is present, only those pages are rewritten that had | |
1021 | * an I/O error in the block to be repaired, since it cannot be | |
1022 | * determined, which copy of the other pages is better (and it | |
1023 | * could happen otherwise that a correct page would be | |
1024 | * overwritten by a bad one). | |
1025 | */ | |
1026 | for (mirror_index = 0; | |
1027 | mirror_index < BTRFS_MAX_MIRRORS && | |
1028 | sblocks_for_recheck[mirror_index].page_count > 0; | |
1029 | mirror_index++) { | |
cb2ced73 | 1030 | struct scrub_block *sblock_other; |
b5d67f64 | 1031 | |
cb2ced73 SB |
1032 | if (mirror_index == failed_mirror_index) |
1033 | continue; | |
1034 | sblock_other = sblocks_for_recheck + mirror_index; | |
1035 | ||
1036 | /* build and submit the bios, check checksums */ | |
34f5c8e9 SB |
1037 | scrub_recheck_block(fs_info, sblock_other, is_metadata, |
1038 | have_csum, csum, generation, | |
1039 | sctx->csum_size); | |
1040 | ||
1041 | if (!sblock_other->header_error && | |
b5d67f64 SB |
1042 | !sblock_other->checksum_error && |
1043 | sblock_other->no_io_error_seen) { | |
ff023aac SB |
1044 | if (sctx->is_dev_replace) { |
1045 | scrub_write_block_to_dev_replace(sblock_other); | |
1046 | } else { | |
1047 | int force_write = is_metadata || have_csum; | |
1048 | ||
1049 | ret = scrub_repair_block_from_good_copy( | |
1050 | sblock_bad, sblock_other, | |
1051 | force_write); | |
1052 | } | |
b5d67f64 SB |
1053 | if (0 == ret) |
1054 | goto corrected_error; | |
1055 | } | |
1056 | } | |
a2de733c AJ |
1057 | |
1058 | /* | |
ff023aac SB |
1059 | * for dev_replace, pick good pages and write to the target device. |
1060 | */ | |
1061 | if (sctx->is_dev_replace) { | |
1062 | success = 1; | |
1063 | for (page_num = 0; page_num < sblock_bad->page_count; | |
1064 | page_num++) { | |
1065 | int sub_success; | |
1066 | ||
1067 | sub_success = 0; | |
1068 | for (mirror_index = 0; | |
1069 | mirror_index < BTRFS_MAX_MIRRORS && | |
1070 | sblocks_for_recheck[mirror_index].page_count > 0; | |
1071 | mirror_index++) { | |
1072 | struct scrub_block *sblock_other = | |
1073 | sblocks_for_recheck + mirror_index; | |
1074 | struct scrub_page *page_other = | |
1075 | sblock_other->pagev[page_num]; | |
1076 | ||
1077 | if (!page_other->io_error) { | |
1078 | ret = scrub_write_page_to_dev_replace( | |
1079 | sblock_other, page_num); | |
1080 | if (ret == 0) { | |
1081 | /* succeeded for this page */ | |
1082 | sub_success = 1; | |
1083 | break; | |
1084 | } else { | |
1085 | btrfs_dev_replace_stats_inc( | |
1086 | &sctx->dev_root-> | |
1087 | fs_info->dev_replace. | |
1088 | num_write_errors); | |
1089 | } | |
1090 | } | |
1091 | } | |
1092 | ||
1093 | if (!sub_success) { | |
1094 | /* | |
1095 | * did not find a mirror to fetch the page | |
1096 | * from. scrub_write_page_to_dev_replace() | |
1097 | * handles this case (page->io_error), by | |
1098 | * filling the block with zeros before | |
1099 | * submitting the write request | |
1100 | */ | |
1101 | success = 0; | |
1102 | ret = scrub_write_page_to_dev_replace( | |
1103 | sblock_bad, page_num); | |
1104 | if (ret) | |
1105 | btrfs_dev_replace_stats_inc( | |
1106 | &sctx->dev_root->fs_info-> | |
1107 | dev_replace.num_write_errors); | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | goto out; | |
1112 | } | |
1113 | ||
1114 | /* | |
1115 | * for regular scrub, repair those pages that are errored. | |
1116 | * In case of I/O errors in the area that is supposed to be | |
b5d67f64 SB |
1117 | * repaired, continue by picking good copies of those pages. |
1118 | * Select the good pages from mirrors to rewrite bad pages from | |
1119 | * the area to fix. Afterwards verify the checksum of the block | |
1120 | * that is supposed to be repaired. This verification step is | |
1121 | * only done for the purpose of statistic counting and for the | |
1122 | * final scrub report, whether errors remain. | |
1123 | * A perfect algorithm could make use of the checksum and try | |
1124 | * all possible combinations of pages from the different mirrors | |
1125 | * until the checksum verification succeeds. For example, when | |
1126 | * the 2nd page of mirror #1 faces I/O errors, and the 2nd page | |
1127 | * of mirror #2 is readable but the final checksum test fails, | |
1128 | * then the 2nd page of mirror #3 could be tried, whether now | |
1129 | * the final checksum succeedes. But this would be a rare | |
1130 | * exception and is therefore not implemented. At least it is | |
1131 | * avoided that the good copy is overwritten. | |
1132 | * A more useful improvement would be to pick the sectors | |
1133 | * without I/O error based on sector sizes (512 bytes on legacy | |
1134 | * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one | |
1135 | * mirror could be repaired by taking 512 byte of a different | |
1136 | * mirror, even if other 512 byte sectors in the same PAGE_SIZE | |
1137 | * area are unreadable. | |
a2de733c | 1138 | */ |
a2de733c | 1139 | |
b5d67f64 SB |
1140 | /* can only fix I/O errors from here on */ |
1141 | if (sblock_bad->no_io_error_seen) | |
1142 | goto did_not_correct_error; | |
1143 | ||
1144 | success = 1; | |
1145 | for (page_num = 0; page_num < sblock_bad->page_count; page_num++) { | |
7a9e9987 | 1146 | struct scrub_page *page_bad = sblock_bad->pagev[page_num]; |
b5d67f64 SB |
1147 | |
1148 | if (!page_bad->io_error) | |
a2de733c | 1149 | continue; |
b5d67f64 SB |
1150 | |
1151 | for (mirror_index = 0; | |
1152 | mirror_index < BTRFS_MAX_MIRRORS && | |
1153 | sblocks_for_recheck[mirror_index].page_count > 0; | |
1154 | mirror_index++) { | |
1155 | struct scrub_block *sblock_other = sblocks_for_recheck + | |
1156 | mirror_index; | |
7a9e9987 SB |
1157 | struct scrub_page *page_other = sblock_other->pagev[ |
1158 | page_num]; | |
b5d67f64 SB |
1159 | |
1160 | if (!page_other->io_error) { | |
1161 | ret = scrub_repair_page_from_good_copy( | |
1162 | sblock_bad, sblock_other, page_num, 0); | |
1163 | if (0 == ret) { | |
1164 | page_bad->io_error = 0; | |
1165 | break; /* succeeded for this page */ | |
1166 | } | |
1167 | } | |
96e36920 | 1168 | } |
a2de733c | 1169 | |
b5d67f64 SB |
1170 | if (page_bad->io_error) { |
1171 | /* did not find a mirror to copy the page from */ | |
1172 | success = 0; | |
1173 | } | |
a2de733c | 1174 | } |
a2de733c | 1175 | |
b5d67f64 SB |
1176 | if (success) { |
1177 | if (is_metadata || have_csum) { | |
1178 | /* | |
1179 | * need to verify the checksum now that all | |
1180 | * sectors on disk are repaired (the write | |
1181 | * request for data to be repaired is on its way). | |
1182 | * Just be lazy and use scrub_recheck_block() | |
1183 | * which re-reads the data before the checksum | |
1184 | * is verified, but most likely the data comes out | |
1185 | * of the page cache. | |
1186 | */ | |
34f5c8e9 SB |
1187 | scrub_recheck_block(fs_info, sblock_bad, |
1188 | is_metadata, have_csum, csum, | |
1189 | generation, sctx->csum_size); | |
1190 | if (!sblock_bad->header_error && | |
b5d67f64 SB |
1191 | !sblock_bad->checksum_error && |
1192 | sblock_bad->no_io_error_seen) | |
1193 | goto corrected_error; | |
1194 | else | |
1195 | goto did_not_correct_error; | |
1196 | } else { | |
1197 | corrected_error: | |
d9d181c1 SB |
1198 | spin_lock(&sctx->stat_lock); |
1199 | sctx->stat.corrected_errors++; | |
1200 | spin_unlock(&sctx->stat_lock); | |
606686ee | 1201 | printk_ratelimited_in_rcu(KERN_ERR |
efe120a0 | 1202 | "BTRFS: fixed up error at logical %llu on dev %s\n", |
c1c9ff7c | 1203 | logical, rcu_str_deref(dev->name)); |
8628764e | 1204 | } |
b5d67f64 SB |
1205 | } else { |
1206 | did_not_correct_error: | |
d9d181c1 SB |
1207 | spin_lock(&sctx->stat_lock); |
1208 | sctx->stat.uncorrectable_errors++; | |
1209 | spin_unlock(&sctx->stat_lock); | |
606686ee | 1210 | printk_ratelimited_in_rcu(KERN_ERR |
efe120a0 | 1211 | "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n", |
c1c9ff7c | 1212 | logical, rcu_str_deref(dev->name)); |
96e36920 | 1213 | } |
a2de733c | 1214 | |
b5d67f64 SB |
1215 | out: |
1216 | if (sblocks_for_recheck) { | |
1217 | for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS; | |
1218 | mirror_index++) { | |
1219 | struct scrub_block *sblock = sblocks_for_recheck + | |
1220 | mirror_index; | |
1221 | int page_index; | |
1222 | ||
7a9e9987 SB |
1223 | for (page_index = 0; page_index < sblock->page_count; |
1224 | page_index++) { | |
1225 | sblock->pagev[page_index]->sblock = NULL; | |
1226 | scrub_page_put(sblock->pagev[page_index]); | |
1227 | } | |
b5d67f64 SB |
1228 | } |
1229 | kfree(sblocks_for_recheck); | |
1230 | } | |
a2de733c | 1231 | |
b5d67f64 SB |
1232 | return 0; |
1233 | } | |
a2de733c | 1234 | |
d9d181c1 | 1235 | static int scrub_setup_recheck_block(struct scrub_ctx *sctx, |
3ec706c8 | 1236 | struct btrfs_fs_info *fs_info, |
ff023aac | 1237 | struct scrub_block *original_sblock, |
b5d67f64 SB |
1238 | u64 length, u64 logical, |
1239 | struct scrub_block *sblocks_for_recheck) | |
1240 | { | |
1241 | int page_index; | |
1242 | int mirror_index; | |
1243 | int ret; | |
1244 | ||
1245 | /* | |
7a9e9987 | 1246 | * note: the two members ref_count and outstanding_pages |
b5d67f64 SB |
1247 | * are not used (and not set) in the blocks that are used for |
1248 | * the recheck procedure | |
1249 | */ | |
1250 | ||
1251 | page_index = 0; | |
1252 | while (length > 0) { | |
1253 | u64 sublen = min_t(u64, length, PAGE_SIZE); | |
1254 | u64 mapped_length = sublen; | |
1255 | struct btrfs_bio *bbio = NULL; | |
a2de733c | 1256 | |
b5d67f64 SB |
1257 | /* |
1258 | * with a length of PAGE_SIZE, each returned stripe | |
1259 | * represents one mirror | |
1260 | */ | |
29a8d9a0 SB |
1261 | ret = btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, logical, |
1262 | &mapped_length, &bbio, 0); | |
b5d67f64 SB |
1263 | if (ret || !bbio || mapped_length < sublen) { |
1264 | kfree(bbio); | |
1265 | return -EIO; | |
1266 | } | |
a2de733c | 1267 | |
ff023aac | 1268 | BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO); |
b5d67f64 SB |
1269 | for (mirror_index = 0; mirror_index < (int)bbio->num_stripes; |
1270 | mirror_index++) { | |
1271 | struct scrub_block *sblock; | |
1272 | struct scrub_page *page; | |
1273 | ||
1274 | if (mirror_index >= BTRFS_MAX_MIRRORS) | |
1275 | continue; | |
1276 | ||
1277 | sblock = sblocks_for_recheck + mirror_index; | |
7a9e9987 SB |
1278 | sblock->sctx = sctx; |
1279 | page = kzalloc(sizeof(*page), GFP_NOFS); | |
1280 | if (!page) { | |
1281 | leave_nomem: | |
d9d181c1 SB |
1282 | spin_lock(&sctx->stat_lock); |
1283 | sctx->stat.malloc_errors++; | |
1284 | spin_unlock(&sctx->stat_lock); | |
cf93dcce | 1285 | kfree(bbio); |
b5d67f64 SB |
1286 | return -ENOMEM; |
1287 | } | |
7a9e9987 SB |
1288 | scrub_page_get(page); |
1289 | sblock->pagev[page_index] = page; | |
1290 | page->logical = logical; | |
1291 | page->physical = bbio->stripes[mirror_index].physical; | |
ff023aac SB |
1292 | BUG_ON(page_index >= original_sblock->page_count); |
1293 | page->physical_for_dev_replace = | |
1294 | original_sblock->pagev[page_index]-> | |
1295 | physical_for_dev_replace; | |
7a9e9987 SB |
1296 | /* for missing devices, dev->bdev is NULL */ |
1297 | page->dev = bbio->stripes[mirror_index].dev; | |
1298 | page->mirror_num = mirror_index + 1; | |
b5d67f64 | 1299 | sblock->page_count++; |
7a9e9987 SB |
1300 | page->page = alloc_page(GFP_NOFS); |
1301 | if (!page->page) | |
1302 | goto leave_nomem; | |
b5d67f64 SB |
1303 | } |
1304 | kfree(bbio); | |
1305 | length -= sublen; | |
1306 | logical += sublen; | |
1307 | page_index++; | |
1308 | } | |
1309 | ||
1310 | return 0; | |
96e36920 ID |
1311 | } |
1312 | ||
b5d67f64 SB |
1313 | /* |
1314 | * this function will check the on disk data for checksum errors, header | |
1315 | * errors and read I/O errors. If any I/O errors happen, the exact pages | |
1316 | * which are errored are marked as being bad. The goal is to enable scrub | |
1317 | * to take those pages that are not errored from all the mirrors so that | |
1318 | * the pages that are errored in the just handled mirror can be repaired. | |
1319 | */ | |
34f5c8e9 SB |
1320 | static void scrub_recheck_block(struct btrfs_fs_info *fs_info, |
1321 | struct scrub_block *sblock, int is_metadata, | |
1322 | int have_csum, u8 *csum, u64 generation, | |
1323 | u16 csum_size) | |
96e36920 | 1324 | { |
b5d67f64 | 1325 | int page_num; |
96e36920 | 1326 | |
b5d67f64 SB |
1327 | sblock->no_io_error_seen = 1; |
1328 | sblock->header_error = 0; | |
1329 | sblock->checksum_error = 0; | |
96e36920 | 1330 | |
b5d67f64 SB |
1331 | for (page_num = 0; page_num < sblock->page_count; page_num++) { |
1332 | struct bio *bio; | |
7a9e9987 | 1333 | struct scrub_page *page = sblock->pagev[page_num]; |
b5d67f64 | 1334 | |
442a4f63 | 1335 | if (page->dev->bdev == NULL) { |
ea9947b4 SB |
1336 | page->io_error = 1; |
1337 | sblock->no_io_error_seen = 0; | |
1338 | continue; | |
1339 | } | |
1340 | ||
7a9e9987 | 1341 | WARN_ON(!page->page); |
9be3395b | 1342 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
34f5c8e9 SB |
1343 | if (!bio) { |
1344 | page->io_error = 1; | |
1345 | sblock->no_io_error_seen = 0; | |
1346 | continue; | |
1347 | } | |
442a4f63 | 1348 | bio->bi_bdev = page->dev->bdev; |
b5d67f64 | 1349 | bio->bi_sector = page->physical >> 9; |
b5d67f64 | 1350 | |
34f5c8e9 | 1351 | bio_add_page(bio, page->page, PAGE_SIZE, 0); |
c170bbb4 | 1352 | if (btrfsic_submit_bio_wait(READ, bio)) |
b5d67f64 | 1353 | sblock->no_io_error_seen = 0; |
c170bbb4 | 1354 | |
b5d67f64 SB |
1355 | bio_put(bio); |
1356 | } | |
96e36920 | 1357 | |
b5d67f64 SB |
1358 | if (sblock->no_io_error_seen) |
1359 | scrub_recheck_block_checksum(fs_info, sblock, is_metadata, | |
1360 | have_csum, csum, generation, | |
1361 | csum_size); | |
1362 | ||
34f5c8e9 | 1363 | return; |
a2de733c AJ |
1364 | } |
1365 | ||
b5d67f64 SB |
1366 | static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info, |
1367 | struct scrub_block *sblock, | |
1368 | int is_metadata, int have_csum, | |
1369 | const u8 *csum, u64 generation, | |
1370 | u16 csum_size) | |
a2de733c | 1371 | { |
b5d67f64 SB |
1372 | int page_num; |
1373 | u8 calculated_csum[BTRFS_CSUM_SIZE]; | |
1374 | u32 crc = ~(u32)0; | |
b5d67f64 SB |
1375 | void *mapped_buffer; |
1376 | ||
7a9e9987 | 1377 | WARN_ON(!sblock->pagev[0]->page); |
b5d67f64 SB |
1378 | if (is_metadata) { |
1379 | struct btrfs_header *h; | |
1380 | ||
7a9e9987 | 1381 | mapped_buffer = kmap_atomic(sblock->pagev[0]->page); |
b5d67f64 SB |
1382 | h = (struct btrfs_header *)mapped_buffer; |
1383 | ||
3cae210f | 1384 | if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) || |
b5d67f64 SB |
1385 | memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) || |
1386 | memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid, | |
442a4f63 | 1387 | BTRFS_UUID_SIZE)) { |
b5d67f64 | 1388 | sblock->header_error = 1; |
3cae210f | 1389 | } else if (generation != btrfs_stack_header_generation(h)) { |
442a4f63 SB |
1390 | sblock->header_error = 1; |
1391 | sblock->generation_error = 1; | |
1392 | } | |
b5d67f64 SB |
1393 | csum = h->csum; |
1394 | } else { | |
1395 | if (!have_csum) | |
1396 | return; | |
a2de733c | 1397 | |
7a9e9987 | 1398 | mapped_buffer = kmap_atomic(sblock->pagev[0]->page); |
b5d67f64 | 1399 | } |
a2de733c | 1400 | |
b5d67f64 SB |
1401 | for (page_num = 0;;) { |
1402 | if (page_num == 0 && is_metadata) | |
b0496686 | 1403 | crc = btrfs_csum_data( |
b5d67f64 SB |
1404 | ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE, |
1405 | crc, PAGE_SIZE - BTRFS_CSUM_SIZE); | |
1406 | else | |
b0496686 | 1407 | crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE); |
b5d67f64 | 1408 | |
9613bebb | 1409 | kunmap_atomic(mapped_buffer); |
b5d67f64 SB |
1410 | page_num++; |
1411 | if (page_num >= sblock->page_count) | |
1412 | break; | |
7a9e9987 | 1413 | WARN_ON(!sblock->pagev[page_num]->page); |
b5d67f64 | 1414 | |
7a9e9987 | 1415 | mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page); |
b5d67f64 SB |
1416 | } |
1417 | ||
1418 | btrfs_csum_final(crc, calculated_csum); | |
1419 | if (memcmp(calculated_csum, csum, csum_size)) | |
1420 | sblock->checksum_error = 1; | |
a2de733c AJ |
1421 | } |
1422 | ||
b5d67f64 SB |
1423 | static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad, |
1424 | struct scrub_block *sblock_good, | |
1425 | int force_write) | |
1426 | { | |
1427 | int page_num; | |
1428 | int ret = 0; | |
96e36920 | 1429 | |
b5d67f64 SB |
1430 | for (page_num = 0; page_num < sblock_bad->page_count; page_num++) { |
1431 | int ret_sub; | |
96e36920 | 1432 | |
b5d67f64 SB |
1433 | ret_sub = scrub_repair_page_from_good_copy(sblock_bad, |
1434 | sblock_good, | |
1435 | page_num, | |
1436 | force_write); | |
1437 | if (ret_sub) | |
1438 | ret = ret_sub; | |
a2de733c | 1439 | } |
b5d67f64 SB |
1440 | |
1441 | return ret; | |
1442 | } | |
1443 | ||
1444 | static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad, | |
1445 | struct scrub_block *sblock_good, | |
1446 | int page_num, int force_write) | |
1447 | { | |
7a9e9987 SB |
1448 | struct scrub_page *page_bad = sblock_bad->pagev[page_num]; |
1449 | struct scrub_page *page_good = sblock_good->pagev[page_num]; | |
b5d67f64 | 1450 | |
7a9e9987 SB |
1451 | BUG_ON(page_bad->page == NULL); |
1452 | BUG_ON(page_good->page == NULL); | |
b5d67f64 SB |
1453 | if (force_write || sblock_bad->header_error || |
1454 | sblock_bad->checksum_error || page_bad->io_error) { | |
1455 | struct bio *bio; | |
1456 | int ret; | |
b5d67f64 | 1457 | |
ff023aac | 1458 | if (!page_bad->dev->bdev) { |
efe120a0 FH |
1459 | printk_ratelimited(KERN_WARNING "BTRFS: " |
1460 | "scrub_repair_page_from_good_copy(bdev == NULL) " | |
1461 | "is unexpected!\n"); | |
ff023aac SB |
1462 | return -EIO; |
1463 | } | |
1464 | ||
9be3395b | 1465 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
e627ee7b TI |
1466 | if (!bio) |
1467 | return -EIO; | |
442a4f63 | 1468 | bio->bi_bdev = page_bad->dev->bdev; |
b5d67f64 | 1469 | bio->bi_sector = page_bad->physical >> 9; |
b5d67f64 SB |
1470 | |
1471 | ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0); | |
1472 | if (PAGE_SIZE != ret) { | |
1473 | bio_put(bio); | |
1474 | return -EIO; | |
13db62b7 | 1475 | } |
b5d67f64 | 1476 | |
c170bbb4 | 1477 | if (btrfsic_submit_bio_wait(WRITE, bio)) { |
442a4f63 SB |
1478 | btrfs_dev_stat_inc_and_print(page_bad->dev, |
1479 | BTRFS_DEV_STAT_WRITE_ERRS); | |
ff023aac SB |
1480 | btrfs_dev_replace_stats_inc( |
1481 | &sblock_bad->sctx->dev_root->fs_info-> | |
1482 | dev_replace.num_write_errors); | |
442a4f63 SB |
1483 | bio_put(bio); |
1484 | return -EIO; | |
1485 | } | |
b5d67f64 | 1486 | bio_put(bio); |
a2de733c AJ |
1487 | } |
1488 | ||
b5d67f64 SB |
1489 | return 0; |
1490 | } | |
1491 | ||
ff023aac SB |
1492 | static void scrub_write_block_to_dev_replace(struct scrub_block *sblock) |
1493 | { | |
1494 | int page_num; | |
1495 | ||
1496 | for (page_num = 0; page_num < sblock->page_count; page_num++) { | |
1497 | int ret; | |
1498 | ||
1499 | ret = scrub_write_page_to_dev_replace(sblock, page_num); | |
1500 | if (ret) | |
1501 | btrfs_dev_replace_stats_inc( | |
1502 | &sblock->sctx->dev_root->fs_info->dev_replace. | |
1503 | num_write_errors); | |
1504 | } | |
1505 | } | |
1506 | ||
1507 | static int scrub_write_page_to_dev_replace(struct scrub_block *sblock, | |
1508 | int page_num) | |
1509 | { | |
1510 | struct scrub_page *spage = sblock->pagev[page_num]; | |
1511 | ||
1512 | BUG_ON(spage->page == NULL); | |
1513 | if (spage->io_error) { | |
1514 | void *mapped_buffer = kmap_atomic(spage->page); | |
1515 | ||
1516 | memset(mapped_buffer, 0, PAGE_CACHE_SIZE); | |
1517 | flush_dcache_page(spage->page); | |
1518 | kunmap_atomic(mapped_buffer); | |
1519 | } | |
1520 | return scrub_add_page_to_wr_bio(sblock->sctx, spage); | |
1521 | } | |
1522 | ||
1523 | static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx, | |
1524 | struct scrub_page *spage) | |
1525 | { | |
1526 | struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx; | |
1527 | struct scrub_bio *sbio; | |
1528 | int ret; | |
1529 | ||
1530 | mutex_lock(&wr_ctx->wr_lock); | |
1531 | again: | |
1532 | if (!wr_ctx->wr_curr_bio) { | |
1533 | wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio), | |
1534 | GFP_NOFS); | |
1535 | if (!wr_ctx->wr_curr_bio) { | |
1536 | mutex_unlock(&wr_ctx->wr_lock); | |
1537 | return -ENOMEM; | |
1538 | } | |
1539 | wr_ctx->wr_curr_bio->sctx = sctx; | |
1540 | wr_ctx->wr_curr_bio->page_count = 0; | |
1541 | } | |
1542 | sbio = wr_ctx->wr_curr_bio; | |
1543 | if (sbio->page_count == 0) { | |
1544 | struct bio *bio; | |
1545 | ||
1546 | sbio->physical = spage->physical_for_dev_replace; | |
1547 | sbio->logical = spage->logical; | |
1548 | sbio->dev = wr_ctx->tgtdev; | |
1549 | bio = sbio->bio; | |
1550 | if (!bio) { | |
9be3395b | 1551 | bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio); |
ff023aac SB |
1552 | if (!bio) { |
1553 | mutex_unlock(&wr_ctx->wr_lock); | |
1554 | return -ENOMEM; | |
1555 | } | |
1556 | sbio->bio = bio; | |
1557 | } | |
1558 | ||
1559 | bio->bi_private = sbio; | |
1560 | bio->bi_end_io = scrub_wr_bio_end_io; | |
1561 | bio->bi_bdev = sbio->dev->bdev; | |
1562 | bio->bi_sector = sbio->physical >> 9; | |
1563 | sbio->err = 0; | |
1564 | } else if (sbio->physical + sbio->page_count * PAGE_SIZE != | |
1565 | spage->physical_for_dev_replace || | |
1566 | sbio->logical + sbio->page_count * PAGE_SIZE != | |
1567 | spage->logical) { | |
1568 | scrub_wr_submit(sctx); | |
1569 | goto again; | |
1570 | } | |
1571 | ||
1572 | ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0); | |
1573 | if (ret != PAGE_SIZE) { | |
1574 | if (sbio->page_count < 1) { | |
1575 | bio_put(sbio->bio); | |
1576 | sbio->bio = NULL; | |
1577 | mutex_unlock(&wr_ctx->wr_lock); | |
1578 | return -EIO; | |
1579 | } | |
1580 | scrub_wr_submit(sctx); | |
1581 | goto again; | |
1582 | } | |
1583 | ||
1584 | sbio->pagev[sbio->page_count] = spage; | |
1585 | scrub_page_get(spage); | |
1586 | sbio->page_count++; | |
1587 | if (sbio->page_count == wr_ctx->pages_per_wr_bio) | |
1588 | scrub_wr_submit(sctx); | |
1589 | mutex_unlock(&wr_ctx->wr_lock); | |
1590 | ||
1591 | return 0; | |
1592 | } | |
1593 | ||
1594 | static void scrub_wr_submit(struct scrub_ctx *sctx) | |
1595 | { | |
1596 | struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx; | |
1597 | struct scrub_bio *sbio; | |
1598 | ||
1599 | if (!wr_ctx->wr_curr_bio) | |
1600 | return; | |
1601 | ||
1602 | sbio = wr_ctx->wr_curr_bio; | |
1603 | wr_ctx->wr_curr_bio = NULL; | |
1604 | WARN_ON(!sbio->bio->bi_bdev); | |
1605 | scrub_pending_bio_inc(sctx); | |
1606 | /* process all writes in a single worker thread. Then the block layer | |
1607 | * orders the requests before sending them to the driver which | |
1608 | * doubled the write performance on spinning disks when measured | |
1609 | * with Linux 3.5 */ | |
1610 | btrfsic_submit_bio(WRITE, sbio->bio); | |
1611 | } | |
1612 | ||
1613 | static void scrub_wr_bio_end_io(struct bio *bio, int err) | |
1614 | { | |
1615 | struct scrub_bio *sbio = bio->bi_private; | |
1616 | struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info; | |
1617 | ||
1618 | sbio->err = err; | |
1619 | sbio->bio = bio; | |
1620 | ||
0339ef2f QW |
1621 | btrfs_init_work(&sbio->work, scrub_wr_bio_end_io_worker, NULL, NULL); |
1622 | btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work); | |
ff023aac SB |
1623 | } |
1624 | ||
0339ef2f | 1625 | static void scrub_wr_bio_end_io_worker(struct btrfs_work_struct *work) |
ff023aac SB |
1626 | { |
1627 | struct scrub_bio *sbio = container_of(work, struct scrub_bio, work); | |
1628 | struct scrub_ctx *sctx = sbio->sctx; | |
1629 | int i; | |
1630 | ||
1631 | WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO); | |
1632 | if (sbio->err) { | |
1633 | struct btrfs_dev_replace *dev_replace = | |
1634 | &sbio->sctx->dev_root->fs_info->dev_replace; | |
1635 | ||
1636 | for (i = 0; i < sbio->page_count; i++) { | |
1637 | struct scrub_page *spage = sbio->pagev[i]; | |
1638 | ||
1639 | spage->io_error = 1; | |
1640 | btrfs_dev_replace_stats_inc(&dev_replace-> | |
1641 | num_write_errors); | |
1642 | } | |
1643 | } | |
1644 | ||
1645 | for (i = 0; i < sbio->page_count; i++) | |
1646 | scrub_page_put(sbio->pagev[i]); | |
1647 | ||
1648 | bio_put(sbio->bio); | |
1649 | kfree(sbio); | |
1650 | scrub_pending_bio_dec(sctx); | |
1651 | } | |
1652 | ||
1653 | static int scrub_checksum(struct scrub_block *sblock) | |
b5d67f64 SB |
1654 | { |
1655 | u64 flags; | |
1656 | int ret; | |
1657 | ||
7a9e9987 SB |
1658 | WARN_ON(sblock->page_count < 1); |
1659 | flags = sblock->pagev[0]->flags; | |
b5d67f64 SB |
1660 | ret = 0; |
1661 | if (flags & BTRFS_EXTENT_FLAG_DATA) | |
1662 | ret = scrub_checksum_data(sblock); | |
1663 | else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) | |
1664 | ret = scrub_checksum_tree_block(sblock); | |
1665 | else if (flags & BTRFS_EXTENT_FLAG_SUPER) | |
1666 | (void)scrub_checksum_super(sblock); | |
1667 | else | |
1668 | WARN_ON(1); | |
1669 | if (ret) | |
1670 | scrub_handle_errored_block(sblock); | |
ff023aac SB |
1671 | |
1672 | return ret; | |
a2de733c AJ |
1673 | } |
1674 | ||
b5d67f64 | 1675 | static int scrub_checksum_data(struct scrub_block *sblock) |
a2de733c | 1676 | { |
d9d181c1 | 1677 | struct scrub_ctx *sctx = sblock->sctx; |
a2de733c | 1678 | u8 csum[BTRFS_CSUM_SIZE]; |
b5d67f64 SB |
1679 | u8 *on_disk_csum; |
1680 | struct page *page; | |
1681 | void *buffer; | |
a2de733c AJ |
1682 | u32 crc = ~(u32)0; |
1683 | int fail = 0; | |
b5d67f64 SB |
1684 | u64 len; |
1685 | int index; | |
a2de733c | 1686 | |
b5d67f64 | 1687 | BUG_ON(sblock->page_count < 1); |
7a9e9987 | 1688 | if (!sblock->pagev[0]->have_csum) |
a2de733c AJ |
1689 | return 0; |
1690 | ||
7a9e9987 SB |
1691 | on_disk_csum = sblock->pagev[0]->csum; |
1692 | page = sblock->pagev[0]->page; | |
9613bebb | 1693 | buffer = kmap_atomic(page); |
b5d67f64 | 1694 | |
d9d181c1 | 1695 | len = sctx->sectorsize; |
b5d67f64 SB |
1696 | index = 0; |
1697 | for (;;) { | |
1698 | u64 l = min_t(u64, len, PAGE_SIZE); | |
1699 | ||
b0496686 | 1700 | crc = btrfs_csum_data(buffer, crc, l); |
9613bebb | 1701 | kunmap_atomic(buffer); |
b5d67f64 SB |
1702 | len -= l; |
1703 | if (len == 0) | |
1704 | break; | |
1705 | index++; | |
1706 | BUG_ON(index >= sblock->page_count); | |
7a9e9987 SB |
1707 | BUG_ON(!sblock->pagev[index]->page); |
1708 | page = sblock->pagev[index]->page; | |
9613bebb | 1709 | buffer = kmap_atomic(page); |
b5d67f64 SB |
1710 | } |
1711 | ||
a2de733c | 1712 | btrfs_csum_final(crc, csum); |
d9d181c1 | 1713 | if (memcmp(csum, on_disk_csum, sctx->csum_size)) |
a2de733c AJ |
1714 | fail = 1; |
1715 | ||
a2de733c AJ |
1716 | return fail; |
1717 | } | |
1718 | ||
b5d67f64 | 1719 | static int scrub_checksum_tree_block(struct scrub_block *sblock) |
a2de733c | 1720 | { |
d9d181c1 | 1721 | struct scrub_ctx *sctx = sblock->sctx; |
a2de733c | 1722 | struct btrfs_header *h; |
a36cf8b8 | 1723 | struct btrfs_root *root = sctx->dev_root; |
a2de733c | 1724 | struct btrfs_fs_info *fs_info = root->fs_info; |
b5d67f64 SB |
1725 | u8 calculated_csum[BTRFS_CSUM_SIZE]; |
1726 | u8 on_disk_csum[BTRFS_CSUM_SIZE]; | |
1727 | struct page *page; | |
1728 | void *mapped_buffer; | |
1729 | u64 mapped_size; | |
1730 | void *p; | |
a2de733c AJ |
1731 | u32 crc = ~(u32)0; |
1732 | int fail = 0; | |
1733 | int crc_fail = 0; | |
b5d67f64 SB |
1734 | u64 len; |
1735 | int index; | |
1736 | ||
1737 | BUG_ON(sblock->page_count < 1); | |
7a9e9987 | 1738 | page = sblock->pagev[0]->page; |
9613bebb | 1739 | mapped_buffer = kmap_atomic(page); |
b5d67f64 | 1740 | h = (struct btrfs_header *)mapped_buffer; |
d9d181c1 | 1741 | memcpy(on_disk_csum, h->csum, sctx->csum_size); |
a2de733c AJ |
1742 | |
1743 | /* | |
1744 | * we don't use the getter functions here, as we | |
1745 | * a) don't have an extent buffer and | |
1746 | * b) the page is already kmapped | |
1747 | */ | |
a2de733c | 1748 | |
3cae210f | 1749 | if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h)) |
a2de733c AJ |
1750 | ++fail; |
1751 | ||
3cae210f | 1752 | if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) |
a2de733c AJ |
1753 | ++fail; |
1754 | ||
1755 | if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE)) | |
1756 | ++fail; | |
1757 | ||
1758 | if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid, | |
1759 | BTRFS_UUID_SIZE)) | |
1760 | ++fail; | |
1761 | ||
ff023aac | 1762 | WARN_ON(sctx->nodesize != sctx->leafsize); |
d9d181c1 | 1763 | len = sctx->nodesize - BTRFS_CSUM_SIZE; |
b5d67f64 SB |
1764 | mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE; |
1765 | p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE; | |
1766 | index = 0; | |
1767 | for (;;) { | |
1768 | u64 l = min_t(u64, len, mapped_size); | |
1769 | ||
b0496686 | 1770 | crc = btrfs_csum_data(p, crc, l); |
9613bebb | 1771 | kunmap_atomic(mapped_buffer); |
b5d67f64 SB |
1772 | len -= l; |
1773 | if (len == 0) | |
1774 | break; | |
1775 | index++; | |
1776 | BUG_ON(index >= sblock->page_count); | |
7a9e9987 SB |
1777 | BUG_ON(!sblock->pagev[index]->page); |
1778 | page = sblock->pagev[index]->page; | |
9613bebb | 1779 | mapped_buffer = kmap_atomic(page); |
b5d67f64 SB |
1780 | mapped_size = PAGE_SIZE; |
1781 | p = mapped_buffer; | |
1782 | } | |
1783 | ||
1784 | btrfs_csum_final(crc, calculated_csum); | |
d9d181c1 | 1785 | if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size)) |
a2de733c AJ |
1786 | ++crc_fail; |
1787 | ||
a2de733c AJ |
1788 | return fail || crc_fail; |
1789 | } | |
1790 | ||
b5d67f64 | 1791 | static int scrub_checksum_super(struct scrub_block *sblock) |
a2de733c AJ |
1792 | { |
1793 | struct btrfs_super_block *s; | |
d9d181c1 | 1794 | struct scrub_ctx *sctx = sblock->sctx; |
a36cf8b8 | 1795 | struct btrfs_root *root = sctx->dev_root; |
a2de733c | 1796 | struct btrfs_fs_info *fs_info = root->fs_info; |
b5d67f64 SB |
1797 | u8 calculated_csum[BTRFS_CSUM_SIZE]; |
1798 | u8 on_disk_csum[BTRFS_CSUM_SIZE]; | |
1799 | struct page *page; | |
1800 | void *mapped_buffer; | |
1801 | u64 mapped_size; | |
1802 | void *p; | |
a2de733c | 1803 | u32 crc = ~(u32)0; |
442a4f63 SB |
1804 | int fail_gen = 0; |
1805 | int fail_cor = 0; | |
b5d67f64 SB |
1806 | u64 len; |
1807 | int index; | |
a2de733c | 1808 | |
b5d67f64 | 1809 | BUG_ON(sblock->page_count < 1); |
7a9e9987 | 1810 | page = sblock->pagev[0]->page; |
9613bebb | 1811 | mapped_buffer = kmap_atomic(page); |
b5d67f64 | 1812 | s = (struct btrfs_super_block *)mapped_buffer; |
d9d181c1 | 1813 | memcpy(on_disk_csum, s->csum, sctx->csum_size); |
a2de733c | 1814 | |
3cae210f | 1815 | if (sblock->pagev[0]->logical != btrfs_super_bytenr(s)) |
442a4f63 | 1816 | ++fail_cor; |
a2de733c | 1817 | |
3cae210f | 1818 | if (sblock->pagev[0]->generation != btrfs_super_generation(s)) |
442a4f63 | 1819 | ++fail_gen; |
a2de733c AJ |
1820 | |
1821 | if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE)) | |
442a4f63 | 1822 | ++fail_cor; |
a2de733c | 1823 | |
b5d67f64 SB |
1824 | len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE; |
1825 | mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE; | |
1826 | p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE; | |
1827 | index = 0; | |
1828 | for (;;) { | |
1829 | u64 l = min_t(u64, len, mapped_size); | |
1830 | ||
b0496686 | 1831 | crc = btrfs_csum_data(p, crc, l); |
9613bebb | 1832 | kunmap_atomic(mapped_buffer); |
b5d67f64 SB |
1833 | len -= l; |
1834 | if (len == 0) | |
1835 | break; | |
1836 | index++; | |
1837 | BUG_ON(index >= sblock->page_count); | |
7a9e9987 SB |
1838 | BUG_ON(!sblock->pagev[index]->page); |
1839 | page = sblock->pagev[index]->page; | |
9613bebb | 1840 | mapped_buffer = kmap_atomic(page); |
b5d67f64 SB |
1841 | mapped_size = PAGE_SIZE; |
1842 | p = mapped_buffer; | |
1843 | } | |
1844 | ||
1845 | btrfs_csum_final(crc, calculated_csum); | |
d9d181c1 | 1846 | if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size)) |
442a4f63 | 1847 | ++fail_cor; |
a2de733c | 1848 | |
442a4f63 | 1849 | if (fail_cor + fail_gen) { |
a2de733c AJ |
1850 | /* |
1851 | * if we find an error in a super block, we just report it. | |
1852 | * They will get written with the next transaction commit | |
1853 | * anyway | |
1854 | */ | |
d9d181c1 SB |
1855 | spin_lock(&sctx->stat_lock); |
1856 | ++sctx->stat.super_errors; | |
1857 | spin_unlock(&sctx->stat_lock); | |
442a4f63 | 1858 | if (fail_cor) |
7a9e9987 | 1859 | btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev, |
442a4f63 SB |
1860 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
1861 | else | |
7a9e9987 | 1862 | btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev, |
442a4f63 | 1863 | BTRFS_DEV_STAT_GENERATION_ERRS); |
a2de733c AJ |
1864 | } |
1865 | ||
442a4f63 | 1866 | return fail_cor + fail_gen; |
a2de733c AJ |
1867 | } |
1868 | ||
b5d67f64 SB |
1869 | static void scrub_block_get(struct scrub_block *sblock) |
1870 | { | |
1871 | atomic_inc(&sblock->ref_count); | |
1872 | } | |
1873 | ||
1874 | static void scrub_block_put(struct scrub_block *sblock) | |
1875 | { | |
1876 | if (atomic_dec_and_test(&sblock->ref_count)) { | |
1877 | int i; | |
1878 | ||
1879 | for (i = 0; i < sblock->page_count; i++) | |
7a9e9987 | 1880 | scrub_page_put(sblock->pagev[i]); |
b5d67f64 SB |
1881 | kfree(sblock); |
1882 | } | |
1883 | } | |
1884 | ||
7a9e9987 SB |
1885 | static void scrub_page_get(struct scrub_page *spage) |
1886 | { | |
1887 | atomic_inc(&spage->ref_count); | |
1888 | } | |
1889 | ||
1890 | static void scrub_page_put(struct scrub_page *spage) | |
1891 | { | |
1892 | if (atomic_dec_and_test(&spage->ref_count)) { | |
1893 | if (spage->page) | |
1894 | __free_page(spage->page); | |
1895 | kfree(spage); | |
1896 | } | |
1897 | } | |
1898 | ||
d9d181c1 | 1899 | static void scrub_submit(struct scrub_ctx *sctx) |
a2de733c AJ |
1900 | { |
1901 | struct scrub_bio *sbio; | |
1902 | ||
d9d181c1 | 1903 | if (sctx->curr == -1) |
1623edeb | 1904 | return; |
a2de733c | 1905 | |
d9d181c1 SB |
1906 | sbio = sctx->bios[sctx->curr]; |
1907 | sctx->curr = -1; | |
b6bfebc1 | 1908 | scrub_pending_bio_inc(sctx); |
a2de733c | 1909 | |
ff023aac SB |
1910 | if (!sbio->bio->bi_bdev) { |
1911 | /* | |
1912 | * this case should not happen. If btrfs_map_block() is | |
1913 | * wrong, it could happen for dev-replace operations on | |
1914 | * missing devices when no mirrors are available, but in | |
1915 | * this case it should already fail the mount. | |
1916 | * This case is handled correctly (but _very_ slowly). | |
1917 | */ | |
1918 | printk_ratelimited(KERN_WARNING | |
efe120a0 | 1919 | "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n"); |
ff023aac SB |
1920 | bio_endio(sbio->bio, -EIO); |
1921 | } else { | |
1922 | btrfsic_submit_bio(READ, sbio->bio); | |
1923 | } | |
a2de733c AJ |
1924 | } |
1925 | ||
ff023aac SB |
1926 | static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx, |
1927 | struct scrub_page *spage) | |
a2de733c | 1928 | { |
b5d67f64 | 1929 | struct scrub_block *sblock = spage->sblock; |
a2de733c | 1930 | struct scrub_bio *sbio; |
69f4cb52 | 1931 | int ret; |
a2de733c AJ |
1932 | |
1933 | again: | |
1934 | /* | |
1935 | * grab a fresh bio or wait for one to become available | |
1936 | */ | |
d9d181c1 SB |
1937 | while (sctx->curr == -1) { |
1938 | spin_lock(&sctx->list_lock); | |
1939 | sctx->curr = sctx->first_free; | |
1940 | if (sctx->curr != -1) { | |
1941 | sctx->first_free = sctx->bios[sctx->curr]->next_free; | |
1942 | sctx->bios[sctx->curr]->next_free = -1; | |
1943 | sctx->bios[sctx->curr]->page_count = 0; | |
1944 | spin_unlock(&sctx->list_lock); | |
a2de733c | 1945 | } else { |
d9d181c1 SB |
1946 | spin_unlock(&sctx->list_lock); |
1947 | wait_event(sctx->list_wait, sctx->first_free != -1); | |
a2de733c AJ |
1948 | } |
1949 | } | |
d9d181c1 | 1950 | sbio = sctx->bios[sctx->curr]; |
b5d67f64 | 1951 | if (sbio->page_count == 0) { |
69f4cb52 AJ |
1952 | struct bio *bio; |
1953 | ||
b5d67f64 SB |
1954 | sbio->physical = spage->physical; |
1955 | sbio->logical = spage->logical; | |
a36cf8b8 | 1956 | sbio->dev = spage->dev; |
b5d67f64 SB |
1957 | bio = sbio->bio; |
1958 | if (!bio) { | |
9be3395b | 1959 | bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio); |
b5d67f64 SB |
1960 | if (!bio) |
1961 | return -ENOMEM; | |
1962 | sbio->bio = bio; | |
1963 | } | |
69f4cb52 AJ |
1964 | |
1965 | bio->bi_private = sbio; | |
1966 | bio->bi_end_io = scrub_bio_end_io; | |
a36cf8b8 SB |
1967 | bio->bi_bdev = sbio->dev->bdev; |
1968 | bio->bi_sector = sbio->physical >> 9; | |
69f4cb52 | 1969 | sbio->err = 0; |
b5d67f64 SB |
1970 | } else if (sbio->physical + sbio->page_count * PAGE_SIZE != |
1971 | spage->physical || | |
1972 | sbio->logical + sbio->page_count * PAGE_SIZE != | |
a36cf8b8 SB |
1973 | spage->logical || |
1974 | sbio->dev != spage->dev) { | |
d9d181c1 | 1975 | scrub_submit(sctx); |
a2de733c AJ |
1976 | goto again; |
1977 | } | |
69f4cb52 | 1978 | |
b5d67f64 SB |
1979 | sbio->pagev[sbio->page_count] = spage; |
1980 | ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0); | |
1981 | if (ret != PAGE_SIZE) { | |
1982 | if (sbio->page_count < 1) { | |
1983 | bio_put(sbio->bio); | |
1984 | sbio->bio = NULL; | |
1985 | return -EIO; | |
1986 | } | |
d9d181c1 | 1987 | scrub_submit(sctx); |
69f4cb52 AJ |
1988 | goto again; |
1989 | } | |
1990 | ||
ff023aac | 1991 | scrub_block_get(sblock); /* one for the page added to the bio */ |
b5d67f64 SB |
1992 | atomic_inc(&sblock->outstanding_pages); |
1993 | sbio->page_count++; | |
ff023aac | 1994 | if (sbio->page_count == sctx->pages_per_rd_bio) |
d9d181c1 | 1995 | scrub_submit(sctx); |
b5d67f64 SB |
1996 | |
1997 | return 0; | |
1998 | } | |
1999 | ||
d9d181c1 | 2000 | static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len, |
a36cf8b8 | 2001 | u64 physical, struct btrfs_device *dev, u64 flags, |
ff023aac SB |
2002 | u64 gen, int mirror_num, u8 *csum, int force, |
2003 | u64 physical_for_dev_replace) | |
b5d67f64 SB |
2004 | { |
2005 | struct scrub_block *sblock; | |
2006 | int index; | |
2007 | ||
2008 | sblock = kzalloc(sizeof(*sblock), GFP_NOFS); | |
2009 | if (!sblock) { | |
d9d181c1 SB |
2010 | spin_lock(&sctx->stat_lock); |
2011 | sctx->stat.malloc_errors++; | |
2012 | spin_unlock(&sctx->stat_lock); | |
b5d67f64 | 2013 | return -ENOMEM; |
a2de733c | 2014 | } |
b5d67f64 | 2015 | |
7a9e9987 SB |
2016 | /* one ref inside this function, plus one for each page added to |
2017 | * a bio later on */ | |
b5d67f64 | 2018 | atomic_set(&sblock->ref_count, 1); |
d9d181c1 | 2019 | sblock->sctx = sctx; |
b5d67f64 SB |
2020 | sblock->no_io_error_seen = 1; |
2021 | ||
2022 | for (index = 0; len > 0; index++) { | |
7a9e9987 | 2023 | struct scrub_page *spage; |
b5d67f64 SB |
2024 | u64 l = min_t(u64, len, PAGE_SIZE); |
2025 | ||
7a9e9987 SB |
2026 | spage = kzalloc(sizeof(*spage), GFP_NOFS); |
2027 | if (!spage) { | |
2028 | leave_nomem: | |
d9d181c1 SB |
2029 | spin_lock(&sctx->stat_lock); |
2030 | sctx->stat.malloc_errors++; | |
2031 | spin_unlock(&sctx->stat_lock); | |
7a9e9987 | 2032 | scrub_block_put(sblock); |
b5d67f64 SB |
2033 | return -ENOMEM; |
2034 | } | |
7a9e9987 SB |
2035 | BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK); |
2036 | scrub_page_get(spage); | |
2037 | sblock->pagev[index] = spage; | |
b5d67f64 | 2038 | spage->sblock = sblock; |
a36cf8b8 | 2039 | spage->dev = dev; |
b5d67f64 SB |
2040 | spage->flags = flags; |
2041 | spage->generation = gen; | |
2042 | spage->logical = logical; | |
2043 | spage->physical = physical; | |
ff023aac | 2044 | spage->physical_for_dev_replace = physical_for_dev_replace; |
b5d67f64 SB |
2045 | spage->mirror_num = mirror_num; |
2046 | if (csum) { | |
2047 | spage->have_csum = 1; | |
d9d181c1 | 2048 | memcpy(spage->csum, csum, sctx->csum_size); |
b5d67f64 SB |
2049 | } else { |
2050 | spage->have_csum = 0; | |
2051 | } | |
2052 | sblock->page_count++; | |
7a9e9987 SB |
2053 | spage->page = alloc_page(GFP_NOFS); |
2054 | if (!spage->page) | |
2055 | goto leave_nomem; | |
b5d67f64 SB |
2056 | len -= l; |
2057 | logical += l; | |
2058 | physical += l; | |
ff023aac | 2059 | physical_for_dev_replace += l; |
b5d67f64 SB |
2060 | } |
2061 | ||
7a9e9987 | 2062 | WARN_ON(sblock->page_count == 0); |
b5d67f64 | 2063 | for (index = 0; index < sblock->page_count; index++) { |
7a9e9987 | 2064 | struct scrub_page *spage = sblock->pagev[index]; |
1bc87793 AJ |
2065 | int ret; |
2066 | ||
ff023aac | 2067 | ret = scrub_add_page_to_rd_bio(sctx, spage); |
b5d67f64 SB |
2068 | if (ret) { |
2069 | scrub_block_put(sblock); | |
1bc87793 | 2070 | return ret; |
b5d67f64 | 2071 | } |
1bc87793 | 2072 | } |
a2de733c | 2073 | |
b5d67f64 | 2074 | if (force) |
d9d181c1 | 2075 | scrub_submit(sctx); |
a2de733c | 2076 | |
b5d67f64 SB |
2077 | /* last one frees, either here or in bio completion for last page */ |
2078 | scrub_block_put(sblock); | |
a2de733c AJ |
2079 | return 0; |
2080 | } | |
2081 | ||
b5d67f64 SB |
2082 | static void scrub_bio_end_io(struct bio *bio, int err) |
2083 | { | |
2084 | struct scrub_bio *sbio = bio->bi_private; | |
a36cf8b8 | 2085 | struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info; |
b5d67f64 SB |
2086 | |
2087 | sbio->err = err; | |
2088 | sbio->bio = bio; | |
2089 | ||
0339ef2f | 2090 | btrfs_queue_work(fs_info->scrub_workers, &sbio->work); |
b5d67f64 SB |
2091 | } |
2092 | ||
0339ef2f | 2093 | static void scrub_bio_end_io_worker(struct btrfs_work_struct *work) |
b5d67f64 SB |
2094 | { |
2095 | struct scrub_bio *sbio = container_of(work, struct scrub_bio, work); | |
d9d181c1 | 2096 | struct scrub_ctx *sctx = sbio->sctx; |
b5d67f64 SB |
2097 | int i; |
2098 | ||
ff023aac | 2099 | BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO); |
b5d67f64 SB |
2100 | if (sbio->err) { |
2101 | for (i = 0; i < sbio->page_count; i++) { | |
2102 | struct scrub_page *spage = sbio->pagev[i]; | |
2103 | ||
2104 | spage->io_error = 1; | |
2105 | spage->sblock->no_io_error_seen = 0; | |
2106 | } | |
2107 | } | |
2108 | ||
2109 | /* now complete the scrub_block items that have all pages completed */ | |
2110 | for (i = 0; i < sbio->page_count; i++) { | |
2111 | struct scrub_page *spage = sbio->pagev[i]; | |
2112 | struct scrub_block *sblock = spage->sblock; | |
2113 | ||
2114 | if (atomic_dec_and_test(&sblock->outstanding_pages)) | |
2115 | scrub_block_complete(sblock); | |
2116 | scrub_block_put(sblock); | |
2117 | } | |
2118 | ||
b5d67f64 SB |
2119 | bio_put(sbio->bio); |
2120 | sbio->bio = NULL; | |
d9d181c1 SB |
2121 | spin_lock(&sctx->list_lock); |
2122 | sbio->next_free = sctx->first_free; | |
2123 | sctx->first_free = sbio->index; | |
2124 | spin_unlock(&sctx->list_lock); | |
ff023aac SB |
2125 | |
2126 | if (sctx->is_dev_replace && | |
2127 | atomic_read(&sctx->wr_ctx.flush_all_writes)) { | |
2128 | mutex_lock(&sctx->wr_ctx.wr_lock); | |
2129 | scrub_wr_submit(sctx); | |
2130 | mutex_unlock(&sctx->wr_ctx.wr_lock); | |
2131 | } | |
2132 | ||
b6bfebc1 | 2133 | scrub_pending_bio_dec(sctx); |
b5d67f64 SB |
2134 | } |
2135 | ||
2136 | static void scrub_block_complete(struct scrub_block *sblock) | |
2137 | { | |
ff023aac | 2138 | if (!sblock->no_io_error_seen) { |
b5d67f64 | 2139 | scrub_handle_errored_block(sblock); |
ff023aac SB |
2140 | } else { |
2141 | /* | |
2142 | * if has checksum error, write via repair mechanism in | |
2143 | * dev replace case, otherwise write here in dev replace | |
2144 | * case. | |
2145 | */ | |
2146 | if (!scrub_checksum(sblock) && sblock->sctx->is_dev_replace) | |
2147 | scrub_write_block_to_dev_replace(sblock); | |
2148 | } | |
b5d67f64 SB |
2149 | } |
2150 | ||
d9d181c1 | 2151 | static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len, |
a2de733c AJ |
2152 | u8 *csum) |
2153 | { | |
2154 | struct btrfs_ordered_sum *sum = NULL; | |
f51a4a18 | 2155 | unsigned long index; |
a2de733c | 2156 | unsigned long num_sectors; |
a2de733c | 2157 | |
d9d181c1 SB |
2158 | while (!list_empty(&sctx->csum_list)) { |
2159 | sum = list_first_entry(&sctx->csum_list, | |
a2de733c AJ |
2160 | struct btrfs_ordered_sum, list); |
2161 | if (sum->bytenr > logical) | |
2162 | return 0; | |
2163 | if (sum->bytenr + sum->len > logical) | |
2164 | break; | |
2165 | ||
d9d181c1 | 2166 | ++sctx->stat.csum_discards; |
a2de733c AJ |
2167 | list_del(&sum->list); |
2168 | kfree(sum); | |
2169 | sum = NULL; | |
2170 | } | |
2171 | if (!sum) | |
2172 | return 0; | |
2173 | ||
f51a4a18 | 2174 | index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize; |
d9d181c1 | 2175 | num_sectors = sum->len / sctx->sectorsize; |
f51a4a18 MX |
2176 | memcpy(csum, sum->sums + index, sctx->csum_size); |
2177 | if (index == num_sectors - 1) { | |
a2de733c AJ |
2178 | list_del(&sum->list); |
2179 | kfree(sum); | |
2180 | } | |
f51a4a18 | 2181 | return 1; |
a2de733c AJ |
2182 | } |
2183 | ||
2184 | /* scrub extent tries to collect up to 64 kB for each bio */ | |
d9d181c1 | 2185 | static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len, |
a36cf8b8 | 2186 | u64 physical, struct btrfs_device *dev, u64 flags, |
ff023aac | 2187 | u64 gen, int mirror_num, u64 physical_for_dev_replace) |
a2de733c AJ |
2188 | { |
2189 | int ret; | |
2190 | u8 csum[BTRFS_CSUM_SIZE]; | |
b5d67f64 SB |
2191 | u32 blocksize; |
2192 | ||
2193 | if (flags & BTRFS_EXTENT_FLAG_DATA) { | |
d9d181c1 SB |
2194 | blocksize = sctx->sectorsize; |
2195 | spin_lock(&sctx->stat_lock); | |
2196 | sctx->stat.data_extents_scrubbed++; | |
2197 | sctx->stat.data_bytes_scrubbed += len; | |
2198 | spin_unlock(&sctx->stat_lock); | |
b5d67f64 | 2199 | } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
ff023aac | 2200 | WARN_ON(sctx->nodesize != sctx->leafsize); |
d9d181c1 SB |
2201 | blocksize = sctx->nodesize; |
2202 | spin_lock(&sctx->stat_lock); | |
2203 | sctx->stat.tree_extents_scrubbed++; | |
2204 | sctx->stat.tree_bytes_scrubbed += len; | |
2205 | spin_unlock(&sctx->stat_lock); | |
b5d67f64 | 2206 | } else { |
d9d181c1 | 2207 | blocksize = sctx->sectorsize; |
ff023aac | 2208 | WARN_ON(1); |
b5d67f64 | 2209 | } |
a2de733c AJ |
2210 | |
2211 | while (len) { | |
b5d67f64 | 2212 | u64 l = min_t(u64, len, blocksize); |
a2de733c AJ |
2213 | int have_csum = 0; |
2214 | ||
2215 | if (flags & BTRFS_EXTENT_FLAG_DATA) { | |
2216 | /* push csums to sbio */ | |
d9d181c1 | 2217 | have_csum = scrub_find_csum(sctx, logical, l, csum); |
a2de733c | 2218 | if (have_csum == 0) |
d9d181c1 | 2219 | ++sctx->stat.no_csum; |
ff023aac SB |
2220 | if (sctx->is_dev_replace && !have_csum) { |
2221 | ret = copy_nocow_pages(sctx, logical, l, | |
2222 | mirror_num, | |
2223 | physical_for_dev_replace); | |
2224 | goto behind_scrub_pages; | |
2225 | } | |
a2de733c | 2226 | } |
a36cf8b8 | 2227 | ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen, |
ff023aac SB |
2228 | mirror_num, have_csum ? csum : NULL, 0, |
2229 | physical_for_dev_replace); | |
2230 | behind_scrub_pages: | |
a2de733c AJ |
2231 | if (ret) |
2232 | return ret; | |
2233 | len -= l; | |
2234 | logical += l; | |
2235 | physical += l; | |
ff023aac | 2236 | physical_for_dev_replace += l; |
a2de733c AJ |
2237 | } |
2238 | return 0; | |
2239 | } | |
2240 | ||
d9d181c1 | 2241 | static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, |
a36cf8b8 SB |
2242 | struct map_lookup *map, |
2243 | struct btrfs_device *scrub_dev, | |
ff023aac SB |
2244 | int num, u64 base, u64 length, |
2245 | int is_dev_replace) | |
a2de733c AJ |
2246 | { |
2247 | struct btrfs_path *path; | |
a36cf8b8 | 2248 | struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info; |
a2de733c AJ |
2249 | struct btrfs_root *root = fs_info->extent_root; |
2250 | struct btrfs_root *csum_root = fs_info->csum_root; | |
2251 | struct btrfs_extent_item *extent; | |
e7786c3a | 2252 | struct blk_plug plug; |
a2de733c AJ |
2253 | u64 flags; |
2254 | int ret; | |
2255 | int slot; | |
a2de733c | 2256 | u64 nstripes; |
a2de733c AJ |
2257 | struct extent_buffer *l; |
2258 | struct btrfs_key key; | |
2259 | u64 physical; | |
2260 | u64 logical; | |
625f1c8d | 2261 | u64 logic_end; |
a2de733c | 2262 | u64 generation; |
e12fa9cd | 2263 | int mirror_num; |
7a26285e AJ |
2264 | struct reada_control *reada1; |
2265 | struct reada_control *reada2; | |
2266 | struct btrfs_key key_start; | |
2267 | struct btrfs_key key_end; | |
a2de733c AJ |
2268 | u64 increment = map->stripe_len; |
2269 | u64 offset; | |
ff023aac SB |
2270 | u64 extent_logical; |
2271 | u64 extent_physical; | |
2272 | u64 extent_len; | |
2273 | struct btrfs_device *extent_dev; | |
2274 | int extent_mirror_num; | |
625f1c8d | 2275 | int stop_loop; |
a2de733c | 2276 | |
53b381b3 DW |
2277 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
2278 | BTRFS_BLOCK_GROUP_RAID6)) { | |
2279 | if (num >= nr_data_stripes(map)) { | |
2280 | return 0; | |
2281 | } | |
2282 | } | |
2283 | ||
a2de733c AJ |
2284 | nstripes = length; |
2285 | offset = 0; | |
2286 | do_div(nstripes, map->stripe_len); | |
2287 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { | |
2288 | offset = map->stripe_len * num; | |
2289 | increment = map->stripe_len * map->num_stripes; | |
193ea74b | 2290 | mirror_num = 1; |
a2de733c AJ |
2291 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
2292 | int factor = map->num_stripes / map->sub_stripes; | |
2293 | offset = map->stripe_len * (num / map->sub_stripes); | |
2294 | increment = map->stripe_len * factor; | |
193ea74b | 2295 | mirror_num = num % map->sub_stripes + 1; |
a2de733c AJ |
2296 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
2297 | increment = map->stripe_len; | |
193ea74b | 2298 | mirror_num = num % map->num_stripes + 1; |
a2de733c AJ |
2299 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
2300 | increment = map->stripe_len; | |
193ea74b | 2301 | mirror_num = num % map->num_stripes + 1; |
a2de733c AJ |
2302 | } else { |
2303 | increment = map->stripe_len; | |
193ea74b | 2304 | mirror_num = 1; |
a2de733c AJ |
2305 | } |
2306 | ||
2307 | path = btrfs_alloc_path(); | |
2308 | if (!path) | |
2309 | return -ENOMEM; | |
2310 | ||
b5d67f64 SB |
2311 | /* |
2312 | * work on commit root. The related disk blocks are static as | |
2313 | * long as COW is applied. This means, it is save to rewrite | |
2314 | * them to repair disk errors without any race conditions | |
2315 | */ | |
a2de733c AJ |
2316 | path->search_commit_root = 1; |
2317 | path->skip_locking = 1; | |
2318 | ||
2319 | /* | |
7a26285e AJ |
2320 | * trigger the readahead for extent tree csum tree and wait for |
2321 | * completion. During readahead, the scrub is officially paused | |
2322 | * to not hold off transaction commits | |
a2de733c AJ |
2323 | */ |
2324 | logical = base + offset; | |
a2de733c | 2325 | |
d9d181c1 | 2326 | wait_event(sctx->list_wait, |
b6bfebc1 | 2327 | atomic_read(&sctx->bios_in_flight) == 0); |
cb7ab021 | 2328 | scrub_blocked_if_needed(fs_info); |
7a26285e AJ |
2329 | |
2330 | /* FIXME it might be better to start readahead at commit root */ | |
2331 | key_start.objectid = logical; | |
2332 | key_start.type = BTRFS_EXTENT_ITEM_KEY; | |
2333 | key_start.offset = (u64)0; | |
2334 | key_end.objectid = base + offset + nstripes * increment; | |
3173a18f JB |
2335 | key_end.type = BTRFS_METADATA_ITEM_KEY; |
2336 | key_end.offset = (u64)-1; | |
7a26285e AJ |
2337 | reada1 = btrfs_reada_add(root, &key_start, &key_end); |
2338 | ||
2339 | key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID; | |
2340 | key_start.type = BTRFS_EXTENT_CSUM_KEY; | |
2341 | key_start.offset = logical; | |
2342 | key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID; | |
2343 | key_end.type = BTRFS_EXTENT_CSUM_KEY; | |
2344 | key_end.offset = base + offset + nstripes * increment; | |
2345 | reada2 = btrfs_reada_add(csum_root, &key_start, &key_end); | |
2346 | ||
2347 | if (!IS_ERR(reada1)) | |
2348 | btrfs_reada_wait(reada1); | |
2349 | if (!IS_ERR(reada2)) | |
2350 | btrfs_reada_wait(reada2); | |
2351 | ||
a2de733c AJ |
2352 | |
2353 | /* | |
2354 | * collect all data csums for the stripe to avoid seeking during | |
2355 | * the scrub. This might currently (crc32) end up to be about 1MB | |
2356 | */ | |
e7786c3a | 2357 | blk_start_plug(&plug); |
a2de733c | 2358 | |
a2de733c AJ |
2359 | /* |
2360 | * now find all extents for each stripe and scrub them | |
2361 | */ | |
7a26285e AJ |
2362 | logical = base + offset; |
2363 | physical = map->stripes[num].physical; | |
625f1c8d | 2364 | logic_end = logical + increment * nstripes; |
a2de733c | 2365 | ret = 0; |
625f1c8d | 2366 | while (logical < logic_end) { |
a2de733c AJ |
2367 | /* |
2368 | * canceled? | |
2369 | */ | |
2370 | if (atomic_read(&fs_info->scrub_cancel_req) || | |
d9d181c1 | 2371 | atomic_read(&sctx->cancel_req)) { |
a2de733c AJ |
2372 | ret = -ECANCELED; |
2373 | goto out; | |
2374 | } | |
2375 | /* | |
2376 | * check to see if we have to pause | |
2377 | */ | |
2378 | if (atomic_read(&fs_info->scrub_pause_req)) { | |
2379 | /* push queued extents */ | |
ff023aac | 2380 | atomic_set(&sctx->wr_ctx.flush_all_writes, 1); |
d9d181c1 | 2381 | scrub_submit(sctx); |
ff023aac SB |
2382 | mutex_lock(&sctx->wr_ctx.wr_lock); |
2383 | scrub_wr_submit(sctx); | |
2384 | mutex_unlock(&sctx->wr_ctx.wr_lock); | |
d9d181c1 | 2385 | wait_event(sctx->list_wait, |
b6bfebc1 | 2386 | atomic_read(&sctx->bios_in_flight) == 0); |
ff023aac | 2387 | atomic_set(&sctx->wr_ctx.flush_all_writes, 0); |
3cb0929a | 2388 | scrub_blocked_if_needed(fs_info); |
a2de733c AJ |
2389 | } |
2390 | ||
7c76edb7 WS |
2391 | if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
2392 | key.type = BTRFS_METADATA_ITEM_KEY; | |
2393 | else | |
2394 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
a2de733c | 2395 | key.objectid = logical; |
625f1c8d | 2396 | key.offset = (u64)-1; |
a2de733c AJ |
2397 | |
2398 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2399 | if (ret < 0) | |
2400 | goto out; | |
3173a18f | 2401 | |
8c51032f | 2402 | if (ret > 0) { |
ade2e0b3 | 2403 | ret = btrfs_previous_extent_item(root, path, 0); |
a2de733c AJ |
2404 | if (ret < 0) |
2405 | goto out; | |
8c51032f AJ |
2406 | if (ret > 0) { |
2407 | /* there's no smaller item, so stick with the | |
2408 | * larger one */ | |
2409 | btrfs_release_path(path); | |
2410 | ret = btrfs_search_slot(NULL, root, &key, | |
2411 | path, 0, 0); | |
2412 | if (ret < 0) | |
2413 | goto out; | |
2414 | } | |
a2de733c AJ |
2415 | } |
2416 | ||
625f1c8d | 2417 | stop_loop = 0; |
a2de733c | 2418 | while (1) { |
3173a18f JB |
2419 | u64 bytes; |
2420 | ||
a2de733c AJ |
2421 | l = path->nodes[0]; |
2422 | slot = path->slots[0]; | |
2423 | if (slot >= btrfs_header_nritems(l)) { | |
2424 | ret = btrfs_next_leaf(root, path); | |
2425 | if (ret == 0) | |
2426 | continue; | |
2427 | if (ret < 0) | |
2428 | goto out; | |
2429 | ||
625f1c8d | 2430 | stop_loop = 1; |
a2de733c AJ |
2431 | break; |
2432 | } | |
2433 | btrfs_item_key_to_cpu(l, &key, slot); | |
2434 | ||
3173a18f JB |
2435 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
2436 | bytes = root->leafsize; | |
2437 | else | |
2438 | bytes = key.offset; | |
2439 | ||
2440 | if (key.objectid + bytes <= logical) | |
a2de733c AJ |
2441 | goto next; |
2442 | ||
625f1c8d LB |
2443 | if (key.type != BTRFS_EXTENT_ITEM_KEY && |
2444 | key.type != BTRFS_METADATA_ITEM_KEY) | |
2445 | goto next; | |
a2de733c | 2446 | |
625f1c8d LB |
2447 | if (key.objectid >= logical + map->stripe_len) { |
2448 | /* out of this device extent */ | |
2449 | if (key.objectid >= logic_end) | |
2450 | stop_loop = 1; | |
2451 | break; | |
2452 | } | |
a2de733c AJ |
2453 | |
2454 | extent = btrfs_item_ptr(l, slot, | |
2455 | struct btrfs_extent_item); | |
2456 | flags = btrfs_extent_flags(l, extent); | |
2457 | generation = btrfs_extent_generation(l, extent); | |
2458 | ||
2459 | if (key.objectid < logical && | |
2460 | (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) { | |
efe120a0 FH |
2461 | btrfs_err(fs_info, |
2462 | "scrub: tree block %llu spanning " | |
2463 | "stripes, ignored. logical=%llu", | |
c1c9ff7c | 2464 | key.objectid, logical); |
a2de733c AJ |
2465 | goto next; |
2466 | } | |
2467 | ||
625f1c8d LB |
2468 | again: |
2469 | extent_logical = key.objectid; | |
2470 | extent_len = bytes; | |
2471 | ||
a2de733c AJ |
2472 | /* |
2473 | * trim extent to this stripe | |
2474 | */ | |
625f1c8d LB |
2475 | if (extent_logical < logical) { |
2476 | extent_len -= logical - extent_logical; | |
2477 | extent_logical = logical; | |
a2de733c | 2478 | } |
625f1c8d | 2479 | if (extent_logical + extent_len > |
a2de733c | 2480 | logical + map->stripe_len) { |
625f1c8d LB |
2481 | extent_len = logical + map->stripe_len - |
2482 | extent_logical; | |
a2de733c AJ |
2483 | } |
2484 | ||
625f1c8d | 2485 | extent_physical = extent_logical - logical + physical; |
ff023aac SB |
2486 | extent_dev = scrub_dev; |
2487 | extent_mirror_num = mirror_num; | |
2488 | if (is_dev_replace) | |
2489 | scrub_remap_extent(fs_info, extent_logical, | |
2490 | extent_len, &extent_physical, | |
2491 | &extent_dev, | |
2492 | &extent_mirror_num); | |
625f1c8d LB |
2493 | |
2494 | ret = btrfs_lookup_csums_range(csum_root, logical, | |
2495 | logical + map->stripe_len - 1, | |
2496 | &sctx->csum_list, 1); | |
2497 | if (ret) | |
2498 | goto out; | |
2499 | ||
ff023aac SB |
2500 | ret = scrub_extent(sctx, extent_logical, extent_len, |
2501 | extent_physical, extent_dev, flags, | |
2502 | generation, extent_mirror_num, | |
115930cb | 2503 | extent_logical - logical + physical); |
a2de733c AJ |
2504 | if (ret) |
2505 | goto out; | |
2506 | ||
d88d46c6 | 2507 | scrub_free_csums(sctx); |
625f1c8d LB |
2508 | if (extent_logical + extent_len < |
2509 | key.objectid + bytes) { | |
2510 | logical += increment; | |
2511 | physical += map->stripe_len; | |
2512 | ||
2513 | if (logical < key.objectid + bytes) { | |
2514 | cond_resched(); | |
2515 | goto again; | |
2516 | } | |
2517 | ||
2518 | if (logical >= logic_end) { | |
2519 | stop_loop = 1; | |
2520 | break; | |
2521 | } | |
2522 | } | |
a2de733c AJ |
2523 | next: |
2524 | path->slots[0]++; | |
2525 | } | |
71267333 | 2526 | btrfs_release_path(path); |
a2de733c AJ |
2527 | logical += increment; |
2528 | physical += map->stripe_len; | |
d9d181c1 | 2529 | spin_lock(&sctx->stat_lock); |
625f1c8d LB |
2530 | if (stop_loop) |
2531 | sctx->stat.last_physical = map->stripes[num].physical + | |
2532 | length; | |
2533 | else | |
2534 | sctx->stat.last_physical = physical; | |
d9d181c1 | 2535 | spin_unlock(&sctx->stat_lock); |
625f1c8d LB |
2536 | if (stop_loop) |
2537 | break; | |
a2de733c | 2538 | } |
ff023aac | 2539 | out: |
a2de733c | 2540 | /* push queued extents */ |
d9d181c1 | 2541 | scrub_submit(sctx); |
ff023aac SB |
2542 | mutex_lock(&sctx->wr_ctx.wr_lock); |
2543 | scrub_wr_submit(sctx); | |
2544 | mutex_unlock(&sctx->wr_ctx.wr_lock); | |
a2de733c | 2545 | |
e7786c3a | 2546 | blk_finish_plug(&plug); |
a2de733c AJ |
2547 | btrfs_free_path(path); |
2548 | return ret < 0 ? ret : 0; | |
2549 | } | |
2550 | ||
d9d181c1 | 2551 | static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx, |
a36cf8b8 SB |
2552 | struct btrfs_device *scrub_dev, |
2553 | u64 chunk_tree, u64 chunk_objectid, | |
2554 | u64 chunk_offset, u64 length, | |
ff023aac | 2555 | u64 dev_offset, int is_dev_replace) |
a2de733c AJ |
2556 | { |
2557 | struct btrfs_mapping_tree *map_tree = | |
a36cf8b8 | 2558 | &sctx->dev_root->fs_info->mapping_tree; |
a2de733c AJ |
2559 | struct map_lookup *map; |
2560 | struct extent_map *em; | |
2561 | int i; | |
ff023aac | 2562 | int ret = 0; |
a2de733c AJ |
2563 | |
2564 | read_lock(&map_tree->map_tree.lock); | |
2565 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); | |
2566 | read_unlock(&map_tree->map_tree.lock); | |
2567 | ||
2568 | if (!em) | |
2569 | return -EINVAL; | |
2570 | ||
2571 | map = (struct map_lookup *)em->bdev; | |
2572 | if (em->start != chunk_offset) | |
2573 | goto out; | |
2574 | ||
2575 | if (em->len < length) | |
2576 | goto out; | |
2577 | ||
2578 | for (i = 0; i < map->num_stripes; ++i) { | |
a36cf8b8 | 2579 | if (map->stripes[i].dev->bdev == scrub_dev->bdev && |
859acaf1 | 2580 | map->stripes[i].physical == dev_offset) { |
a36cf8b8 | 2581 | ret = scrub_stripe(sctx, map, scrub_dev, i, |
ff023aac SB |
2582 | chunk_offset, length, |
2583 | is_dev_replace); | |
a2de733c AJ |
2584 | if (ret) |
2585 | goto out; | |
2586 | } | |
2587 | } | |
2588 | out: | |
2589 | free_extent_map(em); | |
2590 | ||
2591 | return ret; | |
2592 | } | |
2593 | ||
2594 | static noinline_for_stack | |
a36cf8b8 | 2595 | int scrub_enumerate_chunks(struct scrub_ctx *sctx, |
ff023aac SB |
2596 | struct btrfs_device *scrub_dev, u64 start, u64 end, |
2597 | int is_dev_replace) | |
a2de733c AJ |
2598 | { |
2599 | struct btrfs_dev_extent *dev_extent = NULL; | |
2600 | struct btrfs_path *path; | |
a36cf8b8 | 2601 | struct btrfs_root *root = sctx->dev_root; |
a2de733c AJ |
2602 | struct btrfs_fs_info *fs_info = root->fs_info; |
2603 | u64 length; | |
2604 | u64 chunk_tree; | |
2605 | u64 chunk_objectid; | |
2606 | u64 chunk_offset; | |
2607 | int ret; | |
2608 | int slot; | |
2609 | struct extent_buffer *l; | |
2610 | struct btrfs_key key; | |
2611 | struct btrfs_key found_key; | |
2612 | struct btrfs_block_group_cache *cache; | |
ff023aac | 2613 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
a2de733c AJ |
2614 | |
2615 | path = btrfs_alloc_path(); | |
2616 | if (!path) | |
2617 | return -ENOMEM; | |
2618 | ||
2619 | path->reada = 2; | |
2620 | path->search_commit_root = 1; | |
2621 | path->skip_locking = 1; | |
2622 | ||
a36cf8b8 | 2623 | key.objectid = scrub_dev->devid; |
a2de733c AJ |
2624 | key.offset = 0ull; |
2625 | key.type = BTRFS_DEV_EXTENT_KEY; | |
2626 | ||
a2de733c AJ |
2627 | while (1) { |
2628 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2629 | if (ret < 0) | |
8c51032f AJ |
2630 | break; |
2631 | if (ret > 0) { | |
2632 | if (path->slots[0] >= | |
2633 | btrfs_header_nritems(path->nodes[0])) { | |
2634 | ret = btrfs_next_leaf(root, path); | |
2635 | if (ret) | |
2636 | break; | |
2637 | } | |
2638 | } | |
a2de733c AJ |
2639 | |
2640 | l = path->nodes[0]; | |
2641 | slot = path->slots[0]; | |
2642 | ||
2643 | btrfs_item_key_to_cpu(l, &found_key, slot); | |
2644 | ||
a36cf8b8 | 2645 | if (found_key.objectid != scrub_dev->devid) |
a2de733c AJ |
2646 | break; |
2647 | ||
8c51032f | 2648 | if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY) |
a2de733c AJ |
2649 | break; |
2650 | ||
2651 | if (found_key.offset >= end) | |
2652 | break; | |
2653 | ||
2654 | if (found_key.offset < key.offset) | |
2655 | break; | |
2656 | ||
2657 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
2658 | length = btrfs_dev_extent_length(l, dev_extent); | |
2659 | ||
2660 | if (found_key.offset + length <= start) { | |
2661 | key.offset = found_key.offset + length; | |
71267333 | 2662 | btrfs_release_path(path); |
a2de733c AJ |
2663 | continue; |
2664 | } | |
2665 | ||
2666 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); | |
2667 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); | |
2668 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); | |
2669 | ||
2670 | /* | |
2671 | * get a reference on the corresponding block group to prevent | |
2672 | * the chunk from going away while we scrub it | |
2673 | */ | |
2674 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); | |
2675 | if (!cache) { | |
2676 | ret = -ENOENT; | |
8c51032f | 2677 | break; |
a2de733c | 2678 | } |
ff023aac SB |
2679 | dev_replace->cursor_right = found_key.offset + length; |
2680 | dev_replace->cursor_left = found_key.offset; | |
2681 | dev_replace->item_needs_writeback = 1; | |
a36cf8b8 | 2682 | ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid, |
ff023aac SB |
2683 | chunk_offset, length, found_key.offset, |
2684 | is_dev_replace); | |
2685 | ||
2686 | /* | |
2687 | * flush, submit all pending read and write bios, afterwards | |
2688 | * wait for them. | |
2689 | * Note that in the dev replace case, a read request causes | |
2690 | * write requests that are submitted in the read completion | |
2691 | * worker. Therefore in the current situation, it is required | |
2692 | * that all write requests are flushed, so that all read and | |
2693 | * write requests are really completed when bios_in_flight | |
2694 | * changes to 0. | |
2695 | */ | |
2696 | atomic_set(&sctx->wr_ctx.flush_all_writes, 1); | |
2697 | scrub_submit(sctx); | |
2698 | mutex_lock(&sctx->wr_ctx.wr_lock); | |
2699 | scrub_wr_submit(sctx); | |
2700 | mutex_unlock(&sctx->wr_ctx.wr_lock); | |
2701 | ||
2702 | wait_event(sctx->list_wait, | |
2703 | atomic_read(&sctx->bios_in_flight) == 0); | |
12cf9372 WS |
2704 | atomic_inc(&fs_info->scrubs_paused); |
2705 | wake_up(&fs_info->scrub_pause_wait); | |
2706 | ||
2707 | /* | |
2708 | * must be called before we decrease @scrub_paused. | |
2709 | * make sure we don't block transaction commit while | |
2710 | * we are waiting pending workers finished. | |
2711 | */ | |
ff023aac SB |
2712 | wait_event(sctx->list_wait, |
2713 | atomic_read(&sctx->workers_pending) == 0); | |
12cf9372 WS |
2714 | atomic_set(&sctx->wr_ctx.flush_all_writes, 0); |
2715 | ||
2716 | mutex_lock(&fs_info->scrub_lock); | |
2717 | __scrub_blocked_if_needed(fs_info); | |
2718 | atomic_dec(&fs_info->scrubs_paused); | |
2719 | mutex_unlock(&fs_info->scrub_lock); | |
2720 | wake_up(&fs_info->scrub_pause_wait); | |
ff023aac | 2721 | |
a2de733c AJ |
2722 | btrfs_put_block_group(cache); |
2723 | if (ret) | |
2724 | break; | |
af1be4f8 SB |
2725 | if (is_dev_replace && |
2726 | atomic64_read(&dev_replace->num_write_errors) > 0) { | |
ff023aac SB |
2727 | ret = -EIO; |
2728 | break; | |
2729 | } | |
2730 | if (sctx->stat.malloc_errors > 0) { | |
2731 | ret = -ENOMEM; | |
2732 | break; | |
2733 | } | |
a2de733c | 2734 | |
539f358a ID |
2735 | dev_replace->cursor_left = dev_replace->cursor_right; |
2736 | dev_replace->item_needs_writeback = 1; | |
2737 | ||
a2de733c | 2738 | key.offset = found_key.offset + length; |
71267333 | 2739 | btrfs_release_path(path); |
a2de733c AJ |
2740 | } |
2741 | ||
a2de733c | 2742 | btrfs_free_path(path); |
8c51032f AJ |
2743 | |
2744 | /* | |
2745 | * ret can still be 1 from search_slot or next_leaf, | |
2746 | * that's not an error | |
2747 | */ | |
2748 | return ret < 0 ? ret : 0; | |
a2de733c AJ |
2749 | } |
2750 | ||
a36cf8b8 SB |
2751 | static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx, |
2752 | struct btrfs_device *scrub_dev) | |
a2de733c AJ |
2753 | { |
2754 | int i; | |
2755 | u64 bytenr; | |
2756 | u64 gen; | |
2757 | int ret; | |
a36cf8b8 | 2758 | struct btrfs_root *root = sctx->dev_root; |
a2de733c | 2759 | |
87533c47 | 2760 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
79787eaa JM |
2761 | return -EIO; |
2762 | ||
a2de733c AJ |
2763 | gen = root->fs_info->last_trans_committed; |
2764 | ||
2765 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { | |
2766 | bytenr = btrfs_sb_offset(i); | |
a36cf8b8 | 2767 | if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes) |
a2de733c AJ |
2768 | break; |
2769 | ||
d9d181c1 | 2770 | ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr, |
a36cf8b8 | 2771 | scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i, |
ff023aac | 2772 | NULL, 1, bytenr); |
a2de733c AJ |
2773 | if (ret) |
2774 | return ret; | |
2775 | } | |
b6bfebc1 | 2776 | wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0); |
a2de733c AJ |
2777 | |
2778 | return 0; | |
2779 | } | |
2780 | ||
2781 | /* | |
2782 | * get a reference count on fs_info->scrub_workers. start worker if necessary | |
2783 | */ | |
ff023aac SB |
2784 | static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info, |
2785 | int is_dev_replace) | |
a2de733c | 2786 | { |
0dc3b84a | 2787 | int ret = 0; |
0339ef2f QW |
2788 | int flags = WQ_FREEZABLE | WQ_UNBOUND; |
2789 | int max_active = fs_info->thread_pool_size; | |
a2de733c | 2790 | |
632dd772 | 2791 | if (fs_info->scrub_workers_refcnt == 0) { |
ff023aac | 2792 | if (is_dev_replace) |
0339ef2f QW |
2793 | fs_info->scrub_workers = |
2794 | btrfs_alloc_workqueue("btrfs-scrub", flags, | |
2795 | 1, 4); | |
ff023aac | 2796 | else |
0339ef2f QW |
2797 | fs_info->scrub_workers = |
2798 | btrfs_alloc_workqueue("btrfs-scrub", flags, | |
2799 | max_active, 4); | |
2800 | if (!fs_info->scrub_workers) { | |
2801 | ret = -ENOMEM; | |
0dc3b84a | 2802 | goto out; |
0339ef2f QW |
2803 | } |
2804 | fs_info->scrub_wr_completion_workers = | |
2805 | btrfs_alloc_workqueue("btrfs-scrubwrc", flags, | |
2806 | max_active, 2); | |
2807 | if (!fs_info->scrub_wr_completion_workers) { | |
2808 | ret = -ENOMEM; | |
ff023aac | 2809 | goto out; |
0339ef2f QW |
2810 | } |
2811 | fs_info->scrub_nocow_workers = | |
2812 | btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0); | |
2813 | if (!fs_info->scrub_nocow_workers) { | |
2814 | ret = -ENOMEM; | |
ff023aac | 2815 | goto out; |
0339ef2f | 2816 | } |
632dd772 | 2817 | } |
a2de733c | 2818 | ++fs_info->scrub_workers_refcnt; |
0dc3b84a | 2819 | out: |
0dc3b84a | 2820 | return ret; |
a2de733c AJ |
2821 | } |
2822 | ||
aa1b8cd4 | 2823 | static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info) |
a2de733c | 2824 | { |
ff023aac | 2825 | if (--fs_info->scrub_workers_refcnt == 0) { |
0339ef2f QW |
2826 | btrfs_destroy_workqueue(fs_info->scrub_workers); |
2827 | btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers); | |
2828 | btrfs_destroy_workqueue(fs_info->scrub_nocow_workers); | |
ff023aac | 2829 | } |
a2de733c | 2830 | WARN_ON(fs_info->scrub_workers_refcnt < 0); |
a2de733c AJ |
2831 | } |
2832 | ||
aa1b8cd4 SB |
2833 | int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, |
2834 | u64 end, struct btrfs_scrub_progress *progress, | |
63a212ab | 2835 | int readonly, int is_dev_replace) |
a2de733c | 2836 | { |
d9d181c1 | 2837 | struct scrub_ctx *sctx; |
a2de733c AJ |
2838 | int ret; |
2839 | struct btrfs_device *dev; | |
2840 | ||
aa1b8cd4 | 2841 | if (btrfs_fs_closing(fs_info)) |
a2de733c AJ |
2842 | return -EINVAL; |
2843 | ||
2844 | /* | |
2845 | * check some assumptions | |
2846 | */ | |
aa1b8cd4 | 2847 | if (fs_info->chunk_root->nodesize != fs_info->chunk_root->leafsize) { |
efe120a0 FH |
2848 | btrfs_err(fs_info, |
2849 | "scrub: size assumption nodesize == leafsize (%d == %d) fails", | |
aa1b8cd4 SB |
2850 | fs_info->chunk_root->nodesize, |
2851 | fs_info->chunk_root->leafsize); | |
b5d67f64 SB |
2852 | return -EINVAL; |
2853 | } | |
2854 | ||
aa1b8cd4 | 2855 | if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) { |
b5d67f64 SB |
2856 | /* |
2857 | * in this case scrub is unable to calculate the checksum | |
2858 | * the way scrub is implemented. Do not handle this | |
2859 | * situation at all because it won't ever happen. | |
2860 | */ | |
efe120a0 FH |
2861 | btrfs_err(fs_info, |
2862 | "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails", | |
aa1b8cd4 | 2863 | fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN); |
b5d67f64 SB |
2864 | return -EINVAL; |
2865 | } | |
2866 | ||
aa1b8cd4 | 2867 | if (fs_info->chunk_root->sectorsize != PAGE_SIZE) { |
b5d67f64 | 2868 | /* not supported for data w/o checksums */ |
efe120a0 FH |
2869 | btrfs_err(fs_info, |
2870 | "scrub: size assumption sectorsize != PAGE_SIZE " | |
2871 | "(%d != %lu) fails", | |
27f9f023 | 2872 | fs_info->chunk_root->sectorsize, PAGE_SIZE); |
a2de733c AJ |
2873 | return -EINVAL; |
2874 | } | |
2875 | ||
7a9e9987 SB |
2876 | if (fs_info->chunk_root->nodesize > |
2877 | PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK || | |
2878 | fs_info->chunk_root->sectorsize > | |
2879 | PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) { | |
2880 | /* | |
2881 | * would exhaust the array bounds of pagev member in | |
2882 | * struct scrub_block | |
2883 | */ | |
efe120a0 FH |
2884 | btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize " |
2885 | "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails", | |
7a9e9987 SB |
2886 | fs_info->chunk_root->nodesize, |
2887 | SCRUB_MAX_PAGES_PER_BLOCK, | |
2888 | fs_info->chunk_root->sectorsize, | |
2889 | SCRUB_MAX_PAGES_PER_BLOCK); | |
2890 | return -EINVAL; | |
2891 | } | |
2892 | ||
a2de733c | 2893 | |
aa1b8cd4 SB |
2894 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
2895 | dev = btrfs_find_device(fs_info, devid, NULL, NULL); | |
63a212ab | 2896 | if (!dev || (dev->missing && !is_dev_replace)) { |
aa1b8cd4 | 2897 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
a2de733c AJ |
2898 | return -ENODEV; |
2899 | } | |
a2de733c | 2900 | |
3b7a016f | 2901 | mutex_lock(&fs_info->scrub_lock); |
63a212ab | 2902 | if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) { |
a2de733c | 2903 | mutex_unlock(&fs_info->scrub_lock); |
aa1b8cd4 | 2904 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
aa1b8cd4 | 2905 | return -EIO; |
a2de733c AJ |
2906 | } |
2907 | ||
8dabb742 SB |
2908 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
2909 | if (dev->scrub_device || | |
2910 | (!is_dev_replace && | |
2911 | btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) { | |
2912 | btrfs_dev_replace_unlock(&fs_info->dev_replace); | |
a2de733c | 2913 | mutex_unlock(&fs_info->scrub_lock); |
aa1b8cd4 | 2914 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
a2de733c AJ |
2915 | return -EINPROGRESS; |
2916 | } | |
8dabb742 | 2917 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
3b7a016f WS |
2918 | |
2919 | ret = scrub_workers_get(fs_info, is_dev_replace); | |
2920 | if (ret) { | |
2921 | mutex_unlock(&fs_info->scrub_lock); | |
2922 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | |
2923 | return ret; | |
2924 | } | |
2925 | ||
63a212ab | 2926 | sctx = scrub_setup_ctx(dev, is_dev_replace); |
d9d181c1 | 2927 | if (IS_ERR(sctx)) { |
a2de733c | 2928 | mutex_unlock(&fs_info->scrub_lock); |
aa1b8cd4 SB |
2929 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
2930 | scrub_workers_put(fs_info); | |
d9d181c1 | 2931 | return PTR_ERR(sctx); |
a2de733c | 2932 | } |
d9d181c1 SB |
2933 | sctx->readonly = readonly; |
2934 | dev->scrub_device = sctx; | |
3cb0929a | 2935 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
a2de733c | 2936 | |
3cb0929a WS |
2937 | /* |
2938 | * checking @scrub_pause_req here, we can avoid | |
2939 | * race between committing transaction and scrubbing. | |
2940 | */ | |
cb7ab021 | 2941 | __scrub_blocked_if_needed(fs_info); |
a2de733c AJ |
2942 | atomic_inc(&fs_info->scrubs_running); |
2943 | mutex_unlock(&fs_info->scrub_lock); | |
a2de733c | 2944 | |
ff023aac | 2945 | if (!is_dev_replace) { |
9b011adf WS |
2946 | /* |
2947 | * by holding device list mutex, we can | |
2948 | * kick off writing super in log tree sync. | |
2949 | */ | |
3cb0929a | 2950 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
ff023aac | 2951 | ret = scrub_supers(sctx, dev); |
3cb0929a | 2952 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
ff023aac | 2953 | } |
a2de733c AJ |
2954 | |
2955 | if (!ret) | |
ff023aac SB |
2956 | ret = scrub_enumerate_chunks(sctx, dev, start, end, |
2957 | is_dev_replace); | |
a2de733c | 2958 | |
b6bfebc1 | 2959 | wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0); |
a2de733c AJ |
2960 | atomic_dec(&fs_info->scrubs_running); |
2961 | wake_up(&fs_info->scrub_pause_wait); | |
2962 | ||
b6bfebc1 | 2963 | wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0); |
0ef8e451 | 2964 | |
a2de733c | 2965 | if (progress) |
d9d181c1 | 2966 | memcpy(progress, &sctx->stat, sizeof(*progress)); |
a2de733c AJ |
2967 | |
2968 | mutex_lock(&fs_info->scrub_lock); | |
2969 | dev->scrub_device = NULL; | |
3b7a016f | 2970 | scrub_workers_put(fs_info); |
a2de733c AJ |
2971 | mutex_unlock(&fs_info->scrub_lock); |
2972 | ||
d9d181c1 | 2973 | scrub_free_ctx(sctx); |
a2de733c AJ |
2974 | |
2975 | return ret; | |
2976 | } | |
2977 | ||
143bede5 | 2978 | void btrfs_scrub_pause(struct btrfs_root *root) |
a2de733c AJ |
2979 | { |
2980 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2981 | ||
2982 | mutex_lock(&fs_info->scrub_lock); | |
2983 | atomic_inc(&fs_info->scrub_pause_req); | |
2984 | while (atomic_read(&fs_info->scrubs_paused) != | |
2985 | atomic_read(&fs_info->scrubs_running)) { | |
2986 | mutex_unlock(&fs_info->scrub_lock); | |
2987 | wait_event(fs_info->scrub_pause_wait, | |
2988 | atomic_read(&fs_info->scrubs_paused) == | |
2989 | atomic_read(&fs_info->scrubs_running)); | |
2990 | mutex_lock(&fs_info->scrub_lock); | |
2991 | } | |
2992 | mutex_unlock(&fs_info->scrub_lock); | |
a2de733c AJ |
2993 | } |
2994 | ||
143bede5 | 2995 | void btrfs_scrub_continue(struct btrfs_root *root) |
a2de733c AJ |
2996 | { |
2997 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2998 | ||
2999 | atomic_dec(&fs_info->scrub_pause_req); | |
3000 | wake_up(&fs_info->scrub_pause_wait); | |
a2de733c AJ |
3001 | } |
3002 | ||
aa1b8cd4 | 3003 | int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info) |
a2de733c | 3004 | { |
a2de733c AJ |
3005 | mutex_lock(&fs_info->scrub_lock); |
3006 | if (!atomic_read(&fs_info->scrubs_running)) { | |
3007 | mutex_unlock(&fs_info->scrub_lock); | |
3008 | return -ENOTCONN; | |
3009 | } | |
3010 | ||
3011 | atomic_inc(&fs_info->scrub_cancel_req); | |
3012 | while (atomic_read(&fs_info->scrubs_running)) { | |
3013 | mutex_unlock(&fs_info->scrub_lock); | |
3014 | wait_event(fs_info->scrub_pause_wait, | |
3015 | atomic_read(&fs_info->scrubs_running) == 0); | |
3016 | mutex_lock(&fs_info->scrub_lock); | |
3017 | } | |
3018 | atomic_dec(&fs_info->scrub_cancel_req); | |
3019 | mutex_unlock(&fs_info->scrub_lock); | |
3020 | ||
3021 | return 0; | |
3022 | } | |
3023 | ||
aa1b8cd4 SB |
3024 | int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info, |
3025 | struct btrfs_device *dev) | |
49b25e05 | 3026 | { |
d9d181c1 | 3027 | struct scrub_ctx *sctx; |
a2de733c AJ |
3028 | |
3029 | mutex_lock(&fs_info->scrub_lock); | |
d9d181c1 SB |
3030 | sctx = dev->scrub_device; |
3031 | if (!sctx) { | |
a2de733c AJ |
3032 | mutex_unlock(&fs_info->scrub_lock); |
3033 | return -ENOTCONN; | |
3034 | } | |
d9d181c1 | 3035 | atomic_inc(&sctx->cancel_req); |
a2de733c AJ |
3036 | while (dev->scrub_device) { |
3037 | mutex_unlock(&fs_info->scrub_lock); | |
3038 | wait_event(fs_info->scrub_pause_wait, | |
3039 | dev->scrub_device == NULL); | |
3040 | mutex_lock(&fs_info->scrub_lock); | |
3041 | } | |
3042 | mutex_unlock(&fs_info->scrub_lock); | |
3043 | ||
3044 | return 0; | |
3045 | } | |
1623edeb | 3046 | |
a2de733c AJ |
3047 | int btrfs_scrub_progress(struct btrfs_root *root, u64 devid, |
3048 | struct btrfs_scrub_progress *progress) | |
3049 | { | |
3050 | struct btrfs_device *dev; | |
d9d181c1 | 3051 | struct scrub_ctx *sctx = NULL; |
a2de733c AJ |
3052 | |
3053 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); | |
aa1b8cd4 | 3054 | dev = btrfs_find_device(root->fs_info, devid, NULL, NULL); |
a2de733c | 3055 | if (dev) |
d9d181c1 SB |
3056 | sctx = dev->scrub_device; |
3057 | if (sctx) | |
3058 | memcpy(progress, &sctx->stat, sizeof(*progress)); | |
a2de733c AJ |
3059 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
3060 | ||
d9d181c1 | 3061 | return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV; |
a2de733c | 3062 | } |
ff023aac SB |
3063 | |
3064 | static void scrub_remap_extent(struct btrfs_fs_info *fs_info, | |
3065 | u64 extent_logical, u64 extent_len, | |
3066 | u64 *extent_physical, | |
3067 | struct btrfs_device **extent_dev, | |
3068 | int *extent_mirror_num) | |
3069 | { | |
3070 | u64 mapped_length; | |
3071 | struct btrfs_bio *bbio = NULL; | |
3072 | int ret; | |
3073 | ||
3074 | mapped_length = extent_len; | |
3075 | ret = btrfs_map_block(fs_info, READ, extent_logical, | |
3076 | &mapped_length, &bbio, 0); | |
3077 | if (ret || !bbio || mapped_length < extent_len || | |
3078 | !bbio->stripes[0].dev->bdev) { | |
3079 | kfree(bbio); | |
3080 | return; | |
3081 | } | |
3082 | ||
3083 | *extent_physical = bbio->stripes[0].physical; | |
3084 | *extent_mirror_num = bbio->mirror_num; | |
3085 | *extent_dev = bbio->stripes[0].dev; | |
3086 | kfree(bbio); | |
3087 | } | |
3088 | ||
3089 | static int scrub_setup_wr_ctx(struct scrub_ctx *sctx, | |
3090 | struct scrub_wr_ctx *wr_ctx, | |
3091 | struct btrfs_fs_info *fs_info, | |
3092 | struct btrfs_device *dev, | |
3093 | int is_dev_replace) | |
3094 | { | |
3095 | WARN_ON(wr_ctx->wr_curr_bio != NULL); | |
3096 | ||
3097 | mutex_init(&wr_ctx->wr_lock); | |
3098 | wr_ctx->wr_curr_bio = NULL; | |
3099 | if (!is_dev_replace) | |
3100 | return 0; | |
3101 | ||
3102 | WARN_ON(!dev->bdev); | |
3103 | wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO, | |
3104 | bio_get_nr_vecs(dev->bdev)); | |
3105 | wr_ctx->tgtdev = dev; | |
3106 | atomic_set(&wr_ctx->flush_all_writes, 0); | |
3107 | return 0; | |
3108 | } | |
3109 | ||
3110 | static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx) | |
3111 | { | |
3112 | mutex_lock(&wr_ctx->wr_lock); | |
3113 | kfree(wr_ctx->wr_curr_bio); | |
3114 | wr_ctx->wr_curr_bio = NULL; | |
3115 | mutex_unlock(&wr_ctx->wr_lock); | |
3116 | } | |
3117 | ||
3118 | static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len, | |
3119 | int mirror_num, u64 physical_for_dev_replace) | |
3120 | { | |
3121 | struct scrub_copy_nocow_ctx *nocow_ctx; | |
3122 | struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info; | |
3123 | ||
3124 | nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS); | |
3125 | if (!nocow_ctx) { | |
3126 | spin_lock(&sctx->stat_lock); | |
3127 | sctx->stat.malloc_errors++; | |
3128 | spin_unlock(&sctx->stat_lock); | |
3129 | return -ENOMEM; | |
3130 | } | |
3131 | ||
3132 | scrub_pending_trans_workers_inc(sctx); | |
3133 | ||
3134 | nocow_ctx->sctx = sctx; | |
3135 | nocow_ctx->logical = logical; | |
3136 | nocow_ctx->len = len; | |
3137 | nocow_ctx->mirror_num = mirror_num; | |
3138 | nocow_ctx->physical_for_dev_replace = physical_for_dev_replace; | |
0339ef2f | 3139 | btrfs_init_work(&nocow_ctx->work, copy_nocow_pages_worker, NULL, NULL); |
652f25a2 | 3140 | INIT_LIST_HEAD(&nocow_ctx->inodes); |
0339ef2f QW |
3141 | btrfs_queue_work(fs_info->scrub_nocow_workers, |
3142 | &nocow_ctx->work); | |
ff023aac SB |
3143 | |
3144 | return 0; | |
3145 | } | |
3146 | ||
652f25a2 JB |
3147 | static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx) |
3148 | { | |
3149 | struct scrub_copy_nocow_ctx *nocow_ctx = ctx; | |
3150 | struct scrub_nocow_inode *nocow_inode; | |
3151 | ||
3152 | nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS); | |
3153 | if (!nocow_inode) | |
3154 | return -ENOMEM; | |
3155 | nocow_inode->inum = inum; | |
3156 | nocow_inode->offset = offset; | |
3157 | nocow_inode->root = root; | |
3158 | list_add_tail(&nocow_inode->list, &nocow_ctx->inodes); | |
3159 | return 0; | |
3160 | } | |
3161 | ||
3162 | #define COPY_COMPLETE 1 | |
3163 | ||
0339ef2f | 3164 | static void copy_nocow_pages_worker(struct btrfs_work_struct *work) |
ff023aac SB |
3165 | { |
3166 | struct scrub_copy_nocow_ctx *nocow_ctx = | |
3167 | container_of(work, struct scrub_copy_nocow_ctx, work); | |
3168 | struct scrub_ctx *sctx = nocow_ctx->sctx; | |
3169 | u64 logical = nocow_ctx->logical; | |
3170 | u64 len = nocow_ctx->len; | |
3171 | int mirror_num = nocow_ctx->mirror_num; | |
3172 | u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace; | |
3173 | int ret; | |
3174 | struct btrfs_trans_handle *trans = NULL; | |
3175 | struct btrfs_fs_info *fs_info; | |
3176 | struct btrfs_path *path; | |
3177 | struct btrfs_root *root; | |
3178 | int not_written = 0; | |
3179 | ||
3180 | fs_info = sctx->dev_root->fs_info; | |
3181 | root = fs_info->extent_root; | |
3182 | ||
3183 | path = btrfs_alloc_path(); | |
3184 | if (!path) { | |
3185 | spin_lock(&sctx->stat_lock); | |
3186 | sctx->stat.malloc_errors++; | |
3187 | spin_unlock(&sctx->stat_lock); | |
3188 | not_written = 1; | |
3189 | goto out; | |
3190 | } | |
3191 | ||
3192 | trans = btrfs_join_transaction(root); | |
3193 | if (IS_ERR(trans)) { | |
3194 | not_written = 1; | |
3195 | goto out; | |
3196 | } | |
3197 | ||
3198 | ret = iterate_inodes_from_logical(logical, fs_info, path, | |
652f25a2 | 3199 | record_inode_for_nocow, nocow_ctx); |
ff023aac | 3200 | if (ret != 0 && ret != -ENOENT) { |
efe120a0 FH |
3201 | btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, " |
3202 | "phys %llu, len %llu, mir %u, ret %d", | |
118a0a25 GU |
3203 | logical, physical_for_dev_replace, len, mirror_num, |
3204 | ret); | |
ff023aac SB |
3205 | not_written = 1; |
3206 | goto out; | |
3207 | } | |
3208 | ||
652f25a2 JB |
3209 | btrfs_end_transaction(trans, root); |
3210 | trans = NULL; | |
3211 | while (!list_empty(&nocow_ctx->inodes)) { | |
3212 | struct scrub_nocow_inode *entry; | |
3213 | entry = list_first_entry(&nocow_ctx->inodes, | |
3214 | struct scrub_nocow_inode, | |
3215 | list); | |
3216 | list_del_init(&entry->list); | |
3217 | ret = copy_nocow_pages_for_inode(entry->inum, entry->offset, | |
3218 | entry->root, nocow_ctx); | |
3219 | kfree(entry); | |
3220 | if (ret == COPY_COMPLETE) { | |
3221 | ret = 0; | |
3222 | break; | |
3223 | } else if (ret) { | |
3224 | break; | |
3225 | } | |
3226 | } | |
ff023aac | 3227 | out: |
652f25a2 JB |
3228 | while (!list_empty(&nocow_ctx->inodes)) { |
3229 | struct scrub_nocow_inode *entry; | |
3230 | entry = list_first_entry(&nocow_ctx->inodes, | |
3231 | struct scrub_nocow_inode, | |
3232 | list); | |
3233 | list_del_init(&entry->list); | |
3234 | kfree(entry); | |
3235 | } | |
ff023aac SB |
3236 | if (trans && !IS_ERR(trans)) |
3237 | btrfs_end_transaction(trans, root); | |
3238 | if (not_written) | |
3239 | btrfs_dev_replace_stats_inc(&fs_info->dev_replace. | |
3240 | num_uncorrectable_read_errors); | |
3241 | ||
3242 | btrfs_free_path(path); | |
3243 | kfree(nocow_ctx); | |
3244 | ||
3245 | scrub_pending_trans_workers_dec(sctx); | |
3246 | } | |
3247 | ||
652f25a2 JB |
3248 | static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root, |
3249 | struct scrub_copy_nocow_ctx *nocow_ctx) | |
ff023aac | 3250 | { |
826aa0a8 | 3251 | struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info; |
ff023aac | 3252 | struct btrfs_key key; |
826aa0a8 MX |
3253 | struct inode *inode; |
3254 | struct page *page; | |
ff023aac | 3255 | struct btrfs_root *local_root; |
652f25a2 JB |
3256 | struct btrfs_ordered_extent *ordered; |
3257 | struct extent_map *em; | |
3258 | struct extent_state *cached_state = NULL; | |
3259 | struct extent_io_tree *io_tree; | |
ff023aac | 3260 | u64 physical_for_dev_replace; |
652f25a2 JB |
3261 | u64 len = nocow_ctx->len; |
3262 | u64 lockstart = offset, lockend = offset + len - 1; | |
826aa0a8 | 3263 | unsigned long index; |
6f1c3605 | 3264 | int srcu_index; |
652f25a2 JB |
3265 | int ret = 0; |
3266 | int err = 0; | |
ff023aac SB |
3267 | |
3268 | key.objectid = root; | |
3269 | key.type = BTRFS_ROOT_ITEM_KEY; | |
3270 | key.offset = (u64)-1; | |
6f1c3605 LB |
3271 | |
3272 | srcu_index = srcu_read_lock(&fs_info->subvol_srcu); | |
3273 | ||
ff023aac | 3274 | local_root = btrfs_read_fs_root_no_name(fs_info, &key); |
6f1c3605 LB |
3275 | if (IS_ERR(local_root)) { |
3276 | srcu_read_unlock(&fs_info->subvol_srcu, srcu_index); | |
ff023aac | 3277 | return PTR_ERR(local_root); |
6f1c3605 | 3278 | } |
ff023aac SB |
3279 | |
3280 | key.type = BTRFS_INODE_ITEM_KEY; | |
3281 | key.objectid = inum; | |
3282 | key.offset = 0; | |
3283 | inode = btrfs_iget(fs_info->sb, &key, local_root, NULL); | |
6f1c3605 | 3284 | srcu_read_unlock(&fs_info->subvol_srcu, srcu_index); |
ff023aac SB |
3285 | if (IS_ERR(inode)) |
3286 | return PTR_ERR(inode); | |
3287 | ||
edd1400b MX |
3288 | /* Avoid truncate/dio/punch hole.. */ |
3289 | mutex_lock(&inode->i_mutex); | |
3290 | inode_dio_wait(inode); | |
3291 | ||
ff023aac | 3292 | physical_for_dev_replace = nocow_ctx->physical_for_dev_replace; |
652f25a2 JB |
3293 | io_tree = &BTRFS_I(inode)->io_tree; |
3294 | ||
3295 | lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state); | |
3296 | ordered = btrfs_lookup_ordered_range(inode, lockstart, len); | |
3297 | if (ordered) { | |
3298 | btrfs_put_ordered_extent(ordered); | |
3299 | goto out_unlock; | |
3300 | } | |
3301 | ||
3302 | em = btrfs_get_extent(inode, NULL, 0, lockstart, len, 0); | |
3303 | if (IS_ERR(em)) { | |
3304 | ret = PTR_ERR(em); | |
3305 | goto out_unlock; | |
3306 | } | |
3307 | ||
3308 | /* | |
3309 | * This extent does not actually cover the logical extent anymore, | |
3310 | * move on to the next inode. | |
3311 | */ | |
3312 | if (em->block_start > nocow_ctx->logical || | |
3313 | em->block_start + em->block_len < nocow_ctx->logical + len) { | |
3314 | free_extent_map(em); | |
3315 | goto out_unlock; | |
3316 | } | |
3317 | free_extent_map(em); | |
3318 | ||
ff023aac | 3319 | while (len >= PAGE_CACHE_SIZE) { |
ff023aac | 3320 | index = offset >> PAGE_CACHE_SHIFT; |
edd1400b | 3321 | again: |
ff023aac SB |
3322 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); |
3323 | if (!page) { | |
efe120a0 | 3324 | btrfs_err(fs_info, "find_or_create_page() failed"); |
ff023aac | 3325 | ret = -ENOMEM; |
826aa0a8 | 3326 | goto out; |
ff023aac SB |
3327 | } |
3328 | ||
3329 | if (PageUptodate(page)) { | |
3330 | if (PageDirty(page)) | |
3331 | goto next_page; | |
3332 | } else { | |
3333 | ClearPageError(page); | |
652f25a2 JB |
3334 | err = extent_read_full_page_nolock(io_tree, page, |
3335 | btrfs_get_extent, | |
3336 | nocow_ctx->mirror_num); | |
826aa0a8 MX |
3337 | if (err) { |
3338 | ret = err; | |
ff023aac SB |
3339 | goto next_page; |
3340 | } | |
edd1400b | 3341 | |
26b25891 | 3342 | lock_page(page); |
edd1400b MX |
3343 | /* |
3344 | * If the page has been remove from the page cache, | |
3345 | * the data on it is meaningless, because it may be | |
3346 | * old one, the new data may be written into the new | |
3347 | * page in the page cache. | |
3348 | */ | |
3349 | if (page->mapping != inode->i_mapping) { | |
652f25a2 | 3350 | unlock_page(page); |
edd1400b MX |
3351 | page_cache_release(page); |
3352 | goto again; | |
3353 | } | |
ff023aac SB |
3354 | if (!PageUptodate(page)) { |
3355 | ret = -EIO; | |
3356 | goto next_page; | |
3357 | } | |
3358 | } | |
826aa0a8 MX |
3359 | err = write_page_nocow(nocow_ctx->sctx, |
3360 | physical_for_dev_replace, page); | |
3361 | if (err) | |
3362 | ret = err; | |
ff023aac | 3363 | next_page: |
826aa0a8 MX |
3364 | unlock_page(page); |
3365 | page_cache_release(page); | |
3366 | ||
3367 | if (ret) | |
3368 | break; | |
3369 | ||
ff023aac SB |
3370 | offset += PAGE_CACHE_SIZE; |
3371 | physical_for_dev_replace += PAGE_CACHE_SIZE; | |
3372 | len -= PAGE_CACHE_SIZE; | |
3373 | } | |
652f25a2 JB |
3374 | ret = COPY_COMPLETE; |
3375 | out_unlock: | |
3376 | unlock_extent_cached(io_tree, lockstart, lockend, &cached_state, | |
3377 | GFP_NOFS); | |
826aa0a8 | 3378 | out: |
edd1400b | 3379 | mutex_unlock(&inode->i_mutex); |
826aa0a8 | 3380 | iput(inode); |
ff023aac SB |
3381 | return ret; |
3382 | } | |
3383 | ||
3384 | static int write_page_nocow(struct scrub_ctx *sctx, | |
3385 | u64 physical_for_dev_replace, struct page *page) | |
3386 | { | |
3387 | struct bio *bio; | |
3388 | struct btrfs_device *dev; | |
3389 | int ret; | |
ff023aac SB |
3390 | |
3391 | dev = sctx->wr_ctx.tgtdev; | |
3392 | if (!dev) | |
3393 | return -EIO; | |
3394 | if (!dev->bdev) { | |
3395 | printk_ratelimited(KERN_WARNING | |
efe120a0 | 3396 | "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n"); |
ff023aac SB |
3397 | return -EIO; |
3398 | } | |
9be3395b | 3399 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
ff023aac SB |
3400 | if (!bio) { |
3401 | spin_lock(&sctx->stat_lock); | |
3402 | sctx->stat.malloc_errors++; | |
3403 | spin_unlock(&sctx->stat_lock); | |
3404 | return -ENOMEM; | |
3405 | } | |
ff023aac SB |
3406 | bio->bi_size = 0; |
3407 | bio->bi_sector = physical_for_dev_replace >> 9; | |
3408 | bio->bi_bdev = dev->bdev; | |
3409 | ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0); | |
3410 | if (ret != PAGE_CACHE_SIZE) { | |
3411 | leave_with_eio: | |
3412 | bio_put(bio); | |
3413 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); | |
3414 | return -EIO; | |
3415 | } | |
ff023aac | 3416 | |
c170bbb4 | 3417 | if (btrfsic_submit_bio_wait(WRITE_SYNC, bio)) |
ff023aac SB |
3418 | goto leave_with_eio; |
3419 | ||
3420 | bio_put(bio); | |
3421 | return 0; | |
3422 | } |