blob: 12ed8a77f11cd5fa1933c9e1a4fc3fe022d75303 [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);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400254static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
255 struct scrub_block *sblock,
256 int is_metadata, int have_csum,
257 const u8 *csum, u64 generation,
258 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400259static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800260 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400261static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
262 struct scrub_block *sblock_good,
263 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100264static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
265static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
266 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400267static int scrub_checksum_data(struct scrub_block *sblock);
268static int scrub_checksum_tree_block(struct scrub_block *sblock);
269static int scrub_checksum_super(struct scrub_block *sblock);
270static void scrub_block_get(struct scrub_block *sblock);
271static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100272static void scrub_page_get(struct scrub_page *spage);
273static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800274static void scrub_parity_get(struct scrub_parity *sparity);
275static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100276static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
277 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100278static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100279 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100280 u64 gen, int mirror_num, u8 *csum, int force,
281 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200282static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400283static void scrub_bio_end_io_worker(struct btrfs_work *work);
284static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100285static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
286 u64 extent_logical, u64 extent_len,
287 u64 *extent_physical,
288 struct btrfs_device **extent_dev,
289 int *extent_mirror_num);
290static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
291 struct scrub_wr_ctx *wr_ctx,
292 struct btrfs_fs_info *fs_info,
293 struct btrfs_device *dev,
294 int is_dev_replace);
295static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
296static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
297 struct scrub_page *spage);
298static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200299static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100300static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
301static int write_page_nocow(struct scrub_ctx *sctx,
302 u64 physical_for_dev_replace, struct page *page);
303static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400304 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100305static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
306 int mirror_num, u64 physical_for_dev_replace);
307static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800308static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800309static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000310static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400311
312
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100313static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
314{
Filipe Mananaf55985f2015-02-09 21:14:24 +0000315 atomic_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100316 atomic_inc(&sctx->bios_in_flight);
317}
318
319static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
320{
321 atomic_dec(&sctx->bios_in_flight);
322 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000323 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100324}
325
Wang Shilongcb7ab022013-12-04 21:16:53 +0800326static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800327{
328 while (atomic_read(&fs_info->scrub_pause_req)) {
329 mutex_unlock(&fs_info->scrub_lock);
330 wait_event(fs_info->scrub_pause_wait,
331 atomic_read(&fs_info->scrub_pause_req) == 0);
332 mutex_lock(&fs_info->scrub_lock);
333 }
334}
335
Zhaolei0e22be82015-08-05 16:43:28 +0800336static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800337{
338 atomic_inc(&fs_info->scrubs_paused);
339 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800340}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800341
Zhaolei0e22be82015-08-05 16:43:28 +0800342static void scrub_pause_off(struct btrfs_fs_info *fs_info)
343{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800344 mutex_lock(&fs_info->scrub_lock);
345 __scrub_blocked_if_needed(fs_info);
346 atomic_dec(&fs_info->scrubs_paused);
347 mutex_unlock(&fs_info->scrub_lock);
348
349 wake_up(&fs_info->scrub_pause_wait);
350}
351
Zhaolei0e22be82015-08-05 16:43:28 +0800352static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
353{
354 scrub_pause_on(fs_info);
355 scrub_pause_off(fs_info);
356}
357
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100358/*
359 * used for workers that require transaction commits (i.e., for the
360 * NOCOW case)
361 */
362static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
363{
364 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
365
Filipe Mananaf55985f2015-02-09 21:14:24 +0000366 atomic_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100367 /*
368 * increment scrubs_running to prevent cancel requests from
369 * completing as long as a worker is running. we must also
370 * increment scrubs_paused to prevent deadlocking on pause
371 * requests used for transactions commits (as the worker uses a
372 * transaction context). it is safe to regard the worker
373 * as paused for all matters practical. effectively, we only
374 * avoid cancellation requests from completing.
375 */
376 mutex_lock(&fs_info->scrub_lock);
377 atomic_inc(&fs_info->scrubs_running);
378 atomic_inc(&fs_info->scrubs_paused);
379 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800380
381 /*
382 * check if @scrubs_running=@scrubs_paused condition
383 * inside wait_event() is not an atomic operation.
384 * which means we may inc/dec @scrub_running/paused
385 * at any time. Let's wake up @scrub_pause_wait as
386 * much as we can to let commit transaction blocked less.
387 */
388 wake_up(&fs_info->scrub_pause_wait);
389
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100390 atomic_inc(&sctx->workers_pending);
391}
392
393/* used for workers that require transaction commits */
394static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
395{
396 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
397
398 /*
399 * see scrub_pending_trans_workers_inc() why we're pretending
400 * to be paused in the scrub counters
401 */
402 mutex_lock(&fs_info->scrub_lock);
403 atomic_dec(&fs_info->scrubs_running);
404 atomic_dec(&fs_info->scrubs_paused);
405 mutex_unlock(&fs_info->scrub_lock);
406 atomic_dec(&sctx->workers_pending);
407 wake_up(&fs_info->scrub_pause_wait);
408 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000409 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100410}
411
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100412static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100413{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100414 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100415 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100416 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100417 struct btrfs_ordered_sum, list);
418 list_del(&sum->list);
419 kfree(sum);
420 }
421}
422
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100423static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100424{
425 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100426
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100427 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100428 return;
429
Stefan Behrensff023aa2012-11-06 11:43:11 +0100430 scrub_free_wr_ctx(&sctx->wr_ctx);
431
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400432 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100433 if (sctx->curr != -1) {
434 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400435
436 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100437 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400438 scrub_block_put(sbio->pagev[i]->sblock);
439 }
440 bio_put(sbio->bio);
441 }
442
Stefan Behrensff023aa2012-11-06 11:43:11 +0100443 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100444 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100445
446 if (!sbio)
447 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100448 kfree(sbio);
449 }
450
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100451 scrub_free_csums(sctx);
452 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100453}
454
Filipe Mananaf55985f2015-02-09 21:14:24 +0000455static void scrub_put_ctx(struct scrub_ctx *sctx)
456{
457 if (atomic_dec_and_test(&sctx->refs))
458 scrub_free_ctx(sctx);
459}
460
Arne Jansena2de7332011-03-08 14:14:00 +0100461static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100462struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100463{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100464 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100465 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100466 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100467 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100468
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100469 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
470 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100471 goto nomem;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000472 atomic_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100473 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200474 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100475 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100476 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100477 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100478 struct scrub_bio *sbio;
479
480 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
481 if (!sbio)
482 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100483 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100484
Arne Jansena2de7332011-03-08 14:14:00 +0100485 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100486 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400487 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800488 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
489 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100490
Stefan Behrensff023aa2012-11-06 11:43:11 +0100491 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100492 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200493 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100494 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100495 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100496 sctx->first_free = 0;
497 sctx->nodesize = dev->dev_root->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100498 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100499 atomic_set(&sctx->bios_in_flight, 0);
500 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100501 atomic_set(&sctx->cancel_req, 0);
502 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
503 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100504
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100505 spin_lock_init(&sctx->list_lock);
506 spin_lock_init(&sctx->stat_lock);
507 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100508
509 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
510 fs_info->dev_replace.tgtdev, is_dev_replace);
511 if (ret) {
512 scrub_free_ctx(sctx);
513 return ERR_PTR(ret);
514 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100515 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100516
517nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100518 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100519 return ERR_PTR(-ENOMEM);
520}
521
Stefan Behrensff023aa2012-11-06 11:43:11 +0100522static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
523 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200524{
525 u64 isize;
526 u32 nlink;
527 int ret;
528 int i;
529 struct extent_buffer *eb;
530 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100531 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200532 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
533 struct inode_fs_paths *ipath = NULL;
534 struct btrfs_root *local_root;
535 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100536 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200537
538 root_key.objectid = root;
539 root_key.type = BTRFS_ROOT_ITEM_KEY;
540 root_key.offset = (u64)-1;
541 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
542 if (IS_ERR(local_root)) {
543 ret = PTR_ERR(local_root);
544 goto err;
545 }
546
David Sterba14692cc2015-01-02 18:55:46 +0100547 /*
548 * this makes the path point to (inum INODE_ITEM ioff)
549 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100550 key.objectid = inum;
551 key.type = BTRFS_INODE_ITEM_KEY;
552 key.offset = 0;
553
554 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200555 if (ret) {
556 btrfs_release_path(swarn->path);
557 goto err;
558 }
559
560 eb = swarn->path->nodes[0];
561 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
562 struct btrfs_inode_item);
563 isize = btrfs_inode_size(eb, inode_item);
564 nlink = btrfs_inode_nlink(eb, inode_item);
565 btrfs_release_path(swarn->path);
566
567 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300568 if (IS_ERR(ipath)) {
569 ret = PTR_ERR(ipath);
570 ipath = NULL;
571 goto err;
572 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200573 ret = paths_from_inode(inum, ipath);
574
575 if (ret < 0)
576 goto err;
577
578 /*
579 * we deliberately ignore the bit ipath might have been too small to
580 * hold all of the paths here
581 */
582 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
David Sterbaecaeb142015-10-08 09:01:03 +0200583 btrfs_warn_in_rcu(fs_info, "%s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200584 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
David Sterbaecaeb142015-10-08 09:01:03 +0200585 "length %llu, links %u (path: %s)", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400586 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200587 (unsigned long long)swarn->sector, root, inum, offset,
588 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500589 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200590
591 free_ipath(ipath);
592 return 0;
593
594err:
David Sterbaecaeb142015-10-08 09:01:03 +0200595 btrfs_warn_in_rcu(fs_info, "%s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200596 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
David Sterbaecaeb142015-10-08 09:01:03 +0200597 "resolving failed with ret=%d", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400598 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200599 (unsigned long long)swarn->sector, root, inum, offset, ret);
600
601 free_ipath(ipath);
602 return 0;
603}
604
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400605static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200606{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100607 struct btrfs_device *dev;
608 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200609 struct btrfs_path *path;
610 struct btrfs_key found_key;
611 struct extent_buffer *eb;
612 struct btrfs_extent_item *ei;
613 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200614 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100615 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600616 u64 flags = 0;
617 u64 ref_root;
618 u32 item_size;
619 u8 ref_level;
Liu Bo69917e42012-09-07 20:01:28 -0600620 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200621
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100622 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100623 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100624 fs_info = sblock->sctx->dev_root->fs_info;
625
Jan Schmidt558540c2011-06-13 19:59:12 +0200626 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200627 if (!path)
628 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200629
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100630 swarn.sector = (sblock->pagev[0]->physical) >> 9;
631 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200632 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100633 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200634
Liu Bo69917e42012-09-07 20:01:28 -0600635 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
636 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200637 if (ret < 0)
638 goto out;
639
Jan Schmidt4692cf52011-12-02 14:56:41 +0100640 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200641 swarn.extent_item_size = found_key.offset;
642
643 eb = path->nodes[0];
644 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
645 item_size = btrfs_item_size_nr(eb, path->slots[0]);
646
Liu Bo69917e42012-09-07 20:01:28 -0600647 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200648 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800649 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
650 item_size, &ref_root,
651 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200652 btrfs_warn_in_rcu(fs_info,
653 "%s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200654 "sector %llu: metadata %s (level %d) in tree "
David Sterbaecaeb142015-10-08 09:01:03 +0200655 "%llu", errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400656 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200657 (unsigned long long)swarn.sector,
658 ref_level ? "node" : "leaf",
659 ret < 0 ? -1 : ref_level,
660 ret < 0 ? -1 : ref_root);
661 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600662 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200663 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600664 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200665 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100666 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100667 iterate_extent_inodes(fs_info, found_key.objectid,
668 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200669 scrub_print_warning_inode, &swarn);
670 }
671
672out:
673 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200674}
675
Stefan Behrensff023aa2012-11-06 11:43:11 +0100676static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200677{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200678 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200679 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100680 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200681 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200682 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200683 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200684 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000685 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200686 u64 end = offset + PAGE_SIZE - 1;
687 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000688 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200689
690 key.objectid = root;
691 key.type = BTRFS_ROOT_ITEM_KEY;
692 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000693
694 fs_info = fixup->root->fs_info;
695 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
696
697 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
698 if (IS_ERR(local_root)) {
699 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200700 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000701 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200702
703 key.type = BTRFS_INODE_ITEM_KEY;
704 key.objectid = inum;
705 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000706 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
707 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200708 if (IS_ERR(inode))
709 return PTR_ERR(inode);
710
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200711 index = offset >> PAGE_CACHE_SHIFT;
712
713 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200714 if (!page) {
715 ret = -ENOMEM;
716 goto out;
717 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200718
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200719 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200720 if (PageDirty(page)) {
721 /*
722 * we need to write the data to the defect sector. the
723 * data that was in that sector is not in memory,
724 * because the page was modified. we must not write the
725 * modified page to that sector.
726 *
727 * TODO: what could be done here: wait for the delalloc
728 * runner to write out that page (might involve
729 * COW) and see whether the sector is still
730 * referenced afterwards.
731 *
732 * For the meantime, we'll treat this error
733 * incorrectable, although there is a chance that a
734 * later scrub will find the bad sector again and that
735 * there's no dirty page in memory, then.
736 */
737 ret = -EIO;
738 goto out;
739 }
Miao Xie1203b682014-09-12 18:44:01 +0800740 ret = repair_io_failure(inode, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200741 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800742 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200743 fixup->mirror_num);
744 unlock_page(page);
745 corrected = !ret;
746 } else {
747 /*
748 * we need to get good data first. the general readpage path
749 * will call repair_io_failure for us, we just have to make
750 * sure we read the bad mirror.
751 */
752 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
753 EXTENT_DAMAGED, GFP_NOFS);
754 if (ret) {
755 /* set_extent_bits should give proper error */
756 WARN_ON(ret > 0);
757 if (ret > 0)
758 ret = -EFAULT;
759 goto out;
760 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200761
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200762 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
763 btrfs_get_extent,
764 fixup->mirror_num);
765 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200766
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200767 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
768 end, EXTENT_DAMAGED, 0, NULL);
769 if (!corrected)
770 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
771 EXTENT_DAMAGED, GFP_NOFS);
772 }
773
774out:
775 if (page)
776 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200777
778 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200779
780 if (ret < 0)
781 return ret;
782
783 if (ret == 0 && corrected) {
784 /*
785 * we only need to call readpage for one of the inodes belonging
786 * to this extent. so make iterate_extent_inodes stop
787 */
788 return 1;
789 }
790
791 return -EIO;
792}
793
794static void scrub_fixup_nodatasum(struct btrfs_work *work)
795{
796 int ret;
797 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100798 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200799 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200800 struct btrfs_path *path;
801 int uncorrectable = 0;
802
803 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100804 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200805
806 path = btrfs_alloc_path();
807 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100808 spin_lock(&sctx->stat_lock);
809 ++sctx->stat.malloc_errors;
810 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200811 uncorrectable = 1;
812 goto out;
813 }
814
815 trans = btrfs_join_transaction(fixup->root);
816 if (IS_ERR(trans)) {
817 uncorrectable = 1;
818 goto out;
819 }
820
821 /*
822 * the idea is to trigger a regular read through the standard path. we
823 * read a page from the (failed) logical address by specifying the
824 * corresponding copynum of the failed sector. thus, that readpage is
825 * expected to fail.
826 * that is the point where on-the-fly error correction will kick in
827 * (once it's finished) and rewrite the failed sector if a good copy
828 * can be found.
829 */
830 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
831 path, scrub_fixup_readpage,
832 fixup);
833 if (ret < 0) {
834 uncorrectable = 1;
835 goto out;
836 }
837 WARN_ON(ret != 1);
838
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100839 spin_lock(&sctx->stat_lock);
840 ++sctx->stat.corrected_errors;
841 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200842
843out:
844 if (trans && !IS_ERR(trans))
845 btrfs_end_transaction(trans, fixup->root);
846 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100847 spin_lock(&sctx->stat_lock);
848 ++sctx->stat.uncorrectable_errors;
849 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100850 btrfs_dev_replace_stats_inc(
851 &sctx->dev_root->fs_info->dev_replace.
852 num_uncorrectable_read_errors);
David Sterbab14af3b2015-10-08 10:43:10 +0200853 btrfs_err_rl_in_rcu(sctx->dev_root->fs_info,
854 "unable to fixup (nodatasum) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200855 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200856 }
857
858 btrfs_free_path(path);
859 kfree(fixup);
860
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100861 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200862}
863
Miao Xieaf8e2d12014-10-23 14:42:50 +0800864static inline void scrub_get_recover(struct scrub_recover *recover)
865{
866 atomic_inc(&recover->refs);
867}
868
869static inline void scrub_put_recover(struct scrub_recover *recover)
870{
871 if (atomic_dec_and_test(&recover->refs)) {
Zhao Lei6e9606d2015-01-20 15:11:34 +0800872 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +0800873 kfree(recover);
874 }
875}
876
Arne Jansena2de7332011-03-08 14:14:00 +0100877/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400878 * scrub_handle_errored_block gets called when either verification of the
879 * pages failed or the bio failed to read, e.g. with EIO. In the latter
880 * case, this function handles all pages in the bio, even though only one
881 * may be bad.
882 * The goal of this function is to repair the errored block by using the
883 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100884 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400885static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100886{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100887 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100888 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400889 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100890 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400891 u64 logical;
892 u64 generation;
893 unsigned int failed_mirror_index;
894 unsigned int is_metadata;
895 unsigned int have_csum;
896 u8 *csum;
897 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
898 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100899 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400900 int mirror_index;
901 int page_num;
902 int success;
903 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
904 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100905
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400906 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100907 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000908 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
909 /*
910 * if we find an error in a super block, we just report it.
911 * They will get written with the next transaction commit
912 * anyway
913 */
914 spin_lock(&sctx->stat_lock);
915 ++sctx->stat.super_errors;
916 spin_unlock(&sctx->stat_lock);
917 return 0;
918 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400919 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100920 logical = sblock_to_check->pagev[0]->logical;
921 generation = sblock_to_check->pagev[0]->generation;
922 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
923 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
924 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400925 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100926 have_csum = sblock_to_check->pagev[0]->have_csum;
927 csum = sblock_to_check->pagev[0]->csum;
928 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400929
Stefan Behrensff023aa2012-11-06 11:43:11 +0100930 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
931 sblocks_for_recheck = NULL;
932 goto nodatasum_case;
933 }
934
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400935 /*
936 * read all mirrors one after the other. This includes to
937 * re-read the extent or metadata block that failed (that was
938 * the cause that this fixup code is called) another time,
939 * page by page this time in order to know which pages
940 * caused I/O errors and which ones are good (for all mirrors).
941 * It is the goal to handle the situation when more than one
942 * mirror contains I/O errors, but the errors do not
943 * overlap, i.e. the data can be repaired by selecting the
944 * pages from those mirrors without I/O error on the
945 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
946 * would be that mirror #1 has an I/O error on the first page,
947 * the second page is good, and mirror #2 has an I/O error on
948 * the second page, but the first page is good.
949 * Then the first page of the first mirror can be repaired by
950 * taking the first page of the second mirror, and the
951 * second page of the second mirror can be repaired by
952 * copying the contents of the 2nd page of the 1st mirror.
953 * One more note: if the pages of one mirror contain I/O
954 * errors, the checksum cannot be verified. In order to get
955 * the best data for repairing, the first attempt is to find
956 * a mirror without I/O errors and with a validated checksum.
957 * Only if this is not possible, the pages are picked from
958 * mirrors with I/O errors without considering the checksum.
959 * If the latter is the case, at the end, the checksum of the
960 * repaired area is verified in order to correctly maintain
961 * the statistics.
962 */
963
David Sterba31e818f2015-02-20 18:00:26 +0100964 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
965 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400966 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100967 spin_lock(&sctx->stat_lock);
968 sctx->stat.malloc_errors++;
969 sctx->stat.read_errors++;
970 sctx->stat.uncorrectable_errors++;
971 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100972 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400973 goto out;
974 }
975
976 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +0800977 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400978 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100979 spin_lock(&sctx->stat_lock);
980 sctx->stat.read_errors++;
981 sctx->stat.uncorrectable_errors++;
982 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100983 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400984 goto out;
985 }
986 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
987 sblock_bad = sblocks_for_recheck + failed_mirror_index;
988
989 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100990 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +0800991 csum, generation, sctx->csum_size, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400992
993 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
994 sblock_bad->no_io_error_seen) {
995 /*
996 * the error disappeared after reading page by page, or
997 * the area was part of a huge bio and other parts of the
998 * bio caused I/O errors, or the block layer merged several
999 * read requests into one and the error is caused by a
1000 * different bio (usually one of the two latter cases is
1001 * the cause)
1002 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001003 spin_lock(&sctx->stat_lock);
1004 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001005 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001006 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001007
Stefan Behrensff023aa2012-11-06 11:43:11 +01001008 if (sctx->is_dev_replace)
1009 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001010 goto out;
1011 }
1012
1013 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001014 spin_lock(&sctx->stat_lock);
1015 sctx->stat.read_errors++;
1016 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001017 if (__ratelimit(&_rs))
1018 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001019 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001020 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001021 spin_lock(&sctx->stat_lock);
1022 sctx->stat.csum_errors++;
1023 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001024 if (__ratelimit(&_rs))
1025 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001026 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001027 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001028 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001029 spin_lock(&sctx->stat_lock);
1030 sctx->stat.verify_errors++;
1031 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001032 if (__ratelimit(&_rs))
1033 scrub_print_warning("checksum/header error",
1034 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001035 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001036 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001037 BTRFS_DEV_STAT_GENERATION_ERRS);
1038 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001039 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001040 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001041 }
1042
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001043 if (sctx->readonly) {
1044 ASSERT(!sctx->is_dev_replace);
1045 goto out;
1046 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001047
1048 if (!is_metadata && !have_csum) {
1049 struct scrub_fixup_nodatasum *fixup_nodatasum;
1050
Stefan Behrensff023aa2012-11-06 11:43:11 +01001051 WARN_ON(sctx->is_dev_replace);
1052
Zhao Leib25c94c2015-01-20 15:11:35 +08001053nodatasum_case:
1054
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001055 /*
1056 * !is_metadata and !have_csum, this means that the data
1057 * might not be COW'ed, that it might be modified
1058 * concurrently. The general strategy to work on the
1059 * commit root does not help in the case when COW is not
1060 * used.
1061 */
1062 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1063 if (!fixup_nodatasum)
1064 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001065 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001066 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001067 fixup_nodatasum->logical = logical;
1068 fixup_nodatasum->root = fs_info->extent_root;
1069 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001070 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001071 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1072 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001073 btrfs_queue_work(fs_info->scrub_workers,
1074 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001075 goto out;
1076 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001077
1078 /*
1079 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001080 * checksums.
1081 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001082 * errors and also does not have a checksum error.
1083 * If one is found, and if a checksum is present, the full block
1084 * that is known to contain an error is rewritten. Afterwards
1085 * the block is known to be corrected.
1086 * If a mirror is found which is completely correct, and no
1087 * checksum is present, only those pages are rewritten that had
1088 * an I/O error in the block to be repaired, since it cannot be
1089 * determined, which copy of the other pages is better (and it
1090 * could happen otherwise that a correct page would be
1091 * overwritten by a bad one).
1092 */
1093 for (mirror_index = 0;
1094 mirror_index < BTRFS_MAX_MIRRORS &&
1095 sblocks_for_recheck[mirror_index].page_count > 0;
1096 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001097 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001098
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001099 if (mirror_index == failed_mirror_index)
1100 continue;
1101 sblock_other = sblocks_for_recheck + mirror_index;
1102
1103 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001104 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1105 have_csum, csum, generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001106 sctx->csum_size, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001107
1108 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001109 !sblock_other->checksum_error &&
1110 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001111 if (sctx->is_dev_replace) {
1112 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001113 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001114 } else {
1115 ret = scrub_repair_block_from_good_copy(
1116 sblock_bad, sblock_other);
1117 if (!ret)
1118 goto corrected_error;
1119 }
Arne Jansena2de7332011-03-08 14:14:00 +01001120 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001121 }
1122
Zhao Leib968fed2015-01-20 15:11:41 +08001123 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1124 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001125
1126 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001127 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001128 * repaired, continue by picking good copies of those pages.
1129 * Select the good pages from mirrors to rewrite bad pages from
1130 * the area to fix. Afterwards verify the checksum of the block
1131 * that is supposed to be repaired. This verification step is
1132 * only done for the purpose of statistic counting and for the
1133 * final scrub report, whether errors remain.
1134 * A perfect algorithm could make use of the checksum and try
1135 * all possible combinations of pages from the different mirrors
1136 * until the checksum verification succeeds. For example, when
1137 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1138 * of mirror #2 is readable but the final checksum test fails,
1139 * then the 2nd page of mirror #3 could be tried, whether now
1140 * the final checksum succeedes. But this would be a rare
1141 * exception and is therefore not implemented. At least it is
1142 * avoided that the good copy is overwritten.
1143 * A more useful improvement would be to pick the sectors
1144 * without I/O error based on sector sizes (512 bytes on legacy
1145 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1146 * mirror could be repaired by taking 512 byte of a different
1147 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1148 * area are unreadable.
1149 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001150 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001151 for (page_num = 0; page_num < sblock_bad->page_count;
1152 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001153 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001154 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001155
Zhao Leib968fed2015-01-20 15:11:41 +08001156 /* skip no-io-error page in scrub */
1157 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001158 continue;
1159
Zhao Leib968fed2015-01-20 15:11:41 +08001160 /* try to find no-io-error page in mirrors */
1161 if (page_bad->io_error) {
1162 for (mirror_index = 0;
1163 mirror_index < BTRFS_MAX_MIRRORS &&
1164 sblocks_for_recheck[mirror_index].page_count > 0;
1165 mirror_index++) {
1166 if (!sblocks_for_recheck[mirror_index].
1167 pagev[page_num]->io_error) {
1168 sblock_other = sblocks_for_recheck +
1169 mirror_index;
1170 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001171 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001172 }
Zhao Leib968fed2015-01-20 15:11:41 +08001173 if (!sblock_other)
1174 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001175 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001176
Zhao Leib968fed2015-01-20 15:11:41 +08001177 if (sctx->is_dev_replace) {
1178 /*
1179 * did not find a mirror to fetch the page
1180 * from. scrub_write_page_to_dev_replace()
1181 * handles this case (page->io_error), by
1182 * filling the block with zeros before
1183 * submitting the write request
1184 */
1185 if (!sblock_other)
1186 sblock_other = sblock_bad;
1187
1188 if (scrub_write_page_to_dev_replace(sblock_other,
1189 page_num) != 0) {
1190 btrfs_dev_replace_stats_inc(
1191 &sctx->dev_root->
1192 fs_info->dev_replace.
1193 num_write_errors);
1194 success = 0;
1195 }
1196 } else if (sblock_other) {
1197 ret = scrub_repair_page_from_good_copy(sblock_bad,
1198 sblock_other,
1199 page_num, 0);
1200 if (0 == ret)
1201 page_bad->io_error = 0;
1202 else
1203 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001204 }
1205 }
1206
Zhao Leib968fed2015-01-20 15:11:41 +08001207 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001208 if (is_metadata || have_csum) {
1209 /*
1210 * need to verify the checksum now that all
1211 * sectors on disk are repaired (the write
1212 * request for data to be repaired is on its way).
1213 * Just be lazy and use scrub_recheck_block()
1214 * which re-reads the data before the checksum
1215 * is verified, but most likely the data comes out
1216 * of the page cache.
1217 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001218 scrub_recheck_block(fs_info, sblock_bad,
1219 is_metadata, have_csum, csum,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001220 generation, sctx->csum_size, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001221 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001222 !sblock_bad->checksum_error &&
1223 sblock_bad->no_io_error_seen)
1224 goto corrected_error;
1225 else
1226 goto did_not_correct_error;
1227 } else {
1228corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001229 spin_lock(&sctx->stat_lock);
1230 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001231 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001232 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001233 btrfs_err_rl_in_rcu(fs_info,
1234 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001235 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001236 }
1237 } else {
1238did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001239 spin_lock(&sctx->stat_lock);
1240 sctx->stat.uncorrectable_errors++;
1241 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001242 btrfs_err_rl_in_rcu(fs_info,
1243 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001244 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001245 }
1246
1247out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001248 if (sblocks_for_recheck) {
1249 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1250 mirror_index++) {
1251 struct scrub_block *sblock = sblocks_for_recheck +
1252 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001253 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001254 int page_index;
1255
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001256 for (page_index = 0; page_index < sblock->page_count;
1257 page_index++) {
1258 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001259 recover = sblock->pagev[page_index]->recover;
1260 if (recover) {
1261 scrub_put_recover(recover);
1262 sblock->pagev[page_index]->recover =
1263 NULL;
1264 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001265 scrub_page_put(sblock->pagev[page_index]);
1266 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001267 }
1268 kfree(sblocks_for_recheck);
1269 }
1270
1271 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001272}
1273
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001274static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001275{
Zhao Lei10f11902015-01-20 15:11:43 +08001276 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1277 return 2;
1278 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1279 return 3;
1280 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001281 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001282}
1283
Zhao Lei10f11902015-01-20 15:11:43 +08001284static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1285 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001286 u64 mapped_length,
1287 int nstripes, int mirror,
1288 int *stripe_index,
1289 u64 *stripe_offset)
1290{
1291 int i;
1292
Zhao Leiffe2d202015-01-20 15:11:44 +08001293 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001294 /* RAID5/6 */
1295 for (i = 0; i < nstripes; i++) {
1296 if (raid_map[i] == RAID6_Q_STRIPE ||
1297 raid_map[i] == RAID5_P_STRIPE)
1298 continue;
1299
1300 if (logical >= raid_map[i] &&
1301 logical < raid_map[i] + mapped_length)
1302 break;
1303 }
1304
1305 *stripe_index = i;
1306 *stripe_offset = logical - raid_map[i];
1307 } else {
1308 /* The other RAID type */
1309 *stripe_index = mirror;
1310 *stripe_offset = 0;
1311 }
1312}
1313
Zhao Leibe50a8d2015-01-20 15:11:42 +08001314static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001315 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001316{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001317 struct scrub_ctx *sctx = original_sblock->sctx;
1318 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
1319 u64 length = original_sblock->page_count * PAGE_SIZE;
1320 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001321 u64 generation = original_sblock->pagev[0]->generation;
1322 u64 flags = original_sblock->pagev[0]->flags;
1323 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001324 struct scrub_recover *recover;
1325 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001326 u64 sublen;
1327 u64 mapped_length;
1328 u64 stripe_offset;
1329 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001330 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001331 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001332 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001333 int ret;
1334
1335 /*
Zhao Lei57019342015-01-20 15:11:45 +08001336 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001337 * are not used (and not set) in the blocks that are used for
1338 * the recheck procedure
1339 */
1340
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001341 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001342 sublen = min_t(u64, length, PAGE_SIZE);
1343 mapped_length = sublen;
1344 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001345
1346 /*
1347 * with a length of PAGE_SIZE, each returned stripe
1348 * represents one mirror
1349 */
Miao Xieaf8e2d12014-10-23 14:42:50 +08001350 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001351 &mapped_length, &bbio, 0, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001352 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001353 btrfs_put_bbio(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001354 return -EIO;
1355 }
1356
Miao Xieaf8e2d12014-10-23 14:42:50 +08001357 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1358 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001359 btrfs_put_bbio(bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001360 return -ENOMEM;
1361 }
1362
1363 atomic_set(&recover->refs, 1);
1364 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001365 recover->map_length = mapped_length;
1366
Stefan Behrensff023aa2012-11-06 11:43:11 +01001367 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001368
Zhao Leibe50a8d2015-01-20 15:11:42 +08001369 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001370
Miao Xieaf8e2d12014-10-23 14:42:50 +08001371 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001372 mirror_index++) {
1373 struct scrub_block *sblock;
1374 struct scrub_page *page;
1375
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001376 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001377 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001378
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001379 page = kzalloc(sizeof(*page), GFP_NOFS);
1380 if (!page) {
1381leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001382 spin_lock(&sctx->stat_lock);
1383 sctx->stat.malloc_errors++;
1384 spin_unlock(&sctx->stat_lock);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001385 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001386 return -ENOMEM;
1387 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001388 scrub_page_get(page);
1389 sblock->pagev[page_index] = page;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001390 page->sblock = sblock;
1391 page->flags = flags;
1392 page->generation = generation;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001393 page->logical = logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001394 page->have_csum = have_csum;
1395 if (have_csum)
1396 memcpy(page->csum,
1397 original_sblock->pagev[0]->csum,
1398 sctx->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001399
Zhao Lei10f11902015-01-20 15:11:43 +08001400 scrub_stripe_index_and_offset(logical,
1401 bbio->map_type,
1402 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001403 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001404 bbio->num_stripes -
1405 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001406 mirror_index,
1407 &stripe_index,
1408 &stripe_offset);
1409 page->physical = bbio->stripes[stripe_index].physical +
1410 stripe_offset;
1411 page->dev = bbio->stripes[stripe_index].dev;
1412
Stefan Behrensff023aa2012-11-06 11:43:11 +01001413 BUG_ON(page_index >= original_sblock->page_count);
1414 page->physical_for_dev_replace =
1415 original_sblock->pagev[page_index]->
1416 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001417 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001418 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001419 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001420 page->page = alloc_page(GFP_NOFS);
1421 if (!page->page)
1422 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001423
1424 scrub_get_recover(recover);
1425 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001426 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001427 scrub_put_recover(recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001428 length -= sublen;
1429 logical += sublen;
1430 page_index++;
1431 }
1432
1433 return 0;
1434}
1435
Miao Xieaf8e2d12014-10-23 14:42:50 +08001436struct scrub_bio_ret {
1437 struct completion event;
1438 int error;
1439};
1440
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001441static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001442{
1443 struct scrub_bio_ret *ret = bio->bi_private;
1444
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001445 ret->error = bio->bi_error;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001446 complete(&ret->event);
1447}
1448
1449static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1450{
Zhao Lei10f11902015-01-20 15:11:43 +08001451 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001452 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001453}
1454
1455static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1456 struct bio *bio,
1457 struct scrub_page *page)
1458{
1459 struct scrub_bio_ret done;
1460 int ret;
1461
1462 init_completion(&done.event);
1463 done.error = 0;
1464 bio->bi_iter.bi_sector = page->logical >> 9;
1465 bio->bi_private = &done;
1466 bio->bi_end_io = scrub_bio_wait_endio;
1467
1468 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001469 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001470 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001471 if (ret)
1472 return ret;
1473
1474 wait_for_completion(&done.event);
1475 if (done.error)
1476 return -EIO;
1477
1478 return 0;
1479}
1480
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001481/*
1482 * this function will check the on disk data for checksum errors, header
1483 * errors and read I/O errors. If any I/O errors happen, the exact pages
1484 * which are errored are marked as being bad. The goal is to enable scrub
1485 * to take those pages that are not errored from all the mirrors so that
1486 * the pages that are errored in the just handled mirror can be repaired.
1487 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001488static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1489 struct scrub_block *sblock, int is_metadata,
1490 int have_csum, u8 *csum, u64 generation,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001491 u16 csum_size, int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001492{
1493 int page_num;
1494
1495 sblock->no_io_error_seen = 1;
1496 sblock->header_error = 0;
1497 sblock->checksum_error = 0;
1498
1499 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1500 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001501 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001502
Stefan Behrens442a4f62012-05-25 16:06:08 +02001503 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001504 page->io_error = 1;
1505 sblock->no_io_error_seen = 0;
1506 continue;
1507 }
1508
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001509 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001510 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001511 if (!bio) {
1512 page->io_error = 1;
1513 sblock->no_io_error_seen = 0;
1514 continue;
1515 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001516 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001517
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001518 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001519 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
1520 if (scrub_submit_raid56_bio_wait(fs_info, bio, page))
1521 sblock->no_io_error_seen = 0;
1522 } else {
1523 bio->bi_iter.bi_sector = page->physical >> 9;
1524
1525 if (btrfsic_submit_bio_wait(READ, bio))
1526 sblock->no_io_error_seen = 0;
1527 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001528
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001529 bio_put(bio);
1530 }
1531
1532 if (sblock->no_io_error_seen)
1533 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1534 have_csum, csum, generation,
1535 csum_size);
1536
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001537 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001538}
1539
Miao Xie17a9be22014-07-24 11:37:08 +08001540static inline int scrub_check_fsid(u8 fsid[],
1541 struct scrub_page *spage)
1542{
1543 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1544 int ret;
1545
1546 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1547 return !ret;
1548}
1549
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001550static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1551 struct scrub_block *sblock,
1552 int is_metadata, int have_csum,
1553 const u8 *csum, u64 generation,
1554 u16 csum_size)
1555{
1556 int page_num;
1557 u8 calculated_csum[BTRFS_CSUM_SIZE];
1558 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001559 void *mapped_buffer;
1560
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001561 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001562 if (is_metadata) {
1563 struct btrfs_header *h;
1564
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001565 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001566 h = (struct btrfs_header *)mapped_buffer;
1567
Qu Wenruo3cae2102013-07-16 11:19:18 +08001568 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Miao Xie17a9be22014-07-24 11:37:08 +08001569 !scrub_check_fsid(h->fsid, sblock->pagev[0]) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001570 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001571 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001572 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001573 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001574 sblock->header_error = 1;
1575 sblock->generation_error = 1;
1576 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001577 csum = h->csum;
1578 } else {
1579 if (!have_csum)
1580 return;
1581
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001582 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001583 }
1584
1585 for (page_num = 0;;) {
1586 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001587 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001588 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1589 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1590 else
Liu Bob0496682013-03-14 14:57:45 +00001591 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001592
Linus Torvalds9613beb2012-03-30 12:44:29 -07001593 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001594 page_num++;
1595 if (page_num >= sblock->page_count)
1596 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001597 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001598
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001599 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001600 }
1601
1602 btrfs_csum_final(crc, calculated_csum);
1603 if (memcmp(calculated_csum, csum, csum_size))
1604 sblock->checksum_error = 1;
1605}
1606
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001607static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001608 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001609{
1610 int page_num;
1611 int ret = 0;
1612
1613 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1614 int ret_sub;
1615
1616 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1617 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001618 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001619 if (ret_sub)
1620 ret = ret_sub;
1621 }
1622
1623 return ret;
1624}
1625
1626static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1627 struct scrub_block *sblock_good,
1628 int page_num, int force_write)
1629{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001630 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1631 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001632
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001633 BUG_ON(page_bad->page == NULL);
1634 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001635 if (force_write || sblock_bad->header_error ||
1636 sblock_bad->checksum_error || page_bad->io_error) {
1637 struct bio *bio;
1638 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001639
Stefan Behrensff023aa2012-11-06 11:43:11 +01001640 if (!page_bad->dev->bdev) {
David Sterba94647322015-10-08 11:01:36 +02001641 btrfs_warn_rl(sblock_bad->sctx->dev_root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001642 "scrub_repair_page_from_good_copy(bdev == NULL) "
David Sterba94647322015-10-08 11:01:36 +02001643 "is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001644 return -EIO;
1645 }
1646
Chris Mason9be33952013-05-17 18:30:14 -04001647 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001648 if (!bio)
1649 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001650 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001651 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001652
1653 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1654 if (PAGE_SIZE != ret) {
1655 bio_put(bio);
1656 return -EIO;
1657 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001658
Kent Overstreet33879d42013-11-23 22:33:32 -08001659 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001660 btrfs_dev_stat_inc_and_print(page_bad->dev,
1661 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001662 btrfs_dev_replace_stats_inc(
1663 &sblock_bad->sctx->dev_root->fs_info->
1664 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001665 bio_put(bio);
1666 return -EIO;
1667 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001668 bio_put(bio);
1669 }
1670
1671 return 0;
1672}
1673
Stefan Behrensff023aa2012-11-06 11:43:11 +01001674static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1675{
1676 int page_num;
1677
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001678 /*
1679 * This block is used for the check of the parity on the source device,
1680 * so the data needn't be written into the destination device.
1681 */
1682 if (sblock->sparity)
1683 return;
1684
Stefan Behrensff023aa2012-11-06 11:43:11 +01001685 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1686 int ret;
1687
1688 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1689 if (ret)
1690 btrfs_dev_replace_stats_inc(
1691 &sblock->sctx->dev_root->fs_info->dev_replace.
1692 num_write_errors);
1693 }
1694}
1695
1696static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1697 int page_num)
1698{
1699 struct scrub_page *spage = sblock->pagev[page_num];
1700
1701 BUG_ON(spage->page == NULL);
1702 if (spage->io_error) {
1703 void *mapped_buffer = kmap_atomic(spage->page);
1704
1705 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1706 flush_dcache_page(spage->page);
1707 kunmap_atomic(mapped_buffer);
1708 }
1709 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1710}
1711
1712static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1713 struct scrub_page *spage)
1714{
1715 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1716 struct scrub_bio *sbio;
1717 int ret;
1718
1719 mutex_lock(&wr_ctx->wr_lock);
1720again:
1721 if (!wr_ctx->wr_curr_bio) {
1722 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1723 GFP_NOFS);
1724 if (!wr_ctx->wr_curr_bio) {
1725 mutex_unlock(&wr_ctx->wr_lock);
1726 return -ENOMEM;
1727 }
1728 wr_ctx->wr_curr_bio->sctx = sctx;
1729 wr_ctx->wr_curr_bio->page_count = 0;
1730 }
1731 sbio = wr_ctx->wr_curr_bio;
1732 if (sbio->page_count == 0) {
1733 struct bio *bio;
1734
1735 sbio->physical = spage->physical_for_dev_replace;
1736 sbio->logical = spage->logical;
1737 sbio->dev = wr_ctx->tgtdev;
1738 bio = sbio->bio;
1739 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001740 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001741 if (!bio) {
1742 mutex_unlock(&wr_ctx->wr_lock);
1743 return -ENOMEM;
1744 }
1745 sbio->bio = bio;
1746 }
1747
1748 bio->bi_private = sbio;
1749 bio->bi_end_io = scrub_wr_bio_end_io;
1750 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001751 bio->bi_iter.bi_sector = sbio->physical >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001752 sbio->err = 0;
1753 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1754 spage->physical_for_dev_replace ||
1755 sbio->logical + sbio->page_count * PAGE_SIZE !=
1756 spage->logical) {
1757 scrub_wr_submit(sctx);
1758 goto again;
1759 }
1760
1761 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1762 if (ret != PAGE_SIZE) {
1763 if (sbio->page_count < 1) {
1764 bio_put(sbio->bio);
1765 sbio->bio = NULL;
1766 mutex_unlock(&wr_ctx->wr_lock);
1767 return -EIO;
1768 }
1769 scrub_wr_submit(sctx);
1770 goto again;
1771 }
1772
1773 sbio->pagev[sbio->page_count] = spage;
1774 scrub_page_get(spage);
1775 sbio->page_count++;
1776 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1777 scrub_wr_submit(sctx);
1778 mutex_unlock(&wr_ctx->wr_lock);
1779
1780 return 0;
1781}
1782
1783static void scrub_wr_submit(struct scrub_ctx *sctx)
1784{
1785 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1786 struct scrub_bio *sbio;
1787
1788 if (!wr_ctx->wr_curr_bio)
1789 return;
1790
1791 sbio = wr_ctx->wr_curr_bio;
1792 wr_ctx->wr_curr_bio = NULL;
1793 WARN_ON(!sbio->bio->bi_bdev);
1794 scrub_pending_bio_inc(sctx);
1795 /* process all writes in a single worker thread. Then the block layer
1796 * orders the requests before sending them to the driver which
1797 * doubled the write performance on spinning disks when measured
1798 * with Linux 3.5 */
1799 btrfsic_submit_bio(WRITE, sbio->bio);
1800}
1801
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001802static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001803{
1804 struct scrub_bio *sbio = bio->bi_private;
1805 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1806
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001807 sbio->err = bio->bi_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001808 sbio->bio = bio;
1809
Liu Bo9e0af232014-08-15 23:36:53 +08001810 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1811 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001812 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001813}
1814
1815static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1816{
1817 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1818 struct scrub_ctx *sctx = sbio->sctx;
1819 int i;
1820
1821 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1822 if (sbio->err) {
1823 struct btrfs_dev_replace *dev_replace =
1824 &sbio->sctx->dev_root->fs_info->dev_replace;
1825
1826 for (i = 0; i < sbio->page_count; i++) {
1827 struct scrub_page *spage = sbio->pagev[i];
1828
1829 spage->io_error = 1;
1830 btrfs_dev_replace_stats_inc(&dev_replace->
1831 num_write_errors);
1832 }
1833 }
1834
1835 for (i = 0; i < sbio->page_count; i++)
1836 scrub_page_put(sbio->pagev[i]);
1837
1838 bio_put(sbio->bio);
1839 kfree(sbio);
1840 scrub_pending_bio_dec(sctx);
1841}
1842
1843static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001844{
1845 u64 flags;
1846 int ret;
1847
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001848 WARN_ON(sblock->page_count < 1);
1849 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001850 ret = 0;
1851 if (flags & BTRFS_EXTENT_FLAG_DATA)
1852 ret = scrub_checksum_data(sblock);
1853 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1854 ret = scrub_checksum_tree_block(sblock);
1855 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1856 (void)scrub_checksum_super(sblock);
1857 else
1858 WARN_ON(1);
1859 if (ret)
1860 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001861
1862 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001863}
1864
1865static int scrub_checksum_data(struct scrub_block *sblock)
1866{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001867 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001868 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001869 u8 *on_disk_csum;
1870 struct page *page;
1871 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001872 u32 crc = ~(u32)0;
1873 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001874 u64 len;
1875 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001876
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001877 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001878 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001879 return 0;
1880
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001881 on_disk_csum = sblock->pagev[0]->csum;
1882 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001883 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001884
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001885 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001886 index = 0;
1887 for (;;) {
1888 u64 l = min_t(u64, len, PAGE_SIZE);
1889
Liu Bob0496682013-03-14 14:57:45 +00001890 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001891 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001892 len -= l;
1893 if (len == 0)
1894 break;
1895 index++;
1896 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001897 BUG_ON(!sblock->pagev[index]->page);
1898 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001899 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001900 }
1901
Arne Jansena2de7332011-03-08 14:14:00 +01001902 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001903 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001904 fail = 1;
1905
Arne Jansena2de7332011-03-08 14:14:00 +01001906 return fail;
1907}
1908
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001909static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001910{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001911 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001912 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001913 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001914 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001915 u8 calculated_csum[BTRFS_CSUM_SIZE];
1916 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1917 struct page *page;
1918 void *mapped_buffer;
1919 u64 mapped_size;
1920 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001921 u32 crc = ~(u32)0;
1922 int fail = 0;
1923 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001924 u64 len;
1925 int index;
1926
1927 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001928 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001929 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001930 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001931 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001932
1933 /*
1934 * we don't use the getter functions here, as we
1935 * a) don't have an extent buffer and
1936 * b) the page is already kmapped
1937 */
Arne Jansena2de7332011-03-08 14:14:00 +01001938
Qu Wenruo3cae2102013-07-16 11:19:18 +08001939 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001940 ++fail;
1941
Qu Wenruo3cae2102013-07-16 11:19:18 +08001942 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001943 ++fail;
1944
Miao Xie17a9be22014-07-24 11:37:08 +08001945 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Arne Jansena2de7332011-03-08 14:14:00 +01001946 ++fail;
1947
1948 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1949 BTRFS_UUID_SIZE))
1950 ++fail;
1951
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001952 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001953 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1954 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1955 index = 0;
1956 for (;;) {
1957 u64 l = min_t(u64, len, mapped_size);
1958
Liu Bob0496682013-03-14 14:57:45 +00001959 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001960 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001961 len -= l;
1962 if (len == 0)
1963 break;
1964 index++;
1965 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001966 BUG_ON(!sblock->pagev[index]->page);
1967 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001968 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001969 mapped_size = PAGE_SIZE;
1970 p = mapped_buffer;
1971 }
1972
1973 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001974 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001975 ++crc_fail;
1976
Arne Jansena2de7332011-03-08 14:14:00 +01001977 return fail || crc_fail;
1978}
1979
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001980static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001981{
1982 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001983 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001984 u8 calculated_csum[BTRFS_CSUM_SIZE];
1985 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1986 struct page *page;
1987 void *mapped_buffer;
1988 u64 mapped_size;
1989 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001990 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001991 int fail_gen = 0;
1992 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001993 u64 len;
1994 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001995
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001996 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001997 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001998 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001999 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002000 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002001
Qu Wenruo3cae2102013-07-16 11:19:18 +08002002 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002003 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002004
Qu Wenruo3cae2102013-07-16 11:19:18 +08002005 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002006 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002007
Miao Xie17a9be22014-07-24 11:37:08 +08002008 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002009 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002010
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002011 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2012 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2013 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2014 index = 0;
2015 for (;;) {
2016 u64 l = min_t(u64, len, mapped_size);
2017
Liu Bob0496682013-03-14 14:57:45 +00002018 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002019 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002020 len -= l;
2021 if (len == 0)
2022 break;
2023 index++;
2024 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002025 BUG_ON(!sblock->pagev[index]->page);
2026 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002027 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002028 mapped_size = PAGE_SIZE;
2029 p = mapped_buffer;
2030 }
2031
2032 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002033 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002034 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002035
Stefan Behrens442a4f62012-05-25 16:06:08 +02002036 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002037 /*
2038 * if we find an error in a super block, we just report it.
2039 * They will get written with the next transaction commit
2040 * anyway
2041 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002042 spin_lock(&sctx->stat_lock);
2043 ++sctx->stat.super_errors;
2044 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002045 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002046 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002047 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2048 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002049 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002050 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002051 }
2052
Stefan Behrens442a4f62012-05-25 16:06:08 +02002053 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002054}
2055
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002056static void scrub_block_get(struct scrub_block *sblock)
2057{
Zhao Lei57019342015-01-20 15:11:45 +08002058 atomic_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002059}
2060
2061static void scrub_block_put(struct scrub_block *sblock)
2062{
Zhao Lei57019342015-01-20 15:11:45 +08002063 if (atomic_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002064 int i;
2065
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002066 if (sblock->sparity)
2067 scrub_parity_put(sblock->sparity);
2068
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002069 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002070 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002071 kfree(sblock);
2072 }
2073}
2074
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002075static void scrub_page_get(struct scrub_page *spage)
2076{
Zhao Lei57019342015-01-20 15:11:45 +08002077 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002078}
2079
2080static void scrub_page_put(struct scrub_page *spage)
2081{
Zhao Lei57019342015-01-20 15:11:45 +08002082 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002083 if (spage->page)
2084 __free_page(spage->page);
2085 kfree(spage);
2086 }
2087}
2088
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002089static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002090{
2091 struct scrub_bio *sbio;
2092
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002093 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002094 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002095
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002096 sbio = sctx->bios[sctx->curr];
2097 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002098 scrub_pending_bio_inc(sctx);
Omar Sandoval03679ad2015-06-19 11:52:48 -07002099 btrfsic_submit_bio(READ, sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002100}
2101
Stefan Behrensff023aa2012-11-06 11:43:11 +01002102static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2103 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002104{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002105 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002106 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002107 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002108
2109again:
2110 /*
2111 * grab a fresh bio or wait for one to become available
2112 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002113 while (sctx->curr == -1) {
2114 spin_lock(&sctx->list_lock);
2115 sctx->curr = sctx->first_free;
2116 if (sctx->curr != -1) {
2117 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2118 sctx->bios[sctx->curr]->next_free = -1;
2119 sctx->bios[sctx->curr]->page_count = 0;
2120 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002121 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002122 spin_unlock(&sctx->list_lock);
2123 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002124 }
2125 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002126 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002127 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002128 struct bio *bio;
2129
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002130 sbio->physical = spage->physical;
2131 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002132 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002133 bio = sbio->bio;
2134 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04002135 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002136 if (!bio)
2137 return -ENOMEM;
2138 sbio->bio = bio;
2139 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002140
2141 bio->bi_private = sbio;
2142 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002143 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002144 bio->bi_iter.bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002145 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002146 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2147 spage->physical ||
2148 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002149 spage->logical ||
2150 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002151 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002152 goto again;
2153 }
2154
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002155 sbio->pagev[sbio->page_count] = spage;
2156 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2157 if (ret != PAGE_SIZE) {
2158 if (sbio->page_count < 1) {
2159 bio_put(sbio->bio);
2160 sbio->bio = NULL;
2161 return -EIO;
2162 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002163 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002164 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002165 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002166
Stefan Behrensff023aa2012-11-06 11:43:11 +01002167 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002168 atomic_inc(&sblock->outstanding_pages);
2169 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002170 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002171 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002172
2173 return 0;
2174}
2175
Linus Torvalds22365972015-09-05 15:14:43 -07002176static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002177{
2178 struct scrub_block *sblock = bio->bi_private;
2179 struct btrfs_fs_info *fs_info = sblock->sctx->dev_root->fs_info;
2180
Linus Torvalds22365972015-09-05 15:14:43 -07002181 if (bio->bi_error)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002182 sblock->no_io_error_seen = 0;
2183
2184 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2185}
2186
2187static void scrub_missing_raid56_worker(struct btrfs_work *work)
2188{
2189 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2190 struct scrub_ctx *sctx = sblock->sctx;
2191 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2192 unsigned int is_metadata;
2193 unsigned int have_csum;
2194 u8 *csum;
2195 u64 generation;
2196 u64 logical;
2197 struct btrfs_device *dev;
2198
2199 is_metadata = !(sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA);
2200 have_csum = sblock->pagev[0]->have_csum;
2201 csum = sblock->pagev[0]->csum;
2202 generation = sblock->pagev[0]->generation;
2203 logical = sblock->pagev[0]->logical;
2204 dev = sblock->pagev[0]->dev;
2205
2206 if (sblock->no_io_error_seen) {
2207 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
2208 have_csum, csum, generation,
2209 sctx->csum_size);
2210 }
2211
2212 if (!sblock->no_io_error_seen) {
2213 spin_lock(&sctx->stat_lock);
2214 sctx->stat.read_errors++;
2215 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02002216 btrfs_err_rl_in_rcu(fs_info,
2217 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002218 logical, rcu_str_deref(dev->name));
2219 } else if (sblock->header_error || sblock->checksum_error) {
2220 spin_lock(&sctx->stat_lock);
2221 sctx->stat.uncorrectable_errors++;
2222 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02002223 btrfs_err_rl_in_rcu(fs_info,
2224 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002225 logical, rcu_str_deref(dev->name));
2226 } else {
2227 scrub_write_block_to_dev_replace(sblock);
2228 }
2229
2230 scrub_block_put(sblock);
2231
2232 if (sctx->is_dev_replace &&
2233 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2234 mutex_lock(&sctx->wr_ctx.wr_lock);
2235 scrub_wr_submit(sctx);
2236 mutex_unlock(&sctx->wr_ctx.wr_lock);
2237 }
2238
2239 scrub_pending_bio_dec(sctx);
2240}
2241
2242static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2243{
2244 struct scrub_ctx *sctx = sblock->sctx;
2245 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2246 u64 length = sblock->page_count * PAGE_SIZE;
2247 u64 logical = sblock->pagev[0]->logical;
2248 struct btrfs_bio *bbio;
2249 struct bio *bio;
2250 struct btrfs_raid_bio *rbio;
2251 int ret;
2252 int i;
2253
2254 ret = btrfs_map_sblock(fs_info, REQ_GET_READ_MIRRORS, logical, &length,
2255 &bbio, 0, 1);
2256 if (ret || !bbio || !bbio->raid_map)
2257 goto bbio_out;
2258
2259 if (WARN_ON(!sctx->is_dev_replace ||
2260 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2261 /*
2262 * We shouldn't be scrubbing a missing device. Even for dev
2263 * replace, we should only get here for RAID 5/6. We either
2264 * managed to mount something with no mirrors remaining or
2265 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2266 */
2267 goto bbio_out;
2268 }
2269
2270 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2271 if (!bio)
2272 goto bbio_out;
2273
2274 bio->bi_iter.bi_sector = logical >> 9;
2275 bio->bi_private = sblock;
2276 bio->bi_end_io = scrub_missing_raid56_end_io;
2277
2278 rbio = raid56_alloc_missing_rbio(sctx->dev_root, bio, bbio, length);
2279 if (!rbio)
2280 goto rbio_out;
2281
2282 for (i = 0; i < sblock->page_count; i++) {
2283 struct scrub_page *spage = sblock->pagev[i];
2284
2285 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2286 }
2287
2288 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2289 scrub_missing_raid56_worker, NULL, NULL);
2290 scrub_block_get(sblock);
2291 scrub_pending_bio_inc(sctx);
2292 raid56_submit_missing_rbio(rbio);
2293 return;
2294
2295rbio_out:
2296 bio_put(bio);
2297bbio_out:
2298 btrfs_put_bbio(bbio);
2299 spin_lock(&sctx->stat_lock);
2300 sctx->stat.malloc_errors++;
2301 spin_unlock(&sctx->stat_lock);
2302}
2303
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002304static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002305 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002306 u64 gen, int mirror_num, u8 *csum, int force,
2307 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002308{
2309 struct scrub_block *sblock;
2310 int index;
2311
2312 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2313 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002314 spin_lock(&sctx->stat_lock);
2315 sctx->stat.malloc_errors++;
2316 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002317 return -ENOMEM;
2318 }
2319
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002320 /* one ref inside this function, plus one for each page added to
2321 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002322 atomic_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002323 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002324 sblock->no_io_error_seen = 1;
2325
2326 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002327 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002328 u64 l = min_t(u64, len, PAGE_SIZE);
2329
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002330 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2331 if (!spage) {
2332leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002333 spin_lock(&sctx->stat_lock);
2334 sctx->stat.malloc_errors++;
2335 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002336 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002337 return -ENOMEM;
2338 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002339 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2340 scrub_page_get(spage);
2341 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002342 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002343 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002344 spage->flags = flags;
2345 spage->generation = gen;
2346 spage->logical = logical;
2347 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002348 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002349 spage->mirror_num = mirror_num;
2350 if (csum) {
2351 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002352 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002353 } else {
2354 spage->have_csum = 0;
2355 }
2356 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002357 spage->page = alloc_page(GFP_NOFS);
2358 if (!spage->page)
2359 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002360 len -= l;
2361 logical += l;
2362 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002363 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002364 }
2365
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002366 WARN_ON(sblock->page_count == 0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002367 if (dev->missing) {
2368 /*
2369 * This case should only be hit for RAID 5/6 device replace. See
2370 * the comment in scrub_missing_raid56_pages() for details.
2371 */
2372 scrub_missing_raid56_pages(sblock);
2373 } else {
2374 for (index = 0; index < sblock->page_count; index++) {
2375 struct scrub_page *spage = sblock->pagev[index];
2376 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002377
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002378 ret = scrub_add_page_to_rd_bio(sctx, spage);
2379 if (ret) {
2380 scrub_block_put(sblock);
2381 return ret;
2382 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002383 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002384
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002385 if (force)
2386 scrub_submit(sctx);
2387 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002388
2389 /* last one frees, either here or in bio completion for last page */
2390 scrub_block_put(sblock);
2391 return 0;
2392}
2393
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002394static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002395{
2396 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002397 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002398
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002399 sbio->err = bio->bi_error;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002400 sbio->bio = bio;
2401
Qu Wenruo0339ef22014-02-28 10:46:17 +08002402 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002403}
2404
2405static void scrub_bio_end_io_worker(struct btrfs_work *work)
2406{
2407 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002408 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002409 int i;
2410
Stefan Behrensff023aa2012-11-06 11:43:11 +01002411 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002412 if (sbio->err) {
2413 for (i = 0; i < sbio->page_count; i++) {
2414 struct scrub_page *spage = sbio->pagev[i];
2415
2416 spage->io_error = 1;
2417 spage->sblock->no_io_error_seen = 0;
2418 }
2419 }
2420
2421 /* now complete the scrub_block items that have all pages completed */
2422 for (i = 0; i < sbio->page_count; i++) {
2423 struct scrub_page *spage = sbio->pagev[i];
2424 struct scrub_block *sblock = spage->sblock;
2425
2426 if (atomic_dec_and_test(&sblock->outstanding_pages))
2427 scrub_block_complete(sblock);
2428 scrub_block_put(sblock);
2429 }
2430
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002431 bio_put(sbio->bio);
2432 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002433 spin_lock(&sctx->list_lock);
2434 sbio->next_free = sctx->first_free;
2435 sctx->first_free = sbio->index;
2436 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002437
2438 if (sctx->is_dev_replace &&
2439 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2440 mutex_lock(&sctx->wr_ctx.wr_lock);
2441 scrub_wr_submit(sctx);
2442 mutex_unlock(&sctx->wr_ctx.wr_lock);
2443 }
2444
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002445 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002446}
2447
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002448static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2449 unsigned long *bitmap,
2450 u64 start, u64 len)
2451{
David Sterba9d644a62015-02-20 18:42:11 +01002452 u32 offset;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002453 int nsectors;
2454 int sectorsize = sparity->sctx->dev_root->sectorsize;
2455
2456 if (len >= sparity->stripe_len) {
2457 bitmap_set(bitmap, 0, sparity->nsectors);
2458 return;
2459 }
2460
2461 start -= sparity->logic_start;
David Sterba47c57132015-02-20 18:43:47 +01002462 start = div_u64_rem(start, sparity->stripe_len, &offset);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002463 offset /= sectorsize;
2464 nsectors = (int)len / sectorsize;
2465
2466 if (offset + nsectors <= sparity->nsectors) {
2467 bitmap_set(bitmap, offset, nsectors);
2468 return;
2469 }
2470
2471 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2472 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2473}
2474
2475static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2476 u64 start, u64 len)
2477{
2478 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2479}
2480
2481static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2482 u64 start, u64 len)
2483{
2484 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2485}
2486
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002487static void scrub_block_complete(struct scrub_block *sblock)
2488{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002489 int corrupted = 0;
2490
Stefan Behrensff023aa2012-11-06 11:43:11 +01002491 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002492 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002493 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002494 } else {
2495 /*
2496 * if has checksum error, write via repair mechanism in
2497 * dev replace case, otherwise write here in dev replace
2498 * case.
2499 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002500 corrupted = scrub_checksum(sblock);
2501 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002502 scrub_write_block_to_dev_replace(sblock);
2503 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002504
2505 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2506 u64 start = sblock->pagev[0]->logical;
2507 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2508 PAGE_SIZE;
2509
2510 scrub_parity_mark_sectors_error(sblock->sparity,
2511 start, end - start);
2512 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002513}
2514
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002515static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002516 u8 *csum)
2517{
2518 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002519 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002520 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002521
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002522 while (!list_empty(&sctx->csum_list)) {
2523 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002524 struct btrfs_ordered_sum, list);
2525 if (sum->bytenr > logical)
2526 return 0;
2527 if (sum->bytenr + sum->len > logical)
2528 break;
2529
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002530 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002531 list_del(&sum->list);
2532 kfree(sum);
2533 sum = NULL;
2534 }
2535 if (!sum)
2536 return 0;
2537
Miao Xief51a4a12013-06-19 10:36:09 +08002538 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002539 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002540 memcpy(csum, sum->sums + index, sctx->csum_size);
2541 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002542 list_del(&sum->list);
2543 kfree(sum);
2544 }
Miao Xief51a4a12013-06-19 10:36:09 +08002545 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002546}
2547
2548/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002549static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002550 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002551 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002552{
2553 int ret;
2554 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002555 u32 blocksize;
2556
2557 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002558 blocksize = sctx->sectorsize;
2559 spin_lock(&sctx->stat_lock);
2560 sctx->stat.data_extents_scrubbed++;
2561 sctx->stat.data_bytes_scrubbed += len;
2562 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002563 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002564 blocksize = sctx->nodesize;
2565 spin_lock(&sctx->stat_lock);
2566 sctx->stat.tree_extents_scrubbed++;
2567 sctx->stat.tree_bytes_scrubbed += len;
2568 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002569 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002570 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002571 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002572 }
Arne Jansena2de7332011-03-08 14:14:00 +01002573
2574 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002575 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002576 int have_csum = 0;
2577
2578 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2579 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002580 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002581 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002582 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002583 if (sctx->is_dev_replace && !have_csum) {
2584 ret = copy_nocow_pages(sctx, logical, l,
2585 mirror_num,
2586 physical_for_dev_replace);
2587 goto behind_scrub_pages;
2588 }
Arne Jansena2de7332011-03-08 14:14:00 +01002589 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002590 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002591 mirror_num, have_csum ? csum : NULL, 0,
2592 physical_for_dev_replace);
2593behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002594 if (ret)
2595 return ret;
2596 len -= l;
2597 logical += l;
2598 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002599 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002600 }
2601 return 0;
2602}
2603
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002604static int scrub_pages_for_parity(struct scrub_parity *sparity,
2605 u64 logical, u64 len,
2606 u64 physical, struct btrfs_device *dev,
2607 u64 flags, u64 gen, int mirror_num, u8 *csum)
2608{
2609 struct scrub_ctx *sctx = sparity->sctx;
2610 struct scrub_block *sblock;
2611 int index;
2612
2613 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2614 if (!sblock) {
2615 spin_lock(&sctx->stat_lock);
2616 sctx->stat.malloc_errors++;
2617 spin_unlock(&sctx->stat_lock);
2618 return -ENOMEM;
2619 }
2620
2621 /* one ref inside this function, plus one for each page added to
2622 * a bio later on */
Zhao Lei57019342015-01-20 15:11:45 +08002623 atomic_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002624 sblock->sctx = sctx;
2625 sblock->no_io_error_seen = 1;
2626 sblock->sparity = sparity;
2627 scrub_parity_get(sparity);
2628
2629 for (index = 0; len > 0; index++) {
2630 struct scrub_page *spage;
2631 u64 l = min_t(u64, len, PAGE_SIZE);
2632
2633 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2634 if (!spage) {
2635leave_nomem:
2636 spin_lock(&sctx->stat_lock);
2637 sctx->stat.malloc_errors++;
2638 spin_unlock(&sctx->stat_lock);
2639 scrub_block_put(sblock);
2640 return -ENOMEM;
2641 }
2642 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2643 /* For scrub block */
2644 scrub_page_get(spage);
2645 sblock->pagev[index] = spage;
2646 /* For scrub parity */
2647 scrub_page_get(spage);
2648 list_add_tail(&spage->list, &sparity->spages);
2649 spage->sblock = sblock;
2650 spage->dev = dev;
2651 spage->flags = flags;
2652 spage->generation = gen;
2653 spage->logical = logical;
2654 spage->physical = physical;
2655 spage->mirror_num = mirror_num;
2656 if (csum) {
2657 spage->have_csum = 1;
2658 memcpy(spage->csum, csum, sctx->csum_size);
2659 } else {
2660 spage->have_csum = 0;
2661 }
2662 sblock->page_count++;
2663 spage->page = alloc_page(GFP_NOFS);
2664 if (!spage->page)
2665 goto leave_nomem;
2666 len -= l;
2667 logical += l;
2668 physical += l;
2669 }
2670
2671 WARN_ON(sblock->page_count == 0);
2672 for (index = 0; index < sblock->page_count; index++) {
2673 struct scrub_page *spage = sblock->pagev[index];
2674 int ret;
2675
2676 ret = scrub_add_page_to_rd_bio(sctx, spage);
2677 if (ret) {
2678 scrub_block_put(sblock);
2679 return ret;
2680 }
2681 }
2682
2683 /* last one frees, either here or in bio completion for last page */
2684 scrub_block_put(sblock);
2685 return 0;
2686}
2687
2688static int scrub_extent_for_parity(struct scrub_parity *sparity,
2689 u64 logical, u64 len,
2690 u64 physical, struct btrfs_device *dev,
2691 u64 flags, u64 gen, int mirror_num)
2692{
2693 struct scrub_ctx *sctx = sparity->sctx;
2694 int ret;
2695 u8 csum[BTRFS_CSUM_SIZE];
2696 u32 blocksize;
2697
Omar Sandoval4a770892015-06-19 11:52:52 -07002698 if (dev->missing) {
2699 scrub_parity_mark_sectors_error(sparity, logical, len);
2700 return 0;
2701 }
2702
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002703 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2704 blocksize = sctx->sectorsize;
2705 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2706 blocksize = sctx->nodesize;
2707 } else {
2708 blocksize = sctx->sectorsize;
2709 WARN_ON(1);
2710 }
2711
2712 while (len) {
2713 u64 l = min_t(u64, len, blocksize);
2714 int have_csum = 0;
2715
2716 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2717 /* push csums to sbio */
2718 have_csum = scrub_find_csum(sctx, logical, l, csum);
2719 if (have_csum == 0)
2720 goto skip;
2721 }
2722 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2723 flags, gen, mirror_num,
2724 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002725 if (ret)
2726 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002727skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002728 len -= l;
2729 logical += l;
2730 physical += l;
2731 }
2732 return 0;
2733}
2734
Wang Shilong3b080b22014-04-01 18:01:43 +08002735/*
2736 * Given a physical address, this will calculate it's
2737 * logical offset. if this is a parity stripe, it will return
2738 * the most left data stripe's logical offset.
2739 *
2740 * return 0 if it is a data stripe, 1 means parity stripe.
2741 */
2742static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002743 struct map_lookup *map, u64 *offset,
2744 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002745{
2746 int i;
2747 int j = 0;
2748 u64 stripe_nr;
2749 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002750 u32 stripe_index;
2751 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002752
2753 last_offset = (physical - map->stripes[num].physical) *
2754 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002755 if (stripe_start)
2756 *stripe_start = last_offset;
2757
Wang Shilong3b080b22014-04-01 18:01:43 +08002758 *offset = last_offset;
2759 for (i = 0; i < nr_data_stripes(map); i++) {
2760 *offset = last_offset + i * map->stripe_len;
2761
David Sterbab8b93ad2015-01-16 17:26:13 +01002762 stripe_nr = div_u64(*offset, map->stripe_len);
2763 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002764
2765 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002766 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002767 /* calculate which stripe this data locates */
2768 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002769 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002770 if (stripe_index == num)
2771 return 0;
2772 if (stripe_index < num)
2773 j++;
2774 }
2775 *offset = last_offset + j * map->stripe_len;
2776 return 1;
2777}
2778
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002779static void scrub_free_parity(struct scrub_parity *sparity)
2780{
2781 struct scrub_ctx *sctx = sparity->sctx;
2782 struct scrub_page *curr, *next;
2783 int nbits;
2784
2785 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2786 if (nbits) {
2787 spin_lock(&sctx->stat_lock);
2788 sctx->stat.read_errors += nbits;
2789 sctx->stat.uncorrectable_errors += nbits;
2790 spin_unlock(&sctx->stat_lock);
2791 }
2792
2793 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2794 list_del_init(&curr->list);
2795 scrub_page_put(curr);
2796 }
2797
2798 kfree(sparity);
2799}
2800
Zhao Lei20b2e302015-06-04 20:09:15 +08002801static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2802{
2803 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2804 work);
2805 struct scrub_ctx *sctx = sparity->sctx;
2806
2807 scrub_free_parity(sparity);
2808 scrub_pending_bio_dec(sctx);
2809}
2810
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002811static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002812{
2813 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002814
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002815 if (bio->bi_error)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002816 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2817 sparity->nsectors);
2818
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002819 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002820
2821 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2822 scrub_parity_bio_endio_worker, NULL, NULL);
2823 btrfs_queue_work(sparity->sctx->dev_root->fs_info->scrub_parity_workers,
2824 &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002825}
2826
2827static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2828{
2829 struct scrub_ctx *sctx = sparity->sctx;
2830 struct bio *bio;
2831 struct btrfs_raid_bio *rbio;
2832 struct scrub_page *spage;
2833 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002834 u64 length;
2835 int ret;
2836
2837 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
2838 sparity->nsectors))
2839 goto out;
2840
Zhao Leia0dd59d2015-07-21 15:42:26 +08002841 length = sparity->logic_end - sparity->logic_start;
Miao Xie76035972014-11-14 17:45:42 +08002842 ret = btrfs_map_sblock(sctx->dev_root->fs_info, WRITE,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002843 sparity->logic_start,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002844 &length, &bbio, 0, 1);
2845 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002846 goto bbio_out;
2847
2848 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2849 if (!bio)
2850 goto bbio_out;
2851
2852 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
2853 bio->bi_private = sparity;
2854 bio->bi_end_io = scrub_parity_bio_endio;
2855
2856 rbio = raid56_parity_alloc_scrub_rbio(sctx->dev_root, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08002857 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002858 sparity->dbitmap,
2859 sparity->nsectors);
2860 if (!rbio)
2861 goto rbio_out;
2862
2863 list_for_each_entry(spage, &sparity->spages, list)
Omar Sandovalb4ee1782015-06-19 11:52:50 -07002864 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002865
2866 scrub_pending_bio_inc(sctx);
2867 raid56_parity_submit_scrub_rbio(rbio);
2868 return;
2869
2870rbio_out:
2871 bio_put(bio);
2872bbio_out:
Zhao Lei6e9606d2015-01-20 15:11:34 +08002873 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002874 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2875 sparity->nsectors);
2876 spin_lock(&sctx->stat_lock);
2877 sctx->stat.malloc_errors++;
2878 spin_unlock(&sctx->stat_lock);
2879out:
2880 scrub_free_parity(sparity);
2881}
2882
2883static inline int scrub_calc_parity_bitmap_len(int nsectors)
2884{
2885 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8);
2886}
2887
2888static void scrub_parity_get(struct scrub_parity *sparity)
2889{
Zhao Lei57019342015-01-20 15:11:45 +08002890 atomic_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002891}
2892
2893static void scrub_parity_put(struct scrub_parity *sparity)
2894{
Zhao Lei57019342015-01-20 15:11:45 +08002895 if (!atomic_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002896 return;
2897
2898 scrub_parity_check_and_repair(sparity);
2899}
2900
2901static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2902 struct map_lookup *map,
2903 struct btrfs_device *sdev,
2904 struct btrfs_path *path,
2905 u64 logic_start,
2906 u64 logic_end)
2907{
2908 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
2909 struct btrfs_root *root = fs_info->extent_root;
2910 struct btrfs_root *csum_root = fs_info->csum_root;
2911 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07002912 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002913 u64 flags;
2914 int ret;
2915 int slot;
2916 struct extent_buffer *l;
2917 struct btrfs_key key;
2918 u64 generation;
2919 u64 extent_logical;
2920 u64 extent_physical;
2921 u64 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07002922 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002923 struct btrfs_device *extent_dev;
2924 struct scrub_parity *sparity;
2925 int nsectors;
2926 int bitmap_len;
2927 int extent_mirror_num;
2928 int stop_loop = 0;
2929
2930 nsectors = map->stripe_len / root->sectorsize;
2931 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2932 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2933 GFP_NOFS);
2934 if (!sparity) {
2935 spin_lock(&sctx->stat_lock);
2936 sctx->stat.malloc_errors++;
2937 spin_unlock(&sctx->stat_lock);
2938 return -ENOMEM;
2939 }
2940
2941 sparity->stripe_len = map->stripe_len;
2942 sparity->nsectors = nsectors;
2943 sparity->sctx = sctx;
2944 sparity->scrub_dev = sdev;
2945 sparity->logic_start = logic_start;
2946 sparity->logic_end = logic_end;
Zhao Lei57019342015-01-20 15:11:45 +08002947 atomic_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002948 INIT_LIST_HEAD(&sparity->spages);
2949 sparity->dbitmap = sparity->bitmap;
2950 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
2951
2952 ret = 0;
2953 while (logic_start < logic_end) {
2954 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2955 key.type = BTRFS_METADATA_ITEM_KEY;
2956 else
2957 key.type = BTRFS_EXTENT_ITEM_KEY;
2958 key.objectid = logic_start;
2959 key.offset = (u64)-1;
2960
2961 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2962 if (ret < 0)
2963 goto out;
2964
2965 if (ret > 0) {
2966 ret = btrfs_previous_extent_item(root, path, 0);
2967 if (ret < 0)
2968 goto out;
2969 if (ret > 0) {
2970 btrfs_release_path(path);
2971 ret = btrfs_search_slot(NULL, root, &key,
2972 path, 0, 0);
2973 if (ret < 0)
2974 goto out;
2975 }
2976 }
2977
2978 stop_loop = 0;
2979 while (1) {
2980 u64 bytes;
2981
2982 l = path->nodes[0];
2983 slot = path->slots[0];
2984 if (slot >= btrfs_header_nritems(l)) {
2985 ret = btrfs_next_leaf(root, path);
2986 if (ret == 0)
2987 continue;
2988 if (ret < 0)
2989 goto out;
2990
2991 stop_loop = 1;
2992 break;
2993 }
2994 btrfs_item_key_to_cpu(l, &key, slot);
2995
Zhao Leid7cad232015-07-22 13:14:48 +08002996 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2997 key.type != BTRFS_METADATA_ITEM_KEY)
2998 goto next;
2999
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003000 if (key.type == BTRFS_METADATA_ITEM_KEY)
3001 bytes = root->nodesize;
3002 else
3003 bytes = key.offset;
3004
3005 if (key.objectid + bytes <= logic_start)
3006 goto next;
3007
Zhao Leia0dd59d2015-07-21 15:42:26 +08003008 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003009 stop_loop = 1;
3010 break;
3011 }
3012
3013 while (key.objectid >= logic_start + map->stripe_len)
3014 logic_start += map->stripe_len;
3015
3016 extent = btrfs_item_ptr(l, slot,
3017 struct btrfs_extent_item);
3018 flags = btrfs_extent_flags(l, extent);
3019 generation = btrfs_extent_generation(l, extent);
3020
Zhao Leia323e812015-07-23 12:29:49 +08003021 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3022 (key.objectid < logic_start ||
3023 key.objectid + bytes >
3024 logic_start + map->stripe_len)) {
3025 btrfs_err(fs_info, "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
3026 key.objectid, logic_start);
Zhao Lei9799d2c2015-08-25 21:31:40 +08003027 spin_lock(&sctx->stat_lock);
3028 sctx->stat.uncorrectable_errors++;
3029 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003030 goto next;
3031 }
3032again:
3033 extent_logical = key.objectid;
3034 extent_len = bytes;
3035
3036 if (extent_logical < logic_start) {
3037 extent_len -= logic_start - extent_logical;
3038 extent_logical = logic_start;
3039 }
3040
3041 if (extent_logical + extent_len >
3042 logic_start + map->stripe_len)
3043 extent_len = logic_start + map->stripe_len -
3044 extent_logical;
3045
3046 scrub_parity_mark_sectors_data(sparity, extent_logical,
3047 extent_len);
3048
Omar Sandoval4a770892015-06-19 11:52:52 -07003049 mapped_length = extent_len;
3050 ret = btrfs_map_block(fs_info, READ, extent_logical,
3051 &mapped_length, &bbio, 0);
3052 if (!ret) {
3053 if (!bbio || mapped_length < extent_len)
3054 ret = -EIO;
3055 }
3056 if (ret) {
3057 btrfs_put_bbio(bbio);
3058 goto out;
3059 }
3060 extent_physical = bbio->stripes[0].physical;
3061 extent_mirror_num = bbio->mirror_num;
3062 extent_dev = bbio->stripes[0].dev;
3063 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003064
3065 ret = btrfs_lookup_csums_range(csum_root,
3066 extent_logical,
3067 extent_logical + extent_len - 1,
3068 &sctx->csum_list, 1);
3069 if (ret)
3070 goto out;
3071
3072 ret = scrub_extent_for_parity(sparity, extent_logical,
3073 extent_len,
3074 extent_physical,
3075 extent_dev, flags,
3076 generation,
3077 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003078
3079 scrub_free_csums(sctx);
3080
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003081 if (ret)
3082 goto out;
3083
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003084 if (extent_logical + extent_len <
3085 key.objectid + bytes) {
3086 logic_start += map->stripe_len;
3087
3088 if (logic_start >= logic_end) {
3089 stop_loop = 1;
3090 break;
3091 }
3092
3093 if (logic_start < key.objectid + bytes) {
3094 cond_resched();
3095 goto again;
3096 }
3097 }
3098next:
3099 path->slots[0]++;
3100 }
3101
3102 btrfs_release_path(path);
3103
3104 if (stop_loop)
3105 break;
3106
3107 logic_start += map->stripe_len;
3108 }
3109out:
3110 if (ret < 0)
3111 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003112 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003113 scrub_parity_put(sparity);
3114 scrub_submit(sctx);
3115 mutex_lock(&sctx->wr_ctx.wr_lock);
3116 scrub_wr_submit(sctx);
3117 mutex_unlock(&sctx->wr_ctx.wr_lock);
3118
3119 btrfs_release_path(path);
3120 return ret < 0 ? ret : 0;
3121}
3122
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003123static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003124 struct map_lookup *map,
3125 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003126 int num, u64 base, u64 length,
3127 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003128{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003129 struct btrfs_path *path, *ppath;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003130 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003131 struct btrfs_root *root = fs_info->extent_root;
3132 struct btrfs_root *csum_root = fs_info->csum_root;
3133 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003134 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003135 u64 flags;
3136 int ret;
3137 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003138 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003139 struct extent_buffer *l;
3140 struct btrfs_key key;
3141 u64 physical;
3142 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003143 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003144 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003145 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003146 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003147 struct reada_control *reada1;
3148 struct reada_control *reada2;
3149 struct btrfs_key key_start;
3150 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003151 u64 increment = map->stripe_len;
3152 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003153 u64 extent_logical;
3154 u64 extent_physical;
3155 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003156 u64 stripe_logical;
3157 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003158 struct btrfs_device *extent_dev;
3159 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003160 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003161
Wang Shilong3b080b22014-04-01 18:01:43 +08003162 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003163 offset = 0;
David Sterbab8b93ad2015-01-16 17:26:13 +01003164 nstripes = div_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003165 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3166 offset = map->stripe_len * num;
3167 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003168 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003169 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3170 int factor = map->num_stripes / map->sub_stripes;
3171 offset = map->stripe_len * (num / map->sub_stripes);
3172 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003173 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003174 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3175 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003176 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003177 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3178 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003179 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003180 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003181 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003182 increment = map->stripe_len * nr_data_stripes(map);
3183 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003184 } else {
3185 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003186 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003187 }
3188
3189 path = btrfs_alloc_path();
3190 if (!path)
3191 return -ENOMEM;
3192
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003193 ppath = btrfs_alloc_path();
3194 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003195 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003196 return -ENOMEM;
3197 }
3198
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003199 /*
3200 * work on commit root. The related disk blocks are static as
3201 * long as COW is applied. This means, it is save to rewrite
3202 * them to repair disk errors without any race conditions
3203 */
Arne Jansena2de7332011-03-08 14:14:00 +01003204 path->search_commit_root = 1;
3205 path->skip_locking = 1;
3206
Gui Hecheng063c54d2015-01-09 09:39:40 +08003207 ppath->search_commit_root = 1;
3208 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003209 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003210 * trigger the readahead for extent tree csum tree and wait for
3211 * completion. During readahead, the scrub is officially paused
3212 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003213 */
3214 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003215 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003216 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003217 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003218 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003219 logic_end += base;
3220 } else {
3221 logic_end = logical + increment * nstripes;
3222 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003223 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003224 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003225 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003226
Arne Jansen7a262852011-06-10 12:39:23 +02003227 /* FIXME it might be better to start readahead at commit root */
3228 key_start.objectid = logical;
3229 key_start.type = BTRFS_EXTENT_ITEM_KEY;
3230 key_start.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003231 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003232 key_end.type = BTRFS_METADATA_ITEM_KEY;
3233 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02003234 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003235
Arne Jansen7a262852011-06-10 12:39:23 +02003236 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3237 key_start.type = BTRFS_EXTENT_CSUM_KEY;
3238 key_start.offset = logical;
3239 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3240 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003241 key_end.offset = logic_end;
Arne Jansen7a262852011-06-10 12:39:23 +02003242 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003243
Arne Jansen7a262852011-06-10 12:39:23 +02003244 if (!IS_ERR(reada1))
3245 btrfs_reada_wait(reada1);
3246 if (!IS_ERR(reada2))
3247 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003248
Arne Jansena2de7332011-03-08 14:14:00 +01003249
3250 /*
3251 * collect all data csums for the stripe to avoid seeking during
3252 * the scrub. This might currently (crc32) end up to be about 1MB
3253 */
Arne Jansene7786c32011-05-28 20:58:38 +00003254 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003255
Arne Jansena2de7332011-03-08 14:14:00 +01003256 /*
3257 * now find all extents for each stripe and scrub them
3258 */
Arne Jansena2de7332011-03-08 14:14:00 +01003259 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003260 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003261 /*
3262 * canceled?
3263 */
3264 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003265 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003266 ret = -ECANCELED;
3267 goto out;
3268 }
3269 /*
3270 * check to see if we have to pause
3271 */
3272 if (atomic_read(&fs_info->scrub_pause_req)) {
3273 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003274 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003275 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003276 mutex_lock(&sctx->wr_ctx.wr_lock);
3277 scrub_wr_submit(sctx);
3278 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003279 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003280 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003281 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003282 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003283 }
3284
Zhao Leif2f66a22015-07-21 12:22:29 +08003285 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3286 ret = get_raid56_logic_offset(physical, num, map,
3287 &logical,
3288 &stripe_logical);
3289 logical += base;
3290 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003291 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003292 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003293 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003294 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3295 ppath, stripe_logical,
3296 stripe_end);
3297 if (ret)
3298 goto out;
3299 goto skip;
3300 }
3301 }
3302
Wang Shilong7c76edb2014-01-12 21:38:32 +08003303 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3304 key.type = BTRFS_METADATA_ITEM_KEY;
3305 else
3306 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003307 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003308 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003309
3310 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3311 if (ret < 0)
3312 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003313
Arne Jansen8c510322011-06-03 10:09:26 +02003314 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003315 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003316 if (ret < 0)
3317 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003318 if (ret > 0) {
3319 /* there's no smaller item, so stick with the
3320 * larger one */
3321 btrfs_release_path(path);
3322 ret = btrfs_search_slot(NULL, root, &key,
3323 path, 0, 0);
3324 if (ret < 0)
3325 goto out;
3326 }
Arne Jansena2de7332011-03-08 14:14:00 +01003327 }
3328
Liu Bo625f1c8d2013-04-27 02:56:57 +00003329 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003330 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003331 u64 bytes;
3332
Arne Jansena2de7332011-03-08 14:14:00 +01003333 l = path->nodes[0];
3334 slot = path->slots[0];
3335 if (slot >= btrfs_header_nritems(l)) {
3336 ret = btrfs_next_leaf(root, path);
3337 if (ret == 0)
3338 continue;
3339 if (ret < 0)
3340 goto out;
3341
Liu Bo625f1c8d2013-04-27 02:56:57 +00003342 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003343 break;
3344 }
3345 btrfs_item_key_to_cpu(l, &key, slot);
3346
Zhao Leid7cad232015-07-22 13:14:48 +08003347 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3348 key.type != BTRFS_METADATA_ITEM_KEY)
3349 goto next;
3350
Josef Bacik3173a182013-03-07 14:22:04 -05003351 if (key.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02003352 bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003353 else
3354 bytes = key.offset;
3355
3356 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003357 goto next;
3358
Liu Bo625f1c8d2013-04-27 02:56:57 +00003359 if (key.objectid >= logical + map->stripe_len) {
3360 /* out of this device extent */
3361 if (key.objectid >= logic_end)
3362 stop_loop = 1;
3363 break;
3364 }
Arne Jansena2de7332011-03-08 14:14:00 +01003365
3366 extent = btrfs_item_ptr(l, slot,
3367 struct btrfs_extent_item);
3368 flags = btrfs_extent_flags(l, extent);
3369 generation = btrfs_extent_generation(l, extent);
3370
Zhao Leia323e812015-07-23 12:29:49 +08003371 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3372 (key.objectid < logical ||
3373 key.objectid + bytes >
3374 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003375 btrfs_err(fs_info,
3376 "scrub: tree block %llu spanning "
3377 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003378 key.objectid, logical);
Zhao Lei9799d2c2015-08-25 21:31:40 +08003379 spin_lock(&sctx->stat_lock);
3380 sctx->stat.uncorrectable_errors++;
3381 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003382 goto next;
3383 }
3384
Liu Bo625f1c8d2013-04-27 02:56:57 +00003385again:
3386 extent_logical = key.objectid;
3387 extent_len = bytes;
3388
Arne Jansena2de7332011-03-08 14:14:00 +01003389 /*
3390 * trim extent to this stripe
3391 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003392 if (extent_logical < logical) {
3393 extent_len -= logical - extent_logical;
3394 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003395 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003396 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003397 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003398 extent_len = logical + map->stripe_len -
3399 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003400 }
3401
Liu Bo625f1c8d2013-04-27 02:56:57 +00003402 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003403 extent_dev = scrub_dev;
3404 extent_mirror_num = mirror_num;
3405 if (is_dev_replace)
3406 scrub_remap_extent(fs_info, extent_logical,
3407 extent_len, &extent_physical,
3408 &extent_dev,
3409 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003410
Zhao Leife8cf652015-07-22 13:14:47 +08003411 ret = btrfs_lookup_csums_range(csum_root,
3412 extent_logical,
3413 extent_logical +
3414 extent_len - 1,
3415 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003416 if (ret)
3417 goto out;
3418
Liu Bo625f1c8d2013-04-27 02:56:57 +00003419 ret = scrub_extent(sctx, extent_logical, extent_len,
3420 extent_physical, extent_dev, flags,
3421 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003422 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003423
3424 scrub_free_csums(sctx);
3425
Liu Bo625f1c8d2013-04-27 02:56:57 +00003426 if (ret)
3427 goto out;
3428
3429 if (extent_logical + extent_len <
3430 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003431 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003432 /*
3433 * loop until we find next data stripe
3434 * or we have finished all stripes.
3435 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003436loop:
3437 physical += map->stripe_len;
3438 ret = get_raid56_logic_offset(physical,
3439 num, map, &logical,
3440 &stripe_logical);
3441 logical += base;
3442
3443 if (ret && physical < physical_end) {
3444 stripe_logical += base;
3445 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003446 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003447 ret = scrub_raid56_parity(sctx,
3448 map, scrub_dev, ppath,
3449 stripe_logical,
3450 stripe_end);
3451 if (ret)
3452 goto out;
3453 goto loop;
3454 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003455 } else {
3456 physical += map->stripe_len;
3457 logical += increment;
3458 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003459 if (logical < key.objectid + bytes) {
3460 cond_resched();
3461 goto again;
3462 }
3463
Wang Shilong3b080b22014-04-01 18:01:43 +08003464 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003465 stop_loop = 1;
3466 break;
3467 }
3468 }
Arne Jansena2de7332011-03-08 14:14:00 +01003469next:
3470 path->slots[0]++;
3471 }
Chris Mason71267332011-05-23 06:30:52 -04003472 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003473skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003474 logical += increment;
3475 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003476 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003477 if (stop_loop)
3478 sctx->stat.last_physical = map->stripes[num].physical +
3479 length;
3480 else
3481 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003482 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003483 if (stop_loop)
3484 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003485 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003486out:
Arne Jansena2de7332011-03-08 14:14:00 +01003487 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003488 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003489 mutex_lock(&sctx->wr_ctx.wr_lock);
3490 scrub_wr_submit(sctx);
3491 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003492
Arne Jansene7786c32011-05-28 20:58:38 +00003493 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003494 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003495 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003496 return ret < 0 ? ret : 0;
3497}
3498
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003499static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003500 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003501 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003502 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003503{
3504 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003505 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003506 struct map_lookup *map;
3507 struct extent_map *em;
3508 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003509 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003510
3511 read_lock(&map_tree->map_tree.lock);
3512 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3513 read_unlock(&map_tree->map_tree.lock);
3514
3515 if (!em)
3516 return -EINVAL;
3517
3518 map = (struct map_lookup *)em->bdev;
3519 if (em->start != chunk_offset)
3520 goto out;
3521
3522 if (em->len < length)
3523 goto out;
3524
3525 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003526 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003527 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003528 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003529 chunk_offset, length,
3530 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003531 if (ret)
3532 goto out;
3533 }
3534 }
3535out:
3536 free_extent_map(em);
3537
3538 return ret;
3539}
3540
3541static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003542int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003543 struct btrfs_device *scrub_dev, u64 start, u64 end,
3544 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003545{
3546 struct btrfs_dev_extent *dev_extent = NULL;
3547 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003548 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003549 struct btrfs_fs_info *fs_info = root->fs_info;
3550 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003551 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003552 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003553 int slot;
3554 struct extent_buffer *l;
3555 struct btrfs_key key;
3556 struct btrfs_key found_key;
3557 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003558 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003559
3560 path = btrfs_alloc_path();
3561 if (!path)
3562 return -ENOMEM;
3563
3564 path->reada = 2;
3565 path->search_commit_root = 1;
3566 path->skip_locking = 1;
3567
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003568 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003569 key.offset = 0ull;
3570 key.type = BTRFS_DEV_EXTENT_KEY;
3571
Arne Jansena2de7332011-03-08 14:14:00 +01003572 while (1) {
3573 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3574 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003575 break;
3576 if (ret > 0) {
3577 if (path->slots[0] >=
3578 btrfs_header_nritems(path->nodes[0])) {
3579 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003580 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003581 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003582 if (ret > 0) {
3583 ret = 0;
3584 break;
3585 }
3586 } else {
3587 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003588 }
3589 }
Arne Jansena2de7332011-03-08 14:14:00 +01003590
3591 l = path->nodes[0];
3592 slot = path->slots[0];
3593
3594 btrfs_item_key_to_cpu(l, &found_key, slot);
3595
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003596 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003597 break;
3598
David Sterba962a2982014-06-04 18:41:45 +02003599 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003600 break;
3601
3602 if (found_key.offset >= end)
3603 break;
3604
3605 if (found_key.offset < key.offset)
3606 break;
3607
3608 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3609 length = btrfs_dev_extent_length(l, dev_extent);
3610
Qu Wenruoced96ed2014-06-19 10:42:51 +08003611 if (found_key.offset + length <= start)
3612 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003613
Arne Jansena2de7332011-03-08 14:14:00 +01003614 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3615
3616 /*
3617 * get a reference on the corresponding block group to prevent
3618 * the chunk from going away while we scrub it
3619 */
3620 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003621
3622 /* some chunks are removed but not committed to disk yet,
3623 * continue scrubbing */
3624 if (!cache)
3625 goto skip;
3626
Zhaolei55e3a602015-08-05 16:43:30 +08003627 /*
3628 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3629 * to avoid deadlock caused by:
3630 * btrfs_inc_block_group_ro()
3631 * -> btrfs_wait_for_commit()
3632 * -> btrfs_commit_transaction()
3633 * -> btrfs_scrub_pause()
3634 */
3635 scrub_pause_on(fs_info);
3636 ret = btrfs_inc_block_group_ro(root, cache);
3637 scrub_pause_off(fs_info);
3638 if (ret) {
3639 btrfs_put_block_group(cache);
3640 break;
3641 }
3642
Stefan Behrensff023aa2012-11-06 11:43:11 +01003643 dev_replace->cursor_right = found_key.offset + length;
3644 dev_replace->cursor_left = found_key.offset;
3645 dev_replace->item_needs_writeback = 1;
Zhao Lei8c204c92015-08-19 15:02:40 +08003646 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
3647 found_key.offset, is_dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003648
3649 /*
3650 * flush, submit all pending read and write bios, afterwards
3651 * wait for them.
3652 * Note that in the dev replace case, a read request causes
3653 * write requests that are submitted in the read completion
3654 * worker. Therefore in the current situation, it is required
3655 * that all write requests are flushed, so that all read and
3656 * write requests are really completed when bios_in_flight
3657 * changes to 0.
3658 */
3659 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3660 scrub_submit(sctx);
3661 mutex_lock(&sctx->wr_ctx.wr_lock);
3662 scrub_wr_submit(sctx);
3663 mutex_unlock(&sctx->wr_ctx.wr_lock);
3664
3665 wait_event(sctx->list_wait,
3666 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003667
3668 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003669
3670 /*
3671 * must be called before we decrease @scrub_paused.
3672 * make sure we don't block transaction commit while
3673 * we are waiting pending workers finished.
3674 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003675 wait_event(sctx->list_wait,
3676 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003677 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3678
Zhaoleib708ce92015-08-05 16:43:29 +08003679 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003680
Zhaolei55e3a602015-08-05 16:43:30 +08003681 btrfs_dec_block_group_ro(root, cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003682
Arne Jansena2de7332011-03-08 14:14:00 +01003683 btrfs_put_block_group(cache);
3684 if (ret)
3685 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003686 if (is_dev_replace &&
3687 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003688 ret = -EIO;
3689 break;
3690 }
3691 if (sctx->stat.malloc_errors > 0) {
3692 ret = -ENOMEM;
3693 break;
3694 }
Arne Jansena2de7332011-03-08 14:14:00 +01003695
Ilya Dryomov539f3582013-10-07 13:42:57 +03003696 dev_replace->cursor_left = dev_replace->cursor_right;
3697 dev_replace->item_needs_writeback = 1;
Qu Wenruoced96ed2014-06-19 10:42:51 +08003698skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003699 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003700 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003701 }
3702
Arne Jansena2de7332011-03-08 14:14:00 +01003703 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003704
Zhaolei55e3a602015-08-05 16:43:30 +08003705 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003706}
3707
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003708static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3709 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003710{
3711 int i;
3712 u64 bytenr;
3713 u64 gen;
3714 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003715 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003716
Miao Xie87533c42013-01-29 10:14:48 +00003717 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003718 return -EIO;
3719
Miao Xie5f546062014-07-24 11:37:09 +08003720 /* Seed devices of a new filesystem has their own generation. */
3721 if (scrub_dev->fs_devices != root->fs_info->fs_devices)
3722 gen = scrub_dev->generation;
3723 else
3724 gen = root->fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003725
3726 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3727 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003728 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3729 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003730 break;
3731
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003732 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003733 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003734 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003735 if (ret)
3736 return ret;
3737 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003738 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003739
3740 return 0;
3741}
3742
3743/*
3744 * get a reference count on fs_info->scrub_workers. start worker if necessary
3745 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003746static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3747 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003748{
David Sterba6f011052015-02-16 18:34:01 +01003749 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003750 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01003751
Arne Jansen632dd772011-06-10 12:07:07 +02003752 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003753 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08003754 fs_info->scrub_workers =
3755 btrfs_alloc_workqueue("btrfs-scrub", flags,
3756 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003757 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08003758 fs_info->scrub_workers =
3759 btrfs_alloc_workqueue("btrfs-scrub", flags,
3760 max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08003761 if (!fs_info->scrub_workers)
3762 goto fail_scrub_workers;
3763
Qu Wenruo0339ef22014-02-28 10:46:17 +08003764 fs_info->scrub_wr_completion_workers =
3765 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
3766 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003767 if (!fs_info->scrub_wr_completion_workers)
3768 goto fail_scrub_wr_completion_workers;
3769
Qu Wenruo0339ef22014-02-28 10:46:17 +08003770 fs_info->scrub_nocow_workers =
3771 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
Zhao Leie82afc52015-06-12 20:36:58 +08003772 if (!fs_info->scrub_nocow_workers)
3773 goto fail_scrub_nocow_workers;
Zhao Lei20b2e302015-06-04 20:09:15 +08003774 fs_info->scrub_parity_workers =
3775 btrfs_alloc_workqueue("btrfs-scrubparity", flags,
3776 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08003777 if (!fs_info->scrub_parity_workers)
3778 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02003779 }
Arne Jansena2de7332011-03-08 14:14:00 +01003780 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08003781 return 0;
3782
3783fail_scrub_parity_workers:
3784 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
3785fail_scrub_nocow_workers:
3786 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3787fail_scrub_wr_completion_workers:
3788 btrfs_destroy_workqueue(fs_info->scrub_workers);
3789fail_scrub_workers:
3790 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01003791}
3792
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003793static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003794{
Stefan Behrensff023aa2012-11-06 11:43:11 +01003795 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08003796 btrfs_destroy_workqueue(fs_info->scrub_workers);
3797 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
3798 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08003799 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003800 }
Arne Jansena2de7332011-03-08 14:14:00 +01003801 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003802}
3803
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003804int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3805 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003806 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003807{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003808 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003809 int ret;
3810 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08003811 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01003812
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003813 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01003814 return -EINVAL;
3815
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003816 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003817 /*
3818 * in this case scrub is unable to calculate the checksum
3819 * the way scrub is implemented. Do not handle this
3820 * situation at all because it won't ever happen.
3821 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003822 btrfs_err(fs_info,
3823 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003824 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003825 return -EINVAL;
3826 }
3827
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003828 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003829 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05003830 btrfs_err(fs_info,
3831 "scrub: size assumption sectorsize != PAGE_SIZE "
3832 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02003833 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01003834 return -EINVAL;
3835 }
3836
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003837 if (fs_info->chunk_root->nodesize >
3838 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
3839 fs_info->chunk_root->sectorsize >
3840 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
3841 /*
3842 * would exhaust the array bounds of pagev member in
3843 * struct scrub_block
3844 */
Frank Holtonefe120a2013-12-20 11:37:06 -05003845 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
3846 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01003847 fs_info->chunk_root->nodesize,
3848 SCRUB_MAX_PAGES_PER_BLOCK,
3849 fs_info->chunk_root->sectorsize,
3850 SCRUB_MAX_PAGES_PER_BLOCK);
3851 return -EINVAL;
3852 }
3853
Arne Jansena2de7332011-03-08 14:14:00 +01003854
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003855 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3856 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003857 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003858 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003859 return -ENODEV;
3860 }
Arne Jansena2de7332011-03-08 14:14:00 +01003861
Miao Xie5d68da32014-07-24 11:37:07 +08003862 if (!is_dev_replace && !readonly && !dev->writeable) {
3863 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3864 rcu_read_lock();
3865 name = rcu_dereference(dev->name);
3866 btrfs_err(fs_info, "scrub: device %s is not writable",
3867 name->str);
3868 rcu_read_unlock();
3869 return -EROFS;
3870 }
3871
Wang Shilong3b7a0162013-10-12 02:11:12 +08003872 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01003873 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01003874 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003875 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003876 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01003877 }
3878
Stefan Behrens8dabb742012-11-06 13:15:27 +01003879 btrfs_dev_replace_lock(&fs_info->dev_replace);
3880 if (dev->scrub_device ||
3881 (!is_dev_replace &&
3882 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
3883 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003884 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003885 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003886 return -EINPROGRESS;
3887 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01003888 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08003889
3890 ret = scrub_workers_get(fs_info, is_dev_replace);
3891 if (ret) {
3892 mutex_unlock(&fs_info->scrub_lock);
3893 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3894 return ret;
3895 }
3896
Stefan Behrens63a212a2012-11-05 18:29:28 +01003897 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003898 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003899 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003900 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3901 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003902 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003903 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003904 sctx->readonly = readonly;
3905 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08003906 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01003907
Wang Shilong3cb09292013-12-04 21:15:19 +08003908 /*
3909 * checking @scrub_pause_req here, we can avoid
3910 * race between committing transaction and scrubbing.
3911 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08003912 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003913 atomic_inc(&fs_info->scrubs_running);
3914 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003915
Stefan Behrensff023aa2012-11-06 11:43:11 +01003916 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08003917 /*
3918 * by holding device list mutex, we can
3919 * kick off writing super in log tree sync.
3920 */
Wang Shilong3cb09292013-12-04 21:15:19 +08003921 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003922 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08003923 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003924 }
Arne Jansena2de7332011-03-08 14:14:00 +01003925
3926 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003927 ret = scrub_enumerate_chunks(sctx, dev, start, end,
3928 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003929
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003930 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003931 atomic_dec(&fs_info->scrubs_running);
3932 wake_up(&fs_info->scrub_pause_wait);
3933
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003934 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02003935
Arne Jansena2de7332011-03-08 14:14:00 +01003936 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003937 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003938
3939 mutex_lock(&fs_info->scrub_lock);
3940 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08003941 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003942 mutex_unlock(&fs_info->scrub_lock);
3943
Filipe Mananaf55985f2015-02-09 21:14:24 +00003944 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01003945
3946 return ret;
3947}
3948
Jeff Mahoney143bede2012-03-01 14:56:26 +01003949void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003950{
3951 struct btrfs_fs_info *fs_info = root->fs_info;
3952
3953 mutex_lock(&fs_info->scrub_lock);
3954 atomic_inc(&fs_info->scrub_pause_req);
3955 while (atomic_read(&fs_info->scrubs_paused) !=
3956 atomic_read(&fs_info->scrubs_running)) {
3957 mutex_unlock(&fs_info->scrub_lock);
3958 wait_event(fs_info->scrub_pause_wait,
3959 atomic_read(&fs_info->scrubs_paused) ==
3960 atomic_read(&fs_info->scrubs_running));
3961 mutex_lock(&fs_info->scrub_lock);
3962 }
3963 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003964}
3965
Jeff Mahoney143bede2012-03-01 14:56:26 +01003966void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01003967{
3968 struct btrfs_fs_info *fs_info = root->fs_info;
3969
3970 atomic_dec(&fs_info->scrub_pause_req);
3971 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003972}
3973
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003974int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003975{
Arne Jansena2de7332011-03-08 14:14:00 +01003976 mutex_lock(&fs_info->scrub_lock);
3977 if (!atomic_read(&fs_info->scrubs_running)) {
3978 mutex_unlock(&fs_info->scrub_lock);
3979 return -ENOTCONN;
3980 }
3981
3982 atomic_inc(&fs_info->scrub_cancel_req);
3983 while (atomic_read(&fs_info->scrubs_running)) {
3984 mutex_unlock(&fs_info->scrub_lock);
3985 wait_event(fs_info->scrub_pause_wait,
3986 atomic_read(&fs_info->scrubs_running) == 0);
3987 mutex_lock(&fs_info->scrub_lock);
3988 }
3989 atomic_dec(&fs_info->scrub_cancel_req);
3990 mutex_unlock(&fs_info->scrub_lock);
3991
3992 return 0;
3993}
3994
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003995int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3996 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003997{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003998 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003999
4000 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004001 sctx = dev->scrub_device;
4002 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004003 mutex_unlock(&fs_info->scrub_lock);
4004 return -ENOTCONN;
4005 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004006 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01004007 while (dev->scrub_device) {
4008 mutex_unlock(&fs_info->scrub_lock);
4009 wait_event(fs_info->scrub_pause_wait,
4010 dev->scrub_device == NULL);
4011 mutex_lock(&fs_info->scrub_lock);
4012 }
4013 mutex_unlock(&fs_info->scrub_lock);
4014
4015 return 0;
4016}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004017
Arne Jansena2de7332011-03-08 14:14:00 +01004018int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
4019 struct btrfs_scrub_progress *progress)
4020{
4021 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004022 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004023
4024 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004025 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004026 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004027 sctx = dev->scrub_device;
4028 if (sctx)
4029 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01004030 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
4031
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004032 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004033}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004034
4035static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4036 u64 extent_logical, u64 extent_len,
4037 u64 *extent_physical,
4038 struct btrfs_device **extent_dev,
4039 int *extent_mirror_num)
4040{
4041 u64 mapped_length;
4042 struct btrfs_bio *bbio = NULL;
4043 int ret;
4044
4045 mapped_length = extent_len;
4046 ret = btrfs_map_block(fs_info, READ, extent_logical,
4047 &mapped_length, &bbio, 0);
4048 if (ret || !bbio || mapped_length < extent_len ||
4049 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08004050 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004051 return;
4052 }
4053
4054 *extent_physical = bbio->stripes[0].physical;
4055 *extent_mirror_num = bbio->mirror_num;
4056 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004057 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004058}
4059
4060static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
4061 struct scrub_wr_ctx *wr_ctx,
4062 struct btrfs_fs_info *fs_info,
4063 struct btrfs_device *dev,
4064 int is_dev_replace)
4065{
4066 WARN_ON(wr_ctx->wr_curr_bio != NULL);
4067
4068 mutex_init(&wr_ctx->wr_lock);
4069 wr_ctx->wr_curr_bio = NULL;
4070 if (!is_dev_replace)
4071 return 0;
4072
4073 WARN_ON(!dev->bdev);
Kent Overstreetb54ffb72015-05-19 14:31:01 +02004074 wr_ctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004075 wr_ctx->tgtdev = dev;
4076 atomic_set(&wr_ctx->flush_all_writes, 0);
4077 return 0;
4078}
4079
4080static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
4081{
4082 mutex_lock(&wr_ctx->wr_lock);
4083 kfree(wr_ctx->wr_curr_bio);
4084 wr_ctx->wr_curr_bio = NULL;
4085 mutex_unlock(&wr_ctx->wr_lock);
4086}
4087
4088static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
4089 int mirror_num, u64 physical_for_dev_replace)
4090{
4091 struct scrub_copy_nocow_ctx *nocow_ctx;
4092 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
4093
4094 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
4095 if (!nocow_ctx) {
4096 spin_lock(&sctx->stat_lock);
4097 sctx->stat.malloc_errors++;
4098 spin_unlock(&sctx->stat_lock);
4099 return -ENOMEM;
4100 }
4101
4102 scrub_pending_trans_workers_inc(sctx);
4103
4104 nocow_ctx->sctx = sctx;
4105 nocow_ctx->logical = logical;
4106 nocow_ctx->len = len;
4107 nocow_ctx->mirror_num = mirror_num;
4108 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08004109 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
4110 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04004111 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08004112 btrfs_queue_work(fs_info->scrub_nocow_workers,
4113 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004114
4115 return 0;
4116}
4117
Josef Bacik652f25a2013-09-12 16:58:28 -04004118static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
4119{
4120 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
4121 struct scrub_nocow_inode *nocow_inode;
4122
4123 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
4124 if (!nocow_inode)
4125 return -ENOMEM;
4126 nocow_inode->inum = inum;
4127 nocow_inode->offset = offset;
4128 nocow_inode->root = root;
4129 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
4130 return 0;
4131}
4132
4133#define COPY_COMPLETE 1
4134
Stefan Behrensff023aa2012-11-06 11:43:11 +01004135static void copy_nocow_pages_worker(struct btrfs_work *work)
4136{
4137 struct scrub_copy_nocow_ctx *nocow_ctx =
4138 container_of(work, struct scrub_copy_nocow_ctx, work);
4139 struct scrub_ctx *sctx = nocow_ctx->sctx;
4140 u64 logical = nocow_ctx->logical;
4141 u64 len = nocow_ctx->len;
4142 int mirror_num = nocow_ctx->mirror_num;
4143 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4144 int ret;
4145 struct btrfs_trans_handle *trans = NULL;
4146 struct btrfs_fs_info *fs_info;
4147 struct btrfs_path *path;
4148 struct btrfs_root *root;
4149 int not_written = 0;
4150
4151 fs_info = sctx->dev_root->fs_info;
4152 root = fs_info->extent_root;
4153
4154 path = btrfs_alloc_path();
4155 if (!path) {
4156 spin_lock(&sctx->stat_lock);
4157 sctx->stat.malloc_errors++;
4158 spin_unlock(&sctx->stat_lock);
4159 not_written = 1;
4160 goto out;
4161 }
4162
4163 trans = btrfs_join_transaction(root);
4164 if (IS_ERR(trans)) {
4165 not_written = 1;
4166 goto out;
4167 }
4168
4169 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04004170 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004171 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004172 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
4173 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02004174 logical, physical_for_dev_replace, len, mirror_num,
4175 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004176 not_written = 1;
4177 goto out;
4178 }
4179
Josef Bacik652f25a2013-09-12 16:58:28 -04004180 btrfs_end_transaction(trans, root);
4181 trans = NULL;
4182 while (!list_empty(&nocow_ctx->inodes)) {
4183 struct scrub_nocow_inode *entry;
4184 entry = list_first_entry(&nocow_ctx->inodes,
4185 struct scrub_nocow_inode,
4186 list);
4187 list_del_init(&entry->list);
4188 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4189 entry->root, nocow_ctx);
4190 kfree(entry);
4191 if (ret == COPY_COMPLETE) {
4192 ret = 0;
4193 break;
4194 } else if (ret) {
4195 break;
4196 }
4197 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004198out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004199 while (!list_empty(&nocow_ctx->inodes)) {
4200 struct scrub_nocow_inode *entry;
4201 entry = list_first_entry(&nocow_ctx->inodes,
4202 struct scrub_nocow_inode,
4203 list);
4204 list_del_init(&entry->list);
4205 kfree(entry);
4206 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004207 if (trans && !IS_ERR(trans))
4208 btrfs_end_transaction(trans, root);
4209 if (not_written)
4210 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4211 num_uncorrectable_read_errors);
4212
4213 btrfs_free_path(path);
4214 kfree(nocow_ctx);
4215
4216 scrub_pending_trans_workers_dec(sctx);
4217}
4218
Gui Hecheng32159242014-11-10 15:36:08 +08004219static int check_extent_to_block(struct inode *inode, u64 start, u64 len,
4220 u64 logical)
4221{
4222 struct extent_state *cached_state = NULL;
4223 struct btrfs_ordered_extent *ordered;
4224 struct extent_io_tree *io_tree;
4225 struct extent_map *em;
4226 u64 lockstart = start, lockend = start + len - 1;
4227 int ret = 0;
4228
4229 io_tree = &BTRFS_I(inode)->io_tree;
4230
4231 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
4232 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
4233 if (ordered) {
4234 btrfs_put_ordered_extent(ordered);
4235 ret = 1;
4236 goto out_unlock;
4237 }
4238
4239 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4240 if (IS_ERR(em)) {
4241 ret = PTR_ERR(em);
4242 goto out_unlock;
4243 }
4244
4245 /*
4246 * This extent does not actually cover the logical extent anymore,
4247 * move on to the next inode.
4248 */
4249 if (em->block_start > logical ||
4250 em->block_start + em->block_len < logical + len) {
4251 free_extent_map(em);
4252 ret = 1;
4253 goto out_unlock;
4254 }
4255 free_extent_map(em);
4256
4257out_unlock:
4258 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4259 GFP_NOFS);
4260 return ret;
4261}
4262
Josef Bacik652f25a2013-09-12 16:58:28 -04004263static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4264 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004265{
Miao Xie826aa0a2013-06-27 18:50:59 +08004266 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004267 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004268 struct inode *inode;
4269 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004270 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004271 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004272 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004273 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004274 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004275 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004276 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004277 int ret = 0;
4278 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004279
4280 key.objectid = root;
4281 key.type = BTRFS_ROOT_ITEM_KEY;
4282 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004283
4284 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4285
Stefan Behrensff023aa2012-11-06 11:43:11 +01004286 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004287 if (IS_ERR(local_root)) {
4288 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004289 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004290 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004291
4292 key.type = BTRFS_INODE_ITEM_KEY;
4293 key.objectid = inum;
4294 key.offset = 0;
4295 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004296 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004297 if (IS_ERR(inode))
4298 return PTR_ERR(inode);
4299
Miao Xieedd14002013-06-27 18:51:00 +08004300 /* Avoid truncate/dio/punch hole.. */
4301 mutex_lock(&inode->i_mutex);
4302 inode_dio_wait(inode);
4303
Stefan Behrensff023aa2012-11-06 11:43:11 +01004304 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004305 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004306 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004307
Gui Hecheng32159242014-11-10 15:36:08 +08004308 ret = check_extent_to_block(inode, offset, len, nocow_ctx_logical);
4309 if (ret) {
4310 ret = ret > 0 ? 0 : ret;
4311 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004312 }
4313
Stefan Behrensff023aa2012-11-06 11:43:11 +01004314 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004315 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004316again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004317 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4318 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004319 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004320 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004321 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004322 }
4323
4324 if (PageUptodate(page)) {
4325 if (PageDirty(page))
4326 goto next_page;
4327 } else {
4328 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004329 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004330 btrfs_get_extent,
4331 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004332 if (err) {
4333 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004334 goto next_page;
4335 }
Miao Xieedd14002013-06-27 18:51:00 +08004336
Miao Xie26b258912013-06-27 18:50:58 +08004337 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004338 /*
4339 * If the page has been remove from the page cache,
4340 * the data on it is meaningless, because it may be
4341 * old one, the new data may be written into the new
4342 * page in the page cache.
4343 */
4344 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004345 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004346 page_cache_release(page);
4347 goto again;
4348 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004349 if (!PageUptodate(page)) {
4350 ret = -EIO;
4351 goto next_page;
4352 }
4353 }
Gui Hecheng32159242014-11-10 15:36:08 +08004354
4355 ret = check_extent_to_block(inode, offset, len,
4356 nocow_ctx_logical);
4357 if (ret) {
4358 ret = ret > 0 ? 0 : ret;
4359 goto next_page;
4360 }
4361
Miao Xie826aa0a2013-06-27 18:50:59 +08004362 err = write_page_nocow(nocow_ctx->sctx,
4363 physical_for_dev_replace, page);
4364 if (err)
4365 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004366next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004367 unlock_page(page);
4368 page_cache_release(page);
4369
4370 if (ret)
4371 break;
4372
Stefan Behrensff023aa2012-11-06 11:43:11 +01004373 offset += PAGE_CACHE_SIZE;
4374 physical_for_dev_replace += PAGE_CACHE_SIZE;
Gui Hecheng32159242014-11-10 15:36:08 +08004375 nocow_ctx_logical += PAGE_CACHE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004376 len -= PAGE_CACHE_SIZE;
4377 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004378 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004379out:
Miao Xieedd14002013-06-27 18:51:00 +08004380 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08004381 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004382 return ret;
4383}
4384
4385static int write_page_nocow(struct scrub_ctx *sctx,
4386 u64 physical_for_dev_replace, struct page *page)
4387{
4388 struct bio *bio;
4389 struct btrfs_device *dev;
4390 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004391
4392 dev = sctx->wr_ctx.tgtdev;
4393 if (!dev)
4394 return -EIO;
4395 if (!dev->bdev) {
David Sterba94647322015-10-08 11:01:36 +02004396 btrfs_warn_rl(dev->dev_root->fs_info,
4397 "scrub write_page_nocow(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004398 return -EIO;
4399 }
Chris Mason9be33952013-05-17 18:30:14 -04004400 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004401 if (!bio) {
4402 spin_lock(&sctx->stat_lock);
4403 sctx->stat.malloc_errors++;
4404 spin_unlock(&sctx->stat_lock);
4405 return -ENOMEM;
4406 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004407 bio->bi_iter.bi_size = 0;
4408 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004409 bio->bi_bdev = dev->bdev;
4410 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
4411 if (ret != PAGE_CACHE_SIZE) {
4412leave_with_eio:
4413 bio_put(bio);
4414 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4415 return -EIO;
4416 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004417
Kent Overstreet33879d42013-11-23 22:33:32 -08004418 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004419 goto leave_with_eio;
4420
4421 bio_put(bio);
4422 return 0;
4423}