blob: 1ae527c9f8f4bf8f733313191f39368086964308 [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;
Miao Xieaf8e2d12014-10-23 14:42:50 +080069 u64 map_length;
70};
71
Arne Jansena2de7332011-03-08 14:14:00 +010072struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040073 struct scrub_block *sblock;
74 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020075 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080076 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010077 u64 flags; /* extent flags */
78 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040079 u64 logical;
80 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010081 u64 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +010082 atomic_t ref_count;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040083 struct {
84 unsigned int mirror_num:8;
85 unsigned int have_csum:1;
86 unsigned int io_error:1;
87 };
Arne Jansena2de7332011-03-08 14:14:00 +010088 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080089
90 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010091};
92
93struct scrub_bio {
94 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010095 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010096 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010097 struct bio *bio;
98 int err;
99 u64 logical;
100 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100101#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
102 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
103#else
104 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
105#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400106 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +0100107 int next_free;
108 struct btrfs_work work;
109};
110
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400111struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100112 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400113 int page_count;
114 atomic_t outstanding_pages;
115 atomic_t ref_count; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100116 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800117 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400118 struct {
119 unsigned int header_error:1;
120 unsigned int checksum_error:1;
121 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200122 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800123
124 /* The following is for the data used to check parity */
125 /* It is for the data with checksum */
126 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400127 };
128};
129
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800130/* Used for the chunks with parity stripe such RAID5/6 */
131struct scrub_parity {
132 struct scrub_ctx *sctx;
133
134 struct btrfs_device *scrub_dev;
135
136 u64 logic_start;
137
138 u64 logic_end;
139
140 int nsectors;
141
142 int stripe_len;
143
144 atomic_t ref_count;
145
146 struct list_head spages;
147
148 /* Work of parity check and repair */
149 struct btrfs_work work;
150
151 /* Mark the parity blocks which have data */
152 unsigned long *dbitmap;
153
154 /*
155 * Mark the parity blocks which have data, but errors happen when
156 * read data or check data
157 */
158 unsigned long *ebitmap;
159
160 unsigned long bitmap[0];
161};
162
Stefan Behrensff023aa2012-11-06 11:43:11 +0100163struct scrub_wr_ctx {
164 struct scrub_bio *wr_curr_bio;
165 struct btrfs_device *tgtdev;
166 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
167 atomic_t flush_all_writes;
168 struct mutex wr_lock;
169};
170
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100171struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100172 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100173 struct btrfs_root *dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +0100174 int first_free;
175 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100176 atomic_t bios_in_flight;
177 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100178 spinlock_t list_lock;
179 wait_queue_head_t list_wait;
180 u16 csum_size;
181 struct list_head csum_list;
182 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100183 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100184 int pages_per_rd_bio;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400185 u32 sectorsize;
186 u32 nodesize;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100187
188 int is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100189 struct scrub_wr_ctx wr_ctx;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100190
Arne Jansena2de7332011-03-08 14:14:00 +0100191 /*
192 * statistics
193 */
194 struct btrfs_scrub_progress stat;
195 spinlock_t stat_lock;
196};
197
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200198struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100199 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100200 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200201 u64 logical;
202 struct btrfs_root *root;
203 struct btrfs_work work;
204 int mirror_num;
205};
206
Josef Bacik652f25a2013-09-12 16:58:28 -0400207struct scrub_nocow_inode {
208 u64 inum;
209 u64 offset;
210 u64 root;
211 struct list_head list;
212};
213
Stefan Behrensff023aa2012-11-06 11:43:11 +0100214struct scrub_copy_nocow_ctx {
215 struct scrub_ctx *sctx;
216 u64 logical;
217 u64 len;
218 int mirror_num;
219 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -0400220 struct list_head inodes;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100221 struct btrfs_work work;
222};
223
Jan Schmidt558540c2011-06-13 19:59:12 +0200224struct scrub_warning {
225 struct btrfs_path *path;
226 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200227 const char *errstr;
228 sector_t sector;
229 u64 logical;
230 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200231};
232
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100233static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
234static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
235static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
236static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400237static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Zhao Leibe50a8d2015-01-20 15:11:42 +0800238static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100239 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100240static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
241 struct scrub_block *sblock, int is_metadata,
242 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800243 u16 csum_size, int retry_failed_mirror);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400244static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
245 struct scrub_block *sblock,
246 int is_metadata, int have_csum,
247 const u8 *csum, u64 generation,
248 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400249static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800250 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400251static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
252 struct scrub_block *sblock_good,
253 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100254static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
255static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
256 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400257static int scrub_checksum_data(struct scrub_block *sblock);
258static int scrub_checksum_tree_block(struct scrub_block *sblock);
259static int scrub_checksum_super(struct scrub_block *sblock);
260static void scrub_block_get(struct scrub_block *sblock);
261static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100262static void scrub_page_get(struct scrub_page *spage);
263static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800264static void scrub_parity_get(struct scrub_parity *sparity);
265static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100266static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
267 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100268static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100269 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100270 u64 gen, int mirror_num, u8 *csum, int force,
271 u64 physical_for_dev_replace);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400272static void scrub_bio_end_io(struct bio *bio, int err);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400273static void scrub_bio_end_io_worker(struct btrfs_work *work);
274static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100275static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
276 u64 extent_logical, u64 extent_len,
277 u64 *extent_physical,
278 struct btrfs_device **extent_dev,
279 int *extent_mirror_num);
280static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
281 struct scrub_wr_ctx *wr_ctx,
282 struct btrfs_fs_info *fs_info,
283 struct btrfs_device *dev,
284 int is_dev_replace);
285static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
286static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
287 struct scrub_page *spage);
288static void scrub_wr_submit(struct scrub_ctx *sctx);
289static void scrub_wr_bio_end_io(struct bio *bio, int err);
290static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
291static int write_page_nocow(struct scrub_ctx *sctx,
292 u64 physical_for_dev_replace, struct page *page);
293static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400294 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100295static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
296 int mirror_num, u64 physical_for_dev_replace);
297static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800298static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800299static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400300
301
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100302static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
303{
304 atomic_inc(&sctx->bios_in_flight);
305}
306
307static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
308{
309 atomic_dec(&sctx->bios_in_flight);
310 wake_up(&sctx->list_wait);
311}
312
Wang Shilongcb7ab022013-12-04 21:16:53 +0800313static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800314{
315 while (atomic_read(&fs_info->scrub_pause_req)) {
316 mutex_unlock(&fs_info->scrub_lock);
317 wait_event(fs_info->scrub_pause_wait,
318 atomic_read(&fs_info->scrub_pause_req) == 0);
319 mutex_lock(&fs_info->scrub_lock);
320 }
321}
322
Wang Shilongcb7ab022013-12-04 21:16:53 +0800323static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
324{
325 atomic_inc(&fs_info->scrubs_paused);
326 wake_up(&fs_info->scrub_pause_wait);
327
328 mutex_lock(&fs_info->scrub_lock);
329 __scrub_blocked_if_needed(fs_info);
330 atomic_dec(&fs_info->scrubs_paused);
331 mutex_unlock(&fs_info->scrub_lock);
332
333 wake_up(&fs_info->scrub_pause_wait);
334}
335
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100336/*
337 * used for workers that require transaction commits (i.e., for the
338 * NOCOW case)
339 */
340static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
341{
342 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
343
344 /*
345 * increment scrubs_running to prevent cancel requests from
346 * completing as long as a worker is running. we must also
347 * increment scrubs_paused to prevent deadlocking on pause
348 * requests used for transactions commits (as the worker uses a
349 * transaction context). it is safe to regard the worker
350 * as paused for all matters practical. effectively, we only
351 * avoid cancellation requests from completing.
352 */
353 mutex_lock(&fs_info->scrub_lock);
354 atomic_inc(&fs_info->scrubs_running);
355 atomic_inc(&fs_info->scrubs_paused);
356 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800357
358 /*
359 * check if @scrubs_running=@scrubs_paused condition
360 * inside wait_event() is not an atomic operation.
361 * which means we may inc/dec @scrub_running/paused
362 * at any time. Let's wake up @scrub_pause_wait as
363 * much as we can to let commit transaction blocked less.
364 */
365 wake_up(&fs_info->scrub_pause_wait);
366
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100367 atomic_inc(&sctx->workers_pending);
368}
369
370/* used for workers that require transaction commits */
371static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
372{
373 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
374
375 /*
376 * see scrub_pending_trans_workers_inc() why we're pretending
377 * to be paused in the scrub counters
378 */
379 mutex_lock(&fs_info->scrub_lock);
380 atomic_dec(&fs_info->scrubs_running);
381 atomic_dec(&fs_info->scrubs_paused);
382 mutex_unlock(&fs_info->scrub_lock);
383 atomic_dec(&sctx->workers_pending);
384 wake_up(&fs_info->scrub_pause_wait);
385 wake_up(&sctx->list_wait);
386}
387
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100388static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100389{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100390 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100391 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100392 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100393 struct btrfs_ordered_sum, list);
394 list_del(&sum->list);
395 kfree(sum);
396 }
397}
398
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100399static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100400{
401 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100402
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100403 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100404 return;
405
Stefan Behrensff023aa2012-11-06 11:43:11 +0100406 scrub_free_wr_ctx(&sctx->wr_ctx);
407
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400408 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100409 if (sctx->curr != -1) {
410 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400411
412 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100413 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400414 scrub_block_put(sbio->pagev[i]->sblock);
415 }
416 bio_put(sbio->bio);
417 }
418
Stefan Behrensff023aa2012-11-06 11:43:11 +0100419 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100420 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100421
422 if (!sbio)
423 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100424 kfree(sbio);
425 }
426
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100427 scrub_free_csums(sctx);
428 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100429}
430
431static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100432struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100433{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100434 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100435 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100436 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100437 int pages_per_rd_bio;
438 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100439
Stefan Behrensff023aa2012-11-06 11:43:11 +0100440 /*
441 * the setting of pages_per_rd_bio is correct for scrub but might
442 * be wrong for the dev_replace code where we might read from
443 * different devices in the initial huge bios. However, that
444 * code is able to correctly handle the case when adding a page
445 * to a bio fails.
446 */
447 if (dev->bdev)
448 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
449 bio_get_nr_vecs(dev->bdev));
450 else
451 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100452 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
453 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100454 goto nomem;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100455 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100456 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100457 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100458 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100459 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100460 struct scrub_bio *sbio;
461
462 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
463 if (!sbio)
464 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100465 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100466
Arne Jansena2de7332011-03-08 14:14:00 +0100467 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100468 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400469 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800470 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
471 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100472
Stefan Behrensff023aa2012-11-06 11:43:11 +0100473 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100474 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200475 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100476 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100477 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100478 sctx->first_free = 0;
479 sctx->nodesize = dev->dev_root->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100480 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100481 atomic_set(&sctx->bios_in_flight, 0);
482 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100483 atomic_set(&sctx->cancel_req, 0);
484 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
485 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100486
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100487 spin_lock_init(&sctx->list_lock);
488 spin_lock_init(&sctx->stat_lock);
489 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100490
491 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
492 fs_info->dev_replace.tgtdev, is_dev_replace);
493 if (ret) {
494 scrub_free_ctx(sctx);
495 return ERR_PTR(ret);
496 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100497 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100498
499nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100500 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100501 return ERR_PTR(-ENOMEM);
502}
503
Stefan Behrensff023aa2012-11-06 11:43:11 +0100504static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
505 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200506{
507 u64 isize;
508 u32 nlink;
509 int ret;
510 int i;
511 struct extent_buffer *eb;
512 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100513 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200514 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
515 struct inode_fs_paths *ipath = NULL;
516 struct btrfs_root *local_root;
517 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100518 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200519
520 root_key.objectid = root;
521 root_key.type = BTRFS_ROOT_ITEM_KEY;
522 root_key.offset = (u64)-1;
523 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
524 if (IS_ERR(local_root)) {
525 ret = PTR_ERR(local_root);
526 goto err;
527 }
528
David Sterba14692cc2015-01-02 18:55:46 +0100529 /*
530 * this makes the path point to (inum INODE_ITEM ioff)
531 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100532 key.objectid = inum;
533 key.type = BTRFS_INODE_ITEM_KEY;
534 key.offset = 0;
535
536 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200537 if (ret) {
538 btrfs_release_path(swarn->path);
539 goto err;
540 }
541
542 eb = swarn->path->nodes[0];
543 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
544 struct btrfs_inode_item);
545 isize = btrfs_inode_size(eb, inode_item);
546 nlink = btrfs_inode_nlink(eb, inode_item);
547 btrfs_release_path(swarn->path);
548
549 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300550 if (IS_ERR(ipath)) {
551 ret = PTR_ERR(ipath);
552 ipath = NULL;
553 goto err;
554 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200555 ret = paths_from_inode(inum, ipath);
556
557 if (ret < 0)
558 goto err;
559
560 /*
561 * we deliberately ignore the bit ipath might have been too small to
562 * hold all of the paths here
563 */
564 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Frank Holtonefe120a2013-12-20 11:37:06 -0500565 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200566 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
567 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400568 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200569 (unsigned long long)swarn->sector, root, inum, offset,
570 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500571 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200572
573 free_ipath(ipath);
574 return 0;
575
576err:
Frank Holtonefe120a2013-12-20 11:37:06 -0500577 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200578 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
579 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400580 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200581 (unsigned long long)swarn->sector, root, inum, offset, ret);
582
583 free_ipath(ipath);
584 return 0;
585}
586
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400587static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200588{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100589 struct btrfs_device *dev;
590 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200591 struct btrfs_path *path;
592 struct btrfs_key found_key;
593 struct extent_buffer *eb;
594 struct btrfs_extent_item *ei;
595 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200596 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100597 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600598 u64 flags = 0;
599 u64 ref_root;
600 u32 item_size;
601 u8 ref_level;
Liu Bo69917e42012-09-07 20:01:28 -0600602 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200603
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100604 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100605 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100606 fs_info = sblock->sctx->dev_root->fs_info;
607
Jan Schmidt558540c2011-06-13 19:59:12 +0200608 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200609 if (!path)
610 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200611
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100612 swarn.sector = (sblock->pagev[0]->physical) >> 9;
613 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200614 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100615 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200616
Liu Bo69917e42012-09-07 20:01:28 -0600617 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
618 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200619 if (ret < 0)
620 goto out;
621
Jan Schmidt4692cf52011-12-02 14:56:41 +0100622 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200623 swarn.extent_item_size = found_key.offset;
624
625 eb = path->nodes[0];
626 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
627 item_size = btrfs_item_size_nr(eb, path->slots[0]);
628
Liu Bo69917e42012-09-07 20:01:28 -0600629 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200630 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800631 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
632 item_size, &ref_root,
633 &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400634 printk_in_rcu(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -0500635 "BTRFS: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200636 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400637 "%llu\n", errstr, swarn.logical,
638 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200639 (unsigned long long)swarn.sector,
640 ref_level ? "node" : "leaf",
641 ret < 0 ? -1 : ref_level,
642 ret < 0 ? -1 : ref_root);
643 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600644 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200645 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600646 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200647 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100648 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100649 iterate_extent_inodes(fs_info, found_key.objectid,
650 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200651 scrub_print_warning_inode, &swarn);
652 }
653
654out:
655 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200656}
657
Stefan Behrensff023aa2012-11-06 11:43:11 +0100658static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200659{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200660 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200661 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100662 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200663 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200664 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200665 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200666 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000667 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200668 u64 end = offset + PAGE_SIZE - 1;
669 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000670 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200671
672 key.objectid = root;
673 key.type = BTRFS_ROOT_ITEM_KEY;
674 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000675
676 fs_info = fixup->root->fs_info;
677 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
678
679 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
680 if (IS_ERR(local_root)) {
681 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200682 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000683 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200684
685 key.type = BTRFS_INODE_ITEM_KEY;
686 key.objectid = inum;
687 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000688 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
689 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200690 if (IS_ERR(inode))
691 return PTR_ERR(inode);
692
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200693 index = offset >> PAGE_CACHE_SHIFT;
694
695 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200696 if (!page) {
697 ret = -ENOMEM;
698 goto out;
699 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200700
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200701 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200702 if (PageDirty(page)) {
703 /*
704 * we need to write the data to the defect sector. the
705 * data that was in that sector is not in memory,
706 * because the page was modified. we must not write the
707 * modified page to that sector.
708 *
709 * TODO: what could be done here: wait for the delalloc
710 * runner to write out that page (might involve
711 * COW) and see whether the sector is still
712 * referenced afterwards.
713 *
714 * For the meantime, we'll treat this error
715 * incorrectable, although there is a chance that a
716 * later scrub will find the bad sector again and that
717 * there's no dirty page in memory, then.
718 */
719 ret = -EIO;
720 goto out;
721 }
Miao Xie1203b682014-09-12 18:44:01 +0800722 ret = repair_io_failure(inode, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200723 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800724 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200725 fixup->mirror_num);
726 unlock_page(page);
727 corrected = !ret;
728 } else {
729 /*
730 * we need to get good data first. the general readpage path
731 * will call repair_io_failure for us, we just have to make
732 * sure we read the bad mirror.
733 */
734 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
735 EXTENT_DAMAGED, GFP_NOFS);
736 if (ret) {
737 /* set_extent_bits should give proper error */
738 WARN_ON(ret > 0);
739 if (ret > 0)
740 ret = -EFAULT;
741 goto out;
742 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200743
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200744 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
745 btrfs_get_extent,
746 fixup->mirror_num);
747 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200748
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200749 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
750 end, EXTENT_DAMAGED, 0, NULL);
751 if (!corrected)
752 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
753 EXTENT_DAMAGED, GFP_NOFS);
754 }
755
756out:
757 if (page)
758 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200759
760 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200761
762 if (ret < 0)
763 return ret;
764
765 if (ret == 0 && corrected) {
766 /*
767 * we only need to call readpage for one of the inodes belonging
768 * to this extent. so make iterate_extent_inodes stop
769 */
770 return 1;
771 }
772
773 return -EIO;
774}
775
776static void scrub_fixup_nodatasum(struct btrfs_work *work)
777{
778 int ret;
779 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100780 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200781 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200782 struct btrfs_path *path;
783 int uncorrectable = 0;
784
785 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100786 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200787
788 path = btrfs_alloc_path();
789 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100790 spin_lock(&sctx->stat_lock);
791 ++sctx->stat.malloc_errors;
792 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200793 uncorrectable = 1;
794 goto out;
795 }
796
797 trans = btrfs_join_transaction(fixup->root);
798 if (IS_ERR(trans)) {
799 uncorrectable = 1;
800 goto out;
801 }
802
803 /*
804 * the idea is to trigger a regular read through the standard path. we
805 * read a page from the (failed) logical address by specifying the
806 * corresponding copynum of the failed sector. thus, that readpage is
807 * expected to fail.
808 * that is the point where on-the-fly error correction will kick in
809 * (once it's finished) and rewrite the failed sector if a good copy
810 * can be found.
811 */
812 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
813 path, scrub_fixup_readpage,
814 fixup);
815 if (ret < 0) {
816 uncorrectable = 1;
817 goto out;
818 }
819 WARN_ON(ret != 1);
820
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100821 spin_lock(&sctx->stat_lock);
822 ++sctx->stat.corrected_errors;
823 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200824
825out:
826 if (trans && !IS_ERR(trans))
827 btrfs_end_transaction(trans, fixup->root);
828 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100829 spin_lock(&sctx->stat_lock);
830 ++sctx->stat.uncorrectable_errors;
831 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100832 btrfs_dev_replace_stats_inc(
833 &sctx->dev_root->fs_info->dev_replace.
834 num_uncorrectable_read_errors);
Frank Holtonefe120a2013-12-20 11:37:06 -0500835 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
836 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200837 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200838 }
839
840 btrfs_free_path(path);
841 kfree(fixup);
842
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100843 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200844}
845
Miao Xieaf8e2d12014-10-23 14:42:50 +0800846static inline void scrub_get_recover(struct scrub_recover *recover)
847{
848 atomic_inc(&recover->refs);
849}
850
851static inline void scrub_put_recover(struct scrub_recover *recover)
852{
853 if (atomic_dec_and_test(&recover->refs)) {
Zhao Lei6e9606d2015-01-20 15:11:34 +0800854 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800855 kfree(recover);
856 }
857}
858
Arne Jansena2de7332011-03-08 14:14:00 +0100859/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400860 * scrub_handle_errored_block gets called when either verification of the
861 * pages failed or the bio failed to read, e.g. with EIO. In the latter
862 * case, this function handles all pages in the bio, even though only one
863 * may be bad.
864 * The goal of this function is to repair the errored block by using the
865 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100866 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400867static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100868{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100869 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100870 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400871 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100872 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400873 u64 logical;
874 u64 generation;
875 unsigned int failed_mirror_index;
876 unsigned int is_metadata;
877 unsigned int have_csum;
878 u8 *csum;
879 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
880 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100881 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400882 int mirror_index;
883 int page_num;
884 int success;
885 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
886 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100887
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400888 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100889 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000890 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
891 /*
892 * if we find an error in a super block, we just report it.
893 * They will get written with the next transaction commit
894 * anyway
895 */
896 spin_lock(&sctx->stat_lock);
897 ++sctx->stat.super_errors;
898 spin_unlock(&sctx->stat_lock);
899 return 0;
900 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400901 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100902 logical = sblock_to_check->pagev[0]->logical;
903 generation = sblock_to_check->pagev[0]->generation;
904 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
905 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
906 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400907 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100908 have_csum = sblock_to_check->pagev[0]->have_csum;
909 csum = sblock_to_check->pagev[0]->csum;
910 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400911
Stefan Behrensff023aa2012-11-06 11:43:11 +0100912 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
913 sblocks_for_recheck = NULL;
914 goto nodatasum_case;
915 }
916
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400917 /*
918 * read all mirrors one after the other. This includes to
919 * re-read the extent or metadata block that failed (that was
920 * the cause that this fixup code is called) another time,
921 * page by page this time in order to know which pages
922 * caused I/O errors and which ones are good (for all mirrors).
923 * It is the goal to handle the situation when more than one
924 * mirror contains I/O errors, but the errors do not
925 * overlap, i.e. the data can be repaired by selecting the
926 * pages from those mirrors without I/O error on the
927 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
928 * would be that mirror #1 has an I/O error on the first page,
929 * the second page is good, and mirror #2 has an I/O error on
930 * the second page, but the first page is good.
931 * Then the first page of the first mirror can be repaired by
932 * taking the first page of the second mirror, and the
933 * second page of the second mirror can be repaired by
934 * copying the contents of the 2nd page of the 1st mirror.
935 * One more note: if the pages of one mirror contain I/O
936 * errors, the checksum cannot be verified. In order to get
937 * the best data for repairing, the first attempt is to find
938 * a mirror without I/O errors and with a validated checksum.
939 * Only if this is not possible, the pages are picked from
940 * mirrors with I/O errors without considering the checksum.
941 * If the latter is the case, at the end, the checksum of the
942 * repaired area is verified in order to correctly maintain
943 * the statistics.
944 */
945
946 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
947 sizeof(*sblocks_for_recheck),
948 GFP_NOFS);
949 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100950 spin_lock(&sctx->stat_lock);
951 sctx->stat.malloc_errors++;
952 sctx->stat.read_errors++;
953 sctx->stat.uncorrectable_errors++;
954 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100955 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400956 goto out;
957 }
958
959 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800960 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400961 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100962 spin_lock(&sctx->stat_lock);
963 sctx->stat.read_errors++;
964 sctx->stat.uncorrectable_errors++;
965 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100966 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400967 goto out;
968 }
969 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
970 sblock_bad = sblocks_for_recheck + failed_mirror_index;
971
972 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100973 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800974 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400975
976 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
977 sblock_bad->no_io_error_seen) {
978 /*
979 * the error disappeared after reading page by page, or
980 * the area was part of a huge bio and other parts of the
981 * bio caused I/O errors, or the block layer merged several
982 * read requests into one and the error is caused by a
983 * different bio (usually one of the two latter cases is
984 * the cause)
985 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100986 spin_lock(&sctx->stat_lock);
987 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800988 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100989 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400990
Stefan Behrensff023aa2012-11-06 11:43:11 +0100991 if (sctx->is_dev_replace)
992 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400993 goto out;
994 }
995
996 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100997 spin_lock(&sctx->stat_lock);
998 sctx->stat.read_errors++;
999 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001000 if (__ratelimit(&_rs))
1001 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001002 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001003 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001004 spin_lock(&sctx->stat_lock);
1005 sctx->stat.csum_errors++;
1006 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001007 if (__ratelimit(&_rs))
1008 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001009 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001010 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001011 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001012 spin_lock(&sctx->stat_lock);
1013 sctx->stat.verify_errors++;
1014 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001015 if (__ratelimit(&_rs))
1016 scrub_print_warning("checksum/header error",
1017 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001018 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001019 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001020 BTRFS_DEV_STAT_GENERATION_ERRS);
1021 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001022 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001023 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001024 }
1025
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001026 if (sctx->readonly) {
1027 ASSERT(!sctx->is_dev_replace);
1028 goto out;
1029 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001030
1031 if (!is_metadata && !have_csum) {
1032 struct scrub_fixup_nodatasum *fixup_nodatasum;
1033
Stefan Behrensff023aa2012-11-06 11:43:11 +01001034 WARN_ON(sctx->is_dev_replace);
1035
Zhao Leib25c94c2015-01-20 15:11:35 +08001036nodatasum_case:
1037
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001038 /*
1039 * !is_metadata and !have_csum, this means that the data
1040 * might not be COW'ed, that it might be modified
1041 * concurrently. The general strategy to work on the
1042 * commit root does not help in the case when COW is not
1043 * used.
1044 */
1045 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1046 if (!fixup_nodatasum)
1047 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001048 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001049 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001050 fixup_nodatasum->logical = logical;
1051 fixup_nodatasum->root = fs_info->extent_root;
1052 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001053 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001054 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1055 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001056 btrfs_queue_work(fs_info->scrub_workers,
1057 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001058 goto out;
1059 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001060
1061 /*
1062 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001063 * checksums.
1064 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001065 * errors and also does not have a checksum error.
1066 * If one is found, and if a checksum is present, the full block
1067 * that is known to contain an error is rewritten. Afterwards
1068 * the block is known to be corrected.
1069 * If a mirror is found which is completely correct, and no
1070 * checksum is present, only those pages are rewritten that had
1071 * an I/O error in the block to be repaired, since it cannot be
1072 * determined, which copy of the other pages is better (and it
1073 * could happen otherwise that a correct page would be
1074 * overwritten by a bad one).
1075 */
1076 for (mirror_index = 0;
1077 mirror_index < BTRFS_MAX_MIRRORS &&
1078 sblocks_for_recheck[mirror_index].page_count > 0;
1079 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001080 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001081
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001082 if (mirror_index == failed_mirror_index)
1083 continue;
1084 sblock_other = sblocks_for_recheck + mirror_index;
1085
1086 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001087 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1088 have_csum, csum, generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001089 sctx->csum_size, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001090
1091 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001092 !sblock_other->checksum_error &&
1093 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001094 if (sctx->is_dev_replace) {
1095 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001096 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001097 } else {
1098 ret = scrub_repair_block_from_good_copy(
1099 sblock_bad, sblock_other);
1100 if (!ret)
1101 goto corrected_error;
1102 }
Arne Jansena2de7332011-03-08 14:14:00 +01001103 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001104 }
1105
Zhao Leib968fed2015-01-20 15:11:41 +08001106 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1107 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001108
1109 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001110 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001111 * repaired, continue by picking good copies of those pages.
1112 * Select the good pages from mirrors to rewrite bad pages from
1113 * the area to fix. Afterwards verify the checksum of the block
1114 * that is supposed to be repaired. This verification step is
1115 * only done for the purpose of statistic counting and for the
1116 * final scrub report, whether errors remain.
1117 * A perfect algorithm could make use of the checksum and try
1118 * all possible combinations of pages from the different mirrors
1119 * until the checksum verification succeeds. For example, when
1120 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1121 * of mirror #2 is readable but the final checksum test fails,
1122 * then the 2nd page of mirror #3 could be tried, whether now
1123 * the final checksum succeedes. But this would be a rare
1124 * exception and is therefore not implemented. At least it is
1125 * avoided that the good copy is overwritten.
1126 * A more useful improvement would be to pick the sectors
1127 * without I/O error based on sector sizes (512 bytes on legacy
1128 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1129 * mirror could be repaired by taking 512 byte of a different
1130 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1131 * area are unreadable.
1132 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001133 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001134 for (page_num = 0; page_num < sblock_bad->page_count;
1135 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001136 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001137 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001138
Zhao Leib968fed2015-01-20 15:11:41 +08001139 /* skip no-io-error page in scrub */
1140 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001141 continue;
1142
Zhao Leib968fed2015-01-20 15:11:41 +08001143 /* try to find no-io-error page in mirrors */
1144 if (page_bad->io_error) {
1145 for (mirror_index = 0;
1146 mirror_index < BTRFS_MAX_MIRRORS &&
1147 sblocks_for_recheck[mirror_index].page_count > 0;
1148 mirror_index++) {
1149 if (!sblocks_for_recheck[mirror_index].
1150 pagev[page_num]->io_error) {
1151 sblock_other = sblocks_for_recheck +
1152 mirror_index;
1153 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001154 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001155 }
Zhao Leib968fed2015-01-20 15:11:41 +08001156 if (!sblock_other)
1157 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001158 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001159
Zhao Leib968fed2015-01-20 15:11:41 +08001160 if (sctx->is_dev_replace) {
1161 /*
1162 * did not find a mirror to fetch the page
1163 * from. scrub_write_page_to_dev_replace()
1164 * handles this case (page->io_error), by
1165 * filling the block with zeros before
1166 * submitting the write request
1167 */
1168 if (!sblock_other)
1169 sblock_other = sblock_bad;
1170
1171 if (scrub_write_page_to_dev_replace(sblock_other,
1172 page_num) != 0) {
1173 btrfs_dev_replace_stats_inc(
1174 &sctx->dev_root->
1175 fs_info->dev_replace.
1176 num_write_errors);
1177 success = 0;
1178 }
1179 } else if (sblock_other) {
1180 ret = scrub_repair_page_from_good_copy(sblock_bad,
1181 sblock_other,
1182 page_num, 0);
1183 if (0 == ret)
1184 page_bad->io_error = 0;
1185 else
1186 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001187 }
1188 }
1189
Zhao Leib968fed2015-01-20 15:11:41 +08001190 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001191 if (is_metadata || have_csum) {
1192 /*
1193 * need to verify the checksum now that all
1194 * sectors on disk are repaired (the write
1195 * request for data to be repaired is on its way).
1196 * Just be lazy and use scrub_recheck_block()
1197 * which re-reads the data before the checksum
1198 * is verified, but most likely the data comes out
1199 * of the page cache.
1200 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001201 scrub_recheck_block(fs_info, sblock_bad,
1202 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001203 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001204 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001205 !sblock_bad->checksum_error &&
1206 sblock_bad->no_io_error_seen)
1207 goto corrected_error;
1208 else
1209 goto did_not_correct_error;
1210 } else {
1211corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001212 spin_lock(&sctx->stat_lock);
1213 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001214 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001215 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001216 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001217 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001218 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001219 }
1220 } else {
1221did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001222 spin_lock(&sctx->stat_lock);
1223 sctx->stat.uncorrectable_errors++;
1224 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001225 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001226 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001227 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001228 }
1229
1230out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001231 if (sblocks_for_recheck) {
1232 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1233 mirror_index++) {
1234 struct scrub_block *sblock = sblocks_for_recheck +
1235 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001236 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001237 int page_index;
1238
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001239 for (page_index = 0; page_index < sblock->page_count;
1240 page_index++) {
1241 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001242 recover = sblock->pagev[page_index]->recover;
1243 if (recover) {
1244 scrub_put_recover(recover);
1245 sblock->pagev[page_index]->recover =
1246 NULL;
1247 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001248 scrub_page_put(sblock->pagev[page_index]);
1249 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001250 }
1251 kfree(sblocks_for_recheck);
1252 }
1253
1254 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001255}
1256
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001257static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001258{
Zhao Lei10f11902015-01-20 15:11:43 +08001259 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1260 return 2;
1261 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1262 return 3;
1263 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001264 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001265}
1266
Zhao Lei10f11902015-01-20 15:11:43 +08001267static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1268 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001269 u64 mapped_length,
1270 int nstripes, int mirror,
1271 int *stripe_index,
1272 u64 *stripe_offset)
1273{
1274 int i;
1275
Zhao Leiffe2d202015-01-20 15:11:44 +08001276 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001277 /* RAID5/6 */
1278 for (i = 0; i < nstripes; i++) {
1279 if (raid_map[i] == RAID6_Q_STRIPE ||
1280 raid_map[i] == RAID5_P_STRIPE)
1281 continue;
1282
1283 if (logical >= raid_map[i] &&
1284 logical < raid_map[i] + mapped_length)
1285 break;
1286 }
1287
1288 *stripe_index = i;
1289 *stripe_offset = logical - raid_map[i];
1290 } else {
1291 /* The other RAID type */
1292 *stripe_index = mirror;
1293 *stripe_offset = 0;
1294 }
1295}
1296
Zhao Leibe50a8d2015-01-20 15:11:42 +08001297static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001298 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001299{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001300 struct scrub_ctx *sctx = original_sblock->sctx;
1301 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1302 u64 length = original_sblock->page_count * PAGE_SIZE;
1303 u64 logical = original_sblock->pagev[0]->logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001304 struct scrub_recover *recover;
1305 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001306 u64 sublen;
1307 u64 mapped_length;
1308 u64 stripe_offset;
1309 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001310 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001311 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001312 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001313 int ret;
1314
1315 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001316 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001317 * are not used (and not set) in the blocks that are used for
1318 * the recheck procedure
1319 */
1320
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001321 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001322 sublen = min_t(u64, length, PAGE_SIZE);
1323 mapped_length = sublen;
1324 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001325
1326 /*
1327 * with a length of PAGE_SIZE, each returned stripe
1328 * represents one mirror
1329 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001330 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001331 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001332 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001333 btrfs_put_bbio(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001334 return -EIO;
1335 }
1336
Miao Xieaf8e2d12014-10-23 14:42:50 +08001337 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1338 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001339 btrfs_put_bbio(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001340 return -ENOMEM;
1341 }
1342
1343 atomic_set(&recover->refs, 1);
1344 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001345 recover->map_length = mapped_length;
1346
Stefan Behrensff023aa2012-11-06 11:43:11 +01001347 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001348
Zhao Leibe50a8d2015-01-20 15:11:42 +08001349 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001350
Miao Xieaf8e2d12014-10-23 14:42:50 +08001351 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001352 mirror_index++) {
1353 struct scrub_block *sblock;
1354 struct scrub_page *page;
1355
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001356 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001357 sblock->sctx = sctx;
1358 page = kzalloc(sizeof(*page), GFP_NOFS);
1359 if (!page) {
1360leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001361 spin_lock(&sctx->stat_lock);
1362 sctx->stat.malloc_errors++;
1363 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001364 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001365 return -ENOMEM;
1366 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001367 scrub_page_get(page);
1368 sblock->pagev[page_index] = page;
1369 page->logical = logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001370
Zhao Lei10f11902015-01-20 15:11:43 +08001371 scrub_stripe_index_and_offset(logical,
1372 bbio->map_type,
1373 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001374 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001375 bbio->num_stripes -
1376 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001377 mirror_index,
1378 &stripe_index,
1379 &stripe_offset);
1380 page->physical = bbio->stripes[stripe_index].physical +
1381 stripe_offset;
1382 page->dev = bbio->stripes[stripe_index].dev;
1383
Stefan Behrensff023aa2012-11-06 11:43:11 +01001384 BUG_ON(page_index >= original_sblock->page_count);
1385 page->physical_for_dev_replace =
1386 original_sblock->pagev[page_index]->
1387 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001388 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001389 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001390 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001391 page->page = alloc_page(GFP_NOFS);
1392 if (!page->page)
1393 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001394
1395 scrub_get_recover(recover);
1396 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001397 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001398 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001399 length -= sublen;
1400 logical += sublen;
1401 page_index++;
1402 }
1403
1404 return 0;
1405}
1406
Miao Xieaf8e2d12014-10-23 14:42:50 +08001407struct scrub_bio_ret {
1408 struct completion event;
1409 int error;
1410};
1411
1412static void scrub_bio_wait_endio(struct bio *bio, int error)
1413{
1414 struct scrub_bio_ret *ret = bio->bi_private;
1415
1416 ret->error = error;
1417 complete(&ret->event);
1418}
1419
1420static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1421{
Zhao Lei10f11902015-01-20 15:11:43 +08001422 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001423 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001424}
1425
1426static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1427 struct bio *bio,
1428 struct scrub_page *page)
1429{
1430 struct scrub_bio_ret done;
1431 int ret;
1432
1433 init_completion(&done.event);
1434 done.error = 0;
1435 bio->bi_iter.bi_sector = page->logical >> 9;
1436 bio->bi_private = &done;
1437 bio->bi_end_io = scrub_bio_wait_endio;
1438
1439 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001440 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001441 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001442 if (ret)
1443 return ret;
1444
1445 wait_for_completion(&done.event);
1446 if (done.error)
1447 return -EIO;
1448
1449 return 0;
1450}
1451
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001452/*
1453 * this function will check the on disk data for checksum errors, header
1454 * errors and read I/O errors. If any I/O errors happen, the exact pages
1455 * which are errored are marked as being bad. The goal is to enable scrub
1456 * to take those pages that are not errored from all the mirrors so that
1457 * the pages that are errored in the just handled mirror can be repaired.
1458 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001459static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1460 struct scrub_block *sblock, int is_metadata,
1461 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001462 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001463{
1464 int page_num;
1465
1466 sblock->no_io_error_seen = 1;
1467 sblock->header_error = 0;
1468 sblock->checksum_error = 0;
1469
1470 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1471 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001472 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001473
Stefan Behrens442a4f62012-05-25 16:06:08 +02001474 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001475 page->io_error = 1;
1476 sblock->no_io_error_seen = 0;
1477 continue;
1478 }
1479
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001480 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001481 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001482 if (!bio) {
1483 page->io_error = 1;
1484 sblock->no_io_error_seen = 0;
1485 continue;
1486 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001487 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001488
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001489 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001490 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1491 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1492 sblock->no_io_error_seen = 0;
1493 } else {
1494 bio->bi_iter.bi_sector = page->physical >> 9;
1495
1496 if (btrfsic_submit_bio_wait(READ, bio))
1497 sblock->no_io_error_seen = 0;
1498 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001499
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001500 bio_put(bio);
1501 }
1502
1503 if (sblock->no_io_error_seen)
1504 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1505 have_csum, csum, generation,
1506 csum_size);
1507
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001508 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001509}
1510
Miao Xie17a9be22014-07-24 11:37:08 +08001511static inline int scrub_check_fsid(u8 fsid[],
1512 struct scrub_page *spage)
1513{
1514 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1515 int ret;
1516
1517 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1518 return !ret;
1519}
1520
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001521static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1522 struct scrub_block *sblock,
1523 int is_metadata, int have_csum,
1524 const u8 *csum, u64 generation,
1525 u16 csum_size)
1526{
1527 int page_num;
1528 u8 calculated_csum[BTRFS_CSUM_SIZE];
1529 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001530 void *mapped_buffer;
1531
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001532 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001533 if (is_metadata) {
1534 struct btrfs_header *h;
1535
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001536 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001537 h = (struct btrfs_header *)mapped_buffer;
1538
Qu Wenruo3cae2102013-07-16 11:19:18 +08001539 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001540 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001541 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001542 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001543 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001544 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001545 sblock->header_error = 1;
1546 sblock->generation_error = 1;
1547 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001548 csum = h->csum;
1549 } else {
1550 if (!have_csum)
1551 return;
1552
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001553 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001554 }
1555
1556 for (page_num = 0;;) {
1557 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001558 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001559 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1560 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1561 else
Liu Bob0496682013-03-14 14:57:45 +00001562 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001563
Linus Torvalds9613beb2012-03-30 12:44:29 -07001564 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001565 page_num++;
1566 if (page_num >= sblock->page_count)
1567 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001568 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001569
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001570 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001571 }
1572
1573 btrfs_csum_final(crc, calculated_csum);
1574 if (memcmp(calculated_csum, csum, csum_size))
1575 sblock->checksum_error = 1;
1576}
1577
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001578static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001579 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001580{
1581 int page_num;
1582 int ret = 0;
1583
1584 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1585 int ret_sub;
1586
1587 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1588 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001589 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001590 if (ret_sub)
1591 ret = ret_sub;
1592 }
1593
1594 return ret;
1595}
1596
1597static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1598 struct scrub_block *sblock_good,
1599 int page_num, int force_write)
1600{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001601 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1602 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001603
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001604 BUG_ON(page_bad->page == NULL);
1605 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001606 if (force_write || sblock_bad->header_error ||
1607 sblock_bad->checksum_error || page_bad->io_error) {
1608 struct bio *bio;
1609 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001610
Stefan Behrensff023aa2012-11-06 11:43:11 +01001611 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001612 printk_ratelimited(KERN_WARNING "BTRFS: "
1613 "scrub_repair_page_from_good_copy(bdev == NULL) "
1614 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001615 return -EIO;
1616 }
1617
Chris Mason9be33952013-05-17 18:30:14 -04001618 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001619 if (!bio)
1620 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001621 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001622 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001623
1624 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1625 if (PAGE_SIZE != ret) {
1626 bio_put(bio);
1627 return -EIO;
1628 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001629
Kent Overstreet33879d42013-11-23 22:33:32 -08001630 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001631 btrfs_dev_stat_inc_and_print(page_bad->dev,
1632 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001633 btrfs_dev_replace_stats_inc(
1634 &sblock_bad->sctx->dev_root->fs_info->
1635 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001636 bio_put(bio);
1637 return -EIO;
1638 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001639 bio_put(bio);
1640 }
1641
1642 return 0;
1643}
1644
Stefan Behrensff023aa2012-11-06 11:43:11 +01001645static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1646{
1647 int page_num;
1648
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001649 /*
1650 * This block is used for the check of the parity on the source device,
1651 * so the data needn't be written into the destination device.
1652 */
1653 if (sblock->sparity)
1654 return;
1655
Stefan Behrensff023aa2012-11-06 11:43:11 +01001656 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1657 int ret;
1658
1659 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1660 if (ret)
1661 btrfs_dev_replace_stats_inc(
1662 &sblock->sctx->dev_root->fs_info->dev_replace.
1663 num_write_errors);
1664 }
1665}
1666
1667static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1668 int page_num)
1669{
1670 struct scrub_page *spage = sblock->pagev[page_num];
1671
1672 BUG_ON(spage->page == NULL);
1673 if (spage->io_error) {
1674 void *mapped_buffer = kmap_atomic(spage->page);
1675
1676 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1677 flush_dcache_page(spage->page);
1678 kunmap_atomic(mapped_buffer);
1679 }
1680 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1681}
1682
1683static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1684 struct scrub_page *spage)
1685{
1686 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1687 struct scrub_bio *sbio;
1688 int ret;
1689
1690 mutex_lock(&wr_ctx->wr_lock);
1691again:
1692 if (!wr_ctx->wr_curr_bio) {
1693 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1694 GFP_NOFS);
1695 if (!wr_ctx->wr_curr_bio) {
1696 mutex_unlock(&wr_ctx->wr_lock);
1697 return -ENOMEM;
1698 }
1699 wr_ctx->wr_curr_bio->sctx = sctx;
1700 wr_ctx->wr_curr_bio->page_count = 0;
1701 }
1702 sbio = wr_ctx->wr_curr_bio;
1703 if (sbio->page_count == 0) {
1704 struct bio *bio;
1705
1706 sbio->physical = spage->physical_for_dev_replace;
1707 sbio->logical = spage->logical;
1708 sbio->dev = wr_ctx->tgtdev;
1709 bio = sbio->bio;
1710 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001711 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001712 if (!bio) {
1713 mutex_unlock(&wr_ctx->wr_lock);
1714 return -ENOMEM;
1715 }
1716 sbio->bio = bio;
1717 }
1718
1719 bio->bi_private = sbio;
1720 bio->bi_end_io = scrub_wr_bio_end_io;
1721 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001722 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001723 sbio->err = 0;
1724 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1725 spage->physical_for_dev_replace ||
1726 sbio->logical + sbio->page_count * PAGE_SIZE !=
1727 spage->logical) {
1728 scrub_wr_submit(sctx);
1729 goto again;
1730 }
1731
1732 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1733 if (ret != PAGE_SIZE) {
1734 if (sbio->page_count < 1) {
1735 bio_put(sbio->bio);
1736 sbio->bio = NULL;
1737 mutex_unlock(&wr_ctx->wr_lock);
1738 return -EIO;
1739 }
1740 scrub_wr_submit(sctx);
1741 goto again;
1742 }
1743
1744 sbio->pagev[sbio->page_count] = spage;
1745 scrub_page_get(spage);
1746 sbio->page_count++;
1747 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1748 scrub_wr_submit(sctx);
1749 mutex_unlock(&wr_ctx->wr_lock);
1750
1751 return 0;
1752}
1753
1754static void scrub_wr_submit(struct scrub_ctx *sctx)
1755{
1756 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1757 struct scrub_bio *sbio;
1758
1759 if (!wr_ctx->wr_curr_bio)
1760 return;
1761
1762 sbio = wr_ctx->wr_curr_bio;
1763 wr_ctx->wr_curr_bio = NULL;
1764 WARN_ON(!sbio->bio->bi_bdev);
1765 scrub_pending_bio_inc(sctx);
1766 /* process all writes in a single worker thread. Then the block layer
1767 * orders the requests before sending them to the driver which
1768 * doubled the write performance on spinning disks when measured
1769 * with Linux 3.5 */
1770 btrfsic_submit_bio(WRITE, sbio->bio);
1771}
1772
1773static void scrub_wr_bio_end_io(struct bio *bio, int err)
1774{
1775 struct scrub_bio *sbio = bio->bi_private;
1776 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1777
1778 sbio->err = err;
1779 sbio->bio = bio;
1780
Liu Bo9e0af232014-08-15 23:36:53 +08001781 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1782 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001783 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001784}
1785
1786static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1787{
1788 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1789 struct scrub_ctx *sctx = sbio->sctx;
1790 int i;
1791
1792 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1793 if (sbio->err) {
1794 struct btrfs_dev_replace *dev_replace =
1795 &sbio->sctx->dev_root->fs_info->dev_replace;
1796
1797 for (i = 0; i < sbio->page_count; i++) {
1798 struct scrub_page *spage = sbio->pagev[i];
1799
1800 spage->io_error = 1;
1801 btrfs_dev_replace_stats_inc(&dev_replace->
1802 num_write_errors);
1803 }
1804 }
1805
1806 for (i = 0; i < sbio->page_count; i++)
1807 scrub_page_put(sbio->pagev[i]);
1808
1809 bio_put(sbio->bio);
1810 kfree(sbio);
1811 scrub_pending_bio_dec(sctx);
1812}
1813
1814static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001815{
1816 u64 flags;
1817 int ret;
1818
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001819 WARN_ON(sblock->page_count < 1);
1820 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001821 ret = 0;
1822 if (flags & BTRFS_EXTENT_FLAG_DATA)
1823 ret = scrub_checksum_data(sblock);
1824 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1825 ret = scrub_checksum_tree_block(sblock);
1826 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1827 (void)scrub_checksum_super(sblock);
1828 else
1829 WARN_ON(1);
1830 if (ret)
1831 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001832
1833 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001834}
1835
1836static int scrub_checksum_data(struct scrub_block *sblock)
1837{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001838 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001839 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001840 u8 *on_disk_csum;
1841 struct page *page;
1842 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001843 u32 crc = ~(u32)0;
1844 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001845 u64 len;
1846 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001847
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001848 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001849 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001850 return 0;
1851
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001852 on_disk_csum = sblock->pagev[0]->csum;
1853 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001854 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001855
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001856 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001857 index = 0;
1858 for (;;) {
1859 u64 l = min_t(u64, len, PAGE_SIZE);
1860
Liu Bob0496682013-03-14 14:57:45 +00001861 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001862 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001863 len -= l;
1864 if (len == 0)
1865 break;
1866 index++;
1867 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001868 BUG_ON(!sblock->pagev[index]->page);
1869 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001870 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001871 }
1872
Arne Jansena2de7332011-03-08 14:14:00 +01001873 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001874 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001875 fail = 1;
1876
Arne Jansena2de7332011-03-08 14:14:00 +01001877 return fail;
1878}
1879
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001880static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001881{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001882 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001883 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001884 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001885 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001886 u8 calculated_csum[BTRFS_CSUM_SIZE];
1887 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1888 struct page *page;
1889 void *mapped_buffer;
1890 u64 mapped_size;
1891 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001892 u32 crc = ~(u32)0;
1893 int fail = 0;
1894 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001895 u64 len;
1896 int index;
1897
1898 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001899 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001900 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001901 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001902 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001903
1904 /*
1905 * we don't use the getter functions here, as we
1906 * a) don't have an extent buffer and
1907 * b) the page is already kmapped
1908 */
Arne Jansena2de7332011-03-08 14:14:00 +01001909
Qu Wenruo3cae2102013-07-16 11:19:18 +08001910 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001911 ++fail;
1912
Qu Wenruo3cae2102013-07-16 11:19:18 +08001913 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001914 ++fail;
1915
Miao Xie17a9be22014-07-24 11:37:08 +08001916 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001917 ++fail;
1918
1919 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1920 BTRFS_UUID_SIZE))
1921 ++fail;
1922
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001923 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001924 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1925 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1926 index = 0;
1927 for (;;) {
1928 u64 l = min_t(u64, len, mapped_size);
1929
Liu Bob0496682013-03-14 14:57:45 +00001930 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001931 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001932 len -= l;
1933 if (len == 0)
1934 break;
1935 index++;
1936 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001937 BUG_ON(!sblock->pagev[index]->page);
1938 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001939 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001940 mapped_size = PAGE_SIZE;
1941 p = mapped_buffer;
1942 }
1943
1944 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001945 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001946 ++crc_fail;
1947
Arne Jansena2de7332011-03-08 14:14:00 +01001948 return fail || crc_fail;
1949}
1950
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001951static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001952{
1953 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001954 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001955 u8 calculated_csum[BTRFS_CSUM_SIZE];
1956 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1957 struct page *page;
1958 void *mapped_buffer;
1959 u64 mapped_size;
1960 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001961 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001962 int fail_gen = 0;
1963 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001964 u64 len;
1965 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001966
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001967 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001968 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001969 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001970 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001971 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001972
Qu Wenruo3cae2102013-07-16 11:19:18 +08001973 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001974 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001975
Qu Wenruo3cae2102013-07-16 11:19:18 +08001976 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001977 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001978
Miao Xie17a9be22014-07-24 11:37:08 +08001979 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001980 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001981
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001982 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1983 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1984 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1985 index = 0;
1986 for (;;) {
1987 u64 l = min_t(u64, len, mapped_size);
1988
Liu Bob0496682013-03-14 14:57:45 +00001989 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001990 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001991 len -= l;
1992 if (len == 0)
1993 break;
1994 index++;
1995 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001996 BUG_ON(!sblock->pagev[index]->page);
1997 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001998 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001999 mapped_size = PAGE_SIZE;
2000 p = mapped_buffer;
2001 }
2002
2003 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002004 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002005 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002006
Stefan Behrens442a4f62012-05-25 16:06:08 +02002007 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002008 /*
2009 * if we find an error in a super block, we just report it.
2010 * They will get written with the next transaction commit
2011 * anyway
2012 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002013 spin_lock(&sctx->stat_lock);
2014 ++sctx->stat.super_errors;
2015 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002016 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002017 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002018 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2019 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002020 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002021 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002022 }
2023
Stefan Behrens442a4f62012-05-25 16:06:08 +02002024 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002025}
2026
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002027static void scrub_block_get(struct scrub_block *sblock)
2028{
2029 atomic_inc(&sblock->ref_count);
2030}
2031
2032static void scrub_block_put(struct scrub_block *sblock)
2033{
2034 if (atomic_dec_and_test(&sblock->ref_count)) {
2035 int i;
2036
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002037 if (sblock->sparity)
2038 scrub_parity_put(sblock->sparity);
2039
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002040 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002041 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002042 kfree(sblock);
2043 }
2044}
2045
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002046static void scrub_page_get(struct scrub_page *spage)
2047{
2048 atomic_inc(&spage->ref_count);
2049}
2050
2051static void scrub_page_put(struct scrub_page *spage)
2052{
2053 if (atomic_dec_and_test(&spage->ref_count)) {
2054 if (spage->page)
2055 __free_page(spage->page);
2056 kfree(spage);
2057 }
2058}
2059
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002060static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002061{
2062 struct scrub_bio *sbio;
2063
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002064 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002065 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002066
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002067 sbio = sctx->bios[sctx->curr];
2068 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002069 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002070
Stefan Behrensff023aa2012-11-06 11:43:11 +01002071 if (!sbio->bio->bi_bdev) {
2072 /*
2073 * this case should not happen. If btrfs_map_block() is
2074 * wrong, it could happen for dev-replace operations on
2075 * missing devices when no mirrors are available, but in
2076 * this case it should already fail the mount.
2077 * This case is handled correctly (but _very_ slowly).
2078 */
2079 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05002080 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01002081 bio_endio(sbio->bio, -EIO);
2082 } else {
2083 btrfsic_submit_bio(READ, sbio->bio);
2084 }
Arne Jansena2de7332011-03-08 14:14:00 +01002085}
2086
Stefan Behrensff023aa2012-11-06 11:43:11 +01002087static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2088 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002089{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002090 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002091 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002092 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002093
2094again:
2095 /*
2096 * grab a fresh bio or wait for one to become available
2097 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002098 while (sctx->curr == -1) {
2099 spin_lock(&sctx->list_lock);
2100 sctx->curr = sctx->first_free;
2101 if (sctx->curr != -1) {
2102 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2103 sctx->bios[sctx->curr]->next_free = -1;
2104 sctx->bios[sctx->curr]->page_count = 0;
2105 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002106 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002107 spin_unlock(&sctx->list_lock);
2108 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002109 }
2110 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002111 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002112 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002113 struct bio *bio;
2114
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002115 sbio->physical = spage->physical;
2116 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002117 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002118 bio = sbio->bio;
2119 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002120 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002121 if (!bio)
2122 return -ENOMEM;
2123 sbio->bio = bio;
2124 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002125
2126 bio->bi_private = sbio;
2127 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002128 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002129 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002130 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002131 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2132 spage->physical ||
2133 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002134 spage->logical ||
2135 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002136 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002137 goto again;
2138 }
2139
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002140 sbio->pagev[sbio->page_count] = spage;
2141 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2142 if (ret != PAGE_SIZE) {
2143 if (sbio->page_count < 1) {
2144 bio_put(sbio->bio);
2145 sbio->bio = NULL;
2146 return -EIO;
2147 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002148 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002149 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002150 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002151
Stefan Behrensff023aa2012-11-06 11:43:11 +01002152 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002153 atomic_inc(&sblock->outstanding_pages);
2154 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002155 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002156 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002157
2158 return 0;
2159}
2160
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002161static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002162 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002163 u64 gen, int mirror_num, u8 *csum, int force,
2164 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002165{
2166 struct scrub_block *sblock;
2167 int index;
2168
2169 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2170 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002171 spin_lock(&sctx->stat_lock);
2172 sctx->stat.malloc_errors++;
2173 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002174 return -ENOMEM;
2175 }
2176
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002177 /* one ref inside this function, plus one for each page added to
2178 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002179 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002180 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002181 sblock->no_io_error_seen = 1;
2182
2183 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002184 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002185 u64 l = min_t(u64, len, PAGE_SIZE);
2186
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002187 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2188 if (!spage) {
2189leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002190 spin_lock(&sctx->stat_lock);
2191 sctx->stat.malloc_errors++;
2192 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002193 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002194 return -ENOMEM;
2195 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002196 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2197 scrub_page_get(spage);
2198 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002199 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002200 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002201 spage->flags = flags;
2202 spage->generation = gen;
2203 spage->logical = logical;
2204 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002205 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002206 spage->mirror_num = mirror_num;
2207 if (csum) {
2208 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002209 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002210 } else {
2211 spage->have_csum = 0;
2212 }
2213 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002214 spage->page = alloc_page(GFP_NOFS);
2215 if (!spage->page)
2216 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002217 len -= l;
2218 logical += l;
2219 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002220 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002221 }
2222
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002223 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002224 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002225 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002226 int ret;
2227
Stefan Behrensff023aa2012-11-06 11:43:11 +01002228 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002229 if (ret) {
2230 scrub_block_put(sblock);
2231 return ret;
2232 }
2233 }
2234
2235 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002236 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002237
2238 /* last one frees, either here or in bio completion for last page */
2239 scrub_block_put(sblock);
2240 return 0;
2241}
2242
2243static void scrub_bio_end_io(struct bio *bio, int err)
2244{
2245 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002246 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002247
2248 sbio->err = err;
2249 sbio->bio = bio;
2250
Qu Wenruo0339ef22014-02-28 10:46:17 +08002251 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002252}
2253
2254static void scrub_bio_end_io_worker(struct btrfs_work *work)
2255{
2256 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002257 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002258 int i;
2259
Stefan Behrensff023aa2012-11-06 11:43:11 +01002260 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002261 if (sbio->err) {
2262 for (i = 0; i < sbio->page_count; i++) {
2263 struct scrub_page *spage = sbio->pagev[i];
2264
2265 spage->io_error = 1;
2266 spage->sblock->no_io_error_seen = 0;
2267 }
2268 }
2269
2270 /* now complete the scrub_block items that have all pages completed */
2271 for (i = 0; i < sbio->page_count; i++) {
2272 struct scrub_page *spage = sbio->pagev[i];
2273 struct scrub_block *sblock = spage->sblock;
2274
2275 if (atomic_dec_and_test(&sblock->outstanding_pages))
2276 scrub_block_complete(sblock);
2277 scrub_block_put(sblock);
2278 }
2279
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002280 bio_put(sbio->bio);
2281 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002282 spin_lock(&sctx->list_lock);
2283 sbio->next_free = sctx->first_free;
2284 sctx->first_free = sbio->index;
2285 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002286
2287 if (sctx->is_dev_replace &&
2288 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2289 mutex_lock(&sctx->wr_ctx.wr_lock);
2290 scrub_wr_submit(sctx);
2291 mutex_unlock(&sctx->wr_ctx.wr_lock);
2292 }
2293
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002294 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002295}
2296
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002297static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2298 unsigned long *bitmap,
2299 u64 start, u64 len)
2300{
2301 int offset;
2302 int nsectors;
2303 int sectorsize = sparity->sctx->dev_root->sectorsize;
2304
2305 if (len >= sparity->stripe_len) {
2306 bitmap_set(bitmap, 0, sparity->nsectors);
2307 return;
2308 }
2309
2310 start -= sparity->logic_start;
2311 offset = (int)do_div(start, sparity->stripe_len);
2312 offset /= sectorsize;
2313 nsectors = (int)len / sectorsize;
2314
2315 if (offset + nsectors <= sparity->nsectors) {
2316 bitmap_set(bitmap, offset, nsectors);
2317 return;
2318 }
2319
2320 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2321 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2322}
2323
2324static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2325 u64 start, u64 len)
2326{
2327 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2328}
2329
2330static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2331 u64 start, u64 len)
2332{
2333 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2334}
2335
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002336static void scrub_block_complete(struct scrub_block *sblock)
2337{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002338 int corrupted = 0;
2339
Stefan Behrensff023aa2012-11-06 11:43:11 +01002340 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002341 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002342 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002343 } else {
2344 /*
2345 * if has checksum error, write via repair mechanism in
2346 * dev replace case, otherwise write here in dev replace
2347 * case.
2348 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002349 corrupted = scrub_checksum(sblock);
2350 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002351 scrub_write_block_to_dev_replace(sblock);
2352 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002353
2354 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2355 u64 start = sblock->pagev[0]->logical;
2356 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2357 PAGE_SIZE;
2358
2359 scrub_parity_mark_sectors_error(sblock->sparity,
2360 start, end - start);
2361 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002362}
2363
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002364static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002365 u8 *csum)
2366{
2367 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002368 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002369 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002370
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002371 while (!list_empty(&sctx->csum_list)) {
2372 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002373 struct btrfs_ordered_sum, list);
2374 if (sum->bytenr > logical)
2375 return 0;
2376 if (sum->bytenr + sum->len > logical)
2377 break;
2378
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002379 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002380 list_del(&sum->list);
2381 kfree(sum);
2382 sum = NULL;
2383 }
2384 if (!sum)
2385 return 0;
2386
Miao Xief51a4a12013-06-19 10:36:09 +08002387 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002388 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002389 memcpy(csum, sum->sums + index, sctx->csum_size);
2390 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002391 list_del(&sum->list);
2392 kfree(sum);
2393 }
Miao Xief51a4a12013-06-19 10:36:09 +08002394 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002395}
2396
2397/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002398static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002399 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002400 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002401{
2402 int ret;
2403 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002404 u32 blocksize;
2405
2406 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002407 blocksize = sctx->sectorsize;
2408 spin_lock(&sctx->stat_lock);
2409 sctx->stat.data_extents_scrubbed++;
2410 sctx->stat.data_bytes_scrubbed += len;
2411 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002412 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002413 blocksize = sctx->nodesize;
2414 spin_lock(&sctx->stat_lock);
2415 sctx->stat.tree_extents_scrubbed++;
2416 sctx->stat.tree_bytes_scrubbed += len;
2417 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002418 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002419 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002420 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002421 }
Arne Jansena2de7332011-03-08 14:14:00 +01002422
2423 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002424 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002425 int have_csum = 0;
2426
2427 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2428 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002429 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002430 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002431 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002432 if (sctx->is_dev_replace && !have_csum) {
2433 ret = copy_nocow_pages(sctx, logical, l,
2434 mirror_num,
2435 physical_for_dev_replace);
2436 goto behind_scrub_pages;
2437 }
Arne Jansena2de7332011-03-08 14:14:00 +01002438 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002439 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002440 mirror_num, have_csum ? csum : NULL, 0,
2441 physical_for_dev_replace);
2442behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002443 if (ret)
2444 return ret;
2445 len -= l;
2446 logical += l;
2447 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002448 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002449 }
2450 return 0;
2451}
2452
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002453static int scrub_pages_for_parity(struct scrub_parity *sparity,
2454 u64 logical, u64 len,
2455 u64 physical, struct btrfs_device *dev,
2456 u64 flags, u64 gen, int mirror_num, u8 *csum)
2457{
2458 struct scrub_ctx *sctx = sparity->sctx;
2459 struct scrub_block *sblock;
2460 int index;
2461
2462 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2463 if (!sblock) {
2464 spin_lock(&sctx->stat_lock);
2465 sctx->stat.malloc_errors++;
2466 spin_unlock(&sctx->stat_lock);
2467 return -ENOMEM;
2468 }
2469
2470 /* one ref inside this function, plus one for each page added to
2471 * a bio later on */
2472 atomic_set(&sblock->ref_count, 1);
2473 sblock->sctx = sctx;
2474 sblock->no_io_error_seen = 1;
2475 sblock->sparity = sparity;
2476 scrub_parity_get(sparity);
2477
2478 for (index = 0; len > 0; index++) {
2479 struct scrub_page *spage;
2480 u64 l = min_t(u64, len, PAGE_SIZE);
2481
2482 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2483 if (!spage) {
2484leave_nomem:
2485 spin_lock(&sctx->stat_lock);
2486 sctx->stat.malloc_errors++;
2487 spin_unlock(&sctx->stat_lock);
2488 scrub_block_put(sblock);
2489 return -ENOMEM;
2490 }
2491 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2492 /* For scrub block */
2493 scrub_page_get(spage);
2494 sblock->pagev[index] = spage;
2495 /* For scrub parity */
2496 scrub_page_get(spage);
2497 list_add_tail(&spage->list, &sparity->spages);
2498 spage->sblock = sblock;
2499 spage->dev = dev;
2500 spage->flags = flags;
2501 spage->generation = gen;
2502 spage->logical = logical;
2503 spage->physical = physical;
2504 spage->mirror_num = mirror_num;
2505 if (csum) {
2506 spage->have_csum = 1;
2507 memcpy(spage->csum, csum, sctx->csum_size);
2508 } else {
2509 spage->have_csum = 0;
2510 }
2511 sblock->page_count++;
2512 spage->page = alloc_page(GFP_NOFS);
2513 if (!spage->page)
2514 goto leave_nomem;
2515 len -= l;
2516 logical += l;
2517 physical += l;
2518 }
2519
2520 WARN_ON(sblock->page_count == 0);
2521 for (index = 0; index < sblock->page_count; index++) {
2522 struct scrub_page *spage = sblock->pagev[index];
2523 int ret;
2524
2525 ret = scrub_add_page_to_rd_bio(sctx, spage);
2526 if (ret) {
2527 scrub_block_put(sblock);
2528 return ret;
2529 }
2530 }
2531
2532 /* last one frees, either here or in bio completion for last page */
2533 scrub_block_put(sblock);
2534 return 0;
2535}
2536
2537static int scrub_extent_for_parity(struct scrub_parity *sparity,
2538 u64 logical, u64 len,
2539 u64 physical, struct btrfs_device *dev,
2540 u64 flags, u64 gen, int mirror_num)
2541{
2542 struct scrub_ctx *sctx = sparity->sctx;
2543 int ret;
2544 u8 csum[BTRFS_CSUM_SIZE];
2545 u32 blocksize;
2546
2547 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2548 blocksize = sctx->sectorsize;
2549 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2550 blocksize = sctx->nodesize;
2551 } else {
2552 blocksize = sctx->sectorsize;
2553 WARN_ON(1);
2554 }
2555
2556 while (len) {
2557 u64 l = min_t(u64, len, blocksize);
2558 int have_csum = 0;
2559
2560 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2561 /* push csums to sbio */
2562 have_csum = scrub_find_csum(sctx, logical, l, csum);
2563 if (have_csum == 0)
2564 goto skip;
2565 }
2566 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2567 flags, gen, mirror_num,
2568 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002569 if (ret)
2570 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002571skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002572 len -= l;
2573 logical += l;
2574 physical += l;
2575 }
2576 return 0;
2577}
2578
Wang Shilong3b080b22014-04-01 18:01:43 +08002579/*
2580 * Given a physical address, this will calculate it's
2581 * logical offset. if this is a parity stripe, it will return
2582 * the most left data stripe's logical offset.
2583 *
2584 * return 0 if it is a data stripe, 1 means parity stripe.
2585 */
2586static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002587 struct map_lookup *map, u64 *offset,
2588 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002589{
2590 int i;
2591 int j = 0;
2592 u64 stripe_nr;
2593 u64 last_offset;
2594 int stripe_index;
2595 int rot;
2596
2597 last_offset = (physical - map->stripes[num].physical) *
2598 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002599 if (stripe_start)
2600 *stripe_start = last_offset;
2601
Wang Shilong3b080b22014-04-01 18:01:43 +08002602 *offset = last_offset;
2603 for (i = 0; i < nr_data_stripes(map); i++) {
2604 *offset = last_offset + i * map->stripe_len;
2605
2606 stripe_nr = *offset;
2607 do_div(stripe_nr, map->stripe_len);
2608 do_div(stripe_nr, nr_data_stripes(map));
2609
2610 /* Work out the disk rotation on this stripe-set */
2611 rot = do_div(stripe_nr, map->num_stripes);
2612 /* calculate which stripe this data locates */
2613 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002614 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002615 if (stripe_index == num)
2616 return 0;
2617 if (stripe_index < num)
2618 j++;
2619 }
2620 *offset = last_offset + j * map->stripe_len;
2621 return 1;
2622}
2623
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002624static void scrub_free_parity(struct scrub_parity *sparity)
2625{
2626 struct scrub_ctx *sctx = sparity->sctx;
2627 struct scrub_page *curr, *next;
2628 int nbits;
2629
2630 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2631 if (nbits) {
2632 spin_lock(&sctx->stat_lock);
2633 sctx->stat.read_errors += nbits;
2634 sctx->stat.uncorrectable_errors += nbits;
2635 spin_unlock(&sctx->stat_lock);
2636 }
2637
2638 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2639 list_del_init(&curr->list);
2640 scrub_page_put(curr);
2641 }
2642
2643 kfree(sparity);
2644}
2645
2646static void scrub_parity_bio_endio(struct bio *bio, int error)
2647{
2648 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2649 struct scrub_ctx *sctx = sparity->sctx;
2650
2651 if (error)
2652 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2653 sparity->nsectors);
2654
2655 scrub_free_parity(sparity);
2656 scrub_pending_bio_dec(sctx);
2657 bio_put(bio);
2658}
2659
2660static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2661{
2662 struct scrub_ctx *sctx = sparity->sctx;
2663 struct bio *bio;
2664 struct btrfs_raid_bio *rbio;
2665 struct scrub_page *spage;
2666 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002667 u64 length;
2668 int ret;
2669
2670 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2671 sparity->nsectors))
2672 goto out;
2673
2674 length = sparity->logic_end - sparity->logic_start + 1;
Miao Xie76035972014-11-14 17:45:42 +08002675 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002676 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002677 &length, &bbio, 0, 1);
2678 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002679 goto bbio_out;
2680
2681 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2682 if (!bio)
2683 goto bbio_out;
2684
2685 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2686 bio->bi_private = sparity;
2687 bio->bi_end_io = scrub_parity_bio_endio;
2688
2689 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002690 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002691 sparity->dbitmap,
2692 sparity->nsectors);
2693 if (!rbio)
2694 goto rbio_out;
2695
2696 list_for_each_entry(spage, &sparity->spages, list)
2697 raid56_parity_add_scrub_pages(rbio, spage->page,
2698 spage->logical);
2699
2700 scrub_pending_bio_inc(sctx);
2701 raid56_parity_submit_scrub_rbio(rbio);
2702 return;
2703
2704rbio_out:
2705 bio_put(bio);
2706bbio_out:
Zhao Lei6e9606d2015-01-20 15:11:34 +08002707 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002708 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2709 sparity->nsectors);
2710 spin_lock(&sctx->stat_lock);
2711 sctx->stat.malloc_errors++;
2712 spin_unlock(&sctx->stat_lock);
2713out:
2714 scrub_free_parity(sparity);
2715}
2716
2717static inline int scrub_calc_parity_bitmap_len(int nsectors)
2718{
2719 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2720}
2721
2722static void scrub_parity_get(struct scrub_parity *sparity)
2723{
2724 atomic_inc(&sparity->ref_count);
2725}
2726
2727static void scrub_parity_put(struct scrub_parity *sparity)
2728{
2729 if (!atomic_dec_and_test(&sparity->ref_count))
2730 return;
2731
2732 scrub_parity_check_and_repair(sparity);
2733}
2734
2735static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2736 struct map_lookup *map,
2737 struct btrfs_device *sdev,
2738 struct btrfs_path *path,
2739 u64 logic_start,
2740 u64 logic_end)
2741{
2742 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2743 struct btrfs_root *root = fs_info->extent_root;
2744 struct btrfs_root *csum_root = fs_info->csum_root;
2745 struct btrfs_extent_item *extent;
2746 u64 flags;
2747 int ret;
2748 int slot;
2749 struct extent_buffer *l;
2750 struct btrfs_key key;
2751 u64 generation;
2752 u64 extent_logical;
2753 u64 extent_physical;
2754 u64 extent_len;
2755 struct btrfs_device *extent_dev;
2756 struct scrub_parity *sparity;
2757 int nsectors;
2758 int bitmap_len;
2759 int extent_mirror_num;
2760 int stop_loop = 0;
2761
2762 nsectors = map->stripe_len / root->sectorsize;
2763 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2764 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2765 GFP_NOFS);
2766 if (!sparity) {
2767 spin_lock(&sctx->stat_lock);
2768 sctx->stat.malloc_errors++;
2769 spin_unlock(&sctx->stat_lock);
2770 return -ENOMEM;
2771 }
2772
2773 sparity->stripe_len = map->stripe_len;
2774 sparity->nsectors = nsectors;
2775 sparity->sctx = sctx;
2776 sparity->scrub_dev = sdev;
2777 sparity->logic_start = logic_start;
2778 sparity->logic_end = logic_end;
2779 atomic_set(&sparity->ref_count, 1);
2780 INIT_LIST_HEAD(&sparity->spages);
2781 sparity->dbitmap = sparity->bitmap;
2782 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2783
2784 ret = 0;
2785 while (logic_start < logic_end) {
2786 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2787 key.type = BTRFS_METADATA_ITEM_KEY;
2788 else
2789 key.type = BTRFS_EXTENT_ITEM_KEY;
2790 key.objectid = logic_start;
2791 key.offset = (u64)-1;
2792
2793 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2794 if (ret < 0)
2795 goto out;
2796
2797 if (ret > 0) {
2798 ret = btrfs_previous_extent_item(root, path, 0);
2799 if (ret < 0)
2800 goto out;
2801 if (ret > 0) {
2802 btrfs_release_path(path);
2803 ret = btrfs_search_slot(NULL, root, &key,
2804 path, 0, 0);
2805 if (ret < 0)
2806 goto out;
2807 }
2808 }
2809
2810 stop_loop = 0;
2811 while (1) {
2812 u64 bytes;
2813
2814 l = path->nodes[0];
2815 slot = path->slots[0];
2816 if (slot >= btrfs_header_nritems(l)) {
2817 ret = btrfs_next_leaf(root, path);
2818 if (ret == 0)
2819 continue;
2820 if (ret < 0)
2821 goto out;
2822
2823 stop_loop = 1;
2824 break;
2825 }
2826 btrfs_item_key_to_cpu(l, &key, slot);
2827
2828 if (key.type == BTRFS_METADATA_ITEM_KEY)
2829 bytes = root->nodesize;
2830 else
2831 bytes = key.offset;
2832
2833 if (key.objectid + bytes <= logic_start)
2834 goto next;
2835
2836 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2837 key.type != BTRFS_METADATA_ITEM_KEY)
2838 goto next;
2839
2840 if (key.objectid > logic_end) {
2841 stop_loop = 1;
2842 break;
2843 }
2844
2845 while (key.objectid >= logic_start + map->stripe_len)
2846 logic_start += map->stripe_len;
2847
2848 extent = btrfs_item_ptr(l, slot,
2849 struct btrfs_extent_item);
2850 flags = btrfs_extent_flags(l, extent);
2851 generation = btrfs_extent_generation(l, extent);
2852
2853 if (key.objectid < logic_start &&
2854 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2855 btrfs_err(fs_info,
2856 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2857 key.objectid, logic_start);
2858 goto next;
2859 }
2860again:
2861 extent_logical = key.objectid;
2862 extent_len = bytes;
2863
2864 if (extent_logical < logic_start) {
2865 extent_len -= logic_start - extent_logical;
2866 extent_logical = logic_start;
2867 }
2868
2869 if (extent_logical + extent_len >
2870 logic_start + map->stripe_len)
2871 extent_len = logic_start + map->stripe_len -
2872 extent_logical;
2873
2874 scrub_parity_mark_sectors_data(sparity, extent_logical,
2875 extent_len);
2876
2877 scrub_remap_extent(fs_info, extent_logical,
2878 extent_len, &extent_physical,
2879 &extent_dev,
2880 &extent_mirror_num);
2881
2882 ret = btrfs_lookup_csums_range(csum_root,
2883 extent_logical,
2884 extent_logical + extent_len - 1,
2885 &sctx->csum_list, 1);
2886 if (ret)
2887 goto out;
2888
2889 ret = scrub_extent_for_parity(sparity, extent_logical,
2890 extent_len,
2891 extent_physical,
2892 extent_dev, flags,
2893 generation,
2894 extent_mirror_num);
2895 if (ret)
2896 goto out;
2897
2898 scrub_free_csums(sctx);
2899 if (extent_logical + extent_len <
2900 key.objectid + bytes) {
2901 logic_start += map->stripe_len;
2902
2903 if (logic_start >= logic_end) {
2904 stop_loop = 1;
2905 break;
2906 }
2907
2908 if (logic_start < key.objectid + bytes) {
2909 cond_resched();
2910 goto again;
2911 }
2912 }
2913next:
2914 path->slots[0]++;
2915 }
2916
2917 btrfs_release_path(path);
2918
2919 if (stop_loop)
2920 break;
2921
2922 logic_start += map->stripe_len;
2923 }
2924out:
2925 if (ret < 0)
2926 scrub_parity_mark_sectors_error(sparity, logic_start,
2927 logic_end - logic_start + 1);
2928 scrub_parity_put(sparity);
2929 scrub_submit(sctx);
2930 mutex_lock(&sctx->wr_ctx.wr_lock);
2931 scrub_wr_submit(sctx);
2932 mutex_unlock(&sctx->wr_ctx.wr_lock);
2933
2934 btrfs_release_path(path);
2935 return ret < 0 ? ret : 0;
2936}
2937
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002938static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002939 struct map_lookup *map,
2940 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002941 int num, u64 base, u64 length,
2942 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002943{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002944 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002945 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002946 struct btrfs_root *root = fs_info->extent_root;
2947 struct btrfs_root *csum_root = fs_info->csum_root;
2948 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002949 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002950 u64 flags;
2951 int ret;
2952 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002953 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002954 struct extent_buffer *l;
2955 struct btrfs_key key;
2956 u64 physical;
2957 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002958 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08002959 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002960 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002961 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002962 struct reada_control *reada1;
2963 struct reada_control *reada2;
2964 struct btrfs_key key_start;
2965 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002966 u64 increment = map->stripe_len;
2967 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002968 u64 extent_logical;
2969 u64 extent_physical;
2970 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002971 u64 stripe_logical;
2972 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002973 struct btrfs_device *extent_dev;
2974 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08002975 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002976
Arne Jansena2de7332011-03-08 14:14:00 +01002977 nstripes = length;
Wang Shilong3b080b22014-04-01 18:01:43 +08002978 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01002979 offset = 0;
2980 do_div(nstripes, map->stripe_len);
2981 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2982 offset = map->stripe_len * num;
2983 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02002984 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002985 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2986 int factor = map->num_stripes / map->sub_stripes;
2987 offset = map->stripe_len * (num / map->sub_stripes);
2988 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02002989 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002990 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2991 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002992 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002993 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2994 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002995 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08002996 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002997 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08002998 increment = map->stripe_len * nr_data_stripes(map);
2999 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003000 } else {
3001 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003002 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003003 }
3004
3005 path = btrfs_alloc_path();
3006 if (!path)
3007 return -ENOMEM;
3008
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003009 ppath = btrfs_alloc_path();
3010 if (!ppath) {
3011 btrfs_free_path(ppath);
3012 return -ENOMEM;
3013 }
3014
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003015 /*
3016 * work on commit root. The related disk blocks are static as
3017 * long as COW is applied. This means, it is save to rewrite
3018 * them to repair disk errors without any race conditions
3019 */
Arne Jansena2de7332011-03-08 14:14:00 +01003020 path->search_commit_root = 1;
3021 path->skip_locking = 1;
3022
3023 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003024 * trigger the readahead for extent tree csum tree and wait for
3025 * completion. During readahead, the scrub is officially paused
3026 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003027 */
3028 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003029 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003030 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003031 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003032 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003033 logic_end += base;
3034 } else {
3035 logic_end = logical + increment * nstripes;
3036 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003037 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003038 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003039 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003040
Arne Jansen7a262852011-06-10 12:39:23 +02003041 /* FIXME it might be better to start readahead at commit root */
3042 key_start.objectid = logical;
3043 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3044 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003045 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003046 key_end.type = BTRFS_METADATA_ITEM_KEY;
3047 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003048 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003049
Arne Jansen7a262852011-06-10 12:39:23 +02003050 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3051 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3052 key_start.offset = logical;
3053 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3054 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003055 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003056 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003057
Arne Jansen7a262852011-06-10 12:39:23 +02003058 if (!IS_ERR(reada1))
3059 btrfs_reada_wait(reada1);
3060 if (!IS_ERR(reada2))
3061 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003062
Arne Jansena2de7332011-03-08 14:14:00 +01003063
3064 /*
3065 * collect all data csums for the stripe to avoid seeking during
3066 * the scrub. This might currently (crc32) end up to be about 1MB
3067 */
Arne Jansene7786c32011-05-28 20:58:38 +00003068 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003069
Arne Jansena2de7332011-03-08 14:14:00 +01003070 /*
3071 * now find all extents for each stripe and scrub them
3072 */
Arne Jansena2de7332011-03-08 14:14:00 +01003073 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003074 while (physical < physical_end) {
3075 /* for raid56, we skip parity stripe */
Zhao Leiffe2d202015-01-20 15:11:44 +08003076 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003077 ret = get_raid56_logic_offset(physical, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003078 map, &logical, &stripe_logical);
Wang Shilong3b080b22014-04-01 18:01:43 +08003079 logical += base;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003080 if (ret) {
3081 stripe_logical += base;
3082 stripe_end = stripe_logical + increment - 1;
3083 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3084 ppath, stripe_logical,
3085 stripe_end);
3086 if (ret)
3087 goto out;
Wang Shilong3b080b22014-04-01 18:01:43 +08003088 goto skip;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003089 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003090 }
Arne Jansena2de7332011-03-08 14:14:00 +01003091 /*
3092 * canceled?
3093 */
3094 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003095 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003096 ret = -ECANCELED;
3097 goto out;
3098 }
3099 /*
3100 * check to see if we have to pause
3101 */
3102 if (atomic_read(&fs_info->scrub_pause_req)) {
3103 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003104 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003105 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003106 mutex_lock(&sctx->wr_ctx.wr_lock);
3107 scrub_wr_submit(sctx);
3108 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003109 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003110 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003111 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003112 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003113 }
3114
Wang Shilong7c76edb2014-01-12 21:38:32 +08003115 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3116 key.type = BTRFS_METADATA_ITEM_KEY;
3117 else
3118 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003119 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003120 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003121
3122 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3123 if (ret < 0)
3124 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003125
Arne Jansen8c510322011-06-03 10:09:26 +02003126 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003127 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003128 if (ret < 0)
3129 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003130 if (ret > 0) {
3131 /* there's no smaller item, so stick with the
3132 * larger one */
3133 btrfs_release_path(path);
3134 ret = btrfs_search_slot(NULL, root, &key,
3135 path, 0, 0);
3136 if (ret < 0)
3137 goto out;
3138 }
Arne Jansena2de7332011-03-08 14:14:00 +01003139 }
3140
Liu Bo625f1c8d2013-04-27 02:56:57 +00003141 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003142 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003143 u64 bytes;
3144
Arne Jansena2de7332011-03-08 14:14:00 +01003145 l = path->nodes[0];
3146 slot = path->slots[0];
3147 if (slot >= btrfs_header_nritems(l)) {
3148 ret = btrfs_next_leaf(root, path);
3149 if (ret == 0)
3150 continue;
3151 if (ret < 0)
3152 goto out;
3153
Liu Bo625f1c8d2013-04-27 02:56:57 +00003154 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003155 break;
3156 }
3157 btrfs_item_key_to_cpu(l, &key, slot);
3158
Josef Bacik3173a182013-03-07 14:22:04 -05003159 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003160 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003161 else
3162 bytes = key.offset;
3163
3164 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003165 goto next;
3166
Liu Bo625f1c8d2013-04-27 02:56:57 +00003167 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3168 key.type != BTRFS_METADATA_ITEM_KEY)
3169 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01003170
Liu Bo625f1c8d2013-04-27 02:56:57 +00003171 if (key.objectid >= logical + map->stripe_len) {
3172 /* out of this device extent */
3173 if (key.objectid >= logic_end)
3174 stop_loop = 1;
3175 break;
3176 }
Arne Jansena2de7332011-03-08 14:14:00 +01003177
3178 extent = btrfs_item_ptr(l, slot,
3179 struct btrfs_extent_item);
3180 flags = btrfs_extent_flags(l, extent);
3181 generation = btrfs_extent_generation(l, extent);
3182
3183 if (key.objectid < logical &&
3184 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003185 btrfs_err(fs_info,
3186 "scrub: tree block %llu spanning "
3187 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003188 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01003189 goto next;
3190 }
3191
Liu Bo625f1c8d2013-04-27 02:56:57 +00003192again:
3193 extent_logical = key.objectid;
3194 extent_len = bytes;
3195
Arne Jansena2de7332011-03-08 14:14:00 +01003196 /*
3197 * trim extent to this stripe
3198 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003199 if (extent_logical < logical) {
3200 extent_len -= logical - extent_logical;
3201 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003202 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003203 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003204 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003205 extent_len = logical + map->stripe_len -
3206 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003207 }
3208
Liu Bo625f1c8d2013-04-27 02:56:57 +00003209 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003210 extent_dev = scrub_dev;
3211 extent_mirror_num = mirror_num;
3212 if (is_dev_replace)
3213 scrub_remap_extent(fs_info, extent_logical,
3214 extent_len, &extent_physical,
3215 &extent_dev,
3216 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003217
3218 ret = btrfs_lookup_csums_range(csum_root, logical,
3219 logical + map->stripe_len - 1,
3220 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003221 if (ret)
3222 goto out;
3223
Liu Bo625f1c8d2013-04-27 02:56:57 +00003224 ret = scrub_extent(sctx, extent_logical, extent_len,
3225 extent_physical, extent_dev, flags,
3226 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003227 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003228 if (ret)
3229 goto out;
3230
Josef Bacikd88d46c2013-06-10 12:59:04 +00003231 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003232 if (extent_logical + extent_len <
3233 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003234 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003235 /*
3236 * loop until we find next data stripe
3237 * or we have finished all stripes.
3238 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003239loop:
3240 physical += map->stripe_len;
3241 ret = get_raid56_logic_offset(physical,
3242 num, map, &logical,
3243 &stripe_logical);
3244 logical += base;
3245
3246 if (ret && physical < physical_end) {
3247 stripe_logical += base;
3248 stripe_end = stripe_logical +
3249 increment - 1;
3250 ret = scrub_raid56_parity(sctx,
3251 map, scrub_dev, ppath,
3252 stripe_logical,
3253 stripe_end);
3254 if (ret)
3255 goto out;
3256 goto loop;
3257 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003258 } else {
3259 physical += map->stripe_len;
3260 logical += increment;
3261 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003262 if (logical < key.objectid + bytes) {
3263 cond_resched();
3264 goto again;
3265 }
3266
Wang Shilong3b080b22014-04-01 18:01:43 +08003267 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003268 stop_loop = 1;
3269 break;
3270 }
3271 }
Arne Jansena2de7332011-03-08 14:14:00 +01003272next:
3273 path->slots[0]++;
3274 }
Chris Mason71267332011-05-23 06:30:52 -04003275 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003276skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003277 logical += increment;
3278 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003279 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003280 if (stop_loop)
3281 sctx->stat.last_physical = map->stripes[num].physical +
3282 length;
3283 else
3284 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003285 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003286 if (stop_loop)
3287 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003288 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003289out:
Arne Jansena2de7332011-03-08 14:14:00 +01003290 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003291 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003292 mutex_lock(&sctx->wr_ctx.wr_lock);
3293 scrub_wr_submit(sctx);
3294 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003295
Arne Jansene7786c32011-05-28 20:58:38 +00003296 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003297 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003298 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003299 return ret < 0 ? ret : 0;
3300}
3301
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003302static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003303 struct btrfs_device *scrub_dev,
3304 u64 chunk_tree, u64 chunk_objectid,
3305 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003306 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003307{
3308 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003309 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003310 struct map_lookup *map;
3311 struct extent_map *em;
3312 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003313 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003314
3315 read_lock(&map_tree->map_tree.lock);
3316 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3317 read_unlock(&map_tree->map_tree.lock);
3318
3319 if (!em)
3320 return -EINVAL;
3321
3322 map = (struct map_lookup *)em->bdev;
3323 if (em->start != chunk_offset)
3324 goto out;
3325
3326 if (em->len < length)
3327 goto out;
3328
3329 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003330 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003331 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003332 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003333 chunk_offset, length,
3334 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003335 if (ret)
3336 goto out;
3337 }
3338 }
3339out:
3340 free_extent_map(em);
3341
3342 return ret;
3343}
3344
3345static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003346int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003347 struct btrfs_device *scrub_dev, u64 start, u64 end,
3348 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003349{
3350 struct btrfs_dev_extent *dev_extent = NULL;
3351 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003352 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003353 struct btrfs_fs_info *fs_info = root->fs_info;
3354 u64 length;
3355 u64 chunk_tree;
3356 u64 chunk_objectid;
3357 u64 chunk_offset;
3358 int ret;
3359 int slot;
3360 struct extent_buffer *l;
3361 struct btrfs_key key;
3362 struct btrfs_key found_key;
3363 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003364 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003365
3366 path = btrfs_alloc_path();
3367 if (!path)
3368 return -ENOMEM;
3369
3370 path->reada = 2;
3371 path->search_commit_root = 1;
3372 path->skip_locking = 1;
3373
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003374 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003375 key.offset = 0ull;
3376 key.type = BTRFS_DEV_EXTENT_KEY;
3377
Arne Jansena2de7332011-03-08 14:14:00 +01003378 while (1) {
3379 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3380 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003381 break;
3382 if (ret > 0) {
3383 if (path->slots[0] >=
3384 btrfs_header_nritems(path->nodes[0])) {
3385 ret = btrfs_next_leaf(root, path);
3386 if (ret)
3387 break;
3388 }
3389 }
Arne Jansena2de7332011-03-08 14:14:00 +01003390
3391 l = path->nodes[0];
3392 slot = path->slots[0];
3393
3394 btrfs_item_key_to_cpu(l, &found_key, slot);
3395
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003396 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003397 break;
3398
David Sterba962a2982014-06-04 18:41:45 +02003399 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003400 break;
3401
3402 if (found_key.offset >= end)
3403 break;
3404
3405 if (found_key.offset < key.offset)
3406 break;
3407
3408 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3409 length = btrfs_dev_extent_length(l, dev_extent);
3410
Qu Wenruoced96ed2014-06-19 10:42:51 +08003411 if (found_key.offset + length <= start)
3412 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003413
3414 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3415 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3416 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3417
3418 /*
3419 * get a reference on the corresponding block group to prevent
3420 * the chunk from going away while we scrub it
3421 */
3422 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003423
3424 /* some chunks are removed but not committed to disk yet,
3425 * continue scrubbing */
3426 if (!cache)
3427 goto skip;
3428
Stefan Behrensff023aa2012-11-06 11:43:11 +01003429 dev_replace->cursor_right = found_key.offset + length;
3430 dev_replace->cursor_left = found_key.offset;
3431 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003432 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003433 chunk_offset, length, found_key.offset,
3434 is_dev_replace);
3435
3436 /*
3437 * flush, submit all pending read and write bios, afterwards
3438 * wait for them.
3439 * Note that in the dev replace case, a read request causes
3440 * write requests that are submitted in the read completion
3441 * worker. Therefore in the current situation, it is required
3442 * that all write requests are flushed, so that all read and
3443 * write requests are really completed when bios_in_flight
3444 * changes to 0.
3445 */
3446 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3447 scrub_submit(sctx);
3448 mutex_lock(&sctx->wr_ctx.wr_lock);
3449 scrub_wr_submit(sctx);
3450 mutex_unlock(&sctx->wr_ctx.wr_lock);
3451
3452 wait_event(sctx->list_wait,
3453 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003454 atomic_inc(&fs_info->scrubs_paused);
3455 wake_up(&fs_info->scrub_pause_wait);
3456
3457 /*
3458 * must be called before we decrease @scrub_paused.
3459 * make sure we don't block transaction commit while
3460 * we are waiting pending workers finished.
3461 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003462 wait_event(sctx->list_wait,
3463 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003464 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3465
3466 mutex_lock(&fs_info->scrub_lock);
3467 __scrub_blocked_if_needed(fs_info);
3468 atomic_dec(&fs_info->scrubs_paused);
3469 mutex_unlock(&fs_info->scrub_lock);
3470 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003471
Arne Jansena2de7332011-03-08 14:14:00 +01003472 btrfs_put_block_group(cache);
3473 if (ret)
3474 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003475 if (is_dev_replace &&
3476 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003477 ret = -EIO;
3478 break;
3479 }
3480 if (sctx->stat.malloc_errors > 0) {
3481 ret = -ENOMEM;
3482 break;
3483 }
Arne Jansena2de7332011-03-08 14:14:00 +01003484
Ilya Dryomov539f3582013-10-07 13:42:57 +03003485 dev_replace->cursor_left = dev_replace->cursor_right;
3486 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003487skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003488 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003489 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003490 }
3491
Arne Jansena2de7332011-03-08 14:14:00 +01003492 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003493
3494 /*
3495 * ret can still be 1 from search_slot or next_leaf,
3496 * that's not an error
3497 */
3498 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003499}
3500
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003501static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3502 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003503{
3504 int i;
3505 u64 bytenr;
3506 u64 gen;
3507 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003508 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003509
Miao Xie87533c42013-01-29 10:14:48 +00003510 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003511 return -EIO;
3512
Miao Xie5f546062014-07-24 11:37:09 +08003513 /* Seed devices of a new filesystem has their own generation. */
3514 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3515 gen = scrub_dev->generation;
3516 else
3517 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003518
3519 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3520 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003521 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3522 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003523 break;
3524
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003525 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003526 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003527 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003528 if (ret)
3529 return ret;
3530 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003531 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003532
3533 return 0;
3534}
3535
3536/*
3537 * get a reference count on fs_info->scrub_workers. start worker if necessary
3538 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003539static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3540 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003541{
Josef Bacik0dc3b842011-11-18 14:37:27 -05003542 int ret = 0;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003543 int flags = WQ_FREEZABLE | WQ_UNBOUND;
3544 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003545
Arne Jansen632dd772011-06-10 12:07:07 +02003546 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003547 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003548 fs_info->scrub_workers =
3549 btrfs_alloc_workqueue("btrfs-scrub", flags,
3550 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003551 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003552 fs_info->scrub_workers =
3553 btrfs_alloc_workqueue("btrfs-scrub", flags,
3554 max_active, 4);
3555 if (!fs_info->scrub_workers) {
3556 ret = -ENOMEM;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003557 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003558 }
3559 fs_info->scrub_wr_completion_workers =
3560 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3561 max_active, 2);
3562 if (!fs_info->scrub_wr_completion_workers) {
3563 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003564 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003565 }
3566 fs_info->scrub_nocow_workers =
3567 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
3568 if (!fs_info->scrub_nocow_workers) {
3569 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003570 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003571 }
Arne Jansen632dd772011-06-10 12:07:07 +02003572 }
Arne Jansena2de7332011-03-08 14:14:00 +01003573 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003574out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05003575 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003576}
3577
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003578static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003579{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003580 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003581 btrfs_destroy_workqueue(fs_info->scrub_workers);
3582 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3583 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003584 }
Arne Jansena2de7332011-03-08 14:14:00 +01003585 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003586}
3587
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003588int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3589 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003590 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003591{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003592 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003593 int ret;
3594 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003595 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003596
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003597 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003598 return -EINVAL;
3599
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003600 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003601 /*
3602 * in this case scrub is unable to calculate the checksum
3603 * the way scrub is implemented. Do not handle this
3604 * situation at all because it won't ever happen.
3605 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003606 btrfs_err(fs_info,
3607 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003608 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003609 return -EINVAL;
3610 }
3611
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003612 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003613 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003614 btrfs_err(fs_info,
3615 "scrub: size assumption sectorsize != PAGE_SIZE "
3616 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003617 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003618 return -EINVAL;
3619 }
3620
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003621 if (fs_info->chunk_root->nodesize >
3622 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3623 fs_info->chunk_root->sectorsize >
3624 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3625 /*
3626 * would exhaust the array bounds of pagev member in
3627 * struct scrub_block
3628 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003629 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3630 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003631 fs_info->chunk_root->nodesize,
3632 SCRUB_MAX_PAGES_PER_BLOCK,
3633 fs_info->chunk_root->sectorsize,
3634 SCRUB_MAX_PAGES_PER_BLOCK);
3635 return -EINVAL;
3636 }
3637
Arne Jansena2de7332011-03-08 14:14:00 +01003638
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003639 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3640 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003641 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003642 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003643 return -ENODEV;
3644 }
Arne Jansena2de7332011-03-08 14:14:00 +01003645
Miao Xie5d68da32014-07-24 11:37:07 +08003646 if (!is_dev_replace && !readonly && !dev->writeable) {
3647 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3648 rcu_read_lock();
3649 name = rcu_dereference(dev->name);
3650 btrfs_err(fs_info, "scrub: device %s is not writable",
3651 name->str);
3652 rcu_read_unlock();
3653 return -EROFS;
3654 }
3655
Wang Shilong3b7a0162013-10-12 02:11:12 +08003656 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003657 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003658 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003659 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003660 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003661 }
3662
Stefan Behrens8dabb742012-11-06 13:15:27 +01003663 btrfs_dev_replace_lock(&fs_info->dev_replace);
3664 if (dev->scrub_device ||
3665 (!is_dev_replace &&
3666 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3667 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003668 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003669 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003670 return -EINPROGRESS;
3671 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003672 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003673
3674 ret = scrub_workers_get(fs_info, is_dev_replace);
3675 if (ret) {
3676 mutex_unlock(&fs_info->scrub_lock);
3677 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3678 return ret;
3679 }
3680
Stefan Behrens63a212a2012-11-05 18:29:28 +01003681 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003682 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003683 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003684 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3685 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003686 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003687 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003688 sctx->readonly = readonly;
3689 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003690 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003691
Wang Shilong3cb09292013-12-04 21:15:19 +08003692 /*
3693 * checking @scrub_pause_req here, we can avoid
3694 * race between committing transaction and scrubbing.
3695 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003696 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003697 atomic_inc(&fs_info->scrubs_running);
3698 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003699
Stefan Behrensff023aa2012-11-06 11:43:11 +01003700 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003701 /*
3702 * by holding device list mutex, we can
3703 * kick off writing super in log tree sync.
3704 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003705 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003706 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003707 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003708 }
Arne Jansena2de7332011-03-08 14:14:00 +01003709
3710 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003711 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3712 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003713
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003714 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003715 atomic_dec(&fs_info->scrubs_running);
3716 wake_up(&fs_info->scrub_pause_wait);
3717
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003718 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003719
Arne Jansena2de7332011-03-08 14:14:00 +01003720 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003721 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003722
3723 mutex_lock(&fs_info->scrub_lock);
3724 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003725 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003726 mutex_unlock(&fs_info->scrub_lock);
3727
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003728 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003729
3730 return ret;
3731}
3732
Jeff Mahoney143bede2012-03-01 14:56:26 +01003733void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003734{
3735 struct btrfs_fs_info *fs_info = root->fs_info;
3736
3737 mutex_lock(&fs_info->scrub_lock);
3738 atomic_inc(&fs_info->scrub_pause_req);
3739 while (atomic_read(&fs_info->scrubs_paused) !=
3740 atomic_read(&fs_info->scrubs_running)) {
3741 mutex_unlock(&fs_info->scrub_lock);
3742 wait_event(fs_info->scrub_pause_wait,
3743 atomic_read(&fs_info->scrubs_paused) ==
3744 atomic_read(&fs_info->scrubs_running));
3745 mutex_lock(&fs_info->scrub_lock);
3746 }
3747 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003748}
3749
Jeff Mahoney143bede2012-03-01 14:56:26 +01003750void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003751{
3752 struct btrfs_fs_info *fs_info = root->fs_info;
3753
3754 atomic_dec(&fs_info->scrub_pause_req);
3755 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003756}
3757
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003758int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003759{
Arne Jansena2de7332011-03-08 14:14:00 +01003760 mutex_lock(&fs_info->scrub_lock);
3761 if (!atomic_read(&fs_info->scrubs_running)) {
3762 mutex_unlock(&fs_info->scrub_lock);
3763 return -ENOTCONN;
3764 }
3765
3766 atomic_inc(&fs_info->scrub_cancel_req);
3767 while (atomic_read(&fs_info->scrubs_running)) {
3768 mutex_unlock(&fs_info->scrub_lock);
3769 wait_event(fs_info->scrub_pause_wait,
3770 atomic_read(&fs_info->scrubs_running) == 0);
3771 mutex_lock(&fs_info->scrub_lock);
3772 }
3773 atomic_dec(&fs_info->scrub_cancel_req);
3774 mutex_unlock(&fs_info->scrub_lock);
3775
3776 return 0;
3777}
3778
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003779int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3780 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003781{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003782 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003783
3784 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003785 sctx = dev->scrub_device;
3786 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003787 mutex_unlock(&fs_info->scrub_lock);
3788 return -ENOTCONN;
3789 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003790 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003791 while (dev->scrub_device) {
3792 mutex_unlock(&fs_info->scrub_lock);
3793 wait_event(fs_info->scrub_pause_wait,
3794 dev->scrub_device == NULL);
3795 mutex_lock(&fs_info->scrub_lock);
3796 }
3797 mutex_unlock(&fs_info->scrub_lock);
3798
3799 return 0;
3800}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003801
Arne Jansena2de7332011-03-08 14:14:00 +01003802int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3803 struct btrfs_scrub_progress *progress)
3804{
3805 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003806 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003807
3808 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003809 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003810 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003811 sctx = dev->scrub_device;
3812 if (sctx)
3813 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003814 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3815
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003816 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003817}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003818
3819static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3820 u64 extent_logical, u64 extent_len,
3821 u64 *extent_physical,
3822 struct btrfs_device **extent_dev,
3823 int *extent_mirror_num)
3824{
3825 u64 mapped_length;
3826 struct btrfs_bio *bbio = NULL;
3827 int ret;
3828
3829 mapped_length = extent_len;
3830 ret = btrfs_map_block(fs_info, READ, extent_logical,
3831 &mapped_length, &bbio, 0);
3832 if (ret || !bbio || mapped_length < extent_len ||
3833 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08003834 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003835 return;
3836 }
3837
3838 *extent_physical = bbio->stripes[0].physical;
3839 *extent_mirror_num = bbio->mirror_num;
3840 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08003841 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003842}
3843
3844static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3845 struct scrub_wr_ctx *wr_ctx,
3846 struct btrfs_fs_info *fs_info,
3847 struct btrfs_device *dev,
3848 int is_dev_replace)
3849{
3850 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3851
3852 mutex_init(&wr_ctx->wr_lock);
3853 wr_ctx->wr_curr_bio = NULL;
3854 if (!is_dev_replace)
3855 return 0;
3856
3857 WARN_ON(!dev->bdev);
3858 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3859 bio_get_nr_vecs(dev->bdev));
3860 wr_ctx->tgtdev = dev;
3861 atomic_set(&wr_ctx->flush_all_writes, 0);
3862 return 0;
3863}
3864
3865static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3866{
3867 mutex_lock(&wr_ctx->wr_lock);
3868 kfree(wr_ctx->wr_curr_bio);
3869 wr_ctx->wr_curr_bio = NULL;
3870 mutex_unlock(&wr_ctx->wr_lock);
3871}
3872
3873static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3874 int mirror_num, u64 physical_for_dev_replace)
3875{
3876 struct scrub_copy_nocow_ctx *nocow_ctx;
3877 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3878
3879 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3880 if (!nocow_ctx) {
3881 spin_lock(&sctx->stat_lock);
3882 sctx->stat.malloc_errors++;
3883 spin_unlock(&sctx->stat_lock);
3884 return -ENOMEM;
3885 }
3886
3887 scrub_pending_trans_workers_inc(sctx);
3888
3889 nocow_ctx->sctx = sctx;
3890 nocow_ctx->logical = logical;
3891 nocow_ctx->len = len;
3892 nocow_ctx->mirror_num = mirror_num;
3893 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08003894 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3895 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003896 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003897 btrfs_queue_work(fs_info->scrub_nocow_workers,
3898 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003899
3900 return 0;
3901}
3902
Josef Bacik652f25a2013-09-12 16:58:28 -04003903static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3904{
3905 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3906 struct scrub_nocow_inode *nocow_inode;
3907
3908 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3909 if (!nocow_inode)
3910 return -ENOMEM;
3911 nocow_inode->inum = inum;
3912 nocow_inode->offset = offset;
3913 nocow_inode->root = root;
3914 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3915 return 0;
3916}
3917
3918#define COPY_COMPLETE 1
3919
Stefan Behrensff023aa2012-11-06 11:43:11 +01003920static void copy_nocow_pages_worker(struct btrfs_work *work)
3921{
3922 struct scrub_copy_nocow_ctx *nocow_ctx =
3923 container_of(work, struct scrub_copy_nocow_ctx, work);
3924 struct scrub_ctx *sctx = nocow_ctx->sctx;
3925 u64 logical = nocow_ctx->logical;
3926 u64 len = nocow_ctx->len;
3927 int mirror_num = nocow_ctx->mirror_num;
3928 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3929 int ret;
3930 struct btrfs_trans_handle *trans = NULL;
3931 struct btrfs_fs_info *fs_info;
3932 struct btrfs_path *path;
3933 struct btrfs_root *root;
3934 int not_written = 0;
3935
3936 fs_info = sctx->dev_root->fs_info;
3937 root = fs_info->extent_root;
3938
3939 path = btrfs_alloc_path();
3940 if (!path) {
3941 spin_lock(&sctx->stat_lock);
3942 sctx->stat.malloc_errors++;
3943 spin_unlock(&sctx->stat_lock);
3944 not_written = 1;
3945 goto out;
3946 }
3947
3948 trans = btrfs_join_transaction(root);
3949 if (IS_ERR(trans)) {
3950 not_written = 1;
3951 goto out;
3952 }
3953
3954 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04003955 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003956 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003957 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
3958 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02003959 logical, physical_for_dev_replace, len, mirror_num,
3960 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003961 not_written = 1;
3962 goto out;
3963 }
3964
Josef Bacik652f25a2013-09-12 16:58:28 -04003965 btrfs_end_transaction(trans, root);
3966 trans = NULL;
3967 while (!list_empty(&nocow_ctx->inodes)) {
3968 struct scrub_nocow_inode *entry;
3969 entry = list_first_entry(&nocow_ctx->inodes,
3970 struct scrub_nocow_inode,
3971 list);
3972 list_del_init(&entry->list);
3973 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
3974 entry->root, nocow_ctx);
3975 kfree(entry);
3976 if (ret == COPY_COMPLETE) {
3977 ret = 0;
3978 break;
3979 } else if (ret) {
3980 break;
3981 }
3982 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003983out:
Josef Bacik652f25a2013-09-12 16:58:28 -04003984 while (!list_empty(&nocow_ctx->inodes)) {
3985 struct scrub_nocow_inode *entry;
3986 entry = list_first_entry(&nocow_ctx->inodes,
3987 struct scrub_nocow_inode,
3988 list);
3989 list_del_init(&entry->list);
3990 kfree(entry);
3991 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003992 if (trans && !IS_ERR(trans))
3993 btrfs_end_transaction(trans, root);
3994 if (not_written)
3995 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
3996 num_uncorrectable_read_errors);
3997
3998 btrfs_free_path(path);
3999 kfree(nocow_ctx);
4000
4001 scrub_pending_trans_workers_dec(sctx);
4002}
4003
Gui Hecheng32159242014-11-10 15:36:08 +08004004static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4005 u64 logical)
4006{
4007 struct extent_state *cached_state = NULL;
4008 struct btrfs_ordered_extent *ordered;
4009 struct extent_io_tree *io_tree;
4010 struct extent_map *em;
4011 u64 lockstart = start, lockend = start + len - 1;
4012 int ret = 0;
4013
4014 io_tree = &BTRFS_I(inode)->io_tree;
4015
4016 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4017 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4018 if (ordered) {
4019 btrfs_put_ordered_extent(ordered);
4020 ret = 1;
4021 goto out_unlock;
4022 }
4023
4024 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4025 if (IS_ERR(em)) {
4026 ret = PTR_ERR(em);
4027 goto out_unlock;
4028 }
4029
4030 /*
4031 * This extent does not actually cover the logical extent anymore,
4032 * move on to the next inode.
4033 */
4034 if (em->block_start > logical ||
4035 em->block_start + em->block_len < logical + len) {
4036 free_extent_map(em);
4037 ret = 1;
4038 goto out_unlock;
4039 }
4040 free_extent_map(em);
4041
4042out_unlock:
4043 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4044 GFP_NOFS);
4045 return ret;
4046}
4047
Josef Bacik652f25a2013-09-12 16:58:28 -04004048static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4049 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004050{
Miao Xie826aa0a2013-06-27 18:50:59 +08004051 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004052 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004053 struct inode *inode;
4054 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004055 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004056 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004057 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004058 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004059 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004060 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004061 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004062 int ret = 0;
4063 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004064
4065 key.objectid = root;
4066 key.type = BTRFS_ROOT_ITEM_KEY;
4067 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004068
4069 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4070
Stefan Behrensff023aa2012-11-06 11:43:11 +01004071 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004072 if (IS_ERR(local_root)) {
4073 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004074 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004075 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004076
4077 key.type = BTRFS_INODE_ITEM_KEY;
4078 key.objectid = inum;
4079 key.offset = 0;
4080 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004081 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004082 if (IS_ERR(inode))
4083 return PTR_ERR(inode);
4084
Miao Xieedd14002013-06-27 18:51:00 +08004085 /* Avoid truncate/dio/punch hole.. */
4086 mutex_lock(&inode->i_mutex);
4087 inode_dio_wait(inode);
4088
Stefan Behrensff023aa2012-11-06 11:43:11 +01004089 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004090 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004091 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004092
Gui Hecheng32159242014-11-10 15:36:08 +08004093 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4094 if (ret) {
4095 ret = ret > 0 ? 0 : ret;
4096 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004097 }
4098
Stefan Behrensff023aa2012-11-06 11:43:11 +01004099 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004100 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004101again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004102 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4103 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004104 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004105 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004106 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004107 }
4108
4109 if (PageUptodate(page)) {
4110 if (PageDirty(page))
4111 goto next_page;
4112 } else {
4113 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004114 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004115 btrfs_get_extent,
4116 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004117 if (err) {
4118 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004119 goto next_page;
4120 }
Miao Xieedd14002013-06-27 18:51:00 +08004121
Miao Xie26b258912013-06-27 18:50:58 +08004122 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004123 /*
4124 * If the page has been remove from the page cache,
4125 * the data on it is meaningless, because it may be
4126 * old one, the new data may be written into the new
4127 * page in the page cache.
4128 */
4129 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004130 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004131 page_cache_release(page);
4132 goto again;
4133 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004134 if (!PageUptodate(page)) {
4135 ret = -EIO;
4136 goto next_page;
4137 }
4138 }
Gui Hecheng32159242014-11-10 15:36:08 +08004139
4140 ret = check_extent_to_block(inode, offset, len,
4141 nocow_ctx_logical);
4142 if (ret) {
4143 ret = ret > 0 ? 0 : ret;
4144 goto next_page;
4145 }
4146
Miao Xie826aa0a2013-06-27 18:50:59 +08004147 err = write_page_nocow(nocow_ctx->sctx,
4148 physical_for_dev_replace, page);
4149 if (err)
4150 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004151next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004152 unlock_page(page);
4153 page_cache_release(page);
4154
4155 if (ret)
4156 break;
4157
Stefan Behrensff023aa2012-11-06 11:43:11 +01004158 offset += PAGE_CACHE_SIZE;
4159 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004160 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004161 len -= PAGE_CACHE_SIZE;
4162 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004163 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004164out:
Miao Xieedd14002013-06-27 18:51:00 +08004165 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004166 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004167 return ret;
4168}
4169
4170static int write_page_nocow(struct scrub_ctx *sctx,
4171 u64 physical_for_dev_replace, struct page *page)
4172{
4173 struct bio *bio;
4174 struct btrfs_device *dev;
4175 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004176
4177 dev = sctx->wr_ctx.tgtdev;
4178 if (!dev)
4179 return -EIO;
4180 if (!dev->bdev) {
4181 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05004182 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004183 return -EIO;
4184 }
Chris Mason9be33952013-05-17 18:30:14 -04004185 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004186 if (!bio) {
4187 spin_lock(&sctx->stat_lock);
4188 sctx->stat.malloc_errors++;
4189 spin_unlock(&sctx->stat_lock);
4190 return -ENOMEM;
4191 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004192 bio->bi_iter.bi_size = 0;
4193 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004194 bio->bi_bdev = dev->bdev;
4195 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4196 if (ret != PAGE_CACHE_SIZE) {
4197leave_with_eio:
4198 bio_put(bio);
4199 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4200 return -EIO;
4201 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004202
Kent Overstreet33879d42013-11-23 22:33:32 -08004203 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004204 goto leave_with_eio;
4205
4206 bio_put(bio);
4207 return 0;
4208}