blob: 53575a45f7d1b61f95e8f85870b9c219f8295284 [file] [log] [blame]
Arne Jansena2de7332011-03-08 14:14:00 +01001/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
Arne Jansena2de7332011-03-08 14:14:00 +01003 *
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
Arne Jansena2de7332011-03-08 14:14:00 +010019#include <linux/blkdev.h>
Jan Schmidt558540c2011-06-13 19:59:12 +020020#include <linux/ratelimit.h>
Arne Jansena2de7332011-03-08 14:14:00 +010021#include "ctree.h"
22#include "volumes.h"
23#include "disk-io.h"
24#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020025#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020026#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020027#include "extent_io.h"
Stefan Behrensff023aa2012-11-06 11:43:11 +010028#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010029#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040030#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050031#include "raid56.h"
Arne Jansena2de7332011-03-08 14:14:00 +010032
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:
Arne Jansena2de7332011-03-08 14:14:00 +010040 * - In case an unrepairable extent is encountered, track which files are
41 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010042 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010043 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010044 */
45
Stefan Behrensb5d67f62012-03-27 14:21:27 -040046struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010047struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010048
Stefan Behrensff023aa2012-11-06 11:43:11 +010049/*
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 */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010058
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 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040064#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010065
Miao Xieaf8e2d12014-10-23 14:42:50 +080066struct scrub_recover {
67 atomic_t refs;
68 struct btrfs_bio *bbio;
69 u64 *raid_map;
70 u64 map_length;
71};
72
Arne Jansena2de7332011-03-08 14:14:00 +010073struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040074 struct scrub_block *sblock;
75 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020076 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080077 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010078 u64 flags; /* extent flags */
79 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040080 u64 logical;
81 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010082 u64 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +010083 atomic_t ref_count;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040084 struct {
85 unsigned int mirror_num:8;
86 unsigned int have_csum:1;
87 unsigned int io_error:1;
88 };
Arne Jansena2de7332011-03-08 14:14:00 +010089 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080090
91 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010092};
93
94struct scrub_bio {
95 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010096 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010097 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010098 struct bio *bio;
99 int err;
100 u64 logical;
101 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100102#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
103 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
104#else
105 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
106#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400107 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +0100108 int next_free;
109 struct btrfs_work work;
110};
111
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400112struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100113 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400114 int page_count;
115 atomic_t outstanding_pages;
116 atomic_t ref_count; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100117 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800118 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400119 struct {
120 unsigned int header_error:1;
121 unsigned int checksum_error:1;
122 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200123 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800124
125 /* The following is for the data used to check parity */
126 /* It is for the data with checksum */
127 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400128 };
129};
130
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800131/* Used for the chunks with parity stripe such RAID5/6 */
132struct scrub_parity {
133 struct scrub_ctx *sctx;
134
135 struct btrfs_device *scrub_dev;
136
137 u64 logic_start;
138
139 u64 logic_end;
140
141 int nsectors;
142
143 int stripe_len;
144
145 atomic_t ref_count;
146
147 struct list_head spages;
148
149 /* Work of parity check and repair */
150 struct btrfs_work work;
151
152 /* Mark the parity blocks which have data */
153 unsigned long *dbitmap;
154
155 /*
156 * Mark the parity blocks which have data, but errors happen when
157 * read data or check data
158 */
159 unsigned long *ebitmap;
160
161 unsigned long bitmap[0];
162};
163
Stefan Behrensff023aa2012-11-06 11:43:11 +0100164struct scrub_wr_ctx {
165 struct scrub_bio *wr_curr_bio;
166 struct btrfs_device *tgtdev;
167 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
168 atomic_t flush_all_writes;
169 struct mutex wr_lock;
170};
171
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100172struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100173 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100174 struct btrfs_root *dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +0100175 int first_free;
176 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100177 atomic_t bios_in_flight;
178 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100179 spinlock_t list_lock;
180 wait_queue_head_t list_wait;
181 u16 csum_size;
182 struct list_head csum_list;
183 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100184 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100185 int pages_per_rd_bio;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400186 u32 sectorsize;
187 u32 nodesize;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100188
189 int is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100190 struct scrub_wr_ctx wr_ctx;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100191
Arne Jansena2de7332011-03-08 14:14:00 +0100192 /*
193 * statistics
194 */
195 struct btrfs_scrub_progress stat;
196 spinlock_t stat_lock;
197};
198
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200199struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100200 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100201 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200202 u64 logical;
203 struct btrfs_root *root;
204 struct btrfs_work work;
205 int mirror_num;
206};
207
Josef Bacik652f25a2013-09-12 16:58:28 -0400208struct scrub_nocow_inode {
209 u64 inum;
210 u64 offset;
211 u64 root;
212 struct list_head list;
213};
214
Stefan Behrensff023aa2012-11-06 11:43:11 +0100215struct scrub_copy_nocow_ctx {
216 struct scrub_ctx *sctx;
217 u64 logical;
218 u64 len;
219 int mirror_num;
220 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -0400221 struct list_head inodes;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100222 struct btrfs_work work;
223};
224
Jan Schmidt558540c2011-06-13 19:59:12 +0200225struct scrub_warning {
226 struct btrfs_path *path;
227 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200228 const char *errstr;
229 sector_t sector;
230 u64 logical;
231 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200232};
233
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100234static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
235static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
236static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
237static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400238static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100239static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100240 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100241 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400242 u64 length, u64 logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100243 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100244static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
245 struct scrub_block *sblock, int is_metadata,
246 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800247 u16 csum_size, int retry_failed_mirror);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400248static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
249 struct scrub_block *sblock,
250 int is_metadata, int have_csum,
251 const u8 *csum, u64 generation,
252 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400253static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
254 struct scrub_block *sblock_good,
255 int force_write);
256static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
257 struct scrub_block *sblock_good,
258 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100259static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
260static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
261 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400262static int scrub_checksum_data(struct scrub_block *sblock);
263static int scrub_checksum_tree_block(struct scrub_block *sblock);
264static int scrub_checksum_super(struct scrub_block *sblock);
265static void scrub_block_get(struct scrub_block *sblock);
266static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100267static void scrub_page_get(struct scrub_page *spage);
268static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800269static void scrub_parity_get(struct scrub_parity *sparity);
270static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100271static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
272 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100273static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100274 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100275 u64 gen, int mirror_num, u8 *csum, int force,
276 u64 physical_for_dev_replace);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400277static void scrub_bio_end_io(struct bio *bio, int err);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400278static void scrub_bio_end_io_worker(struct btrfs_work *work);
279static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100280static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
281 u64 extent_logical, u64 extent_len,
282 u64 *extent_physical,
283 struct btrfs_device **extent_dev,
284 int *extent_mirror_num);
285static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
286 struct scrub_wr_ctx *wr_ctx,
287 struct btrfs_fs_info *fs_info,
288 struct btrfs_device *dev,
289 int is_dev_replace);
290static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
291static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
292 struct scrub_page *spage);
293static void scrub_wr_submit(struct scrub_ctx *sctx);
294static void scrub_wr_bio_end_io(struct bio *bio, int err);
295static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
296static int write_page_nocow(struct scrub_ctx *sctx,
297 u64 physical_for_dev_replace, struct page *page);
298static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400299 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100300static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
301 int mirror_num, u64 physical_for_dev_replace);
302static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800303static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800304static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400305
306
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100307static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
308{
309 atomic_inc(&sctx->bios_in_flight);
310}
311
312static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
313{
314 atomic_dec(&sctx->bios_in_flight);
315 wake_up(&sctx->list_wait);
316}
317
Wang Shilongcb7ab022013-12-04 21:16:53 +0800318static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800319{
320 while (atomic_read(&fs_info->scrub_pause_req)) {
321 mutex_unlock(&fs_info->scrub_lock);
322 wait_event(fs_info->scrub_pause_wait,
323 atomic_read(&fs_info->scrub_pause_req) == 0);
324 mutex_lock(&fs_info->scrub_lock);
325 }
326}
327
Wang Shilongcb7ab022013-12-04 21:16:53 +0800328static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
329{
330 atomic_inc(&fs_info->scrubs_paused);
331 wake_up(&fs_info->scrub_pause_wait);
332
333 mutex_lock(&fs_info->scrub_lock);
334 __scrub_blocked_if_needed(fs_info);
335 atomic_dec(&fs_info->scrubs_paused);
336 mutex_unlock(&fs_info->scrub_lock);
337
338 wake_up(&fs_info->scrub_pause_wait);
339}
340
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100341/*
342 * used for workers that require transaction commits (i.e., for the
343 * NOCOW case)
344 */
345static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
346{
347 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
348
349 /*
350 * increment scrubs_running to prevent cancel requests from
351 * completing as long as a worker is running. we must also
352 * increment scrubs_paused to prevent deadlocking on pause
353 * requests used for transactions commits (as the worker uses a
354 * transaction context). it is safe to regard the worker
355 * as paused for all matters practical. effectively, we only
356 * avoid cancellation requests from completing.
357 */
358 mutex_lock(&fs_info->scrub_lock);
359 atomic_inc(&fs_info->scrubs_running);
360 atomic_inc(&fs_info->scrubs_paused);
361 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800362
363 /*
364 * check if @scrubs_running=@scrubs_paused condition
365 * inside wait_event() is not an atomic operation.
366 * which means we may inc/dec @scrub_running/paused
367 * at any time. Let's wake up @scrub_pause_wait as
368 * much as we can to let commit transaction blocked less.
369 */
370 wake_up(&fs_info->scrub_pause_wait);
371
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100372 atomic_inc(&sctx->workers_pending);
373}
374
375/* used for workers that require transaction commits */
376static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
377{
378 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
379
380 /*
381 * see scrub_pending_trans_workers_inc() why we're pretending
382 * to be paused in the scrub counters
383 */
384 mutex_lock(&fs_info->scrub_lock);
385 atomic_dec(&fs_info->scrubs_running);
386 atomic_dec(&fs_info->scrubs_paused);
387 mutex_unlock(&fs_info->scrub_lock);
388 atomic_dec(&sctx->workers_pending);
389 wake_up(&fs_info->scrub_pause_wait);
390 wake_up(&sctx->list_wait);
391}
392
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100393static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100394{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100395 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100396 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100397 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100398 struct btrfs_ordered_sum, list);
399 list_del(&sum->list);
400 kfree(sum);
401 }
402}
403
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100404static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100405{
406 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100407
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100408 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100409 return;
410
Stefan Behrensff023aa2012-11-06 11:43:11 +0100411 scrub_free_wr_ctx(&sctx->wr_ctx);
412
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400413 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100414 if (sctx->curr != -1) {
415 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400416
417 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100418 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400419 scrub_block_put(sbio->pagev[i]->sblock);
420 }
421 bio_put(sbio->bio);
422 }
423
Stefan Behrensff023aa2012-11-06 11:43:11 +0100424 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100425 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100426
427 if (!sbio)
428 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100429 kfree(sbio);
430 }
431
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100432 scrub_free_csums(sctx);
433 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100434}
435
436static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100437struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100438{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100439 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100440 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100441 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100442 int pages_per_rd_bio;
443 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100444
Stefan Behrensff023aa2012-11-06 11:43:11 +0100445 /*
446 * the setting of pages_per_rd_bio is correct for scrub but might
447 * be wrong for the dev_replace code where we might read from
448 * different devices in the initial huge bios. However, that
449 * code is able to correctly handle the case when adding a page
450 * to a bio fails.
451 */
452 if (dev->bdev)
453 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
454 bio_get_nr_vecs(dev->bdev));
455 else
456 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100457 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
458 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100459 goto nomem;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100460 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100461 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100462 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100463 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100464 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100465 struct scrub_bio *sbio;
466
467 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
468 if (!sbio)
469 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100470 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100471
Arne Jansena2de7332011-03-08 14:14:00 +0100472 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100473 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400474 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800475 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
476 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100477
Stefan Behrensff023aa2012-11-06 11:43:11 +0100478 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100479 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200480 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100481 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100482 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100483 sctx->first_free = 0;
484 sctx->nodesize = dev->dev_root->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100485 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100486 atomic_set(&sctx->bios_in_flight, 0);
487 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100488 atomic_set(&sctx->cancel_req, 0);
489 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
490 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100491
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100492 spin_lock_init(&sctx->list_lock);
493 spin_lock_init(&sctx->stat_lock);
494 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100495
496 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
497 fs_info->dev_replace.tgtdev, is_dev_replace);
498 if (ret) {
499 scrub_free_ctx(sctx);
500 return ERR_PTR(ret);
501 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100502 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100503
504nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100505 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100506 return ERR_PTR(-ENOMEM);
507}
508
Stefan Behrensff023aa2012-11-06 11:43:11 +0100509static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
510 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200511{
512 u64 isize;
513 u32 nlink;
514 int ret;
515 int i;
516 struct extent_buffer *eb;
517 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100518 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200519 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
520 struct inode_fs_paths *ipath = NULL;
521 struct btrfs_root *local_root;
522 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100523 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200524
525 root_key.objectid = root;
526 root_key.type = BTRFS_ROOT_ITEM_KEY;
527 root_key.offset = (u64)-1;
528 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
529 if (IS_ERR(local_root)) {
530 ret = PTR_ERR(local_root);
531 goto err;
532 }
533
David Sterba14692cc2015-01-02 18:55:46 +0100534 /*
535 * this makes the path point to (inum INODE_ITEM ioff)
536 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100537 key.objectid = inum;
538 key.type = BTRFS_INODE_ITEM_KEY;
539 key.offset = 0;
540
541 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200542 if (ret) {
543 btrfs_release_path(swarn->path);
544 goto err;
545 }
546
547 eb = swarn->path->nodes[0];
548 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
549 struct btrfs_inode_item);
550 isize = btrfs_inode_size(eb, inode_item);
551 nlink = btrfs_inode_nlink(eb, inode_item);
552 btrfs_release_path(swarn->path);
553
554 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300555 if (IS_ERR(ipath)) {
556 ret = PTR_ERR(ipath);
557 ipath = NULL;
558 goto err;
559 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200560 ret = paths_from_inode(inum, ipath);
561
562 if (ret < 0)
563 goto err;
564
565 /*
566 * we deliberately ignore the bit ipath might have been too small to
567 * hold all of the paths here
568 */
569 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Frank Holtonefe120a2013-12-20 11:37:06 -0500570 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200571 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
572 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400573 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200574 (unsigned long long)swarn->sector, root, inum, offset,
575 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500576 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200577
578 free_ipath(ipath);
579 return 0;
580
581err:
Frank Holtonefe120a2013-12-20 11:37:06 -0500582 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200583 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
584 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400585 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200586 (unsigned long long)swarn->sector, root, inum, offset, ret);
587
588 free_ipath(ipath);
589 return 0;
590}
591
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400592static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200593{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100594 struct btrfs_device *dev;
595 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200596 struct btrfs_path *path;
597 struct btrfs_key found_key;
598 struct extent_buffer *eb;
599 struct btrfs_extent_item *ei;
600 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200601 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100602 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600603 u64 flags = 0;
604 u64 ref_root;
605 u32 item_size;
606 u8 ref_level;
Liu Bo69917e42012-09-07 20:01:28 -0600607 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200608
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100609 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100610 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100611 fs_info = sblock->sctx->dev_root->fs_info;
612
Jan Schmidt558540c2011-06-13 19:59:12 +0200613 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200614 if (!path)
615 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200616
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100617 swarn.sector = (sblock->pagev[0]->physical) >> 9;
618 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200619 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100620 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200621
Liu Bo69917e42012-09-07 20:01:28 -0600622 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
623 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200624 if (ret < 0)
625 goto out;
626
Jan Schmidt4692cf52011-12-02 14:56:41 +0100627 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200628 swarn.extent_item_size = found_key.offset;
629
630 eb = path->nodes[0];
631 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
632 item_size = btrfs_item_size_nr(eb, path->slots[0]);
633
Liu Bo69917e42012-09-07 20:01:28 -0600634 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200635 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800636 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
637 item_size, &ref_root,
638 &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400639 printk_in_rcu(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -0500640 "BTRFS: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200641 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400642 "%llu\n", errstr, swarn.logical,
643 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200644 (unsigned long long)swarn.sector,
645 ref_level ? "node" : "leaf",
646 ret < 0 ? -1 : ref_level,
647 ret < 0 ? -1 : ref_root);
648 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600649 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200650 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600651 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200652 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100653 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100654 iterate_extent_inodes(fs_info, found_key.objectid,
655 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200656 scrub_print_warning_inode, &swarn);
657 }
658
659out:
660 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200661}
662
Stefan Behrensff023aa2012-11-06 11:43:11 +0100663static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200664{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200665 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200666 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100667 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200668 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200669 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200670 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200671 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000672 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200673 u64 end = offset + PAGE_SIZE - 1;
674 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000675 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200676
677 key.objectid = root;
678 key.type = BTRFS_ROOT_ITEM_KEY;
679 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000680
681 fs_info = fixup->root->fs_info;
682 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
683
684 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
685 if (IS_ERR(local_root)) {
686 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200687 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000688 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200689
690 key.type = BTRFS_INODE_ITEM_KEY;
691 key.objectid = inum;
692 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000693 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
694 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200695 if (IS_ERR(inode))
696 return PTR_ERR(inode);
697
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200698 index = offset >> PAGE_CACHE_SHIFT;
699
700 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200701 if (!page) {
702 ret = -ENOMEM;
703 goto out;
704 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200705
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200706 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200707 if (PageDirty(page)) {
708 /*
709 * we need to write the data to the defect sector. the
710 * data that was in that sector is not in memory,
711 * because the page was modified. we must not write the
712 * modified page to that sector.
713 *
714 * TODO: what could be done here: wait for the delalloc
715 * runner to write out that page (might involve
716 * COW) and see whether the sector is still
717 * referenced afterwards.
718 *
719 * For the meantime, we'll treat this error
720 * incorrectable, although there is a chance that a
721 * later scrub will find the bad sector again and that
722 * there's no dirty page in memory, then.
723 */
724 ret = -EIO;
725 goto out;
726 }
Miao Xie1203b682014-09-12 18:44:01 +0800727 ret = repair_io_failure(inode, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200728 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800729 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200730 fixup->mirror_num);
731 unlock_page(page);
732 corrected = !ret;
733 } else {
734 /*
735 * we need to get good data first. the general readpage path
736 * will call repair_io_failure for us, we just have to make
737 * sure we read the bad mirror.
738 */
739 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
740 EXTENT_DAMAGED, GFP_NOFS);
741 if (ret) {
742 /* set_extent_bits should give proper error */
743 WARN_ON(ret > 0);
744 if (ret > 0)
745 ret = -EFAULT;
746 goto out;
747 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200748
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200749 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
750 btrfs_get_extent,
751 fixup->mirror_num);
752 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200753
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200754 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
755 end, EXTENT_DAMAGED, 0, NULL);
756 if (!corrected)
757 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
758 EXTENT_DAMAGED, GFP_NOFS);
759 }
760
761out:
762 if (page)
763 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200764
765 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200766
767 if (ret < 0)
768 return ret;
769
770 if (ret == 0 && corrected) {
771 /*
772 * we only need to call readpage for one of the inodes belonging
773 * to this extent. so make iterate_extent_inodes stop
774 */
775 return 1;
776 }
777
778 return -EIO;
779}
780
781static void scrub_fixup_nodatasum(struct btrfs_work *work)
782{
783 int ret;
784 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100785 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200786 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200787 struct btrfs_path *path;
788 int uncorrectable = 0;
789
790 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100791 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200792
793 path = btrfs_alloc_path();
794 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100795 spin_lock(&sctx->stat_lock);
796 ++sctx->stat.malloc_errors;
797 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200798 uncorrectable = 1;
799 goto out;
800 }
801
802 trans = btrfs_join_transaction(fixup->root);
803 if (IS_ERR(trans)) {
804 uncorrectable = 1;
805 goto out;
806 }
807
808 /*
809 * the idea is to trigger a regular read through the standard path. we
810 * read a page from the (failed) logical address by specifying the
811 * corresponding copynum of the failed sector. thus, that readpage is
812 * expected to fail.
813 * that is the point where on-the-fly error correction will kick in
814 * (once it's finished) and rewrite the failed sector if a good copy
815 * can be found.
816 */
817 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
818 path, scrub_fixup_readpage,
819 fixup);
820 if (ret < 0) {
821 uncorrectable = 1;
822 goto out;
823 }
824 WARN_ON(ret != 1);
825
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100826 spin_lock(&sctx->stat_lock);
827 ++sctx->stat.corrected_errors;
828 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200829
830out:
831 if (trans && !IS_ERR(trans))
832 btrfs_end_transaction(trans, fixup->root);
833 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100834 spin_lock(&sctx->stat_lock);
835 ++sctx->stat.uncorrectable_errors;
836 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100837 btrfs_dev_replace_stats_inc(
838 &sctx->dev_root->fs_info->dev_replace.
839 num_uncorrectable_read_errors);
Frank Holtonefe120a2013-12-20 11:37:06 -0500840 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
841 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200842 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200843 }
844
845 btrfs_free_path(path);
846 kfree(fixup);
847
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100848 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200849}
850
Miao Xieaf8e2d12014-10-23 14:42:50 +0800851static inline void scrub_get_recover(struct scrub_recover *recover)
852{
853 atomic_inc(&recover->refs);
854}
855
856static inline void scrub_put_recover(struct scrub_recover *recover)
857{
858 if (atomic_dec_and_test(&recover->refs)) {
859 kfree(recover->bbio);
860 kfree(recover->raid_map);
861 kfree(recover);
862 }
863}
864
Arne Jansena2de7332011-03-08 14:14:00 +0100865/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400866 * scrub_handle_errored_block gets called when either verification of the
867 * pages failed or the bio failed to read, e.g. with EIO. In the latter
868 * case, this function handles all pages in the bio, even though only one
869 * may be bad.
870 * The goal of this function is to repair the errored block by using the
871 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100872 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400873static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100874{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100875 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100876 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400877 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100878 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400879 u64 logical;
880 u64 generation;
881 unsigned int failed_mirror_index;
882 unsigned int is_metadata;
883 unsigned int have_csum;
884 u8 *csum;
885 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
886 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100887 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400888 int mirror_index;
889 int page_num;
890 int success;
891 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
892 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100893
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400894 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100895 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000896 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
897 /*
898 * if we find an error in a super block, we just report it.
899 * They will get written with the next transaction commit
900 * anyway
901 */
902 spin_lock(&sctx->stat_lock);
903 ++sctx->stat.super_errors;
904 spin_unlock(&sctx->stat_lock);
905 return 0;
906 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400907 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100908 logical = sblock_to_check->pagev[0]->logical;
909 generation = sblock_to_check->pagev[0]->generation;
910 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
911 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
912 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400913 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100914 have_csum = sblock_to_check->pagev[0]->have_csum;
915 csum = sblock_to_check->pagev[0]->csum;
916 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400917
Stefan Behrensff023aa2012-11-06 11:43:11 +0100918 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
919 sblocks_for_recheck = NULL;
920 goto nodatasum_case;
921 }
922
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400923 /*
924 * read all mirrors one after the other. This includes to
925 * re-read the extent or metadata block that failed (that was
926 * the cause that this fixup code is called) another time,
927 * page by page this time in order to know which pages
928 * caused I/O errors and which ones are good (for all mirrors).
929 * It is the goal to handle the situation when more than one
930 * mirror contains I/O errors, but the errors do not
931 * overlap, i.e. the data can be repaired by selecting the
932 * pages from those mirrors without I/O error on the
933 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
934 * would be that mirror #1 has an I/O error on the first page,
935 * the second page is good, and mirror #2 has an I/O error on
936 * the second page, but the first page is good.
937 * Then the first page of the first mirror can be repaired by
938 * taking the first page of the second mirror, and the
939 * second page of the second mirror can be repaired by
940 * copying the contents of the 2nd page of the 1st mirror.
941 * One more note: if the pages of one mirror contain I/O
942 * errors, the checksum cannot be verified. In order to get
943 * the best data for repairing, the first attempt is to find
944 * a mirror without I/O errors and with a validated checksum.
945 * Only if this is not possible, the pages are picked from
946 * mirrors with I/O errors without considering the checksum.
947 * If the latter is the case, at the end, the checksum of the
948 * repaired area is verified in order to correctly maintain
949 * the statistics.
950 */
951
952 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
953 sizeof(*sblocks_for_recheck),
954 GFP_NOFS);
955 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100956 spin_lock(&sctx->stat_lock);
957 sctx->stat.malloc_errors++;
958 sctx->stat.read_errors++;
959 sctx->stat.uncorrectable_errors++;
960 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100961 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400962 goto out;
963 }
964
965 /* setup the context, map the logical blocks and alloc the pages */
Stefan Behrensff023aa2012-11-06 11:43:11 +0100966 ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400967 logical, sblocks_for_recheck);
968 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100969 spin_lock(&sctx->stat_lock);
970 sctx->stat.read_errors++;
971 sctx->stat.uncorrectable_errors++;
972 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100973 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400974 goto out;
975 }
976 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
977 sblock_bad = sblocks_for_recheck + failed_mirror_index;
978
979 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100980 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800981 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400982
983 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
984 sblock_bad->no_io_error_seen) {
985 /*
986 * the error disappeared after reading page by page, or
987 * the area was part of a huge bio and other parts of the
988 * bio caused I/O errors, or the block layer merged several
989 * read requests into one and the error is caused by a
990 * different bio (usually one of the two latter cases is
991 * the cause)
992 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100993 spin_lock(&sctx->stat_lock);
994 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800995 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100996 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400997
Stefan Behrensff023aa2012-11-06 11:43:11 +0100998 if (sctx->is_dev_replace)
999 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001000 goto out;
1001 }
1002
1003 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001004 spin_lock(&sctx->stat_lock);
1005 sctx->stat.read_errors++;
1006 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001007 if (__ratelimit(&_rs))
1008 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001009 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001010 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001011 spin_lock(&sctx->stat_lock);
1012 sctx->stat.csum_errors++;
1013 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001014 if (__ratelimit(&_rs))
1015 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001016 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001017 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001018 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001019 spin_lock(&sctx->stat_lock);
1020 sctx->stat.verify_errors++;
1021 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001022 if (__ratelimit(&_rs))
1023 scrub_print_warning("checksum/header error",
1024 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001025 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001026 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001027 BTRFS_DEV_STAT_GENERATION_ERRS);
1028 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001029 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001030 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001031 }
1032
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001033 if (sctx->readonly) {
1034 ASSERT(!sctx->is_dev_replace);
1035 goto out;
1036 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001037
1038 if (!is_metadata && !have_csum) {
1039 struct scrub_fixup_nodatasum *fixup_nodatasum;
1040
Stefan Behrensff023aa2012-11-06 11:43:11 +01001041nodatasum_case:
1042 WARN_ON(sctx->is_dev_replace);
1043
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001044 /*
1045 * !is_metadata and !have_csum, this means that the data
1046 * might not be COW'ed, that it might be modified
1047 * concurrently. The general strategy to work on the
1048 * commit root does not help in the case when COW is not
1049 * used.
1050 */
1051 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1052 if (!fixup_nodatasum)
1053 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001054 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001055 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001056 fixup_nodatasum->logical = logical;
1057 fixup_nodatasum->root = fs_info->extent_root;
1058 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001059 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001060 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1061 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001062 btrfs_queue_work(fs_info->scrub_workers,
1063 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001064 goto out;
1065 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001066
1067 /*
1068 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001069 * checksums.
1070 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001071 * errors and also does not have a checksum error.
1072 * If one is found, and if a checksum is present, the full block
1073 * that is known to contain an error is rewritten. Afterwards
1074 * the block is known to be corrected.
1075 * If a mirror is found which is completely correct, and no
1076 * checksum is present, only those pages are rewritten that had
1077 * an I/O error in the block to be repaired, since it cannot be
1078 * determined, which copy of the other pages is better (and it
1079 * could happen otherwise that a correct page would be
1080 * overwritten by a bad one).
1081 */
1082 for (mirror_index = 0;
1083 mirror_index < BTRFS_MAX_MIRRORS &&
1084 sblocks_for_recheck[mirror_index].page_count > 0;
1085 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001086 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001087
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001088 if (mirror_index == failed_mirror_index)
1089 continue;
1090 sblock_other = sblocks_for_recheck + mirror_index;
1091
1092 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001093 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1094 have_csum, csum, generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001095 sctx->csum_size, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001096
1097 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001098 !sblock_other->checksum_error &&
1099 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001100 if (sctx->is_dev_replace) {
1101 scrub_write_block_to_dev_replace(sblock_other);
1102 } else {
1103 int force_write = is_metadata || have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001104
Stefan Behrensff023aa2012-11-06 11:43:11 +01001105 ret = scrub_repair_block_from_good_copy(
1106 sblock_bad, sblock_other,
1107 force_write);
1108 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001109 if (0 == ret)
1110 goto corrected_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001111 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001112 }
1113
1114 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001115 * for dev_replace, pick good pages and write to the target device.
1116 */
1117 if (sctx->is_dev_replace) {
1118 success = 1;
1119 for (page_num = 0; page_num < sblock_bad->page_count;
1120 page_num++) {
1121 int sub_success;
1122
1123 sub_success = 0;
1124 for (mirror_index = 0;
1125 mirror_index < BTRFS_MAX_MIRRORS &&
1126 sblocks_for_recheck[mirror_index].page_count > 0;
1127 mirror_index++) {
1128 struct scrub_block *sblock_other =
1129 sblocks_for_recheck + mirror_index;
1130 struct scrub_page *page_other =
1131 sblock_other->pagev[page_num];
1132
1133 if (!page_other->io_error) {
1134 ret = scrub_write_page_to_dev_replace(
1135 sblock_other, page_num);
1136 if (ret == 0) {
1137 /* succeeded for this page */
1138 sub_success = 1;
1139 break;
1140 } else {
1141 btrfs_dev_replace_stats_inc(
1142 &sctx->dev_root->
1143 fs_info->dev_replace.
1144 num_write_errors);
1145 }
1146 }
1147 }
1148
1149 if (!sub_success) {
1150 /*
1151 * did not find a mirror to fetch the page
1152 * from. scrub_write_page_to_dev_replace()
1153 * handles this case (page->io_error), by
1154 * filling the block with zeros before
1155 * submitting the write request
1156 */
1157 success = 0;
1158 ret = scrub_write_page_to_dev_replace(
1159 sblock_bad, page_num);
1160 if (ret)
1161 btrfs_dev_replace_stats_inc(
1162 &sctx->dev_root->fs_info->
1163 dev_replace.num_write_errors);
1164 }
1165 }
1166
1167 goto out;
1168 }
1169
1170 /*
1171 * for regular scrub, repair those pages that are errored.
1172 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001173 * repaired, continue by picking good copies of those pages.
1174 * Select the good pages from mirrors to rewrite bad pages from
1175 * the area to fix. Afterwards verify the checksum of the block
1176 * that is supposed to be repaired. This verification step is
1177 * only done for the purpose of statistic counting and for the
1178 * final scrub report, whether errors remain.
1179 * A perfect algorithm could make use of the checksum and try
1180 * all possible combinations of pages from the different mirrors
1181 * until the checksum verification succeeds. For example, when
1182 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1183 * of mirror #2 is readable but the final checksum test fails,
1184 * then the 2nd page of mirror #3 could be tried, whether now
1185 * the final checksum succeedes. But this would be a rare
1186 * exception and is therefore not implemented. At least it is
1187 * avoided that the good copy is overwritten.
1188 * A more useful improvement would be to pick the sectors
1189 * without I/O error based on sector sizes (512 bytes on legacy
1190 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1191 * mirror could be repaired by taking 512 byte of a different
1192 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1193 * area are unreadable.
1194 */
1195
1196 /* can only fix I/O errors from here on */
1197 if (sblock_bad->no_io_error_seen)
1198 goto did_not_correct_error;
1199
1200 success = 1;
1201 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001202 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001203
1204 if (!page_bad->io_error)
1205 continue;
1206
1207 for (mirror_index = 0;
1208 mirror_index < BTRFS_MAX_MIRRORS &&
1209 sblocks_for_recheck[mirror_index].page_count > 0;
1210 mirror_index++) {
1211 struct scrub_block *sblock_other = sblocks_for_recheck +
1212 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001213 struct scrub_page *page_other = sblock_other->pagev[
1214 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001215
1216 if (!page_other->io_error) {
1217 ret = scrub_repair_page_from_good_copy(
1218 sblock_bad, sblock_other, page_num, 0);
1219 if (0 == ret) {
1220 page_bad->io_error = 0;
1221 break; /* succeeded for this page */
1222 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001223 }
1224 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001225
1226 if (page_bad->io_error) {
1227 /* did not find a mirror to copy the page from */
1228 success = 0;
1229 }
1230 }
1231
1232 if (success) {
1233 if (is_metadata || have_csum) {
1234 /*
1235 * need to verify the checksum now that all
1236 * sectors on disk are repaired (the write
1237 * request for data to be repaired is on its way).
1238 * Just be lazy and use scrub_recheck_block()
1239 * which re-reads the data before the checksum
1240 * is verified, but most likely the data comes out
1241 * of the page cache.
1242 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001243 scrub_recheck_block(fs_info, sblock_bad,
1244 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001245 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001246 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001247 !sblock_bad->checksum_error &&
1248 sblock_bad->no_io_error_seen)
1249 goto corrected_error;
1250 else
1251 goto did_not_correct_error;
1252 } else {
1253corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001254 spin_lock(&sctx->stat_lock);
1255 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001256 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001257 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001258 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001259 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001260 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001261 }
1262 } else {
1263did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001264 spin_lock(&sctx->stat_lock);
1265 sctx->stat.uncorrectable_errors++;
1266 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001267 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001268 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001269 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001270 }
1271
1272out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001273 if (sblocks_for_recheck) {
1274 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1275 mirror_index++) {
1276 struct scrub_block *sblock = sblocks_for_recheck +
1277 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001278 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001279 int page_index;
1280
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001281 for (page_index = 0; page_index < sblock->page_count;
1282 page_index++) {
1283 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001284 recover = sblock->pagev[page_index]->recover;
1285 if (recover) {
1286 scrub_put_recover(recover);
1287 sblock->pagev[page_index]->recover =
1288 NULL;
1289 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001290 scrub_page_put(sblock->pagev[page_index]);
1291 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001292 }
1293 kfree(sblocks_for_recheck);
1294 }
1295
1296 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001297}
1298
Miao Xieaf8e2d12014-10-23 14:42:50 +08001299static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio, u64 *raid_map)
1300{
1301 if (raid_map) {
1302 if (raid_map[bbio->num_stripes - 1] == RAID6_Q_STRIPE)
1303 return 3;
1304 else
1305 return 2;
1306 } else {
1307 return (int)bbio->num_stripes;
1308 }
1309}
1310
1311static inline void scrub_stripe_index_and_offset(u64 logical, u64 *raid_map,
1312 u64 mapped_length,
1313 int nstripes, int mirror,
1314 int *stripe_index,
1315 u64 *stripe_offset)
1316{
1317 int i;
1318
1319 if (raid_map) {
1320 /* RAID5/6 */
1321 for (i = 0; i < nstripes; i++) {
1322 if (raid_map[i] == RAID6_Q_STRIPE ||
1323 raid_map[i] == RAID5_P_STRIPE)
1324 continue;
1325
1326 if (logical >= raid_map[i] &&
1327 logical < raid_map[i] + mapped_length)
1328 break;
1329 }
1330
1331 *stripe_index = i;
1332 *stripe_offset = logical - raid_map[i];
1333 } else {
1334 /* The other RAID type */
1335 *stripe_index = mirror;
1336 *stripe_offset = 0;
1337 }
1338}
1339
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001340static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001341 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001342 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001343 u64 length, u64 logical,
1344 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001345{
Miao Xieaf8e2d12014-10-23 14:42:50 +08001346 struct scrub_recover *recover;
1347 struct btrfs_bio *bbio;
1348 u64 *raid_map;
1349 u64 sublen;
1350 u64 mapped_length;
1351 u64 stripe_offset;
1352 int stripe_index;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001353 int page_index;
1354 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001355 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001356 int ret;
1357
1358 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001359 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001360 * are not used (and not set) in the blocks that are used for
1361 * the recheck procedure
1362 */
1363
1364 page_index = 0;
1365 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001366 sublen = min_t(u64, length, PAGE_SIZE);
1367 mapped_length = sublen;
1368 bbio = NULL;
1369 raid_map = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001370
1371 /*
1372 * with a length of PAGE_SIZE, each returned stripe
1373 * represents one mirror
1374 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001375 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
1376 &mapped_length, &bbio, 0, &raid_map);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001377 if (ret || !bbio || mapped_length < sublen) {
1378 kfree(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001379 kfree(raid_map);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001380 return -EIO;
1381 }
1382
Miao Xieaf8e2d12014-10-23 14:42:50 +08001383 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1384 if (!recover) {
1385 kfree(bbio);
1386 kfree(raid_map);
1387 return -ENOMEM;
1388 }
1389
1390 atomic_set(&recover->refs, 1);
1391 recover->bbio = bbio;
1392 recover->raid_map = raid_map;
1393 recover->map_length = mapped_length;
1394
Stefan Behrensff023aa2012-11-06 11:43:11 +01001395 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001396
1397 nmirrors = scrub_nr_raid_mirrors(bbio, raid_map);
1398 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001399 mirror_index++) {
1400 struct scrub_block *sblock;
1401 struct scrub_page *page;
1402
1403 if (mirror_index >= BTRFS_MAX_MIRRORS)
1404 continue;
1405
1406 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001407 sblock->sctx = sctx;
1408 page = kzalloc(sizeof(*page), GFP_NOFS);
1409 if (!page) {
1410leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001411 spin_lock(&sctx->stat_lock);
1412 sctx->stat.malloc_errors++;
1413 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001414 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001415 return -ENOMEM;
1416 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001417 scrub_page_get(page);
1418 sblock->pagev[page_index] = page;
1419 page->logical = logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001420
1421 scrub_stripe_index_and_offset(logical, raid_map,
1422 mapped_length,
1423 bbio->num_stripes,
1424 mirror_index,
1425 &stripe_index,
1426 &stripe_offset);
1427 page->physical = bbio->stripes[stripe_index].physical +
1428 stripe_offset;
1429 page->dev = bbio->stripes[stripe_index].dev;
1430
Stefan Behrensff023aa2012-11-06 11:43:11 +01001431 BUG_ON(page_index >= original_sblock->page_count);
1432 page->physical_for_dev_replace =
1433 original_sblock->pagev[page_index]->
1434 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001435 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001436 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001437 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001438 page->page = alloc_page(GFP_NOFS);
1439 if (!page->page)
1440 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001441
1442 scrub_get_recover(recover);
1443 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001444 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001445 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001446 length -= sublen;
1447 logical += sublen;
1448 page_index++;
1449 }
1450
1451 return 0;
1452}
1453
Miao Xieaf8e2d12014-10-23 14:42:50 +08001454struct scrub_bio_ret {
1455 struct completion event;
1456 int error;
1457};
1458
1459static void scrub_bio_wait_endio(struct bio *bio, int error)
1460{
1461 struct scrub_bio_ret *ret = bio->bi_private;
1462
1463 ret->error = error;
1464 complete(&ret->event);
1465}
1466
1467static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1468{
1469 return page->recover && page->recover->raid_map;
1470}
1471
1472static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1473 struct bio *bio,
1474 struct scrub_page *page)
1475{
1476 struct scrub_bio_ret done;
1477 int ret;
1478
1479 init_completion(&done.event);
1480 done.error = 0;
1481 bio->bi_iter.bi_sector = page->logical >> 9;
1482 bio->bi_private = &done;
1483 bio->bi_end_io = scrub_bio_wait_endio;
1484
1485 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
1486 page->recover->raid_map,
1487 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001488 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001489 if (ret)
1490 return ret;
1491
1492 wait_for_completion(&done.event);
1493 if (done.error)
1494 return -EIO;
1495
1496 return 0;
1497}
1498
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001499/*
1500 * this function will check the on disk data for checksum errors, header
1501 * errors and read I/O errors. If any I/O errors happen, the exact pages
1502 * which are errored are marked as being bad. The goal is to enable scrub
1503 * to take those pages that are not errored from all the mirrors so that
1504 * the pages that are errored in the just handled mirror can be repaired.
1505 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001506static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1507 struct scrub_block *sblock, int is_metadata,
1508 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001509 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001510{
1511 int page_num;
1512
1513 sblock->no_io_error_seen = 1;
1514 sblock->header_error = 0;
1515 sblock->checksum_error = 0;
1516
1517 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1518 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001519 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001520
Stefan Behrens442a4f62012-05-25 16:06:08 +02001521 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001522 page->io_error = 1;
1523 sblock->no_io_error_seen = 0;
1524 continue;
1525 }
1526
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001527 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001528 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001529 if (!bio) {
1530 page->io_error = 1;
1531 sblock->no_io_error_seen = 0;
1532 continue;
1533 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001534 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001535
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001536 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001537 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1538 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1539 sblock->no_io_error_seen = 0;
1540 } else {
1541 bio->bi_iter.bi_sector = page->physical >> 9;
1542
1543 if (btrfsic_submit_bio_wait(READ, bio))
1544 sblock->no_io_error_seen = 0;
1545 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001546
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001547 bio_put(bio);
1548 }
1549
1550 if (sblock->no_io_error_seen)
1551 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1552 have_csum, csum, generation,
1553 csum_size);
1554
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001555 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001556}
1557
Miao Xie17a9be22014-07-24 11:37:08 +08001558static inline int scrub_check_fsid(u8 fsid[],
1559 struct scrub_page *spage)
1560{
1561 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1562 int ret;
1563
1564 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1565 return !ret;
1566}
1567
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001568static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1569 struct scrub_block *sblock,
1570 int is_metadata, int have_csum,
1571 const u8 *csum, u64 generation,
1572 u16 csum_size)
1573{
1574 int page_num;
1575 u8 calculated_csum[BTRFS_CSUM_SIZE];
1576 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001577 void *mapped_buffer;
1578
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001579 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001580 if (is_metadata) {
1581 struct btrfs_header *h;
1582
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001583 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001584 h = (struct btrfs_header *)mapped_buffer;
1585
Qu Wenruo3cae2102013-07-16 11:19:18 +08001586 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001587 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001588 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001589 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001590 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001591 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001592 sblock->header_error = 1;
1593 sblock->generation_error = 1;
1594 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001595 csum = h->csum;
1596 } else {
1597 if (!have_csum)
1598 return;
1599
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001600 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001601 }
1602
1603 for (page_num = 0;;) {
1604 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001605 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001606 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1607 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1608 else
Liu Bob0496682013-03-14 14:57:45 +00001609 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001610
Linus Torvalds9613beb2012-03-30 12:44:29 -07001611 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001612 page_num++;
1613 if (page_num >= sblock->page_count)
1614 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001615 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001616
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001617 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001618 }
1619
1620 btrfs_csum_final(crc, calculated_csum);
1621 if (memcmp(calculated_csum, csum, csum_size))
1622 sblock->checksum_error = 1;
1623}
1624
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001625static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1626 struct scrub_block *sblock_good,
1627 int force_write)
1628{
1629 int page_num;
1630 int ret = 0;
1631
1632 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1633 int ret_sub;
1634
1635 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1636 sblock_good,
1637 page_num,
1638 force_write);
1639 if (ret_sub)
1640 ret = ret_sub;
1641 }
1642
1643 return ret;
1644}
1645
1646static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1647 struct scrub_block *sblock_good,
1648 int page_num, int force_write)
1649{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001650 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1651 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001652
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001653 BUG_ON(page_bad->page == NULL);
1654 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001655 if (force_write || sblock_bad->header_error ||
1656 sblock_bad->checksum_error || page_bad->io_error) {
1657 struct bio *bio;
1658 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001659
Stefan Behrensff023aa2012-11-06 11:43:11 +01001660 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001661 printk_ratelimited(KERN_WARNING "BTRFS: "
1662 "scrub_repair_page_from_good_copy(bdev == NULL) "
1663 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001664 return -EIO;
1665 }
1666
Chris Mason9be33952013-05-17 18:30:14 -04001667 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001668 if (!bio)
1669 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001670 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001671 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001672
1673 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1674 if (PAGE_SIZE != ret) {
1675 bio_put(bio);
1676 return -EIO;
1677 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001678
Kent Overstreet33879d42013-11-23 22:33:32 -08001679 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001680 btrfs_dev_stat_inc_and_print(page_bad->dev,
1681 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001682 btrfs_dev_replace_stats_inc(
1683 &sblock_bad->sctx->dev_root->fs_info->
1684 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001685 bio_put(bio);
1686 return -EIO;
1687 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001688 bio_put(bio);
1689 }
1690
1691 return 0;
1692}
1693
Stefan Behrensff023aa2012-11-06 11:43:11 +01001694static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1695{
1696 int page_num;
1697
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001698 /*
1699 * This block is used for the check of the parity on the source device,
1700 * so the data needn't be written into the destination device.
1701 */
1702 if (sblock->sparity)
1703 return;
1704
Stefan Behrensff023aa2012-11-06 11:43:11 +01001705 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1706 int ret;
1707
1708 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1709 if (ret)
1710 btrfs_dev_replace_stats_inc(
1711 &sblock->sctx->dev_root->fs_info->dev_replace.
1712 num_write_errors);
1713 }
1714}
1715
1716static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1717 int page_num)
1718{
1719 struct scrub_page *spage = sblock->pagev[page_num];
1720
1721 BUG_ON(spage->page == NULL);
1722 if (spage->io_error) {
1723 void *mapped_buffer = kmap_atomic(spage->page);
1724
1725 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1726 flush_dcache_page(spage->page);
1727 kunmap_atomic(mapped_buffer);
1728 }
1729 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1730}
1731
1732static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1733 struct scrub_page *spage)
1734{
1735 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1736 struct scrub_bio *sbio;
1737 int ret;
1738
1739 mutex_lock(&wr_ctx->wr_lock);
1740again:
1741 if (!wr_ctx->wr_curr_bio) {
1742 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1743 GFP_NOFS);
1744 if (!wr_ctx->wr_curr_bio) {
1745 mutex_unlock(&wr_ctx->wr_lock);
1746 return -ENOMEM;
1747 }
1748 wr_ctx->wr_curr_bio->sctx = sctx;
1749 wr_ctx->wr_curr_bio->page_count = 0;
1750 }
1751 sbio = wr_ctx->wr_curr_bio;
1752 if (sbio->page_count == 0) {
1753 struct bio *bio;
1754
1755 sbio->physical = spage->physical_for_dev_replace;
1756 sbio->logical = spage->logical;
1757 sbio->dev = wr_ctx->tgtdev;
1758 bio = sbio->bio;
1759 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001760 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001761 if (!bio) {
1762 mutex_unlock(&wr_ctx->wr_lock);
1763 return -ENOMEM;
1764 }
1765 sbio->bio = bio;
1766 }
1767
1768 bio->bi_private = sbio;
1769 bio->bi_end_io = scrub_wr_bio_end_io;
1770 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001771 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001772 sbio->err = 0;
1773 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1774 spage->physical_for_dev_replace ||
1775 sbio->logical + sbio->page_count * PAGE_SIZE !=
1776 spage->logical) {
1777 scrub_wr_submit(sctx);
1778 goto again;
1779 }
1780
1781 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1782 if (ret != PAGE_SIZE) {
1783 if (sbio->page_count < 1) {
1784 bio_put(sbio->bio);
1785 sbio->bio = NULL;
1786 mutex_unlock(&wr_ctx->wr_lock);
1787 return -EIO;
1788 }
1789 scrub_wr_submit(sctx);
1790 goto again;
1791 }
1792
1793 sbio->pagev[sbio->page_count] = spage;
1794 scrub_page_get(spage);
1795 sbio->page_count++;
1796 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1797 scrub_wr_submit(sctx);
1798 mutex_unlock(&wr_ctx->wr_lock);
1799
1800 return 0;
1801}
1802
1803static void scrub_wr_submit(struct scrub_ctx *sctx)
1804{
1805 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1806 struct scrub_bio *sbio;
1807
1808 if (!wr_ctx->wr_curr_bio)
1809 return;
1810
1811 sbio = wr_ctx->wr_curr_bio;
1812 wr_ctx->wr_curr_bio = NULL;
1813 WARN_ON(!sbio->bio->bi_bdev);
1814 scrub_pending_bio_inc(sctx);
1815 /* process all writes in a single worker thread. Then the block layer
1816 * orders the requests before sending them to the driver which
1817 * doubled the write performance on spinning disks when measured
1818 * with Linux 3.5 */
1819 btrfsic_submit_bio(WRITE, sbio->bio);
1820}
1821
1822static void scrub_wr_bio_end_io(struct bio *bio, int err)
1823{
1824 struct scrub_bio *sbio = bio->bi_private;
1825 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1826
1827 sbio->err = err;
1828 sbio->bio = bio;
1829
Liu Bo9e0af232014-08-15 23:36:53 +08001830 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1831 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001832 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001833}
1834
1835static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1836{
1837 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1838 struct scrub_ctx *sctx = sbio->sctx;
1839 int i;
1840
1841 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1842 if (sbio->err) {
1843 struct btrfs_dev_replace *dev_replace =
1844 &sbio->sctx->dev_root->fs_info->dev_replace;
1845
1846 for (i = 0; i < sbio->page_count; i++) {
1847 struct scrub_page *spage = sbio->pagev[i];
1848
1849 spage->io_error = 1;
1850 btrfs_dev_replace_stats_inc(&dev_replace->
1851 num_write_errors);
1852 }
1853 }
1854
1855 for (i = 0; i < sbio->page_count; i++)
1856 scrub_page_put(sbio->pagev[i]);
1857
1858 bio_put(sbio->bio);
1859 kfree(sbio);
1860 scrub_pending_bio_dec(sctx);
1861}
1862
1863static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001864{
1865 u64 flags;
1866 int ret;
1867
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001868 WARN_ON(sblock->page_count < 1);
1869 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001870 ret = 0;
1871 if (flags & BTRFS_EXTENT_FLAG_DATA)
1872 ret = scrub_checksum_data(sblock);
1873 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1874 ret = scrub_checksum_tree_block(sblock);
1875 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1876 (void)scrub_checksum_super(sblock);
1877 else
1878 WARN_ON(1);
1879 if (ret)
1880 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001881
1882 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001883}
1884
1885static int scrub_checksum_data(struct scrub_block *sblock)
1886{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001887 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001888 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001889 u8 *on_disk_csum;
1890 struct page *page;
1891 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001892 u32 crc = ~(u32)0;
1893 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001894 u64 len;
1895 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001896
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001897 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001898 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001899 return 0;
1900
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001901 on_disk_csum = sblock->pagev[0]->csum;
1902 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001903 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001904
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001905 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001906 index = 0;
1907 for (;;) {
1908 u64 l = min_t(u64, len, PAGE_SIZE);
1909
Liu Bob0496682013-03-14 14:57:45 +00001910 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001911 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001912 len -= l;
1913 if (len == 0)
1914 break;
1915 index++;
1916 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001917 BUG_ON(!sblock->pagev[index]->page);
1918 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001919 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001920 }
1921
Arne Jansena2de7332011-03-08 14:14:00 +01001922 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001923 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001924 fail = 1;
1925
Arne Jansena2de7332011-03-08 14:14:00 +01001926 return fail;
1927}
1928
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001929static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001930{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001931 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001932 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001933 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001934 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001935 u8 calculated_csum[BTRFS_CSUM_SIZE];
1936 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1937 struct page *page;
1938 void *mapped_buffer;
1939 u64 mapped_size;
1940 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001941 u32 crc = ~(u32)0;
1942 int fail = 0;
1943 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001944 u64 len;
1945 int index;
1946
1947 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001948 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001949 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001950 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001951 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001952
1953 /*
1954 * we don't use the getter functions here, as we
1955 * a) don't have an extent buffer and
1956 * b) the page is already kmapped
1957 */
Arne Jansena2de7332011-03-08 14:14:00 +01001958
Qu Wenruo3cae2102013-07-16 11:19:18 +08001959 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001960 ++fail;
1961
Qu Wenruo3cae2102013-07-16 11:19:18 +08001962 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001963 ++fail;
1964
Miao Xie17a9be22014-07-24 11:37:08 +08001965 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001966 ++fail;
1967
1968 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1969 BTRFS_UUID_SIZE))
1970 ++fail;
1971
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001972 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001973 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1974 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1975 index = 0;
1976 for (;;) {
1977 u64 l = min_t(u64, len, mapped_size);
1978
Liu Bob0496682013-03-14 14:57:45 +00001979 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001980 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001981 len -= l;
1982 if (len == 0)
1983 break;
1984 index++;
1985 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001986 BUG_ON(!sblock->pagev[index]->page);
1987 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001988 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001989 mapped_size = PAGE_SIZE;
1990 p = mapped_buffer;
1991 }
1992
1993 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001994 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001995 ++crc_fail;
1996
Arne Jansena2de7332011-03-08 14:14:00 +01001997 return fail || crc_fail;
1998}
1999
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002000static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002001{
2002 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002003 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002004 u8 calculated_csum[BTRFS_CSUM_SIZE];
2005 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2006 struct page *page;
2007 void *mapped_buffer;
2008 u64 mapped_size;
2009 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002010 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002011 int fail_gen = 0;
2012 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002013 u64 len;
2014 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002015
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002016 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002017 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002018 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002019 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002020 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002021
Qu Wenruo3cae2102013-07-16 11:19:18 +08002022 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002023 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002024
Qu Wenruo3cae2102013-07-16 11:19:18 +08002025 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002026 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002027
Miao Xie17a9be22014-07-24 11:37:08 +08002028 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002029 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002030
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002031 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2032 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2033 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2034 index = 0;
2035 for (;;) {
2036 u64 l = min_t(u64, len, mapped_size);
2037
Liu Bob0496682013-03-14 14:57:45 +00002038 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002039 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002040 len -= l;
2041 if (len == 0)
2042 break;
2043 index++;
2044 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002045 BUG_ON(!sblock->pagev[index]->page);
2046 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002047 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002048 mapped_size = PAGE_SIZE;
2049 p = mapped_buffer;
2050 }
2051
2052 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002053 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002054 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002055
Stefan Behrens442a4f62012-05-25 16:06:08 +02002056 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002057 /*
2058 * if we find an error in a super block, we just report it.
2059 * They will get written with the next transaction commit
2060 * anyway
2061 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002062 spin_lock(&sctx->stat_lock);
2063 ++sctx->stat.super_errors;
2064 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002065 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002066 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002067 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2068 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002069 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002070 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002071 }
2072
Stefan Behrens442a4f62012-05-25 16:06:08 +02002073 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002074}
2075
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002076static void scrub_block_get(struct scrub_block *sblock)
2077{
2078 atomic_inc(&sblock->ref_count);
2079}
2080
2081static void scrub_block_put(struct scrub_block *sblock)
2082{
2083 if (atomic_dec_and_test(&sblock->ref_count)) {
2084 int i;
2085
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002086 if (sblock->sparity)
2087 scrub_parity_put(sblock->sparity);
2088
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002089 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002090 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002091 kfree(sblock);
2092 }
2093}
2094
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002095static void scrub_page_get(struct scrub_page *spage)
2096{
2097 atomic_inc(&spage->ref_count);
2098}
2099
2100static void scrub_page_put(struct scrub_page *spage)
2101{
2102 if (atomic_dec_and_test(&spage->ref_count)) {
2103 if (spage->page)
2104 __free_page(spage->page);
2105 kfree(spage);
2106 }
2107}
2108
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002109static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002110{
2111 struct scrub_bio *sbio;
2112
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002113 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002114 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002115
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002116 sbio = sctx->bios[sctx->curr];
2117 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002118 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002119
Stefan Behrensff023aa2012-11-06 11:43:11 +01002120 if (!sbio->bio->bi_bdev) {
2121 /*
2122 * this case should not happen. If btrfs_map_block() is
2123 * wrong, it could happen for dev-replace operations on
2124 * missing devices when no mirrors are available, but in
2125 * this case it should already fail the mount.
2126 * This case is handled correctly (but _very_ slowly).
2127 */
2128 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05002129 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01002130 bio_endio(sbio->bio, -EIO);
2131 } else {
2132 btrfsic_submit_bio(READ, sbio->bio);
2133 }
Arne Jansena2de7332011-03-08 14:14:00 +01002134}
2135
Stefan Behrensff023aa2012-11-06 11:43:11 +01002136static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2137 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002138{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002139 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002140 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002141 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002142
2143again:
2144 /*
2145 * grab a fresh bio or wait for one to become available
2146 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002147 while (sctx->curr == -1) {
2148 spin_lock(&sctx->list_lock);
2149 sctx->curr = sctx->first_free;
2150 if (sctx->curr != -1) {
2151 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2152 sctx->bios[sctx->curr]->next_free = -1;
2153 sctx->bios[sctx->curr]->page_count = 0;
2154 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002155 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002156 spin_unlock(&sctx->list_lock);
2157 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002158 }
2159 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002160 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002161 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002162 struct bio *bio;
2163
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002164 sbio->physical = spage->physical;
2165 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002166 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002167 bio = sbio->bio;
2168 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002169 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002170 if (!bio)
2171 return -ENOMEM;
2172 sbio->bio = bio;
2173 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002174
2175 bio->bi_private = sbio;
2176 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002177 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002178 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002179 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002180 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2181 spage->physical ||
2182 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002183 spage->logical ||
2184 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002185 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002186 goto again;
2187 }
2188
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002189 sbio->pagev[sbio->page_count] = spage;
2190 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2191 if (ret != PAGE_SIZE) {
2192 if (sbio->page_count < 1) {
2193 bio_put(sbio->bio);
2194 sbio->bio = NULL;
2195 return -EIO;
2196 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002197 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002198 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002199 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002200
Stefan Behrensff023aa2012-11-06 11:43:11 +01002201 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002202 atomic_inc(&sblock->outstanding_pages);
2203 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002204 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002205 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002206
2207 return 0;
2208}
2209
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002210static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002211 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002212 u64 gen, int mirror_num, u8 *csum, int force,
2213 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002214{
2215 struct scrub_block *sblock;
2216 int index;
2217
2218 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2219 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002220 spin_lock(&sctx->stat_lock);
2221 sctx->stat.malloc_errors++;
2222 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002223 return -ENOMEM;
2224 }
2225
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002226 /* one ref inside this function, plus one for each page added to
2227 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002228 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002229 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002230 sblock->no_io_error_seen = 1;
2231
2232 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002233 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002234 u64 l = min_t(u64, len, PAGE_SIZE);
2235
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002236 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2237 if (!spage) {
2238leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002239 spin_lock(&sctx->stat_lock);
2240 sctx->stat.malloc_errors++;
2241 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002242 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002243 return -ENOMEM;
2244 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002245 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2246 scrub_page_get(spage);
2247 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002248 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002249 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002250 spage->flags = flags;
2251 spage->generation = gen;
2252 spage->logical = logical;
2253 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002254 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002255 spage->mirror_num = mirror_num;
2256 if (csum) {
2257 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002258 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002259 } else {
2260 spage->have_csum = 0;
2261 }
2262 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002263 spage->page = alloc_page(GFP_NOFS);
2264 if (!spage->page)
2265 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002266 len -= l;
2267 logical += l;
2268 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002269 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002270 }
2271
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002272 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002273 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002274 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002275 int ret;
2276
Stefan Behrensff023aa2012-11-06 11:43:11 +01002277 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002278 if (ret) {
2279 scrub_block_put(sblock);
2280 return ret;
2281 }
2282 }
2283
2284 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002285 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002286
2287 /* last one frees, either here or in bio completion for last page */
2288 scrub_block_put(sblock);
2289 return 0;
2290}
2291
2292static void scrub_bio_end_io(struct bio *bio, int err)
2293{
2294 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002295 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002296
2297 sbio->err = err;
2298 sbio->bio = bio;
2299
Qu Wenruo0339ef22014-02-28 10:46:17 +08002300 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002301}
2302
2303static void scrub_bio_end_io_worker(struct btrfs_work *work)
2304{
2305 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002306 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002307 int i;
2308
Stefan Behrensff023aa2012-11-06 11:43:11 +01002309 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002310 if (sbio->err) {
2311 for (i = 0; i < sbio->page_count; i++) {
2312 struct scrub_page *spage = sbio->pagev[i];
2313
2314 spage->io_error = 1;
2315 spage->sblock->no_io_error_seen = 0;
2316 }
2317 }
2318
2319 /* now complete the scrub_block items that have all pages completed */
2320 for (i = 0; i < sbio->page_count; i++) {
2321 struct scrub_page *spage = sbio->pagev[i];
2322 struct scrub_block *sblock = spage->sblock;
2323
2324 if (atomic_dec_and_test(&sblock->outstanding_pages))
2325 scrub_block_complete(sblock);
2326 scrub_block_put(sblock);
2327 }
2328
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002329 bio_put(sbio->bio);
2330 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002331 spin_lock(&sctx->list_lock);
2332 sbio->next_free = sctx->first_free;
2333 sctx->first_free = sbio->index;
2334 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002335
2336 if (sctx->is_dev_replace &&
2337 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2338 mutex_lock(&sctx->wr_ctx.wr_lock);
2339 scrub_wr_submit(sctx);
2340 mutex_unlock(&sctx->wr_ctx.wr_lock);
2341 }
2342
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002343 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002344}
2345
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002346static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2347 unsigned long *bitmap,
2348 u64 start, u64 len)
2349{
2350 int offset;
2351 int nsectors;
2352 int sectorsize = sparity->sctx->dev_root->sectorsize;
2353
2354 if (len >= sparity->stripe_len) {
2355 bitmap_set(bitmap, 0, sparity->nsectors);
2356 return;
2357 }
2358
2359 start -= sparity->logic_start;
2360 offset = (int)do_div(start, sparity->stripe_len);
2361 offset /= sectorsize;
2362 nsectors = (int)len / sectorsize;
2363
2364 if (offset + nsectors <= sparity->nsectors) {
2365 bitmap_set(bitmap, offset, nsectors);
2366 return;
2367 }
2368
2369 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2370 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2371}
2372
2373static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2374 u64 start, u64 len)
2375{
2376 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2377}
2378
2379static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2380 u64 start, u64 len)
2381{
2382 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2383}
2384
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002385static void scrub_block_complete(struct scrub_block *sblock)
2386{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002387 int corrupted = 0;
2388
Stefan Behrensff023aa2012-11-06 11:43:11 +01002389 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002390 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002391 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002392 } else {
2393 /*
2394 * if has checksum error, write via repair mechanism in
2395 * dev replace case, otherwise write here in dev replace
2396 * case.
2397 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002398 corrupted = scrub_checksum(sblock);
2399 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002400 scrub_write_block_to_dev_replace(sblock);
2401 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002402
2403 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2404 u64 start = sblock->pagev[0]->logical;
2405 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2406 PAGE_SIZE;
2407
2408 scrub_parity_mark_sectors_error(sblock->sparity,
2409 start, end - start);
2410 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002411}
2412
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002413static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002414 u8 *csum)
2415{
2416 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002417 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002418 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002419
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002420 while (!list_empty(&sctx->csum_list)) {
2421 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002422 struct btrfs_ordered_sum, list);
2423 if (sum->bytenr > logical)
2424 return 0;
2425 if (sum->bytenr + sum->len > logical)
2426 break;
2427
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002428 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002429 list_del(&sum->list);
2430 kfree(sum);
2431 sum = NULL;
2432 }
2433 if (!sum)
2434 return 0;
2435
Miao Xief51a4a12013-06-19 10:36:09 +08002436 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002437 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002438 memcpy(csum, sum->sums + index, sctx->csum_size);
2439 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002440 list_del(&sum->list);
2441 kfree(sum);
2442 }
Miao Xief51a4a12013-06-19 10:36:09 +08002443 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002444}
2445
2446/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002447static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002448 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002449 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002450{
2451 int ret;
2452 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002453 u32 blocksize;
2454
2455 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002456 blocksize = sctx->sectorsize;
2457 spin_lock(&sctx->stat_lock);
2458 sctx->stat.data_extents_scrubbed++;
2459 sctx->stat.data_bytes_scrubbed += len;
2460 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002461 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002462 blocksize = sctx->nodesize;
2463 spin_lock(&sctx->stat_lock);
2464 sctx->stat.tree_extents_scrubbed++;
2465 sctx->stat.tree_bytes_scrubbed += len;
2466 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002467 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002468 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002469 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002470 }
Arne Jansena2de7332011-03-08 14:14:00 +01002471
2472 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002473 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002474 int have_csum = 0;
2475
2476 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2477 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002478 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002479 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002480 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002481 if (sctx->is_dev_replace && !have_csum) {
2482 ret = copy_nocow_pages(sctx, logical, l,
2483 mirror_num,
2484 physical_for_dev_replace);
2485 goto behind_scrub_pages;
2486 }
Arne Jansena2de7332011-03-08 14:14:00 +01002487 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002488 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002489 mirror_num, have_csum ? csum : NULL, 0,
2490 physical_for_dev_replace);
2491behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002492 if (ret)
2493 return ret;
2494 len -= l;
2495 logical += l;
2496 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002497 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002498 }
2499 return 0;
2500}
2501
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002502static int scrub_pages_for_parity(struct scrub_parity *sparity,
2503 u64 logical, u64 len,
2504 u64 physical, struct btrfs_device *dev,
2505 u64 flags, u64 gen, int mirror_num, u8 *csum)
2506{
2507 struct scrub_ctx *sctx = sparity->sctx;
2508 struct scrub_block *sblock;
2509 int index;
2510
2511 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2512 if (!sblock) {
2513 spin_lock(&sctx->stat_lock);
2514 sctx->stat.malloc_errors++;
2515 spin_unlock(&sctx->stat_lock);
2516 return -ENOMEM;
2517 }
2518
2519 /* one ref inside this function, plus one for each page added to
2520 * a bio later on */
2521 atomic_set(&sblock->ref_count, 1);
2522 sblock->sctx = sctx;
2523 sblock->no_io_error_seen = 1;
2524 sblock->sparity = sparity;
2525 scrub_parity_get(sparity);
2526
2527 for (index = 0; len > 0; index++) {
2528 struct scrub_page *spage;
2529 u64 l = min_t(u64, len, PAGE_SIZE);
2530
2531 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2532 if (!spage) {
2533leave_nomem:
2534 spin_lock(&sctx->stat_lock);
2535 sctx->stat.malloc_errors++;
2536 spin_unlock(&sctx->stat_lock);
2537 scrub_block_put(sblock);
2538 return -ENOMEM;
2539 }
2540 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2541 /* For scrub block */
2542 scrub_page_get(spage);
2543 sblock->pagev[index] = spage;
2544 /* For scrub parity */
2545 scrub_page_get(spage);
2546 list_add_tail(&spage->list, &sparity->spages);
2547 spage->sblock = sblock;
2548 spage->dev = dev;
2549 spage->flags = flags;
2550 spage->generation = gen;
2551 spage->logical = logical;
2552 spage->physical = physical;
2553 spage->mirror_num = mirror_num;
2554 if (csum) {
2555 spage->have_csum = 1;
2556 memcpy(spage->csum, csum, sctx->csum_size);
2557 } else {
2558 spage->have_csum = 0;
2559 }
2560 sblock->page_count++;
2561 spage->page = alloc_page(GFP_NOFS);
2562 if (!spage->page)
2563 goto leave_nomem;
2564 len -= l;
2565 logical += l;
2566 physical += l;
2567 }
2568
2569 WARN_ON(sblock->page_count == 0);
2570 for (index = 0; index < sblock->page_count; index++) {
2571 struct scrub_page *spage = sblock->pagev[index];
2572 int ret;
2573
2574 ret = scrub_add_page_to_rd_bio(sctx, spage);
2575 if (ret) {
2576 scrub_block_put(sblock);
2577 return ret;
2578 }
2579 }
2580
2581 /* last one frees, either here or in bio completion for last page */
2582 scrub_block_put(sblock);
2583 return 0;
2584}
2585
2586static int scrub_extent_for_parity(struct scrub_parity *sparity,
2587 u64 logical, u64 len,
2588 u64 physical, struct btrfs_device *dev,
2589 u64 flags, u64 gen, int mirror_num)
2590{
2591 struct scrub_ctx *sctx = sparity->sctx;
2592 int ret;
2593 u8 csum[BTRFS_CSUM_SIZE];
2594 u32 blocksize;
2595
2596 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2597 blocksize = sctx->sectorsize;
2598 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2599 blocksize = sctx->nodesize;
2600 } else {
2601 blocksize = sctx->sectorsize;
2602 WARN_ON(1);
2603 }
2604
2605 while (len) {
2606 u64 l = min_t(u64, len, blocksize);
2607 int have_csum = 0;
2608
2609 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2610 /* push csums to sbio */
2611 have_csum = scrub_find_csum(sctx, logical, l, csum);
2612 if (have_csum == 0)
2613 goto skip;
2614 }
2615 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2616 flags, gen, mirror_num,
2617 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002618 if (ret)
2619 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002620skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002621 len -= l;
2622 logical += l;
2623 physical += l;
2624 }
2625 return 0;
2626}
2627
Wang Shilong3b080b22014-04-01 18:01:43 +08002628/*
2629 * Given a physical address, this will calculate it's
2630 * logical offset. if this is a parity stripe, it will return
2631 * the most left data stripe's logical offset.
2632 *
2633 * return 0 if it is a data stripe, 1 means parity stripe.
2634 */
2635static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002636 struct map_lookup *map, u64 *offset,
2637 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002638{
2639 int i;
2640 int j = 0;
2641 u64 stripe_nr;
2642 u64 last_offset;
2643 int stripe_index;
2644 int rot;
2645
2646 last_offset = (physical - map->stripes[num].physical) *
2647 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002648 if (stripe_start)
2649 *stripe_start = last_offset;
2650
Wang Shilong3b080b22014-04-01 18:01:43 +08002651 *offset = last_offset;
2652 for (i = 0; i < nr_data_stripes(map); i++) {
2653 *offset = last_offset + i * map->stripe_len;
2654
2655 stripe_nr = *offset;
2656 do_div(stripe_nr, map->stripe_len);
2657 do_div(stripe_nr, nr_data_stripes(map));
2658
2659 /* Work out the disk rotation on this stripe-set */
2660 rot = do_div(stripe_nr, map->num_stripes);
2661 /* calculate which stripe this data locates */
2662 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002663 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002664 if (stripe_index == num)
2665 return 0;
2666 if (stripe_index < num)
2667 j++;
2668 }
2669 *offset = last_offset + j * map->stripe_len;
2670 return 1;
2671}
2672
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002673static void scrub_free_parity(struct scrub_parity *sparity)
2674{
2675 struct scrub_ctx *sctx = sparity->sctx;
2676 struct scrub_page *curr, *next;
2677 int nbits;
2678
2679 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2680 if (nbits) {
2681 spin_lock(&sctx->stat_lock);
2682 sctx->stat.read_errors += nbits;
2683 sctx->stat.uncorrectable_errors += nbits;
2684 spin_unlock(&sctx->stat_lock);
2685 }
2686
2687 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2688 list_del_init(&curr->list);
2689 scrub_page_put(curr);
2690 }
2691
2692 kfree(sparity);
2693}
2694
2695static void scrub_parity_bio_endio(struct bio *bio, int error)
2696{
2697 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2698 struct scrub_ctx *sctx = sparity->sctx;
2699
2700 if (error)
2701 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2702 sparity->nsectors);
2703
2704 scrub_free_parity(sparity);
2705 scrub_pending_bio_dec(sctx);
2706 bio_put(bio);
2707}
2708
2709static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2710{
2711 struct scrub_ctx *sctx = sparity->sctx;
2712 struct bio *bio;
2713 struct btrfs_raid_bio *rbio;
2714 struct scrub_page *spage;
2715 struct btrfs_bio *bbio = NULL;
2716 u64 *raid_map = NULL;
2717 u64 length;
2718 int ret;
2719
2720 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2721 sparity->nsectors))
2722 goto out;
2723
2724 length = sparity->logic_end - sparity->logic_start + 1;
Miao Xie76035972014-11-14 17:45:42 +08002725 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002726 sparity->logic_start,
2727 &length, &bbio, 0, &raid_map);
2728 if (ret || !bbio || !raid_map)
2729 goto bbio_out;
2730
2731 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2732 if (!bio)
2733 goto bbio_out;
2734
2735 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2736 bio->bi_private = sparity;
2737 bio->bi_end_io = scrub_parity_bio_endio;
2738
2739 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
2740 raid_map, length,
2741 sparity->scrub_dev,
2742 sparity->dbitmap,
2743 sparity->nsectors);
2744 if (!rbio)
2745 goto rbio_out;
2746
2747 list_for_each_entry(spage, &sparity->spages, list)
2748 raid56_parity_add_scrub_pages(rbio, spage->page,
2749 spage->logical);
2750
2751 scrub_pending_bio_inc(sctx);
2752 raid56_parity_submit_scrub_rbio(rbio);
2753 return;
2754
2755rbio_out:
2756 bio_put(bio);
2757bbio_out:
2758 kfree(bbio);
2759 kfree(raid_map);
2760 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2761 sparity->nsectors);
2762 spin_lock(&sctx->stat_lock);
2763 sctx->stat.malloc_errors++;
2764 spin_unlock(&sctx->stat_lock);
2765out:
2766 scrub_free_parity(sparity);
2767}
2768
2769static inline int scrub_calc_parity_bitmap_len(int nsectors)
2770{
2771 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2772}
2773
2774static void scrub_parity_get(struct scrub_parity *sparity)
2775{
2776 atomic_inc(&sparity->ref_count);
2777}
2778
2779static void scrub_parity_put(struct scrub_parity *sparity)
2780{
2781 if (!atomic_dec_and_test(&sparity->ref_count))
2782 return;
2783
2784 scrub_parity_check_and_repair(sparity);
2785}
2786
2787static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2788 struct map_lookup *map,
2789 struct btrfs_device *sdev,
2790 struct btrfs_path *path,
2791 u64 logic_start,
2792 u64 logic_end)
2793{
2794 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2795 struct btrfs_root *root = fs_info->extent_root;
2796 struct btrfs_root *csum_root = fs_info->csum_root;
2797 struct btrfs_extent_item *extent;
2798 u64 flags;
2799 int ret;
2800 int slot;
2801 struct extent_buffer *l;
2802 struct btrfs_key key;
2803 u64 generation;
2804 u64 extent_logical;
2805 u64 extent_physical;
2806 u64 extent_len;
2807 struct btrfs_device *extent_dev;
2808 struct scrub_parity *sparity;
2809 int nsectors;
2810 int bitmap_len;
2811 int extent_mirror_num;
2812 int stop_loop = 0;
2813
2814 nsectors = map->stripe_len / root->sectorsize;
2815 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2816 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2817 GFP_NOFS);
2818 if (!sparity) {
2819 spin_lock(&sctx->stat_lock);
2820 sctx->stat.malloc_errors++;
2821 spin_unlock(&sctx->stat_lock);
2822 return -ENOMEM;
2823 }
2824
2825 sparity->stripe_len = map->stripe_len;
2826 sparity->nsectors = nsectors;
2827 sparity->sctx = sctx;
2828 sparity->scrub_dev = sdev;
2829 sparity->logic_start = logic_start;
2830 sparity->logic_end = logic_end;
2831 atomic_set(&sparity->ref_count, 1);
2832 INIT_LIST_HEAD(&sparity->spages);
2833 sparity->dbitmap = sparity->bitmap;
2834 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2835
2836 ret = 0;
2837 while (logic_start < logic_end) {
2838 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2839 key.type = BTRFS_METADATA_ITEM_KEY;
2840 else
2841 key.type = BTRFS_EXTENT_ITEM_KEY;
2842 key.objectid = logic_start;
2843 key.offset = (u64)-1;
2844
2845 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2846 if (ret < 0)
2847 goto out;
2848
2849 if (ret > 0) {
2850 ret = btrfs_previous_extent_item(root, path, 0);
2851 if (ret < 0)
2852 goto out;
2853 if (ret > 0) {
2854 btrfs_release_path(path);
2855 ret = btrfs_search_slot(NULL, root, &key,
2856 path, 0, 0);
2857 if (ret < 0)
2858 goto out;
2859 }
2860 }
2861
2862 stop_loop = 0;
2863 while (1) {
2864 u64 bytes;
2865
2866 l = path->nodes[0];
2867 slot = path->slots[0];
2868 if (slot >= btrfs_header_nritems(l)) {
2869 ret = btrfs_next_leaf(root, path);
2870 if (ret == 0)
2871 continue;
2872 if (ret < 0)
2873 goto out;
2874
2875 stop_loop = 1;
2876 break;
2877 }
2878 btrfs_item_key_to_cpu(l, &key, slot);
2879
2880 if (key.type == BTRFS_METADATA_ITEM_KEY)
2881 bytes = root->nodesize;
2882 else
2883 bytes = key.offset;
2884
2885 if (key.objectid + bytes <= logic_start)
2886 goto next;
2887
2888 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2889 key.type != BTRFS_METADATA_ITEM_KEY)
2890 goto next;
2891
2892 if (key.objectid > logic_end) {
2893 stop_loop = 1;
2894 break;
2895 }
2896
2897 while (key.objectid >= logic_start + map->stripe_len)
2898 logic_start += map->stripe_len;
2899
2900 extent = btrfs_item_ptr(l, slot,
2901 struct btrfs_extent_item);
2902 flags = btrfs_extent_flags(l, extent);
2903 generation = btrfs_extent_generation(l, extent);
2904
2905 if (key.objectid < logic_start &&
2906 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2907 btrfs_err(fs_info,
2908 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2909 key.objectid, logic_start);
2910 goto next;
2911 }
2912again:
2913 extent_logical = key.objectid;
2914 extent_len = bytes;
2915
2916 if (extent_logical < logic_start) {
2917 extent_len -= logic_start - extent_logical;
2918 extent_logical = logic_start;
2919 }
2920
2921 if (extent_logical + extent_len >
2922 logic_start + map->stripe_len)
2923 extent_len = logic_start + map->stripe_len -
2924 extent_logical;
2925
2926 scrub_parity_mark_sectors_data(sparity, extent_logical,
2927 extent_len);
2928
2929 scrub_remap_extent(fs_info, extent_logical,
2930 extent_len, &extent_physical,
2931 &extent_dev,
2932 &extent_mirror_num);
2933
2934 ret = btrfs_lookup_csums_range(csum_root,
2935 extent_logical,
2936 extent_logical + extent_len - 1,
2937 &sctx->csum_list, 1);
2938 if (ret)
2939 goto out;
2940
2941 ret = scrub_extent_for_parity(sparity, extent_logical,
2942 extent_len,
2943 extent_physical,
2944 extent_dev, flags,
2945 generation,
2946 extent_mirror_num);
2947 if (ret)
2948 goto out;
2949
2950 scrub_free_csums(sctx);
2951 if (extent_logical + extent_len <
2952 key.objectid + bytes) {
2953 logic_start += map->stripe_len;
2954
2955 if (logic_start >= logic_end) {
2956 stop_loop = 1;
2957 break;
2958 }
2959
2960 if (logic_start < key.objectid + bytes) {
2961 cond_resched();
2962 goto again;
2963 }
2964 }
2965next:
2966 path->slots[0]++;
2967 }
2968
2969 btrfs_release_path(path);
2970
2971 if (stop_loop)
2972 break;
2973
2974 logic_start += map->stripe_len;
2975 }
2976out:
2977 if (ret < 0)
2978 scrub_parity_mark_sectors_error(sparity, logic_start,
2979 logic_end - logic_start + 1);
2980 scrub_parity_put(sparity);
2981 scrub_submit(sctx);
2982 mutex_lock(&sctx->wr_ctx.wr_lock);
2983 scrub_wr_submit(sctx);
2984 mutex_unlock(&sctx->wr_ctx.wr_lock);
2985
2986 btrfs_release_path(path);
2987 return ret < 0 ? ret : 0;
2988}
2989
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002990static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002991 struct map_lookup *map,
2992 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002993 int num, u64 base, u64 length,
2994 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002995{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002996 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002997 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002998 struct btrfs_root *root = fs_info->extent_root;
2999 struct btrfs_root *csum_root = fs_info->csum_root;
3000 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003001 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003002 u64 flags;
3003 int ret;
3004 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003005 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003006 struct extent_buffer *l;
3007 struct btrfs_key key;
3008 u64 physical;
3009 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003010 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003011 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003012 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003013 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003014 struct reada_control *reada1;
3015 struct reada_control *reada2;
3016 struct btrfs_key key_start;
3017 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003018 u64 increment = map->stripe_len;
3019 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003020 u64 extent_logical;
3021 u64 extent_physical;
3022 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003023 u64 stripe_logical;
3024 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003025 struct btrfs_device *extent_dev;
3026 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003027 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003028
Arne Jansena2de7332011-03-08 14:14:00 +01003029 nstripes = length;
Wang Shilong3b080b22014-04-01 18:01:43 +08003030 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003031 offset = 0;
3032 do_div(nstripes, map->stripe_len);
3033 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3034 offset = map->stripe_len * num;
3035 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003036 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003037 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3038 int factor = map->num_stripes / map->sub_stripes;
3039 offset = map->stripe_len * (num / map->sub_stripes);
3040 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003041 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003042 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3043 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003044 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003045 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3046 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003047 mirror_num = num % map->num_stripes + 1;
Wang Shilong3b080b22014-04-01 18:01:43 +08003048 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3049 BTRFS_BLOCK_GROUP_RAID6)) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003050 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003051 increment = map->stripe_len * nr_data_stripes(map);
3052 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003053 } else {
3054 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003055 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003056 }
3057
3058 path = btrfs_alloc_path();
3059 if (!path)
3060 return -ENOMEM;
3061
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003062 ppath = btrfs_alloc_path();
3063 if (!ppath) {
3064 btrfs_free_path(ppath);
3065 return -ENOMEM;
3066 }
3067
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003068 /*
3069 * work on commit root. The related disk blocks are static as
3070 * long as COW is applied. This means, it is save to rewrite
3071 * them to repair disk errors without any race conditions
3072 */
Arne Jansena2de7332011-03-08 14:14:00 +01003073 path->search_commit_root = 1;
3074 path->skip_locking = 1;
3075
3076 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003077 * trigger the readahead for extent tree csum tree and wait for
3078 * completion. During readahead, the scrub is officially paused
3079 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003080 */
3081 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003082 physical_end = physical + nstripes * map->stripe_len;
3083 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3084 BTRFS_BLOCK_GROUP_RAID6)) {
3085 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003086 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003087 logic_end += base;
3088 } else {
3089 logic_end = logical + increment * nstripes;
3090 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003091 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003092 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003093 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003094
Arne Jansen7a262852011-06-10 12:39:23 +02003095 /* FIXME it might be better to start readahead at commit root */
3096 key_start.objectid = logical;
3097 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3098 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003099 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003100 key_end.type = BTRFS_METADATA_ITEM_KEY;
3101 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003102 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003103
Arne Jansen7a262852011-06-10 12:39:23 +02003104 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3105 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3106 key_start.offset = logical;
3107 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3108 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003109 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003110 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003111
Arne Jansen7a262852011-06-10 12:39:23 +02003112 if (!IS_ERR(reada1))
3113 btrfs_reada_wait(reada1);
3114 if (!IS_ERR(reada2))
3115 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003116
Arne Jansena2de7332011-03-08 14:14:00 +01003117
3118 /*
3119 * collect all data csums for the stripe to avoid seeking during
3120 * the scrub. This might currently (crc32) end up to be about 1MB
3121 */
Arne Jansene7786c32011-05-28 20:58:38 +00003122 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003123
Arne Jansena2de7332011-03-08 14:14:00 +01003124 /*
3125 * now find all extents for each stripe and scrub them
3126 */
Arne Jansena2de7332011-03-08 14:14:00 +01003127 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003128 while (physical < physical_end) {
3129 /* for raid56, we skip parity stripe */
3130 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3131 BTRFS_BLOCK_GROUP_RAID6)) {
3132 ret = get_raid56_logic_offset(physical, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003133 map, &logical, &stripe_logical);
Wang Shilong3b080b22014-04-01 18:01:43 +08003134 logical += base;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003135 if (ret) {
3136 stripe_logical += base;
3137 stripe_end = stripe_logical + increment - 1;
3138 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3139 ppath, stripe_logical,
3140 stripe_end);
3141 if (ret)
3142 goto out;
Wang Shilong3b080b22014-04-01 18:01:43 +08003143 goto skip;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003144 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003145 }
Arne Jansena2de7332011-03-08 14:14:00 +01003146 /*
3147 * canceled?
3148 */
3149 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003150 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003151 ret = -ECANCELED;
3152 goto out;
3153 }
3154 /*
3155 * check to see if we have to pause
3156 */
3157 if (atomic_read(&fs_info->scrub_pause_req)) {
3158 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003159 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003160 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003161 mutex_lock(&sctx->wr_ctx.wr_lock);
3162 scrub_wr_submit(sctx);
3163 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003164 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003165 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003166 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003167 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003168 }
3169
Wang Shilong7c76edb2014-01-12 21:38:32 +08003170 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3171 key.type = BTRFS_METADATA_ITEM_KEY;
3172 else
3173 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003174 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003175 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003176
3177 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3178 if (ret < 0)
3179 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003180
Arne Jansen8c510322011-06-03 10:09:26 +02003181 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003182 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003183 if (ret < 0)
3184 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003185 if (ret > 0) {
3186 /* there's no smaller item, so stick with the
3187 * larger one */
3188 btrfs_release_path(path);
3189 ret = btrfs_search_slot(NULL, root, &key,
3190 path, 0, 0);
3191 if (ret < 0)
3192 goto out;
3193 }
Arne Jansena2de7332011-03-08 14:14:00 +01003194 }
3195
Liu Bo625f1c8d2013-04-27 02:56:57 +00003196 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003197 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003198 u64 bytes;
3199
Arne Jansena2de7332011-03-08 14:14:00 +01003200 l = path->nodes[0];
3201 slot = path->slots[0];
3202 if (slot >= btrfs_header_nritems(l)) {
3203 ret = btrfs_next_leaf(root, path);
3204 if (ret == 0)
3205 continue;
3206 if (ret < 0)
3207 goto out;
3208
Liu Bo625f1c8d2013-04-27 02:56:57 +00003209 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003210 break;
3211 }
3212 btrfs_item_key_to_cpu(l, &key, slot);
3213
Josef Bacik3173a182013-03-07 14:22:04 -05003214 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003215 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003216 else
3217 bytes = key.offset;
3218
3219 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003220 goto next;
3221
Liu Bo625f1c8d2013-04-27 02:56:57 +00003222 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3223 key.type != BTRFS_METADATA_ITEM_KEY)
3224 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01003225
Liu Bo625f1c8d2013-04-27 02:56:57 +00003226 if (key.objectid >= logical + map->stripe_len) {
3227 /* out of this device extent */
3228 if (key.objectid >= logic_end)
3229 stop_loop = 1;
3230 break;
3231 }
Arne Jansena2de7332011-03-08 14:14:00 +01003232
3233 extent = btrfs_item_ptr(l, slot,
3234 struct btrfs_extent_item);
3235 flags = btrfs_extent_flags(l, extent);
3236 generation = btrfs_extent_generation(l, extent);
3237
3238 if (key.objectid < logical &&
3239 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003240 btrfs_err(fs_info,
3241 "scrub: tree block %llu spanning "
3242 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003243 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01003244 goto next;
3245 }
3246
Liu Bo625f1c8d2013-04-27 02:56:57 +00003247again:
3248 extent_logical = key.objectid;
3249 extent_len = bytes;
3250
Arne Jansena2de7332011-03-08 14:14:00 +01003251 /*
3252 * trim extent to this stripe
3253 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003254 if (extent_logical < logical) {
3255 extent_len -= logical - extent_logical;
3256 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003257 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003258 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003259 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003260 extent_len = logical + map->stripe_len -
3261 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003262 }
3263
Liu Bo625f1c8d2013-04-27 02:56:57 +00003264 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003265 extent_dev = scrub_dev;
3266 extent_mirror_num = mirror_num;
3267 if (is_dev_replace)
3268 scrub_remap_extent(fs_info, extent_logical,
3269 extent_len, &extent_physical,
3270 &extent_dev,
3271 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003272
3273 ret = btrfs_lookup_csums_range(csum_root, logical,
3274 logical + map->stripe_len - 1,
3275 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003276 if (ret)
3277 goto out;
3278
Liu Bo625f1c8d2013-04-27 02:56:57 +00003279 ret = scrub_extent(sctx, extent_logical, extent_len,
3280 extent_physical, extent_dev, flags,
3281 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003282 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003283 if (ret)
3284 goto out;
3285
Josef Bacikd88d46c2013-06-10 12:59:04 +00003286 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003287 if (extent_logical + extent_len <
3288 key.objectid + bytes) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003289 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
3290 BTRFS_BLOCK_GROUP_RAID6)) {
3291 /*
3292 * loop until we find next data stripe
3293 * or we have finished all stripes.
3294 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003295loop:
3296 physical += map->stripe_len;
3297 ret = get_raid56_logic_offset(physical,
3298 num, map, &logical,
3299 &stripe_logical);
3300 logical += base;
3301
3302 if (ret && physical < physical_end) {
3303 stripe_logical += base;
3304 stripe_end = stripe_logical +
3305 increment - 1;
3306 ret = scrub_raid56_parity(sctx,
3307 map, scrub_dev, ppath,
3308 stripe_logical,
3309 stripe_end);
3310 if (ret)
3311 goto out;
3312 goto loop;
3313 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003314 } else {
3315 physical += map->stripe_len;
3316 logical += increment;
3317 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003318 if (logical < key.objectid + bytes) {
3319 cond_resched();
3320 goto again;
3321 }
3322
Wang Shilong3b080b22014-04-01 18:01:43 +08003323 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003324 stop_loop = 1;
3325 break;
3326 }
3327 }
Arne Jansena2de7332011-03-08 14:14:00 +01003328next:
3329 path->slots[0]++;
3330 }
Chris Mason71267332011-05-23 06:30:52 -04003331 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003332skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003333 logical += increment;
3334 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003335 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003336 if (stop_loop)
3337 sctx->stat.last_physical = map->stripes[num].physical +
3338 length;
3339 else
3340 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003341 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003342 if (stop_loop)
3343 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003344 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003345out:
Arne Jansena2de7332011-03-08 14:14:00 +01003346 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003347 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003348 mutex_lock(&sctx->wr_ctx.wr_lock);
3349 scrub_wr_submit(sctx);
3350 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003351
Arne Jansene7786c32011-05-28 20:58:38 +00003352 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003353 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003354 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003355 return ret < 0 ? ret : 0;
3356}
3357
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003358static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003359 struct btrfs_device *scrub_dev,
3360 u64 chunk_tree, u64 chunk_objectid,
3361 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003362 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003363{
3364 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003365 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003366 struct map_lookup *map;
3367 struct extent_map *em;
3368 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003369 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003370
3371 read_lock(&map_tree->map_tree.lock);
3372 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3373 read_unlock(&map_tree->map_tree.lock);
3374
3375 if (!em)
3376 return -EINVAL;
3377
3378 map = (struct map_lookup *)em->bdev;
3379 if (em->start != chunk_offset)
3380 goto out;
3381
3382 if (em->len < length)
3383 goto out;
3384
3385 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003386 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003387 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003388 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003389 chunk_offset, length,
3390 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003391 if (ret)
3392 goto out;
3393 }
3394 }
3395out:
3396 free_extent_map(em);
3397
3398 return ret;
3399}
3400
3401static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003402int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003403 struct btrfs_device *scrub_dev, u64 start, u64 end,
3404 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003405{
3406 struct btrfs_dev_extent *dev_extent = NULL;
3407 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003408 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003409 struct btrfs_fs_info *fs_info = root->fs_info;
3410 u64 length;
3411 u64 chunk_tree;
3412 u64 chunk_objectid;
3413 u64 chunk_offset;
3414 int ret;
3415 int slot;
3416 struct extent_buffer *l;
3417 struct btrfs_key key;
3418 struct btrfs_key found_key;
3419 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003420 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003421
3422 path = btrfs_alloc_path();
3423 if (!path)
3424 return -ENOMEM;
3425
3426 path->reada = 2;
3427 path->search_commit_root = 1;
3428 path->skip_locking = 1;
3429
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003430 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003431 key.offset = 0ull;
3432 key.type = BTRFS_DEV_EXTENT_KEY;
3433
Arne Jansena2de7332011-03-08 14:14:00 +01003434 while (1) {
3435 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3436 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003437 break;
3438 if (ret > 0) {
3439 if (path->slots[0] >=
3440 btrfs_header_nritems(path->nodes[0])) {
3441 ret = btrfs_next_leaf(root, path);
3442 if (ret)
3443 break;
3444 }
3445 }
Arne Jansena2de7332011-03-08 14:14:00 +01003446
3447 l = path->nodes[0];
3448 slot = path->slots[0];
3449
3450 btrfs_item_key_to_cpu(l, &found_key, slot);
3451
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003452 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003453 break;
3454
David Sterba962a2982014-06-04 18:41:45 +02003455 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003456 break;
3457
3458 if (found_key.offset >= end)
3459 break;
3460
3461 if (found_key.offset < key.offset)
3462 break;
3463
3464 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3465 length = btrfs_dev_extent_length(l, dev_extent);
3466
Qu Wenruoced96ed2014-06-19 10:42:51 +08003467 if (found_key.offset + length <= start)
3468 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003469
3470 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3471 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3472 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3473
3474 /*
3475 * get a reference on the corresponding block group to prevent
3476 * the chunk from going away while we scrub it
3477 */
3478 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003479
3480 /* some chunks are removed but not committed to disk yet,
3481 * continue scrubbing */
3482 if (!cache)
3483 goto skip;
3484
Stefan Behrensff023aa2012-11-06 11:43:11 +01003485 dev_replace->cursor_right = found_key.offset + length;
3486 dev_replace->cursor_left = found_key.offset;
3487 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003488 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003489 chunk_offset, length, found_key.offset,
3490 is_dev_replace);
3491
3492 /*
3493 * flush, submit all pending read and write bios, afterwards
3494 * wait for them.
3495 * Note that in the dev replace case, a read request causes
3496 * write requests that are submitted in the read completion
3497 * worker. Therefore in the current situation, it is required
3498 * that all write requests are flushed, so that all read and
3499 * write requests are really completed when bios_in_flight
3500 * changes to 0.
3501 */
3502 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3503 scrub_submit(sctx);
3504 mutex_lock(&sctx->wr_ctx.wr_lock);
3505 scrub_wr_submit(sctx);
3506 mutex_unlock(&sctx->wr_ctx.wr_lock);
3507
3508 wait_event(sctx->list_wait,
3509 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003510 atomic_inc(&fs_info->scrubs_paused);
3511 wake_up(&fs_info->scrub_pause_wait);
3512
3513 /*
3514 * must be called before we decrease @scrub_paused.
3515 * make sure we don't block transaction commit while
3516 * we are waiting pending workers finished.
3517 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003518 wait_event(sctx->list_wait,
3519 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003520 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3521
3522 mutex_lock(&fs_info->scrub_lock);
3523 __scrub_blocked_if_needed(fs_info);
3524 atomic_dec(&fs_info->scrubs_paused);
3525 mutex_unlock(&fs_info->scrub_lock);
3526 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003527
Arne Jansena2de7332011-03-08 14:14:00 +01003528 btrfs_put_block_group(cache);
3529 if (ret)
3530 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003531 if (is_dev_replace &&
3532 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003533 ret = -EIO;
3534 break;
3535 }
3536 if (sctx->stat.malloc_errors > 0) {
3537 ret = -ENOMEM;
3538 break;
3539 }
Arne Jansena2de7332011-03-08 14:14:00 +01003540
Ilya Dryomov539f3582013-10-07 13:42:57 +03003541 dev_replace->cursor_left = dev_replace->cursor_right;
3542 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003543skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003544 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003545 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003546 }
3547
Arne Jansena2de7332011-03-08 14:14:00 +01003548 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003549
3550 /*
3551 * ret can still be 1 from search_slot or next_leaf,
3552 * that's not an error
3553 */
3554 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003555}
3556
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003557static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3558 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003559{
3560 int i;
3561 u64 bytenr;
3562 u64 gen;
3563 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003564 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003565
Miao Xie87533c42013-01-29 10:14:48 +00003566 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003567 return -EIO;
3568
Miao Xie5f546062014-07-24 11:37:09 +08003569 /* Seed devices of a new filesystem has their own generation. */
3570 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3571 gen = scrub_dev->generation;
3572 else
3573 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003574
3575 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3576 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003577 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3578 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003579 break;
3580
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003581 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003582 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003583 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003584 if (ret)
3585 return ret;
3586 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003587 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003588
3589 return 0;
3590}
3591
3592/*
3593 * get a reference count on fs_info->scrub_workers. start worker if necessary
3594 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003595static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3596 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003597{
Josef Bacik0dc3b842011-11-18 14:37:27 -05003598 int ret = 0;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003599 int flags = WQ_FREEZABLE | WQ_UNBOUND;
3600 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003601
Arne Jansen632dd772011-06-10 12:07:07 +02003602 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003603 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003604 fs_info->scrub_workers =
3605 btrfs_alloc_workqueue("btrfs-scrub", flags,
3606 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003607 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003608 fs_info->scrub_workers =
3609 btrfs_alloc_workqueue("btrfs-scrub", flags,
3610 max_active, 4);
3611 if (!fs_info->scrub_workers) {
3612 ret = -ENOMEM;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003613 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003614 }
3615 fs_info->scrub_wr_completion_workers =
3616 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3617 max_active, 2);
3618 if (!fs_info->scrub_wr_completion_workers) {
3619 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003620 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003621 }
3622 fs_info->scrub_nocow_workers =
3623 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
3624 if (!fs_info->scrub_nocow_workers) {
3625 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003626 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003627 }
Arne Jansen632dd772011-06-10 12:07:07 +02003628 }
Arne Jansena2de7332011-03-08 14:14:00 +01003629 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003630out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05003631 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003632}
3633
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003634static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003635{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003636 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003637 btrfs_destroy_workqueue(fs_info->scrub_workers);
3638 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3639 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003640 }
Arne Jansena2de7332011-03-08 14:14:00 +01003641 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003642}
3643
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003644int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3645 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003646 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003647{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003648 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003649 int ret;
3650 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003651 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003652
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003653 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003654 return -EINVAL;
3655
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003656 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003657 /*
3658 * in this case scrub is unable to calculate the checksum
3659 * the way scrub is implemented. Do not handle this
3660 * situation at all because it won't ever happen.
3661 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003662 btrfs_err(fs_info,
3663 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003664 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003665 return -EINVAL;
3666 }
3667
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003668 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003669 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003670 btrfs_err(fs_info,
3671 "scrub: size assumption sectorsize != PAGE_SIZE "
3672 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003673 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003674 return -EINVAL;
3675 }
3676
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003677 if (fs_info->chunk_root->nodesize >
3678 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3679 fs_info->chunk_root->sectorsize >
3680 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3681 /*
3682 * would exhaust the array bounds of pagev member in
3683 * struct scrub_block
3684 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003685 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3686 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003687 fs_info->chunk_root->nodesize,
3688 SCRUB_MAX_PAGES_PER_BLOCK,
3689 fs_info->chunk_root->sectorsize,
3690 SCRUB_MAX_PAGES_PER_BLOCK);
3691 return -EINVAL;
3692 }
3693
Arne Jansena2de7332011-03-08 14:14:00 +01003694
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003695 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3696 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003697 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003698 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003699 return -ENODEV;
3700 }
Arne Jansena2de7332011-03-08 14:14:00 +01003701
Miao Xie5d68da32014-07-24 11:37:07 +08003702 if (!is_dev_replace && !readonly && !dev->writeable) {
3703 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3704 rcu_read_lock();
3705 name = rcu_dereference(dev->name);
3706 btrfs_err(fs_info, "scrub: device %s is not writable",
3707 name->str);
3708 rcu_read_unlock();
3709 return -EROFS;
3710 }
3711
Wang Shilong3b7a0162013-10-12 02:11:12 +08003712 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003713 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003714 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003715 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003716 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003717 }
3718
Stefan Behrens8dabb742012-11-06 13:15:27 +01003719 btrfs_dev_replace_lock(&fs_info->dev_replace);
3720 if (dev->scrub_device ||
3721 (!is_dev_replace &&
3722 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3723 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003724 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003725 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003726 return -EINPROGRESS;
3727 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003728 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003729
3730 ret = scrub_workers_get(fs_info, is_dev_replace);
3731 if (ret) {
3732 mutex_unlock(&fs_info->scrub_lock);
3733 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3734 return ret;
3735 }
3736
Stefan Behrens63a212a2012-11-05 18:29:28 +01003737 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003738 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003739 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003740 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3741 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003742 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003743 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003744 sctx->readonly = readonly;
3745 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003746 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003747
Wang Shilong3cb09292013-12-04 21:15:19 +08003748 /*
3749 * checking @scrub_pause_req here, we can avoid
3750 * race between committing transaction and scrubbing.
3751 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003752 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003753 atomic_inc(&fs_info->scrubs_running);
3754 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003755
Stefan Behrensff023aa2012-11-06 11:43:11 +01003756 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003757 /*
3758 * by holding device list mutex, we can
3759 * kick off writing super in log tree sync.
3760 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003761 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003762 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003763 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003764 }
Arne Jansena2de7332011-03-08 14:14:00 +01003765
3766 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003767 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3768 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003769
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003770 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003771 atomic_dec(&fs_info->scrubs_running);
3772 wake_up(&fs_info->scrub_pause_wait);
3773
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003774 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003775
Arne Jansena2de7332011-03-08 14:14:00 +01003776 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003777 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003778
3779 mutex_lock(&fs_info->scrub_lock);
3780 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003781 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003782 mutex_unlock(&fs_info->scrub_lock);
3783
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003784 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003785
3786 return ret;
3787}
3788
Jeff Mahoney143bede2012-03-01 14:56:26 +01003789void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003790{
3791 struct btrfs_fs_info *fs_info = root->fs_info;
3792
3793 mutex_lock(&fs_info->scrub_lock);
3794 atomic_inc(&fs_info->scrub_pause_req);
3795 while (atomic_read(&fs_info->scrubs_paused) !=
3796 atomic_read(&fs_info->scrubs_running)) {
3797 mutex_unlock(&fs_info->scrub_lock);
3798 wait_event(fs_info->scrub_pause_wait,
3799 atomic_read(&fs_info->scrubs_paused) ==
3800 atomic_read(&fs_info->scrubs_running));
3801 mutex_lock(&fs_info->scrub_lock);
3802 }
3803 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003804}
3805
Jeff Mahoney143bede2012-03-01 14:56:26 +01003806void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003807{
3808 struct btrfs_fs_info *fs_info = root->fs_info;
3809
3810 atomic_dec(&fs_info->scrub_pause_req);
3811 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003812}
3813
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003814int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003815{
Arne Jansena2de7332011-03-08 14:14:00 +01003816 mutex_lock(&fs_info->scrub_lock);
3817 if (!atomic_read(&fs_info->scrubs_running)) {
3818 mutex_unlock(&fs_info->scrub_lock);
3819 return -ENOTCONN;
3820 }
3821
3822 atomic_inc(&fs_info->scrub_cancel_req);
3823 while (atomic_read(&fs_info->scrubs_running)) {
3824 mutex_unlock(&fs_info->scrub_lock);
3825 wait_event(fs_info->scrub_pause_wait,
3826 atomic_read(&fs_info->scrubs_running) == 0);
3827 mutex_lock(&fs_info->scrub_lock);
3828 }
3829 atomic_dec(&fs_info->scrub_cancel_req);
3830 mutex_unlock(&fs_info->scrub_lock);
3831
3832 return 0;
3833}
3834
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003835int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3836 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003837{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003838 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003839
3840 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003841 sctx = dev->scrub_device;
3842 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003843 mutex_unlock(&fs_info->scrub_lock);
3844 return -ENOTCONN;
3845 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003846 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003847 while (dev->scrub_device) {
3848 mutex_unlock(&fs_info->scrub_lock);
3849 wait_event(fs_info->scrub_pause_wait,
3850 dev->scrub_device == NULL);
3851 mutex_lock(&fs_info->scrub_lock);
3852 }
3853 mutex_unlock(&fs_info->scrub_lock);
3854
3855 return 0;
3856}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003857
Arne Jansena2de7332011-03-08 14:14:00 +01003858int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3859 struct btrfs_scrub_progress *progress)
3860{
3861 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003862 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003863
3864 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003865 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003866 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003867 sctx = dev->scrub_device;
3868 if (sctx)
3869 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003870 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3871
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003872 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003873}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003874
3875static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3876 u64 extent_logical, u64 extent_len,
3877 u64 *extent_physical,
3878 struct btrfs_device **extent_dev,
3879 int *extent_mirror_num)
3880{
3881 u64 mapped_length;
3882 struct btrfs_bio *bbio = NULL;
3883 int ret;
3884
3885 mapped_length = extent_len;
3886 ret = btrfs_map_block(fs_info, READ, extent_logical,
3887 &mapped_length, &bbio, 0);
3888 if (ret || !bbio || mapped_length < extent_len ||
3889 !bbio->stripes[0].dev->bdev) {
3890 kfree(bbio);
3891 return;
3892 }
3893
3894 *extent_physical = bbio->stripes[0].physical;
3895 *extent_mirror_num = bbio->mirror_num;
3896 *extent_dev = bbio->stripes[0].dev;
3897 kfree(bbio);
3898}
3899
3900static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3901 struct scrub_wr_ctx *wr_ctx,
3902 struct btrfs_fs_info *fs_info,
3903 struct btrfs_device *dev,
3904 int is_dev_replace)
3905{
3906 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3907
3908 mutex_init(&wr_ctx->wr_lock);
3909 wr_ctx->wr_curr_bio = NULL;
3910 if (!is_dev_replace)
3911 return 0;
3912
3913 WARN_ON(!dev->bdev);
3914 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3915 bio_get_nr_vecs(dev->bdev));
3916 wr_ctx->tgtdev = dev;
3917 atomic_set(&wr_ctx->flush_all_writes, 0);
3918 return 0;
3919}
3920
3921static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3922{
3923 mutex_lock(&wr_ctx->wr_lock);
3924 kfree(wr_ctx->wr_curr_bio);
3925 wr_ctx->wr_curr_bio = NULL;
3926 mutex_unlock(&wr_ctx->wr_lock);
3927}
3928
3929static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3930 int mirror_num, u64 physical_for_dev_replace)
3931{
3932 struct scrub_copy_nocow_ctx *nocow_ctx;
3933 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3934
3935 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3936 if (!nocow_ctx) {
3937 spin_lock(&sctx->stat_lock);
3938 sctx->stat.malloc_errors++;
3939 spin_unlock(&sctx->stat_lock);
3940 return -ENOMEM;
3941 }
3942
3943 scrub_pending_trans_workers_inc(sctx);
3944
3945 nocow_ctx->sctx = sctx;
3946 nocow_ctx->logical = logical;
3947 nocow_ctx->len = len;
3948 nocow_ctx->mirror_num = mirror_num;
3949 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08003950 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3951 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003952 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003953 btrfs_queue_work(fs_info->scrub_nocow_workers,
3954 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003955
3956 return 0;
3957}
3958
Josef Bacik652f25a2013-09-12 16:58:28 -04003959static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3960{
3961 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3962 struct scrub_nocow_inode *nocow_inode;
3963
3964 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3965 if (!nocow_inode)
3966 return -ENOMEM;
3967 nocow_inode->inum = inum;
3968 nocow_inode->offset = offset;
3969 nocow_inode->root = root;
3970 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3971 return 0;
3972}
3973
3974#define COPY_COMPLETE 1
3975
Stefan Behrensff023aa2012-11-06 11:43:11 +01003976static void copy_nocow_pages_worker(struct btrfs_work *work)
3977{
3978 struct scrub_copy_nocow_ctx *nocow_ctx =
3979 container_of(work, struct scrub_copy_nocow_ctx, work);
3980 struct scrub_ctx *sctx = nocow_ctx->sctx;
3981 u64 logical = nocow_ctx->logical;
3982 u64 len = nocow_ctx->len;
3983 int mirror_num = nocow_ctx->mirror_num;
3984 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3985 int ret;
3986 struct btrfs_trans_handle *trans = NULL;
3987 struct btrfs_fs_info *fs_info;
3988 struct btrfs_path *path;
3989 struct btrfs_root *root;
3990 int not_written = 0;
3991
3992 fs_info = sctx->dev_root->fs_info;
3993 root = fs_info->extent_root;
3994
3995 path = btrfs_alloc_path();
3996 if (!path) {
3997 spin_lock(&sctx->stat_lock);
3998 sctx->stat.malloc_errors++;
3999 spin_unlock(&sctx->stat_lock);
4000 not_written = 1;
4001 goto out;
4002 }
4003
4004 trans = btrfs_join_transaction(root);
4005 if (IS_ERR(trans)) {
4006 not_written = 1;
4007 goto out;
4008 }
4009
4010 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04004011 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004012 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004013 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
4014 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02004015 logical, physical_for_dev_replace, len, mirror_num,
4016 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004017 not_written = 1;
4018 goto out;
4019 }
4020
Josef Bacik652f25a2013-09-12 16:58:28 -04004021 btrfs_end_transaction(trans, root);
4022 trans = NULL;
4023 while (!list_empty(&nocow_ctx->inodes)) {
4024 struct scrub_nocow_inode *entry;
4025 entry = list_first_entry(&nocow_ctx->inodes,
4026 struct scrub_nocow_inode,
4027 list);
4028 list_del_init(&entry->list);
4029 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4030 entry->root, nocow_ctx);
4031 kfree(entry);
4032 if (ret == COPY_COMPLETE) {
4033 ret = 0;
4034 break;
4035 } else if (ret) {
4036 break;
4037 }
4038 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004039out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004040 while (!list_empty(&nocow_ctx->inodes)) {
4041 struct scrub_nocow_inode *entry;
4042 entry = list_first_entry(&nocow_ctx->inodes,
4043 struct scrub_nocow_inode,
4044 list);
4045 list_del_init(&entry->list);
4046 kfree(entry);
4047 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004048 if (trans && !IS_ERR(trans))
4049 btrfs_end_transaction(trans, root);
4050 if (not_written)
4051 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4052 num_uncorrectable_read_errors);
4053
4054 btrfs_free_path(path);
4055 kfree(nocow_ctx);
4056
4057 scrub_pending_trans_workers_dec(sctx);
4058}
4059
Gui Hecheng32159242014-11-10 15:36:08 +08004060static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4061 u64 logical)
4062{
4063 struct extent_state *cached_state = NULL;
4064 struct btrfs_ordered_extent *ordered;
4065 struct extent_io_tree *io_tree;
4066 struct extent_map *em;
4067 u64 lockstart = start, lockend = start + len - 1;
4068 int ret = 0;
4069
4070 io_tree = &BTRFS_I(inode)->io_tree;
4071
4072 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4073 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4074 if (ordered) {
4075 btrfs_put_ordered_extent(ordered);
4076 ret = 1;
4077 goto out_unlock;
4078 }
4079
4080 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4081 if (IS_ERR(em)) {
4082 ret = PTR_ERR(em);
4083 goto out_unlock;
4084 }
4085
4086 /*
4087 * This extent does not actually cover the logical extent anymore,
4088 * move on to the next inode.
4089 */
4090 if (em->block_start > logical ||
4091 em->block_start + em->block_len < logical + len) {
4092 free_extent_map(em);
4093 ret = 1;
4094 goto out_unlock;
4095 }
4096 free_extent_map(em);
4097
4098out_unlock:
4099 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4100 GFP_NOFS);
4101 return ret;
4102}
4103
Josef Bacik652f25a2013-09-12 16:58:28 -04004104static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4105 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004106{
Miao Xie826aa0a2013-06-27 18:50:59 +08004107 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004108 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004109 struct inode *inode;
4110 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004111 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004112 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004113 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004114 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004115 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004116 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004117 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004118 int ret = 0;
4119 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004120
4121 key.objectid = root;
4122 key.type = BTRFS_ROOT_ITEM_KEY;
4123 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004124
4125 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4126
Stefan Behrensff023aa2012-11-06 11:43:11 +01004127 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004128 if (IS_ERR(local_root)) {
4129 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004130 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004131 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004132
4133 key.type = BTRFS_INODE_ITEM_KEY;
4134 key.objectid = inum;
4135 key.offset = 0;
4136 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004137 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004138 if (IS_ERR(inode))
4139 return PTR_ERR(inode);
4140
Miao Xieedd14002013-06-27 18:51:00 +08004141 /* Avoid truncate/dio/punch hole.. */
4142 mutex_lock(&inode->i_mutex);
4143 inode_dio_wait(inode);
4144
Stefan Behrensff023aa2012-11-06 11:43:11 +01004145 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004146 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004147 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004148
Gui Hecheng32159242014-11-10 15:36:08 +08004149 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4150 if (ret) {
4151 ret = ret > 0 ? 0 : ret;
4152 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004153 }
4154
Stefan Behrensff023aa2012-11-06 11:43:11 +01004155 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004156 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004157again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004158 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4159 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004160 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004161 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004162 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004163 }
4164
4165 if (PageUptodate(page)) {
4166 if (PageDirty(page))
4167 goto next_page;
4168 } else {
4169 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004170 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004171 btrfs_get_extent,
4172 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004173 if (err) {
4174 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004175 goto next_page;
4176 }
Miao Xieedd14002013-06-27 18:51:00 +08004177
Miao Xie26b258912013-06-27 18:50:58 +08004178 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004179 /*
4180 * If the page has been remove from the page cache,
4181 * the data on it is meaningless, because it may be
4182 * old one, the new data may be written into the new
4183 * page in the page cache.
4184 */
4185 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004186 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004187 page_cache_release(page);
4188 goto again;
4189 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004190 if (!PageUptodate(page)) {
4191 ret = -EIO;
4192 goto next_page;
4193 }
4194 }
Gui Hecheng32159242014-11-10 15:36:08 +08004195
4196 ret = check_extent_to_block(inode, offset, len,
4197 nocow_ctx_logical);
4198 if (ret) {
4199 ret = ret > 0 ? 0 : ret;
4200 goto next_page;
4201 }
4202
Miao Xie826aa0a2013-06-27 18:50:59 +08004203 err = write_page_nocow(nocow_ctx->sctx,
4204 physical_for_dev_replace, page);
4205 if (err)
4206 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004207next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004208 unlock_page(page);
4209 page_cache_release(page);
4210
4211 if (ret)
4212 break;
4213
Stefan Behrensff023aa2012-11-06 11:43:11 +01004214 offset += PAGE_CACHE_SIZE;
4215 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004216 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004217 len -= PAGE_CACHE_SIZE;
4218 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004219 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004220out:
Miao Xieedd14002013-06-27 18:51:00 +08004221 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004222 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004223 return ret;
4224}
4225
4226static int write_page_nocow(struct scrub_ctx *sctx,
4227 u64 physical_for_dev_replace, struct page *page)
4228{
4229 struct bio *bio;
4230 struct btrfs_device *dev;
4231 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004232
4233 dev = sctx->wr_ctx.tgtdev;
4234 if (!dev)
4235 return -EIO;
4236 if (!dev->bdev) {
4237 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05004238 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004239 return -EIO;
4240 }
Chris Mason9be33952013-05-17 18:30:14 -04004241 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004242 if (!bio) {
4243 spin_lock(&sctx->stat_lock);
4244 sctx->stat.malloc_errors++;
4245 spin_unlock(&sctx->stat_lock);
4246 return -ENOMEM;
4247 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004248 bio->bi_iter.bi_size = 0;
4249 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004250 bio->bi_bdev = dev->bdev;
4251 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4252 if (ret != PAGE_CACHE_SIZE) {
4253leave_with_eio:
4254 bio_put(bio);
4255 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4256 return -EIO;
4257 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004258
Kent Overstreet33879d42013-11-23 22:33:32 -08004259 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004260 goto leave_with_eio;
4261
4262 bio_put(bio);
4263 return 0;
4264}