blob: 6d6e155c8c8bb0773971e22d403aaa6b623cf34f [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;
Zhao Lei57019342015-01-20 15:11:45 +080082 atomic_t refs;
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;
Zhao Lei57019342015-01-20 15:11:45 +0800115 atomic_t refs; /* 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
Zhao Lei57019342015-01-20 15:11:45 +0800144 atomic_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800145
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{
Filipe Mananade554a42015-01-27 23:06:42 +0000309 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
310
311 /*
312 * Hold the scrub_lock while doing the wakeup to ensure the
313 * sctx (and its wait queue list_wait) isn't destroyed/freed
314 * during the wakeup.
315 */
316 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100317 atomic_dec(&sctx->bios_in_flight);
318 wake_up(&sctx->list_wait);
Filipe Mananade554a42015-01-27 23:06:42 +0000319 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100320}
321
Wang Shilongcb7ab022013-12-04 21:16:53 +0800322static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800323{
324 while (atomic_read(&fs_info->scrub_pause_req)) {
325 mutex_unlock(&fs_info->scrub_lock);
326 wait_event(fs_info->scrub_pause_wait,
327 atomic_read(&fs_info->scrub_pause_req) == 0);
328 mutex_lock(&fs_info->scrub_lock);
329 }
330}
331
Wang Shilongcb7ab022013-12-04 21:16:53 +0800332static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
333{
334 atomic_inc(&fs_info->scrubs_paused);
335 wake_up(&fs_info->scrub_pause_wait);
336
337 mutex_lock(&fs_info->scrub_lock);
338 __scrub_blocked_if_needed(fs_info);
339 atomic_dec(&fs_info->scrubs_paused);
340 mutex_unlock(&fs_info->scrub_lock);
341
342 wake_up(&fs_info->scrub_pause_wait);
343}
344
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100345/*
346 * used for workers that require transaction commits (i.e., for the
347 * NOCOW case)
348 */
349static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
350{
351 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
352
353 /*
354 * increment scrubs_running to prevent cancel requests from
355 * completing as long as a worker is running. we must also
356 * increment scrubs_paused to prevent deadlocking on pause
357 * requests used for transactions commits (as the worker uses a
358 * transaction context). it is safe to regard the worker
359 * as paused for all matters practical. effectively, we only
360 * avoid cancellation requests from completing.
361 */
362 mutex_lock(&fs_info->scrub_lock);
363 atomic_inc(&fs_info->scrubs_running);
364 atomic_inc(&fs_info->scrubs_paused);
365 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800366
367 /*
368 * check if @scrubs_running=@scrubs_paused condition
369 * inside wait_event() is not an atomic operation.
370 * which means we may inc/dec @scrub_running/paused
371 * at any time. Let's wake up @scrub_pause_wait as
372 * much as we can to let commit transaction blocked less.
373 */
374 wake_up(&fs_info->scrub_pause_wait);
375
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100376 atomic_inc(&sctx->workers_pending);
377}
378
379/* used for workers that require transaction commits */
380static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
381{
382 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
383
384 /*
385 * see scrub_pending_trans_workers_inc() why we're pretending
386 * to be paused in the scrub counters
387 */
388 mutex_lock(&fs_info->scrub_lock);
389 atomic_dec(&fs_info->scrubs_running);
390 atomic_dec(&fs_info->scrubs_paused);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100391 atomic_dec(&sctx->workers_pending);
392 wake_up(&fs_info->scrub_pause_wait);
Filipe Mananade554a42015-01-27 23:06:42 +0000393 /*
394 * Hold the scrub_lock while doing the wakeup to ensure the
395 * sctx (and its wait queue list_wait) isn't destroyed/freed
396 * during the wakeup.
397 */
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100398 wake_up(&sctx->list_wait);
Filipe Mananade554a42015-01-27 23:06:42 +0000399 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100400}
401
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100402static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100403{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100404 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100405 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100406 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100407 struct btrfs_ordered_sum, list);
408 list_del(&sum->list);
409 kfree(sum);
410 }
411}
412
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100413static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100414{
415 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100416
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100417 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100418 return;
419
Stefan Behrensff023aa2012-11-06 11:43:11 +0100420 scrub_free_wr_ctx(&sctx->wr_ctx);
421
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400422 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100423 if (sctx->curr != -1) {
424 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400425
426 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100427 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400428 scrub_block_put(sbio->pagev[i]->sblock);
429 }
430 bio_put(sbio->bio);
431 }
432
Stefan Behrensff023aa2012-11-06 11:43:11 +0100433 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100434 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100435
436 if (!sbio)
437 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100438 kfree(sbio);
439 }
440
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100441 scrub_free_csums(sctx);
442 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100443}
444
445static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100446struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100447{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100448 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100449 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100450 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100451 int pages_per_rd_bio;
452 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100453
Stefan Behrensff023aa2012-11-06 11:43:11 +0100454 /*
455 * the setting of pages_per_rd_bio is correct for scrub but might
456 * be wrong for the dev_replace code where we might read from
457 * different devices in the initial huge bios. However, that
458 * code is able to correctly handle the case when adding a page
459 * to a bio fails.
460 */
461 if (dev->bdev)
462 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
463 bio_get_nr_vecs(dev->bdev));
464 else
465 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100466 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
467 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100468 goto nomem;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100469 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100470 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100471 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100472 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100473 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100474 struct scrub_bio *sbio;
475
476 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
477 if (!sbio)
478 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100479 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100480
Arne Jansena2de7332011-03-08 14:14:00 +0100481 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100482 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400483 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800484 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
485 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100486
Stefan Behrensff023aa2012-11-06 11:43:11 +0100487 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100488 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200489 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100490 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100491 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100492 sctx->first_free = 0;
493 sctx->nodesize = dev->dev_root->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100494 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100495 atomic_set(&sctx->bios_in_flight, 0);
496 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100497 atomic_set(&sctx->cancel_req, 0);
498 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
499 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100500
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100501 spin_lock_init(&sctx->list_lock);
502 spin_lock_init(&sctx->stat_lock);
503 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100504
505 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
506 fs_info->dev_replace.tgtdev, is_dev_replace);
507 if (ret) {
508 scrub_free_ctx(sctx);
509 return ERR_PTR(ret);
510 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100511 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100512
513nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100514 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100515 return ERR_PTR(-ENOMEM);
516}
517
Stefan Behrensff023aa2012-11-06 11:43:11 +0100518static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
519 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200520{
521 u64 isize;
522 u32 nlink;
523 int ret;
524 int i;
525 struct extent_buffer *eb;
526 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100527 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200528 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
529 struct inode_fs_paths *ipath = NULL;
530 struct btrfs_root *local_root;
531 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100532 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200533
534 root_key.objectid = root;
535 root_key.type = BTRFS_ROOT_ITEM_KEY;
536 root_key.offset = (u64)-1;
537 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
538 if (IS_ERR(local_root)) {
539 ret = PTR_ERR(local_root);
540 goto err;
541 }
542
David Sterba14692cc2015-01-02 18:55:46 +0100543 /*
544 * this makes the path point to (inum INODE_ITEM ioff)
545 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100546 key.objectid = inum;
547 key.type = BTRFS_INODE_ITEM_KEY;
548 key.offset = 0;
549
550 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200551 if (ret) {
552 btrfs_release_path(swarn->path);
553 goto err;
554 }
555
556 eb = swarn->path->nodes[0];
557 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
558 struct btrfs_inode_item);
559 isize = btrfs_inode_size(eb, inode_item);
560 nlink = btrfs_inode_nlink(eb, inode_item);
561 btrfs_release_path(swarn->path);
562
563 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300564 if (IS_ERR(ipath)) {
565 ret = PTR_ERR(ipath);
566 ipath = NULL;
567 goto err;
568 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200569 ret = paths_from_inode(inum, ipath);
570
571 if (ret < 0)
572 goto err;
573
574 /*
575 * we deliberately ignore the bit ipath might have been too small to
576 * hold all of the paths here
577 */
578 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Frank Holtonefe120a2013-12-20 11:37:06 -0500579 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200580 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
581 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400582 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200583 (unsigned long long)swarn->sector, root, inum, offset,
584 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500585 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200586
587 free_ipath(ipath);
588 return 0;
589
590err:
Frank Holtonefe120a2013-12-20 11:37:06 -0500591 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200592 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
593 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400594 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200595 (unsigned long long)swarn->sector, root, inum, offset, ret);
596
597 free_ipath(ipath);
598 return 0;
599}
600
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400601static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200602{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100603 struct btrfs_device *dev;
604 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200605 struct btrfs_path *path;
606 struct btrfs_key found_key;
607 struct extent_buffer *eb;
608 struct btrfs_extent_item *ei;
609 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200610 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100611 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600612 u64 flags = 0;
613 u64 ref_root;
614 u32 item_size;
615 u8 ref_level;
Liu Bo69917e42012-09-07 20:01:28 -0600616 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200617
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100618 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100619 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100620 fs_info = sblock->sctx->dev_root->fs_info;
621
Jan Schmidt558540c2011-06-13 19:59:12 +0200622 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200623 if (!path)
624 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200625
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100626 swarn.sector = (sblock->pagev[0]->physical) >> 9;
627 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200628 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100629 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200630
Liu Bo69917e42012-09-07 20:01:28 -0600631 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
632 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200633 if (ret < 0)
634 goto out;
635
Jan Schmidt4692cf52011-12-02 14:56:41 +0100636 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200637 swarn.extent_item_size = found_key.offset;
638
639 eb = path->nodes[0];
640 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
641 item_size = btrfs_item_size_nr(eb, path->slots[0]);
642
Liu Bo69917e42012-09-07 20:01:28 -0600643 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200644 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800645 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
646 item_size, &ref_root,
647 &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400648 printk_in_rcu(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -0500649 "BTRFS: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200650 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400651 "%llu\n", errstr, swarn.logical,
652 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200653 (unsigned long long)swarn.sector,
654 ref_level ? "node" : "leaf",
655 ret < 0 ? -1 : ref_level,
656 ret < 0 ? -1 : ref_root);
657 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600658 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200659 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600660 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200661 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100662 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100663 iterate_extent_inodes(fs_info, found_key.objectid,
664 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200665 scrub_print_warning_inode, &swarn);
666 }
667
668out:
669 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200670}
671
Stefan Behrensff023aa2012-11-06 11:43:11 +0100672static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200673{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200674 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200675 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100676 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200677 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200678 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200679 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200680 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000681 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200682 u64 end = offset + PAGE_SIZE - 1;
683 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000684 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200685
686 key.objectid = root;
687 key.type = BTRFS_ROOT_ITEM_KEY;
688 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000689
690 fs_info = fixup->root->fs_info;
691 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
692
693 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
694 if (IS_ERR(local_root)) {
695 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200696 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000697 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200698
699 key.type = BTRFS_INODE_ITEM_KEY;
700 key.objectid = inum;
701 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000702 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
703 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200704 if (IS_ERR(inode))
705 return PTR_ERR(inode);
706
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200707 index = offset >> PAGE_CACHE_SHIFT;
708
709 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200710 if (!page) {
711 ret = -ENOMEM;
712 goto out;
713 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200714
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200715 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200716 if (PageDirty(page)) {
717 /*
718 * we need to write the data to the defect sector. the
719 * data that was in that sector is not in memory,
720 * because the page was modified. we must not write the
721 * modified page to that sector.
722 *
723 * TODO: what could be done here: wait for the delalloc
724 * runner to write out that page (might involve
725 * COW) and see whether the sector is still
726 * referenced afterwards.
727 *
728 * For the meantime, we'll treat this error
729 * incorrectable, although there is a chance that a
730 * later scrub will find the bad sector again and that
731 * there's no dirty page in memory, then.
732 */
733 ret = -EIO;
734 goto out;
735 }
Miao Xie1203b682014-09-12 18:44:01 +0800736 ret = repair_io_failure(inode, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200737 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800738 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200739 fixup->mirror_num);
740 unlock_page(page);
741 corrected = !ret;
742 } else {
743 /*
744 * we need to get good data first. the general readpage path
745 * will call repair_io_failure for us, we just have to make
746 * sure we read the bad mirror.
747 */
748 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
749 EXTENT_DAMAGED, GFP_NOFS);
750 if (ret) {
751 /* set_extent_bits should give proper error */
752 WARN_ON(ret > 0);
753 if (ret > 0)
754 ret = -EFAULT;
755 goto out;
756 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200757
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200758 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
759 btrfs_get_extent,
760 fixup->mirror_num);
761 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200762
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200763 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
764 end, EXTENT_DAMAGED, 0, NULL);
765 if (!corrected)
766 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
767 EXTENT_DAMAGED, GFP_NOFS);
768 }
769
770out:
771 if (page)
772 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200773
774 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200775
776 if (ret < 0)
777 return ret;
778
779 if (ret == 0 && corrected) {
780 /*
781 * we only need to call readpage for one of the inodes belonging
782 * to this extent. so make iterate_extent_inodes stop
783 */
784 return 1;
785 }
786
787 return -EIO;
788}
789
790static void scrub_fixup_nodatasum(struct btrfs_work *work)
791{
792 int ret;
793 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100794 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200795 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200796 struct btrfs_path *path;
797 int uncorrectable = 0;
798
799 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100800 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200801
802 path = btrfs_alloc_path();
803 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100804 spin_lock(&sctx->stat_lock);
805 ++sctx->stat.malloc_errors;
806 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200807 uncorrectable = 1;
808 goto out;
809 }
810
811 trans = btrfs_join_transaction(fixup->root);
812 if (IS_ERR(trans)) {
813 uncorrectable = 1;
814 goto out;
815 }
816
817 /*
818 * the idea is to trigger a regular read through the standard path. we
819 * read a page from the (failed) logical address by specifying the
820 * corresponding copynum of the failed sector. thus, that readpage is
821 * expected to fail.
822 * that is the point where on-the-fly error correction will kick in
823 * (once it's finished) and rewrite the failed sector if a good copy
824 * can be found.
825 */
826 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
827 path, scrub_fixup_readpage,
828 fixup);
829 if (ret < 0) {
830 uncorrectable = 1;
831 goto out;
832 }
833 WARN_ON(ret != 1);
834
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100835 spin_lock(&sctx->stat_lock);
836 ++sctx->stat.corrected_errors;
837 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200838
839out:
840 if (trans && !IS_ERR(trans))
841 btrfs_end_transaction(trans, fixup->root);
842 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100843 spin_lock(&sctx->stat_lock);
844 ++sctx->stat.uncorrectable_errors;
845 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100846 btrfs_dev_replace_stats_inc(
847 &sctx->dev_root->fs_info->dev_replace.
848 num_uncorrectable_read_errors);
Frank Holtonefe120a2013-12-20 11:37:06 -0500849 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
850 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200851 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200852 }
853
854 btrfs_free_path(path);
855 kfree(fixup);
856
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100857 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200858}
859
Miao Xieaf8e2d12014-10-23 14:42:50 +0800860static inline void scrub_get_recover(struct scrub_recover *recover)
861{
862 atomic_inc(&recover->refs);
863}
864
865static inline void scrub_put_recover(struct scrub_recover *recover)
866{
867 if (atomic_dec_and_test(&recover->refs)) {
Zhao Lei6e9606d2015-01-20 15:11:34 +0800868 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800869 kfree(recover);
870 }
871}
872
Arne Jansena2de7332011-03-08 14:14:00 +0100873/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400874 * scrub_handle_errored_block gets called when either verification of the
875 * pages failed or the bio failed to read, e.g. with EIO. In the latter
876 * case, this function handles all pages in the bio, even though only one
877 * may be bad.
878 * The goal of this function is to repair the errored block by using the
879 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100880 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400881static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100882{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100883 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100884 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400885 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100886 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400887 u64 logical;
888 u64 generation;
889 unsigned int failed_mirror_index;
890 unsigned int is_metadata;
891 unsigned int have_csum;
892 u8 *csum;
893 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
894 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100895 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400896 int mirror_index;
897 int page_num;
898 int success;
899 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
900 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100901
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400902 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100903 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000904 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
905 /*
906 * if we find an error in a super block, we just report it.
907 * They will get written with the next transaction commit
908 * anyway
909 */
910 spin_lock(&sctx->stat_lock);
911 ++sctx->stat.super_errors;
912 spin_unlock(&sctx->stat_lock);
913 return 0;
914 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400915 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100916 logical = sblock_to_check->pagev[0]->logical;
917 generation = sblock_to_check->pagev[0]->generation;
918 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
919 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
920 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400921 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100922 have_csum = sblock_to_check->pagev[0]->have_csum;
923 csum = sblock_to_check->pagev[0]->csum;
924 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400925
Stefan Behrensff023aa2012-11-06 11:43:11 +0100926 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
927 sblocks_for_recheck = NULL;
928 goto nodatasum_case;
929 }
930
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400931 /*
932 * read all mirrors one after the other. This includes to
933 * re-read the extent or metadata block that failed (that was
934 * the cause that this fixup code is called) another time,
935 * page by page this time in order to know which pages
936 * caused I/O errors and which ones are good (for all mirrors).
937 * It is the goal to handle the situation when more than one
938 * mirror contains I/O errors, but the errors do not
939 * overlap, i.e. the data can be repaired by selecting the
940 * pages from those mirrors without I/O error on the
941 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
942 * would be that mirror #1 has an I/O error on the first page,
943 * the second page is good, and mirror #2 has an I/O error on
944 * the second page, but the first page is good.
945 * Then the first page of the first mirror can be repaired by
946 * taking the first page of the second mirror, and the
947 * second page of the second mirror can be repaired by
948 * copying the contents of the 2nd page of the 1st mirror.
949 * One more note: if the pages of one mirror contain I/O
950 * errors, the checksum cannot be verified. In order to get
951 * the best data for repairing, the first attempt is to find
952 * a mirror without I/O errors and with a validated checksum.
953 * Only if this is not possible, the pages are picked from
954 * mirrors with I/O errors without considering the checksum.
955 * If the latter is the case, at the end, the checksum of the
956 * repaired area is verified in order to correctly maintain
957 * the statistics.
958 */
959
960 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
961 sizeof(*sblocks_for_recheck),
962 GFP_NOFS);
963 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100964 spin_lock(&sctx->stat_lock);
965 sctx->stat.malloc_errors++;
966 sctx->stat.read_errors++;
967 sctx->stat.uncorrectable_errors++;
968 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100969 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400970 goto out;
971 }
972
973 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800974 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400975 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100976 spin_lock(&sctx->stat_lock);
977 sctx->stat.read_errors++;
978 sctx->stat.uncorrectable_errors++;
979 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100980 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400981 goto out;
982 }
983 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
984 sblock_bad = sblocks_for_recheck + failed_mirror_index;
985
986 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100987 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800988 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400989
990 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
991 sblock_bad->no_io_error_seen) {
992 /*
993 * the error disappeared after reading page by page, or
994 * the area was part of a huge bio and other parts of the
995 * bio caused I/O errors, or the block layer merged several
996 * read requests into one and the error is caused by a
997 * different bio (usually one of the two latter cases is
998 * the cause)
999 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001000 spin_lock(&sctx->stat_lock);
1001 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001002 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001003 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001004
Stefan Behrensff023aa2012-11-06 11:43:11 +01001005 if (sctx->is_dev_replace)
1006 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001007 goto out;
1008 }
1009
1010 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001011 spin_lock(&sctx->stat_lock);
1012 sctx->stat.read_errors++;
1013 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001014 if (__ratelimit(&_rs))
1015 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001016 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001017 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001018 spin_lock(&sctx->stat_lock);
1019 sctx->stat.csum_errors++;
1020 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001021 if (__ratelimit(&_rs))
1022 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001023 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001024 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001025 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001026 spin_lock(&sctx->stat_lock);
1027 sctx->stat.verify_errors++;
1028 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001029 if (__ratelimit(&_rs))
1030 scrub_print_warning("checksum/header error",
1031 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001032 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001033 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001034 BTRFS_DEV_STAT_GENERATION_ERRS);
1035 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001036 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001037 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001038 }
1039
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001040 if (sctx->readonly) {
1041 ASSERT(!sctx->is_dev_replace);
1042 goto out;
1043 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001044
1045 if (!is_metadata && !have_csum) {
1046 struct scrub_fixup_nodatasum *fixup_nodatasum;
1047
Stefan Behrensff023aa2012-11-06 11:43:11 +01001048 WARN_ON(sctx->is_dev_replace);
1049
Zhao Leib25c94c2015-01-20 15:11:35 +08001050nodatasum_case:
1051
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001052 /*
1053 * !is_metadata and !have_csum, this means that the data
1054 * might not be COW'ed, that it might be modified
1055 * concurrently. The general strategy to work on the
1056 * commit root does not help in the case when COW is not
1057 * used.
1058 */
1059 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1060 if (!fixup_nodatasum)
1061 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001062 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001063 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001064 fixup_nodatasum->logical = logical;
1065 fixup_nodatasum->root = fs_info->extent_root;
1066 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001067 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001068 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1069 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001070 btrfs_queue_work(fs_info->scrub_workers,
1071 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001072 goto out;
1073 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001074
1075 /*
1076 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001077 * checksums.
1078 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001079 * errors and also does not have a checksum error.
1080 * If one is found, and if a checksum is present, the full block
1081 * that is known to contain an error is rewritten. Afterwards
1082 * the block is known to be corrected.
1083 * If a mirror is found which is completely correct, and no
1084 * checksum is present, only those pages are rewritten that had
1085 * an I/O error in the block to be repaired, since it cannot be
1086 * determined, which copy of the other pages is better (and it
1087 * could happen otherwise that a correct page would be
1088 * overwritten by a bad one).
1089 */
1090 for (mirror_index = 0;
1091 mirror_index < BTRFS_MAX_MIRRORS &&
1092 sblocks_for_recheck[mirror_index].page_count > 0;
1093 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001094 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001095
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001096 if (mirror_index == failed_mirror_index)
1097 continue;
1098 sblock_other = sblocks_for_recheck + mirror_index;
1099
1100 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001101 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1102 have_csum, csum, generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001103 sctx->csum_size, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001104
1105 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001106 !sblock_other->checksum_error &&
1107 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001108 if (sctx->is_dev_replace) {
1109 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001110 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001111 } else {
1112 ret = scrub_repair_block_from_good_copy(
1113 sblock_bad, sblock_other);
1114 if (!ret)
1115 goto corrected_error;
1116 }
Arne Jansena2de7332011-03-08 14:14:00 +01001117 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001118 }
1119
Zhao Leib968fed2015-01-20 15:11:41 +08001120 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1121 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001122
1123 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001124 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001125 * repaired, continue by picking good copies of those pages.
1126 * Select the good pages from mirrors to rewrite bad pages from
1127 * the area to fix. Afterwards verify the checksum of the block
1128 * that is supposed to be repaired. This verification step is
1129 * only done for the purpose of statistic counting and for the
1130 * final scrub report, whether errors remain.
1131 * A perfect algorithm could make use of the checksum and try
1132 * all possible combinations of pages from the different mirrors
1133 * until the checksum verification succeeds. For example, when
1134 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1135 * of mirror #2 is readable but the final checksum test fails,
1136 * then the 2nd page of mirror #3 could be tried, whether now
1137 * the final checksum succeedes. But this would be a rare
1138 * exception and is therefore not implemented. At least it is
1139 * avoided that the good copy is overwritten.
1140 * A more useful improvement would be to pick the sectors
1141 * without I/O error based on sector sizes (512 bytes on legacy
1142 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1143 * mirror could be repaired by taking 512 byte of a different
1144 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1145 * area are unreadable.
1146 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001147 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001148 for (page_num = 0; page_num < sblock_bad->page_count;
1149 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001150 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001151 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001152
Zhao Leib968fed2015-01-20 15:11:41 +08001153 /* skip no-io-error page in scrub */
1154 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001155 continue;
1156
Zhao Leib968fed2015-01-20 15:11:41 +08001157 /* try to find no-io-error page in mirrors */
1158 if (page_bad->io_error) {
1159 for (mirror_index = 0;
1160 mirror_index < BTRFS_MAX_MIRRORS &&
1161 sblocks_for_recheck[mirror_index].page_count > 0;
1162 mirror_index++) {
1163 if (!sblocks_for_recheck[mirror_index].
1164 pagev[page_num]->io_error) {
1165 sblock_other = sblocks_for_recheck +
1166 mirror_index;
1167 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001168 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001169 }
Zhao Leib968fed2015-01-20 15:11:41 +08001170 if (!sblock_other)
1171 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001172 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001173
Zhao Leib968fed2015-01-20 15:11:41 +08001174 if (sctx->is_dev_replace) {
1175 /*
1176 * did not find a mirror to fetch the page
1177 * from. scrub_write_page_to_dev_replace()
1178 * handles this case (page->io_error), by
1179 * filling the block with zeros before
1180 * submitting the write request
1181 */
1182 if (!sblock_other)
1183 sblock_other = sblock_bad;
1184
1185 if (scrub_write_page_to_dev_replace(sblock_other,
1186 page_num) != 0) {
1187 btrfs_dev_replace_stats_inc(
1188 &sctx->dev_root->
1189 fs_info->dev_replace.
1190 num_write_errors);
1191 success = 0;
1192 }
1193 } else if (sblock_other) {
1194 ret = scrub_repair_page_from_good_copy(sblock_bad,
1195 sblock_other,
1196 page_num, 0);
1197 if (0 == ret)
1198 page_bad->io_error = 0;
1199 else
1200 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001201 }
1202 }
1203
Zhao Leib968fed2015-01-20 15:11:41 +08001204 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001205 if (is_metadata || have_csum) {
1206 /*
1207 * need to verify the checksum now that all
1208 * sectors on disk are repaired (the write
1209 * request for data to be repaired is on its way).
1210 * Just be lazy and use scrub_recheck_block()
1211 * which re-reads the data before the checksum
1212 * is verified, but most likely the data comes out
1213 * of the page cache.
1214 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001215 scrub_recheck_block(fs_info, sblock_bad,
1216 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001217 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001218 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001219 !sblock_bad->checksum_error &&
1220 sblock_bad->no_io_error_seen)
1221 goto corrected_error;
1222 else
1223 goto did_not_correct_error;
1224 } else {
1225corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001226 spin_lock(&sctx->stat_lock);
1227 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001228 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001229 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001230 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001231 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001232 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001233 }
1234 } else {
1235did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001236 spin_lock(&sctx->stat_lock);
1237 sctx->stat.uncorrectable_errors++;
1238 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001239 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001240 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001241 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001242 }
1243
1244out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001245 if (sblocks_for_recheck) {
1246 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1247 mirror_index++) {
1248 struct scrub_block *sblock = sblocks_for_recheck +
1249 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001250 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001251 int page_index;
1252
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001253 for (page_index = 0; page_index < sblock->page_count;
1254 page_index++) {
1255 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001256 recover = sblock->pagev[page_index]->recover;
1257 if (recover) {
1258 scrub_put_recover(recover);
1259 sblock->pagev[page_index]->recover =
1260 NULL;
1261 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001262 scrub_page_put(sblock->pagev[page_index]);
1263 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001264 }
1265 kfree(sblocks_for_recheck);
1266 }
1267
1268 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001269}
1270
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001271static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001272{
Zhao Lei10f11902015-01-20 15:11:43 +08001273 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1274 return 2;
1275 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1276 return 3;
1277 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001278 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001279}
1280
Zhao Lei10f11902015-01-20 15:11:43 +08001281static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1282 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001283 u64 mapped_length,
1284 int nstripes, int mirror,
1285 int *stripe_index,
1286 u64 *stripe_offset)
1287{
1288 int i;
1289
Zhao Leiffe2d202015-01-20 15:11:44 +08001290 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001291 /* RAID5/6 */
1292 for (i = 0; i < nstripes; i++) {
1293 if (raid_map[i] == RAID6_Q_STRIPE ||
1294 raid_map[i] == RAID5_P_STRIPE)
1295 continue;
1296
1297 if (logical >= raid_map[i] &&
1298 logical < raid_map[i] + mapped_length)
1299 break;
1300 }
1301
1302 *stripe_index = i;
1303 *stripe_offset = logical - raid_map[i];
1304 } else {
1305 /* The other RAID type */
1306 *stripe_index = mirror;
1307 *stripe_offset = 0;
1308 }
1309}
1310
Zhao Leibe50a8d2015-01-20 15:11:42 +08001311static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001312 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001313{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001314 struct scrub_ctx *sctx = original_sblock->sctx;
1315 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1316 u64 length = original_sblock->page_count * PAGE_SIZE;
1317 u64 logical = original_sblock->pagev[0]->logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001318 struct scrub_recover *recover;
1319 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001320 u64 sublen;
1321 u64 mapped_length;
1322 u64 stripe_offset;
1323 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001324 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001325 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001326 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001327 int ret;
1328
1329 /*
Zhao Lei57019342015-01-20 15:11:45 +08001330 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001331 * are not used (and not set) in the blocks that are used for
1332 * the recheck procedure
1333 */
1334
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001335 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001336 sublen = min_t(u64, length, PAGE_SIZE);
1337 mapped_length = sublen;
1338 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001339
1340 /*
1341 * with a length of PAGE_SIZE, each returned stripe
1342 * represents one mirror
1343 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001344 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001345 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001346 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001347 btrfs_put_bbio(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001348 return -EIO;
1349 }
1350
Miao Xieaf8e2d12014-10-23 14:42:50 +08001351 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1352 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001353 btrfs_put_bbio(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001354 return -ENOMEM;
1355 }
1356
1357 atomic_set(&recover->refs, 1);
1358 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001359 recover->map_length = mapped_length;
1360
Stefan Behrensff023aa2012-11-06 11:43:11 +01001361 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001362
Zhao Leibe50a8d2015-01-20 15:11:42 +08001363 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001364
Miao Xieaf8e2d12014-10-23 14:42:50 +08001365 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001366 mirror_index++) {
1367 struct scrub_block *sblock;
1368 struct scrub_page *page;
1369
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001370 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001371 sblock->sctx = sctx;
1372 page = kzalloc(sizeof(*page), GFP_NOFS);
1373 if (!page) {
1374leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001375 spin_lock(&sctx->stat_lock);
1376 sctx->stat.malloc_errors++;
1377 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001378 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001379 return -ENOMEM;
1380 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001381 scrub_page_get(page);
1382 sblock->pagev[page_index] = page;
1383 page->logical = logical;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001384
Zhao Lei10f11902015-01-20 15:11:43 +08001385 scrub_stripe_index_and_offset(logical,
1386 bbio->map_type,
1387 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001388 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001389 bbio->num_stripes -
1390 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001391 mirror_index,
1392 &stripe_index,
1393 &stripe_offset);
1394 page->physical = bbio->stripes[stripe_index].physical +
1395 stripe_offset;
1396 page->dev = bbio->stripes[stripe_index].dev;
1397
Stefan Behrensff023aa2012-11-06 11:43:11 +01001398 BUG_ON(page_index >= original_sblock->page_count);
1399 page->physical_for_dev_replace =
1400 original_sblock->pagev[page_index]->
1401 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001402 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001403 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001404 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001405 page->page = alloc_page(GFP_NOFS);
1406 if (!page->page)
1407 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001408
1409 scrub_get_recover(recover);
1410 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001411 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001412 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001413 length -= sublen;
1414 logical += sublen;
1415 page_index++;
1416 }
1417
1418 return 0;
1419}
1420
Miao Xieaf8e2d12014-10-23 14:42:50 +08001421struct scrub_bio_ret {
1422 struct completion event;
1423 int error;
1424};
1425
1426static void scrub_bio_wait_endio(struct bio *bio, int error)
1427{
1428 struct scrub_bio_ret *ret = bio->bi_private;
1429
1430 ret->error = error;
1431 complete(&ret->event);
1432}
1433
1434static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1435{
Zhao Lei10f11902015-01-20 15:11:43 +08001436 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001437 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001438}
1439
1440static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1441 struct bio *bio,
1442 struct scrub_page *page)
1443{
1444 struct scrub_bio_ret done;
1445 int ret;
1446
1447 init_completion(&done.event);
1448 done.error = 0;
1449 bio->bi_iter.bi_sector = page->logical >> 9;
1450 bio->bi_private = &done;
1451 bio->bi_end_io = scrub_bio_wait_endio;
1452
1453 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001454 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001455 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001456 if (ret)
1457 return ret;
1458
1459 wait_for_completion(&done.event);
1460 if (done.error)
1461 return -EIO;
1462
1463 return 0;
1464}
1465
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001466/*
1467 * this function will check the on disk data for checksum errors, header
1468 * errors and read I/O errors. If any I/O errors happen, the exact pages
1469 * which are errored are marked as being bad. The goal is to enable scrub
1470 * to take those pages that are not errored from all the mirrors so that
1471 * the pages that are errored in the just handled mirror can be repaired.
1472 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001473static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1474 struct scrub_block *sblock, int is_metadata,
1475 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001476 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001477{
1478 int page_num;
1479
1480 sblock->no_io_error_seen = 1;
1481 sblock->header_error = 0;
1482 sblock->checksum_error = 0;
1483
1484 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1485 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001486 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001487
Stefan Behrens442a4f62012-05-25 16:06:08 +02001488 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001489 page->io_error = 1;
1490 sblock->no_io_error_seen = 0;
1491 continue;
1492 }
1493
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001494 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001495 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001496 if (!bio) {
1497 page->io_error = 1;
1498 sblock->no_io_error_seen = 0;
1499 continue;
1500 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001501 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001502
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001503 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001504 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1505 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1506 sblock->no_io_error_seen = 0;
1507 } else {
1508 bio->bi_iter.bi_sector = page->physical >> 9;
1509
1510 if (btrfsic_submit_bio_wait(READ, bio))
1511 sblock->no_io_error_seen = 0;
1512 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001513
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001514 bio_put(bio);
1515 }
1516
1517 if (sblock->no_io_error_seen)
1518 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1519 have_csum, csum, generation,
1520 csum_size);
1521
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001522 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001523}
1524
Miao Xie17a9be22014-07-24 11:37:08 +08001525static inline int scrub_check_fsid(u8 fsid[],
1526 struct scrub_page *spage)
1527{
1528 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1529 int ret;
1530
1531 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1532 return !ret;
1533}
1534
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001535static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1536 struct scrub_block *sblock,
1537 int is_metadata, int have_csum,
1538 const u8 *csum, u64 generation,
1539 u16 csum_size)
1540{
1541 int page_num;
1542 u8 calculated_csum[BTRFS_CSUM_SIZE];
1543 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001544 void *mapped_buffer;
1545
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001546 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001547 if (is_metadata) {
1548 struct btrfs_header *h;
1549
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001550 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001551 h = (struct btrfs_header *)mapped_buffer;
1552
Qu Wenruo3cae2102013-07-16 11:19:18 +08001553 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001554 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001555 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001556 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001557 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001558 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001559 sblock->header_error = 1;
1560 sblock->generation_error = 1;
1561 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001562 csum = h->csum;
1563 } else {
1564 if (!have_csum)
1565 return;
1566
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001567 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001568 }
1569
1570 for (page_num = 0;;) {
1571 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001572 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001573 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1574 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1575 else
Liu Bob0496682013-03-14 14:57:45 +00001576 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001577
Linus Torvalds9613beb2012-03-30 12:44:29 -07001578 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001579 page_num++;
1580 if (page_num >= sblock->page_count)
1581 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001582 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001583
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001584 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001585 }
1586
1587 btrfs_csum_final(crc, calculated_csum);
1588 if (memcmp(calculated_csum, csum, csum_size))
1589 sblock->checksum_error = 1;
1590}
1591
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001592static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001593 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001594{
1595 int page_num;
1596 int ret = 0;
1597
1598 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1599 int ret_sub;
1600
1601 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1602 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001603 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001604 if (ret_sub)
1605 ret = ret_sub;
1606 }
1607
1608 return ret;
1609}
1610
1611static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1612 struct scrub_block *sblock_good,
1613 int page_num, int force_write)
1614{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001615 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1616 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001617
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001618 BUG_ON(page_bad->page == NULL);
1619 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001620 if (force_write || sblock_bad->header_error ||
1621 sblock_bad->checksum_error || page_bad->io_error) {
1622 struct bio *bio;
1623 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001624
Stefan Behrensff023aa2012-11-06 11:43:11 +01001625 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001626 printk_ratelimited(KERN_WARNING "BTRFS: "
1627 "scrub_repair_page_from_good_copy(bdev == NULL) "
1628 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001629 return -EIO;
1630 }
1631
Chris Mason9be33952013-05-17 18:30:14 -04001632 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001633 if (!bio)
1634 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001635 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001636 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001637
1638 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1639 if (PAGE_SIZE != ret) {
1640 bio_put(bio);
1641 return -EIO;
1642 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001643
Kent Overstreet33879d42013-11-23 22:33:32 -08001644 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001645 btrfs_dev_stat_inc_and_print(page_bad->dev,
1646 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001647 btrfs_dev_replace_stats_inc(
1648 &sblock_bad->sctx->dev_root->fs_info->
1649 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001650 bio_put(bio);
1651 return -EIO;
1652 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001653 bio_put(bio);
1654 }
1655
1656 return 0;
1657}
1658
Stefan Behrensff023aa2012-11-06 11:43:11 +01001659static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1660{
1661 int page_num;
1662
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001663 /*
1664 * This block is used for the check of the parity on the source device,
1665 * so the data needn't be written into the destination device.
1666 */
1667 if (sblock->sparity)
1668 return;
1669
Stefan Behrensff023aa2012-11-06 11:43:11 +01001670 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1671 int ret;
1672
1673 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1674 if (ret)
1675 btrfs_dev_replace_stats_inc(
1676 &sblock->sctx->dev_root->fs_info->dev_replace.
1677 num_write_errors);
1678 }
1679}
1680
1681static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1682 int page_num)
1683{
1684 struct scrub_page *spage = sblock->pagev[page_num];
1685
1686 BUG_ON(spage->page == NULL);
1687 if (spage->io_error) {
1688 void *mapped_buffer = kmap_atomic(spage->page);
1689
1690 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1691 flush_dcache_page(spage->page);
1692 kunmap_atomic(mapped_buffer);
1693 }
1694 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1695}
1696
1697static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1698 struct scrub_page *spage)
1699{
1700 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1701 struct scrub_bio *sbio;
1702 int ret;
1703
1704 mutex_lock(&wr_ctx->wr_lock);
1705again:
1706 if (!wr_ctx->wr_curr_bio) {
1707 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1708 GFP_NOFS);
1709 if (!wr_ctx->wr_curr_bio) {
1710 mutex_unlock(&wr_ctx->wr_lock);
1711 return -ENOMEM;
1712 }
1713 wr_ctx->wr_curr_bio->sctx = sctx;
1714 wr_ctx->wr_curr_bio->page_count = 0;
1715 }
1716 sbio = wr_ctx->wr_curr_bio;
1717 if (sbio->page_count == 0) {
1718 struct bio *bio;
1719
1720 sbio->physical = spage->physical_for_dev_replace;
1721 sbio->logical = spage->logical;
1722 sbio->dev = wr_ctx->tgtdev;
1723 bio = sbio->bio;
1724 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001725 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001726 if (!bio) {
1727 mutex_unlock(&wr_ctx->wr_lock);
1728 return -ENOMEM;
1729 }
1730 sbio->bio = bio;
1731 }
1732
1733 bio->bi_private = sbio;
1734 bio->bi_end_io = scrub_wr_bio_end_io;
1735 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001736 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001737 sbio->err = 0;
1738 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1739 spage->physical_for_dev_replace ||
1740 sbio->logical + sbio->page_count * PAGE_SIZE !=
1741 spage->logical) {
1742 scrub_wr_submit(sctx);
1743 goto again;
1744 }
1745
1746 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1747 if (ret != PAGE_SIZE) {
1748 if (sbio->page_count < 1) {
1749 bio_put(sbio->bio);
1750 sbio->bio = NULL;
1751 mutex_unlock(&wr_ctx->wr_lock);
1752 return -EIO;
1753 }
1754 scrub_wr_submit(sctx);
1755 goto again;
1756 }
1757
1758 sbio->pagev[sbio->page_count] = spage;
1759 scrub_page_get(spage);
1760 sbio->page_count++;
1761 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1762 scrub_wr_submit(sctx);
1763 mutex_unlock(&wr_ctx->wr_lock);
1764
1765 return 0;
1766}
1767
1768static void scrub_wr_submit(struct scrub_ctx *sctx)
1769{
1770 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1771 struct scrub_bio *sbio;
1772
1773 if (!wr_ctx->wr_curr_bio)
1774 return;
1775
1776 sbio = wr_ctx->wr_curr_bio;
1777 wr_ctx->wr_curr_bio = NULL;
1778 WARN_ON(!sbio->bio->bi_bdev);
1779 scrub_pending_bio_inc(sctx);
1780 /* process all writes in a single worker thread. Then the block layer
1781 * orders the requests before sending them to the driver which
1782 * doubled the write performance on spinning disks when measured
1783 * with Linux 3.5 */
1784 btrfsic_submit_bio(WRITE, sbio->bio);
1785}
1786
1787static void scrub_wr_bio_end_io(struct bio *bio, int err)
1788{
1789 struct scrub_bio *sbio = bio->bi_private;
1790 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1791
1792 sbio->err = err;
1793 sbio->bio = bio;
1794
Liu Bo9e0af232014-08-15 23:36:53 +08001795 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1796 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001797 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001798}
1799
1800static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1801{
1802 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1803 struct scrub_ctx *sctx = sbio->sctx;
1804 int i;
1805
1806 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1807 if (sbio->err) {
1808 struct btrfs_dev_replace *dev_replace =
1809 &sbio->sctx->dev_root->fs_info->dev_replace;
1810
1811 for (i = 0; i < sbio->page_count; i++) {
1812 struct scrub_page *spage = sbio->pagev[i];
1813
1814 spage->io_error = 1;
1815 btrfs_dev_replace_stats_inc(&dev_replace->
1816 num_write_errors);
1817 }
1818 }
1819
1820 for (i = 0; i < sbio->page_count; i++)
1821 scrub_page_put(sbio->pagev[i]);
1822
1823 bio_put(sbio->bio);
1824 kfree(sbio);
1825 scrub_pending_bio_dec(sctx);
1826}
1827
1828static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001829{
1830 u64 flags;
1831 int ret;
1832
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001833 WARN_ON(sblock->page_count < 1);
1834 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001835 ret = 0;
1836 if (flags & BTRFS_EXTENT_FLAG_DATA)
1837 ret = scrub_checksum_data(sblock);
1838 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1839 ret = scrub_checksum_tree_block(sblock);
1840 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1841 (void)scrub_checksum_super(sblock);
1842 else
1843 WARN_ON(1);
1844 if (ret)
1845 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001846
1847 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001848}
1849
1850static int scrub_checksum_data(struct scrub_block *sblock)
1851{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001852 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001853 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001854 u8 *on_disk_csum;
1855 struct page *page;
1856 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001857 u32 crc = ~(u32)0;
1858 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001859 u64 len;
1860 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001861
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001862 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001863 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001864 return 0;
1865
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001866 on_disk_csum = sblock->pagev[0]->csum;
1867 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001868 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001869
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001870 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001871 index = 0;
1872 for (;;) {
1873 u64 l = min_t(u64, len, PAGE_SIZE);
1874
Liu Bob0496682013-03-14 14:57:45 +00001875 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001876 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001877 len -= l;
1878 if (len == 0)
1879 break;
1880 index++;
1881 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001882 BUG_ON(!sblock->pagev[index]->page);
1883 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001884 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001885 }
1886
Arne Jansena2de7332011-03-08 14:14:00 +01001887 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001888 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001889 fail = 1;
1890
Arne Jansena2de7332011-03-08 14:14:00 +01001891 return fail;
1892}
1893
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001894static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001895{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001896 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001897 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001898 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001899 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001900 u8 calculated_csum[BTRFS_CSUM_SIZE];
1901 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1902 struct page *page;
1903 void *mapped_buffer;
1904 u64 mapped_size;
1905 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001906 u32 crc = ~(u32)0;
1907 int fail = 0;
1908 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001909 u64 len;
1910 int index;
1911
1912 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001913 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001914 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001915 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001916 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001917
1918 /*
1919 * we don't use the getter functions here, as we
1920 * a) don't have an extent buffer and
1921 * b) the page is already kmapped
1922 */
Arne Jansena2de7332011-03-08 14:14:00 +01001923
Qu Wenruo3cae2102013-07-16 11:19:18 +08001924 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001925 ++fail;
1926
Qu Wenruo3cae2102013-07-16 11:19:18 +08001927 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001928 ++fail;
1929
Miao Xie17a9be22014-07-24 11:37:08 +08001930 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001931 ++fail;
1932
1933 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1934 BTRFS_UUID_SIZE))
1935 ++fail;
1936
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001937 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001938 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1939 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1940 index = 0;
1941 for (;;) {
1942 u64 l = min_t(u64, len, mapped_size);
1943
Liu Bob0496682013-03-14 14:57:45 +00001944 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001945 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001946 len -= l;
1947 if (len == 0)
1948 break;
1949 index++;
1950 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001951 BUG_ON(!sblock->pagev[index]->page);
1952 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001953 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001954 mapped_size = PAGE_SIZE;
1955 p = mapped_buffer;
1956 }
1957
1958 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001959 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001960 ++crc_fail;
1961
Arne Jansena2de7332011-03-08 14:14:00 +01001962 return fail || crc_fail;
1963}
1964
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001965static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001966{
1967 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001968 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001969 u8 calculated_csum[BTRFS_CSUM_SIZE];
1970 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1971 struct page *page;
1972 void *mapped_buffer;
1973 u64 mapped_size;
1974 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001975 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001976 int fail_gen = 0;
1977 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001978 u64 len;
1979 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001980
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001981 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001982 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001983 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001984 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001985 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001986
Qu Wenruo3cae2102013-07-16 11:19:18 +08001987 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001988 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001989
Qu Wenruo3cae2102013-07-16 11:19:18 +08001990 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001991 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001992
Miao Xie17a9be22014-07-24 11:37:08 +08001993 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001994 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001995
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001996 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1997 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1998 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1999 index = 0;
2000 for (;;) {
2001 u64 l = min_t(u64, len, mapped_size);
2002
Liu Bob0496682013-03-14 14:57:45 +00002003 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002004 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002005 len -= l;
2006 if (len == 0)
2007 break;
2008 index++;
2009 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002010 BUG_ON(!sblock->pagev[index]->page);
2011 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002012 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002013 mapped_size = PAGE_SIZE;
2014 p = mapped_buffer;
2015 }
2016
2017 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002018 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002019 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002020
Stefan Behrens442a4f62012-05-25 16:06:08 +02002021 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002022 /*
2023 * if we find an error in a super block, we just report it.
2024 * They will get written with the next transaction commit
2025 * anyway
2026 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002027 spin_lock(&sctx->stat_lock);
2028 ++sctx->stat.super_errors;
2029 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002030 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002031 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002032 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2033 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002034 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002035 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002036 }
2037
Stefan Behrens442a4f62012-05-25 16:06:08 +02002038 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002039}
2040
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002041static void scrub_block_get(struct scrub_block *sblock)
2042{
Zhao Lei57019342015-01-20 15:11:45 +08002043 atomic_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002044}
2045
2046static void scrub_block_put(struct scrub_block *sblock)
2047{
Zhao Lei57019342015-01-20 15:11:45 +08002048 if (atomic_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002049 int i;
2050
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002051 if (sblock->sparity)
2052 scrub_parity_put(sblock->sparity);
2053
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002054 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002055 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002056 kfree(sblock);
2057 }
2058}
2059
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002060static void scrub_page_get(struct scrub_page *spage)
2061{
Zhao Lei57019342015-01-20 15:11:45 +08002062 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002063}
2064
2065static void scrub_page_put(struct scrub_page *spage)
2066{
Zhao Lei57019342015-01-20 15:11:45 +08002067 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002068 if (spage->page)
2069 __free_page(spage->page);
2070 kfree(spage);
2071 }
2072}
2073
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002074static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002075{
2076 struct scrub_bio *sbio;
2077
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002078 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002079 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002080
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002081 sbio = sctx->bios[sctx->curr];
2082 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002083 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002084
Stefan Behrensff023aa2012-11-06 11:43:11 +01002085 if (!sbio->bio->bi_bdev) {
2086 /*
2087 * this case should not happen. If btrfs_map_block() is
2088 * wrong, it could happen for dev-replace operations on
2089 * missing devices when no mirrors are available, but in
2090 * this case it should already fail the mount.
2091 * This case is handled correctly (but _very_ slowly).
2092 */
2093 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05002094 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01002095 bio_endio(sbio->bio, -EIO);
2096 } else {
2097 btrfsic_submit_bio(READ, sbio->bio);
2098 }
Arne Jansena2de7332011-03-08 14:14:00 +01002099}
2100
Stefan Behrensff023aa2012-11-06 11:43:11 +01002101static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2102 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002103{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002104 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002105 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002106 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002107
2108again:
2109 /*
2110 * grab a fresh bio or wait for one to become available
2111 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002112 while (sctx->curr == -1) {
2113 spin_lock(&sctx->list_lock);
2114 sctx->curr = sctx->first_free;
2115 if (sctx->curr != -1) {
2116 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2117 sctx->bios[sctx->curr]->next_free = -1;
2118 sctx->bios[sctx->curr]->page_count = 0;
2119 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002120 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002121 spin_unlock(&sctx->list_lock);
2122 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002123 }
2124 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002125 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002126 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002127 struct bio *bio;
2128
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002129 sbio->physical = spage->physical;
2130 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002131 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002132 bio = sbio->bio;
2133 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002134 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002135 if (!bio)
2136 return -ENOMEM;
2137 sbio->bio = bio;
2138 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002139
2140 bio->bi_private = sbio;
2141 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002142 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002143 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002144 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002145 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2146 spage->physical ||
2147 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002148 spage->logical ||
2149 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002150 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002151 goto again;
2152 }
2153
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002154 sbio->pagev[sbio->page_count] = spage;
2155 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2156 if (ret != PAGE_SIZE) {
2157 if (sbio->page_count < 1) {
2158 bio_put(sbio->bio);
2159 sbio->bio = NULL;
2160 return -EIO;
2161 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002162 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002163 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002164 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002165
Stefan Behrensff023aa2012-11-06 11:43:11 +01002166 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002167 atomic_inc(&sblock->outstanding_pages);
2168 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002169 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002170 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002171
2172 return 0;
2173}
2174
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002175static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002176 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002177 u64 gen, int mirror_num, u8 *csum, int force,
2178 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002179{
2180 struct scrub_block *sblock;
2181 int index;
2182
2183 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2184 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002185 spin_lock(&sctx->stat_lock);
2186 sctx->stat.malloc_errors++;
2187 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002188 return -ENOMEM;
2189 }
2190
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002191 /* one ref inside this function, plus one for each page added to
2192 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002193 atomic_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002194 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002195 sblock->no_io_error_seen = 1;
2196
2197 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002198 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002199 u64 l = min_t(u64, len, PAGE_SIZE);
2200
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002201 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2202 if (!spage) {
2203leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002204 spin_lock(&sctx->stat_lock);
2205 sctx->stat.malloc_errors++;
2206 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002207 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002208 return -ENOMEM;
2209 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002210 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2211 scrub_page_get(spage);
2212 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002213 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002214 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002215 spage->flags = flags;
2216 spage->generation = gen;
2217 spage->logical = logical;
2218 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002219 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002220 spage->mirror_num = mirror_num;
2221 if (csum) {
2222 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002223 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002224 } else {
2225 spage->have_csum = 0;
2226 }
2227 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002228 spage->page = alloc_page(GFP_NOFS);
2229 if (!spage->page)
2230 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002231 len -= l;
2232 logical += l;
2233 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002234 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002235 }
2236
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002237 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002238 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002239 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002240 int ret;
2241
Stefan Behrensff023aa2012-11-06 11:43:11 +01002242 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002243 if (ret) {
2244 scrub_block_put(sblock);
2245 return ret;
2246 }
2247 }
2248
2249 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002250 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002251
2252 /* last one frees, either here or in bio completion for last page */
2253 scrub_block_put(sblock);
2254 return 0;
2255}
2256
2257static void scrub_bio_end_io(struct bio *bio, int err)
2258{
2259 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002260 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002261
2262 sbio->err = err;
2263 sbio->bio = bio;
2264
Qu Wenruo0339ef22014-02-28 10:46:17 +08002265 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002266}
2267
2268static void scrub_bio_end_io_worker(struct btrfs_work *work)
2269{
2270 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002271 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002272 int i;
2273
Stefan Behrensff023aa2012-11-06 11:43:11 +01002274 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002275 if (sbio->err) {
2276 for (i = 0; i < sbio->page_count; i++) {
2277 struct scrub_page *spage = sbio->pagev[i];
2278
2279 spage->io_error = 1;
2280 spage->sblock->no_io_error_seen = 0;
2281 }
2282 }
2283
2284 /* now complete the scrub_block items that have all pages completed */
2285 for (i = 0; i < sbio->page_count; i++) {
2286 struct scrub_page *spage = sbio->pagev[i];
2287 struct scrub_block *sblock = spage->sblock;
2288
2289 if (atomic_dec_and_test(&sblock->outstanding_pages))
2290 scrub_block_complete(sblock);
2291 scrub_block_put(sblock);
2292 }
2293
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002294 bio_put(sbio->bio);
2295 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002296 spin_lock(&sctx->list_lock);
2297 sbio->next_free = sctx->first_free;
2298 sctx->first_free = sbio->index;
2299 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002300
2301 if (sctx->is_dev_replace &&
2302 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2303 mutex_lock(&sctx->wr_ctx.wr_lock);
2304 scrub_wr_submit(sctx);
2305 mutex_unlock(&sctx->wr_ctx.wr_lock);
2306 }
2307
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002308 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002309}
2310
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002311static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2312 unsigned long *bitmap,
2313 u64 start, u64 len)
2314{
2315 int offset;
2316 int nsectors;
2317 int sectorsize = sparity->sctx->dev_root->sectorsize;
2318
2319 if (len >= sparity->stripe_len) {
2320 bitmap_set(bitmap, 0, sparity->nsectors);
2321 return;
2322 }
2323
2324 start -= sparity->logic_start;
2325 offset = (int)do_div(start, sparity->stripe_len);
2326 offset /= sectorsize;
2327 nsectors = (int)len / sectorsize;
2328
2329 if (offset + nsectors <= sparity->nsectors) {
2330 bitmap_set(bitmap, offset, nsectors);
2331 return;
2332 }
2333
2334 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2335 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2336}
2337
2338static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2339 u64 start, u64 len)
2340{
2341 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2342}
2343
2344static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2345 u64 start, u64 len)
2346{
2347 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2348}
2349
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002350static void scrub_block_complete(struct scrub_block *sblock)
2351{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002352 int corrupted = 0;
2353
Stefan Behrensff023aa2012-11-06 11:43:11 +01002354 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002355 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002356 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002357 } else {
2358 /*
2359 * if has checksum error, write via repair mechanism in
2360 * dev replace case, otherwise write here in dev replace
2361 * case.
2362 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002363 corrupted = scrub_checksum(sblock);
2364 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002365 scrub_write_block_to_dev_replace(sblock);
2366 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002367
2368 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2369 u64 start = sblock->pagev[0]->logical;
2370 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2371 PAGE_SIZE;
2372
2373 scrub_parity_mark_sectors_error(sblock->sparity,
2374 start, end - start);
2375 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002376}
2377
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002378static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002379 u8 *csum)
2380{
2381 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002382 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002383 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002384
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002385 while (!list_empty(&sctx->csum_list)) {
2386 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002387 struct btrfs_ordered_sum, list);
2388 if (sum->bytenr > logical)
2389 return 0;
2390 if (sum->bytenr + sum->len > logical)
2391 break;
2392
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002393 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002394 list_del(&sum->list);
2395 kfree(sum);
2396 sum = NULL;
2397 }
2398 if (!sum)
2399 return 0;
2400
Miao Xief51a4a12013-06-19 10:36:09 +08002401 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002402 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002403 memcpy(csum, sum->sums + index, sctx->csum_size);
2404 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002405 list_del(&sum->list);
2406 kfree(sum);
2407 }
Miao Xief51a4a12013-06-19 10:36:09 +08002408 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002409}
2410
2411/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002412static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002413 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002414 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002415{
2416 int ret;
2417 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002418 u32 blocksize;
2419
2420 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002421 blocksize = sctx->sectorsize;
2422 spin_lock(&sctx->stat_lock);
2423 sctx->stat.data_extents_scrubbed++;
2424 sctx->stat.data_bytes_scrubbed += len;
2425 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002426 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002427 blocksize = sctx->nodesize;
2428 spin_lock(&sctx->stat_lock);
2429 sctx->stat.tree_extents_scrubbed++;
2430 sctx->stat.tree_bytes_scrubbed += len;
2431 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002432 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002433 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002434 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002435 }
Arne Jansena2de7332011-03-08 14:14:00 +01002436
2437 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002438 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002439 int have_csum = 0;
2440
2441 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2442 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002443 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002444 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002445 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002446 if (sctx->is_dev_replace && !have_csum) {
2447 ret = copy_nocow_pages(sctx, logical, l,
2448 mirror_num,
2449 physical_for_dev_replace);
2450 goto behind_scrub_pages;
2451 }
Arne Jansena2de7332011-03-08 14:14:00 +01002452 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002453 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002454 mirror_num, have_csum ? csum : NULL, 0,
2455 physical_for_dev_replace);
2456behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002457 if (ret)
2458 return ret;
2459 len -= l;
2460 logical += l;
2461 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002462 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002463 }
2464 return 0;
2465}
2466
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002467static int scrub_pages_for_parity(struct scrub_parity *sparity,
2468 u64 logical, u64 len,
2469 u64 physical, struct btrfs_device *dev,
2470 u64 flags, u64 gen, int mirror_num, u8 *csum)
2471{
2472 struct scrub_ctx *sctx = sparity->sctx;
2473 struct scrub_block *sblock;
2474 int index;
2475
2476 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2477 if (!sblock) {
2478 spin_lock(&sctx->stat_lock);
2479 sctx->stat.malloc_errors++;
2480 spin_unlock(&sctx->stat_lock);
2481 return -ENOMEM;
2482 }
2483
2484 /* one ref inside this function, plus one for each page added to
2485 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002486 atomic_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002487 sblock->sctx = sctx;
2488 sblock->no_io_error_seen = 1;
2489 sblock->sparity = sparity;
2490 scrub_parity_get(sparity);
2491
2492 for (index = 0; len > 0; index++) {
2493 struct scrub_page *spage;
2494 u64 l = min_t(u64, len, PAGE_SIZE);
2495
2496 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2497 if (!spage) {
2498leave_nomem:
2499 spin_lock(&sctx->stat_lock);
2500 sctx->stat.malloc_errors++;
2501 spin_unlock(&sctx->stat_lock);
2502 scrub_block_put(sblock);
2503 return -ENOMEM;
2504 }
2505 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2506 /* For scrub block */
2507 scrub_page_get(spage);
2508 sblock->pagev[index] = spage;
2509 /* For scrub parity */
2510 scrub_page_get(spage);
2511 list_add_tail(&spage->list, &sparity->spages);
2512 spage->sblock = sblock;
2513 spage->dev = dev;
2514 spage->flags = flags;
2515 spage->generation = gen;
2516 spage->logical = logical;
2517 spage->physical = physical;
2518 spage->mirror_num = mirror_num;
2519 if (csum) {
2520 spage->have_csum = 1;
2521 memcpy(spage->csum, csum, sctx->csum_size);
2522 } else {
2523 spage->have_csum = 0;
2524 }
2525 sblock->page_count++;
2526 spage->page = alloc_page(GFP_NOFS);
2527 if (!spage->page)
2528 goto leave_nomem;
2529 len -= l;
2530 logical += l;
2531 physical += l;
2532 }
2533
2534 WARN_ON(sblock->page_count == 0);
2535 for (index = 0; index < sblock->page_count; index++) {
2536 struct scrub_page *spage = sblock->pagev[index];
2537 int ret;
2538
2539 ret = scrub_add_page_to_rd_bio(sctx, spage);
2540 if (ret) {
2541 scrub_block_put(sblock);
2542 return ret;
2543 }
2544 }
2545
2546 /* last one frees, either here or in bio completion for last page */
2547 scrub_block_put(sblock);
2548 return 0;
2549}
2550
2551static int scrub_extent_for_parity(struct scrub_parity *sparity,
2552 u64 logical, u64 len,
2553 u64 physical, struct btrfs_device *dev,
2554 u64 flags, u64 gen, int mirror_num)
2555{
2556 struct scrub_ctx *sctx = sparity->sctx;
2557 int ret;
2558 u8 csum[BTRFS_CSUM_SIZE];
2559 u32 blocksize;
2560
2561 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2562 blocksize = sctx->sectorsize;
2563 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2564 blocksize = sctx->nodesize;
2565 } else {
2566 blocksize = sctx->sectorsize;
2567 WARN_ON(1);
2568 }
2569
2570 while (len) {
2571 u64 l = min_t(u64, len, blocksize);
2572 int have_csum = 0;
2573
2574 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2575 /* push csums to sbio */
2576 have_csum = scrub_find_csum(sctx, logical, l, csum);
2577 if (have_csum == 0)
2578 goto skip;
2579 }
2580 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2581 flags, gen, mirror_num,
2582 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002583 if (ret)
2584 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002585skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002586 len -= l;
2587 logical += l;
2588 physical += l;
2589 }
2590 return 0;
2591}
2592
Wang Shilong3b080b22014-04-01 18:01:43 +08002593/*
2594 * Given a physical address, this will calculate it's
2595 * logical offset. if this is a parity stripe, it will return
2596 * the most left data stripe's logical offset.
2597 *
2598 * return 0 if it is a data stripe, 1 means parity stripe.
2599 */
2600static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002601 struct map_lookup *map, u64 *offset,
2602 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002603{
2604 int i;
2605 int j = 0;
2606 u64 stripe_nr;
2607 u64 last_offset;
2608 int stripe_index;
2609 int rot;
2610
2611 last_offset = (physical - map->stripes[num].physical) *
2612 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002613 if (stripe_start)
2614 *stripe_start = last_offset;
2615
Wang Shilong3b080b22014-04-01 18:01:43 +08002616 *offset = last_offset;
2617 for (i = 0; i < nr_data_stripes(map); i++) {
2618 *offset = last_offset + i * map->stripe_len;
2619
2620 stripe_nr = *offset;
2621 do_div(stripe_nr, map->stripe_len);
2622 do_div(stripe_nr, nr_data_stripes(map));
2623
2624 /* Work out the disk rotation on this stripe-set */
2625 rot = do_div(stripe_nr, map->num_stripes);
2626 /* calculate which stripe this data locates */
2627 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002628 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002629 if (stripe_index == num)
2630 return 0;
2631 if (stripe_index < num)
2632 j++;
2633 }
2634 *offset = last_offset + j * map->stripe_len;
2635 return 1;
2636}
2637
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002638static void scrub_free_parity(struct scrub_parity *sparity)
2639{
2640 struct scrub_ctx *sctx = sparity->sctx;
2641 struct scrub_page *curr, *next;
2642 int nbits;
2643
2644 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2645 if (nbits) {
2646 spin_lock(&sctx->stat_lock);
2647 sctx->stat.read_errors += nbits;
2648 sctx->stat.uncorrectable_errors += nbits;
2649 spin_unlock(&sctx->stat_lock);
2650 }
2651
2652 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2653 list_del_init(&curr->list);
2654 scrub_page_put(curr);
2655 }
2656
2657 kfree(sparity);
2658}
2659
2660static void scrub_parity_bio_endio(struct bio *bio, int error)
2661{
2662 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2663 struct scrub_ctx *sctx = sparity->sctx;
2664
2665 if (error)
2666 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2667 sparity->nsectors);
2668
2669 scrub_free_parity(sparity);
2670 scrub_pending_bio_dec(sctx);
2671 bio_put(bio);
2672}
2673
2674static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2675{
2676 struct scrub_ctx *sctx = sparity->sctx;
2677 struct bio *bio;
2678 struct btrfs_raid_bio *rbio;
2679 struct scrub_page *spage;
2680 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002681 u64 length;
2682 int ret;
2683
2684 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2685 sparity->nsectors))
2686 goto out;
2687
2688 length = sparity->logic_end - sparity->logic_start + 1;
Miao Xie76035972014-11-14 17:45:42 +08002689 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002690 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002691 &length, &bbio, 0, 1);
2692 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002693 goto bbio_out;
2694
2695 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2696 if (!bio)
2697 goto bbio_out;
2698
2699 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2700 bio->bi_private = sparity;
2701 bio->bi_end_io = scrub_parity_bio_endio;
2702
2703 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002704 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002705 sparity->dbitmap,
2706 sparity->nsectors);
2707 if (!rbio)
2708 goto rbio_out;
2709
2710 list_for_each_entry(spage, &sparity->spages, list)
2711 raid56_parity_add_scrub_pages(rbio, spage->page,
2712 spage->logical);
2713
2714 scrub_pending_bio_inc(sctx);
2715 raid56_parity_submit_scrub_rbio(rbio);
2716 return;
2717
2718rbio_out:
2719 bio_put(bio);
2720bbio_out:
Zhao Lei6e9606d2015-01-20 15:11:34 +08002721 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002722 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2723 sparity->nsectors);
2724 spin_lock(&sctx->stat_lock);
2725 sctx->stat.malloc_errors++;
2726 spin_unlock(&sctx->stat_lock);
2727out:
2728 scrub_free_parity(sparity);
2729}
2730
2731static inline int scrub_calc_parity_bitmap_len(int nsectors)
2732{
2733 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2734}
2735
2736static void scrub_parity_get(struct scrub_parity *sparity)
2737{
Zhao Lei57019342015-01-20 15:11:45 +08002738 atomic_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002739}
2740
2741static void scrub_parity_put(struct scrub_parity *sparity)
2742{
Zhao Lei57019342015-01-20 15:11:45 +08002743 if (!atomic_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002744 return;
2745
2746 scrub_parity_check_and_repair(sparity);
2747}
2748
2749static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2750 struct map_lookup *map,
2751 struct btrfs_device *sdev,
2752 struct btrfs_path *path,
2753 u64 logic_start,
2754 u64 logic_end)
2755{
2756 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2757 struct btrfs_root *root = fs_info->extent_root;
2758 struct btrfs_root *csum_root = fs_info->csum_root;
2759 struct btrfs_extent_item *extent;
2760 u64 flags;
2761 int ret;
2762 int slot;
2763 struct extent_buffer *l;
2764 struct btrfs_key key;
2765 u64 generation;
2766 u64 extent_logical;
2767 u64 extent_physical;
2768 u64 extent_len;
2769 struct btrfs_device *extent_dev;
2770 struct scrub_parity *sparity;
2771 int nsectors;
2772 int bitmap_len;
2773 int extent_mirror_num;
2774 int stop_loop = 0;
2775
2776 nsectors = map->stripe_len / root->sectorsize;
2777 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2778 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2779 GFP_NOFS);
2780 if (!sparity) {
2781 spin_lock(&sctx->stat_lock);
2782 sctx->stat.malloc_errors++;
2783 spin_unlock(&sctx->stat_lock);
2784 return -ENOMEM;
2785 }
2786
2787 sparity->stripe_len = map->stripe_len;
2788 sparity->nsectors = nsectors;
2789 sparity->sctx = sctx;
2790 sparity->scrub_dev = sdev;
2791 sparity->logic_start = logic_start;
2792 sparity->logic_end = logic_end;
Zhao Lei57019342015-01-20 15:11:45 +08002793 atomic_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002794 INIT_LIST_HEAD(&sparity->spages);
2795 sparity->dbitmap = sparity->bitmap;
2796 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2797
2798 ret = 0;
2799 while (logic_start < logic_end) {
2800 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2801 key.type = BTRFS_METADATA_ITEM_KEY;
2802 else
2803 key.type = BTRFS_EXTENT_ITEM_KEY;
2804 key.objectid = logic_start;
2805 key.offset = (u64)-1;
2806
2807 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2808 if (ret < 0)
2809 goto out;
2810
2811 if (ret > 0) {
2812 ret = btrfs_previous_extent_item(root, path, 0);
2813 if (ret < 0)
2814 goto out;
2815 if (ret > 0) {
2816 btrfs_release_path(path);
2817 ret = btrfs_search_slot(NULL, root, &key,
2818 path, 0, 0);
2819 if (ret < 0)
2820 goto out;
2821 }
2822 }
2823
2824 stop_loop = 0;
2825 while (1) {
2826 u64 bytes;
2827
2828 l = path->nodes[0];
2829 slot = path->slots[0];
2830 if (slot >= btrfs_header_nritems(l)) {
2831 ret = btrfs_next_leaf(root, path);
2832 if (ret == 0)
2833 continue;
2834 if (ret < 0)
2835 goto out;
2836
2837 stop_loop = 1;
2838 break;
2839 }
2840 btrfs_item_key_to_cpu(l, &key, slot);
2841
2842 if (key.type == BTRFS_METADATA_ITEM_KEY)
2843 bytes = root->nodesize;
2844 else
2845 bytes = key.offset;
2846
2847 if (key.objectid + bytes <= logic_start)
2848 goto next;
2849
2850 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2851 key.type != BTRFS_METADATA_ITEM_KEY)
2852 goto next;
2853
2854 if (key.objectid > logic_end) {
2855 stop_loop = 1;
2856 break;
2857 }
2858
2859 while (key.objectid >= logic_start + map->stripe_len)
2860 logic_start += map->stripe_len;
2861
2862 extent = btrfs_item_ptr(l, slot,
2863 struct btrfs_extent_item);
2864 flags = btrfs_extent_flags(l, extent);
2865 generation = btrfs_extent_generation(l, extent);
2866
2867 if (key.objectid < logic_start &&
2868 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
2869 btrfs_err(fs_info,
2870 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2871 key.objectid, logic_start);
2872 goto next;
2873 }
2874again:
2875 extent_logical = key.objectid;
2876 extent_len = bytes;
2877
2878 if (extent_logical < logic_start) {
2879 extent_len -= logic_start - extent_logical;
2880 extent_logical = logic_start;
2881 }
2882
2883 if (extent_logical + extent_len >
2884 logic_start + map->stripe_len)
2885 extent_len = logic_start + map->stripe_len -
2886 extent_logical;
2887
2888 scrub_parity_mark_sectors_data(sparity, extent_logical,
2889 extent_len);
2890
2891 scrub_remap_extent(fs_info, extent_logical,
2892 extent_len, &extent_physical,
2893 &extent_dev,
2894 &extent_mirror_num);
2895
2896 ret = btrfs_lookup_csums_range(csum_root,
2897 extent_logical,
2898 extent_logical + extent_len - 1,
2899 &sctx->csum_list, 1);
2900 if (ret)
2901 goto out;
2902
2903 ret = scrub_extent_for_parity(sparity, extent_logical,
2904 extent_len,
2905 extent_physical,
2906 extent_dev, flags,
2907 generation,
2908 extent_mirror_num);
2909 if (ret)
2910 goto out;
2911
2912 scrub_free_csums(sctx);
2913 if (extent_logical + extent_len <
2914 key.objectid + bytes) {
2915 logic_start += map->stripe_len;
2916
2917 if (logic_start >= logic_end) {
2918 stop_loop = 1;
2919 break;
2920 }
2921
2922 if (logic_start < key.objectid + bytes) {
2923 cond_resched();
2924 goto again;
2925 }
2926 }
2927next:
2928 path->slots[0]++;
2929 }
2930
2931 btrfs_release_path(path);
2932
2933 if (stop_loop)
2934 break;
2935
2936 logic_start += map->stripe_len;
2937 }
2938out:
2939 if (ret < 0)
2940 scrub_parity_mark_sectors_error(sparity, logic_start,
2941 logic_end - logic_start + 1);
2942 scrub_parity_put(sparity);
2943 scrub_submit(sctx);
2944 mutex_lock(&sctx->wr_ctx.wr_lock);
2945 scrub_wr_submit(sctx);
2946 mutex_unlock(&sctx->wr_ctx.wr_lock);
2947
2948 btrfs_release_path(path);
2949 return ret < 0 ? ret : 0;
2950}
2951
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002952static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002953 struct map_lookup *map,
2954 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002955 int num, u64 base, u64 length,
2956 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002957{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002958 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002959 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002960 struct btrfs_root *root = fs_info->extent_root;
2961 struct btrfs_root *csum_root = fs_info->csum_root;
2962 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002963 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002964 u64 flags;
2965 int ret;
2966 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002967 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002968 struct extent_buffer *l;
2969 struct btrfs_key key;
2970 u64 physical;
2971 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002972 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08002973 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002974 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002975 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002976 struct reada_control *reada1;
2977 struct reada_control *reada2;
2978 struct btrfs_key key_start;
2979 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002980 u64 increment = map->stripe_len;
2981 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002982 u64 extent_logical;
2983 u64 extent_physical;
2984 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002985 u64 stripe_logical;
2986 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002987 struct btrfs_device *extent_dev;
2988 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08002989 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05002990
Arne Jansena2de7332011-03-08 14:14:00 +01002991 nstripes = length;
Wang Shilong3b080b22014-04-01 18:01:43 +08002992 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01002993 offset = 0;
2994 do_div(nstripes, map->stripe_len);
2995 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2996 offset = map->stripe_len * num;
2997 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02002998 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002999 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3000 int factor = map->num_stripes / map->sub_stripes;
3001 offset = map->stripe_len * (num / map->sub_stripes);
3002 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003003 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003004 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3005 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003006 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003007 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3008 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003009 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003010 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003011 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003012 increment = map->stripe_len * nr_data_stripes(map);
3013 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003014 } else {
3015 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003016 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003017 }
3018
3019 path = btrfs_alloc_path();
3020 if (!path)
3021 return -ENOMEM;
3022
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003023 ppath = btrfs_alloc_path();
3024 if (!ppath) {
3025 btrfs_free_path(ppath);
3026 return -ENOMEM;
3027 }
3028
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003029 /*
3030 * work on commit root. The related disk blocks are static as
3031 * long as COW is applied. This means, it is save to rewrite
3032 * them to repair disk errors without any race conditions
3033 */
Arne Jansena2de7332011-03-08 14:14:00 +01003034 path->search_commit_root = 1;
3035 path->skip_locking = 1;
3036
3037 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003038 * trigger the readahead for extent tree csum tree and wait for
3039 * completion. During readahead, the scrub is officially paused
3040 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003041 */
3042 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003043 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003044 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003045 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003046 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003047 logic_end += base;
3048 } else {
3049 logic_end = logical + increment * nstripes;
3050 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003051 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003052 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003053 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003054
Arne Jansen7a262852011-06-10 12:39:23 +02003055 /* FIXME it might be better to start readahead at commit root */
3056 key_start.objectid = logical;
3057 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3058 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003059 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003060 key_end.type = BTRFS_METADATA_ITEM_KEY;
3061 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003062 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003063
Arne Jansen7a262852011-06-10 12:39:23 +02003064 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3065 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3066 key_start.offset = logical;
3067 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3068 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003069 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003070 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003071
Arne Jansen7a262852011-06-10 12:39:23 +02003072 if (!IS_ERR(reada1))
3073 btrfs_reada_wait(reada1);
3074 if (!IS_ERR(reada2))
3075 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003076
Arne Jansena2de7332011-03-08 14:14:00 +01003077
3078 /*
3079 * collect all data csums for the stripe to avoid seeking during
3080 * the scrub. This might currently (crc32) end up to be about 1MB
3081 */
Arne Jansene7786c32011-05-28 20:58:38 +00003082 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003083
Arne Jansena2de7332011-03-08 14:14:00 +01003084 /*
3085 * now find all extents for each stripe and scrub them
3086 */
Arne Jansena2de7332011-03-08 14:14:00 +01003087 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003088 while (physical < physical_end) {
3089 /* for raid56, we skip parity stripe */
Zhao Leiffe2d202015-01-20 15:11:44 +08003090 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003091 ret = get_raid56_logic_offset(physical, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003092 map, &logical, &stripe_logical);
Wang Shilong3b080b22014-04-01 18:01:43 +08003093 logical += base;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003094 if (ret) {
3095 stripe_logical += base;
3096 stripe_end = stripe_logical + increment - 1;
3097 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3098 ppath, stripe_logical,
3099 stripe_end);
3100 if (ret)
3101 goto out;
Wang Shilong3b080b22014-04-01 18:01:43 +08003102 goto skip;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003103 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003104 }
Arne Jansena2de7332011-03-08 14:14:00 +01003105 /*
3106 * canceled?
3107 */
3108 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003109 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003110 ret = -ECANCELED;
3111 goto out;
3112 }
3113 /*
3114 * check to see if we have to pause
3115 */
3116 if (atomic_read(&fs_info->scrub_pause_req)) {
3117 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003118 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003119 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003120 mutex_lock(&sctx->wr_ctx.wr_lock);
3121 scrub_wr_submit(sctx);
3122 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003123 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003124 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003125 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003126 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003127 }
3128
Wang Shilong7c76edb2014-01-12 21:38:32 +08003129 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3130 key.type = BTRFS_METADATA_ITEM_KEY;
3131 else
3132 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003133 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003134 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003135
3136 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3137 if (ret < 0)
3138 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003139
Arne Jansen8c510322011-06-03 10:09:26 +02003140 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003141 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003142 if (ret < 0)
3143 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003144 if (ret > 0) {
3145 /* there's no smaller item, so stick with the
3146 * larger one */
3147 btrfs_release_path(path);
3148 ret = btrfs_search_slot(NULL, root, &key,
3149 path, 0, 0);
3150 if (ret < 0)
3151 goto out;
3152 }
Arne Jansena2de7332011-03-08 14:14:00 +01003153 }
3154
Liu Bo625f1c8d2013-04-27 02:56:57 +00003155 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003156 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003157 u64 bytes;
3158
Arne Jansena2de7332011-03-08 14:14:00 +01003159 l = path->nodes[0];
3160 slot = path->slots[0];
3161 if (slot >= btrfs_header_nritems(l)) {
3162 ret = btrfs_next_leaf(root, path);
3163 if (ret == 0)
3164 continue;
3165 if (ret < 0)
3166 goto out;
3167
Liu Bo625f1c8d2013-04-27 02:56:57 +00003168 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003169 break;
3170 }
3171 btrfs_item_key_to_cpu(l, &key, slot);
3172
Josef Bacik3173a182013-03-07 14:22:04 -05003173 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003174 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003175 else
3176 bytes = key.offset;
3177
3178 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003179 goto next;
3180
Liu Bo625f1c8d2013-04-27 02:56:57 +00003181 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3182 key.type != BTRFS_METADATA_ITEM_KEY)
3183 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01003184
Liu Bo625f1c8d2013-04-27 02:56:57 +00003185 if (key.objectid >= logical + map->stripe_len) {
3186 /* out of this device extent */
3187 if (key.objectid >= logic_end)
3188 stop_loop = 1;
3189 break;
3190 }
Arne Jansena2de7332011-03-08 14:14:00 +01003191
3192 extent = btrfs_item_ptr(l, slot,
3193 struct btrfs_extent_item);
3194 flags = btrfs_extent_flags(l, extent);
3195 generation = btrfs_extent_generation(l, extent);
3196
3197 if (key.objectid < logical &&
3198 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003199 btrfs_err(fs_info,
3200 "scrub: tree block %llu spanning "
3201 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003202 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01003203 goto next;
3204 }
3205
Liu Bo625f1c8d2013-04-27 02:56:57 +00003206again:
3207 extent_logical = key.objectid;
3208 extent_len = bytes;
3209
Arne Jansena2de7332011-03-08 14:14:00 +01003210 /*
3211 * trim extent to this stripe
3212 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003213 if (extent_logical < logical) {
3214 extent_len -= logical - extent_logical;
3215 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003216 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003217 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003218 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003219 extent_len = logical + map->stripe_len -
3220 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003221 }
3222
Liu Bo625f1c8d2013-04-27 02:56:57 +00003223 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003224 extent_dev = scrub_dev;
3225 extent_mirror_num = mirror_num;
3226 if (is_dev_replace)
3227 scrub_remap_extent(fs_info, extent_logical,
3228 extent_len, &extent_physical,
3229 &extent_dev,
3230 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003231
3232 ret = btrfs_lookup_csums_range(csum_root, logical,
3233 logical + map->stripe_len - 1,
3234 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003235 if (ret)
3236 goto out;
3237
Liu Bo625f1c8d2013-04-27 02:56:57 +00003238 ret = scrub_extent(sctx, extent_logical, extent_len,
3239 extent_physical, extent_dev, flags,
3240 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003241 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003242 if (ret)
3243 goto out;
3244
Josef Bacikd88d46c2013-06-10 12:59:04 +00003245 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003246 if (extent_logical + extent_len <
3247 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003248 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003249 /*
3250 * loop until we find next data stripe
3251 * or we have finished all stripes.
3252 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003253loop:
3254 physical += map->stripe_len;
3255 ret = get_raid56_logic_offset(physical,
3256 num, map, &logical,
3257 &stripe_logical);
3258 logical += base;
3259
3260 if (ret && physical < physical_end) {
3261 stripe_logical += base;
3262 stripe_end = stripe_logical +
3263 increment - 1;
3264 ret = scrub_raid56_parity(sctx,
3265 map, scrub_dev, ppath,
3266 stripe_logical,
3267 stripe_end);
3268 if (ret)
3269 goto out;
3270 goto loop;
3271 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003272 } else {
3273 physical += map->stripe_len;
3274 logical += increment;
3275 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003276 if (logical < key.objectid + bytes) {
3277 cond_resched();
3278 goto again;
3279 }
3280
Wang Shilong3b080b22014-04-01 18:01:43 +08003281 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003282 stop_loop = 1;
3283 break;
3284 }
3285 }
Arne Jansena2de7332011-03-08 14:14:00 +01003286next:
3287 path->slots[0]++;
3288 }
Chris Mason71267332011-05-23 06:30:52 -04003289 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003290skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003291 logical += increment;
3292 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003293 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003294 if (stop_loop)
3295 sctx->stat.last_physical = map->stripes[num].physical +
3296 length;
3297 else
3298 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003299 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003300 if (stop_loop)
3301 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003302 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003303out:
Arne Jansena2de7332011-03-08 14:14:00 +01003304 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003305 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003306 mutex_lock(&sctx->wr_ctx.wr_lock);
3307 scrub_wr_submit(sctx);
3308 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003309
Arne Jansene7786c32011-05-28 20:58:38 +00003310 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003311 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003312 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003313 return ret < 0 ? ret : 0;
3314}
3315
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003316static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003317 struct btrfs_device *scrub_dev,
3318 u64 chunk_tree, u64 chunk_objectid,
3319 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003320 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003321{
3322 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003323 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003324 struct map_lookup *map;
3325 struct extent_map *em;
3326 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003327 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003328
3329 read_lock(&map_tree->map_tree.lock);
3330 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3331 read_unlock(&map_tree->map_tree.lock);
3332
3333 if (!em)
3334 return -EINVAL;
3335
3336 map = (struct map_lookup *)em->bdev;
3337 if (em->start != chunk_offset)
3338 goto out;
3339
3340 if (em->len < length)
3341 goto out;
3342
3343 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003344 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003345 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003346 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003347 chunk_offset, length,
3348 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003349 if (ret)
3350 goto out;
3351 }
3352 }
3353out:
3354 free_extent_map(em);
3355
3356 return ret;
3357}
3358
3359static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003360int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003361 struct btrfs_device *scrub_dev, u64 start, u64 end,
3362 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003363{
3364 struct btrfs_dev_extent *dev_extent = NULL;
3365 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003366 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003367 struct btrfs_fs_info *fs_info = root->fs_info;
3368 u64 length;
3369 u64 chunk_tree;
3370 u64 chunk_objectid;
3371 u64 chunk_offset;
3372 int ret;
3373 int slot;
3374 struct extent_buffer *l;
3375 struct btrfs_key key;
3376 struct btrfs_key found_key;
3377 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003378 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003379
3380 path = btrfs_alloc_path();
3381 if (!path)
3382 return -ENOMEM;
3383
3384 path->reada = 2;
3385 path->search_commit_root = 1;
3386 path->skip_locking = 1;
3387
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003388 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003389 key.offset = 0ull;
3390 key.type = BTRFS_DEV_EXTENT_KEY;
3391
Arne Jansena2de7332011-03-08 14:14:00 +01003392 while (1) {
3393 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3394 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003395 break;
3396 if (ret > 0) {
3397 if (path->slots[0] >=
3398 btrfs_header_nritems(path->nodes[0])) {
3399 ret = btrfs_next_leaf(root, path);
3400 if (ret)
3401 break;
3402 }
3403 }
Arne Jansena2de7332011-03-08 14:14:00 +01003404
3405 l = path->nodes[0];
3406 slot = path->slots[0];
3407
3408 btrfs_item_key_to_cpu(l, &found_key, slot);
3409
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003410 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003411 break;
3412
David Sterba962a2982014-06-04 18:41:45 +02003413 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003414 break;
3415
3416 if (found_key.offset >= end)
3417 break;
3418
3419 if (found_key.offset < key.offset)
3420 break;
3421
3422 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3423 length = btrfs_dev_extent_length(l, dev_extent);
3424
Qu Wenruoced96ed2014-06-19 10:42:51 +08003425 if (found_key.offset + length <= start)
3426 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003427
3428 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3429 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3430 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3431
3432 /*
3433 * get a reference on the corresponding block group to prevent
3434 * the chunk from going away while we scrub it
3435 */
3436 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003437
3438 /* some chunks are removed but not committed to disk yet,
3439 * continue scrubbing */
3440 if (!cache)
3441 goto skip;
3442
Stefan Behrensff023aa2012-11-06 11:43:11 +01003443 dev_replace->cursor_right = found_key.offset + length;
3444 dev_replace->cursor_left = found_key.offset;
3445 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003446 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003447 chunk_offset, length, found_key.offset,
3448 is_dev_replace);
3449
3450 /*
3451 * flush, submit all pending read and write bios, afterwards
3452 * wait for them.
3453 * Note that in the dev replace case, a read request causes
3454 * write requests that are submitted in the read completion
3455 * worker. Therefore in the current situation, it is required
3456 * that all write requests are flushed, so that all read and
3457 * write requests are really completed when bios_in_flight
3458 * changes to 0.
3459 */
3460 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3461 scrub_submit(sctx);
3462 mutex_lock(&sctx->wr_ctx.wr_lock);
3463 scrub_wr_submit(sctx);
3464 mutex_unlock(&sctx->wr_ctx.wr_lock);
3465
3466 wait_event(sctx->list_wait,
3467 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003468 atomic_inc(&fs_info->scrubs_paused);
3469 wake_up(&fs_info->scrub_pause_wait);
3470
3471 /*
3472 * must be called before we decrease @scrub_paused.
3473 * make sure we don't block transaction commit while
3474 * we are waiting pending workers finished.
3475 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003476 wait_event(sctx->list_wait,
3477 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003478 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3479
3480 mutex_lock(&fs_info->scrub_lock);
3481 __scrub_blocked_if_needed(fs_info);
3482 atomic_dec(&fs_info->scrubs_paused);
3483 mutex_unlock(&fs_info->scrub_lock);
3484 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003485
Arne Jansena2de7332011-03-08 14:14:00 +01003486 btrfs_put_block_group(cache);
3487 if (ret)
3488 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003489 if (is_dev_replace &&
3490 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003491 ret = -EIO;
3492 break;
3493 }
3494 if (sctx->stat.malloc_errors > 0) {
3495 ret = -ENOMEM;
3496 break;
3497 }
Arne Jansena2de7332011-03-08 14:14:00 +01003498
Ilya Dryomov539f3582013-10-07 13:42:57 +03003499 dev_replace->cursor_left = dev_replace->cursor_right;
3500 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003501skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003502 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003503 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003504 }
3505
Arne Jansena2de7332011-03-08 14:14:00 +01003506 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003507
3508 /*
3509 * ret can still be 1 from search_slot or next_leaf,
3510 * that's not an error
3511 */
3512 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003513}
3514
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003515static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3516 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003517{
3518 int i;
3519 u64 bytenr;
3520 u64 gen;
3521 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003522 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003523
Miao Xie87533c42013-01-29 10:14:48 +00003524 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003525 return -EIO;
3526
Miao Xie5f546062014-07-24 11:37:09 +08003527 /* Seed devices of a new filesystem has their own generation. */
3528 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3529 gen = scrub_dev->generation;
3530 else
3531 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003532
3533 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3534 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003535 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3536 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003537 break;
3538
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003539 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003540 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003541 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003542 if (ret)
3543 return ret;
3544 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003545 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003546
3547 return 0;
3548}
3549
3550/*
3551 * get a reference count on fs_info->scrub_workers. start worker if necessary
3552 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003553static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3554 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003555{
Josef Bacik0dc3b842011-11-18 14:37:27 -05003556 int ret = 0;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003557 int flags = WQ_FREEZABLE | WQ_UNBOUND;
3558 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003559
Arne Jansen632dd772011-06-10 12:07:07 +02003560 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003561 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003562 fs_info->scrub_workers =
3563 btrfs_alloc_workqueue("btrfs-scrub", flags,
3564 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003565 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003566 fs_info->scrub_workers =
3567 btrfs_alloc_workqueue("btrfs-scrub", flags,
3568 max_active, 4);
3569 if (!fs_info->scrub_workers) {
3570 ret = -ENOMEM;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003571 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003572 }
3573 fs_info->scrub_wr_completion_workers =
3574 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3575 max_active, 2);
3576 if (!fs_info->scrub_wr_completion_workers) {
3577 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003578 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003579 }
3580 fs_info->scrub_nocow_workers =
3581 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
3582 if (!fs_info->scrub_nocow_workers) {
3583 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003584 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003585 }
Arne Jansen632dd772011-06-10 12:07:07 +02003586 }
Arne Jansena2de7332011-03-08 14:14:00 +01003587 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05003588out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05003589 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003590}
3591
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003592static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003593{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003594 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003595 btrfs_destroy_workqueue(fs_info->scrub_workers);
3596 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3597 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003598 }
Arne Jansena2de7332011-03-08 14:14:00 +01003599 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003600}
3601
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003602int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3603 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003604 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003605{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003606 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003607 int ret;
3608 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003609 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003610
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003611 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003612 return -EINVAL;
3613
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003614 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003615 /*
3616 * in this case scrub is unable to calculate the checksum
3617 * the way scrub is implemented. Do not handle this
3618 * situation at all because it won't ever happen.
3619 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003620 btrfs_err(fs_info,
3621 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003622 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003623 return -EINVAL;
3624 }
3625
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003626 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003627 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003628 btrfs_err(fs_info,
3629 "scrub: size assumption sectorsize != PAGE_SIZE "
3630 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003631 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003632 return -EINVAL;
3633 }
3634
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003635 if (fs_info->chunk_root->nodesize >
3636 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3637 fs_info->chunk_root->sectorsize >
3638 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3639 /*
3640 * would exhaust the array bounds of pagev member in
3641 * struct scrub_block
3642 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003643 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3644 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003645 fs_info->chunk_root->nodesize,
3646 SCRUB_MAX_PAGES_PER_BLOCK,
3647 fs_info->chunk_root->sectorsize,
3648 SCRUB_MAX_PAGES_PER_BLOCK);
3649 return -EINVAL;
3650 }
3651
Arne Jansena2de7332011-03-08 14:14:00 +01003652
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003653 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3654 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003655 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003656 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003657 return -ENODEV;
3658 }
Arne Jansena2de7332011-03-08 14:14:00 +01003659
Miao Xie5d68da32014-07-24 11:37:07 +08003660 if (!is_dev_replace && !readonly && !dev->writeable) {
3661 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3662 rcu_read_lock();
3663 name = rcu_dereference(dev->name);
3664 btrfs_err(fs_info, "scrub: device %s is not writable",
3665 name->str);
3666 rcu_read_unlock();
3667 return -EROFS;
3668 }
3669
Wang Shilong3b7a0162013-10-12 02:11:12 +08003670 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003671 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003672 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003673 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003674 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003675 }
3676
Stefan Behrens8dabb742012-11-06 13:15:27 +01003677 btrfs_dev_replace_lock(&fs_info->dev_replace);
3678 if (dev->scrub_device ||
3679 (!is_dev_replace &&
3680 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3681 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003682 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003683 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003684 return -EINPROGRESS;
3685 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003686 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003687
3688 ret = scrub_workers_get(fs_info, is_dev_replace);
3689 if (ret) {
3690 mutex_unlock(&fs_info->scrub_lock);
3691 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3692 return ret;
3693 }
3694
Stefan Behrens63a212a2012-11-05 18:29:28 +01003695 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003696 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003697 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003698 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3699 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003700 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003701 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003702 sctx->readonly = readonly;
3703 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003704 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003705
Wang Shilong3cb09292013-12-04 21:15:19 +08003706 /*
3707 * checking @scrub_pause_req here, we can avoid
3708 * race between committing transaction and scrubbing.
3709 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003710 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003711 atomic_inc(&fs_info->scrubs_running);
3712 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003713
Stefan Behrensff023aa2012-11-06 11:43:11 +01003714 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003715 /*
3716 * by holding device list mutex, we can
3717 * kick off writing super in log tree sync.
3718 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003719 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003720 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003721 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003722 }
Arne Jansena2de7332011-03-08 14:14:00 +01003723
3724 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003725 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3726 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003727
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003728 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003729 atomic_dec(&fs_info->scrubs_running);
3730 wake_up(&fs_info->scrub_pause_wait);
3731
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003732 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003733
Arne Jansena2de7332011-03-08 14:14:00 +01003734 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003735 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003736
3737 mutex_lock(&fs_info->scrub_lock);
3738 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003739 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003740 mutex_unlock(&fs_info->scrub_lock);
3741
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003742 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003743
3744 return ret;
3745}
3746
Jeff Mahoney143bede2012-03-01 14:56:26 +01003747void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003748{
3749 struct btrfs_fs_info *fs_info = root->fs_info;
3750
3751 mutex_lock(&fs_info->scrub_lock);
3752 atomic_inc(&fs_info->scrub_pause_req);
3753 while (atomic_read(&fs_info->scrubs_paused) !=
3754 atomic_read(&fs_info->scrubs_running)) {
3755 mutex_unlock(&fs_info->scrub_lock);
3756 wait_event(fs_info->scrub_pause_wait,
3757 atomic_read(&fs_info->scrubs_paused) ==
3758 atomic_read(&fs_info->scrubs_running));
3759 mutex_lock(&fs_info->scrub_lock);
3760 }
3761 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003762}
3763
Jeff Mahoney143bede2012-03-01 14:56:26 +01003764void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003765{
3766 struct btrfs_fs_info *fs_info = root->fs_info;
3767
3768 atomic_dec(&fs_info->scrub_pause_req);
3769 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003770}
3771
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003772int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003773{
Arne Jansena2de7332011-03-08 14:14:00 +01003774 mutex_lock(&fs_info->scrub_lock);
3775 if (!atomic_read(&fs_info->scrubs_running)) {
3776 mutex_unlock(&fs_info->scrub_lock);
3777 return -ENOTCONN;
3778 }
3779
3780 atomic_inc(&fs_info->scrub_cancel_req);
3781 while (atomic_read(&fs_info->scrubs_running)) {
3782 mutex_unlock(&fs_info->scrub_lock);
3783 wait_event(fs_info->scrub_pause_wait,
3784 atomic_read(&fs_info->scrubs_running) == 0);
3785 mutex_lock(&fs_info->scrub_lock);
3786 }
3787 atomic_dec(&fs_info->scrub_cancel_req);
3788 mutex_unlock(&fs_info->scrub_lock);
3789
3790 return 0;
3791}
3792
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003793int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3794 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003795{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003796 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003797
3798 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003799 sctx = dev->scrub_device;
3800 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003801 mutex_unlock(&fs_info->scrub_lock);
3802 return -ENOTCONN;
3803 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003804 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003805 while (dev->scrub_device) {
3806 mutex_unlock(&fs_info->scrub_lock);
3807 wait_event(fs_info->scrub_pause_wait,
3808 dev->scrub_device == NULL);
3809 mutex_lock(&fs_info->scrub_lock);
3810 }
3811 mutex_unlock(&fs_info->scrub_lock);
3812
3813 return 0;
3814}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003815
Arne Jansena2de7332011-03-08 14:14:00 +01003816int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3817 struct btrfs_scrub_progress *progress)
3818{
3819 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003820 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003821
3822 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003823 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003824 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003825 sctx = dev->scrub_device;
3826 if (sctx)
3827 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003828 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3829
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003830 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003831}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003832
3833static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3834 u64 extent_logical, u64 extent_len,
3835 u64 *extent_physical,
3836 struct btrfs_device **extent_dev,
3837 int *extent_mirror_num)
3838{
3839 u64 mapped_length;
3840 struct btrfs_bio *bbio = NULL;
3841 int ret;
3842
3843 mapped_length = extent_len;
3844 ret = btrfs_map_block(fs_info, READ, extent_logical,
3845 &mapped_length, &bbio, 0);
3846 if (ret || !bbio || mapped_length < extent_len ||
3847 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08003848 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003849 return;
3850 }
3851
3852 *extent_physical = bbio->stripes[0].physical;
3853 *extent_mirror_num = bbio->mirror_num;
3854 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08003855 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003856}
3857
3858static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3859 struct scrub_wr_ctx *wr_ctx,
3860 struct btrfs_fs_info *fs_info,
3861 struct btrfs_device *dev,
3862 int is_dev_replace)
3863{
3864 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3865
3866 mutex_init(&wr_ctx->wr_lock);
3867 wr_ctx->wr_curr_bio = NULL;
3868 if (!is_dev_replace)
3869 return 0;
3870
3871 WARN_ON(!dev->bdev);
3872 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3873 bio_get_nr_vecs(dev->bdev));
3874 wr_ctx->tgtdev = dev;
3875 atomic_set(&wr_ctx->flush_all_writes, 0);
3876 return 0;
3877}
3878
3879static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3880{
3881 mutex_lock(&wr_ctx->wr_lock);
3882 kfree(wr_ctx->wr_curr_bio);
3883 wr_ctx->wr_curr_bio = NULL;
3884 mutex_unlock(&wr_ctx->wr_lock);
3885}
3886
3887static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3888 int mirror_num, u64 physical_for_dev_replace)
3889{
3890 struct scrub_copy_nocow_ctx *nocow_ctx;
3891 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3892
3893 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3894 if (!nocow_ctx) {
3895 spin_lock(&sctx->stat_lock);
3896 sctx->stat.malloc_errors++;
3897 spin_unlock(&sctx->stat_lock);
3898 return -ENOMEM;
3899 }
3900
3901 scrub_pending_trans_workers_inc(sctx);
3902
3903 nocow_ctx->sctx = sctx;
3904 nocow_ctx->logical = logical;
3905 nocow_ctx->len = len;
3906 nocow_ctx->mirror_num = mirror_num;
3907 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08003908 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
3909 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003910 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003911 btrfs_queue_work(fs_info->scrub_nocow_workers,
3912 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003913
3914 return 0;
3915}
3916
Josef Bacik652f25a2013-09-12 16:58:28 -04003917static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3918{
3919 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3920 struct scrub_nocow_inode *nocow_inode;
3921
3922 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3923 if (!nocow_inode)
3924 return -ENOMEM;
3925 nocow_inode->inum = inum;
3926 nocow_inode->offset = offset;
3927 nocow_inode->root = root;
3928 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3929 return 0;
3930}
3931
3932#define COPY_COMPLETE 1
3933
Stefan Behrensff023aa2012-11-06 11:43:11 +01003934static void copy_nocow_pages_worker(struct btrfs_work *work)
3935{
3936 struct scrub_copy_nocow_ctx *nocow_ctx =
3937 container_of(work, struct scrub_copy_nocow_ctx, work);
3938 struct scrub_ctx *sctx = nocow_ctx->sctx;
3939 u64 logical = nocow_ctx->logical;
3940 u64 len = nocow_ctx->len;
3941 int mirror_num = nocow_ctx->mirror_num;
3942 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3943 int ret;
3944 struct btrfs_trans_handle *trans = NULL;
3945 struct btrfs_fs_info *fs_info;
3946 struct btrfs_path *path;
3947 struct btrfs_root *root;
3948 int not_written = 0;
3949
3950 fs_info = sctx->dev_root->fs_info;
3951 root = fs_info->extent_root;
3952
3953 path = btrfs_alloc_path();
3954 if (!path) {
3955 spin_lock(&sctx->stat_lock);
3956 sctx->stat.malloc_errors++;
3957 spin_unlock(&sctx->stat_lock);
3958 not_written = 1;
3959 goto out;
3960 }
3961
3962 trans = btrfs_join_transaction(root);
3963 if (IS_ERR(trans)) {
3964 not_written = 1;
3965 goto out;
3966 }
3967
3968 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04003969 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003970 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003971 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
3972 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02003973 logical, physical_for_dev_replace, len, mirror_num,
3974 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003975 not_written = 1;
3976 goto out;
3977 }
3978
Josef Bacik652f25a2013-09-12 16:58:28 -04003979 btrfs_end_transaction(trans, root);
3980 trans = NULL;
3981 while (!list_empty(&nocow_ctx->inodes)) {
3982 struct scrub_nocow_inode *entry;
3983 entry = list_first_entry(&nocow_ctx->inodes,
3984 struct scrub_nocow_inode,
3985 list);
3986 list_del_init(&entry->list);
3987 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
3988 entry->root, nocow_ctx);
3989 kfree(entry);
3990 if (ret == COPY_COMPLETE) {
3991 ret = 0;
3992 break;
3993 } else if (ret) {
3994 break;
3995 }
3996 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003997out:
Josef Bacik652f25a2013-09-12 16:58:28 -04003998 while (!list_empty(&nocow_ctx->inodes)) {
3999 struct scrub_nocow_inode *entry;
4000 entry = list_first_entry(&nocow_ctx->inodes,
4001 struct scrub_nocow_inode,
4002 list);
4003 list_del_init(&entry->list);
4004 kfree(entry);
4005 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004006 if (trans && !IS_ERR(trans))
4007 btrfs_end_transaction(trans, root);
4008 if (not_written)
4009 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4010 num_uncorrectable_read_errors);
4011
4012 btrfs_free_path(path);
4013 kfree(nocow_ctx);
4014
4015 scrub_pending_trans_workers_dec(sctx);
4016}
4017
Gui Hecheng32159242014-11-10 15:36:08 +08004018static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4019 u64 logical)
4020{
4021 struct extent_state *cached_state = NULL;
4022 struct btrfs_ordered_extent *ordered;
4023 struct extent_io_tree *io_tree;
4024 struct extent_map *em;
4025 u64 lockstart = start, lockend = start + len - 1;
4026 int ret = 0;
4027
4028 io_tree = &BTRFS_I(inode)->io_tree;
4029
4030 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4031 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4032 if (ordered) {
4033 btrfs_put_ordered_extent(ordered);
4034 ret = 1;
4035 goto out_unlock;
4036 }
4037
4038 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4039 if (IS_ERR(em)) {
4040 ret = PTR_ERR(em);
4041 goto out_unlock;
4042 }
4043
4044 /*
4045 * This extent does not actually cover the logical extent anymore,
4046 * move on to the next inode.
4047 */
4048 if (em->block_start > logical ||
4049 em->block_start + em->block_len < logical + len) {
4050 free_extent_map(em);
4051 ret = 1;
4052 goto out_unlock;
4053 }
4054 free_extent_map(em);
4055
4056out_unlock:
4057 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4058 GFP_NOFS);
4059 return ret;
4060}
4061
Josef Bacik652f25a2013-09-12 16:58:28 -04004062static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4063 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004064{
Miao Xie826aa0a2013-06-27 18:50:59 +08004065 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004066 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004067 struct inode *inode;
4068 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004069 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004070 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004071 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004072 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004073 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004074 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004075 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004076 int ret = 0;
4077 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004078
4079 key.objectid = root;
4080 key.type = BTRFS_ROOT_ITEM_KEY;
4081 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004082
4083 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4084
Stefan Behrensff023aa2012-11-06 11:43:11 +01004085 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004086 if (IS_ERR(local_root)) {
4087 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004088 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004089 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004090
4091 key.type = BTRFS_INODE_ITEM_KEY;
4092 key.objectid = inum;
4093 key.offset = 0;
4094 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004095 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004096 if (IS_ERR(inode))
4097 return PTR_ERR(inode);
4098
Miao Xieedd14002013-06-27 18:51:00 +08004099 /* Avoid truncate/dio/punch hole.. */
4100 mutex_lock(&inode->i_mutex);
4101 inode_dio_wait(inode);
4102
Stefan Behrensff023aa2012-11-06 11:43:11 +01004103 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004104 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004105 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004106
Gui Hecheng32159242014-11-10 15:36:08 +08004107 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4108 if (ret) {
4109 ret = ret > 0 ? 0 : ret;
4110 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004111 }
4112
Stefan Behrensff023aa2012-11-06 11:43:11 +01004113 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004114 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004115again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004116 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4117 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004118 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004119 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004120 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004121 }
4122
4123 if (PageUptodate(page)) {
4124 if (PageDirty(page))
4125 goto next_page;
4126 } else {
4127 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004128 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004129 btrfs_get_extent,
4130 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004131 if (err) {
4132 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004133 goto next_page;
4134 }
Miao Xieedd14002013-06-27 18:51:00 +08004135
Miao Xie26b258912013-06-27 18:50:58 +08004136 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004137 /*
4138 * If the page has been remove from the page cache,
4139 * the data on it is meaningless, because it may be
4140 * old one, the new data may be written into the new
4141 * page in the page cache.
4142 */
4143 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004144 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004145 page_cache_release(page);
4146 goto again;
4147 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004148 if (!PageUptodate(page)) {
4149 ret = -EIO;
4150 goto next_page;
4151 }
4152 }
Gui Hecheng32159242014-11-10 15:36:08 +08004153
4154 ret = check_extent_to_block(inode, offset, len,
4155 nocow_ctx_logical);
4156 if (ret) {
4157 ret = ret > 0 ? 0 : ret;
4158 goto next_page;
4159 }
4160
Miao Xie826aa0a2013-06-27 18:50:59 +08004161 err = write_page_nocow(nocow_ctx->sctx,
4162 physical_for_dev_replace, page);
4163 if (err)
4164 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004165next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004166 unlock_page(page);
4167 page_cache_release(page);
4168
4169 if (ret)
4170 break;
4171
Stefan Behrensff023aa2012-11-06 11:43:11 +01004172 offset += PAGE_CACHE_SIZE;
4173 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004174 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004175 len -= PAGE_CACHE_SIZE;
4176 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004177 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004178out:
Miao Xieedd14002013-06-27 18:51:00 +08004179 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004180 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004181 return ret;
4182}
4183
4184static int write_page_nocow(struct scrub_ctx *sctx,
4185 u64 physical_for_dev_replace, struct page *page)
4186{
4187 struct bio *bio;
4188 struct btrfs_device *dev;
4189 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004190
4191 dev = sctx->wr_ctx.tgtdev;
4192 if (!dev)
4193 return -EIO;
4194 if (!dev->bdev) {
4195 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05004196 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004197 return -EIO;
4198 }
Chris Mason9be33952013-05-17 18:30:14 -04004199 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004200 if (!bio) {
4201 spin_lock(&sctx->stat_lock);
4202 sctx->stat.malloc_errors++;
4203 spin_unlock(&sctx->stat_lock);
4204 return -ENOMEM;
4205 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004206 bio->bi_iter.bi_size = 0;
4207 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004208 bio->bi_bdev = dev->bdev;
4209 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4210 if (ret != PAGE_CACHE_SIZE) {
4211leave_with_eio:
4212 bio_put(bio);
4213 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4214 return -EIO;
4215 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004216
Kent Overstreet33879d42013-11-23 22:33:32 -08004217 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004218 goto leave_with_eio;
4219
4220 bio_put(bio);
4221 return 0;
4222}