blob: ffe785ec529867706bd801acaac71b231f66098a [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);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100292static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
293 struct scrub_page *spage);
294static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200295static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100296static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
297static int write_page_nocow(struct scrub_ctx *sctx,
298 u64 physical_for_dev_replace, struct page *page);
299static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400300 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100301static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
302 int mirror_num, u64 physical_for_dev_replace);
303static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800304static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800305static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000306static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400307
308
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100309static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
310{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200311 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100312 atomic_inc(&sctx->bios_in_flight);
313}
314
315static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
316{
317 atomic_dec(&sctx->bios_in_flight);
318 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000319 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100320}
321
Wang Shilongcb7ab022013-12-04 21:16:53 +0800322static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800323{
324 while (atomic_read(&fs_info->scrub_pause_req)) {
325 mutex_unlock(&fs_info->scrub_lock);
326 wait_event(fs_info->scrub_pause_wait,
327 atomic_read(&fs_info->scrub_pause_req) == 0);
328 mutex_lock(&fs_info->scrub_lock);
329 }
330}
331
Zhaolei0e22be82015-08-05 16:43:28 +0800332static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800333{
334 atomic_inc(&fs_info->scrubs_paused);
335 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800336}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800337
Zhaolei0e22be82015-08-05 16:43:28 +0800338static void scrub_pause_off(struct btrfs_fs_info *fs_info)
339{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800340 mutex_lock(&fs_info->scrub_lock);
341 __scrub_blocked_if_needed(fs_info);
342 atomic_dec(&fs_info->scrubs_paused);
343 mutex_unlock(&fs_info->scrub_lock);
344
345 wake_up(&fs_info->scrub_pause_wait);
346}
347
Zhaolei0e22be82015-08-05 16:43:28 +0800348static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
349{
350 scrub_pause_on(fs_info);
351 scrub_pause_off(fs_info);
352}
353
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100354/*
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800355 * Insert new full stripe lock into full stripe locks tree
356 *
357 * Return pointer to existing or newly inserted full_stripe_lock structure if
358 * everything works well.
359 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
360 *
361 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
362 * function
363 */
364static struct full_stripe_lock *insert_full_stripe_lock(
365 struct btrfs_full_stripe_locks_tree *locks_root,
366 u64 fstripe_logical)
367{
368 struct rb_node **p;
369 struct rb_node *parent = NULL;
370 struct full_stripe_lock *entry;
371 struct full_stripe_lock *ret;
372
373 WARN_ON(!mutex_is_locked(&locks_root->lock));
374
375 p = &locks_root->root.rb_node;
376 while (*p) {
377 parent = *p;
378 entry = rb_entry(parent, struct full_stripe_lock, node);
379 if (fstripe_logical < entry->logical) {
380 p = &(*p)->rb_left;
381 } else if (fstripe_logical > entry->logical) {
382 p = &(*p)->rb_right;
383 } else {
384 entry->refs++;
385 return entry;
386 }
387 }
388
389 /* Insert new lock */
390 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
391 if (!ret)
392 return ERR_PTR(-ENOMEM);
393 ret->logical = fstripe_logical;
394 ret->refs = 1;
395 mutex_init(&ret->mutex);
396
397 rb_link_node(&ret->node, parent, p);
398 rb_insert_color(&ret->node, &locks_root->root);
399 return ret;
400}
401
402/*
403 * Search for a full stripe lock of a block group
404 *
405 * Return pointer to existing full stripe lock if found
406 * Return NULL if not found
407 */
408static struct full_stripe_lock *search_full_stripe_lock(
409 struct btrfs_full_stripe_locks_tree *locks_root,
410 u64 fstripe_logical)
411{
412 struct rb_node *node;
413 struct full_stripe_lock *entry;
414
415 WARN_ON(!mutex_is_locked(&locks_root->lock));
416
417 node = locks_root->root.rb_node;
418 while (node) {
419 entry = rb_entry(node, struct full_stripe_lock, node);
420 if (fstripe_logical < entry->logical)
421 node = node->rb_left;
422 else if (fstripe_logical > entry->logical)
423 node = node->rb_right;
424 else
425 return entry;
426 }
427 return NULL;
428}
429
430/*
431 * Helper to get full stripe logical from a normal bytenr.
432 *
433 * Caller must ensure @cache is a RAID56 block group.
434 */
435static u64 get_full_stripe_logical(struct btrfs_block_group_cache *cache,
436 u64 bytenr)
437{
438 u64 ret;
439
440 /*
441 * Due to chunk item size limit, full stripe length should not be
442 * larger than U32_MAX. Just a sanity check here.
443 */
444 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
445
446 /*
447 * round_down() can only handle power of 2, while RAID56 full
448 * stripe length can be 64KiB * n, so we need to manually round down.
449 */
450 ret = div64_u64(bytenr - cache->key.objectid, cache->full_stripe_len) *
451 cache->full_stripe_len + cache->key.objectid;
452 return ret;
453}
454
455/*
456 * Lock a full stripe to avoid concurrency of recovery and read
457 *
458 * It's only used for profiles with parities (RAID5/6), for other profiles it
459 * does nothing.
460 *
461 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
462 * So caller must call unlock_full_stripe() at the same context.
463 *
464 * Return <0 if encounters error.
465 */
466static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
467 bool *locked_ret)
468{
469 struct btrfs_block_group_cache *bg_cache;
470 struct btrfs_full_stripe_locks_tree *locks_root;
471 struct full_stripe_lock *existing;
472 u64 fstripe_start;
473 int ret = 0;
474
475 *locked_ret = false;
476 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
477 if (!bg_cache) {
478 ASSERT(0);
479 return -ENOENT;
480 }
481
482 /* Profiles not based on parity don't need full stripe lock */
483 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
484 goto out;
485 locks_root = &bg_cache->full_stripe_locks_root;
486
487 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
488
489 /* Now insert the full stripe lock */
490 mutex_lock(&locks_root->lock);
491 existing = insert_full_stripe_lock(locks_root, fstripe_start);
492 mutex_unlock(&locks_root->lock);
493 if (IS_ERR(existing)) {
494 ret = PTR_ERR(existing);
495 goto out;
496 }
497 mutex_lock(&existing->mutex);
498 *locked_ret = true;
499out:
500 btrfs_put_block_group(bg_cache);
501 return ret;
502}
503
504/*
505 * Unlock a full stripe.
506 *
507 * NOTE: Caller must ensure it's the same context calling corresponding
508 * lock_full_stripe().
509 *
510 * Return 0 if we unlock full stripe without problem.
511 * Return <0 for error
512 */
513static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
514 bool locked)
515{
516 struct btrfs_block_group_cache *bg_cache;
517 struct btrfs_full_stripe_locks_tree *locks_root;
518 struct full_stripe_lock *fstripe_lock;
519 u64 fstripe_start;
520 bool freeit = false;
521 int ret = 0;
522
523 /* If we didn't acquire full stripe lock, no need to continue */
524 if (!locked)
525 return 0;
526
527 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
528 if (!bg_cache) {
529 ASSERT(0);
530 return -ENOENT;
531 }
532 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
533 goto out;
534
535 locks_root = &bg_cache->full_stripe_locks_root;
536 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
537
538 mutex_lock(&locks_root->lock);
539 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
540 /* Unpaired unlock_full_stripe() detected */
541 if (!fstripe_lock) {
542 WARN_ON(1);
543 ret = -ENOENT;
544 mutex_unlock(&locks_root->lock);
545 goto out;
546 }
547
548 if (fstripe_lock->refs == 0) {
549 WARN_ON(1);
550 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
551 fstripe_lock->logical);
552 } else {
553 fstripe_lock->refs--;
554 }
555
556 if (fstripe_lock->refs == 0) {
557 rb_erase(&fstripe_lock->node, &locks_root->root);
558 freeit = true;
559 }
560 mutex_unlock(&locks_root->lock);
561
562 mutex_unlock(&fstripe_lock->mutex);
563 if (freeit)
564 kfree(fstripe_lock);
565out:
566 btrfs_put_block_group(bg_cache);
567 return ret;
568}
569
570/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100571 * used for workers that require transaction commits (i.e., for the
572 * NOCOW case)
573 */
574static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
575{
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400576 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100577
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200578 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100579 /*
580 * increment scrubs_running to prevent cancel requests from
581 * completing as long as a worker is running. we must also
582 * increment scrubs_paused to prevent deadlocking on pause
583 * requests used for transactions commits (as the worker uses a
584 * transaction context). it is safe to regard the worker
585 * as paused for all matters practical. effectively, we only
586 * avoid cancellation requests from completing.
587 */
588 mutex_lock(&fs_info->scrub_lock);
589 atomic_inc(&fs_info->scrubs_running);
590 atomic_inc(&fs_info->scrubs_paused);
591 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800592
593 /*
594 * check if @scrubs_running=@scrubs_paused condition
595 * inside wait_event() is not an atomic operation.
596 * which means we may inc/dec @scrub_running/paused
597 * at any time. Let's wake up @scrub_pause_wait as
598 * much as we can to let commit transaction blocked less.
599 */
600 wake_up(&fs_info->scrub_pause_wait);
601
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100602 atomic_inc(&sctx->workers_pending);
603}
604
605/* used for workers that require transaction commits */
606static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
607{
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400608 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100609
610 /*
611 * see scrub_pending_trans_workers_inc() why we're pretending
612 * to be paused in the scrub counters
613 */
614 mutex_lock(&fs_info->scrub_lock);
615 atomic_dec(&fs_info->scrubs_running);
616 atomic_dec(&fs_info->scrubs_paused);
617 mutex_unlock(&fs_info->scrub_lock);
618 atomic_dec(&sctx->workers_pending);
619 wake_up(&fs_info->scrub_pause_wait);
620 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000621 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100622}
623
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100624static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100625{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100626 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100627 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100628 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100629 struct btrfs_ordered_sum, list);
630 list_del(&sum->list);
631 kfree(sum);
632 }
633}
634
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100635static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100636{
637 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100638
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100639 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100640 return;
641
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400642 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100643 if (sctx->curr != -1) {
644 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400645
646 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100647 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400648 scrub_block_put(sbio->pagev[i]->sblock);
649 }
650 bio_put(sbio->bio);
651 }
652
Stefan Behrensff023aa2012-11-06 11:43:11 +0100653 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100654 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100655
656 if (!sbio)
657 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100658 kfree(sbio);
659 }
660
David Sterba4e2814e2017-05-16 19:10:29 +0200661 kfree(sctx->wr_ctx.wr_curr_bio);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100662 scrub_free_csums(sctx);
663 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100664}
665
Filipe Mananaf55985f2015-02-09 21:14:24 +0000666static void scrub_put_ctx(struct scrub_ctx *sctx)
667{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200668 if (refcount_dec_and_test(&sctx->refs))
Filipe Mananaf55985f2015-02-09 21:14:24 +0000669 scrub_free_ctx(sctx);
670}
671
Arne Jansena2de7332011-03-08 14:14:00 +0100672static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100673struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100674{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100675 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100676 int i;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400677 struct btrfs_fs_info *fs_info = dev->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100678
David Sterba58c4e172016-02-11 10:49:42 +0100679 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100680 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100681 goto nomem;
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200682 refcount_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100683 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200684 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100685 sctx->curr = -1;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400686 sctx->fs_info = dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100687 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100688 struct scrub_bio *sbio;
689
David Sterba58c4e172016-02-11 10:49:42 +0100690 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
Arne Jansena2de7332011-03-08 14:14:00 +0100691 if (!sbio)
692 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100693 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100694
Arne Jansena2de7332011-03-08 14:14:00 +0100695 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100696 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400697 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800698 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
699 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100700
Stefan Behrensff023aa2012-11-06 11:43:11 +0100701 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100702 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200703 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100704 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100705 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100706 sctx->first_free = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400707 sctx->nodesize = fs_info->nodesize;
708 sctx->sectorsize = fs_info->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100709 atomic_set(&sctx->bios_in_flight, 0);
710 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100711 atomic_set(&sctx->cancel_req, 0);
712 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
713 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100714
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100715 spin_lock_init(&sctx->list_lock);
716 spin_lock_init(&sctx->stat_lock);
717 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100718
David Sterba8fcdac32017-05-16 19:10:23 +0200719 WARN_ON(sctx->wr_ctx.wr_curr_bio != NULL);
720 mutex_init(&sctx->wr_ctx.wr_lock);
721 sctx->wr_ctx.wr_curr_bio = NULL;
722 if (is_dev_replace) {
723 WARN_ON(!dev->bdev);
724 sctx->wr_ctx.pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
725 sctx->wr_ctx.tgtdev = dev;
726 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100727 }
David Sterba8fcdac32017-05-16 19:10:23 +0200728
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100729 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100730
731nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100732 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100733 return ERR_PTR(-ENOMEM);
734}
735
Stefan Behrensff023aa2012-11-06 11:43:11 +0100736static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
737 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200738{
739 u64 isize;
740 u32 nlink;
741 int ret;
742 int i;
743 struct extent_buffer *eb;
744 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100745 struct scrub_warning *swarn = warn_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400746 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200747 struct inode_fs_paths *ipath = NULL;
748 struct btrfs_root *local_root;
749 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100750 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200751
752 root_key.objectid = root;
753 root_key.type = BTRFS_ROOT_ITEM_KEY;
754 root_key.offset = (u64)-1;
755 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
756 if (IS_ERR(local_root)) {
757 ret = PTR_ERR(local_root);
758 goto err;
759 }
760
David Sterba14692cc2015-01-02 18:55:46 +0100761 /*
762 * this makes the path point to (inum INODE_ITEM ioff)
763 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100764 key.objectid = inum;
765 key.type = BTRFS_INODE_ITEM_KEY;
766 key.offset = 0;
767
768 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200769 if (ret) {
770 btrfs_release_path(swarn->path);
771 goto err;
772 }
773
774 eb = swarn->path->nodes[0];
775 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
776 struct btrfs_inode_item);
777 isize = btrfs_inode_size(eb, inode_item);
778 nlink = btrfs_inode_nlink(eb, inode_item);
779 btrfs_release_path(swarn->path);
780
781 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300782 if (IS_ERR(ipath)) {
783 ret = PTR_ERR(ipath);
784 ipath = NULL;
785 goto err;
786 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200787 ret = paths_from_inode(inum, ipath);
788
789 if (ret < 0)
790 goto err;
791
792 /*
793 * we deliberately ignore the bit ipath might have been too small to
794 * hold all of the paths here
795 */
796 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400797 btrfs_warn_in_rcu(fs_info,
798 "%s at logical %llu on dev %s, sector %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
799 swarn->errstr, swarn->logical,
800 rcu_str_deref(swarn->dev->name),
801 (unsigned long long)swarn->sector,
802 root, inum, offset,
803 min(isize - offset, (u64)PAGE_SIZE), nlink,
804 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200805
806 free_ipath(ipath);
807 return 0;
808
809err:
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400810 btrfs_warn_in_rcu(fs_info,
811 "%s at logical %llu on dev %s, sector %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
812 swarn->errstr, swarn->logical,
813 rcu_str_deref(swarn->dev->name),
814 (unsigned long long)swarn->sector,
815 root, inum, offset, ret);
Jan Schmidt558540c2011-06-13 19:59:12 +0200816
817 free_ipath(ipath);
818 return 0;
819}
820
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400821static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200822{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100823 struct btrfs_device *dev;
824 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200825 struct btrfs_path *path;
826 struct btrfs_key found_key;
827 struct extent_buffer *eb;
828 struct btrfs_extent_item *ei;
829 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200830 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100831 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600832 u64 flags = 0;
833 u64 ref_root;
834 u32 item_size;
Dan Carpenter07c9a8e2016-03-11 11:08:56 +0300835 u8 ref_level = 0;
Liu Bo69917e42012-09-07 20:01:28 -0600836 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200837
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100838 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100839 dev = sblock->pagev[0]->dev;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400840 fs_info = sblock->sctx->fs_info;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100841
Jan Schmidt558540c2011-06-13 19:59:12 +0200842 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200843 if (!path)
844 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200845
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100846 swarn.sector = (sblock->pagev[0]->physical) >> 9;
847 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200848 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100849 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200850
Liu Bo69917e42012-09-07 20:01:28 -0600851 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
852 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200853 if (ret < 0)
854 goto out;
855
Jan Schmidt4692cf52011-12-02 14:56:41 +0100856 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200857 swarn.extent_item_size = found_key.offset;
858
859 eb = path->nodes[0];
860 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
861 item_size = btrfs_item_size_nr(eb, path->slots[0]);
862
Liu Bo69917e42012-09-07 20:01:28 -0600863 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200864 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800865 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
866 item_size, &ref_root,
867 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200868 btrfs_warn_in_rcu(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400869 "%s at logical %llu on dev %s, sector %llu: metadata %s (level %d) in tree %llu",
870 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400871 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200872 (unsigned long long)swarn.sector,
873 ref_level ? "node" : "leaf",
874 ret < 0 ? -1 : ref_level,
875 ret < 0 ? -1 : ref_root);
876 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600877 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200878 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600879 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200880 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100881 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100882 iterate_extent_inodes(fs_info, found_key.objectid,
883 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200884 scrub_print_warning_inode, &swarn);
885 }
886
887out:
888 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200889}
890
Stefan Behrensff023aa2012-11-06 11:43:11 +0100891static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200892{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200893 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200894 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100895 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200896 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200897 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200898 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200899 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000900 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200901 u64 end = offset + PAGE_SIZE - 1;
902 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000903 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200904
905 key.objectid = root;
906 key.type = BTRFS_ROOT_ITEM_KEY;
907 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000908
909 fs_info = fixup->root->fs_info;
910 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
911
912 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
913 if (IS_ERR(local_root)) {
914 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200915 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000916 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200917
918 key.type = BTRFS_INODE_ITEM_KEY;
919 key.objectid = inum;
920 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000921 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
922 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200923 if (IS_ERR(inode))
924 return PTR_ERR(inode);
925
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300926 index = offset >> PAGE_SHIFT;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200927
928 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200929 if (!page) {
930 ret = -ENOMEM;
931 goto out;
932 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200933
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200934 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200935 if (PageDirty(page)) {
936 /*
937 * we need to write the data to the defect sector. the
938 * data that was in that sector is not in memory,
939 * because the page was modified. we must not write the
940 * modified page to that sector.
941 *
942 * TODO: what could be done here: wait for the delalloc
943 * runner to write out that page (might involve
944 * COW) and see whether the sector is still
945 * referenced afterwards.
946 *
947 * For the meantime, we'll treat this error
948 * incorrectable, although there is a chance that a
949 * later scrub will find the bad sector again and that
950 * there's no dirty page in memory, then.
951 */
952 ret = -EIO;
953 goto out;
954 }
Josef Bacik6ec656b2017-05-05 11:57:14 -0400955 ret = repair_io_failure(fs_info, inum, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200956 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800957 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200958 fixup->mirror_num);
959 unlock_page(page);
960 corrected = !ret;
961 } else {
962 /*
963 * we need to get good data first. the general readpage path
964 * will call repair_io_failure for us, we just have to make
965 * sure we read the bad mirror.
966 */
967 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +0200968 EXTENT_DAMAGED);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200969 if (ret) {
970 /* set_extent_bits should give proper error */
971 WARN_ON(ret > 0);
972 if (ret > 0)
973 ret = -EFAULT;
974 goto out;
975 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200976
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200977 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
978 btrfs_get_extent,
979 fixup->mirror_num);
980 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200981
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200982 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
983 end, EXTENT_DAMAGED, 0, NULL);
984 if (!corrected)
985 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
David Sterba91166212016-04-26 23:54:39 +0200986 EXTENT_DAMAGED);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200987 }
988
989out:
990 if (page)
991 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200992
993 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200994
995 if (ret < 0)
996 return ret;
997
998 if (ret == 0 && corrected) {
999 /*
1000 * we only need to call readpage for one of the inodes belonging
1001 * to this extent. so make iterate_extent_inodes stop
1002 */
1003 return 1;
1004 }
1005
1006 return -EIO;
1007}
1008
1009static void scrub_fixup_nodatasum(struct btrfs_work *work)
1010{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001011 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001012 int ret;
1013 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001014 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001015 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001016 struct btrfs_path *path;
1017 int uncorrectable = 0;
1018
1019 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001020 sctx = fixup->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001021 fs_info = fixup->root->fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001022
1023 path = btrfs_alloc_path();
1024 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001025 spin_lock(&sctx->stat_lock);
1026 ++sctx->stat.malloc_errors;
1027 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001028 uncorrectable = 1;
1029 goto out;
1030 }
1031
1032 trans = btrfs_join_transaction(fixup->root);
1033 if (IS_ERR(trans)) {
1034 uncorrectable = 1;
1035 goto out;
1036 }
1037
1038 /*
1039 * the idea is to trigger a regular read through the standard path. we
1040 * read a page from the (failed) logical address by specifying the
1041 * corresponding copynum of the failed sector. thus, that readpage is
1042 * expected to fail.
1043 * that is the point where on-the-fly error correction will kick in
1044 * (once it's finished) and rewrite the failed sector if a good copy
1045 * can be found.
1046 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001047 ret = iterate_inodes_from_logical(fixup->logical, fs_info, path,
1048 scrub_fixup_readpage, fixup);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001049 if (ret < 0) {
1050 uncorrectable = 1;
1051 goto out;
1052 }
1053 WARN_ON(ret != 1);
1054
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001055 spin_lock(&sctx->stat_lock);
1056 ++sctx->stat.corrected_errors;
1057 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001058
1059out:
1060 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001061 btrfs_end_transaction(trans);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001062 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001063 spin_lock(&sctx->stat_lock);
1064 ++sctx->stat.uncorrectable_errors;
1065 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001066 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001067 &fs_info->dev_replace.num_uncorrectable_read_errors);
1068 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02001069 "unable to fixup (nodatasum) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001070 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001071 }
1072
1073 btrfs_free_path(path);
1074 kfree(fixup);
1075
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001076 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001077}
1078
Miao Xieaf8e2d12014-10-23 14:42:50 +08001079static inline void scrub_get_recover(struct scrub_recover *recover)
1080{
Elena Reshetova6f615012017-03-03 10:55:21 +02001081 refcount_inc(&recover->refs);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001082}
1083
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001084static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
1085 struct scrub_recover *recover)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001086{
Elena Reshetova6f615012017-03-03 10:55:21 +02001087 if (refcount_dec_and_test(&recover->refs)) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001088 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08001089 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001090 kfree(recover);
1091 }
1092}
1093
Arne Jansena2de7332011-03-08 14:14:00 +01001094/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001095 * scrub_handle_errored_block gets called when either verification of the
1096 * pages failed or the bio failed to read, e.g. with EIO. In the latter
1097 * case, this function handles all pages in the bio, even though only one
1098 * may be bad.
1099 * The goal of this function is to repair the errored block by using the
1100 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +01001101 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001102static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +01001103{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001104 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001105 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001106 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01001107 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001108 u64 logical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001109 unsigned int failed_mirror_index;
1110 unsigned int is_metadata;
1111 unsigned int have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001112 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
1113 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +01001114 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001115 int mirror_index;
1116 int page_num;
1117 int success;
Qu Wenruo28d70e22017-04-14 08:35:55 +08001118 bool full_stripe_locked;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001119 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1120 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +01001121
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001122 BUG_ON(sblock_to_check->page_count < 1);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001123 fs_info = sctx->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +00001124 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
1125 /*
1126 * if we find an error in a super block, we just report it.
1127 * They will get written with the next transaction commit
1128 * anyway
1129 */
1130 spin_lock(&sctx->stat_lock);
1131 ++sctx->stat.super_errors;
1132 spin_unlock(&sctx->stat_lock);
1133 return 0;
1134 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001135 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001136 logical = sblock_to_check->pagev[0]->logical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001137 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
1138 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
1139 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001140 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001141 have_csum = sblock_to_check->pagev[0]->have_csum;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001142 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001143
Qu Wenruo28d70e22017-04-14 08:35:55 +08001144 /*
1145 * For RAID5/6, race can happen for a different device scrub thread.
1146 * For data corruption, Parity and Data threads will both try
1147 * to recovery the data.
1148 * Race can lead to doubly added csum error, or even unrecoverable
1149 * error.
1150 */
1151 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
1152 if (ret < 0) {
1153 spin_lock(&sctx->stat_lock);
1154 if (ret == -ENOMEM)
1155 sctx->stat.malloc_errors++;
1156 sctx->stat.read_errors++;
1157 sctx->stat.uncorrectable_errors++;
1158 spin_unlock(&sctx->stat_lock);
1159 return ret;
1160 }
1161
Stefan Behrensff023aa2012-11-06 11:43:11 +01001162 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
1163 sblocks_for_recheck = NULL;
1164 goto nodatasum_case;
1165 }
1166
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001167 /*
1168 * read all mirrors one after the other. This includes to
1169 * re-read the extent or metadata block that failed (that was
1170 * the cause that this fixup code is called) another time,
1171 * page by page this time in order to know which pages
1172 * caused I/O errors and which ones are good (for all mirrors).
1173 * It is the goal to handle the situation when more than one
1174 * mirror contains I/O errors, but the errors do not
1175 * overlap, i.e. the data can be repaired by selecting the
1176 * pages from those mirrors without I/O error on the
1177 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
1178 * would be that mirror #1 has an I/O error on the first page,
1179 * the second page is good, and mirror #2 has an I/O error on
1180 * the second page, but the first page is good.
1181 * Then the first page of the first mirror can be repaired by
1182 * taking the first page of the second mirror, and the
1183 * second page of the second mirror can be repaired by
1184 * copying the contents of the 2nd page of the 1st mirror.
1185 * One more note: if the pages of one mirror contain I/O
1186 * errors, the checksum cannot be verified. In order to get
1187 * the best data for repairing, the first attempt is to find
1188 * a mirror without I/O errors and with a validated checksum.
1189 * Only if this is not possible, the pages are picked from
1190 * mirrors with I/O errors without considering the checksum.
1191 * If the latter is the case, at the end, the checksum of the
1192 * repaired area is verified in order to correctly maintain
1193 * the statistics.
1194 */
1195
David Sterba31e818f2015-02-20 18:00:26 +01001196 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
1197 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001198 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001199 spin_lock(&sctx->stat_lock);
1200 sctx->stat.malloc_errors++;
1201 sctx->stat.read_errors++;
1202 sctx->stat.uncorrectable_errors++;
1203 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001204 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001205 goto out;
1206 }
1207
1208 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +08001209 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001210 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001211 spin_lock(&sctx->stat_lock);
1212 sctx->stat.read_errors++;
1213 sctx->stat.uncorrectable_errors++;
1214 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001215 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001216 goto out;
1217 }
1218 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
1219 sblock_bad = sblocks_for_recheck + failed_mirror_index;
1220
1221 /* build and submit the bios for the failed mirror, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001222 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001223
1224 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
1225 sblock_bad->no_io_error_seen) {
1226 /*
1227 * the error disappeared after reading page by page, or
1228 * the area was part of a huge bio and other parts of the
1229 * bio caused I/O errors, or the block layer merged several
1230 * read requests into one and the error is caused by a
1231 * different bio (usually one of the two latter cases is
1232 * the cause)
1233 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001234 spin_lock(&sctx->stat_lock);
1235 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001236 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001237 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001238
Stefan Behrensff023aa2012-11-06 11:43:11 +01001239 if (sctx->is_dev_replace)
1240 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001241 goto out;
1242 }
1243
1244 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001245 spin_lock(&sctx->stat_lock);
1246 sctx->stat.read_errors++;
1247 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001248 if (__ratelimit(&_rs))
1249 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001250 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001251 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001252 spin_lock(&sctx->stat_lock);
1253 sctx->stat.csum_errors++;
1254 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001255 if (__ratelimit(&_rs))
1256 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001257 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001258 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001259 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001260 spin_lock(&sctx->stat_lock);
1261 sctx->stat.verify_errors++;
1262 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001263 if (__ratelimit(&_rs))
1264 scrub_print_warning("checksum/header error",
1265 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001266 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001267 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001268 BTRFS_DEV_STAT_GENERATION_ERRS);
1269 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001270 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001271 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001272 }
1273
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001274 if (sctx->readonly) {
1275 ASSERT(!sctx->is_dev_replace);
1276 goto out;
1277 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001278
1279 if (!is_metadata && !have_csum) {
1280 struct scrub_fixup_nodatasum *fixup_nodatasum;
1281
Stefan Behrensff023aa2012-11-06 11:43:11 +01001282 WARN_ON(sctx->is_dev_replace);
1283
Zhao Leib25c94c2015-01-20 15:11:35 +08001284nodatasum_case:
1285
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001286 /*
1287 * !is_metadata and !have_csum, this means that the data
Nicholas D Steeves01327612016-05-19 21:18:45 -04001288 * might not be COWed, that it might be modified
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001289 * concurrently. The general strategy to work on the
1290 * commit root does not help in the case when COW is not
1291 * used.
1292 */
1293 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1294 if (!fixup_nodatasum)
1295 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001296 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001297 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001298 fixup_nodatasum->logical = logical;
1299 fixup_nodatasum->root = fs_info->extent_root;
1300 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001301 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001302 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1303 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001304 btrfs_queue_work(fs_info->scrub_workers,
1305 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001306 goto out;
1307 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001308
1309 /*
1310 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001311 * checksums.
1312 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001313 * errors and also does not have a checksum error.
1314 * If one is found, and if a checksum is present, the full block
1315 * that is known to contain an error is rewritten. Afterwards
1316 * the block is known to be corrected.
1317 * If a mirror is found which is completely correct, and no
1318 * checksum is present, only those pages are rewritten that had
1319 * an I/O error in the block to be repaired, since it cannot be
1320 * determined, which copy of the other pages is better (and it
1321 * could happen otherwise that a correct page would be
1322 * overwritten by a bad one).
1323 */
1324 for (mirror_index = 0;
1325 mirror_index < BTRFS_MAX_MIRRORS &&
1326 sblocks_for_recheck[mirror_index].page_count > 0;
1327 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001328 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001329
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001330 if (mirror_index == failed_mirror_index)
1331 continue;
1332 sblock_other = sblocks_for_recheck + mirror_index;
1333
1334 /* build and submit the bios, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001335 scrub_recheck_block(fs_info, sblock_other, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001336
1337 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001338 !sblock_other->checksum_error &&
1339 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001340 if (sctx->is_dev_replace) {
1341 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001342 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001343 } else {
1344 ret = scrub_repair_block_from_good_copy(
1345 sblock_bad, sblock_other);
1346 if (!ret)
1347 goto corrected_error;
1348 }
Arne Jansena2de7332011-03-08 14:14:00 +01001349 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001350 }
1351
Zhao Leib968fed2015-01-20 15:11:41 +08001352 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1353 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001354
1355 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001356 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001357 * repaired, continue by picking good copies of those pages.
1358 * Select the good pages from mirrors to rewrite bad pages from
1359 * the area to fix. Afterwards verify the checksum of the block
1360 * that is supposed to be repaired. This verification step is
1361 * only done for the purpose of statistic counting and for the
1362 * final scrub report, whether errors remain.
1363 * A perfect algorithm could make use of the checksum and try
1364 * all possible combinations of pages from the different mirrors
1365 * until the checksum verification succeeds. For example, when
1366 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1367 * of mirror #2 is readable but the final checksum test fails,
1368 * then the 2nd page of mirror #3 could be tried, whether now
Nicholas D Steeves01327612016-05-19 21:18:45 -04001369 * the final checksum succeeds. But this would be a rare
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001370 * exception and is therefore not implemented. At least it is
1371 * avoided that the good copy is overwritten.
1372 * A more useful improvement would be to pick the sectors
1373 * without I/O error based on sector sizes (512 bytes on legacy
1374 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1375 * mirror could be repaired by taking 512 byte of a different
1376 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1377 * area are unreadable.
1378 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001379 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001380 for (page_num = 0; page_num < sblock_bad->page_count;
1381 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001382 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001383 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001384
Zhao Leib968fed2015-01-20 15:11:41 +08001385 /* skip no-io-error page in scrub */
1386 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001387 continue;
1388
Zhao Leib968fed2015-01-20 15:11:41 +08001389 /* try to find no-io-error page in mirrors */
1390 if (page_bad->io_error) {
1391 for (mirror_index = 0;
1392 mirror_index < BTRFS_MAX_MIRRORS &&
1393 sblocks_for_recheck[mirror_index].page_count > 0;
1394 mirror_index++) {
1395 if (!sblocks_for_recheck[mirror_index].
1396 pagev[page_num]->io_error) {
1397 sblock_other = sblocks_for_recheck +
1398 mirror_index;
1399 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001400 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001401 }
Zhao Leib968fed2015-01-20 15:11:41 +08001402 if (!sblock_other)
1403 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001404 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001405
Zhao Leib968fed2015-01-20 15:11:41 +08001406 if (sctx->is_dev_replace) {
1407 /*
1408 * did not find a mirror to fetch the page
1409 * from. scrub_write_page_to_dev_replace()
1410 * handles this case (page->io_error), by
1411 * filling the block with zeros before
1412 * submitting the write request
1413 */
1414 if (!sblock_other)
1415 sblock_other = sblock_bad;
1416
1417 if (scrub_write_page_to_dev_replace(sblock_other,
1418 page_num) != 0) {
1419 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001420 &fs_info->dev_replace.num_write_errors);
Zhao Leib968fed2015-01-20 15:11:41 +08001421 success = 0;
1422 }
1423 } else if (sblock_other) {
1424 ret = scrub_repair_page_from_good_copy(sblock_bad,
1425 sblock_other,
1426 page_num, 0);
1427 if (0 == ret)
1428 page_bad->io_error = 0;
1429 else
1430 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001431 }
1432 }
1433
Zhao Leib968fed2015-01-20 15:11:41 +08001434 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001435 if (is_metadata || have_csum) {
1436 /*
1437 * need to verify the checksum now that all
1438 * sectors on disk are repaired (the write
1439 * request for data to be repaired is on its way).
1440 * Just be lazy and use scrub_recheck_block()
1441 * which re-reads the data before the checksum
1442 * is verified, but most likely the data comes out
1443 * of the page cache.
1444 */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001445 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001446 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001447 !sblock_bad->checksum_error &&
1448 sblock_bad->no_io_error_seen)
1449 goto corrected_error;
1450 else
1451 goto did_not_correct_error;
1452 } else {
1453corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001454 spin_lock(&sctx->stat_lock);
1455 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001456 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001457 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001458 btrfs_err_rl_in_rcu(fs_info,
1459 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001460 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001461 }
1462 } else {
1463did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001464 spin_lock(&sctx->stat_lock);
1465 sctx->stat.uncorrectable_errors++;
1466 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001467 btrfs_err_rl_in_rcu(fs_info,
1468 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001469 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001470 }
1471
1472out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001473 if (sblocks_for_recheck) {
1474 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1475 mirror_index++) {
1476 struct scrub_block *sblock = sblocks_for_recheck +
1477 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001478 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001479 int page_index;
1480
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001481 for (page_index = 0; page_index < sblock->page_count;
1482 page_index++) {
1483 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001484 recover = sblock->pagev[page_index]->recover;
1485 if (recover) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001486 scrub_put_recover(fs_info, recover);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001487 sblock->pagev[page_index]->recover =
1488 NULL;
1489 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001490 scrub_page_put(sblock->pagev[page_index]);
1491 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001492 }
1493 kfree(sblocks_for_recheck);
1494 }
1495
Qu Wenruo28d70e22017-04-14 08:35:55 +08001496 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
1497 if (ret < 0)
1498 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001499 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001500}
1501
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001502static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001503{
Zhao Lei10f11902015-01-20 15:11:43 +08001504 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1505 return 2;
1506 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1507 return 3;
1508 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001509 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001510}
1511
Zhao Lei10f11902015-01-20 15:11:43 +08001512static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1513 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001514 u64 mapped_length,
1515 int nstripes, int mirror,
1516 int *stripe_index,
1517 u64 *stripe_offset)
1518{
1519 int i;
1520
Zhao Leiffe2d202015-01-20 15:11:44 +08001521 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001522 /* RAID5/6 */
1523 for (i = 0; i < nstripes; i++) {
1524 if (raid_map[i] == RAID6_Q_STRIPE ||
1525 raid_map[i] == RAID5_P_STRIPE)
1526 continue;
1527
1528 if (logical >= raid_map[i] &&
1529 logical < raid_map[i] + mapped_length)
1530 break;
1531 }
1532
1533 *stripe_index = i;
1534 *stripe_offset = logical - raid_map[i];
1535 } else {
1536 /* The other RAID type */
1537 *stripe_index = mirror;
1538 *stripe_offset = 0;
1539 }
1540}
1541
Zhao Leibe50a8d2015-01-20 15:11:42 +08001542static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001543 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001544{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001545 struct scrub_ctx *sctx = original_sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001546 struct btrfs_fs_info *fs_info = sctx->fs_info;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001547 u64 length = original_sblock->page_count * PAGE_SIZE;
1548 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001549 u64 generation = original_sblock->pagev[0]->generation;
1550 u64 flags = original_sblock->pagev[0]->flags;
1551 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001552 struct scrub_recover *recover;
1553 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001554 u64 sublen;
1555 u64 mapped_length;
1556 u64 stripe_offset;
1557 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001558 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001559 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001560 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001561 int ret;
1562
1563 /*
Zhao Lei57019342015-01-20 15:11:45 +08001564 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001565 * are not used (and not set) in the blocks that are used for
1566 * the recheck procedure
1567 */
1568
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001569 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001570 sublen = min_t(u64, length, PAGE_SIZE);
1571 mapped_length = sublen;
1572 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001573
1574 /*
1575 * with a length of PAGE_SIZE, each returned stripe
1576 * represents one mirror
1577 */
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001578 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001579 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
David Sterba825ad4c2017-03-28 14:45:22 +02001580 logical, &mapped_length, &bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001581 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001582 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001583 btrfs_bio_counter_dec(fs_info);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001584 return -EIO;
1585 }
1586
Miao Xieaf8e2d12014-10-23 14:42:50 +08001587 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1588 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001589 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001590 btrfs_bio_counter_dec(fs_info);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001591 return -ENOMEM;
1592 }
1593
Elena Reshetova6f615012017-03-03 10:55:21 +02001594 refcount_set(&recover->refs, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001595 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001596 recover->map_length = mapped_length;
1597
Ashish Samant24731142016-04-29 18:33:59 -07001598 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001599
Zhao Leibe50a8d2015-01-20 15:11:42 +08001600 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001601
Miao Xieaf8e2d12014-10-23 14:42:50 +08001602 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001603 mirror_index++) {
1604 struct scrub_block *sblock;
1605 struct scrub_page *page;
1606
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001607 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001608 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001609
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001610 page = kzalloc(sizeof(*page), GFP_NOFS);
1611 if (!page) {
1612leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001613 spin_lock(&sctx->stat_lock);
1614 sctx->stat.malloc_errors++;
1615 spin_unlock(&sctx->stat_lock);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001616 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001617 return -ENOMEM;
1618 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001619 scrub_page_get(page);
1620 sblock->pagev[page_index] = page;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001621 page->sblock = sblock;
1622 page->flags = flags;
1623 page->generation = generation;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001624 page->logical = logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001625 page->have_csum = have_csum;
1626 if (have_csum)
1627 memcpy(page->csum,
1628 original_sblock->pagev[0]->csum,
1629 sctx->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001630
Zhao Lei10f11902015-01-20 15:11:43 +08001631 scrub_stripe_index_and_offset(logical,
1632 bbio->map_type,
1633 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001634 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001635 bbio->num_stripes -
1636 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001637 mirror_index,
1638 &stripe_index,
1639 &stripe_offset);
1640 page->physical = bbio->stripes[stripe_index].physical +
1641 stripe_offset;
1642 page->dev = bbio->stripes[stripe_index].dev;
1643
Stefan Behrensff023aa2012-11-06 11:43:11 +01001644 BUG_ON(page_index >= original_sblock->page_count);
1645 page->physical_for_dev_replace =
1646 original_sblock->pagev[page_index]->
1647 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001648 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001649 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001650 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001651 page->page = alloc_page(GFP_NOFS);
1652 if (!page->page)
1653 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001654
1655 scrub_get_recover(recover);
1656 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001657 }
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001658 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001659 length -= sublen;
1660 logical += sublen;
1661 page_index++;
1662 }
1663
1664 return 0;
1665}
1666
Miao Xieaf8e2d12014-10-23 14:42:50 +08001667struct scrub_bio_ret {
1668 struct completion event;
1669 int error;
1670};
1671
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001672static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001673{
1674 struct scrub_bio_ret *ret = bio->bi_private;
1675
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001676 ret->error = bio->bi_error;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001677 complete(&ret->event);
1678}
1679
1680static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1681{
Zhao Lei10f11902015-01-20 15:11:43 +08001682 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001683 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001684}
1685
1686static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1687 struct bio *bio,
1688 struct scrub_page *page)
1689{
1690 struct scrub_bio_ret done;
1691 int ret;
1692
1693 init_completion(&done.event);
1694 done.error = 0;
1695 bio->bi_iter.bi_sector = page->logical >> 9;
1696 bio->bi_private = &done;
1697 bio->bi_end_io = scrub_bio_wait_endio;
1698
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001699 ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001700 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001701 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001702 if (ret)
1703 return ret;
1704
1705 wait_for_completion(&done.event);
1706 if (done.error)
1707 return -EIO;
1708
1709 return 0;
1710}
1711
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001712/*
1713 * this function will check the on disk data for checksum errors, header
1714 * errors and read I/O errors. If any I/O errors happen, the exact pages
1715 * which are errored are marked as being bad. The goal is to enable scrub
1716 * to take those pages that are not errored from all the mirrors so that
1717 * the pages that are errored in the just handled mirror can be repaired.
1718 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001719static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001720 struct scrub_block *sblock,
1721 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001722{
1723 int page_num;
1724
1725 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001726
1727 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1728 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001729 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001730
Stefan Behrens442a4f62012-05-25 16:06:08 +02001731 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001732 page->io_error = 1;
1733 sblock->no_io_error_seen = 0;
1734 continue;
1735 }
1736
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001737 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001738 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001739 if (!bio) {
1740 page->io_error = 1;
1741 sblock->no_io_error_seen = 0;
1742 continue;
1743 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001744 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001745
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001746 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001747 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001748 if (scrub_submit_raid56_bio_wait(fs_info, bio, page)) {
1749 page->io_error = 1;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001750 sblock->no_io_error_seen = 0;
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001751 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001752 } else {
1753 bio->bi_iter.bi_sector = page->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001754 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001755
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001756 if (btrfsic_submit_bio_wait(bio)) {
1757 page->io_error = 1;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001758 sblock->no_io_error_seen = 0;
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001759 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001760 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001761
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001762 bio_put(bio);
1763 }
1764
1765 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001766 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001767}
1768
Miao Xie17a9be22014-07-24 11:37:08 +08001769static inline int scrub_check_fsid(u8 fsid[],
1770 struct scrub_page *spage)
1771{
1772 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1773 int ret;
1774
1775 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1776 return !ret;
1777}
1778
Zhao Leiba7cf982015-08-24 21:18:02 +08001779static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001780{
Zhao Leiba7cf982015-08-24 21:18:02 +08001781 sblock->header_error = 0;
1782 sblock->checksum_error = 0;
1783 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001784
Zhao Leiba7cf982015-08-24 21:18:02 +08001785 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1786 scrub_checksum_data(sblock);
1787 else
1788 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001789}
1790
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001791static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001792 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001793{
1794 int page_num;
1795 int ret = 0;
1796
1797 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1798 int ret_sub;
1799
1800 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1801 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001802 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001803 if (ret_sub)
1804 ret = ret_sub;
1805 }
1806
1807 return ret;
1808}
1809
1810static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1811 struct scrub_block *sblock_good,
1812 int page_num, int force_write)
1813{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001814 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1815 struct scrub_page *page_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001816 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001817
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001818 BUG_ON(page_bad->page == NULL);
1819 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001820 if (force_write || sblock_bad->header_error ||
1821 sblock_bad->checksum_error || page_bad->io_error) {
1822 struct bio *bio;
1823 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001824
Stefan Behrensff023aa2012-11-06 11:43:11 +01001825 if (!page_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001826 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001827 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001828 return -EIO;
1829 }
1830
Chris Mason9be33952013-05-17 18:30:14 -04001831 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001832 if (!bio)
1833 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001834 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001835 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001836 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001837
1838 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1839 if (PAGE_SIZE != ret) {
1840 bio_put(bio);
1841 return -EIO;
1842 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001843
Mike Christie4e49ea42016-06-05 14:31:41 -05001844 if (btrfsic_submit_bio_wait(bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001845 btrfs_dev_stat_inc_and_print(page_bad->dev,
1846 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001847 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 &fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001849 bio_put(bio);
1850 return -EIO;
1851 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001852 bio_put(bio);
1853 }
1854
1855 return 0;
1856}
1857
Stefan Behrensff023aa2012-11-06 11:43:11 +01001858static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1859{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001860 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001861 int page_num;
1862
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001863 /*
1864 * This block is used for the check of the parity on the source device,
1865 * so the data needn't be written into the destination device.
1866 */
1867 if (sblock->sparity)
1868 return;
1869
Stefan Behrensff023aa2012-11-06 11:43:11 +01001870 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1871 int ret;
1872
1873 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1874 if (ret)
1875 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001876 &fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001877 }
1878}
1879
1880static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1881 int page_num)
1882{
1883 struct scrub_page *spage = sblock->pagev[page_num];
1884
1885 BUG_ON(spage->page == NULL);
1886 if (spage->io_error) {
1887 void *mapped_buffer = kmap_atomic(spage->page);
1888
David Sterba619a9742017-03-29 20:48:44 +02001889 clear_page(mapped_buffer);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001890 flush_dcache_page(spage->page);
1891 kunmap_atomic(mapped_buffer);
1892 }
1893 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1894}
1895
1896static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1897 struct scrub_page *spage)
1898{
1899 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1900 struct scrub_bio *sbio;
1901 int ret;
1902
1903 mutex_lock(&wr_ctx->wr_lock);
1904again:
1905 if (!wr_ctx->wr_curr_bio) {
1906 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001907 GFP_KERNEL);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001908 if (!wr_ctx->wr_curr_bio) {
1909 mutex_unlock(&wr_ctx->wr_lock);
1910 return -ENOMEM;
1911 }
1912 wr_ctx->wr_curr_bio->sctx = sctx;
1913 wr_ctx->wr_curr_bio->page_count = 0;
1914 }
1915 sbio = wr_ctx->wr_curr_bio;
1916 if (sbio->page_count == 0) {
1917 struct bio *bio;
1918
1919 sbio->physical = spage->physical_for_dev_replace;
1920 sbio->logical = spage->logical;
1921 sbio->dev = wr_ctx->tgtdev;
1922 bio = sbio->bio;
1923 if (!bio) {
David Sterba58c4e172016-02-11 10:49:42 +01001924 bio = btrfs_io_bio_alloc(GFP_KERNEL,
1925 wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001926 if (!bio) {
1927 mutex_unlock(&wr_ctx->wr_lock);
1928 return -ENOMEM;
1929 }
1930 sbio->bio = bio;
1931 }
1932
1933 bio->bi_private = sbio;
1934 bio->bi_end_io = scrub_wr_bio_end_io;
1935 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001936 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001937 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001938 sbio->err = 0;
1939 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1940 spage->physical_for_dev_replace ||
1941 sbio->logical + sbio->page_count * PAGE_SIZE !=
1942 spage->logical) {
1943 scrub_wr_submit(sctx);
1944 goto again;
1945 }
1946
1947 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1948 if (ret != PAGE_SIZE) {
1949 if (sbio->page_count < 1) {
1950 bio_put(sbio->bio);
1951 sbio->bio = NULL;
1952 mutex_unlock(&wr_ctx->wr_lock);
1953 return -EIO;
1954 }
1955 scrub_wr_submit(sctx);
1956 goto again;
1957 }
1958
1959 sbio->pagev[sbio->page_count] = spage;
1960 scrub_page_get(spage);
1961 sbio->page_count++;
1962 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1963 scrub_wr_submit(sctx);
1964 mutex_unlock(&wr_ctx->wr_lock);
1965
1966 return 0;
1967}
1968
1969static void scrub_wr_submit(struct scrub_ctx *sctx)
1970{
1971 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1972 struct scrub_bio *sbio;
1973
1974 if (!wr_ctx->wr_curr_bio)
1975 return;
1976
1977 sbio = wr_ctx->wr_curr_bio;
1978 wr_ctx->wr_curr_bio = NULL;
1979 WARN_ON(!sbio->bio->bi_bdev);
1980 scrub_pending_bio_inc(sctx);
1981 /* process all writes in a single worker thread. Then the block layer
1982 * orders the requests before sending them to the driver which
1983 * doubled the write performance on spinning disks when measured
1984 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001985 btrfsic_submit_bio(sbio->bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001986}
1987
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001988static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001989{
1990 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001991 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001992
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001993 sbio->err = bio->bi_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001994 sbio->bio = bio;
1995
Liu Bo9e0af232014-08-15 23:36:53 +08001996 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1997 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001998 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001999}
2000
2001static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
2002{
2003 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
2004 struct scrub_ctx *sctx = sbio->sctx;
2005 int i;
2006
2007 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
2008 if (sbio->err) {
2009 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002010 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002011
2012 for (i = 0; i < sbio->page_count; i++) {
2013 struct scrub_page *spage = sbio->pagev[i];
2014
2015 spage->io_error = 1;
2016 btrfs_dev_replace_stats_inc(&dev_replace->
2017 num_write_errors);
2018 }
2019 }
2020
2021 for (i = 0; i < sbio->page_count; i++)
2022 scrub_page_put(sbio->pagev[i]);
2023
2024 bio_put(sbio->bio);
2025 kfree(sbio);
2026 scrub_pending_bio_dec(sctx);
2027}
2028
2029static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002030{
2031 u64 flags;
2032 int ret;
2033
Zhao Leiba7cf982015-08-24 21:18:02 +08002034 /*
2035 * No need to initialize these stats currently,
2036 * because this function only use return value
2037 * instead of these stats value.
2038 *
2039 * Todo:
2040 * always use stats
2041 */
2042 sblock->header_error = 0;
2043 sblock->generation_error = 0;
2044 sblock->checksum_error = 0;
2045
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002046 WARN_ON(sblock->page_count < 1);
2047 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002048 ret = 0;
2049 if (flags & BTRFS_EXTENT_FLAG_DATA)
2050 ret = scrub_checksum_data(sblock);
2051 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
2052 ret = scrub_checksum_tree_block(sblock);
2053 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
2054 (void)scrub_checksum_super(sblock);
2055 else
2056 WARN_ON(1);
2057 if (ret)
2058 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002059
2060 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002061}
2062
2063static int scrub_checksum_data(struct scrub_block *sblock)
2064{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002065 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002066 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002067 u8 *on_disk_csum;
2068 struct page *page;
2069 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01002070 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002071 u64 len;
2072 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002073
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002074 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002075 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002076 return 0;
2077
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002078 on_disk_csum = sblock->pagev[0]->csum;
2079 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002080 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002081
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002082 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002083 index = 0;
2084 for (;;) {
2085 u64 l = min_t(u64, len, PAGE_SIZE);
2086
Liu Bob0496682013-03-14 14:57:45 +00002087 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002088 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002089 len -= l;
2090 if (len == 0)
2091 break;
2092 index++;
2093 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002094 BUG_ON(!sblock->pagev[index]->page);
2095 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002096 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002097 }
2098
Arne Jansena2de7332011-03-08 14:14:00 +01002099 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002100 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002101 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002102
Zhao Leiba7cf982015-08-24 21:18:02 +08002103 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002104}
2105
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002106static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002107{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002108 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002109 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002110 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002111 u8 calculated_csum[BTRFS_CSUM_SIZE];
2112 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2113 struct page *page;
2114 void *mapped_buffer;
2115 u64 mapped_size;
2116 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002117 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002118 u64 len;
2119 int index;
2120
2121 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002122 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002123 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002124 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002125 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002126
2127 /*
2128 * we don't use the getter functions here, as we
2129 * a) don't have an extent buffer and
2130 * b) the page is already kmapped
2131 */
Qu Wenruo3cae2102013-07-16 11:19:18 +08002132 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08002133 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002134
Zhao Leiba7cf982015-08-24 21:18:02 +08002135 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
2136 sblock->header_error = 1;
2137 sblock->generation_error = 1;
2138 }
Arne Jansena2de7332011-03-08 14:14:00 +01002139
Miao Xie17a9be22014-07-24 11:37:08 +08002140 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Zhao Leiba7cf982015-08-24 21:18:02 +08002141 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002142
2143 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
2144 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08002145 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002146
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002147 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002148 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2149 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2150 index = 0;
2151 for (;;) {
2152 u64 l = min_t(u64, len, mapped_size);
2153
Liu Bob0496682013-03-14 14:57:45 +00002154 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002155 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002156 len -= l;
2157 if (len == 0)
2158 break;
2159 index++;
2160 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002161 BUG_ON(!sblock->pagev[index]->page);
2162 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002163 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002164 mapped_size = PAGE_SIZE;
2165 p = mapped_buffer;
2166 }
2167
2168 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002169 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002170 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002171
Zhao Leiba7cf982015-08-24 21:18:02 +08002172 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002173}
2174
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002175static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002176{
2177 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002178 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002179 u8 calculated_csum[BTRFS_CSUM_SIZE];
2180 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2181 struct page *page;
2182 void *mapped_buffer;
2183 u64 mapped_size;
2184 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002185 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002186 int fail_gen = 0;
2187 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002188 u64 len;
2189 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002190
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002191 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002192 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002193 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002194 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002195 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002196
Qu Wenruo3cae2102013-07-16 11:19:18 +08002197 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002198 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002199
Qu Wenruo3cae2102013-07-16 11:19:18 +08002200 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002201 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002202
Miao Xie17a9be22014-07-24 11:37:08 +08002203 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002204 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002205
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002206 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2207 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2208 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2209 index = 0;
2210 for (;;) {
2211 u64 l = min_t(u64, len, mapped_size);
2212
Liu Bob0496682013-03-14 14:57:45 +00002213 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002214 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002215 len -= l;
2216 if (len == 0)
2217 break;
2218 index++;
2219 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002220 BUG_ON(!sblock->pagev[index]->page);
2221 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002222 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002223 mapped_size = PAGE_SIZE;
2224 p = mapped_buffer;
2225 }
2226
2227 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002228 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002229 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002230
Stefan Behrens442a4f62012-05-25 16:06:08 +02002231 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002232 /*
2233 * if we find an error in a super block, we just report it.
2234 * They will get written with the next transaction commit
2235 * anyway
2236 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002237 spin_lock(&sctx->stat_lock);
2238 ++sctx->stat.super_errors;
2239 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002240 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002241 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002242 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2243 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002244 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002245 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002246 }
2247
Stefan Behrens442a4f62012-05-25 16:06:08 +02002248 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002249}
2250
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002251static void scrub_block_get(struct scrub_block *sblock)
2252{
Elena Reshetova186debd2017-03-03 10:55:23 +02002253 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002254}
2255
2256static void scrub_block_put(struct scrub_block *sblock)
2257{
Elena Reshetova186debd2017-03-03 10:55:23 +02002258 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002259 int i;
2260
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002261 if (sblock->sparity)
2262 scrub_parity_put(sblock->sparity);
2263
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002264 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002265 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002266 kfree(sblock);
2267 }
2268}
2269
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002270static void scrub_page_get(struct scrub_page *spage)
2271{
Zhao Lei57019342015-01-20 15:11:45 +08002272 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002273}
2274
2275static void scrub_page_put(struct scrub_page *spage)
2276{
Zhao Lei57019342015-01-20 15:11:45 +08002277 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002278 if (spage->page)
2279 __free_page(spage->page);
2280 kfree(spage);
2281 }
2282}
2283
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002284static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002285{
2286 struct scrub_bio *sbio;
2287
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002288 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002289 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002290
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002291 sbio = sctx->bios[sctx->curr];
2292 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002293 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002294 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002295}
2296
Stefan Behrensff023aa2012-11-06 11:43:11 +01002297static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2298 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002299{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002300 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002301 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002302 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002303
2304again:
2305 /*
2306 * grab a fresh bio or wait for one to become available
2307 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002308 while (sctx->curr == -1) {
2309 spin_lock(&sctx->list_lock);
2310 sctx->curr = sctx->first_free;
2311 if (sctx->curr != -1) {
2312 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2313 sctx->bios[sctx->curr]->next_free = -1;
2314 sctx->bios[sctx->curr]->page_count = 0;
2315 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002316 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002317 spin_unlock(&sctx->list_lock);
2318 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002319 }
2320 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002321 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002322 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002323 struct bio *bio;
2324
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002325 sbio->physical = spage->physical;
2326 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002327 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002328 bio = sbio->bio;
2329 if (!bio) {
David Sterba58c4e172016-02-11 10:49:42 +01002330 bio = btrfs_io_bio_alloc(GFP_KERNEL,
2331 sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002332 if (!bio)
2333 return -ENOMEM;
2334 sbio->bio = bio;
2335 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002336
2337 bio->bi_private = sbio;
2338 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002339 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002340 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05002341 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002342 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002343 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2344 spage->physical ||
2345 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002346 spage->logical ||
2347 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002348 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002349 goto again;
2350 }
2351
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002352 sbio->pagev[sbio->page_count] = spage;
2353 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2354 if (ret != PAGE_SIZE) {
2355 if (sbio->page_count < 1) {
2356 bio_put(sbio->bio);
2357 sbio->bio = NULL;
2358 return -EIO;
2359 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002360 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002361 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002362 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002363
Stefan Behrensff023aa2012-11-06 11:43:11 +01002364 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002365 atomic_inc(&sblock->outstanding_pages);
2366 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002367 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002368 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002369
2370 return 0;
2371}
2372
Linus Torvalds22365972015-09-05 15:14:43 -07002373static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002374{
2375 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002376 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002377
Linus Torvalds22365972015-09-05 15:14:43 -07002378 if (bio->bi_error)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002379 sblock->no_io_error_seen = 0;
2380
Scott Talbert46732722016-05-09 09:14:28 -04002381 bio_put(bio);
2382
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002383 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2384}
2385
2386static void scrub_missing_raid56_worker(struct btrfs_work *work)
2387{
2388 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2389 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002390 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002391 u64 logical;
2392 struct btrfs_device *dev;
2393
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002394 logical = sblock->pagev[0]->logical;
2395 dev = sblock->pagev[0]->dev;
2396
Zhao Leiaffe4a52015-08-24 21:32:06 +08002397 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002398 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002399
2400 if (!sblock->no_io_error_seen) {
2401 spin_lock(&sctx->stat_lock);
2402 sctx->stat.read_errors++;
2403 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002404 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002405 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002406 logical, rcu_str_deref(dev->name));
2407 } else if (sblock->header_error || sblock->checksum_error) {
2408 spin_lock(&sctx->stat_lock);
2409 sctx->stat.uncorrectable_errors++;
2410 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002411 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002412 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002413 logical, rcu_str_deref(dev->name));
2414 } else {
2415 scrub_write_block_to_dev_replace(sblock);
2416 }
2417
2418 scrub_block_put(sblock);
2419
2420 if (sctx->is_dev_replace &&
2421 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2422 mutex_lock(&sctx->wr_ctx.wr_lock);
2423 scrub_wr_submit(sctx);
2424 mutex_unlock(&sctx->wr_ctx.wr_lock);
2425 }
2426
2427 scrub_pending_bio_dec(sctx);
2428}
2429
2430static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2431{
2432 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002433 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002434 u64 length = sblock->page_count * PAGE_SIZE;
2435 u64 logical = sblock->pagev[0]->logical;
Zhao Leif1fee652016-05-17 17:37:38 +08002436 struct btrfs_bio *bbio = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002437 struct bio *bio;
2438 struct btrfs_raid_bio *rbio;
2439 int ret;
2440 int i;
2441
Qu Wenruoae6529c2017-03-29 09:33:21 +08002442 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002443 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
David Sterba825ad4c2017-03-28 14:45:22 +02002444 &length, &bbio);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002445 if (ret || !bbio || !bbio->raid_map)
2446 goto bbio_out;
2447
2448 if (WARN_ON(!sctx->is_dev_replace ||
2449 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2450 /*
2451 * We shouldn't be scrubbing a missing device. Even for dev
2452 * replace, we should only get here for RAID 5/6. We either
2453 * managed to mount something with no mirrors remaining or
2454 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2455 */
2456 goto bbio_out;
2457 }
2458
2459 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
2460 if (!bio)
2461 goto bbio_out;
2462
2463 bio->bi_iter.bi_sector = logical >> 9;
2464 bio->bi_private = sblock;
2465 bio->bi_end_io = scrub_missing_raid56_end_io;
2466
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002467 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002468 if (!rbio)
2469 goto rbio_out;
2470
2471 for (i = 0; i < sblock->page_count; i++) {
2472 struct scrub_page *spage = sblock->pagev[i];
2473
2474 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2475 }
2476
2477 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2478 scrub_missing_raid56_worker, NULL, NULL);
2479 scrub_block_get(sblock);
2480 scrub_pending_bio_inc(sctx);
2481 raid56_submit_missing_rbio(rbio);
2482 return;
2483
2484rbio_out:
2485 bio_put(bio);
2486bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002487 btrfs_bio_counter_dec(fs_info);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002488 btrfs_put_bbio(bbio);
2489 spin_lock(&sctx->stat_lock);
2490 sctx->stat.malloc_errors++;
2491 spin_unlock(&sctx->stat_lock);
2492}
2493
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002494static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002495 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002496 u64 gen, int mirror_num, u8 *csum, int force,
2497 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002498{
2499 struct scrub_block *sblock;
2500 int index;
2501
David Sterba58c4e172016-02-11 10:49:42 +01002502 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002503 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002504 spin_lock(&sctx->stat_lock);
2505 sctx->stat.malloc_errors++;
2506 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002507 return -ENOMEM;
2508 }
2509
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002510 /* one ref inside this function, plus one for each page added to
2511 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002512 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002513 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002514 sblock->no_io_error_seen = 1;
2515
2516 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002517 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002518 u64 l = min_t(u64, len, PAGE_SIZE);
2519
David Sterba58c4e172016-02-11 10:49:42 +01002520 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002521 if (!spage) {
2522leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002523 spin_lock(&sctx->stat_lock);
2524 sctx->stat.malloc_errors++;
2525 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002526 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002527 return -ENOMEM;
2528 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002529 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2530 scrub_page_get(spage);
2531 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002532 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002533 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002534 spage->flags = flags;
2535 spage->generation = gen;
2536 spage->logical = logical;
2537 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002538 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002539 spage->mirror_num = mirror_num;
2540 if (csum) {
2541 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002542 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002543 } else {
2544 spage->have_csum = 0;
2545 }
2546 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002547 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002548 if (!spage->page)
2549 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002550 len -= l;
2551 logical += l;
2552 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002553 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002554 }
2555
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002556 WARN_ON(sblock->page_count == 0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002557 if (dev->missing) {
2558 /*
2559 * This case should only be hit for RAID 5/6 device replace. See
2560 * the comment in scrub_missing_raid56_pages() for details.
2561 */
2562 scrub_missing_raid56_pages(sblock);
2563 } else {
2564 for (index = 0; index < sblock->page_count; index++) {
2565 struct scrub_page *spage = sblock->pagev[index];
2566 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002567
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002568 ret = scrub_add_page_to_rd_bio(sctx, spage);
2569 if (ret) {
2570 scrub_block_put(sblock);
2571 return ret;
2572 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002573 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002574
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002575 if (force)
2576 scrub_submit(sctx);
2577 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002578
2579 /* last one frees, either here or in bio completion for last page */
2580 scrub_block_put(sblock);
2581 return 0;
2582}
2583
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002584static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002585{
2586 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002587 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002588
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002589 sbio->err = bio->bi_error;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002590 sbio->bio = bio;
2591
Qu Wenruo0339ef22014-02-28 10:46:17 +08002592 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002593}
2594
2595static void scrub_bio_end_io_worker(struct btrfs_work *work)
2596{
2597 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002598 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002599 int i;
2600
Stefan Behrensff023aa2012-11-06 11:43:11 +01002601 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002602 if (sbio->err) {
2603 for (i = 0; i < sbio->page_count; i++) {
2604 struct scrub_page *spage = sbio->pagev[i];
2605
2606 spage->io_error = 1;
2607 spage->sblock->no_io_error_seen = 0;
2608 }
2609 }
2610
2611 /* now complete the scrub_block items that have all pages completed */
2612 for (i = 0; i < sbio->page_count; i++) {
2613 struct scrub_page *spage = sbio->pagev[i];
2614 struct scrub_block *sblock = spage->sblock;
2615
2616 if (atomic_dec_and_test(&sblock->outstanding_pages))
2617 scrub_block_complete(sblock);
2618 scrub_block_put(sblock);
2619 }
2620
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002621 bio_put(sbio->bio);
2622 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002623 spin_lock(&sctx->list_lock);
2624 sbio->next_free = sctx->first_free;
2625 sctx->first_free = sbio->index;
2626 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002627
2628 if (sctx->is_dev_replace &&
2629 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2630 mutex_lock(&sctx->wr_ctx.wr_lock);
2631 scrub_wr_submit(sctx);
2632 mutex_unlock(&sctx->wr_ctx.wr_lock);
2633 }
2634
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002635 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002636}
2637
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002638static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2639 unsigned long *bitmap,
2640 u64 start, u64 len)
2641{
Liu Bo972d7212017-04-03 13:45:33 -07002642 u64 offset;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002643 int nsectors;
Jeff Mahoneyda170662016-06-15 09:22:56 -04002644 int sectorsize = sparity->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002645
2646 if (len >= sparity->stripe_len) {
2647 bitmap_set(bitmap, 0, sparity->nsectors);
2648 return;
2649 }
2650
2651 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002652 start = div64_u64_rem(start, sparity->stripe_len, &offset);
2653 offset = div_u64(offset, sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002654 nsectors = (int)len / sectorsize;
2655
2656 if (offset + nsectors <= sparity->nsectors) {
2657 bitmap_set(bitmap, offset, nsectors);
2658 return;
2659 }
2660
2661 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2662 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2663}
2664
2665static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2666 u64 start, u64 len)
2667{
2668 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2669}
2670
2671static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2672 u64 start, u64 len)
2673{
2674 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2675}
2676
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002677static void scrub_block_complete(struct scrub_block *sblock)
2678{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002679 int corrupted = 0;
2680
Stefan Behrensff023aa2012-11-06 11:43:11 +01002681 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002682 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002683 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002684 } else {
2685 /*
2686 * if has checksum error, write via repair mechanism in
2687 * dev replace case, otherwise write here in dev replace
2688 * case.
2689 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002690 corrupted = scrub_checksum(sblock);
2691 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002692 scrub_write_block_to_dev_replace(sblock);
2693 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002694
2695 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2696 u64 start = sblock->pagev[0]->logical;
2697 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2698 PAGE_SIZE;
2699
2700 scrub_parity_mark_sectors_error(sblock->sparity,
2701 start, end - start);
2702 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002703}
2704
Zhao Lei3b5753e2015-08-24 22:03:02 +08002705static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002706{
2707 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002708 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002709 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002710
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002711 while (!list_empty(&sctx->csum_list)) {
2712 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002713 struct btrfs_ordered_sum, list);
2714 if (sum->bytenr > logical)
2715 return 0;
2716 if (sum->bytenr + sum->len > logical)
2717 break;
2718
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002719 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002720 list_del(&sum->list);
2721 kfree(sum);
2722 sum = NULL;
2723 }
2724 if (!sum)
2725 return 0;
2726
Miao Xief51a4a12013-06-19 10:36:09 +08002727 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002728 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002729 memcpy(csum, sum->sums + index, sctx->csum_size);
2730 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002731 list_del(&sum->list);
2732 kfree(sum);
2733 }
Miao Xief51a4a12013-06-19 10:36:09 +08002734 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002735}
2736
2737/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002738static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002739 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002740 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002741{
2742 int ret;
2743 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002744 u32 blocksize;
2745
2746 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002747 blocksize = sctx->sectorsize;
2748 spin_lock(&sctx->stat_lock);
2749 sctx->stat.data_extents_scrubbed++;
2750 sctx->stat.data_bytes_scrubbed += len;
2751 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002752 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002753 blocksize = sctx->nodesize;
2754 spin_lock(&sctx->stat_lock);
2755 sctx->stat.tree_extents_scrubbed++;
2756 sctx->stat.tree_bytes_scrubbed += len;
2757 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002758 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002759 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002760 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002761 }
Arne Jansena2de7332011-03-08 14:14:00 +01002762
2763 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002764 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002765 int have_csum = 0;
2766
2767 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2768 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002769 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002770 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002771 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002772 if (sctx->is_dev_replace && !have_csum) {
2773 ret = copy_nocow_pages(sctx, logical, l,
2774 mirror_num,
2775 physical_for_dev_replace);
2776 goto behind_scrub_pages;
2777 }
Arne Jansena2de7332011-03-08 14:14:00 +01002778 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002779 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002780 mirror_num, have_csum ? csum : NULL, 0,
2781 physical_for_dev_replace);
2782behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002783 if (ret)
2784 return ret;
2785 len -= l;
2786 logical += l;
2787 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002788 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002789 }
2790 return 0;
2791}
2792
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002793static int scrub_pages_for_parity(struct scrub_parity *sparity,
2794 u64 logical, u64 len,
2795 u64 physical, struct btrfs_device *dev,
2796 u64 flags, u64 gen, int mirror_num, u8 *csum)
2797{
2798 struct scrub_ctx *sctx = sparity->sctx;
2799 struct scrub_block *sblock;
2800 int index;
2801
David Sterba58c4e172016-02-11 10:49:42 +01002802 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002803 if (!sblock) {
2804 spin_lock(&sctx->stat_lock);
2805 sctx->stat.malloc_errors++;
2806 spin_unlock(&sctx->stat_lock);
2807 return -ENOMEM;
2808 }
2809
2810 /* one ref inside this function, plus one for each page added to
2811 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002812 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002813 sblock->sctx = sctx;
2814 sblock->no_io_error_seen = 1;
2815 sblock->sparity = sparity;
2816 scrub_parity_get(sparity);
2817
2818 for (index = 0; len > 0; index++) {
2819 struct scrub_page *spage;
2820 u64 l = min_t(u64, len, PAGE_SIZE);
2821
David Sterba58c4e172016-02-11 10:49:42 +01002822 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002823 if (!spage) {
2824leave_nomem:
2825 spin_lock(&sctx->stat_lock);
2826 sctx->stat.malloc_errors++;
2827 spin_unlock(&sctx->stat_lock);
2828 scrub_block_put(sblock);
2829 return -ENOMEM;
2830 }
2831 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2832 /* For scrub block */
2833 scrub_page_get(spage);
2834 sblock->pagev[index] = spage;
2835 /* For scrub parity */
2836 scrub_page_get(spage);
2837 list_add_tail(&spage->list, &sparity->spages);
2838 spage->sblock = sblock;
2839 spage->dev = dev;
2840 spage->flags = flags;
2841 spage->generation = gen;
2842 spage->logical = logical;
2843 spage->physical = physical;
2844 spage->mirror_num = mirror_num;
2845 if (csum) {
2846 spage->have_csum = 1;
2847 memcpy(spage->csum, csum, sctx->csum_size);
2848 } else {
2849 spage->have_csum = 0;
2850 }
2851 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002852 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002853 if (!spage->page)
2854 goto leave_nomem;
2855 len -= l;
2856 logical += l;
2857 physical += l;
2858 }
2859
2860 WARN_ON(sblock->page_count == 0);
2861 for (index = 0; index < sblock->page_count; index++) {
2862 struct scrub_page *spage = sblock->pagev[index];
2863 int ret;
2864
2865 ret = scrub_add_page_to_rd_bio(sctx, spage);
2866 if (ret) {
2867 scrub_block_put(sblock);
2868 return ret;
2869 }
2870 }
2871
2872 /* last one frees, either here or in bio completion for last page */
2873 scrub_block_put(sblock);
2874 return 0;
2875}
2876
2877static int scrub_extent_for_parity(struct scrub_parity *sparity,
2878 u64 logical, u64 len,
2879 u64 physical, struct btrfs_device *dev,
2880 u64 flags, u64 gen, int mirror_num)
2881{
2882 struct scrub_ctx *sctx = sparity->sctx;
2883 int ret;
2884 u8 csum[BTRFS_CSUM_SIZE];
2885 u32 blocksize;
2886
Omar Sandoval4a770892015-06-19 11:52:52 -07002887 if (dev->missing) {
2888 scrub_parity_mark_sectors_error(sparity, logical, len);
2889 return 0;
2890 }
2891
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002892 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2893 blocksize = sctx->sectorsize;
2894 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
2895 blocksize = sctx->nodesize;
2896 } else {
2897 blocksize = sctx->sectorsize;
2898 WARN_ON(1);
2899 }
2900
2901 while (len) {
2902 u64 l = min_t(u64, len, blocksize);
2903 int have_csum = 0;
2904
2905 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2906 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002907 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002908 if (have_csum == 0)
2909 goto skip;
2910 }
2911 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2912 flags, gen, mirror_num,
2913 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002914 if (ret)
2915 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002916skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002917 len -= l;
2918 logical += l;
2919 physical += l;
2920 }
2921 return 0;
2922}
2923
Wang Shilong3b080b22014-04-01 18:01:43 +08002924/*
2925 * Given a physical address, this will calculate it's
2926 * logical offset. if this is a parity stripe, it will return
2927 * the most left data stripe's logical offset.
2928 *
2929 * return 0 if it is a data stripe, 1 means parity stripe.
2930 */
2931static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002932 struct map_lookup *map, u64 *offset,
2933 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002934{
2935 int i;
2936 int j = 0;
2937 u64 stripe_nr;
2938 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002939 u32 stripe_index;
2940 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002941
2942 last_offset = (physical - map->stripes[num].physical) *
2943 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002944 if (stripe_start)
2945 *stripe_start = last_offset;
2946
Wang Shilong3b080b22014-04-01 18:01:43 +08002947 *offset = last_offset;
2948 for (i = 0; i < nr_data_stripes(map); i++) {
2949 *offset = last_offset + i * map->stripe_len;
2950
Liu Bo42c61ab2017-04-03 13:45:24 -07002951 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01002952 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002953
2954 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002955 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002956 /* calculate which stripe this data locates */
2957 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002958 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002959 if (stripe_index == num)
2960 return 0;
2961 if (stripe_index < num)
2962 j++;
2963 }
2964 *offset = last_offset + j * map->stripe_len;
2965 return 1;
2966}
2967
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002968static void scrub_free_parity(struct scrub_parity *sparity)
2969{
2970 struct scrub_ctx *sctx = sparity->sctx;
2971 struct scrub_page *curr, *next;
2972 int nbits;
2973
2974 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2975 if (nbits) {
2976 spin_lock(&sctx->stat_lock);
2977 sctx->stat.read_errors += nbits;
2978 sctx->stat.uncorrectable_errors += nbits;
2979 spin_unlock(&sctx->stat_lock);
2980 }
2981
2982 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2983 list_del_init(&curr->list);
2984 scrub_page_put(curr);
2985 }
2986
2987 kfree(sparity);
2988}
2989
Zhao Lei20b2e302015-06-04 20:09:15 +08002990static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2991{
2992 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2993 work);
2994 struct scrub_ctx *sctx = sparity->sctx;
2995
2996 scrub_free_parity(sparity);
2997 scrub_pending_bio_dec(sctx);
2998}
2999
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003000static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003001{
3002 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003003 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003004
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003005 if (bio->bi_error)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003006 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
3007 sparity->nsectors);
3008
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003009 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08003010
3011 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
3012 scrub_parity_bio_endio_worker, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003013 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003014}
3015
3016static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
3017{
3018 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003019 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003020 struct bio *bio;
3021 struct btrfs_raid_bio *rbio;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003022 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003023 u64 length;
3024 int ret;
3025
3026 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
3027 sparity->nsectors))
3028 goto out;
3029
Zhao Leia0dd59d2015-07-21 15:42:26 +08003030 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08003031
3032 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003033 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
David Sterba825ad4c2017-03-28 14:45:22 +02003034 &length, &bbio);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003035 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003036 goto bbio_out;
3037
3038 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
3039 if (!bio)
3040 goto bbio_out;
3041
3042 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
3043 bio->bi_private = sparity;
3044 bio->bi_end_io = scrub_parity_bio_endio;
3045
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003046 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003047 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003048 sparity->dbitmap,
3049 sparity->nsectors);
3050 if (!rbio)
3051 goto rbio_out;
3052
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003053 scrub_pending_bio_inc(sctx);
3054 raid56_parity_submit_scrub_rbio(rbio);
3055 return;
3056
3057rbio_out:
3058 bio_put(bio);
3059bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08003060 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08003061 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003062 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
3063 sparity->nsectors);
3064 spin_lock(&sctx->stat_lock);
3065 sctx->stat.malloc_errors++;
3066 spin_unlock(&sctx->stat_lock);
3067out:
3068 scrub_free_parity(sparity);
3069}
3070
3071static inline int scrub_calc_parity_bitmap_len(int nsectors)
3072{
Zhao Leibfca9a62014-12-08 19:55:57 +08003073 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003074}
3075
3076static void scrub_parity_get(struct scrub_parity *sparity)
3077{
Elena Reshetova78a76452017-03-03 10:55:24 +02003078 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003079}
3080
3081static void scrub_parity_put(struct scrub_parity *sparity)
3082{
Elena Reshetova78a76452017-03-03 10:55:24 +02003083 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003084 return;
3085
3086 scrub_parity_check_and_repair(sparity);
3087}
3088
3089static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
3090 struct map_lookup *map,
3091 struct btrfs_device *sdev,
3092 struct btrfs_path *path,
3093 u64 logic_start,
3094 u64 logic_end)
3095{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003096 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003097 struct btrfs_root *root = fs_info->extent_root;
3098 struct btrfs_root *csum_root = fs_info->csum_root;
3099 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07003100 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003101 u64 flags;
3102 int ret;
3103 int slot;
3104 struct extent_buffer *l;
3105 struct btrfs_key key;
3106 u64 generation;
3107 u64 extent_logical;
3108 u64 extent_physical;
3109 u64 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07003110 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003111 struct btrfs_device *extent_dev;
3112 struct scrub_parity *sparity;
3113 int nsectors;
3114 int bitmap_len;
3115 int extent_mirror_num;
3116 int stop_loop = 0;
3117
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003118 nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003119 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
3120 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
3121 GFP_NOFS);
3122 if (!sparity) {
3123 spin_lock(&sctx->stat_lock);
3124 sctx->stat.malloc_errors++;
3125 spin_unlock(&sctx->stat_lock);
3126 return -ENOMEM;
3127 }
3128
3129 sparity->stripe_len = map->stripe_len;
3130 sparity->nsectors = nsectors;
3131 sparity->sctx = sctx;
3132 sparity->scrub_dev = sdev;
3133 sparity->logic_start = logic_start;
3134 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02003135 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003136 INIT_LIST_HEAD(&sparity->spages);
3137 sparity->dbitmap = sparity->bitmap;
3138 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
3139
3140 ret = 0;
3141 while (logic_start < logic_end) {
3142 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3143 key.type = BTRFS_METADATA_ITEM_KEY;
3144 else
3145 key.type = BTRFS_EXTENT_ITEM_KEY;
3146 key.objectid = logic_start;
3147 key.offset = (u64)-1;
3148
3149 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3150 if (ret < 0)
3151 goto out;
3152
3153 if (ret > 0) {
3154 ret = btrfs_previous_extent_item(root, path, 0);
3155 if (ret < 0)
3156 goto out;
3157 if (ret > 0) {
3158 btrfs_release_path(path);
3159 ret = btrfs_search_slot(NULL, root, &key,
3160 path, 0, 0);
3161 if (ret < 0)
3162 goto out;
3163 }
3164 }
3165
3166 stop_loop = 0;
3167 while (1) {
3168 u64 bytes;
3169
3170 l = path->nodes[0];
3171 slot = path->slots[0];
3172 if (slot >= btrfs_header_nritems(l)) {
3173 ret = btrfs_next_leaf(root, path);
3174 if (ret == 0)
3175 continue;
3176 if (ret < 0)
3177 goto out;
3178
3179 stop_loop = 1;
3180 break;
3181 }
3182 btrfs_item_key_to_cpu(l, &key, slot);
3183
Zhao Leid7cad232015-07-22 13:14:48 +08003184 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3185 key.type != BTRFS_METADATA_ITEM_KEY)
3186 goto next;
3187
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003188 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003189 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003190 else
3191 bytes = key.offset;
3192
3193 if (key.objectid + bytes <= logic_start)
3194 goto next;
3195
Zhao Leia0dd59d2015-07-21 15:42:26 +08003196 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003197 stop_loop = 1;
3198 break;
3199 }
3200
3201 while (key.objectid >= logic_start + map->stripe_len)
3202 logic_start += map->stripe_len;
3203
3204 extent = btrfs_item_ptr(l, slot,
3205 struct btrfs_extent_item);
3206 flags = btrfs_extent_flags(l, extent);
3207 generation = btrfs_extent_generation(l, extent);
3208
Zhao Leia323e812015-07-23 12:29:49 +08003209 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3210 (key.objectid < logic_start ||
3211 key.objectid + bytes >
3212 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003213 btrfs_err(fs_info,
3214 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08003215 key.objectid, logic_start);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003216 spin_lock(&sctx->stat_lock);
3217 sctx->stat.uncorrectable_errors++;
3218 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003219 goto next;
3220 }
3221again:
3222 extent_logical = key.objectid;
3223 extent_len = bytes;
3224
3225 if (extent_logical < logic_start) {
3226 extent_len -= logic_start - extent_logical;
3227 extent_logical = logic_start;
3228 }
3229
3230 if (extent_logical + extent_len >
3231 logic_start + map->stripe_len)
3232 extent_len = logic_start + map->stripe_len -
3233 extent_logical;
3234
3235 scrub_parity_mark_sectors_data(sparity, extent_logical,
3236 extent_len);
3237
Omar Sandoval4a770892015-06-19 11:52:52 -07003238 mapped_length = extent_len;
Zhao Leif1fee652016-05-17 17:37:38 +08003239 bbio = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003240 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
3241 extent_logical, &mapped_length, &bbio,
3242 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07003243 if (!ret) {
3244 if (!bbio || mapped_length < extent_len)
3245 ret = -EIO;
3246 }
3247 if (ret) {
3248 btrfs_put_bbio(bbio);
3249 goto out;
3250 }
3251 extent_physical = bbio->stripes[0].physical;
3252 extent_mirror_num = bbio->mirror_num;
3253 extent_dev = bbio->stripes[0].dev;
3254 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003255
3256 ret = btrfs_lookup_csums_range(csum_root,
3257 extent_logical,
3258 extent_logical + extent_len - 1,
3259 &sctx->csum_list, 1);
3260 if (ret)
3261 goto out;
3262
3263 ret = scrub_extent_for_parity(sparity, extent_logical,
3264 extent_len,
3265 extent_physical,
3266 extent_dev, flags,
3267 generation,
3268 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003269
3270 scrub_free_csums(sctx);
3271
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003272 if (ret)
3273 goto out;
3274
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003275 if (extent_logical + extent_len <
3276 key.objectid + bytes) {
3277 logic_start += map->stripe_len;
3278
3279 if (logic_start >= logic_end) {
3280 stop_loop = 1;
3281 break;
3282 }
3283
3284 if (logic_start < key.objectid + bytes) {
3285 cond_resched();
3286 goto again;
3287 }
3288 }
3289next:
3290 path->slots[0]++;
3291 }
3292
3293 btrfs_release_path(path);
3294
3295 if (stop_loop)
3296 break;
3297
3298 logic_start += map->stripe_len;
3299 }
3300out:
3301 if (ret < 0)
3302 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003303 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003304 scrub_parity_put(sparity);
3305 scrub_submit(sctx);
3306 mutex_lock(&sctx->wr_ctx.wr_lock);
3307 scrub_wr_submit(sctx);
3308 mutex_unlock(&sctx->wr_ctx.wr_lock);
3309
3310 btrfs_release_path(path);
3311 return ret < 0 ? ret : 0;
3312}
3313
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003314static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003315 struct map_lookup *map,
3316 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003317 int num, u64 base, u64 length,
3318 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003319{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003320 struct btrfs_path *path, *ppath;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003321 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003322 struct btrfs_root *root = fs_info->extent_root;
3323 struct btrfs_root *csum_root = fs_info->csum_root;
3324 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003325 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003326 u64 flags;
3327 int ret;
3328 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003329 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003330 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003331 u64 physical;
3332 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003333 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003334 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003335 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003336 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003337 struct reada_control *reada1;
3338 struct reada_control *reada2;
David Sterbae6c11f92016-03-24 18:00:53 +01003339 struct btrfs_key key;
Arne Jansen7a262852011-06-10 12:39:23 +02003340 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003341 u64 increment = map->stripe_len;
3342 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003343 u64 extent_logical;
3344 u64 extent_physical;
3345 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003346 u64 stripe_logical;
3347 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003348 struct btrfs_device *extent_dev;
3349 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003350 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003351
Wang Shilong3b080b22014-04-01 18:01:43 +08003352 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003353 offset = 0;
Liu Bo42c61ab2017-04-03 13:45:24 -07003354 nstripes = div64_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003355 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3356 offset = map->stripe_len * num;
3357 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003358 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003359 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3360 int factor = map->num_stripes / map->sub_stripes;
3361 offset = map->stripe_len * (num / map->sub_stripes);
3362 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003363 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003364 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3365 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003366 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003367 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3368 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003369 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003370 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003371 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003372 increment = map->stripe_len * nr_data_stripes(map);
3373 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003374 } else {
3375 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003376 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003377 }
3378
3379 path = btrfs_alloc_path();
3380 if (!path)
3381 return -ENOMEM;
3382
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003383 ppath = btrfs_alloc_path();
3384 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003385 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003386 return -ENOMEM;
3387 }
3388
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003389 /*
3390 * work on commit root. The related disk blocks are static as
3391 * long as COW is applied. This means, it is save to rewrite
3392 * them to repair disk errors without any race conditions
3393 */
Arne Jansena2de7332011-03-08 14:14:00 +01003394 path->search_commit_root = 1;
3395 path->skip_locking = 1;
3396
Gui Hecheng063c54d2015-01-09 09:39:40 +08003397 ppath->search_commit_root = 1;
3398 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003399 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003400 * trigger the readahead for extent tree csum tree and wait for
3401 * completion. During readahead, the scrub is officially paused
3402 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003403 */
3404 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003405 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003406 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003407 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003408 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003409 logic_end += base;
3410 } else {
3411 logic_end = logical + increment * nstripes;
3412 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003413 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003414 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003415 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003416
Arne Jansen7a262852011-06-10 12:39:23 +02003417 /* FIXME it might be better to start readahead at commit root */
David Sterbae6c11f92016-03-24 18:00:53 +01003418 key.objectid = logical;
3419 key.type = BTRFS_EXTENT_ITEM_KEY;
3420 key.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003421 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003422 key_end.type = BTRFS_METADATA_ITEM_KEY;
3423 key_end.offset = (u64)-1;
David Sterbae6c11f92016-03-24 18:00:53 +01003424 reada1 = btrfs_reada_add(root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003425
David Sterbae6c11f92016-03-24 18:00:53 +01003426 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3427 key.type = BTRFS_EXTENT_CSUM_KEY;
3428 key.offset = logical;
Arne Jansen7a262852011-06-10 12:39:23 +02003429 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3430 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003431 key_end.offset = logic_end;
David Sterbae6c11f92016-03-24 18:00:53 +01003432 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003433
Arne Jansen7a262852011-06-10 12:39:23 +02003434 if (!IS_ERR(reada1))
3435 btrfs_reada_wait(reada1);
3436 if (!IS_ERR(reada2))
3437 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003438
Arne Jansena2de7332011-03-08 14:14:00 +01003439
3440 /*
3441 * collect all data csums for the stripe to avoid seeking during
3442 * the scrub. This might currently (crc32) end up to be about 1MB
3443 */
Arne Jansene7786c32011-05-28 20:58:38 +00003444 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003445
Arne Jansena2de7332011-03-08 14:14:00 +01003446 /*
3447 * now find all extents for each stripe and scrub them
3448 */
Arne Jansena2de7332011-03-08 14:14:00 +01003449 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003450 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003451 /*
3452 * canceled?
3453 */
3454 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003455 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003456 ret = -ECANCELED;
3457 goto out;
3458 }
3459 /*
3460 * check to see if we have to pause
3461 */
3462 if (atomic_read(&fs_info->scrub_pause_req)) {
3463 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003464 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003465 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003466 mutex_lock(&sctx->wr_ctx.wr_lock);
3467 scrub_wr_submit(sctx);
3468 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003469 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003470 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003471 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003472 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003473 }
3474
Zhao Leif2f66a22015-07-21 12:22:29 +08003475 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3476 ret = get_raid56_logic_offset(physical, num, map,
3477 &logical,
3478 &stripe_logical);
3479 logical += base;
3480 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003481 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003482 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003483 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003484 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3485 ppath, stripe_logical,
3486 stripe_end);
3487 if (ret)
3488 goto out;
3489 goto skip;
3490 }
3491 }
3492
Wang Shilong7c76edb2014-01-12 21:38:32 +08003493 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3494 key.type = BTRFS_METADATA_ITEM_KEY;
3495 else
3496 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003497 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003498 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003499
3500 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3501 if (ret < 0)
3502 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003503
Arne Jansen8c510322011-06-03 10:09:26 +02003504 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003505 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003506 if (ret < 0)
3507 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003508 if (ret > 0) {
3509 /* there's no smaller item, so stick with the
3510 * larger one */
3511 btrfs_release_path(path);
3512 ret = btrfs_search_slot(NULL, root, &key,
3513 path, 0, 0);
3514 if (ret < 0)
3515 goto out;
3516 }
Arne Jansena2de7332011-03-08 14:14:00 +01003517 }
3518
Liu Bo625f1c8d2013-04-27 02:56:57 +00003519 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003520 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003521 u64 bytes;
3522
Arne Jansena2de7332011-03-08 14:14:00 +01003523 l = path->nodes[0];
3524 slot = path->slots[0];
3525 if (slot >= btrfs_header_nritems(l)) {
3526 ret = btrfs_next_leaf(root, path);
3527 if (ret == 0)
3528 continue;
3529 if (ret < 0)
3530 goto out;
3531
Liu Bo625f1c8d2013-04-27 02:56:57 +00003532 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003533 break;
3534 }
3535 btrfs_item_key_to_cpu(l, &key, slot);
3536
Zhao Leid7cad232015-07-22 13:14:48 +08003537 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3538 key.type != BTRFS_METADATA_ITEM_KEY)
3539 goto next;
3540
Josef Bacik3173a182013-03-07 14:22:04 -05003541 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003542 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003543 else
3544 bytes = key.offset;
3545
3546 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003547 goto next;
3548
Liu Bo625f1c8d2013-04-27 02:56:57 +00003549 if (key.objectid >= logical + map->stripe_len) {
3550 /* out of this device extent */
3551 if (key.objectid >= logic_end)
3552 stop_loop = 1;
3553 break;
3554 }
Arne Jansena2de7332011-03-08 14:14:00 +01003555
3556 extent = btrfs_item_ptr(l, slot,
3557 struct btrfs_extent_item);
3558 flags = btrfs_extent_flags(l, extent);
3559 generation = btrfs_extent_generation(l, extent);
3560
Zhao Leia323e812015-07-23 12:29:49 +08003561 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3562 (key.objectid < logical ||
3563 key.objectid + bytes >
3564 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003565 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003566 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003567 key.objectid, logical);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003568 spin_lock(&sctx->stat_lock);
3569 sctx->stat.uncorrectable_errors++;
3570 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003571 goto next;
3572 }
3573
Liu Bo625f1c8d2013-04-27 02:56:57 +00003574again:
3575 extent_logical = key.objectid;
3576 extent_len = bytes;
3577
Arne Jansena2de7332011-03-08 14:14:00 +01003578 /*
3579 * trim extent to this stripe
3580 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003581 if (extent_logical < logical) {
3582 extent_len -= logical - extent_logical;
3583 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003584 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003585 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003586 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003587 extent_len = logical + map->stripe_len -
3588 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003589 }
3590
Liu Bo625f1c8d2013-04-27 02:56:57 +00003591 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003592 extent_dev = scrub_dev;
3593 extent_mirror_num = mirror_num;
3594 if (is_dev_replace)
3595 scrub_remap_extent(fs_info, extent_logical,
3596 extent_len, &extent_physical,
3597 &extent_dev,
3598 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003599
Zhao Leife8cf652015-07-22 13:14:47 +08003600 ret = btrfs_lookup_csums_range(csum_root,
3601 extent_logical,
3602 extent_logical +
3603 extent_len - 1,
3604 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003605 if (ret)
3606 goto out;
3607
Liu Bo625f1c8d2013-04-27 02:56:57 +00003608 ret = scrub_extent(sctx, extent_logical, extent_len,
3609 extent_physical, extent_dev, flags,
3610 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003611 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003612
3613 scrub_free_csums(sctx);
3614
Liu Bo625f1c8d2013-04-27 02:56:57 +00003615 if (ret)
3616 goto out;
3617
3618 if (extent_logical + extent_len <
3619 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003620 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003621 /*
3622 * loop until we find next data stripe
3623 * or we have finished all stripes.
3624 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003625loop:
3626 physical += map->stripe_len;
3627 ret = get_raid56_logic_offset(physical,
3628 num, map, &logical,
3629 &stripe_logical);
3630 logical += base;
3631
3632 if (ret && physical < physical_end) {
3633 stripe_logical += base;
3634 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003635 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003636 ret = scrub_raid56_parity(sctx,
3637 map, scrub_dev, ppath,
3638 stripe_logical,
3639 stripe_end);
3640 if (ret)
3641 goto out;
3642 goto loop;
3643 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003644 } else {
3645 physical += map->stripe_len;
3646 logical += increment;
3647 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003648 if (logical < key.objectid + bytes) {
3649 cond_resched();
3650 goto again;
3651 }
3652
Wang Shilong3b080b22014-04-01 18:01:43 +08003653 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003654 stop_loop = 1;
3655 break;
3656 }
3657 }
Arne Jansena2de7332011-03-08 14:14:00 +01003658next:
3659 path->slots[0]++;
3660 }
Chris Mason71267332011-05-23 06:30:52 -04003661 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003662skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003663 logical += increment;
3664 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003665 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003666 if (stop_loop)
3667 sctx->stat.last_physical = map->stripes[num].physical +
3668 length;
3669 else
3670 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003671 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003672 if (stop_loop)
3673 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003674 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003675out:
Arne Jansena2de7332011-03-08 14:14:00 +01003676 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003677 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003678 mutex_lock(&sctx->wr_ctx.wr_lock);
3679 scrub_wr_submit(sctx);
3680 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003681
Arne Jansene7786c32011-05-28 20:58:38 +00003682 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003683 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003684 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003685 return ret < 0 ? ret : 0;
3686}
3687
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003688static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003689 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003690 u64 chunk_offset, u64 length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003691 u64 dev_offset,
3692 struct btrfs_block_group_cache *cache,
3693 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003694{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003695 struct btrfs_fs_info *fs_info = sctx->fs_info;
3696 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003697 struct map_lookup *map;
3698 struct extent_map *em;
3699 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003700 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003701
3702 read_lock(&map_tree->map_tree.lock);
3703 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3704 read_unlock(&map_tree->map_tree.lock);
3705
Filipe Manana020d5b72015-11-19 10:57:20 +00003706 if (!em) {
3707 /*
3708 * Might have been an unused block group deleted by the cleaner
3709 * kthread or relocation.
3710 */
3711 spin_lock(&cache->lock);
3712 if (!cache->removed)
3713 ret = -EINVAL;
3714 spin_unlock(&cache->lock);
3715
3716 return ret;
3717 }
Arne Jansena2de7332011-03-08 14:14:00 +01003718
Jeff Mahoney95617d62015-06-03 10:55:48 -04003719 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003720 if (em->start != chunk_offset)
3721 goto out;
3722
3723 if (em->len < length)
3724 goto out;
3725
3726 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003727 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003728 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003729 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003730 chunk_offset, length,
3731 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003732 if (ret)
3733 goto out;
3734 }
3735 }
3736out:
3737 free_extent_map(em);
3738
3739 return ret;
3740}
3741
3742static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003743int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003744 struct btrfs_device *scrub_dev, u64 start, u64 end,
3745 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003746{
3747 struct btrfs_dev_extent *dev_extent = NULL;
3748 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003749 struct btrfs_fs_info *fs_info = sctx->fs_info;
3750 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003751 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003752 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003753 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003754 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003755 int slot;
3756 struct extent_buffer *l;
3757 struct btrfs_key key;
3758 struct btrfs_key found_key;
3759 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003760 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003761
3762 path = btrfs_alloc_path();
3763 if (!path)
3764 return -ENOMEM;
3765
David Sterbae4058b52015-11-27 16:31:35 +01003766 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003767 path->search_commit_root = 1;
3768 path->skip_locking = 1;
3769
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003770 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003771 key.offset = 0ull;
3772 key.type = BTRFS_DEV_EXTENT_KEY;
3773
Arne Jansena2de7332011-03-08 14:14:00 +01003774 while (1) {
3775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3776 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003777 break;
3778 if (ret > 0) {
3779 if (path->slots[0] >=
3780 btrfs_header_nritems(path->nodes[0])) {
3781 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003782 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003783 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003784 if (ret > 0) {
3785 ret = 0;
3786 break;
3787 }
3788 } else {
3789 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003790 }
3791 }
Arne Jansena2de7332011-03-08 14:14:00 +01003792
3793 l = path->nodes[0];
3794 slot = path->slots[0];
3795
3796 btrfs_item_key_to_cpu(l, &found_key, slot);
3797
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003798 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003799 break;
3800
David Sterba962a2982014-06-04 18:41:45 +02003801 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003802 break;
3803
3804 if (found_key.offset >= end)
3805 break;
3806
3807 if (found_key.offset < key.offset)
3808 break;
3809
3810 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3811 length = btrfs_dev_extent_length(l, dev_extent);
3812
Qu Wenruoced96ed2014-06-19 10:42:51 +08003813 if (found_key.offset + length <= start)
3814 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003815
Arne Jansena2de7332011-03-08 14:14:00 +01003816 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3817
3818 /*
3819 * get a reference on the corresponding block group to prevent
3820 * the chunk from going away while we scrub it
3821 */
3822 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003823
3824 /* some chunks are removed but not committed to disk yet,
3825 * continue scrubbing */
3826 if (!cache)
3827 goto skip;
3828
Zhaolei55e3a602015-08-05 16:43:30 +08003829 /*
3830 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3831 * to avoid deadlock caused by:
3832 * btrfs_inc_block_group_ro()
3833 * -> btrfs_wait_for_commit()
3834 * -> btrfs_commit_transaction()
3835 * -> btrfs_scrub_pause()
3836 */
3837 scrub_pause_on(fs_info);
Jeff Mahoney5e00f192017-02-15 16:28:29 -05003838 ret = btrfs_inc_block_group_ro(fs_info, cache);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003839 if (!ret && is_dev_replace) {
3840 /*
3841 * If we are doing a device replace wait for any tasks
3842 * that started dellaloc right before we set the block
3843 * group to RO mode, as they might have just allocated
3844 * an extent from it or decided they could do a nocow
3845 * write. And if any such tasks did that, wait for their
3846 * ordered extents to complete and then commit the
3847 * current transaction, so that we can later see the new
3848 * extent items in the extent tree - the ordered extents
3849 * create delayed data references (for cow writes) when
3850 * they complete, which will be run and insert the
3851 * corresponding extent items into the extent tree when
3852 * we commit the transaction they used when running
3853 * inode.c:btrfs_finish_ordered_io(). We later use
3854 * the commit root of the extent tree to find extents
3855 * to copy from the srcdev into the tgtdev, and we don't
3856 * want to miss any new extents.
3857 */
3858 btrfs_wait_block_group_reservations(cache);
3859 btrfs_wait_nocow_writers(cache);
3860 ret = btrfs_wait_ordered_roots(fs_info, -1,
3861 cache->key.objectid,
3862 cache->key.offset);
3863 if (ret > 0) {
3864 struct btrfs_trans_handle *trans;
3865
3866 trans = btrfs_join_transaction(root);
3867 if (IS_ERR(trans))
3868 ret = PTR_ERR(trans);
3869 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003870 ret = btrfs_commit_transaction(trans);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003871 if (ret) {
3872 scrub_pause_off(fs_info);
3873 btrfs_put_block_group(cache);
3874 break;
3875 }
3876 }
3877 }
Zhaolei55e3a602015-08-05 16:43:30 +08003878 scrub_pause_off(fs_info);
Zhaolei76a8efa2015-11-17 18:46:17 +08003879
3880 if (ret == 0) {
3881 ro_set = 1;
3882 } else if (ret == -ENOSPC) {
3883 /*
3884 * btrfs_inc_block_group_ro return -ENOSPC when it
3885 * failed in creating new chunk for metadata.
3886 * It is not a problem for scrub/replace, because
3887 * metadata are always cowed, and our scrub paused
3888 * commit_transactions.
3889 */
3890 ro_set = 0;
3891 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003892 btrfs_warn(fs_info,
3893 "failed setting block group ro, ret=%d\n",
Zhaolei76a8efa2015-11-17 18:46:17 +08003894 ret);
Zhaolei55e3a602015-08-05 16:43:30 +08003895 btrfs_put_block_group(cache);
3896 break;
3897 }
3898
Filipe Manana81e87a72016-05-14 16:32:35 +01003899 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003900 dev_replace->cursor_right = found_key.offset + length;
3901 dev_replace->cursor_left = found_key.offset;
3902 dev_replace->item_needs_writeback = 1;
Filipe Manana81e87a72016-05-14 16:32:35 +01003903 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
Zhao Lei8c204c92015-08-19 15:02:40 +08003904 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003905 found_key.offset, cache, is_dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003906
3907 /*
3908 * flush, submit all pending read and write bios, afterwards
3909 * wait for them.
3910 * Note that in the dev replace case, a read request causes
3911 * write requests that are submitted in the read completion
3912 * worker. Therefore in the current situation, it is required
3913 * that all write requests are flushed, so that all read and
3914 * write requests are really completed when bios_in_flight
3915 * changes to 0.
3916 */
3917 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
3918 scrub_submit(sctx);
3919 mutex_lock(&sctx->wr_ctx.wr_lock);
3920 scrub_wr_submit(sctx);
3921 mutex_unlock(&sctx->wr_ctx.wr_lock);
3922
3923 wait_event(sctx->list_wait,
3924 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003925
3926 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003927
3928 /*
3929 * must be called before we decrease @scrub_paused.
3930 * make sure we don't block transaction commit while
3931 * we are waiting pending workers finished.
3932 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003933 wait_event(sctx->list_wait,
3934 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003935 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
3936
Zhaoleib708ce92015-08-05 16:43:29 +08003937 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003938
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003939 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
3940 dev_replace->cursor_left = dev_replace->cursor_right;
3941 dev_replace->item_needs_writeback = 1;
3942 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
3943
Zhaolei76a8efa2015-11-17 18:46:17 +08003944 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003945 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003946
Filipe Manana758f2df2015-11-19 11:45:48 +00003947 /*
3948 * We might have prevented the cleaner kthread from deleting
3949 * this block group if it was already unused because we raced
3950 * and set it to RO mode first. So add it back to the unused
3951 * list, otherwise it might not ever be deleted unless a manual
3952 * balance is triggered or it becomes used and unused again.
3953 */
3954 spin_lock(&cache->lock);
3955 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3956 btrfs_block_group_used(&cache->item) == 0) {
3957 spin_unlock(&cache->lock);
3958 spin_lock(&fs_info->unused_bgs_lock);
3959 if (list_empty(&cache->bg_list)) {
3960 btrfs_get_block_group(cache);
3961 list_add_tail(&cache->bg_list,
3962 &fs_info->unused_bgs);
3963 }
3964 spin_unlock(&fs_info->unused_bgs_lock);
3965 } else {
3966 spin_unlock(&cache->lock);
3967 }
3968
Arne Jansena2de7332011-03-08 14:14:00 +01003969 btrfs_put_block_group(cache);
3970 if (ret)
3971 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003972 if (is_dev_replace &&
3973 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003974 ret = -EIO;
3975 break;
3976 }
3977 if (sctx->stat.malloc_errors > 0) {
3978 ret = -ENOMEM;
3979 break;
3980 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003981skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003982 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003983 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003984 }
3985
Arne Jansena2de7332011-03-08 14:14:00 +01003986 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003987
Zhaolei55e3a602015-08-05 16:43:30 +08003988 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003989}
3990
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003991static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3992 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003993{
3994 int i;
3995 u64 bytenr;
3996 u64 gen;
3997 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003998 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003999
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004000 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004001 return -EIO;
4002
Miao Xie5f546062014-07-24 11:37:09 +08004003 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004004 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08004005 gen = scrub_dev->generation;
4006 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004007 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01004008
4009 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
4010 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08004011 if (bytenr + BTRFS_SUPER_INFO_SIZE >
4012 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01004013 break;
4014
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004015 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01004016 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004017 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01004018 if (ret)
4019 return ret;
4020 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004021 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004022
4023 return 0;
4024}
4025
4026/*
4027 * get a reference count on fs_info->scrub_workers. start worker if necessary
4028 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01004029static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
4030 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004031{
David Sterba6f011052015-02-16 18:34:01 +01004032 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08004033 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01004034
Arne Jansen632dd772011-06-10 12:07:07 +02004035 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004036 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08004037 fs_info->scrub_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004038 btrfs_alloc_workqueue(fs_info, "scrub", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004039 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004040 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08004041 fs_info->scrub_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004042 btrfs_alloc_workqueue(fs_info, "scrub", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004043 max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08004044 if (!fs_info->scrub_workers)
4045 goto fail_scrub_workers;
4046
Qu Wenruo0339ef22014-02-28 10:46:17 +08004047 fs_info->scrub_wr_completion_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004048 btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004049 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004050 if (!fs_info->scrub_wr_completion_workers)
4051 goto fail_scrub_wr_completion_workers;
4052
Qu Wenruo0339ef22014-02-28 10:46:17 +08004053 fs_info->scrub_nocow_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004054 btrfs_alloc_workqueue(fs_info, "scrubnc", flags, 1, 0);
Zhao Leie82afc52015-06-12 20:36:58 +08004055 if (!fs_info->scrub_nocow_workers)
4056 goto fail_scrub_nocow_workers;
Zhao Lei20b2e302015-06-04 20:09:15 +08004057 fs_info->scrub_parity_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004058 btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
Zhao Lei20b2e302015-06-04 20:09:15 +08004059 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004060 if (!fs_info->scrub_parity_workers)
4061 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02004062 }
Arne Jansena2de7332011-03-08 14:14:00 +01004063 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08004064 return 0;
4065
4066fail_scrub_parity_workers:
4067 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
4068fail_scrub_nocow_workers:
4069 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4070fail_scrub_wr_completion_workers:
4071 btrfs_destroy_workqueue(fs_info->scrub_workers);
4072fail_scrub_workers:
4073 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01004074}
4075
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004076static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004077{
Stefan Behrensff023aa2012-11-06 11:43:11 +01004078 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08004079 btrfs_destroy_workqueue(fs_info->scrub_workers);
4080 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4081 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08004082 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004083 }
Arne Jansena2de7332011-03-08 14:14:00 +01004084 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004085}
4086
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004087int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
4088 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004089 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004090{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004091 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004092 int ret;
4093 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08004094 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01004095
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004096 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01004097 return -EINVAL;
4098
Jeff Mahoneyda170662016-06-15 09:22:56 -04004099 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004100 /*
4101 * in this case scrub is unable to calculate the checksum
4102 * the way scrub is implemented. Do not handle this
4103 * situation at all because it won't ever happen.
4104 */
Frank Holtonefe120a2013-12-20 11:37:06 -05004105 btrfs_err(fs_info,
4106 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004107 fs_info->nodesize,
4108 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004109 return -EINVAL;
4110 }
4111
Jeff Mahoneyda170662016-06-15 09:22:56 -04004112 if (fs_info->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004113 /* not supported for data w/o checksums */
Chandan Rajendra751bebbe2016-07-04 10:04:39 +05304114 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004115 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004116 fs_info->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01004117 return -EINVAL;
4118 }
4119
Jeff Mahoneyda170662016-06-15 09:22:56 -04004120 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004121 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04004122 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004123 /*
4124 * would exhaust the array bounds of pagev member in
4125 * struct scrub_block
4126 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004127 btrfs_err(fs_info,
4128 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004129 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004130 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004131 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004132 SCRUB_MAX_PAGES_PER_BLOCK);
4133 return -EINVAL;
4134 }
4135
Arne Jansena2de7332011-03-08 14:14:00 +01004136
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004137 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4138 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01004139 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004140 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004141 return -ENODEV;
4142 }
Arne Jansena2de7332011-03-08 14:14:00 +01004143
Miao Xie5d68da32014-07-24 11:37:07 +08004144 if (!is_dev_replace && !readonly && !dev->writeable) {
4145 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4146 rcu_read_lock();
4147 name = rcu_dereference(dev->name);
4148 btrfs_err(fs_info, "scrub: device %s is not writable",
4149 name->str);
4150 rcu_read_unlock();
4151 return -EROFS;
4152 }
4153
Wang Shilong3b7a0162013-10-12 02:11:12 +08004154 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01004155 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01004156 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004157 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004158 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01004159 }
4160
Liu Bo73beece2015-07-17 16:49:19 +08004161 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01004162 if (dev->scrub_device ||
4163 (!is_dev_replace &&
4164 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
Liu Bo73beece2015-07-17 16:49:19 +08004165 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004166 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004167 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004168 return -EINPROGRESS;
4169 }
Liu Bo73beece2015-07-17 16:49:19 +08004170 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Wang Shilong3b7a0162013-10-12 02:11:12 +08004171
4172 ret = scrub_workers_get(fs_info, is_dev_replace);
4173 if (ret) {
4174 mutex_unlock(&fs_info->scrub_lock);
4175 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4176 return ret;
4177 }
4178
Stefan Behrens63a212a2012-11-05 18:29:28 +01004179 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004180 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004181 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004182 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4183 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004184 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004185 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004186 sctx->readonly = readonly;
4187 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08004188 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004189
Wang Shilong3cb09292013-12-04 21:15:19 +08004190 /*
4191 * checking @scrub_pause_req here, we can avoid
4192 * race between committing transaction and scrubbing.
4193 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08004194 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004195 atomic_inc(&fs_info->scrubs_running);
4196 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004197
Stefan Behrensff023aa2012-11-06 11:43:11 +01004198 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08004199 /*
4200 * by holding device list mutex, we can
4201 * kick off writing super in log tree sync.
4202 */
Wang Shilong3cb09292013-12-04 21:15:19 +08004203 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004204 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08004205 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004206 }
Arne Jansena2de7332011-03-08 14:14:00 +01004207
4208 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004209 ret = scrub_enumerate_chunks(sctx, dev, start, end,
4210 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01004211
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004212 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004213 atomic_dec(&fs_info->scrubs_running);
4214 wake_up(&fs_info->scrub_pause_wait);
4215
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004216 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02004217
Arne Jansena2de7332011-03-08 14:14:00 +01004218 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004219 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01004220
4221 mutex_lock(&fs_info->scrub_lock);
4222 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08004223 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004224 mutex_unlock(&fs_info->scrub_lock);
4225
Filipe Mananaf55985f2015-02-09 21:14:24 +00004226 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004227
4228 return ret;
4229}
4230
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004231void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004232{
Arne Jansena2de7332011-03-08 14:14:00 +01004233 mutex_lock(&fs_info->scrub_lock);
4234 atomic_inc(&fs_info->scrub_pause_req);
4235 while (atomic_read(&fs_info->scrubs_paused) !=
4236 atomic_read(&fs_info->scrubs_running)) {
4237 mutex_unlock(&fs_info->scrub_lock);
4238 wait_event(fs_info->scrub_pause_wait,
4239 atomic_read(&fs_info->scrubs_paused) ==
4240 atomic_read(&fs_info->scrubs_running));
4241 mutex_lock(&fs_info->scrub_lock);
4242 }
4243 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004244}
4245
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004246void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004247{
Arne Jansena2de7332011-03-08 14:14:00 +01004248 atomic_dec(&fs_info->scrub_pause_req);
4249 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01004250}
4251
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004252int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004253{
Arne Jansena2de7332011-03-08 14:14:00 +01004254 mutex_lock(&fs_info->scrub_lock);
4255 if (!atomic_read(&fs_info->scrubs_running)) {
4256 mutex_unlock(&fs_info->scrub_lock);
4257 return -ENOTCONN;
4258 }
4259
4260 atomic_inc(&fs_info->scrub_cancel_req);
4261 while (atomic_read(&fs_info->scrubs_running)) {
4262 mutex_unlock(&fs_info->scrub_lock);
4263 wait_event(fs_info->scrub_pause_wait,
4264 atomic_read(&fs_info->scrubs_running) == 0);
4265 mutex_lock(&fs_info->scrub_lock);
4266 }
4267 atomic_dec(&fs_info->scrub_cancel_req);
4268 mutex_unlock(&fs_info->scrub_lock);
4269
4270 return 0;
4271}
4272
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004273int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
4274 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004275{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004276 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004277
4278 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004279 sctx = dev->scrub_device;
4280 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004281 mutex_unlock(&fs_info->scrub_lock);
4282 return -ENOTCONN;
4283 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004284 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01004285 while (dev->scrub_device) {
4286 mutex_unlock(&fs_info->scrub_lock);
4287 wait_event(fs_info->scrub_pause_wait,
4288 dev->scrub_device == NULL);
4289 mutex_lock(&fs_info->scrub_lock);
4290 }
4291 mutex_unlock(&fs_info->scrub_lock);
4292
4293 return 0;
4294}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004295
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004296int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01004297 struct btrfs_scrub_progress *progress)
4298{
4299 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004300 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004301
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004302 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4303 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004304 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004305 sctx = dev->scrub_device;
4306 if (sctx)
4307 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004308 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004309
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004310 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004311}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004312
4313static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4314 u64 extent_logical, u64 extent_len,
4315 u64 *extent_physical,
4316 struct btrfs_device **extent_dev,
4317 int *extent_mirror_num)
4318{
4319 u64 mapped_length;
4320 struct btrfs_bio *bbio = NULL;
4321 int ret;
4322
4323 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004324 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004325 &mapped_length, &bbio, 0);
4326 if (ret || !bbio || mapped_length < extent_len ||
4327 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08004328 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004329 return;
4330 }
4331
4332 *extent_physical = bbio->stripes[0].physical;
4333 *extent_mirror_num = bbio->mirror_num;
4334 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004335 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004336}
4337
Stefan Behrensff023aa2012-11-06 11:43:11 +01004338static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
4339 int mirror_num, u64 physical_for_dev_replace)
4340{
4341 struct scrub_copy_nocow_ctx *nocow_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004342 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004343
4344 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
4345 if (!nocow_ctx) {
4346 spin_lock(&sctx->stat_lock);
4347 sctx->stat.malloc_errors++;
4348 spin_unlock(&sctx->stat_lock);
4349 return -ENOMEM;
4350 }
4351
4352 scrub_pending_trans_workers_inc(sctx);
4353
4354 nocow_ctx->sctx = sctx;
4355 nocow_ctx->logical = logical;
4356 nocow_ctx->len = len;
4357 nocow_ctx->mirror_num = mirror_num;
4358 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08004359 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
4360 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04004361 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08004362 btrfs_queue_work(fs_info->scrub_nocow_workers,
4363 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004364
4365 return 0;
4366}
4367
Josef Bacik652f25a2013-09-12 16:58:28 -04004368static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
4369{
4370 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
4371 struct scrub_nocow_inode *nocow_inode;
4372
4373 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
4374 if (!nocow_inode)
4375 return -ENOMEM;
4376 nocow_inode->inum = inum;
4377 nocow_inode->offset = offset;
4378 nocow_inode->root = root;
4379 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
4380 return 0;
4381}
4382
4383#define COPY_COMPLETE 1
4384
Stefan Behrensff023aa2012-11-06 11:43:11 +01004385static void copy_nocow_pages_worker(struct btrfs_work *work)
4386{
4387 struct scrub_copy_nocow_ctx *nocow_ctx =
4388 container_of(work, struct scrub_copy_nocow_ctx, work);
4389 struct scrub_ctx *sctx = nocow_ctx->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004390 struct btrfs_fs_info *fs_info = sctx->fs_info;
4391 struct btrfs_root *root = fs_info->extent_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004392 u64 logical = nocow_ctx->logical;
4393 u64 len = nocow_ctx->len;
4394 int mirror_num = nocow_ctx->mirror_num;
4395 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4396 int ret;
4397 struct btrfs_trans_handle *trans = NULL;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004398 struct btrfs_path *path;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004399 int not_written = 0;
4400
Stefan Behrensff023aa2012-11-06 11:43:11 +01004401 path = btrfs_alloc_path();
4402 if (!path) {
4403 spin_lock(&sctx->stat_lock);
4404 sctx->stat.malloc_errors++;
4405 spin_unlock(&sctx->stat_lock);
4406 not_written = 1;
4407 goto out;
4408 }
4409
4410 trans = btrfs_join_transaction(root);
4411 if (IS_ERR(trans)) {
4412 not_written = 1;
4413 goto out;
4414 }
4415
4416 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04004417 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004418 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004419 btrfs_warn(fs_info,
4420 "iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d",
4421 logical, physical_for_dev_replace, len, mirror_num,
4422 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004423 not_written = 1;
4424 goto out;
4425 }
4426
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004427 btrfs_end_transaction(trans);
Josef Bacik652f25a2013-09-12 16:58:28 -04004428 trans = NULL;
4429 while (!list_empty(&nocow_ctx->inodes)) {
4430 struct scrub_nocow_inode *entry;
4431 entry = list_first_entry(&nocow_ctx->inodes,
4432 struct scrub_nocow_inode,
4433 list);
4434 list_del_init(&entry->list);
4435 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4436 entry->root, nocow_ctx);
4437 kfree(entry);
4438 if (ret == COPY_COMPLETE) {
4439 ret = 0;
4440 break;
4441 } else if (ret) {
4442 break;
4443 }
4444 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004445out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004446 while (!list_empty(&nocow_ctx->inodes)) {
4447 struct scrub_nocow_inode *entry;
4448 entry = list_first_entry(&nocow_ctx->inodes,
4449 struct scrub_nocow_inode,
4450 list);
4451 list_del_init(&entry->list);
4452 kfree(entry);
4453 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004454 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004455 btrfs_end_transaction(trans);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004456 if (not_written)
4457 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4458 num_uncorrectable_read_errors);
4459
4460 btrfs_free_path(path);
4461 kfree(nocow_ctx);
4462
4463 scrub_pending_trans_workers_dec(sctx);
4464}
4465
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004466static int check_extent_to_block(struct btrfs_inode *inode, u64 start, u64 len,
Gui Hecheng32159242014-11-10 15:36:08 +08004467 u64 logical)
4468{
4469 struct extent_state *cached_state = NULL;
4470 struct btrfs_ordered_extent *ordered;
4471 struct extent_io_tree *io_tree;
4472 struct extent_map *em;
4473 u64 lockstart = start, lockend = start + len - 1;
4474 int ret = 0;
4475
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004476 io_tree = &inode->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004477
David Sterbaff13db42015-12-03 14:30:40 +01004478 lock_extent_bits(io_tree, lockstart, lockend, &cached_state);
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004479 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
Gui Hecheng32159242014-11-10 15:36:08 +08004480 if (ordered) {
4481 btrfs_put_ordered_extent(ordered);
4482 ret = 1;
4483 goto out_unlock;
4484 }
4485
4486 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4487 if (IS_ERR(em)) {
4488 ret = PTR_ERR(em);
4489 goto out_unlock;
4490 }
4491
4492 /*
4493 * This extent does not actually cover the logical extent anymore,
4494 * move on to the next inode.
4495 */
4496 if (em->block_start > logical ||
4497 em->block_start + em->block_len < logical + len) {
4498 free_extent_map(em);
4499 ret = 1;
4500 goto out_unlock;
4501 }
4502 free_extent_map(em);
4503
4504out_unlock:
4505 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4506 GFP_NOFS);
4507 return ret;
4508}
4509
Josef Bacik652f25a2013-09-12 16:58:28 -04004510static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4511 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004512{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004513 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004514 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004515 struct inode *inode;
4516 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004517 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004518 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004519 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004520 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004521 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004522 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004523 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004524 int ret = 0;
4525 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004526
4527 key.objectid = root;
4528 key.type = BTRFS_ROOT_ITEM_KEY;
4529 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004530
4531 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4532
Stefan Behrensff023aa2012-11-06 11:43:11 +01004533 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004534 if (IS_ERR(local_root)) {
4535 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004536 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004537 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004538
4539 key.type = BTRFS_INODE_ITEM_KEY;
4540 key.objectid = inum;
4541 key.offset = 0;
4542 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004543 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004544 if (IS_ERR(inode))
4545 return PTR_ERR(inode);
4546
Miao Xieedd14002013-06-27 18:51:00 +08004547 /* Avoid truncate/dio/punch hole.. */
Al Viro59551022016-01-22 15:40:57 -05004548 inode_lock(inode);
Miao Xieedd14002013-06-27 18:51:00 +08004549 inode_dio_wait(inode);
4550
Stefan Behrensff023aa2012-11-06 11:43:11 +01004551 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004552 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004553 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004554
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004555 ret = check_extent_to_block(BTRFS_I(inode), offset, len,
4556 nocow_ctx_logical);
Gui Hecheng32159242014-11-10 15:36:08 +08004557 if (ret) {
4558 ret = ret > 0 ? 0 : ret;
4559 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004560 }
4561
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004562 while (len >= PAGE_SIZE) {
4563 index = offset >> PAGE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004564again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004565 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4566 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004567 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004568 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004569 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004570 }
4571
4572 if (PageUptodate(page)) {
4573 if (PageDirty(page))
4574 goto next_page;
4575 } else {
4576 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004577 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004578 btrfs_get_extent,
4579 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004580 if (err) {
4581 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004582 goto next_page;
4583 }
Miao Xieedd14002013-06-27 18:51:00 +08004584
Miao Xie26b258912013-06-27 18:50:58 +08004585 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004586 /*
4587 * If the page has been remove from the page cache,
4588 * the data on it is meaningless, because it may be
4589 * old one, the new data may be written into the new
4590 * page in the page cache.
4591 */
4592 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004593 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004594 put_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004595 goto again;
4596 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004597 if (!PageUptodate(page)) {
4598 ret = -EIO;
4599 goto next_page;
4600 }
4601 }
Gui Hecheng32159242014-11-10 15:36:08 +08004602
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004603 ret = check_extent_to_block(BTRFS_I(inode), offset, len,
Gui Hecheng32159242014-11-10 15:36:08 +08004604 nocow_ctx_logical);
4605 if (ret) {
4606 ret = ret > 0 ? 0 : ret;
4607 goto next_page;
4608 }
4609
Miao Xie826aa0a2013-06-27 18:50:59 +08004610 err = write_page_nocow(nocow_ctx->sctx,
4611 physical_for_dev_replace, page);
4612 if (err)
4613 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004614next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004615 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004616 put_page(page);
Miao Xie826aa0a2013-06-27 18:50:59 +08004617
4618 if (ret)
4619 break;
4620
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004621 offset += PAGE_SIZE;
4622 physical_for_dev_replace += PAGE_SIZE;
4623 nocow_ctx_logical += PAGE_SIZE;
4624 len -= PAGE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004625 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004626 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004627out:
Al Viro59551022016-01-22 15:40:57 -05004628 inode_unlock(inode);
Miao Xie826aa0a2013-06-27 18:50:59 +08004629 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004630 return ret;
4631}
4632
4633static int write_page_nocow(struct scrub_ctx *sctx,
4634 u64 physical_for_dev_replace, struct page *page)
4635{
4636 struct bio *bio;
4637 struct btrfs_device *dev;
4638 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004639
4640 dev = sctx->wr_ctx.tgtdev;
4641 if (!dev)
4642 return -EIO;
4643 if (!dev->bdev) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004644 btrfs_warn_rl(dev->fs_info,
David Sterba94647322015-10-08 11:01:36 +02004645 "scrub write_page_nocow(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004646 return -EIO;
4647 }
Chris Mason9be33952013-05-17 18:30:14 -04004648 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004649 if (!bio) {
4650 spin_lock(&sctx->stat_lock);
4651 sctx->stat.malloc_errors++;
4652 spin_unlock(&sctx->stat_lock);
4653 return -ENOMEM;
4654 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07004655 bio->bi_iter.bi_size = 0;
4656 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004657 bio->bi_bdev = dev->bdev;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06004658 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004659 ret = bio_add_page(bio, page, PAGE_SIZE, 0);
4660 if (ret != PAGE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004661leave_with_eio:
4662 bio_put(bio);
4663 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4664 return -EIO;
4665 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004666
Mike Christie4e49ea42016-06-05 14:31:41 -05004667 if (btrfsic_submit_bio_wait(bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004668 goto leave_with_eio;
4669
4670 bio_put(bio);
4671 return 0;
4672}