blob: d766c73eb29a63d416793a3eef9920be6a5827e2 [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;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020099 blk_status_t status;
Arne Jansena2de7332011-03-08 14:14:00 +0100100 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 */
David Sterba3fb99302017-05-16 19:10:32 +0200185 struct btrfs_device *wr_tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200186 bool flush_all_writes;
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;
David Sterba6aa21262017-10-04 17:07:07 +0200234 u64 physical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200235 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) {
David Sterbaded56182017-06-26 15:19:00 +0200717 WARN_ON(!fs_info->dev_replace.tgtdev);
David Sterba3fb99302017-05-16 19:10:32 +0200718 sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
David Sterbaded56182017-06-26 15:19:00 +0200719 sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200720 sctx->flush_all_writes = false;
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,
David Sterba6aa21262017-10-04 17:07:07 +0200800"%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400801 swarn->errstr, swarn->logical,
802 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200803 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400804 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,
David Sterba6aa21262017-10-04 17:07:07 +0200813 "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400814 swarn->errstr, swarn->logical,
815 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200816 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400817 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
David Sterba6aa21262017-10-04 17:07:07 +0200848 swarn.physical = sblock->pagev[0]->physical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100849 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,
David Sterba6aa21262017-10-04 17:07:07 +0200871"%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400872 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400873 rcu_str_deref(dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200874 swarn.physical,
Jan Schmidt558540c2011-06-13 19:59:12 +0200875 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,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400886 scrub_print_warning_inode, &swarn, false);
Jan Schmidt558540c2011-06-13 19:59:12 +0200887 }
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,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001050 scrub_fixup_readpage, fixup, false);
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
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001669static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001670{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001671 complete(bio->bi_private);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001672}
1673
1674static inline int scrub_is_page_on_raid56(struct scrub_page *page)
1675{
Zhao Lei10f11902015-01-20 15:11:43 +08001676 return page->recover &&
Zhao Leiffe2d202015-01-20 15:11:44 +08001677 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001678}
1679
1680static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1681 struct bio *bio,
1682 struct scrub_page *page)
1683{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001684 DECLARE_COMPLETION_ONSTACK(done);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001685 int ret;
1686
Miao Xieaf8e2d12014-10-23 14:42:50 +08001687 bio->bi_iter.bi_sector = page->logical >> 9;
1688 bio->bi_private = &done;
1689 bio->bi_end_io = scrub_bio_wait_endio;
1690
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001691 ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001692 page->recover->map_length,
Miao Xie42452152014-11-25 16:39:28 +08001693 page->mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001694 if (ret)
1695 return ret;
1696
Liu Bob4ff5ad2017-11-30 17:26:39 -07001697 wait_for_completion_io(&done);
1698 return blk_status_to_errno(bio->bi_status);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001699}
1700
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001701/*
1702 * this function will check the on disk data for checksum errors, header
1703 * errors and read I/O errors. If any I/O errors happen, the exact pages
1704 * which are errored are marked as being bad. The goal is to enable scrub
1705 * to take those pages that are not errored from all the mirrors so that
1706 * the pages that are errored in the just handled mirror can be repaired.
1707 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001708static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001709 struct scrub_block *sblock,
1710 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001711{
1712 int page_num;
1713
1714 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001715
1716 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1717 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001718 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001719
Stefan Behrens442a4f62012-05-25 16:06:08 +02001720 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001721 page->io_error = 1;
1722 sblock->no_io_error_seen = 0;
1723 continue;
1724 }
1725
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001726 WARN_ON(!page->page);
David Sterbac5e4c3d2017-06-12 17:29:41 +02001727 bio = btrfs_io_bio_alloc(1);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001728 bio_set_dev(bio, page->dev->bdev);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001729
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001730 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001731 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001732 if (scrub_submit_raid56_bio_wait(fs_info, bio, page)) {
1733 page->io_error = 1;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001734 sblock->no_io_error_seen = 0;
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001735 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001736 } else {
1737 bio->bi_iter.bi_sector = page->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001738 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001739
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001740 if (btrfsic_submit_bio_wait(bio)) {
1741 page->io_error = 1;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001742 sblock->no_io_error_seen = 0;
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001743 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001744 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001745
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001746 bio_put(bio);
1747 }
1748
1749 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001750 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001751}
1752
Miao Xie17a9be22014-07-24 11:37:08 +08001753static inline int scrub_check_fsid(u8 fsid[],
1754 struct scrub_page *spage)
1755{
1756 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1757 int ret;
1758
Anand Jain44880fd2017-07-29 17:50:09 +08001759 ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Miao Xie17a9be22014-07-24 11:37:08 +08001760 return !ret;
1761}
1762
Zhao Leiba7cf982015-08-24 21:18:02 +08001763static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001764{
Zhao Leiba7cf982015-08-24 21:18:02 +08001765 sblock->header_error = 0;
1766 sblock->checksum_error = 0;
1767 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001768
Zhao Leiba7cf982015-08-24 21:18:02 +08001769 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1770 scrub_checksum_data(sblock);
1771 else
1772 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001773}
1774
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001775static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001776 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001777{
1778 int page_num;
1779 int ret = 0;
1780
1781 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1782 int ret_sub;
1783
1784 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1785 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001786 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001787 if (ret_sub)
1788 ret = ret_sub;
1789 }
1790
1791 return ret;
1792}
1793
1794static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1795 struct scrub_block *sblock_good,
1796 int page_num, int force_write)
1797{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001798 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1799 struct scrub_page *page_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001800 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001801
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001802 BUG_ON(page_bad->page == NULL);
1803 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001804 if (force_write || sblock_bad->header_error ||
1805 sblock_bad->checksum_error || page_bad->io_error) {
1806 struct bio *bio;
1807 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001808
Stefan Behrensff023aa2012-11-06 11:43:11 +01001809 if (!page_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001810 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001811 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001812 return -EIO;
1813 }
1814
David Sterbac5e4c3d2017-06-12 17:29:41 +02001815 bio = btrfs_io_bio_alloc(1);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001816 bio_set_dev(bio, page_bad->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001817 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001818 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001819
1820 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1821 if (PAGE_SIZE != ret) {
1822 bio_put(bio);
1823 return -EIO;
1824 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001825
Mike Christie4e49ea42016-06-05 14:31:41 -05001826 if (btrfsic_submit_bio_wait(bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001827 btrfs_dev_stat_inc_and_print(page_bad->dev,
1828 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001829 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001830 &fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001831 bio_put(bio);
1832 return -EIO;
1833 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001834 bio_put(bio);
1835 }
1836
1837 return 0;
1838}
1839
Stefan Behrensff023aa2012-11-06 11:43:11 +01001840static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1841{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001842 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001843 int page_num;
1844
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001845 /*
1846 * This block is used for the check of the parity on the source device,
1847 * so the data needn't be written into the destination device.
1848 */
1849 if (sblock->sparity)
1850 return;
1851
Stefan Behrensff023aa2012-11-06 11:43:11 +01001852 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1853 int ret;
1854
1855 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1856 if (ret)
1857 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001858 &fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001859 }
1860}
1861
1862static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1863 int page_num)
1864{
1865 struct scrub_page *spage = sblock->pagev[page_num];
1866
1867 BUG_ON(spage->page == NULL);
1868 if (spage->io_error) {
1869 void *mapped_buffer = kmap_atomic(spage->page);
1870
David Sterba619a9742017-03-29 20:48:44 +02001871 clear_page(mapped_buffer);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001872 flush_dcache_page(spage->page);
1873 kunmap_atomic(mapped_buffer);
1874 }
1875 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1876}
1877
1878static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1879 struct scrub_page *spage)
1880{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001881 struct scrub_bio *sbio;
1882 int ret;
1883
David Sterba3fb99302017-05-16 19:10:32 +02001884 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001885again:
David Sterba3fb99302017-05-16 19:10:32 +02001886 if (!sctx->wr_curr_bio) {
1887 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001888 GFP_KERNEL);
David Sterba3fb99302017-05-16 19:10:32 +02001889 if (!sctx->wr_curr_bio) {
1890 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001891 return -ENOMEM;
1892 }
David Sterba3fb99302017-05-16 19:10:32 +02001893 sctx->wr_curr_bio->sctx = sctx;
1894 sctx->wr_curr_bio->page_count = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001895 }
David Sterba3fb99302017-05-16 19:10:32 +02001896 sbio = sctx->wr_curr_bio;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001897 if (sbio->page_count == 0) {
1898 struct bio *bio;
1899
1900 sbio->physical = spage->physical_for_dev_replace;
1901 sbio->logical = spage->logical;
David Sterba3fb99302017-05-16 19:10:32 +02001902 sbio->dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001903 bio = sbio->bio;
1904 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02001905 bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001906 sbio->bio = bio;
1907 }
1908
1909 bio->bi_private = sbio;
1910 bio->bi_end_io = scrub_wr_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001911 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001912 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001913 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001914 sbio->status = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001915 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1916 spage->physical_for_dev_replace ||
1917 sbio->logical + sbio->page_count * PAGE_SIZE !=
1918 spage->logical) {
1919 scrub_wr_submit(sctx);
1920 goto again;
1921 }
1922
1923 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1924 if (ret != PAGE_SIZE) {
1925 if (sbio->page_count < 1) {
1926 bio_put(sbio->bio);
1927 sbio->bio = NULL;
David Sterba3fb99302017-05-16 19:10:32 +02001928 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001929 return -EIO;
1930 }
1931 scrub_wr_submit(sctx);
1932 goto again;
1933 }
1934
1935 sbio->pagev[sbio->page_count] = spage;
1936 scrub_page_get(spage);
1937 sbio->page_count++;
David Sterba3fb99302017-05-16 19:10:32 +02001938 if (sbio->page_count == sctx->pages_per_wr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001939 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02001940 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001941
1942 return 0;
1943}
1944
1945static void scrub_wr_submit(struct scrub_ctx *sctx)
1946{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001947 struct scrub_bio *sbio;
1948
David Sterba3fb99302017-05-16 19:10:32 +02001949 if (!sctx->wr_curr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001950 return;
1951
David Sterba3fb99302017-05-16 19:10:32 +02001952 sbio = sctx->wr_curr_bio;
1953 sctx->wr_curr_bio = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001954 WARN_ON(!sbio->bio->bi_disk);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001955 scrub_pending_bio_inc(sctx);
1956 /* process all writes in a single worker thread. Then the block layer
1957 * orders the requests before sending them to the driver which
1958 * doubled the write performance on spinning disks when measured
1959 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001960 btrfsic_submit_bio(sbio->bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001961}
1962
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001963static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001964{
1965 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001966 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001967
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001968 sbio->status = bio->bi_status;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001969 sbio->bio = bio;
1970
Liu Bo9e0af232014-08-15 23:36:53 +08001971 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
1972 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001973 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001974}
1975
1976static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
1977{
1978 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1979 struct scrub_ctx *sctx = sbio->sctx;
1980 int i;
1981
1982 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001983 if (sbio->status) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001984 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001985 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001986
1987 for (i = 0; i < sbio->page_count; i++) {
1988 struct scrub_page *spage = sbio->pagev[i];
1989
1990 spage->io_error = 1;
1991 btrfs_dev_replace_stats_inc(&dev_replace->
1992 num_write_errors);
1993 }
1994 }
1995
1996 for (i = 0; i < sbio->page_count; i++)
1997 scrub_page_put(sbio->pagev[i]);
1998
1999 bio_put(sbio->bio);
2000 kfree(sbio);
2001 scrub_pending_bio_dec(sctx);
2002}
2003
2004static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002005{
2006 u64 flags;
2007 int ret;
2008
Zhao Leiba7cf982015-08-24 21:18:02 +08002009 /*
2010 * No need to initialize these stats currently,
2011 * because this function only use return value
2012 * instead of these stats value.
2013 *
2014 * Todo:
2015 * always use stats
2016 */
2017 sblock->header_error = 0;
2018 sblock->generation_error = 0;
2019 sblock->checksum_error = 0;
2020
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002021 WARN_ON(sblock->page_count < 1);
2022 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002023 ret = 0;
2024 if (flags & BTRFS_EXTENT_FLAG_DATA)
2025 ret = scrub_checksum_data(sblock);
2026 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
2027 ret = scrub_checksum_tree_block(sblock);
2028 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
2029 (void)scrub_checksum_super(sblock);
2030 else
2031 WARN_ON(1);
2032 if (ret)
2033 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002034
2035 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002036}
2037
2038static int scrub_checksum_data(struct scrub_block *sblock)
2039{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002040 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002041 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002042 u8 *on_disk_csum;
2043 struct page *page;
2044 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01002045 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002046 u64 len;
2047 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002048
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002049 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002050 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002051 return 0;
2052
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002053 on_disk_csum = sblock->pagev[0]->csum;
2054 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002055 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002056
David Sterba25cc1222017-05-16 19:10:41 +02002057 len = sctx->fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002058 index = 0;
2059 for (;;) {
2060 u64 l = min_t(u64, len, PAGE_SIZE);
2061
Liu Bob0496682013-03-14 14:57:45 +00002062 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002063 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002064 len -= l;
2065 if (len == 0)
2066 break;
2067 index++;
2068 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002069 BUG_ON(!sblock->pagev[index]->page);
2070 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002071 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002072 }
2073
Arne Jansena2de7332011-03-08 14:14:00 +01002074 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002075 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002076 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002077
Zhao Leiba7cf982015-08-24 21:18:02 +08002078 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002079}
2080
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002081static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002082{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002083 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002084 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002085 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002086 u8 calculated_csum[BTRFS_CSUM_SIZE];
2087 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2088 struct page *page;
2089 void *mapped_buffer;
2090 u64 mapped_size;
2091 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002092 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002093 u64 len;
2094 int index;
2095
2096 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002097 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002098 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002099 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002100 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002101
2102 /*
2103 * we don't use the getter functions here, as we
2104 * a) don't have an extent buffer and
2105 * b) the page is already kmapped
2106 */
Qu Wenruo3cae2102013-07-16 11:19:18 +08002107 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08002108 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002109
Zhao Leiba7cf982015-08-24 21:18:02 +08002110 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
2111 sblock->header_error = 1;
2112 sblock->generation_error = 1;
2113 }
Arne Jansena2de7332011-03-08 14:14:00 +01002114
Miao Xie17a9be22014-07-24 11:37:08 +08002115 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Zhao Leiba7cf982015-08-24 21:18:02 +08002116 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002117
2118 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
2119 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08002120 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002121
David Sterba25cc1222017-05-16 19:10:41 +02002122 len = sctx->fs_info->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002123 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2124 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2125 index = 0;
2126 for (;;) {
2127 u64 l = min_t(u64, len, mapped_size);
2128
Liu Bob0496682013-03-14 14:57:45 +00002129 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002130 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002131 len -= l;
2132 if (len == 0)
2133 break;
2134 index++;
2135 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002136 BUG_ON(!sblock->pagev[index]->page);
2137 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002138 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002139 mapped_size = PAGE_SIZE;
2140 p = mapped_buffer;
2141 }
2142
2143 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002144 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002145 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002146
Zhao Leiba7cf982015-08-24 21:18:02 +08002147 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002148}
2149
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002150static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002151{
2152 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002153 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002154 u8 calculated_csum[BTRFS_CSUM_SIZE];
2155 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2156 struct page *page;
2157 void *mapped_buffer;
2158 u64 mapped_size;
2159 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002160 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002161 int fail_gen = 0;
2162 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002163 u64 len;
2164 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002165
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002166 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002167 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002168 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002169 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002170 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002171
Qu Wenruo3cae2102013-07-16 11:19:18 +08002172 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002173 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002174
Qu Wenruo3cae2102013-07-16 11:19:18 +08002175 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002176 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002177
Miao Xie17a9be22014-07-24 11:37:08 +08002178 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002179 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002180
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002181 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2182 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2183 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2184 index = 0;
2185 for (;;) {
2186 u64 l = min_t(u64, len, mapped_size);
2187
Liu Bob0496682013-03-14 14:57:45 +00002188 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002189 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002190 len -= l;
2191 if (len == 0)
2192 break;
2193 index++;
2194 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002195 BUG_ON(!sblock->pagev[index]->page);
2196 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002197 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002198 mapped_size = PAGE_SIZE;
2199 p = mapped_buffer;
2200 }
2201
2202 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002203 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002204 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002205
Stefan Behrens442a4f62012-05-25 16:06:08 +02002206 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002207 /*
2208 * if we find an error in a super block, we just report it.
2209 * They will get written with the next transaction commit
2210 * anyway
2211 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002212 spin_lock(&sctx->stat_lock);
2213 ++sctx->stat.super_errors;
2214 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002215 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002216 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002217 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2218 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002219 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002220 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002221 }
2222
Stefan Behrens442a4f62012-05-25 16:06:08 +02002223 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002224}
2225
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002226static void scrub_block_get(struct scrub_block *sblock)
2227{
Elena Reshetova186debd2017-03-03 10:55:23 +02002228 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002229}
2230
2231static void scrub_block_put(struct scrub_block *sblock)
2232{
Elena Reshetova186debd2017-03-03 10:55:23 +02002233 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002234 int i;
2235
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002236 if (sblock->sparity)
2237 scrub_parity_put(sblock->sparity);
2238
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002239 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002240 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002241 kfree(sblock);
2242 }
2243}
2244
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002245static void scrub_page_get(struct scrub_page *spage)
2246{
Zhao Lei57019342015-01-20 15:11:45 +08002247 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002248}
2249
2250static void scrub_page_put(struct scrub_page *spage)
2251{
Zhao Lei57019342015-01-20 15:11:45 +08002252 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002253 if (spage->page)
2254 __free_page(spage->page);
2255 kfree(spage);
2256 }
2257}
2258
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002259static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002260{
2261 struct scrub_bio *sbio;
2262
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002263 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002264 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002265
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002266 sbio = sctx->bios[sctx->curr];
2267 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002268 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002269 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002270}
2271
Stefan Behrensff023aa2012-11-06 11:43:11 +01002272static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2273 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002274{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002275 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002276 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002277 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002278
2279again:
2280 /*
2281 * grab a fresh bio or wait for one to become available
2282 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002283 while (sctx->curr == -1) {
2284 spin_lock(&sctx->list_lock);
2285 sctx->curr = sctx->first_free;
2286 if (sctx->curr != -1) {
2287 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2288 sctx->bios[sctx->curr]->next_free = -1;
2289 sctx->bios[sctx->curr]->page_count = 0;
2290 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002291 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002292 spin_unlock(&sctx->list_lock);
2293 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002294 }
2295 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002296 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002297 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002298 struct bio *bio;
2299
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002300 sbio->physical = spage->physical;
2301 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002302 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002303 bio = sbio->bio;
2304 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02002305 bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002306 sbio->bio = bio;
2307 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002308
2309 bio->bi_private = sbio;
2310 bio->bi_end_io = scrub_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002311 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002312 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05002313 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002314 sbio->status = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002315 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2316 spage->physical ||
2317 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002318 spage->logical ||
2319 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002320 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002321 goto again;
2322 }
2323
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002324 sbio->pagev[sbio->page_count] = spage;
2325 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2326 if (ret != PAGE_SIZE) {
2327 if (sbio->page_count < 1) {
2328 bio_put(sbio->bio);
2329 sbio->bio = NULL;
2330 return -EIO;
2331 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002332 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002333 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002334 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002335
Stefan Behrensff023aa2012-11-06 11:43:11 +01002336 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002337 atomic_inc(&sblock->outstanding_pages);
2338 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002339 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002340 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002341
2342 return 0;
2343}
2344
Linus Torvalds22365972015-09-05 15:14:43 -07002345static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002346{
2347 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002348 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002349
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002350 if (bio->bi_status)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002351 sblock->no_io_error_seen = 0;
2352
Scott Talbert46732722016-05-09 09:14:28 -04002353 bio_put(bio);
2354
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002355 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2356}
2357
2358static void scrub_missing_raid56_worker(struct btrfs_work *work)
2359{
2360 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2361 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002362 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002363 u64 logical;
2364 struct btrfs_device *dev;
2365
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002366 logical = sblock->pagev[0]->logical;
2367 dev = sblock->pagev[0]->dev;
2368
Zhao Leiaffe4a52015-08-24 21:32:06 +08002369 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002370 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002371
2372 if (!sblock->no_io_error_seen) {
2373 spin_lock(&sctx->stat_lock);
2374 sctx->stat.read_errors++;
2375 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002376 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002377 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002378 logical, rcu_str_deref(dev->name));
2379 } else if (sblock->header_error || sblock->checksum_error) {
2380 spin_lock(&sctx->stat_lock);
2381 sctx->stat.uncorrectable_errors++;
2382 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002383 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002384 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002385 logical, rcu_str_deref(dev->name));
2386 } else {
2387 scrub_write_block_to_dev_replace(sblock);
2388 }
2389
2390 scrub_block_put(sblock);
2391
David Sterba2073c4c2017-03-31 17:12:51 +02002392 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002393 mutex_lock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002394 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002395 mutex_unlock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002396 }
2397
2398 scrub_pending_bio_dec(sctx);
2399}
2400
2401static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2402{
2403 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002404 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002405 u64 length = sblock->page_count * PAGE_SIZE;
2406 u64 logical = sblock->pagev[0]->logical;
Zhao Leif1fee652016-05-17 17:37:38 +08002407 struct btrfs_bio *bbio = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002408 struct bio *bio;
2409 struct btrfs_raid_bio *rbio;
2410 int ret;
2411 int i;
2412
Qu Wenruoae6529c2017-03-29 09:33:21 +08002413 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002414 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
David Sterba825ad4c2017-03-28 14:45:22 +02002415 &length, &bbio);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002416 if (ret || !bbio || !bbio->raid_map)
2417 goto bbio_out;
2418
2419 if (WARN_ON(!sctx->is_dev_replace ||
2420 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2421 /*
2422 * We shouldn't be scrubbing a missing device. Even for dev
2423 * replace, we should only get here for RAID 5/6. We either
2424 * managed to mount something with no mirrors remaining or
2425 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2426 */
2427 goto bbio_out;
2428 }
2429
David Sterbac5e4c3d2017-06-12 17:29:41 +02002430 bio = btrfs_io_bio_alloc(0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002431 bio->bi_iter.bi_sector = logical >> 9;
2432 bio->bi_private = sblock;
2433 bio->bi_end_io = scrub_missing_raid56_end_io;
2434
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002435 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002436 if (!rbio)
2437 goto rbio_out;
2438
2439 for (i = 0; i < sblock->page_count; i++) {
2440 struct scrub_page *spage = sblock->pagev[i];
2441
2442 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2443 }
2444
2445 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2446 scrub_missing_raid56_worker, NULL, NULL);
2447 scrub_block_get(sblock);
2448 scrub_pending_bio_inc(sctx);
2449 raid56_submit_missing_rbio(rbio);
2450 return;
2451
2452rbio_out:
2453 bio_put(bio);
2454bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002455 btrfs_bio_counter_dec(fs_info);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002456 btrfs_put_bbio(bbio);
2457 spin_lock(&sctx->stat_lock);
2458 sctx->stat.malloc_errors++;
2459 spin_unlock(&sctx->stat_lock);
2460}
2461
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002462static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002463 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002464 u64 gen, int mirror_num, u8 *csum, int force,
2465 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002466{
2467 struct scrub_block *sblock;
2468 int index;
2469
David Sterba58c4e172016-02-11 10:49:42 +01002470 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002471 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002472 spin_lock(&sctx->stat_lock);
2473 sctx->stat.malloc_errors++;
2474 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002475 return -ENOMEM;
2476 }
2477
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002478 /* one ref inside this function, plus one for each page added to
2479 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002480 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002481 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002482 sblock->no_io_error_seen = 1;
2483
2484 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002485 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002486 u64 l = min_t(u64, len, PAGE_SIZE);
2487
David Sterba58c4e172016-02-11 10:49:42 +01002488 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002489 if (!spage) {
2490leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002491 spin_lock(&sctx->stat_lock);
2492 sctx->stat.malloc_errors++;
2493 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002494 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002495 return -ENOMEM;
2496 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002497 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2498 scrub_page_get(spage);
2499 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002500 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002501 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002502 spage->flags = flags;
2503 spage->generation = gen;
2504 spage->logical = logical;
2505 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002506 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002507 spage->mirror_num = mirror_num;
2508 if (csum) {
2509 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002510 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002511 } else {
2512 spage->have_csum = 0;
2513 }
2514 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002515 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002516 if (!spage->page)
2517 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002518 len -= l;
2519 logical += l;
2520 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002521 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002522 }
2523
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002524 WARN_ON(sblock->page_count == 0);
Anand Jaine6e674b2017-12-04 12:54:54 +08002525 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002526 /*
2527 * This case should only be hit for RAID 5/6 device replace. See
2528 * the comment in scrub_missing_raid56_pages() for details.
2529 */
2530 scrub_missing_raid56_pages(sblock);
2531 } else {
2532 for (index = 0; index < sblock->page_count; index++) {
2533 struct scrub_page *spage = sblock->pagev[index];
2534 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002535
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002536 ret = scrub_add_page_to_rd_bio(sctx, spage);
2537 if (ret) {
2538 scrub_block_put(sblock);
2539 return ret;
2540 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002541 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002542
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002543 if (force)
2544 scrub_submit(sctx);
2545 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002546
2547 /* last one frees, either here or in bio completion for last page */
2548 scrub_block_put(sblock);
2549 return 0;
2550}
2551
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002552static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002553{
2554 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002555 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002556
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002557 sbio->status = bio->bi_status;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002558 sbio->bio = bio;
2559
Qu Wenruo0339ef22014-02-28 10:46:17 +08002560 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002561}
2562
2563static void scrub_bio_end_io_worker(struct btrfs_work *work)
2564{
2565 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002566 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002567 int i;
2568
Stefan Behrensff023aa2012-11-06 11:43:11 +01002569 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002570 if (sbio->status) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002571 for (i = 0; i < sbio->page_count; i++) {
2572 struct scrub_page *spage = sbio->pagev[i];
2573
2574 spage->io_error = 1;
2575 spage->sblock->no_io_error_seen = 0;
2576 }
2577 }
2578
2579 /* now complete the scrub_block items that have all pages completed */
2580 for (i = 0; i < sbio->page_count; i++) {
2581 struct scrub_page *spage = sbio->pagev[i];
2582 struct scrub_block *sblock = spage->sblock;
2583
2584 if (atomic_dec_and_test(&sblock->outstanding_pages))
2585 scrub_block_complete(sblock);
2586 scrub_block_put(sblock);
2587 }
2588
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002589 bio_put(sbio->bio);
2590 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002591 spin_lock(&sctx->list_lock);
2592 sbio->next_free = sctx->first_free;
2593 sctx->first_free = sbio->index;
2594 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002595
David Sterba2073c4c2017-03-31 17:12:51 +02002596 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002597 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002598 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002599 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002600 }
2601
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002602 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002603}
2604
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002605static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2606 unsigned long *bitmap,
2607 u64 start, u64 len)
2608{
Liu Bo972d7212017-04-03 13:45:33 -07002609 u64 offset;
David Sterba7736b0a2017-03-31 18:02:48 +02002610 u64 nsectors64;
2611 u32 nsectors;
Jeff Mahoneyda170662016-06-15 09:22:56 -04002612 int sectorsize = sparity->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002613
2614 if (len >= sparity->stripe_len) {
2615 bitmap_set(bitmap, 0, sparity->nsectors);
2616 return;
2617 }
2618
2619 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002620 start = div64_u64_rem(start, sparity->stripe_len, &offset);
2621 offset = div_u64(offset, sectorsize);
David Sterba7736b0a2017-03-31 18:02:48 +02002622 nsectors64 = div_u64(len, sectorsize);
2623
2624 ASSERT(nsectors64 < UINT_MAX);
2625 nsectors = (u32)nsectors64;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002626
2627 if (offset + nsectors <= sparity->nsectors) {
2628 bitmap_set(bitmap, offset, nsectors);
2629 return;
2630 }
2631
2632 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2633 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2634}
2635
2636static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2637 u64 start, u64 len)
2638{
2639 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2640}
2641
2642static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2643 u64 start, u64 len)
2644{
2645 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2646}
2647
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002648static void scrub_block_complete(struct scrub_block *sblock)
2649{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002650 int corrupted = 0;
2651
Stefan Behrensff023aa2012-11-06 11:43:11 +01002652 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002653 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002654 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002655 } else {
2656 /*
2657 * if has checksum error, write via repair mechanism in
2658 * dev replace case, otherwise write here in dev replace
2659 * case.
2660 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002661 corrupted = scrub_checksum(sblock);
2662 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002663 scrub_write_block_to_dev_replace(sblock);
2664 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002665
2666 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2667 u64 start = sblock->pagev[0]->logical;
2668 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2669 PAGE_SIZE;
2670
2671 scrub_parity_mark_sectors_error(sblock->sparity,
2672 start, end - start);
2673 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002674}
2675
Zhao Lei3b5753e2015-08-24 22:03:02 +08002676static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002677{
2678 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002679 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002680 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002681
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002682 while (!list_empty(&sctx->csum_list)) {
2683 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002684 struct btrfs_ordered_sum, list);
2685 if (sum->bytenr > logical)
2686 return 0;
2687 if (sum->bytenr + sum->len > logical)
2688 break;
2689
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002690 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002691 list_del(&sum->list);
2692 kfree(sum);
2693 sum = NULL;
2694 }
2695 if (!sum)
2696 return 0;
2697
David Sterba1d1bf922017-03-31 18:02:48 +02002698 index = div_u64(logical - sum->bytenr, sctx->fs_info->sectorsize);
2699 ASSERT(index < UINT_MAX);
2700
David Sterba25cc1222017-05-16 19:10:41 +02002701 num_sectors = sum->len / sctx->fs_info->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002702 memcpy(csum, sum->sums + index, sctx->csum_size);
2703 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002704 list_del(&sum->list);
2705 kfree(sum);
2706 }
Miao Xief51a4a12013-06-19 10:36:09 +08002707 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002708}
2709
2710/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002711static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002712 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002713 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002714{
2715 int ret;
2716 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002717 u32 blocksize;
2718
2719 if (flags & BTRFS_EXTENT_FLAG_DATA) {
David Sterba25cc1222017-05-16 19:10:41 +02002720 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002721 spin_lock(&sctx->stat_lock);
2722 sctx->stat.data_extents_scrubbed++;
2723 sctx->stat.data_bytes_scrubbed += len;
2724 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002725 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
David Sterba25cc1222017-05-16 19:10:41 +02002726 blocksize = sctx->fs_info->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002727 spin_lock(&sctx->stat_lock);
2728 sctx->stat.tree_extents_scrubbed++;
2729 sctx->stat.tree_bytes_scrubbed += len;
2730 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002731 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002732 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002733 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002734 }
Arne Jansena2de7332011-03-08 14:14:00 +01002735
2736 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002737 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002738 int have_csum = 0;
2739
2740 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2741 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002742 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002743 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002744 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002745 if (sctx->is_dev_replace && !have_csum) {
2746 ret = copy_nocow_pages(sctx, logical, l,
2747 mirror_num,
2748 physical_for_dev_replace);
2749 goto behind_scrub_pages;
2750 }
Arne Jansena2de7332011-03-08 14:14:00 +01002751 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002752 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002753 mirror_num, have_csum ? csum : NULL, 0,
2754 physical_for_dev_replace);
2755behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002756 if (ret)
2757 return ret;
2758 len -= l;
2759 logical += l;
2760 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002761 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002762 }
2763 return 0;
2764}
2765
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002766static int scrub_pages_for_parity(struct scrub_parity *sparity,
2767 u64 logical, u64 len,
2768 u64 physical, struct btrfs_device *dev,
2769 u64 flags, u64 gen, int mirror_num, u8 *csum)
2770{
2771 struct scrub_ctx *sctx = sparity->sctx;
2772 struct scrub_block *sblock;
2773 int index;
2774
David Sterba58c4e172016-02-11 10:49:42 +01002775 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002776 if (!sblock) {
2777 spin_lock(&sctx->stat_lock);
2778 sctx->stat.malloc_errors++;
2779 spin_unlock(&sctx->stat_lock);
2780 return -ENOMEM;
2781 }
2782
2783 /* one ref inside this function, plus one for each page added to
2784 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002785 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002786 sblock->sctx = sctx;
2787 sblock->no_io_error_seen = 1;
2788 sblock->sparity = sparity;
2789 scrub_parity_get(sparity);
2790
2791 for (index = 0; len > 0; index++) {
2792 struct scrub_page *spage;
2793 u64 l = min_t(u64, len, PAGE_SIZE);
2794
David Sterba58c4e172016-02-11 10:49:42 +01002795 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002796 if (!spage) {
2797leave_nomem:
2798 spin_lock(&sctx->stat_lock);
2799 sctx->stat.malloc_errors++;
2800 spin_unlock(&sctx->stat_lock);
2801 scrub_block_put(sblock);
2802 return -ENOMEM;
2803 }
2804 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2805 /* For scrub block */
2806 scrub_page_get(spage);
2807 sblock->pagev[index] = spage;
2808 /* For scrub parity */
2809 scrub_page_get(spage);
2810 list_add_tail(&spage->list, &sparity->spages);
2811 spage->sblock = sblock;
2812 spage->dev = dev;
2813 spage->flags = flags;
2814 spage->generation = gen;
2815 spage->logical = logical;
2816 spage->physical = physical;
2817 spage->mirror_num = mirror_num;
2818 if (csum) {
2819 spage->have_csum = 1;
2820 memcpy(spage->csum, csum, sctx->csum_size);
2821 } else {
2822 spage->have_csum = 0;
2823 }
2824 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002825 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002826 if (!spage->page)
2827 goto leave_nomem;
2828 len -= l;
2829 logical += l;
2830 physical += l;
2831 }
2832
2833 WARN_ON(sblock->page_count == 0);
2834 for (index = 0; index < sblock->page_count; index++) {
2835 struct scrub_page *spage = sblock->pagev[index];
2836 int ret;
2837
2838 ret = scrub_add_page_to_rd_bio(sctx, spage);
2839 if (ret) {
2840 scrub_block_put(sblock);
2841 return ret;
2842 }
2843 }
2844
2845 /* last one frees, either here or in bio completion for last page */
2846 scrub_block_put(sblock);
2847 return 0;
2848}
2849
2850static int scrub_extent_for_parity(struct scrub_parity *sparity,
2851 u64 logical, u64 len,
2852 u64 physical, struct btrfs_device *dev,
2853 u64 flags, u64 gen, int mirror_num)
2854{
2855 struct scrub_ctx *sctx = sparity->sctx;
2856 int ret;
2857 u8 csum[BTRFS_CSUM_SIZE];
2858 u32 blocksize;
2859
Anand Jaine6e674b2017-12-04 12:54:54 +08002860 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval4a770892015-06-19 11:52:52 -07002861 scrub_parity_mark_sectors_error(sparity, logical, len);
2862 return 0;
2863 }
2864
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002865 if (flags & BTRFS_EXTENT_FLAG_DATA) {
David Sterba25cc1222017-05-16 19:10:41 +02002866 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002867 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
David Sterba25cc1222017-05-16 19:10:41 +02002868 blocksize = sctx->fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002869 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002870 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002871 WARN_ON(1);
2872 }
2873
2874 while (len) {
2875 u64 l = min_t(u64, len, blocksize);
2876 int have_csum = 0;
2877
2878 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2879 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002880 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002881 if (have_csum == 0)
2882 goto skip;
2883 }
2884 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2885 flags, gen, mirror_num,
2886 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002887 if (ret)
2888 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002889skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002890 len -= l;
2891 logical += l;
2892 physical += l;
2893 }
2894 return 0;
2895}
2896
Wang Shilong3b080b22014-04-01 18:01:43 +08002897/*
2898 * Given a physical address, this will calculate it's
2899 * logical offset. if this is a parity stripe, it will return
2900 * the most left data stripe's logical offset.
2901 *
2902 * return 0 if it is a data stripe, 1 means parity stripe.
2903 */
2904static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002905 struct map_lookup *map, u64 *offset,
2906 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002907{
2908 int i;
2909 int j = 0;
2910 u64 stripe_nr;
2911 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002912 u32 stripe_index;
2913 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002914
2915 last_offset = (physical - map->stripes[num].physical) *
2916 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002917 if (stripe_start)
2918 *stripe_start = last_offset;
2919
Wang Shilong3b080b22014-04-01 18:01:43 +08002920 *offset = last_offset;
2921 for (i = 0; i < nr_data_stripes(map); i++) {
2922 *offset = last_offset + i * map->stripe_len;
2923
Liu Bo42c61ab2017-04-03 13:45:24 -07002924 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01002925 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002926
2927 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002928 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002929 /* calculate which stripe this data locates */
2930 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002931 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002932 if (stripe_index == num)
2933 return 0;
2934 if (stripe_index < num)
2935 j++;
2936 }
2937 *offset = last_offset + j * map->stripe_len;
2938 return 1;
2939}
2940
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002941static void scrub_free_parity(struct scrub_parity *sparity)
2942{
2943 struct scrub_ctx *sctx = sparity->sctx;
2944 struct scrub_page *curr, *next;
2945 int nbits;
2946
2947 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2948 if (nbits) {
2949 spin_lock(&sctx->stat_lock);
2950 sctx->stat.read_errors += nbits;
2951 sctx->stat.uncorrectable_errors += nbits;
2952 spin_unlock(&sctx->stat_lock);
2953 }
2954
2955 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2956 list_del_init(&curr->list);
2957 scrub_page_put(curr);
2958 }
2959
2960 kfree(sparity);
2961}
2962
Zhao Lei20b2e302015-06-04 20:09:15 +08002963static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2964{
2965 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2966 work);
2967 struct scrub_ctx *sctx = sparity->sctx;
2968
2969 scrub_free_parity(sparity);
2970 scrub_pending_bio_dec(sctx);
2971}
2972
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002973static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002974{
2975 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002976 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002977
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002978 if (bio->bi_status)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002979 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
2980 sparity->nsectors);
2981
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002982 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08002983
2984 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2985 scrub_parity_bio_endio_worker, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002986 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002987}
2988
2989static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2990{
2991 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002992 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002993 struct bio *bio;
2994 struct btrfs_raid_bio *rbio;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002995 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002996 u64 length;
2997 int ret;
2998
2999 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
3000 sparity->nsectors))
3001 goto out;
3002
Zhao Leia0dd59d2015-07-21 15:42:26 +08003003 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08003004
3005 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003006 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
David Sterba825ad4c2017-03-28 14:45:22 +02003007 &length, &bbio);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003008 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003009 goto bbio_out;
3010
David Sterbac5e4c3d2017-06-12 17:29:41 +02003011 bio = btrfs_io_bio_alloc(0);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003012 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
3013 bio->bi_private = sparity;
3014 bio->bi_end_io = scrub_parity_bio_endio;
3015
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003016 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003017 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003018 sparity->dbitmap,
3019 sparity->nsectors);
3020 if (!rbio)
3021 goto rbio_out;
3022
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003023 scrub_pending_bio_inc(sctx);
3024 raid56_parity_submit_scrub_rbio(rbio);
3025 return;
3026
3027rbio_out:
3028 bio_put(bio);
3029bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08003030 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08003031 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003032 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
3033 sparity->nsectors);
3034 spin_lock(&sctx->stat_lock);
3035 sctx->stat.malloc_errors++;
3036 spin_unlock(&sctx->stat_lock);
3037out:
3038 scrub_free_parity(sparity);
3039}
3040
3041static inline int scrub_calc_parity_bitmap_len(int nsectors)
3042{
Zhao Leibfca9a62014-12-08 19:55:57 +08003043 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003044}
3045
3046static void scrub_parity_get(struct scrub_parity *sparity)
3047{
Elena Reshetova78a76452017-03-03 10:55:24 +02003048 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003049}
3050
3051static void scrub_parity_put(struct scrub_parity *sparity)
3052{
Elena Reshetova78a76452017-03-03 10:55:24 +02003053 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003054 return;
3055
3056 scrub_parity_check_and_repair(sparity);
3057}
3058
3059static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
3060 struct map_lookup *map,
3061 struct btrfs_device *sdev,
3062 struct btrfs_path *path,
3063 u64 logic_start,
3064 u64 logic_end)
3065{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003066 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003067 struct btrfs_root *root = fs_info->extent_root;
3068 struct btrfs_root *csum_root = fs_info->csum_root;
3069 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07003070 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003071 u64 flags;
3072 int ret;
3073 int slot;
3074 struct extent_buffer *l;
3075 struct btrfs_key key;
3076 u64 generation;
3077 u64 extent_logical;
3078 u64 extent_physical;
3079 u64 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07003080 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003081 struct btrfs_device *extent_dev;
3082 struct scrub_parity *sparity;
3083 int nsectors;
3084 int bitmap_len;
3085 int extent_mirror_num;
3086 int stop_loop = 0;
3087
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003088 nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003089 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
3090 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
3091 GFP_NOFS);
3092 if (!sparity) {
3093 spin_lock(&sctx->stat_lock);
3094 sctx->stat.malloc_errors++;
3095 spin_unlock(&sctx->stat_lock);
3096 return -ENOMEM;
3097 }
3098
3099 sparity->stripe_len = map->stripe_len;
3100 sparity->nsectors = nsectors;
3101 sparity->sctx = sctx;
3102 sparity->scrub_dev = sdev;
3103 sparity->logic_start = logic_start;
3104 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02003105 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003106 INIT_LIST_HEAD(&sparity->spages);
3107 sparity->dbitmap = sparity->bitmap;
3108 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
3109
3110 ret = 0;
3111 while (logic_start < logic_end) {
3112 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3113 key.type = BTRFS_METADATA_ITEM_KEY;
3114 else
3115 key.type = BTRFS_EXTENT_ITEM_KEY;
3116 key.objectid = logic_start;
3117 key.offset = (u64)-1;
3118
3119 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3120 if (ret < 0)
3121 goto out;
3122
3123 if (ret > 0) {
3124 ret = btrfs_previous_extent_item(root, path, 0);
3125 if (ret < 0)
3126 goto out;
3127 if (ret > 0) {
3128 btrfs_release_path(path);
3129 ret = btrfs_search_slot(NULL, root, &key,
3130 path, 0, 0);
3131 if (ret < 0)
3132 goto out;
3133 }
3134 }
3135
3136 stop_loop = 0;
3137 while (1) {
3138 u64 bytes;
3139
3140 l = path->nodes[0];
3141 slot = path->slots[0];
3142 if (slot >= btrfs_header_nritems(l)) {
3143 ret = btrfs_next_leaf(root, path);
3144 if (ret == 0)
3145 continue;
3146 if (ret < 0)
3147 goto out;
3148
3149 stop_loop = 1;
3150 break;
3151 }
3152 btrfs_item_key_to_cpu(l, &key, slot);
3153
Zhao Leid7cad232015-07-22 13:14:48 +08003154 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3155 key.type != BTRFS_METADATA_ITEM_KEY)
3156 goto next;
3157
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003158 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003159 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003160 else
3161 bytes = key.offset;
3162
3163 if (key.objectid + bytes <= logic_start)
3164 goto next;
3165
Zhao Leia0dd59d2015-07-21 15:42:26 +08003166 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003167 stop_loop = 1;
3168 break;
3169 }
3170
3171 while (key.objectid >= logic_start + map->stripe_len)
3172 logic_start += map->stripe_len;
3173
3174 extent = btrfs_item_ptr(l, slot,
3175 struct btrfs_extent_item);
3176 flags = btrfs_extent_flags(l, extent);
3177 generation = btrfs_extent_generation(l, extent);
3178
Zhao Leia323e812015-07-23 12:29:49 +08003179 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3180 (key.objectid < logic_start ||
3181 key.objectid + bytes >
3182 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003183 btrfs_err(fs_info,
3184 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08003185 key.objectid, logic_start);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003186 spin_lock(&sctx->stat_lock);
3187 sctx->stat.uncorrectable_errors++;
3188 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003189 goto next;
3190 }
3191again:
3192 extent_logical = key.objectid;
3193 extent_len = bytes;
3194
3195 if (extent_logical < logic_start) {
3196 extent_len -= logic_start - extent_logical;
3197 extent_logical = logic_start;
3198 }
3199
3200 if (extent_logical + extent_len >
3201 logic_start + map->stripe_len)
3202 extent_len = logic_start + map->stripe_len -
3203 extent_logical;
3204
3205 scrub_parity_mark_sectors_data(sparity, extent_logical,
3206 extent_len);
3207
Omar Sandoval4a770892015-06-19 11:52:52 -07003208 mapped_length = extent_len;
Zhao Leif1fee652016-05-17 17:37:38 +08003209 bbio = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003210 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
3211 extent_logical, &mapped_length, &bbio,
3212 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07003213 if (!ret) {
3214 if (!bbio || mapped_length < extent_len)
3215 ret = -EIO;
3216 }
3217 if (ret) {
3218 btrfs_put_bbio(bbio);
3219 goto out;
3220 }
3221 extent_physical = bbio->stripes[0].physical;
3222 extent_mirror_num = bbio->mirror_num;
3223 extent_dev = bbio->stripes[0].dev;
3224 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003225
3226 ret = btrfs_lookup_csums_range(csum_root,
3227 extent_logical,
3228 extent_logical + extent_len - 1,
3229 &sctx->csum_list, 1);
3230 if (ret)
3231 goto out;
3232
3233 ret = scrub_extent_for_parity(sparity, extent_logical,
3234 extent_len,
3235 extent_physical,
3236 extent_dev, flags,
3237 generation,
3238 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003239
3240 scrub_free_csums(sctx);
3241
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003242 if (ret)
3243 goto out;
3244
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003245 if (extent_logical + extent_len <
3246 key.objectid + bytes) {
3247 logic_start += map->stripe_len;
3248
3249 if (logic_start >= logic_end) {
3250 stop_loop = 1;
3251 break;
3252 }
3253
3254 if (logic_start < key.objectid + bytes) {
3255 cond_resched();
3256 goto again;
3257 }
3258 }
3259next:
3260 path->slots[0]++;
3261 }
3262
3263 btrfs_release_path(path);
3264
3265 if (stop_loop)
3266 break;
3267
3268 logic_start += map->stripe_len;
3269 }
3270out:
3271 if (ret < 0)
3272 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003273 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003274 scrub_parity_put(sparity);
3275 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003276 mutex_lock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003277 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003278 mutex_unlock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003279
3280 btrfs_release_path(path);
3281 return ret < 0 ? ret : 0;
3282}
3283
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003284static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003285 struct map_lookup *map,
3286 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003287 int num, u64 base, u64 length,
3288 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003289{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003290 struct btrfs_path *path, *ppath;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003291 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003292 struct btrfs_root *root = fs_info->extent_root;
3293 struct btrfs_root *csum_root = fs_info->csum_root;
3294 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003295 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003296 u64 flags;
3297 int ret;
3298 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003299 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003300 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003301 u64 physical;
3302 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003303 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003304 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003305 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003306 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003307 struct reada_control *reada1;
3308 struct reada_control *reada2;
David Sterbae6c11f92016-03-24 18:00:53 +01003309 struct btrfs_key key;
Arne Jansen7a262852011-06-10 12:39:23 +02003310 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003311 u64 increment = map->stripe_len;
3312 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003313 u64 extent_logical;
3314 u64 extent_physical;
3315 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003316 u64 stripe_logical;
3317 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003318 struct btrfs_device *extent_dev;
3319 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003320 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003321
Wang Shilong3b080b22014-04-01 18:01:43 +08003322 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003323 offset = 0;
Liu Bo42c61ab2017-04-03 13:45:24 -07003324 nstripes = div64_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003325 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3326 offset = map->stripe_len * num;
3327 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003328 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003329 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3330 int factor = map->num_stripes / map->sub_stripes;
3331 offset = map->stripe_len * (num / map->sub_stripes);
3332 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003333 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003334 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3335 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003336 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003337 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3338 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003339 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003340 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003341 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003342 increment = map->stripe_len * nr_data_stripes(map);
3343 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003344 } else {
3345 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003346 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003347 }
3348
3349 path = btrfs_alloc_path();
3350 if (!path)
3351 return -ENOMEM;
3352
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003353 ppath = btrfs_alloc_path();
3354 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003355 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003356 return -ENOMEM;
3357 }
3358
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003359 /*
3360 * work on commit root. The related disk blocks are static as
3361 * long as COW is applied. This means, it is save to rewrite
3362 * them to repair disk errors without any race conditions
3363 */
Arne Jansena2de7332011-03-08 14:14:00 +01003364 path->search_commit_root = 1;
3365 path->skip_locking = 1;
3366
Gui Hecheng063c54d2015-01-09 09:39:40 +08003367 ppath->search_commit_root = 1;
3368 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003369 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003370 * trigger the readahead for extent tree csum tree and wait for
3371 * completion. During readahead, the scrub is officially paused
3372 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003373 */
3374 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003375 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003376 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003377 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003378 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003379 logic_end += base;
3380 } else {
3381 logic_end = logical + increment * nstripes;
3382 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003383 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003384 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003385 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003386
Arne Jansen7a262852011-06-10 12:39:23 +02003387 /* FIXME it might be better to start readahead at commit root */
David Sterbae6c11f92016-03-24 18:00:53 +01003388 key.objectid = logical;
3389 key.type = BTRFS_EXTENT_ITEM_KEY;
3390 key.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003391 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003392 key_end.type = BTRFS_METADATA_ITEM_KEY;
3393 key_end.offset = (u64)-1;
David Sterbae6c11f92016-03-24 18:00:53 +01003394 reada1 = btrfs_reada_add(root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003395
David Sterbae6c11f92016-03-24 18:00:53 +01003396 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3397 key.type = BTRFS_EXTENT_CSUM_KEY;
3398 key.offset = logical;
Arne Jansen7a262852011-06-10 12:39:23 +02003399 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3400 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003401 key_end.offset = logic_end;
David Sterbae6c11f92016-03-24 18:00:53 +01003402 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003403
Arne Jansen7a262852011-06-10 12:39:23 +02003404 if (!IS_ERR(reada1))
3405 btrfs_reada_wait(reada1);
3406 if (!IS_ERR(reada2))
3407 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003408
Arne Jansena2de7332011-03-08 14:14:00 +01003409
3410 /*
3411 * collect all data csums for the stripe to avoid seeking during
3412 * the scrub. This might currently (crc32) end up to be about 1MB
3413 */
Arne Jansene7786c32011-05-28 20:58:38 +00003414 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003415
Arne Jansena2de7332011-03-08 14:14:00 +01003416 /*
3417 * now find all extents for each stripe and scrub them
3418 */
Arne Jansena2de7332011-03-08 14:14:00 +01003419 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003420 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003421 /*
3422 * canceled?
3423 */
3424 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003425 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003426 ret = -ECANCELED;
3427 goto out;
3428 }
3429 /*
3430 * check to see if we have to pause
3431 */
3432 if (atomic_read(&fs_info->scrub_pause_req)) {
3433 /* push queued extents */
David Sterba2073c4c2017-03-31 17:12:51 +02003434 sctx->flush_all_writes = true;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003435 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003436 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003437 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003438 mutex_unlock(&sctx->wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003439 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003440 atomic_read(&sctx->bios_in_flight) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003441 sctx->flush_all_writes = false;
Wang Shilong3cb09292013-12-04 21:15:19 +08003442 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003443 }
3444
Zhao Leif2f66a22015-07-21 12:22:29 +08003445 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3446 ret = get_raid56_logic_offset(physical, num, map,
3447 &logical,
3448 &stripe_logical);
3449 logical += base;
3450 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003451 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003452 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003453 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003454 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3455 ppath, stripe_logical,
3456 stripe_end);
3457 if (ret)
3458 goto out;
3459 goto skip;
3460 }
3461 }
3462
Wang Shilong7c76edb2014-01-12 21:38:32 +08003463 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3464 key.type = BTRFS_METADATA_ITEM_KEY;
3465 else
3466 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003467 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003468 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003469
3470 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3471 if (ret < 0)
3472 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003473
Arne Jansen8c510322011-06-03 10:09:26 +02003474 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003475 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003476 if (ret < 0)
3477 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003478 if (ret > 0) {
3479 /* there's no smaller item, so stick with the
3480 * larger one */
3481 btrfs_release_path(path);
3482 ret = btrfs_search_slot(NULL, root, &key,
3483 path, 0, 0);
3484 if (ret < 0)
3485 goto out;
3486 }
Arne Jansena2de7332011-03-08 14:14:00 +01003487 }
3488
Liu Bo625f1c8d2013-04-27 02:56:57 +00003489 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003490 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003491 u64 bytes;
3492
Arne Jansena2de7332011-03-08 14:14:00 +01003493 l = path->nodes[0];
3494 slot = path->slots[0];
3495 if (slot >= btrfs_header_nritems(l)) {
3496 ret = btrfs_next_leaf(root, path);
3497 if (ret == 0)
3498 continue;
3499 if (ret < 0)
3500 goto out;
3501
Liu Bo625f1c8d2013-04-27 02:56:57 +00003502 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003503 break;
3504 }
3505 btrfs_item_key_to_cpu(l, &key, slot);
3506
Zhao Leid7cad232015-07-22 13:14:48 +08003507 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3508 key.type != BTRFS_METADATA_ITEM_KEY)
3509 goto next;
3510
Josef Bacik3173a182013-03-07 14:22:04 -05003511 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003512 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003513 else
3514 bytes = key.offset;
3515
3516 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003517 goto next;
3518
Liu Bo625f1c8d2013-04-27 02:56:57 +00003519 if (key.objectid >= logical + map->stripe_len) {
3520 /* out of this device extent */
3521 if (key.objectid >= logic_end)
3522 stop_loop = 1;
3523 break;
3524 }
Arne Jansena2de7332011-03-08 14:14:00 +01003525
3526 extent = btrfs_item_ptr(l, slot,
3527 struct btrfs_extent_item);
3528 flags = btrfs_extent_flags(l, extent);
3529 generation = btrfs_extent_generation(l, extent);
3530
Zhao Leia323e812015-07-23 12:29:49 +08003531 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3532 (key.objectid < logical ||
3533 key.objectid + bytes >
3534 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003535 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003536 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003537 key.objectid, logical);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003538 spin_lock(&sctx->stat_lock);
3539 sctx->stat.uncorrectable_errors++;
3540 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003541 goto next;
3542 }
3543
Liu Bo625f1c8d2013-04-27 02:56:57 +00003544again:
3545 extent_logical = key.objectid;
3546 extent_len = bytes;
3547
Arne Jansena2de7332011-03-08 14:14:00 +01003548 /*
3549 * trim extent to this stripe
3550 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003551 if (extent_logical < logical) {
3552 extent_len -= logical - extent_logical;
3553 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003554 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003555 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003556 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003557 extent_len = logical + map->stripe_len -
3558 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003559 }
3560
Liu Bo625f1c8d2013-04-27 02:56:57 +00003561 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003562 extent_dev = scrub_dev;
3563 extent_mirror_num = mirror_num;
3564 if (is_dev_replace)
3565 scrub_remap_extent(fs_info, extent_logical,
3566 extent_len, &extent_physical,
3567 &extent_dev,
3568 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003569
Zhao Leife8cf652015-07-22 13:14:47 +08003570 ret = btrfs_lookup_csums_range(csum_root,
3571 extent_logical,
3572 extent_logical +
3573 extent_len - 1,
3574 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003575 if (ret)
3576 goto out;
3577
Liu Bo625f1c8d2013-04-27 02:56:57 +00003578 ret = scrub_extent(sctx, extent_logical, extent_len,
3579 extent_physical, extent_dev, flags,
3580 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003581 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003582
3583 scrub_free_csums(sctx);
3584
Liu Bo625f1c8d2013-04-27 02:56:57 +00003585 if (ret)
3586 goto out;
3587
3588 if (extent_logical + extent_len <
3589 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003590 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003591 /*
3592 * loop until we find next data stripe
3593 * or we have finished all stripes.
3594 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003595loop:
3596 physical += map->stripe_len;
3597 ret = get_raid56_logic_offset(physical,
3598 num, map, &logical,
3599 &stripe_logical);
3600 logical += base;
3601
3602 if (ret && physical < physical_end) {
3603 stripe_logical += base;
3604 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003605 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003606 ret = scrub_raid56_parity(sctx,
3607 map, scrub_dev, ppath,
3608 stripe_logical,
3609 stripe_end);
3610 if (ret)
3611 goto out;
3612 goto loop;
3613 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003614 } else {
3615 physical += map->stripe_len;
3616 logical += increment;
3617 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003618 if (logical < key.objectid + bytes) {
3619 cond_resched();
3620 goto again;
3621 }
3622
Wang Shilong3b080b22014-04-01 18:01:43 +08003623 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003624 stop_loop = 1;
3625 break;
3626 }
3627 }
Arne Jansena2de7332011-03-08 14:14:00 +01003628next:
3629 path->slots[0]++;
3630 }
Chris Mason71267332011-05-23 06:30:52 -04003631 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003632skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003633 logical += increment;
3634 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003635 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003636 if (stop_loop)
3637 sctx->stat.last_physical = map->stripes[num].physical +
3638 length;
3639 else
3640 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003641 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003642 if (stop_loop)
3643 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003644 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003645out:
Arne Jansena2de7332011-03-08 14:14:00 +01003646 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003647 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003648 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003649 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003650 mutex_unlock(&sctx->wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003651
Arne Jansene7786c32011-05-28 20:58:38 +00003652 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003653 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003654 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003655 return ret < 0 ? ret : 0;
3656}
3657
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003658static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003659 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003660 u64 chunk_offset, u64 length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003661 u64 dev_offset,
3662 struct btrfs_block_group_cache *cache,
3663 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003664{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003665 struct btrfs_fs_info *fs_info = sctx->fs_info;
3666 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003667 struct map_lookup *map;
3668 struct extent_map *em;
3669 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003670 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003671
3672 read_lock(&map_tree->map_tree.lock);
3673 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3674 read_unlock(&map_tree->map_tree.lock);
3675
Filipe Manana020d5b72015-11-19 10:57:20 +00003676 if (!em) {
3677 /*
3678 * Might have been an unused block group deleted by the cleaner
3679 * kthread or relocation.
3680 */
3681 spin_lock(&cache->lock);
3682 if (!cache->removed)
3683 ret = -EINVAL;
3684 spin_unlock(&cache->lock);
3685
3686 return ret;
3687 }
Arne Jansena2de7332011-03-08 14:14:00 +01003688
Jeff Mahoney95617d62015-06-03 10:55:48 -04003689 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003690 if (em->start != chunk_offset)
3691 goto out;
3692
3693 if (em->len < length)
3694 goto out;
3695
3696 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003697 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003698 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003699 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003700 chunk_offset, length,
3701 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003702 if (ret)
3703 goto out;
3704 }
3705 }
3706out:
3707 free_extent_map(em);
3708
3709 return ret;
3710}
3711
3712static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003713int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003714 struct btrfs_device *scrub_dev, u64 start, u64 end,
3715 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003716{
3717 struct btrfs_dev_extent *dev_extent = NULL;
3718 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003719 struct btrfs_fs_info *fs_info = sctx->fs_info;
3720 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003721 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003722 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003723 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003724 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003725 int slot;
3726 struct extent_buffer *l;
3727 struct btrfs_key key;
3728 struct btrfs_key found_key;
3729 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003730 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003731
3732 path = btrfs_alloc_path();
3733 if (!path)
3734 return -ENOMEM;
3735
David Sterbae4058b52015-11-27 16:31:35 +01003736 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003737 path->search_commit_root = 1;
3738 path->skip_locking = 1;
3739
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003740 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003741 key.offset = 0ull;
3742 key.type = BTRFS_DEV_EXTENT_KEY;
3743
Arne Jansena2de7332011-03-08 14:14:00 +01003744 while (1) {
3745 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3746 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003747 break;
3748 if (ret > 0) {
3749 if (path->slots[0] >=
3750 btrfs_header_nritems(path->nodes[0])) {
3751 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003752 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003753 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003754 if (ret > 0) {
3755 ret = 0;
3756 break;
3757 }
3758 } else {
3759 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003760 }
3761 }
Arne Jansena2de7332011-03-08 14:14:00 +01003762
3763 l = path->nodes[0];
3764 slot = path->slots[0];
3765
3766 btrfs_item_key_to_cpu(l, &found_key, slot);
3767
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003768 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003769 break;
3770
David Sterba962a2982014-06-04 18:41:45 +02003771 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003772 break;
3773
3774 if (found_key.offset >= end)
3775 break;
3776
3777 if (found_key.offset < key.offset)
3778 break;
3779
3780 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3781 length = btrfs_dev_extent_length(l, dev_extent);
3782
Qu Wenruoced96ed2014-06-19 10:42:51 +08003783 if (found_key.offset + length <= start)
3784 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003785
Arne Jansena2de7332011-03-08 14:14:00 +01003786 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3787
3788 /*
3789 * get a reference on the corresponding block group to prevent
3790 * the chunk from going away while we scrub it
3791 */
3792 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003793
3794 /* some chunks are removed but not committed to disk yet,
3795 * continue scrubbing */
3796 if (!cache)
3797 goto skip;
3798
Zhaolei55e3a602015-08-05 16:43:30 +08003799 /*
3800 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3801 * to avoid deadlock caused by:
3802 * btrfs_inc_block_group_ro()
3803 * -> btrfs_wait_for_commit()
3804 * -> btrfs_commit_transaction()
3805 * -> btrfs_scrub_pause()
3806 */
3807 scrub_pause_on(fs_info);
Jeff Mahoney5e00f192017-02-15 16:28:29 -05003808 ret = btrfs_inc_block_group_ro(fs_info, cache);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003809 if (!ret && is_dev_replace) {
3810 /*
3811 * If we are doing a device replace wait for any tasks
3812 * that started dellaloc right before we set the block
3813 * group to RO mode, as they might have just allocated
3814 * an extent from it or decided they could do a nocow
3815 * write. And if any such tasks did that, wait for their
3816 * ordered extents to complete and then commit the
3817 * current transaction, so that we can later see the new
3818 * extent items in the extent tree - the ordered extents
3819 * create delayed data references (for cow writes) when
3820 * they complete, which will be run and insert the
3821 * corresponding extent items into the extent tree when
3822 * we commit the transaction they used when running
3823 * inode.c:btrfs_finish_ordered_io(). We later use
3824 * the commit root of the extent tree to find extents
3825 * to copy from the srcdev into the tgtdev, and we don't
3826 * want to miss any new extents.
3827 */
3828 btrfs_wait_block_group_reservations(cache);
3829 btrfs_wait_nocow_writers(cache);
Chris Mason6374e57a2017-06-23 09:48:21 -07003830 ret = btrfs_wait_ordered_roots(fs_info, U64_MAX,
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003831 cache->key.objectid,
3832 cache->key.offset);
3833 if (ret > 0) {
3834 struct btrfs_trans_handle *trans;
3835
3836 trans = btrfs_join_transaction(root);
3837 if (IS_ERR(trans))
3838 ret = PTR_ERR(trans);
3839 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003840 ret = btrfs_commit_transaction(trans);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003841 if (ret) {
3842 scrub_pause_off(fs_info);
3843 btrfs_put_block_group(cache);
3844 break;
3845 }
3846 }
3847 }
Zhaolei55e3a602015-08-05 16:43:30 +08003848 scrub_pause_off(fs_info);
Zhaolei76a8efa2015-11-17 18:46:17 +08003849
3850 if (ret == 0) {
3851 ro_set = 1;
3852 } else if (ret == -ENOSPC) {
3853 /*
3854 * btrfs_inc_block_group_ro return -ENOSPC when it
3855 * failed in creating new chunk for metadata.
3856 * It is not a problem for scrub/replace, because
3857 * metadata are always cowed, and our scrub paused
3858 * commit_transactions.
3859 */
3860 ro_set = 0;
3861 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003862 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02003863 "failed setting block group ro: %d", ret);
Zhaolei55e3a602015-08-05 16:43:30 +08003864 btrfs_put_block_group(cache);
3865 break;
3866 }
3867
Filipe Manana81e87a72016-05-14 16:32:35 +01003868 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003869 dev_replace->cursor_right = found_key.offset + length;
3870 dev_replace->cursor_left = found_key.offset;
3871 dev_replace->item_needs_writeback = 1;
Filipe Manana81e87a72016-05-14 16:32:35 +01003872 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
Zhao Lei8c204c92015-08-19 15:02:40 +08003873 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003874 found_key.offset, cache, is_dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003875
3876 /*
3877 * flush, submit all pending read and write bios, afterwards
3878 * wait for them.
3879 * Note that in the dev replace case, a read request causes
3880 * write requests that are submitted in the read completion
3881 * worker. Therefore in the current situation, it is required
3882 * that all write requests are flushed, so that all read and
3883 * write requests are really completed when bios_in_flight
3884 * changes to 0.
3885 */
David Sterba2073c4c2017-03-31 17:12:51 +02003886 sctx->flush_all_writes = true;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003887 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003888 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003889 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003890 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003891
3892 wait_event(sctx->list_wait,
3893 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003894
3895 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003896
3897 /*
3898 * must be called before we decrease @scrub_paused.
3899 * make sure we don't block transaction commit while
3900 * we are waiting pending workers finished.
3901 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003902 wait_event(sctx->list_wait,
3903 atomic_read(&sctx->workers_pending) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003904 sctx->flush_all_writes = false;
Wang Shilong12cf9372014-02-19 19:24:17 +08003905
Zhaoleib708ce92015-08-05 16:43:29 +08003906 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003907
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003908 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
3909 dev_replace->cursor_left = dev_replace->cursor_right;
3910 dev_replace->item_needs_writeback = 1;
3911 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
3912
Zhaolei76a8efa2015-11-17 18:46:17 +08003913 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003914 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003915
Filipe Manana758f2df2015-11-19 11:45:48 +00003916 /*
3917 * We might have prevented the cleaner kthread from deleting
3918 * this block group if it was already unused because we raced
3919 * and set it to RO mode first. So add it back to the unused
3920 * list, otherwise it might not ever be deleted unless a manual
3921 * balance is triggered or it becomes used and unused again.
3922 */
3923 spin_lock(&cache->lock);
3924 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3925 btrfs_block_group_used(&cache->item) == 0) {
3926 spin_unlock(&cache->lock);
3927 spin_lock(&fs_info->unused_bgs_lock);
3928 if (list_empty(&cache->bg_list)) {
3929 btrfs_get_block_group(cache);
3930 list_add_tail(&cache->bg_list,
3931 &fs_info->unused_bgs);
3932 }
3933 spin_unlock(&fs_info->unused_bgs_lock);
3934 } else {
3935 spin_unlock(&cache->lock);
3936 }
3937
Arne Jansena2de7332011-03-08 14:14:00 +01003938 btrfs_put_block_group(cache);
3939 if (ret)
3940 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003941 if (is_dev_replace &&
3942 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003943 ret = -EIO;
3944 break;
3945 }
3946 if (sctx->stat.malloc_errors > 0) {
3947 ret = -ENOMEM;
3948 break;
3949 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003950skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003951 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003952 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003953 }
3954
Arne Jansena2de7332011-03-08 14:14:00 +01003955 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003956
Zhaolei55e3a602015-08-05 16:43:30 +08003957 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003958}
3959
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003960static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3961 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003962{
3963 int i;
3964 u64 bytenr;
3965 u64 gen;
3966 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003967 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003968
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003969 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003970 return -EIO;
3971
Miao Xie5f546062014-07-24 11:37:09 +08003972 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003973 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08003974 gen = scrub_dev->generation;
3975 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003976 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01003977
3978 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3979 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003980 if (bytenr + BTRFS_SUPER_INFO_SIZE >
3981 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01003982 break;
3983
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003984 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003985 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003986 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01003987 if (ret)
3988 return ret;
3989 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003990 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003991
3992 return 0;
3993}
3994
3995/*
3996 * get a reference count on fs_info->scrub_workers. start worker if necessary
3997 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003998static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
3999 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004000{
David Sterba6f011052015-02-16 18:34:01 +01004001 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08004002 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01004003
Arne Jansen632dd772011-06-10 12:07:07 +02004004 if (fs_info->scrub_workers_refcnt == 0) {
David Sterbaaf1cbe02017-03-31 18:42:57 +02004005 fs_info->scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub",
4006 flags, is_dev_replace ? 1 : max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08004007 if (!fs_info->scrub_workers)
4008 goto fail_scrub_workers;
4009
Qu Wenruo0339ef22014-02-28 10:46:17 +08004010 fs_info->scrub_wr_completion_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004011 btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004012 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004013 if (!fs_info->scrub_wr_completion_workers)
4014 goto fail_scrub_wr_completion_workers;
4015
Qu Wenruo0339ef22014-02-28 10:46:17 +08004016 fs_info->scrub_nocow_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004017 btrfs_alloc_workqueue(fs_info, "scrubnc", flags, 1, 0);
Zhao Leie82afc52015-06-12 20:36:58 +08004018 if (!fs_info->scrub_nocow_workers)
4019 goto fail_scrub_nocow_workers;
Zhao Lei20b2e302015-06-04 20:09:15 +08004020 fs_info->scrub_parity_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004021 btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
Zhao Lei20b2e302015-06-04 20:09:15 +08004022 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004023 if (!fs_info->scrub_parity_workers)
4024 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02004025 }
Arne Jansena2de7332011-03-08 14:14:00 +01004026 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08004027 return 0;
4028
4029fail_scrub_parity_workers:
4030 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
4031fail_scrub_nocow_workers:
4032 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4033fail_scrub_wr_completion_workers:
4034 btrfs_destroy_workqueue(fs_info->scrub_workers);
4035fail_scrub_workers:
4036 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01004037}
4038
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004039static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004040{
Stefan Behrensff023aa2012-11-06 11:43:11 +01004041 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08004042 btrfs_destroy_workqueue(fs_info->scrub_workers);
4043 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4044 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08004045 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004046 }
Arne Jansena2de7332011-03-08 14:14:00 +01004047 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004048}
4049
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004050int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
4051 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004052 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004053{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004054 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004055 int ret;
4056 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08004057 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01004058
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004059 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01004060 return -EINVAL;
4061
Jeff Mahoneyda170662016-06-15 09:22:56 -04004062 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004063 /*
4064 * in this case scrub is unable to calculate the checksum
4065 * the way scrub is implemented. Do not handle this
4066 * situation at all because it won't ever happen.
4067 */
Frank Holtonefe120a2013-12-20 11:37:06 -05004068 btrfs_err(fs_info,
4069 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004070 fs_info->nodesize,
4071 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004072 return -EINVAL;
4073 }
4074
Jeff Mahoneyda170662016-06-15 09:22:56 -04004075 if (fs_info->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004076 /* not supported for data w/o checksums */
Chandan Rajendra751bebb2016-07-04 10:04:39 +05304077 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004078 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004079 fs_info->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01004080 return -EINVAL;
4081 }
4082
Jeff Mahoneyda170662016-06-15 09:22:56 -04004083 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004084 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04004085 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004086 /*
4087 * would exhaust the array bounds of pagev member in
4088 * struct scrub_block
4089 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004090 btrfs_err(fs_info,
4091 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004092 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004093 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004094 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004095 SCRUB_MAX_PAGES_PER_BLOCK);
4096 return -EINVAL;
4097 }
4098
Arne Jansena2de7332011-03-08 14:14:00 +01004099
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004100 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4101 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jaine6e674b2017-12-04 12:54:54 +08004102 if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
4103 !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004104 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004105 return -ENODEV;
4106 }
Arne Jansena2de7332011-03-08 14:14:00 +01004107
Anand Jainebbede42017-12-04 12:54:52 +08004108 if (!is_dev_replace && !readonly &&
4109 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Miao Xie5d68da32014-07-24 11:37:07 +08004110 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4111 rcu_read_lock();
4112 name = rcu_dereference(dev->name);
4113 btrfs_err(fs_info, "scrub: device %s is not writable",
4114 name->str);
4115 rcu_read_unlock();
4116 return -EROFS;
4117 }
4118
Wang Shilong3b7a0162013-10-12 02:11:12 +08004119 mutex_lock(&fs_info->scrub_lock);
Anand Jaine12c9622017-12-04 12:54:53 +08004120 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004121 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004122 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004123 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004124 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01004125 }
4126
Liu Bo73beece2015-07-17 16:49:19 +08004127 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01004128 if (dev->scrub_device ||
4129 (!is_dev_replace &&
4130 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
Liu Bo73beece2015-07-17 16:49:19 +08004131 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004132 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004133 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004134 return -EINPROGRESS;
4135 }
Liu Bo73beece2015-07-17 16:49:19 +08004136 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Wang Shilong3b7a0162013-10-12 02:11:12 +08004137
4138 ret = scrub_workers_get(fs_info, is_dev_replace);
4139 if (ret) {
4140 mutex_unlock(&fs_info->scrub_lock);
4141 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4142 return ret;
4143 }
4144
Stefan Behrens63a212a2012-11-05 18:29:28 +01004145 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004146 if (IS_ERR(sctx)) {
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);
4149 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004150 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004151 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004152 sctx->readonly = readonly;
4153 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08004154 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004155
Wang Shilong3cb09292013-12-04 21:15:19 +08004156 /*
4157 * checking @scrub_pause_req here, we can avoid
4158 * race between committing transaction and scrubbing.
4159 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08004160 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004161 atomic_inc(&fs_info->scrubs_running);
4162 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004163
Stefan Behrensff023aa2012-11-06 11:43:11 +01004164 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08004165 /*
4166 * by holding device list mutex, we can
4167 * kick off writing super in log tree sync.
4168 */
Wang Shilong3cb09292013-12-04 21:15:19 +08004169 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004170 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08004171 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004172 }
Arne Jansena2de7332011-03-08 14:14:00 +01004173
4174 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004175 ret = scrub_enumerate_chunks(sctx, dev, start, end,
4176 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01004177
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004178 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004179 atomic_dec(&fs_info->scrubs_running);
4180 wake_up(&fs_info->scrub_pause_wait);
4181
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004182 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02004183
Arne Jansena2de7332011-03-08 14:14:00 +01004184 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004185 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01004186
4187 mutex_lock(&fs_info->scrub_lock);
4188 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08004189 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004190 mutex_unlock(&fs_info->scrub_lock);
4191
Filipe Mananaf55985f2015-02-09 21:14:24 +00004192 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004193
4194 return ret;
4195}
4196
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004197void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004198{
Arne Jansena2de7332011-03-08 14:14:00 +01004199 mutex_lock(&fs_info->scrub_lock);
4200 atomic_inc(&fs_info->scrub_pause_req);
4201 while (atomic_read(&fs_info->scrubs_paused) !=
4202 atomic_read(&fs_info->scrubs_running)) {
4203 mutex_unlock(&fs_info->scrub_lock);
4204 wait_event(fs_info->scrub_pause_wait,
4205 atomic_read(&fs_info->scrubs_paused) ==
4206 atomic_read(&fs_info->scrubs_running));
4207 mutex_lock(&fs_info->scrub_lock);
4208 }
4209 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004210}
4211
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004212void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004213{
Arne Jansena2de7332011-03-08 14:14:00 +01004214 atomic_dec(&fs_info->scrub_pause_req);
4215 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01004216}
4217
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004218int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004219{
Arne Jansena2de7332011-03-08 14:14:00 +01004220 mutex_lock(&fs_info->scrub_lock);
4221 if (!atomic_read(&fs_info->scrubs_running)) {
4222 mutex_unlock(&fs_info->scrub_lock);
4223 return -ENOTCONN;
4224 }
4225
4226 atomic_inc(&fs_info->scrub_cancel_req);
4227 while (atomic_read(&fs_info->scrubs_running)) {
4228 mutex_unlock(&fs_info->scrub_lock);
4229 wait_event(fs_info->scrub_pause_wait,
4230 atomic_read(&fs_info->scrubs_running) == 0);
4231 mutex_lock(&fs_info->scrub_lock);
4232 }
4233 atomic_dec(&fs_info->scrub_cancel_req);
4234 mutex_unlock(&fs_info->scrub_lock);
4235
4236 return 0;
4237}
4238
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004239int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
4240 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004241{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004242 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004243
4244 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004245 sctx = dev->scrub_device;
4246 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004247 mutex_unlock(&fs_info->scrub_lock);
4248 return -ENOTCONN;
4249 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004250 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01004251 while (dev->scrub_device) {
4252 mutex_unlock(&fs_info->scrub_lock);
4253 wait_event(fs_info->scrub_pause_wait,
4254 dev->scrub_device == NULL);
4255 mutex_lock(&fs_info->scrub_lock);
4256 }
4257 mutex_unlock(&fs_info->scrub_lock);
4258
4259 return 0;
4260}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004261
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004262int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01004263 struct btrfs_scrub_progress *progress)
4264{
4265 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004266 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004267
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004268 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4269 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004270 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004271 sctx = dev->scrub_device;
4272 if (sctx)
4273 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004274 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004275
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004276 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004277}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004278
4279static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4280 u64 extent_logical, u64 extent_len,
4281 u64 *extent_physical,
4282 struct btrfs_device **extent_dev,
4283 int *extent_mirror_num)
4284{
4285 u64 mapped_length;
4286 struct btrfs_bio *bbio = NULL;
4287 int ret;
4288
4289 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004290 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004291 &mapped_length, &bbio, 0);
4292 if (ret || !bbio || mapped_length < extent_len ||
4293 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08004294 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004295 return;
4296 }
4297
4298 *extent_physical = bbio->stripes[0].physical;
4299 *extent_mirror_num = bbio->mirror_num;
4300 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004301 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004302}
4303
Stefan Behrensff023aa2012-11-06 11:43:11 +01004304static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
4305 int mirror_num, u64 physical_for_dev_replace)
4306{
4307 struct scrub_copy_nocow_ctx *nocow_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004308 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004309
4310 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
4311 if (!nocow_ctx) {
4312 spin_lock(&sctx->stat_lock);
4313 sctx->stat.malloc_errors++;
4314 spin_unlock(&sctx->stat_lock);
4315 return -ENOMEM;
4316 }
4317
4318 scrub_pending_trans_workers_inc(sctx);
4319
4320 nocow_ctx->sctx = sctx;
4321 nocow_ctx->logical = logical;
4322 nocow_ctx->len = len;
4323 nocow_ctx->mirror_num = mirror_num;
4324 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08004325 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
4326 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04004327 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08004328 btrfs_queue_work(fs_info->scrub_nocow_workers,
4329 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004330
4331 return 0;
4332}
4333
Josef Bacik652f25a2013-09-12 16:58:28 -04004334static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
4335{
4336 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
4337 struct scrub_nocow_inode *nocow_inode;
4338
4339 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
4340 if (!nocow_inode)
4341 return -ENOMEM;
4342 nocow_inode->inum = inum;
4343 nocow_inode->offset = offset;
4344 nocow_inode->root = root;
4345 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
4346 return 0;
4347}
4348
4349#define COPY_COMPLETE 1
4350
Stefan Behrensff023aa2012-11-06 11:43:11 +01004351static void copy_nocow_pages_worker(struct btrfs_work *work)
4352{
4353 struct scrub_copy_nocow_ctx *nocow_ctx =
4354 container_of(work, struct scrub_copy_nocow_ctx, work);
4355 struct scrub_ctx *sctx = nocow_ctx->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004356 struct btrfs_fs_info *fs_info = sctx->fs_info;
4357 struct btrfs_root *root = fs_info->extent_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004358 u64 logical = nocow_ctx->logical;
4359 u64 len = nocow_ctx->len;
4360 int mirror_num = nocow_ctx->mirror_num;
4361 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4362 int ret;
4363 struct btrfs_trans_handle *trans = NULL;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004364 struct btrfs_path *path;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004365 int not_written = 0;
4366
Stefan Behrensff023aa2012-11-06 11:43:11 +01004367 path = btrfs_alloc_path();
4368 if (!path) {
4369 spin_lock(&sctx->stat_lock);
4370 sctx->stat.malloc_errors++;
4371 spin_unlock(&sctx->stat_lock);
4372 not_written = 1;
4373 goto out;
4374 }
4375
4376 trans = btrfs_join_transaction(root);
4377 if (IS_ERR(trans)) {
4378 not_written = 1;
4379 goto out;
4380 }
4381
4382 ret = iterate_inodes_from_logical(logical, fs_info, path,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04004383 record_inode_for_nocow, nocow_ctx, false);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004384 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004385 btrfs_warn(fs_info,
4386 "iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d",
4387 logical, physical_for_dev_replace, len, mirror_num,
4388 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004389 not_written = 1;
4390 goto out;
4391 }
4392
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004393 btrfs_end_transaction(trans);
Josef Bacik652f25a2013-09-12 16:58:28 -04004394 trans = NULL;
4395 while (!list_empty(&nocow_ctx->inodes)) {
4396 struct scrub_nocow_inode *entry;
4397 entry = list_first_entry(&nocow_ctx->inodes,
4398 struct scrub_nocow_inode,
4399 list);
4400 list_del_init(&entry->list);
4401 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4402 entry->root, nocow_ctx);
4403 kfree(entry);
4404 if (ret == COPY_COMPLETE) {
4405 ret = 0;
4406 break;
4407 } else if (ret) {
4408 break;
4409 }
4410 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004411out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004412 while (!list_empty(&nocow_ctx->inodes)) {
4413 struct scrub_nocow_inode *entry;
4414 entry = list_first_entry(&nocow_ctx->inodes,
4415 struct scrub_nocow_inode,
4416 list);
4417 list_del_init(&entry->list);
4418 kfree(entry);
4419 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004420 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004421 btrfs_end_transaction(trans);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004422 if (not_written)
4423 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4424 num_uncorrectable_read_errors);
4425
4426 btrfs_free_path(path);
4427 kfree(nocow_ctx);
4428
4429 scrub_pending_trans_workers_dec(sctx);
4430}
4431
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004432static int check_extent_to_block(struct btrfs_inode *inode, u64 start, u64 len,
Gui Hecheng32159242014-11-10 15:36:08 +08004433 u64 logical)
4434{
4435 struct extent_state *cached_state = NULL;
4436 struct btrfs_ordered_extent *ordered;
4437 struct extent_io_tree *io_tree;
4438 struct extent_map *em;
4439 u64 lockstart = start, lockend = start + len - 1;
4440 int ret = 0;
4441
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004442 io_tree = &inode->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004443
David Sterbaff13db42015-12-03 14:30:40 +01004444 lock_extent_bits(io_tree, lockstart, lockend, &cached_state);
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004445 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
Gui Hecheng32159242014-11-10 15:36:08 +08004446 if (ordered) {
4447 btrfs_put_ordered_extent(ordered);
4448 ret = 1;
4449 goto out_unlock;
4450 }
4451
4452 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4453 if (IS_ERR(em)) {
4454 ret = PTR_ERR(em);
4455 goto out_unlock;
4456 }
4457
4458 /*
4459 * This extent does not actually cover the logical extent anymore,
4460 * move on to the next inode.
4461 */
4462 if (em->block_start > logical ||
4463 em->block_start + em->block_len < logical + len) {
4464 free_extent_map(em);
4465 ret = 1;
4466 goto out_unlock;
4467 }
4468 free_extent_map(em);
4469
4470out_unlock:
4471 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
4472 GFP_NOFS);
4473 return ret;
4474}
4475
Josef Bacik652f25a2013-09-12 16:58:28 -04004476static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4477 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004478{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004479 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004480 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004481 struct inode *inode;
4482 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004483 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004484 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004485 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004486 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004487 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004488 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004489 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004490 int ret = 0;
4491 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004492
4493 key.objectid = root;
4494 key.type = BTRFS_ROOT_ITEM_KEY;
4495 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004496
4497 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4498
Stefan Behrensff023aa2012-11-06 11:43:11 +01004499 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004500 if (IS_ERR(local_root)) {
4501 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004502 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004503 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004504
4505 key.type = BTRFS_INODE_ITEM_KEY;
4506 key.objectid = inum;
4507 key.offset = 0;
4508 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004509 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004510 if (IS_ERR(inode))
4511 return PTR_ERR(inode);
4512
Miao Xieedd14002013-06-27 18:51:00 +08004513 /* Avoid truncate/dio/punch hole.. */
Al Viro59551022016-01-22 15:40:57 -05004514 inode_lock(inode);
Miao Xieedd14002013-06-27 18:51:00 +08004515 inode_dio_wait(inode);
4516
Stefan Behrensff023aa2012-11-06 11:43:11 +01004517 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004518 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004519 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004520
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004521 ret = check_extent_to_block(BTRFS_I(inode), offset, len,
4522 nocow_ctx_logical);
Gui Hecheng32159242014-11-10 15:36:08 +08004523 if (ret) {
4524 ret = ret > 0 ? 0 : ret;
4525 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004526 }
4527
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004528 while (len >= PAGE_SIZE) {
4529 index = offset >> PAGE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004530again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004531 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4532 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004533 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004534 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004535 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004536 }
4537
4538 if (PageUptodate(page)) {
4539 if (PageDirty(page))
4540 goto next_page;
4541 } else {
4542 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004543 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004544 btrfs_get_extent,
4545 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004546 if (err) {
4547 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004548 goto next_page;
4549 }
Miao Xieedd14002013-06-27 18:51:00 +08004550
Miao Xie26b258912013-06-27 18:50:58 +08004551 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004552 /*
4553 * If the page has been remove from the page cache,
4554 * the data on it is meaningless, because it may be
4555 * old one, the new data may be written into the new
4556 * page in the page cache.
4557 */
4558 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004559 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004560 put_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004561 goto again;
4562 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004563 if (!PageUptodate(page)) {
4564 ret = -EIO;
4565 goto next_page;
4566 }
4567 }
Gui Hecheng32159242014-11-10 15:36:08 +08004568
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004569 ret = check_extent_to_block(BTRFS_I(inode), offset, len,
Gui Hecheng32159242014-11-10 15:36:08 +08004570 nocow_ctx_logical);
4571 if (ret) {
4572 ret = ret > 0 ? 0 : ret;
4573 goto next_page;
4574 }
4575
Miao Xie826aa0a2013-06-27 18:50:59 +08004576 err = write_page_nocow(nocow_ctx->sctx,
4577 physical_for_dev_replace, page);
4578 if (err)
4579 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004580next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004581 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004582 put_page(page);
Miao Xie826aa0a2013-06-27 18:50:59 +08004583
4584 if (ret)
4585 break;
4586
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004587 offset += PAGE_SIZE;
4588 physical_for_dev_replace += PAGE_SIZE;
4589 nocow_ctx_logical += PAGE_SIZE;
4590 len -= PAGE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004591 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004592 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004593out:
Al Viro59551022016-01-22 15:40:57 -05004594 inode_unlock(inode);
Miao Xie826aa0a2013-06-27 18:50:59 +08004595 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004596 return ret;
4597}
4598
4599static int write_page_nocow(struct scrub_ctx *sctx,
4600 u64 physical_for_dev_replace, struct page *page)
4601{
4602 struct bio *bio;
4603 struct btrfs_device *dev;
4604 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004605
David Sterba3fb99302017-05-16 19:10:32 +02004606 dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004607 if (!dev)
4608 return -EIO;
4609 if (!dev->bdev) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004610 btrfs_warn_rl(dev->fs_info,
David Sterba94647322015-10-08 11:01:36 +02004611 "scrub write_page_nocow(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004612 return -EIO;
4613 }
David Sterbac5e4c3d2017-06-12 17:29:41 +02004614 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004615 bio->bi_iter.bi_size = 0;
4616 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02004617 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06004618 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004619 ret = bio_add_page(bio, page, PAGE_SIZE, 0);
4620 if (ret != PAGE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004621leave_with_eio:
4622 bio_put(bio);
4623 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4624 return -EIO;
4625 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004626
Mike Christie4e49ea42016-06-05 14:31:41 -05004627 if (btrfsic_submit_bio_wait(bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01004628 goto leave_with_eio;
4629
4630 bio_put(bio);
4631 return 0;
4632}