blob: 1e2dfea00b2fc3951a3dc0a7a10e708c07c3e5fc [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>
David Sterbade2491f2017-05-31 19:21:38 +020021#include <linux/sched/mm.h>
Arne Jansena2de7332011-03-08 14:14:00 +010022#include "ctree.h"
23#include "volumes.h"
24#include "disk-io.h"
25#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020026#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020027#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020028#include "extent_io.h"
Stefan Behrensff023aa2012-11-06 11:43:11 +010029#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010030#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040031#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050032#include "raid56.h"
Arne Jansena2de7332011-03-08 14:14:00 +010033
34/*
35 * This is only the first step towards a full-features scrub. It reads all
36 * extent and super block and verifies the checksums. In case a bad checksum
37 * is found or the extent cannot be read, good data will be written back if
38 * any can be found.
39 *
40 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010041 * - In case an unrepairable extent is encountered, track which files are
42 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010043 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010044 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010045 */
46
Stefan Behrensb5d67f62012-03-27 14:21:27 -040047struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010048struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010049
Stefan Behrensff023aa2012-11-06 11:43:11 +010050/*
51 * the following three values only influence the performance.
52 * The last one configures the number of parallel and outstanding I/O
53 * operations. The first two values configure an upper limit for the number
54 * of (dynamically allocated) pages that are added to a bio.
55 */
56#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
57#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
58#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010059
60/*
61 * the following value times PAGE_SIZE needs to be large enough to match the
62 * largest node/leaf/sector size that shall be supported.
63 * Values larger than BTRFS_STRIPE_LEN are not supported.
64 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040065#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010066
Miao Xieaf8e2d12014-10-23 14:42:50 +080067struct scrub_recover {
Elena Reshetova6f615012017-03-03 10:55:21 +020068 refcount_t refs;
Miao Xieaf8e2d12014-10-23 14:42:50 +080069 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +080070 u64 map_length;
71};
72
Arne Jansena2de7332011-03-08 14:14:00 +010073struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040074 struct scrub_block *sblock;
75 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020076 struct btrfs_device *dev;
Miao Xie5a6ac9e2014-11-06 17:20:58 +080077 struct list_head list;
Arne Jansena2de7332011-03-08 14:14:00 +010078 u64 flags; /* extent flags */
79 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040080 u64 logical;
81 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010082 u64 physical_for_dev_replace;
Zhao Lei57019342015-01-20 15:11:45 +080083 atomic_t refs;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040084 struct {
85 unsigned int mirror_num:8;
86 unsigned int have_csum:1;
87 unsigned int io_error:1;
88 };
Arne Jansena2de7332011-03-08 14:14:00 +010089 u8 csum[BTRFS_CSUM_SIZE];
Miao Xieaf8e2d12014-10-23 14:42:50 +080090
91 struct scrub_recover *recover;
Arne Jansena2de7332011-03-08 14:14:00 +010092};
93
94struct scrub_bio {
95 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010096 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010097 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010098 struct bio *bio;
99 int err;
100 u64 logical;
101 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100102#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
103 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
104#else
105 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
106#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400107 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +0100108 int next_free;
109 struct btrfs_work work;
110};
111
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400112struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100113 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400114 int page_count;
115 atomic_t outstanding_pages;
Elena Reshetova186debd2017-03-03 10:55:23 +0200116 refcount_t refs; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100117 struct scrub_ctx *sctx;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800118 struct scrub_parity *sparity;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400119 struct {
120 unsigned int header_error:1;
121 unsigned int checksum_error:1;
122 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200123 unsigned int generation_error:1; /* also sets header_error */
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800124
125 /* The following is for the data used to check parity */
126 /* It is for the data with checksum */
127 unsigned int data_corrected:1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400128 };
Omar Sandoval73ff61d2015-06-19 11:52:51 -0700129 struct btrfs_work work;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400130};
131
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800132/* Used for the chunks with parity stripe such RAID5/6 */
133struct scrub_parity {
134 struct scrub_ctx *sctx;
135
136 struct btrfs_device *scrub_dev;
137
138 u64 logic_start;
139
140 u64 logic_end;
141
142 int nsectors;
143
Liu Bo972d7212017-04-03 13:45:33 -0700144 u64 stripe_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800145
Elena Reshetova78a76452017-03-03 10:55:24 +0200146 refcount_t refs;
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800147
148 struct list_head spages;
149
150 /* Work of parity check and repair */
151 struct btrfs_work work;
152
153 /* Mark the parity blocks which have data */
154 unsigned long *dbitmap;
155
156 /*
157 * Mark the parity blocks which have data, but errors happen when
158 * read data or check data
159 */
160 unsigned long *ebitmap;
161
162 unsigned long bitmap[0];
163};
164
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100165struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100166 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400167 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100168 int first_free;
169 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100170 atomic_t bios_in_flight;
171 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100172 spinlock_t list_lock;
173 wait_queue_head_t list_wait;
174 u16 csum_size;
175 struct list_head csum_list;
176 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100177 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100178 int pages_per_rd_bio;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100179
180 int is_dev_replace;
David Sterba3fb99302017-05-16 19:10:32 +0200181
182 struct scrub_bio *wr_curr_bio;
183 struct mutex wr_lock;
184 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
185 atomic_t flush_all_writes;
186 struct btrfs_device *wr_tgtdev;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100187
Arne Jansena2de7332011-03-08 14:14:00 +0100188 /*
189 * statistics
190 */
191 struct btrfs_scrub_progress stat;
192 spinlock_t stat_lock;
Filipe Mananaf55985f2015-02-09 21:14:24 +0000193
194 /*
195 * Use a ref counter to avoid use-after-free issues. Scrub workers
196 * decrement bios_in_flight and workers_pending and then do a wakeup
197 * on the list_wait wait queue. We must ensure the main scrub task
198 * doesn't free the scrub context before or while the workers are
199 * doing the wakeup() call.
200 */
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200201 refcount_t refs;
Arne Jansena2de7332011-03-08 14:14:00 +0100202};
203
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200204struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100205 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100206 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200207 u64 logical;
208 struct btrfs_root *root;
209 struct btrfs_work work;
210 int mirror_num;
211};
212
Josef Bacik652f25a2013-09-12 16:58:28 -0400213struct scrub_nocow_inode {
214 u64 inum;
215 u64 offset;
216 u64 root;
217 struct list_head list;
218};
219
Stefan Behrensff023aa2012-11-06 11:43:11 +0100220struct scrub_copy_nocow_ctx {
221 struct scrub_ctx *sctx;
222 u64 logical;
223 u64 len;
224 int mirror_num;
225 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -0400226 struct list_head inodes;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100227 struct btrfs_work work;
228};
229
Jan Schmidt558540c2011-06-13 19:59:12 +0200230struct scrub_warning {
231 struct btrfs_path *path;
232 u64 extent_item_size;
Jan Schmidt558540c2011-06-13 19:59:12 +0200233 const char *errstr;
234 sector_t sector;
235 u64 logical;
236 struct btrfs_device *dev;
Jan Schmidt558540c2011-06-13 19:59:12 +0200237};
238
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800239struct full_stripe_lock {
240 struct rb_node node;
241 u64 logical;
242 u64 refs;
243 struct mutex mutex;
244};
245
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100246static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
247static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
248static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
249static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400250static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Zhao Leibe50a8d2015-01-20 15:11:42 +0800251static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100252 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100253static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +0800254 struct scrub_block *sblock,
255 int retry_failed_mirror);
Zhao Leiba7cf982015-08-24 21:18:02 +0800256static void scrub_recheck_block_checksum(struct scrub_block *sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400257static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +0800258 struct scrub_block *sblock_good);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400259static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
260 struct scrub_block *sblock_good,
261 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100262static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
263static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
264 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400265static int scrub_checksum_data(struct scrub_block *sblock);
266static int scrub_checksum_tree_block(struct scrub_block *sblock);
267static int scrub_checksum_super(struct scrub_block *sblock);
268static void scrub_block_get(struct scrub_block *sblock);
269static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100270static void scrub_page_get(struct scrub_page *spage);
271static void scrub_page_put(struct scrub_page *spage);
Miao Xie5a6ac9e2014-11-06 17:20:58 +0800272static void scrub_parity_get(struct scrub_parity *sparity);
273static void scrub_parity_put(struct scrub_parity *sparity);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100274static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
275 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100276static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100277 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100278 u64 gen, int mirror_num, u8 *csum, int force,
279 u64 physical_for_dev_replace);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200280static void scrub_bio_end_io(struct bio *bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400281static void scrub_bio_end_io_worker(struct btrfs_work *work);
282static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100283static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
284 u64 extent_logical, u64 extent_len,
285 u64 *extent_physical,
286 struct btrfs_device **extent_dev,
287 int *extent_mirror_num);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100288static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
289 struct scrub_page *spage);
290static void scrub_wr_submit(struct scrub_ctx *sctx);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200291static void scrub_wr_bio_end_io(struct bio *bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100292static void scrub_wr_bio_end_io_worker(struct btrfs_work *work);
293static int write_page_nocow(struct scrub_ctx *sctx,
294 u64 physical_for_dev_replace, struct page *page);
295static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400296 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100297static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
298 int mirror_num, u64 physical_for_dev_replace);
299static void copy_nocow_pages_worker(struct btrfs_work *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800300static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800301static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000302static void scrub_put_ctx(struct scrub_ctx *sctx);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400303
304
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100305static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
306{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200307 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100308 atomic_inc(&sctx->bios_in_flight);
309}
310
311static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
312{
313 atomic_dec(&sctx->bios_in_flight);
314 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000315 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100316}
317
Wang Shilongcb7ab022013-12-04 21:16:53 +0800318static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800319{
320 while (atomic_read(&fs_info->scrub_pause_req)) {
321 mutex_unlock(&fs_info->scrub_lock);
322 wait_event(fs_info->scrub_pause_wait,
323 atomic_read(&fs_info->scrub_pause_req) == 0);
324 mutex_lock(&fs_info->scrub_lock);
325 }
326}
327
Zhaolei0e22be82015-08-05 16:43:28 +0800328static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800329{
330 atomic_inc(&fs_info->scrubs_paused);
331 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800332}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800333
Zhaolei0e22be82015-08-05 16:43:28 +0800334static void scrub_pause_off(struct btrfs_fs_info *fs_info)
335{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800336 mutex_lock(&fs_info->scrub_lock);
337 __scrub_blocked_if_needed(fs_info);
338 atomic_dec(&fs_info->scrubs_paused);
339 mutex_unlock(&fs_info->scrub_lock);
340
341 wake_up(&fs_info->scrub_pause_wait);
342}
343
Zhaolei0e22be82015-08-05 16:43:28 +0800344static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
345{
346 scrub_pause_on(fs_info);
347 scrub_pause_off(fs_info);
348}
349
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100350/*
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800351 * Insert new full stripe lock into full stripe locks tree
352 *
353 * Return pointer to existing or newly inserted full_stripe_lock structure if
354 * everything works well.
355 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
356 *
357 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
358 * function
359 */
360static struct full_stripe_lock *insert_full_stripe_lock(
361 struct btrfs_full_stripe_locks_tree *locks_root,
362 u64 fstripe_logical)
363{
364 struct rb_node **p;
365 struct rb_node *parent = NULL;
366 struct full_stripe_lock *entry;
367 struct full_stripe_lock *ret;
368
369 WARN_ON(!mutex_is_locked(&locks_root->lock));
370
371 p = &locks_root->root.rb_node;
372 while (*p) {
373 parent = *p;
374 entry = rb_entry(parent, struct full_stripe_lock, node);
375 if (fstripe_logical < entry->logical) {
376 p = &(*p)->rb_left;
377 } else if (fstripe_logical > entry->logical) {
378 p = &(*p)->rb_right;
379 } else {
380 entry->refs++;
381 return entry;
382 }
383 }
384
385 /* Insert new lock */
386 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
387 if (!ret)
388 return ERR_PTR(-ENOMEM);
389 ret->logical = fstripe_logical;
390 ret->refs = 1;
391 mutex_init(&ret->mutex);
392
393 rb_link_node(&ret->node, parent, p);
394 rb_insert_color(&ret->node, &locks_root->root);
395 return ret;
396}
397
398/*
399 * Search for a full stripe lock of a block group
400 *
401 * Return pointer to existing full stripe lock if found
402 * Return NULL if not found
403 */
404static struct full_stripe_lock *search_full_stripe_lock(
405 struct btrfs_full_stripe_locks_tree *locks_root,
406 u64 fstripe_logical)
407{
408 struct rb_node *node;
409 struct full_stripe_lock *entry;
410
411 WARN_ON(!mutex_is_locked(&locks_root->lock));
412
413 node = locks_root->root.rb_node;
414 while (node) {
415 entry = rb_entry(node, struct full_stripe_lock, node);
416 if (fstripe_logical < entry->logical)
417 node = node->rb_left;
418 else if (fstripe_logical > entry->logical)
419 node = node->rb_right;
420 else
421 return entry;
422 }
423 return NULL;
424}
425
426/*
427 * Helper to get full stripe logical from a normal bytenr.
428 *
429 * Caller must ensure @cache is a RAID56 block group.
430 */
431static u64 get_full_stripe_logical(struct btrfs_block_group_cache *cache,
432 u64 bytenr)
433{
434 u64 ret;
435
436 /*
437 * Due to chunk item size limit, full stripe length should not be
438 * larger than U32_MAX. Just a sanity check here.
439 */
440 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
441
442 /*
443 * round_down() can only handle power of 2, while RAID56 full
444 * stripe length can be 64KiB * n, so we need to manually round down.
445 */
446 ret = div64_u64(bytenr - cache->key.objectid, cache->full_stripe_len) *
447 cache->full_stripe_len + cache->key.objectid;
448 return ret;
449}
450
451/*
452 * Lock a full stripe to avoid concurrency of recovery and read
453 *
454 * It's only used for profiles with parities (RAID5/6), for other profiles it
455 * does nothing.
456 *
457 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
458 * So caller must call unlock_full_stripe() at the same context.
459 *
460 * Return <0 if encounters error.
461 */
462static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
463 bool *locked_ret)
464{
465 struct btrfs_block_group_cache *bg_cache;
466 struct btrfs_full_stripe_locks_tree *locks_root;
467 struct full_stripe_lock *existing;
468 u64 fstripe_start;
469 int ret = 0;
470
471 *locked_ret = false;
472 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
473 if (!bg_cache) {
474 ASSERT(0);
475 return -ENOENT;
476 }
477
478 /* Profiles not based on parity don't need full stripe lock */
479 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
480 goto out;
481 locks_root = &bg_cache->full_stripe_locks_root;
482
483 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
484
485 /* Now insert the full stripe lock */
486 mutex_lock(&locks_root->lock);
487 existing = insert_full_stripe_lock(locks_root, fstripe_start);
488 mutex_unlock(&locks_root->lock);
489 if (IS_ERR(existing)) {
490 ret = PTR_ERR(existing);
491 goto out;
492 }
493 mutex_lock(&existing->mutex);
494 *locked_ret = true;
495out:
496 btrfs_put_block_group(bg_cache);
497 return ret;
498}
499
500/*
501 * Unlock a full stripe.
502 *
503 * NOTE: Caller must ensure it's the same context calling corresponding
504 * lock_full_stripe().
505 *
506 * Return 0 if we unlock full stripe without problem.
507 * Return <0 for error
508 */
509static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
510 bool locked)
511{
512 struct btrfs_block_group_cache *bg_cache;
513 struct btrfs_full_stripe_locks_tree *locks_root;
514 struct full_stripe_lock *fstripe_lock;
515 u64 fstripe_start;
516 bool freeit = false;
517 int ret = 0;
518
519 /* If we didn't acquire full stripe lock, no need to continue */
520 if (!locked)
521 return 0;
522
523 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
524 if (!bg_cache) {
525 ASSERT(0);
526 return -ENOENT;
527 }
528 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
529 goto out;
530
531 locks_root = &bg_cache->full_stripe_locks_root;
532 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
533
534 mutex_lock(&locks_root->lock);
535 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
536 /* Unpaired unlock_full_stripe() detected */
537 if (!fstripe_lock) {
538 WARN_ON(1);
539 ret = -ENOENT;
540 mutex_unlock(&locks_root->lock);
541 goto out;
542 }
543
544 if (fstripe_lock->refs == 0) {
545 WARN_ON(1);
546 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
547 fstripe_lock->logical);
548 } else {
549 fstripe_lock->refs--;
550 }
551
552 if (fstripe_lock->refs == 0) {
553 rb_erase(&fstripe_lock->node, &locks_root->root);
554 freeit = true;
555 }
556 mutex_unlock(&locks_root->lock);
557
558 mutex_unlock(&fstripe_lock->mutex);
559 if (freeit)
560 kfree(fstripe_lock);
561out:
562 btrfs_put_block_group(bg_cache);
563 return ret;
564}
565
566/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100567 * used for workers that require transaction commits (i.e., for the
568 * NOCOW case)
569 */
570static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
571{
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400572 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100573
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200574 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100575 /*
576 * increment scrubs_running to prevent cancel requests from
577 * completing as long as a worker is running. we must also
578 * increment scrubs_paused to prevent deadlocking on pause
579 * requests used for transactions commits (as the worker uses a
580 * transaction context). it is safe to regard the worker
581 * as paused for all matters practical. effectively, we only
582 * avoid cancellation requests from completing.
583 */
584 mutex_lock(&fs_info->scrub_lock);
585 atomic_inc(&fs_info->scrubs_running);
586 atomic_inc(&fs_info->scrubs_paused);
587 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800588
589 /*
590 * check if @scrubs_running=@scrubs_paused condition
591 * inside wait_event() is not an atomic operation.
592 * which means we may inc/dec @scrub_running/paused
593 * at any time. Let's wake up @scrub_pause_wait as
594 * much as we can to let commit transaction blocked less.
595 */
596 wake_up(&fs_info->scrub_pause_wait);
597
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100598 atomic_inc(&sctx->workers_pending);
599}
600
601/* used for workers that require transaction commits */
602static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
603{
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400604 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100605
606 /*
607 * see scrub_pending_trans_workers_inc() why we're pretending
608 * to be paused in the scrub counters
609 */
610 mutex_lock(&fs_info->scrub_lock);
611 atomic_dec(&fs_info->scrubs_running);
612 atomic_dec(&fs_info->scrubs_paused);
613 mutex_unlock(&fs_info->scrub_lock);
614 atomic_dec(&sctx->workers_pending);
615 wake_up(&fs_info->scrub_pause_wait);
616 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000617 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100618}
619
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100620static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100621{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100622 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100623 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100624 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100625 struct btrfs_ordered_sum, list);
626 list_del(&sum->list);
627 kfree(sum);
628 }
629}
630
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100631static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100632{
633 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100634
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100635 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100636 return;
637
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400638 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100639 if (sctx->curr != -1) {
640 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400641
642 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100643 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400644 scrub_block_put(sbio->pagev[i]->sblock);
645 }
646 bio_put(sbio->bio);
647 }
648
Stefan Behrensff023aa2012-11-06 11:43:11 +0100649 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100650 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100651
652 if (!sbio)
653 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100654 kfree(sbio);
655 }
656
David Sterba3fb99302017-05-16 19:10:32 +0200657 kfree(sctx->wr_curr_bio);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100658 scrub_free_csums(sctx);
659 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100660}
661
Filipe Mananaf55985f2015-02-09 21:14:24 +0000662static void scrub_put_ctx(struct scrub_ctx *sctx)
663{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200664 if (refcount_dec_and_test(&sctx->refs))
Filipe Mananaf55985f2015-02-09 21:14:24 +0000665 scrub_free_ctx(sctx);
666}
667
Arne Jansena2de7332011-03-08 14:14:00 +0100668static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100669struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100670{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100671 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100672 int i;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400673 struct btrfs_fs_info *fs_info = dev->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100674
David Sterba58c4e172016-02-11 10:49:42 +0100675 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100676 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100677 goto nomem;
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200678 refcount_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100679 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200680 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100681 sctx->curr = -1;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400682 sctx->fs_info = dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100683 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100684 struct scrub_bio *sbio;
685
David Sterba58c4e172016-02-11 10:49:42 +0100686 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
Arne Jansena2de7332011-03-08 14:14:00 +0100687 if (!sbio)
688 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100689 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100690
Arne Jansena2de7332011-03-08 14:14:00 +0100691 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100692 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400693 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800694 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
695 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100696
Stefan Behrensff023aa2012-11-06 11:43:11 +0100697 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100698 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200699 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100700 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100701 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100702 sctx->first_free = 0;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100703 atomic_set(&sctx->bios_in_flight, 0);
704 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100705 atomic_set(&sctx->cancel_req, 0);
706 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
707 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100708
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100709 spin_lock_init(&sctx->list_lock);
710 spin_lock_init(&sctx->stat_lock);
711 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100712
David Sterba3fb99302017-05-16 19:10:32 +0200713 WARN_ON(sctx->wr_curr_bio != NULL);
714 mutex_init(&sctx->wr_lock);
715 sctx->wr_curr_bio = NULL;
David Sterba8fcdac32017-05-16 19:10:23 +0200716 if (is_dev_replace) {
717 WARN_ON(!dev->bdev);
David Sterba3fb99302017-05-16 19:10:32 +0200718 sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
719 sctx->wr_tgtdev = dev;
720 atomic_set(&sctx->flush_all_writes, 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100721 }
David Sterba8fcdac32017-05-16 19:10:23 +0200722
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100723 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100724
725nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100726 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100727 return ERR_PTR(-ENOMEM);
728}
729
Stefan Behrensff023aa2012-11-06 11:43:11 +0100730static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
731 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200732{
733 u64 isize;
734 u32 nlink;
735 int ret;
736 int i;
David Sterbade2491f2017-05-31 19:21:38 +0200737 unsigned nofs_flag;
Jan Schmidt558540c2011-06-13 19:59:12 +0200738 struct extent_buffer *eb;
739 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100740 struct scrub_warning *swarn = warn_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400741 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200742 struct inode_fs_paths *ipath = NULL;
743 struct btrfs_root *local_root;
744 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100745 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200746
747 root_key.objectid = root;
748 root_key.type = BTRFS_ROOT_ITEM_KEY;
749 root_key.offset = (u64)-1;
750 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
751 if (IS_ERR(local_root)) {
752 ret = PTR_ERR(local_root);
753 goto err;
754 }
755
David Sterba14692cc2015-01-02 18:55:46 +0100756 /*
757 * this makes the path point to (inum INODE_ITEM ioff)
758 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100759 key.objectid = inum;
760 key.type = BTRFS_INODE_ITEM_KEY;
761 key.offset = 0;
762
763 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200764 if (ret) {
765 btrfs_release_path(swarn->path);
766 goto err;
767 }
768
769 eb = swarn->path->nodes[0];
770 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
771 struct btrfs_inode_item);
772 isize = btrfs_inode_size(eb, inode_item);
773 nlink = btrfs_inode_nlink(eb, inode_item);
774 btrfs_release_path(swarn->path);
775
David Sterbade2491f2017-05-31 19:21:38 +0200776 /*
777 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
778 * uses GFP_NOFS in this context, so we keep it consistent but it does
779 * not seem to be strictly necessary.
780 */
781 nofs_flag = memalloc_nofs_save();
Jan Schmidt558540c2011-06-13 19:59:12 +0200782 ipath = init_ipath(4096, local_root, swarn->path);
David Sterbade2491f2017-05-31 19:21:38 +0200783 memalloc_nofs_restore(nofs_flag);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300784 if (IS_ERR(ipath)) {
785 ret = PTR_ERR(ipath);
786 ipath = NULL;
787 goto err;
788 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200789 ret = paths_from_inode(inum, ipath);
790
791 if (ret < 0)
792 goto err;
793
794 /*
795 * we deliberately ignore the bit ipath might have been too small to
796 * hold all of the paths here
797 */
798 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400799 btrfs_warn_in_rcu(fs_info,
800 "%s at logical %llu on dev %s, sector %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
801 swarn->errstr, swarn->logical,
802 rcu_str_deref(swarn->dev->name),
803 (unsigned long long)swarn->sector,
804 root, inum, offset,
805 min(isize - offset, (u64)PAGE_SIZE), nlink,
806 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200807
808 free_ipath(ipath);
809 return 0;
810
811err:
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400812 btrfs_warn_in_rcu(fs_info,
813 "%s at logical %llu on dev %s, sector %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
814 swarn->errstr, swarn->logical,
815 rcu_str_deref(swarn->dev->name),
816 (unsigned long long)swarn->sector,
817 root, inum, offset, ret);
Jan Schmidt558540c2011-06-13 19:59:12 +0200818
819 free_ipath(ipath);
820 return 0;
821}
822
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400823static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200824{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100825 struct btrfs_device *dev;
826 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200827 struct btrfs_path *path;
828 struct btrfs_key found_key;
829 struct extent_buffer *eb;
830 struct btrfs_extent_item *ei;
831 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200832 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100833 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600834 u64 flags = 0;
835 u64 ref_root;
836 u32 item_size;
Dan Carpenter07c9a8e2016-03-11 11:08:56 +0300837 u8 ref_level = 0;
Liu Bo69917e42012-09-07 20:01:28 -0600838 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200839
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100840 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100841 dev = sblock->pagev[0]->dev;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400842 fs_info = sblock->sctx->fs_info;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100843
Jan Schmidt558540c2011-06-13 19:59:12 +0200844 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200845 if (!path)
846 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200847
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100848 swarn.sector = (sblock->pagev[0]->physical) >> 9;
849 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200850 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100851 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200852
Liu Bo69917e42012-09-07 20:01:28 -0600853 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
854 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200855 if (ret < 0)
856 goto out;
857
Jan Schmidt4692cf52011-12-02 14:56:41 +0100858 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200859 swarn.extent_item_size = found_key.offset;
860
861 eb = path->nodes[0];
862 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
863 item_size = btrfs_item_size_nr(eb, path->slots[0]);
864
Liu Bo69917e42012-09-07 20:01:28 -0600865 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200866 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800867 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
868 item_size, &ref_root,
869 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200870 btrfs_warn_in_rcu(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400871 "%s at logical %llu on dev %s, sector %llu: metadata %s (level %d) in tree %llu",
872 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400873 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200874 (unsigned long long)swarn.sector,
875 ref_level ? "node" : "leaf",
876 ret < 0 ? -1 : ref_level,
877 ret < 0 ? -1 : ref_root);
878 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600879 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200880 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600881 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200882 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100883 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100884 iterate_extent_inodes(fs_info, found_key.objectid,
885 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200886 scrub_print_warning_inode, &swarn);
887 }
888
889out:
890 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200891}
892
Stefan Behrensff023aa2012-11-06 11:43:11 +0100893static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200894{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200895 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200896 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100897 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200898 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200899 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200900 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200901 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000902 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200903 u64 end = offset + PAGE_SIZE - 1;
904 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000905 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200906
907 key.objectid = root;
908 key.type = BTRFS_ROOT_ITEM_KEY;
909 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000910
911 fs_info = fixup->root->fs_info;
912 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
913
914 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
915 if (IS_ERR(local_root)) {
916 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200917 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000918 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200919
920 key.type = BTRFS_INODE_ITEM_KEY;
921 key.objectid = inum;
922 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000923 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
924 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200925 if (IS_ERR(inode))
926 return PTR_ERR(inode);
927
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300928 index = offset >> PAGE_SHIFT;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200929
930 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200931 if (!page) {
932 ret = -ENOMEM;
933 goto out;
934 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200935
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200936 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200937 if (PageDirty(page)) {
938 /*
939 * we need to write the data to the defect sector. the
940 * data that was in that sector is not in memory,
941 * because the page was modified. we must not write the
942 * modified page to that sector.
943 *
944 * TODO: what could be done here: wait for the delalloc
945 * runner to write out that page (might involve
946 * COW) and see whether the sector is still
947 * referenced afterwards.
948 *
949 * For the meantime, we'll treat this error
950 * incorrectable, although there is a chance that a
951 * later scrub will find the bad sector again and that
952 * there's no dirty page in memory, then.
953 */
954 ret = -EIO;
955 goto out;
956 }
Josef Bacik6ec656b2017-05-05 11:57:14 -0400957 ret = repair_io_failure(fs_info, inum, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200958 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800959 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200960 fixup->mirror_num);
961 unlock_page(page);
962 corrected = !ret;
963 } else {
964 /*
965 * we need to get good data first. the general readpage path
966 * will call repair_io_failure for us, we just have to make
967 * sure we read the bad mirror.
968 */
969 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +0200970 EXTENT_DAMAGED);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200971 if (ret) {
972 /* set_extent_bits should give proper error */
973 WARN_ON(ret > 0);
974 if (ret > 0)
975 ret = -EFAULT;
976 goto out;
977 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200978
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200979 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
980 btrfs_get_extent,
981 fixup->mirror_num);
982 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200983
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200984 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
985 end, EXTENT_DAMAGED, 0, NULL);
986 if (!corrected)
987 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
David Sterba91166212016-04-26 23:54:39 +0200988 EXTENT_DAMAGED);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200989 }
990
991out:
992 if (page)
993 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200994
995 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200996
997 if (ret < 0)
998 return ret;
999
1000 if (ret == 0 && corrected) {
1001 /*
1002 * we only need to call readpage for one of the inodes belonging
1003 * to this extent. so make iterate_extent_inodes stop
1004 */
1005 return 1;
1006 }
1007
1008 return -EIO;
1009}
1010
1011static void scrub_fixup_nodatasum(struct btrfs_work *work)
1012{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001014 int ret;
1015 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001016 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001017 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001018 struct btrfs_path *path;
1019 int uncorrectable = 0;
1020
1021 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001022 sctx = fixup->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001023 fs_info = fixup->root->fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001024
1025 path = btrfs_alloc_path();
1026 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001027 spin_lock(&sctx->stat_lock);
1028 ++sctx->stat.malloc_errors;
1029 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001030 uncorrectable = 1;
1031 goto out;
1032 }
1033
1034 trans = btrfs_join_transaction(fixup->root);
1035 if (IS_ERR(trans)) {
1036 uncorrectable = 1;
1037 goto out;
1038 }
1039
1040 /*
1041 * the idea is to trigger a regular read through the standard path. we
1042 * read a page from the (failed) logical address by specifying the
1043 * corresponding copynum of the failed sector. thus, that readpage is
1044 * expected to fail.
1045 * that is the point where on-the-fly error correction will kick in
1046 * (once it's finished) and rewrite the failed sector if a good copy
1047 * can be found.
1048 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001049 ret = iterate_inodes_from_logical(fixup->logical, fs_info, path,
1050 scrub_fixup_readpage, fixup);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001051 if (ret < 0) {
1052 uncorrectable = 1;
1053 goto out;
1054 }
1055 WARN_ON(ret != 1);
1056
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001057 spin_lock(&sctx->stat_lock);
1058 ++sctx->stat.corrected_errors;
1059 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001060
1061out:
1062 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001063 btrfs_end_transaction(trans);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001064 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001065 spin_lock(&sctx->stat_lock);
1066 ++sctx->stat.uncorrectable_errors;
1067 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001068 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001069 &fs_info->dev_replace.num_uncorrectable_read_errors);
1070 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02001071 "unable to fixup (nodatasum) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001072 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001073 }
1074
1075 btrfs_free_path(path);
1076 kfree(fixup);
1077
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001078 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001079}
1080
Miao Xieaf8e2d12014-10-23 14:42:50 +08001081static inline void scrub_get_recover(struct scrub_recover *recover)
1082{
Elena Reshetova6f615012017-03-03 10:55:21 +02001083 refcount_inc(&recover->refs);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001084}
1085
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001086static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
1087 struct scrub_recover *recover)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001088{
Elena Reshetova6f615012017-03-03 10:55:21 +02001089 if (refcount_dec_and_test(&recover->refs)) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001090 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08001091 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001092 kfree(recover);
1093 }
1094}
1095
Arne Jansena2de7332011-03-08 14:14:00 +01001096/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001097 * scrub_handle_errored_block gets called when either verification of the
1098 * pages failed or the bio failed to read, e.g. with EIO. In the latter
1099 * case, this function handles all pages in the bio, even though only one
1100 * may be bad.
1101 * The goal of this function is to repair the errored block by using the
1102 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +01001103 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001104static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +01001105{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001106 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001107 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001108 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01001109 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001110 u64 logical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001111 unsigned int failed_mirror_index;
1112 unsigned int is_metadata;
1113 unsigned int have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001114 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
1115 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +01001116 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001117 int mirror_index;
1118 int page_num;
1119 int success;
Qu Wenruo28d70e22017-04-14 08:35:55 +08001120 bool full_stripe_locked;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001121 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1122 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +01001123
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001124 BUG_ON(sblock_to_check->page_count < 1);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001125 fs_info = sctx->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +00001126 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
1127 /*
1128 * if we find an error in a super block, we just report it.
1129 * They will get written with the next transaction commit
1130 * anyway
1131 */
1132 spin_lock(&sctx->stat_lock);
1133 ++sctx->stat.super_errors;
1134 spin_unlock(&sctx->stat_lock);
1135 return 0;
1136 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001137 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001138 logical = sblock_to_check->pagev[0]->logical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001139 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
1140 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
1141 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001142 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001143 have_csum = sblock_to_check->pagev[0]->have_csum;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001144 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001145
Qu Wenruo28d70e22017-04-14 08:35:55 +08001146 /*
1147 * For RAID5/6, race can happen for a different device scrub thread.
1148 * For data corruption, Parity and Data threads will both try
1149 * to recovery the data.
1150 * Race can lead to doubly added csum error, or even unrecoverable
1151 * error.
1152 */
1153 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
1154 if (ret < 0) {
1155 spin_lock(&sctx->stat_lock);
1156 if (ret == -ENOMEM)
1157 sctx->stat.malloc_errors++;
1158 sctx->stat.read_errors++;
1159 sctx->stat.uncorrectable_errors++;
1160 spin_unlock(&sctx->stat_lock);
1161 return ret;
1162 }
1163
Stefan Behrensff023aa2012-11-06 11:43:11 +01001164 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
1165 sblocks_for_recheck = NULL;
1166 goto nodatasum_case;
1167 }
1168
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001169 /*
1170 * read all mirrors one after the other. This includes to
1171 * re-read the extent or metadata block that failed (that was
1172 * the cause that this fixup code is called) another time,
1173 * page by page this time in order to know which pages
1174 * caused I/O errors and which ones are good (for all mirrors).
1175 * It is the goal to handle the situation when more than one
1176 * mirror contains I/O errors, but the errors do not
1177 * overlap, i.e. the data can be repaired by selecting the
1178 * pages from those mirrors without I/O error on the
1179 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
1180 * would be that mirror #1 has an I/O error on the first page,
1181 * the second page is good, and mirror #2 has an I/O error on
1182 * the second page, but the first page is good.
1183 * Then the first page of the first mirror can be repaired by
1184 * taking the first page of the second mirror, and the
1185 * second page of the second mirror can be repaired by
1186 * copying the contents of the 2nd page of the 1st mirror.
1187 * One more note: if the pages of one mirror contain I/O
1188 * errors, the checksum cannot be verified. In order to get
1189 * the best data for repairing, the first attempt is to find
1190 * a mirror without I/O errors and with a validated checksum.
1191 * Only if this is not possible, the pages are picked from
1192 * mirrors with I/O errors without considering the checksum.
1193 * If the latter is the case, at the end, the checksum of the
1194 * repaired area is verified in order to correctly maintain
1195 * the statistics.
1196 */
1197
David Sterba31e818f2015-02-20 18:00:26 +01001198 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
1199 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001200 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001201 spin_lock(&sctx->stat_lock);
1202 sctx->stat.malloc_errors++;
1203 sctx->stat.read_errors++;
1204 sctx->stat.uncorrectable_errors++;
1205 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001206 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001207 goto out;
1208 }
1209
1210 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +08001211 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001212 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001213 spin_lock(&sctx->stat_lock);
1214 sctx->stat.read_errors++;
1215 sctx->stat.uncorrectable_errors++;
1216 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001217 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001218 goto out;
1219 }
1220 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
1221 sblock_bad = sblocks_for_recheck + failed_mirror_index;
1222
1223 /* build and submit the bios for the failed mirror, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001224 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001225
1226 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
1227 sblock_bad->no_io_error_seen) {
1228 /*
1229 * the error disappeared after reading page by page, or
1230 * the area was part of a huge bio and other parts of the
1231 * bio caused I/O errors, or the block layer merged several
1232 * read requests into one and the error is caused by a
1233 * different bio (usually one of the two latter cases is
1234 * the cause)
1235 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001236 spin_lock(&sctx->stat_lock);
1237 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001238 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001239 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001240
Stefan Behrensff023aa2012-11-06 11:43:11 +01001241 if (sctx->is_dev_replace)
1242 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001243 goto out;
1244 }
1245
1246 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001247 spin_lock(&sctx->stat_lock);
1248 sctx->stat.read_errors++;
1249 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001250 if (__ratelimit(&_rs))
1251 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001252 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001253 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001254 spin_lock(&sctx->stat_lock);
1255 sctx->stat.csum_errors++;
1256 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001257 if (__ratelimit(&_rs))
1258 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001259 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001260 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001261 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001262 spin_lock(&sctx->stat_lock);
1263 sctx->stat.verify_errors++;
1264 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001265 if (__ratelimit(&_rs))
1266 scrub_print_warning("checksum/header error",
1267 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001268 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001269 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001270 BTRFS_DEV_STAT_GENERATION_ERRS);
1271 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001272 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001273 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001274 }
1275
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001276 if (sctx->readonly) {
1277 ASSERT(!sctx->is_dev_replace);
1278 goto out;
1279 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001280
1281 if (!is_metadata && !have_csum) {
1282 struct scrub_fixup_nodatasum *fixup_nodatasum;
1283
Stefan Behrensff023aa2012-11-06 11:43:11 +01001284 WARN_ON(sctx->is_dev_replace);
1285
Zhao Leib25c94c2015-01-20 15:11:35 +08001286nodatasum_case:
1287
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001288 /*
1289 * !is_metadata and !have_csum, this means that the data
Nicholas D Steeves01327612016-05-19 21:18:45 -04001290 * might not be COWed, that it might be modified
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001291 * concurrently. The general strategy to work on the
1292 * commit root does not help in the case when COW is not
1293 * used.
1294 */
1295 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1296 if (!fixup_nodatasum)
1297 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001298 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001299 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001300 fixup_nodatasum->logical = logical;
1301 fixup_nodatasum->root = fs_info->extent_root;
1302 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001303 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001304 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1305 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001306 btrfs_queue_work(fs_info->scrub_workers,
1307 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001308 goto out;
1309 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001310
1311 /*
1312 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001313 * checksums.
1314 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001315 * errors and also does not have a checksum error.
1316 * If one is found, and if a checksum is present, the full block
1317 * that is known to contain an error is rewritten. Afterwards
1318 * the block is known to be corrected.
1319 * If a mirror is found which is completely correct, and no
1320 * checksum is present, only those pages are rewritten that had
1321 * an I/O error in the block to be repaired, since it cannot be
1322 * determined, which copy of the other pages is better (and it
1323 * could happen otherwise that a correct page would be
1324 * overwritten by a bad one).
1325 */
1326 for (mirror_index = 0;
1327 mirror_index < BTRFS_MAX_MIRRORS &&
1328 sblocks_for_recheck[mirror_index].page_count > 0;
1329 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001330 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001331
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001332 if (mirror_index == failed_mirror_index)
1333 continue;
1334 sblock_other = sblocks_for_recheck + mirror_index;
1335
1336 /* build and submit the bios, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001337 scrub_recheck_block(fs_info, sblock_other, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001338
1339 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001340 !sblock_other->checksum_error &&
1341 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001342 if (sctx->is_dev_replace) {
1343 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001344 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001345 } else {
1346 ret = scrub_repair_block_from_good_copy(
1347 sblock_bad, sblock_other);
1348 if (!ret)
1349 goto corrected_error;
1350 }
Arne Jansena2de7332011-03-08 14:14:00 +01001351 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001352 }
1353
Zhao Leib968fed2015-01-20 15:11:41 +08001354 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1355 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001356
1357 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001358 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001359 * repaired, continue by picking good copies of those pages.
1360 * Select the good pages from mirrors to rewrite bad pages from
1361 * the area to fix. Afterwards verify the checksum of the block
1362 * that is supposed to be repaired. This verification step is
1363 * only done for the purpose of statistic counting and for the
1364 * final scrub report, whether errors remain.
1365 * A perfect algorithm could make use of the checksum and try
1366 * all possible combinations of pages from the different mirrors
1367 * until the checksum verification succeeds. For example, when
1368 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1369 * of mirror #2 is readable but the final checksum test fails,
1370 * then the 2nd page of mirror #3 could be tried, whether now
Nicholas D Steeves01327612016-05-19 21:18:45 -04001371 * the final checksum succeeds. But this would be a rare
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001372 * exception and is therefore not implemented. At least it is
1373 * avoided that the good copy is overwritten.
1374 * A more useful improvement would be to pick the sectors
1375 * without I/O error based on sector sizes (512 bytes on legacy
1376 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1377 * mirror could be repaired by taking 512 byte of a different
1378 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1379 * area are unreadable.
1380 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001381 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001382 for (page_num = 0; page_num < sblock_bad->page_count;
1383 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001384 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001385 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001386
Zhao Leib968fed2015-01-20 15:11:41 +08001387 /* skip no-io-error page in scrub */
1388 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001389 continue;
1390
Zhao Leib968fed2015-01-20 15:11:41 +08001391 /* try to find no-io-error page in mirrors */
1392 if (page_bad->io_error) {
1393 for (mirror_index = 0;
1394 mirror_index < BTRFS_MAX_MIRRORS &&
1395 sblocks_for_recheck[mirror_index].page_count > 0;
1396 mirror_index++) {
1397 if (!sblocks_for_recheck[mirror_index].
1398 pagev[page_num]->io_error) {
1399 sblock_other = sblocks_for_recheck +
1400 mirror_index;
1401 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001402 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001403 }
Zhao Leib968fed2015-01-20 15:11:41 +08001404 if (!sblock_other)
1405 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001406 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001407
Zhao Leib968fed2015-01-20 15:11:41 +08001408 if (sctx->is_dev_replace) {
1409 /*
1410 * did not find a mirror to fetch the page
1411 * from. scrub_write_page_to_dev_replace()
1412 * handles this case (page->io_error), by
1413 * filling the block with zeros before
1414 * submitting the write request
1415 */
1416 if (!sblock_other)
1417 sblock_other = sblock_bad;
1418
1419 if (scrub_write_page_to_dev_replace(sblock_other,
1420 page_num) != 0) {
1421 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001422 &fs_info->dev_replace.num_write_errors);
Zhao Leib968fed2015-01-20 15:11:41 +08001423 success = 0;
1424 }
1425 } else if (sblock_other) {
1426 ret = scrub_repair_page_from_good_copy(sblock_bad,
1427 sblock_other,
1428 page_num, 0);
1429 if (0 == ret)
1430 page_bad->io_error = 0;
1431 else
1432 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001433 }
1434 }
1435
Zhao Leib968fed2015-01-20 15:11:41 +08001436 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001437 if (is_metadata || have_csum) {
1438 /*
1439 * need to verify the checksum now that all
1440 * sectors on disk are repaired (the write
1441 * request for data to be repaired is on its way).
1442 * Just be lazy and use scrub_recheck_block()
1443 * which re-reads the data before the checksum
1444 * is verified, but most likely the data comes out
1445 * of the page cache.
1446 */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001447 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001448 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001449 !sblock_bad->checksum_error &&
1450 sblock_bad->no_io_error_seen)
1451 goto corrected_error;
1452 else
1453 goto did_not_correct_error;
1454 } else {
1455corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001456 spin_lock(&sctx->stat_lock);
1457 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001458 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001459 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001460 btrfs_err_rl_in_rcu(fs_info,
1461 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001462 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001463 }
1464 } else {
1465did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001466 spin_lock(&sctx->stat_lock);
1467 sctx->stat.uncorrectable_errors++;
1468 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001469 btrfs_err_rl_in_rcu(fs_info,
1470 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001471 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001472 }
1473
1474out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001475 if (sblocks_for_recheck) {
1476 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1477 mirror_index++) {
1478 struct scrub_block *sblock = sblocks_for_recheck +
1479 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001480 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001481 int page_index;
1482
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001483 for (page_index = 0; page_index < sblock->page_count;
1484 page_index++) {
1485 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001486 recover = sblock->pagev[page_index]->recover;
1487 if (recover) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001488 scrub_put_recover(fs_info, recover);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001489 sblock->pagev[page_index]->recover =
1490 NULL;
1491 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001492 scrub_page_put(sblock->pagev[page_index]);
1493 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001494 }
1495 kfree(sblocks_for_recheck);
1496 }
1497
Qu Wenruo28d70e22017-04-14 08:35:55 +08001498 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
1499 if (ret < 0)
1500 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001501 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001502}
1503
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001504static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001505{
Zhao Lei10f11902015-01-20 15:11:43 +08001506 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1507 return 2;
1508 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1509 return 3;
1510 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001511 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001512}
1513
Zhao Lei10f11902015-01-20 15:11:43 +08001514static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1515 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001516 u64 mapped_length,
1517 int nstripes, int mirror,
1518 int *stripe_index,
1519 u64 *stripe_offset)
1520{
1521 int i;
1522
Zhao Leiffe2d202015-01-20 15:11:44 +08001523 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001524 /* RAID5/6 */
1525 for (i = 0; i < nstripes; i++) {
1526 if (raid_map[i] == RAID6_Q_STRIPE ||
1527 raid_map[i] == RAID5_P_STRIPE)
1528 continue;
1529
1530 if (logical >= raid_map[i] &&
1531 logical < raid_map[i] + mapped_length)
1532 break;
1533 }
1534
1535 *stripe_index = i;
1536 *stripe_offset = logical - raid_map[i];
1537 } else {
1538 /* The other RAID type */
1539 *stripe_index = mirror;
1540 *stripe_offset = 0;
1541 }
1542}
1543
Zhao Leibe50a8d2015-01-20 15:11:42 +08001544static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001545 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001546{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001547 struct scrub_ctx *sctx = original_sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001548 struct btrfs_fs_info *fs_info = sctx->fs_info;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001549 u64 length = original_sblock->page_count * PAGE_SIZE;
1550 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001551 u64 generation = original_sblock->pagev[0]->generation;
1552 u64 flags = original_sblock->pagev[0]->flags;
1553 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001554 struct scrub_recover *recover;
1555 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001556 u64 sublen;
1557 u64 mapped_length;
1558 u64 stripe_offset;
1559 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001560 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001561 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001562 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001563 int ret;
1564
1565 /*
Zhao Lei57019342015-01-20 15:11:45 +08001566 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001567 * are not used (and not set) in the blocks that are used for
1568 * the recheck procedure
1569 */
1570
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001571 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001572 sublen = min_t(u64, length, PAGE_SIZE);
1573 mapped_length = sublen;
1574 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001575
1576 /*
1577 * with a length of PAGE_SIZE, each returned stripe
1578 * represents one mirror
1579 */
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001580 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001581 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
David Sterba825ad4c2017-03-28 14:45:22 +02001582 logical, &mapped_length, &bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001583 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001584 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001585 btrfs_bio_counter_dec(fs_info);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001586 return -EIO;
1587 }
1588
Miao Xieaf8e2d12014-10-23 14:42:50 +08001589 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1590 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001591 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001592 btrfs_bio_counter_dec(fs_info);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001593 return -ENOMEM;
1594 }
1595
Elena Reshetova6f615012017-03-03 10:55:21 +02001596 refcount_set(&recover->refs, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001597 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001598 recover->map_length = mapped_length;
1599
Ashish Samant24731142016-04-29 18:33:59 -07001600 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001601
Zhao Leibe50a8d2015-01-20 15:11:42 +08001602 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001603
Miao Xieaf8e2d12014-10-23 14:42:50 +08001604 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001605 mirror_index++) {
1606 struct scrub_block *sblock;
1607 struct scrub_page *page;
1608
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001609 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001610 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001611
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001612 page = kzalloc(sizeof(*page), GFP_NOFS);
1613 if (!page) {
1614leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001615 spin_lock(&sctx->stat_lock);
1616 sctx->stat.malloc_errors++;
1617 spin_unlock(&sctx->stat_lock);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001618 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001619 return -ENOMEM;
1620 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001621 scrub_page_get(page);
1622 sblock->pagev[page_index] = page;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001623 page->sblock = sblock;
1624 page->flags = flags;
1625 page->generation = generation;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001626 page->logical = logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001627 page->have_csum = have_csum;
1628 if (have_csum)
1629 memcpy(page->csum,
1630 original_sblock->pagev[0]->csum,
1631 sctx->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001632
Zhao Lei10f11902015-01-20 15:11:43 +08001633 scrub_stripe_index_and_offset(logical,
1634 bbio->map_type,
1635 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001636 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001637 bbio->num_stripes -
1638 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001639 mirror_index,
1640 &stripe_index,
1641 &stripe_offset);
1642 page->physical = bbio->stripes[stripe_index].physical +
1643 stripe_offset;
1644 page->dev = bbio->stripes[stripe_index].dev;
1645
Stefan Behrensff023aa2012-11-06 11:43:11 +01001646 BUG_ON(page_index >= original_sblock->page_count);
1647 page->physical_for_dev_replace =
1648 original_sblock->pagev[page_index]->
1649 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001650 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001651 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001652 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001653 page->page = alloc_page(GFP_NOFS);
1654 if (!page->page)
1655 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001656
1657 scrub_get_recover(recover);
1658 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001659 }
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001660 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001661 length -= sublen;
1662 logical += sublen;
1663 page_index++;
1664 }
1665
1666 return 0;
1667}
1668
Miao Xieaf8e2d12014-10-23 14:42:50 +08001669struct scrub_bio_ret {
1670 struct completion event;
1671 int error;
1672};
1673
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001674static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001675{
1676 struct scrub_bio_ret *ret = bio->bi_private;
1677
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001678 ret->error = bio->bi_error;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001679 complete(&ret->event);
1680}
1681
1682static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1683{
Zhao Lei10f11902015-01-20 15:11:43 +08001684 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001685 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001686}
1687
1688static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1689 struct bio *bio,
1690 struct scrub_page *page)
1691{
1692 struct scrub_bio_ret done;
1693 int ret;
1694
1695 init_completion(&done.event);
1696 done.error = 0;
1697 bio->bi_iter.bi_sector = page->logical >> 9;
1698 bio->bi_private = &done;
1699 bio->bi_end_io = scrub_bio_wait_endio;
1700
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001701 ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001702 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001703 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001704 if (ret)
1705 return ret;
1706
1707 wait_for_completion(&done.event);
1708 if (done.error)
1709 return -EIO;
1710
1711 return 0;
1712}
1713
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001714/*
1715 * this function will check the on disk data for checksum errors, header
1716 * errors and read I/O errors. If any I/O errors happen, the exact pages
1717 * which are errored are marked as being bad. The goal is to enable scrub
1718 * to take those pages that are not errored from all the mirrors so that
1719 * the pages that are errored in the just handled mirror can be repaired.
1720 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001721static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001722 struct scrub_block *sblock,
1723 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001724{
1725 int page_num;
1726
1727 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001728
1729 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1730 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001731 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001732
Stefan Behrens442a4f62012-05-25 16:06:08 +02001733 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001734 page->io_error = 1;
1735 sblock->no_io_error_seen = 0;
1736 continue;
1737 }
1738
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001739 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001740 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001741 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001742
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001743 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001744 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001745 if (scrub_submit_raid56_bio_wait(fs_info, bio, page)) {
1746 page->io_error = 1;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001747 sblock->no_io_error_seen = 0;
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001748 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001749 } else {
1750 bio->bi_iter.bi_sector = page->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001751 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001752
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001753 if (btrfsic_submit_bio_wait(bio)) {
1754 page->io_error = 1;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001755 sblock->no_io_error_seen = 0;
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001756 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001757 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001758
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001759 bio_put(bio);
1760 }
1761
1762 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001763 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001764}
1765
Miao Xie17a9be22014-07-24 11:37:08 +08001766static inline int scrub_check_fsid(u8 fsid[],
1767 struct scrub_page *spage)
1768{
1769 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1770 int ret;
1771
1772 ret = memcmp(fsid, fs_devices->fsid, BTRFS_UUID_SIZE);
1773 return !ret;
1774}
1775
Zhao Leiba7cf982015-08-24 21:18:02 +08001776static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001777{
Zhao Leiba7cf982015-08-24 21:18:02 +08001778 sblock->header_error = 0;
1779 sblock->checksum_error = 0;
1780 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001781
Zhao Leiba7cf982015-08-24 21:18:02 +08001782 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1783 scrub_checksum_data(sblock);
1784 else
1785 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001786}
1787
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001788static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001789 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001790{
1791 int page_num;
1792 int ret = 0;
1793
1794 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1795 int ret_sub;
1796
1797 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1798 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001799 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001800 if (ret_sub)
1801 ret = ret_sub;
1802 }
1803
1804 return ret;
1805}
1806
1807static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1808 struct scrub_block *sblock_good,
1809 int page_num, int force_write)
1810{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001811 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1812 struct scrub_page *page_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001813 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001814
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001815 BUG_ON(page_bad->page == NULL);
1816 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001817 if (force_write || sblock_bad->header_error ||
1818 sblock_bad->checksum_error || page_bad->io_error) {
1819 struct bio *bio;
1820 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001821
Stefan Behrensff023aa2012-11-06 11:43:11 +01001822 if (!page_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001823 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001824 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001825 return -EIO;
1826 }
1827
Chris Mason9be33952013-05-17 18:30:14 -04001828 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001829 bio->bi_bdev = page_bad->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001830 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001831 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001832
1833 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1834 if (PAGE_SIZE != ret) {
1835 bio_put(bio);
1836 return -EIO;
1837 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001838
Mike Christie4e49ea42016-06-05 14:31:41 -05001839 if (btrfsic_submit_bio_wait(bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001840 btrfs_dev_stat_inc_and_print(page_bad->dev,
1841 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001842 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001843 &fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001844 bio_put(bio);
1845 return -EIO;
1846 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001847 bio_put(bio);
1848 }
1849
1850 return 0;
1851}
1852
Stefan Behrensff023aa2012-11-06 11:43:11 +01001853static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1854{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001855 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001856 int page_num;
1857
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001858 /*
1859 * This block is used for the check of the parity on the source device,
1860 * so the data needn't be written into the destination device.
1861 */
1862 if (sblock->sparity)
1863 return;
1864
Stefan Behrensff023aa2012-11-06 11:43:11 +01001865 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1866 int ret;
1867
1868 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1869 if (ret)
1870 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001871 &fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001872 }
1873}
1874
1875static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1876 int page_num)
1877{
1878 struct scrub_page *spage = sblock->pagev[page_num];
1879
1880 BUG_ON(spage->page == NULL);
1881 if (spage->io_error) {
1882 void *mapped_buffer = kmap_atomic(spage->page);
1883
David Sterba619a9742017-03-29 20:48:44 +02001884 clear_page(mapped_buffer);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001885 flush_dcache_page(spage->page);
1886 kunmap_atomic(mapped_buffer);
1887 }
1888 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1889}
1890
1891static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1892 struct scrub_page *spage)
1893{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001894 struct scrub_bio *sbio;
1895 int ret;
1896
David Sterba3fb99302017-05-16 19:10:32 +02001897 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001898again:
David Sterba3fb99302017-05-16 19:10:32 +02001899 if (!sctx->wr_curr_bio) {
1900 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001901 GFP_KERNEL);
David Sterba3fb99302017-05-16 19:10:32 +02001902 if (!sctx->wr_curr_bio) {
1903 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001904 return -ENOMEM;
1905 }
David Sterba3fb99302017-05-16 19:10:32 +02001906 sctx->wr_curr_bio->sctx = sctx;
1907 sctx->wr_curr_bio->page_count = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001908 }
David Sterba3fb99302017-05-16 19:10:32 +02001909 sbio = sctx->wr_curr_bio;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001910 if (sbio->page_count == 0) {
1911 struct bio *bio;
1912
1913 sbio->physical = spage->physical_for_dev_replace;
1914 sbio->logical = spage->logical;
David Sterba3fb99302017-05-16 19:10:32 +02001915 sbio->dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001916 bio = sbio->bio;
1917 if (!bio) {
David Sterba58c4e172016-02-11 10:49:42 +01001918 bio = btrfs_io_bio_alloc(GFP_KERNEL,
David Sterba3fb99302017-05-16 19:10:32 +02001919 sctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001920 sbio->bio = bio;
1921 }
1922
1923 bio->bi_private = sbio;
1924 bio->bi_end_io = scrub_wr_bio_end_io;
1925 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001926 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001927 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001928 sbio->err = 0;
1929 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1930 spage->physical_for_dev_replace ||
1931 sbio->logical + sbio->page_count * PAGE_SIZE !=
1932 spage->logical) {
1933 scrub_wr_submit(sctx);
1934 goto again;
1935 }
1936
1937 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1938 if (ret != PAGE_SIZE) {
1939 if (sbio->page_count < 1) {
1940 bio_put(sbio->bio);
1941 sbio->bio = NULL;
David Sterba3fb99302017-05-16 19:10:32 +02001942 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001943 return -EIO;
1944 }
1945 scrub_wr_submit(sctx);
1946 goto again;
1947 }
1948
1949 sbio->pagev[sbio->page_count] = spage;
1950 scrub_page_get(spage);
1951 sbio->page_count++;
David Sterba3fb99302017-05-16 19:10:32 +02001952 if (sbio->page_count == sctx->pages_per_wr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001953 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02001954 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001955
1956 return 0;
1957}
1958
1959static void scrub_wr_submit(struct scrub_ctx *sctx)
1960{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001961 struct scrub_bio *sbio;
1962
David Sterba3fb99302017-05-16 19:10:32 +02001963 if (!sctx->wr_curr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001964 return;
1965
David Sterba3fb99302017-05-16 19:10:32 +02001966 sbio = sctx->wr_curr_bio;
1967 sctx->wr_curr_bio = NULL;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001968 WARN_ON(!sbio->bio->bi_bdev);
1969 scrub_pending_bio_inc(sctx);
1970 /* process all writes in a single worker thread. Then the block layer
1971 * orders the requests before sending them to the driver which
1972 * doubled the write performance on spinning disks when measured
1973 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001974 btrfsic_submit_bio(sbio->bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001975}
1976
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001977static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001978{
1979 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001980 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001981
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001982 sbio->err = bio->bi_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001983 sbio->bio = bio;
1984
Liu Bo9e0af232014-08-15 23:36:53 +08001985 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1986 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001987 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001988}
1989
1990static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1991{
1992 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1993 struct scrub_ctx *sctx = sbio->sctx;
1994 int i;
1995
1996 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1997 if (sbio->err) {
1998 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001999 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002000
2001 for (i = 0; i < sbio->page_count; i++) {
2002 struct scrub_page *spage = sbio->pagev[i];
2003
2004 spage->io_error = 1;
2005 btrfs_dev_replace_stats_inc(&dev_replace->
2006 num_write_errors);
2007 }
2008 }
2009
2010 for (i = 0; i < sbio->page_count; i++)
2011 scrub_page_put(sbio->pagev[i]);
2012
2013 bio_put(sbio->bio);
2014 kfree(sbio);
2015 scrub_pending_bio_dec(sctx);
2016}
2017
2018static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002019{
2020 u64 flags;
2021 int ret;
2022
Zhao Leiba7cf982015-08-24 21:18:02 +08002023 /*
2024 * No need to initialize these stats currently,
2025 * because this function only use return value
2026 * instead of these stats value.
2027 *
2028 * Todo:
2029 * always use stats
2030 */
2031 sblock->header_error = 0;
2032 sblock->generation_error = 0;
2033 sblock->checksum_error = 0;
2034
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002035 WARN_ON(sblock->page_count < 1);
2036 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002037 ret = 0;
2038 if (flags & BTRFS_EXTENT_FLAG_DATA)
2039 ret = scrub_checksum_data(sblock);
2040 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
2041 ret = scrub_checksum_tree_block(sblock);
2042 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
2043 (void)scrub_checksum_super(sblock);
2044 else
2045 WARN_ON(1);
2046 if (ret)
2047 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002048
2049 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002050}
2051
2052static int scrub_checksum_data(struct scrub_block *sblock)
2053{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002054 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002055 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002056 u8 *on_disk_csum;
2057 struct page *page;
2058 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01002059 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002060 u64 len;
2061 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002062
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002063 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002064 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002065 return 0;
2066
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002067 on_disk_csum = sblock->pagev[0]->csum;
2068 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002069 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002070
David Sterba25cc1222017-05-16 19:10:41 +02002071 len = sctx->fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002072 index = 0;
2073 for (;;) {
2074 u64 l = min_t(u64, len, PAGE_SIZE);
2075
Liu Bob0496682013-03-14 14:57:45 +00002076 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002077 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002078 len -= l;
2079 if (len == 0)
2080 break;
2081 index++;
2082 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002083 BUG_ON(!sblock->pagev[index]->page);
2084 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002085 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002086 }
2087
Arne Jansena2de7332011-03-08 14:14:00 +01002088 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002089 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002090 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002091
Zhao Leiba7cf982015-08-24 21:18:02 +08002092 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002093}
2094
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002095static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002096{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002097 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002098 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002099 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002100 u8 calculated_csum[BTRFS_CSUM_SIZE];
2101 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2102 struct page *page;
2103 void *mapped_buffer;
2104 u64 mapped_size;
2105 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002106 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002107 u64 len;
2108 int index;
2109
2110 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002111 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002112 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002113 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002114 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002115
2116 /*
2117 * we don't use the getter functions here, as we
2118 * a) don't have an extent buffer and
2119 * b) the page is already kmapped
2120 */
Qu Wenruo3cae2102013-07-16 11:19:18 +08002121 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08002122 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002123
Zhao Leiba7cf982015-08-24 21:18:02 +08002124 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
2125 sblock->header_error = 1;
2126 sblock->generation_error = 1;
2127 }
Arne Jansena2de7332011-03-08 14:14:00 +01002128
Miao Xie17a9be22014-07-24 11:37:08 +08002129 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Zhao Leiba7cf982015-08-24 21:18:02 +08002130 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002131
2132 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
2133 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08002134 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002135
David Sterba25cc1222017-05-16 19:10:41 +02002136 len = sctx->fs_info->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002137 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2138 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2139 index = 0;
2140 for (;;) {
2141 u64 l = min_t(u64, len, mapped_size);
2142
Liu Bob0496682013-03-14 14:57:45 +00002143 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002144 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002145 len -= l;
2146 if (len == 0)
2147 break;
2148 index++;
2149 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002150 BUG_ON(!sblock->pagev[index]->page);
2151 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002152 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002153 mapped_size = PAGE_SIZE;
2154 p = mapped_buffer;
2155 }
2156
2157 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002158 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002159 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002160
Zhao Leiba7cf982015-08-24 21:18:02 +08002161 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002162}
2163
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002164static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002165{
2166 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002167 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002168 u8 calculated_csum[BTRFS_CSUM_SIZE];
2169 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2170 struct page *page;
2171 void *mapped_buffer;
2172 u64 mapped_size;
2173 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002174 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002175 int fail_gen = 0;
2176 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002177 u64 len;
2178 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002179
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002180 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002181 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002182 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002183 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002184 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002185
Qu Wenruo3cae2102013-07-16 11:19:18 +08002186 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002187 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002188
Qu Wenruo3cae2102013-07-16 11:19:18 +08002189 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002190 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002191
Miao Xie17a9be22014-07-24 11:37:08 +08002192 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002193 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002194
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002195 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2196 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2197 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2198 index = 0;
2199 for (;;) {
2200 u64 l = min_t(u64, len, mapped_size);
2201
Liu Bob0496682013-03-14 14:57:45 +00002202 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002203 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002204 len -= l;
2205 if (len == 0)
2206 break;
2207 index++;
2208 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002209 BUG_ON(!sblock->pagev[index]->page);
2210 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002211 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002212 mapped_size = PAGE_SIZE;
2213 p = mapped_buffer;
2214 }
2215
2216 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002217 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002218 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002219
Stefan Behrens442a4f62012-05-25 16:06:08 +02002220 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002221 /*
2222 * if we find an error in a super block, we just report it.
2223 * They will get written with the next transaction commit
2224 * anyway
2225 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002226 spin_lock(&sctx->stat_lock);
2227 ++sctx->stat.super_errors;
2228 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002229 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002230 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002231 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2232 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002233 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002234 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002235 }
2236
Stefan Behrens442a4f62012-05-25 16:06:08 +02002237 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002238}
2239
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002240static void scrub_block_get(struct scrub_block *sblock)
2241{
Elena Reshetova186debd2017-03-03 10:55:23 +02002242 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002243}
2244
2245static void scrub_block_put(struct scrub_block *sblock)
2246{
Elena Reshetova186debd2017-03-03 10:55:23 +02002247 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002248 int i;
2249
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002250 if (sblock->sparity)
2251 scrub_parity_put(sblock->sparity);
2252
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002253 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002254 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002255 kfree(sblock);
2256 }
2257}
2258
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002259static void scrub_page_get(struct scrub_page *spage)
2260{
Zhao Lei57019342015-01-20 15:11:45 +08002261 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002262}
2263
2264static void scrub_page_put(struct scrub_page *spage)
2265{
Zhao Lei57019342015-01-20 15:11:45 +08002266 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002267 if (spage->page)
2268 __free_page(spage->page);
2269 kfree(spage);
2270 }
2271}
2272
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002273static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002274{
2275 struct scrub_bio *sbio;
2276
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002277 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002278 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002279
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002280 sbio = sctx->bios[sctx->curr];
2281 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002282 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002283 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002284}
2285
Stefan Behrensff023aa2012-11-06 11:43:11 +01002286static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2287 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002288{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002289 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002290 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002291 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002292
2293again:
2294 /*
2295 * grab a fresh bio or wait for one to become available
2296 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002297 while (sctx->curr == -1) {
2298 spin_lock(&sctx->list_lock);
2299 sctx->curr = sctx->first_free;
2300 if (sctx->curr != -1) {
2301 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2302 sctx->bios[sctx->curr]->next_free = -1;
2303 sctx->bios[sctx->curr]->page_count = 0;
2304 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002305 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002306 spin_unlock(&sctx->list_lock);
2307 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002308 }
2309 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002310 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002311 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002312 struct bio *bio;
2313
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002314 sbio->physical = spage->physical;
2315 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002316 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002317 bio = sbio->bio;
2318 if (!bio) {
David Sterba58c4e172016-02-11 10:49:42 +01002319 bio = btrfs_io_bio_alloc(GFP_KERNEL,
2320 sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002321 sbio->bio = bio;
2322 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002323
2324 bio->bi_private = sbio;
2325 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002326 bio->bi_bdev = sbio->dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002327 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05002328 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002329 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002330 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2331 spage->physical ||
2332 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002333 spage->logical ||
2334 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002335 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002336 goto again;
2337 }
2338
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002339 sbio->pagev[sbio->page_count] = spage;
2340 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2341 if (ret != PAGE_SIZE) {
2342 if (sbio->page_count < 1) {
2343 bio_put(sbio->bio);
2344 sbio->bio = NULL;
2345 return -EIO;
2346 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002347 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002348 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002349 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002350
Stefan Behrensff023aa2012-11-06 11:43:11 +01002351 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002352 atomic_inc(&sblock->outstanding_pages);
2353 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002354 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002355 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002356
2357 return 0;
2358}
2359
Linus Torvalds22365972015-09-05 15:14:43 -07002360static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002361{
2362 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002363 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002364
Linus Torvalds22365972015-09-05 15:14:43 -07002365 if (bio->bi_error)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002366 sblock->no_io_error_seen = 0;
2367
Scott Talbert46732722016-05-09 09:14:28 -04002368 bio_put(bio);
2369
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002370 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2371}
2372
2373static void scrub_missing_raid56_worker(struct btrfs_work *work)
2374{
2375 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2376 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002377 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002378 u64 logical;
2379 struct btrfs_device *dev;
2380
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002381 logical = sblock->pagev[0]->logical;
2382 dev = sblock->pagev[0]->dev;
2383
Zhao Leiaffe4a52015-08-24 21:32:06 +08002384 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002385 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002386
2387 if (!sblock->no_io_error_seen) {
2388 spin_lock(&sctx->stat_lock);
2389 sctx->stat.read_errors++;
2390 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002391 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002392 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002393 logical, rcu_str_deref(dev->name));
2394 } else if (sblock->header_error || sblock->checksum_error) {
2395 spin_lock(&sctx->stat_lock);
2396 sctx->stat.uncorrectable_errors++;
2397 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002398 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002399 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002400 logical, rcu_str_deref(dev->name));
2401 } else {
2402 scrub_write_block_to_dev_replace(sblock);
2403 }
2404
2405 scrub_block_put(sblock);
2406
2407 if (sctx->is_dev_replace &&
David Sterba3fb99302017-05-16 19:10:32 +02002408 atomic_read(&sctx->flush_all_writes)) {
2409 mutex_lock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002410 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002411 mutex_unlock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002412 }
2413
2414 scrub_pending_bio_dec(sctx);
2415}
2416
2417static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2418{
2419 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002420 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002421 u64 length = sblock->page_count * PAGE_SIZE;
2422 u64 logical = sblock->pagev[0]->logical;
Zhao Leif1fee652016-05-17 17:37:38 +08002423 struct btrfs_bio *bbio = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002424 struct bio *bio;
2425 struct btrfs_raid_bio *rbio;
2426 int ret;
2427 int i;
2428
Qu Wenruoae6529c2017-03-29 09:33:21 +08002429 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002430 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
David Sterba825ad4c2017-03-28 14:45:22 +02002431 &length, &bbio);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002432 if (ret || !bbio || !bbio->raid_map)
2433 goto bbio_out;
2434
2435 if (WARN_ON(!sctx->is_dev_replace ||
2436 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2437 /*
2438 * We shouldn't be scrubbing a missing device. Even for dev
2439 * replace, we should only get here for RAID 5/6. We either
2440 * managed to mount something with no mirrors remaining or
2441 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2442 */
2443 goto bbio_out;
2444 }
2445
2446 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002447 bio->bi_iter.bi_sector = logical >> 9;
2448 bio->bi_private = sblock;
2449 bio->bi_end_io = scrub_missing_raid56_end_io;
2450
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002451 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002452 if (!rbio)
2453 goto rbio_out;
2454
2455 for (i = 0; i < sblock->page_count; i++) {
2456 struct scrub_page *spage = sblock->pagev[i];
2457
2458 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2459 }
2460
2461 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2462 scrub_missing_raid56_worker, NULL, NULL);
2463 scrub_block_get(sblock);
2464 scrub_pending_bio_inc(sctx);
2465 raid56_submit_missing_rbio(rbio);
2466 return;
2467
2468rbio_out:
2469 bio_put(bio);
2470bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002471 btrfs_bio_counter_dec(fs_info);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002472 btrfs_put_bbio(bbio);
2473 spin_lock(&sctx->stat_lock);
2474 sctx->stat.malloc_errors++;
2475 spin_unlock(&sctx->stat_lock);
2476}
2477
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002478static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002479 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002480 u64 gen, int mirror_num, u8 *csum, int force,
2481 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002482{
2483 struct scrub_block *sblock;
2484 int index;
2485
David Sterba58c4e172016-02-11 10:49:42 +01002486 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002487 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002488 spin_lock(&sctx->stat_lock);
2489 sctx->stat.malloc_errors++;
2490 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002491 return -ENOMEM;
2492 }
2493
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002494 /* one ref inside this function, plus one for each page added to
2495 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002496 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002497 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002498 sblock->no_io_error_seen = 1;
2499
2500 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002501 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002502 u64 l = min_t(u64, len, PAGE_SIZE);
2503
David Sterba58c4e172016-02-11 10:49:42 +01002504 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002505 if (!spage) {
2506leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002507 spin_lock(&sctx->stat_lock);
2508 sctx->stat.malloc_errors++;
2509 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002510 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002511 return -ENOMEM;
2512 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002513 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2514 scrub_page_get(spage);
2515 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002516 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002517 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002518 spage->flags = flags;
2519 spage->generation = gen;
2520 spage->logical = logical;
2521 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002522 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002523 spage->mirror_num = mirror_num;
2524 if (csum) {
2525 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002526 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002527 } else {
2528 spage->have_csum = 0;
2529 }
2530 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002531 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002532 if (!spage->page)
2533 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002534 len -= l;
2535 logical += l;
2536 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002537 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002538 }
2539
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002540 WARN_ON(sblock->page_count == 0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002541 if (dev->missing) {
2542 /*
2543 * This case should only be hit for RAID 5/6 device replace. See
2544 * the comment in scrub_missing_raid56_pages() for details.
2545 */
2546 scrub_missing_raid56_pages(sblock);
2547 } else {
2548 for (index = 0; index < sblock->page_count; index++) {
2549 struct scrub_page *spage = sblock->pagev[index];
2550 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002551
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002552 ret = scrub_add_page_to_rd_bio(sctx, spage);
2553 if (ret) {
2554 scrub_block_put(sblock);
2555 return ret;
2556 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002557 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002558
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002559 if (force)
2560 scrub_submit(sctx);
2561 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002562
2563 /* last one frees, either here or in bio completion for last page */
2564 scrub_block_put(sblock);
2565 return 0;
2566}
2567
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002568static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002569{
2570 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002571 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002572
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002573 sbio->err = bio->bi_error;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002574 sbio->bio = bio;
2575
Qu Wenruo0339ef22014-02-28 10:46:17 +08002576 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002577}
2578
2579static void scrub_bio_end_io_worker(struct btrfs_work *work)
2580{
2581 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002582 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002583 int i;
2584
Stefan Behrensff023aa2012-11-06 11:43:11 +01002585 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002586 if (sbio->err) {
2587 for (i = 0; i < sbio->page_count; i++) {
2588 struct scrub_page *spage = sbio->pagev[i];
2589
2590 spage->io_error = 1;
2591 spage->sblock->no_io_error_seen = 0;
2592 }
2593 }
2594
2595 /* now complete the scrub_block items that have all pages completed */
2596 for (i = 0; i < sbio->page_count; i++) {
2597 struct scrub_page *spage = sbio->pagev[i];
2598 struct scrub_block *sblock = spage->sblock;
2599
2600 if (atomic_dec_and_test(&sblock->outstanding_pages))
2601 scrub_block_complete(sblock);
2602 scrub_block_put(sblock);
2603 }
2604
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002605 bio_put(sbio->bio);
2606 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002607 spin_lock(&sctx->list_lock);
2608 sbio->next_free = sctx->first_free;
2609 sctx->first_free = sbio->index;
2610 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002611
2612 if (sctx->is_dev_replace &&
David Sterba3fb99302017-05-16 19:10:32 +02002613 atomic_read(&sctx->flush_all_writes)) {
2614 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002615 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002616 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002617 }
2618
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002619 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002620}
2621
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002622static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2623 unsigned long *bitmap,
2624 u64 start, u64 len)
2625{
Liu Bo972d7212017-04-03 13:45:33 -07002626 u64 offset;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002627 int nsectors;
Jeff Mahoneyda170662016-06-15 09:22:56 -04002628 int sectorsize = sparity->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002629
2630 if (len >= sparity->stripe_len) {
2631 bitmap_set(bitmap, 0, sparity->nsectors);
2632 return;
2633 }
2634
2635 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002636 start = div64_u64_rem(start, sparity->stripe_len, &offset);
2637 offset = div_u64(offset, sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002638 nsectors = (int)len / sectorsize;
2639
2640 if (offset + nsectors <= sparity->nsectors) {
2641 bitmap_set(bitmap, offset, nsectors);
2642 return;
2643 }
2644
2645 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2646 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2647}
2648
2649static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2650 u64 start, u64 len)
2651{
2652 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2653}
2654
2655static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2656 u64 start, u64 len)
2657{
2658 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2659}
2660
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002661static void scrub_block_complete(struct scrub_block *sblock)
2662{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002663 int corrupted = 0;
2664
Stefan Behrensff023aa2012-11-06 11:43:11 +01002665 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002666 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002667 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002668 } else {
2669 /*
2670 * if has checksum error, write via repair mechanism in
2671 * dev replace case, otherwise write here in dev replace
2672 * case.
2673 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002674 corrupted = scrub_checksum(sblock);
2675 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002676 scrub_write_block_to_dev_replace(sblock);
2677 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002678
2679 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2680 u64 start = sblock->pagev[0]->logical;
2681 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2682 PAGE_SIZE;
2683
2684 scrub_parity_mark_sectors_error(sblock->sparity,
2685 start, end - start);
2686 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002687}
2688
Zhao Lei3b5753e2015-08-24 22:03:02 +08002689static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002690{
2691 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002692 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002693 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002694
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002695 while (!list_empty(&sctx->csum_list)) {
2696 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002697 struct btrfs_ordered_sum, list);
2698 if (sum->bytenr > logical)
2699 return 0;
2700 if (sum->bytenr + sum->len > logical)
2701 break;
2702
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002703 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002704 list_del(&sum->list);
2705 kfree(sum);
2706 sum = NULL;
2707 }
2708 if (!sum)
2709 return 0;
2710
David Sterba25cc1222017-05-16 19:10:41 +02002711 index = ((u32)(logical - sum->bytenr)) / sctx->fs_info->sectorsize;
2712 num_sectors = sum->len / sctx->fs_info->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002713 memcpy(csum, sum->sums + index, sctx->csum_size);
2714 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002715 list_del(&sum->list);
2716 kfree(sum);
2717 }
Miao Xief51a4a12013-06-19 10:36:09 +08002718 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002719}
2720
2721/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002722static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002723 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002724 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002725{
2726 int ret;
2727 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002728 u32 blocksize;
2729
2730 if (flags & BTRFS_EXTENT_FLAG_DATA) {
David Sterba25cc1222017-05-16 19:10:41 +02002731 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002732 spin_lock(&sctx->stat_lock);
2733 sctx->stat.data_extents_scrubbed++;
2734 sctx->stat.data_bytes_scrubbed += len;
2735 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002736 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
David Sterba25cc1222017-05-16 19:10:41 +02002737 blocksize = sctx->fs_info->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002738 spin_lock(&sctx->stat_lock);
2739 sctx->stat.tree_extents_scrubbed++;
2740 sctx->stat.tree_bytes_scrubbed += len;
2741 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002742 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002743 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002744 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002745 }
Arne Jansena2de7332011-03-08 14:14:00 +01002746
2747 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002748 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002749 int have_csum = 0;
2750
2751 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2752 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002753 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002754 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002755 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002756 if (sctx->is_dev_replace && !have_csum) {
2757 ret = copy_nocow_pages(sctx, logical, l,
2758 mirror_num,
2759 physical_for_dev_replace);
2760 goto behind_scrub_pages;
2761 }
Arne Jansena2de7332011-03-08 14:14:00 +01002762 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002763 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002764 mirror_num, have_csum ? csum : NULL, 0,
2765 physical_for_dev_replace);
2766behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002767 if (ret)
2768 return ret;
2769 len -= l;
2770 logical += l;
2771 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002772 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002773 }
2774 return 0;
2775}
2776
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002777static int scrub_pages_for_parity(struct scrub_parity *sparity,
2778 u64 logical, u64 len,
2779 u64 physical, struct btrfs_device *dev,
2780 u64 flags, u64 gen, int mirror_num, u8 *csum)
2781{
2782 struct scrub_ctx *sctx = sparity->sctx;
2783 struct scrub_block *sblock;
2784 int index;
2785
David Sterba58c4e172016-02-11 10:49:42 +01002786 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002787 if (!sblock) {
2788 spin_lock(&sctx->stat_lock);
2789 sctx->stat.malloc_errors++;
2790 spin_unlock(&sctx->stat_lock);
2791 return -ENOMEM;
2792 }
2793
2794 /* one ref inside this function, plus one for each page added to
2795 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002796 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002797 sblock->sctx = sctx;
2798 sblock->no_io_error_seen = 1;
2799 sblock->sparity = sparity;
2800 scrub_parity_get(sparity);
2801
2802 for (index = 0; len > 0; index++) {
2803 struct scrub_page *spage;
2804 u64 l = min_t(u64, len, PAGE_SIZE);
2805
David Sterba58c4e172016-02-11 10:49:42 +01002806 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002807 if (!spage) {
2808leave_nomem:
2809 spin_lock(&sctx->stat_lock);
2810 sctx->stat.malloc_errors++;
2811 spin_unlock(&sctx->stat_lock);
2812 scrub_block_put(sblock);
2813 return -ENOMEM;
2814 }
2815 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2816 /* For scrub block */
2817 scrub_page_get(spage);
2818 sblock->pagev[index] = spage;
2819 /* For scrub parity */
2820 scrub_page_get(spage);
2821 list_add_tail(&spage->list, &sparity->spages);
2822 spage->sblock = sblock;
2823 spage->dev = dev;
2824 spage->flags = flags;
2825 spage->generation = gen;
2826 spage->logical = logical;
2827 spage->physical = physical;
2828 spage->mirror_num = mirror_num;
2829 if (csum) {
2830 spage->have_csum = 1;
2831 memcpy(spage->csum, csum, sctx->csum_size);
2832 } else {
2833 spage->have_csum = 0;
2834 }
2835 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002836 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002837 if (!spage->page)
2838 goto leave_nomem;
2839 len -= l;
2840 logical += l;
2841 physical += l;
2842 }
2843
2844 WARN_ON(sblock->page_count == 0);
2845 for (index = 0; index < sblock->page_count; index++) {
2846 struct scrub_page *spage = sblock->pagev[index];
2847 int ret;
2848
2849 ret = scrub_add_page_to_rd_bio(sctx, spage);
2850 if (ret) {
2851 scrub_block_put(sblock);
2852 return ret;
2853 }
2854 }
2855
2856 /* last one frees, either here or in bio completion for last page */
2857 scrub_block_put(sblock);
2858 return 0;
2859}
2860
2861static int scrub_extent_for_parity(struct scrub_parity *sparity,
2862 u64 logical, u64 len,
2863 u64 physical, struct btrfs_device *dev,
2864 u64 flags, u64 gen, int mirror_num)
2865{
2866 struct scrub_ctx *sctx = sparity->sctx;
2867 int ret;
2868 u8 csum[BTRFS_CSUM_SIZE];
2869 u32 blocksize;
2870
Omar Sandoval4a770892015-06-19 11:52:52 -07002871 if (dev->missing) {
2872 scrub_parity_mark_sectors_error(sparity, logical, len);
2873 return 0;
2874 }
2875
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002876 if (flags & BTRFS_EXTENT_FLAG_DATA) {
David Sterba25cc1222017-05-16 19:10:41 +02002877 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002878 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
David Sterba25cc1222017-05-16 19:10:41 +02002879 blocksize = sctx->fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002880 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002881 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002882 WARN_ON(1);
2883 }
2884
2885 while (len) {
2886 u64 l = min_t(u64, len, blocksize);
2887 int have_csum = 0;
2888
2889 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2890 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002891 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002892 if (have_csum == 0)
2893 goto skip;
2894 }
2895 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2896 flags, gen, mirror_num,
2897 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002898 if (ret)
2899 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002900skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002901 len -= l;
2902 logical += l;
2903 physical += l;
2904 }
2905 return 0;
2906}
2907
Wang Shilong3b080b22014-04-01 18:01:43 +08002908/*
2909 * Given a physical address, this will calculate it's
2910 * logical offset. if this is a parity stripe, it will return
2911 * the most left data stripe's logical offset.
2912 *
2913 * return 0 if it is a data stripe, 1 means parity stripe.
2914 */
2915static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002916 struct map_lookup *map, u64 *offset,
2917 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002918{
2919 int i;
2920 int j = 0;
2921 u64 stripe_nr;
2922 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002923 u32 stripe_index;
2924 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002925
2926 last_offset = (physical - map->stripes[num].physical) *
2927 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002928 if (stripe_start)
2929 *stripe_start = last_offset;
2930
Wang Shilong3b080b22014-04-01 18:01:43 +08002931 *offset = last_offset;
2932 for (i = 0; i < nr_data_stripes(map); i++) {
2933 *offset = last_offset + i * map->stripe_len;
2934
Liu Bo42c61ab2017-04-03 13:45:24 -07002935 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01002936 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002937
2938 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002939 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002940 /* calculate which stripe this data locates */
2941 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002942 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002943 if (stripe_index == num)
2944 return 0;
2945 if (stripe_index < num)
2946 j++;
2947 }
2948 *offset = last_offset + j * map->stripe_len;
2949 return 1;
2950}
2951
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002952static void scrub_free_parity(struct scrub_parity *sparity)
2953{
2954 struct scrub_ctx *sctx = sparity->sctx;
2955 struct scrub_page *curr, *next;
2956 int nbits;
2957
2958 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2959 if (nbits) {
2960 spin_lock(&sctx->stat_lock);
2961 sctx->stat.read_errors += nbits;
2962 sctx->stat.uncorrectable_errors += nbits;
2963 spin_unlock(&sctx->stat_lock);
2964 }
2965
2966 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2967 list_del_init(&curr->list);
2968 scrub_page_put(curr);
2969 }
2970
2971 kfree(sparity);
2972}
2973
Zhao Lei20b2e302015-06-04 20:09:15 +08002974static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2975{
2976 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2977 work);
2978 struct scrub_ctx *sctx = sparity->sctx;
2979
2980 scrub_free_parity(sparity);
2981 scrub_pending_bio_dec(sctx);
2982}
2983
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002984static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002985{
2986 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002987 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002988
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002989 if (bio->bi_error)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002990 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2991 sparity->nsectors);
2992
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002993 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002994
2995 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2996 scrub_parity_bio_endio_worker, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002997 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002998}
2999
3000static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
3001{
3002 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003003 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003004 struct bio *bio;
3005 struct btrfs_raid_bio *rbio;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003006 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003007 u64 length;
3008 int ret;
3009
3010 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
3011 sparity->nsectors))
3012 goto out;
3013
Zhao Leia0dd59d2015-07-21 15:42:26 +08003014 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08003015
3016 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003017 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
David Sterba825ad4c2017-03-28 14:45:22 +02003018 &length, &bbio);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003019 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003020 goto bbio_out;
3021
3022 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003023 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
3024 bio->bi_private = sparity;
3025 bio->bi_end_io = scrub_parity_bio_endio;
3026
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003027 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003028 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003029 sparity->dbitmap,
3030 sparity->nsectors);
3031 if (!rbio)
3032 goto rbio_out;
3033
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003034 scrub_pending_bio_inc(sctx);
3035 raid56_parity_submit_scrub_rbio(rbio);
3036 return;
3037
3038rbio_out:
3039 bio_put(bio);
3040bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08003041 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08003042 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003043 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
3044 sparity->nsectors);
3045 spin_lock(&sctx->stat_lock);
3046 sctx->stat.malloc_errors++;
3047 spin_unlock(&sctx->stat_lock);
3048out:
3049 scrub_free_parity(sparity);
3050}
3051
3052static inline int scrub_calc_parity_bitmap_len(int nsectors)
3053{
Zhao Leibfca9a62014-12-08 19:55:57 +08003054 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003055}
3056
3057static void scrub_parity_get(struct scrub_parity *sparity)
3058{
Elena Reshetova78a76452017-03-03 10:55:24 +02003059 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003060}
3061
3062static void scrub_parity_put(struct scrub_parity *sparity)
3063{
Elena Reshetova78a76452017-03-03 10:55:24 +02003064 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003065 return;
3066
3067 scrub_parity_check_and_repair(sparity);
3068}
3069
3070static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
3071 struct map_lookup *map,
3072 struct btrfs_device *sdev,
3073 struct btrfs_path *path,
3074 u64 logic_start,
3075 u64 logic_end)
3076{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003077 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003078 struct btrfs_root *root = fs_info->extent_root;
3079 struct btrfs_root *csum_root = fs_info->csum_root;
3080 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07003081 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003082 u64 flags;
3083 int ret;
3084 int slot;
3085 struct extent_buffer *l;
3086 struct btrfs_key key;
3087 u64 generation;
3088 u64 extent_logical;
3089 u64 extent_physical;
3090 u64 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07003091 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003092 struct btrfs_device *extent_dev;
3093 struct scrub_parity *sparity;
3094 int nsectors;
3095 int bitmap_len;
3096 int extent_mirror_num;
3097 int stop_loop = 0;
3098
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003099 nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003100 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
3101 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
3102 GFP_NOFS);
3103 if (!sparity) {
3104 spin_lock(&sctx->stat_lock);
3105 sctx->stat.malloc_errors++;
3106 spin_unlock(&sctx->stat_lock);
3107 return -ENOMEM;
3108 }
3109
3110 sparity->stripe_len = map->stripe_len;
3111 sparity->nsectors = nsectors;
3112 sparity->sctx = sctx;
3113 sparity->scrub_dev = sdev;
3114 sparity->logic_start = logic_start;
3115 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02003116 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003117 INIT_LIST_HEAD(&sparity->spages);
3118 sparity->dbitmap = sparity->bitmap;
3119 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
3120
3121 ret = 0;
3122 while (logic_start < logic_end) {
3123 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3124 key.type = BTRFS_METADATA_ITEM_KEY;
3125 else
3126 key.type = BTRFS_EXTENT_ITEM_KEY;
3127 key.objectid = logic_start;
3128 key.offset = (u64)-1;
3129
3130 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3131 if (ret < 0)
3132 goto out;
3133
3134 if (ret > 0) {
3135 ret = btrfs_previous_extent_item(root, path, 0);
3136 if (ret < 0)
3137 goto out;
3138 if (ret > 0) {
3139 btrfs_release_path(path);
3140 ret = btrfs_search_slot(NULL, root, &key,
3141 path, 0, 0);
3142 if (ret < 0)
3143 goto out;
3144 }
3145 }
3146
3147 stop_loop = 0;
3148 while (1) {
3149 u64 bytes;
3150
3151 l = path->nodes[0];
3152 slot = path->slots[0];
3153 if (slot >= btrfs_header_nritems(l)) {
3154 ret = btrfs_next_leaf(root, path);
3155 if (ret == 0)
3156 continue;
3157 if (ret < 0)
3158 goto out;
3159
3160 stop_loop = 1;
3161 break;
3162 }
3163 btrfs_item_key_to_cpu(l, &key, slot);
3164
Zhao Leid7cad232015-07-22 13:14:48 +08003165 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3166 key.type != BTRFS_METADATA_ITEM_KEY)
3167 goto next;
3168
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003169 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003170 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003171 else
3172 bytes = key.offset;
3173
3174 if (key.objectid + bytes <= logic_start)
3175 goto next;
3176
Zhao Leia0dd59d2015-07-21 15:42:26 +08003177 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003178 stop_loop = 1;
3179 break;
3180 }
3181
3182 while (key.objectid >= logic_start + map->stripe_len)
3183 logic_start += map->stripe_len;
3184
3185 extent = btrfs_item_ptr(l, slot,
3186 struct btrfs_extent_item);
3187 flags = btrfs_extent_flags(l, extent);
3188 generation = btrfs_extent_generation(l, extent);
3189
Zhao Leia323e812015-07-23 12:29:49 +08003190 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3191 (key.objectid < logic_start ||
3192 key.objectid + bytes >
3193 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003194 btrfs_err(fs_info,
3195 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08003196 key.objectid, logic_start);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003197 spin_lock(&sctx->stat_lock);
3198 sctx->stat.uncorrectable_errors++;
3199 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003200 goto next;
3201 }
3202again:
3203 extent_logical = key.objectid;
3204 extent_len = bytes;
3205
3206 if (extent_logical < logic_start) {
3207 extent_len -= logic_start - extent_logical;
3208 extent_logical = logic_start;
3209 }
3210
3211 if (extent_logical + extent_len >
3212 logic_start + map->stripe_len)
3213 extent_len = logic_start + map->stripe_len -
3214 extent_logical;
3215
3216 scrub_parity_mark_sectors_data(sparity, extent_logical,
3217 extent_len);
3218
Omar Sandoval4a770892015-06-19 11:52:52 -07003219 mapped_length = extent_len;
Zhao Leif1fee652016-05-17 17:37:38 +08003220 bbio = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003221 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
3222 extent_logical, &mapped_length, &bbio,
3223 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07003224 if (!ret) {
3225 if (!bbio || mapped_length < extent_len)
3226 ret = -EIO;
3227 }
3228 if (ret) {
3229 btrfs_put_bbio(bbio);
3230 goto out;
3231 }
3232 extent_physical = bbio->stripes[0].physical;
3233 extent_mirror_num = bbio->mirror_num;
3234 extent_dev = bbio->stripes[0].dev;
3235 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003236
3237 ret = btrfs_lookup_csums_range(csum_root,
3238 extent_logical,
3239 extent_logical + extent_len - 1,
3240 &sctx->csum_list, 1);
3241 if (ret)
3242 goto out;
3243
3244 ret = scrub_extent_for_parity(sparity, extent_logical,
3245 extent_len,
3246 extent_physical,
3247 extent_dev, flags,
3248 generation,
3249 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003250
3251 scrub_free_csums(sctx);
3252
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003253 if (ret)
3254 goto out;
3255
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003256 if (extent_logical + extent_len <
3257 key.objectid + bytes) {
3258 logic_start += map->stripe_len;
3259
3260 if (logic_start >= logic_end) {
3261 stop_loop = 1;
3262 break;
3263 }
3264
3265 if (logic_start < key.objectid + bytes) {
3266 cond_resched();
3267 goto again;
3268 }
3269 }
3270next:
3271 path->slots[0]++;
3272 }
3273
3274 btrfs_release_path(path);
3275
3276 if (stop_loop)
3277 break;
3278
3279 logic_start += map->stripe_len;
3280 }
3281out:
3282 if (ret < 0)
3283 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003284 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003285 scrub_parity_put(sparity);
3286 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003287 mutex_lock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003288 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003289 mutex_unlock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003290
3291 btrfs_release_path(path);
3292 return ret < 0 ? ret : 0;
3293}
3294
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003295static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003296 struct map_lookup *map,
3297 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003298 int num, u64 base, u64 length,
3299 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003300{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003301 struct btrfs_path *path, *ppath;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003302 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003303 struct btrfs_root *root = fs_info->extent_root;
3304 struct btrfs_root *csum_root = fs_info->csum_root;
3305 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003306 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003307 u64 flags;
3308 int ret;
3309 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003310 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003311 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003312 u64 physical;
3313 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003314 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003315 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003316 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003317 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003318 struct reada_control *reada1;
3319 struct reada_control *reada2;
David Sterbae6c11f92016-03-24 18:00:53 +01003320 struct btrfs_key key;
Arne Jansen7a262852011-06-10 12:39:23 +02003321 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003322 u64 increment = map->stripe_len;
3323 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003324 u64 extent_logical;
3325 u64 extent_physical;
3326 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003327 u64 stripe_logical;
3328 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003329 struct btrfs_device *extent_dev;
3330 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003331 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003332
Wang Shilong3b080b22014-04-01 18:01:43 +08003333 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003334 offset = 0;
Liu Bo42c61ab2017-04-03 13:45:24 -07003335 nstripes = div64_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003336 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3337 offset = map->stripe_len * num;
3338 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003339 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003340 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3341 int factor = map->num_stripes / map->sub_stripes;
3342 offset = map->stripe_len * (num / map->sub_stripes);
3343 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003344 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003345 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3346 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003347 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003348 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3349 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003350 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003351 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003352 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003353 increment = map->stripe_len * nr_data_stripes(map);
3354 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003355 } else {
3356 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003357 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003358 }
3359
3360 path = btrfs_alloc_path();
3361 if (!path)
3362 return -ENOMEM;
3363
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003364 ppath = btrfs_alloc_path();
3365 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003366 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003367 return -ENOMEM;
3368 }
3369
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003370 /*
3371 * work on commit root. The related disk blocks are static as
3372 * long as COW is applied. This means, it is save to rewrite
3373 * them to repair disk errors without any race conditions
3374 */
Arne Jansena2de7332011-03-08 14:14:00 +01003375 path->search_commit_root = 1;
3376 path->skip_locking = 1;
3377
Gui Hecheng063c54d2015-01-09 09:39:40 +08003378 ppath->search_commit_root = 1;
3379 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003380 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003381 * trigger the readahead for extent tree csum tree and wait for
3382 * completion. During readahead, the scrub is officially paused
3383 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003384 */
3385 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003386 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003387 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003388 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003389 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003390 logic_end += base;
3391 } else {
3392 logic_end = logical + increment * nstripes;
3393 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003394 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003395 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003396 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003397
Arne Jansen7a262852011-06-10 12:39:23 +02003398 /* FIXME it might be better to start readahead at commit root */
David Sterbae6c11f92016-03-24 18:00:53 +01003399 key.objectid = logical;
3400 key.type = BTRFS_EXTENT_ITEM_KEY;
3401 key.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003402 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003403 key_end.type = BTRFS_METADATA_ITEM_KEY;
3404 key_end.offset = (u64)-1;
David Sterbae6c11f92016-03-24 18:00:53 +01003405 reada1 = btrfs_reada_add(root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003406
David Sterbae6c11f92016-03-24 18:00:53 +01003407 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3408 key.type = BTRFS_EXTENT_CSUM_KEY;
3409 key.offset = logical;
Arne Jansen7a262852011-06-10 12:39:23 +02003410 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3411 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003412 key_end.offset = logic_end;
David Sterbae6c11f92016-03-24 18:00:53 +01003413 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003414
Arne Jansen7a262852011-06-10 12:39:23 +02003415 if (!IS_ERR(reada1))
3416 btrfs_reada_wait(reada1);
3417 if (!IS_ERR(reada2))
3418 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003419
Arne Jansena2de7332011-03-08 14:14:00 +01003420
3421 /*
3422 * collect all data csums for the stripe to avoid seeking during
3423 * the scrub. This might currently (crc32) end up to be about 1MB
3424 */
Arne Jansene7786c32011-05-28 20:58:38 +00003425 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003426
Arne Jansena2de7332011-03-08 14:14:00 +01003427 /*
3428 * now find all extents for each stripe and scrub them
3429 */
Arne Jansena2de7332011-03-08 14:14:00 +01003430 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003431 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003432 /*
3433 * canceled?
3434 */
3435 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003436 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003437 ret = -ECANCELED;
3438 goto out;
3439 }
3440 /*
3441 * check to see if we have to pause
3442 */
3443 if (atomic_read(&fs_info->scrub_pause_req)) {
3444 /* push queued extents */
David Sterba3fb99302017-05-16 19:10:32 +02003445 atomic_set(&sctx->flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003446 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003447 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003448 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003449 mutex_unlock(&sctx->wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003450 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003451 atomic_read(&sctx->bios_in_flight) == 0);
David Sterba3fb99302017-05-16 19:10:32 +02003452 atomic_set(&sctx->flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08003453 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003454 }
3455
Zhao Leif2f66a22015-07-21 12:22:29 +08003456 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3457 ret = get_raid56_logic_offset(physical, num, map,
3458 &logical,
3459 &stripe_logical);
3460 logical += base;
3461 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003462 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003463 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003464 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003465 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3466 ppath, stripe_logical,
3467 stripe_end);
3468 if (ret)
3469 goto out;
3470 goto skip;
3471 }
3472 }
3473
Wang Shilong7c76edb2014-01-12 21:38:32 +08003474 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3475 key.type = BTRFS_METADATA_ITEM_KEY;
3476 else
3477 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003478 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003479 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003480
3481 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3482 if (ret < 0)
3483 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003484
Arne Jansen8c510322011-06-03 10:09:26 +02003485 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003486 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003487 if (ret < 0)
3488 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003489 if (ret > 0) {
3490 /* there's no smaller item, so stick with the
3491 * larger one */
3492 btrfs_release_path(path);
3493 ret = btrfs_search_slot(NULL, root, &key,
3494 path, 0, 0);
3495 if (ret < 0)
3496 goto out;
3497 }
Arne Jansena2de7332011-03-08 14:14:00 +01003498 }
3499
Liu Bo625f1c8d2013-04-27 02:56:57 +00003500 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003501 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003502 u64 bytes;
3503
Arne Jansena2de7332011-03-08 14:14:00 +01003504 l = path->nodes[0];
3505 slot = path->slots[0];
3506 if (slot >= btrfs_header_nritems(l)) {
3507 ret = btrfs_next_leaf(root, path);
3508 if (ret == 0)
3509 continue;
3510 if (ret < 0)
3511 goto out;
3512
Liu Bo625f1c8d2013-04-27 02:56:57 +00003513 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003514 break;
3515 }
3516 btrfs_item_key_to_cpu(l, &key, slot);
3517
Zhao Leid7cad232015-07-22 13:14:48 +08003518 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3519 key.type != BTRFS_METADATA_ITEM_KEY)
3520 goto next;
3521
Josef Bacik3173a182013-03-07 14:22:04 -05003522 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003523 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003524 else
3525 bytes = key.offset;
3526
3527 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003528 goto next;
3529
Liu Bo625f1c8d2013-04-27 02:56:57 +00003530 if (key.objectid >= logical + map->stripe_len) {
3531 /* out of this device extent */
3532 if (key.objectid >= logic_end)
3533 stop_loop = 1;
3534 break;
3535 }
Arne Jansena2de7332011-03-08 14:14:00 +01003536
3537 extent = btrfs_item_ptr(l, slot,
3538 struct btrfs_extent_item);
3539 flags = btrfs_extent_flags(l, extent);
3540 generation = btrfs_extent_generation(l, extent);
3541
Zhao Leia323e812015-07-23 12:29:49 +08003542 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3543 (key.objectid < logical ||
3544 key.objectid + bytes >
3545 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003546 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003547 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003548 key.objectid, logical);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003549 spin_lock(&sctx->stat_lock);
3550 sctx->stat.uncorrectable_errors++;
3551 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003552 goto next;
3553 }
3554
Liu Bo625f1c8d2013-04-27 02:56:57 +00003555again:
3556 extent_logical = key.objectid;
3557 extent_len = bytes;
3558
Arne Jansena2de7332011-03-08 14:14:00 +01003559 /*
3560 * trim extent to this stripe
3561 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003562 if (extent_logical < logical) {
3563 extent_len -= logical - extent_logical;
3564 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003565 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003566 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003567 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003568 extent_len = logical + map->stripe_len -
3569 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003570 }
3571
Liu Bo625f1c8d2013-04-27 02:56:57 +00003572 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003573 extent_dev = scrub_dev;
3574 extent_mirror_num = mirror_num;
3575 if (is_dev_replace)
3576 scrub_remap_extent(fs_info, extent_logical,
3577 extent_len, &extent_physical,
3578 &extent_dev,
3579 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003580
Zhao Leife8cf652015-07-22 13:14:47 +08003581 ret = btrfs_lookup_csums_range(csum_root,
3582 extent_logical,
3583 extent_logical +
3584 extent_len - 1,
3585 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003586 if (ret)
3587 goto out;
3588
Liu Bo625f1c8d2013-04-27 02:56:57 +00003589 ret = scrub_extent(sctx, extent_logical, extent_len,
3590 extent_physical, extent_dev, flags,
3591 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003592 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003593
3594 scrub_free_csums(sctx);
3595
Liu Bo625f1c8d2013-04-27 02:56:57 +00003596 if (ret)
3597 goto out;
3598
3599 if (extent_logical + extent_len <
3600 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003601 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003602 /*
3603 * loop until we find next data stripe
3604 * or we have finished all stripes.
3605 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003606loop:
3607 physical += map->stripe_len;
3608 ret = get_raid56_logic_offset(physical,
3609 num, map, &logical,
3610 &stripe_logical);
3611 logical += base;
3612
3613 if (ret && physical < physical_end) {
3614 stripe_logical += base;
3615 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003616 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003617 ret = scrub_raid56_parity(sctx,
3618 map, scrub_dev, ppath,
3619 stripe_logical,
3620 stripe_end);
3621 if (ret)
3622 goto out;
3623 goto loop;
3624 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003625 } else {
3626 physical += map->stripe_len;
3627 logical += increment;
3628 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003629 if (logical < key.objectid + bytes) {
3630 cond_resched();
3631 goto again;
3632 }
3633
Wang Shilong3b080b22014-04-01 18:01:43 +08003634 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003635 stop_loop = 1;
3636 break;
3637 }
3638 }
Arne Jansena2de7332011-03-08 14:14:00 +01003639next:
3640 path->slots[0]++;
3641 }
Chris Mason71267332011-05-23 06:30:52 -04003642 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003643skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003644 logical += increment;
3645 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003646 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003647 if (stop_loop)
3648 sctx->stat.last_physical = map->stripes[num].physical +
3649 length;
3650 else
3651 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003652 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003653 if (stop_loop)
3654 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003655 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003656out:
Arne Jansena2de7332011-03-08 14:14:00 +01003657 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003658 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003659 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003660 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003661 mutex_unlock(&sctx->wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003662
Arne Jansene7786c32011-05-28 20:58:38 +00003663 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003664 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003665 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003666 return ret < 0 ? ret : 0;
3667}
3668
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003669static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003670 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003671 u64 chunk_offset, u64 length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003672 u64 dev_offset,
3673 struct btrfs_block_group_cache *cache,
3674 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003675{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003676 struct btrfs_fs_info *fs_info = sctx->fs_info;
3677 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003678 struct map_lookup *map;
3679 struct extent_map *em;
3680 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003681 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003682
3683 read_lock(&map_tree->map_tree.lock);
3684 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3685 read_unlock(&map_tree->map_tree.lock);
3686
Filipe Manana020d5b72015-11-19 10:57:20 +00003687 if (!em) {
3688 /*
3689 * Might have been an unused block group deleted by the cleaner
3690 * kthread or relocation.
3691 */
3692 spin_lock(&cache->lock);
3693 if (!cache->removed)
3694 ret = -EINVAL;
3695 spin_unlock(&cache->lock);
3696
3697 return ret;
3698 }
Arne Jansena2de7332011-03-08 14:14:00 +01003699
Jeff Mahoney95617d62015-06-03 10:55:48 -04003700 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003701 if (em->start != chunk_offset)
3702 goto out;
3703
3704 if (em->len < length)
3705 goto out;
3706
3707 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003708 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003709 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003710 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003711 chunk_offset, length,
3712 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003713 if (ret)
3714 goto out;
3715 }
3716 }
3717out:
3718 free_extent_map(em);
3719
3720 return ret;
3721}
3722
3723static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003724int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003725 struct btrfs_device *scrub_dev, u64 start, u64 end,
3726 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003727{
3728 struct btrfs_dev_extent *dev_extent = NULL;
3729 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003730 struct btrfs_fs_info *fs_info = sctx->fs_info;
3731 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003732 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003733 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003734 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003735 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003736 int slot;
3737 struct extent_buffer *l;
3738 struct btrfs_key key;
3739 struct btrfs_key found_key;
3740 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003741 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003742
3743 path = btrfs_alloc_path();
3744 if (!path)
3745 return -ENOMEM;
3746
David Sterbae4058b52015-11-27 16:31:35 +01003747 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003748 path->search_commit_root = 1;
3749 path->skip_locking = 1;
3750
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003751 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003752 key.offset = 0ull;
3753 key.type = BTRFS_DEV_EXTENT_KEY;
3754
Arne Jansena2de7332011-03-08 14:14:00 +01003755 while (1) {
3756 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3757 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003758 break;
3759 if (ret > 0) {
3760 if (path->slots[0] >=
3761 btrfs_header_nritems(path->nodes[0])) {
3762 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003763 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003764 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003765 if (ret > 0) {
3766 ret = 0;
3767 break;
3768 }
3769 } else {
3770 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003771 }
3772 }
Arne Jansena2de7332011-03-08 14:14:00 +01003773
3774 l = path->nodes[0];
3775 slot = path->slots[0];
3776
3777 btrfs_item_key_to_cpu(l, &found_key, slot);
3778
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003779 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003780 break;
3781
David Sterba962a2982014-06-04 18:41:45 +02003782 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003783 break;
3784
3785 if (found_key.offset >= end)
3786 break;
3787
3788 if (found_key.offset < key.offset)
3789 break;
3790
3791 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3792 length = btrfs_dev_extent_length(l, dev_extent);
3793
Qu Wenruoced96ed2014-06-19 10:42:51 +08003794 if (found_key.offset + length <= start)
3795 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003796
Arne Jansena2de7332011-03-08 14:14:00 +01003797 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3798
3799 /*
3800 * get a reference on the corresponding block group to prevent
3801 * the chunk from going away while we scrub it
3802 */
3803 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003804
3805 /* some chunks are removed but not committed to disk yet,
3806 * continue scrubbing */
3807 if (!cache)
3808 goto skip;
3809
Zhaolei55e3a602015-08-05 16:43:30 +08003810 /*
3811 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3812 * to avoid deadlock caused by:
3813 * btrfs_inc_block_group_ro()
3814 * -> btrfs_wait_for_commit()
3815 * -> btrfs_commit_transaction()
3816 * -> btrfs_scrub_pause()
3817 */
3818 scrub_pause_on(fs_info);
Jeff Mahoney5e00f192017-02-15 16:28:29 -05003819 ret = btrfs_inc_block_group_ro(fs_info, cache);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003820 if (!ret && is_dev_replace) {
3821 /*
3822 * If we are doing a device replace wait for any tasks
3823 * that started dellaloc right before we set the block
3824 * group to RO mode, as they might have just allocated
3825 * an extent from it or decided they could do a nocow
3826 * write. And if any such tasks did that, wait for their
3827 * ordered extents to complete and then commit the
3828 * current transaction, so that we can later see the new
3829 * extent items in the extent tree - the ordered extents
3830 * create delayed data references (for cow writes) when
3831 * they complete, which will be run and insert the
3832 * corresponding extent items into the extent tree when
3833 * we commit the transaction they used when running
3834 * inode.c:btrfs_finish_ordered_io(). We later use
3835 * the commit root of the extent tree to find extents
3836 * to copy from the srcdev into the tgtdev, and we don't
3837 * want to miss any new extents.
3838 */
3839 btrfs_wait_block_group_reservations(cache);
3840 btrfs_wait_nocow_writers(cache);
3841 ret = btrfs_wait_ordered_roots(fs_info, -1,
3842 cache->key.objectid,
3843 cache->key.offset);
3844 if (ret > 0) {
3845 struct btrfs_trans_handle *trans;
3846
3847 trans = btrfs_join_transaction(root);
3848 if (IS_ERR(trans))
3849 ret = PTR_ERR(trans);
3850 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003851 ret = btrfs_commit_transaction(trans);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003852 if (ret) {
3853 scrub_pause_off(fs_info);
3854 btrfs_put_block_group(cache);
3855 break;
3856 }
3857 }
3858 }
Zhaolei55e3a602015-08-05 16:43:30 +08003859 scrub_pause_off(fs_info);
Zhaolei76a8efa2015-11-17 18:46:17 +08003860
3861 if (ret == 0) {
3862 ro_set = 1;
3863 } else if (ret == -ENOSPC) {
3864 /*
3865 * btrfs_inc_block_group_ro return -ENOSPC when it
3866 * failed in creating new chunk for metadata.
3867 * It is not a problem for scrub/replace, because
3868 * metadata are always cowed, and our scrub paused
3869 * commit_transactions.
3870 */
3871 ro_set = 0;
3872 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003873 btrfs_warn(fs_info,
3874 "failed setting block group ro, ret=%d\n",
Zhaolei76a8efa2015-11-17 18:46:17 +08003875 ret);
Zhaolei55e3a602015-08-05 16:43:30 +08003876 btrfs_put_block_group(cache);
3877 break;
3878 }
3879
Filipe Manana81e87a72016-05-14 16:32:35 +01003880 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003881 dev_replace->cursor_right = found_key.offset + length;
3882 dev_replace->cursor_left = found_key.offset;
3883 dev_replace->item_needs_writeback = 1;
Filipe Manana81e87a72016-05-14 16:32:35 +01003884 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
Zhao Lei8c204c92015-08-19 15:02:40 +08003885 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003886 found_key.offset, cache, is_dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003887
3888 /*
3889 * flush, submit all pending read and write bios, afterwards
3890 * wait for them.
3891 * Note that in the dev replace case, a read request causes
3892 * write requests that are submitted in the read completion
3893 * worker. Therefore in the current situation, it is required
3894 * that all write requests are flushed, so that all read and
3895 * write requests are really completed when bios_in_flight
3896 * changes to 0.
3897 */
David Sterba3fb99302017-05-16 19:10:32 +02003898 atomic_set(&sctx->flush_all_writes, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003899 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003900 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003901 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003902 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003903
3904 wait_event(sctx->list_wait,
3905 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003906
3907 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003908
3909 /*
3910 * must be called before we decrease @scrub_paused.
3911 * make sure we don't block transaction commit while
3912 * we are waiting pending workers finished.
3913 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003914 wait_event(sctx->list_wait,
3915 atomic_read(&sctx->workers_pending) == 0);
David Sterba3fb99302017-05-16 19:10:32 +02003916 atomic_set(&sctx->flush_all_writes, 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08003917
Zhaoleib708ce92015-08-05 16:43:29 +08003918 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003919
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003920 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
3921 dev_replace->cursor_left = dev_replace->cursor_right;
3922 dev_replace->item_needs_writeback = 1;
3923 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
3924
Zhaolei76a8efa2015-11-17 18:46:17 +08003925 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003926 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003927
Filipe Manana758f2df2015-11-19 11:45:48 +00003928 /*
3929 * We might have prevented the cleaner kthread from deleting
3930 * this block group if it was already unused because we raced
3931 * and set it to RO mode first. So add it back to the unused
3932 * list, otherwise it might not ever be deleted unless a manual
3933 * balance is triggered or it becomes used and unused again.
3934 */
3935 spin_lock(&cache->lock);
3936 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3937 btrfs_block_group_used(&cache->item) == 0) {
3938 spin_unlock(&cache->lock);
3939 spin_lock(&fs_info->unused_bgs_lock);
3940 if (list_empty(&cache->bg_list)) {
3941 btrfs_get_block_group(cache);
3942 list_add_tail(&cache->bg_list,
3943 &fs_info->unused_bgs);
3944 }
3945 spin_unlock(&fs_info->unused_bgs_lock);
3946 } else {
3947 spin_unlock(&cache->lock);
3948 }
3949
Arne Jansena2de7332011-03-08 14:14:00 +01003950 btrfs_put_block_group(cache);
3951 if (ret)
3952 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003953 if (is_dev_replace &&
3954 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003955 ret = -EIO;
3956 break;
3957 }
3958 if (sctx->stat.malloc_errors > 0) {
3959 ret = -ENOMEM;
3960 break;
3961 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003962skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003963 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003964 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003965 }
3966
Arne Jansena2de7332011-03-08 14:14:00 +01003967 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003968
Zhaolei55e3a602015-08-05 16:43:30 +08003969 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003970}
3971
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003972static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3973 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003974{
3975 int i;
3976 u64 bytenr;
3977 u64 gen;
3978 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003979 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003980
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003981 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003982 return -EIO;
3983
Miao Xie5f546062014-07-24 11:37:09 +08003984 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003985 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08003986 gen = scrub_dev->generation;
3987 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003988 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003989
3990 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3991 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003992 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3993 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003994 break;
3995
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003996 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003997 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003998 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003999 if (ret)
4000 return ret;
4001 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004002 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004003
4004 return 0;
4005}
4006
4007/*
4008 * get a reference count on fs_info->scrub_workers. start worker if necessary
4009 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01004010static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
4011 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004012{
David Sterba6f011052015-02-16 18:34:01 +01004013 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08004014 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01004015
Arne Jansen632dd772011-06-10 12:07:07 +02004016 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004017 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08004018 fs_info->scrub_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004019 btrfs_alloc_workqueue(fs_info, "scrub", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004020 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004021 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08004022 fs_info->scrub_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004023 btrfs_alloc_workqueue(fs_info, "scrub", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004024 max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08004025 if (!fs_info->scrub_workers)
4026 goto fail_scrub_workers;
4027
Qu Wenruo0339ef22014-02-28 10:46:17 +08004028 fs_info->scrub_wr_completion_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004029 btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004030 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004031 if (!fs_info->scrub_wr_completion_workers)
4032 goto fail_scrub_wr_completion_workers;
4033
Qu Wenruo0339ef22014-02-28 10:46:17 +08004034 fs_info->scrub_nocow_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004035 btrfs_alloc_workqueue(fs_info, "scrubnc", flags, 1, 0);
Zhao Leie82afc52015-06-12 20:36:58 +08004036 if (!fs_info->scrub_nocow_workers)
4037 goto fail_scrub_nocow_workers;
Zhao Lei20b2e302015-06-04 20:09:15 +08004038 fs_info->scrub_parity_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004039 btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
Zhao Lei20b2e302015-06-04 20:09:15 +08004040 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004041 if (!fs_info->scrub_parity_workers)
4042 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02004043 }
Arne Jansena2de7332011-03-08 14:14:00 +01004044 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08004045 return 0;
4046
4047fail_scrub_parity_workers:
4048 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
4049fail_scrub_nocow_workers:
4050 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4051fail_scrub_wr_completion_workers:
4052 btrfs_destroy_workqueue(fs_info->scrub_workers);
4053fail_scrub_workers:
4054 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01004055}
4056
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004057static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004058{
Stefan Behrensff023aa2012-11-06 11:43:11 +01004059 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08004060 btrfs_destroy_workqueue(fs_info->scrub_workers);
4061 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4062 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08004063 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004064 }
Arne Jansena2de7332011-03-08 14:14:00 +01004065 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004066}
4067
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004068int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
4069 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004070 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004071{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004072 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004073 int ret;
4074 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08004075 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01004076
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004077 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01004078 return -EINVAL;
4079
Jeff Mahoneyda170662016-06-15 09:22:56 -04004080 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004081 /*
4082 * in this case scrub is unable to calculate the checksum
4083 * the way scrub is implemented. Do not handle this
4084 * situation at all because it won't ever happen.
4085 */
Frank Holtonefe120a2013-12-20 11:37:06 -05004086 btrfs_err(fs_info,
4087 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004088 fs_info->nodesize,
4089 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004090 return -EINVAL;
4091 }
4092
Jeff Mahoneyda170662016-06-15 09:22:56 -04004093 if (fs_info->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004094 /* not supported for data w/o checksums */
Chandan Rajendra751bebb2016-07-04 10:04:39 +05304095 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004096 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004097 fs_info->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01004098 return -EINVAL;
4099 }
4100
Jeff Mahoneyda170662016-06-15 09:22:56 -04004101 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004102 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04004103 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004104 /*
4105 * would exhaust the array bounds of pagev member in
4106 * struct scrub_block
4107 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004108 btrfs_err(fs_info,
4109 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004110 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004111 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004112 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004113 SCRUB_MAX_PAGES_PER_BLOCK);
4114 return -EINVAL;
4115 }
4116
Arne Jansena2de7332011-03-08 14:14:00 +01004117
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004118 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4119 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01004120 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004121 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004122 return -ENODEV;
4123 }
Arne Jansena2de7332011-03-08 14:14:00 +01004124
Miao Xie5d68da32014-07-24 11:37:07 +08004125 if (!is_dev_replace && !readonly && !dev->writeable) {
4126 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4127 rcu_read_lock();
4128 name = rcu_dereference(dev->name);
4129 btrfs_err(fs_info, "scrub: device %s is not writable",
4130 name->str);
4131 rcu_read_unlock();
4132 return -EROFS;
4133 }
4134
Wang Shilong3b7a0162013-10-12 02:11:12 +08004135 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01004136 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01004137 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004138 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004139 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01004140 }
4141
Liu Bo73beece2015-07-17 16:49:19 +08004142 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01004143 if (dev->scrub_device ||
4144 (!is_dev_replace &&
4145 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
Liu Bo73beece2015-07-17 16:49:19 +08004146 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004147 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004148 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004149 return -EINPROGRESS;
4150 }
Liu Bo73beece2015-07-17 16:49:19 +08004151 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Wang Shilong3b7a0162013-10-12 02:11:12 +08004152
4153 ret = scrub_workers_get(fs_info, is_dev_replace);
4154 if (ret) {
4155 mutex_unlock(&fs_info->scrub_lock);
4156 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4157 return ret;
4158 }
4159
Stefan Behrens63a212a2012-11-05 18:29:28 +01004160 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004161 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004162 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004163 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4164 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004165 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004166 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004167 sctx->readonly = readonly;
4168 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08004169 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004170
Wang Shilong3cb09292013-12-04 21:15:19 +08004171 /*
4172 * checking @scrub_pause_req here, we can avoid
4173 * race between committing transaction and scrubbing.
4174 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08004175 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004176 atomic_inc(&fs_info->scrubs_running);
4177 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004178
Stefan Behrensff023aa2012-11-06 11:43:11 +01004179 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08004180 /*
4181 * by holding device list mutex, we can
4182 * kick off writing super in log tree sync.
4183 */
Wang Shilong3cb09292013-12-04 21:15:19 +08004184 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004185 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08004186 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004187 }
Arne Jansena2de7332011-03-08 14:14:00 +01004188
4189 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004190 ret = scrub_enumerate_chunks(sctx, dev, start, end,
4191 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01004192
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004193 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004194 atomic_dec(&fs_info->scrubs_running);
4195 wake_up(&fs_info->scrub_pause_wait);
4196
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004197 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02004198
Arne Jansena2de7332011-03-08 14:14:00 +01004199 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004200 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01004201
4202 mutex_lock(&fs_info->scrub_lock);
4203 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08004204 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004205 mutex_unlock(&fs_info->scrub_lock);
4206
Filipe Mananaf55985f2015-02-09 21:14:24 +00004207 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004208
4209 return ret;
4210}
4211
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004212void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004213{
Arne Jansena2de7332011-03-08 14:14:00 +01004214 mutex_lock(&fs_info->scrub_lock);
4215 atomic_inc(&fs_info->scrub_pause_req);
4216 while (atomic_read(&fs_info->scrubs_paused) !=
4217 atomic_read(&fs_info->scrubs_running)) {
4218 mutex_unlock(&fs_info->scrub_lock);
4219 wait_event(fs_info->scrub_pause_wait,
4220 atomic_read(&fs_info->scrubs_paused) ==
4221 atomic_read(&fs_info->scrubs_running));
4222 mutex_lock(&fs_info->scrub_lock);
4223 }
4224 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004225}
4226
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004227void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004228{
Arne Jansena2de7332011-03-08 14:14:00 +01004229 atomic_dec(&fs_info->scrub_pause_req);
4230 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01004231}
4232
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004233int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004234{
Arne Jansena2de7332011-03-08 14:14:00 +01004235 mutex_lock(&fs_info->scrub_lock);
4236 if (!atomic_read(&fs_info->scrubs_running)) {
4237 mutex_unlock(&fs_info->scrub_lock);
4238 return -ENOTCONN;
4239 }
4240
4241 atomic_inc(&fs_info->scrub_cancel_req);
4242 while (atomic_read(&fs_info->scrubs_running)) {
4243 mutex_unlock(&fs_info->scrub_lock);
4244 wait_event(fs_info->scrub_pause_wait,
4245 atomic_read(&fs_info->scrubs_running) == 0);
4246 mutex_lock(&fs_info->scrub_lock);
4247 }
4248 atomic_dec(&fs_info->scrub_cancel_req);
4249 mutex_unlock(&fs_info->scrub_lock);
4250
4251 return 0;
4252}
4253
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004254int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
4255 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004256{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004257 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004258
4259 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004260 sctx = dev->scrub_device;
4261 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004262 mutex_unlock(&fs_info->scrub_lock);
4263 return -ENOTCONN;
4264 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004265 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01004266 while (dev->scrub_device) {
4267 mutex_unlock(&fs_info->scrub_lock);
4268 wait_event(fs_info->scrub_pause_wait,
4269 dev->scrub_device == NULL);
4270 mutex_lock(&fs_info->scrub_lock);
4271 }
4272 mutex_unlock(&fs_info->scrub_lock);
4273
4274 return 0;
4275}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004276
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004277int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01004278 struct btrfs_scrub_progress *progress)
4279{
4280 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004281 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004282
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004283 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4284 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004285 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004286 sctx = dev->scrub_device;
4287 if (sctx)
4288 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004289 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004290
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004291 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004292}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004293
4294static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4295 u64 extent_logical, u64 extent_len,
4296 u64 *extent_physical,
4297 struct btrfs_device **extent_dev,
4298 int *extent_mirror_num)
4299{
4300 u64 mapped_length;
4301 struct btrfs_bio *bbio = NULL;
4302 int ret;
4303
4304 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004305 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004306 &mapped_length, &bbio, 0);
4307 if (ret || !bbio || mapped_length < extent_len ||
4308 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08004309 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004310 return;
4311 }
4312
4313 *extent_physical = bbio->stripes[0].physical;
4314 *extent_mirror_num = bbio->mirror_num;
4315 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004316 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004317}
4318
Stefan Behrensff023aa2012-11-06 11:43:11 +01004319static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
4320 int mirror_num, u64 physical_for_dev_replace)
4321{
4322 struct scrub_copy_nocow_ctx *nocow_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004323 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004324
4325 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
4326 if (!nocow_ctx) {
4327 spin_lock(&sctx->stat_lock);
4328 sctx->stat.malloc_errors++;
4329 spin_unlock(&sctx->stat_lock);
4330 return -ENOMEM;
4331 }
4332
4333 scrub_pending_trans_workers_inc(sctx);
4334
4335 nocow_ctx->sctx = sctx;
4336 nocow_ctx->logical = logical;
4337 nocow_ctx->len = len;
4338 nocow_ctx->mirror_num = mirror_num;
4339 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08004340 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
4341 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04004342 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08004343 btrfs_queue_work(fs_info->scrub_nocow_workers,
4344 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004345
4346 return 0;
4347}
4348
Josef Bacik652f25a2013-09-12 16:58:28 -04004349static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
4350{
4351 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
4352 struct scrub_nocow_inode *nocow_inode;
4353
4354 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
4355 if (!nocow_inode)
4356 return -ENOMEM;
4357 nocow_inode->inum = inum;
4358 nocow_inode->offset = offset;
4359 nocow_inode->root = root;
4360 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
4361 return 0;
4362}
4363
4364#define COPY_COMPLETE 1
4365
Stefan Behrensff023aa2012-11-06 11:43:11 +01004366static void copy_nocow_pages_worker(struct btrfs_work *work)
4367{
4368 struct scrub_copy_nocow_ctx *nocow_ctx =
4369 container_of(work, struct scrub_copy_nocow_ctx, work);
4370 struct scrub_ctx *sctx = nocow_ctx->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004371 struct btrfs_fs_info *fs_info = sctx->fs_info;
4372 struct btrfs_root *root = fs_info->extent_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004373 u64 logical = nocow_ctx->logical;
4374 u64 len = nocow_ctx->len;
4375 int mirror_num = nocow_ctx->mirror_num;
4376 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4377 int ret;
4378 struct btrfs_trans_handle *trans = NULL;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004379 struct btrfs_path *path;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004380 int not_written = 0;
4381
Stefan Behrensff023aa2012-11-06 11:43:11 +01004382 path = btrfs_alloc_path();
4383 if (!path) {
4384 spin_lock(&sctx->stat_lock);
4385 sctx->stat.malloc_errors++;
4386 spin_unlock(&sctx->stat_lock);
4387 not_written = 1;
4388 goto out;
4389 }
4390
4391 trans = btrfs_join_transaction(root);
4392 if (IS_ERR(trans)) {
4393 not_written = 1;
4394 goto out;
4395 }
4396
4397 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04004398 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004399 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004400 btrfs_warn(fs_info,
4401 "iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d",
4402 logical, physical_for_dev_replace, len, mirror_num,
4403 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004404 not_written = 1;
4405 goto out;
4406 }
4407
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004408 btrfs_end_transaction(trans);
Josef Bacik652f25a2013-09-12 16:58:28 -04004409 trans = NULL;
4410 while (!list_empty(&nocow_ctx->inodes)) {
4411 struct scrub_nocow_inode *entry;
4412 entry = list_first_entry(&nocow_ctx->inodes,
4413 struct scrub_nocow_inode,
4414 list);
4415 list_del_init(&entry->list);
4416 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4417 entry->root, nocow_ctx);
4418 kfree(entry);
4419 if (ret == COPY_COMPLETE) {
4420 ret = 0;
4421 break;
4422 } else if (ret) {
4423 break;
4424 }
4425 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004426out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004427 while (!list_empty(&nocow_ctx->inodes)) {
4428 struct scrub_nocow_inode *entry;
4429 entry = list_first_entry(&nocow_ctx->inodes,
4430 struct scrub_nocow_inode,
4431 list);
4432 list_del_init(&entry->list);
4433 kfree(entry);
4434 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004435 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004436 btrfs_end_transaction(trans);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004437 if (not_written)
4438 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4439 num_uncorrectable_read_errors);
4440
4441 btrfs_free_path(path);
4442 kfree(nocow_ctx);
4443
4444 scrub_pending_trans_workers_dec(sctx);
4445}
4446
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004447static int check_extent_to_block(struct btrfs_inode *inode, u64 start, u64 len,
Gui Hecheng32159242014-11-10 15:36:08 +08004448 u64 logical)
4449{
4450 struct extent_state *cached_state = NULL;
4451 struct btrfs_ordered_extent *ordered;
4452 struct extent_io_tree *io_tree;
4453 struct extent_map *em;
4454 u64 lockstart = start, lockend = start + len - 1;
4455 int ret = 0;
4456
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004457 io_tree = &inode->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004458
David Sterbaff13db42015-12-03 14:30:40 +01004459 lock_extent_bits(io_tree, lockstart, lockend, &cached_state);
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004460 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
Gui Hecheng32159242014-11-10 15:36:08 +08004461 if (ordered) {
4462 btrfs_put_ordered_extent(ordered);
4463 ret = 1;
4464 goto out_unlock;
4465 }
4466
4467 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4468 if (IS_ERR(em)) {
4469 ret = PTR_ERR(em);
4470 goto out_unlock;
4471 }
4472
4473 /*
4474 * This extent does not actually cover the logical extent anymore,
4475 * move on to the next inode.
4476 */
4477 if (em->block_start > logical ||
4478 em->block_start + em->block_len < logical + len) {
4479 free_extent_map(em);
4480 ret = 1;
4481 goto out_unlock;
4482 }
4483 free_extent_map(em);
4484
4485out_unlock:
4486 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4487 GFP_NOFS);
4488 return ret;
4489}
4490
Josef Bacik652f25a2013-09-12 16:58:28 -04004491static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4492 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004493{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004494 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004495 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004496 struct inode *inode;
4497 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004498 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004499 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004500 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004501 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004502 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004503 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004504 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004505 int ret = 0;
4506 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004507
4508 key.objectid = root;
4509 key.type = BTRFS_ROOT_ITEM_KEY;
4510 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004511
4512 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4513
Stefan Behrensff023aa2012-11-06 11:43:11 +01004514 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004515 if (IS_ERR(local_root)) {
4516 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004517 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004518 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004519
4520 key.type = BTRFS_INODE_ITEM_KEY;
4521 key.objectid = inum;
4522 key.offset = 0;
4523 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004524 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004525 if (IS_ERR(inode))
4526 return PTR_ERR(inode);
4527
Miao Xieedd14002013-06-27 18:51:00 +08004528 /* Avoid truncate/dio/punch hole.. */
Al Viro59551022016-01-22 15:40:57 -05004529 inode_lock(inode);
Miao Xieedd14002013-06-27 18:51:00 +08004530 inode_dio_wait(inode);
4531
Stefan Behrensff023aa2012-11-06 11:43:11 +01004532 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004533 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004534 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004535
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004536 ret = check_extent_to_block(BTRFS_I(inode), offset, len,
4537 nocow_ctx_logical);
Gui Hecheng32159242014-11-10 15:36:08 +08004538 if (ret) {
4539 ret = ret > 0 ? 0 : ret;
4540 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004541 }
4542
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004543 while (len >= PAGE_SIZE) {
4544 index = offset >> PAGE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004545again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004546 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4547 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004548 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004549 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004550 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004551 }
4552
4553 if (PageUptodate(page)) {
4554 if (PageDirty(page))
4555 goto next_page;
4556 } else {
4557 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004558 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004559 btrfs_get_extent,
4560 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004561 if (err) {
4562 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004563 goto next_page;
4564 }
Miao Xieedd14002013-06-27 18:51:00 +08004565
Miao Xie26b258912013-06-27 18:50:58 +08004566 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004567 /*
4568 * If the page has been remove from the page cache,
4569 * the data on it is meaningless, because it may be
4570 * old one, the new data may be written into the new
4571 * page in the page cache.
4572 */
4573 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004574 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004575 put_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004576 goto again;
4577 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004578 if (!PageUptodate(page)) {
4579 ret = -EIO;
4580 goto next_page;
4581 }
4582 }
Gui Hecheng32159242014-11-10 15:36:08 +08004583
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004584 ret = check_extent_to_block(BTRFS_I(inode), offset, len,
Gui Hecheng32159242014-11-10 15:36:08 +08004585 nocow_ctx_logical);
4586 if (ret) {
4587 ret = ret > 0 ? 0 : ret;
4588 goto next_page;
4589 }
4590
Miao Xie826aa0a2013-06-27 18:50:59 +08004591 err = write_page_nocow(nocow_ctx->sctx,
4592 physical_for_dev_replace, page);
4593 if (err)
4594 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004595next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004596 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004597 put_page(page);
Miao Xie826aa0a2013-06-27 18:50:59 +08004598
4599 if (ret)
4600 break;
4601
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004602 offset += PAGE_SIZE;
4603 physical_for_dev_replace += PAGE_SIZE;
4604 nocow_ctx_logical += PAGE_SIZE;
4605 len -= PAGE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004606 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004607 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004608out:
Al Viro59551022016-01-22 15:40:57 -05004609 inode_unlock(inode);
Miao Xie826aa0a2013-06-27 18:50:59 +08004610 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004611 return ret;
4612}
4613
4614static int write_page_nocow(struct scrub_ctx *sctx,
4615 u64 physical_for_dev_replace, struct page *page)
4616{
4617 struct bio *bio;
4618 struct btrfs_device *dev;
4619 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004620
David Sterba3fb99302017-05-16 19:10:32 +02004621 dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004622 if (!dev)
4623 return -EIO;
4624 if (!dev->bdev) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004625 btrfs_warn_rl(dev->fs_info,
David Sterba94647322015-10-08 11:01:36 +02004626 "scrub write_page_nocow(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004627 return -EIO;
4628 }
Chris Mason9be33952013-05-17 18:30:14 -04004629 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004630 bio->bi_iter.bi_size = 0;
4631 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004632 bio->bi_bdev = dev->bdev;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06004633 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004634 ret = bio_add_page(bio, page, PAGE_SIZE, 0);
4635 if (ret != PAGE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004636leave_with_eio:
4637 bio_put(bio);
4638 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4639 return -EIO;
4640 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004641
Mike Christie4e49ea42016-06-05 14:31:41 -05004642 if (btrfsic_submit_bio_wait(bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004643 goto leave_with_eio;
4644
4645 bio_put(bio);
4646 return 0;
4647}