blob: 3e4db4b46a7daf3efcfaf839eaa4b3b984363e52 [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 };
Omar Sandoval73ff61d2015-06-19 11:52:51 -0700128 struct btrfs_work work;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400129};
130
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800131/* Used for the chunks with parity stripe such RAID5/6 */
132struct scrub_parity {
133 struct scrub_ctx *sctx;
134
135 struct btrfs_device *scrub_dev;
136
137 u64 logic_start;
138
139 u64 logic_end;
140
141 int nsectors;
142
143 int stripe_len;
144
Zhao Lei57019342015-01-20 15:11:45 +0800145 atomic_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800146
147 struct list_head spages;
148
149 /* Work of parity check and repair */
150 struct btrfs_work work;
151
152 /* Mark the parity blocks which have data */
153 unsigned long *dbitmap;
154
155 /*
156 * Mark the parity blocks which have data, but errors happen when
157 * read data or check data
158 */
159 unsigned long *ebitmap;
160
161 unsigned long bitmap[0];
162};
163
Stefan Behrensff023aa2012-11-06 11:43:11 +0100164struct scrub_wr_ctx {
165 struct scrub_bio *wr_curr_bio;
166 struct btrfs_device *tgtdev;
167 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
168 atomic_t flush_all_writes;
169 struct mutex wr_lock;
170};
171
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100172struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100173 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100174 struct btrfs_root *dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +0100175 int first_free;
176 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100177 atomic_t bios_in_flight;
178 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100179 spinlock_t list_lock;
180 wait_queue_head_t list_wait;
181 u16 csum_size;
182 struct list_head csum_list;
183 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100184 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100185 int pages_per_rd_bio;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400186 u32 sectorsize;
187 u32 nodesize;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100188
189 int is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100190 struct scrub_wr_ctx wr_ctx;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100191
Arne Jansena2de7332011-03-08 14:14:00 +0100192 /*
193 * statistics
194 */
195 struct btrfs_scrub_progress stat;
196 spinlock_t stat_lock;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000197
198 /*
199 * Use a ref counter to avoid use-after-free issues. Scrub workers
200 * decrement bios_in_flight and workers_pending and then do a wakeup
201 * on the list_wait wait queue. We must ensure the main scrub task
202 * doesn't free the scrub context before or while the workers are
203 * doing the wakeup() call.
204 */
205 atomic_t refs;
Arne Jansena2de7332011-03-08 14:14:00 +0100206};
207
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200208struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100209 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100210 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200211 u64 logical;
212 struct btrfs_root *root;
213 struct btrfs_work work;
214 int mirror_num;
215};
216
Josef Bacik652f25a2013-09-12 16:58:28 -0400217struct scrub_nocow_inode {
218 u64 inum;
219 u64 offset;
220 u64 root;
221 struct list_head list;
222};
223
Stefan Behrensff023aa2012-11-06 11:43:11 +0100224struct scrub_copy_nocow_ctx {
225 struct scrub_ctx *sctx;
226 u64 logical;
227 u64 len;
228 int mirror_num;
229 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -0400230 struct list_head inodes;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100231 struct btrfs_work work;
232};
233
Jan Schmidt558540c2011-06-13 19:59:12 +0200234struct scrub_warning {
235 struct btrfs_path *path;
236 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200237 const char *errstr;
238 sector_t sector;
239 u64 logical;
240 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200241};
242
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100243static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
244static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
245static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
246static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400247static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Zhao Leibe50a8d2015-01-20 15:11:42 +0800248static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100249 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100250static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
251 struct scrub_block *sblock, int is_metadata,
252 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800253 u16 csum_size, int retry_failed_mirror);
Zhao Leiba7cf982015-08-24 21:18:02 +0800254static void scrub_recheck_block_checksum(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400255static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800256 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400257static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
258 struct scrub_block *sblock_good,
259 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100260static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
261static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
262 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400263static int scrub_checksum_data(struct scrub_block *sblock);
264static int scrub_checksum_tree_block(struct scrub_block *sblock);
265static int scrub_checksum_super(struct scrub_block *sblock);
266static void scrub_block_get(struct scrub_block *sblock);
267static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100268static void scrub_page_get(struct scrub_page *spage);
269static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800270static void scrub_parity_get(struct scrub_parity *sparity);
271static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100272static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
273 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100274static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100275 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100276 u64 gen, int mirror_num, u8 *csum, int force,
277 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200278static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400279static void scrub_bio_end_io_worker(struct btrfs_work *work);
280static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100281static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
282 u64 extent_logical, u64 extent_len,
283 u64 *extent_physical,
284 struct btrfs_device **extent_dev,
285 int *extent_mirror_num);
286static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
287 struct scrub_wr_ctx *wr_ctx,
288 struct btrfs_fs_info *fs_info,
289 struct btrfs_device *dev,
290 int is_dev_replace);
291static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
292static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
293 struct scrub_page *spage);
294static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200295static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100296static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
297static int write_page_nocow(struct scrub_ctx *sctx,
298 u64 physical_for_dev_replace, struct page *page);
299static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400300 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100301static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
302 int mirror_num, u64 physical_for_dev_replace);
303static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800304static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800305static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000306static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400307
308
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100309static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
310{
Filipe Mananaf55985f2015-02-09 21:14:24 +0000311 atomic_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100312 atomic_inc(&sctx->bios_in_flight);
313}
314
315static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
316{
317 atomic_dec(&sctx->bios_in_flight);
318 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000319 scrub_put_ctx(sctx);
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
Zhaolei0e22be82015-08-05 16:43:28 +0800332static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800333{
334 atomic_inc(&fs_info->scrubs_paused);
335 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800336}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800337
Zhaolei0e22be82015-08-05 16:43:28 +0800338static void scrub_pause_off(struct btrfs_fs_info *fs_info)
339{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800340 mutex_lock(&fs_info->scrub_lock);
341 __scrub_blocked_if_needed(fs_info);
342 atomic_dec(&fs_info->scrubs_paused);
343 mutex_unlock(&fs_info->scrub_lock);
344
345 wake_up(&fs_info->scrub_pause_wait);
346}
347
Zhaolei0e22be82015-08-05 16:43:28 +0800348static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
349{
350 scrub_pause_on(fs_info);
351 scrub_pause_off(fs_info);
352}
353
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100354/*
355 * used for workers that require transaction commits (i.e., for the
356 * NOCOW case)
357 */
358static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
359{
360 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
361
Filipe Mananaf55985f2015-02-09 21:14:24 +0000362 atomic_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100363 /*
364 * increment scrubs_running to prevent cancel requests from
365 * completing as long as a worker is running. we must also
366 * increment scrubs_paused to prevent deadlocking on pause
367 * requests used for transactions commits (as the worker uses a
368 * transaction context). it is safe to regard the worker
369 * as paused for all matters practical. effectively, we only
370 * avoid cancellation requests from completing.
371 */
372 mutex_lock(&fs_info->scrub_lock);
373 atomic_inc(&fs_info->scrubs_running);
374 atomic_inc(&fs_info->scrubs_paused);
375 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800376
377 /*
378 * check if @scrubs_running=@scrubs_paused condition
379 * inside wait_event() is not an atomic operation.
380 * which means we may inc/dec @scrub_running/paused
381 * at any time. Let's wake up @scrub_pause_wait as
382 * much as we can to let commit transaction blocked less.
383 */
384 wake_up(&fs_info->scrub_pause_wait);
385
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100386 atomic_inc(&sctx->workers_pending);
387}
388
389/* used for workers that require transaction commits */
390static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
391{
392 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
393
394 /*
395 * see scrub_pending_trans_workers_inc() why we're pretending
396 * to be paused in the scrub counters
397 */
398 mutex_lock(&fs_info->scrub_lock);
399 atomic_dec(&fs_info->scrubs_running);
400 atomic_dec(&fs_info->scrubs_paused);
401 mutex_unlock(&fs_info->scrub_lock);
402 atomic_dec(&sctx->workers_pending);
403 wake_up(&fs_info->scrub_pause_wait);
404 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000405 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100406}
407
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100408static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100409{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100410 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100411 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100412 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100413 struct btrfs_ordered_sum, list);
414 list_del(&sum->list);
415 kfree(sum);
416 }
417}
418
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100419static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100420{
421 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100422
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100423 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100424 return;
425
Stefan Behrensff023aa2012-11-06 11:43:11 +0100426 scrub_free_wr_ctx(&sctx->wr_ctx);
427
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400428 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100429 if (sctx->curr != -1) {
430 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400431
432 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100433 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400434 scrub_block_put(sbio->pagev[i]->sblock);
435 }
436 bio_put(sbio->bio);
437 }
438
Stefan Behrensff023aa2012-11-06 11:43:11 +0100439 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100440 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100441
442 if (!sbio)
443 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100444 kfree(sbio);
445 }
446
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100447 scrub_free_csums(sctx);
448 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100449}
450
Filipe Mananaf55985f2015-02-09 21:14:24 +0000451static void scrub_put_ctx(struct scrub_ctx *sctx)
452{
453 if (atomic_dec_and_test(&sctx->refs))
454 scrub_free_ctx(sctx);
455}
456
Arne Jansena2de7332011-03-08 14:14:00 +0100457static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100458struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100459{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100460 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100461 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100462 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100463 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100464
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100465 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
466 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100467 goto nomem;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000468 atomic_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100469 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200470 sctx->pages_per_rd_bio = SCRUB_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)
David Sterbaecaeb142015-10-08 09:01:03 +0200579 btrfs_warn_in_rcu(fs_info, "%s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200580 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
David Sterbaecaeb142015-10-08 09:01:03 +0200581 "length %llu, links %u (path: %s)", 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:
David Sterbaecaeb142015-10-08 09:01:03 +0200591 btrfs_warn_in_rcu(fs_info, "%s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200592 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
David Sterbaecaeb142015-10-08 09:01:03 +0200593 "resolving failed with ret=%d", 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);
David Sterbaecaeb142015-10-08 09:01:03 +0200648 btrfs_warn_in_rcu(fs_info,
649 "%s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200650 "sector %llu: metadata %s (level %d) in tree "
David Sterbaecaeb142015-10-08 09:01:03 +0200651 "%llu", errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400652 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);
David Sterbab14af3b2015-10-08 10:43:10 +0200849 btrfs_err_rl_in_rcu(sctx->dev_root->fs_info,
850 "unable to fixup (nodatasum) error at logical %llu on dev %s",
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
David Sterba31e818f2015-02-20 18:00:26 +0100960 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
961 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400962 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100963 spin_lock(&sctx->stat_lock);
964 sctx->stat.malloc_errors++;
965 sctx->stat.read_errors++;
966 sctx->stat.uncorrectable_errors++;
967 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100968 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400969 goto out;
970 }
971
972 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800973 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400974 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100975 spin_lock(&sctx->stat_lock);
976 sctx->stat.read_errors++;
977 sctx->stat.uncorrectable_errors++;
978 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100979 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400980 goto out;
981 }
982 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
983 sblock_bad = sblocks_for_recheck + failed_mirror_index;
984
985 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100986 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800987 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400988
989 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
990 sblock_bad->no_io_error_seen) {
991 /*
992 * the error disappeared after reading page by page, or
993 * the area was part of a huge bio and other parts of the
994 * bio caused I/O errors, or the block layer merged several
995 * read requests into one and the error is caused by a
996 * different bio (usually one of the two latter cases is
997 * the cause)
998 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100999 spin_lock(&sctx->stat_lock);
1000 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001001 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001002 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001003
Stefan Behrensff023aa2012-11-06 11:43:11 +01001004 if (sctx->is_dev_replace)
1005 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001006 goto out;
1007 }
1008
1009 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001010 spin_lock(&sctx->stat_lock);
1011 sctx->stat.read_errors++;
1012 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001013 if (__ratelimit(&_rs))
1014 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001015 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001016 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001017 spin_lock(&sctx->stat_lock);
1018 sctx->stat.csum_errors++;
1019 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001020 if (__ratelimit(&_rs))
1021 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001022 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001023 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001024 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001025 spin_lock(&sctx->stat_lock);
1026 sctx->stat.verify_errors++;
1027 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001028 if (__ratelimit(&_rs))
1029 scrub_print_warning("checksum/header error",
1030 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001031 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001032 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001033 BTRFS_DEV_STAT_GENERATION_ERRS);
1034 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001035 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001036 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001037 }
1038
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001039 if (sctx->readonly) {
1040 ASSERT(!sctx->is_dev_replace);
1041 goto out;
1042 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001043
1044 if (!is_metadata && !have_csum) {
1045 struct scrub_fixup_nodatasum *fixup_nodatasum;
1046
Stefan Behrensff023aa2012-11-06 11:43:11 +01001047 WARN_ON(sctx->is_dev_replace);
1048
Zhao Leib25c94c2015-01-20 15:11:35 +08001049nodatasum_case:
1050
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001051 /*
1052 * !is_metadata and !have_csum, this means that the data
1053 * might not be COW'ed, that it might be modified
1054 * concurrently. The general strategy to work on the
1055 * commit root does not help in the case when COW is not
1056 * used.
1057 */
1058 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1059 if (!fixup_nodatasum)
1060 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001061 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001062 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001063 fixup_nodatasum->logical = logical;
1064 fixup_nodatasum->root = fs_info->extent_root;
1065 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001066 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001067 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1068 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001069 btrfs_queue_work(fs_info->scrub_workers,
1070 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001071 goto out;
1072 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001073
1074 /*
1075 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001076 * checksums.
1077 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001078 * errors and also does not have a checksum error.
1079 * If one is found, and if a checksum is present, the full block
1080 * that is known to contain an error is rewritten. Afterwards
1081 * the block is known to be corrected.
1082 * If a mirror is found which is completely correct, and no
1083 * checksum is present, only those pages are rewritten that had
1084 * an I/O error in the block to be repaired, since it cannot be
1085 * determined, which copy of the other pages is better (and it
1086 * could happen otherwise that a correct page would be
1087 * overwritten by a bad one).
1088 */
1089 for (mirror_index = 0;
1090 mirror_index < BTRFS_MAX_MIRRORS &&
1091 sblocks_for_recheck[mirror_index].page_count > 0;
1092 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001093 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001094
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001095 if (mirror_index == failed_mirror_index)
1096 continue;
1097 sblock_other = sblocks_for_recheck + mirror_index;
1098
1099 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001100 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1101 have_csum, csum, generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001102 sctx->csum_size, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001103
1104 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001105 !sblock_other->checksum_error &&
1106 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001107 if (sctx->is_dev_replace) {
1108 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001109 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001110 } else {
1111 ret = scrub_repair_block_from_good_copy(
1112 sblock_bad, sblock_other);
1113 if (!ret)
1114 goto corrected_error;
1115 }
Arne Jansena2de7332011-03-08 14:14:00 +01001116 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001117 }
1118
Zhao Leib968fed2015-01-20 15:11:41 +08001119 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1120 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001121
1122 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001123 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001124 * repaired, continue by picking good copies of those pages.
1125 * Select the good pages from mirrors to rewrite bad pages from
1126 * the area to fix. Afterwards verify the checksum of the block
1127 * that is supposed to be repaired. This verification step is
1128 * only done for the purpose of statistic counting and for the
1129 * final scrub report, whether errors remain.
1130 * A perfect algorithm could make use of the checksum and try
1131 * all possible combinations of pages from the different mirrors
1132 * until the checksum verification succeeds. For example, when
1133 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1134 * of mirror #2 is readable but the final checksum test fails,
1135 * then the 2nd page of mirror #3 could be tried, whether now
1136 * the final checksum succeedes. But this would be a rare
1137 * exception and is therefore not implemented. At least it is
1138 * avoided that the good copy is overwritten.
1139 * A more useful improvement would be to pick the sectors
1140 * without I/O error based on sector sizes (512 bytes on legacy
1141 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1142 * mirror could be repaired by taking 512 byte of a different
1143 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1144 * area are unreadable.
1145 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001146 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001147 for (page_num = 0; page_num < sblock_bad->page_count;
1148 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001149 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001150 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001151
Zhao Leib968fed2015-01-20 15:11:41 +08001152 /* skip no-io-error page in scrub */
1153 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001154 continue;
1155
Zhao Leib968fed2015-01-20 15:11:41 +08001156 /* try to find no-io-error page in mirrors */
1157 if (page_bad->io_error) {
1158 for (mirror_index = 0;
1159 mirror_index < BTRFS_MAX_MIRRORS &&
1160 sblocks_for_recheck[mirror_index].page_count > 0;
1161 mirror_index++) {
1162 if (!sblocks_for_recheck[mirror_index].
1163 pagev[page_num]->io_error) {
1164 sblock_other = sblocks_for_recheck +
1165 mirror_index;
1166 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001167 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001168 }
Zhao Leib968fed2015-01-20 15:11:41 +08001169 if (!sblock_other)
1170 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001171 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001172
Zhao Leib968fed2015-01-20 15:11:41 +08001173 if (sctx->is_dev_replace) {
1174 /*
1175 * did not find a mirror to fetch the page
1176 * from. scrub_write_page_to_dev_replace()
1177 * handles this case (page->io_error), by
1178 * filling the block with zeros before
1179 * submitting the write request
1180 */
1181 if (!sblock_other)
1182 sblock_other = sblock_bad;
1183
1184 if (scrub_write_page_to_dev_replace(sblock_other,
1185 page_num) != 0) {
1186 btrfs_dev_replace_stats_inc(
1187 &sctx->dev_root->
1188 fs_info->dev_replace.
1189 num_write_errors);
1190 success = 0;
1191 }
1192 } else if (sblock_other) {
1193 ret = scrub_repair_page_from_good_copy(sblock_bad,
1194 sblock_other,
1195 page_num, 0);
1196 if (0 == ret)
1197 page_bad->io_error = 0;
1198 else
1199 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001200 }
1201 }
1202
Zhao Leib968fed2015-01-20 15:11:41 +08001203 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001204 if (is_metadata || have_csum) {
1205 /*
1206 * need to verify the checksum now that all
1207 * sectors on disk are repaired (the write
1208 * request for data to be repaired is on its way).
1209 * Just be lazy and use scrub_recheck_block()
1210 * which re-reads the data before the checksum
1211 * is verified, but most likely the data comes out
1212 * of the page cache.
1213 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001214 scrub_recheck_block(fs_info, sblock_bad,
1215 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001216 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001217 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001218 !sblock_bad->checksum_error &&
1219 sblock_bad->no_io_error_seen)
1220 goto corrected_error;
1221 else
1222 goto did_not_correct_error;
1223 } else {
1224corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001225 spin_lock(&sctx->stat_lock);
1226 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001227 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001228 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001229 btrfs_err_rl_in_rcu(fs_info,
1230 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001231 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001232 }
1233 } else {
1234did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001235 spin_lock(&sctx->stat_lock);
1236 sctx->stat.uncorrectable_errors++;
1237 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001238 btrfs_err_rl_in_rcu(fs_info,
1239 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001240 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001241 }
1242
1243out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001244 if (sblocks_for_recheck) {
1245 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1246 mirror_index++) {
1247 struct scrub_block *sblock = sblocks_for_recheck +
1248 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001249 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001250 int page_index;
1251
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001252 for (page_index = 0; page_index < sblock->page_count;
1253 page_index++) {
1254 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001255 recover = sblock->pagev[page_index]->recover;
1256 if (recover) {
1257 scrub_put_recover(recover);
1258 sblock->pagev[page_index]->recover =
1259 NULL;
1260 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001261 scrub_page_put(sblock->pagev[page_index]);
1262 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001263 }
1264 kfree(sblocks_for_recheck);
1265 }
1266
1267 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001268}
1269
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001270static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001271{
Zhao Lei10f11902015-01-20 15:11:43 +08001272 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1273 return 2;
1274 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1275 return 3;
1276 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001277 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001278}
1279
Zhao Lei10f11902015-01-20 15:11:43 +08001280static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1281 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001282 u64 mapped_length,
1283 int nstripes, int mirror,
1284 int *stripe_index,
1285 u64 *stripe_offset)
1286{
1287 int i;
1288
Zhao Leiffe2d202015-01-20 15:11:44 +08001289 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001290 /* RAID5/6 */
1291 for (i = 0; i < nstripes; i++) {
1292 if (raid_map[i] == RAID6_Q_STRIPE ||
1293 raid_map[i] == RAID5_P_STRIPE)
1294 continue;
1295
1296 if (logical >= raid_map[i] &&
1297 logical < raid_map[i] + mapped_length)
1298 break;
1299 }
1300
1301 *stripe_index = i;
1302 *stripe_offset = logical - raid_map[i];
1303 } else {
1304 /* The other RAID type */
1305 *stripe_index = mirror;
1306 *stripe_offset = 0;
1307 }
1308}
1309
Zhao Leibe50a8d2015-01-20 15:11:42 +08001310static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001311 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001312{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001313 struct scrub_ctx *sctx = original_sblock->sctx;
1314 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1315 u64 length = original_sblock->page_count * PAGE_SIZE;
1316 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001317 u64 generation = original_sblock->pagev[0]->generation;
1318 u64 flags = original_sblock->pagev[0]->flags;
1319 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001320 struct scrub_recover *recover;
1321 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001322 u64 sublen;
1323 u64 mapped_length;
1324 u64 stripe_offset;
1325 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001326 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001327 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001328 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001329 int ret;
1330
1331 /*
Zhao Lei57019342015-01-20 15:11:45 +08001332 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001333 * are not used (and not set) in the blocks that are used for
1334 * the recheck procedure
1335 */
1336
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001337 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001338 sublen = min_t(u64, length, PAGE_SIZE);
1339 mapped_length = sublen;
1340 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001341
1342 /*
1343 * with a length of PAGE_SIZE, each returned stripe
1344 * represents one mirror
1345 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001346 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001347 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001348 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001349 btrfs_put_bbio(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001350 return -EIO;
1351 }
1352
Miao Xieaf8e2d12014-10-23 14:42:50 +08001353 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1354 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001355 btrfs_put_bbio(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001356 return -ENOMEM;
1357 }
1358
1359 atomic_set(&recover->refs, 1);
1360 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001361 recover->map_length = mapped_length;
1362
Stefan Behrensff023aa2012-11-06 11:43:11 +01001363 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001364
Zhao Leibe50a8d2015-01-20 15:11:42 +08001365 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001366
Miao Xieaf8e2d12014-10-23 14:42:50 +08001367 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001368 mirror_index++) {
1369 struct scrub_block *sblock;
1370 struct scrub_page *page;
1371
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001372 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001373 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001374
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001375 page = kzalloc(sizeof(*page), GFP_NOFS);
1376 if (!page) {
1377leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001378 spin_lock(&sctx->stat_lock);
1379 sctx->stat.malloc_errors++;
1380 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001381 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001382 return -ENOMEM;
1383 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001384 scrub_page_get(page);
1385 sblock->pagev[page_index] = page;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001386 page->sblock = sblock;
1387 page->flags = flags;
1388 page->generation = generation;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001389 page->logical = logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001390 page->have_csum = have_csum;
1391 if (have_csum)
1392 memcpy(page->csum,
1393 original_sblock->pagev[0]->csum,
1394 sctx->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001395
Zhao Lei10f11902015-01-20 15:11:43 +08001396 scrub_stripe_index_and_offset(logical,
1397 bbio->map_type,
1398 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001399 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001400 bbio->num_stripes -
1401 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001402 mirror_index,
1403 &stripe_index,
1404 &stripe_offset);
1405 page->physical = bbio->stripes[stripe_index].physical +
1406 stripe_offset;
1407 page->dev = bbio->stripes[stripe_index].dev;
1408
Stefan Behrensff023aa2012-11-06 11:43:11 +01001409 BUG_ON(page_index >= original_sblock->page_count);
1410 page->physical_for_dev_replace =
1411 original_sblock->pagev[page_index]->
1412 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001413 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001414 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001415 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001416 page->page = alloc_page(GFP_NOFS);
1417 if (!page->page)
1418 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001419
1420 scrub_get_recover(recover);
1421 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001422 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001423 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001424 length -= sublen;
1425 logical += sublen;
1426 page_index++;
1427 }
1428
1429 return 0;
1430}
1431
Miao Xieaf8e2d12014-10-23 14:42:50 +08001432struct scrub_bio_ret {
1433 struct completion event;
1434 int error;
1435};
1436
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001437static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001438{
1439 struct scrub_bio_ret *ret = bio->bi_private;
1440
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001441 ret->error = bio->bi_error;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001442 complete(&ret->event);
1443}
1444
1445static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1446{
Zhao Lei10f11902015-01-20 15:11:43 +08001447 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001448 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001449}
1450
1451static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1452 struct bio *bio,
1453 struct scrub_page *page)
1454{
1455 struct scrub_bio_ret done;
1456 int ret;
1457
1458 init_completion(&done.event);
1459 done.error = 0;
1460 bio->bi_iter.bi_sector = page->logical >> 9;
1461 bio->bi_private = &done;
1462 bio->bi_end_io = scrub_bio_wait_endio;
1463
1464 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001465 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001466 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001467 if (ret)
1468 return ret;
1469
1470 wait_for_completion(&done.event);
1471 if (done.error)
1472 return -EIO;
1473
1474 return 0;
1475}
1476
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001477/*
1478 * this function will check the on disk data for checksum errors, header
1479 * errors and read I/O errors. If any I/O errors happen, the exact pages
1480 * which are errored are marked as being bad. The goal is to enable scrub
1481 * to take those pages that are not errored from all the mirrors so that
1482 * the pages that are errored in the just handled mirror can be repaired.
1483 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001484static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1485 struct scrub_block *sblock, int is_metadata,
1486 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001487 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001488{
1489 int page_num;
1490
1491 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001492
1493 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1494 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001495 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001496
Stefan Behrens442a4f62012-05-25 16:06:08 +02001497 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001498 page->io_error = 1;
1499 sblock->no_io_error_seen = 0;
1500 continue;
1501 }
1502
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001503 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001504 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001505 if (!bio) {
1506 page->io_error = 1;
1507 sblock->no_io_error_seen = 0;
1508 continue;
1509 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001510 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001511
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001512 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001513 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1514 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1515 sblock->no_io_error_seen = 0;
1516 } else {
1517 bio->bi_iter.bi_sector = page->physical >> 9;
1518
1519 if (btrfsic_submit_bio_wait(READ, bio))
1520 sblock->no_io_error_seen = 0;
1521 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001522
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001523 bio_put(bio);
1524 }
1525
1526 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001527 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001528
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001529 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001530}
1531
Miao Xie17a9be22014-07-24 11:37:08 +08001532static inline int scrub_check_fsid(u8 fsid[],
1533 struct scrub_page *spage)
1534{
1535 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1536 int ret;
1537
1538 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1539 return !ret;
1540}
1541
Zhao Leiba7cf982015-08-24 21:18:02 +08001542static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001543{
Zhao Leiba7cf982015-08-24 21:18:02 +08001544 sblock->header_error = 0;
1545 sblock->checksum_error = 0;
1546 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001547
Zhao Leiba7cf982015-08-24 21:18:02 +08001548 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1549 scrub_checksum_data(sblock);
1550 else
1551 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001552}
1553
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001554static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001555 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001556{
1557 int page_num;
1558 int ret = 0;
1559
1560 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1561 int ret_sub;
1562
1563 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1564 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001565 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001566 if (ret_sub)
1567 ret = ret_sub;
1568 }
1569
1570 return ret;
1571}
1572
1573static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1574 struct scrub_block *sblock_good,
1575 int page_num, int force_write)
1576{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001577 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1578 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001579
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001580 BUG_ON(page_bad->page == NULL);
1581 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001582 if (force_write || sblock_bad->header_error ||
1583 sblock_bad->checksum_error || page_bad->io_error) {
1584 struct bio *bio;
1585 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001586
Stefan Behrensff023aa2012-11-06 11:43:11 +01001587 if (!page_bad->dev->bdev) {
David Sterba94647322015-10-08 11:01:36 +02001588 btrfs_warn_rl(sblock_bad->sctx->dev_root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001589 "scrub_repair_page_from_good_copy(bdev == NULL) "
David Sterba94647322015-10-08 11:01:36 +02001590 "is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001591 return -EIO;
1592 }
1593
Chris Mason9be33952013-05-17 18:30:14 -04001594 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001595 if (!bio)
1596 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001597 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001598 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001599
1600 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1601 if (PAGE_SIZE != ret) {
1602 bio_put(bio);
1603 return -EIO;
1604 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001605
Kent Overstreet33879d42013-11-23 22:33:32 -08001606 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001607 btrfs_dev_stat_inc_and_print(page_bad->dev,
1608 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001609 btrfs_dev_replace_stats_inc(
1610 &sblock_bad->sctx->dev_root->fs_info->
1611 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001612 bio_put(bio);
1613 return -EIO;
1614 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001615 bio_put(bio);
1616 }
1617
1618 return 0;
1619}
1620
Stefan Behrensff023aa2012-11-06 11:43:11 +01001621static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1622{
1623 int page_num;
1624
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001625 /*
1626 * This block is used for the check of the parity on the source device,
1627 * so the data needn't be written into the destination device.
1628 */
1629 if (sblock->sparity)
1630 return;
1631
Stefan Behrensff023aa2012-11-06 11:43:11 +01001632 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1633 int ret;
1634
1635 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1636 if (ret)
1637 btrfs_dev_replace_stats_inc(
1638 &sblock->sctx->dev_root->fs_info->dev_replace.
1639 num_write_errors);
1640 }
1641}
1642
1643static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1644 int page_num)
1645{
1646 struct scrub_page *spage = sblock->pagev[page_num];
1647
1648 BUG_ON(spage->page == NULL);
1649 if (spage->io_error) {
1650 void *mapped_buffer = kmap_atomic(spage->page);
1651
1652 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1653 flush_dcache_page(spage->page);
1654 kunmap_atomic(mapped_buffer);
1655 }
1656 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1657}
1658
1659static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1660 struct scrub_page *spage)
1661{
1662 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1663 struct scrub_bio *sbio;
1664 int ret;
1665
1666 mutex_lock(&wr_ctx->wr_lock);
1667again:
1668 if (!wr_ctx->wr_curr_bio) {
1669 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1670 GFP_NOFS);
1671 if (!wr_ctx->wr_curr_bio) {
1672 mutex_unlock(&wr_ctx->wr_lock);
1673 return -ENOMEM;
1674 }
1675 wr_ctx->wr_curr_bio->sctx = sctx;
1676 wr_ctx->wr_curr_bio->page_count = 0;
1677 }
1678 sbio = wr_ctx->wr_curr_bio;
1679 if (sbio->page_count == 0) {
1680 struct bio *bio;
1681
1682 sbio->physical = spage->physical_for_dev_replace;
1683 sbio->logical = spage->logical;
1684 sbio->dev = wr_ctx->tgtdev;
1685 bio = sbio->bio;
1686 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001687 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001688 if (!bio) {
1689 mutex_unlock(&wr_ctx->wr_lock);
1690 return -ENOMEM;
1691 }
1692 sbio->bio = bio;
1693 }
1694
1695 bio->bi_private = sbio;
1696 bio->bi_end_io = scrub_wr_bio_end_io;
1697 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001698 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001699 sbio->err = 0;
1700 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1701 spage->physical_for_dev_replace ||
1702 sbio->logical + sbio->page_count * PAGE_SIZE !=
1703 spage->logical) {
1704 scrub_wr_submit(sctx);
1705 goto again;
1706 }
1707
1708 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1709 if (ret != PAGE_SIZE) {
1710 if (sbio->page_count < 1) {
1711 bio_put(sbio->bio);
1712 sbio->bio = NULL;
1713 mutex_unlock(&wr_ctx->wr_lock);
1714 return -EIO;
1715 }
1716 scrub_wr_submit(sctx);
1717 goto again;
1718 }
1719
1720 sbio->pagev[sbio->page_count] = spage;
1721 scrub_page_get(spage);
1722 sbio->page_count++;
1723 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1724 scrub_wr_submit(sctx);
1725 mutex_unlock(&wr_ctx->wr_lock);
1726
1727 return 0;
1728}
1729
1730static void scrub_wr_submit(struct scrub_ctx *sctx)
1731{
1732 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1733 struct scrub_bio *sbio;
1734
1735 if (!wr_ctx->wr_curr_bio)
1736 return;
1737
1738 sbio = wr_ctx->wr_curr_bio;
1739 wr_ctx->wr_curr_bio = NULL;
1740 WARN_ON(!sbio->bio->bi_bdev);
1741 scrub_pending_bio_inc(sctx);
1742 /* process all writes in a single worker thread. Then the block layer
1743 * orders the requests before sending them to the driver which
1744 * doubled the write performance on spinning disks when measured
1745 * with Linux 3.5 */
1746 btrfsic_submit_bio(WRITE, sbio->bio);
1747}
1748
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001749static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001750{
1751 struct scrub_bio *sbio = bio->bi_private;
1752 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1753
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001754 sbio->err = bio->bi_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001755 sbio->bio = bio;
1756
Liu Bo9e0af232014-08-15 23:36:53 +08001757 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1758 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001759 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001760}
1761
1762static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1763{
1764 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1765 struct scrub_ctx *sctx = sbio->sctx;
1766 int i;
1767
1768 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1769 if (sbio->err) {
1770 struct btrfs_dev_replace *dev_replace =
1771 &sbio->sctx->dev_root->fs_info->dev_replace;
1772
1773 for (i = 0; i < sbio->page_count; i++) {
1774 struct scrub_page *spage = sbio->pagev[i];
1775
1776 spage->io_error = 1;
1777 btrfs_dev_replace_stats_inc(&dev_replace->
1778 num_write_errors);
1779 }
1780 }
1781
1782 for (i = 0; i < sbio->page_count; i++)
1783 scrub_page_put(sbio->pagev[i]);
1784
1785 bio_put(sbio->bio);
1786 kfree(sbio);
1787 scrub_pending_bio_dec(sctx);
1788}
1789
1790static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001791{
1792 u64 flags;
1793 int ret;
1794
Zhao Leiba7cf982015-08-24 21:18:02 +08001795 /*
1796 * No need to initialize these stats currently,
1797 * because this function only use return value
1798 * instead of these stats value.
1799 *
1800 * Todo:
1801 * always use stats
1802 */
1803 sblock->header_error = 0;
1804 sblock->generation_error = 0;
1805 sblock->checksum_error = 0;
1806
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001807 WARN_ON(sblock->page_count < 1);
1808 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001809 ret = 0;
1810 if (flags & BTRFS_EXTENT_FLAG_DATA)
1811 ret = scrub_checksum_data(sblock);
1812 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1813 ret = scrub_checksum_tree_block(sblock);
1814 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1815 (void)scrub_checksum_super(sblock);
1816 else
1817 WARN_ON(1);
1818 if (ret)
1819 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001820
1821 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001822}
1823
1824static int scrub_checksum_data(struct scrub_block *sblock)
1825{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001826 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001827 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001828 u8 *on_disk_csum;
1829 struct page *page;
1830 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001831 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001832 u64 len;
1833 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001834
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001835 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001836 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001837 return 0;
1838
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001839 on_disk_csum = sblock->pagev[0]->csum;
1840 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001841 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001842
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001843 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001844 index = 0;
1845 for (;;) {
1846 u64 l = min_t(u64, len, PAGE_SIZE);
1847
Liu Bob0496682013-03-14 14:57:45 +00001848 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001849 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001850 len -= l;
1851 if (len == 0)
1852 break;
1853 index++;
1854 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001855 BUG_ON(!sblock->pagev[index]->page);
1856 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001857 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001858 }
1859
Arne Jansena2de7332011-03-08 14:14:00 +01001860 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001861 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001862 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001863
Zhao Leiba7cf982015-08-24 21:18:02 +08001864 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001865}
1866
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001867static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001868{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001869 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001870 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001871 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001872 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001873 u8 calculated_csum[BTRFS_CSUM_SIZE];
1874 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1875 struct page *page;
1876 void *mapped_buffer;
1877 u64 mapped_size;
1878 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001879 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001880 u64 len;
1881 int index;
1882
1883 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001884 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001885 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001886 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001887 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001888
1889 /*
1890 * we don't use the getter functions here, as we
1891 * a) don't have an extent buffer and
1892 * b) the page is already kmapped
1893 */
Qu Wenruo3cae2102013-07-16 11:19:18 +08001894 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08001895 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001896
Zhao Leiba7cf982015-08-24 21:18:02 +08001897 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
1898 sblock->header_error = 1;
1899 sblock->generation_error = 1;
1900 }
Arne Jansena2de7332011-03-08 14:14:00 +01001901
Miao Xie17a9be22014-07-24 11:37:08 +08001902 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Zhao Leiba7cf982015-08-24 21:18:02 +08001903 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001904
1905 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1906 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08001907 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001908
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001909 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001910 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1911 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1912 index = 0;
1913 for (;;) {
1914 u64 l = min_t(u64, len, mapped_size);
1915
Liu Bob0496682013-03-14 14:57:45 +00001916 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001917 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001918 len -= l;
1919 if (len == 0)
1920 break;
1921 index++;
1922 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001923 BUG_ON(!sblock->pagev[index]->page);
1924 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001925 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001926 mapped_size = PAGE_SIZE;
1927 p = mapped_buffer;
1928 }
1929
1930 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001931 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08001932 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01001933
Zhao Leiba7cf982015-08-24 21:18:02 +08001934 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001935}
1936
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001937static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001938{
1939 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001940 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001941 u8 calculated_csum[BTRFS_CSUM_SIZE];
1942 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1943 struct page *page;
1944 void *mapped_buffer;
1945 u64 mapped_size;
1946 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001947 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001948 int fail_gen = 0;
1949 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001950 u64 len;
1951 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001952
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001953 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001954 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001955 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001956 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001957 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001958
Qu Wenruo3cae2102013-07-16 11:19:18 +08001959 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001960 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001961
Qu Wenruo3cae2102013-07-16 11:19:18 +08001962 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001963 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001964
Miao Xie17a9be22014-07-24 11:37:08 +08001965 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001966 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001967
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001968 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1969 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1970 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1971 index = 0;
1972 for (;;) {
1973 u64 l = min_t(u64, len, mapped_size);
1974
Liu Bob0496682013-03-14 14:57:45 +00001975 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001976 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001977 len -= l;
1978 if (len == 0)
1979 break;
1980 index++;
1981 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001982 BUG_ON(!sblock->pagev[index]->page);
1983 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001984 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001985 mapped_size = PAGE_SIZE;
1986 p = mapped_buffer;
1987 }
1988
1989 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001990 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001991 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001992
Stefan Behrens442a4f62012-05-25 16:06:08 +02001993 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001994 /*
1995 * if we find an error in a super block, we just report it.
1996 * They will get written with the next transaction commit
1997 * anyway
1998 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001999 spin_lock(&sctx->stat_lock);
2000 ++sctx->stat.super_errors;
2001 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002002 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002003 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002004 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2005 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002006 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002007 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002008 }
2009
Stefan Behrens442a4f62012-05-25 16:06:08 +02002010 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002011}
2012
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002013static void scrub_block_get(struct scrub_block *sblock)
2014{
Zhao Lei57019342015-01-20 15:11:45 +08002015 atomic_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002016}
2017
2018static void scrub_block_put(struct scrub_block *sblock)
2019{
Zhao Lei57019342015-01-20 15:11:45 +08002020 if (atomic_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002021 int i;
2022
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002023 if (sblock->sparity)
2024 scrub_parity_put(sblock->sparity);
2025
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002026 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002027 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002028 kfree(sblock);
2029 }
2030}
2031
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002032static void scrub_page_get(struct scrub_page *spage)
2033{
Zhao Lei57019342015-01-20 15:11:45 +08002034 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002035}
2036
2037static void scrub_page_put(struct scrub_page *spage)
2038{
Zhao Lei57019342015-01-20 15:11:45 +08002039 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002040 if (spage->page)
2041 __free_page(spage->page);
2042 kfree(spage);
2043 }
2044}
2045
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002046static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002047{
2048 struct scrub_bio *sbio;
2049
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002050 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002051 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002052
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002053 sbio = sctx->bios[sctx->curr];
2054 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002055 scrub_pending_bio_inc(sctx);
Omar Sandoval03679ad2015-06-19 11:52:48 -07002056 btrfsic_submit_bio(READ, sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002057}
2058
Stefan Behrensff023aa2012-11-06 11:43:11 +01002059static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2060 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002061{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002062 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002063 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002064 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002065
2066again:
2067 /*
2068 * grab a fresh bio or wait for one to become available
2069 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002070 while (sctx->curr == -1) {
2071 spin_lock(&sctx->list_lock);
2072 sctx->curr = sctx->first_free;
2073 if (sctx->curr != -1) {
2074 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2075 sctx->bios[sctx->curr]->next_free = -1;
2076 sctx->bios[sctx->curr]->page_count = 0;
2077 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002078 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002079 spin_unlock(&sctx->list_lock);
2080 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002081 }
2082 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002083 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002084 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002085 struct bio *bio;
2086
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002087 sbio->physical = spage->physical;
2088 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002089 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002090 bio = sbio->bio;
2091 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002092 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002093 if (!bio)
2094 return -ENOMEM;
2095 sbio->bio = bio;
2096 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002097
2098 bio->bi_private = sbio;
2099 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002100 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002101 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002102 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002103 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2104 spage->physical ||
2105 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002106 spage->logical ||
2107 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002108 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002109 goto again;
2110 }
2111
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002112 sbio->pagev[sbio->page_count] = spage;
2113 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2114 if (ret != PAGE_SIZE) {
2115 if (sbio->page_count < 1) {
2116 bio_put(sbio->bio);
2117 sbio->bio = NULL;
2118 return -EIO;
2119 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002120 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002121 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002122 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002123
Stefan Behrensff023aa2012-11-06 11:43:11 +01002124 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002125 atomic_inc(&sblock->outstanding_pages);
2126 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002127 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002128 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002129
2130 return 0;
2131}
2132
Linus Torvalds22365972015-09-05 15:14:43 -07002133static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002134{
2135 struct scrub_block *sblock = bio->bi_private;
2136 struct btrfs_fs_info *fs_info = sblock->sctx->dev_root->fs_info;
2137
Linus Torvalds22365972015-09-05 15:14:43 -07002138 if (bio->bi_error)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002139 sblock->no_io_error_seen = 0;
2140
2141 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2142}
2143
2144static void scrub_missing_raid56_worker(struct btrfs_work *work)
2145{
2146 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2147 struct scrub_ctx *sctx = sblock->sctx;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002148 u64 logical;
2149 struct btrfs_device *dev;
2150
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002151 logical = sblock->pagev[0]->logical;
2152 dev = sblock->pagev[0]->dev;
2153
2154 if (sblock->no_io_error_seen) {
Zhao Leiba7cf982015-08-24 21:18:02 +08002155 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002156 }
2157
2158 if (!sblock->no_io_error_seen) {
2159 spin_lock(&sctx->stat_lock);
2160 sctx->stat.read_errors++;
2161 spin_unlock(&sctx->stat_lock);
Zhao Leiba7cf982015-08-24 21:18:02 +08002162 btrfs_err_rl_in_rcu(sctx->dev_root->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002163 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002164 logical, rcu_str_deref(dev->name));
2165 } else if (sblock->header_error || sblock->checksum_error) {
2166 spin_lock(&sctx->stat_lock);
2167 sctx->stat.uncorrectable_errors++;
2168 spin_unlock(&sctx->stat_lock);
Zhao Leiba7cf982015-08-24 21:18:02 +08002169 btrfs_err_rl_in_rcu(sctx->dev_root->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002170 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002171 logical, rcu_str_deref(dev->name));
2172 } else {
2173 scrub_write_block_to_dev_replace(sblock);
2174 }
2175
2176 scrub_block_put(sblock);
2177
2178 if (sctx->is_dev_replace &&
2179 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2180 mutex_lock(&sctx->wr_ctx.wr_lock);
2181 scrub_wr_submit(sctx);
2182 mutex_unlock(&sctx->wr_ctx.wr_lock);
2183 }
2184
2185 scrub_pending_bio_dec(sctx);
2186}
2187
2188static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2189{
2190 struct scrub_ctx *sctx = sblock->sctx;
2191 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2192 u64 length = sblock->page_count * PAGE_SIZE;
2193 u64 logical = sblock->pagev[0]->logical;
2194 struct btrfs_bio *bbio;
2195 struct bio *bio;
2196 struct btrfs_raid_bio *rbio;
2197 int ret;
2198 int i;
2199
2200 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical, &length,
2201 &bbio, 0, 1);
2202 if (ret || !bbio || !bbio->raid_map)
2203 goto bbio_out;
2204
2205 if (WARN_ON(!sctx->is_dev_replace ||
2206 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2207 /*
2208 * We shouldn't be scrubbing a missing device. Even for dev
2209 * replace, we should only get here for RAID 5/6. We either
2210 * managed to mount something with no mirrors remaining or
2211 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2212 */
2213 goto bbio_out;
2214 }
2215
2216 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2217 if (!bio)
2218 goto bbio_out;
2219
2220 bio->bi_iter.bi_sector = logical >> 9;
2221 bio->bi_private = sblock;
2222 bio->bi_end_io = scrub_missing_raid56_end_io;
2223
2224 rbio = raid56_alloc_missing_rbio(sctx->dev_root, bio, bbio, length);
2225 if (!rbio)
2226 goto rbio_out;
2227
2228 for (i = 0; i < sblock->page_count; i++) {
2229 struct scrub_page *spage = sblock->pagev[i];
2230
2231 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2232 }
2233
2234 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2235 scrub_missing_raid56_worker, NULL, NULL);
2236 scrub_block_get(sblock);
2237 scrub_pending_bio_inc(sctx);
2238 raid56_submit_missing_rbio(rbio);
2239 return;
2240
2241rbio_out:
2242 bio_put(bio);
2243bbio_out:
2244 btrfs_put_bbio(bbio);
2245 spin_lock(&sctx->stat_lock);
2246 sctx->stat.malloc_errors++;
2247 spin_unlock(&sctx->stat_lock);
2248}
2249
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002250static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002251 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002252 u64 gen, int mirror_num, u8 *csum, int force,
2253 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002254{
2255 struct scrub_block *sblock;
2256 int index;
2257
2258 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2259 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002260 spin_lock(&sctx->stat_lock);
2261 sctx->stat.malloc_errors++;
2262 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002263 return -ENOMEM;
2264 }
2265
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002266 /* one ref inside this function, plus one for each page added to
2267 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002268 atomic_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002269 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002270 sblock->no_io_error_seen = 1;
2271
2272 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002273 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002274 u64 l = min_t(u64, len, PAGE_SIZE);
2275
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002276 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2277 if (!spage) {
2278leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002279 spin_lock(&sctx->stat_lock);
2280 sctx->stat.malloc_errors++;
2281 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002282 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002283 return -ENOMEM;
2284 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002285 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2286 scrub_page_get(spage);
2287 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002288 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002289 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002290 spage->flags = flags;
2291 spage->generation = gen;
2292 spage->logical = logical;
2293 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002294 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002295 spage->mirror_num = mirror_num;
2296 if (csum) {
2297 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002298 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002299 } else {
2300 spage->have_csum = 0;
2301 }
2302 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002303 spage->page = alloc_page(GFP_NOFS);
2304 if (!spage->page)
2305 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002306 len -= l;
2307 logical += l;
2308 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002309 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002310 }
2311
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002312 WARN_ON(sblock->page_count == 0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002313 if (dev->missing) {
2314 /*
2315 * This case should only be hit for RAID 5/6 device replace. See
2316 * the comment in scrub_missing_raid56_pages() for details.
2317 */
2318 scrub_missing_raid56_pages(sblock);
2319 } else {
2320 for (index = 0; index < sblock->page_count; index++) {
2321 struct scrub_page *spage = sblock->pagev[index];
2322 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002323
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002324 ret = scrub_add_page_to_rd_bio(sctx, spage);
2325 if (ret) {
2326 scrub_block_put(sblock);
2327 return ret;
2328 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002329 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002330
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002331 if (force)
2332 scrub_submit(sctx);
2333 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002334
2335 /* last one frees, either here or in bio completion for last page */
2336 scrub_block_put(sblock);
2337 return 0;
2338}
2339
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002340static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002341{
2342 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002343 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002344
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002345 sbio->err = bio->bi_error;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002346 sbio->bio = bio;
2347
Qu Wenruo0339ef22014-02-28 10:46:17 +08002348 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002349}
2350
2351static void scrub_bio_end_io_worker(struct btrfs_work *work)
2352{
2353 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002354 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002355 int i;
2356
Stefan Behrensff023aa2012-11-06 11:43:11 +01002357 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002358 if (sbio->err) {
2359 for (i = 0; i < sbio->page_count; i++) {
2360 struct scrub_page *spage = sbio->pagev[i];
2361
2362 spage->io_error = 1;
2363 spage->sblock->no_io_error_seen = 0;
2364 }
2365 }
2366
2367 /* now complete the scrub_block items that have all pages completed */
2368 for (i = 0; i < sbio->page_count; i++) {
2369 struct scrub_page *spage = sbio->pagev[i];
2370 struct scrub_block *sblock = spage->sblock;
2371
2372 if (atomic_dec_and_test(&sblock->outstanding_pages))
2373 scrub_block_complete(sblock);
2374 scrub_block_put(sblock);
2375 }
2376
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002377 bio_put(sbio->bio);
2378 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002379 spin_lock(&sctx->list_lock);
2380 sbio->next_free = sctx->first_free;
2381 sctx->first_free = sbio->index;
2382 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002383
2384 if (sctx->is_dev_replace &&
2385 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2386 mutex_lock(&sctx->wr_ctx.wr_lock);
2387 scrub_wr_submit(sctx);
2388 mutex_unlock(&sctx->wr_ctx.wr_lock);
2389 }
2390
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002391 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002392}
2393
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002394static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2395 unsigned long *bitmap,
2396 u64 start, u64 len)
2397{
David Sterba9d644a62015-02-20 18:42:11 +01002398 u32 offset;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002399 int nsectors;
2400 int sectorsize = sparity->sctx->dev_root->sectorsize;
2401
2402 if (len >= sparity->stripe_len) {
2403 bitmap_set(bitmap, 0, sparity->nsectors);
2404 return;
2405 }
2406
2407 start -= sparity->logic_start;
David Sterba47c57132015-02-20 18:43:47 +01002408 start = div_u64_rem(start, sparity->stripe_len, &offset);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002409 offset /= sectorsize;
2410 nsectors = (int)len / sectorsize;
2411
2412 if (offset + nsectors <= sparity->nsectors) {
2413 bitmap_set(bitmap, offset, nsectors);
2414 return;
2415 }
2416
2417 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2418 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2419}
2420
2421static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2422 u64 start, u64 len)
2423{
2424 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2425}
2426
2427static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2428 u64 start, u64 len)
2429{
2430 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2431}
2432
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002433static void scrub_block_complete(struct scrub_block *sblock)
2434{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002435 int corrupted = 0;
2436
Stefan Behrensff023aa2012-11-06 11:43:11 +01002437 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002438 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002439 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002440 } else {
2441 /*
2442 * if has checksum error, write via repair mechanism in
2443 * dev replace case, otherwise write here in dev replace
2444 * case.
2445 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002446 corrupted = scrub_checksum(sblock);
2447 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002448 scrub_write_block_to_dev_replace(sblock);
2449 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002450
2451 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2452 u64 start = sblock->pagev[0]->logical;
2453 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2454 PAGE_SIZE;
2455
2456 scrub_parity_mark_sectors_error(sblock->sparity,
2457 start, end - start);
2458 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002459}
2460
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002461static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002462 u8 *csum)
2463{
2464 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002465 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002466 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002467
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002468 while (!list_empty(&sctx->csum_list)) {
2469 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002470 struct btrfs_ordered_sum, list);
2471 if (sum->bytenr > logical)
2472 return 0;
2473 if (sum->bytenr + sum->len > logical)
2474 break;
2475
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002476 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002477 list_del(&sum->list);
2478 kfree(sum);
2479 sum = NULL;
2480 }
2481 if (!sum)
2482 return 0;
2483
Miao Xief51a4a12013-06-19 10:36:09 +08002484 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002485 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002486 memcpy(csum, sum->sums + index, sctx->csum_size);
2487 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002488 list_del(&sum->list);
2489 kfree(sum);
2490 }
Miao Xief51a4a12013-06-19 10:36:09 +08002491 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002492}
2493
2494/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002495static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002496 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002497 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002498{
2499 int ret;
2500 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002501 u32 blocksize;
2502
2503 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002504 blocksize = sctx->sectorsize;
2505 spin_lock(&sctx->stat_lock);
2506 sctx->stat.data_extents_scrubbed++;
2507 sctx->stat.data_bytes_scrubbed += len;
2508 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002509 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002510 blocksize = sctx->nodesize;
2511 spin_lock(&sctx->stat_lock);
2512 sctx->stat.tree_extents_scrubbed++;
2513 sctx->stat.tree_bytes_scrubbed += len;
2514 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002515 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002516 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002517 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002518 }
Arne Jansena2de7332011-03-08 14:14:00 +01002519
2520 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002521 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002522 int have_csum = 0;
2523
2524 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2525 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002526 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002527 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002528 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002529 if (sctx->is_dev_replace && !have_csum) {
2530 ret = copy_nocow_pages(sctx, logical, l,
2531 mirror_num,
2532 physical_for_dev_replace);
2533 goto behind_scrub_pages;
2534 }
Arne Jansena2de7332011-03-08 14:14:00 +01002535 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002536 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002537 mirror_num, have_csum ? csum : NULL, 0,
2538 physical_for_dev_replace);
2539behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002540 if (ret)
2541 return ret;
2542 len -= l;
2543 logical += l;
2544 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002545 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002546 }
2547 return 0;
2548}
2549
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002550static int scrub_pages_for_parity(struct scrub_parity *sparity,
2551 u64 logical, u64 len,
2552 u64 physical, struct btrfs_device *dev,
2553 u64 flags, u64 gen, int mirror_num, u8 *csum)
2554{
2555 struct scrub_ctx *sctx = sparity->sctx;
2556 struct scrub_block *sblock;
2557 int index;
2558
2559 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2560 if (!sblock) {
2561 spin_lock(&sctx->stat_lock);
2562 sctx->stat.malloc_errors++;
2563 spin_unlock(&sctx->stat_lock);
2564 return -ENOMEM;
2565 }
2566
2567 /* one ref inside this function, plus one for each page added to
2568 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002569 atomic_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002570 sblock->sctx = sctx;
2571 sblock->no_io_error_seen = 1;
2572 sblock->sparity = sparity;
2573 scrub_parity_get(sparity);
2574
2575 for (index = 0; len > 0; index++) {
2576 struct scrub_page *spage;
2577 u64 l = min_t(u64, len, PAGE_SIZE);
2578
2579 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2580 if (!spage) {
2581leave_nomem:
2582 spin_lock(&sctx->stat_lock);
2583 sctx->stat.malloc_errors++;
2584 spin_unlock(&sctx->stat_lock);
2585 scrub_block_put(sblock);
2586 return -ENOMEM;
2587 }
2588 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2589 /* For scrub block */
2590 scrub_page_get(spage);
2591 sblock->pagev[index] = spage;
2592 /* For scrub parity */
2593 scrub_page_get(spage);
2594 list_add_tail(&spage->list, &sparity->spages);
2595 spage->sblock = sblock;
2596 spage->dev = dev;
2597 spage->flags = flags;
2598 spage->generation = gen;
2599 spage->logical = logical;
2600 spage->physical = physical;
2601 spage->mirror_num = mirror_num;
2602 if (csum) {
2603 spage->have_csum = 1;
2604 memcpy(spage->csum, csum, sctx->csum_size);
2605 } else {
2606 spage->have_csum = 0;
2607 }
2608 sblock->page_count++;
2609 spage->page = alloc_page(GFP_NOFS);
2610 if (!spage->page)
2611 goto leave_nomem;
2612 len -= l;
2613 logical += l;
2614 physical += l;
2615 }
2616
2617 WARN_ON(sblock->page_count == 0);
2618 for (index = 0; index < sblock->page_count; index++) {
2619 struct scrub_page *spage = sblock->pagev[index];
2620 int ret;
2621
2622 ret = scrub_add_page_to_rd_bio(sctx, spage);
2623 if (ret) {
2624 scrub_block_put(sblock);
2625 return ret;
2626 }
2627 }
2628
2629 /* last one frees, either here or in bio completion for last page */
2630 scrub_block_put(sblock);
2631 return 0;
2632}
2633
2634static int scrub_extent_for_parity(struct scrub_parity *sparity,
2635 u64 logical, u64 len,
2636 u64 physical, struct btrfs_device *dev,
2637 u64 flags, u64 gen, int mirror_num)
2638{
2639 struct scrub_ctx *sctx = sparity->sctx;
2640 int ret;
2641 u8 csum[BTRFS_CSUM_SIZE];
2642 u32 blocksize;
2643
Omar Sandoval4a770892015-06-19 11:52:52 -07002644 if (dev->missing) {
2645 scrub_parity_mark_sectors_error(sparity, logical, len);
2646 return 0;
2647 }
2648
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002649 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2650 blocksize = sctx->sectorsize;
2651 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2652 blocksize = sctx->nodesize;
2653 } else {
2654 blocksize = sctx->sectorsize;
2655 WARN_ON(1);
2656 }
2657
2658 while (len) {
2659 u64 l = min_t(u64, len, blocksize);
2660 int have_csum = 0;
2661
2662 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2663 /* push csums to sbio */
2664 have_csum = scrub_find_csum(sctx, logical, l, csum);
2665 if (have_csum == 0)
2666 goto skip;
2667 }
2668 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2669 flags, gen, mirror_num,
2670 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002671 if (ret)
2672 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002673skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002674 len -= l;
2675 logical += l;
2676 physical += l;
2677 }
2678 return 0;
2679}
2680
Wang Shilong3b080b22014-04-01 18:01:43 +08002681/*
2682 * Given a physical address, this will calculate it's
2683 * logical offset. if this is a parity stripe, it will return
2684 * the most left data stripe's logical offset.
2685 *
2686 * return 0 if it is a data stripe, 1 means parity stripe.
2687 */
2688static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002689 struct map_lookup *map, u64 *offset,
2690 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002691{
2692 int i;
2693 int j = 0;
2694 u64 stripe_nr;
2695 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002696 u32 stripe_index;
2697 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002698
2699 last_offset = (physical - map->stripes[num].physical) *
2700 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002701 if (stripe_start)
2702 *stripe_start = last_offset;
2703
Wang Shilong3b080b22014-04-01 18:01:43 +08002704 *offset = last_offset;
2705 for (i = 0; i < nr_data_stripes(map); i++) {
2706 *offset = last_offset + i * map->stripe_len;
2707
David Sterbab8b93ad2015-01-16 17:26:13 +01002708 stripe_nr = div_u64(*offset, map->stripe_len);
2709 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002710
2711 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002712 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002713 /* calculate which stripe this data locates */
2714 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002715 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002716 if (stripe_index == num)
2717 return 0;
2718 if (stripe_index < num)
2719 j++;
2720 }
2721 *offset = last_offset + j * map->stripe_len;
2722 return 1;
2723}
2724
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002725static void scrub_free_parity(struct scrub_parity *sparity)
2726{
2727 struct scrub_ctx *sctx = sparity->sctx;
2728 struct scrub_page *curr, *next;
2729 int nbits;
2730
2731 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2732 if (nbits) {
2733 spin_lock(&sctx->stat_lock);
2734 sctx->stat.read_errors += nbits;
2735 sctx->stat.uncorrectable_errors += nbits;
2736 spin_unlock(&sctx->stat_lock);
2737 }
2738
2739 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2740 list_del_init(&curr->list);
2741 scrub_page_put(curr);
2742 }
2743
2744 kfree(sparity);
2745}
2746
Zhao Lei20b2e302015-06-04 20:09:15 +08002747static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2748{
2749 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2750 work);
2751 struct scrub_ctx *sctx = sparity->sctx;
2752
2753 scrub_free_parity(sparity);
2754 scrub_pending_bio_dec(sctx);
2755}
2756
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002757static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002758{
2759 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002760
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002761 if (bio->bi_error)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002762 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2763 sparity->nsectors);
2764
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002765 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002766
2767 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2768 scrub_parity_bio_endio_worker, NULL, NULL);
2769 btrfs_queue_work(sparity->sctx->dev_root->fs_info->scrub_parity_workers,
2770 &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002771}
2772
2773static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2774{
2775 struct scrub_ctx *sctx = sparity->sctx;
2776 struct bio *bio;
2777 struct btrfs_raid_bio *rbio;
2778 struct scrub_page *spage;
2779 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002780 u64 length;
2781 int ret;
2782
2783 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2784 sparity->nsectors))
2785 goto out;
2786
Zhao Leia0dd59d2015-07-21 15:42:26 +08002787 length = sparity->logic_end - sparity->logic_start;
Miao Xie76035972014-11-14 17:45:42 +08002788 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002789 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002790 &length, &bbio, 0, 1);
2791 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002792 goto bbio_out;
2793
2794 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2795 if (!bio)
2796 goto bbio_out;
2797
2798 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2799 bio->bi_private = sparity;
2800 bio->bi_end_io = scrub_parity_bio_endio;
2801
2802 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002803 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002804 sparity->dbitmap,
2805 sparity->nsectors);
2806 if (!rbio)
2807 goto rbio_out;
2808
2809 list_for_each_entry(spage, &sparity->spages, list)
Omar Sandovalb4ee1782015-06-19 11:52:50 -07002810 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002811
2812 scrub_pending_bio_inc(sctx);
2813 raid56_parity_submit_scrub_rbio(rbio);
2814 return;
2815
2816rbio_out:
2817 bio_put(bio);
2818bbio_out:
Zhao Lei6e9606d2015-01-20 15:11:34 +08002819 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002820 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2821 sparity->nsectors);
2822 spin_lock(&sctx->stat_lock);
2823 sctx->stat.malloc_errors++;
2824 spin_unlock(&sctx->stat_lock);
2825out:
2826 scrub_free_parity(sparity);
2827}
2828
2829static inline int scrub_calc_parity_bitmap_len(int nsectors)
2830{
2831 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2832}
2833
2834static void scrub_parity_get(struct scrub_parity *sparity)
2835{
Zhao Lei57019342015-01-20 15:11:45 +08002836 atomic_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002837}
2838
2839static void scrub_parity_put(struct scrub_parity *sparity)
2840{
Zhao Lei57019342015-01-20 15:11:45 +08002841 if (!atomic_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002842 return;
2843
2844 scrub_parity_check_and_repair(sparity);
2845}
2846
2847static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2848 struct map_lookup *map,
2849 struct btrfs_device *sdev,
2850 struct btrfs_path *path,
2851 u64 logic_start,
2852 u64 logic_end)
2853{
2854 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2855 struct btrfs_root *root = fs_info->extent_root;
2856 struct btrfs_root *csum_root = fs_info->csum_root;
2857 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07002858 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002859 u64 flags;
2860 int ret;
2861 int slot;
2862 struct extent_buffer *l;
2863 struct btrfs_key key;
2864 u64 generation;
2865 u64 extent_logical;
2866 u64 extent_physical;
2867 u64 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07002868 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002869 struct btrfs_device *extent_dev;
2870 struct scrub_parity *sparity;
2871 int nsectors;
2872 int bitmap_len;
2873 int extent_mirror_num;
2874 int stop_loop = 0;
2875
2876 nsectors = map->stripe_len / root->sectorsize;
2877 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2878 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2879 GFP_NOFS);
2880 if (!sparity) {
2881 spin_lock(&sctx->stat_lock);
2882 sctx->stat.malloc_errors++;
2883 spin_unlock(&sctx->stat_lock);
2884 return -ENOMEM;
2885 }
2886
2887 sparity->stripe_len = map->stripe_len;
2888 sparity->nsectors = nsectors;
2889 sparity->sctx = sctx;
2890 sparity->scrub_dev = sdev;
2891 sparity->logic_start = logic_start;
2892 sparity->logic_end = logic_end;
Zhao Lei57019342015-01-20 15:11:45 +08002893 atomic_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002894 INIT_LIST_HEAD(&sparity->spages);
2895 sparity->dbitmap = sparity->bitmap;
2896 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2897
2898 ret = 0;
2899 while (logic_start < logic_end) {
2900 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2901 key.type = BTRFS_METADATA_ITEM_KEY;
2902 else
2903 key.type = BTRFS_EXTENT_ITEM_KEY;
2904 key.objectid = logic_start;
2905 key.offset = (u64)-1;
2906
2907 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2908 if (ret < 0)
2909 goto out;
2910
2911 if (ret > 0) {
2912 ret = btrfs_previous_extent_item(root, path, 0);
2913 if (ret < 0)
2914 goto out;
2915 if (ret > 0) {
2916 btrfs_release_path(path);
2917 ret = btrfs_search_slot(NULL, root, &key,
2918 path, 0, 0);
2919 if (ret < 0)
2920 goto out;
2921 }
2922 }
2923
2924 stop_loop = 0;
2925 while (1) {
2926 u64 bytes;
2927
2928 l = path->nodes[0];
2929 slot = path->slots[0];
2930 if (slot >= btrfs_header_nritems(l)) {
2931 ret = btrfs_next_leaf(root, path);
2932 if (ret == 0)
2933 continue;
2934 if (ret < 0)
2935 goto out;
2936
2937 stop_loop = 1;
2938 break;
2939 }
2940 btrfs_item_key_to_cpu(l, &key, slot);
2941
Zhao Leid7cad232015-07-22 13:14:48 +08002942 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2943 key.type != BTRFS_METADATA_ITEM_KEY)
2944 goto next;
2945
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002946 if (key.type == BTRFS_METADATA_ITEM_KEY)
2947 bytes = root->nodesize;
2948 else
2949 bytes = key.offset;
2950
2951 if (key.objectid + bytes <= logic_start)
2952 goto next;
2953
Zhao Leia0dd59d2015-07-21 15:42:26 +08002954 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002955 stop_loop = 1;
2956 break;
2957 }
2958
2959 while (key.objectid >= logic_start + map->stripe_len)
2960 logic_start += map->stripe_len;
2961
2962 extent = btrfs_item_ptr(l, slot,
2963 struct btrfs_extent_item);
2964 flags = btrfs_extent_flags(l, extent);
2965 generation = btrfs_extent_generation(l, extent);
2966
Zhao Leia323e812015-07-23 12:29:49 +08002967 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
2968 (key.objectid < logic_start ||
2969 key.objectid + bytes >
2970 logic_start + map->stripe_len)) {
2971 btrfs_err(fs_info, "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
2972 key.objectid, logic_start);
Zhao Lei9799d2c2015-08-25 21:31:40 +08002973 spin_lock(&sctx->stat_lock);
2974 sctx->stat.uncorrectable_errors++;
2975 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002976 goto next;
2977 }
2978again:
2979 extent_logical = key.objectid;
2980 extent_len = bytes;
2981
2982 if (extent_logical < logic_start) {
2983 extent_len -= logic_start - extent_logical;
2984 extent_logical = logic_start;
2985 }
2986
2987 if (extent_logical + extent_len >
2988 logic_start + map->stripe_len)
2989 extent_len = logic_start + map->stripe_len -
2990 extent_logical;
2991
2992 scrub_parity_mark_sectors_data(sparity, extent_logical,
2993 extent_len);
2994
Omar Sandoval4a770892015-06-19 11:52:52 -07002995 mapped_length = extent_len;
2996 ret = btrfs_map_block(fs_info, READ, extent_logical,
2997 &mapped_length, &bbio, 0);
2998 if (!ret) {
2999 if (!bbio || mapped_length < extent_len)
3000 ret = -EIO;
3001 }
3002 if (ret) {
3003 btrfs_put_bbio(bbio);
3004 goto out;
3005 }
3006 extent_physical = bbio->stripes[0].physical;
3007 extent_mirror_num = bbio->mirror_num;
3008 extent_dev = bbio->stripes[0].dev;
3009 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003010
3011 ret = btrfs_lookup_csums_range(csum_root,
3012 extent_logical,
3013 extent_logical + extent_len - 1,
3014 &sctx->csum_list, 1);
3015 if (ret)
3016 goto out;
3017
3018 ret = scrub_extent_for_parity(sparity, extent_logical,
3019 extent_len,
3020 extent_physical,
3021 extent_dev, flags,
3022 generation,
3023 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003024
3025 scrub_free_csums(sctx);
3026
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003027 if (ret)
3028 goto out;
3029
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003030 if (extent_logical + extent_len <
3031 key.objectid + bytes) {
3032 logic_start += map->stripe_len;
3033
3034 if (logic_start >= logic_end) {
3035 stop_loop = 1;
3036 break;
3037 }
3038
3039 if (logic_start < key.objectid + bytes) {
3040 cond_resched();
3041 goto again;
3042 }
3043 }
3044next:
3045 path->slots[0]++;
3046 }
3047
3048 btrfs_release_path(path);
3049
3050 if (stop_loop)
3051 break;
3052
3053 logic_start += map->stripe_len;
3054 }
3055out:
3056 if (ret < 0)
3057 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003058 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003059 scrub_parity_put(sparity);
3060 scrub_submit(sctx);
3061 mutex_lock(&sctx->wr_ctx.wr_lock);
3062 scrub_wr_submit(sctx);
3063 mutex_unlock(&sctx->wr_ctx.wr_lock);
3064
3065 btrfs_release_path(path);
3066 return ret < 0 ? ret : 0;
3067}
3068
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003069static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003070 struct map_lookup *map,
3071 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003072 int num, u64 base, u64 length,
3073 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003074{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003075 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003076 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003077 struct btrfs_root *root = fs_info->extent_root;
3078 struct btrfs_root *csum_root = fs_info->csum_root;
3079 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003080 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003081 u64 flags;
3082 int ret;
3083 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003084 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003085 struct extent_buffer *l;
3086 struct btrfs_key key;
3087 u64 physical;
3088 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003089 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003090 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003091 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003092 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003093 struct reada_control *reada1;
3094 struct reada_control *reada2;
3095 struct btrfs_key key_start;
3096 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003097 u64 increment = map->stripe_len;
3098 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003099 u64 extent_logical;
3100 u64 extent_physical;
3101 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003102 u64 stripe_logical;
3103 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003104 struct btrfs_device *extent_dev;
3105 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003106 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003107
Wang Shilong3b080b22014-04-01 18:01:43 +08003108 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003109 offset = 0;
David Sterbab8b93ad2015-01-16 17:26:13 +01003110 nstripes = div_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003111 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3112 offset = map->stripe_len * num;
3113 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003114 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003115 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3116 int factor = map->num_stripes / map->sub_stripes;
3117 offset = map->stripe_len * (num / map->sub_stripes);
3118 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003119 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003120 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3121 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003122 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003123 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3124 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003125 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003126 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003127 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003128 increment = map->stripe_len * nr_data_stripes(map);
3129 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003130 } else {
3131 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003132 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003133 }
3134
3135 path = btrfs_alloc_path();
3136 if (!path)
3137 return -ENOMEM;
3138
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003139 ppath = btrfs_alloc_path();
3140 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003141 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003142 return -ENOMEM;
3143 }
3144
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003145 /*
3146 * work on commit root. The related disk blocks are static as
3147 * long as COW is applied. This means, it is save to rewrite
3148 * them to repair disk errors without any race conditions
3149 */
Arne Jansena2de7332011-03-08 14:14:00 +01003150 path->search_commit_root = 1;
3151 path->skip_locking = 1;
3152
Gui Hecheng063c54d2015-01-09 09:39:40 +08003153 ppath->search_commit_root = 1;
3154 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003155 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003156 * trigger the readahead for extent tree csum tree and wait for
3157 * completion. During readahead, the scrub is officially paused
3158 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003159 */
3160 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003161 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003162 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003163 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003164 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003165 logic_end += base;
3166 } else {
3167 logic_end = logical + increment * nstripes;
3168 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003169 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003170 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003171 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003172
Arne Jansen7a262852011-06-10 12:39:23 +02003173 /* FIXME it might be better to start readahead at commit root */
3174 key_start.objectid = logical;
3175 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3176 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003177 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003178 key_end.type = BTRFS_METADATA_ITEM_KEY;
3179 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003180 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003181
Arne Jansen7a262852011-06-10 12:39:23 +02003182 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3183 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3184 key_start.offset = logical;
3185 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3186 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003187 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003188 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003189
Arne Jansen7a262852011-06-10 12:39:23 +02003190 if (!IS_ERR(reada1))
3191 btrfs_reada_wait(reada1);
3192 if (!IS_ERR(reada2))
3193 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003194
Arne Jansena2de7332011-03-08 14:14:00 +01003195
3196 /*
3197 * collect all data csums for the stripe to avoid seeking during
3198 * the scrub. This might currently (crc32) end up to be about 1MB
3199 */
Arne Jansene7786c32011-05-28 20:58:38 +00003200 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003201
Arne Jansena2de7332011-03-08 14:14:00 +01003202 /*
3203 * now find all extents for each stripe and scrub them
3204 */
Arne Jansena2de7332011-03-08 14:14:00 +01003205 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003206 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003207 /*
3208 * canceled?
3209 */
3210 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003211 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003212 ret = -ECANCELED;
3213 goto out;
3214 }
3215 /*
3216 * check to see if we have to pause
3217 */
3218 if (atomic_read(&fs_info->scrub_pause_req)) {
3219 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003220 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003221 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003222 mutex_lock(&sctx->wr_ctx.wr_lock);
3223 scrub_wr_submit(sctx);
3224 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003225 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003226 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003227 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003228 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003229 }
3230
Zhao Leif2f66a22015-07-21 12:22:29 +08003231 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3232 ret = get_raid56_logic_offset(physical, num, map,
3233 &logical,
3234 &stripe_logical);
3235 logical += base;
3236 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003237 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003238 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003239 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003240 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3241 ppath, stripe_logical,
3242 stripe_end);
3243 if (ret)
3244 goto out;
3245 goto skip;
3246 }
3247 }
3248
Wang Shilong7c76edb2014-01-12 21:38:32 +08003249 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3250 key.type = BTRFS_METADATA_ITEM_KEY;
3251 else
3252 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003253 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003254 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003255
3256 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3257 if (ret < 0)
3258 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003259
Arne Jansen8c510322011-06-03 10:09:26 +02003260 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003261 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003262 if (ret < 0)
3263 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003264 if (ret > 0) {
3265 /* there's no smaller item, so stick with the
3266 * larger one */
3267 btrfs_release_path(path);
3268 ret = btrfs_search_slot(NULL, root, &key,
3269 path, 0, 0);
3270 if (ret < 0)
3271 goto out;
3272 }
Arne Jansena2de7332011-03-08 14:14:00 +01003273 }
3274
Liu Bo625f1c8d2013-04-27 02:56:57 +00003275 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003276 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003277 u64 bytes;
3278
Arne Jansena2de7332011-03-08 14:14:00 +01003279 l = path->nodes[0];
3280 slot = path->slots[0];
3281 if (slot >= btrfs_header_nritems(l)) {
3282 ret = btrfs_next_leaf(root, path);
3283 if (ret == 0)
3284 continue;
3285 if (ret < 0)
3286 goto out;
3287
Liu Bo625f1c8d2013-04-27 02:56:57 +00003288 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003289 break;
3290 }
3291 btrfs_item_key_to_cpu(l, &key, slot);
3292
Zhao Leid7cad232015-07-22 13:14:48 +08003293 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3294 key.type != BTRFS_METADATA_ITEM_KEY)
3295 goto next;
3296
Josef Bacik3173a182013-03-07 14:22:04 -05003297 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003298 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003299 else
3300 bytes = key.offset;
3301
3302 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003303 goto next;
3304
Liu Bo625f1c8d2013-04-27 02:56:57 +00003305 if (key.objectid >= logical + map->stripe_len) {
3306 /* out of this device extent */
3307 if (key.objectid >= logic_end)
3308 stop_loop = 1;
3309 break;
3310 }
Arne Jansena2de7332011-03-08 14:14:00 +01003311
3312 extent = btrfs_item_ptr(l, slot,
3313 struct btrfs_extent_item);
3314 flags = btrfs_extent_flags(l, extent);
3315 generation = btrfs_extent_generation(l, extent);
3316
Zhao Leia323e812015-07-23 12:29:49 +08003317 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3318 (key.objectid < logical ||
3319 key.objectid + bytes >
3320 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003321 btrfs_err(fs_info,
3322 "scrub: tree block %llu spanning "
3323 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003324 key.objectid, logical);
Zhao Lei9799d2c2015-08-25 21:31:40 +08003325 spin_lock(&sctx->stat_lock);
3326 sctx->stat.uncorrectable_errors++;
3327 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003328 goto next;
3329 }
3330
Liu Bo625f1c8d2013-04-27 02:56:57 +00003331again:
3332 extent_logical = key.objectid;
3333 extent_len = bytes;
3334
Arne Jansena2de7332011-03-08 14:14:00 +01003335 /*
3336 * trim extent to this stripe
3337 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003338 if (extent_logical < logical) {
3339 extent_len -= logical - extent_logical;
3340 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003341 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003342 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003343 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003344 extent_len = logical + map->stripe_len -
3345 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003346 }
3347
Liu Bo625f1c8d2013-04-27 02:56:57 +00003348 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003349 extent_dev = scrub_dev;
3350 extent_mirror_num = mirror_num;
3351 if (is_dev_replace)
3352 scrub_remap_extent(fs_info, extent_logical,
3353 extent_len, &extent_physical,
3354 &extent_dev,
3355 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003356
Zhao Leife8cf652015-07-22 13:14:47 +08003357 ret = btrfs_lookup_csums_range(csum_root,
3358 extent_logical,
3359 extent_logical +
3360 extent_len - 1,
3361 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003362 if (ret)
3363 goto out;
3364
Liu Bo625f1c8d2013-04-27 02:56:57 +00003365 ret = scrub_extent(sctx, extent_logical, extent_len,
3366 extent_physical, extent_dev, flags,
3367 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003368 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003369
3370 scrub_free_csums(sctx);
3371
Liu Bo625f1c8d2013-04-27 02:56:57 +00003372 if (ret)
3373 goto out;
3374
3375 if (extent_logical + extent_len <
3376 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003377 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003378 /*
3379 * loop until we find next data stripe
3380 * or we have finished all stripes.
3381 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003382loop:
3383 physical += map->stripe_len;
3384 ret = get_raid56_logic_offset(physical,
3385 num, map, &logical,
3386 &stripe_logical);
3387 logical += base;
3388
3389 if (ret && physical < physical_end) {
3390 stripe_logical += base;
3391 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003392 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003393 ret = scrub_raid56_parity(sctx,
3394 map, scrub_dev, ppath,
3395 stripe_logical,
3396 stripe_end);
3397 if (ret)
3398 goto out;
3399 goto loop;
3400 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003401 } else {
3402 physical += map->stripe_len;
3403 logical += increment;
3404 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003405 if (logical < key.objectid + bytes) {
3406 cond_resched();
3407 goto again;
3408 }
3409
Wang Shilong3b080b22014-04-01 18:01:43 +08003410 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003411 stop_loop = 1;
3412 break;
3413 }
3414 }
Arne Jansena2de7332011-03-08 14:14:00 +01003415next:
3416 path->slots[0]++;
3417 }
Chris Mason71267332011-05-23 06:30:52 -04003418 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003419skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003420 logical += increment;
3421 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003422 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003423 if (stop_loop)
3424 sctx->stat.last_physical = map->stripes[num].physical +
3425 length;
3426 else
3427 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003428 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003429 if (stop_loop)
3430 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003431 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003432out:
Arne Jansena2de7332011-03-08 14:14:00 +01003433 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003434 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003435 mutex_lock(&sctx->wr_ctx.wr_lock);
3436 scrub_wr_submit(sctx);
3437 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003438
Arne Jansene7786c32011-05-28 20:58:38 +00003439 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003440 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003441 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003442 return ret < 0 ? ret : 0;
3443}
3444
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003445static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003446 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003447 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003448 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003449{
3450 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003451 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003452 struct map_lookup *map;
3453 struct extent_map *em;
3454 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003455 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003456
3457 read_lock(&map_tree->map_tree.lock);
3458 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3459 read_unlock(&map_tree->map_tree.lock);
3460
3461 if (!em)
3462 return -EINVAL;
3463
3464 map = (struct map_lookup *)em->bdev;
3465 if (em->start != chunk_offset)
3466 goto out;
3467
3468 if (em->len < length)
3469 goto out;
3470
3471 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003472 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003473 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003474 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003475 chunk_offset, length,
3476 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003477 if (ret)
3478 goto out;
3479 }
3480 }
3481out:
3482 free_extent_map(em);
3483
3484 return ret;
3485}
3486
3487static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003488int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003489 struct btrfs_device *scrub_dev, u64 start, u64 end,
3490 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003491{
3492 struct btrfs_dev_extent *dev_extent = NULL;
3493 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003494 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003495 struct btrfs_fs_info *fs_info = root->fs_info;
3496 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003497 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003498 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003499 int slot;
3500 struct extent_buffer *l;
3501 struct btrfs_key key;
3502 struct btrfs_key found_key;
3503 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003504 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003505
3506 path = btrfs_alloc_path();
3507 if (!path)
3508 return -ENOMEM;
3509
3510 path->reada = 2;
3511 path->search_commit_root = 1;
3512 path->skip_locking = 1;
3513
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003514 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003515 key.offset = 0ull;
3516 key.type = BTRFS_DEV_EXTENT_KEY;
3517
Arne Jansena2de7332011-03-08 14:14:00 +01003518 while (1) {
3519 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3520 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003521 break;
3522 if (ret > 0) {
3523 if (path->slots[0] >=
3524 btrfs_header_nritems(path->nodes[0])) {
3525 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003526 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003527 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003528 if (ret > 0) {
3529 ret = 0;
3530 break;
3531 }
3532 } else {
3533 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003534 }
3535 }
Arne Jansena2de7332011-03-08 14:14:00 +01003536
3537 l = path->nodes[0];
3538 slot = path->slots[0];
3539
3540 btrfs_item_key_to_cpu(l, &found_key, slot);
3541
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003542 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003543 break;
3544
David Sterba962a2982014-06-04 18:41:45 +02003545 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003546 break;
3547
3548 if (found_key.offset >= end)
3549 break;
3550
3551 if (found_key.offset < key.offset)
3552 break;
3553
3554 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3555 length = btrfs_dev_extent_length(l, dev_extent);
3556
Qu Wenruoced96ed2014-06-19 10:42:51 +08003557 if (found_key.offset + length <= start)
3558 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003559
Arne Jansena2de7332011-03-08 14:14:00 +01003560 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3561
3562 /*
3563 * get a reference on the corresponding block group to prevent
3564 * the chunk from going away while we scrub it
3565 */
3566 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003567
3568 /* some chunks are removed but not committed to disk yet,
3569 * continue scrubbing */
3570 if (!cache)
3571 goto skip;
3572
Zhaolei55e3a602015-08-05 16:43:30 +08003573 /*
3574 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3575 * to avoid deadlock caused by:
3576 * btrfs_inc_block_group_ro()
3577 * -> btrfs_wait_for_commit()
3578 * -> btrfs_commit_transaction()
3579 * -> btrfs_scrub_pause()
3580 */
3581 scrub_pause_on(fs_info);
3582 ret = btrfs_inc_block_group_ro(root, cache);
3583 scrub_pause_off(fs_info);
3584 if (ret) {
3585 btrfs_put_block_group(cache);
3586 break;
3587 }
3588
Stefan Behrensff023aa2012-11-06 11:43:11 +01003589 dev_replace->cursor_right = found_key.offset + length;
3590 dev_replace->cursor_left = found_key.offset;
3591 dev_replace->item_needs_writeback = 1;
Zhao Lei8c204c92015-08-19 15:02:40 +08003592 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
3593 found_key.offset, is_dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003594
3595 /*
3596 * flush, submit all pending read and write bios, afterwards
3597 * wait for them.
3598 * Note that in the dev replace case, a read request causes
3599 * write requests that are submitted in the read completion
3600 * worker. Therefore in the current situation, it is required
3601 * that all write requests are flushed, so that all read and
3602 * write requests are really completed when bios_in_flight
3603 * changes to 0.
3604 */
3605 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3606 scrub_submit(sctx);
3607 mutex_lock(&sctx->wr_ctx.wr_lock);
3608 scrub_wr_submit(sctx);
3609 mutex_unlock(&sctx->wr_ctx.wr_lock);
3610
3611 wait_event(sctx->list_wait,
3612 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003613
3614 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003615
3616 /*
3617 * must be called before we decrease @scrub_paused.
3618 * make sure we don't block transaction commit while
3619 * we are waiting pending workers finished.
3620 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003621 wait_event(sctx->list_wait,
3622 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003623 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3624
Zhaoleib708ce92015-08-05 16:43:29 +08003625 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003626
Zhaolei55e3a602015-08-05 16:43:30 +08003627 btrfs_dec_block_group_ro(root, cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003628
Arne Jansena2de7332011-03-08 14:14:00 +01003629 btrfs_put_block_group(cache);
3630 if (ret)
3631 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003632 if (is_dev_replace &&
3633 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003634 ret = -EIO;
3635 break;
3636 }
3637 if (sctx->stat.malloc_errors > 0) {
3638 ret = -ENOMEM;
3639 break;
3640 }
Arne Jansena2de7332011-03-08 14:14:00 +01003641
Ilya Dryomov539f3582013-10-07 13:42:57 +03003642 dev_replace->cursor_left = dev_replace->cursor_right;
3643 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003644skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003645 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003646 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003647 }
3648
Arne Jansena2de7332011-03-08 14:14:00 +01003649 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003650
Zhaolei55e3a602015-08-05 16:43:30 +08003651 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003652}
3653
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003654static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3655 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003656{
3657 int i;
3658 u64 bytenr;
3659 u64 gen;
3660 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003661 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003662
Miao Xie87533c42013-01-29 10:14:48 +00003663 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003664 return -EIO;
3665
Miao Xie5f546062014-07-24 11:37:09 +08003666 /* Seed devices of a new filesystem has their own generation. */
3667 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3668 gen = scrub_dev->generation;
3669 else
3670 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003671
3672 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3673 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003674 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3675 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003676 break;
3677
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003678 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003679 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003680 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003681 if (ret)
3682 return ret;
3683 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003684 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003685
3686 return 0;
3687}
3688
3689/*
3690 * get a reference count on fs_info->scrub_workers. start worker if necessary
3691 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003692static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3693 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003694{
David Sterba6f011052015-02-16 18:34:01 +01003695 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003696 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003697
Arne Jansen632dd772011-06-10 12:07:07 +02003698 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003699 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003700 fs_info->scrub_workers =
3701 btrfs_alloc_workqueue("btrfs-scrub", flags,
3702 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003703 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003704 fs_info->scrub_workers =
3705 btrfs_alloc_workqueue("btrfs-scrub", flags,
3706 max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08003707 if (!fs_info->scrub_workers)
3708 goto fail_scrub_workers;
3709
Qu Wenruo0339ef22014-02-28 10:46:17 +08003710 fs_info->scrub_wr_completion_workers =
3711 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3712 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003713 if (!fs_info->scrub_wr_completion_workers)
3714 goto fail_scrub_wr_completion_workers;
3715
Qu Wenruo0339ef22014-02-28 10:46:17 +08003716 fs_info->scrub_nocow_workers =
3717 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
Zhao Leie82afc52015-06-12 20:36:58 +08003718 if (!fs_info->scrub_nocow_workers)
3719 goto fail_scrub_nocow_workers;
Zhao Lei20b2e302015-06-04 20:09:15 +08003720 fs_info->scrub_parity_workers =
3721 btrfs_alloc_workqueue("btrfs-scrubparity", flags,
3722 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003723 if (!fs_info->scrub_parity_workers)
3724 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02003725 }
Arne Jansena2de7332011-03-08 14:14:00 +01003726 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08003727 return 0;
3728
3729fail_scrub_parity_workers:
3730 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
3731fail_scrub_nocow_workers:
3732 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3733fail_scrub_wr_completion_workers:
3734 btrfs_destroy_workqueue(fs_info->scrub_workers);
3735fail_scrub_workers:
3736 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01003737}
3738
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003739static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003740{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003741 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003742 btrfs_destroy_workqueue(fs_info->scrub_workers);
3743 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3744 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08003745 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003746 }
Arne Jansena2de7332011-03-08 14:14:00 +01003747 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003748}
3749
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003750int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3751 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003752 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003753{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003754 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003755 int ret;
3756 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003757 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003758
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003759 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003760 return -EINVAL;
3761
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003762 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003763 /*
3764 * in this case scrub is unable to calculate the checksum
3765 * the way scrub is implemented. Do not handle this
3766 * situation at all because it won't ever happen.
3767 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003768 btrfs_err(fs_info,
3769 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003770 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003771 return -EINVAL;
3772 }
3773
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003774 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003775 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003776 btrfs_err(fs_info,
3777 "scrub: size assumption sectorsize != PAGE_SIZE "
3778 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003779 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003780 return -EINVAL;
3781 }
3782
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003783 if (fs_info->chunk_root->nodesize >
3784 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3785 fs_info->chunk_root->sectorsize >
3786 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3787 /*
3788 * would exhaust the array bounds of pagev member in
3789 * struct scrub_block
3790 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003791 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3792 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003793 fs_info->chunk_root->nodesize,
3794 SCRUB_MAX_PAGES_PER_BLOCK,
3795 fs_info->chunk_root->sectorsize,
3796 SCRUB_MAX_PAGES_PER_BLOCK);
3797 return -EINVAL;
3798 }
3799
Arne Jansena2de7332011-03-08 14:14:00 +01003800
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003801 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3802 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003803 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003804 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003805 return -ENODEV;
3806 }
Arne Jansena2de7332011-03-08 14:14:00 +01003807
Miao Xie5d68da32014-07-24 11:37:07 +08003808 if (!is_dev_replace && !readonly && !dev->writeable) {
3809 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3810 rcu_read_lock();
3811 name = rcu_dereference(dev->name);
3812 btrfs_err(fs_info, "scrub: device %s is not writable",
3813 name->str);
3814 rcu_read_unlock();
3815 return -EROFS;
3816 }
3817
Wang Shilong3b7a0162013-10-12 02:11:12 +08003818 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003819 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003820 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003821 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003822 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003823 }
3824
Stefan Behrens8dabb742012-11-06 13:15:27 +01003825 btrfs_dev_replace_lock(&fs_info->dev_replace);
3826 if (dev->scrub_device ||
3827 (!is_dev_replace &&
3828 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3829 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003830 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003831 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003832 return -EINPROGRESS;
3833 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003834 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003835
3836 ret = scrub_workers_get(fs_info, is_dev_replace);
3837 if (ret) {
3838 mutex_unlock(&fs_info->scrub_lock);
3839 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3840 return ret;
3841 }
3842
Stefan Behrens63a212a2012-11-05 18:29:28 +01003843 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003844 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003845 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003846 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3847 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003848 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003849 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003850 sctx->readonly = readonly;
3851 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003852 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003853
Wang Shilong3cb09292013-12-04 21:15:19 +08003854 /*
3855 * checking @scrub_pause_req here, we can avoid
3856 * race between committing transaction and scrubbing.
3857 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003858 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003859 atomic_inc(&fs_info->scrubs_running);
3860 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003861
Stefan Behrensff023aa2012-11-06 11:43:11 +01003862 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003863 /*
3864 * by holding device list mutex, we can
3865 * kick off writing super in log tree sync.
3866 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003867 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003868 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003869 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003870 }
Arne Jansena2de7332011-03-08 14:14:00 +01003871
3872 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003873 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3874 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003875
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003876 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003877 atomic_dec(&fs_info->scrubs_running);
3878 wake_up(&fs_info->scrub_pause_wait);
3879
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003880 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003881
Arne Jansena2de7332011-03-08 14:14:00 +01003882 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003883 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003884
3885 mutex_lock(&fs_info->scrub_lock);
3886 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003887 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003888 mutex_unlock(&fs_info->scrub_lock);
3889
Filipe Mananaf55985f2015-02-09 21:14:24 +00003890 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003891
3892 return ret;
3893}
3894
Jeff Mahoney143bede2012-03-01 14:56:26 +01003895void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003896{
3897 struct btrfs_fs_info *fs_info = root->fs_info;
3898
3899 mutex_lock(&fs_info->scrub_lock);
3900 atomic_inc(&fs_info->scrub_pause_req);
3901 while (atomic_read(&fs_info->scrubs_paused) !=
3902 atomic_read(&fs_info->scrubs_running)) {
3903 mutex_unlock(&fs_info->scrub_lock);
3904 wait_event(fs_info->scrub_pause_wait,
3905 atomic_read(&fs_info->scrubs_paused) ==
3906 atomic_read(&fs_info->scrubs_running));
3907 mutex_lock(&fs_info->scrub_lock);
3908 }
3909 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003910}
3911
Jeff Mahoney143bede2012-03-01 14:56:26 +01003912void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003913{
3914 struct btrfs_fs_info *fs_info = root->fs_info;
3915
3916 atomic_dec(&fs_info->scrub_pause_req);
3917 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003918}
3919
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003920int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003921{
Arne Jansena2de7332011-03-08 14:14:00 +01003922 mutex_lock(&fs_info->scrub_lock);
3923 if (!atomic_read(&fs_info->scrubs_running)) {
3924 mutex_unlock(&fs_info->scrub_lock);
3925 return -ENOTCONN;
3926 }
3927
3928 atomic_inc(&fs_info->scrub_cancel_req);
3929 while (atomic_read(&fs_info->scrubs_running)) {
3930 mutex_unlock(&fs_info->scrub_lock);
3931 wait_event(fs_info->scrub_pause_wait,
3932 atomic_read(&fs_info->scrubs_running) == 0);
3933 mutex_lock(&fs_info->scrub_lock);
3934 }
3935 atomic_dec(&fs_info->scrub_cancel_req);
3936 mutex_unlock(&fs_info->scrub_lock);
3937
3938 return 0;
3939}
3940
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003941int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3942 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003943{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003944 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003945
3946 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003947 sctx = dev->scrub_device;
3948 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003949 mutex_unlock(&fs_info->scrub_lock);
3950 return -ENOTCONN;
3951 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003952 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003953 while (dev->scrub_device) {
3954 mutex_unlock(&fs_info->scrub_lock);
3955 wait_event(fs_info->scrub_pause_wait,
3956 dev->scrub_device == NULL);
3957 mutex_lock(&fs_info->scrub_lock);
3958 }
3959 mutex_unlock(&fs_info->scrub_lock);
3960
3961 return 0;
3962}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003963
Arne Jansena2de7332011-03-08 14:14:00 +01003964int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3965 struct btrfs_scrub_progress *progress)
3966{
3967 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003968 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003969
3970 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003971 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003972 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003973 sctx = dev->scrub_device;
3974 if (sctx)
3975 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003976 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3977
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003978 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003979}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003980
3981static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3982 u64 extent_logical, u64 extent_len,
3983 u64 *extent_physical,
3984 struct btrfs_device **extent_dev,
3985 int *extent_mirror_num)
3986{
3987 u64 mapped_length;
3988 struct btrfs_bio *bbio = NULL;
3989 int ret;
3990
3991 mapped_length = extent_len;
3992 ret = btrfs_map_block(fs_info, READ, extent_logical,
3993 &mapped_length, &bbio, 0);
3994 if (ret || !bbio || mapped_length < extent_len ||
3995 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08003996 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003997 return;
3998 }
3999
4000 *extent_physical = bbio->stripes[0].physical;
4001 *extent_mirror_num = bbio->mirror_num;
4002 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004003 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004004}
4005
4006static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
4007 struct scrub_wr_ctx *wr_ctx,
4008 struct btrfs_fs_info *fs_info,
4009 struct btrfs_device *dev,
4010 int is_dev_replace)
4011{
4012 WARN_ON(wr_ctx->wr_curr_bio != NULL);
4013
4014 mutex_init(&wr_ctx->wr_lock);
4015 wr_ctx->wr_curr_bio = NULL;
4016 if (!is_dev_replace)
4017 return 0;
4018
4019 WARN_ON(!dev->bdev);
Kent Overstreetb54ffb72015-05-19 14:31:01 +02004020 wr_ctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004021 wr_ctx->tgtdev = dev;
4022 atomic_set(&wr_ctx->flush_all_writes, 0);
4023 return 0;
4024}
4025
4026static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
4027{
4028 mutex_lock(&wr_ctx->wr_lock);
4029 kfree(wr_ctx->wr_curr_bio);
4030 wr_ctx->wr_curr_bio = NULL;
4031 mutex_unlock(&wr_ctx->wr_lock);
4032}
4033
4034static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
4035 int mirror_num, u64 physical_for_dev_replace)
4036{
4037 struct scrub_copy_nocow_ctx *nocow_ctx;
4038 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
4039
4040 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
4041 if (!nocow_ctx) {
4042 spin_lock(&sctx->stat_lock);
4043 sctx->stat.malloc_errors++;
4044 spin_unlock(&sctx->stat_lock);
4045 return -ENOMEM;
4046 }
4047
4048 scrub_pending_trans_workers_inc(sctx);
4049
4050 nocow_ctx->sctx = sctx;
4051 nocow_ctx->logical = logical;
4052 nocow_ctx->len = len;
4053 nocow_ctx->mirror_num = mirror_num;
4054 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08004055 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
4056 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04004057 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08004058 btrfs_queue_work(fs_info->scrub_nocow_workers,
4059 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004060
4061 return 0;
4062}
4063
Josef Bacik652f25a2013-09-12 16:58:28 -04004064static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
4065{
4066 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
4067 struct scrub_nocow_inode *nocow_inode;
4068
4069 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
4070 if (!nocow_inode)
4071 return -ENOMEM;
4072 nocow_inode->inum = inum;
4073 nocow_inode->offset = offset;
4074 nocow_inode->root = root;
4075 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
4076 return 0;
4077}
4078
4079#define COPY_COMPLETE 1
4080
Stefan Behrensff023aa2012-11-06 11:43:11 +01004081static void copy_nocow_pages_worker(struct btrfs_work *work)
4082{
4083 struct scrub_copy_nocow_ctx *nocow_ctx =
4084 container_of(work, struct scrub_copy_nocow_ctx, work);
4085 struct scrub_ctx *sctx = nocow_ctx->sctx;
4086 u64 logical = nocow_ctx->logical;
4087 u64 len = nocow_ctx->len;
4088 int mirror_num = nocow_ctx->mirror_num;
4089 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4090 int ret;
4091 struct btrfs_trans_handle *trans = NULL;
4092 struct btrfs_fs_info *fs_info;
4093 struct btrfs_path *path;
4094 struct btrfs_root *root;
4095 int not_written = 0;
4096
4097 fs_info = sctx->dev_root->fs_info;
4098 root = fs_info->extent_root;
4099
4100 path = btrfs_alloc_path();
4101 if (!path) {
4102 spin_lock(&sctx->stat_lock);
4103 sctx->stat.malloc_errors++;
4104 spin_unlock(&sctx->stat_lock);
4105 not_written = 1;
4106 goto out;
4107 }
4108
4109 trans = btrfs_join_transaction(root);
4110 if (IS_ERR(trans)) {
4111 not_written = 1;
4112 goto out;
4113 }
4114
4115 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04004116 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004117 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004118 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
4119 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02004120 logical, physical_for_dev_replace, len, mirror_num,
4121 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004122 not_written = 1;
4123 goto out;
4124 }
4125
Josef Bacik652f25a2013-09-12 16:58:28 -04004126 btrfs_end_transaction(trans, root);
4127 trans = NULL;
4128 while (!list_empty(&nocow_ctx->inodes)) {
4129 struct scrub_nocow_inode *entry;
4130 entry = list_first_entry(&nocow_ctx->inodes,
4131 struct scrub_nocow_inode,
4132 list);
4133 list_del_init(&entry->list);
4134 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4135 entry->root, nocow_ctx);
4136 kfree(entry);
4137 if (ret == COPY_COMPLETE) {
4138 ret = 0;
4139 break;
4140 } else if (ret) {
4141 break;
4142 }
4143 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004144out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004145 while (!list_empty(&nocow_ctx->inodes)) {
4146 struct scrub_nocow_inode *entry;
4147 entry = list_first_entry(&nocow_ctx->inodes,
4148 struct scrub_nocow_inode,
4149 list);
4150 list_del_init(&entry->list);
4151 kfree(entry);
4152 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004153 if (trans && !IS_ERR(trans))
4154 btrfs_end_transaction(trans, root);
4155 if (not_written)
4156 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4157 num_uncorrectable_read_errors);
4158
4159 btrfs_free_path(path);
4160 kfree(nocow_ctx);
4161
4162 scrub_pending_trans_workers_dec(sctx);
4163}
4164
Gui Hecheng32159242014-11-10 15:36:08 +08004165static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4166 u64 logical)
4167{
4168 struct extent_state *cached_state = NULL;
4169 struct btrfs_ordered_extent *ordered;
4170 struct extent_io_tree *io_tree;
4171 struct extent_map *em;
4172 u64 lockstart = start, lockend = start + len - 1;
4173 int ret = 0;
4174
4175 io_tree = &BTRFS_I(inode)->io_tree;
4176
4177 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4178 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4179 if (ordered) {
4180 btrfs_put_ordered_extent(ordered);
4181 ret = 1;
4182 goto out_unlock;
4183 }
4184
4185 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4186 if (IS_ERR(em)) {
4187 ret = PTR_ERR(em);
4188 goto out_unlock;
4189 }
4190
4191 /*
4192 * This extent does not actually cover the logical extent anymore,
4193 * move on to the next inode.
4194 */
4195 if (em->block_start > logical ||
4196 em->block_start + em->block_len < logical + len) {
4197 free_extent_map(em);
4198 ret = 1;
4199 goto out_unlock;
4200 }
4201 free_extent_map(em);
4202
4203out_unlock:
4204 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4205 GFP_NOFS);
4206 return ret;
4207}
4208
Josef Bacik652f25a2013-09-12 16:58:28 -04004209static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4210 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004211{
Miao Xie826aa0a2013-06-27 18:50:59 +08004212 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004213 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004214 struct inode *inode;
4215 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004216 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004217 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004218 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004219 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004220 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004221 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004222 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004223 int ret = 0;
4224 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004225
4226 key.objectid = root;
4227 key.type = BTRFS_ROOT_ITEM_KEY;
4228 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004229
4230 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4231
Stefan Behrensff023aa2012-11-06 11:43:11 +01004232 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004233 if (IS_ERR(local_root)) {
4234 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004235 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004236 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004237
4238 key.type = BTRFS_INODE_ITEM_KEY;
4239 key.objectid = inum;
4240 key.offset = 0;
4241 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004242 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004243 if (IS_ERR(inode))
4244 return PTR_ERR(inode);
4245
Miao Xieedd14002013-06-27 18:51:00 +08004246 /* Avoid truncate/dio/punch hole.. */
4247 mutex_lock(&inode->i_mutex);
4248 inode_dio_wait(inode);
4249
Stefan Behrensff023aa2012-11-06 11:43:11 +01004250 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004251 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004252 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004253
Gui Hecheng32159242014-11-10 15:36:08 +08004254 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4255 if (ret) {
4256 ret = ret > 0 ? 0 : ret;
4257 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004258 }
4259
Stefan Behrensff023aa2012-11-06 11:43:11 +01004260 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004261 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004262again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004263 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4264 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004265 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004266 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004267 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004268 }
4269
4270 if (PageUptodate(page)) {
4271 if (PageDirty(page))
4272 goto next_page;
4273 } else {
4274 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004275 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004276 btrfs_get_extent,
4277 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004278 if (err) {
4279 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004280 goto next_page;
4281 }
Miao Xieedd14002013-06-27 18:51:00 +08004282
Miao Xie26b258912013-06-27 18:50:58 +08004283 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004284 /*
4285 * If the page has been remove from the page cache,
4286 * the data on it is meaningless, because it may be
4287 * old one, the new data may be written into the new
4288 * page in the page cache.
4289 */
4290 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004291 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004292 page_cache_release(page);
4293 goto again;
4294 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004295 if (!PageUptodate(page)) {
4296 ret = -EIO;
4297 goto next_page;
4298 }
4299 }
Gui Hecheng32159242014-11-10 15:36:08 +08004300
4301 ret = check_extent_to_block(inode, offset, len,
4302 nocow_ctx_logical);
4303 if (ret) {
4304 ret = ret > 0 ? 0 : ret;
4305 goto next_page;
4306 }
4307
Miao Xie826aa0a2013-06-27 18:50:59 +08004308 err = write_page_nocow(nocow_ctx->sctx,
4309 physical_for_dev_replace, page);
4310 if (err)
4311 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004312next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004313 unlock_page(page);
4314 page_cache_release(page);
4315
4316 if (ret)
4317 break;
4318
Stefan Behrensff023aa2012-11-06 11:43:11 +01004319 offset += PAGE_CACHE_SIZE;
4320 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004321 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004322 len -= PAGE_CACHE_SIZE;
4323 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004324 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004325out:
Miao Xieedd14002013-06-27 18:51:00 +08004326 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004327 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004328 return ret;
4329}
4330
4331static int write_page_nocow(struct scrub_ctx *sctx,
4332 u64 physical_for_dev_replace, struct page *page)
4333{
4334 struct bio *bio;
4335 struct btrfs_device *dev;
4336 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004337
4338 dev = sctx->wr_ctx.tgtdev;
4339 if (!dev)
4340 return -EIO;
4341 if (!dev->bdev) {
David Sterba94647322015-10-08 11:01:36 +02004342 btrfs_warn_rl(dev->dev_root->fs_info,
4343 "scrub write_page_nocow(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004344 return -EIO;
4345 }
Chris Mason9be33952013-05-17 18:30:14 -04004346 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004347 if (!bio) {
4348 spin_lock(&sctx->stat_lock);
4349 sctx->stat.malloc_errors++;
4350 spin_unlock(&sctx->stat_lock);
4351 return -ENOMEM;
4352 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004353 bio->bi_iter.bi_size = 0;
4354 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004355 bio->bi_bdev = dev->bdev;
4356 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4357 if (ret != PAGE_CACHE_SIZE) {
4358leave_with_eio:
4359 bio_put(bio);
4360 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4361 return -EIO;
4362 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004363
Kent Overstreet33879d42013-11-23 22:33:32 -08004364 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004365 goto leave_with_eio;
4366
4367 bio_put(bio);
4368 return 0;
4369}