blob: f4c9b30903b0586742a248e6c9902a2ed08bc7cf [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
Liu Bo762221f2018-01-02 13:36:42 -0700304static inline int scrub_is_page_on_raid56(struct scrub_page *page)
305{
306 return page->recover &&
307 (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK);
308}
Stefan Behrens1623ede2012-03-27 14:21:26 -0400309
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100310static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
311{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200312 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100313 atomic_inc(&sctx->bios_in_flight);
314}
315
316static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
317{
318 atomic_dec(&sctx->bios_in_flight);
319 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000320 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100321}
322
Wang Shilongcb7ab022013-12-04 21:16:53 +0800323static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800324{
325 while (atomic_read(&fs_info->scrub_pause_req)) {
326 mutex_unlock(&fs_info->scrub_lock);
327 wait_event(fs_info->scrub_pause_wait,
328 atomic_read(&fs_info->scrub_pause_req) == 0);
329 mutex_lock(&fs_info->scrub_lock);
330 }
331}
332
Zhaolei0e22be82015-08-05 16:43:28 +0800333static void scrub_pause_on(struct btrfs_fs_info *fs_info)
Wang Shilongcb7ab022013-12-04 21:16:53 +0800334{
335 atomic_inc(&fs_info->scrubs_paused);
336 wake_up(&fs_info->scrub_pause_wait);
Zhaolei0e22be82015-08-05 16:43:28 +0800337}
Wang Shilongcb7ab022013-12-04 21:16:53 +0800338
Zhaolei0e22be82015-08-05 16:43:28 +0800339static void scrub_pause_off(struct btrfs_fs_info *fs_info)
340{
Wang Shilongcb7ab022013-12-04 21:16:53 +0800341 mutex_lock(&fs_info->scrub_lock);
342 __scrub_blocked_if_needed(fs_info);
343 atomic_dec(&fs_info->scrubs_paused);
344 mutex_unlock(&fs_info->scrub_lock);
345
346 wake_up(&fs_info->scrub_pause_wait);
347}
348
Zhaolei0e22be82015-08-05 16:43:28 +0800349static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
350{
351 scrub_pause_on(fs_info);
352 scrub_pause_off(fs_info);
353}
354
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100355/*
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800356 * Insert new full stripe lock into full stripe locks tree
357 *
358 * Return pointer to existing or newly inserted full_stripe_lock structure if
359 * everything works well.
360 * Return ERR_PTR(-ENOMEM) if we failed to allocate memory
361 *
362 * NOTE: caller must hold full_stripe_locks_root->lock before calling this
363 * function
364 */
365static struct full_stripe_lock *insert_full_stripe_lock(
366 struct btrfs_full_stripe_locks_tree *locks_root,
367 u64 fstripe_logical)
368{
369 struct rb_node **p;
370 struct rb_node *parent = NULL;
371 struct full_stripe_lock *entry;
372 struct full_stripe_lock *ret;
373
374 WARN_ON(!mutex_is_locked(&locks_root->lock));
375
376 p = &locks_root->root.rb_node;
377 while (*p) {
378 parent = *p;
379 entry = rb_entry(parent, struct full_stripe_lock, node);
380 if (fstripe_logical < entry->logical) {
381 p = &(*p)->rb_left;
382 } else if (fstripe_logical > entry->logical) {
383 p = &(*p)->rb_right;
384 } else {
385 entry->refs++;
386 return entry;
387 }
388 }
389
390 /* Insert new lock */
391 ret = kmalloc(sizeof(*ret), GFP_KERNEL);
392 if (!ret)
393 return ERR_PTR(-ENOMEM);
394 ret->logical = fstripe_logical;
395 ret->refs = 1;
396 mutex_init(&ret->mutex);
397
398 rb_link_node(&ret->node, parent, p);
399 rb_insert_color(&ret->node, &locks_root->root);
400 return ret;
401}
402
403/*
404 * Search for a full stripe lock of a block group
405 *
406 * Return pointer to existing full stripe lock if found
407 * Return NULL if not found
408 */
409static struct full_stripe_lock *search_full_stripe_lock(
410 struct btrfs_full_stripe_locks_tree *locks_root,
411 u64 fstripe_logical)
412{
413 struct rb_node *node;
414 struct full_stripe_lock *entry;
415
416 WARN_ON(!mutex_is_locked(&locks_root->lock));
417
418 node = locks_root->root.rb_node;
419 while (node) {
420 entry = rb_entry(node, struct full_stripe_lock, node);
421 if (fstripe_logical < entry->logical)
422 node = node->rb_left;
423 else if (fstripe_logical > entry->logical)
424 node = node->rb_right;
425 else
426 return entry;
427 }
428 return NULL;
429}
430
431/*
432 * Helper to get full stripe logical from a normal bytenr.
433 *
434 * Caller must ensure @cache is a RAID56 block group.
435 */
436static u64 get_full_stripe_logical(struct btrfs_block_group_cache *cache,
437 u64 bytenr)
438{
439 u64 ret;
440
441 /*
442 * Due to chunk item size limit, full stripe length should not be
443 * larger than U32_MAX. Just a sanity check here.
444 */
445 WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX);
446
447 /*
448 * round_down() can only handle power of 2, while RAID56 full
449 * stripe length can be 64KiB * n, so we need to manually round down.
450 */
451 ret = div64_u64(bytenr - cache->key.objectid, cache->full_stripe_len) *
452 cache->full_stripe_len + cache->key.objectid;
453 return ret;
454}
455
456/*
457 * Lock a full stripe to avoid concurrency of recovery and read
458 *
459 * It's only used for profiles with parities (RAID5/6), for other profiles it
460 * does nothing.
461 *
462 * Return 0 if we locked full stripe covering @bytenr, with a mutex held.
463 * So caller must call unlock_full_stripe() at the same context.
464 *
465 * Return <0 if encounters error.
466 */
467static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
468 bool *locked_ret)
469{
470 struct btrfs_block_group_cache *bg_cache;
471 struct btrfs_full_stripe_locks_tree *locks_root;
472 struct full_stripe_lock *existing;
473 u64 fstripe_start;
474 int ret = 0;
475
476 *locked_ret = false;
477 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
478 if (!bg_cache) {
479 ASSERT(0);
480 return -ENOENT;
481 }
482
483 /* Profiles not based on parity don't need full stripe lock */
484 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
485 goto out;
486 locks_root = &bg_cache->full_stripe_locks_root;
487
488 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
489
490 /* Now insert the full stripe lock */
491 mutex_lock(&locks_root->lock);
492 existing = insert_full_stripe_lock(locks_root, fstripe_start);
493 mutex_unlock(&locks_root->lock);
494 if (IS_ERR(existing)) {
495 ret = PTR_ERR(existing);
496 goto out;
497 }
498 mutex_lock(&existing->mutex);
499 *locked_ret = true;
500out:
501 btrfs_put_block_group(bg_cache);
502 return ret;
503}
504
505/*
506 * Unlock a full stripe.
507 *
508 * NOTE: Caller must ensure it's the same context calling corresponding
509 * lock_full_stripe().
510 *
511 * Return 0 if we unlock full stripe without problem.
512 * Return <0 for error
513 */
514static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr,
515 bool locked)
516{
517 struct btrfs_block_group_cache *bg_cache;
518 struct btrfs_full_stripe_locks_tree *locks_root;
519 struct full_stripe_lock *fstripe_lock;
520 u64 fstripe_start;
521 bool freeit = false;
522 int ret = 0;
523
524 /* If we didn't acquire full stripe lock, no need to continue */
525 if (!locked)
526 return 0;
527
528 bg_cache = btrfs_lookup_block_group(fs_info, bytenr);
529 if (!bg_cache) {
530 ASSERT(0);
531 return -ENOENT;
532 }
533 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK))
534 goto out;
535
536 locks_root = &bg_cache->full_stripe_locks_root;
537 fstripe_start = get_full_stripe_logical(bg_cache, bytenr);
538
539 mutex_lock(&locks_root->lock);
540 fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start);
541 /* Unpaired unlock_full_stripe() detected */
542 if (!fstripe_lock) {
543 WARN_ON(1);
544 ret = -ENOENT;
545 mutex_unlock(&locks_root->lock);
546 goto out;
547 }
548
549 if (fstripe_lock->refs == 0) {
550 WARN_ON(1);
551 btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow",
552 fstripe_lock->logical);
553 } else {
554 fstripe_lock->refs--;
555 }
556
557 if (fstripe_lock->refs == 0) {
558 rb_erase(&fstripe_lock->node, &locks_root->root);
559 freeit = true;
560 }
561 mutex_unlock(&locks_root->lock);
562
563 mutex_unlock(&fstripe_lock->mutex);
564 if (freeit)
565 kfree(fstripe_lock);
566out:
567 btrfs_put_block_group(bg_cache);
568 return ret;
569}
570
571/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100572 * used for workers that require transaction commits (i.e., for the
573 * NOCOW case)
574 */
575static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
576{
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400577 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100578
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200579 refcount_inc(&sctx->refs);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100580 /*
581 * increment scrubs_running to prevent cancel requests from
582 * completing as long as a worker is running. we must also
583 * increment scrubs_paused to prevent deadlocking on pause
584 * requests used for transactions commits (as the worker uses a
585 * transaction context). it is safe to regard the worker
586 * as paused for all matters practical. effectively, we only
587 * avoid cancellation requests from completing.
588 */
589 mutex_lock(&fs_info->scrub_lock);
590 atomic_inc(&fs_info->scrubs_running);
591 atomic_inc(&fs_info->scrubs_paused);
592 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800593
594 /*
595 * check if @scrubs_running=@scrubs_paused condition
596 * inside wait_event() is not an atomic operation.
597 * which means we may inc/dec @scrub_running/paused
598 * at any time. Let's wake up @scrub_pause_wait as
599 * much as we can to let commit transaction blocked less.
600 */
601 wake_up(&fs_info->scrub_pause_wait);
602
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100603 atomic_inc(&sctx->workers_pending);
604}
605
606/* used for workers that require transaction commits */
607static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
608{
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400609 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100610
611 /*
612 * see scrub_pending_trans_workers_inc() why we're pretending
613 * to be paused in the scrub counters
614 */
615 mutex_lock(&fs_info->scrub_lock);
616 atomic_dec(&fs_info->scrubs_running);
617 atomic_dec(&fs_info->scrubs_paused);
618 mutex_unlock(&fs_info->scrub_lock);
619 atomic_dec(&sctx->workers_pending);
620 wake_up(&fs_info->scrub_pause_wait);
621 wake_up(&sctx->list_wait);
Filipe Mananaf55985f2015-02-09 21:14:24 +0000622 scrub_put_ctx(sctx);
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100623}
624
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100625static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100626{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100627 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100628 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100629 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100630 struct btrfs_ordered_sum, list);
631 list_del(&sum->list);
632 kfree(sum);
633 }
634}
635
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100636static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100637{
638 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100639
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100640 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100641 return;
642
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400643 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100644 if (sctx->curr != -1) {
645 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400646
647 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100648 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400649 scrub_block_put(sbio->pagev[i]->sblock);
650 }
651 bio_put(sbio->bio);
652 }
653
Stefan Behrensff023aa2012-11-06 11:43:11 +0100654 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100655 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100656
657 if (!sbio)
658 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100659 kfree(sbio);
660 }
661
David Sterba3fb99302017-05-16 19:10:32 +0200662 kfree(sctx->wr_curr_bio);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100663 scrub_free_csums(sctx);
664 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100665}
666
Filipe Mananaf55985f2015-02-09 21:14:24 +0000667static void scrub_put_ctx(struct scrub_ctx *sctx)
668{
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200669 if (refcount_dec_and_test(&sctx->refs))
Filipe Mananaf55985f2015-02-09 21:14:24 +0000670 scrub_free_ctx(sctx);
671}
672
Arne Jansena2de7332011-03-08 14:14:00 +0100673static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100674struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100675{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100676 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100677 int i;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400678 struct btrfs_fs_info *fs_info = dev->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100679
David Sterba58c4e172016-02-11 10:49:42 +0100680 sctx = kzalloc(sizeof(*sctx), GFP_KERNEL);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100681 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100682 goto nomem;
Elena Reshetova99f4cdb2017-03-03 10:55:25 +0200683 refcount_set(&sctx->refs, 1);
Stefan Behrens63a212a2012-11-05 18:29:28 +0100684 sctx->is_dev_replace = is_dev_replace;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200685 sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100686 sctx->curr = -1;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400687 sctx->fs_info = dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100688 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100689 struct scrub_bio *sbio;
690
David Sterba58c4e172016-02-11 10:49:42 +0100691 sbio = kzalloc(sizeof(*sbio), GFP_KERNEL);
Arne Jansena2de7332011-03-08 14:14:00 +0100692 if (!sbio)
693 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100694 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100695
Arne Jansena2de7332011-03-08 14:14:00 +0100696 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100697 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400698 sbio->page_count = 0;
Liu Bo9e0af232014-08-15 23:36:53 +0800699 btrfs_init_work(&sbio->work, btrfs_scrub_helper,
700 scrub_bio_end_io_worker, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100701
Stefan Behrensff023aa2012-11-06 11:43:11 +0100702 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100703 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200704 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100705 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100706 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100707 sctx->first_free = 0;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100708 atomic_set(&sctx->bios_in_flight, 0);
709 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100710 atomic_set(&sctx->cancel_req, 0);
711 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
712 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100713
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100714 spin_lock_init(&sctx->list_lock);
715 spin_lock_init(&sctx->stat_lock);
716 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100717
David Sterba3fb99302017-05-16 19:10:32 +0200718 WARN_ON(sctx->wr_curr_bio != NULL);
719 mutex_init(&sctx->wr_lock);
720 sctx->wr_curr_bio = NULL;
David Sterba8fcdac32017-05-16 19:10:23 +0200721 if (is_dev_replace) {
David Sterbaded56182017-06-26 15:19:00 +0200722 WARN_ON(!fs_info->dev_replace.tgtdev);
David Sterba3fb99302017-05-16 19:10:32 +0200723 sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO;
David Sterbaded56182017-06-26 15:19:00 +0200724 sctx->wr_tgtdev = fs_info->dev_replace.tgtdev;
David Sterba2073c4c2017-03-31 17:12:51 +0200725 sctx->flush_all_writes = false;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100726 }
David Sterba8fcdac32017-05-16 19:10:23 +0200727
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100728 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100729
730nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100731 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100732 return ERR_PTR(-ENOMEM);
733}
734
Stefan Behrensff023aa2012-11-06 11:43:11 +0100735static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
736 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200737{
738 u64 isize;
739 u32 nlink;
740 int ret;
741 int i;
David Sterbade2491f2017-05-31 19:21:38 +0200742 unsigned nofs_flag;
Jan Schmidt558540c2011-06-13 19:59:12 +0200743 struct extent_buffer *eb;
744 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100745 struct scrub_warning *swarn = warn_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400746 struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200747 struct inode_fs_paths *ipath = NULL;
748 struct btrfs_root *local_root;
749 struct btrfs_key root_key;
David Sterba1d4c08e2015-01-02 19:36:14 +0100750 struct btrfs_key key;
Jan Schmidt558540c2011-06-13 19:59:12 +0200751
752 root_key.objectid = root;
753 root_key.type = BTRFS_ROOT_ITEM_KEY;
754 root_key.offset = (u64)-1;
755 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
756 if (IS_ERR(local_root)) {
757 ret = PTR_ERR(local_root);
758 goto err;
759 }
760
David Sterba14692cc2015-01-02 18:55:46 +0100761 /*
762 * this makes the path point to (inum INODE_ITEM ioff)
763 */
David Sterba1d4c08e2015-01-02 19:36:14 +0100764 key.objectid = inum;
765 key.type = BTRFS_INODE_ITEM_KEY;
766 key.offset = 0;
767
768 ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0);
Jan Schmidt558540c2011-06-13 19:59:12 +0200769 if (ret) {
770 btrfs_release_path(swarn->path);
771 goto err;
772 }
773
774 eb = swarn->path->nodes[0];
775 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
776 struct btrfs_inode_item);
777 isize = btrfs_inode_size(eb, inode_item);
778 nlink = btrfs_inode_nlink(eb, inode_item);
779 btrfs_release_path(swarn->path);
780
David Sterbade2491f2017-05-31 19:21:38 +0200781 /*
782 * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub
783 * uses GFP_NOFS in this context, so we keep it consistent but it does
784 * not seem to be strictly necessary.
785 */
786 nofs_flag = memalloc_nofs_save();
Jan Schmidt558540c2011-06-13 19:59:12 +0200787 ipath = init_ipath(4096, local_root, swarn->path);
David Sterbade2491f2017-05-31 19:21:38 +0200788 memalloc_nofs_restore(nofs_flag);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300789 if (IS_ERR(ipath)) {
790 ret = PTR_ERR(ipath);
791 ipath = NULL;
792 goto err;
793 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200794 ret = paths_from_inode(inum, ipath);
795
796 if (ret < 0)
797 goto err;
798
799 /*
800 * we deliberately ignore the bit ipath might have been too small to
801 * hold all of the paths here
802 */
803 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400804 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200805"%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 -0400806 swarn->errstr, swarn->logical,
807 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200808 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400809 root, inum, offset,
810 min(isize - offset, (u64)PAGE_SIZE), nlink,
811 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200812
813 free_ipath(ipath);
814 return 0;
815
816err:
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400817 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200818 "%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 -0400819 swarn->errstr, swarn->logical,
820 rcu_str_deref(swarn->dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200821 swarn->physical,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400822 root, inum, offset, ret);
Jan Schmidt558540c2011-06-13 19:59:12 +0200823
824 free_ipath(ipath);
825 return 0;
826}
827
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400828static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200829{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100830 struct btrfs_device *dev;
831 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200832 struct btrfs_path *path;
833 struct btrfs_key found_key;
834 struct extent_buffer *eb;
835 struct btrfs_extent_item *ei;
836 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200837 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100838 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600839 u64 flags = 0;
840 u64 ref_root;
841 u32 item_size;
Dan Carpenter07c9a8e2016-03-11 11:08:56 +0300842 u8 ref_level = 0;
Liu Bo69917e42012-09-07 20:01:28 -0600843 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200844
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100845 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100846 dev = sblock->pagev[0]->dev;
Jeff Mahoneyfb456252016-06-22 18:54:56 -0400847 fs_info = sblock->sctx->fs_info;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100848
Jan Schmidt558540c2011-06-13 19:59:12 +0200849 path = btrfs_alloc_path();
David Sterba8b9456d2014-07-30 01:25:30 +0200850 if (!path)
851 return;
Jan Schmidt558540c2011-06-13 19:59:12 +0200852
David Sterba6aa21262017-10-04 17:07:07 +0200853 swarn.physical = sblock->pagev[0]->physical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100854 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200855 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100856 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200857
Liu Bo69917e42012-09-07 20:01:28 -0600858 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
859 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200860 if (ret < 0)
861 goto out;
862
Jan Schmidt4692cf52011-12-02 14:56:41 +0100863 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200864 swarn.extent_item_size = found_key.offset;
865
866 eb = path->nodes[0];
867 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
868 item_size = btrfs_item_size_nr(eb, path->slots[0]);
869
Liu Bo69917e42012-09-07 20:01:28 -0600870 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200871 do {
Liu Bo6eda71d2014-06-09 10:54:07 +0800872 ret = tree_backref_for_extent(&ptr, eb, &found_key, ei,
873 item_size, &ref_root,
874 &ref_level);
David Sterbaecaeb142015-10-08 09:01:03 +0200875 btrfs_warn_in_rcu(fs_info,
David Sterba6aa21262017-10-04 17:07:07 +0200876"%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400877 errstr, swarn.logical,
Josef Bacik606686e2012-06-04 14:03:51 -0400878 rcu_str_deref(dev->name),
David Sterba6aa21262017-10-04 17:07:07 +0200879 swarn.physical,
Jan Schmidt558540c2011-06-13 19:59:12 +0200880 ref_level ? "node" : "leaf",
881 ret < 0 ? -1 : ref_level,
882 ret < 0 ? -1 : ref_root);
883 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600884 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200885 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600886 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200887 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100888 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100889 iterate_extent_inodes(fs_info, found_key.objectid,
890 extent_item_pos, 1,
Zygo Blaxellc995ab32017-09-22 13:58:45 -0400891 scrub_print_warning_inode, &swarn, false);
Jan Schmidt558540c2011-06-13 19:59:12 +0200892 }
893
894out:
895 btrfs_free_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200896}
897
Stefan Behrensff023aa2012-11-06 11:43:11 +0100898static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200899{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200900 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200901 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100902 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200903 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200904 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200905 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200906 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000907 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200908 u64 end = offset + PAGE_SIZE - 1;
909 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000910 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200911
912 key.objectid = root;
913 key.type = BTRFS_ROOT_ITEM_KEY;
914 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000915
916 fs_info = fixup->root->fs_info;
917 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
918
919 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
920 if (IS_ERR(local_root)) {
921 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200922 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000923 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200924
925 key.type = BTRFS_INODE_ITEM_KEY;
926 key.objectid = inum;
927 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000928 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
929 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200930 if (IS_ERR(inode))
931 return PTR_ERR(inode);
932
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300933 index = offset >> PAGE_SHIFT;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200934
935 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200936 if (!page) {
937 ret = -ENOMEM;
938 goto out;
939 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200940
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200941 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200942 if (PageDirty(page)) {
943 /*
944 * we need to write the data to the defect sector. the
945 * data that was in that sector is not in memory,
946 * because the page was modified. we must not write the
947 * modified page to that sector.
948 *
949 * TODO: what could be done here: wait for the delalloc
950 * runner to write out that page (might involve
951 * COW) and see whether the sector is still
952 * referenced afterwards.
953 *
954 * For the meantime, we'll treat this error
955 * incorrectable, although there is a chance that a
956 * later scrub will find the bad sector again and that
957 * there's no dirty page in memory, then.
958 */
959 ret = -EIO;
960 goto out;
961 }
Josef Bacik6ec656b2017-05-05 11:57:14 -0400962 ret = repair_io_failure(fs_info, inum, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200963 fixup->logical, page,
Miao Xieffdd2012014-09-12 18:44:00 +0800964 offset - page_offset(page),
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200965 fixup->mirror_num);
966 unlock_page(page);
967 corrected = !ret;
968 } else {
969 /*
970 * we need to get good data first. the general readpage path
971 * will call repair_io_failure for us, we just have to make
972 * sure we read the bad mirror.
973 */
974 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
David Sterbaceeb0ae2016-04-26 23:54:39 +0200975 EXTENT_DAMAGED);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200976 if (ret) {
977 /* set_extent_bits should give proper error */
978 WARN_ON(ret > 0);
979 if (ret > 0)
980 ret = -EFAULT;
981 goto out;
982 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200983
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200984 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
985 btrfs_get_extent,
986 fixup->mirror_num);
987 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200988
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200989 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
990 end, EXTENT_DAMAGED, 0, NULL);
991 if (!corrected)
992 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
David Sterba91166212016-04-26 23:54:39 +0200993 EXTENT_DAMAGED);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200994 }
995
996out:
997 if (page)
998 put_page(page);
Tobias Klauser7fb18a02014-04-25 14:58:05 +0200999
1000 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001001
1002 if (ret < 0)
1003 return ret;
1004
1005 if (ret == 0 && corrected) {
1006 /*
1007 * we only need to call readpage for one of the inodes belonging
1008 * to this extent. so make iterate_extent_inodes stop
1009 */
1010 return 1;
1011 }
1012
1013 return -EIO;
1014}
1015
1016static void scrub_fixup_nodatasum(struct btrfs_work *work)
1017{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001018 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001019 int ret;
1020 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001021 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001022 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001023 struct btrfs_path *path;
1024 int uncorrectable = 0;
1025
1026 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001027 sctx = fixup->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001028 fs_info = fixup->root->fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001029
1030 path = btrfs_alloc_path();
1031 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001032 spin_lock(&sctx->stat_lock);
1033 ++sctx->stat.malloc_errors;
1034 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001035 uncorrectable = 1;
1036 goto out;
1037 }
1038
1039 trans = btrfs_join_transaction(fixup->root);
1040 if (IS_ERR(trans)) {
1041 uncorrectable = 1;
1042 goto out;
1043 }
1044
1045 /*
1046 * the idea is to trigger a regular read through the standard path. we
1047 * read a page from the (failed) logical address by specifying the
1048 * corresponding copynum of the failed sector. thus, that readpage is
1049 * expected to fail.
1050 * that is the point where on-the-fly error correction will kick in
1051 * (once it's finished) and rewrite the failed sector if a good copy
1052 * can be found.
1053 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001054 ret = iterate_inodes_from_logical(fixup->logical, fs_info, path,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001055 scrub_fixup_readpage, fixup, false);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001056 if (ret < 0) {
1057 uncorrectable = 1;
1058 goto out;
1059 }
1060 WARN_ON(ret != 1);
1061
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001062 spin_lock(&sctx->stat_lock);
1063 ++sctx->stat.corrected_errors;
1064 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001065
1066out:
1067 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001068 btrfs_end_transaction(trans);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001069 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001070 spin_lock(&sctx->stat_lock);
1071 ++sctx->stat.uncorrectable_errors;
1072 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001073 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001074 &fs_info->dev_replace.num_uncorrectable_read_errors);
1075 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02001076 "unable to fixup (nodatasum) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001077 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001078 }
1079
1080 btrfs_free_path(path);
1081 kfree(fixup);
1082
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001083 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02001084}
1085
Miao Xieaf8e2d12014-10-23 14:42:50 +08001086static inline void scrub_get_recover(struct scrub_recover *recover)
1087{
Elena Reshetova6f615012017-03-03 10:55:21 +02001088 refcount_inc(&recover->refs);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001089}
1090
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001091static inline void scrub_put_recover(struct btrfs_fs_info *fs_info,
1092 struct scrub_recover *recover)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001093{
Elena Reshetova6f615012017-03-03 10:55:21 +02001094 if (refcount_dec_and_test(&recover->refs)) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001095 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08001096 btrfs_put_bbio(recover->bbio);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001097 kfree(recover);
1098 }
1099}
1100
Arne Jansena2de7332011-03-08 14:14:00 +01001101/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001102 * scrub_handle_errored_block gets called when either verification of the
1103 * pages failed or the bio failed to read, e.g. with EIO. In the latter
1104 * case, this function handles all pages in the bio, even though only one
1105 * may be bad.
1106 * The goal of this function is to repair the errored block by using the
1107 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +01001108 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001109static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +01001110{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001111 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001112 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001113 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01001114 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001115 u64 logical;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001116 unsigned int failed_mirror_index;
1117 unsigned int is_metadata;
1118 unsigned int have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001119 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
1120 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +01001121 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001122 int mirror_index;
1123 int page_num;
1124 int success;
Qu Wenruo28d70e22017-04-14 08:35:55 +08001125 bool full_stripe_locked;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001126 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
1127 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +01001128
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001129 BUG_ON(sblock_to_check->page_count < 1);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001130 fs_info = sctx->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +00001131 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
1132 /*
1133 * if we find an error in a super block, we just report it.
1134 * They will get written with the next transaction commit
1135 * anyway
1136 */
1137 spin_lock(&sctx->stat_lock);
1138 ++sctx->stat.super_errors;
1139 spin_unlock(&sctx->stat_lock);
1140 return 0;
1141 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001142 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001143 logical = sblock_to_check->pagev[0]->logical;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001144 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
1145 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
1146 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001147 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001148 have_csum = sblock_to_check->pagev[0]->have_csum;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001149 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001150
Qu Wenruo28d70e22017-04-14 08:35:55 +08001151 /*
1152 * For RAID5/6, race can happen for a different device scrub thread.
1153 * For data corruption, Parity and Data threads will both try
1154 * to recovery the data.
1155 * Race can lead to doubly added csum error, or even unrecoverable
1156 * error.
1157 */
1158 ret = lock_full_stripe(fs_info, logical, &full_stripe_locked);
1159 if (ret < 0) {
1160 spin_lock(&sctx->stat_lock);
1161 if (ret == -ENOMEM)
1162 sctx->stat.malloc_errors++;
1163 sctx->stat.read_errors++;
1164 sctx->stat.uncorrectable_errors++;
1165 spin_unlock(&sctx->stat_lock);
1166 return ret;
1167 }
1168
Stefan Behrensff023aa2012-11-06 11:43:11 +01001169 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
1170 sblocks_for_recheck = NULL;
1171 goto nodatasum_case;
1172 }
1173
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001174 /*
1175 * read all mirrors one after the other. This includes to
1176 * re-read the extent or metadata block that failed (that was
1177 * the cause that this fixup code is called) another time,
1178 * page by page this time in order to know which pages
1179 * caused I/O errors and which ones are good (for all mirrors).
1180 * It is the goal to handle the situation when more than one
1181 * mirror contains I/O errors, but the errors do not
1182 * overlap, i.e. the data can be repaired by selecting the
1183 * pages from those mirrors without I/O error on the
1184 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
1185 * would be that mirror #1 has an I/O error on the first page,
1186 * the second page is good, and mirror #2 has an I/O error on
1187 * the second page, but the first page is good.
1188 * Then the first page of the first mirror can be repaired by
1189 * taking the first page of the second mirror, and the
1190 * second page of the second mirror can be repaired by
1191 * copying the contents of the 2nd page of the 1st mirror.
1192 * One more note: if the pages of one mirror contain I/O
1193 * errors, the checksum cannot be verified. In order to get
1194 * the best data for repairing, the first attempt is to find
1195 * a mirror without I/O errors and with a validated checksum.
1196 * Only if this is not possible, the pages are picked from
1197 * mirrors with I/O errors without considering the checksum.
1198 * If the latter is the case, at the end, the checksum of the
1199 * repaired area is verified in order to correctly maintain
1200 * the statistics.
1201 */
1202
David Sterba31e818f2015-02-20 18:00:26 +01001203 sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS,
1204 sizeof(*sblocks_for_recheck), GFP_NOFS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001205 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001206 spin_lock(&sctx->stat_lock);
1207 sctx->stat.malloc_errors++;
1208 sctx->stat.read_errors++;
1209 sctx->stat.uncorrectable_errors++;
1210 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001211 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001212 goto out;
1213 }
1214
1215 /* setup the context, map the logical blocks and alloc the pages */
Zhao Leibe50a8d2015-01-20 15:11:42 +08001216 ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001217 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001218 spin_lock(&sctx->stat_lock);
1219 sctx->stat.read_errors++;
1220 sctx->stat.uncorrectable_errors++;
1221 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001222 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001223 goto out;
1224 }
1225 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
1226 sblock_bad = sblocks_for_recheck + failed_mirror_index;
1227
1228 /* build and submit the bios for the failed mirror, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001229 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001230
1231 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
1232 sblock_bad->no_io_error_seen) {
1233 /*
1234 * the error disappeared after reading page by page, or
1235 * the area was part of a huge bio and other parts of the
1236 * bio caused I/O errors, or the block layer merged several
1237 * read requests into one and the error is caused by a
1238 * different bio (usually one of the two latter cases is
1239 * the cause)
1240 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001241 spin_lock(&sctx->stat_lock);
1242 sctx->stat.unverified_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001243 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001244 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001245
Stefan Behrensff023aa2012-11-06 11:43:11 +01001246 if (sctx->is_dev_replace)
1247 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001248 goto out;
1249 }
1250
1251 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001252 spin_lock(&sctx->stat_lock);
1253 sctx->stat.read_errors++;
1254 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001255 if (__ratelimit(&_rs))
1256 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001257 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001258 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001259 spin_lock(&sctx->stat_lock);
1260 sctx->stat.csum_errors++;
1261 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001262 if (__ratelimit(&_rs))
1263 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001264 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001265 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001266 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001267 spin_lock(&sctx->stat_lock);
1268 sctx->stat.verify_errors++;
1269 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001270 if (__ratelimit(&_rs))
1271 scrub_print_warning("checksum/header error",
1272 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001273 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001274 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001275 BTRFS_DEV_STAT_GENERATION_ERRS);
1276 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001277 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001278 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001279 }
1280
Ilya Dryomov33ef30a2013-11-03 19:06:38 +02001281 if (sctx->readonly) {
1282 ASSERT(!sctx->is_dev_replace);
1283 goto out;
1284 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001285
1286 if (!is_metadata && !have_csum) {
1287 struct scrub_fixup_nodatasum *fixup_nodatasum;
1288
Stefan Behrensff023aa2012-11-06 11:43:11 +01001289 WARN_ON(sctx->is_dev_replace);
1290
Zhao Leib25c94c2015-01-20 15:11:35 +08001291nodatasum_case:
1292
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001293 /*
1294 * !is_metadata and !have_csum, this means that the data
Nicholas D Steeves01327612016-05-19 21:18:45 -04001295 * might not be COWed, that it might be modified
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001296 * concurrently. The general strategy to work on the
1297 * commit root does not help in the case when COW is not
1298 * used.
1299 */
1300 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
1301 if (!fixup_nodatasum)
1302 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001303 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001304 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001305 fixup_nodatasum->logical = logical;
1306 fixup_nodatasum->root = fs_info->extent_root;
1307 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001308 scrub_pending_trans_workers_inc(sctx);
Liu Bo9e0af232014-08-15 23:36:53 +08001309 btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper,
1310 scrub_fixup_nodatasum, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001311 btrfs_queue_work(fs_info->scrub_workers,
1312 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001313 goto out;
1314 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001315
1316 /*
1317 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001318 * checksums.
1319 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001320 * errors and also does not have a checksum error.
1321 * If one is found, and if a checksum is present, the full block
1322 * that is known to contain an error is rewritten. Afterwards
1323 * the block is known to be corrected.
1324 * If a mirror is found which is completely correct, and no
1325 * checksum is present, only those pages are rewritten that had
1326 * an I/O error in the block to be repaired, since it cannot be
1327 * determined, which copy of the other pages is better (and it
1328 * could happen otherwise that a correct page would be
1329 * overwritten by a bad one).
1330 */
Liu Bo762221f2018-01-02 13:36:42 -07001331 for (mirror_index = 0; ;mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001332 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001333
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001334 if (mirror_index == failed_mirror_index)
1335 continue;
Liu Bo762221f2018-01-02 13:36:42 -07001336
1337 /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */
1338 if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1339 if (mirror_index >= BTRFS_MAX_MIRRORS)
1340 break;
1341 if (!sblocks_for_recheck[mirror_index].page_count)
1342 break;
1343
1344 sblock_other = sblocks_for_recheck + mirror_index;
1345 } else {
1346 struct scrub_recover *r = sblock_bad->pagev[0]->recover;
1347 int max_allowed = r->bbio->num_stripes -
1348 r->bbio->num_tgtdevs;
1349
1350 if (mirror_index >= max_allowed)
1351 break;
1352 if (!sblocks_for_recheck[1].page_count)
1353 break;
1354
1355 ASSERT(failed_mirror_index == 0);
1356 sblock_other = sblocks_for_recheck + 1;
1357 sblock_other->pagev[0]->mirror_num = 1 + mirror_index;
1358 }
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001359
1360 /* build and submit the bios, check checksums */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001361 scrub_recheck_block(fs_info, sblock_other, 0);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001362
1363 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001364 !sblock_other->checksum_error &&
1365 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001366 if (sctx->is_dev_replace) {
1367 scrub_write_block_to_dev_replace(sblock_other);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001368 goto corrected_error;
Zhao Lei114ab502015-01-20 15:11:36 +08001369 } else {
1370 ret = scrub_repair_block_from_good_copy(
1371 sblock_bad, sblock_other);
1372 if (!ret)
1373 goto corrected_error;
1374 }
Arne Jansena2de7332011-03-08 14:14:00 +01001375 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001376 }
1377
Zhao Leib968fed2015-01-20 15:11:41 +08001378 if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace)
1379 goto did_not_correct_error;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001380
1381 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001382 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001383 * repaired, continue by picking good copies of those pages.
1384 * Select the good pages from mirrors to rewrite bad pages from
1385 * the area to fix. Afterwards verify the checksum of the block
1386 * that is supposed to be repaired. This verification step is
1387 * only done for the purpose of statistic counting and for the
1388 * final scrub report, whether errors remain.
1389 * A perfect algorithm could make use of the checksum and try
1390 * all possible combinations of pages from the different mirrors
1391 * until the checksum verification succeeds. For example, when
1392 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1393 * of mirror #2 is readable but the final checksum test fails,
1394 * then the 2nd page of mirror #3 could be tried, whether now
Nicholas D Steeves01327612016-05-19 21:18:45 -04001395 * the final checksum succeeds. But this would be a rare
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001396 * exception and is therefore not implemented. At least it is
1397 * avoided that the good copy is overwritten.
1398 * A more useful improvement would be to pick the sectors
1399 * without I/O error based on sector sizes (512 bytes on legacy
1400 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1401 * mirror could be repaired by taking 512 byte of a different
1402 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1403 * area are unreadable.
1404 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001405 success = 1;
Zhao Leib968fed2015-01-20 15:11:41 +08001406 for (page_num = 0; page_num < sblock_bad->page_count;
1407 page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001408 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Zhao Leib968fed2015-01-20 15:11:41 +08001409 struct scrub_block *sblock_other = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001410
Zhao Leib968fed2015-01-20 15:11:41 +08001411 /* skip no-io-error page in scrub */
1412 if (!page_bad->io_error && !sctx->is_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001413 continue;
1414
Liu Bo47597002018-03-02 16:10:41 -07001415 if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) {
1416 /*
1417 * In case of dev replace, if raid56 rebuild process
1418 * didn't work out correct data, then copy the content
1419 * in sblock_bad to make sure target device is identical
1420 * to source device, instead of writing garbage data in
1421 * sblock_for_recheck array to target device.
1422 */
1423 sblock_other = NULL;
1424 } else if (page_bad->io_error) {
1425 /* try to find no-io-error page in mirrors */
Zhao Leib968fed2015-01-20 15:11:41 +08001426 for (mirror_index = 0;
1427 mirror_index < BTRFS_MAX_MIRRORS &&
1428 sblocks_for_recheck[mirror_index].page_count > 0;
1429 mirror_index++) {
1430 if (!sblocks_for_recheck[mirror_index].
1431 pagev[page_num]->io_error) {
1432 sblock_other = sblocks_for_recheck +
1433 mirror_index;
1434 break;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001435 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001436 }
Zhao Leib968fed2015-01-20 15:11:41 +08001437 if (!sblock_other)
1438 success = 0;
Jan Schmidt13db62b2011-06-13 19:56:13 +02001439 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001440
Zhao Leib968fed2015-01-20 15:11:41 +08001441 if (sctx->is_dev_replace) {
1442 /*
1443 * did not find a mirror to fetch the page
1444 * from. scrub_write_page_to_dev_replace()
1445 * handles this case (page->io_error), by
1446 * filling the block with zeros before
1447 * submitting the write request
1448 */
1449 if (!sblock_other)
1450 sblock_other = sblock_bad;
1451
1452 if (scrub_write_page_to_dev_replace(sblock_other,
1453 page_num) != 0) {
1454 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001455 &fs_info->dev_replace.num_write_errors);
Zhao Leib968fed2015-01-20 15:11:41 +08001456 success = 0;
1457 }
1458 } else if (sblock_other) {
1459 ret = scrub_repair_page_from_good_copy(sblock_bad,
1460 sblock_other,
1461 page_num, 0);
1462 if (0 == ret)
1463 page_bad->io_error = 0;
1464 else
1465 success = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001466 }
1467 }
1468
Zhao Leib968fed2015-01-20 15:11:41 +08001469 if (success && !sctx->is_dev_replace) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001470 if (is_metadata || have_csum) {
1471 /*
1472 * need to verify the checksum now that all
1473 * sectors on disk are repaired (the write
1474 * request for data to be repaired is on its way).
1475 * Just be lazy and use scrub_recheck_block()
1476 * which re-reads the data before the checksum
1477 * is verified, but most likely the data comes out
1478 * of the page cache.
1479 */
Zhao Leiaffe4a52015-08-24 21:32:06 +08001480 scrub_recheck_block(fs_info, sblock_bad, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001481 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001482 !sblock_bad->checksum_error &&
1483 sblock_bad->no_io_error_seen)
1484 goto corrected_error;
1485 else
1486 goto did_not_correct_error;
1487 } else {
1488corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001489 spin_lock(&sctx->stat_lock);
1490 sctx->stat.corrected_errors++;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001491 sblock_to_check->data_corrected = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001492 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001493 btrfs_err_rl_in_rcu(fs_info,
1494 "fixed up error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001495 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001496 }
1497 } else {
1498did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001499 spin_lock(&sctx->stat_lock);
1500 sctx->stat.uncorrectable_errors++;
1501 spin_unlock(&sctx->stat_lock);
David Sterbab14af3b2015-10-08 10:43:10 +02001502 btrfs_err_rl_in_rcu(fs_info,
1503 "unable to fixup (regular) error at logical %llu on dev %s",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001504 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001505 }
1506
1507out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001508 if (sblocks_for_recheck) {
1509 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1510 mirror_index++) {
1511 struct scrub_block *sblock = sblocks_for_recheck +
1512 mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001513 struct scrub_recover *recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001514 int page_index;
1515
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001516 for (page_index = 0; page_index < sblock->page_count;
1517 page_index++) {
1518 sblock->pagev[page_index]->sblock = NULL;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001519 recover = sblock->pagev[page_index]->recover;
1520 if (recover) {
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001521 scrub_put_recover(fs_info, recover);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001522 sblock->pagev[page_index]->recover =
1523 NULL;
1524 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001525 scrub_page_put(sblock->pagev[page_index]);
1526 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001527 }
1528 kfree(sblocks_for_recheck);
1529 }
1530
Qu Wenruo28d70e22017-04-14 08:35:55 +08001531 ret = unlock_full_stripe(fs_info, logical, full_stripe_locked);
1532 if (ret < 0)
1533 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001534 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001535}
1536
Zhao Lei8e5cfb52015-01-20 15:11:33 +08001537static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001538{
Zhao Lei10f11902015-01-20 15:11:43 +08001539 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1540 return 2;
1541 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1542 return 3;
1543 else
Miao Xieaf8e2d12014-10-23 14:42:50 +08001544 return (int)bbio->num_stripes;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001545}
1546
Zhao Lei10f11902015-01-20 15:11:43 +08001547static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type,
1548 u64 *raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001549 u64 mapped_length,
1550 int nstripes, int mirror,
1551 int *stripe_index,
1552 u64 *stripe_offset)
1553{
1554 int i;
1555
Zhao Leiffe2d202015-01-20 15:11:44 +08001556 if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001557 /* RAID5/6 */
1558 for (i = 0; i < nstripes; i++) {
1559 if (raid_map[i] == RAID6_Q_STRIPE ||
1560 raid_map[i] == RAID5_P_STRIPE)
1561 continue;
1562
1563 if (logical >= raid_map[i] &&
1564 logical < raid_map[i] + mapped_length)
1565 break;
1566 }
1567
1568 *stripe_index = i;
1569 *stripe_offset = logical - raid_map[i];
1570 } else {
1571 /* The other RAID type */
1572 *stripe_index = mirror;
1573 *stripe_offset = 0;
1574 }
1575}
1576
Zhao Leibe50a8d2015-01-20 15:11:42 +08001577static int scrub_setup_recheck_block(struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001578 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001579{
Zhao Leibe50a8d2015-01-20 15:11:42 +08001580 struct scrub_ctx *sctx = original_sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001581 struct btrfs_fs_info *fs_info = sctx->fs_info;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001582 u64 length = original_sblock->page_count * PAGE_SIZE;
1583 u64 logical = original_sblock->pagev[0]->logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001584 u64 generation = original_sblock->pagev[0]->generation;
1585 u64 flags = original_sblock->pagev[0]->flags;
1586 u64 have_csum = original_sblock->pagev[0]->have_csum;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001587 struct scrub_recover *recover;
1588 struct btrfs_bio *bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001589 u64 sublen;
1590 u64 mapped_length;
1591 u64 stripe_offset;
1592 int stripe_index;
Zhao Leibe50a8d2015-01-20 15:11:42 +08001593 int page_index = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001594 int mirror_index;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001595 int nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001596 int ret;
1597
1598 /*
Zhao Lei57019342015-01-20 15:11:45 +08001599 * note: the two members refs and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001600 * are not used (and not set) in the blocks that are used for
1601 * the recheck procedure
1602 */
1603
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001604 while (length > 0) {
Miao Xieaf8e2d12014-10-23 14:42:50 +08001605 sublen = min_t(u64, length, PAGE_SIZE);
1606 mapped_length = sublen;
1607 bbio = NULL;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001608
1609 /*
1610 * with a length of PAGE_SIZE, each returned stripe
1611 * represents one mirror
1612 */
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001613 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001614 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
David Sterba825ad4c2017-03-28 14:45:22 +02001615 logical, &mapped_length, &bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001616 if (ret || !bbio || mapped_length < sublen) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001617 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001618 btrfs_bio_counter_dec(fs_info);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001619 return -EIO;
1620 }
1621
Miao Xieaf8e2d12014-10-23 14:42:50 +08001622 recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS);
1623 if (!recover) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08001624 btrfs_put_bbio(bbio);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001625 btrfs_bio_counter_dec(fs_info);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001626 return -ENOMEM;
1627 }
1628
Elena Reshetova6f615012017-03-03 10:55:21 +02001629 refcount_set(&recover->refs, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001630 recover->bbio = bbio;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001631 recover->map_length = mapped_length;
1632
Ashish Samant24731142016-04-29 18:33:59 -07001633 BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001634
Zhao Leibe50a8d2015-01-20 15:11:42 +08001635 nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS);
Zhao Lei10f11902015-01-20 15:11:43 +08001636
Miao Xieaf8e2d12014-10-23 14:42:50 +08001637 for (mirror_index = 0; mirror_index < nmirrors;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001638 mirror_index++) {
1639 struct scrub_block *sblock;
1640 struct scrub_page *page;
1641
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001642 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001643 sblock->sctx = sctx;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001644
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001645 page = kzalloc(sizeof(*page), GFP_NOFS);
1646 if (!page) {
1647leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001648 spin_lock(&sctx->stat_lock);
1649 sctx->stat.malloc_errors++;
1650 spin_unlock(&sctx->stat_lock);
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001651 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001652 return -ENOMEM;
1653 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001654 scrub_page_get(page);
1655 sblock->pagev[page_index] = page;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001656 page->sblock = sblock;
1657 page->flags = flags;
1658 page->generation = generation;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001659 page->logical = logical;
Zhao Lei4734b7e2015-08-19 22:39:18 +08001660 page->have_csum = have_csum;
1661 if (have_csum)
1662 memcpy(page->csum,
1663 original_sblock->pagev[0]->csum,
1664 sctx->csum_size);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001665
Zhao Lei10f11902015-01-20 15:11:43 +08001666 scrub_stripe_index_and_offset(logical,
1667 bbio->map_type,
1668 bbio->raid_map,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001669 mapped_length,
Zhao Leie34c3302015-01-20 15:11:31 +08001670 bbio->num_stripes -
1671 bbio->num_tgtdevs,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001672 mirror_index,
1673 &stripe_index,
1674 &stripe_offset);
1675 page->physical = bbio->stripes[stripe_index].physical +
1676 stripe_offset;
1677 page->dev = bbio->stripes[stripe_index].dev;
1678
Stefan Behrensff023aa2012-11-06 11:43:11 +01001679 BUG_ON(page_index >= original_sblock->page_count);
1680 page->physical_for_dev_replace =
1681 original_sblock->pagev[page_index]->
1682 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001683 /* for missing devices, dev->bdev is NULL */
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001684 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001685 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001686 page->page = alloc_page(GFP_NOFS);
1687 if (!page->page)
1688 goto leave_nomem;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001689
1690 scrub_get_recover(recover);
1691 page->recover = recover;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001692 }
Qu Wenruoe501bfe2017-03-29 09:33:22 +08001693 scrub_put_recover(fs_info, recover);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001694 length -= sublen;
1695 logical += sublen;
1696 page_index++;
1697 }
1698
1699 return 0;
1700}
1701
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001702static void scrub_bio_wait_endio(struct bio *bio)
Miao Xieaf8e2d12014-10-23 14:42:50 +08001703{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001704 complete(bio->bi_private);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001705}
1706
Miao Xieaf8e2d12014-10-23 14:42:50 +08001707static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1708 struct bio *bio,
1709 struct scrub_page *page)
1710{
Liu Bob4ff5ad2017-11-30 17:26:39 -07001711 DECLARE_COMPLETION_ONSTACK(done);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001712 int ret;
Liu Bo762221f2018-01-02 13:36:42 -07001713 int mirror_num;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001714
Miao Xieaf8e2d12014-10-23 14:42:50 +08001715 bio->bi_iter.bi_sector = page->logical >> 9;
1716 bio->bi_private = &done;
1717 bio->bi_end_io = scrub_bio_wait_endio;
1718
Liu Bo762221f2018-01-02 13:36:42 -07001719 mirror_num = page->sblock->pagev[0]->mirror_num;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001720 ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
Miao Xieaf8e2d12014-10-23 14:42:50 +08001721 page->recover->map_length,
Liu Bo762221f2018-01-02 13:36:42 -07001722 mirror_num, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001723 if (ret)
1724 return ret;
1725
Liu Bob4ff5ad2017-11-30 17:26:39 -07001726 wait_for_completion_io(&done);
1727 return blk_status_to_errno(bio->bi_status);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001728}
1729
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001730/*
1731 * this function will check the on disk data for checksum errors, header
1732 * errors and read I/O errors. If any I/O errors happen, the exact pages
1733 * which are errored are marked as being bad. The goal is to enable scrub
1734 * to take those pages that are not errored from all the mirrors so that
1735 * the pages that are errored in the just handled mirror can be repaired.
1736 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001737static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
Zhao Leiaffe4a52015-08-24 21:32:06 +08001738 struct scrub_block *sblock,
1739 int retry_failed_mirror)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001740{
1741 int page_num;
1742
1743 sblock->no_io_error_seen = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001744
1745 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1746 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001747 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001748
Stefan Behrens442a4f62012-05-25 16:06:08 +02001749 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001750 page->io_error = 1;
1751 sblock->no_io_error_seen = 0;
1752 continue;
1753 }
1754
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001755 WARN_ON(!page->page);
David Sterbac5e4c3d2017-06-12 17:29:41 +02001756 bio = btrfs_io_bio_alloc(1);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001757 bio_set_dev(bio, page->dev->bdev);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001758
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001759 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001760 if (!retry_failed_mirror && scrub_is_page_on_raid56(page)) {
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001761 if (scrub_submit_raid56_bio_wait(fs_info, bio, page)) {
1762 page->io_error = 1;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001763 sblock->no_io_error_seen = 0;
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001764 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001765 } else {
1766 bio->bi_iter.bi_sector = page->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001767 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Miao Xieaf8e2d12014-10-23 14:42:50 +08001768
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001769 if (btrfsic_submit_bio_wait(bio)) {
1770 page->io_error = 1;
Miao Xieaf8e2d12014-10-23 14:42:50 +08001771 sblock->no_io_error_seen = 0;
Liu Bo1bcd7aa2017-03-29 10:55:16 -07001772 }
Miao Xieaf8e2d12014-10-23 14:42:50 +08001773 }
Kent Overstreet33879d42013-11-23 22:33:32 -08001774
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001775 bio_put(bio);
1776 }
1777
1778 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08001779 scrub_recheck_block_checksum(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001780}
1781
Miao Xie17a9be22014-07-24 11:37:08 +08001782static inline int scrub_check_fsid(u8 fsid[],
1783 struct scrub_page *spage)
1784{
1785 struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices;
1786 int ret;
1787
Anand Jain44880fd2017-07-29 17:50:09 +08001788 ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Miao Xie17a9be22014-07-24 11:37:08 +08001789 return !ret;
1790}
1791
Zhao Leiba7cf982015-08-24 21:18:02 +08001792static void scrub_recheck_block_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001793{
Zhao Leiba7cf982015-08-24 21:18:02 +08001794 sblock->header_error = 0;
1795 sblock->checksum_error = 0;
1796 sblock->generation_error = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001797
Zhao Leiba7cf982015-08-24 21:18:02 +08001798 if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA)
1799 scrub_checksum_data(sblock);
1800 else
1801 scrub_checksum_tree_block(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001802}
1803
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001804static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
Zhao Lei114ab502015-01-20 15:11:36 +08001805 struct scrub_block *sblock_good)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001806{
1807 int page_num;
1808 int ret = 0;
1809
1810 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1811 int ret_sub;
1812
1813 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1814 sblock_good,
Zhao Lei114ab502015-01-20 15:11:36 +08001815 page_num, 1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001816 if (ret_sub)
1817 ret = ret_sub;
1818 }
1819
1820 return ret;
1821}
1822
1823static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1824 struct scrub_block *sblock_good,
1825 int page_num, int force_write)
1826{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001827 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1828 struct scrub_page *page_good = sblock_good->pagev[page_num];
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001829 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001830
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001831 BUG_ON(page_bad->page == NULL);
1832 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001833 if (force_write || sblock_bad->header_error ||
1834 sblock_bad->checksum_error || page_bad->io_error) {
1835 struct bio *bio;
1836 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001837
Stefan Behrensff023aa2012-11-06 11:43:11 +01001838 if (!page_bad->dev->bdev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001839 btrfs_warn_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001840 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001841 return -EIO;
1842 }
1843
David Sterbac5e4c3d2017-06-12 17:29:41 +02001844 bio = btrfs_io_bio_alloc(1);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001845 bio_set_dev(bio, page_bad->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001846 bio->bi_iter.bi_sector = page_bad->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001847 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001848
1849 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1850 if (PAGE_SIZE != ret) {
1851 bio_put(bio);
1852 return -EIO;
1853 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001854
Mike Christie4e49ea42016-06-05 14:31:41 -05001855 if (btrfsic_submit_bio_wait(bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001856 btrfs_dev_stat_inc_and_print(page_bad->dev,
1857 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001858 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001859 &fs_info->dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001860 bio_put(bio);
1861 return -EIO;
1862 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001863 bio_put(bio);
1864 }
1865
1866 return 0;
1867}
1868
Stefan Behrensff023aa2012-11-06 11:43:11 +01001869static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1870{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001871 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001872 int page_num;
1873
Miao Xie5a6ac9e2014-11-06 17:20:58 +08001874 /*
1875 * This block is used for the check of the parity on the source device,
1876 * so the data needn't be written into the destination device.
1877 */
1878 if (sblock->sparity)
1879 return;
1880
Stefan Behrensff023aa2012-11-06 11:43:11 +01001881 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1882 int ret;
1883
1884 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1885 if (ret)
1886 btrfs_dev_replace_stats_inc(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001887 &fs_info->dev_replace.num_write_errors);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001888 }
1889}
1890
1891static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1892 int page_num)
1893{
1894 struct scrub_page *spage = sblock->pagev[page_num];
1895
1896 BUG_ON(spage->page == NULL);
1897 if (spage->io_error) {
1898 void *mapped_buffer = kmap_atomic(spage->page);
1899
David Sterba619a9742017-03-29 20:48:44 +02001900 clear_page(mapped_buffer);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001901 flush_dcache_page(spage->page);
1902 kunmap_atomic(mapped_buffer);
1903 }
1904 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1905}
1906
1907static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1908 struct scrub_page *spage)
1909{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001910 struct scrub_bio *sbio;
1911 int ret;
1912
David Sterba3fb99302017-05-16 19:10:32 +02001913 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001914again:
David Sterba3fb99302017-05-16 19:10:32 +02001915 if (!sctx->wr_curr_bio) {
1916 sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio),
David Sterba58c4e172016-02-11 10:49:42 +01001917 GFP_KERNEL);
David Sterba3fb99302017-05-16 19:10:32 +02001918 if (!sctx->wr_curr_bio) {
1919 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001920 return -ENOMEM;
1921 }
David Sterba3fb99302017-05-16 19:10:32 +02001922 sctx->wr_curr_bio->sctx = sctx;
1923 sctx->wr_curr_bio->page_count = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001924 }
David Sterba3fb99302017-05-16 19:10:32 +02001925 sbio = sctx->wr_curr_bio;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001926 if (sbio->page_count == 0) {
1927 struct bio *bio;
1928
1929 sbio->physical = spage->physical_for_dev_replace;
1930 sbio->logical = spage->logical;
David Sterba3fb99302017-05-16 19:10:32 +02001931 sbio->dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001932 bio = sbio->bio;
1933 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02001934 bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001935 sbio->bio = bio;
1936 }
1937
1938 bio->bi_private = sbio;
1939 bio->bi_end_io = scrub_wr_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001940 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001941 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05001942 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001943 sbio->status = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001944 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1945 spage->physical_for_dev_replace ||
1946 sbio->logical + sbio->page_count * PAGE_SIZE !=
1947 spage->logical) {
1948 scrub_wr_submit(sctx);
1949 goto again;
1950 }
1951
1952 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1953 if (ret != PAGE_SIZE) {
1954 if (sbio->page_count < 1) {
1955 bio_put(sbio->bio);
1956 sbio->bio = NULL;
David Sterba3fb99302017-05-16 19:10:32 +02001957 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001958 return -EIO;
1959 }
1960 scrub_wr_submit(sctx);
1961 goto again;
1962 }
1963
1964 sbio->pagev[sbio->page_count] = spage;
1965 scrub_page_get(spage);
1966 sbio->page_count++;
David Sterba3fb99302017-05-16 19:10:32 +02001967 if (sbio->page_count == sctx->pages_per_wr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001968 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02001969 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001970
1971 return 0;
1972}
1973
1974static void scrub_wr_submit(struct scrub_ctx *sctx)
1975{
Stefan Behrensff023aa2012-11-06 11:43:11 +01001976 struct scrub_bio *sbio;
1977
David Sterba3fb99302017-05-16 19:10:32 +02001978 if (!sctx->wr_curr_bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001979 return;
1980
David Sterba3fb99302017-05-16 19:10:32 +02001981 sbio = sctx->wr_curr_bio;
1982 sctx->wr_curr_bio = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001983 WARN_ON(!sbio->bio->bi_disk);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001984 scrub_pending_bio_inc(sctx);
1985 /* process all writes in a single worker thread. Then the block layer
1986 * orders the requests before sending them to the driver which
1987 * doubled the write performance on spinning disks when measured
1988 * with Linux 3.5 */
Mike Christie4e49ea42016-06-05 14:31:41 -05001989 btrfsic_submit_bio(sbio->bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001990}
1991
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001992static void scrub_wr_bio_end_io(struct bio *bio)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001993{
1994 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001995 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001996
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001997 sbio->status = bio->bi_status;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001998 sbio->bio = bio;
1999
Liu Bo9e0af232014-08-15 23:36:53 +08002000 btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper,
2001 scrub_wr_bio_end_io_worker, NULL, NULL);
Qu Wenruo0339ef22014-02-28 10:46:17 +08002002 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002003}
2004
2005static void scrub_wr_bio_end_io_worker(struct btrfs_work *work)
2006{
2007 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
2008 struct scrub_ctx *sctx = sbio->sctx;
2009 int i;
2010
2011 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002012 if (sbio->status) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002013 struct btrfs_dev_replace *dev_replace =
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002014 &sbio->sctx->fs_info->dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002015
2016 for (i = 0; i < sbio->page_count; i++) {
2017 struct scrub_page *spage = sbio->pagev[i];
2018
2019 spage->io_error = 1;
2020 btrfs_dev_replace_stats_inc(&dev_replace->
2021 num_write_errors);
2022 }
2023 }
2024
2025 for (i = 0; i < sbio->page_count; i++)
2026 scrub_page_put(sbio->pagev[i]);
2027
2028 bio_put(sbio->bio);
2029 kfree(sbio);
2030 scrub_pending_bio_dec(sctx);
2031}
2032
2033static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002034{
2035 u64 flags;
2036 int ret;
2037
Zhao Leiba7cf982015-08-24 21:18:02 +08002038 /*
2039 * No need to initialize these stats currently,
2040 * because this function only use return value
2041 * instead of these stats value.
2042 *
2043 * Todo:
2044 * always use stats
2045 */
2046 sblock->header_error = 0;
2047 sblock->generation_error = 0;
2048 sblock->checksum_error = 0;
2049
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002050 WARN_ON(sblock->page_count < 1);
2051 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002052 ret = 0;
2053 if (flags & BTRFS_EXTENT_FLAG_DATA)
2054 ret = scrub_checksum_data(sblock);
2055 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
2056 ret = scrub_checksum_tree_block(sblock);
2057 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
2058 (void)scrub_checksum_super(sblock);
2059 else
2060 WARN_ON(1);
2061 if (ret)
2062 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002063
2064 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002065}
2066
2067static int scrub_checksum_data(struct scrub_block *sblock)
2068{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002069 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002070 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002071 u8 *on_disk_csum;
2072 struct page *page;
2073 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01002074 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002075 u64 len;
2076 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002077
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002078 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002079 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002080 return 0;
2081
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002082 on_disk_csum = sblock->pagev[0]->csum;
2083 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002084 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002085
David Sterba25cc1222017-05-16 19:10:41 +02002086 len = sctx->fs_info->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002087 index = 0;
2088 for (;;) {
2089 u64 l = min_t(u64, len, PAGE_SIZE);
2090
Liu Bob0496682013-03-14 14:57:45 +00002091 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002092 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002093 len -= l;
2094 if (len == 0)
2095 break;
2096 index++;
2097 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002098 BUG_ON(!sblock->pagev[index]->page);
2099 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002100 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002101 }
2102
Arne Jansena2de7332011-03-08 14:14:00 +01002103 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002104 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002105 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002106
Zhao Leiba7cf982015-08-24 21:18:02 +08002107 return sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002108}
2109
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002110static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002111{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002112 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002113 struct btrfs_header *h;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002114 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002115 u8 calculated_csum[BTRFS_CSUM_SIZE];
2116 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2117 struct page *page;
2118 void *mapped_buffer;
2119 u64 mapped_size;
2120 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002121 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002122 u64 len;
2123 int index;
2124
2125 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002126 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002127 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002128 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002129 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002130
2131 /*
2132 * we don't use the getter functions here, as we
2133 * a) don't have an extent buffer and
2134 * b) the page is already kmapped
2135 */
Qu Wenruo3cae2102013-07-16 11:19:18 +08002136 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Zhao Leiba7cf982015-08-24 21:18:02 +08002137 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002138
Zhao Leiba7cf982015-08-24 21:18:02 +08002139 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) {
2140 sblock->header_error = 1;
2141 sblock->generation_error = 1;
2142 }
Arne Jansena2de7332011-03-08 14:14:00 +01002143
Miao Xie17a9be22014-07-24 11:37:08 +08002144 if (!scrub_check_fsid(h->fsid, sblock->pagev[0]))
Zhao Leiba7cf982015-08-24 21:18:02 +08002145 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002146
2147 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
2148 BTRFS_UUID_SIZE))
Zhao Leiba7cf982015-08-24 21:18:02 +08002149 sblock->header_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002150
David Sterba25cc1222017-05-16 19:10:41 +02002151 len = sctx->fs_info->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002152 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2153 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2154 index = 0;
2155 for (;;) {
2156 u64 l = min_t(u64, len, mapped_size);
2157
Liu Bob0496682013-03-14 14:57:45 +00002158 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002159 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002160 len -= l;
2161 if (len == 0)
2162 break;
2163 index++;
2164 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002165 BUG_ON(!sblock->pagev[index]->page);
2166 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002167 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002168 mapped_size = PAGE_SIZE;
2169 p = mapped_buffer;
2170 }
2171
2172 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002173 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Zhao Leiba7cf982015-08-24 21:18:02 +08002174 sblock->checksum_error = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002175
Zhao Leiba7cf982015-08-24 21:18:02 +08002176 return sblock->header_error || sblock->checksum_error;
Arne Jansena2de7332011-03-08 14:14:00 +01002177}
2178
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002179static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01002180{
2181 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002182 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002183 u8 calculated_csum[BTRFS_CSUM_SIZE];
2184 u8 on_disk_csum[BTRFS_CSUM_SIZE];
2185 struct page *page;
2186 void *mapped_buffer;
2187 u64 mapped_size;
2188 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01002189 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002190 int fail_gen = 0;
2191 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002192 u64 len;
2193 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01002194
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002195 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002196 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002197 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002198 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002199 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01002200
Qu Wenruo3cae2102013-07-16 11:19:18 +08002201 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002202 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002203
Qu Wenruo3cae2102013-07-16 11:19:18 +08002204 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002205 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002206
Miao Xie17a9be22014-07-24 11:37:08 +08002207 if (!scrub_check_fsid(s->fsid, sblock->pagev[0]))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002208 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002209
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002210 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
2211 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
2212 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
2213 index = 0;
2214 for (;;) {
2215 u64 l = min_t(u64, len, mapped_size);
2216
Liu Bob0496682013-03-14 14:57:45 +00002217 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07002218 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002219 len -= l;
2220 if (len == 0)
2221 break;
2222 index++;
2223 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002224 BUG_ON(!sblock->pagev[index]->page);
2225 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07002226 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002227 mapped_size = PAGE_SIZE;
2228 p = mapped_buffer;
2229 }
2230
2231 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002232 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02002233 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01002234
Stefan Behrens442a4f62012-05-25 16:06:08 +02002235 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01002236 /*
2237 * if we find an error in a super block, we just report it.
2238 * They will get written with the next transaction commit
2239 * anyway
2240 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002241 spin_lock(&sctx->stat_lock);
2242 ++sctx->stat.super_errors;
2243 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002244 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002245 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002246 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2247 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002248 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02002249 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01002250 }
2251
Stefan Behrens442a4f62012-05-25 16:06:08 +02002252 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01002253}
2254
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002255static void scrub_block_get(struct scrub_block *sblock)
2256{
Elena Reshetova186debd2017-03-03 10:55:23 +02002257 refcount_inc(&sblock->refs);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002258}
2259
2260static void scrub_block_put(struct scrub_block *sblock)
2261{
Elena Reshetova186debd2017-03-03 10:55:23 +02002262 if (refcount_dec_and_test(&sblock->refs)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002263 int i;
2264
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002265 if (sblock->sparity)
2266 scrub_parity_put(sblock->sparity);
2267
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002268 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002269 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002270 kfree(sblock);
2271 }
2272}
2273
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002274static void scrub_page_get(struct scrub_page *spage)
2275{
Zhao Lei57019342015-01-20 15:11:45 +08002276 atomic_inc(&spage->refs);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002277}
2278
2279static void scrub_page_put(struct scrub_page *spage)
2280{
Zhao Lei57019342015-01-20 15:11:45 +08002281 if (atomic_dec_and_test(&spage->refs)) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002282 if (spage->page)
2283 __free_page(spage->page);
2284 kfree(spage);
2285 }
2286}
2287
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002288static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01002289{
2290 struct scrub_bio *sbio;
2291
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002292 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04002293 return;
Arne Jansena2de7332011-03-08 14:14:00 +01002294
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002295 sbio = sctx->bios[sctx->curr];
2296 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002297 scrub_pending_bio_inc(sctx);
Mike Christie4e49ea42016-06-05 14:31:41 -05002298 btrfsic_submit_bio(sbio->bio);
Arne Jansena2de7332011-03-08 14:14:00 +01002299}
2300
Stefan Behrensff023aa2012-11-06 11:43:11 +01002301static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
2302 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01002303{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002304 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01002305 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05002306 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002307
2308again:
2309 /*
2310 * grab a fresh bio or wait for one to become available
2311 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002312 while (sctx->curr == -1) {
2313 spin_lock(&sctx->list_lock);
2314 sctx->curr = sctx->first_free;
2315 if (sctx->curr != -1) {
2316 sctx->first_free = sctx->bios[sctx->curr]->next_free;
2317 sctx->bios[sctx->curr]->next_free = -1;
2318 sctx->bios[sctx->curr]->page_count = 0;
2319 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002320 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002321 spin_unlock(&sctx->list_lock);
2322 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01002323 }
2324 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002325 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002326 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05002327 struct bio *bio;
2328
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002329 sbio->physical = spage->physical;
2330 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002331 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002332 bio = sbio->bio;
2333 if (!bio) {
David Sterbac5e4c3d2017-06-12 17:29:41 +02002334 bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002335 sbio->bio = bio;
2336 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05002337
2338 bio->bi_private = sbio;
2339 bio->bi_end_io = scrub_bio_end_io;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002340 bio_set_dev(bio, sbio->dev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002341 bio->bi_iter.bi_sector = sbio->physical >> 9;
Mike Christie37226b22016-06-05 14:31:52 -05002342 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002343 sbio->status = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002344 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
2345 spage->physical ||
2346 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002347 spage->logical ||
2348 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002349 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05002350 goto again;
2351 }
2352
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002353 sbio->pagev[sbio->page_count] = spage;
2354 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
2355 if (ret != PAGE_SIZE) {
2356 if (sbio->page_count < 1) {
2357 bio_put(sbio->bio);
2358 sbio->bio = NULL;
2359 return -EIO;
2360 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002361 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002362 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01002363 }
Arne Jansen1bc87792011-05-28 21:57:55 +02002364
Stefan Behrensff023aa2012-11-06 11:43:11 +01002365 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002366 atomic_inc(&sblock->outstanding_pages);
2367 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002368 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002369 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002370
2371 return 0;
2372}
2373
Linus Torvalds22365972015-09-05 15:14:43 -07002374static void scrub_missing_raid56_end_io(struct bio *bio)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002375{
2376 struct scrub_block *sblock = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002377 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002378
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002379 if (bio->bi_status)
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002380 sblock->no_io_error_seen = 0;
2381
Scott Talbert46732722016-05-09 09:14:28 -04002382 bio_put(bio);
2383
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002384 btrfs_queue_work(fs_info->scrub_workers, &sblock->work);
2385}
2386
2387static void scrub_missing_raid56_worker(struct btrfs_work *work)
2388{
2389 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2390 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002391 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002392 u64 logical;
2393 struct btrfs_device *dev;
2394
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002395 logical = sblock->pagev[0]->logical;
2396 dev = sblock->pagev[0]->dev;
2397
Zhao Leiaffe4a52015-08-24 21:32:06 +08002398 if (sblock->no_io_error_seen)
Zhao Leiba7cf982015-08-24 21:18:02 +08002399 scrub_recheck_block_checksum(sblock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002400
2401 if (!sblock->no_io_error_seen) {
2402 spin_lock(&sctx->stat_lock);
2403 sctx->stat.read_errors++;
2404 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002405 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002406 "IO error rebuilding logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002407 logical, rcu_str_deref(dev->name));
2408 } else if (sblock->header_error || sblock->checksum_error) {
2409 spin_lock(&sctx->stat_lock);
2410 sctx->stat.uncorrectable_errors++;
2411 spin_unlock(&sctx->stat_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002412 btrfs_err_rl_in_rcu(fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02002413 "failed to rebuild valid logical %llu for dev %s",
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002414 logical, rcu_str_deref(dev->name));
2415 } else {
2416 scrub_write_block_to_dev_replace(sblock);
2417 }
2418
2419 scrub_block_put(sblock);
2420
David Sterba2073c4c2017-03-31 17:12:51 +02002421 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002422 mutex_lock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002423 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002424 mutex_unlock(&sctx->wr_lock);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002425 }
2426
2427 scrub_pending_bio_dec(sctx);
2428}
2429
2430static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2431{
2432 struct scrub_ctx *sctx = sblock->sctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002433 struct btrfs_fs_info *fs_info = sctx->fs_info;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002434 u64 length = sblock->page_count * PAGE_SIZE;
2435 u64 logical = sblock->pagev[0]->logical;
Zhao Leif1fee652016-05-17 17:37:38 +08002436 struct btrfs_bio *bbio = NULL;
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002437 struct bio *bio;
2438 struct btrfs_raid_bio *rbio;
2439 int ret;
2440 int i;
2441
Qu Wenruoae6529c2017-03-29 09:33:21 +08002442 btrfs_bio_counter_inc_blocked(fs_info);
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02002443 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
David Sterba825ad4c2017-03-28 14:45:22 +02002444 &length, &bbio);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002445 if (ret || !bbio || !bbio->raid_map)
2446 goto bbio_out;
2447
2448 if (WARN_ON(!sctx->is_dev_replace ||
2449 !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) {
2450 /*
2451 * We shouldn't be scrubbing a missing device. Even for dev
2452 * replace, we should only get here for RAID 5/6. We either
2453 * managed to mount something with no mirrors remaining or
2454 * there's a bug in scrub_remap_extent()/btrfs_map_block().
2455 */
2456 goto bbio_out;
2457 }
2458
David Sterbac5e4c3d2017-06-12 17:29:41 +02002459 bio = btrfs_io_bio_alloc(0);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002460 bio->bi_iter.bi_sector = logical >> 9;
2461 bio->bi_private = sblock;
2462 bio->bi_end_io = scrub_missing_raid56_end_io;
2463
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002464 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002465 if (!rbio)
2466 goto rbio_out;
2467
2468 for (i = 0; i < sblock->page_count; i++) {
2469 struct scrub_page *spage = sblock->pagev[i];
2470
2471 raid56_add_scrub_pages(rbio, spage->page, spage->logical);
2472 }
2473
2474 btrfs_init_work(&sblock->work, btrfs_scrub_helper,
2475 scrub_missing_raid56_worker, NULL, NULL);
2476 scrub_block_get(sblock);
2477 scrub_pending_bio_inc(sctx);
2478 raid56_submit_missing_rbio(rbio);
2479 return;
2480
2481rbio_out:
2482 bio_put(bio);
2483bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08002484 btrfs_bio_counter_dec(fs_info);
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002485 btrfs_put_bbio(bbio);
2486 spin_lock(&sctx->stat_lock);
2487 sctx->stat.malloc_errors++;
2488 spin_unlock(&sctx->stat_lock);
2489}
2490
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002491static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002492 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002493 u64 gen, int mirror_num, u8 *csum, int force,
2494 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002495{
2496 struct scrub_block *sblock;
2497 int index;
2498
David Sterba58c4e172016-02-11 10:49:42 +01002499 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002500 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002501 spin_lock(&sctx->stat_lock);
2502 sctx->stat.malloc_errors++;
2503 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002504 return -ENOMEM;
2505 }
2506
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002507 /* one ref inside this function, plus one for each page added to
2508 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002509 refcount_set(&sblock->refs, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002510 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002511 sblock->no_io_error_seen = 1;
2512
2513 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002514 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002515 u64 l = min_t(u64, len, PAGE_SIZE);
2516
David Sterba58c4e172016-02-11 10:49:42 +01002517 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002518 if (!spage) {
2519leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002520 spin_lock(&sctx->stat_lock);
2521 sctx->stat.malloc_errors++;
2522 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002523 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002524 return -ENOMEM;
2525 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002526 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2527 scrub_page_get(spage);
2528 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002529 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002530 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002531 spage->flags = flags;
2532 spage->generation = gen;
2533 spage->logical = logical;
2534 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002535 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002536 spage->mirror_num = mirror_num;
2537 if (csum) {
2538 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002539 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002540 } else {
2541 spage->have_csum = 0;
2542 }
2543 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002544 spage->page = alloc_page(GFP_KERNEL);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002545 if (!spage->page)
2546 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002547 len -= l;
2548 logical += l;
2549 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002550 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002551 }
2552
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002553 WARN_ON(sblock->page_count == 0);
Anand Jaine6e674b2017-12-04 12:54:54 +08002554 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002555 /*
2556 * This case should only be hit for RAID 5/6 device replace. See
2557 * the comment in scrub_missing_raid56_pages() for details.
2558 */
2559 scrub_missing_raid56_pages(sblock);
2560 } else {
2561 for (index = 0; index < sblock->page_count; index++) {
2562 struct scrub_page *spage = sblock->pagev[index];
2563 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002564
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002565 ret = scrub_add_page_to_rd_bio(sctx, spage);
2566 if (ret) {
2567 scrub_block_put(sblock);
2568 return ret;
2569 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002570 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002571
Omar Sandoval73ff61d2015-06-19 11:52:51 -07002572 if (force)
2573 scrub_submit(sctx);
2574 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002575
2576 /* last one frees, either here or in bio completion for last page */
2577 scrub_block_put(sblock);
2578 return 0;
2579}
2580
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002581static void scrub_bio_end_io(struct bio *bio)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002582{
2583 struct scrub_bio *sbio = bio->bi_private;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002584 struct btrfs_fs_info *fs_info = sbio->dev->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002585
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002586 sbio->status = bio->bi_status;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002587 sbio->bio = bio;
2588
Qu Wenruo0339ef22014-02-28 10:46:17 +08002589 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002590}
2591
2592static void scrub_bio_end_io_worker(struct btrfs_work *work)
2593{
2594 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002595 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002596 int i;
2597
Stefan Behrensff023aa2012-11-06 11:43:11 +01002598 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002599 if (sbio->status) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002600 for (i = 0; i < sbio->page_count; i++) {
2601 struct scrub_page *spage = sbio->pagev[i];
2602
2603 spage->io_error = 1;
2604 spage->sblock->no_io_error_seen = 0;
2605 }
2606 }
2607
2608 /* now complete the scrub_block items that have all pages completed */
2609 for (i = 0; i < sbio->page_count; i++) {
2610 struct scrub_page *spage = sbio->pagev[i];
2611 struct scrub_block *sblock = spage->sblock;
2612
2613 if (atomic_dec_and_test(&sblock->outstanding_pages))
2614 scrub_block_complete(sblock);
2615 scrub_block_put(sblock);
2616 }
2617
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002618 bio_put(sbio->bio);
2619 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002620 spin_lock(&sctx->list_lock);
2621 sbio->next_free = sctx->first_free;
2622 sctx->first_free = sbio->index;
2623 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002624
David Sterba2073c4c2017-03-31 17:12:51 +02002625 if (sctx->is_dev_replace && sctx->flush_all_writes) {
David Sterba3fb99302017-05-16 19:10:32 +02002626 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002627 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02002628 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002629 }
2630
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002631 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002632}
2633
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002634static inline void __scrub_mark_bitmap(struct scrub_parity *sparity,
2635 unsigned long *bitmap,
2636 u64 start, u64 len)
2637{
Liu Bo972d7212017-04-03 13:45:33 -07002638 u64 offset;
David Sterba7736b0a2017-03-31 18:02:48 +02002639 u64 nsectors64;
2640 u32 nsectors;
Jeff Mahoneyda170662016-06-15 09:22:56 -04002641 int sectorsize = sparity->sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002642
2643 if (len >= sparity->stripe_len) {
2644 bitmap_set(bitmap, 0, sparity->nsectors);
2645 return;
2646 }
2647
2648 start -= sparity->logic_start;
Liu Bo972d7212017-04-03 13:45:33 -07002649 start = div64_u64_rem(start, sparity->stripe_len, &offset);
2650 offset = div_u64(offset, sectorsize);
David Sterba7736b0a2017-03-31 18:02:48 +02002651 nsectors64 = div_u64(len, sectorsize);
2652
2653 ASSERT(nsectors64 < UINT_MAX);
2654 nsectors = (u32)nsectors64;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002655
2656 if (offset + nsectors <= sparity->nsectors) {
2657 bitmap_set(bitmap, offset, nsectors);
2658 return;
2659 }
2660
2661 bitmap_set(bitmap, offset, sparity->nsectors - offset);
2662 bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset));
2663}
2664
2665static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity,
2666 u64 start, u64 len)
2667{
2668 __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len);
2669}
2670
2671static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity,
2672 u64 start, u64 len)
2673{
2674 __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len);
2675}
2676
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002677static void scrub_block_complete(struct scrub_block *sblock)
2678{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002679 int corrupted = 0;
2680
Stefan Behrensff023aa2012-11-06 11:43:11 +01002681 if (!sblock->no_io_error_seen) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002682 corrupted = 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002683 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002684 } else {
2685 /*
2686 * if has checksum error, write via repair mechanism in
2687 * dev replace case, otherwise write here in dev replace
2688 * case.
2689 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002690 corrupted = scrub_checksum(sblock);
2691 if (!corrupted && sblock->sctx->is_dev_replace)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002692 scrub_write_block_to_dev_replace(sblock);
2693 }
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002694
2695 if (sblock->sparity && corrupted && !sblock->data_corrected) {
2696 u64 start = sblock->pagev[0]->logical;
2697 u64 end = sblock->pagev[sblock->page_count - 1]->logical +
2698 PAGE_SIZE;
2699
2700 scrub_parity_mark_sectors_error(sblock->sparity,
2701 start, end - start);
2702 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002703}
2704
Zhao Lei3b5753e2015-08-24 22:03:02 +08002705static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum)
Arne Jansena2de7332011-03-08 14:14:00 +01002706{
2707 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002708 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002709 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002710
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002711 while (!list_empty(&sctx->csum_list)) {
2712 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002713 struct btrfs_ordered_sum, list);
2714 if (sum->bytenr > logical)
2715 return 0;
2716 if (sum->bytenr + sum->len > logical)
2717 break;
2718
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002719 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002720 list_del(&sum->list);
2721 kfree(sum);
2722 sum = NULL;
2723 }
2724 if (!sum)
2725 return 0;
2726
David Sterba1d1bf922017-03-31 18:02:48 +02002727 index = div_u64(logical - sum->bytenr, sctx->fs_info->sectorsize);
2728 ASSERT(index < UINT_MAX);
2729
David Sterba25cc1222017-05-16 19:10:41 +02002730 num_sectors = sum->len / sctx->fs_info->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002731 memcpy(csum, sum->sums + index, sctx->csum_size);
2732 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002733 list_del(&sum->list);
2734 kfree(sum);
2735 }
Miao Xief51a4a12013-06-19 10:36:09 +08002736 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002737}
2738
2739/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002740static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002741 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002742 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002743{
2744 int ret;
2745 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002746 u32 blocksize;
2747
2748 if (flags & BTRFS_EXTENT_FLAG_DATA) {
David Sterba25cc1222017-05-16 19:10:41 +02002749 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002750 spin_lock(&sctx->stat_lock);
2751 sctx->stat.data_extents_scrubbed++;
2752 sctx->stat.data_bytes_scrubbed += len;
2753 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002754 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
David Sterba25cc1222017-05-16 19:10:41 +02002755 blocksize = sctx->fs_info->nodesize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002756 spin_lock(&sctx->stat_lock);
2757 sctx->stat.tree_extents_scrubbed++;
2758 sctx->stat.tree_bytes_scrubbed += len;
2759 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002760 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002761 blocksize = sctx->fs_info->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002762 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002763 }
Arne Jansena2de7332011-03-08 14:14:00 +01002764
2765 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002766 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002767 int have_csum = 0;
2768
2769 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2770 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002771 have_csum = scrub_find_csum(sctx, logical, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002772 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002773 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002774 if (sctx->is_dev_replace && !have_csum) {
2775 ret = copy_nocow_pages(sctx, logical, l,
2776 mirror_num,
2777 physical_for_dev_replace);
2778 goto behind_scrub_pages;
2779 }
Arne Jansena2de7332011-03-08 14:14:00 +01002780 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002781 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002782 mirror_num, have_csum ? csum : NULL, 0,
2783 physical_for_dev_replace);
2784behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002785 if (ret)
2786 return ret;
2787 len -= l;
2788 logical += l;
2789 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002790 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002791 }
2792 return 0;
2793}
2794
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002795static int scrub_pages_for_parity(struct scrub_parity *sparity,
2796 u64 logical, u64 len,
2797 u64 physical, struct btrfs_device *dev,
2798 u64 flags, u64 gen, int mirror_num, u8 *csum)
2799{
2800 struct scrub_ctx *sctx = sparity->sctx;
2801 struct scrub_block *sblock;
2802 int index;
2803
David Sterba58c4e172016-02-11 10:49:42 +01002804 sblock = kzalloc(sizeof(*sblock), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002805 if (!sblock) {
2806 spin_lock(&sctx->stat_lock);
2807 sctx->stat.malloc_errors++;
2808 spin_unlock(&sctx->stat_lock);
2809 return -ENOMEM;
2810 }
2811
2812 /* one ref inside this function, plus one for each page added to
2813 * a bio later on */
Elena Reshetova186debd2017-03-03 10:55:23 +02002814 refcount_set(&sblock->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002815 sblock->sctx = sctx;
2816 sblock->no_io_error_seen = 1;
2817 sblock->sparity = sparity;
2818 scrub_parity_get(sparity);
2819
2820 for (index = 0; len > 0; index++) {
2821 struct scrub_page *spage;
2822 u64 l = min_t(u64, len, PAGE_SIZE);
2823
David Sterba58c4e172016-02-11 10:49:42 +01002824 spage = kzalloc(sizeof(*spage), GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002825 if (!spage) {
2826leave_nomem:
2827 spin_lock(&sctx->stat_lock);
2828 sctx->stat.malloc_errors++;
2829 spin_unlock(&sctx->stat_lock);
2830 scrub_block_put(sblock);
2831 return -ENOMEM;
2832 }
2833 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2834 /* For scrub block */
2835 scrub_page_get(spage);
2836 sblock->pagev[index] = spage;
2837 /* For scrub parity */
2838 scrub_page_get(spage);
2839 list_add_tail(&spage->list, &sparity->spages);
2840 spage->sblock = sblock;
2841 spage->dev = dev;
2842 spage->flags = flags;
2843 spage->generation = gen;
2844 spage->logical = logical;
2845 spage->physical = physical;
2846 spage->mirror_num = mirror_num;
2847 if (csum) {
2848 spage->have_csum = 1;
2849 memcpy(spage->csum, csum, sctx->csum_size);
2850 } else {
2851 spage->have_csum = 0;
2852 }
2853 sblock->page_count++;
David Sterba58c4e172016-02-11 10:49:42 +01002854 spage->page = alloc_page(GFP_KERNEL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002855 if (!spage->page)
2856 goto leave_nomem;
2857 len -= l;
2858 logical += l;
2859 physical += l;
2860 }
2861
2862 WARN_ON(sblock->page_count == 0);
2863 for (index = 0; index < sblock->page_count; index++) {
2864 struct scrub_page *spage = sblock->pagev[index];
2865 int ret;
2866
2867 ret = scrub_add_page_to_rd_bio(sctx, spage);
2868 if (ret) {
2869 scrub_block_put(sblock);
2870 return ret;
2871 }
2872 }
2873
2874 /* last one frees, either here or in bio completion for last page */
2875 scrub_block_put(sblock);
2876 return 0;
2877}
2878
2879static int scrub_extent_for_parity(struct scrub_parity *sparity,
2880 u64 logical, u64 len,
2881 u64 physical, struct btrfs_device *dev,
2882 u64 flags, u64 gen, int mirror_num)
2883{
2884 struct scrub_ctx *sctx = sparity->sctx;
2885 int ret;
2886 u8 csum[BTRFS_CSUM_SIZE];
2887 u32 blocksize;
2888
Anand Jaine6e674b2017-12-04 12:54:54 +08002889 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) {
Omar Sandoval4a770892015-06-19 11:52:52 -07002890 scrub_parity_mark_sectors_error(sparity, logical, len);
2891 return 0;
2892 }
2893
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002894 if (flags & BTRFS_EXTENT_FLAG_DATA) {
David Sterba25cc1222017-05-16 19:10:41 +02002895 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002896 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
David Sterba25cc1222017-05-16 19:10:41 +02002897 blocksize = sctx->fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002898 } else {
David Sterba25cc1222017-05-16 19:10:41 +02002899 blocksize = sctx->fs_info->sectorsize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002900 WARN_ON(1);
2901 }
2902
2903 while (len) {
2904 u64 l = min_t(u64, len, blocksize);
2905 int have_csum = 0;
2906
2907 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2908 /* push csums to sbio */
Zhao Lei3b5753e2015-08-24 22:03:02 +08002909 have_csum = scrub_find_csum(sctx, logical, csum);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002910 if (have_csum == 0)
2911 goto skip;
2912 }
2913 ret = scrub_pages_for_parity(sparity, logical, l, physical, dev,
2914 flags, gen, mirror_num,
2915 have_csum ? csum : NULL);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002916 if (ret)
2917 return ret;
Dan Carpenter6b6d24b2014-12-12 22:30:00 +03002918skip:
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002919 len -= l;
2920 logical += l;
2921 physical += l;
2922 }
2923 return 0;
2924}
2925
Wang Shilong3b080b22014-04-01 18:01:43 +08002926/*
2927 * Given a physical address, this will calculate it's
2928 * logical offset. if this is a parity stripe, it will return
2929 * the most left data stripe's logical offset.
2930 *
2931 * return 0 if it is a data stripe, 1 means parity stripe.
2932 */
2933static int get_raid56_logic_offset(u64 physical, int num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002934 struct map_lookup *map, u64 *offset,
2935 u64 *stripe_start)
Wang Shilong3b080b22014-04-01 18:01:43 +08002936{
2937 int i;
2938 int j = 0;
2939 u64 stripe_nr;
2940 u64 last_offset;
David Sterba9d644a62015-02-20 18:42:11 +01002941 u32 stripe_index;
2942 u32 rot;
Wang Shilong3b080b22014-04-01 18:01:43 +08002943
2944 last_offset = (physical - map->stripes[num].physical) *
2945 nr_data_stripes(map);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002946 if (stripe_start)
2947 *stripe_start = last_offset;
2948
Wang Shilong3b080b22014-04-01 18:01:43 +08002949 *offset = last_offset;
2950 for (i = 0; i < nr_data_stripes(map); i++) {
2951 *offset = last_offset + i * map->stripe_len;
2952
Liu Bo42c61ab2017-04-03 13:45:24 -07002953 stripe_nr = div64_u64(*offset, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01002954 stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
Wang Shilong3b080b22014-04-01 18:01:43 +08002955
2956 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01002957 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
Wang Shilong3b080b22014-04-01 18:01:43 +08002958 /* calculate which stripe this data locates */
2959 rot += i;
Wang Shilonge4fbaee2014-04-11 18:32:25 +08002960 stripe_index = rot % map->num_stripes;
Wang Shilong3b080b22014-04-01 18:01:43 +08002961 if (stripe_index == num)
2962 return 0;
2963 if (stripe_index < num)
2964 j++;
2965 }
2966 *offset = last_offset + j * map->stripe_len;
2967 return 1;
2968}
2969
Miao Xie5a6ac9e2014-11-06 17:20:58 +08002970static void scrub_free_parity(struct scrub_parity *sparity)
2971{
2972 struct scrub_ctx *sctx = sparity->sctx;
2973 struct scrub_page *curr, *next;
2974 int nbits;
2975
2976 nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors);
2977 if (nbits) {
2978 spin_lock(&sctx->stat_lock);
2979 sctx->stat.read_errors += nbits;
2980 sctx->stat.uncorrectable_errors += nbits;
2981 spin_unlock(&sctx->stat_lock);
2982 }
2983
2984 list_for_each_entry_safe(curr, next, &sparity->spages, list) {
2985 list_del_init(&curr->list);
2986 scrub_page_put(curr);
2987 }
2988
2989 kfree(sparity);
2990}
2991
Zhao Lei20b2e302015-06-04 20:09:15 +08002992static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2993{
2994 struct scrub_parity *sparity = container_of(work, struct scrub_parity,
2995 work);
2996 struct scrub_ctx *sctx = sparity->sctx;
2997
2998 scrub_free_parity(sparity);
2999 scrub_pending_bio_dec(sctx);
3000}
3001
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003002static void scrub_parity_bio_endio(struct bio *bio)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003003{
3004 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003005 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003006
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003007 if (bio->bi_status)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003008 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
3009 sparity->nsectors);
3010
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003011 bio_put(bio);
Zhao Lei20b2e302015-06-04 20:09:15 +08003012
3013 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
3014 scrub_parity_bio_endio_worker, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003015 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003016}
3017
3018static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
3019{
3020 struct scrub_ctx *sctx = sparity->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003021 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003022 struct bio *bio;
3023 struct btrfs_raid_bio *rbio;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003024 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003025 u64 length;
3026 int ret;
3027
3028 if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap,
3029 sparity->nsectors))
3030 goto out;
3031
Zhao Leia0dd59d2015-07-21 15:42:26 +08003032 length = sparity->logic_end - sparity->logic_start;
Qu Wenruoae6529c2017-03-29 09:33:21 +08003033
3034 btrfs_bio_counter_inc_blocked(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003035 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
David Sterba825ad4c2017-03-28 14:45:22 +02003036 &length, &bbio);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003037 if (ret || !bbio || !bbio->raid_map)
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003038 goto bbio_out;
3039
David Sterbac5e4c3d2017-06-12 17:29:41 +02003040 bio = btrfs_io_bio_alloc(0);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003041 bio->bi_iter.bi_sector = sparity->logic_start >> 9;
3042 bio->bi_private = sparity;
3043 bio->bi_end_io = scrub_parity_bio_endio;
3044
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003045 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08003046 length, sparity->scrub_dev,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003047 sparity->dbitmap,
3048 sparity->nsectors);
3049 if (!rbio)
3050 goto rbio_out;
3051
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003052 scrub_pending_bio_inc(sctx);
3053 raid56_parity_submit_scrub_rbio(rbio);
3054 return;
3055
3056rbio_out:
3057 bio_put(bio);
3058bbio_out:
Qu Wenruoae6529c2017-03-29 09:33:21 +08003059 btrfs_bio_counter_dec(fs_info);
Zhao Lei6e9606d2015-01-20 15:11:34 +08003060 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003061 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
3062 sparity->nsectors);
3063 spin_lock(&sctx->stat_lock);
3064 sctx->stat.malloc_errors++;
3065 spin_unlock(&sctx->stat_lock);
3066out:
3067 scrub_free_parity(sparity);
3068}
3069
3070static inline int scrub_calc_parity_bitmap_len(int nsectors)
3071{
Zhao Leibfca9a62014-12-08 19:55:57 +08003072 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003073}
3074
3075static void scrub_parity_get(struct scrub_parity *sparity)
3076{
Elena Reshetova78a76452017-03-03 10:55:24 +02003077 refcount_inc(&sparity->refs);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003078}
3079
3080static void scrub_parity_put(struct scrub_parity *sparity)
3081{
Elena Reshetova78a76452017-03-03 10:55:24 +02003082 if (!refcount_dec_and_test(&sparity->refs))
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003083 return;
3084
3085 scrub_parity_check_and_repair(sparity);
3086}
3087
3088static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
3089 struct map_lookup *map,
3090 struct btrfs_device *sdev,
3091 struct btrfs_path *path,
3092 u64 logic_start,
3093 u64 logic_end)
3094{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003095 struct btrfs_fs_info *fs_info = sctx->fs_info;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003096 struct btrfs_root *root = fs_info->extent_root;
3097 struct btrfs_root *csum_root = fs_info->csum_root;
3098 struct btrfs_extent_item *extent;
Omar Sandoval4a770892015-06-19 11:52:52 -07003099 struct btrfs_bio *bbio = NULL;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003100 u64 flags;
3101 int ret;
3102 int slot;
3103 struct extent_buffer *l;
3104 struct btrfs_key key;
3105 u64 generation;
3106 u64 extent_logical;
3107 u64 extent_physical;
3108 u64 extent_len;
Omar Sandoval4a770892015-06-19 11:52:52 -07003109 u64 mapped_length;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003110 struct btrfs_device *extent_dev;
3111 struct scrub_parity *sparity;
3112 int nsectors;
3113 int bitmap_len;
3114 int extent_mirror_num;
3115 int stop_loop = 0;
3116
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003117 nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003118 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
3119 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
3120 GFP_NOFS);
3121 if (!sparity) {
3122 spin_lock(&sctx->stat_lock);
3123 sctx->stat.malloc_errors++;
3124 spin_unlock(&sctx->stat_lock);
3125 return -ENOMEM;
3126 }
3127
3128 sparity->stripe_len = map->stripe_len;
3129 sparity->nsectors = nsectors;
3130 sparity->sctx = sctx;
3131 sparity->scrub_dev = sdev;
3132 sparity->logic_start = logic_start;
3133 sparity->logic_end = logic_end;
Elena Reshetova78a76452017-03-03 10:55:24 +02003134 refcount_set(&sparity->refs, 1);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003135 INIT_LIST_HEAD(&sparity->spages);
3136 sparity->dbitmap = sparity->bitmap;
3137 sparity->ebitmap = (void *)sparity->bitmap + bitmap_len;
3138
3139 ret = 0;
3140 while (logic_start < logic_end) {
3141 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3142 key.type = BTRFS_METADATA_ITEM_KEY;
3143 else
3144 key.type = BTRFS_EXTENT_ITEM_KEY;
3145 key.objectid = logic_start;
3146 key.offset = (u64)-1;
3147
3148 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3149 if (ret < 0)
3150 goto out;
3151
3152 if (ret > 0) {
3153 ret = btrfs_previous_extent_item(root, path, 0);
3154 if (ret < 0)
3155 goto out;
3156 if (ret > 0) {
3157 btrfs_release_path(path);
3158 ret = btrfs_search_slot(NULL, root, &key,
3159 path, 0, 0);
3160 if (ret < 0)
3161 goto out;
3162 }
3163 }
3164
3165 stop_loop = 0;
3166 while (1) {
3167 u64 bytes;
3168
3169 l = path->nodes[0];
3170 slot = path->slots[0];
3171 if (slot >= btrfs_header_nritems(l)) {
3172 ret = btrfs_next_leaf(root, path);
3173 if (ret == 0)
3174 continue;
3175 if (ret < 0)
3176 goto out;
3177
3178 stop_loop = 1;
3179 break;
3180 }
3181 btrfs_item_key_to_cpu(l, &key, slot);
3182
Zhao Leid7cad232015-07-22 13:14:48 +08003183 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3184 key.type != BTRFS_METADATA_ITEM_KEY)
3185 goto next;
3186
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003187 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003188 bytes = fs_info->nodesize;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003189 else
3190 bytes = key.offset;
3191
3192 if (key.objectid + bytes <= logic_start)
3193 goto next;
3194
Zhao Leia0dd59d2015-07-21 15:42:26 +08003195 if (key.objectid >= logic_end) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003196 stop_loop = 1;
3197 break;
3198 }
3199
3200 while (key.objectid >= logic_start + map->stripe_len)
3201 logic_start += map->stripe_len;
3202
3203 extent = btrfs_item_ptr(l, slot,
3204 struct btrfs_extent_item);
3205 flags = btrfs_extent_flags(l, extent);
3206 generation = btrfs_extent_generation(l, extent);
3207
Zhao Leia323e812015-07-23 12:29:49 +08003208 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3209 (key.objectid < logic_start ||
3210 key.objectid + bytes >
3211 logic_start + map->stripe_len)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003212 btrfs_err(fs_info,
3213 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Zhao Leia323e812015-07-23 12:29:49 +08003214 key.objectid, logic_start);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003215 spin_lock(&sctx->stat_lock);
3216 sctx->stat.uncorrectable_errors++;
3217 spin_unlock(&sctx->stat_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003218 goto next;
3219 }
3220again:
3221 extent_logical = key.objectid;
3222 extent_len = bytes;
3223
3224 if (extent_logical < logic_start) {
3225 extent_len -= logic_start - extent_logical;
3226 extent_logical = logic_start;
3227 }
3228
3229 if (extent_logical + extent_len >
3230 logic_start + map->stripe_len)
3231 extent_len = logic_start + map->stripe_len -
3232 extent_logical;
3233
3234 scrub_parity_mark_sectors_data(sparity, extent_logical,
3235 extent_len);
3236
Omar Sandoval4a770892015-06-19 11:52:52 -07003237 mapped_length = extent_len;
Zhao Leif1fee652016-05-17 17:37:38 +08003238 bbio = NULL;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02003239 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
3240 extent_logical, &mapped_length, &bbio,
3241 0);
Omar Sandoval4a770892015-06-19 11:52:52 -07003242 if (!ret) {
3243 if (!bbio || mapped_length < extent_len)
3244 ret = -EIO;
3245 }
3246 if (ret) {
3247 btrfs_put_bbio(bbio);
3248 goto out;
3249 }
3250 extent_physical = bbio->stripes[0].physical;
3251 extent_mirror_num = bbio->mirror_num;
3252 extent_dev = bbio->stripes[0].dev;
3253 btrfs_put_bbio(bbio);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003254
3255 ret = btrfs_lookup_csums_range(csum_root,
3256 extent_logical,
3257 extent_logical + extent_len - 1,
3258 &sctx->csum_list, 1);
3259 if (ret)
3260 goto out;
3261
3262 ret = scrub_extent_for_parity(sparity, extent_logical,
3263 extent_len,
3264 extent_physical,
3265 extent_dev, flags,
3266 generation,
3267 extent_mirror_num);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003268
3269 scrub_free_csums(sctx);
3270
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003271 if (ret)
3272 goto out;
3273
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003274 if (extent_logical + extent_len <
3275 key.objectid + bytes) {
3276 logic_start += map->stripe_len;
3277
3278 if (logic_start >= logic_end) {
3279 stop_loop = 1;
3280 break;
3281 }
3282
3283 if (logic_start < key.objectid + bytes) {
3284 cond_resched();
3285 goto again;
3286 }
3287 }
3288next:
3289 path->slots[0]++;
3290 }
3291
3292 btrfs_release_path(path);
3293
3294 if (stop_loop)
3295 break;
3296
3297 logic_start += map->stripe_len;
3298 }
3299out:
3300 if (ret < 0)
3301 scrub_parity_mark_sectors_error(sparity, logic_start,
Zhao Leia0dd59d2015-07-21 15:42:26 +08003302 logic_end - logic_start);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003303 scrub_parity_put(sparity);
3304 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003305 mutex_lock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003306 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003307 mutex_unlock(&sctx->wr_lock);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003308
3309 btrfs_release_path(path);
3310 return ret < 0 ? ret : 0;
3311}
3312
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003313static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003314 struct map_lookup *map,
3315 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003316 int num, u64 base, u64 length,
3317 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003318{
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003319 struct btrfs_path *path, *ppath;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003320 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003321 struct btrfs_root *root = fs_info->extent_root;
3322 struct btrfs_root *csum_root = fs_info->csum_root;
3323 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00003324 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01003325 u64 flags;
3326 int ret;
3327 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01003328 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01003329 struct extent_buffer *l;
Arne Jansena2de7332011-03-08 14:14:00 +01003330 u64 physical;
3331 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003332 u64 logic_end;
Wang Shilong3b080b22014-04-01 18:01:43 +08003333 u64 physical_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003334 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02003335 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02003336 struct reada_control *reada1;
3337 struct reada_control *reada2;
David Sterbae6c11f92016-03-24 18:00:53 +01003338 struct btrfs_key key;
Arne Jansen7a262852011-06-10 12:39:23 +02003339 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01003340 u64 increment = map->stripe_len;
3341 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003342 u64 extent_logical;
3343 u64 extent_physical;
3344 u64 extent_len;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003345 u64 stripe_logical;
3346 u64 stripe_end;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003347 struct btrfs_device *extent_dev;
3348 int extent_mirror_num;
Wang Shilong3b080b22014-04-01 18:01:43 +08003349 int stop_loop = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05003350
Wang Shilong3b080b22014-04-01 18:01:43 +08003351 physical = map->stripes[num].physical;
Arne Jansena2de7332011-03-08 14:14:00 +01003352 offset = 0;
Liu Bo42c61ab2017-04-03 13:45:24 -07003353 nstripes = div64_u64(length, map->stripe_len);
Arne Jansena2de7332011-03-08 14:14:00 +01003354 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3355 offset = map->stripe_len * num;
3356 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02003357 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003358 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3359 int factor = map->num_stripes / map->sub_stripes;
3360 offset = map->stripe_len * (num / map->sub_stripes);
3361 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02003362 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003363 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3364 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003365 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003366 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3367 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003368 mirror_num = num % map->num_stripes + 1;
Zhao Leiffe2d202015-01-20 15:11:44 +08003369 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003370 get_raid56_logic_offset(physical, num, map, &offset, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003371 increment = map->stripe_len * nr_data_stripes(map);
3372 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003373 } else {
3374 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02003375 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003376 }
3377
3378 path = btrfs_alloc_path();
3379 if (!path)
3380 return -ENOMEM;
3381
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003382 ppath = btrfs_alloc_path();
3383 if (!ppath) {
Tsutomu Itoh379d6852015-01-09 17:37:52 +09003384 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003385 return -ENOMEM;
3386 }
3387
Stefan Behrensb5d67f62012-03-27 14:21:27 -04003388 /*
3389 * work on commit root. The related disk blocks are static as
3390 * long as COW is applied. This means, it is save to rewrite
3391 * them to repair disk errors without any race conditions
3392 */
Arne Jansena2de7332011-03-08 14:14:00 +01003393 path->search_commit_root = 1;
3394 path->skip_locking = 1;
3395
Gui Hecheng063c54d2015-01-09 09:39:40 +08003396 ppath->search_commit_root = 1;
3397 ppath->skip_locking = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003398 /*
Arne Jansen7a262852011-06-10 12:39:23 +02003399 * trigger the readahead for extent tree csum tree and wait for
3400 * completion. During readahead, the scrub is officially paused
3401 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01003402 */
3403 logical = base + offset;
Wang Shilong3b080b22014-04-01 18:01:43 +08003404 physical_end = physical + nstripes * map->stripe_len;
Zhao Leiffe2d202015-01-20 15:11:44 +08003405 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003406 get_raid56_logic_offset(physical_end, num,
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003407 map, &logic_end, NULL);
Wang Shilong3b080b22014-04-01 18:01:43 +08003408 logic_end += base;
3409 } else {
3410 logic_end = logical + increment * nstripes;
3411 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003412 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003413 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08003414 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003415
Arne Jansen7a262852011-06-10 12:39:23 +02003416 /* FIXME it might be better to start readahead at commit root */
David Sterbae6c11f92016-03-24 18:00:53 +01003417 key.objectid = logical;
3418 key.type = BTRFS_EXTENT_ITEM_KEY;
3419 key.offset = (u64)0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003420 key_end.objectid = logic_end;
Josef Bacik3173a182013-03-07 14:22:04 -05003421 key_end.type = BTRFS_METADATA_ITEM_KEY;
3422 key_end.offset = (u64)-1;
David Sterbae6c11f92016-03-24 18:00:53 +01003423 reada1 = btrfs_reada_add(root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003424
David Sterbae6c11f92016-03-24 18:00:53 +01003425 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3426 key.type = BTRFS_EXTENT_CSUM_KEY;
3427 key.offset = logical;
Arne Jansen7a262852011-06-10 12:39:23 +02003428 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3429 key_end.type = BTRFS_EXTENT_CSUM_KEY;
Wang Shilong3b080b22014-04-01 18:01:43 +08003430 key_end.offset = logic_end;
David Sterbae6c11f92016-03-24 18:00:53 +01003431 reada2 = btrfs_reada_add(csum_root, &key, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01003432
Arne Jansen7a262852011-06-10 12:39:23 +02003433 if (!IS_ERR(reada1))
3434 btrfs_reada_wait(reada1);
3435 if (!IS_ERR(reada2))
3436 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01003437
Arne Jansena2de7332011-03-08 14:14:00 +01003438
3439 /*
3440 * collect all data csums for the stripe to avoid seeking during
3441 * the scrub. This might currently (crc32) end up to be about 1MB
3442 */
Arne Jansene7786c32011-05-28 20:58:38 +00003443 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003444
Arne Jansena2de7332011-03-08 14:14:00 +01003445 /*
3446 * now find all extents for each stripe and scrub them
3447 */
Arne Jansena2de7332011-03-08 14:14:00 +01003448 ret = 0;
Wang Shilong3b080b22014-04-01 18:01:43 +08003449 while (physical < physical_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01003450 /*
3451 * canceled?
3452 */
3453 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003454 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01003455 ret = -ECANCELED;
3456 goto out;
3457 }
3458 /*
3459 * check to see if we have to pause
3460 */
3461 if (atomic_read(&fs_info->scrub_pause_req)) {
3462 /* push queued extents */
David Sterba2073c4c2017-03-31 17:12:51 +02003463 sctx->flush_all_writes = true;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003464 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003465 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003466 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003467 mutex_unlock(&sctx->wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003468 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01003469 atomic_read(&sctx->bios_in_flight) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003470 sctx->flush_all_writes = false;
Wang Shilong3cb09292013-12-04 21:15:19 +08003471 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01003472 }
3473
Zhao Leif2f66a22015-07-21 12:22:29 +08003474 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
3475 ret = get_raid56_logic_offset(physical, num, map,
3476 &logical,
3477 &stripe_logical);
3478 logical += base;
3479 if (ret) {
Zhao Lei79553232015-08-18 17:54:30 +08003480 /* it is parity strip */
Zhao Leif2f66a22015-07-21 12:22:29 +08003481 stripe_logical += base;
Zhao Leia0dd59d2015-07-21 15:42:26 +08003482 stripe_end = stripe_logical + increment;
Zhao Leif2f66a22015-07-21 12:22:29 +08003483 ret = scrub_raid56_parity(sctx, map, scrub_dev,
3484 ppath, stripe_logical,
3485 stripe_end);
3486 if (ret)
3487 goto out;
3488 goto skip;
3489 }
3490 }
3491
Wang Shilong7c76edb2014-01-12 21:38:32 +08003492 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
3493 key.type = BTRFS_METADATA_ITEM_KEY;
3494 else
3495 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01003496 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00003497 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01003498
3499 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3500 if (ret < 0)
3501 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05003502
Arne Jansen8c510322011-06-03 10:09:26 +02003503 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08003504 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01003505 if (ret < 0)
3506 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02003507 if (ret > 0) {
3508 /* there's no smaller item, so stick with the
3509 * larger one */
3510 btrfs_release_path(path);
3511 ret = btrfs_search_slot(NULL, root, &key,
3512 path, 0, 0);
3513 if (ret < 0)
3514 goto out;
3515 }
Arne Jansena2de7332011-03-08 14:14:00 +01003516 }
3517
Liu Bo625f1c8d2013-04-27 02:56:57 +00003518 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003519 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05003520 u64 bytes;
3521
Arne Jansena2de7332011-03-08 14:14:00 +01003522 l = path->nodes[0];
3523 slot = path->slots[0];
3524 if (slot >= btrfs_header_nritems(l)) {
3525 ret = btrfs_next_leaf(root, path);
3526 if (ret == 0)
3527 continue;
3528 if (ret < 0)
3529 goto out;
3530
Liu Bo625f1c8d2013-04-27 02:56:57 +00003531 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01003532 break;
3533 }
3534 btrfs_item_key_to_cpu(l, &key, slot);
3535
Zhao Leid7cad232015-07-22 13:14:48 +08003536 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3537 key.type != BTRFS_METADATA_ITEM_KEY)
3538 goto next;
3539
Josef Bacik3173a182013-03-07 14:22:04 -05003540 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003541 bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05003542 else
3543 bytes = key.offset;
3544
3545 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01003546 goto next;
3547
Liu Bo625f1c8d2013-04-27 02:56:57 +00003548 if (key.objectid >= logical + map->stripe_len) {
3549 /* out of this device extent */
3550 if (key.objectid >= logic_end)
3551 stop_loop = 1;
3552 break;
3553 }
Arne Jansena2de7332011-03-08 14:14:00 +01003554
3555 extent = btrfs_item_ptr(l, slot,
3556 struct btrfs_extent_item);
3557 flags = btrfs_extent_flags(l, extent);
3558 generation = btrfs_extent_generation(l, extent);
3559
Zhao Leia323e812015-07-23 12:29:49 +08003560 if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) &&
3561 (key.objectid < logical ||
3562 key.objectid + bytes >
3563 logical + map->stripe_len)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003564 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003565 "scrub: tree block %llu spanning stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003566 key.objectid, logical);
Zhao Lei9799d2c32015-08-25 21:31:40 +08003567 spin_lock(&sctx->stat_lock);
3568 sctx->stat.uncorrectable_errors++;
3569 spin_unlock(&sctx->stat_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003570 goto next;
3571 }
3572
Liu Bo625f1c8d2013-04-27 02:56:57 +00003573again:
3574 extent_logical = key.objectid;
3575 extent_len = bytes;
3576
Arne Jansena2de7332011-03-08 14:14:00 +01003577 /*
3578 * trim extent to this stripe
3579 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00003580 if (extent_logical < logical) {
3581 extent_len -= logical - extent_logical;
3582 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003583 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003584 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01003585 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003586 extent_len = logical + map->stripe_len -
3587 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01003588 }
3589
Liu Bo625f1c8d2013-04-27 02:56:57 +00003590 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003591 extent_dev = scrub_dev;
3592 extent_mirror_num = mirror_num;
3593 if (is_dev_replace)
3594 scrub_remap_extent(fs_info, extent_logical,
3595 extent_len, &extent_physical,
3596 &extent_dev,
3597 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003598
Zhao Leife8cf652015-07-22 13:14:47 +08003599 ret = btrfs_lookup_csums_range(csum_root,
3600 extent_logical,
3601 extent_logical +
3602 extent_len - 1,
3603 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01003604 if (ret)
3605 goto out;
3606
Liu Bo625f1c8d2013-04-27 02:56:57 +00003607 ret = scrub_extent(sctx, extent_logical, extent_len,
3608 extent_physical, extent_dev, flags,
3609 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02003610 extent_logical - logical + physical);
Zhao Lei6fa96d72015-07-21 12:22:30 +08003611
3612 scrub_free_csums(sctx);
3613
Liu Bo625f1c8d2013-04-27 02:56:57 +00003614 if (ret)
3615 goto out;
3616
3617 if (extent_logical + extent_len <
3618 key.objectid + bytes) {
Zhao Leiffe2d202015-01-20 15:11:44 +08003619 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Wang Shilong3b080b22014-04-01 18:01:43 +08003620 /*
3621 * loop until we find next data stripe
3622 * or we have finished all stripes.
3623 */
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003624loop:
3625 physical += map->stripe_len;
3626 ret = get_raid56_logic_offset(physical,
3627 num, map, &logical,
3628 &stripe_logical);
3629 logical += base;
3630
3631 if (ret && physical < physical_end) {
3632 stripe_logical += base;
3633 stripe_end = stripe_logical +
Zhao Leia0dd59d2015-07-21 15:42:26 +08003634 increment;
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003635 ret = scrub_raid56_parity(sctx,
3636 map, scrub_dev, ppath,
3637 stripe_logical,
3638 stripe_end);
3639 if (ret)
3640 goto out;
3641 goto loop;
3642 }
Wang Shilong3b080b22014-04-01 18:01:43 +08003643 } else {
3644 physical += map->stripe_len;
3645 logical += increment;
3646 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00003647 if (logical < key.objectid + bytes) {
3648 cond_resched();
3649 goto again;
3650 }
3651
Wang Shilong3b080b22014-04-01 18:01:43 +08003652 if (physical >= physical_end) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00003653 stop_loop = 1;
3654 break;
3655 }
3656 }
Arne Jansena2de7332011-03-08 14:14:00 +01003657next:
3658 path->slots[0]++;
3659 }
Chris Mason71267332011-05-23 06:30:52 -04003660 btrfs_release_path(path);
Wang Shilong3b080b22014-04-01 18:01:43 +08003661skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003662 logical += increment;
3663 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003664 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003665 if (stop_loop)
3666 sctx->stat.last_physical = map->stripes[num].physical +
3667 length;
3668 else
3669 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003670 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00003671 if (stop_loop)
3672 break;
Arne Jansena2de7332011-03-08 14:14:00 +01003673 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003674out:
Arne Jansena2de7332011-03-08 14:14:00 +01003675 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003676 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003677 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003678 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003679 mutex_unlock(&sctx->wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01003680
Arne Jansene7786c32011-05-28 20:58:38 +00003681 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01003682 btrfs_free_path(path);
Miao Xie5a6ac9e2014-11-06 17:20:58 +08003683 btrfs_free_path(ppath);
Arne Jansena2de7332011-03-08 14:14:00 +01003684 return ret < 0 ? ret : 0;
3685}
3686
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003687static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003688 struct btrfs_device *scrub_dev,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003689 u64 chunk_offset, u64 length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003690 u64 dev_offset,
3691 struct btrfs_block_group_cache *cache,
3692 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003693{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003694 struct btrfs_fs_info *fs_info = sctx->fs_info;
3695 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01003696 struct map_lookup *map;
3697 struct extent_map *em;
3698 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003699 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01003700
3701 read_lock(&map_tree->map_tree.lock);
3702 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
3703 read_unlock(&map_tree->map_tree.lock);
3704
Filipe Manana020d5b72015-11-19 10:57:20 +00003705 if (!em) {
3706 /*
3707 * Might have been an unused block group deleted by the cleaner
3708 * kthread or relocation.
3709 */
3710 spin_lock(&cache->lock);
3711 if (!cache->removed)
3712 ret = -EINVAL;
3713 spin_unlock(&cache->lock);
3714
3715 return ret;
3716 }
Arne Jansena2de7332011-03-08 14:14:00 +01003717
Jeff Mahoney95617d62015-06-03 10:55:48 -04003718 map = em->map_lookup;
Arne Jansena2de7332011-03-08 14:14:00 +01003719 if (em->start != chunk_offset)
3720 goto out;
3721
3722 if (em->len < length)
3723 goto out;
3724
3725 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003726 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01003727 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003728 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003729 chunk_offset, length,
3730 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01003731 if (ret)
3732 goto out;
3733 }
3734 }
3735out:
3736 free_extent_map(em);
3737
3738 return ret;
3739}
3740
3741static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003742int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01003743 struct btrfs_device *scrub_dev, u64 start, u64 end,
3744 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01003745{
3746 struct btrfs_dev_extent *dev_extent = NULL;
3747 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003748 struct btrfs_fs_info *fs_info = sctx->fs_info;
3749 struct btrfs_root *root = fs_info->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01003750 u64 length;
Arne Jansena2de7332011-03-08 14:14:00 +01003751 u64 chunk_offset;
Zhaolei55e3a602015-08-05 16:43:30 +08003752 int ret = 0;
Zhaolei76a8efa2015-11-17 18:46:17 +08003753 int ro_set;
Arne Jansena2de7332011-03-08 14:14:00 +01003754 int slot;
3755 struct extent_buffer *l;
3756 struct btrfs_key key;
3757 struct btrfs_key found_key;
3758 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003759 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01003760
3761 path = btrfs_alloc_path();
3762 if (!path)
3763 return -ENOMEM;
3764
David Sterbae4058b52015-11-27 16:31:35 +01003765 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +01003766 path->search_commit_root = 1;
3767 path->skip_locking = 1;
3768
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003769 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01003770 key.offset = 0ull;
3771 key.type = BTRFS_DEV_EXTENT_KEY;
3772
Arne Jansena2de7332011-03-08 14:14:00 +01003773 while (1) {
3774 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3775 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003776 break;
3777 if (ret > 0) {
3778 if (path->slots[0] >=
3779 btrfs_header_nritems(path->nodes[0])) {
3780 ret = btrfs_next_leaf(root, path);
Zhaolei55e3a602015-08-05 16:43:30 +08003781 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02003782 break;
Zhaolei55e3a602015-08-05 16:43:30 +08003783 if (ret > 0) {
3784 ret = 0;
3785 break;
3786 }
3787 } else {
3788 ret = 0;
Arne Jansen8c510322011-06-03 10:09:26 +02003789 }
3790 }
Arne Jansena2de7332011-03-08 14:14:00 +01003791
3792 l = path->nodes[0];
3793 slot = path->slots[0];
3794
3795 btrfs_item_key_to_cpu(l, &found_key, slot);
3796
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003797 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01003798 break;
3799
David Sterba962a2982014-06-04 18:41:45 +02003800 if (found_key.type != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01003801 break;
3802
3803 if (found_key.offset >= end)
3804 break;
3805
3806 if (found_key.offset < key.offset)
3807 break;
3808
3809 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3810 length = btrfs_dev_extent_length(l, dev_extent);
3811
Qu Wenruoced96ed2014-06-19 10:42:51 +08003812 if (found_key.offset + length <= start)
3813 goto skip;
Arne Jansena2de7332011-03-08 14:14:00 +01003814
Arne Jansena2de7332011-03-08 14:14:00 +01003815 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
3816
3817 /*
3818 * get a reference on the corresponding block group to prevent
3819 * the chunk from going away while we scrub it
3820 */
3821 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
Qu Wenruoced96ed2014-06-19 10:42:51 +08003822
3823 /* some chunks are removed but not committed to disk yet,
3824 * continue scrubbing */
3825 if (!cache)
3826 goto skip;
3827
Zhaolei55e3a602015-08-05 16:43:30 +08003828 /*
3829 * we need call btrfs_inc_block_group_ro() with scrubs_paused,
3830 * to avoid deadlock caused by:
3831 * btrfs_inc_block_group_ro()
3832 * -> btrfs_wait_for_commit()
3833 * -> btrfs_commit_transaction()
3834 * -> btrfs_scrub_pause()
3835 */
3836 scrub_pause_on(fs_info);
Jeff Mahoney5e00f192017-02-15 16:28:29 -05003837 ret = btrfs_inc_block_group_ro(fs_info, cache);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003838 if (!ret && is_dev_replace) {
3839 /*
3840 * If we are doing a device replace wait for any tasks
3841 * that started dellaloc right before we set the block
3842 * group to RO mode, as they might have just allocated
3843 * an extent from it or decided they could do a nocow
3844 * write. And if any such tasks did that, wait for their
3845 * ordered extents to complete and then commit the
3846 * current transaction, so that we can later see the new
3847 * extent items in the extent tree - the ordered extents
3848 * create delayed data references (for cow writes) when
3849 * they complete, which will be run and insert the
3850 * corresponding extent items into the extent tree when
3851 * we commit the transaction they used when running
3852 * inode.c:btrfs_finish_ordered_io(). We later use
3853 * the commit root of the extent tree to find extents
3854 * to copy from the srcdev into the tgtdev, and we don't
3855 * want to miss any new extents.
3856 */
3857 btrfs_wait_block_group_reservations(cache);
3858 btrfs_wait_nocow_writers(cache);
Chris Mason6374e57a2017-06-23 09:48:21 -07003859 ret = btrfs_wait_ordered_roots(fs_info, U64_MAX,
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003860 cache->key.objectid,
3861 cache->key.offset);
3862 if (ret > 0) {
3863 struct btrfs_trans_handle *trans;
3864
3865 trans = btrfs_join_transaction(root);
3866 if (IS_ERR(trans))
3867 ret = PTR_ERR(trans);
3868 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003869 ret = btrfs_commit_transaction(trans);
Filipe Mananaf0e9b7d2016-05-14 09:12:53 +01003870 if (ret) {
3871 scrub_pause_off(fs_info);
3872 btrfs_put_block_group(cache);
3873 break;
3874 }
3875 }
3876 }
Zhaolei55e3a602015-08-05 16:43:30 +08003877 scrub_pause_off(fs_info);
Zhaolei76a8efa2015-11-17 18:46:17 +08003878
3879 if (ret == 0) {
3880 ro_set = 1;
3881 } else if (ret == -ENOSPC) {
3882 /*
3883 * btrfs_inc_block_group_ro return -ENOSPC when it
3884 * failed in creating new chunk for metadata.
3885 * It is not a problem for scrub/replace, because
3886 * metadata are always cowed, and our scrub paused
3887 * commit_transactions.
3888 */
3889 ro_set = 0;
3890 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003891 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02003892 "failed setting block group ro: %d", ret);
Zhaolei55e3a602015-08-05 16:43:30 +08003893 btrfs_put_block_group(cache);
3894 break;
3895 }
3896
Filipe Manana81e87a72016-05-14 16:32:35 +01003897 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003898 dev_replace->cursor_right = found_key.offset + length;
3899 dev_replace->cursor_left = found_key.offset;
3900 dev_replace->item_needs_writeback = 1;
Filipe Manana81e87a72016-05-14 16:32:35 +01003901 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
Zhao Lei8c204c92015-08-19 15:02:40 +08003902 ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
Filipe Manana020d5b72015-11-19 10:57:20 +00003903 found_key.offset, cache, is_dev_replace);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003904
3905 /*
3906 * flush, submit all pending read and write bios, afterwards
3907 * wait for them.
3908 * Note that in the dev replace case, a read request causes
3909 * write requests that are submitted in the read completion
3910 * worker. Therefore in the current situation, it is required
3911 * that all write requests are flushed, so that all read and
3912 * write requests are really completed when bios_in_flight
3913 * changes to 0.
3914 */
David Sterba2073c4c2017-03-31 17:12:51 +02003915 sctx->flush_all_writes = true;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003916 scrub_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003917 mutex_lock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003918 scrub_wr_submit(sctx);
David Sterba3fb99302017-05-16 19:10:32 +02003919 mutex_unlock(&sctx->wr_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003920
3921 wait_event(sctx->list_wait,
3922 atomic_read(&sctx->bios_in_flight) == 0);
Zhaoleib708ce92015-08-05 16:43:29 +08003923
3924 scrub_pause_on(fs_info);
Wang Shilong12cf9372014-02-19 19:24:17 +08003925
3926 /*
3927 * must be called before we decrease @scrub_paused.
3928 * make sure we don't block transaction commit while
3929 * we are waiting pending workers finished.
3930 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01003931 wait_event(sctx->list_wait,
3932 atomic_read(&sctx->workers_pending) == 0);
David Sterba2073c4c2017-03-31 17:12:51 +02003933 sctx->flush_all_writes = false;
Wang Shilong12cf9372014-02-19 19:24:17 +08003934
Zhaoleib708ce92015-08-05 16:43:29 +08003935 scrub_pause_off(fs_info);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003936
Filipe Manana1a1a8b72016-05-14 19:44:40 +01003937 btrfs_dev_replace_lock(&fs_info->dev_replace, 1);
3938 dev_replace->cursor_left = dev_replace->cursor_right;
3939 dev_replace->item_needs_writeback = 1;
3940 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
3941
Zhaolei76a8efa2015-11-17 18:46:17 +08003942 if (ro_set)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003943 btrfs_dec_block_group_ro(cache);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003944
Filipe Manana758f2df2015-11-19 11:45:48 +00003945 /*
3946 * We might have prevented the cleaner kthread from deleting
3947 * this block group if it was already unused because we raced
3948 * and set it to RO mode first. So add it back to the unused
3949 * list, otherwise it might not ever be deleted unless a manual
3950 * balance is triggered or it becomes used and unused again.
3951 */
3952 spin_lock(&cache->lock);
3953 if (!cache->removed && !cache->ro && cache->reserved == 0 &&
3954 btrfs_block_group_used(&cache->item) == 0) {
3955 spin_unlock(&cache->lock);
3956 spin_lock(&fs_info->unused_bgs_lock);
3957 if (list_empty(&cache->bg_list)) {
3958 btrfs_get_block_group(cache);
3959 list_add_tail(&cache->bg_list,
3960 &fs_info->unused_bgs);
3961 }
3962 spin_unlock(&fs_info->unused_bgs_lock);
3963 } else {
3964 spin_unlock(&cache->lock);
3965 }
3966
Arne Jansena2de7332011-03-08 14:14:00 +01003967 btrfs_put_block_group(cache);
3968 if (ret)
3969 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00003970 if (is_dev_replace &&
3971 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003972 ret = -EIO;
3973 break;
3974 }
3975 if (sctx->stat.malloc_errors > 0) {
3976 ret = -ENOMEM;
3977 break;
3978 }
Qu Wenruoced96ed2014-06-19 10:42:51 +08003979skip:
Arne Jansena2de7332011-03-08 14:14:00 +01003980 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04003981 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01003982 }
3983
Arne Jansena2de7332011-03-08 14:14:00 +01003984 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02003985
Zhaolei55e3a602015-08-05 16:43:30 +08003986 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01003987}
3988
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01003989static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3990 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01003991{
3992 int i;
3993 u64 bytenr;
3994 u64 gen;
3995 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003996 struct btrfs_fs_info *fs_info = sctx->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01003997
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003998 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003999 return -EIO;
4000
Miao Xie5f546062014-07-24 11:37:09 +08004001 /* Seed devices of a new filesystem has their own generation. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004002 if (scrub_dev->fs_devices != fs_info->fs_devices)
Miao Xie5f546062014-07-24 11:37:09 +08004003 gen = scrub_dev->generation;
4004 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004005 gen = fs_info->last_trans_committed;
Arne Jansena2de7332011-03-08 14:14:00 +01004006
4007 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
4008 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08004009 if (bytenr + BTRFS_SUPER_INFO_SIZE >
4010 scrub_dev->commit_total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01004011 break;
4012
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004013 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01004014 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004015 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01004016 if (ret)
4017 return ret;
4018 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004019 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004020
4021 return 0;
4022}
4023
4024/*
4025 * get a reference count on fs_info->scrub_workers. start worker if necessary
4026 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01004027static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
4028 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004029{
David Sterba6f011052015-02-16 18:34:01 +01004030 unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND;
Qu Wenruo0339ef22014-02-28 10:46:17 +08004031 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01004032
Arne Jansen632dd772011-06-10 12:07:07 +02004033 if (fs_info->scrub_workers_refcnt == 0) {
David Sterbaaf1cbe02017-03-31 18:42:57 +02004034 fs_info->scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub",
4035 flags, is_dev_replace ? 1 : max_active, 4);
Zhao Leie82afc52015-06-12 20:36:58 +08004036 if (!fs_info->scrub_workers)
4037 goto fail_scrub_workers;
4038
Qu Wenruo0339ef22014-02-28 10:46:17 +08004039 fs_info->scrub_wr_completion_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004040 btrfs_alloc_workqueue(fs_info, "scrubwrc", flags,
Qu Wenruo0339ef22014-02-28 10:46:17 +08004041 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004042 if (!fs_info->scrub_wr_completion_workers)
4043 goto fail_scrub_wr_completion_workers;
4044
Qu Wenruo0339ef22014-02-28 10:46:17 +08004045 fs_info->scrub_nocow_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004046 btrfs_alloc_workqueue(fs_info, "scrubnc", flags, 1, 0);
Zhao Leie82afc52015-06-12 20:36:58 +08004047 if (!fs_info->scrub_nocow_workers)
4048 goto fail_scrub_nocow_workers;
Zhao Lei20b2e302015-06-04 20:09:15 +08004049 fs_info->scrub_parity_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04004050 btrfs_alloc_workqueue(fs_info, "scrubparity", flags,
Zhao Lei20b2e302015-06-04 20:09:15 +08004051 max_active, 2);
Zhao Leie82afc52015-06-12 20:36:58 +08004052 if (!fs_info->scrub_parity_workers)
4053 goto fail_scrub_parity_workers;
Arne Jansen632dd772011-06-10 12:07:07 +02004054 }
Arne Jansena2de7332011-03-08 14:14:00 +01004055 ++fs_info->scrub_workers_refcnt;
Zhao Leie82afc52015-06-12 20:36:58 +08004056 return 0;
4057
4058fail_scrub_parity_workers:
4059 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
4060fail_scrub_nocow_workers:
4061 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4062fail_scrub_wr_completion_workers:
4063 btrfs_destroy_workqueue(fs_info->scrub_workers);
4064fail_scrub_workers:
4065 return -ENOMEM;
Arne Jansena2de7332011-03-08 14:14:00 +01004066}
4067
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004068static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004069{
Stefan Behrensff023aa2012-11-06 11:43:11 +01004070 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08004071 btrfs_destroy_workqueue(fs_info->scrub_workers);
4072 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
4073 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Zhao Lei20b2e302015-06-04 20:09:15 +08004074 btrfs_destroy_workqueue(fs_info->scrub_parity_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004075 }
Arne Jansena2de7332011-03-08 14:14:00 +01004076 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004077}
4078
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004079int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
4080 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01004081 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01004082{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004083 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004084 int ret;
4085 struct btrfs_device *dev;
Miao Xie5d68da32014-07-24 11:37:07 +08004086 struct rcu_string *name;
Arne Jansena2de7332011-03-08 14:14:00 +01004087
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004088 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01004089 return -EINVAL;
4090
Jeff Mahoneyda170662016-06-15 09:22:56 -04004091 if (fs_info->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004092 /*
4093 * in this case scrub is unable to calculate the checksum
4094 * the way scrub is implemented. Do not handle this
4095 * situation at all because it won't ever happen.
4096 */
Frank Holtonefe120a2013-12-20 11:37:06 -05004097 btrfs_err(fs_info,
4098 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004099 fs_info->nodesize,
4100 BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004101 return -EINVAL;
4102 }
4103
Jeff Mahoneyda170662016-06-15 09:22:56 -04004104 if (fs_info->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04004105 /* not supported for data w/o checksums */
Chandan Rajendra751bebb2016-07-04 10:04:39 +05304106 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004107 "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004108 fs_info->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01004109 return -EINVAL;
4110 }
4111
Jeff Mahoneyda170662016-06-15 09:22:56 -04004112 if (fs_info->nodesize >
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004113 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
Jeff Mahoneyda170662016-06-15 09:22:56 -04004114 fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004115 /*
4116 * would exhaust the array bounds of pagev member in
4117 * struct scrub_block
4118 */
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004119 btrfs_err(fs_info,
4120 "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Jeff Mahoneyda170662016-06-15 09:22:56 -04004121 fs_info->nodesize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004122 SCRUB_MAX_PAGES_PER_BLOCK,
Jeff Mahoneyda170662016-06-15 09:22:56 -04004123 fs_info->sectorsize,
Stefan Behrens7a9e9982012-11-02 14:58:04 +01004124 SCRUB_MAX_PAGES_PER_BLOCK);
4125 return -EINVAL;
4126 }
4127
Arne Jansena2de7332011-03-08 14:14:00 +01004128
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004129 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4130 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jaine6e674b2017-12-04 12:54:54 +08004131 if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
4132 !is_dev_replace)) {
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 -ENODEV;
4135 }
Arne Jansena2de7332011-03-08 14:14:00 +01004136
Anand Jainebbede42017-12-04 12:54:52 +08004137 if (!is_dev_replace && !readonly &&
4138 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
Miao Xie5d68da32014-07-24 11:37:07 +08004139 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4140 rcu_read_lock();
4141 name = rcu_dereference(dev->name);
4142 btrfs_err(fs_info, "scrub: device %s is not writable",
4143 name->str);
4144 rcu_read_unlock();
4145 return -EROFS;
4146 }
4147
Wang Shilong3b7a0162013-10-12 02:11:12 +08004148 mutex_lock(&fs_info->scrub_lock);
Anand Jaine12c9622017-12-04 12:54:53 +08004149 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004150 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004151 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004152 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004153 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01004154 }
4155
Liu Bo73beece2015-07-17 16:49:19 +08004156 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004157 if (dev->scrub_ctx ||
Stefan Behrens8dabb742012-11-06 13:15:27 +01004158 (!is_dev_replace &&
4159 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
Liu Bo73beece2015-07-17 16:49:19 +08004160 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004161 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004162 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004163 return -EINPROGRESS;
4164 }
Liu Bo73beece2015-07-17 16:49:19 +08004165 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Wang Shilong3b7a0162013-10-12 02:11:12 +08004166
4167 ret = scrub_workers_get(fs_info, is_dev_replace);
4168 if (ret) {
4169 mutex_unlock(&fs_info->scrub_lock);
4170 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4171 return ret;
4172 }
4173
Stefan Behrens63a212a2012-11-05 18:29:28 +01004174 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004175 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01004176 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004177 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4178 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004179 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004180 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004181 sctx->readonly = readonly;
Anand Jaincadbc0a2018-01-03 16:08:30 +08004182 dev->scrub_ctx = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08004183 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004184
Wang Shilong3cb09292013-12-04 21:15:19 +08004185 /*
4186 * checking @scrub_pause_req here, we can avoid
4187 * race between committing transaction and scrubbing.
4188 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08004189 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004190 atomic_inc(&fs_info->scrubs_running);
4191 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004192
Stefan Behrensff023aa2012-11-06 11:43:11 +01004193 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08004194 /*
4195 * by holding device list mutex, we can
4196 * kick off writing super in log tree sync.
4197 */
Wang Shilong3cb09292013-12-04 21:15:19 +08004198 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004199 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08004200 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004201 }
Arne Jansena2de7332011-03-08 14:14:00 +01004202
4203 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004204 ret = scrub_enumerate_chunks(sctx, dev, start, end,
4205 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01004206
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004207 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01004208 atomic_dec(&fs_info->scrubs_running);
4209 wake_up(&fs_info->scrub_pause_wait);
4210
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01004211 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02004212
Arne Jansena2de7332011-03-08 14:14:00 +01004213 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004214 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01004215
4216 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004217 dev->scrub_ctx = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08004218 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01004219 mutex_unlock(&fs_info->scrub_lock);
4220
Filipe Mananaf55985f2015-02-09 21:14:24 +00004221 scrub_put_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01004222
4223 return ret;
4224}
4225
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004226void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004227{
Arne Jansena2de7332011-03-08 14:14:00 +01004228 mutex_lock(&fs_info->scrub_lock);
4229 atomic_inc(&fs_info->scrub_pause_req);
4230 while (atomic_read(&fs_info->scrubs_paused) !=
4231 atomic_read(&fs_info->scrubs_running)) {
4232 mutex_unlock(&fs_info->scrub_lock);
4233 wait_event(fs_info->scrub_pause_wait,
4234 atomic_read(&fs_info->scrubs_paused) ==
4235 atomic_read(&fs_info->scrubs_running));
4236 mutex_lock(&fs_info->scrub_lock);
4237 }
4238 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01004239}
4240
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004241void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004242{
Arne Jansena2de7332011-03-08 14:14:00 +01004243 atomic_dec(&fs_info->scrub_pause_req);
4244 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01004245}
4246
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004247int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01004248{
Arne Jansena2de7332011-03-08 14:14:00 +01004249 mutex_lock(&fs_info->scrub_lock);
4250 if (!atomic_read(&fs_info->scrubs_running)) {
4251 mutex_unlock(&fs_info->scrub_lock);
4252 return -ENOTCONN;
4253 }
4254
4255 atomic_inc(&fs_info->scrub_cancel_req);
4256 while (atomic_read(&fs_info->scrubs_running)) {
4257 mutex_unlock(&fs_info->scrub_lock);
4258 wait_event(fs_info->scrub_pause_wait,
4259 atomic_read(&fs_info->scrubs_running) == 0);
4260 mutex_lock(&fs_info->scrub_lock);
4261 }
4262 atomic_dec(&fs_info->scrub_cancel_req);
4263 mutex_unlock(&fs_info->scrub_lock);
4264
4265 return 0;
4266}
4267
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004268int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
4269 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004270{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004271 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01004272
4273 mutex_lock(&fs_info->scrub_lock);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004274 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004275 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004276 mutex_unlock(&fs_info->scrub_lock);
4277 return -ENOTCONN;
4278 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004279 atomic_inc(&sctx->cancel_req);
Anand Jaincadbc0a2018-01-03 16:08:30 +08004280 while (dev->scrub_ctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01004281 mutex_unlock(&fs_info->scrub_lock);
4282 wait_event(fs_info->scrub_pause_wait,
Anand Jaincadbc0a2018-01-03 16:08:30 +08004283 dev->scrub_ctx == NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004284 mutex_lock(&fs_info->scrub_lock);
4285 }
4286 mutex_unlock(&fs_info->scrub_lock);
4287
4288 return 0;
4289}
Stefan Behrens1623ede2012-03-27 14:21:26 -04004290
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004291int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
Arne Jansena2de7332011-03-08 14:14:00 +01004292 struct btrfs_scrub_progress *progress)
4293{
4294 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004295 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01004296
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004297 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4298 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01004299 if (dev)
Anand Jaincadbc0a2018-01-03 16:08:30 +08004300 sctx = dev->scrub_ctx;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004301 if (sctx)
4302 memcpy(progress, &sctx->stat, sizeof(*progress));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004303 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01004304
Stefan Behrensd9d181c2012-11-02 09:58:09 +01004305 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01004306}
Stefan Behrensff023aa2012-11-06 11:43:11 +01004307
4308static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
4309 u64 extent_logical, u64 extent_len,
4310 u64 *extent_physical,
4311 struct btrfs_device **extent_dev,
4312 int *extent_mirror_num)
4313{
4314 u64 mapped_length;
4315 struct btrfs_bio *bbio = NULL;
4316 int ret;
4317
4318 mapped_length = extent_len;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02004319 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +01004320 &mapped_length, &bbio, 0);
4321 if (ret || !bbio || mapped_length < extent_len ||
4322 !bbio->stripes[0].dev->bdev) {
Zhao Lei6e9606d2015-01-20 15:11:34 +08004323 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004324 return;
4325 }
4326
4327 *extent_physical = bbio->stripes[0].physical;
4328 *extent_mirror_num = bbio->mirror_num;
4329 *extent_dev = bbio->stripes[0].dev;
Zhao Lei6e9606d2015-01-20 15:11:34 +08004330 btrfs_put_bbio(bbio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004331}
4332
Stefan Behrensff023aa2012-11-06 11:43:11 +01004333static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
4334 int mirror_num, u64 physical_for_dev_replace)
4335{
4336 struct scrub_copy_nocow_ctx *nocow_ctx;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004337 struct btrfs_fs_info *fs_info = sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004338
4339 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
4340 if (!nocow_ctx) {
4341 spin_lock(&sctx->stat_lock);
4342 sctx->stat.malloc_errors++;
4343 spin_unlock(&sctx->stat_lock);
4344 return -ENOMEM;
4345 }
4346
4347 scrub_pending_trans_workers_inc(sctx);
4348
4349 nocow_ctx->sctx = sctx;
4350 nocow_ctx->logical = logical;
4351 nocow_ctx->len = len;
4352 nocow_ctx->mirror_num = mirror_num;
4353 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Liu Bo9e0af232014-08-15 23:36:53 +08004354 btrfs_init_work(&nocow_ctx->work, btrfs_scrubnc_helper,
4355 copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04004356 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08004357 btrfs_queue_work(fs_info->scrub_nocow_workers,
4358 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004359
4360 return 0;
4361}
4362
Josef Bacik652f25a2013-09-12 16:58:28 -04004363static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
4364{
4365 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
4366 struct scrub_nocow_inode *nocow_inode;
4367
4368 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
4369 if (!nocow_inode)
4370 return -ENOMEM;
4371 nocow_inode->inum = inum;
4372 nocow_inode->offset = offset;
4373 nocow_inode->root = root;
4374 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
4375 return 0;
4376}
4377
4378#define COPY_COMPLETE 1
4379
Stefan Behrensff023aa2012-11-06 11:43:11 +01004380static void copy_nocow_pages_worker(struct btrfs_work *work)
4381{
4382 struct scrub_copy_nocow_ctx *nocow_ctx =
4383 container_of(work, struct scrub_copy_nocow_ctx, work);
4384 struct scrub_ctx *sctx = nocow_ctx->sctx;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004385 struct btrfs_fs_info *fs_info = sctx->fs_info;
4386 struct btrfs_root *root = fs_info->extent_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004387 u64 logical = nocow_ctx->logical;
4388 u64 len = nocow_ctx->len;
4389 int mirror_num = nocow_ctx->mirror_num;
4390 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4391 int ret;
4392 struct btrfs_trans_handle *trans = NULL;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004393 struct btrfs_path *path;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004394 int not_written = 0;
4395
Stefan Behrensff023aa2012-11-06 11:43:11 +01004396 path = btrfs_alloc_path();
4397 if (!path) {
4398 spin_lock(&sctx->stat_lock);
4399 sctx->stat.malloc_errors++;
4400 spin_unlock(&sctx->stat_lock);
4401 not_written = 1;
4402 goto out;
4403 }
4404
4405 trans = btrfs_join_transaction(root);
4406 if (IS_ERR(trans)) {
4407 not_written = 1;
4408 goto out;
4409 }
4410
4411 ret = iterate_inodes_from_logical(logical, fs_info, path,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04004412 record_inode_for_nocow, nocow_ctx, false);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004413 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004414 btrfs_warn(fs_info,
4415 "iterate_inodes_from_logical() failed: log %llu, phys %llu, len %llu, mir %u, ret %d",
4416 logical, physical_for_dev_replace, len, mirror_num,
4417 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004418 not_written = 1;
4419 goto out;
4420 }
4421
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004422 btrfs_end_transaction(trans);
Josef Bacik652f25a2013-09-12 16:58:28 -04004423 trans = NULL;
4424 while (!list_empty(&nocow_ctx->inodes)) {
4425 struct scrub_nocow_inode *entry;
4426 entry = list_first_entry(&nocow_ctx->inodes,
4427 struct scrub_nocow_inode,
4428 list);
4429 list_del_init(&entry->list);
4430 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
4431 entry->root, nocow_ctx);
4432 kfree(entry);
4433 if (ret == COPY_COMPLETE) {
4434 ret = 0;
4435 break;
4436 } else if (ret) {
4437 break;
4438 }
4439 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004440out:
Josef Bacik652f25a2013-09-12 16:58:28 -04004441 while (!list_empty(&nocow_ctx->inodes)) {
4442 struct scrub_nocow_inode *entry;
4443 entry = list_first_entry(&nocow_ctx->inodes,
4444 struct scrub_nocow_inode,
4445 list);
4446 list_del_init(&entry->list);
4447 kfree(entry);
4448 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004449 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004450 btrfs_end_transaction(trans);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004451 if (not_written)
4452 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
4453 num_uncorrectable_read_errors);
4454
4455 btrfs_free_path(path);
4456 kfree(nocow_ctx);
4457
4458 scrub_pending_trans_workers_dec(sctx);
4459}
4460
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004461static int check_extent_to_block(struct btrfs_inode *inode, u64 start, u64 len,
Gui Hecheng32159242014-11-10 15:36:08 +08004462 u64 logical)
4463{
4464 struct extent_state *cached_state = NULL;
4465 struct btrfs_ordered_extent *ordered;
4466 struct extent_io_tree *io_tree;
4467 struct extent_map *em;
4468 u64 lockstart = start, lockend = start + len - 1;
4469 int ret = 0;
4470
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004471 io_tree = &inode->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004472
David Sterbaff13db42015-12-03 14:30:40 +01004473 lock_extent_bits(io_tree, lockstart, lockend, &cached_state);
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004474 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
Gui Hecheng32159242014-11-10 15:36:08 +08004475 if (ordered) {
4476 btrfs_put_ordered_extent(ordered);
4477 ret = 1;
4478 goto out_unlock;
4479 }
4480
4481 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
4482 if (IS_ERR(em)) {
4483 ret = PTR_ERR(em);
4484 goto out_unlock;
4485 }
4486
4487 /*
4488 * This extent does not actually cover the logical extent anymore,
4489 * move on to the next inode.
4490 */
4491 if (em->block_start > logical ||
Liu Boed5d5f32018-02-27 18:10:58 -07004492 em->block_start + em->block_len < logical + len ||
4493 test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Gui Hecheng32159242014-11-10 15:36:08 +08004494 free_extent_map(em);
4495 ret = 1;
4496 goto out_unlock;
4497 }
4498 free_extent_map(em);
4499
4500out_unlock:
David Sterbae43bbe52017-12-12 21:43:52 +01004501 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state);
Gui Hecheng32159242014-11-10 15:36:08 +08004502 return ret;
4503}
4504
Josef Bacik652f25a2013-09-12 16:58:28 -04004505static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
4506 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01004507{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004508 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004509 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08004510 struct inode *inode;
4511 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004512 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04004513 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004514 u64 physical_for_dev_replace;
Gui Hecheng32159242014-11-10 15:36:08 +08004515 u64 nocow_ctx_logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004516 u64 len = nocow_ctx->len;
Miao Xie826aa0a2013-06-27 18:50:59 +08004517 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00004518 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04004519 int ret = 0;
4520 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004521
4522 key.objectid = root;
4523 key.type = BTRFS_ROOT_ITEM_KEY;
4524 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00004525
4526 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
4527
Stefan Behrensff023aa2012-11-06 11:43:11 +01004528 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00004529 if (IS_ERR(local_root)) {
4530 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004531 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00004532 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004533
4534 key.type = BTRFS_INODE_ITEM_KEY;
4535 key.objectid = inum;
4536 key.offset = 0;
4537 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00004538 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004539 if (IS_ERR(inode))
4540 return PTR_ERR(inode);
4541
Miao Xieedd14002013-06-27 18:51:00 +08004542 /* Avoid truncate/dio/punch hole.. */
Al Viro59551022016-01-22 15:40:57 -05004543 inode_lock(inode);
Miao Xieedd14002013-06-27 18:51:00 +08004544 inode_dio_wait(inode);
4545
Stefan Behrensff023aa2012-11-06 11:43:11 +01004546 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04004547 io_tree = &BTRFS_I(inode)->io_tree;
Gui Hecheng32159242014-11-10 15:36:08 +08004548 nocow_ctx_logical = nocow_ctx->logical;
Josef Bacik652f25a2013-09-12 16:58:28 -04004549
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004550 ret = check_extent_to_block(BTRFS_I(inode), offset, len,
4551 nocow_ctx_logical);
Gui Hecheng32159242014-11-10 15:36:08 +08004552 if (ret) {
4553 ret = ret > 0 ? 0 : ret;
4554 goto out;
Josef Bacik652f25a2013-09-12 16:58:28 -04004555 }
4556
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004557 while (len >= PAGE_SIZE) {
4558 index = offset >> PAGE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08004559again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01004560 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4561 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004562 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004563 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08004564 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004565 }
4566
4567 if (PageUptodate(page)) {
4568 if (PageDirty(page))
4569 goto next_page;
4570 } else {
4571 ClearPageError(page);
Gui Hecheng32159242014-11-10 15:36:08 +08004572 err = extent_read_full_page(io_tree, page,
Josef Bacik652f25a2013-09-12 16:58:28 -04004573 btrfs_get_extent,
4574 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08004575 if (err) {
4576 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004577 goto next_page;
4578 }
Miao Xieedd14002013-06-27 18:51:00 +08004579
Miao Xie26b258912013-06-27 18:50:58 +08004580 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004581 /*
4582 * If the page has been remove from the page cache,
4583 * the data on it is meaningless, because it may be
4584 * old one, the new data may be written into the new
4585 * page in the page cache.
4586 */
4587 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04004588 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004589 put_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08004590 goto again;
4591 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004592 if (!PageUptodate(page)) {
4593 ret = -EIO;
4594 goto next_page;
4595 }
4596 }
Gui Hecheng32159242014-11-10 15:36:08 +08004597
Nikolay Borisov1c8c9c52017-02-20 13:51:05 +02004598 ret = check_extent_to_block(BTRFS_I(inode), offset, len,
Gui Hecheng32159242014-11-10 15:36:08 +08004599 nocow_ctx_logical);
4600 if (ret) {
4601 ret = ret > 0 ? 0 : ret;
4602 goto next_page;
4603 }
4604
Miao Xie826aa0a2013-06-27 18:50:59 +08004605 err = write_page_nocow(nocow_ctx->sctx,
4606 physical_for_dev_replace, page);
4607 if (err)
4608 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004609next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08004610 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004611 put_page(page);
Miao Xie826aa0a2013-06-27 18:50:59 +08004612
4613 if (ret)
4614 break;
4615
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004616 offset += PAGE_SIZE;
4617 physical_for_dev_replace += PAGE_SIZE;
4618 nocow_ctx_logical += PAGE_SIZE;
4619 len -= PAGE_SIZE;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004620 }
Josef Bacik652f25a2013-09-12 16:58:28 -04004621 ret = COPY_COMPLETE;
Miao Xie826aa0a2013-06-27 18:50:59 +08004622out:
Al Viro59551022016-01-22 15:40:57 -05004623 inode_unlock(inode);
Miao Xie826aa0a2013-06-27 18:50:59 +08004624 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01004625 return ret;
4626}
4627
4628static int write_page_nocow(struct scrub_ctx *sctx,
4629 u64 physical_for_dev_replace, struct page *page)
4630{
4631 struct bio *bio;
4632 struct btrfs_device *dev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004633
David Sterba3fb99302017-05-16 19:10:32 +02004634 dev = sctx->wr_tgtdev;
Stefan Behrensff023aa2012-11-06 11:43:11 +01004635 if (!dev)
4636 return -EIO;
4637 if (!dev->bdev) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -04004638 btrfs_warn_rl(dev->fs_info,
David Sterba94647322015-10-08 11:01:36 +02004639 "scrub write_page_nocow(bdev == NULL) is unexpected");
Stefan Behrensff023aa2012-11-06 11:43:11 +01004640 return -EIO;
4641 }
David Sterbac5e4c3d2017-06-12 17:29:41 +02004642 bio = btrfs_io_bio_alloc(1);
Kent Overstreet4f024f32013-10-11 15:44:27 -07004643 bio->bi_iter.bi_size = 0;
4644 bio->bi_iter.bi_sector = physical_for_dev_replace >> 9;
Christoph Hellwig74d46992017-08-23 19:10:32 +02004645 bio_set_dev(bio, dev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06004646 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
Anand Jain7ef2d6a72018-01-05 10:47:07 +08004647 /* bio_add_page won't fail on a freshly allocated bio */
4648 bio_add_page(bio, page, PAGE_SIZE, 0);
4649
4650 if (btrfsic_submit_bio_wait(bio)) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01004651 bio_put(bio);
4652 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
4653 return -EIO;
4654 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01004655
Stefan Behrensff023aa2012-11-06 11:43:11 +01004656 bio_put(bio);
4657 return 0;
4658}