blob: ba9134b5b1303ad4a48ef5d102c688e831697962 [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 {
Elena Reshetova6f615012017-03-03 10:55:21 +020067 refcount_t refs;
Miao Xieaf8e2d12014-10-23 14:42:50 +080068 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;
Elena Reshetova186debd2017-03-03 10:55:23 +0200115 refcount_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
Liu Bo972d7212017-04-03 13:45:33 -0700143 u64 stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800144
Elena Reshetova78a76452017-03-03 10:55:24 +0200145 refcount_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];
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400174 struct btrfs_fs_info *fs_info;
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 */
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200205 refcount_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
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800243struct full_stripe_lock {
244 struct rb_node node;
245 u64 logical;
246 u64 refs;
247 struct mutex mutex;
248};
249
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100250static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
251static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
252static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
253static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400254static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Zhao Leibe50a8d2015-01-20 15:11:42 +0800255static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100256 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100257static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +0800258 struct scrub_block *sblock,
259 int retry_failed_mirror);
Zhao Leiba7cf982015-08-24 21:18:02 +0800260static void scrub_recheck_block_checksum(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400261static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800262 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400263static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
264 struct scrub_block *sblock_good,
265 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100266static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
267static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
268 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400269static int scrub_checksum_data(struct scrub_block *sblock);
270static int scrub_checksum_tree_block(struct scrub_block *sblock);
271static int scrub_checksum_super(struct scrub_block *sblock);
272static void scrub_block_get(struct scrub_block *sblock);
273static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100274static void scrub_page_get(struct scrub_page *spage);
275static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800276static void scrub_parity_get(struct scrub_parity *sparity);
277static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100278static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
279 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100280static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100281 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100282 u64 gen, int mirror_num, u8 *csum, int force,
283 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200284static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400285static void scrub_bio_end_io_worker(struct btrfs_work *work);
286static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100287static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
288 u64 extent_logical, u64 extent_len,
289 u64 *extent_physical,
290 struct btrfs_device **extent_dev,
291 int *extent_mirror_num);
David Sterbae5987e12017-02-10 19:55:54 +0100292static int scrub_setup_wr_ctx(struct scrub_wr_ctx *wr_ctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100293 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{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200315 refcount_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/*
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800359 * Insert new full stripe lock into full stripe locks tree
360 *
361 * Return pointer to existing or newly inserted full_stripe_lock structure if
362 * everything works well.
363 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
364 *
365 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
366 * function
367 */
368static struct full_stripe_lock *insert_full_stripe_lock(
369 struct btrfs_full_stripe_locks_tree *locks_root,
370 u64 fstripe_logical)
371{
372 struct rb_node **p;
373 struct rb_node *parent = NULL;
374 struct full_stripe_lock *entry;
375 struct full_stripe_lock *ret;
376
377 WARN_ON(!mutex_is_locked(&locks_root->lock));
378
379 p = &locks_root->root.rb_node;
380 while (*p) {
381 parent = *p;
382 entry = rb_entry(parent, struct full_stripe_lock, node);
383 if (fstripe_logical < entry->logical) {
384 p = &(*p)->rb_left;
385 } else if (fstripe_logical > entry->logical) {
386 p = &(*p)->rb_right;
387 } else {
388 entry->refs++;
389 return entry;
390 }
391 }
392
393 /* Insert new lock */
394 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
395 if (!ret)
396 return ERR_PTR(-ENOMEM);
397 ret->logical = fstripe_logical;
398 ret->refs = 1;
399 mutex_init(&ret->mutex);
400
401 rb_link_node(&ret->node, parent, p);
402 rb_insert_color(&ret->node, &locks_root->root);
403 return ret;
404}
405
406/*
407 * Search for a full stripe lock of a block group
408 *
409 * Return pointer to existing full stripe lock if found
410 * Return NULL if not found
411 */
412static struct full_stripe_lock *search_full_stripe_lock(
413 struct btrfs_full_stripe_locks_tree *locks_root,
414 u64 fstripe_logical)
415{
416 struct rb_node *node;
417 struct full_stripe_lock *entry;
418
419 WARN_ON(!mutex_is_locked(&locks_root->lock));
420
421 node = locks_root->root.rb_node;
422 while (node) {
423 entry = rb_entry(node, struct full_stripe_lock, node);
424 if (fstripe_logical < entry->logical)
425 node = node->rb_left;
426 else if (fstripe_logical > entry->logical)
427 node = node->rb_right;
428 else
429 return entry;
430 }
431 return NULL;
432}
433
434/*
435 * Helper to get full stripe logical from a normal bytenr.
436 *
437 * Caller must ensure @cache is a RAID56 block group.
438 */
439static u64 get_full_stripe_logical(struct btrfs_block_group_cache *cache,
440 u64 bytenr)
441{
442 u64 ret;
443
444 /*
445 * Due to chunk item size limit, full stripe length should not be
446 * larger than U32_MAX. Just a sanity check here.
447 */
448 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
449
450 /*
451 * round_down() can only handle power of 2, while RAID56 full
452 * stripe length can be 64KiB * n, so we need to manually round down.
453 */
454 ret = div64_u64(bytenr - cache->key.objectid, cache->full_stripe_len) *
455 cache->full_stripe_len + cache->key.objectid;
456 return ret;
457}
458
459/*
460 * Lock a full stripe to avoid concurrency of recovery and read
461 *
462 * It's only used for profiles with parities (RAID5/6), for other profiles it
463 * does nothing.
464 *
465 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
466 * So caller must call unlock_full_stripe() at the same context.
467 *
468 * Return <0 if encounters error.
469 */
470static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
471 bool *locked_ret)
472{
473 struct btrfs_block_group_cache *bg_cache;
474 struct btrfs_full_stripe_locks_tree *locks_root;
475 struct full_stripe_lock *existing;
476 u64 fstripe_start;
477 int ret = 0;
478
479 *locked_ret = false;
480 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
481 if (!bg_cache) {
482 ASSERT(0);
483 return -ENOENT;
484 }
485
486 /* Profiles not based on parity don't need full stripe lock */
487 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
488 goto out;
489 locks_root = &bg_cache->full_stripe_locks_root;
490
491 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
492
493 /* Now insert the full stripe lock */
494 mutex_lock(&locks_root->lock);
495 existing = insert_full_stripe_lock(locks_root, fstripe_start);
496 mutex_unlock(&locks_root->lock);
497 if (IS_ERR(existing)) {
498 ret = PTR_ERR(existing);
499 goto out;
500 }
501 mutex_lock(&existing->mutex);
502 *locked_ret = true;
503out:
504 btrfs_put_block_group(bg_cache);
505 return ret;
506}
507
508/*
509 * Unlock a full stripe.
510 *
511 * NOTE: Caller must ensure it's the same context calling corresponding
512 * lock_full_stripe().
513 *
514 * Return 0 if we unlock full stripe without problem.
515 * Return <0 for error
516 */
517static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
518 bool locked)
519{
520 struct btrfs_block_group_cache *bg_cache;
521 struct btrfs_full_stripe_locks_tree *locks_root;
522 struct full_stripe_lock *fstripe_lock;
523 u64 fstripe_start;
524 bool freeit = false;
525 int ret = 0;
526
527 /* If we didn't acquire full stripe lock, no need to continue */
528 if (!locked)
529 return 0;
530
531 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
532 if (!bg_cache) {
533 ASSERT(0);
534 return -ENOENT;
535 }
536 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
537 goto out;
538
539 locks_root = &bg_cache->full_stripe_locks_root;
540 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
541
542 mutex_lock(&locks_root->lock);
543 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
544 /* Unpaired unlock_full_stripe() detected */
545 if (!fstripe_lock) {
546 WARN_ON(1);
547 ret = -ENOENT;
548 mutex_unlock(&locks_root->lock);
549 goto out;
550 }
551
552 if (fstripe_lock->refs == 0) {
553 WARN_ON(1);
554 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
555 fstripe_lock->logical);
556 } else {
557 fstripe_lock->refs--;
558 }
559
560 if (fstripe_lock->refs == 0) {
561 rb_erase(&fstripe_lock->node, &locks_root->root);
562 freeit = true;
563 }
564 mutex_unlock(&locks_root->lock);
565
566 mutex_unlock(&fstripe_lock->mutex);
567 if (freeit)
568 kfree(fstripe_lock);
569out:
570 btrfs_put_block_group(bg_cache);
571 return ret;
572}
573
574/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100575 * used for workers that require transaction commits (i.e., for the
576 * NOCOW case)
577 */
578static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
579{
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400580 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100581
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200582 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100583 /*
584 * increment scrubs_running to prevent cancel requests from
585 * completing as long as a worker is running. we must also
586 * increment scrubs_paused to prevent deadlocking on pause
587 * requests used for transactions commits (as the worker uses a
588 * transaction context). it is safe to regard the worker
589 * as paused for all matters practical. effectively, we only
590 * avoid cancellation requests from completing.
591 */
592 mutex_lock(&fs_info->scrub_lock);
593 atomic_inc(&fs_info->scrubs_running);
594 atomic_inc(&fs_info->scrubs_paused);
595 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800596
597 /*
598 * check if @scrubs_running=@scrubs_paused condition
599 * inside wait_event() is not an atomic operation.
600 * which means we may inc/dec @scrub_running/paused
601 * at any time. Let's wake up @scrub_pause_wait as
602 * much as we can to let commit transaction blocked less.
603 */
604 wake_up(&fs_info->scrub_pause_wait);
605
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100606 atomic_inc(&sctx->workers_pending);
607}
608
609/* used for workers that require transaction commits */
610static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
611{
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400612 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100613
614 /*
615 * see scrub_pending_trans_workers_inc() why we're pretending
616 * to be paused in the scrub counters
617 */
618 mutex_lock(&fs_info->scrub_lock);
619 atomic_dec(&fs_info->scrubs_running);
620 atomic_dec(&fs_info->scrubs_paused);
621 mutex_unlock(&fs_info->scrub_lock);
622 atomic_dec(&sctx->workers_pending);
623 wake_up(&fs_info->scrub_pause_wait);
624 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000625 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100626}
627
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100628static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100629{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100630 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100631 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100632 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100633 struct btrfs_ordered_sum, list);
634 list_del(&sum->list);
635 kfree(sum);
636 }
637}
638
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100639static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100640{
641 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100642
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100643 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100644 return;
645
Stefan Behrensff023aa2012-11-06 11:43:11 +0100646 scrub_free_wr_ctx(&sctx->wr_ctx);
647
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400648 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100649 if (sctx->curr != -1) {
650 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400651
652 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100653 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400654 scrub_block_put(sbio->pagev[i]->sblock);
655 }
656 bio_put(sbio->bio);
657 }
658
Stefan Behrensff023aa2012-11-06 11:43:11 +0100659 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100660 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100661
662 if (!sbio)
663 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100664 kfree(sbio);
665 }
666
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100667 scrub_free_csums(sctx);
668 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100669}
670
Filipe Mananaf55985f2015-02-09 21:14:24 +0000671static void scrub_put_ctx(struct scrub_ctx *sctx)
672{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200673 if (refcount_dec_and_test(&sctx->refs))
Filipe Mananaf55985f2015-02-09 21:14:24 +0000674 scrub_free_ctx(sctx);
675}
676
Arne Jansena2de7332011-03-08 14:14:00 +0100677static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100678struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100679{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100680 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100681 int i;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400682 struct btrfs_fs_info *fs_info = dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100683 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100684
David Sterba58c4e172016-02-11 10:49:42 +0100685 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100686 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100687 goto nomem;
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200688 refcount_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100689 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200690 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100691 sctx->curr = -1;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400692 sctx->fs_info = dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100693 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100694 struct scrub_bio *sbio;
695
David Sterba58c4e172016-02-11 10:49:42 +0100696 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
Arne Jansena2de7332011-03-08 14:14:00 +0100697 if (!sbio)
698 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100699 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100700
Arne Jansena2de7332011-03-08 14:14:00 +0100701 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100702 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400703 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800704 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
705 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100706
Stefan Behrensff023aa2012-11-06 11:43:11 +0100707 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100708 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200709 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100710 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100711 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100712 sctx->first_free = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400713 sctx->nodesize = fs_info->nodesize;
714 sctx->sectorsize = fs_info->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100715 atomic_set(&sctx->bios_in_flight, 0);
716 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100717 atomic_set(&sctx->cancel_req, 0);
718 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
719 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100720
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100721 spin_lock_init(&sctx->list_lock);
722 spin_lock_init(&sctx->stat_lock);
723 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100724
David Sterbae5987e12017-02-10 19:55:54 +0100725 ret = scrub_setup_wr_ctx(&sctx->wr_ctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100726 fs_info->dev_replace.tgtdev, is_dev_replace);
727 if (ret) {
728 scrub_free_ctx(sctx);
729 return ERR_PTR(ret);
730 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100731 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100732
733nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100734 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100735 return ERR_PTR(-ENOMEM);
736}
737
Stefan Behrensff023aa2012-11-06 11:43:11 +0100738static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
739 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200740{
741 u64 isize;
742 u32 nlink;
743 int ret;
744 int i;
745 struct extent_buffer *eb;
746 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100747 struct scrub_warning *swarn = warn_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400748 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200749 struct inode_fs_paths *ipath = NULL;
750 struct btrfs_root *local_root;
751 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100752 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200753
754 root_key.objectid = root;
755 root_key.type = BTRFS_ROOT_ITEM_KEY;
756 root_key.offset = (u64)-1;
757 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
758 if (IS_ERR(local_root)) {
759 ret = PTR_ERR(local_root);
760 goto err;
761 }
762
David Sterba14692cc2015-01-02 18:55:46 +0100763 /*
764 * this makes the path point to (inum INODE_ITEM ioff)
765 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100766 key.objectid = inum;
767 key.type = BTRFS_INODE_ITEM_KEY;
768 key.offset = 0;
769
770 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200771 if (ret) {
772 btrfs_release_path(swarn->path);
773 goto err;
774 }
775
776 eb = swarn->path->nodes[0];
777 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
778 struct btrfs_inode_item);
779 isize = btrfs_inode_size(eb, inode_item);
780 nlink = btrfs_inode_nlink(eb, inode_item);
781 btrfs_release_path(swarn->path);
782
783 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300784 if (IS_ERR(ipath)) {
785 ret = PTR_ERR(ipath);
786 ipath = NULL;
787 goto err;
788 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200789 ret = paths_from_inode(inum, ipath);
790
791 if (ret < 0)
792 goto err;
793
794 /*
795 * we deliberately ignore the bit ipath might have been too small to
796 * hold all of the paths here
797 */
798 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400799 btrfs_warn_in_rcu(fs_info,
800 "%s at logical %llu on dev %s, sector %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
801 swarn->errstr, swarn->logical,
802 rcu_str_deref(swarn->dev->name),
803 (unsigned long long)swarn->sector,
804 root, inum, offset,
805 min(isize - offset, (u64)PAGE_SIZE), nlink,
806 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200807
808 free_ipath(ipath);
809 return 0;
810
811err:
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400812 btrfs_warn_in_rcu(fs_info,
813 "%s at logical %llu on dev %s, sector %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
814 swarn->errstr, swarn->logical,
815 rcu_str_deref(swarn->dev->name),
816 (unsigned long long)swarn->sector,
817 root, inum, offset, ret);
Jan Schmidt558540c2011-06-13 19:59:12 +0200818
819 free_ipath(ipath);
820 return 0;
821}
822
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400823static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200824{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100825 struct btrfs_device *dev;
826 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200827 struct btrfs_path *path;
828 struct btrfs_key found_key;
829 struct extent_buffer *eb;
830 struct btrfs_extent_item *ei;
831 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200832 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100833 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600834 u64 flags = 0;
835 u64 ref_root;
836 u32 item_size;
Dan Carpenter07c9a8e2016-03-11 11:08:56 +0300837 u8 ref_level = 0;
Liu Bo69917e42012-09-07 20:01:28 -0600838 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200839
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100840 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100841 dev = sblock->pagev[0]->dev;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400842 fs_info = sblock->sctx->fs_info;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100843
Jan Schmidt558540c2011-06-13 19:59:12 +0200844 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200845 if (!path)
846 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200847
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100848 swarn.sector = (sblock->pagev[0]->physical) >> 9;
849 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200850 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100851 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200852
Liu Bo69917e42012-09-07 20:01:28 -0600853 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
854 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200855 if (ret < 0)
856 goto out;
857
Jan Schmidt4692cf52011-12-02 14:56:41 +0100858 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200859 swarn.extent_item_size = found_key.offset;
860
861 eb = path->nodes[0];
862 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
863 item_size = btrfs_item_size_nr(eb, path->slots[0]);
864
Liu Bo69917e42012-09-07 20:01:28 -0600865 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200866 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800867 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
868 item_size, &ref_root,
869 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200870 btrfs_warn_in_rcu(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400871 "%s at logical %llu on dev %s, sector %llu: metadata %s (level %d) in tree %llu",
872 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400873 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200874 (unsigned long long)swarn.sector,
875 ref_level ? "node" : "leaf",
876 ret < 0 ? -1 : ref_level,
877 ret < 0 ? -1 : ref_root);
878 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600879 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200880 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600881 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200882 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100883 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100884 iterate_extent_inodes(fs_info, found_key.objectid,
885 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200886 scrub_print_warning_inode, &swarn);
887 }
888
889out:
890 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200891}
892
Stefan Behrensff023aa2012-11-06 11:43:11 +0100893static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200894{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200895 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200896 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100897 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200898 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200899 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200900 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200901 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000902 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200903 u64 end = offset + PAGE_SIZE - 1;
904 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000905 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200906
907 key.objectid = root;
908 key.type = BTRFS_ROOT_ITEM_KEY;
909 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000910
911 fs_info = fixup->root->fs_info;
912 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
913
914 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
915 if (IS_ERR(local_root)) {
916 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200917 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000918 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200919
920 key.type = BTRFS_INODE_ITEM_KEY;
921 key.objectid = inum;
922 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000923 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
924 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200925 if (IS_ERR(inode))
926 return PTR_ERR(inode);
927
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300928 index = offset >> PAGE_SHIFT;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200929
930 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200931 if (!page) {
932 ret = -ENOMEM;
933 goto out;
934 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200935
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200936 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200937 if (PageDirty(page)) {
938 /*
939 * we need to write the data to the defect sector. the
940 * data that was in that sector is not in memory,
941 * because the page was modified. we must not write the
942 * modified page to that sector.
943 *
944 * TODO: what could be done here: wait for the delalloc
945 * runner to write out that page (might involve
946 * COW) and see whether the sector is still
947 * referenced afterwards.
948 *
949 * For the meantime, we'll treat this error
950 * incorrectable, although there is a chance that a
951 * later scrub will find the bad sector again and that
952 * there's no dirty page in memory, then.
953 */
954 ret = -EIO;
955 goto out;
956 }
Josef Bacik6ec656b2017-05-05 11:57:14 -0400957 ret = repair_io_failure(fs_info, inum, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200958 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800959 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200960 fixup->mirror_num);
961 unlock_page(page);
962 corrected = !ret;
963 } else {
964 /*
965 * we need to get good data first. the general readpage path
966 * will call repair_io_failure for us, we just have to make
967 * sure we read the bad mirror.
968 */
969 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +0200970 EXTENT_DAMAGED);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200971 if (ret) {
972 /* set_extent_bits should give proper error */
973 WARN_ON(ret > 0);
974 if (ret > 0)
975 ret = -EFAULT;
976 goto out;
977 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200978
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200979 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
980 btrfs_get_extent,
981 fixup->mirror_num);
982 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200983
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200984 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
985 end, EXTENT_DAMAGED, 0, NULL);
986 if (!corrected)
987 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
David Sterba91166212016-04-26 23:54:39 +0200988 EXTENT_DAMAGED);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200989 }
990
991out:
992 if (page)
993 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200994
995 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200996
997 if (ret < 0)
998 return ret;
999
1000 if (ret == 0 && corrected) {
1001 /*
1002 * we only need to call readpage for one of the inodes belonging
1003 * to this extent. so make iterate_extent_inodes stop
1004 */
1005 return 1;
1006 }
1007
1008 return -EIO;
1009}
1010
1011static void scrub_fixup_nodatasum(struct btrfs_work *work)
1012{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001014 int ret;
1015 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001016 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001017 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001018 struct btrfs_path *path;
1019 int uncorrectable = 0;
1020
1021 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001022 sctx = fixup->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001023 fs_info = fixup->root->fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001024
1025 path = btrfs_alloc_path();
1026 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001027 spin_lock(&sctx->stat_lock);
1028 ++sctx->stat.malloc_errors;
1029 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001030 uncorrectable = 1;
1031 goto out;
1032 }
1033
1034 trans = btrfs_join_transaction(fixup->root);
1035 if (IS_ERR(trans)) {
1036 uncorrectable = 1;
1037 goto out;
1038 }
1039
1040 /*
1041 * the idea is to trigger a regular read through the standard path. we
1042 * read a page from the (failed) logical address by specifying the
1043 * corresponding copynum of the failed sector. thus, that readpage is
1044 * expected to fail.
1045 * that is the point where on-the-fly error correction will kick in
1046 * (once it's finished) and rewrite the failed sector if a good copy
1047 * can be found.
1048 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001049 ret = iterate_inodes_from_logical(fixup->logical, fs_info, path,
1050 scrub_fixup_readpage, fixup);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001051 if (ret < 0) {
1052 uncorrectable = 1;
1053 goto out;
1054 }
1055 WARN_ON(ret != 1);
1056
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001057 spin_lock(&sctx->stat_lock);
1058 ++sctx->stat.corrected_errors;
1059 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001060
1061out:
1062 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001063 btrfs_end_transaction(trans);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001064 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001065 spin_lock(&sctx->stat_lock);
1066 ++sctx->stat.uncorrectable_errors;
1067 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001068 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001069 &fs_info->dev_replace.num_uncorrectable_read_errors);
1070 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02001071 "unable to fixup (nodatasum) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001072 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001073 }
1074
1075 btrfs_free_path(path);
1076 kfree(fixup);
1077
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001078 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001079}
1080
Miao Xieaf8e2d12014-10-23 14:42:50 +08001081static inline void scrub_get_recover(struct scrub_recover *recover)
1082{
Elena Reshetova6f615012017-03-03 10:55:21 +02001083 refcount_inc(&recover->refs);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001084}
1085
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001086static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
1087 struct scrub_recover *recover)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001088{
Elena Reshetova6f615012017-03-03 10:55:21 +02001089 if (refcount_dec_and_test(&recover->refs)) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001090 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08001091 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001092 kfree(recover);
1093 }
1094}
1095
Arne Jansena2de7332011-03-08 14:14:00 +01001096/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001097 * scrub_handle_errored_block gets called when either verification of the
1098 * pages failed or the bio failed to read, e.g. with EIO. In the latter
1099 * case, this function handles all pages in the bio, even though only one
1100 * may be bad.
1101 * The goal of this function is to repair the errored block by using the
1102 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +01001103 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001104static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +01001105{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001106 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001107 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001108 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01001109 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001110 u64 logical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001111 unsigned int failed_mirror_index;
1112 unsigned int is_metadata;
1113 unsigned int have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001114 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
1115 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +01001116 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001117 int mirror_index;
1118 int page_num;
1119 int success;
Qu Wenruo28d70e22017-04-14 08:35:55 +08001120 bool full_stripe_locked;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001121 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1122 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +01001123
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001124 BUG_ON(sblock_to_check->page_count < 1);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001125 fs_info = sctx->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +00001126 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
1127 /*
1128 * if we find an error in a super block, we just report it.
1129 * They will get written with the next transaction commit
1130 * anyway
1131 */
1132 spin_lock(&sctx->stat_lock);
1133 ++sctx->stat.super_errors;
1134 spin_unlock(&sctx->stat_lock);
1135 return 0;
1136 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001137 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001138 logical = sblock_to_check->pagev[0]->logical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001139 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
1140 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
1141 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001142 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001143 have_csum = sblock_to_check->pagev[0]->have_csum;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001144 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001145
Qu Wenruo28d70e22017-04-14 08:35:55 +08001146 /*
1147 * For RAID5/6, race can happen for a different device scrub thread.
1148 * For data corruption, Parity and Data threads will both try
1149 * to recovery the data.
1150 * Race can lead to doubly added csum error, or even unrecoverable
1151 * error.
1152 */
1153 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
1154 if (ret < 0) {
1155 spin_lock(&sctx->stat_lock);
1156 if (ret == -ENOMEM)
1157 sctx->stat.malloc_errors++;
1158 sctx->stat.read_errors++;
1159 sctx->stat.uncorrectable_errors++;
1160 spin_unlock(&sctx->stat_lock);
1161 return ret;
1162 }
1163
Stefan Behrensff023aa2012-11-06 11:43:11 +01001164 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
1165 sblocks_for_recheck = NULL;
1166 goto nodatasum_case;
1167 }
1168
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001169 /*
1170 * read all mirrors one after the other. This includes to
1171 * re-read the extent or metadata block that failed (that was
1172 * the cause that this fixup code is called) another time,
1173 * page by page this time in order to know which pages
1174 * caused I/O errors and which ones are good (for all mirrors).
1175 * It is the goal to handle the situation when more than one
1176 * mirror contains I/O errors, but the errors do not
1177 * overlap, i.e. the data can be repaired by selecting the
1178 * pages from those mirrors without I/O error on the
1179 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
1180 * would be that mirror #1 has an I/O error on the first page,
1181 * the second page is good, and mirror #2 has an I/O error on
1182 * the second page, but the first page is good.
1183 * Then the first page of the first mirror can be repaired by
1184 * taking the first page of the second mirror, and the
1185 * second page of the second mirror can be repaired by
1186 * copying the contents of the 2nd page of the 1st mirror.
1187 * One more note: if the pages of one mirror contain I/O
1188 * errors, the checksum cannot be verified. In order to get
1189 * the best data for repairing, the first attempt is to find
1190 * a mirror without I/O errors and with a validated checksum.
1191 * Only if this is not possible, the pages are picked from
1192 * mirrors with I/O errors without considering the checksum.
1193 * If the latter is the case, at the end, the checksum of the
1194 * repaired area is verified in order to correctly maintain
1195 * the statistics.
1196 */
1197
David Sterba31e818f2015-02-20 18:00:26 +01001198 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
1199 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001200 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001201 spin_lock(&sctx->stat_lock);
1202 sctx->stat.malloc_errors++;
1203 sctx->stat.read_errors++;
1204 sctx->stat.uncorrectable_errors++;
1205 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001206 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001207 goto out;
1208 }
1209
1210 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +08001211 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001212 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001213 spin_lock(&sctx->stat_lock);
1214 sctx->stat.read_errors++;
1215 sctx->stat.uncorrectable_errors++;
1216 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001217 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001218 goto out;
1219 }
1220 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
1221 sblock_bad = sblocks_for_recheck + failed_mirror_index;
1222
1223 /* build and submit the bios for the failed mirror, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001224 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001225
1226 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
1227 sblock_bad->no_io_error_seen) {
1228 /*
1229 * the error disappeared after reading page by page, or
1230 * the area was part of a huge bio and other parts of the
1231 * bio caused I/O errors, or the block layer merged several
1232 * read requests into one and the error is caused by a
1233 * different bio (usually one of the two latter cases is
1234 * the cause)
1235 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001236 spin_lock(&sctx->stat_lock);
1237 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001238 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001239 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001240
Stefan Behrensff023aa2012-11-06 11:43:11 +01001241 if (sctx->is_dev_replace)
1242 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001243 goto out;
1244 }
1245
1246 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001247 spin_lock(&sctx->stat_lock);
1248 sctx->stat.read_errors++;
1249 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001250 if (__ratelimit(&_rs))
1251 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001252 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001253 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001254 spin_lock(&sctx->stat_lock);
1255 sctx->stat.csum_errors++;
1256 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001257 if (__ratelimit(&_rs))
1258 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001259 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001260 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001261 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001262 spin_lock(&sctx->stat_lock);
1263 sctx->stat.verify_errors++;
1264 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001265 if (__ratelimit(&_rs))
1266 scrub_print_warning("checksum/header error",
1267 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001268 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001269 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001270 BTRFS_DEV_STAT_GENERATION_ERRS);
1271 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001272 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001273 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001274 }
1275
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001276 if (sctx->readonly) {
1277 ASSERT(!sctx->is_dev_replace);
1278 goto out;
1279 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001280
1281 if (!is_metadata && !have_csum) {
1282 struct scrub_fixup_nodatasum *fixup_nodatasum;
1283
Stefan Behrensff023aa2012-11-06 11:43:11 +01001284 WARN_ON(sctx->is_dev_replace);
1285
Zhao Leib25c94c2015-01-20 15:11:35 +08001286nodatasum_case:
1287
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001288 /*
1289 * !is_metadata and !have_csum, this means that the data
Nicholas D Steeves01327612016-05-19 21:18:45 -04001290 * might not be COWed, that it might be modified
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001291 * concurrently. The general strategy to work on the
1292 * commit root does not help in the case when COW is not
1293 * used.
1294 */
1295 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1296 if (!fixup_nodatasum)
1297 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001298 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001299 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001300 fixup_nodatasum->logical = logical;
1301 fixup_nodatasum->root = fs_info->extent_root;
1302 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001303 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001304 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1305 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001306 btrfs_queue_work(fs_info->scrub_workers,
1307 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001308 goto out;
1309 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001310
1311 /*
1312 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001313 * checksums.
1314 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001315 * errors and also does not have a checksum error.
1316 * If one is found, and if a checksum is present, the full block
1317 * that is known to contain an error is rewritten. Afterwards
1318 * the block is known to be corrected.
1319 * If a mirror is found which is completely correct, and no
1320 * checksum is present, only those pages are rewritten that had
1321 * an I/O error in the block to be repaired, since it cannot be
1322 * determined, which copy of the other pages is better (and it
1323 * could happen otherwise that a correct page would be
1324 * overwritten by a bad one).
1325 */
1326 for (mirror_index = 0;
1327 mirror_index < BTRFS_MAX_MIRRORS &&
1328 sblocks_for_recheck[mirror_index].page_count > 0;
1329 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001330 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001331
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001332 if (mirror_index == failed_mirror_index)
1333 continue;
1334 sblock_other = sblocks_for_recheck + mirror_index;
1335
1336 /* build and submit the bios, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001337 scrub_recheck_block(fs_info, sblock_other, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001338
1339 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001340 !sblock_other->checksum_error &&
1341 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001342 if (sctx->is_dev_replace) {
1343 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001344 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001345 } else {
1346 ret = scrub_repair_block_from_good_copy(
1347 sblock_bad, sblock_other);
1348 if (!ret)
1349 goto corrected_error;
1350 }
Arne Jansena2de7332011-03-08 14:14:00 +01001351 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001352 }
1353
Zhao Leib968fed2015-01-20 15:11:41 +08001354 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1355 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001356
1357 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001358 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001359 * repaired, continue by picking good copies of those pages.
1360 * Select the good pages from mirrors to rewrite bad pages from
1361 * the area to fix. Afterwards verify the checksum of the block
1362 * that is supposed to be repaired. This verification step is
1363 * only done for the purpose of statistic counting and for the
1364 * final scrub report, whether errors remain.
1365 * A perfect algorithm could make use of the checksum and try
1366 * all possible combinations of pages from the different mirrors
1367 * until the checksum verification succeeds. For example, when
1368 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1369 * of mirror #2 is readable but the final checksum test fails,
1370 * then the 2nd page of mirror #3 could be tried, whether now
Nicholas D Steeves01327612016-05-19 21:18:45 -04001371 * the final checksum succeeds. But this would be a rare
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001372 * exception and is therefore not implemented. At least it is
1373 * avoided that the good copy is overwritten.
1374 * A more useful improvement would be to pick the sectors
1375 * without I/O error based on sector sizes (512 bytes on legacy
1376 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1377 * mirror could be repaired by taking 512 byte of a different
1378 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1379 * area are unreadable.
1380 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001381 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001382 for (page_num = 0; page_num < sblock_bad->page_count;
1383 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001384 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001385 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001386
Zhao Leib968fed2015-01-20 15:11:41 +08001387 /* skip no-io-error page in scrub */
1388 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001389 continue;
1390
Zhao Leib968fed2015-01-20 15:11:41 +08001391 /* try to find no-io-error page in mirrors */
1392 if (page_bad->io_error) {
1393 for (mirror_index = 0;
1394 mirror_index < BTRFS_MAX_MIRRORS &&
1395 sblocks_for_recheck[mirror_index].page_count > 0;
1396 mirror_index++) {
1397 if (!sblocks_for_recheck[mirror_index].
1398 pagev[page_num]->io_error) {
1399 sblock_other = sblocks_for_recheck +
1400 mirror_index;
1401 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001402 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001403 }
Zhao Leib968fed2015-01-20 15:11:41 +08001404 if (!sblock_other)
1405 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001406 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001407
Zhao Leib968fed2015-01-20 15:11:41 +08001408 if (sctx->is_dev_replace) {
1409 /*
1410 * did not find a mirror to fetch the page
1411 * from. scrub_write_page_to_dev_replace()
1412 * handles this case (page->io_error), by
1413 * filling the block with zeros before
1414 * submitting the write request
1415 */
1416 if (!sblock_other)
1417 sblock_other = sblock_bad;
1418
1419 if (scrub_write_page_to_dev_replace(sblock_other,
1420 page_num) != 0) {
1421 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001422 &fs_info->dev_replace.num_write_errors);
Zhao Leib968fed2015-01-20 15:11:41 +08001423 success = 0;
1424 }
1425 } else if (sblock_other) {
1426 ret = scrub_repair_page_from_good_copy(sblock_bad,
1427 sblock_other,
1428 page_num, 0);
1429 if (0 == ret)
1430 page_bad->io_error = 0;
1431 else
1432 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001433 }
1434 }
1435
Zhao Leib968fed2015-01-20 15:11:41 +08001436 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001437 if (is_metadata || have_csum) {
1438 /*
1439 * need to verify the checksum now that all
1440 * sectors on disk are repaired (the write
1441 * request for data to be repaired is on its way).
1442 * Just be lazy and use scrub_recheck_block()
1443 * which re-reads the data before the checksum
1444 * is verified, but most likely the data comes out
1445 * of the page cache.
1446 */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001447 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001448 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001449 !sblock_bad->checksum_error &&
1450 sblock_bad->no_io_error_seen)
1451 goto corrected_error;
1452 else
1453 goto did_not_correct_error;
1454 } else {
1455corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001456 spin_lock(&sctx->stat_lock);
1457 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001458 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001459 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001460 btrfs_err_rl_in_rcu(fs_info,
1461 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001462 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001463 }
1464 } else {
1465did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001466 spin_lock(&sctx->stat_lock);
1467 sctx->stat.uncorrectable_errors++;
1468 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001469 btrfs_err_rl_in_rcu(fs_info,
1470 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001471 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001472 }
1473
1474out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001475 if (sblocks_for_recheck) {
1476 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1477 mirror_index++) {
1478 struct scrub_block *sblock = sblocks_for_recheck +
1479 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001480 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001481 int page_index;
1482
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001483 for (page_index = 0; page_index < sblock->page_count;
1484 page_index++) {
1485 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001486 recover = sblock->pagev[page_index]->recover;
1487 if (recover) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001488 scrub_put_recover(fs_info, recover);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001489 sblock->pagev[page_index]->recover =
1490 NULL;
1491 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001492 scrub_page_put(sblock->pagev[page_index]);
1493 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001494 }
1495 kfree(sblocks_for_recheck);
1496 }
1497
Qu Wenruo28d70e22017-04-14 08:35:55 +08001498 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
1499 if (ret < 0)
1500 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001501 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001502}
1503
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001504static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001505{
Zhao Lei10f11902015-01-20 15:11:43 +08001506 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1507 return 2;
1508 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1509 return 3;
1510 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001511 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001512}
1513
Zhao Lei10f11902015-01-20 15:11:43 +08001514static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1515 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001516 u64 mapped_length,
1517 int nstripes, int mirror,
1518 int *stripe_index,
1519 u64 *stripe_offset)
1520{
1521 int i;
1522
Zhao Leiffe2d202015-01-20 15:11:44 +08001523 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001524 /* RAID5/6 */
1525 for (i = 0; i < nstripes; i++) {
1526 if (raid_map[i] == RAID6_Q_STRIPE ||
1527 raid_map[i] == RAID5_P_STRIPE)
1528 continue;
1529
1530 if (logical >= raid_map[i] &&
1531 logical < raid_map[i] + mapped_length)
1532 break;
1533 }
1534
1535 *stripe_index = i;
1536 *stripe_offset = logical - raid_map[i];
1537 } else {
1538 /* The other RAID type */
1539 *stripe_index = mirror;
1540 *stripe_offset = 0;
1541 }
1542}
1543
Zhao Leibe50a8d2015-01-20 15:11:42 +08001544static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001545 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001546{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001547 struct scrub_ctx *sctx = original_sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001548 struct btrfs_fs_info *fs_info = sctx->fs_info;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001549 u64 length = original_sblock->page_count * PAGE_SIZE;
1550 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001551 u64 generation = original_sblock->pagev[0]->generation;
1552 u64 flags = original_sblock->pagev[0]->flags;
1553 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001554 struct scrub_recover *recover;
1555 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001556 u64 sublen;
1557 u64 mapped_length;
1558 u64 stripe_offset;
1559 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001560 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001561 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001562 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001563 int ret;
1564
1565 /*
Zhao Lei57019342015-01-20 15:11:45 +08001566 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001567 * are not used (and not set) in the blocks that are used for
1568 * the recheck procedure
1569 */
1570
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001571 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001572 sublen = min_t(u64, length, PAGE_SIZE);
1573 mapped_length = sublen;
1574 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001575
1576 /*
1577 * with a length of PAGE_SIZE, each returned stripe
1578 * represents one mirror
1579 */
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001580 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001581 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
David Sterba825ad4c2017-03-28 14:45:22 +02001582 logical, &mapped_length, &bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001583 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001584 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001585 btrfs_bio_counter_dec(fs_info);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001586 return -EIO;
1587 }
1588
Miao Xieaf8e2d12014-10-23 14:42:50 +08001589 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1590 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001591 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001592 btrfs_bio_counter_dec(fs_info);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001593 return -ENOMEM;
1594 }
1595
Elena Reshetova6f615012017-03-03 10:55:21 +02001596 refcount_set(&recover->refs, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001597 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001598 recover->map_length = mapped_length;
1599
Ashish Samant24731142016-04-29 18:33:59 -07001600 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001601
Zhao Leibe50a8d2015-01-20 15:11:42 +08001602 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001603
Miao Xieaf8e2d12014-10-23 14:42:50 +08001604 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001605 mirror_index++) {
1606 struct scrub_block *sblock;
1607 struct scrub_page *page;
1608
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001609 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001610 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001611
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001612 page = kzalloc(sizeof(*page), GFP_NOFS);
1613 if (!page) {
1614leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001615 spin_lock(&sctx->stat_lock);
1616 sctx->stat.malloc_errors++;
1617 spin_unlock(&sctx->stat_lock);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001618 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001619 return -ENOMEM;
1620 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001621 scrub_page_get(page);
1622 sblock->pagev[page_index] = page;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001623 page->sblock = sblock;
1624 page->flags = flags;
1625 page->generation = generation;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001626 page->logical = logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001627 page->have_csum = have_csum;
1628 if (have_csum)
1629 memcpy(page->csum,
1630 original_sblock->pagev[0]->csum,
1631 sctx->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001632
Zhao Lei10f11902015-01-20 15:11:43 +08001633 scrub_stripe_index_and_offset(logical,
1634 bbio->map_type,
1635 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001636 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001637 bbio->num_stripes -
1638 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001639 mirror_index,
1640 &stripe_index,
1641 &stripe_offset);
1642 page->physical = bbio->stripes[stripe_index].physical +
1643 stripe_offset;
1644 page->dev = bbio->stripes[stripe_index].dev;
1645
Stefan Behrensff023aa2012-11-06 11:43:11 +01001646 BUG_ON(page_index >= original_sblock->page_count);
1647 page->physical_for_dev_replace =
1648 original_sblock->pagev[page_index]->
1649 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001650 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001651 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001652 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001653 page->page = alloc_page(GFP_NOFS);
1654 if (!page->page)
1655 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001656
1657 scrub_get_recover(recover);
1658 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001659 }
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001660 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001661 length -= sublen;
1662 logical += sublen;
1663 page_index++;
1664 }
1665
1666 return 0;
1667}
1668
Miao Xieaf8e2d12014-10-23 14:42:50 +08001669struct scrub_bio_ret {
1670 struct completion event;
1671 int error;
1672};
1673
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001674static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001675{
1676 struct scrub_bio_ret *ret = bio->bi_private;
1677
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001678 ret->error = bio->bi_error;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001679 complete(&ret->event);
1680}
1681
1682static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1683{
Zhao Lei10f11902015-01-20 15:11:43 +08001684 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001685 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001686}
1687
1688static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1689 struct bio *bio,
1690 struct scrub_page *page)
1691{
1692 struct scrub_bio_ret done;
1693 int ret;
1694
1695 init_completion(&done.event);
1696 done.error = 0;
1697 bio->bi_iter.bi_sector = page->logical >> 9;
1698 bio->bi_private = &done;
1699 bio->bi_end_io = scrub_bio_wait_endio;
1700
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001701 ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001702 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001703 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001704 if (ret)
1705 return ret;
1706
1707 wait_for_completion(&done.event);
1708 if (done.error)
1709 return -EIO;
1710
1711 return 0;
1712}
1713
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001714/*
1715 * this function will check the on disk data for checksum errors, header
1716 * errors and read I/O errors. If any I/O errors happen, the exact pages
1717 * which are errored are marked as being bad. The goal is to enable scrub
1718 * to take those pages that are not errored from all the mirrors so that
1719 * the pages that are errored in the just handled mirror can be repaired.
1720 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001721static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001722 struct scrub_block *sblock,
1723 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001724{
1725 int page_num;
1726
1727 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001728
1729 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1730 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001731 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001732
Stefan Behrens442a4f62012-05-25 16:06:08 +02001733 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001734 page->io_error = 1;
1735 sblock->no_io_error_seen = 0;
1736 continue;
1737 }
1738
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001739 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001740 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001741 if (!bio) {
1742 page->io_error = 1;
1743 sblock->no_io_error_seen = 0;
1744 continue;
1745 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001746 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001747
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001748 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001749 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001750 if (scrub_submit_raid56_bio_wait(fs_info, bio, page)) {
1751 page->io_error = 1;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001752 sblock->no_io_error_seen = 0;
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001753 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001754 } else {
1755 bio->bi_iter.bi_sector = page->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001756 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001757
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001758 if (btrfsic_submit_bio_wait(bio)) {
1759 page->io_error = 1;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001760 sblock->no_io_error_seen = 0;
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001761 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001762 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001763
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001764 bio_put(bio);
1765 }
1766
1767 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001768 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001769}
1770
Miao Xie17a9be22014-07-24 11:37:08 +08001771static inline int scrub_check_fsid(u8 fsid[],
1772 struct scrub_page *spage)
1773{
1774 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1775 int ret;
1776
1777 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1778 return !ret;
1779}
1780
Zhao Leiba7cf982015-08-24 21:18:02 +08001781static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001782{
Zhao Leiba7cf982015-08-24 21:18:02 +08001783 sblock->header_error = 0;
1784 sblock->checksum_error = 0;
1785 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001786
Zhao Leiba7cf982015-08-24 21:18:02 +08001787 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1788 scrub_checksum_data(sblock);
1789 else
1790 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001791}
1792
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001793static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001794 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001795{
1796 int page_num;
1797 int ret = 0;
1798
1799 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1800 int ret_sub;
1801
1802 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1803 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001804 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001805 if (ret_sub)
1806 ret = ret_sub;
1807 }
1808
1809 return ret;
1810}
1811
1812static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1813 struct scrub_block *sblock_good,
1814 int page_num, int force_write)
1815{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001816 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1817 struct scrub_page *page_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001818 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001819
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001820 BUG_ON(page_bad->page == NULL);
1821 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001822 if (force_write || sblock_bad->header_error ||
1823 sblock_bad->checksum_error || page_bad->io_error) {
1824 struct bio *bio;
1825 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001826
Stefan Behrensff023aa2012-11-06 11:43:11 +01001827 if (!page_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001828 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001829 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001830 return -EIO;
1831 }
1832
Chris Mason9be33952013-05-17 18:30:14 -04001833 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001834 if (!bio)
1835 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001836 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001837 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001838 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001839
1840 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1841 if (PAGE_SIZE != ret) {
1842 bio_put(bio);
1843 return -EIO;
1844 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001845
Mike Christie4e49ea42016-06-05 14:31:41 -05001846 if (btrfsic_submit_bio_wait(bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001847 btrfs_dev_stat_inc_and_print(page_bad->dev,
1848 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001849 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001850 &fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001851 bio_put(bio);
1852 return -EIO;
1853 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001854 bio_put(bio);
1855 }
1856
1857 return 0;
1858}
1859
Stefan Behrensff023aa2012-11-06 11:43:11 +01001860static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1861{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001862 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001863 int page_num;
1864
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001865 /*
1866 * This block is used for the check of the parity on the source device,
1867 * so the data needn't be written into the destination device.
1868 */
1869 if (sblock->sparity)
1870 return;
1871
Stefan Behrensff023aa2012-11-06 11:43:11 +01001872 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1873 int ret;
1874
1875 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1876 if (ret)
1877 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001878 &fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001879 }
1880}
1881
1882static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1883 int page_num)
1884{
1885 struct scrub_page *spage = sblock->pagev[page_num];
1886
1887 BUG_ON(spage->page == NULL);
1888 if (spage->io_error) {
1889 void *mapped_buffer = kmap_atomic(spage->page);
1890
David Sterba619a9742017-03-29 20:48:44 +02001891 clear_page(mapped_buffer);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001892 flush_dcache_page(spage->page);
1893 kunmap_atomic(mapped_buffer);
1894 }
1895 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1896}
1897
1898static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1899 struct scrub_page *spage)
1900{
1901 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1902 struct scrub_bio *sbio;
1903 int ret;
1904
1905 mutex_lock(&wr_ctx->wr_lock);
1906again:
1907 if (!wr_ctx->wr_curr_bio) {
1908 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001909 GFP_KERNEL);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001910 if (!wr_ctx->wr_curr_bio) {
1911 mutex_unlock(&wr_ctx->wr_lock);
1912 return -ENOMEM;
1913 }
1914 wr_ctx->wr_curr_bio->sctx = sctx;
1915 wr_ctx->wr_curr_bio->page_count = 0;
1916 }
1917 sbio = wr_ctx->wr_curr_bio;
1918 if (sbio->page_count == 0) {
1919 struct bio *bio;
1920
1921 sbio->physical = spage->physical_for_dev_replace;
1922 sbio->logical = spage->logical;
1923 sbio->dev = wr_ctx->tgtdev;
1924 bio = sbio->bio;
1925 if (!bio) {
David Sterba58c4e172016-02-11 10:49:42 +01001926 bio = btrfs_io_bio_alloc(GFP_KERNEL,
1927 wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001928 if (!bio) {
1929 mutex_unlock(&wr_ctx->wr_lock);
1930 return -ENOMEM;
1931 }
1932 sbio->bio = bio;
1933 }
1934
1935 bio->bi_private = sbio;
1936 bio->bi_end_io = scrub_wr_bio_end_io;
1937 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001938 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001939 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001940 sbio->err = 0;
1941 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1942 spage->physical_for_dev_replace ||
1943 sbio->logical + sbio->page_count * PAGE_SIZE !=
1944 spage->logical) {
1945 scrub_wr_submit(sctx);
1946 goto again;
1947 }
1948
1949 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1950 if (ret != PAGE_SIZE) {
1951 if (sbio->page_count < 1) {
1952 bio_put(sbio->bio);
1953 sbio->bio = NULL;
1954 mutex_unlock(&wr_ctx->wr_lock);
1955 return -EIO;
1956 }
1957 scrub_wr_submit(sctx);
1958 goto again;
1959 }
1960
1961 sbio->pagev[sbio->page_count] = spage;
1962 scrub_page_get(spage);
1963 sbio->page_count++;
1964 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1965 scrub_wr_submit(sctx);
1966 mutex_unlock(&wr_ctx->wr_lock);
1967
1968 return 0;
1969}
1970
1971static void scrub_wr_submit(struct scrub_ctx *sctx)
1972{
1973 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1974 struct scrub_bio *sbio;
1975
1976 if (!wr_ctx->wr_curr_bio)
1977 return;
1978
1979 sbio = wr_ctx->wr_curr_bio;
1980 wr_ctx->wr_curr_bio = NULL;
1981 WARN_ON(!sbio->bio->bi_bdev);
1982 scrub_pending_bio_inc(sctx);
1983 /* process all writes in a single worker thread. Then the block layer
1984 * orders the requests before sending them to the driver which
1985 * doubled the write performance on spinning disks when measured
1986 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001987 btrfsic_submit_bio(sbio->bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001988}
1989
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001990static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001991{
1992 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001993 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001994
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001995 sbio->err = bio->bi_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001996 sbio->bio = bio;
1997
Liu Bo9e0af232014-08-15 23:36:53 +08001998 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1999 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08002000 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002001}
2002
2003static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
2004{
2005 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
2006 struct scrub_ctx *sctx = sbio->sctx;
2007 int i;
2008
2009 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
2010 if (sbio->err) {
2011 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002012 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002013
2014 for (i = 0; i < sbio->page_count; i++) {
2015 struct scrub_page *spage = sbio->pagev[i];
2016
2017 spage->io_error = 1;
2018 btrfs_dev_replace_stats_inc(&dev_replace->
2019 num_write_errors);
2020 }
2021 }
2022
2023 for (i = 0; i < sbio->page_count; i++)
2024 scrub_page_put(sbio->pagev[i]);
2025
2026 bio_put(sbio->bio);
2027 kfree(sbio);
2028 scrub_pending_bio_dec(sctx);
2029}
2030
2031static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002032{
2033 u64 flags;
2034 int ret;
2035
Zhao Leiba7cf982015-08-24 21:18:02 +08002036 /*
2037 * No need to initialize these stats currently,
2038 * because this function only use return value
2039 * instead of these stats value.
2040 *
2041 * Todo:
2042 * always use stats
2043 */
2044 sblock->header_error = 0;
2045 sblock->generation_error = 0;
2046 sblock->checksum_error = 0;
2047
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002048 WARN_ON(sblock->page_count < 1);
2049 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002050 ret = 0;
2051 if (flags & BTRFS_EXTENT_FLAG_DATA)
2052 ret = scrub_checksum_data(sblock);
2053 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
2054 ret = scrub_checksum_tree_block(sblock);
2055 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
2056 (void)scrub_checksum_super(sblock);
2057 else
2058 WARN_ON(1);
2059 if (ret)
2060 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002061
2062 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002063}
2064
2065static int scrub_checksum_data(struct scrub_block *sblock)
2066{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002067 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002068 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002069 u8 *on_disk_csum;
2070 struct page *page;
2071 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01002072 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002073 u64 len;
2074 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002075
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002076 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002077 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002078 return 0;
2079
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002080 on_disk_csum = sblock->pagev[0]->csum;
2081 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002082 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002083
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002084 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002085 index = 0;
2086 for (;;) {
2087 u64 l = min_t(u64, len, PAGE_SIZE);
2088
Liu Bob0496682013-03-14 14:57:45 +00002089 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002090 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002091 len -= l;
2092 if (len == 0)
2093 break;
2094 index++;
2095 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002096 BUG_ON(!sblock->pagev[index]->page);
2097 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002098 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002099 }
2100
Arne Jansena2de7332011-03-08 14:14:00 +01002101 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002102 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002103 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002104
Zhao Leiba7cf982015-08-24 21:18:02 +08002105 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002106}
2107
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002108static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002109{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002110 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002111 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002112 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002113 u8 calculated_csum[BTRFS_CSUM_SIZE];
2114 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2115 struct page *page;
2116 void *mapped_buffer;
2117 u64 mapped_size;
2118 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002119 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002120 u64 len;
2121 int index;
2122
2123 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002124 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002125 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002126 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002127 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002128
2129 /*
2130 * we don't use the getter functions here, as we
2131 * a) don't have an extent buffer and
2132 * b) the page is already kmapped
2133 */
Qu Wenruo3cae2102013-07-16 11:19:18 +08002134 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08002135 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002136
Zhao Leiba7cf982015-08-24 21:18:02 +08002137 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
2138 sblock->header_error = 1;
2139 sblock->generation_error = 1;
2140 }
Arne Jansena2de7332011-03-08 14:14:00 +01002141
Miao Xie17a9be22014-07-24 11:37:08 +08002142 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Zhao Leiba7cf982015-08-24 21:18:02 +08002143 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002144
2145 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
2146 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08002147 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002148
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002149 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002150 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2151 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2152 index = 0;
2153 for (;;) {
2154 u64 l = min_t(u64, len, mapped_size);
2155
Liu Bob0496682013-03-14 14:57:45 +00002156 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002157 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002158 len -= l;
2159 if (len == 0)
2160 break;
2161 index++;
2162 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002163 BUG_ON(!sblock->pagev[index]->page);
2164 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002165 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002166 mapped_size = PAGE_SIZE;
2167 p = mapped_buffer;
2168 }
2169
2170 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002171 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002172 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002173
Zhao Leiba7cf982015-08-24 21:18:02 +08002174 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002175}
2176
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002177static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002178{
2179 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002180 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002181 u8 calculated_csum[BTRFS_CSUM_SIZE];
2182 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2183 struct page *page;
2184 void *mapped_buffer;
2185 u64 mapped_size;
2186 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002187 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002188 int fail_gen = 0;
2189 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002190 u64 len;
2191 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002192
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002193 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002194 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002195 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002196 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002197 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002198
Qu Wenruo3cae2102013-07-16 11:19:18 +08002199 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002200 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002201
Qu Wenruo3cae2102013-07-16 11:19:18 +08002202 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002203 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002204
Miao Xie17a9be22014-07-24 11:37:08 +08002205 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002206 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002207
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002208 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2209 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2210 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2211 index = 0;
2212 for (;;) {
2213 u64 l = min_t(u64, len, mapped_size);
2214
Liu Bob0496682013-03-14 14:57:45 +00002215 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002216 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002217 len -= l;
2218 if (len == 0)
2219 break;
2220 index++;
2221 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002222 BUG_ON(!sblock->pagev[index]->page);
2223 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002224 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002225 mapped_size = PAGE_SIZE;
2226 p = mapped_buffer;
2227 }
2228
2229 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002230 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002231 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002232
Stefan Behrens442a4f62012-05-25 16:06:08 +02002233 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002234 /*
2235 * if we find an error in a super block, we just report it.
2236 * They will get written with the next transaction commit
2237 * anyway
2238 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002239 spin_lock(&sctx->stat_lock);
2240 ++sctx->stat.super_errors;
2241 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002242 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002243 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002244 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2245 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002246 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002247 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002248 }
2249
Stefan Behrens442a4f62012-05-25 16:06:08 +02002250 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002251}
2252
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002253static void scrub_block_get(struct scrub_block *sblock)
2254{
Elena Reshetova186debd2017-03-03 10:55:23 +02002255 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002256}
2257
2258static void scrub_block_put(struct scrub_block *sblock)
2259{
Elena Reshetova186debd2017-03-03 10:55:23 +02002260 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002261 int i;
2262
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002263 if (sblock->sparity)
2264 scrub_parity_put(sblock->sparity);
2265
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002266 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002267 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002268 kfree(sblock);
2269 }
2270}
2271
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002272static void scrub_page_get(struct scrub_page *spage)
2273{
Zhao Lei57019342015-01-20 15:11:45 +08002274 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002275}
2276
2277static void scrub_page_put(struct scrub_page *spage)
2278{
Zhao Lei57019342015-01-20 15:11:45 +08002279 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002280 if (spage->page)
2281 __free_page(spage->page);
2282 kfree(spage);
2283 }
2284}
2285
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002286static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002287{
2288 struct scrub_bio *sbio;
2289
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002290 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002291 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002292
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002293 sbio = sctx->bios[sctx->curr];
2294 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002295 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002296 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002297}
2298
Stefan Behrensff023aa2012-11-06 11:43:11 +01002299static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2300 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002301{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002302 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002303 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002304 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002305
2306again:
2307 /*
2308 * grab a fresh bio or wait for one to become available
2309 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002310 while (sctx->curr == -1) {
2311 spin_lock(&sctx->list_lock);
2312 sctx->curr = sctx->first_free;
2313 if (sctx->curr != -1) {
2314 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2315 sctx->bios[sctx->curr]->next_free = -1;
2316 sctx->bios[sctx->curr]->page_count = 0;
2317 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002318 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002319 spin_unlock(&sctx->list_lock);
2320 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002321 }
2322 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002323 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002324 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002325 struct bio *bio;
2326
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002327 sbio->physical = spage->physical;
2328 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002329 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002330 bio = sbio->bio;
2331 if (!bio) {
David Sterba58c4e172016-02-11 10:49:42 +01002332 bio = btrfs_io_bio_alloc(GFP_KERNEL,
2333 sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002334 if (!bio)
2335 return -ENOMEM;
2336 sbio->bio = bio;
2337 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002338
2339 bio->bi_private = sbio;
2340 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002341 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002342 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05002343 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002344 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002345 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2346 spage->physical ||
2347 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002348 spage->logical ||
2349 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002350 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002351 goto again;
2352 }
2353
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002354 sbio->pagev[sbio->page_count] = spage;
2355 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2356 if (ret != PAGE_SIZE) {
2357 if (sbio->page_count < 1) {
2358 bio_put(sbio->bio);
2359 sbio->bio = NULL;
2360 return -EIO;
2361 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002362 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002363 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002364 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002365
Stefan Behrensff023aa2012-11-06 11:43:11 +01002366 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002367 atomic_inc(&sblock->outstanding_pages);
2368 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002369 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002370 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002371
2372 return 0;
2373}
2374
Linus Torvalds22365972015-09-05 15:14:43 -07002375static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002376{
2377 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002378 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002379
Linus Torvalds22365972015-09-05 15:14:43 -07002380 if (bio->bi_error)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002381 sblock->no_io_error_seen = 0;
2382
Scott Talbert46732722016-05-09 09:14:28 -04002383 bio_put(bio);
2384
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002385 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2386}
2387
2388static void scrub_missing_raid56_worker(struct btrfs_work *work)
2389{
2390 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2391 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002392 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002393 u64 logical;
2394 struct btrfs_device *dev;
2395
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002396 logical = sblock->pagev[0]->logical;
2397 dev = sblock->pagev[0]->dev;
2398
Zhao Leiaffe4a52015-08-24 21:32:06 +08002399 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002400 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002401
2402 if (!sblock->no_io_error_seen) {
2403 spin_lock(&sctx->stat_lock);
2404 sctx->stat.read_errors++;
2405 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002406 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002407 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002408 logical, rcu_str_deref(dev->name));
2409 } else if (sblock->header_error || sblock->checksum_error) {
2410 spin_lock(&sctx->stat_lock);
2411 sctx->stat.uncorrectable_errors++;
2412 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002413 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002414 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002415 logical, rcu_str_deref(dev->name));
2416 } else {
2417 scrub_write_block_to_dev_replace(sblock);
2418 }
2419
2420 scrub_block_put(sblock);
2421
2422 if (sctx->is_dev_replace &&
2423 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2424 mutex_lock(&sctx->wr_ctx.wr_lock);
2425 scrub_wr_submit(sctx);
2426 mutex_unlock(&sctx->wr_ctx.wr_lock);
2427 }
2428
2429 scrub_pending_bio_dec(sctx);
2430}
2431
2432static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2433{
2434 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002435 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002436 u64 length = sblock->page_count * PAGE_SIZE;
2437 u64 logical = sblock->pagev[0]->logical;
Zhao Leif1fee652016-05-17 17:37:38 +08002438 struct btrfs_bio *bbio = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002439 struct bio *bio;
2440 struct btrfs_raid_bio *rbio;
2441 int ret;
2442 int i;
2443
Qu Wenruoae6529c2017-03-29 09:33:21 +08002444 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002445 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
David Sterba825ad4c2017-03-28 14:45:22 +02002446 &length, &bbio);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002447 if (ret || !bbio || !bbio->raid_map)
2448 goto bbio_out;
2449
2450 if (WARN_ON(!sctx->is_dev_replace ||
2451 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2452 /*
2453 * We shouldn't be scrubbing a missing device. Even for dev
2454 * replace, we should only get here for RAID 5/6. We either
2455 * managed to mount something with no mirrors remaining or
2456 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2457 */
2458 goto bbio_out;
2459 }
2460
2461 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2462 if (!bio)
2463 goto bbio_out;
2464
2465 bio->bi_iter.bi_sector = logical >> 9;
2466 bio->bi_private = sblock;
2467 bio->bi_end_io = scrub_missing_raid56_end_io;
2468
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002469 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002470 if (!rbio)
2471 goto rbio_out;
2472
2473 for (i = 0; i < sblock->page_count; i++) {
2474 struct scrub_page *spage = sblock->pagev[i];
2475
2476 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2477 }
2478
2479 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2480 scrub_missing_raid56_worker, NULL, NULL);
2481 scrub_block_get(sblock);
2482 scrub_pending_bio_inc(sctx);
2483 raid56_submit_missing_rbio(rbio);
2484 return;
2485
2486rbio_out:
2487 bio_put(bio);
2488bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002489 btrfs_bio_counter_dec(fs_info);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002490 btrfs_put_bbio(bbio);
2491 spin_lock(&sctx->stat_lock);
2492 sctx->stat.malloc_errors++;
2493 spin_unlock(&sctx->stat_lock);
2494}
2495
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002496static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002497 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002498 u64 gen, int mirror_num, u8 *csum, int force,
2499 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002500{
2501 struct scrub_block *sblock;
2502 int index;
2503
David Sterba58c4e172016-02-11 10:49:42 +01002504 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002505 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002506 spin_lock(&sctx->stat_lock);
2507 sctx->stat.malloc_errors++;
2508 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002509 return -ENOMEM;
2510 }
2511
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002512 /* one ref inside this function, plus one for each page added to
2513 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002514 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002515 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002516 sblock->no_io_error_seen = 1;
2517
2518 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002519 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002520 u64 l = min_t(u64, len, PAGE_SIZE);
2521
David Sterba58c4e172016-02-11 10:49:42 +01002522 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002523 if (!spage) {
2524leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002525 spin_lock(&sctx->stat_lock);
2526 sctx->stat.malloc_errors++;
2527 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002528 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002529 return -ENOMEM;
2530 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002531 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2532 scrub_page_get(spage);
2533 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002534 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002535 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002536 spage->flags = flags;
2537 spage->generation = gen;
2538 spage->logical = logical;
2539 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002540 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002541 spage->mirror_num = mirror_num;
2542 if (csum) {
2543 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002544 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002545 } else {
2546 spage->have_csum = 0;
2547 }
2548 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002549 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002550 if (!spage->page)
2551 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002552 len -= l;
2553 logical += l;
2554 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002555 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002556 }
2557
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002558 WARN_ON(sblock->page_count == 0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002559 if (dev->missing) {
2560 /*
2561 * This case should only be hit for RAID 5/6 device replace. See
2562 * the comment in scrub_missing_raid56_pages() for details.
2563 */
2564 scrub_missing_raid56_pages(sblock);
2565 } else {
2566 for (index = 0; index < sblock->page_count; index++) {
2567 struct scrub_page *spage = sblock->pagev[index];
2568 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002569
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002570 ret = scrub_add_page_to_rd_bio(sctx, spage);
2571 if (ret) {
2572 scrub_block_put(sblock);
2573 return ret;
2574 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002575 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002576
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002577 if (force)
2578 scrub_submit(sctx);
2579 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002580
2581 /* last one frees, either here or in bio completion for last page */
2582 scrub_block_put(sblock);
2583 return 0;
2584}
2585
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002586static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002587{
2588 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002589 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002590
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002591 sbio->err = bio->bi_error;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002592 sbio->bio = bio;
2593
Qu Wenruo0339ef22014-02-28 10:46:17 +08002594 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002595}
2596
2597static void scrub_bio_end_io_worker(struct btrfs_work *work)
2598{
2599 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002600 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002601 int i;
2602
Stefan Behrensff023aa2012-11-06 11:43:11 +01002603 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002604 if (sbio->err) {
2605 for (i = 0; i < sbio->page_count; i++) {
2606 struct scrub_page *spage = sbio->pagev[i];
2607
2608 spage->io_error = 1;
2609 spage->sblock->no_io_error_seen = 0;
2610 }
2611 }
2612
2613 /* now complete the scrub_block items that have all pages completed */
2614 for (i = 0; i < sbio->page_count; i++) {
2615 struct scrub_page *spage = sbio->pagev[i];
2616 struct scrub_block *sblock = spage->sblock;
2617
2618 if (atomic_dec_and_test(&sblock->outstanding_pages))
2619 scrub_block_complete(sblock);
2620 scrub_block_put(sblock);
2621 }
2622
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002623 bio_put(sbio->bio);
2624 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002625 spin_lock(&sctx->list_lock);
2626 sbio->next_free = sctx->first_free;
2627 sctx->first_free = sbio->index;
2628 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002629
2630 if (sctx->is_dev_replace &&
2631 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2632 mutex_lock(&sctx->wr_ctx.wr_lock);
2633 scrub_wr_submit(sctx);
2634 mutex_unlock(&sctx->wr_ctx.wr_lock);
2635 }
2636
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002637 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002638}
2639
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002640static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2641 unsigned long *bitmap,
2642 u64 start, u64 len)
2643{
Liu Bo972d7212017-04-03 13:45:33 -07002644 u64 offset;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002645 int nsectors;
Jeff Mahoneyda170662016-06-15 09:22:56 -04002646 int sectorsize = sparity->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002647
2648 if (len >= sparity->stripe_len) {
2649 bitmap_set(bitmap, 0, sparity->nsectors);
2650 return;
2651 }
2652
2653 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002654 start = div64_u64_rem(start, sparity->stripe_len, &offset);
2655 offset = div_u64(offset, sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002656 nsectors = (int)len / sectorsize;
2657
2658 if (offset + nsectors <= sparity->nsectors) {
2659 bitmap_set(bitmap, offset, nsectors);
2660 return;
2661 }
2662
2663 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2664 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2665}
2666
2667static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2668 u64 start, u64 len)
2669{
2670 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2671}
2672
2673static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2674 u64 start, u64 len)
2675{
2676 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2677}
2678
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002679static void scrub_block_complete(struct scrub_block *sblock)
2680{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002681 int corrupted = 0;
2682
Stefan Behrensff023aa2012-11-06 11:43:11 +01002683 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002684 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002685 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002686 } else {
2687 /*
2688 * if has checksum error, write via repair mechanism in
2689 * dev replace case, otherwise write here in dev replace
2690 * case.
2691 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002692 corrupted = scrub_checksum(sblock);
2693 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002694 scrub_write_block_to_dev_replace(sblock);
2695 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002696
2697 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2698 u64 start = sblock->pagev[0]->logical;
2699 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2700 PAGE_SIZE;
2701
2702 scrub_parity_mark_sectors_error(sblock->sparity,
2703 start, end - start);
2704 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002705}
2706
Zhao Lei3b5753e2015-08-24 22:03:02 +08002707static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002708{
2709 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002710 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002711 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002712
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002713 while (!list_empty(&sctx->csum_list)) {
2714 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002715 struct btrfs_ordered_sum, list);
2716 if (sum->bytenr > logical)
2717 return 0;
2718 if (sum->bytenr + sum->len > logical)
2719 break;
2720
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002721 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002722 list_del(&sum->list);
2723 kfree(sum);
2724 sum = NULL;
2725 }
2726 if (!sum)
2727 return 0;
2728
Miao Xief51a4a12013-06-19 10:36:09 +08002729 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002730 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002731 memcpy(csum, sum->sums + index, sctx->csum_size);
2732 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002733 list_del(&sum->list);
2734 kfree(sum);
2735 }
Miao Xief51a4a12013-06-19 10:36:09 +08002736 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002737}
2738
2739/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002740static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002741 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002742 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002743{
2744 int ret;
2745 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002746 u32 blocksize;
2747
2748 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002749 blocksize = sctx->sectorsize;
2750 spin_lock(&sctx->stat_lock);
2751 sctx->stat.data_extents_scrubbed++;
2752 sctx->stat.data_bytes_scrubbed += len;
2753 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002754 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002755 blocksize = sctx->nodesize;
2756 spin_lock(&sctx->stat_lock);
2757 sctx->stat.tree_extents_scrubbed++;
2758 sctx->stat.tree_bytes_scrubbed += len;
2759 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002760 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002761 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002762 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002763 }
Arne Jansena2de7332011-03-08 14:14:00 +01002764
2765 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002766 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002767 int have_csum = 0;
2768
2769 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2770 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002771 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002772 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002773 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002774 if (sctx->is_dev_replace && !have_csum) {
2775 ret = copy_nocow_pages(sctx, logical, l,
2776 mirror_num,
2777 physical_for_dev_replace);
2778 goto behind_scrub_pages;
2779 }
Arne Jansena2de7332011-03-08 14:14:00 +01002780 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002781 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002782 mirror_num, have_csum ? csum : NULL, 0,
2783 physical_for_dev_replace);
2784behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002785 if (ret)
2786 return ret;
2787 len -= l;
2788 logical += l;
2789 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002790 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002791 }
2792 return 0;
2793}
2794
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002795static int scrub_pages_for_parity(struct scrub_parity *sparity,
2796 u64 logical, u64 len,
2797 u64 physical, struct btrfs_device *dev,
2798 u64 flags, u64 gen, int mirror_num, u8 *csum)
2799{
2800 struct scrub_ctx *sctx = sparity->sctx;
2801 struct scrub_block *sblock;
2802 int index;
2803
David Sterba58c4e172016-02-11 10:49:42 +01002804 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002805 if (!sblock) {
2806 spin_lock(&sctx->stat_lock);
2807 sctx->stat.malloc_errors++;
2808 spin_unlock(&sctx->stat_lock);
2809 return -ENOMEM;
2810 }
2811
2812 /* one ref inside this function, plus one for each page added to
2813 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002814 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002815 sblock->sctx = sctx;
2816 sblock->no_io_error_seen = 1;
2817 sblock->sparity = sparity;
2818 scrub_parity_get(sparity);
2819
2820 for (index = 0; len > 0; index++) {
2821 struct scrub_page *spage;
2822 u64 l = min_t(u64, len, PAGE_SIZE);
2823
David Sterba58c4e172016-02-11 10:49:42 +01002824 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002825 if (!spage) {
2826leave_nomem:
2827 spin_lock(&sctx->stat_lock);
2828 sctx->stat.malloc_errors++;
2829 spin_unlock(&sctx->stat_lock);
2830 scrub_block_put(sblock);
2831 return -ENOMEM;
2832 }
2833 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2834 /* For scrub block */
2835 scrub_page_get(spage);
2836 sblock->pagev[index] = spage;
2837 /* For scrub parity */
2838 scrub_page_get(spage);
2839 list_add_tail(&spage->list, &sparity->spages);
2840 spage->sblock = sblock;
2841 spage->dev = dev;
2842 spage->flags = flags;
2843 spage->generation = gen;
2844 spage->logical = logical;
2845 spage->physical = physical;
2846 spage->mirror_num = mirror_num;
2847 if (csum) {
2848 spage->have_csum = 1;
2849 memcpy(spage->csum, csum, sctx->csum_size);
2850 } else {
2851 spage->have_csum = 0;
2852 }
2853 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002854 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002855 if (!spage->page)
2856 goto leave_nomem;
2857 len -= l;
2858 logical += l;
2859 physical += l;
2860 }
2861
2862 WARN_ON(sblock->page_count == 0);
2863 for (index = 0; index < sblock->page_count; index++) {
2864 struct scrub_page *spage = sblock->pagev[index];
2865 int ret;
2866
2867 ret = scrub_add_page_to_rd_bio(sctx, spage);
2868 if (ret) {
2869 scrub_block_put(sblock);
2870 return ret;
2871 }
2872 }
2873
2874 /* last one frees, either here or in bio completion for last page */
2875 scrub_block_put(sblock);
2876 return 0;
2877}
2878
2879static int scrub_extent_for_parity(struct scrub_parity *sparity,
2880 u64 logical, u64 len,
2881 u64 physical, struct btrfs_device *dev,
2882 u64 flags, u64 gen, int mirror_num)
2883{
2884 struct scrub_ctx *sctx = sparity->sctx;
2885 int ret;
2886 u8 csum[BTRFS_CSUM_SIZE];
2887 u32 blocksize;
2888
Omar Sandoval4a770892015-06-19 11:52:52 -07002889 if (dev->missing) {
2890 scrub_parity_mark_sectors_error(sparity, logical, len);
2891 return 0;
2892 }
2893
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002894 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2895 blocksize = sctx->sectorsize;
2896 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2897 blocksize = sctx->nodesize;
2898 } else {
2899 blocksize = sctx->sectorsize;
2900 WARN_ON(1);
2901 }
2902
2903 while (len) {
2904 u64 l = min_t(u64, len, blocksize);
2905 int have_csum = 0;
2906
2907 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2908 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002909 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002910 if (have_csum == 0)
2911 goto skip;
2912 }
2913 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2914 flags, gen, mirror_num,
2915 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002916 if (ret)
2917 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002918skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002919 len -= l;
2920 logical += l;
2921 physical += l;
2922 }
2923 return 0;
2924}
2925
Wang Shilong3b080b22014-04-01 18:01:43 +08002926/*
2927 * Given a physical address, this will calculate it's
2928 * logical offset. if this is a parity stripe, it will return
2929 * the most left data stripe's logical offset.
2930 *
2931 * return 0 if it is a data stripe, 1 means parity stripe.
2932 */
2933static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002934 struct map_lookup *map, u64 *offset,
2935 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002936{
2937 int i;
2938 int j = 0;
2939 u64 stripe_nr;
2940 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002941 u32 stripe_index;
2942 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002943
2944 last_offset = (physical - map->stripes[num].physical) *
2945 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002946 if (stripe_start)
2947 *stripe_start = last_offset;
2948
Wang Shilong3b080b22014-04-01 18:01:43 +08002949 *offset = last_offset;
2950 for (i = 0; i < nr_data_stripes(map); i++) {
2951 *offset = last_offset + i * map->stripe_len;
2952
Liu Bo42c61ab2017-04-03 13:45:24 -07002953 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01002954 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002955
2956 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002957 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002958 /* calculate which stripe this data locates */
2959 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002960 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002961 if (stripe_index == num)
2962 return 0;
2963 if (stripe_index < num)
2964 j++;
2965 }
2966 *offset = last_offset + j * map->stripe_len;
2967 return 1;
2968}
2969
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002970static void scrub_free_parity(struct scrub_parity *sparity)
2971{
2972 struct scrub_ctx *sctx = sparity->sctx;
2973 struct scrub_page *curr, *next;
2974 int nbits;
2975
2976 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2977 if (nbits) {
2978 spin_lock(&sctx->stat_lock);
2979 sctx->stat.read_errors += nbits;
2980 sctx->stat.uncorrectable_errors += nbits;
2981 spin_unlock(&sctx->stat_lock);
2982 }
2983
2984 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2985 list_del_init(&curr->list);
2986 scrub_page_put(curr);
2987 }
2988
2989 kfree(sparity);
2990}
2991
Zhao Lei20b2e302015-06-04 20:09:15 +08002992static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2993{
2994 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2995 work);
2996 struct scrub_ctx *sctx = sparity->sctx;
2997
2998 scrub_free_parity(sparity);
2999 scrub_pending_bio_dec(sctx);
3000}
3001
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003002static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003003{
3004 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003005 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003006
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003007 if (bio->bi_error)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003008 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
3009 sparity->nsectors);
3010
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003011 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08003012
3013 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
3014 scrub_parity_bio_endio_worker, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003015 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003016}
3017
3018static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
3019{
3020 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003021 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003022 struct bio *bio;
3023 struct btrfs_raid_bio *rbio;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003024 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003025 u64 length;
3026 int ret;
3027
3028 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
3029 sparity->nsectors))
3030 goto out;
3031
Zhao Leia0dd59d2015-07-21 15:42:26 +08003032 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08003033
3034 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003035 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
David Sterba825ad4c2017-03-28 14:45:22 +02003036 &length, &bbio);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003037 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003038 goto bbio_out;
3039
3040 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
3041 if (!bio)
3042 goto bbio_out;
3043
3044 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
3045 bio->bi_private = sparity;
3046 bio->bi_end_io = scrub_parity_bio_endio;
3047
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003048 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003049 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003050 sparity->dbitmap,
3051 sparity->nsectors);
3052 if (!rbio)
3053 goto rbio_out;
3054
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003055 scrub_pending_bio_inc(sctx);
3056 raid56_parity_submit_scrub_rbio(rbio);
3057 return;
3058
3059rbio_out:
3060 bio_put(bio);
3061bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08003062 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08003063 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003064 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
3065 sparity->nsectors);
3066 spin_lock(&sctx->stat_lock);
3067 sctx->stat.malloc_errors++;
3068 spin_unlock(&sctx->stat_lock);
3069out:
3070 scrub_free_parity(sparity);
3071}
3072
3073static inline int scrub_calc_parity_bitmap_len(int nsectors)
3074{
Zhao Leibfca9a62014-12-08 19:55:57 +08003075 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003076}
3077
3078static void scrub_parity_get(struct scrub_parity *sparity)
3079{
Elena Reshetova78a76452017-03-03 10:55:24 +02003080 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003081}
3082
3083static void scrub_parity_put(struct scrub_parity *sparity)
3084{
Elena Reshetova78a76452017-03-03 10:55:24 +02003085 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003086 return;
3087
3088 scrub_parity_check_and_repair(sparity);
3089}
3090
3091static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
3092 struct map_lookup *map,
3093 struct btrfs_device *sdev,
3094 struct btrfs_path *path,
3095 u64 logic_start,
3096 u64 logic_end)
3097{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003098 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003099 struct btrfs_root *root = fs_info->extent_root;
3100 struct btrfs_root *csum_root = fs_info->csum_root;
3101 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07003102 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003103 u64 flags;
3104 int ret;
3105 int slot;
3106 struct extent_buffer *l;
3107 struct btrfs_key key;
3108 u64 generation;
3109 u64 extent_logical;
3110 u64 extent_physical;
3111 u64 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07003112 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003113 struct btrfs_device *extent_dev;
3114 struct scrub_parity *sparity;
3115 int nsectors;
3116 int bitmap_len;
3117 int extent_mirror_num;
3118 int stop_loop = 0;
3119
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003120 nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003121 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
3122 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
3123 GFP_NOFS);
3124 if (!sparity) {
3125 spin_lock(&sctx->stat_lock);
3126 sctx->stat.malloc_errors++;
3127 spin_unlock(&sctx->stat_lock);
3128 return -ENOMEM;
3129 }
3130
3131 sparity->stripe_len = map->stripe_len;
3132 sparity->nsectors = nsectors;
3133 sparity->sctx = sctx;
3134 sparity->scrub_dev = sdev;
3135 sparity->logic_start = logic_start;
3136 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02003137 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003138 INIT_LIST_HEAD(&sparity->spages);
3139 sparity->dbitmap = sparity->bitmap;
3140 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
3141
3142 ret = 0;
3143 while (logic_start < logic_end) {
3144 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3145 key.type = BTRFS_METADATA_ITEM_KEY;
3146 else
3147 key.type = BTRFS_EXTENT_ITEM_KEY;
3148 key.objectid = logic_start;
3149 key.offset = (u64)-1;
3150
3151 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3152 if (ret < 0)
3153 goto out;
3154
3155 if (ret > 0) {
3156 ret = btrfs_previous_extent_item(root, path, 0);
3157 if (ret < 0)
3158 goto out;
3159 if (ret > 0) {
3160 btrfs_release_path(path);
3161 ret = btrfs_search_slot(NULL, root, &key,
3162 path, 0, 0);
3163 if (ret < 0)
3164 goto out;
3165 }
3166 }
3167
3168 stop_loop = 0;
3169 while (1) {
3170 u64 bytes;
3171
3172 l = path->nodes[0];
3173 slot = path->slots[0];
3174 if (slot >= btrfs_header_nritems(l)) {
3175 ret = btrfs_next_leaf(root, path);
3176 if (ret == 0)
3177 continue;
3178 if (ret < 0)
3179 goto out;
3180
3181 stop_loop = 1;
3182 break;
3183 }
3184 btrfs_item_key_to_cpu(l, &key, slot);
3185
Zhao Leid7cad232015-07-22 13:14:48 +08003186 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3187 key.type != BTRFS_METADATA_ITEM_KEY)
3188 goto next;
3189
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003190 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003191 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003192 else
3193 bytes = key.offset;
3194
3195 if (key.objectid + bytes <= logic_start)
3196 goto next;
3197
Zhao Leia0dd59d2015-07-21 15:42:26 +08003198 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003199 stop_loop = 1;
3200 break;
3201 }
3202
3203 while (key.objectid >= logic_start + map->stripe_len)
3204 logic_start += map->stripe_len;
3205
3206 extent = btrfs_item_ptr(l, slot,
3207 struct btrfs_extent_item);
3208 flags = btrfs_extent_flags(l, extent);
3209 generation = btrfs_extent_generation(l, extent);
3210
Zhao Leia323e812015-07-23 12:29:49 +08003211 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3212 (key.objectid < logic_start ||
3213 key.objectid + bytes >
3214 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003215 btrfs_err(fs_info,
3216 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08003217 key.objectid, logic_start);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003218 spin_lock(&sctx->stat_lock);
3219 sctx->stat.uncorrectable_errors++;
3220 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003221 goto next;
3222 }
3223again:
3224 extent_logical = key.objectid;
3225 extent_len = bytes;
3226
3227 if (extent_logical < logic_start) {
3228 extent_len -= logic_start - extent_logical;
3229 extent_logical = logic_start;
3230 }
3231
3232 if (extent_logical + extent_len >
3233 logic_start + map->stripe_len)
3234 extent_len = logic_start + map->stripe_len -
3235 extent_logical;
3236
3237 scrub_parity_mark_sectors_data(sparity, extent_logical,
3238 extent_len);
3239
Omar Sandoval4a770892015-06-19 11:52:52 -07003240 mapped_length = extent_len;
Zhao Leif1fee652016-05-17 17:37:38 +08003241 bbio = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003242 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
3243 extent_logical, &mapped_length, &bbio,
3244 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07003245 if (!ret) {
3246 if (!bbio || mapped_length < extent_len)
3247 ret = -EIO;
3248 }
3249 if (ret) {
3250 btrfs_put_bbio(bbio);
3251 goto out;
3252 }
3253 extent_physical = bbio->stripes[0].physical;
3254 extent_mirror_num = bbio->mirror_num;
3255 extent_dev = bbio->stripes[0].dev;
3256 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003257
3258 ret = btrfs_lookup_csums_range(csum_root,
3259 extent_logical,
3260 extent_logical + extent_len - 1,
3261 &sctx->csum_list, 1);
3262 if (ret)
3263 goto out;
3264
3265 ret = scrub_extent_for_parity(sparity, extent_logical,
3266 extent_len,
3267 extent_physical,
3268 extent_dev, flags,
3269 generation,
3270 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003271
3272 scrub_free_csums(sctx);
3273
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003274 if (ret)
3275 goto out;
3276
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003277 if (extent_logical + extent_len <
3278 key.objectid + bytes) {
3279 logic_start += map->stripe_len;
3280
3281 if (logic_start >= logic_end) {
3282 stop_loop = 1;
3283 break;
3284 }
3285
3286 if (logic_start < key.objectid + bytes) {
3287 cond_resched();
3288 goto again;
3289 }
3290 }
3291next:
3292 path->slots[0]++;
3293 }
3294
3295 btrfs_release_path(path);
3296
3297 if (stop_loop)
3298 break;
3299
3300 logic_start += map->stripe_len;
3301 }
3302out:
3303 if (ret < 0)
3304 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003305 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003306 scrub_parity_put(sparity);
3307 scrub_submit(sctx);
3308 mutex_lock(&sctx->wr_ctx.wr_lock);
3309 scrub_wr_submit(sctx);
3310 mutex_unlock(&sctx->wr_ctx.wr_lock);
3311
3312 btrfs_release_path(path);
3313 return ret < 0 ? ret : 0;
3314}
3315
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003316static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003317 struct map_lookup *map,
3318 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003319 int num, u64 base, u64 length,
3320 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003321{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003322 struct btrfs_path *path, *ppath;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003323 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003324 struct btrfs_root *root = fs_info->extent_root;
3325 struct btrfs_root *csum_root = fs_info->csum_root;
3326 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003327 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003328 u64 flags;
3329 int ret;
3330 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003331 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003332 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003333 u64 physical;
3334 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003335 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003336 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003337 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003338 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003339 struct reada_control *reada1;
3340 struct reada_control *reada2;
David Sterbae6c11f92016-03-24 18:00:53 +01003341 struct btrfs_key key;
Arne Jansen7a262852011-06-10 12:39:23 +02003342 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003343 u64 increment = map->stripe_len;
3344 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003345 u64 extent_logical;
3346 u64 extent_physical;
3347 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003348 u64 stripe_logical;
3349 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003350 struct btrfs_device *extent_dev;
3351 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003352 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003353
Wang Shilong3b080b22014-04-01 18:01:43 +08003354 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003355 offset = 0;
Liu Bo42c61ab2017-04-03 13:45:24 -07003356 nstripes = div64_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003357 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3358 offset = map->stripe_len * num;
3359 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003360 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003361 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3362 int factor = map->num_stripes / map->sub_stripes;
3363 offset = map->stripe_len * (num / map->sub_stripes);
3364 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003365 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003366 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3367 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003368 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003369 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3370 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003371 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003372 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003373 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003374 increment = map->stripe_len * nr_data_stripes(map);
3375 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003376 } else {
3377 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003378 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003379 }
3380
3381 path = btrfs_alloc_path();
3382 if (!path)
3383 return -ENOMEM;
3384
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003385 ppath = btrfs_alloc_path();
3386 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003387 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003388 return -ENOMEM;
3389 }
3390
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003391 /*
3392 * work on commit root. The related disk blocks are static as
3393 * long as COW is applied. This means, it is save to rewrite
3394 * them to repair disk errors without any race conditions
3395 */
Arne Jansena2de7332011-03-08 14:14:00 +01003396 path->search_commit_root = 1;
3397 path->skip_locking = 1;
3398
Gui Hecheng063c54d2015-01-09 09:39:40 +08003399 ppath->search_commit_root = 1;
3400 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003401 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003402 * trigger the readahead for extent tree csum tree and wait for
3403 * completion. During readahead, the scrub is officially paused
3404 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003405 */
3406 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003407 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003408 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003409 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003410 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003411 logic_end += base;
3412 } else {
3413 logic_end = logical + increment * nstripes;
3414 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003415 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003416 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003417 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003418
Arne Jansen7a262852011-06-10 12:39:23 +02003419 /* FIXME it might be better to start readahead at commit root */
David Sterbae6c11f92016-03-24 18:00:53 +01003420 key.objectid = logical;
3421 key.type = BTRFS_EXTENT_ITEM_KEY;
3422 key.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003423 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003424 key_end.type = BTRFS_METADATA_ITEM_KEY;
3425 key_end.offset = (u64)-1;
David Sterbae6c11f92016-03-24 18:00:53 +01003426 reada1 = btrfs_reada_add(root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003427
David Sterbae6c11f92016-03-24 18:00:53 +01003428 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3429 key.type = BTRFS_EXTENT_CSUM_KEY;
3430 key.offset = logical;
Arne Jansen7a262852011-06-10 12:39:23 +02003431 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3432 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003433 key_end.offset = logic_end;
David Sterbae6c11f92016-03-24 18:00:53 +01003434 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003435
Arne Jansen7a262852011-06-10 12:39:23 +02003436 if (!IS_ERR(reada1))
3437 btrfs_reada_wait(reada1);
3438 if (!IS_ERR(reada2))
3439 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003440
Arne Jansena2de7332011-03-08 14:14:00 +01003441
3442 /*
3443 * collect all data csums for the stripe to avoid seeking during
3444 * the scrub. This might currently (crc32) end up to be about 1MB
3445 */
Arne Jansene7786c32011-05-28 20:58:38 +00003446 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003447
Arne Jansena2de7332011-03-08 14:14:00 +01003448 /*
3449 * now find all extents for each stripe and scrub them
3450 */
Arne Jansena2de7332011-03-08 14:14:00 +01003451 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003452 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003453 /*
3454 * canceled?
3455 */
3456 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003457 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003458 ret = -ECANCELED;
3459 goto out;
3460 }
3461 /*
3462 * check to see if we have to pause
3463 */
3464 if (atomic_read(&fs_info->scrub_pause_req)) {
3465 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003466 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003467 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003468 mutex_lock(&sctx->wr_ctx.wr_lock);
3469 scrub_wr_submit(sctx);
3470 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003471 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003472 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003473 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003474 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003475 }
3476
Zhao Leif2f66a22015-07-21 12:22:29 +08003477 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3478 ret = get_raid56_logic_offset(physical, num, map,
3479 &logical,
3480 &stripe_logical);
3481 logical += base;
3482 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003483 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003484 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003485 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003486 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3487 ppath, stripe_logical,
3488 stripe_end);
3489 if (ret)
3490 goto out;
3491 goto skip;
3492 }
3493 }
3494
Wang Shilong7c76edb2014-01-12 21:38:32 +08003495 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3496 key.type = BTRFS_METADATA_ITEM_KEY;
3497 else
3498 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003499 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003500 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003501
3502 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3503 if (ret < 0)
3504 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003505
Arne Jansen8c510322011-06-03 10:09:26 +02003506 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003507 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003508 if (ret < 0)
3509 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003510 if (ret > 0) {
3511 /* there's no smaller item, so stick with the
3512 * larger one */
3513 btrfs_release_path(path);
3514 ret = btrfs_search_slot(NULL, root, &key,
3515 path, 0, 0);
3516 if (ret < 0)
3517 goto out;
3518 }
Arne Jansena2de7332011-03-08 14:14:00 +01003519 }
3520
Liu Bo625f1c8d2013-04-27 02:56:57 +00003521 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003522 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003523 u64 bytes;
3524
Arne Jansena2de7332011-03-08 14:14:00 +01003525 l = path->nodes[0];
3526 slot = path->slots[0];
3527 if (slot >= btrfs_header_nritems(l)) {
3528 ret = btrfs_next_leaf(root, path);
3529 if (ret == 0)
3530 continue;
3531 if (ret < 0)
3532 goto out;
3533
Liu Bo625f1c8d2013-04-27 02:56:57 +00003534 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003535 break;
3536 }
3537 btrfs_item_key_to_cpu(l, &key, slot);
3538
Zhao Leid7cad232015-07-22 13:14:48 +08003539 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3540 key.type != BTRFS_METADATA_ITEM_KEY)
3541 goto next;
3542
Josef Bacik3173a182013-03-07 14:22:04 -05003543 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003544 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003545 else
3546 bytes = key.offset;
3547
3548 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003549 goto next;
3550
Liu Bo625f1c8d2013-04-27 02:56:57 +00003551 if (key.objectid >= logical + map->stripe_len) {
3552 /* out of this device extent */
3553 if (key.objectid >= logic_end)
3554 stop_loop = 1;
3555 break;
3556 }
Arne Jansena2de7332011-03-08 14:14:00 +01003557
3558 extent = btrfs_item_ptr(l, slot,
3559 struct btrfs_extent_item);
3560 flags = btrfs_extent_flags(l, extent);
3561 generation = btrfs_extent_generation(l, extent);
3562
Zhao Leia323e812015-07-23 12:29:49 +08003563 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3564 (key.objectid < logical ||
3565 key.objectid + bytes >
3566 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003567 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003568 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003569 key.objectid, logical);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003570 spin_lock(&sctx->stat_lock);
3571 sctx->stat.uncorrectable_errors++;
3572 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003573 goto next;
3574 }
3575
Liu Bo625f1c8d2013-04-27 02:56:57 +00003576again:
3577 extent_logical = key.objectid;
3578 extent_len = bytes;
3579
Arne Jansena2de7332011-03-08 14:14:00 +01003580 /*
3581 * trim extent to this stripe
3582 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003583 if (extent_logical < logical) {
3584 extent_len -= logical - extent_logical;
3585 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003586 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003587 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003588 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003589 extent_len = logical + map->stripe_len -
3590 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003591 }
3592
Liu Bo625f1c8d2013-04-27 02:56:57 +00003593 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003594 extent_dev = scrub_dev;
3595 extent_mirror_num = mirror_num;
3596 if (is_dev_replace)
3597 scrub_remap_extent(fs_info, extent_logical,
3598 extent_len, &extent_physical,
3599 &extent_dev,
3600 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003601
Zhao Leife8cf652015-07-22 13:14:47 +08003602 ret = btrfs_lookup_csums_range(csum_root,
3603 extent_logical,
3604 extent_logical +
3605 extent_len - 1,
3606 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003607 if (ret)
3608 goto out;
3609
Liu Bo625f1c8d2013-04-27 02:56:57 +00003610 ret = scrub_extent(sctx, extent_logical, extent_len,
3611 extent_physical, extent_dev, flags,
3612 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003613 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003614
3615 scrub_free_csums(sctx);
3616
Liu Bo625f1c8d2013-04-27 02:56:57 +00003617 if (ret)
3618 goto out;
3619
3620 if (extent_logical + extent_len <
3621 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003622 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003623 /*
3624 * loop until we find next data stripe
3625 * or we have finished all stripes.
3626 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003627loop:
3628 physical += map->stripe_len;
3629 ret = get_raid56_logic_offset(physical,
3630 num, map, &logical,
3631 &stripe_logical);
3632 logical += base;
3633
3634 if (ret && physical < physical_end) {
3635 stripe_logical += base;
3636 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003637 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003638 ret = scrub_raid56_parity(sctx,
3639 map, scrub_dev, ppath,
3640 stripe_logical,
3641 stripe_end);
3642 if (ret)
3643 goto out;
3644 goto loop;
3645 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003646 } else {
3647 physical += map->stripe_len;
3648 logical += increment;
3649 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003650 if (logical < key.objectid + bytes) {
3651 cond_resched();
3652 goto again;
3653 }
3654
Wang Shilong3b080b22014-04-01 18:01:43 +08003655 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003656 stop_loop = 1;
3657 break;
3658 }
3659 }
Arne Jansena2de7332011-03-08 14:14:00 +01003660next:
3661 path->slots[0]++;
3662 }
Chris Mason71267332011-05-23 06:30:52 -04003663 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003664skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003665 logical += increment;
3666 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003667 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003668 if (stop_loop)
3669 sctx->stat.last_physical = map->stripes[num].physical +
3670 length;
3671 else
3672 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003673 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003674 if (stop_loop)
3675 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003676 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003677out:
Arne Jansena2de7332011-03-08 14:14:00 +01003678 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003679 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003680 mutex_lock(&sctx->wr_ctx.wr_lock);
3681 scrub_wr_submit(sctx);
3682 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003683
Arne Jansene7786c32011-05-28 20:58:38 +00003684 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003685 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003686 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003687 return ret < 0 ? ret : 0;
3688}
3689
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003690static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003691 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003692 u64 chunk_offset, u64 length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003693 u64 dev_offset,
3694 struct btrfs_block_group_cache *cache,
3695 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003696{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003697 struct btrfs_fs_info *fs_info = sctx->fs_info;
3698 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003699 struct map_lookup *map;
3700 struct extent_map *em;
3701 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003702 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003703
3704 read_lock(&map_tree->map_tree.lock);
3705 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3706 read_unlock(&map_tree->map_tree.lock);
3707
Filipe Manana020d5b72015-11-19 10:57:20 +00003708 if (!em) {
3709 /*
3710 * Might have been an unused block group deleted by the cleaner
3711 * kthread or relocation.
3712 */
3713 spin_lock(&cache->lock);
3714 if (!cache->removed)
3715 ret = -EINVAL;
3716 spin_unlock(&cache->lock);
3717
3718 return ret;
3719 }
Arne Jansena2de7332011-03-08 14:14:00 +01003720
Jeff Mahoney95617d62015-06-03 10:55:48 -04003721 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003722 if (em->start != chunk_offset)
3723 goto out;
3724
3725 if (em->len < length)
3726 goto out;
3727
3728 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003729 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003730 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003731 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003732 chunk_offset, length,
3733 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003734 if (ret)
3735 goto out;
3736 }
3737 }
3738out:
3739 free_extent_map(em);
3740
3741 return ret;
3742}
3743
3744static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003745int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003746 struct btrfs_device *scrub_dev, u64 start, u64 end,
3747 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003748{
3749 struct btrfs_dev_extent *dev_extent = NULL;
3750 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003751 struct btrfs_fs_info *fs_info = sctx->fs_info;
3752 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003753 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003754 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003755 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003756 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003757 int slot;
3758 struct extent_buffer *l;
3759 struct btrfs_key key;
3760 struct btrfs_key found_key;
3761 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003762 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003763
3764 path = btrfs_alloc_path();
3765 if (!path)
3766 return -ENOMEM;
3767
David Sterbae4058b52015-11-27 16:31:35 +01003768 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003769 path->search_commit_root = 1;
3770 path->skip_locking = 1;
3771
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003772 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003773 key.offset = 0ull;
3774 key.type = BTRFS_DEV_EXTENT_KEY;
3775
Arne Jansena2de7332011-03-08 14:14:00 +01003776 while (1) {
3777 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3778 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003779 break;
3780 if (ret > 0) {
3781 if (path->slots[0] >=
3782 btrfs_header_nritems(path->nodes[0])) {
3783 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003784 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003785 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003786 if (ret > 0) {
3787 ret = 0;
3788 break;
3789 }
3790 } else {
3791 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003792 }
3793 }
Arne Jansena2de7332011-03-08 14:14:00 +01003794
3795 l = path->nodes[0];
3796 slot = path->slots[0];
3797
3798 btrfs_item_key_to_cpu(l, &found_key, slot);
3799
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003800 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003801 break;
3802
David Sterba962a2982014-06-04 18:41:45 +02003803 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003804 break;
3805
3806 if (found_key.offset >= end)
3807 break;
3808
3809 if (found_key.offset < key.offset)
3810 break;
3811
3812 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3813 length = btrfs_dev_extent_length(l, dev_extent);
3814
Qu Wenruoced96ed2014-06-19 10:42:51 +08003815 if (found_key.offset + length <= start)
3816 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003817
Arne Jansena2de7332011-03-08 14:14:00 +01003818 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3819
3820 /*
3821 * get a reference on the corresponding block group to prevent
3822 * the chunk from going away while we scrub it
3823 */
3824 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003825
3826 /* some chunks are removed but not committed to disk yet,
3827 * continue scrubbing */
3828 if (!cache)
3829 goto skip;
3830
Zhaolei55e3a602015-08-05 16:43:30 +08003831 /*
3832 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3833 * to avoid deadlock caused by:
3834 * btrfs_inc_block_group_ro()
3835 * -> btrfs_wait_for_commit()
3836 * -> btrfs_commit_transaction()
3837 * -> btrfs_scrub_pause()
3838 */
3839 scrub_pause_on(fs_info);
Jeff Mahoney5e00f192017-02-15 16:28:29 -05003840 ret = btrfs_inc_block_group_ro(fs_info, cache);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003841 if (!ret && is_dev_replace) {
3842 /*
3843 * If we are doing a device replace wait for any tasks
3844 * that started dellaloc right before we set the block
3845 * group to RO mode, as they might have just allocated
3846 * an extent from it or decided they could do a nocow
3847 * write. And if any such tasks did that, wait for their
3848 * ordered extents to complete and then commit the
3849 * current transaction, so that we can later see the new
3850 * extent items in the extent tree - the ordered extents
3851 * create delayed data references (for cow writes) when
3852 * they complete, which will be run and insert the
3853 * corresponding extent items into the extent tree when
3854 * we commit the transaction they used when running
3855 * inode.c:btrfs_finish_ordered_io(). We later use
3856 * the commit root of the extent tree to find extents
3857 * to copy from the srcdev into the tgtdev, and we don't
3858 * want to miss any new extents.
3859 */
3860 btrfs_wait_block_group_reservations(cache);
3861 btrfs_wait_nocow_writers(cache);
3862 ret = btrfs_wait_ordered_roots(fs_info, -1,
3863 cache->key.objectid,
3864 cache->key.offset);
3865 if (ret > 0) {
3866 struct btrfs_trans_handle *trans;
3867
3868 trans = btrfs_join_transaction(root);
3869 if (IS_ERR(trans))
3870 ret = PTR_ERR(trans);
3871 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003872 ret = btrfs_commit_transaction(trans);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003873 if (ret) {
3874 scrub_pause_off(fs_info);
3875 btrfs_put_block_group(cache);
3876 break;
3877 }
3878 }
3879 }
Zhaolei55e3a602015-08-05 16:43:30 +08003880 scrub_pause_off(fs_info);
Zhaolei76a8efa2015-11-17 18:46:17 +08003881
3882 if (ret == 0) {
3883 ro_set = 1;
3884 } else if (ret == -ENOSPC) {
3885 /*
3886 * btrfs_inc_block_group_ro return -ENOSPC when it
3887 * failed in creating new chunk for metadata.
3888 * It is not a problem for scrub/replace, because
3889 * metadata are always cowed, and our scrub paused
3890 * commit_transactions.
3891 */
3892 ro_set = 0;
3893 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003894 btrfs_warn(fs_info,
3895 "failed setting block group ro, ret=%d\n",
Zhaolei76a8efa2015-11-17 18:46:17 +08003896 ret);
Zhaolei55e3a602015-08-05 16:43:30 +08003897 btrfs_put_block_group(cache);
3898 break;
3899 }
3900
Filipe Manana81e87a72016-05-14 16:32:35 +01003901 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003902 dev_replace->cursor_right = found_key.offset + length;
3903 dev_replace->cursor_left = found_key.offset;
3904 dev_replace->item_needs_writeback = 1;
Filipe Manana81e87a72016-05-14 16:32:35 +01003905 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
Zhao Lei8c204c92015-08-19 15:02:40 +08003906 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003907 found_key.offset, cache, is_dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003908
3909 /*
3910 * flush, submit all pending read and write bios, afterwards
3911 * wait for them.
3912 * Note that in the dev replace case, a read request causes
3913 * write requests that are submitted in the read completion
3914 * worker. Therefore in the current situation, it is required
3915 * that all write requests are flushed, so that all read and
3916 * write requests are really completed when bios_in_flight
3917 * changes to 0.
3918 */
3919 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3920 scrub_submit(sctx);
3921 mutex_lock(&sctx->wr_ctx.wr_lock);
3922 scrub_wr_submit(sctx);
3923 mutex_unlock(&sctx->wr_ctx.wr_lock);
3924
3925 wait_event(sctx->list_wait,
3926 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003927
3928 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003929
3930 /*
3931 * must be called before we decrease @scrub_paused.
3932 * make sure we don't block transaction commit while
3933 * we are waiting pending workers finished.
3934 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003935 wait_event(sctx->list_wait,
3936 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003937 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3938
Zhaoleib708ce92015-08-05 16:43:29 +08003939 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003940
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003941 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
3942 dev_replace->cursor_left = dev_replace->cursor_right;
3943 dev_replace->item_needs_writeback = 1;
3944 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
3945
Zhaolei76a8efa2015-11-17 18:46:17 +08003946 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003947 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003948
Filipe Manana758f2df2015-11-19 11:45:48 +00003949 /*
3950 * We might have prevented the cleaner kthread from deleting
3951 * this block group if it was already unused because we raced
3952 * and set it to RO mode first. So add it back to the unused
3953 * list, otherwise it might not ever be deleted unless a manual
3954 * balance is triggered or it becomes used and unused again.
3955 */
3956 spin_lock(&cache->lock);
3957 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3958 btrfs_block_group_used(&cache->item) == 0) {
3959 spin_unlock(&cache->lock);
3960 spin_lock(&fs_info->unused_bgs_lock);
3961 if (list_empty(&cache->bg_list)) {
3962 btrfs_get_block_group(cache);
3963 list_add_tail(&cache->bg_list,
3964 &fs_info->unused_bgs);
3965 }
3966 spin_unlock(&fs_info->unused_bgs_lock);
3967 } else {
3968 spin_unlock(&cache->lock);
3969 }
3970
Arne Jansena2de7332011-03-08 14:14:00 +01003971 btrfs_put_block_group(cache);
3972 if (ret)
3973 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003974 if (is_dev_replace &&
3975 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003976 ret = -EIO;
3977 break;
3978 }
3979 if (sctx->stat.malloc_errors > 0) {
3980 ret = -ENOMEM;
3981 break;
3982 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003983skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003984 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003985 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003986 }
3987
Arne Jansena2de7332011-03-08 14:14:00 +01003988 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003989
Zhaolei55e3a602015-08-05 16:43:30 +08003990 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003991}
3992
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003993static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3994 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003995{
3996 int i;
3997 u64 bytenr;
3998 u64 gen;
3999 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004000 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01004001
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004002 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004003 return -EIO;
4004
Miao Xie5f546062014-07-24 11:37:09 +08004005 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004006 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08004007 gen = scrub_dev->generation;
4008 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004009 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01004010
4011 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
4012 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08004013 if (bytenr + BTRFS_SUPER_INFO_SIZE >
4014 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01004015 break;
4016
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004017 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01004018 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004019 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01004020 if (ret)
4021 return ret;
4022 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004023 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004024
4025 return 0;
4026}
4027
4028/*
4029 * get a reference count on fs_info->scrub_workers. start worker if necessary
4030 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01004031static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
4032 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004033{
David Sterba6f011052015-02-16 18:34:01 +01004034 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08004035 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01004036
Arne Jansen632dd772011-06-10 12:07:07 +02004037 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004038 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08004039 fs_info->scrub_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004040 btrfs_alloc_workqueue(fs_info, "scrub", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004041 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004042 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08004043 fs_info->scrub_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004044 btrfs_alloc_workqueue(fs_info, "scrub", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004045 max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08004046 if (!fs_info->scrub_workers)
4047 goto fail_scrub_workers;
4048
Qu Wenruo0339ef22014-02-28 10:46:17 +08004049 fs_info->scrub_wr_completion_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004050 btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004051 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004052 if (!fs_info->scrub_wr_completion_workers)
4053 goto fail_scrub_wr_completion_workers;
4054
Qu Wenruo0339ef22014-02-28 10:46:17 +08004055 fs_info->scrub_nocow_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004056 btrfs_alloc_workqueue(fs_info, "scrubnc", flags, 1, 0);
Zhao Leie82afc52015-06-12 20:36:58 +08004057 if (!fs_info->scrub_nocow_workers)
4058 goto fail_scrub_nocow_workers;
Zhao Lei20b2e302015-06-04 20:09:15 +08004059 fs_info->scrub_parity_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004060 btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
Zhao Lei20b2e302015-06-04 20:09:15 +08004061 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004062 if (!fs_info->scrub_parity_workers)
4063 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02004064 }
Arne Jansena2de7332011-03-08 14:14:00 +01004065 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08004066 return 0;
4067
4068fail_scrub_parity_workers:
4069 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
4070fail_scrub_nocow_workers:
4071 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4072fail_scrub_wr_completion_workers:
4073 btrfs_destroy_workqueue(fs_info->scrub_workers);
4074fail_scrub_workers:
4075 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01004076}
4077
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004078static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004079{
Stefan Behrensff023aa2012-11-06 11:43:11 +01004080 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08004081 btrfs_destroy_workqueue(fs_info->scrub_workers);
4082 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4083 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08004084 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004085 }
Arne Jansena2de7332011-03-08 14:14:00 +01004086 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004087}
4088
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004089int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
4090 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004091 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004092{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004093 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004094 int ret;
4095 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08004096 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01004097
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004098 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01004099 return -EINVAL;
4100
Jeff Mahoneyda170662016-06-15 09:22:56 -04004101 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004102 /*
4103 * in this case scrub is unable to calculate the checksum
4104 * the way scrub is implemented. Do not handle this
4105 * situation at all because it won't ever happen.
4106 */
Frank Holtonefe120a2013-12-20 11:37:06 -05004107 btrfs_err(fs_info,
4108 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004109 fs_info->nodesize,
4110 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004111 return -EINVAL;
4112 }
4113
Jeff Mahoneyda170662016-06-15 09:22:56 -04004114 if (fs_info->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004115 /* not supported for data w/o checksums */
Chandan Rajendra751bebbe2016-07-04 10:04:39 +05304116 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004117 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004118 fs_info->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01004119 return -EINVAL;
4120 }
4121
Jeff Mahoneyda170662016-06-15 09:22:56 -04004122 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004123 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04004124 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004125 /*
4126 * would exhaust the array bounds of pagev member in
4127 * struct scrub_block
4128 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004129 btrfs_err(fs_info,
4130 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004131 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004132 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004133 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004134 SCRUB_MAX_PAGES_PER_BLOCK);
4135 return -EINVAL;
4136 }
4137
Arne Jansena2de7332011-03-08 14:14:00 +01004138
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004139 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4140 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01004141 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004142 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004143 return -ENODEV;
4144 }
Arne Jansena2de7332011-03-08 14:14:00 +01004145
Miao Xie5d68da32014-07-24 11:37:07 +08004146 if (!is_dev_replace && !readonly && !dev->writeable) {
4147 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4148 rcu_read_lock();
4149 name = rcu_dereference(dev->name);
4150 btrfs_err(fs_info, "scrub: device %s is not writable",
4151 name->str);
4152 rcu_read_unlock();
4153 return -EROFS;
4154 }
4155
Wang Shilong3b7a0162013-10-12 02:11:12 +08004156 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01004157 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01004158 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004159 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004160 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01004161 }
4162
Liu Bo73beece2015-07-17 16:49:19 +08004163 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01004164 if (dev->scrub_device ||
4165 (!is_dev_replace &&
4166 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
Liu Bo73beece2015-07-17 16:49:19 +08004167 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004168 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004169 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004170 return -EINPROGRESS;
4171 }
Liu Bo73beece2015-07-17 16:49:19 +08004172 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Wang Shilong3b7a0162013-10-12 02:11:12 +08004173
4174 ret = scrub_workers_get(fs_info, is_dev_replace);
4175 if (ret) {
4176 mutex_unlock(&fs_info->scrub_lock);
4177 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4178 return ret;
4179 }
4180
Stefan Behrens63a212a2012-11-05 18:29:28 +01004181 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004182 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004183 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004184 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4185 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004186 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004187 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004188 sctx->readonly = readonly;
4189 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08004190 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004191
Wang Shilong3cb09292013-12-04 21:15:19 +08004192 /*
4193 * checking @scrub_pause_req here, we can avoid
4194 * race between committing transaction and scrubbing.
4195 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08004196 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004197 atomic_inc(&fs_info->scrubs_running);
4198 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004199
Stefan Behrensff023aa2012-11-06 11:43:11 +01004200 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08004201 /*
4202 * by holding device list mutex, we can
4203 * kick off writing super in log tree sync.
4204 */
Wang Shilong3cb09292013-12-04 21:15:19 +08004205 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004206 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08004207 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004208 }
Arne Jansena2de7332011-03-08 14:14:00 +01004209
4210 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004211 ret = scrub_enumerate_chunks(sctx, dev, start, end,
4212 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01004213
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004214 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004215 atomic_dec(&fs_info->scrubs_running);
4216 wake_up(&fs_info->scrub_pause_wait);
4217
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004218 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02004219
Arne Jansena2de7332011-03-08 14:14:00 +01004220 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004221 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01004222
4223 mutex_lock(&fs_info->scrub_lock);
4224 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08004225 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004226 mutex_unlock(&fs_info->scrub_lock);
4227
Filipe Mananaf55985f2015-02-09 21:14:24 +00004228 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004229
4230 return ret;
4231}
4232
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004233void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004234{
Arne Jansena2de7332011-03-08 14:14:00 +01004235 mutex_lock(&fs_info->scrub_lock);
4236 atomic_inc(&fs_info->scrub_pause_req);
4237 while (atomic_read(&fs_info->scrubs_paused) !=
4238 atomic_read(&fs_info->scrubs_running)) {
4239 mutex_unlock(&fs_info->scrub_lock);
4240 wait_event(fs_info->scrub_pause_wait,
4241 atomic_read(&fs_info->scrubs_paused) ==
4242 atomic_read(&fs_info->scrubs_running));
4243 mutex_lock(&fs_info->scrub_lock);
4244 }
4245 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004246}
4247
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004248void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004249{
Arne Jansena2de7332011-03-08 14:14:00 +01004250 atomic_dec(&fs_info->scrub_pause_req);
4251 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01004252}
4253
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004254int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004255{
Arne Jansena2de7332011-03-08 14:14:00 +01004256 mutex_lock(&fs_info->scrub_lock);
4257 if (!atomic_read(&fs_info->scrubs_running)) {
4258 mutex_unlock(&fs_info->scrub_lock);
4259 return -ENOTCONN;
4260 }
4261
4262 atomic_inc(&fs_info->scrub_cancel_req);
4263 while (atomic_read(&fs_info->scrubs_running)) {
4264 mutex_unlock(&fs_info->scrub_lock);
4265 wait_event(fs_info->scrub_pause_wait,
4266 atomic_read(&fs_info->scrubs_running) == 0);
4267 mutex_lock(&fs_info->scrub_lock);
4268 }
4269 atomic_dec(&fs_info->scrub_cancel_req);
4270 mutex_unlock(&fs_info->scrub_lock);
4271
4272 return 0;
4273}
4274
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004275int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
4276 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004277{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004278 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004279
4280 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004281 sctx = dev->scrub_device;
4282 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004283 mutex_unlock(&fs_info->scrub_lock);
4284 return -ENOTCONN;
4285 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004286 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01004287 while (dev->scrub_device) {
4288 mutex_unlock(&fs_info->scrub_lock);
4289 wait_event(fs_info->scrub_pause_wait,
4290 dev->scrub_device == NULL);
4291 mutex_lock(&fs_info->scrub_lock);
4292 }
4293 mutex_unlock(&fs_info->scrub_lock);
4294
4295 return 0;
4296}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004297
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004298int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01004299 struct btrfs_scrub_progress *progress)
4300{
4301 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004302 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004303
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004304 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4305 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004306 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004307 sctx = dev->scrub_device;
4308 if (sctx)
4309 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004310 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004311
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004312 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004313}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004314
4315static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4316 u64 extent_logical, u64 extent_len,
4317 u64 *extent_physical,
4318 struct btrfs_device **extent_dev,
4319 int *extent_mirror_num)
4320{
4321 u64 mapped_length;
4322 struct btrfs_bio *bbio = NULL;
4323 int ret;
4324
4325 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004326 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004327 &mapped_length, &bbio, 0);
4328 if (ret || !bbio || mapped_length < extent_len ||
4329 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08004330 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004331 return;
4332 }
4333
4334 *extent_physical = bbio->stripes[0].physical;
4335 *extent_mirror_num = bbio->mirror_num;
4336 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004337 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004338}
4339
David Sterbae5987e12017-02-10 19:55:54 +01004340static int scrub_setup_wr_ctx(struct scrub_wr_ctx *wr_ctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004341 struct btrfs_device *dev,
4342 int is_dev_replace)
4343{
4344 WARN_ON(wr_ctx->wr_curr_bio != NULL);
4345
4346 mutex_init(&wr_ctx->wr_lock);
4347 wr_ctx->wr_curr_bio = NULL;
4348 if (!is_dev_replace)
4349 return 0;
4350
4351 WARN_ON(!dev->bdev);
Kent Overstreetb54ffb72015-05-19 14:31:01 +02004352 wr_ctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004353 wr_ctx->tgtdev = dev;
4354 atomic_set(&wr_ctx->flush_all_writes, 0);
4355 return 0;
4356}
4357
4358static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
4359{
4360 mutex_lock(&wr_ctx->wr_lock);
4361 kfree(wr_ctx->wr_curr_bio);
4362 wr_ctx->wr_curr_bio = NULL;
4363 mutex_unlock(&wr_ctx->wr_lock);
4364}
4365
4366static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
4367 int mirror_num, u64 physical_for_dev_replace)
4368{
4369 struct scrub_copy_nocow_ctx *nocow_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004370 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004371
4372 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
4373 if (!nocow_ctx) {
4374 spin_lock(&sctx->stat_lock);
4375 sctx->stat.malloc_errors++;
4376 spin_unlock(&sctx->stat_lock);
4377 return -ENOMEM;
4378 }
4379
4380 scrub_pending_trans_workers_inc(sctx);
4381
4382 nocow_ctx->sctx = sctx;
4383 nocow_ctx->logical = logical;
4384 nocow_ctx->len = len;
4385 nocow_ctx->mirror_num = mirror_num;
4386 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08004387 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
4388 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04004389 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08004390 btrfs_queue_work(fs_info->scrub_nocow_workers,
4391 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004392
4393 return 0;
4394}
4395
Josef Bacik652f25a2013-09-12 16:58:28 -04004396static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
4397{
4398 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
4399 struct scrub_nocow_inode *nocow_inode;
4400
4401 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
4402 if (!nocow_inode)
4403 return -ENOMEM;
4404 nocow_inode->inum = inum;
4405 nocow_inode->offset = offset;
4406 nocow_inode->root = root;
4407 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
4408 return 0;
4409}
4410
4411#define COPY_COMPLETE 1
4412
Stefan Behrensff023aa2012-11-06 11:43:11 +01004413static void copy_nocow_pages_worker(struct btrfs_work *work)
4414{
4415 struct scrub_copy_nocow_ctx *nocow_ctx =
4416 container_of(work, struct scrub_copy_nocow_ctx, work);
4417 struct scrub_ctx *sctx = nocow_ctx->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004418 struct btrfs_fs_info *fs_info = sctx->fs_info;
4419 struct btrfs_root *root = fs_info->extent_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004420 u64 logical = nocow_ctx->logical;
4421 u64 len = nocow_ctx->len;
4422 int mirror_num = nocow_ctx->mirror_num;
4423 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4424 int ret;
4425 struct btrfs_trans_handle *trans = NULL;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004426 struct btrfs_path *path;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004427 int not_written = 0;
4428
Stefan Behrensff023aa2012-11-06 11:43:11 +01004429 path = btrfs_alloc_path();
4430 if (!path) {
4431 spin_lock(&sctx->stat_lock);
4432 sctx->stat.malloc_errors++;
4433 spin_unlock(&sctx->stat_lock);
4434 not_written = 1;
4435 goto out;
4436 }
4437
4438 trans = btrfs_join_transaction(root);
4439 if (IS_ERR(trans)) {
4440 not_written = 1;
4441 goto out;
4442 }
4443
4444 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04004445 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004446 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004447 btrfs_warn(fs_info,
4448 "iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d",
4449 logical, physical_for_dev_replace, len, mirror_num,
4450 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004451 not_written = 1;
4452 goto out;
4453 }
4454
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004455 btrfs_end_transaction(trans);
Josef Bacik652f25a2013-09-12 16:58:28 -04004456 trans = NULL;
4457 while (!list_empty(&nocow_ctx->inodes)) {
4458 struct scrub_nocow_inode *entry;
4459 entry = list_first_entry(&nocow_ctx->inodes,
4460 struct scrub_nocow_inode,
4461 list);
4462 list_del_init(&entry->list);
4463 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4464 entry->root, nocow_ctx);
4465 kfree(entry);
4466 if (ret == COPY_COMPLETE) {
4467 ret = 0;
4468 break;
4469 } else if (ret) {
4470 break;
4471 }
4472 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004473out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004474 while (!list_empty(&nocow_ctx->inodes)) {
4475 struct scrub_nocow_inode *entry;
4476 entry = list_first_entry(&nocow_ctx->inodes,
4477 struct scrub_nocow_inode,
4478 list);
4479 list_del_init(&entry->list);
4480 kfree(entry);
4481 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004482 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004483 btrfs_end_transaction(trans);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004484 if (not_written)
4485 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4486 num_uncorrectable_read_errors);
4487
4488 btrfs_free_path(path);
4489 kfree(nocow_ctx);
4490
4491 scrub_pending_trans_workers_dec(sctx);
4492}
4493
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004494static int check_extent_to_block(struct btrfs_inode *inode, u64 start, u64 len,
Gui Hecheng32159242014-11-10 15:36:08 +08004495 u64 logical)
4496{
4497 struct extent_state *cached_state = NULL;
4498 struct btrfs_ordered_extent *ordered;
4499 struct extent_io_tree *io_tree;
4500 struct extent_map *em;
4501 u64 lockstart = start, lockend = start + len - 1;
4502 int ret = 0;
4503
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004504 io_tree = &inode->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004505
David Sterbaff13db42015-12-03 14:30:40 +01004506 lock_extent_bits(io_tree, lockstart, lockend, &cached_state);
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004507 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
Gui Hecheng32159242014-11-10 15:36:08 +08004508 if (ordered) {
4509 btrfs_put_ordered_extent(ordered);
4510 ret = 1;
4511 goto out_unlock;
4512 }
4513
4514 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4515 if (IS_ERR(em)) {
4516 ret = PTR_ERR(em);
4517 goto out_unlock;
4518 }
4519
4520 /*
4521 * This extent does not actually cover the logical extent anymore,
4522 * move on to the next inode.
4523 */
4524 if (em->block_start > logical ||
4525 em->block_start + em->block_len < logical + len) {
4526 free_extent_map(em);
4527 ret = 1;
4528 goto out_unlock;
4529 }
4530 free_extent_map(em);
4531
4532out_unlock:
4533 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4534 GFP_NOFS);
4535 return ret;
4536}
4537
Josef Bacik652f25a2013-09-12 16:58:28 -04004538static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4539 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004540{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004541 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004542 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004543 struct inode *inode;
4544 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004545 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004546 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004547 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004548 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004549 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004550 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004551 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004552 int ret = 0;
4553 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004554
4555 key.objectid = root;
4556 key.type = BTRFS_ROOT_ITEM_KEY;
4557 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004558
4559 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4560
Stefan Behrensff023aa2012-11-06 11:43:11 +01004561 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004562 if (IS_ERR(local_root)) {
4563 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004564 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004565 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004566
4567 key.type = BTRFS_INODE_ITEM_KEY;
4568 key.objectid = inum;
4569 key.offset = 0;
4570 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004571 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004572 if (IS_ERR(inode))
4573 return PTR_ERR(inode);
4574
Miao Xieedd14002013-06-27 18:51:00 +08004575 /* Avoid truncate/dio/punch hole.. */
Al Viro59551022016-01-22 15:40:57 -05004576 inode_lock(inode);
Miao Xieedd14002013-06-27 18:51:00 +08004577 inode_dio_wait(inode);
4578
Stefan Behrensff023aa2012-11-06 11:43:11 +01004579 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004580 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004581 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004582
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004583 ret = check_extent_to_block(BTRFS_I(inode), offset, len,
4584 nocow_ctx_logical);
Gui Hecheng32159242014-11-10 15:36:08 +08004585 if (ret) {
4586 ret = ret > 0 ? 0 : ret;
4587 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004588 }
4589
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004590 while (len >= PAGE_SIZE) {
4591 index = offset >> PAGE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004592again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004593 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4594 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004595 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004596 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004597 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004598 }
4599
4600 if (PageUptodate(page)) {
4601 if (PageDirty(page))
4602 goto next_page;
4603 } else {
4604 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004605 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004606 btrfs_get_extent,
4607 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004608 if (err) {
4609 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004610 goto next_page;
4611 }
Miao Xieedd14002013-06-27 18:51:00 +08004612
Miao Xie26b258912013-06-27 18:50:58 +08004613 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004614 /*
4615 * If the page has been remove from the page cache,
4616 * the data on it is meaningless, because it may be
4617 * old one, the new data may be written into the new
4618 * page in the page cache.
4619 */
4620 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004621 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004622 put_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004623 goto again;
4624 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004625 if (!PageUptodate(page)) {
4626 ret = -EIO;
4627 goto next_page;
4628 }
4629 }
Gui Hecheng32159242014-11-10 15:36:08 +08004630
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004631 ret = check_extent_to_block(BTRFS_I(inode), offset, len,
Gui Hecheng32159242014-11-10 15:36:08 +08004632 nocow_ctx_logical);
4633 if (ret) {
4634 ret = ret > 0 ? 0 : ret;
4635 goto next_page;
4636 }
4637
Miao Xie826aa0a2013-06-27 18:50:59 +08004638 err = write_page_nocow(nocow_ctx->sctx,
4639 physical_for_dev_replace, page);
4640 if (err)
4641 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004642next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004643 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004644 put_page(page);
Miao Xie826aa0a2013-06-27 18:50:59 +08004645
4646 if (ret)
4647 break;
4648
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004649 offset += PAGE_SIZE;
4650 physical_for_dev_replace += PAGE_SIZE;
4651 nocow_ctx_logical += PAGE_SIZE;
4652 len -= PAGE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004653 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004654 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004655out:
Al Viro59551022016-01-22 15:40:57 -05004656 inode_unlock(inode);
Miao Xie826aa0a2013-06-27 18:50:59 +08004657 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004658 return ret;
4659}
4660
4661static int write_page_nocow(struct scrub_ctx *sctx,
4662 u64 physical_for_dev_replace, struct page *page)
4663{
4664 struct bio *bio;
4665 struct btrfs_device *dev;
4666 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004667
4668 dev = sctx->wr_ctx.tgtdev;
4669 if (!dev)
4670 return -EIO;
4671 if (!dev->bdev) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004672 btrfs_warn_rl(dev->fs_info,
David Sterba94647322015-10-08 11:01:36 +02004673 "scrub write_page_nocow(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004674 return -EIO;
4675 }
Chris Mason9be33952013-05-17 18:30:14 -04004676 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004677 if (!bio) {
4678 spin_lock(&sctx->stat_lock);
4679 sctx->stat.malloc_errors++;
4680 spin_unlock(&sctx->stat_lock);
4681 return -ENOMEM;
4682 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004683 bio->bi_iter.bi_size = 0;
4684 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004685 bio->bi_bdev = dev->bdev;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06004686 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004687 ret = bio_add_page(bio, page, PAGE_SIZE, 0);
4688 if (ret != PAGE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004689leave_with_eio:
4690 bio_put(bio);
4691 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4692 return -EIO;
4693 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004694
Mike Christie4e49ea42016-06-05 14:31:41 -05004695 if (btrfsic_submit_bio_wait(bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004696 goto leave_with_eio;
4697
4698 bio_put(bio);
4699 return 0;
4700}